Merge pull request #2519 from SatoshiOdawara/fix_context_of_eval

fixed evaluation context of eval(string) and instance_eval(string)
This commit is contained in:
Yukihiro "Matz" Matsumoto
2014-08-08 17:13:27 +09:00
2 changed files with 16 additions and 0 deletions
+3
View File
@@ -132,6 +132,9 @@ create_proc_from_string(mrb_state *mrb, char *s, int len, mrb_value binding, cha
}
proc = mrb_generate_code(mrb, p);
if (mrb->c->ci[-1].proc->target_class) {
proc->target_class = mrb->c->ci[-1].proc->target_class;
}
e = (struct REnv*)mrb_obj_alloc(mrb, MRB_TT_ENV, (struct RClass*)mrb->c->ci[-1].proc->env);
e->mid = mrb->c->ci[-1].mid;
e->cioff = mrb->c->ci - mrb->c->cibase - 1;
+13
View File
@@ -60,3 +60,16 @@ assert('String instance_eval') do
assert_equal('test') { obj.instance_eval('@test') }
assert_equal('test') { obj.instance_eval { @test } }
end
assert('Kernel.#eval(string) context') do
class TestEvalConstScope
EVAL_CONST_CLASS = 'class'
def const_string
eval 'EVAL_CONST_CLASS'
end
end
obj = TestEvalConstScope.new
assert_raise(NameError) { eval 'EVAL_CONST_CLASS' }
assert_equal('class') { obj.const_string }
end