Commit f8aac56d authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto

git push origin masterMerge branch 'take-cheeze-instance_eval'

parents d5dd3e94 abf52c77
......@@ -173,10 +173,36 @@ f_eval(mrb_state *mrb, mrb_value self)
return ret;
}
mrb_value mrb_obj_instance_eval(mrb_state *mrb, mrb_value self);
static mrb_value
f_instance_eval(mrb_state *mrb, mrb_value self)
{
mrb_value b;
mrb_int argc; mrb_value *argv;
mrb_get_args(mrb, "*&", &argv, &argc, &b);
if (mrb_nil_p(b)) {
char *s;
mrb_int len;
char *file = NULL;
mrb_int line = 1;
mrb_get_args(mrb, "s|zi", &s, &len, &file, &line);
return mrb_run(mrb, create_proc_from_string(mrb, s, len, mrb_nil_value(), file, line), self);
} else {
mrb_get_args(mrb, "&", &b);
return mrb_obj_instance_eval(mrb, self);
}
}
void
mrb_mruby_eval_gem_init(mrb_state* mrb)
{
mrb_define_module_function(mrb, mrb->kernel_module, "eval", f_eval, MRB_ARGS_ARG(1, 3));
mrb_define_method(mrb, mrb->kernel_module, "instance_eval", f_instance_eval, MRB_ARGS_ARG(1, 2));
}
void
......
......@@ -50,3 +50,13 @@ assert 'eval syntax error' do
eval 'p "test'
end
end
assert('String instance_eval') do
obj = Object.new
obj.instance_variable_set :@test, 'test'
assert_raise(ArgumentError) { obj.instance_eval(0) { } }
assert_raise(ArgumentError) { obj.instance_eval('0', 'test', 0, 'test') }
assert_equal(['test.rb', 10]) { obj.instance_eval('[__FILE__, __LINE__]', 'test.rb', 10)}
assert_equal('test') { obj.instance_eval('@test') }
assert_equal('test') { obj.instance_eval { @test } }
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment