ruby-changes:28136
From: nobu <ko1@a...>
Date: Mon, 8 Apr 2013 23:57:38 +0900 (JST)
Subject: [ruby-changes:28136] nobu:r40188 (trunk): object.c: extract common code
nobu 2013-04-08 23:57:29 +0900 (Mon, 08 Apr 2013) New Revision: 40188 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=40188 Log: object.c: extract common code * object.c (id_for_setter): extract common code from const, class variable, instance variable setters. Modified files: trunk/ChangeLog trunk/object.c Index: ChangeLog =================================================================== --- ChangeLog (revision 40187) +++ ChangeLog (revision 40188) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Mon Apr 8 23:57:21 2013 Nobuyoshi Nakada <nobu@r...> + + * object.c (id_for_setter): extract common code from const, class + variable, instance variable setters. + Mon Apr 8 23:55:53 2013 Nobuyoshi Nakada <nobu@r...> * ext/depend (ENCOBJS, TRANSOBJS): use explicit path to ruby.h for Index: object.c =================================================================== --- object.c (revision 40187) +++ object.c (revision 40188) @@ -1813,6 +1813,33 @@ rb_class_get_superclass(VALUE klass) https://github.com/ruby/ruby/blob/trunk/object.c#L1813 return RCLASS_SUPER(klass); } +#define id_for_setter(name, type, message) \ + check_setter_id(name, rb_is_##type##_id, rb_is_##type##_name, message) +static ID +check_setter_id(VALUE name, int (*valid_id_p)(ID), int (*valid_name_p)(VALUE), + const char *message) +{ + ID id; + if (SYMBOL_P(name)) { + id = SYM2ID(name); + if (!valid_id_p(id)) { + rb_name_error(id, message, QUOTE_ID(id)); + } + } + else { + VALUE str = rb_check_string_type(name); + if (NIL_P(str)) { + rb_raise(rb_eTypeError, "%+"PRIsVALUE" is not a symbol or string", + str); + } + if (!valid_name_p(str)) { + rb_name_error_str(str, message, QUOTE(str)); + } + id = rb_to_id(str); + } + return id; +} + /* * call-seq: * attr_reader(symbol, ...) -> nil @@ -2043,26 +2070,7 @@ rb_mod_const_get(int argc, VALUE *argv, https://github.com/ruby/ruby/blob/trunk/object.c#L2070 static VALUE rb_mod_const_set(VALUE mod, VALUE name, VALUE value) { - ID id; - if (SYMBOL_P(name)) { - id = SYM2ID(name); - if (!rb_is_const_id(id)) { - rb_name_error(id, "wrong constant name %"PRIsVALUE, - QUOTE_ID(id)); - } - } - else { - VALUE cname = rb_check_string_type(name); - if (NIL_P(cname)) { - rb_raise(rb_eTypeError, "%+"PRIsVALUE" is not a symbol or string", - name); - } - if (!rb_is_const_name(cname)) { - rb_name_error_str(cname, "wrong constant name %"PRIsVALUE, - QUOTE(cname)); - } - id = rb_to_id(cname); - } + ID id = id_for_setter(name, const, "wrong constant name %"PRIsVALUE); rb_const_set(mod, id, value); return value; } @@ -2180,26 +2188,7 @@ rb_obj_ivar_get(VALUE obj, VALUE iv) https://github.com/ruby/ruby/blob/trunk/object.c#L2188 static VALUE rb_obj_ivar_set(VALUE obj, VALUE iv, VALUE val) { - ID id; - - if (SYMBOL_P(iv)) { - id = SYM2ID(iv); - if (!rb_is_instance_id(id)) { - rb_name_error(id, "`%"PRIsVALUE"' is not allowed as an instance variable name", - QUOTE_ID(id)); - } - } - else { - VALUE name = rb_check_string_type(iv); - if (NIL_P(name)) { - rb_raise(rb_eTypeError, "%+"PRIsVALUE" is not a symbol or string", iv); - } - if (!rb_is_instance_name(name)) { - rb_name_error_str(iv, "`%"PRIsVALUE"' is not allowed as an instance variable name", - QUOTE(iv)); - } - id = rb_to_id(name); - } + ID id = id_for_setter(iv, instance, "`%"PRIsVALUE"' is not allowed as an instance variable name"); return rb_ivar_set(obj, id, val); } @@ -2305,27 +2294,7 @@ rb_mod_cvar_get(VALUE obj, VALUE iv) https://github.com/ruby/ruby/blob/trunk/object.c#L2294 static VALUE rb_mod_cvar_set(VALUE obj, VALUE iv, VALUE val) { - ID id; - - if (SYMBOL_P(iv)) { - id = SYM2ID(iv); - if (!rb_is_class_id(id)) { - rb_name_error(id, "`%"PRIsVALUE"' is not allowed as an class variable name", - QUOTE_ID(id)); - } - } - else { - VALUE cname = rb_check_string_type(iv); - if (NIL_P(cname)) { - rb_raise(rb_eTypeError, "%+"PRIsVALUE" is not a symbol or string", - iv); - } - if (!rb_is_class_name(cname)) { - rb_name_error_str(iv, "`%"PRIsVALUE"' is not allowed as a class variable name", - QUOTE(cname)); - } - id = rb_to_id(cname); - } + ID id = id_for_setter(iv, class, "`%"PRIsVALUE"' is not allowed as a class variable name"); rb_cvar_set(obj, id, val); return val; } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/