From d0baa0dd52fb1d72e23633bb2ba5a551957a4776 Mon Sep 17 00:00:00 2001 From: mame Date: Mon, 26 Dec 2011 14:20:15 +0000 Subject: [PATCH] * vm_core.h (struct rb_iseq_struct), compile.c (iseq_set_arguments), iseq.c (rb_iseq_parameters), vm_insnhelper.c (vm_callee_setup_arg_complex): support Method#parameters for keyword arguments. The provisional spec is what Benoit Daloze proposed. [ruby-core:40518] * test/ruby/test_keyword.rb: add a test for above. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34137 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 10 ++++++++++ compile.c | 15 +++++---------- iseq.c | 18 +++++++++++++++++- test/ruby/test_keyword.rb | 9 +++++++++ vm_core.h | 1 + vm_insnhelper.c | 2 +- 6 files changed, 43 insertions(+), 12 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8ec1bcd6c4..6c908f8d33 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +Mon Dec 26 22:31:07 2011 Yusuke Endoh + + * vm_core.h (struct rb_iseq_struct), compile.c (iseq_set_arguments), + iseq.c (rb_iseq_parameters), vm_insnhelper.c + (vm_callee_setup_arg_complex): support Method#parameters for keyword + arguments. The provisional spec is what Benoit Daloze proposed. + [ruby-core:40518] + + * test/ruby/test_keyword.rb: add a test for above. + Mon Dec 26 22:15:27 2011 Yusuke Endoh * vm_core.h (struct rb_iseq_struct), compile.c (iseq_set_arguments, diff --git a/compile.c b/compile.c index 31d9ad2d9b..7fc14d590f 100644 --- a/compile.c +++ b/compile.c @@ -1140,16 +1140,11 @@ iseq_set_arguments(rb_iseq_t *iseq, LINK_ANCHOR *optargs, NODE *node_args) node = node->nd_next; i += 1; } - if ((args->kw_rest_arg->nd_vid & ID_SCOPE_MASK) == ID_JUNK) { - iseq->arg_keywords = i; - iseq->arg_keyword_table = ALLOC_N(ID, i); - for (j = 0; j < i; j++) { - iseq->arg_keyword_table[j] = FIX2INT(RARRAY_PTR(keywords)[j]); - } - } - else { - iseq->arg_keywords = 0; - iseq->arg_keyword_table = 0; + iseq->arg_keyword_check = (args->kw_rest_arg->nd_vid & ID_SCOPE_MASK) == ID_JUNK; + iseq->arg_keywords = i; + iseq->arg_keyword_table = ALLOC_N(ID, i); + for (j = 0; j < i; j++) { + iseq->arg_keyword_table[j] = FIX2INT(RARRAY_PTR(keywords)[j]); } ADD_INSN(optargs, nd_line(args->kw_args), pop); } diff --git a/iseq.c b/iseq.c index 279386e1a7..62f26d5ee7 100644 --- a/iseq.c +++ b/iseq.c @@ -1386,7 +1386,7 @@ rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc) { int i, r; VALUE a, args = rb_ary_new2(iseq->arg_size); - ID req, opt, rest, block; + ID req, opt, rest, block, key, keyrest; #define PARAM_TYPE(type) rb_ary_push(a = rb_ary_new2(2), ID2SYM(type)) #define PARAM_ID(i) iseq->local_table[(i)] #define PARAM(i, type) ( \ @@ -1412,7 +1412,9 @@ rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc) r = iseq->arg_rest != -1 ? iseq->arg_rest : iseq->arg_post_len > 0 ? iseq->arg_post_start : iseq->arg_block != -1 ? iseq->arg_block : + iseq->arg_keyword != -1 ? iseq->arg_keyword : iseq->arg_size; + if (iseq->arg_keyword != -1) r -= iseq->arg_keywords; for (; i < r; i++) { PARAM_TYPE(opt); if (rb_id2name(PARAM_ID(i))) { @@ -1437,6 +1439,20 @@ rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc) rb_ary_push(args, PARAM(i, req)); } } + if (iseq->arg_keyword != -1) { + CONST_ID(key, "key"); + for (i = 0; i < iseq->arg_keywords; i++) { + PARAM_TYPE(key); + if (rb_id2name(iseq->arg_keyword_table[i])) { + rb_ary_push(a, ID2SYM(iseq->arg_keyword_table[i])); + } + rb_ary_push(args, a); + } + if (rb_id2name(iseq->local_table[iseq->arg_keyword])) { + CONST_ID(keyrest, "keyrest"); + rb_ary_push(args, PARAM(iseq->arg_keyword, keyrest)); + } + } if (iseq->arg_block != -1) { CONST_ID(block, "block"); rb_ary_push(args, PARAM(iseq->arg_block, block)); diff --git a/test/ruby/test_keyword.rb b/test/ruby/test_keyword.rb index 27edc6b8fd..9c94069d6f 100644 --- a/test/ruby/test_keyword.rb +++ b/test/ruby/test_keyword.rb @@ -94,6 +94,15 @@ class TestKeywordArguments < Test::Unit::TestCase assert_equal([[1, 2, 3], "bar", 424242, {}], f7(1, 2, 3, str: "bar")) end + def test_method_parameters + assert_equal([[:key, :str], [:key, :num]], method(:f1).parameters); + assert_equal([[:req, :x], [:key, :str], [:key, :num]], method(:f2).parameters); + assert_equal([[:key, :str], [:key, :num], [:keyrest, :h]], method(:f3).parameters); + assert_equal([[:key, :str], [:key, :num]], method(:f4).parameters); + assert_equal([[:key, :str], [:key, :num], [:keyrest, :h]], method(:f5).parameters); + assert_equal([[:key, :str], [:key, :num], [:keyrest, :h], [:block, :blk]], method(:f6).parameters); + assert_equal([[:rest, :r], [:key, :str], [:key, :num], [:keyrest, :h]], method(:f7).parameters); + end def test_lambda f = ->(str: "foo", num: 424242) { [str, num] } diff --git a/vm_core.h b/vm_core.h index df186a0b58..c22621864e 100644 --- a/vm_core.h +++ b/vm_core.h @@ -221,6 +221,7 @@ struct rb_iseq_struct { int arg_size; VALUE *arg_opt_table; int arg_keyword; + int arg_keyword_check; /* if this is true, raise an ArgumentError when unknown keyword argument is passed */ int arg_keywords; ID *arg_keyword_table; diff --git a/vm_insnhelper.c b/vm_insnhelper.c index 9a22741cc3..5ab1eac1ea 100644 --- a/vm_insnhelper.c +++ b/vm_insnhelper.c @@ -172,7 +172,7 @@ vm_callee_setup_arg_complex(rb_thread_t *th, const rb_iseq_t * iseq, if (!NIL_P(keyword_hash)) { argc--; keyword_hash = rb_hash_dup(keyword_hash); - if (iseq->arg_keywords) { + 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++; }