Skip to content

Commit

Permalink
Directly allocate FrozenCore as an ICLASS
Browse files Browse the repository at this point in the history
It's a bad idea to overwrite the flags as the garbage collector may have
set other flags.
  • Loading branch information
peterzhu2118 committed Jun 14, 2023
1 parent 3751349 commit 813a5f4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
10 changes: 8 additions & 2 deletions class.c
Expand Up @@ -1129,10 +1129,16 @@ rb_define_module_id_under(VALUE outer, ID id)
return module;
}

VALUE
rb_iclass_alloc(VALUE klass)
{
return class_alloc(T_ICLASS, klass);
}

VALUE
rb_include_class_new(VALUE module, VALUE super)
{
VALUE klass = class_alloc(T_ICLASS, rb_cClass);
VALUE klass = rb_iclass_alloc(rb_cClass);

RCLASS_M_TBL(klass) = RCLASS_M_TBL(module);

Expand Down Expand Up @@ -1409,7 +1415,7 @@ ensure_origin(VALUE klass)
{
VALUE origin = RCLASS_ORIGIN(klass);
if (origin == klass) {
origin = class_alloc(T_ICLASS, klass);
origin = rb_iclass_alloc(klass);
RCLASS_SET_SUPER(origin, RCLASS_SUPER(klass));
RCLASS_SET_SUPER(klass, origin);
RCLASS_SET_ORIGIN(klass, origin);
Expand Down
3 changes: 2 additions & 1 deletion internal/class.h
Expand Up @@ -119,6 +119,7 @@ VALUE rb_module_s_alloc(VALUE klass);
void rb_module_set_initialized(VALUE module);
void rb_module_check_initializable(VALUE module);
VALUE rb_make_metaclass(VALUE, VALUE);
VALUE rb_iclass_alloc(VALUE klass);
VALUE rb_include_class_new(VALUE, VALUE);
void rb_class_foreach_subclass(VALUE klass, void (*f)(VALUE, VALUE), VALUE);
void rb_class_detach_subclasses(VALUE);
Expand Down Expand Up @@ -205,7 +206,7 @@ RCLASS_SET_SUPER(VALUE klass, VALUE super)
static inline void
RCLASS_SET_CLASSPATH(VALUE klass, VALUE classpath, bool permanent)
{
assert(BUILTIN_TYPE(klass) == T_CLASS || BUILTIN_TYPE(klass) == T_MODULE);
assert(BUILTIN_TYPE(klass) == T_CLASS || BUILTIN_TYPE(klass) == T_MODULE || klass == rb_mRubyVMFrozenCore);
assert(classpath == 0 || BUILTIN_TYPE(classpath) == T_STRING);

RB_OBJ_WRITE(klass, &(RCLASS_EXT(klass)->classpath), classpath);
Expand Down
5 changes: 1 addition & 4 deletions vm.c
Expand Up @@ -3638,7 +3638,6 @@ Init_VM(void)
{
VALUE opts;
VALUE klass;
VALUE fcore;

/*
* Document-class: RubyVM
Expand All @@ -3664,9 +3663,8 @@ Init_VM(void)
#endif

/* FrozenCore (hidden) */
fcore = rb_class_new(rb_cBasicObject);
VALUE fcore = rb_mRubyVMFrozenCore = rb_iclass_alloc(rb_cBasicObject);
rb_set_class_path(fcore, rb_cRubyVM, "FrozenCore");
RBASIC(fcore)->flags = T_ICLASS;
klass = rb_singleton_class(fcore);
rb_define_method_id(klass, id_core_set_method_alias, m_core_set_method_alias, 3);
rb_define_method_id(klass, id_core_set_variable_alias, m_core_set_variable_alias, 2);
Expand All @@ -3685,7 +3683,6 @@ Init_VM(void)
RBASIC_CLEAR_CLASS(klass);
rb_obj_freeze(klass);
rb_gc_register_mark_object(fcore);
rb_mRubyVMFrozenCore = fcore;

/*
* Document-class: Thread
Expand Down

0 comments on commit 813a5f4

Please sign in to comment.