ruby-changes:26606
From: ktsj <ko1@a...>
Date: Sun, 30 Dec 2012 11:06:41 +0900 (JST)
Subject: [ruby-changes:26606] ktsj:r38657 (trunk): * vm_insnhelper.c: set keyword hash on Proc/block calls.
ktsj 2012-12-30 11:06:28 +0900 (Sun, 30 Dec 2012) New Revision: 38657 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=38657 Log: * vm_insnhelper.c: set keyword hash on Proc/block calls. [ruby-core:51172] [Bug #7630] * test/ruby/test_keyword.rb: add tests for above. Modified files: trunk/ChangeLog trunk/test/ruby/test_keyword.rb trunk/vm_insnhelper.c Index: ChangeLog =================================================================== --- ChangeLog (revision 38656) +++ ChangeLog (revision 38657) @@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Sun Dec 30 10:51:29 2012 Kazuki Tsujimoto <kazuki@c...> + + * vm_insnhelper.c: set keyword hash on Proc/block calls. + [ruby-core:51172] [Bug #7630] + + * test/ruby/test_keyword.rb: add tests for above. + Sat Dec 29 21:57:11 2012 Keiju Ishitsuka <keiju@i...> * lib/irb/completion.rb: treat rightly comletion for symbol on irb Index: vm_insnhelper.c =================================================================== --- vm_insnhelper.c (revision 38656) +++ vm_insnhelper.c (revision 38657) @@ -1065,6 +1065,34 @@ vm_caller_setup_args(const rb_thread_t * https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L1065 } static inline int +vm_callee_setup_keyword_arg(const rb_iseq_t *iseq, int argc, VALUE *orig_argv) +{ + VALUE keyword_hash = Qnil; + int i, j; + + if (argc > 0) keyword_hash = rb_check_hash_type(orig_argv[argc-1]); + if (!NIL_P(keyword_hash)) { + argc--; + keyword_hash = rb_hash_dup(keyword_hash); + if (iseq->arg_keyword_check) { + for (i = j = 0; i < iseq->arg_keywords; i++) { + if (st_lookup(RHASH_TBL(keyword_hash), ID2SYM(iseq->arg_keyword_table[i]), 0)) j++; + } + if (RHASH_TBL(keyword_hash)->num_entries > (unsigned int) j) { + unknown_keyword_error(iseq, keyword_hash); + } + } + } + else { + keyword_hash = rb_hash_new(); + } + + orig_argv[iseq->arg_keyword] = keyword_hash; + + return argc; +} + +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) { const int m = iseq->argc; @@ -1074,29 +1102,13 @@ vm_callee_setup_arg_complex(rb_thread_t https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L1102 const int orig_argc = ci->argc; int argc = orig_argc; VALUE *argv = orig_argv; - VALUE keyword_hash = Qnil; rb_num_t opt_pc = 0; th->mark_stack_len = argc + iseq->arg_size; + /* keyword argument */ if (iseq->arg_keyword != -1) { - int i, j; - if (argc > 0) keyword_hash = rb_check_hash_type(argv[argc-1]); - if (!NIL_P(keyword_hash)) { - argc--; - keyword_hash = rb_hash_dup(keyword_hash); - if (iseq->arg_keyword_check) { - for (i = j = 0; i < iseq->arg_keywords; i++) { - if (st_lookup(RHASH_TBL(keyword_hash), ID2SYM(iseq->arg_keyword_table[i]), 0)) j++; - } - if (RHASH_TBL(keyword_hash)->num_entries > (unsigned int) j) { - unknown_keyword_error(iseq, keyword_hash); - } - } - } - else { - keyword_hash = rb_hash_new(); - } + argc = vm_callee_setup_keyword_arg(iseq, argc, orig_argv); } /* mandatory */ @@ -1142,11 +1154,6 @@ vm_callee_setup_arg_complex(rb_thread_t https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L1154 argc = 0; } - /* keyword argument */ - if (iseq->arg_keyword != -1) { - orig_argv[iseq->arg_keyword] = keyword_hash; - } - /* block arguments */ if (iseq->arg_block != -1) { VALUE blockval = Qnil; @@ -2085,6 +2092,11 @@ vm_yield_setup_block_args(rb_thread_t *t https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L2092 th->mark_stack_len = argc; + /* keyword argument */ + if (iseq->arg_keyword != -1) { + argc = vm_callee_setup_keyword_arg(iseq, argc, argv); + } + /* * yield [1, 2] * => {|a|} => a = [1, 2] Index: test/ruby/test_keyword.rb =================================================================== --- test/ruby/test_keyword.rb (revision 38656) +++ test/ruby/test_keyword.rb (revision 38657) @@ -135,4 +135,123 @@ class TestKeywordArguments < Test::Unit: https://github.com/ruby/ruby/blob/trunk/test/ruby/test_keyword.rb#L135 assert_equal(["foo", 111111], f[num: 111111]) assert_equal(["bar", 111111], f[str: "bar", num: 111111]) end + + + def p1 + Proc.new do |str: "foo", num: 424242| + [str, num] + end + end + + def test_p1 + assert_equal(["foo", 424242], p1[]) + assert_equal(["bar", 424242], p1[str: "bar"]) + assert_equal(["foo", 111111], p1[num: 111111]) + assert_equal(["bar", 111111], p1[str: "bar", num: 111111]) + assert_raise(ArgumentError) { p1[str: "bar", check: true] } + assert_equal(["foo", 424242], p1["string"] ) + end + + + def p2 + Proc.new do |x, str: "foo", num: 424242| + [x, str, num] + end + end + + def test_p2 + assert_equal([nil, "foo", 424242], p2[]) + assert_equal([:xyz, "foo", 424242], p2[:xyz]) + end + + + def p3 + Proc.new do |str: "foo", num: 424242, **h| + [str, num, h] + end + end + + def test_p3 + assert_equal(["foo", 424242, {}], p3[]) + assert_equal(["bar", 424242, {}], p3[str: "bar"]) + assert_equal(["foo", 111111, {}], p3[num: 111111]) + assert_equal(["bar", 111111, {}], p3[str: "bar", num: 111111]) + assert_equal(["bar", 424242, {:check=>true}], p3[str: "bar", check: true]) + assert_equal(["foo", 424242, {}], p3["string"]) + end + + + def p4 + Proc.new do |str: "foo", num: 424242, **h, &blk| + [str, num, h, blk] + end + end + + def test_p4 + assert_equal(["foo", 424242, {}, nil], p4[]) + assert_equal(["bar", 424242, {}, nil], p4[str: "bar"]) + assert_equal(["foo", 111111, {}, nil], p4[num: 111111]) + assert_equal(["bar", 111111, {}, nil], p4[str: "bar", num: 111111]) + assert_equal(["bar", 424242, {:check=>true}, nil], p4[str: "bar", check: true]) + a = p4.call {|x| x + 42 } + assert_equal(["foo", 424242, {}], a[0, 3]) + assert_equal(43, a.last.call(1)) + end + + + def p5 + Proc.new do |*r, str: "foo", num: 424242, **h| + [r, str, num, h] + end + end + + def test_p5 + assert_equal([[], "foo", 424242, {}], p5[]) + assert_equal([[], "bar", 424242, {}], p5[str: "bar"]) + assert_equal([[], "foo", 111111, {}], p5[num: 111111]) + assert_equal([[], "bar", 111111, {}], p5[str: "bar", num: 111111]) + assert_equal([[1], "foo", 424242, {}], p5[1]) + assert_equal([[1, 2], "foo", 424242, {}], p5[1, 2]) + assert_equal([[1, 2, 3], "foo", 424242, {}], p5[1, 2, 3]) + assert_equal([[1], "bar", 424242, {}], p5[1, str: "bar"]) + assert_equal([[1, 2], "bar", 424242, {}], p5[1, 2, str: "bar"]) + assert_equal([[1, 2, 3], "bar", 424242, {}], p5[1, 2, 3, str: "bar"]) + end + + + def p6 + Proc.new do |o1, o2=42, *args, p, k: :key, **kw, &b| + [o1, o2, args, p, k, kw, b] + end + end + + def test_p6 + assert_equal([nil, 42, [], nil, :key, {}, nil], p6[]) + assert_equal([1, 42, [], 2, :key, {}, nil], p6[1, 2]) + assert_equal([1, 2, [], 3, :key, {}, nil], p6[1, 2, 3]) + assert_equal([1, 2, [3], 4, :key, {}, nil], p6[1, 2, 3, 4]) + assert_equal([1, 2, [3, 4], 5, :key, {str: "bar"}, nil], p6[1, 2, 3, 4, 5, str: "bar"]) + end + + def test_proc_parameters + assert_equal([[:key, :str], [:key, :num]], p1.parameters); + assert_equal([[:opt, :x], [:key, :str], [:key, :num]], p2.parameters); + assert_equal([[:key, :str], [:key, :num], [:keyrest, :h]], p3.parameters); + assert_equal([[:key, :str], [:key, :num], [:keyrest, :h], [:block, :blk]], p4.parameters); + assert_equal([[:rest, :r], [:key, :str], [:key, :num], [:keyrest, :h]], p5.parameters); + assert_equal([[:opt, :o1], [:opt, :o2], [:rest, :args], [:opt, :p], [:key, :k], + [:keyrest, :kw], [:block, :b]], p6.parameters) + end + + def m1(*args) + yield *args + end + + def test_block + blk = Proc.new {|str: "foo", num: 424242| [str, num] } + assert_equal(["foo", 424242], m1(&blk)) + assert_equal(["bar", 424242], m1(str: "bar", &blk)) + assert_equal(["foo", 111111], m1(num: 111111, &blk)) + assert_equal(["bar", 111111], m1(str: "bar", num: 111111, &blk)) + end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/