[前][次][番号順一覧][スレッド一覧]

ruby-changes:31956

From: nobu <ko1@a...>
Date: Fri, 6 Dec 2013 17:10:53 +0900 (JST)
Subject: [ruby-changes:31956] nobu:r44035 (trunk): vm_insnhelper.c: rb_get_kwargs

nobu	2013-12-06 17:10:47 +0900 (Fri, 06 Dec 2013)

  New Revision: 44035

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=44035

  Log:
    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.

  Modified files:
    trunk/ChangeLog
    trunk/dir.c
    trunk/gc.c
    trunk/internal.h
    trunk/vm_insnhelper.c
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 44034)
+++ ChangeLog	(revision 44035)
@@ -1,3 +1,12 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Dec  6 17:10:44 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* 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@r...>
 
 	* misc/ruby-mode.el (ruby-brace-to-do-end): split single line block.
Index: dir.c
===================================================================
--- dir.c	(revision 44034)
+++ dir.c	(revision 44035)
@@ -436,9 +436,8 @@ dir_initialize(int argc, VALUE *argv, VA https://github.com/ruby/ruby/blob/trunk/dir.c#L436
 
     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);
 	}
     }
Index: gc.c
===================================================================
--- gc.c	(revision 44034)
+++ gc.c	(revision 44035)
@@ -5014,26 +5014,24 @@ gc_start_internal(int argc, VALUE *argv, https://github.com/ruby/ruby/blob/trunk/gc.c#L5014
     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);
Index: internal.h
===================================================================
--- internal.h	(revision 44034)
+++ internal.h	(revision 44035)
@@ -775,7 +775,7 @@ VALUE rb_catch_protect(VALUE t, rb_block https://github.com/ruby/ruby/blob/trunk/internal.h#L775
 
 /* 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 */
Index: vm_insnhelper.c
===================================================================
--- vm_insnhelper.c	(revision 44034)
+++ vm_insnhelper.c	(revision 44035)
@@ -1113,17 +1113,27 @@ rb_extract_keywords(VALUE *orighash) https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L1113
     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_h https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L1143
     }
     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_ise https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L1170
 	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();

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]