diff options
| author | take_cheeze <[email protected]> | 2014-04-29 20:46:24 +0900 |
|---|---|---|
| committer | take_cheeze <[email protected]> | 2014-04-29 20:47:50 +0900 |
| commit | 7ea82fedd5585bc753464aae7fc406cd2f1ce08e (patch) | |
| tree | 245665c2b8daa72a1f9cce82ca9a358a93728782 /mrbgems/mruby-proc-ext | |
| parent | 4c7f9897c2fc1b180a7758130c8e6694d2361c96 (diff) | |
| download | mruby-7ea82fedd5585bc753464aae7fc406cd2f1ce08e.tar.gz mruby-7ea82fedd5585bc753464aae7fc406cd2f1ce08e.zip | |
Implement Kernel.local_variables for testing dumped local variables information.
Diffstat (limited to 'mrbgems/mruby-proc-ext')
| -rw-r--r-- | mrbgems/mruby-proc-ext/src/proc.c | 24 | ||||
| -rw-r--r-- | mrbgems/mruby-proc-ext/test/proc.rb | 8 |
2 files changed, 32 insertions, 0 deletions
diff --git a/mrbgems/mruby-proc-ext/src/proc.c b/mrbgems/mruby-proc-ext/src/proc.c index b105c95d7..964a7c9bb 100644 --- a/mrbgems/mruby-proc-ext/src/proc.c +++ b/mrbgems/mruby-proc-ext/src/proc.c @@ -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 diff --git a/mrbgems/mruby-proc-ext/test/proc.rb b/mrbgems/mruby-proc-ext/test/proc.rb index 0f5ecfb94..a5611e586 100644 --- a/mrbgems/mruby-proc-ext/test/proc.rb +++ b/mrbgems/mruby-proc-ext/test/proc.rb @@ -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 |
