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

ruby-changes:34977

From: normal <ko1@a...>
Date: Mon, 4 Aug 2014 10:12:58 +0900 (JST)
Subject: [ruby-changes:34977] normal:r47059 (trunk): variable: cleanup to use rb_const_lookup

normal	2014-08-04 10:12:53 +0900 (Mon, 04 Aug 2014)

  New Revision: 47059

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

  Log:
    variable: cleanup to use rb_const_lookup
    
    * variable.c: cleanup to use rb_const_lookup
    * vm_insnshelper.c: ditto
    
    This reduces casting and long lines.  This should make it easier to
    switch to alternatives to st for constant storage.

  Modified files:
    trunk/ChangeLog
    trunk/constant.h
    trunk/variable.c
    trunk/vm_insnhelper.c
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 47058)
+++ ChangeLog	(revision 47059)
@@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Mon Aug  4 09:12:47 2014  Eric Wong  <e@8...>
+
+	* variable.c: cleanup to use rb_const_lookup
+	  [Feature #10107]
+
+	* vm_insnshelper.c: ditto
+
 Sun Aug  3 10:55:07 2014  Nobuyoshi Nakada  <nobu@r...>
 
 	* include/ruby/encoding.h (rb_check_symbol_cstr): ditto.
Index: variable.c
===================================================================
--- variable.c	(revision 47058)
+++ variable.c	(revision 47059)
@@ -1611,6 +1611,7 @@ rb_autoload(VALUE mod, ID id, const char https://github.com/ruby/ruby/blob/trunk/variable.c#L1611
     VALUE ad, fn;
     struct st_table *tbl;
     struct autoload_data_i *ele;
+    rb_const_entry_t *ce;
 
     if (!rb_is_const_id(id)) {
 	rb_raise(rb_eNameError, "autoload must be constant name: %"PRIsVALUE"",
@@ -1620,8 +1621,10 @@ rb_autoload(VALUE mod, ID id, const char https://github.com/ruby/ruby/blob/trunk/variable.c#L1621
 	rb_raise(rb_eArgError, "empty file name");
     }
 
-    if ((tbl = RCLASS_CONST_TBL(mod)) && st_lookup(tbl, (st_data_t)id, &av) && ((rb_const_entry_t*)av)->value != Qundef)
+    ce = rb_const_lookup(mod, id);
+    if (ce && ce->value != Qundef) {
 	return;
+    }
 
     rb_const_set(mod, id, Qundef);
     tbl = RCLASS_IV_TBL(mod);
@@ -1735,10 +1738,9 @@ rb_autoloading_value(VALUE mod, ID id, V https://github.com/ruby/ruby/blob/trunk/variable.c#L1738
 static int
 autoload_defined_p(VALUE mod, ID id)
 {
-    struct st_table *tbl = RCLASS_CONST_TBL(mod);
-    st_data_t val;
+    rb_const_entry_t *ce = rb_const_lookup(mod, id);
 
-    if (!tbl || !st_lookup(tbl, (st_data_t)id, &val) || ((rb_const_entry_t*)val)->value != Qundef) {
+    if (!ce || ce->value != Qundef) {
 	return 0;
     }
     return !rb_autoloading_value(mod, id, NULL);
@@ -1836,9 +1838,9 @@ rb_const_get_0(VALUE klass, ID id, int e https://github.com/ruby/ruby/blob/trunk/variable.c#L1838
   retry:
     while (RTEST(tmp)) {
 	VALUE am = 0;
-	st_data_t data;
-	while (RCLASS_CONST_TBL(tmp) && st_lookup(RCLASS_CONST_TBL(tmp), (st_data_t)id, &data)) {
-	    rb_const_entry_t *ce = (rb_const_entry_t *)data;
+	rb_const_entry_t *ce;
+
+	while ((ce = rb_const_lookup(tmp, id))) {
 	    if (visibility && ce->flag == CONST_PRIVATE) {
 		rb_name_error(id, "private constant %"PRIsVALUE"::%"PRIsVALUE" referenced",
 			      rb_class_name(klass), QUOTE_ID(id));
@@ -2088,15 +2090,14 @@ rb_mod_constants(int argc, const VALUE * https://github.com/ruby/ruby/blob/trunk/variable.c#L2090
 static int
 rb_const_defined_0(VALUE klass, ID id, int exclude, int recurse, int visibility)
 {
-    st_data_t value;
     VALUE tmp;
     int mod_retry = 0;
+    rb_const_entry_t *ce;
 
     tmp = klass;
   retry:
     while (tmp) {
-	if (RCLASS_CONST_TBL(tmp) && st_lookup(RCLASS_CONST_TBL(tmp), (st_data_t)id, &value)) {
-	    rb_const_entry_t *ce = (rb_const_entry_t *)value;
+	if ((ce = rb_const_lookup(tmp, id))) {
 	    if (visibility && ce->flag == CONST_PRIVATE) {
 		return (int)Qfalse;
 	    }
@@ -2173,10 +2174,8 @@ rb_const_set(VALUE klass, ID id, VALUE v https://github.com/ruby/ruby/blob/trunk/variable.c#L2174
 	RCLASS_CONST_TBL(klass) = st_init_numtable();
     }
     else {
-	st_data_t value;
-
-	if (st_lookup(RCLASS_CONST_TBL(klass), (st_data_t)id, &value)) {
-	    rb_const_entry_t *ce = (rb_const_entry_t*)value;
+	ce = rb_const_lookup(klass, id);
+	if (ce) {
 	    if (ce->value == Qundef) {
 		VALUE load;
 		struct autoload_data_i *ele;
@@ -2242,7 +2241,7 @@ static void https://github.com/ruby/ruby/blob/trunk/variable.c#L2241
 set_const_visibility(VALUE mod, int argc, const VALUE *argv, rb_const_flag_t flag)
 {
     int i;
-    st_data_t v;
+    rb_const_entry_t *ce;
     ID id;
 
     if (argc == 0) {
@@ -2262,9 +2261,8 @@ set_const_visibility(VALUE mod, int argc https://github.com/ruby/ruby/blob/trunk/variable.c#L2261
 	    rb_name_error_str(val, "constant %"PRIsVALUE"::%"PRIsVALUE" not defined",
 			      rb_class_name(mod), QUOTE(val));
 	}
-	if (RCLASS_CONST_TBL(mod) &&
-	    st_lookup(RCLASS_CONST_TBL(mod), (st_data_t)id, &v)) {
-	    ((rb_const_entry_t*)v)->flag = flag;
+	if ((ce = rb_const_lookup(mod, id))) {
+	    ce->flag = flag;
 	}
 	else {
 	    if (i > 0) {
@@ -2636,3 +2634,15 @@ rb_st_copy(VALUE obj, struct st_table *o https://github.com/ruby/ruby/blob/trunk/variable.c#L2634
     st_foreach(new_tbl, tbl_copy_i, (st_data_t)obj);
     return new_tbl;
 }
+
+rb_const_entry_t *
+rb_const_lookup(VALUE klass, ID id)
+{
+    st_table *tbl = RCLASS_CONST_TBL(klass);
+    st_data_t val;
+
+    if (tbl && st_lookup(tbl, (st_data_t)id, &val)) {
+	return (rb_const_entry_t *)val;
+    }
+    return 0;
+}
Index: vm_insnhelper.c
===================================================================
--- vm_insnhelper.c	(revision 47058)
+++ vm_insnhelper.c	(revision 47059)
@@ -394,11 +394,10 @@ vm_get_ev_const(rb_thread_t *th, const r https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L394
 
 	    if (!NIL_P(klass)) {
 		VALUE av, am = 0;
-		st_data_t data;
+		rb_const_entry_t *ce;
 	      search_continue:
-		if (RCLASS_CONST_TBL(klass) &&
-		    st_lookup(RCLASS_CONST_TBL(klass), id, &data)) {
-		    val = ((rb_const_entry_t*)data)->value;
+		if ((ce = rb_const_lookup(klass, id))) {
+		    val = ce->value;
 		    if (val == Qundef) {
 			if (am == klass) break;
 			am = klass;
Index: constant.h
===================================================================
--- constant.h	(revision 47058)
+++ constant.h	(revision 47059)
@@ -32,5 +32,6 @@ VALUE rb_public_const_get_from(VALUE kla https://github.com/ruby/ruby/blob/trunk/constant.h#L32
 int rb_public_const_defined(VALUE klass, ID id);
 int rb_public_const_defined_at(VALUE klass, ID id);
 int rb_public_const_defined_from(VALUE klass, ID id);
+rb_const_entry_t *rb_const_lookup(VALUE klass, ID id);
 
 #endif /* CONSTANT_H */

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

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