summaryrefslogtreecommitdiff
path: root/class.c
diff options
context:
space:
mode:
authorko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-05-13 10:49:11 +0000
committerko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-05-13 10:49:11 +0000
commit83aba0486298d61b39f3ed3492042690a889807f (patch)
tree42d060575f0931f834aa85d2493d3c9693b18a4e /class.c
parentaacd7710462142df7397618ffff4279e495f10f9 (diff)
* include/ruby/ruby.h: constify RBasic::klass and add
RBASIC_CLASS(obj) macro which returns a class of `obj'. This change is a part of RGENGC branch [ruby-trunk - Feature #8339]. * object.c: add new function rb_obj_reveal(). This function reveal interal (hidden) object by rb_obj_hide(). Note that do not change class before and after hiding. Only permitted example is: klass = RBASIC_CLASS(obj); rb_obj_hide(obj); .... rb_obj_reveal(obj, klass); TODO: API design. rb_obj_reveal() should be replaced with others. TODO: modify constified variables using cast may be harmful for compiler's analysis and optimizaton. Any idea to prohibt inserting RBasic::klass directly? If rename RBasic::klass and force to use RBASIC_CLASS(obj), then all codes such as `RBASIC(obj)->klass' will be compilation error. Is it acceptable? (We have similar experience at Ruby 1.9, for example "RARRAY(ary)->ptr" to "RARRAY_PTR(ary)". * internal.h: add some macros. * RBASIC_CLEAR_CLASS(obj) clear RBasic::klass to make it internal object. * RBASIC_SET_CLASS(obj, cls) set RBasic::klass. * RBASIC_SET_CLASS_RAW(obj, cls) same as RBASIC_SET_CLASS without write barrier (planned). * RCLASS_SET_SUPER(a, b) set super class of a. * array.c, class.c, compile.c, encoding.c, enum.c, error.c, eval.c, file.c, gc.c, hash.c, io.c, iseq.c, marshal.c, object.c, parse.y, proc.c, process.c, random.c, ruby.c, sprintf.c, string.c, thread.c, transcode.c, vm.c, vm_eval.c, win32/file.c: Use above macros and functions to access RBasic::klass. * ext/coverage/coverage.c, ext/readline/readline.c, ext/socket/ancdata.c, ext/socket/init.c, * ext/zlib/zlib.c: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40691 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'class.c')
-rw-r--r--class.c49
1 files changed, 25 insertions, 24 deletions
diff --git a/class.c b/class.c
index a37a153..eeca410 100644
--- a/class.c
+++ b/class.c
@@ -54,7 +54,7 @@ class_alloc(VALUE flags, VALUE klass)
RCLASS_IV_TBL(obj) = 0;
RCLASS_CONST_TBL(obj) = 0;
RCLASS_M_TBL(obj) = 0;
- RCLASS_SUPER(obj) = 0;
+ RCLASS_SET_SUPER((VALUE)obj, 0);
RCLASS_ORIGIN(obj) = (VALUE)obj;
RCLASS_IV_INDEX_TBL(obj) = 0;
RCLASS_REFINED_CLASS(obj) = Qnil;
@@ -77,7 +77,7 @@ rb_class_boot(VALUE super)
{
VALUE klass = class_alloc(T_CLASS, rb_cClass);
- RCLASS_SUPER(klass) = super;
+ RCLASS_SET_SUPER(klass, super);
RCLASS_M_TBL(klass) = st_init_numtable();
OBJ_INFECT(klass, super);
@@ -200,10 +200,10 @@ rb_mod_init_copy(VALUE clone, VALUE orig)
}
rb_obj_init_copy(clone, orig);
if (!FL_TEST(CLASS_OF(clone), FL_SINGLETON)) {
- RBASIC(clone)->klass = rb_singleton_class_clone(orig);
+ RBASIC_SET_CLASS(clone, rb_singleton_class_clone(orig));
rb_singleton_class_attached(RBASIC(clone)->klass, (VALUE)clone);
}
- RCLASS_SUPER(clone) = RCLASS_SUPER(orig);
+ RCLASS_SET_SUPER(clone, RCLASS_SUPER(orig));
RCLASS_EXT(clone)->allocator = RCLASS_EXT(orig)->allocator;
if (RCLASS_IV_TBL(orig)) {
st_data_t id;
@@ -255,13 +255,13 @@ rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach)
VALUE clone = class_alloc(RBASIC(klass)->flags, 0);
if (BUILTIN_TYPE(obj) == T_CLASS) {
- RBASIC(clone)->klass = clone;
+ RBASIC_SET_CLASS(clone, clone);
}
else {
- RBASIC(clone)->klass = rb_singleton_class_clone(klass);
+ RBASIC_SET_CLASS(clone, rb_singleton_class_clone(klass));
}
- RCLASS_SUPER(clone) = RCLASS_SUPER(klass);
+ RCLASS_SET_SUPER(clone, RCLASS_SUPER(klass));
RCLASS_EXT(clone)->allocator = RCLASS_EXT(klass)->allocator;
if (RCLASS_IV_TBL(klass)) {
RCLASS_IV_TBL(clone) = st_copy(RCLASS_IV_TBL(klass));
@@ -299,6 +299,7 @@ rb_singleton_class_attached(VALUE klass, VALUE obj)
#define METACLASS_OF(k) RBASIC(k)->klass
+#define SET_METACLASS_OF(k, cls) RBASIC_SET_CLASS(k, cls)
/*!
* whether k is a meta^(n)-class of Class class
@@ -346,17 +347,18 @@ make_metaclass(VALUE klass)
rb_singleton_class_attached(metaclass, klass);
if (META_CLASS_OF_CLASS_CLASS_P(klass)) {
- METACLASS_OF(klass) = METACLASS_OF(metaclass) = metaclass;
+ SET_METACLASS_OF(klass, metaclass);
+ SET_METACLASS_OF(metaclass, metaclass);
}
else {
VALUE tmp = METACLASS_OF(klass); /* for a meta^(n)-class klass, tmp is meta^(n)-class of Class class */
- METACLASS_OF(klass) = metaclass;
- METACLASS_OF(metaclass) = ENSURE_EIGENCLASS(tmp);
+ SET_METACLASS_OF(klass, metaclass);
+ SET_METACLASS_OF(metaclass, ENSURE_EIGENCLASS(tmp));
}
super = RCLASS_SUPER(klass);
while (RB_TYPE_P(super, T_ICLASS)) super = RCLASS_SUPER(super);
- RCLASS_SUPER(metaclass) = super ? ENSURE_EIGENCLASS(super) : rb_cClass;
+ RCLASS_SET_SUPER(metaclass, super ? ENSURE_EIGENCLASS(super) : rb_cClass);
OBJ_INFECT(metaclass, RCLASS_SUPER(metaclass));
@@ -376,10 +378,10 @@ make_singleton_class(VALUE obj)
VALUE klass = rb_class_boot(orig_class);
FL_SET(klass, FL_SINGLETON);
- RBASIC(obj)->klass = klass;
+ RBASIC_SET_CLASS(obj, klass);
rb_singleton_class_attached(klass, obj);
- METACLASS_OF(klass) = METACLASS_OF(rb_class_real(orig_class));
+ SET_METACLASS_OF(klass, METACLASS_OF(rb_class_real(orig_class)));
return klass;
}
@@ -406,11 +408,10 @@ Init_class_hierarchy(void)
rb_cClass = boot_defclass("Class", rb_cModule);
rb_const_set(rb_cObject, rb_intern("BasicObject"), rb_cBasicObject);
- RBASIC(rb_cClass)->klass
- = RBASIC(rb_cModule)->klass
- = RBASIC(rb_cObject)->klass
- = RBASIC(rb_cBasicObject)->klass
- = rb_cClass;
+ RBASIC_SET_CLASS(rb_cClass, rb_cClass);
+ RBASIC_SET_CLASS(rb_cModule, rb_cClass);
+ RBASIC_SET_CLASS(rb_cObject, rb_cClass);
+ RBASIC_SET_CLASS(rb_cBasicObject, rb_cClass);
}
@@ -674,12 +675,12 @@ rb_include_class_new(VALUE module, VALUE super)
RCLASS_IV_TBL(klass) = RCLASS_IV_TBL(module);
RCLASS_CONST_TBL(klass) = RCLASS_CONST_TBL(module);
RCLASS_M_TBL(klass) = RCLASS_M_TBL(RCLASS_ORIGIN(module));
- RCLASS_SUPER(klass) = super;
+ RCLASS_SET_SUPER(klass, super);
if (RB_TYPE_P(module, T_ICLASS)) {
- RBASIC(klass)->klass = RBASIC(module)->klass;
+ RBASIC_SET_CLASS(klass, RBASIC(module)->klass);
}
else {
- RBASIC(klass)->klass = module;
+ RBASIC_SET_CLASS(klass, module);
}
OBJ_INFECT(klass, module);
OBJ_INFECT(klass, super);
@@ -748,7 +749,7 @@ include_modules_at(const VALUE klass, VALUE c, VALUE module)
break;
}
}
- c = RCLASS_SUPER(c) = rb_include_class_new(module, RCLASS_SUPER(c));
+ c = RCLASS_SET_SUPER(c, rb_include_class_new(module, RCLASS_SUPER(c)));
if (FL_TEST(klass, RMODULE_IS_REFINEMENT)) {
VALUE refined_class =
rb_refinement_module_get_refined_class(klass);
@@ -812,8 +813,8 @@ rb_prepend_module(VALUE klass, VALUE module)
origin = RCLASS_ORIGIN(klass);
if (origin == klass) {
origin = class_alloc(T_ICLASS, klass);
- RCLASS_SUPER(origin) = RCLASS_SUPER(klass);
- RCLASS_SUPER(klass) = origin;
+ RCLASS_SET_SUPER(origin, RCLASS_SUPER(klass));
+ RCLASS_SET_SUPER(klass, origin);
RCLASS_ORIGIN(klass) = origin;
RCLASS_M_TBL(origin) = RCLASS_M_TBL(klass);
RCLASS_M_TBL(klass) = st_init_numtable();