diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-03-13 16:18:45 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-03-13 16:18:45 +0000 |
commit | 163f9abe4f2ba66ef433b327e0ceb2edb249e3ae (patch) | |
tree | 6dbc317886a3e1125fa0ebfdfca2eea5c4af54a8 | |
parent | 2deb6e8ee2db4e0ce02b6e161508633dcc44ded1 (diff) | |
download | ruby-163f9abe4f2ba66ef433b327e0ceb2edb249e3ae.tar.gz |
vm_insnhelper.c: relax arity check
* vm.c (invoke_block_from_c): add splattable argument.
* vm.c (vm_invoke_proc): disallow to splat when directly invoked.
* vm_insnhelper.c (vm_callee_setup_arg_complex, vm_callee_setup_arg):
relax arity check of yielded lambda. [ruby-core:61340] [Bug #9605]
* test/ruby/test_yield.rb (TestRubyYieldGen#emu_bind_params): no
longer raise ArgumentError when splatting to lambda.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45327 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 12 | ||||
-rw-r--r-- | test/ruby/test_enum.rb | 111 | ||||
-rw-r--r-- | test/ruby/test_lambda.rb | 13 | ||||
-rw-r--r-- | test/ruby/test_yield.rb | 2 | ||||
-rw-r--r-- | vm.c | 12 | ||||
-rw-r--r-- | vm_insnhelper.c | 39 |
6 files changed, 173 insertions, 16 deletions
@@ -1,3 +1,15 @@ +Fri Mar 14 01:18:24 2014 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * vm.c (invoke_block_from_c): add splattable argument. + + * vm.c (vm_invoke_proc): disallow to splat when directly invoked. + + * vm_insnhelper.c (vm_callee_setup_arg_complex, vm_callee_setup_arg): + relax arity check of yielded lambda. [ruby-core:61340] [Bug #9605] + + * test/ruby/test_yield.rb (TestRubyYieldGen#emu_bind_params): no + longer raise ArgumentError when splatting to lambda. + Thu Mar 13 23:51:02 2014 NAKAMURA Usaku <usa@ruby-lang.org> * ext/-test-/win32/dln/libdlntest.c (dlntest_ordinal): no need to diff --git a/test/ruby/test_enum.rb b/test/ruby/test_enum.rb index beddb817cd..5668ce4de0 100644 --- a/test/ruby/test_enum.rb +++ b/test/ruby/test_enum.rb @@ -56,6 +56,11 @@ class TestEnumerable < Test::Unit::TestCase bug5801 = '[ruby-dev:45041]' @empty.grep(//) assert_nothing_raised(bug5801) {100.times {@empty.block.call}} + + a = [] + lambda = ->(x, i) {a << [x, i]} + @obj.each_with_index.grep(proc{|x,i|x==2}, &lambda) + assert_equal([[2, 1], [2, 4]], a) end def test_count @@ -81,6 +86,8 @@ class TestEnumerable < Test::Unit::TestCase assert_equal(2, @obj.find {|x| x % 2 == 0 }) assert_equal(nil, @obj.find {|x| false }) assert_equal(:foo, @obj.find(proc { :foo }) {|x| false }) + cond = ->(x, i) { x % 2 == 0 } + assert_equal([2, 1], @obj.each_with_index.find(&cond)) end def test_find_index @@ -93,10 +100,14 @@ class TestEnumerable < Test::Unit::TestCase def test_find_all assert_equal([1, 3, 1], @obj.find_all {|x| x % 2 == 1 }) + cond = ->(x, i) { x % 2 == 1 } + assert_equal([[1, 0], [3, 2], [1, 3]], @obj.each_with_index.find_all(&cond)) end def test_reject assert_equal([2, 3, 2], @obj.reject {|x| x < 2 }) + cond = ->(x, i) {x < 2} + assert_equal([[2, 1], [3, 2], [2, 4]], @obj.each_with_index.reject(&cond)) end def test_to_a @@ -144,11 +155,17 @@ class TestEnumerable < Test::Unit::TestCase def test_partition assert_equal([[1, 3, 1], [2, 2]], @obj.partition {|x| x % 2 == 1 }) + cond = ->(x, i) { x % 2 == 1 } + assert_equal([[[1, 0], [3, 2], [1, 3]], [[2, 1], [2, 4]]], @obj.each_with_index.partition(&cond)) end def test_group_by h = { 1 => [1, 1], 2 => [2, 2], 3 => [3] } assert_equal(h, @obj.group_by {|x| x }) + + h = {1=>[[1, 0], [1, 3]], 2=>[[2, 1], [2, 4]], 3=>[[3, 2]]} + cond = ->(x, i) { x } + assert_equal(h, @obj.each_with_index.group_by(&cond)) end def test_first @@ -164,6 +181,9 @@ class TestEnumerable < Test::Unit::TestCase def test_sort_by assert_equal([3, 2, 2, 1, 1], @obj.sort_by {|x| -x }) assert_equal((1..300).to_a.reverse, (1..300).sort_by {|x| -x }) + + cond = ->(x, i) { [-x, i] } + assert_equal([[3, 2], [2, 1], [2, 4], [1, 0], [1, 3]], @obj.each_with_index.sort_by(&cond)) end def test_all @@ -205,6 +225,8 @@ class TestEnumerable < Test::Unit::TestCase def test_min assert_equal(1, @obj.min) assert_equal(3, @obj.min {|a,b| b <=> a }) + cond = ->((a, ia), (b, ib)) { (b <=> a).nonzero? or ia <=> ib } + assert_equal([3, 2], @obj.each_with_index.min(&cond)) ary = %w(albatross dog horse) assert_equal("albatross", ary.min) assert_equal("dog", ary.min {|a,b| a.length <=> b.length }) @@ -217,6 +239,8 @@ class TestEnumerable < Test::Unit::TestCase def test_max assert_equal(3, @obj.max) assert_equal(1, @obj.max {|a,b| b <=> a }) + cond = ->((a, ia), (b, ib)) { (b <=> a).nonzero? or ia <=> ib } + assert_equal([1, 3], @obj.each_with_index.max(&cond)) ary = %w(albatross dog horse) assert_equal("horse", ary.max) assert_equal("albatross", ary.max {|a,b| a.length <=> b.length }) @@ -239,6 +263,8 @@ class TestEnumerable < Test::Unit::TestCase def test_min_by assert_equal(3, @obj.min_by {|x| -x }) + cond = ->(x, i) { -x } + assert_equal([3, 2], @obj.each_with_index.min_by(&cond)) a = %w(albatross dog horse) assert_equal("dog", a.min_by {|x| x.length }) assert_equal(3, [2,3,1].min_by {|x| -x }) @@ -247,6 +273,8 @@ class TestEnumerable < Test::Unit::TestCase def test_max_by assert_equal(1, @obj.max_by {|x| -x }) + cond = ->(x, i) { -x } + assert_equal([1, 0], @obj.each_with_index.max_by(&cond)) a = %w(albatross dog horse) assert_equal("albatross", a.max_by {|x| x.length }) assert_equal(1, [2,3,1].max_by {|x| -x }) @@ -255,6 +283,8 @@ class TestEnumerable < Test::Unit::TestCase def test_minmax_by assert_equal([3, 1], @obj.minmax_by {|x| -x }) + cond = ->(x, i) { -x } + assert_equal([[3, 2], [1, 0]], @obj.each_with_index.minmax_by(&cond)) a = %w(albatross dog horse) assert_equal(["dog", "albatross"], a.minmax_by {|x| x.length }) assert_equal([3, 1], [2,3,1].minmax_by {|x| -x }) @@ -302,6 +332,10 @@ class TestEnumerable < Test::Unit::TestCase def test_each_entry assert_equal([1, 2, 3], [1, 2, 3].each_entry.to_a) assert_equal([1, [1, 2]], Foo.new.each_entry.to_a) + a = [] + cond = ->(x, i) { a << x } + @obj.each_with_index.each_entry(&cond) + assert_equal([1, 2, 3, 1, 2], a) end def test_zip @@ -311,6 +345,11 @@ class TestEnumerable < Test::Unit::TestCase assert_equal([[1,:a],[2,:b],[3,:c],[1,nil],[2,nil]], a) a = [] + cond = ->((x, i), y) { a << [x, y, i] } + @obj.each_with_index.zip([:a, :b, :c], &cond) + assert_equal([[1,:a,0],[2,:b,1],[3,:c,2],[1,nil,3],[2,nil,4]], a) + + a = [] @obj.zip({a: "A", b: "B", c: "C"}) {|x,y| a << [x, y] } assert_equal([[1,[:a,"A"]],[2,[:b,"B"]],[3,[:c,"C"]],[1,nil],[2,nil]], a) @@ -329,6 +368,8 @@ class TestEnumerable < Test::Unit::TestCase def test_take_while assert_equal([1,2], @obj.take_while {|x| x <= 2}) + cond = ->(x, i) {x <= 2} + assert_equal([[1, 0], [2, 1]], @obj.each_with_index.take_while(&cond)) bug5801 = '[ruby-dev:45040]' @empty.take_while {true} @@ -341,10 +382,19 @@ class TestEnumerable < Test::Unit::TestCase def test_drop_while assert_equal([3,1,2], @obj.drop_while {|x| x <= 2}) + cond = ->(x, i) {x <= 2} + assert_equal([[3, 2], [1, 3], [2, 4]], @obj.each_with_index.drop_while(&cond)) end def test_cycle assert_equal([1,2,3,1,2,1,2,3,1,2], @obj.cycle.take(10)) + a = [] + @obj.cycle(2) {|x| a << x} + assert_equal([1,2,3,1,2,1,2,3,1,2], a) + a = [] + cond = ->(x, i) {a << x} + @obj.each_with_index.cycle(2, &cond) + assert_equal([1,2,3,1,2,1,2,3,1,2], a) end def test_callcc @@ -459,4 +509,65 @@ class TestEnumerable < Test::Unit::TestCase assert_not_warn{ss.slice_before(/\A...\z/).to_a} end + def test_detect + @obj = ('a'..'z') + assert_equal('c', @obj.detect {|x| x == 'c' }) + + proc = Proc.new {|x| x == 'c' } + assert_equal('c', @obj.detect(&proc)) + + lambda = ->(x) { x == 'c' } + assert_equal('c', @obj.detect(&lambda)) + + assert_equal(['c',2], @obj.each_with_index.detect {|x, i| x == 'c' }) + + proc2 = Proc.new {|x, i| x == 'c' } + assert_equal(['c',2], @obj.each_with_index.detect(&proc2)) + + bug9605 = '[ruby-core:61340]' + lambda2 = ->(x, i) { x == 'c' } + assert_equal(['c',2], @obj.each_with_index.detect(&lambda2)) + end + + def test_select + @obj = ('a'..'z') + assert_equal(['c'], @obj.select {|x| x == 'c' }) + + proc = Proc.new {|x| x == 'c' } + assert_equal(['c'], @obj.select(&proc)) + + lambda = ->(x) { x == 'c' } + assert_equal(['c'], @obj.select(&lambda)) + + assert_equal([['c',2]], @obj.each_with_index.select {|x, i| x == 'c' }) + + proc2 = Proc.new {|x, i| x == 'c' } + assert_equal([['c',2]], @obj.each_with_index.select(&proc2)) + + bug9605 = '[ruby-core:61340]' + lambda2 = ->(x, i) { x == 'c' } + assert_equal([['c',2]], @obj.each_with_index.select(&lambda2)) + end + + def test_map + @obj = ('a'..'e') + assert_equal(['A', 'B', 'C', 'D', 'E'], @obj.map {|x| x.upcase }) + + proc = Proc.new {|x| x.upcase } + assert_equal(['A', 'B', 'C', 'D', 'E'], @obj.map(&proc)) + + lambda = ->(x) { x.upcase } + assert_equal(['A', 'B', 'C', 'D', 'E'], @obj.map(&lambda)) + + assert_equal([['A',0], ['B',1], ['C',2], ['D',3], ['E',4]], + @obj.each_with_index.map {|x, i| [x.upcase, i] }) + + proc2 = Proc.new {|x, i| [x.upcase, i] } + assert_equal([['A',0], ['B',1], ['C',2], ['D',3], ['E',4]], + @obj.each_with_index.map(&proc2)) + + lambda2 = ->(x, i) { [x.upcase, i] } + assert_equal([['A',0], ['B',1], ['C',2], ['D',3], ['E',4]], + @obj.each_with_index.map(&lambda2)) + end end diff --git a/test/ruby/test_lambda.rb b/test/ruby/test_lambda.rb index f45ca6af71..40a6b84021 100644 --- a/test/ruby/test_lambda.rb +++ b/test/ruby/test_lambda.rb @@ -25,8 +25,13 @@ class TestLambdaParameters < Test::Unit::TestCase def test_lambda_as_iterator a = 0 2.times(&->(_){ a += 1 }) - assert_equal(a, 2) + assert_equal(2, a) assert_raise(ArgumentError) {1.times(&->(){ a += 1 })} + bug9605 = '[ruby-core:61468] [Bug #9605]' + assert_nothing_raised(ArgumentError, bug9605) {1.times(&->(n){ a += 1 })} + assert_equal(3, a, bug9605) + assert_nothing_raised(ArgumentError, bug9605) {a = [[1, 2]].map(&->(x, y) {x+y})} + assert_equal([3], a, bug9605) end def test_call_rest_args @@ -60,6 +65,12 @@ class TestLambdaParameters < Test::Unit::TestCase assert_nil(b) end + def test_call_block_from_lambda + bug9605 = '[ruby-core:61470] [Bug #9605]' + plus = ->(x,y) {x+y} + assert_raise(ArgumentError, bug9605) {proc(&plus).call [1,2]} + end + def foo assert_equal(nil, ->(&b){ b }.call) end diff --git a/test/ruby/test_yield.rb b/test/ruby/test_yield.rb index 143ee55a9f..a0af3eded6 100644 --- a/test/ruby/test_yield.rb +++ b/test/ruby/test_yield.rb @@ -244,7 +244,7 @@ class TestRubyYieldGen < Test::Unit::TestCase throw :emuerror, ArgumentError end else - if args.length != params.length + if args.length != params.length and !(args.length == 1 and Array === args[0] and args[0].length == params.length) throw :emuerror, ArgumentError end end @@ -716,7 +716,7 @@ static inline VALUE invoke_block_from_c(rb_thread_t *th, const rb_block_t *block, VALUE self, int argc, const VALUE *argv, const rb_block_t *blockptr, const NODE *cref, - VALUE defined_class) + VALUE defined_class, int splattable) { if (SPECIAL_CONST_P(block->iseq)) return Qnil; @@ -734,7 +734,7 @@ invoke_block_from_c(rb_thread_t *th, const rb_block_t *block, } opt_pc = vm_yield_setup_args(th, iseq, argc, cfp->sp, blockptr, - type == VM_FRAME_MAGIC_LAMBDA); + (type == VM_FRAME_MAGIC_LAMBDA) ? splattable+1 : 0); vm_push_frame(th, iseq, type | VM_FRAME_FLAG_FINISH, self, defined_class, @@ -772,7 +772,7 @@ vm_yield_with_cref(rb_thread_t *th, int argc, const VALUE *argv, const NODE *cre { const rb_block_t *blockptr = check_block(th); return invoke_block_from_c(th, blockptr, blockptr->self, argc, argv, 0, cref, - blockptr->klass); + blockptr->klass, 1); } static inline VALUE @@ -780,7 +780,7 @@ vm_yield(rb_thread_t *th, int argc, const VALUE *argv) { const rb_block_t *blockptr = check_block(th); return invoke_block_from_c(th, blockptr, blockptr->self, argc, argv, 0, 0, - blockptr->klass); + blockptr->klass, 1); } static inline VALUE @@ -788,7 +788,7 @@ vm_yield_with_block(rb_thread_t *th, int argc, const VALUE *argv, const rb_block { const rb_block_t *blockptr = check_block(th); return invoke_block_from_c(th, blockptr, blockptr->self, argc, argv, blockargptr, 0, - blockptr->klass); + blockptr->klass, 1); } static VALUE @@ -805,7 +805,7 @@ vm_invoke_proc(rb_thread_t *th, rb_proc_t *proc, VALUE self, VALUE defined_class th->safe_level = proc->safe_level; } val = invoke_block_from_c(th, &proc->block, self, argc, argv, blockptr, 0, - defined_class); + defined_class, 0); } TH_POP_TAG(); diff --git a/vm_insnhelper.c b/vm_insnhelper.c index 6c050440c7..1bca35ea22 100644 --- a/vm_insnhelper.c +++ b/vm_insnhelper.c @@ -1095,13 +1095,14 @@ vm_callee_setup_keyword_arg(const rb_iseq_t *iseq, int argc, int m, VALUE *orig_ } static inline int -vm_callee_setup_arg_complex(rb_thread_t *th, rb_call_info_t *ci, const rb_iseq_t *iseq, VALUE *orig_argv) +vm_callee_setup_arg_complex(rb_thread_t *th, rb_call_info_t *ci, const rb_iseq_t *iseq, VALUE *orig_argv, + int splattable) { const int m = iseq->argc; const int opts = iseq->arg_opts - (iseq->arg_opts > 0); const int min = m + iseq->arg_post_len; const int max = (iseq->arg_rest == -1) ? m + opts + iseq->arg_post_len : UNLIMITED_ARGUMENTS; - const int orig_argc = ci->argc; + int orig_argc = ci->argc; int argc = orig_argc; VALUE *argv = orig_argv; VALUE keyword_hash = Qnil; @@ -1116,7 +1117,18 @@ vm_callee_setup_arg_complex(rb_thread_t *th, rb_call_info_t *ci, const rb_iseq_t /* mandatory */ if ((argc < min) || (argc > max && max != UNLIMITED_ARGUMENTS)) { - argument_error(iseq, argc, min, max); + VALUE arg0; + long len; + if (!splattable || + argc != 1 || + !RB_TYPE_P(arg0 = argv[0], T_ARRAY) || + (len = RARRAY_LEN(arg0)) < (long)min || + (len > (long)max && max != UNLIMITED_ARGUMENTS)) { + argument_error(iseq, argc, min, max); + } + CHECK_VM_STACK_OVERFLOW(th->cfp, len - 1); + MEMCPY(argv, RARRAY_CONST_PTR(arg0), VALUE, len); + ci->argc = argc = orig_argc = (int)len; } argv += m; @@ -1203,7 +1215,17 @@ vm_callee_setup_arg(rb_thread_t *th, rb_call_info_t *ci, const rb_iseq_t *iseq, if (LIKELY(iseq->arg_simple & 0x01)) { /* simple check */ if (ci->argc != iseq->argc) { - argument_error(iseq, ci->argc, iseq->argc, iseq->argc); + VALUE arg0; + long len; + if (!(is_lambda > 1) || + ci->argc != 1 || + !RB_TYPE_P(arg0 = argv[0], T_ARRAY) || + (len = RARRAY_LEN(arg0)) != (long)iseq->argc) { + argument_error(iseq, ci->argc, iseq->argc, iseq->argc); + } + CHECK_VM_STACK_OVERFLOW(th->cfp, len - 1); + MEMCPY(argv, RARRAY_CONST_PTR(arg0), VALUE, len); + ci->argc = (int)len; } ci->aux.opt_pc = 0; CI_SET_FASTPATH(ci, @@ -1215,7 +1237,7 @@ vm_callee_setup_arg(rb_thread_t *th, rb_call_info_t *ci, const rb_iseq_t *iseq, !(ci->me->flag & NOEX_PROTECTED))); } else { - ci->aux.opt_pc = vm_callee_setup_arg_complex(th, ci, iseq, argv); + ci->aux.opt_pc = vm_callee_setup_arg_complex(th, ci, iseq, argv, is_lambda > 1); } } @@ -2290,7 +2312,8 @@ vm_yield_setup_block_args(rb_thread_t *th, const rb_iseq_t * iseq, static inline int vm_yield_setup_args(rb_thread_t * const th, const rb_iseq_t *iseq, - int argc, VALUE *argv, const rb_block_t *blockptr, int lambda) + int argc, VALUE *argv, const rb_block_t *blockptr, + int lambda) { if (0) { /* for debug */ printf(" argc: %d\n", argc); @@ -2309,7 +2332,7 @@ vm_yield_setup_args(rb_thread_t * const th, const rb_iseq_t *iseq, ci_entry.flag = 0; ci_entry.argc = argc; ci_entry.blockptr = (rb_block_t *)blockptr; - vm_callee_setup_arg(th, &ci_entry, iseq, argv, 1); + vm_callee_setup_arg(th, &ci_entry, iseq, argv, lambda); return ci_entry.aux.opt_pc; } else { @@ -2340,7 +2363,7 @@ vm_invoke_block(rb_thread_t *th, rb_control_frame_t *reg_cfp, rb_call_info_t *ci VALUE * const rsp = GET_SP() - ci->argc; SET_SP(rsp); - opt_pc = vm_yield_setup_args(th, iseq, ci->argc, rsp, 0, is_lambda); + opt_pc = vm_yield_setup_args(th, iseq, ci->argc, rsp, 0, is_lambda * 2); vm_push_frame(th, iseq, is_lambda ? VM_FRAME_MAGIC_LAMBDA : VM_FRAME_MAGIC_BLOCK, |