ruby-changes:33386
From: nobu <ko1@a...>
Date: Fri, 28 Mar 2014 21:36:32 +0900 (JST)
Subject: [ruby-changes:33386] nobu:r45465 (trunk): struct.c: avoid pinning down
nobu 2014-03-28 21:36:27 +0900 (Fri, 28 Mar 2014) New Revision: 45465 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=45465 Log: struct.c: avoid pinning down * 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. Modified files: trunk/struct.c trunk/test/ruby/test_struct.rb Index: struct.c =================================================================== --- struct.c (revision 45464) +++ struct.c (revision 45465) @@ -148,16 +148,19 @@ rb_struct_modify(VALUE s) https://github.com/ruby/ruby/blob/trunk/struct.c#L148 static VALUE rb_struct_set(VALUE obj, VALUE val) { - VALUE members, slot; + VALUE members, slot, fsym; long i, len; ID fid = rb_frame_this_func(); members = rb_struct_members(obj); len = RARRAY_LEN(members); 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++) { slot = RARRAY_AREF(members, i); - if (rb_id_attrset(SYM2ID(slot)) == fid) { + if (slot == fsym) { RSTRUCT_SET(obj, i, val); return val; } @@ -715,17 +718,17 @@ rb_struct_init_copy(VALUE copy, VALUE s) https://github.com/ruby/ruby/blob/trunk/struct.c#L718 } static VALUE -rb_struct_aref_id(VALUE s, ID id) +rb_struct_aref_sym(VALUE s, VALUE name) { VALUE members = rb_struct_members(s); long i, len = RARRAY_LEN(members); for (i=0; i<len; i++) { - if (SYM2ID(RARRAY_AREF(members, i)) == id) { + if (RARRAY_AREF(members, i) == name) { 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; } @@ -753,7 +756,7 @@ rb_struct_aref(VALUE s, VALUE idx) https://github.com/ruby/ruby/blob/trunk/struct.c#L756 long i; 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)) { ID id = rb_check_id_without_pindown(&idx); @@ -761,7 +764,7 @@ rb_struct_aref(VALUE s, VALUE idx) https://github.com/ruby/ruby/blob/trunk/struct.c#L764 rb_name_error_str(idx, "no member '%"PRIsVALUE"' in struct", QUOTE(idx)); } - return rb_struct_aref_id(s, id); + return rb_struct_aref_sym(s, ID2SYM(id)); } i = NUM2LONG(idx); @@ -776,7 +779,7 @@ rb_struct_aref(VALUE s, VALUE idx) https://github.com/ruby/ruby/blob/trunk/struct.c#L779 } 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); long i, len = RARRAY_LEN(members); @@ -787,13 +790,13 @@ rb_struct_aset_id(VALUE s, ID id, VALUE https://github.com/ruby/ruby/blob/trunk/struct.c#L790 } for (i=0; i<len; i++) { - if (SYM2ID(RARRAY_AREF(members, i)) == id) { + if (RARRAY_AREF(members, i) == name) { rb_struct_modify(s); RSTRUCT_SET(s, i, 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; } @@ -823,7 +826,7 @@ rb_struct_aset(VALUE s, VALUE idx, VALUE https://github.com/ruby/ruby/blob/trunk/struct.c#L826 long i; 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)) { ID id = rb_check_id(&idx); @@ -831,7 +834,7 @@ rb_struct_aset(VALUE s, VALUE idx, VALUE https://github.com/ruby/ruby/blob/trunk/struct.c#L834 rb_name_error_str(idx, "no member '%"PRIsVALUE"' in struct", QUOTE(idx)); } - return rb_struct_aset_id(s, id, val); + return rb_struct_aset_sym(s, ID2SYM(id), val); } i = NUM2LONG(idx); Index: test/ruby/test_struct.rb =================================================================== --- test/ruby/test_struct.rb (revision 45464) +++ test/ruby/test_struct.rb (revision 45465) @@ -158,6 +158,8 @@ module TestStruct https://github.com/ruby/ruby/blob/trunk/test/ruby/test_struct.rb#L158 assert_equal(1, o[0]) assert_raise(IndexError) { o[-2] } assert_raise(IndexError) { o[1] } + assert_raise_with_message(NameError, /foo/) {o["foo"]} + assert_raise_with_message(NameError, /foo/) {o[:foo]} end def test_aset @@ -167,6 +169,8 @@ module TestStruct https://github.com/ruby/ruby/blob/trunk/test/ruby/test_struct.rb#L169 assert_equal(2, o[:a]) assert_raise(IndexError) { o[-2] = 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 def test_values_at -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/