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

ruby-changes:34905

From: nobu <ko1@a...>
Date: Mon, 28 Jul 2014 17:15:56 +0900 (JST)
Subject: [ruby-changes:34905] nobu:r46988 (trunk): symbols instead of IDs

nobu	2014-07-28 17:15:42 +0900 (Mon, 28 Jul 2014)

  New Revision: 46988

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

  Log:
    symbols instead of IDs
    
    * encoding.c (rb_enc_get_index): deal with symbols instead of IDs
      to get rid of inadvertent pin-downs.
    
    * enum.c (chunk_ii): ditto.
    
    * enumerator.c (append_method): ditto.
    
    * iseq.c (iseq_load): ditto.
    
    * marshal.c (w_symbol, r_symlink, r_symreal, r_symbol): ditto.
    
    * signal.c (trap_handler): ditto.

  Modified files:
    trunk/encoding.c
    trunk/enum.c
    trunk/enumerator.c
    trunk/iseq.c
    trunk/marshal.c
    trunk/signal.c
Index: encoding.c
===================================================================
--- encoding.c	(revision 46987)
+++ encoding.c	(revision 46988)
@@ -767,7 +767,7 @@ rb_enc_get_index(VALUE obj) https://github.com/ruby/ruby/blob/trunk/encoding.c#L767
 
     if (SPECIAL_CONST_P(obj)) {
 	if (!SYMBOL_P(obj)) return -1;
-	obj = rb_id2str(SYM2ID(obj));
+	obj = rb_sym2str(obj);
     }
     switch (BUILTIN_TYPE(obj)) {
       as_default:
Index: enumerator.c
===================================================================
--- enumerator.c	(revision 46987)
+++ enumerator.c	(revision 46988)
@@ -975,13 +975,15 @@ append_method(VALUE obj, VALUE str, ID d https://github.com/ruby/ruby/blob/trunk/enumerator.c#L975
 
     method = rb_attr_get(obj, id_method);
     if (method != Qfalse) {
-	ID mid = default_method;
 	if (!NIL_P(method)) {
 	    Check_Type(method, T_SYMBOL);
-	    mid = SYM2ID(method);
+	    method = rb_sym2str(method);
+	}
+	else {
+	    method = rb_id2str(default_method);
 	}
 	rb_str_buf_cat2(str, ":");
-	rb_str_buf_append(str, rb_id2str(mid));
+	rb_str_buf_append(str, method);
     }
 
     eargs = rb_attr_get(obj, id_arguments);
Index: iseq.c
===================================================================
--- iseq.c	(revision 46987)
+++ iseq.c	(revision 46988)
@@ -475,17 +475,29 @@ rb_iseq_new_with_bopt(NODE *node, VALUE https://github.com/ruby/ruby/blob/trunk/iseq.c#L475
 static inline VALUE CHECK_INTEGER(VALUE v) {(void)NUM2LONG(v); return v;}
 
 static enum iseq_type
-iseq_type_from_id(const ID typeid)
+iseq_type_from_sym(VALUE type)
 {
-    if (typeid == rb_intern("top")) return ISEQ_TYPE_TOP;
-    if (typeid == rb_intern("method")) return ISEQ_TYPE_METHOD;
-    if (typeid == rb_intern("block")) return ISEQ_TYPE_BLOCK;
-    if (typeid == rb_intern("class")) return ISEQ_TYPE_CLASS;
-    if (typeid == rb_intern("rescue")) return ISEQ_TYPE_RESCUE;
-    if (typeid == rb_intern("ensure")) return ISEQ_TYPE_ENSURE;
-    if (typeid == rb_intern("eval")) return ISEQ_TYPE_EVAL;
-    if (typeid == rb_intern("main")) return ISEQ_TYPE_MAIN;
-    if (typeid == rb_intern("defined_guard")) return ISEQ_TYPE_DEFINED_GUARD;
+    const ID id_top = rb_intern("top");
+    const ID id_method = rb_intern("method");
+    const ID id_block = rb_intern("block");
+    const ID id_class = rb_intern("class");
+    const ID id_rescue = rb_intern("rescue");
+    const ID id_ensure = rb_intern("ensure");
+    const ID id_eval = rb_intern("eval");
+    const ID id_main = rb_intern("main");
+    const ID id_defined_guard = rb_intern("defined_guard");
+    /* ensure all symbols are static or pinned down before
+     * conversion */
+    const ID typeid = rb_check_id(&type);
+    if (typeid == id_top) return ISEQ_TYPE_TOP;
+    if (typeid == id_method) return ISEQ_TYPE_METHOD;
+    if (typeid == id_block) return ISEQ_TYPE_BLOCK;
+    if (typeid == id_class) return ISEQ_TYPE_CLASS;
+    if (typeid == id_rescue) return ISEQ_TYPE_RESCUE;
+    if (typeid == id_ensure) return ISEQ_TYPE_ENSURE;
+    if (typeid == id_eval) return ISEQ_TYPE_EVAL;
+    if (typeid == id_main) return ISEQ_TYPE_MAIN;
+    if (typeid == id_defined_guard) return ISEQ_TYPE_DEFINED_GUARD;
     return (enum iseq_type)-1;
 }
 
@@ -499,7 +511,6 @@ iseq_load(VALUE self, VALUE data, VALUE https://github.com/ruby/ruby/blob/trunk/iseq.c#L511
     VALUE type, body, locals, args, exception;
 
     st_data_t iseq_type;
-    ID typeid;
     rb_iseq_t *iseq;
     rb_compile_option_t option;
     int i = 0;
@@ -539,14 +550,9 @@ iseq_load(VALUE self, VALUE data, VALUE https://github.com/ruby/ruby/blob/trunk/iseq.c#L550
     iseq->self = iseqval;
     iseq->local_iseq = iseq;
 
-    typeid = SYM2ID(type);
-    iseq_type = iseq_type_from_id(typeid);
+    iseq_type = iseq_type_from_sym(type);
     if (iseq_type == (enum iseq_type)-1) {
-	VALUE typename = rb_id2str(typeid);
-	if (typename)
-	    rb_raise(rb_eTypeError, "unsupport type: :%"PRIsVALUE, typename);
-	else
-	    rb_raise(rb_eTypeError, "unsupport type: %p", (void *)typeid);
+	rb_raise(rb_eTypeError, "unsupport type: :%"PRIsVALUE, rb_sym2str(type));
     }
 
     if (parent == Qnil) {
@@ -1615,11 +1621,7 @@ ruby_node_name(int node) https://github.com/ruby/ruby/blob/trunk/iseq.c#L1621
 static VALUE
 register_label(struct st_table *table, unsigned long idx)
 {
-    VALUE sym;
-    char buff[7 + DECIMAL_SIZE_OF_BITS(sizeof(idx) * CHAR_BIT)];
-
-    snprintf(buff, sizeof(buff), "label_%lu", idx);
-    sym = ID2SYM(rb_intern(buff));
+    VALUE sym = rb_str_dynamic_intern(rb_sprintf("label_%lu", idx));
     st_insert(table, idx, sym);
     return sym;
 }
Index: enum.c
===================================================================
--- enum.c	(revision 46987)
+++ enum.c	(revision 46988)
@@ -2679,7 +2679,7 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/enum.c#L2679
 chunk_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, _argp))
 {
     struct chunk_arg *argp = MEMO_FOR(struct chunk_arg, _argp);
-    VALUE v;
+    VALUE v, s;
     VALUE alone = ID2SYM(rb_intern("_alone"));
     VALUE separator = ID2SYM(rb_intern("_separator"));
 
@@ -2703,7 +2703,7 @@ chunk_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, _ https://github.com/ruby/ruby/blob/trunk/enum.c#L2703
             argp->prev_value = argp->prev_elts = Qnil;
         }
     }
-    else if (SYMBOL_P(v) && rb_id2name(SYM2ID(v))[0] == '_') {
+    else if (SYMBOL_P(v) && (s = rb_sym2str(v), RSTRING_PTR(s)[0] == '_')) {
 	rb_raise(rb_eRuntimeError, "symbols beginning with an underscore are reserved");
     }
     else {
Index: marshal.c
===================================================================
--- marshal.c	(revision 46987)
+++ marshal.c	(revision 46988)
@@ -169,6 +169,7 @@ mark_dump_arg(void *ptr) https://github.com/ruby/ruby/blob/trunk/marshal.c#L169
     struct dump_arg *p = ptr;
     if (!p->symbols)
         return;
+    rb_mark_set(p->symbols);
     rb_mark_set(p->data);
     rb_mark_hash(p->compat_tbl);
     rb_gc_mark(p->str);
@@ -411,20 +412,19 @@ w_float(double d, struct dump_arg *arg) https://github.com/ruby/ruby/blob/trunk/marshal.c#L412
 }
 
 static void
-w_symbol(ID id, struct dump_arg *arg)
+w_symbol(VALUE sym, struct dump_arg *arg)
 {
-    VALUE sym;
     st_data_t num;
     VALUE encname;
 
-    if (st_lookup(arg->symbols, id, &num)) {
+    if (st_lookup(arg->symbols, sym, &num)) {
 	w_byte(TYPE_SYMLINK, arg);
 	w_long((long)num, arg);
     }
     else {
-	sym = rb_id2str(id);
+	sym = rb_sym2str(sym);
 	if (!sym) {
-	    rb_raise(rb_eTypeError, "can't dump anonymous ID %"PRIdVALUE, id);
+	    rb_raise(rb_eTypeError, "can't dump anonymous ID %"PRIdVALUE, sym);
 	}
 	encname = encoding_name(sym, arg);
 	if (NIL_P(encname) ||
@@ -436,7 +436,7 @@ w_symbol(ID id, struct dump_arg *arg) https://github.com/ruby/ruby/blob/trunk/marshal.c#L436
 	}
 	w_byte(TYPE_SYMBOL, arg);
 	w_bytes(RSTRING_PTR(sym), RSTRING_LEN(sym), arg);
-	st_add_direct(arg->symbols, id, arg->symbols->num_entries);
+	st_add_direct(arg->symbols, sym, arg->symbols->num_entries);
 	if (!NIL_P(encname)) {
 	    struct dump_call_arg c_arg;
 	    c_arg.limit = 1;
@@ -451,7 +451,7 @@ static void https://github.com/ruby/ruby/blob/trunk/marshal.c#L451
 w_unique(VALUE s, struct dump_arg *arg)
 {
     must_not_be_anonymous("class", s);
-    w_symbol(rb_intern_str(s), arg);
+    w_symbol(rb_str_dynamic_intern(s), arg);
 }
 
 static void w_object(VALUE,struct dump_arg*,int);
@@ -527,7 +527,7 @@ w_obj_each(st_data_t key, st_data_t val, https://github.com/ruby/ruby/blob/trunk/marshal.c#L527
     struct dump_call_arg *arg = (struct dump_call_arg *)a;
 
     if (to_be_skipped_id(id)) return ST_CONTINUE;
-    w_symbol(id, arg->arg);
+    w_symbol(ID2SYM(id), arg->arg);
     w_object(value, arg->arg, arg->limit);
     return ST_CONTINUE;
 }
@@ -574,12 +574,12 @@ w_encoding(VALUE encname, struct dump_ca https://github.com/ruby/ruby/blob/trunk/marshal.c#L574
     switch (encname) {
       case Qfalse:
       case Qtrue:
-	w_symbol(rb_intern("E"), arg->arg);
+	w_symbol(ID2SYM(rb_intern("E")), arg->arg);
 	w_object(encname, arg->arg, arg->limit + 1);
       case Qnil:
 	return;
     }
-    w_symbol(rb_id_encoding(), arg->arg);
+    w_symbol(ID2SYM(rb_id_encoding()), arg->arg);
     w_object(encname, arg->arg, arg->limit + 1);
 }
 
@@ -671,7 +671,7 @@ w_object(VALUE obj, struct dump_arg *arg https://github.com/ruby/ruby/blob/trunk/marshal.c#L671
 #endif
     }
     else if (SYMBOL_P(obj)) {
-	w_symbol(SYM2ID(obj), arg);
+	w_symbol(obj, arg);
     }
     else if (FLONUM_P(obj)) {
 	st_add_direct(arg->data, obj, arg->data->num_entries);
@@ -863,7 +863,7 @@ w_object(VALUE obj, struct dump_arg *arg https://github.com/ruby/ruby/blob/trunk/marshal.c#L863
 		w_long(len, arg);
 		mem = rb_struct_members(obj);
 		for (i=0; i<len; i++) {
-		    w_symbol(SYM2ID(RARRAY_AREF(mem, i)), arg);
+		    w_symbol(RARRAY_AREF(mem, i), arg);
 		    w_object(RSTRUCT_GET(obj, i), arg, limit);
 		}
 	    }
@@ -1044,6 +1044,7 @@ mark_load_arg(void *ptr) https://github.com/ruby/ruby/blob/trunk/marshal.c#L1044
     struct load_arg *p = ptr;
     if (!p->symbols)
         return;
+    rb_mark_tbl(p->symbols);
     rb_mark_tbl(p->data);
     rb_mark_hash(p->compat_tbl);
 }
@@ -1070,7 +1071,7 @@ static const rb_data_type_t load_arg_dat https://github.com/ruby/ruby/blob/trunk/marshal.c#L1071
 #define r_entry(v, arg) r_entry0((v), (arg)->data->num_entries, (arg))
 static VALUE r_entry0(VALUE v, st_index_t num, struct load_arg *arg);
 static VALUE r_object(struct load_arg *arg);
-static ID r_symbol(struct load_arg *arg);
+static VALUE r_symbol(struct load_arg *arg);
 static VALUE path2class(VALUE path);
 
 NORETURN(static void too_short(void));
@@ -1276,13 +1277,13 @@ r_bytes0(long len, struct load_arg *arg) https://github.com/ruby/ruby/blob/trunk/marshal.c#L1277
 }
 
 static int
-id2encidx(ID id, VALUE val)
+sym2encidx(VALUE sym, VALUE val)
 {
-    if (id == rb_id_encoding()) {
+    if (sym == ID2SYM(rb_id_encoding())) {
 	int idx = rb_enc_find_index(StringValueCStr(val));
 	return idx;
     }
-    else if (id == rb_intern("E")) {
+    else if (sym == ID2SYM(rb_intern("E"))) {
 	if (val == Qfalse) return rb_usascii_encindex();
 	else if (val == Qtrue) return rb_utf8_encindex();
 	/* bogus ignore */
@@ -1290,23 +1291,23 @@ id2encidx(ID id, VALUE val) https://github.com/ruby/ruby/blob/trunk/marshal.c#L1291
     return -1;
 }
 
-static ID
+static VALUE
 r_symlink(struct load_arg *arg)
 {
-    st_data_t id;
+    st_data_t sym;
     long num = r_long(arg);
 
-    if (!st_lookup(arg->symbols, num, &id)) {
+    if (!st_lookup(arg->symbols, num, &sym)) {
 	rb_raise(rb_eArgError, "bad symbol");
     }
-    return (ID)id;
+    return (VALUE)sym;
 }
 
-static ID
+static VALUE
 r_symreal(struct load_arg *arg, int ivar)
 {
     VALUE s = r_bytes(arg);
-    ID id;
+    VALUE sym;
     int idx = -1;
     st_index_t n = arg->symbols->num_entries;
 
@@ -1314,18 +1315,17 @@ r_symreal(struct load_arg *arg, int ivar https://github.com/ruby/ruby/blob/trunk/marshal.c#L1315
     if (ivar) {
 	long num = r_long(arg);
 	while (num-- > 0) {
-	    id = r_symbol(arg);
-	    idx = id2encidx(id, r_object(arg));
+	    idx = sym2encidx(r_symbol(arg), r_object(arg));
 	}
     }
     if (idx > 0) rb_enc_associate_index(s, idx);
-    id = rb_intern_str(s);
-    st_insert(arg->symbols, (st_data_t)n, (st_data_t)id);
+    sym = rb_str_dynamic_intern(s);
+    st_insert(arg->symbols, (st_data_t)n, (st_data_t)sym);
 
-    return id;
+    return sym;
 }
 
-static ID
+static VALUE
 r_symbol(struct load_arg *arg)
 {
     int type, ivar = 0;
@@ -1350,7 +1350,7 @@ r_symbol(struct load_arg *arg) https://github.com/ruby/ruby/blob/trunk/marshal.c#L1350
 static VALUE
 r_unique(struct load_arg *arg)
 {
-    return rb_id2str(r_symbol(arg));
+    return rb_sym2str(r_symbol(arg));
 }
 
 static VALUE
@@ -1440,15 +1440,15 @@ r_ivar(VALUE obj, int *has_encoding, str https://github.com/ruby/ruby/blob/trunk/marshal.c#L1440
     len = r_long(arg);
     if (len > 0) {
 	do {
-	    ID id = r_symbol(arg);
+	    VALUE sym = r_symbol(arg);
 	    VALUE val = r_object(arg);
-	    int idx = id2encidx(id, val);
+	    int idx = sym2encidx(sym, val);
 	    if (idx >= 0) {
 		rb_enc_associate_index(obj, idx);
 		if (has_encoding) *has_encoding = TRUE;
 	    }
 	    else {
-		rb_ivar_set(obj, id, val);
+		rb_ivar_set(obj, SYM2ID(sym), val);
 	    }
 	} while (--len > 0);
     }
@@ -1750,19 +1750,19 @@ r_object0(struct load_arg *arg, int *ivp https://github.com/ruby/ruby/blob/trunk/marshal.c#L1750
 	{
 	    VALUE mem, values;
 	    long i;
-	    ID slot;
+	    VALUE slot;
 	    st_index_t idx = r_prepare(arg);
 	    VALUE klass = path2class(r_unique(arg));
 	    long len = r_long(arg);
 
             v = rb_obj_alloc(klass);
 	    if (!RB_TYPE_P(v, T_STRUCT)) {
-		rb_raise(rb_eTypeError, "class %s not a struct", rb_class2name(klass));
+		rb_raise(rb_eTypeError, "class %"PRIsVALUE" not a struct", rb_class_name(klass));
 	    }
 	    mem = rb_struct_s_members(klass);
             if (RARRAY_LEN(mem) != len) {
-                rb_raise(rb_eTypeError, "struct %s not compatible (struct size differs)",
-                         rb_class2name(klass));
+                rb_raise(rb_eTypeError, "struct %"PRIsVALUE" not compatible (struct size differs)",
+                         rb_class_name(klass));
             }
 
 	    arg->readable += (len - 1) * 2;
@@ -1771,11 +1771,11 @@ r_object0(struct load_arg *arg, int *ivp https://github.com/ruby/ruby/blob/trunk/marshal.c#L1771
 	    for (i=0; i<len; i++) {
 		slot = r_symbol(arg);
 
-		if (RARRAY_AREF(mem, i) != ID2SYM(slot)) {
-		    rb_raise(rb_eTypeError, "struct %s not compatible (:%s for :%s)",
-			     rb_class2name(klass),
-			     rb_id2name(slot),
-			     rb_id2name(SYM2ID(RARRAY_AREF(mem, i))));
+		if (RARRAY_AREF(mem, i) != slot) {
+		    rb_raise(rb_eTypeError, "struct %"PRIsVALUE" not compatible (:%"PRIsVALUE" for :%"PRIsVALUE")",
+			     rb_class_name(klass),
+			     rb_sym2str(slot),
+			     rb_sym2str(RARRAY_AREF(mem, i)));
 		}
                 rb_ary_push(values, r_object(arg));
 		arg->readable -= 2;
@@ -1907,17 +1907,17 @@ r_object0(struct load_arg *arg, int *ivp https://github.com/ruby/ruby/blob/trunk/marshal.c#L1907
 
       case TYPE_SYMBOL:
 	if (ivp) {
-	    v = ID2SYM(r_symreal(arg, *ivp));
+	    v = r_symreal(arg, *ivp);
 	    *ivp = FALSE;
 	}
 	else {
-	    v = ID2SYM(r_symreal(arg, 0));
+	    v = r_symreal(arg, 0);
 	}
 	v = r_leave(v, arg);
 	break;
 
       case TYPE_SYMLINK:
-	v = ID2SYM(r_symlink(arg));
+	v = r_symlink(arg);
 	break;
 
       default:
Index: signal.c
===================================================================
--- signal.c	(revision 46987)
+++ signal.c	(revision 46988)
@@ -976,7 +976,7 @@ trap_handler(VALUE *cmd, int sig) https://github.com/ruby/ruby/blob/trunk/signal.c#L976
     else {
 	command = rb_check_string_type(*cmd);
 	if (NIL_P(command) && SYMBOL_P(*cmd)) {
-	    command = rb_id2str(SYM2ID(*cmd));
+	    command = rb_sym2str(*cmd);
 	    if (!command) rb_raise(rb_eArgError, "bad handler");
 	}
 	if (!NIL_P(command)) {

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

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