diff options
Diffstat (limited to 'mrbgems/mruby-proc-ext')
| -rw-r--r-- | mrbgems/mruby-proc-ext/mrblib/proc.rb | 8 | ||||
| -rw-r--r-- | mrbgems/mruby-proc-ext/src/proc.c | 31 | ||||
| -rw-r--r-- | mrbgems/mruby-proc-ext/test/proc.rb | 18 |
3 files changed, 43 insertions, 14 deletions
diff --git a/mrbgems/mruby-proc-ext/mrblib/proc.rb b/mrbgems/mruby-proc-ext/mrblib/proc.rb index 789b7a3ac..abe9c7944 100644 --- a/mrbgems/mruby-proc-ext/mrblib/proc.rb +++ b/mrbgems/mruby-proc-ext/mrblib/proc.rb @@ -39,4 +39,12 @@ class Proc make_curry.call end + def <<(other) + ->(*args, &block) { call(other.call(*args, &block)) } + end + + def >>(other) + ->(*args, &block) { other.call(call(*args, &block)) } + end + end diff --git a/mrbgems/mruby-proc-ext/src/proc.c b/mrbgems/mruby-proc-ext/src/proc.c index 9ce6c1831..17884e3c6 100644 --- a/mrbgems/mruby-proc-ext/src/proc.c +++ b/mrbgems/mruby-proc-ext/src/proc.c @@ -25,8 +25,8 @@ mrb_proc_source_location(mrb_state *mrb, mrb_value self) int32_t line; const char *filename; - filename = mrb_debug_get_filename(irep, 0); - line = mrb_debug_get_line(irep, 0); + filename = mrb_debug_get_filename(mrb, irep, 0); + line = mrb_debug_get_line(mrb, irep, 0); return (!filename && line == -1)? mrb_nil_value() : mrb_assoc_new(mrb, mrb_str_new_cstr(mrb, filename), mrb_fixnum_value(line)); @@ -46,11 +46,11 @@ mrb_proc_inspect(mrb_state *mrb, mrb_value self) int32_t line; mrb_str_cat_lit(mrb, str, "@"); - filename = mrb_debug_get_filename(irep, 0); + filename = mrb_debug_get_filename(mrb, irep, 0); mrb_str_cat_cstr(mrb, str, filename ? filename : "-"); mrb_str_cat_lit(mrb, str, ":"); - line = mrb_debug_get_line(irep, 0); + line = mrb_debug_get_line(mrb, irep, 0); if (line != -1) { str = mrb_format(mrb, "%S:%S", str, mrb_fixnum_value(line)); } @@ -94,20 +94,21 @@ static mrb_value mrb_proc_parameters(mrb_state *mrb, mrb_value self) { struct parameters_type { - int size; + size_t len; const char *name; + int size; } *p, parameters_list [] = { - {0, "req"}, - {0, "opt"}, - {0, "rest"}, - {0, "req"}, - {0, "block"}, - {0, NULL} + {sizeof("req") - 1, "req", 0}, + {sizeof("opt") - 1, "opt", 0}, + {sizeof("rest") - 1, "rest", 0}, + {sizeof("req") - 1, "req", 0}, + {sizeof("block") - 1, "block", 0}, + {0, NULL, 0} }; const struct RProc *proc = mrb_proc_ptr(self); const struct mrb_irep *irep = proc->body.irep; mrb_aspec aspec; - mrb_value sname, parameters; + mrb_value parameters; int i, j; int max = -1; @@ -126,7 +127,9 @@ mrb_proc_parameters(mrb_state *mrb, mrb_value self) } if (!MRB_PROC_STRICT_P(proc)) { + parameters_list[0].len = sizeof("opt") - 1; parameters_list[0].name = "opt"; + parameters_list[3].len = sizeof("opt") - 1; parameters_list[3].name = "opt"; } @@ -141,8 +144,8 @@ mrb_proc_parameters(mrb_state *mrb, mrb_value self) max = irep->nlocals-1; for (i = 0, p = parameters_list; p->name; p++) { - if (p->size <= 0) continue; - sname = mrb_symbol_value(mrb_intern_cstr(mrb, p->name)); + mrb_value sname = mrb_symbol_value(mrb_intern_static(mrb, p->name, p->len)); + for (j = 0; j < p->size; i++, j++) { mrb_value a; diff --git a/mrbgems/mruby-proc-ext/test/proc.rb b/mrbgems/mruby-proc-ext/test/proc.rb index 037d8d124..1220841c8 100644 --- a/mrbgems/mruby-proc-ext/test/proc.rb +++ b/mrbgems/mruby-proc-ext/test/proc.rb @@ -13,6 +13,11 @@ assert('Proc#inspect') do assert_kind_of String, ins end +assert('Proc#parameters') do + parameters = Proc.new{|x,y=42,*other|}.parameters + assert_equal [[:opt, :x], [:opt, :y], [:rest, :other]], parameters +end + assert('Proc#lambda?') do assert_true lambda{}.lambda? assert_true !Proc.new{}.lambda? @@ -72,6 +77,19 @@ assert('Kernel#proc') do end end +assert "Proc#<< and Proc#>>" do + add3 = ->(n) { n + 3 } + mul2 = ->(n) { n * 2 } + + f1 = mul2 << add3 + assert_kind_of Proc, f1 + assert_equal 16, f1.call(5) + + f2 = mul2 >> add3 + assert_kind_of Proc, f2 + assert_equal 13, f2.call(5) +end + assert('mrb_proc_new_cfunc_with_env') do ProcExtTest.mrb_proc_new_cfunc_with_env(:test) ProcExtTest.mrb_proc_new_cfunc_with_env(:mruby) |
