Skip to content

Commit

Permalink
object.c: extract common code
Browse files Browse the repository at this point in the history
* object.c (id_for_setter): extract common code from const, class
  variable, instance variable setters.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40188 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nobu committed Apr 8, 2013
1 parent a436540 commit f507cce
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 61 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Mon Apr 8 23:57:21 2013 Nobuyoshi Nakada <[email protected]>

* object.c (id_for_setter): extract common code from const, class
variable, instance variable setters.

Mon Apr 8 23:55:53 2013 Nobuyoshi Nakada <[email protected]>

* ext/depend (ENCOBJS, TRANSOBJS): use explicit path to ruby.h for
Expand Down
91 changes: 30 additions & 61 deletions object.c
Original file line number Diff line number Diff line change
Expand Up @@ -1813,6 +1813,33 @@ rb_class_get_superclass(VALUE klass)
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
Expand Down Expand Up @@ -2043,26 +2070,7 @@ rb_mod_const_get(int argc, VALUE *argv, VALUE mod)
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;
}
Expand Down Expand Up @@ -2180,26 +2188,7 @@ rb_obj_ivar_get(VALUE obj, VALUE iv)
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);
}

Expand Down Expand Up @@ -2305,27 +2294,7 @@ rb_mod_cvar_get(VALUE obj, VALUE iv)
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;
}
Expand Down

0 comments on commit f507cce

Please sign in to comment.