diff options
author | ktsj <ktsj@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-12-30 02:06:28 +0000 |
---|---|---|
committer | ktsj <ktsj@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-12-30 02:06:28 +0000 |
commit | 371c07f2a2ce59ff9a045181b997826b3da3ef8a (patch) | |
tree | 88071fd4b0226109772dafc1ce71b2019735a21b | |
parent | b0767cf8c03ad9a656dec63712a8b3f8cf00394f (diff) | |
download | ruby-371c07f2a2ce59ff9a045181b997826b3da3ef8a.tar.gz |
* vm_insnhelper.c: set keyword hash on Proc/block calls.
[ruby-core:51172] [Bug #7630]
* test/ruby/test_keyword.rb: add tests for above.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38657 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | test/ruby/test_keyword.rb | 119 | ||||
-rw-r--r-- | vm_insnhelper.c | 58 |
3 files changed, 161 insertions, 23 deletions
@@ -1,3 +1,10 @@ +Sun Dec 30 10:51:29 2012 Kazuki Tsujimoto <kazuki@callcc.net> + + * 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@ishitsuka.com> * lib/irb/completion.rb: treat rightly comletion for symbol on irb diff --git a/test/ruby/test_keyword.rb b/test/ruby/test_keyword.rb index b9a4afc8dd..e9d82f8899 100644 --- a/test/ruby/test_keyword.rb +++ b/test/ruby/test_keyword.rb @@ -135,4 +135,123 @@ class TestKeywordArguments < Test::Unit::TestCase 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 diff --git a/vm_insnhelper.c b/vm_insnhelper.c index 144fc1fb4a..d6cc9f90ef 100644 --- a/vm_insnhelper.c +++ b/vm_insnhelper.c @@ -1065,6 +1065,34 @@ vm_caller_setup_args(const rb_thread_t *th, rb_control_frame_t *cfp, rb_call_inf } 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 *th, rb_call_info_t *ci, const rb_iseq_t 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 *th, rb_call_info_t *ci, const rb_iseq_t 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 *th, const rb_iseq_t * iseq, 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] |