Commit 7ea82fed authored by take_cheeze's avatar take_cheeze

Implement Kernel.local_variables for testing dumped local variables information.

parent 4c7f9897
......@@ -122,6 +122,29 @@ mrb_kernel_proc(mrb_state *mrb, mrb_value self)
return blk;
}
static mrb_value
mrb_local_variables(mrb_state *mrb, mrb_value self)
{
mrb_value ret;
struct RProc *proc;
struct mrb_irep *irep;
size_t i;
proc = mrb->c->ci[-1].proc;
if (MRB_PROC_CFUNC_P(proc)) {
return mrb_ary_new(mrb);
}
irep = proc->body.irep;
ret = mrb_ary_new_capa(mrb, irep->lv_len);
for (i = 0; i < irep->lv_len; ++i) {
mrb_ary_push(mrb, ret, mrb_symbol_value(irep->lv[i].name));
}
return ret;
}
void
mrb_mruby_proc_ext_gem_init(mrb_state* mrb)
{
......@@ -133,6 +156,7 @@ mrb_mruby_proc_ext_gem_init(mrb_state* mrb)
mrb_define_class_method(mrb, mrb->kernel_module, "proc", mrb_kernel_proc, MRB_ARGS_NONE());
mrb_define_method(mrb, mrb->kernel_module, "proc", mrb_kernel_proc, MRB_ARGS_NONE());
mrb_define_module_function(mrb, mrb->kernel_module, "local_variables", mrb_local_variables, MRB_ARGS_NONE());
}
void
......
......@@ -74,3 +74,11 @@ assert('mrb_cfunc_env_get') do
assert_equal 1, t.get_int(1)
end
assert('Kernel.local_variables') do
a, b = 0, 1
a += b
vars = Kernel.local_variables.sort
assert_equal [:a, :b, :vars], vars
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