зеркало из https://github.com/github/ruby.git
class.c, vm_insnhelper.c: check unknown keywords
* class.c (rb_get_kwargs): if optional is negative, unknown keywords are allowed. * vm_insnhelper.c (vm_callee_setup_keyword_arg): check unknown keywords. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44069 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
Родитель
72f60a2ff1
Коммит
dd8710d243
|
@ -1,3 +1,11 @@
|
||||||
|
Sun Dec 8 16:19:28 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* class.c (rb_get_kwargs): if optional is negative, unknown
|
||||||
|
keywords are allowed.
|
||||||
|
|
||||||
|
* vm_insnhelper.c (vm_callee_setup_keyword_arg): check unknown
|
||||||
|
keywords.
|
||||||
|
|
||||||
Sun Dec 8 14:55:12 2013 Kazuki Tsujimoto <kazuki@callcc.net>
|
Sun Dec 8 14:55:12 2013 Kazuki Tsujimoto <kazuki@callcc.net>
|
||||||
|
|
||||||
* array.c (rb_ary_shuffle_bang, rb_ary_sample): rename local variables.
|
* array.c (rb_ary_shuffle_bang, rb_ary_sample): rename local variables.
|
||||||
|
|
7
class.c
7
class.c
|
@ -1912,8 +1912,13 @@ int
|
||||||
rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
|
rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
|
||||||
{
|
{
|
||||||
int i = 0, j;
|
int i = 0, j;
|
||||||
|
int rest = 0;
|
||||||
VALUE missing = Qnil;
|
VALUE missing = Qnil;
|
||||||
|
|
||||||
|
if (optional < 0) {
|
||||||
|
rest = 1;
|
||||||
|
optional = -1-optional;
|
||||||
|
}
|
||||||
if (values) {
|
if (values) {
|
||||||
for (j = 0; j < required + optional; j++) {
|
for (j = 0; j < required + optional; j++) {
|
||||||
values[j] = Qundef;
|
values[j] = Qundef;
|
||||||
|
@ -1945,6 +1950,8 @@ rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, V
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if (!rest && keyword_hash) {
|
||||||
if (RHASH_SIZE(keyword_hash) > (unsigned int)j) {
|
if (RHASH_SIZE(keyword_hash) > (unsigned int)j) {
|
||||||
unknown_keyword_error(keyword_hash, table, required+optional);
|
unknown_keyword_error(keyword_hash, table, required+optional);
|
||||||
}
|
}
|
||||||
|
|
|
@ -306,6 +306,7 @@ class TestKeywordArguments < Test::Unit::TestCase
|
||||||
eval("def o.bar(a:,**b) [a, b]; end")
|
eval("def o.bar(a:,**b) [a, b]; end")
|
||||||
end
|
end
|
||||||
assert_raise_with_message(ArgumentError, /missing keyword/, feature7701) {o.foo}
|
assert_raise_with_message(ArgumentError, /missing keyword/, feature7701) {o.foo}
|
||||||
|
assert_raise_with_message(ArgumentError, /unknown keyword/, feature7701) {o.foo(a:0, b:1)}
|
||||||
assert_equal(42, o.foo(a: 42), feature7701)
|
assert_equal(42, o.foo(a: 42), feature7701)
|
||||||
assert_equal([[:keyreq, :a]], o.method(:foo).parameters, feature7701)
|
assert_equal([[:keyreq, :a]], o.method(:foo).parameters, feature7701)
|
||||||
|
|
||||||
|
@ -323,6 +324,7 @@ class TestKeywordArguments < Test::Unit::TestCase
|
||||||
break eval("proc {|a:| a}")
|
break eval("proc {|a:| a}")
|
||||||
end
|
end
|
||||||
assert_raise_with_message(ArgumentError, /missing keyword/, feature7701) {b.call}
|
assert_raise_with_message(ArgumentError, /missing keyword/, feature7701) {b.call}
|
||||||
|
assert_raise_with_message(ArgumentError, /unknown keyword/, feature7701) {b.call(a:0, b:1)}
|
||||||
assert_equal(42, b.call(a: 42), feature7701)
|
assert_equal(42, b.call(a: 42), feature7701)
|
||||||
assert_equal([[:keyreq, :a]], b.parameters, feature7701)
|
assert_equal([[:keyreq, :a]], b.parameters, feature7701)
|
||||||
|
|
||||||
|
|
|
@ -1067,7 +1067,8 @@ vm_caller_setup_args(const rb_thread_t *th, rb_control_frame_t *cfp, rb_call_inf
|
||||||
static inline int
|
static inline int
|
||||||
vm_callee_setup_keyword_arg(const rb_iseq_t *iseq, int argc, int m, VALUE *orig_argv, VALUE *kwd)
|
vm_callee_setup_keyword_arg(const rb_iseq_t *iseq, int argc, int m, VALUE *orig_argv, VALUE *kwd)
|
||||||
{
|
{
|
||||||
VALUE keyword_hash, orig_hash;
|
VALUE keyword_hash = 0, orig_hash;
|
||||||
|
int optional = iseq->arg_keywords - iseq->arg_keyword_required;
|
||||||
|
|
||||||
if (argc > m &&
|
if (argc > m &&
|
||||||
!NIL_P(orig_hash = rb_check_hash_type(orig_argv[argc-1])) &&
|
!NIL_P(orig_hash = rb_check_hash_type(orig_argv[argc-1])) &&
|
||||||
|
@ -1078,14 +1079,11 @@ vm_callee_setup_keyword_arg(const rb_iseq_t *iseq, int argc, int m, VALUE *orig_
|
||||||
else {
|
else {
|
||||||
orig_argv[argc-1] = orig_hash;
|
orig_argv[argc-1] = orig_hash;
|
||||||
}
|
}
|
||||||
rb_get_kwargs(keyword_hash, iseq->arg_keyword_table, iseq->arg_keyword_required,
|
|
||||||
iseq->arg_keyword_check ? iseq->arg_keywords - iseq->arg_keyword_required : 0,
|
|
||||||
NULL);
|
|
||||||
}
|
}
|
||||||
else if (iseq->arg_keyword_required) {
|
rb_get_kwargs(keyword_hash, iseq->arg_keyword_table, iseq->arg_keyword_required,
|
||||||
rb_get_kwargs(0, iseq->arg_keyword_table, iseq->arg_keyword_required, 0, NULL);
|
(iseq->arg_keyword_check ? optional : -1-optional),
|
||||||
}
|
NULL);
|
||||||
else {
|
if (!keyword_hash) {
|
||||||
keyword_hash = rb_hash_new();
|
keyword_hash = rb_hash_new();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Загрузка…
Ссылка в новой задаче