vm_insnhelper.c: rb_get_kwargs

* vm_insnhelper.c (rb_get_kwargs): get keyword argument values from an
  option hash, not only checking keys.
* dir.c (dir_initialize): use rb_get_kwargs.
* gc.c (gc_start_internal): ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44035 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2013-12-06 08:10:47 +00:00
Родитель c7572f2fe7
Коммит b9cafaf524
5 изменённых файлов: 43 добавлений и 21 удалений

Просмотреть файл

@ -1,3 +1,12 @@
Fri Dec 6 17:10:44 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
* vm_insnhelper.c (rb_get_kwargs): get keyword argument values from an
option hash, not only checking keys.
* dir.c (dir_initialize): use rb_get_kwargs.
* gc.c (gc_start_internal): ditto.
Fri Dec 6 16:47:45 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
* misc/ruby-mode.el (ruby-brace-to-do-end): split single line block.

5
dir.c
Просмотреть файл

@ -436,9 +436,8 @@ dir_initialize(int argc, VALUE *argv, VALUE dir)
if (!NIL_P(opt)) {
VALUE enc;
rb_check_keyword_opthash(opt, keyword_ids, 0, 1);
enc = rb_hash_aref(opt, ID2SYM(keyword_ids[0]));
if (!NIL_P(enc)) {
rb_get_kwargs(opt, keyword_ids, 0, 1, &enc);
if (enc != Qundef && !NIL_P(enc)) {
fsenc = rb_to_encoding(enc);
}
}

16
gc.c
Просмотреть файл

@ -5014,26 +5014,24 @@ gc_start_internal(int argc, VALUE *argv, VALUE self)
rb_objspace_t *objspace = &rb_objspace;
int full_mark = TRUE, immediate_sweep = TRUE;
VALUE opt = Qnil;
VALUE kwval;
static ID keyword_ids[2];
static VALUE keyword_syms[2];
rb_scan_args(argc, argv, "0:", &opt);
if (!NIL_P(opt)) {
VALUE kwvals[2];
if (!keyword_ids[0]) {
keyword_ids[0] = rb_intern("full_mark");
keyword_ids[1] = rb_intern("immediate_sweep");
keyword_syms[0] = ID2SYM(keyword_ids[0]);
keyword_syms[1] = ID2SYM(keyword_ids[1]);
}
rb_check_keyword_opthash(opt, keyword_ids, 0, 2);
rb_get_kwargs(opt, keyword_ids, 0, 2, kwvals);
if ((kwval = rb_hash_lookup2(opt, keyword_syms[0], Qundef)) != Qundef)
full_mark = RTEST(kwval);
if ((kwval = rb_hash_lookup2(opt, keyword_syms[1], Qundef)) != Qundef)
immediate_sweep = RTEST(kwval);
if (kwvals[0] != Qundef)
full_mark = RTEST(kwvals[0]);
if (kwvals[1] != Qundef)
immediate_sweep = RTEST(kwvals[1]);
}
garbage_collect(objspace, full_mark, immediate_sweep, GPR_FLAG_METHOD);

Просмотреть файл

@ -775,7 +775,7 @@ VALUE rb_catch_protect(VALUE t, rb_block_call_func *func, VALUE data, int *state
/* vm_insnhelper.c */
VALUE rb_equal_opt(VALUE obj1, VALUE obj2);
void rb_check_keyword_opthash(VALUE keyword_hash, const ID *table, int required, int optional);
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *);
VALUE rb_extract_keywords(VALUE *orighash);
/* vm_method.c */

Просмотреть файл

@ -1113,17 +1113,27 @@ rb_extract_keywords(VALUE *orighash)
return parthash[0];
}
void
rb_check_keyword_opthash(VALUE keyword_hash, const ID *table, int required, int optional)
int
rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
{
int i = 0, j;
VALUE missing = Qnil;
if (values) {
for (j = 0; j < required + optional; j++) {
values[j] = Qundef;
}
}
if (required) {
for (; i < required; i++) {
VALUE keyword = ID2SYM(table[i]);
if (keyword_hash && st_lookup(rb_hash_tbl_raw(keyword_hash), (st_data_t)keyword, 0))
continue;
if (keyword_hash) {
st_data_t val;
if (st_lookup(rb_hash_tbl_raw(keyword_hash), (st_data_t)keyword, &val)) {
if (values) values[i] = (VALUE)val;
continue;
}
}
if (NIL_P(missing)) missing = rb_ary_tmp_new(1);
rb_ary_push(missing, keyword);
}
@ -1133,12 +1143,17 @@ rb_check_keyword_opthash(VALUE keyword_hash, const ID *table, int required, int
}
if (optional && keyword_hash) {
for (j = i, i = 0; i < optional; i++) {
if (st_lookup(rb_hash_tbl_raw(keyword_hash), ID2SYM(table[required+i]), 0)) j++;
st_data_t val;
if (st_lookup(rb_hash_tbl_raw(keyword_hash), ID2SYM(table[required+i]), &val)) {
if (values) values[required+i] = (VALUE)val;
j++;
}
}
if (RHASH_SIZE(keyword_hash) > (unsigned int)j) {
unknown_keyword_error(keyword_hash, table, required+optional);
}
}
return j;
}
static inline int
@ -1155,11 +1170,12 @@ vm_callee_setup_keyword_arg(const rb_iseq_t *iseq, int argc, int m, VALUE *orig_
else {
orig_argv[argc-1] = orig_hash;
}
rb_check_keyword_opthash(keyword_hash, iseq->arg_keyword_table, iseq->arg_keyword_required,
iseq->arg_keyword_check ? iseq->arg_keywords - iseq->arg_keyword_required : 0);
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_check_keyword_opthash(0, iseq->arg_keyword_table, iseq->arg_keyword_required, 0);
rb_get_kwargs(0, iseq->arg_keyword_table, iseq->arg_keyword_required, 0, NULL);
}
else {
keyword_hash = rb_hash_new();