diff options
| author | Yusuke Endoh <[email protected]> | 2020-05-24 01:25:03 +0900 |
|---|---|---|
| committer | Yusuke Endoh <[email protected]> | 2020-05-24 01:25:03 +0900 |
| commit | 6f4c585bd73fc43fb9e34a70d74b5a50852600ea (patch) | |
| tree | 588252a0fdbd163af58a85b85b43804fd17a1935 | |
| parent | 47ea60814b61d6f52a71b4799b4143f59191ff82 (diff) | |
| download | mruby-6f4c585bd73fc43fb9e34a70d74b5a50852600ea.tar.gz mruby-6f4c585bd73fc43fb9e34a70d74b5a50852600ea.zip | |
Do not destruct rest arguments for __send__
Formerly, `__send__(*args)` modified `args` with `Array#shift`.
This bug affects optcarrot.
This changeset avoids the array destruction by using
`args = args[1, len-1]`.
| -rw-r--r-- | include/mruby/array.h | 3 | ||||
| -rw-r--r-- | src/array.c | 7 | ||||
| -rw-r--r-- | src/vm.c | 2 | ||||
| -rw-r--r-- | test/t/kernel.rb | 4 |
4 files changed, 15 insertions, 1 deletions
diff --git a/include/mruby/array.h b/include/mruby/array.h index fd4094a02..92c86a8c5 100644 --- a/include/mruby/array.h +++ b/include/mruby/array.h @@ -292,6 +292,9 @@ MRB_API mrb_value mrb_ary_join(mrb_state *mrb, mrb_value ary, mrb_value sep); */ MRB_API mrb_value mrb_ary_resize(mrb_state *mrb, mrb_value ary, mrb_int new_len); +/* helper functions */ +mrb_value mrb_ary_subseq(mrb_state *mrb, mrb_value ary, mrb_int beg, mrb_int len); + MRB_END_DECL #endif /* MRUBY_ARRAY_H */ diff --git a/src/array.c b/src/array.c index 6e73bcd8e..ad0d5b8db 100644 --- a/src/array.c +++ b/src/array.c @@ -808,6 +808,13 @@ ary_subseq(mrb_state *mrb, struct RArray *a, mrb_int beg, mrb_int len) return mrb_obj_value(b); } +mrb_value +mrb_ary_subseq(mrb_state *mrb, mrb_value ary, mrb_int beg, mrb_int len) +{ + struct RArray *a = mrb_ary_ptr(ary); + return ary_subseq(mrb, a, beg, len); +} + static mrb_int aget_index(mrb_state *mrb, mrb_value index) { @@ -625,7 +625,7 @@ mrb_f_send(mrb_state *mrb, mrb_value self) ci->argc--; } else { /* variable length arguments */ - mrb_ary_shift(mrb, regs[0]); + regs[0] = mrb_ary_subseq(mrb, regs[0], 1, RARRAY_LEN(regs[0]) - 1); } if (MRB_METHOD_CFUNC_P(m)) { diff --git a/test/t/kernel.rb b/test/t/kernel.rb index aac6373fa..e3b9fe8ab 100644 --- a/test/t/kernel.rb +++ b/test/t/kernel.rb @@ -100,6 +100,10 @@ assert('Kernel#__send__', '15.3.1.3.4') do assert_true __send__(:respond_to?, :nil?) # test without argument and without block assert_equal String, __send__(:to_s).class + + args = [:respond_to?, :nil?] + assert_true __send__(*args) + assert_equal [:respond_to?, :nil?], args end assert('Kernel#block_given?', '15.3.1.3.6') do |
