* 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.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@46988 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2014-07-28 08:15:42 +00:00
Родитель e4c92f24fe
Коммит 95d1b61a14
6 изменённых файлов: 78 добавлений и 74 удалений

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

@ -767,7 +767,7 @@ rb_enc_get_index(VALUE obj)
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:

4
enum.c
Просмотреть файл

@ -2679,7 +2679,7 @@ static VALUE
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, _argp))
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 {

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

@ -975,13 +975,15 @@ append_method(VALUE obj, VALUE str, ID default_method, VALUE default_args)
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);

48
iseq.c
Просмотреть файл

@ -475,17 +475,29 @@ rb_iseq_new_with_bopt(NODE *node, VALUE name, VALUE path, VALUE absolute_path, V
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 parent, VALUE opt)
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 parent, VALUE opt)
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)
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;
}

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

@ -169,6 +169,7 @@ mark_dump_arg(void *ptr)
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)
}
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)
}
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
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, st_data_t a)
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_call_arg *arg)
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, int limit)
#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, int limit)
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)
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_data = {
#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)
}
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)
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)
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)
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, struct load_arg *arg)
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, VALUE extmod)
{
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, VALUE extmod)
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, VALUE extmod)
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:

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

@ -976,7 +976,7 @@ trap_handler(VALUE *cmd, int sig)
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)) {