ruby-changes:33248
From: nobu <ko1@a...>
Date: Fri, 14 Mar 2014 01:18:53 +0900 (JST)
Subject: [ruby-changes:33248] nobu:r45327 (trunk): vm_insnhelper.c: relax arity check
nobu 2014-03-14 01:18:45 +0900 (Fri, 14 Mar 2014) New Revision: 45327 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=45327 Log: 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. Modified files: trunk/ChangeLog trunk/test/ruby/test_enum.rb trunk/test/ruby/test_lambda.rb trunk/test/ruby/test_yield.rb trunk/vm.c trunk/vm_insnhelper.c Index: ChangeLog =================================================================== --- ChangeLog (revision 45326) +++ ChangeLog (revision 45327) @@ -1,3 +1,15 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Fri Mar 14 01:18:24 2014 Nobuyoshi Nakada <nobu@r...> + + * 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@r...> * ext/-test-/win32/dln/libdlntest.c (dlntest_ordinal): no need to Index: vm.c =================================================================== --- vm.c (revision 45326) +++ vm.c (revision 45327) @@ -716,7 +716,7 @@ static inline VALUE https://github.com/ruby/ruby/blob/trunk/vm.c#L716 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, con https://github.com/ruby/ruby/blob/trunk/vm.c#L734 } 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 https://github.com/ruby/ruby/blob/trunk/vm.c#L772 { 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, cons https://github.com/ruby/ruby/blob/trunk/vm.c#L780 { 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 https://github.com/ruby/ruby/blob/trunk/vm.c#L788 { 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_ https://github.com/ruby/ruby/blob/trunk/vm.c#L805 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(); Index: vm_insnhelper.c =================================================================== --- vm_insnhelper.c (revision 45326) +++ vm_insnhelper.c (revision 45327) @@ -1095,13 +1095,14 @@ vm_callee_setup_keyword_arg(const rb_ise https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L1095 } 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 https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L1117 /* 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_ https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L1215 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_ https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L1237 !(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 *t https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L2312 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 https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L2332 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_cont https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L2363 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, Index: test/ruby/test_lambda.rb =================================================================== --- test/ruby/test_lambda.rb (revision 45326) +++ test/ruby/test_lambda.rb (revision 45327) @@ -25,8 +25,13 @@ class TestLambdaParameters < Test::Unit: https://github.com/ruby/ruby/blob/trunk/test/ruby/test_lambda.rb#L25 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: https://github.com/ruby/ruby/blob/trunk/test/ruby/test_lambda.rb#L65 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 Index: test/ruby/test_enum.rb =================================================================== --- test/ruby/test_enum.rb (revision 45326) +++ test/ruby/test_enum.rb (revision 45327) @@ -56,6 +56,11 @@ class TestEnumerable < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L56 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::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L86 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::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L100 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::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L155 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::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L181 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::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L225 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::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L239 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::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L263 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::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L273 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::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L283 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::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L332 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::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L345 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::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L368 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::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L382 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::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L509 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 Index: test/ruby/test_yield.rb =================================================================== --- test/ruby/test_yield.rb (revision 45326) +++ test/ruby/test_yield.rb (revision 45327) @@ -244,7 +244,7 @@ class TestRubyYieldGen < Test::Unit::Tes https://github.com/ruby/ruby/blob/trunk/test/ruby/test_yield.rb#L244 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 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/