* struct.c (rb_struct_set, rb_struct_aref, rb_struct_aset): get
  rid of pinning down dynamic symbols by SYM2ID.

* struct.c (rb_struct_aref_sym, rb_struct_aset_sym): use Symbol
  instead of ID.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45465 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2014-03-28 12:36:27 +00:00
Родитель fd8f3cb987
Коммит 1d223a3bdf
2 изменённых файлов: 19 добавлений и 12 удалений

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

@ -148,16 +148,19 @@ rb_struct_modify(VALUE s)
static VALUE static VALUE
rb_struct_set(VALUE obj, VALUE val) rb_struct_set(VALUE obj, VALUE val)
{ {
VALUE members, slot; VALUE members, slot, fsym;
long i, len; long i, len;
ID fid = rb_frame_this_func(); ID fid = rb_frame_this_func();
members = rb_struct_members(obj); members = rb_struct_members(obj);
len = RARRAY_LEN(members); len = RARRAY_LEN(members);
rb_struct_modify(obj); rb_struct_modify(obj);
fid = rb_id_attrget(fid);
if (!fid) not_a_member(rb_frame_this_func());
fsym = ID2SYM(fid);
for (i=0; i<len; i++) { for (i=0; i<len; i++) {
slot = RARRAY_AREF(members, i); slot = RARRAY_AREF(members, i);
if (rb_id_attrset(SYM2ID(slot)) == fid) { if (slot == fsym) {
RSTRUCT_SET(obj, i, val); RSTRUCT_SET(obj, i, val);
return val; return val;
} }
@ -715,17 +718,17 @@ rb_struct_init_copy(VALUE copy, VALUE s)
} }
static VALUE static VALUE
rb_struct_aref_id(VALUE s, ID id) rb_struct_aref_sym(VALUE s, VALUE name)
{ {
VALUE members = rb_struct_members(s); VALUE members = rb_struct_members(s);
long i, len = RARRAY_LEN(members); long i, len = RARRAY_LEN(members);
for (i=0; i<len; i++) { for (i=0; i<len; i++) {
if (SYM2ID(RARRAY_AREF(members, i)) == id) { if (RARRAY_AREF(members, i) == name) {
return RSTRUCT_GET(s, i); return RSTRUCT_GET(s, i);
} }
} }
rb_name_error(id, "no member '%s' in struct", rb_id2name(id)); rb_name_error_str(name, "no member '% "PRIsVALUE"' in struct", name);
UNREACHABLE; UNREACHABLE;
} }
@ -753,7 +756,7 @@ rb_struct_aref(VALUE s, VALUE idx)
long i; long i;
if (RB_TYPE_P(idx, T_SYMBOL)) { if (RB_TYPE_P(idx, T_SYMBOL)) {
return rb_struct_aref_id(s, SYM2ID(idx)); return rb_struct_aref_sym(s, idx);
} }
else if (RB_TYPE_P(idx, T_STRING)) { else if (RB_TYPE_P(idx, T_STRING)) {
ID id = rb_check_id_without_pindown(&idx); ID id = rb_check_id_without_pindown(&idx);
@ -761,7 +764,7 @@ rb_struct_aref(VALUE s, VALUE idx)
rb_name_error_str(idx, "no member '%"PRIsVALUE"' in struct", rb_name_error_str(idx, "no member '%"PRIsVALUE"' in struct",
QUOTE(idx)); QUOTE(idx));
} }
return rb_struct_aref_id(s, id); return rb_struct_aref_sym(s, ID2SYM(id));
} }
i = NUM2LONG(idx); i = NUM2LONG(idx);
@ -776,7 +779,7 @@ rb_struct_aref(VALUE s, VALUE idx)
} }
static VALUE static VALUE
rb_struct_aset_id(VALUE s, ID id, VALUE val) rb_struct_aset_sym(VALUE s, VALUE name, VALUE val)
{ {
VALUE members = rb_struct_members(s); VALUE members = rb_struct_members(s);
long i, len = RARRAY_LEN(members); long i, len = RARRAY_LEN(members);
@ -787,13 +790,13 @@ rb_struct_aset_id(VALUE s, ID id, VALUE val)
} }
for (i=0; i<len; i++) { for (i=0; i<len; i++) {
if (SYM2ID(RARRAY_AREF(members, i)) == id) { if (RARRAY_AREF(members, i) == name) {
rb_struct_modify(s); rb_struct_modify(s);
RSTRUCT_SET(s, i, val); RSTRUCT_SET(s, i, val);
return val; return val;
} }
} }
rb_name_error(id, "no member '%s' in struct", rb_id2name(id)); rb_name_error_str(name, "no member '% "PRIsVALUE"' in struct", name);
UNREACHABLE; UNREACHABLE;
} }
@ -823,7 +826,7 @@ rb_struct_aset(VALUE s, VALUE idx, VALUE val)
long i; long i;
if (RB_TYPE_P(idx, T_SYMBOL)) { if (RB_TYPE_P(idx, T_SYMBOL)) {
return rb_struct_aset_id(s, SYM2ID(idx), val); return rb_struct_aset_sym(s, idx, val);
} }
if (RB_TYPE_P(idx, T_STRING)) { if (RB_TYPE_P(idx, T_STRING)) {
ID id = rb_check_id(&idx); ID id = rb_check_id(&idx);
@ -831,7 +834,7 @@ rb_struct_aset(VALUE s, VALUE idx, VALUE val)
rb_name_error_str(idx, "no member '%"PRIsVALUE"' in struct", rb_name_error_str(idx, "no member '%"PRIsVALUE"' in struct",
QUOTE(idx)); QUOTE(idx));
} }
return rb_struct_aset_id(s, id, val); return rb_struct_aset_sym(s, ID2SYM(id), val);
} }
i = NUM2LONG(idx); i = NUM2LONG(idx);

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

@ -158,6 +158,8 @@ module TestStruct
assert_equal(1, o[0]) assert_equal(1, o[0])
assert_raise(IndexError) { o[-2] } assert_raise(IndexError) { o[-2] }
assert_raise(IndexError) { o[1] } assert_raise(IndexError) { o[1] }
assert_raise_with_message(NameError, /foo/) {o["foo"]}
assert_raise_with_message(NameError, /foo/) {o[:foo]}
end end
def test_aset def test_aset
@ -167,6 +169,8 @@ module TestStruct
assert_equal(2, o[:a]) assert_equal(2, o[:a])
assert_raise(IndexError) { o[-2] = 3 } assert_raise(IndexError) { o[-2] = 3 }
assert_raise(IndexError) { o[1] = 3 } assert_raise(IndexError) { o[1] = 3 }
assert_raise_with_message(NameError, /foo/) {o["foo"] = 3}
assert_raise_with_message(NameError, /foo/) {o[:foo] = 3}
end end
def test_values_at def test_values_at