1#ifndef INTERNAL_CLASS_H
2#define INTERNAL_CLASS_H
13#include "internal/box.h"
14#include "internal/serial.h"
15#include "internal/static_assert.h"
16#include "internal/variable.h"
21#include "ruby_assert.h"
40 rb_serial_t global_cvar_state;
73 const VALUE refined_class;
79 VALUE attached_object;
85 attr_index_t max_iv_count;
86 uint16_t superclass_depth;
87 unsigned char variation_count;
88 bool permanent_classpath : 1;
90 bool shared_const_tbl : 1;
91 bool iclass_is_origin : 1;
92 bool iclass_origin_shared_mtbl : 1;
93 bool superclasses_with_self : 1;
98STATIC_ASSERT(shape_max_variations, SHAPE_MAX_VARIATIONS < (1 << (
sizeof(((
rb_classext_t *)0)->variation_count) * CHAR_BIT)));
114#if SIZEOF_VALUE >= SIZEOF_LONG_LONG
125static const uint16_t RCLASS_MAX_SUPERCLASS_DEPTH = ((uint16_t)-1);
127static inline bool RCLASS_SINGLETON_P(
VALUE klass);
129static inline bool RCLASS_PRIME_CLASSEXT_READABLE_P(
VALUE obj);
130static inline bool RCLASS_PRIME_CLASSEXT_WRITABLE_P(
VALUE obj);
131static inline void RCLASS_SET_PRIME_CLASSEXT_WRITABLE(
VALUE obj,
bool writable);
133#define RCLASS_EXT_PRIME(c) (&((struct RClass_and_rb_classext_t*)(c))->classext)
134#define RCLASS_EXT_PRIME_P(ext, c) (&((struct RClass_and_rb_classext_t*)(c))->classext == ext)
142#define RCLASSEXT_BOX(ext) (ext->box)
143#define RCLASSEXT_SUPER(ext) (ext->super)
144#define RCLASSEXT_FIELDS(ext) (ext->fields_obj ? ROBJECT_FIELDS(ext->fields_obj) : NULL)
145#define RCLASSEXT_FIELDS_OBJ(ext) (ext->fields_obj)
146#define RCLASSEXT_M_TBL(ext) (ext->m_tbl)
147#define RCLASSEXT_CONST_TBL(ext) (ext->const_tbl)
148#define RCLASSEXT_CALLABLE_M_TBL(ext) (ext->callable_m_tbl)
149#define RCLASSEXT_CC_TBL(ext) (ext->cc_tbl)
150#define RCLASSEXT_CVC_TBL(ext) (ext->cvc_tbl)
151#define RCLASSEXT_SUPERCLASS_DEPTH(ext) (ext->superclass_depth)
152#define RCLASSEXT_SUPERCLASSES(ext) (ext->superclasses)
153#define RCLASSEXT_SUBCLASSES(ext) (ext->subclasses)
154#define RCLASSEXT_SUBCLASS_ENTRY(ext) (ext->subclass_entry)
155#define RCLASSEXT_MODULE_SUBCLASS_ENTRY(ext) (ext->module_subclass_entry)
156#define RCLASSEXT_ORIGIN(ext) (ext->origin_)
157#define RCLASSEXT_REFINED_CLASS(ext) (ext->refined_class)
159#define RCLASSEXT_INCLUDER(ext) (ext->as.iclass.includer)
160#define RCLASSEXT_PERMANENT_CLASSPATH(ext) (ext->permanent_classpath)
161#define RCLASSEXT_CLONED(ext) (ext->cloned)
162#define RCLASSEXT_SHARED_CONST_TBL(ext) (ext->shared_const_tbl)
163#define RCLASSEXT_ICLASS_IS_ORIGIN(ext) (ext->iclass_is_origin)
164#define RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext) (ext->iclass_origin_shared_mtbl)
165#define RCLASSEXT_SUPERCLASSES_WITH_SELF(ext) (ext->superclasses_with_self)
166#define RCLASSEXT_CLASSPATH(ext) (ext->classpath)
172#define RCLASS_PRIME_BOX(c) (RCLASS_EXT_PRIME(c)->box)
175#define RCLASS_PRIME_FIELDS_OBJ(c) (RCLASS_EXT_PRIME(c)->fields_obj)
176#define RCLASS_PRIME_M_TBL(c) (RCLASS_EXT_PRIME(c)->m_tbl)
177#define RCLASS_PRIME_CONST_TBL(c) (RCLASS_EXT_PRIME(c)->const_tbl)
178#define RCLASS_PRIME_CALLABLE_M_TBL(c) (RCLASS_EXT_PRIME(c)->callable_m_tbl)
179#define RCLASS_PRIME_CC_TBL(c) (RCLASS_EXT_PRIME(c)->cc_tbl)
180#define RCLASS_M_TBL_NOT_PRIME_P(c, tbl) (RCLASS_EXT_PRIME(c)->m_tbl != tbl)
181#define RCLASS_CALLABLE_M_TBL_NOT_PRIME_P(c, tbl) (RCLASS_EXT_PRIME(c)->callable_m_tbl != tbl)
182#define RCLASS_CC_TBL_NOT_PRIME_P(c, tbl) (RCLASS_EXT_PRIME(c)->cc_tbl != tbl)
185#define RCLASS_SUPER(c) (RCLASS_EXT_READABLE(c)->super)
186#define RCLASS_M_TBL(c) (RCLASS_EXT_READABLE(c)->m_tbl)
187#define RCLASS_CONST_TBL(c) (RCLASS_EXT_READABLE(c)->const_tbl)
192#define RCLASS_CVC_TBL(c) (RCLASS_EXT_READABLE(c)->cvc_tbl)
193#define RCLASS_SUBCLASSES(c) (RCLASS_EXT_PRIME(c)->subclasses)
194#define RCLASS_SUBCLASSES_FIRST(c) (RCLASS_EXT_PRIME(c)->subclasses ? RCLASS_EXT_PRIME(c)->subclasses->next : NULL)
195#define RCLASS_ORIGIN(c) (RCLASS_EXT_READABLE(c)->origin_)
196#define RICLASS_IS_ORIGIN_P(c) (RCLASS_EXT_READABLE(c)->iclass_is_origin)
197#define RCLASS_PERMANENT_CLASSPATH_P(c) (RCLASS_EXT_READABLE(c)->permanent_classpath)
198#define RCLASS_CLONED_P(c) (RCLASS_EXT_READABLE(c)->cloned)
199#define RCLASS_CLASSPATH(c) (RCLASS_EXT_READABLE(c)->classpath)
202#define RCLASS_SUPERCLASS_DEPTH(c) (RCLASS_EXT_PRIME(c)->superclass_depth)
203#define RCLASS_SUPERCLASSES(c) (RCLASS_EXT_PRIME(c)->superclasses)
204#define RCLASS_SUPERCLASSES_WITH_SELF_P(c) (RCLASS_EXT_PRIME(c)->superclasses_with_self)
207#define RCLASS_REFINED_CLASS(c) (RCLASS_EXT_PRIME(c)->refined_class)
208#define RCLASS_ATTACHED_OBJECT(c) (RCLASS_EXT_PRIME(c)->as.singleton_class.attached_object)
209#define RCLASS_INCLUDER(c) (RCLASS_EXT_PRIME(c)->as.iclass.includer)
212#define RCLASS_MAX_IV_COUNT(ext) (RCLASS_EXT_PRIME(ext)->max_iv_count)
213#define RCLASS_VARIATION_COUNT(ext) (RCLASS_EXT_PRIME(ext)->variation_count)
216#define RCLASS_WRITABLE_M_TBL(c) (RCLASS_EXT_WRITABLE(c)->m_tbl)
217#define RCLASS_WRITABLE_CONST_TBL(c) (RCLASS_EXT_WRITABLE(c)->const_tbl)
218#define RCLASS_WRITABLE_CALLABLE_M_TBL(c) (RCLASS_EXT_WRITABLE(c)->callable_m_tbl)
219#define RCLASS_WRITABLE_CC_TBL(c) (RCLASS_EXT_WRITABLE(c)->cc_tbl)
220#define RCLASS_WRITABLE_CVC_TBL(c) (RCLASS_EXT_WRITABLE(c)->cvc_tbl)
222#define RCLASS_WRITABLE_SUBCLASSES(c) (RCLASS_EXT_PRIME(c)->subclasses)
224static inline void RCLASS_SET_SUPER(
VALUE klass,
VALUE super);
225static inline void RCLASS_WRITE_SUPER(
VALUE klass,
VALUE super);
226static inline void RCLASS_SET_CONST_TBL(
VALUE klass,
struct rb_id_table *table,
bool shared);
227static inline void RCLASS_WRITE_CONST_TBL(
VALUE klass,
struct rb_id_table *table,
bool shared);
228static inline void RCLASS_WRITE_CALLABLE_M_TBL(
VALUE klass,
struct rb_id_table *table);
229static inline void RCLASS_WRITE_CC_TBL(
VALUE klass,
VALUE table);
230static inline void RCLASS_SET_CVC_TBL(
VALUE klass,
VALUE table);
231static inline void RCLASS_WRITE_CVC_TBL(
VALUE klass,
VALUE table);
233static inline void RCLASS_WRITE_SUPERCLASSES(
VALUE klass,
size_t depth,
VALUE *superclasses,
bool with_self);
236static inline void RCLASS_SET_ORIGIN(
VALUE klass,
VALUE origin);
237static inline void RCLASS_WRITE_ORIGIN(
VALUE klass,
VALUE origin);
238static inline void RICLASS_SET_ORIGIN_SHARED_MTBL(
VALUE iclass);
239static inline void RICLASS_WRITE_ORIGIN_SHARED_MTBL(
VALUE iclass);
240static inline bool RICLASS_OWNS_M_TBL_P(
VALUE iclass);
242static inline void RCLASS_SET_REFINED_CLASS(
VALUE klass,
VALUE refined);
245static inline VALUE RCLASS_SET_ATTACHED_OBJECT(
VALUE klass,
VALUE attached_object);
247static inline void RCLASS_SET_INCLUDER(
VALUE iclass,
VALUE klass);
248static inline void RCLASS_SET_MAX_IV_COUNT(
VALUE klass, attr_index_t count);
249static inline void RCLASS_SET_CLONED(
VALUE klass,
bool cloned);
250static inline void RCLASS_SET_CLASSPATH(
VALUE klass,
VALUE classpath,
bool permanent);
251static inline void RCLASS_WRITE_CLASSPATH(
VALUE klass,
VALUE classpath,
bool permanent);
253#define RCLASS_IS_ROOT FL_USER0
255#define RCLASS_PRIME_CLASSEXT_WRITABLE FL_USER2
256#define RCLASS_IS_INITIALIZED FL_USER3
258#define RCLASS_BOXABLE FL_USER4
259#define RCLASS_ALLOCATOR_DEFINED FL_USER5
260#define RCLASS_HAS_SUBCLASSES FL_USER6
263RCLASS_CLASSEXT_TBL(
VALUE klass)
267 return box_klass->box_classext_tbl;
277 box_klass->box_classext_tbl = tbl;
282void rb_class_ensure_writable(
VALUE obj);
290 st_table *tbl = RCLASS_CLASSEXT_TBL(obj);
291 VM_ASSERT(BOX_USER_P(box));
292 VM_ASSERT(box->box_object);
293 VM_ASSERT(RCLASSEXT_BOX(ext) == box);
295 tbl = st_init_numtable_with_size(1);
296 RCLASS_SET_CLASSEXT_TBL(obj, tbl);
298 if (rb_st_table_size(tbl) == 0) {
302 rb_class_set_box_classext(obj, box, ext);
307#define VM_ASSERT_BOXABLE_TYPE(klass) \
308 VM_ASSERT(RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_MODULE) || RB_TYPE_P(klass, T_ICLASS), "%s is not boxable type", rb_type_str(BUILTIN_TYPE(klass)))
311RCLASS_PRIME_CLASSEXT_READABLE_P(
VALUE klass)
313 VM_ASSERT(klass != 0,
"klass should be a valid object");
314 VM_ASSERT_BOXABLE_TYPE(klass);
316 return !
FL_TEST_RAW(klass, RCLASS_BOXABLE) || RCLASS_CLASSEXT_TBL(klass) == NULL;
320RCLASS_PRIME_CLASSEXT_WRITABLE_P(
VALUE klass)
322 VM_ASSERT(klass != 0,
"klass should be a valid object");
323 VM_ASSERT_BOXABLE_TYPE(klass);
324 return FL_TEST(klass, RCLASS_PRIME_CLASSEXT_WRITABLE);
328RCLASS_SET_PRIME_CLASSEXT_WRITABLE(
VALUE klass,
bool writable)
330 VM_ASSERT(klass != 0,
"klass should be a valid object");
331 VM_ASSERT_BOXABLE_TYPE(klass);
333 FL_SET(klass, RCLASS_PRIME_CLASSEXT_WRITABLE);
336 FL_UNSET(klass, RCLASS_PRIME_CLASSEXT_WRITABLE);
341RCLASS_EXT_TABLE_LOOKUP_INTERNAL(
VALUE obj,
const rb_box_t *box)
343 st_data_t classext_ptr;
344 st_table *classext_tbl = RCLASS_CLASSEXT_TBL(obj);
346 if (rb_st_lookup(classext_tbl, (st_data_t)box->box_object, &classext_ptr)) {
356 rb_classext_t *ext = RCLASS_EXT_TABLE_LOOKUP_INTERNAL(obj, box);
360 return RCLASS_EXT_PRIME(obj);
367 || RCLASS_PRIME_CLASSEXT_READABLE_P(obj)) {
368 return RCLASS_EXT_PRIME(obj);
370 return RCLASS_EXT_READABLE_LOOKUP(obj, box);
374RCLASS_EXT_READABLE(
VALUE obj)
377 if (RCLASS_PRIME_CLASSEXT_READABLE_P(obj)) {
378 return RCLASS_EXT_PRIME(obj);
381 box = rb_current_box();
382 if (BOX_ROOT_P(box)) {
383 return RCLASS_EXT_PRIME(obj);
385 return RCLASS_EXT_READABLE_LOOKUP(obj, box);
394 ext = RCLASS_EXT_TABLE_LOOKUP_INTERNAL(obj, box);
400 ext = RCLASS_EXT_TABLE_LOOKUP_INTERNAL(obj, box);
402 ext = rb_class_duplicate_classext(RCLASS_EXT_PRIME(obj), obj, box);
403 first_set = RCLASS_SET_BOX_CLASSEXT(obj, box, ext);
406 RCLASS_SET_PRIME_CLASSEXT_WRITABLE(obj,
false);
417 || RCLASS_PRIME_CLASSEXT_WRITABLE_P(obj)) {
418 return RCLASS_EXT_PRIME(obj);
420 return RCLASS_EXT_WRITABLE_LOOKUP(obj, box);
424RCLASS_EXT_WRITABLE(
VALUE obj)
427 if (LIKELY(RCLASS_PRIME_CLASSEXT_WRITABLE_P(obj))) {
428 return RCLASS_EXT_PRIME(obj);
431 box = rb_current_box();
432 if (BOX_ROOT_P(box)) {
433 return RCLASS_EXT_PRIME(obj);
435 return RCLASS_EXT_WRITABLE_LOOKUP(obj, box);
448 RB_OBJ_WRITE(klass, &(RCLASSEXT_INCLUDER(ext)), includer);
452typedef void rb_class_classext_foreach_callback_func(
rb_classext_t *classext,
bool is_prime,
VALUE box_value,
void *arg);
453void rb_class_classext_foreach(
VALUE klass, rb_class_classext_foreach_callback_func *func,
void *arg);
454void rb_class_subclass_add(
VALUE super,
VALUE klass);
456void rb_class_update_superclasses(
VALUE);
457int rb_singleton_class_internal_p(
VALUE sklass);
462void rb_class_set_initialized(
VALUE klass);
463void rb_module_check_initializable(
VALUE module);
471VALUE rb_class_undefined_instance_methods(
VALUE mod);
475void rb_undef_methods_from(
VALUE klass,
VALUE super);
477VALUE rb_keyword_error_new(
const char *,
VALUE);
483RUBY_SYMBOL_EXPORT_BEGIN
488unsigned char rb_class_variation_count(
VALUE klass);
490RUBY_SYMBOL_EXPORT_END
493RCLASS_SINGLETON_P(
VALUE klass)
502 RB_OBJ_WRITE(klass, &RCLASSEXT_SUPER(RCLASS_EXT_PRIME(klass)), super);
508 RB_OBJ_WRITE(klass, &RCLASSEXT_SUPER(RCLASS_EXT_WRITABLE(klass)), super);
512RCLASS_WRITABLE_ENSURE_FIELDS_OBJ(
VALUE obj)
516 if (!ext->fields_obj) {
517 RB_OBJ_WRITE(obj, &ext->fields_obj, rb_imemo_fields_new(obj, 1,
true));
519 return ext->fields_obj;
523RCLASS_WRITABLE_FIELDS_OBJ(
VALUE obj)
526 return RCLASSEXT_FIELDS_OBJ(RCLASS_EXT_WRITABLE(obj));
534 RB_OBJ_ATOMIC_WRITE(obj, &ext->fields_obj, fields_obj);
538RCLASS_WRITABLE_SET_FIELDS_OBJ(
VALUE obj,
VALUE fields_obj)
542 RCLASSEXT_SET_FIELDS_OBJ(obj, RCLASS_EXT_WRITABLE(obj), fields_obj);
545static inline uint32_t
546RCLASS_FIELDS_COUNT(
VALUE obj)
550 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
552 if (rb_shape_obj_too_complex_p(fields_obj)) {
553 return (uint32_t)rb_st_table_size(rb_imemo_fields_complex_tbl(fields_obj));
556 return RSHAPE_LEN(RBASIC_SHAPE_ID(fields_obj));
565 RCLASSEXT_M_TBL(RCLASS_EXT_PRIME(klass)) = table;
571 RCLASSEXT_M_TBL(RCLASS_EXT_WRITABLE(klass)) = table;
578 RCLASSEXT_CONST_TBL(ext) = table;
580 RCLASSEXT_SHARED_CONST_TBL(ext) =
true;
587 RCLASSEXT_CONST_TBL(ext) = table;
589 RCLASSEXT_SHARED_CONST_TBL(ext) =
true;
595 RCLASSEXT_CALLABLE_M_TBL(RCLASS_EXT_WRITABLE(klass)) = table;
601 RB_OBJ_ATOMIC_WRITE(klass, &RCLASSEXT_CC_TBL(RCLASS_EXT_WRITABLE(klass)), table);
607 RB_OBJ_ATOMIC_WRITE(klass, &RCLASSEXT_CVC_TBL(RCLASS_EXT_PRIME(klass)), table);
613 RB_OBJ_ATOMIC_WRITE(klass, &RCLASSEXT_CVC_TBL(RCLASS_EXT_WRITABLE(klass)), table);
617RCLASS_SET_REFINED_CLASS(
VALUE klass,
VALUE refined)
619 RB_OBJ_WRITE(klass, &RCLASSEXT_REFINED_CLASS(RCLASS_EXT_PRIME(klass)), refined);
623RCLASS_ALLOCATOR(
VALUE klass)
625 RBIMPL_ASSERT_TYPE(klass,
T_CLASS);
627 return RCLASS_EXT_PRIME(klass)->as.class.allocator;
635 RCLASS_EXT_PRIME(klass)->as.class.allocator = allocator;
643 if (klass != origin) RCLASSEXT_ICLASS_IS_ORIGIN(RCLASS_EXT_WRITABLE(origin)) =
true;
651 if (klass != origin) RCLASSEXT_ICLASS_IS_ORIGIN(RCLASS_EXT_WRITABLE(origin)) =
true;
655RICLASS_SET_ORIGIN_SHARED_MTBL(
VALUE iclass)
657 RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(RCLASS_EXT_PRIME(iclass)) =
true;
661RICLASS_WRITE_ORIGIN_SHARED_MTBL(
VALUE iclass)
663 RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(RCLASS_EXT_WRITABLE(iclass)) =
true;
667RICLASS_OWNS_M_TBL_P(
VALUE iclass)
670 return RCLASSEXT_ICLASS_IS_ORIGIN(ext) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext);
681RCLASS_WRITE_SUPERCLASSES(
VALUE klass,
size_t depth,
VALUE *superclasses,
bool with_self)
686 RCLASSEXT_SUPERCLASS_DEPTH(ext) = depth;
687 RCLASSEXT_SUPERCLASSES(ext) = superclasses;
688 RCLASSEXT_SUPERCLASSES_WITH_SELF(ext) = with_self;
695 RCLASSEXT_SUBCLASSES(ext) = head;
699RCLASS_SET_CLASSPATH(
VALUE klass,
VALUE classpath,
bool permanent)
706 RB_OBJ_WRITE(klass, &(RCLASSEXT_CLASSPATH(ext)), classpath);
707 RCLASSEXT_PERMANENT_CLASSPATH(ext) = permanent;
711RCLASS_WRITE_CLASSPATH(
VALUE klass,
VALUE classpath,
bool permanent)
718 RB_OBJ_WRITE(klass, &(RCLASSEXT_CLASSPATH(ext)), classpath);
719 RCLASSEXT_PERMANENT_CLASSPATH(ext) = permanent;
723RCLASS_SET_ATTACHED_OBJECT(
VALUE klass,
VALUE attached_object)
725 assert(RCLASS_SINGLETON_P(klass));
727 RB_OBJ_WRITE(klass, &RCLASS_EXT_PRIME(klass)->as.singleton_class.attached_object, attached_object);
728 return attached_object;
732RCLASS_SET_MAX_IV_COUNT(
VALUE klass, attr_index_t count)
734 RCLASS_MAX_IV_COUNT(klass) = count;
738RCLASS_SET_CLONED(
VALUE klass,
bool cloned)
740 RCLASSEXT_CLONED(RCLASS_EXT_PRIME(klass)) = cloned;
744RCLASS_INITIALIZED_P(
VALUE klass)
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
static bool RB_FL_ABLE(VALUE obj)
Checks if the object is flaggable.
@ RUBY_FL_SHAREABLE
This flag has something to do with Ractor.
VALUE rb_class_boot(VALUE)
A utility function that wraps class_alloc.
VALUE rb_class_inherited(VALUE, VALUE)
Calls Class::inherited.
VALUE rb_singleton_class_get(VALUE obj)
Returns the singleton class of obj, or nil if obj is not a singleton object.
#define FL_SINGLETON
Old name of RUBY_FL_SINGLETON.
#define T_STRING
Old name of RUBY_T_STRING.
#define T_MODULE
Old name of RUBY_T_MODULE.
#define T_ICLASS
Old name of RUBY_T_ICLASS.
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
#define FL_SET
Old name of RB_FL_SET.
#define T_CLASS
Old name of RUBY_T_CLASS.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
#define FL_TEST
Old name of RB_FL_TEST.
#define FL_UNSET
Old name of RB_FL_UNSET.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
VALUE(* rb_alloc_func_t)(VALUE klass)
This is the type of functions that ruby calls when trying to allocate an object.
Ruby object's base components.
Internal header for Ruby Box.
struct rb_subclass_entry * subclass_entry
Back-pointer to this class's entry in its superclass's subclasses list.
struct rb_subclass_entry * module_subclass_entry
In the case that this is an ICLASS, module_subclass_entry points to the entry in the module's subclas...
struct rb_subclass_entry * subclasses
The head of the subclasses linked list.
Internal header for Class.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static enum ruby_value_type RB_BUILTIN_TYPE(VALUE obj)
Queries the type of the object.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.