diff options
| author | ksss <[email protected]> | 2017-02-24 11:06:47 +0900 |
|---|---|---|
| committer | ksss <[email protected]> | 2017-02-24 11:06:47 +0900 |
| commit | e8988197f24d19558353f945f6c5f106211491b0 (patch) | |
| tree | c340de213f70fb1127f4450efa4a5369e7be6994 /mrbgems/mruby-binding | |
| parent | a9f7b41219810fdbe0cffa872051cd091fc070ac (diff) | |
| download | mruby-e8988197f24d19558353f945f6c5f106211491b0.tar.gz mruby-e8988197f24d19558353f945f6c5f106211491b0.zip | |
Try Binding class
Diffstat (limited to 'mrbgems/mruby-binding')
| -rw-r--r-- | mrbgems/mruby-binding/mrbgem.rake | 7 | ||||
| -rw-r--r-- | mrbgems/mruby-binding/mrblib/binding.rb | 5 | ||||
| -rw-r--r-- | mrbgems/mruby-binding/src/binding.c | 46 | ||||
| -rw-r--r-- | mrbgems/mruby-binding/test/binding.rb | 17 |
4 files changed, 75 insertions, 0 deletions
diff --git a/mrbgems/mruby-binding/mrbgem.rake b/mrbgems/mruby-binding/mrbgem.rake new file mode 100644 index 000000000..1dbc768de --- /dev/null +++ b/mrbgems/mruby-binding/mrbgem.rake @@ -0,0 +1,7 @@ +MRuby::Gem::Specification.new('mruby-binding') do |spec| + spec.license = 'MIT' + spec.author = 'mruby developers' + spec.summary = 'Binding class' + + spec.add_dependency('mruby-eval', :core => 'mruby-eval') +end diff --git a/mrbgems/mruby-binding/mrblib/binding.rb b/mrbgems/mruby-binding/mrblib/binding.rb new file mode 100644 index 000000000..b07480db1 --- /dev/null +++ b/mrbgems/mruby-binding/mrblib/binding.rb @@ -0,0 +1,5 @@ +class Binding + def eval(expr, *args) + Kernel.eval(expr, self, *args) + end +end diff --git a/mrbgems/mruby-binding/src/binding.c b/mrbgems/mruby-binding/src/binding.c new file mode 100644 index 000000000..4899dcc20 --- /dev/null +++ b/mrbgems/mruby-binding/src/binding.c @@ -0,0 +1,46 @@ +#include "mruby.h" +#include "mruby/array.h" +#include "mruby/hash.h" +#include "mruby/proc.h" +#include "mruby/variable.h" + +mrb_value proc_local_variables(mrb_state *mrb, struct RProc *proc); + +static mrb_value +binding_local_variables(mrb_state *mrb, mrb_value self) +{ + struct RProc *proc = mrb_proc_ptr(mrb_iv_get(mrb, self, mrb_intern_lit(mrb, "proc"))); + return proc_local_variables(mrb, proc); +} + +static mrb_value +mrb_f_binding(mrb_state *mrb, mrb_value self) +{ + struct RObject *obj; + mrb_value binding; + struct RProc *proc; + mrb_int i; + + obj = (struct RObject*)mrb_obj_alloc(mrb, MRB_TT_OBJECT, mrb_class_get(mrb, "Binding")); + binding = mrb_obj_value(obj); + proc = mrb->c->ci[-1].proc; + mrb_iv_set(mrb, binding, mrb_intern_lit(mrb, "proc"), mrb_obj_value(proc)); + mrb_iv_set(mrb, binding, mrb_intern_lit(mrb, "recv"), self); + return binding; +} + +void +mrb_mruby_binding_gem_init(mrb_state *mrb) +{ + struct RClass *binding = mrb_define_class(mrb, "Binding", mrb->object_class); + mrb_undef_class_method(mrb, binding, "new"); + + mrb_define_method(mrb, mrb->kernel_module, "binding", mrb_f_binding, MRB_ARGS_NONE()); + + mrb_define_method(mrb, binding, "local_variables", binding_local_variables, MRB_ARGS_NONE()); +} + +void +mrb_mruby_binding_gem_final(mrb_state *mrb) +{ +} diff --git a/mrbgems/mruby-binding/test/binding.rb b/mrbgems/mruby-binding/test/binding.rb new file mode 100644 index 000000000..3bed9b31e --- /dev/null +++ b/mrbgems/mruby-binding/test/binding.rb @@ -0,0 +1,17 @@ +assert("Kernel.#binding") do + assert_kind_of Binding, binding +end + +assert("Binding#local_variables") do + block = Proc.new do |a| + b = 1 + binding + end + assert_equal [:a, :b, :block], block.call(0).local_variables +end + +assert("Binding#eval") do + b = nil + 1.times { x, y, z = 1, 2, 3; [x,y,z]; b = binding } + assert_equal([1, 2, 3], b.eval("[x, y, z]")) +end |
