mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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
This commit is contained in:
parent
a0a2c144b8
commit
d0baa0dd52
6 changed files with 43 additions and 12 deletions
10
ChangeLog
10
ChangeLog
|
@ -1,3 +1,13 @@
|
|||
Mon Dec 26 22:31:07 2011 Yusuke Endoh <mame@tsg.ne.jp>
|
||||
|
||||
* 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 <mame@tsg.ne.jp>
|
||||
|
||||
* vm_core.h (struct rb_iseq_struct), compile.c (iseq_set_arguments,
|
||||
|
|
15
compile.c
15
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);
|
||||
}
|
||||
|
|
18
iseq.c
18
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));
|
||||
|
|
|
@ -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] }
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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++;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue