Ruby 4.1.0dev (2026-12-02 revision 4ff592f0ab1c35343efe112d4cc31b853f455a38)
class.h
1#ifndef INTERNAL_CLASS_H /*-*-C-*-vi:se ft=c:*/
2#define INTERNAL_CLASS_H
11#include "id.h"
12#include "id_table.h" /* for struct rb_id_table */
13#include "internal/box.h"
14#include "internal/serial.h" /* for rb_serial_t */
15#include "ractor_core.h" /* for rb_ractor_main_p */
16#include "internal/static_assert.h"
17#include "internal/variable.h" /* for rb_class_ivar_set */
18#include "ruby/internal/stdbool.h" /* for bool */
19#include "ruby/intern.h" /* for rb_alloc_func_t */
20#include "ruby/ruby.h" /* for struct RBasic */
21#include "shape.h"
22#include "ruby_assert.h"
23#include "vm_core.h"
24#include "vm_sync.h"
25#include "method.h" /* for rb_cref_t */
26
27#ifdef RCLASS_SUPER
28# undef RCLASS_SUPER
29#endif
30
32 VALUE imemo_flags;
33 uint32_t index;
34 rb_serial_t global_cvar_state;
35 const rb_cref_t *cref;
36 VALUE class_value;
37};
38
40 const rb_box_t *box;
41 VALUE super;
42 VALUE fields_obj; // Fields are either ivar or other internal properties stored inline
43 struct rb_id_table *m_tbl;
44 struct rb_id_table *const_tbl;
45 struct rb_id_table *callable_m_tbl;
46 VALUE cc_tbl; /* { ID => { cme, [cc1, cc2, ...] }, ... } */
47 VALUE cvc_tbl;
48 VALUE *superclasses;
54
55 const VALUE origin_;
56 const VALUE refined_class;
57 union {
58 struct {
59 rb_alloc_func_t allocator;
60 VALUE classpath;
61 } class;
62 struct {
63 VALUE attached_object;
64 VALUE reserved;
65 } singleton_class;
66 struct {
67 const VALUE includer;
68 VALUE refinement_iclasses;
69 } iclass;
70 } as;
71 /* Id of the Ractor that created this class/module; only that Ractor may modify
72 * it. 0 is the main Ractor, so a program that never leaves it stores nothing.
73 * Ids are never reused, so a terminated owner leaves the class read-only for
74 * everybody. Prime classext only; always 0 for T_ICLASS. */
75 rb_serial_t owner_ractor_id;
76 uint16_t superclass_depth;
77 attr_index_t max_iv_count;
78 uint8_t variation_count;
79 bool permanent_classpath : 1;
80 bool shared_const_tbl : 1;
81 bool iclass_is_origin : 1;
82 bool iclass_origin_shared_mtbl : 1;
83 bool superclasses_with_self : 1;
84 bool expect_no_ivar : 1;
85 bool has_struct_inline_field : 1; // See RCLASS_STRUCT_INLINE_FIELD.
86};
87typedef struct rb_classext_struct rb_classext_t;
88
89STATIC_ASSERT(shape_max_variations, SHAPE_MAX_VARIATIONS < (1 << (sizeof(((rb_classext_t *)0)->variation_count) * CHAR_BIT)));
90
91struct RClass {
92 struct RBasic basic;
93 VALUE object_id;
94 /*
95 * If box_classext_tbl is NULL, then the prime classext is readable (because no other classext exists).
96 * For the check whether writable or not, check flag RCLASS_PRIME_CLASSEXT_WRITABLE
97 */
98};
99
101 struct RClass rclass;
102 rb_classext_t classext;
103 /* VALUE struct_inline_field (optional) */
104};
105
106#if SIZEOF_VALUE >= SIZEOF_LONG_LONG
107// Assert that classes can be embedded in heaps[3] (256B slot size on 64-bit).
108// On 32bit platforms there is no variable width allocation so it doesn't matter.
109STATIC_ASSERT(sizeof_rb_classext_t, sizeof(struct RClass_and_rb_classext_t) <= 256);
110#endif
111
113 struct RClass_and_rb_classext_t base;
114 st_table *box_classext_tbl; // box_object -> (rb_classext_t *)
115 /* VALUE struct_inline_field (optional) */
116};
117
118static const uint16_t RCLASS_MAX_SUPERCLASS_DEPTH = ((uint16_t)-1);
119
120static inline bool RCLASS_SINGLETON_P(VALUE klass);
121
122static inline bool RCLASS_PRIME_CLASSEXT_READABLE_P(VALUE obj);
123static inline bool RCLASS_PRIME_CLASSEXT_WRITABLE_P(VALUE obj);
124static inline void RCLASS_SET_PRIME_CLASSEXT_WRITABLE(VALUE obj, bool writable);
125
126#define RCLASS_EXT_PRIME(c) (&((struct RClass_and_rb_classext_t*)(c))->classext)
127#define RCLASS_EXT_PRIME_P(ext, c) (&((struct RClass_and_rb_classext_t*)(c))->classext == ext)
128
129// Class ownership. See rb_classext_struct::owner_ractor_id.
130#define RCLASSEXT_OWNER_RACTOR_ID(ext) (ext->owner_ractor_id)
131
132static inline rb_serial_t
133RCLASS_OWNER_RACTOR_ID(VALUE klass)
134{
135 return RCLASS_EXT_PRIME(klass)->owner_ractor_id;
136}
137
138static inline void
139RCLASS_SET_OWNER_RACTOR_ID(VALUE klass, rb_serial_t ractor_id)
140{
141 // not a VALUE: no write barrier, nothing for the GC to mark or move
142 RCLASS_EXT_PRIME(klass)->owner_ractor_id = ractor_id;
143}
144
145bool rb_class_owned_by_ractor_p(rb_serial_t owner_id); // rb_class_owned_p's slow half
146void rb_class_owner_check(VALUE klass); // raise Ractor::IsolationError unless rb_class_owned_p(klass)
147void rb_class_take_ownership(VALUE klass); // for a moved object's singleton class only
148void rb_class_check_singleton_movable(VALUE klass); // raise if it holds unshareable values
149
150// true if the current Ractor created klass. Inline because the class ivar and
151// constant read paths take it on every access.
152static inline bool
153rb_class_owned_p(VALUE klass)
154{
155 rb_serial_t owner_id = RCLASS_OWNER_RACTOR_ID(klass);
156 if (LIKELY(!owner_id)) return rb_ractor_main_p(); // the only case single-Ractor programs take
157 return rb_class_owned_by_ractor_p(owner_id);
158}
159
160static inline rb_classext_t * RCLASS_EXT_READABLE_IN_BOX(VALUE obj, const rb_box_t *box);
161static inline rb_classext_t * RCLASS_EXT_READABLE(VALUE obj);
162static inline rb_classext_t * RCLASS_EXT_WRITABLE_IN_BOX(VALUE obj, const rb_box_t *box);
163static inline rb_classext_t * RCLASS_EXT_WRITABLE(VALUE obj);
164
165// Raw accessor
166#define RCLASSEXT_BOX(ext) (ext->box)
167#define RCLASSEXT_SUPER(ext) (ext->super)
168#define RCLASSEXT_FIELDS_OBJ(ext) rbimpl_atomic_value_load(&(ext)->fields_obj, RBIMPL_ATOMIC_ACQUIRE)
169#define RCLASSEXT_M_TBL(ext) (ext->m_tbl)
170#define RCLASSEXT_CONST_TBL(ext) (ext->const_tbl)
171#define RCLASSEXT_CALLABLE_M_TBL(ext) (ext->callable_m_tbl)
172#define RCLASSEXT_CC_TBL(ext) (ext->cc_tbl)
173#define RCLASSEXT_CVC_TBL(ext) (ext->cvc_tbl)
174#define RCLASSEXT_SUPERCLASS_DEPTH(ext) (ext->superclass_depth)
175#define RCLASSEXT_SUPERCLASSES(ext) (ext->superclasses)
176#define RCLASSEXT_SUBCLASSES(ext) (ext->subclasses)
177#define RCLASSEXT_ORIGIN(ext) (ext->origin_)
178#define RCLASSEXT_REFINED_CLASS(ext) (ext->refined_class)
179// class.allocator/singleton_class.attached_object are not accessed directly via RCLASSEXT_*
180#define RCLASSEXT_INCLUDER(ext) (ext->as.iclass.includer)
181#define RCLASSEXT_PERMANENT_CLASSPATH(ext) (ext->permanent_classpath)
182#define RCLASSEXT_SHARED_CONST_TBL(ext) (ext->shared_const_tbl)
183#define RCLASSEXT_ICLASS_IS_ORIGIN(ext) (ext->iclass_is_origin)
184#define RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext) (ext->iclass_origin_shared_mtbl)
185#define RCLASSEXT_SUPERCLASSES_WITH_SELF(ext) (ext->superclasses_with_self)
186#define RCLASSEXT_CLASSPATH(ext) (ext->as.class.classpath)
187#define RCLASSEXT_MODULE_REFINEMENT_ICLASSES(ext) (ext->as.iclass.refinement_iclasses)
188
189static inline void RCLASSEXT_SET_ORIGIN(rb_classext_t *ext, VALUE klass, VALUE origin);
190static inline void RCLASSEXT_SET_INCLUDER(rb_classext_t *ext, VALUE klass, VALUE includer);
191
192/* Prime classext entry accessor for very specific reason */
193#define RCLASS_PRIME_BOX(c) (RCLASS_EXT_PRIME(c)->box)
194// To invalidate CC by inserting&invalidating method entry into tables containing the target cme
195// See clear_method_cache_by_id_in_class()
196#define RCLASS_PRIME_FIELDS_OBJ(c) RCLASSEXT_FIELDS_OBJ(RCLASS_EXT_PRIME(c))
197#define RCLASS_PRIME_M_TBL(c) (RCLASS_EXT_PRIME(c)->m_tbl)
198#define RCLASS_PRIME_CONST_TBL(c) (RCLASS_EXT_PRIME(c)->const_tbl)
199#define RCLASS_PRIME_CALLABLE_M_TBL(c) (RCLASS_EXT_PRIME(c)->callable_m_tbl)
200#define RCLASS_PRIME_CC_TBL(c) (RCLASS_EXT_PRIME(c)->cc_tbl)
201#define RCLASS_M_TBL_NOT_PRIME_P(c, tbl) (RCLASS_EXT_PRIME(c)->m_tbl != tbl)
202#define RCLASS_CALLABLE_M_TBL_NOT_PRIME_P(c, tbl) (RCLASS_EXT_PRIME(c)->callable_m_tbl != tbl)
203#define RCLASS_CC_TBL_NOT_PRIME_P(c, tbl) (RCLASS_EXT_PRIME(c)->cc_tbl != tbl)
204
205// Read accessor, regarding box
206#define RCLASS_SUPER(c) (RCLASS_EXT_READABLE(c)->super)
207#define RCLASS_M_TBL(c) (RCLASS_EXT_READABLE(c)->m_tbl)
208#define RCLASS_CONST_TBL(c) (RCLASS_EXT_READABLE(c)->const_tbl)
209/*
210 * Both cc_tbl/callable_m_tbl are cache-like and always be changed when referreed,
211 * so always those should be writable.
212 */
213#define RCLASS_CVC_TBL(c) (RCLASS_EXT_READABLE(c)->cvc_tbl)
214#define RCLASS_SUBCLASSES(c) (RCLASS_EXT_PRIME(c)->subclasses)
215#define RCLASS_ORIGIN(c) (RCLASS_EXT_READABLE(c)->origin_)
216#define RICLASS_IS_ORIGIN_P(c) (RCLASS_EXT_READABLE(c)->iclass_is_origin)
217#define RCLASS_PERMANENT_CLASSPATH_P(c) (RCLASS_EXT_READABLE(c)->permanent_classpath)
218#define RCLASS_CLASSPATH(c) (RCLASS_EXT_READABLE(c)->as.class.classpath)
219
220// Superclasses can't be changed after initialization
221#define RCLASS_SUPERCLASS_DEPTH(c) (RCLASS_EXT_PRIME(c)->superclass_depth)
222#define RCLASS_SUPERCLASSES(c) (RCLASS_EXT_PRIME(c)->superclasses)
223#define RCLASS_SUPERCLASSES_WITH_SELF_P(c) (RCLASS_EXT_PRIME(c)->superclasses_with_self)
224
225// Ruby Box doesn't make changes on these refined_class/attached_object/includer
226#define RCLASS_REFINED_CLASS(c) (RCLASS_EXT_PRIME(c)->refined_class)
227#define RCLASS_ATTACHED_OBJECT(c) (RCLASS_EXT_PRIME(c)->as.singleton_class.attached_object)
228#define RCLASS_INCLUDER(c) (RCLASS_EXT_PRIME(c)->as.iclass.includer)
229#define RCLASS_MODULE_REFINEMENT_ICLASSES(c) (RCLASS_EXT_PRIME(c)->as.iclass.refinement_iclasses)
230
231// max IV count and variation count are just hints, so they don't need to be per-box
232#define RCLASS_MAX_IV_COUNT(ext) (RCLASS_EXT_PRIME(ext)->max_iv_count)
233#define RCLASS_VARIATION_COUNT(ext) (RCLASS_EXT_PRIME(ext)->variation_count)
234
235// Writable classext entries (instead of RCLASS_SET_*) because member data will be operated directly
236#define RCLASS_WRITABLE_M_TBL(c) (RCLASS_EXT_WRITABLE(c)->m_tbl)
237#define RCLASS_WRITABLE_CONST_TBL(c) (RCLASS_EXT_WRITABLE(c)->const_tbl)
238#define RCLASS_WRITABLE_CALLABLE_M_TBL(c) (RCLASS_EXT_WRITABLE(c)->callable_m_tbl)
239#define RCLASS_WRITABLE_CC_TBL(c) (RCLASS_EXT_WRITABLE(c)->cc_tbl)
240#define RCLASS_WRITABLE_CVC_TBL(c) (RCLASS_EXT_WRITABLE(c)->cvc_tbl)
241// Subclasses are only in the prime classext (box-invariant)
242#define RCLASS_WRITABLE_SUBCLASSES(c) (RCLASS_EXT_PRIME(c)->subclasses)
243
244static inline void RCLASS_SET_SUPER(VALUE klass, VALUE super);
245static inline void RCLASS_WRITE_SUPER(VALUE klass, VALUE super);
246static inline void RCLASS_SET_CONST_TBL(VALUE klass, struct rb_id_table *table, bool shared);
247static inline void RCLASS_WRITE_CONST_TBL(VALUE klass, struct rb_id_table *table, bool shared);
248static inline void RCLASS_WRITE_CALLABLE_M_TBL(VALUE klass, struct rb_id_table *table);
249static inline void RCLASS_WRITE_CC_TBL(VALUE klass, VALUE table);
250static inline void RCLASS_SET_CVC_TBL(VALUE klass, VALUE table);
251static inline void RCLASS_WRITE_CVC_TBL(VALUE klass, VALUE table);
252
253static inline void RCLASS_WRITE_SUPERCLASSES(VALUE klass, size_t depth, VALUE *superclasses, bool with_self);
254static inline void RCLASS_SET_SUBCLASSES(VALUE klass, VALUE subclasses);
255
256static inline void RCLASS_SET_ORIGIN(VALUE klass, VALUE origin);
257static inline void RCLASS_WRITE_ORIGIN(VALUE klass, VALUE origin);
258static inline void RICLASS_SET_ORIGIN_SHARED_MTBL(VALUE iclass);
259static inline void RICLASS_WRITE_ORIGIN_SHARED_MTBL(VALUE iclass);
260static inline bool RICLASS_OWNS_M_TBL_P(VALUE iclass);
261
262static inline void RCLASS_SET_REFINED_CLASS(VALUE klass, VALUE refined);
263static inline rb_alloc_func_t RCLASS_ALLOCATOR(VALUE klass);
264static inline void RCLASS_SET_ALLOCATOR(VALUE klass, rb_alloc_func_t allocator);
265static inline VALUE RCLASS_SET_ATTACHED_OBJECT(VALUE klass, VALUE attached_object);
266
267static inline void RCLASS_SET_INCLUDER(VALUE iclass, VALUE klass);
268static inline void RCLASS_SET_MODULE_REFINEMENT_ICLASSES(VALUE iclass, VALUE table);
269static inline void RCLASS_SET_MAX_IV_COUNT(VALUE klass, attr_index_t count);
270static inline void RCLASS_SET_CLASSPATH(VALUE klass, VALUE classpath, bool permanent);
271static inline void RCLASS_WRITE_CLASSPATH(VALUE klass, VALUE classpath, bool permanent);
272
273#define RCLASS_IS_ROOT FL_USER0
274// 1 is for RUBY_FL_SINGLETON or RMODULE_IS_REFINEMENT
275#define RCLASS_PRIME_CLASSEXT_WRITABLE FL_USER2
276#define RCLASS_IS_INITIALIZED FL_USER3
277// 3 is RMODULE_IS_REFINEMENT for RMODULE
278#define RCLASS_BOXABLE FL_USER4
279#define RCLASS_ALLOCATOR_DEFINED FL_USER5
280#define RCLASS_HAS_SUBCLASSES FL_USER6
281
282static inline st_table *
283RCLASS_CLASSEXT_TBL(VALUE klass)
284{
285 if (FL_TEST_RAW(klass, RCLASS_BOXABLE)) {
286 struct RClass_boxable *box_klass = (struct RClass_boxable *)klass;
287 return box_klass->box_classext_tbl;
288 }
289 return NULL;
290}
291
292static inline void
293RCLASS_SET_CLASSEXT_TBL(VALUE klass, st_table *tbl)
294{
295 RUBY_ASSERT(FL_TEST_RAW(klass, RCLASS_BOXABLE));
296 struct RClass_boxable *box_klass = (struct RClass_boxable *)klass;
297 box_klass->box_classext_tbl = tbl;
298}
299
300/* A "struct inline field" is stored right after the class in the same slot
301 * if its `has_struct_inline_field` is true.
302 * It starts as nil and belongs to the class, not to a box.
303 * Subclasses and copies of the class inherit the same value.
304 */
305static inline bool
306RCLASS_HAS_STRUCT_INLINE_FIELD(VALUE klass)
307{
308 return RCLASS_EXT_PRIME(klass)->has_struct_inline_field;
309}
310
311static inline VALUE *
312RCLASS_STRUCT_INLINE_FIELD_PTR(VALUE klass)
313{
314 RUBY_ASSERT(RCLASS_HAS_STRUCT_INLINE_FIELD(klass));
315 size_t offset = FL_TEST_RAW(klass, RCLASS_BOXABLE) ? sizeof(struct RClass_boxable) : sizeof(struct RClass_and_rb_classext_t);
316 return (VALUE *)((char *)klass + offset);
317}
318
319static inline VALUE
320RCLASS_STRUCT_INLINE_FIELD(VALUE klass)
321{
322 return *RCLASS_STRUCT_INLINE_FIELD_PTR(klass);
323}
324
325static inline void
326RCLASS_SET_STRUCT_INLINE_FIELD(VALUE klass, VALUE value)
327{
328 RB_OBJ_WRITE(klass, RCLASS_STRUCT_INLINE_FIELD_PTR(klass), value);
329}
330
331/* class.c */
332rb_classext_t * rb_class_duplicate_classext(rb_classext_t *orig, VALUE obj, const rb_box_t *box, int *first_set);
333void rb_class_ensure_writable(VALUE obj);
334
335void rb_class_set_box_classext(VALUE obj, const rb_box_t *box, rb_classext_t *ext);
336
337static inline int
338RCLASS_SET_BOX_CLASSEXT(VALUE obj, const rb_box_t *box, rb_classext_t *ext)
339{
340 int first_set = 0;
341 st_table *tbl = RCLASS_CLASSEXT_TBL(obj);
342 VM_ASSERT(BOX_MUTABLE_P(box)); // Setting non-prime classext never happens on the master box
343 VM_ASSERT(box->box_object);
344 VM_ASSERT(RCLASSEXT_BOX(ext) == box);
345 if (!tbl) {
346 tbl = st_init_numtable_with_size(1);
347 RCLASS_SET_CLASSEXT_TBL(obj, tbl);
348 }
349 if (rb_st_table_size(tbl) == 0) {
350 first_set = 1;
351 }
352
353 rb_class_set_box_classext(obj, box, ext);
354
355 return first_set;
356}
357
358#define VM_ASSERT_BOXABLE_TYPE(klass) \
359 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)))
360
361static inline bool
362RCLASS_PRIME_CLASSEXT_READABLE_P(VALUE klass)
363{
364 VM_ASSERT(klass != 0, "klass should be a valid object");
365 VM_ASSERT_BOXABLE_TYPE(klass);
366 // if the lookup table exists, then it means the prime classext is NOT directly readable.
367 return !FL_TEST_RAW(klass, RCLASS_BOXABLE) || RCLASS_CLASSEXT_TBL(klass) == NULL;
368}
369
370static inline bool
371RCLASS_PRIME_CLASSEXT_WRITABLE_P(VALUE klass)
372{
373 VM_ASSERT(klass != 0, "klass should be a valid object");
374 VM_ASSERT_BOXABLE_TYPE(klass);
375 RBIMPL_ASSUME(klass != 0);
376 return FL_TEST_RAW(klass, RCLASS_PRIME_CLASSEXT_WRITABLE);
377}
378
379static inline void
380RCLASS_SET_PRIME_CLASSEXT_WRITABLE(VALUE klass, bool writable)
381{
382 VM_ASSERT(klass != 0, "klass should be a valid object");
383 VM_ASSERT_BOXABLE_TYPE(klass);
384 if (writable) {
385 FL_SET_RAW(klass, RCLASS_PRIME_CLASSEXT_WRITABLE);
386 }
387 else {
388 FL_UNSET_RAW(klass, RCLASS_PRIME_CLASSEXT_WRITABLE);
389 }
390}
391
392static inline rb_classext_t *
393RCLASS_EXT_TABLE_LOOKUP_INTERNAL(VALUE obj, const rb_box_t *box)
394{
395 st_data_t classext_ptr;
396 st_table *classext_tbl = RCLASS_CLASSEXT_TBL(obj);
397 if (classext_tbl) {
398 if (rb_st_lookup(classext_tbl, (st_data_t)box->box_object, &classext_ptr)) {
399 return (rb_classext_t *)classext_ptr;
400 }
401 }
402 return NULL;
403}
404
405static inline rb_classext_t *
406RCLASS_EXT_READABLE_LOOKUP(VALUE obj, const rb_box_t *box)
407{
408 rb_classext_t *ext = RCLASS_EXT_TABLE_LOOKUP_INTERNAL(obj, box);
409 if (ext)
410 return ext;
411 // Classext for the ns not found. Refer the prime one instead.
412 return RCLASS_EXT_PRIME(obj);
413}
414
415static inline rb_classext_t *
416RCLASS_EXT_READABLE_IN_BOX(VALUE obj, const rb_box_t *box)
417{
418 if (BOX_MASTER_P(box)
419 || RCLASS_PRIME_CLASSEXT_READABLE_P(obj)) {
420 return RCLASS_EXT_PRIME(obj);
421 }
422 return RCLASS_EXT_READABLE_LOOKUP(obj, box);
423}
424
425static inline rb_classext_t *
426RCLASS_EXT_READABLE(VALUE obj)
427{
428 const rb_box_t *box;
429 if (RCLASS_PRIME_CLASSEXT_READABLE_P(obj)) {
430 return RCLASS_EXT_PRIME(obj);
431 }
432 // delay determining the current box to optimize for unmodified classes
433 box = rb_current_box();
434 if (BOX_MASTER_P(box)) {
435 return RCLASS_EXT_PRIME(obj);
436 }
437 return RCLASS_EXT_READABLE_LOOKUP(obj, box);
438}
439
440static inline rb_classext_t *
441RCLASS_EXT_WRITABLE_LOOKUP(VALUE obj, const rb_box_t *box)
442{
443 rb_classext_t *ext;
444 int first_set = 0;
445
446 ext = RCLASS_EXT_TABLE_LOOKUP_INTERNAL(obj, box);
447 if (ext)
448 return ext;
449
450 RB_VM_LOCKING() {
451 // re-check the classext is not created to avoid the multi-thread race
452 ext = RCLASS_EXT_TABLE_LOOKUP_INTERNAL(obj, box);
453 if (!ext) {
454 ext = rb_class_duplicate_classext(RCLASS_EXT_PRIME(obj), obj, box, &first_set);
455 if (first_set) {
456 // TODO: are there any case that a class/module become non-writable after its birthtime?
457 RCLASS_SET_PRIME_CLASSEXT_WRITABLE(obj, false);
458 }
459 }
460 }
461 return ext;
462}
463
464static inline rb_classext_t *
465RCLASS_EXT_WRITABLE_IN_BOX(VALUE obj, const rb_box_t *box)
466{
467 if (BOX_MASTER_P(box)
468 || RCLASS_PRIME_CLASSEXT_WRITABLE_P(obj)) {
469 return RCLASS_EXT_PRIME(obj);
470 }
471 return RCLASS_EXT_WRITABLE_LOOKUP(obj, box);
472}
473
474static inline rb_classext_t *
475RCLASS_EXT_WRITABLE(VALUE obj)
476{
477 const rb_box_t *box;
478 if (LIKELY(RCLASS_PRIME_CLASSEXT_WRITABLE_P(obj))) {
479 return RCLASS_EXT_PRIME(obj);
480 }
481 // delay determining the current box to optimize for unmodified classes
482 box = rb_current_box();
483 if (BOX_MASTER_P(box)) {
484 return RCLASS_EXT_PRIME(obj);
485 }
486 return RCLASS_EXT_WRITABLE_LOOKUP(obj, box);
487}
488
489static inline void
490RCLASSEXT_SET_ORIGIN(rb_classext_t *ext, VALUE klass, VALUE origin)
491{
492 RB_OBJ_WRITE(klass, &(RCLASSEXT_ORIGIN(ext)), origin);
493}
494
495static inline void
496RCLASSEXT_SET_INCLUDER(rb_classext_t *ext, VALUE klass, VALUE includer)
497{
499 RB_OBJ_WRITE(klass, &(RCLASSEXT_INCLUDER(ext)), includer);
500}
501
502/* class.c */
503typedef void rb_class_classext_foreach_callback_func(rb_classext_t *classext, bool is_prime, VALUE box_value, void *arg);
504void rb_class_classext_foreach(VALUE klass, rb_class_classext_foreach_callback_func *func, void *arg);
505void rb_class_subclass_add(VALUE super, VALUE klass);
506void rb_class_foreach_subclass(VALUE klass, void (*f)(VALUE, VALUE), VALUE);
507void rb_class_update_superclasses(VALUE);
508int rb_singleton_class_internal_p(VALUE sklass);
509VALUE rb_class_set_super(VALUE klass, VALUE super);
511VALUE rb_class_new_with_struct_inline_field(VALUE super);
512VALUE rb_class_alloc_with_struct_inline_field_of(VALUE from);
513VALUE rb_module_s_alloc(VALUE klass);
514void rb_class_set_initialized(VALUE klass);
515void rb_module_check_initializable(VALUE module);
516VALUE rb_make_metaclass(VALUE, VALUE);
517VALUE rb_include_class_new(VALUE, VALUE);
518VALUE rb_define_class_id_under_no_pin(VALUE outer, ID id, VALUE super);
519VALUE rb_obj_methods(int argc, const VALUE *argv, VALUE obj);
520VALUE rb_obj_protected_methods(int argc, const VALUE *argv, VALUE obj);
521VALUE rb_obj_private_methods(int argc, const VALUE *argv, VALUE obj);
522VALUE rb_obj_public_methods(int argc, const VALUE *argv, VALUE obj);
523VALUE rb_class_undefined_instance_methods(VALUE mod);
524VALUE rb_special_singleton_class(VALUE);
525VALUE rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach);
527void rb_undef_methods_from(VALUE klass, VALUE super);
529VALUE rb_keyword_error_new(const char *, VALUE);
530
531rb_classext_t *rb_class_unlink_classext(VALUE klass, const rb_box_t *box);
532void rb_class_classext_free(VALUE klass, rb_classext_t *ext, bool is_prime);
533void rb_iclass_classext_free(VALUE klass, rb_classext_t *ext, bool is_prime);
534void rb_freeze_singleton_class(VALUE attached_object);
535
536RUBY_SYMBOL_EXPORT_BEGIN
537
538/* for objspace */
540VALUE rb_class_singleton_p(VALUE klass);
541unsigned char rb_class_variation_count(VALUE klass);
542
543RUBY_SYMBOL_EXPORT_END
544
545static inline bool
546RCLASS_SINGLETON_P(VALUE klass)
547{
548 RUBY_ASSERT(RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_MODULE) || RB_TYPE_P(klass, T_ICLASS));
549 return RB_BUILTIN_TYPE(klass) == T_CLASS && FL_TEST_RAW(klass, FL_SINGLETON);
550}
551
552static inline void
553RCLASS_SET_SUPER(VALUE klass, VALUE super)
554{
555 RB_OBJ_WRITE(klass, &RCLASSEXT_SUPER(RCLASS_EXT_PRIME(klass)), super);
556}
557
558static inline void
559RCLASS_WRITE_SUPER(VALUE klass, VALUE super)
560{
561 RB_OBJ_WRITE(klass, &RCLASSEXT_SUPER(RCLASS_EXT_WRITABLE(klass)), super);
562}
563
564static inline VALUE
565RCLASS_WRITABLE_FIELDS_OBJ(VALUE obj)
566{
568 return RCLASSEXT_FIELDS_OBJ(RCLASS_EXT_WRITABLE(obj));
569}
570
571static inline void
572RCLASSEXT_SET_FIELDS_OBJ(VALUE obj, rb_classext_t *ext, VALUE fields_obj)
573{
575
576 RB_OBJ_ATOMIC_WRITE(obj, &ext->fields_obj, fields_obj);
577}
578
579static inline void
580RCLASS_WRITABLE_SET_FIELDS_OBJ(VALUE obj, VALUE fields_obj)
581{
583
584 RCLASSEXT_SET_FIELDS_OBJ(obj, RCLASS_EXT_WRITABLE(obj), fields_obj);
585}
586
587static inline uint32_t
588RCLASS_FIELDS_COUNT(VALUE obj)
589{
591
592 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
593 if (fields_obj) {
594 if (rb_obj_shape_complex_p(fields_obj)) {
595 return (uint32_t)rb_st_table_size(rb_imemo_fields_complex_tbl(fields_obj));
596 }
597 else {
598 return RSHAPE_LEN(RBASIC_SHAPE_ID(fields_obj));
599 }
600 }
601 return 0;
602}
603
604static inline void
605RCLASS_SET_M_TBL(VALUE klass, struct rb_id_table *table)
606{
607 RCLASSEXT_M_TBL(RCLASS_EXT_PRIME(klass)) = table;
608}
609
610static inline void
611RCLASS_WRITE_M_TBL(VALUE klass, struct rb_id_table *table)
612{
613 RCLASSEXT_M_TBL(RCLASS_EXT_WRITABLE(klass)) = table;
614}
615
616static inline void
617RCLASS_SET_CONST_TBL(VALUE klass, struct rb_id_table *table, bool shared)
618{
619 rb_classext_t *ext = RCLASS_EXT_PRIME(klass);
620 RCLASSEXT_CONST_TBL(ext) = table;
621 if (shared)
622 RCLASSEXT_SHARED_CONST_TBL(ext) = true;
623}
624
625static inline void
626RCLASS_WRITE_CONST_TBL(VALUE klass, struct rb_id_table *table, bool shared)
627{
628 rb_classext_t *ext = RCLASS_EXT_WRITABLE(klass);
629 RCLASSEXT_CONST_TBL(ext) = table;
630 if (shared)
631 RCLASSEXT_SHARED_CONST_TBL(ext) = true;
632}
633
634static inline void
635RCLASS_WRITE_CALLABLE_M_TBL(VALUE klass, struct rb_id_table *table)
636{
637 RCLASSEXT_CALLABLE_M_TBL(RCLASS_EXT_WRITABLE(klass)) = table;
638}
639
640static inline void
641RCLASS_WRITE_CC_TBL(VALUE klass, VALUE table)
642{
643 RB_OBJ_ATOMIC_WRITE(klass, &RCLASSEXT_CC_TBL(RCLASS_EXT_WRITABLE(klass)), table);
644}
645
646static inline void
647RCLASS_SET_CVC_TBL(VALUE klass, VALUE table)
648{
649 RB_OBJ_ATOMIC_WRITE(klass, &RCLASSEXT_CVC_TBL(RCLASS_EXT_PRIME(klass)), table);
650}
651
652static inline void
653RCLASS_WRITE_CVC_TBL(VALUE klass, VALUE table)
654{
655 RB_OBJ_ATOMIC_WRITE(klass, &RCLASSEXT_CVC_TBL(RCLASS_EXT_WRITABLE(klass)), table);
656}
657
658static inline void
659RCLASS_SET_REFINED_CLASS(VALUE klass, VALUE refined)
660{
661 RB_OBJ_WRITE(klass, &RCLASSEXT_REFINED_CLASS(RCLASS_EXT_PRIME(klass)), refined);
662}
663
664static inline rb_alloc_func_t
665RCLASS_ALLOCATOR(VALUE klass)
666{
667 RBIMPL_ASSERT_TYPE(klass, T_CLASS);
668 RUBY_ASSERT(!RCLASS_SINGLETON_P(klass));
669 return RCLASS_EXT_PRIME(klass)->as.class.allocator;
670}
671
672static inline void
673RCLASS_SET_ALLOCATOR(VALUE klass, rb_alloc_func_t allocator)
674{
676 RUBY_ASSERT(!RCLASS_SINGLETON_P(klass));
677 RCLASS_EXT_PRIME(klass)->as.class.allocator = allocator; // Allocator is set only on the initial definition
678}
679
680static inline void
681RCLASS_SET_ORIGIN(VALUE klass, VALUE origin)
682{
683 rb_classext_t *ext = RCLASS_EXT_PRIME(klass);
684 RB_OBJ_WRITE(klass, &RCLASSEXT_ORIGIN(ext), origin);
685 if (klass != origin) RCLASSEXT_ICLASS_IS_ORIGIN(RCLASS_EXT_WRITABLE(origin)) = true;
686}
687
688static inline void
689RCLASS_WRITE_ORIGIN(VALUE klass, VALUE origin)
690{
691 rb_classext_t *ext = RCLASS_EXT_WRITABLE(klass);
692 RB_OBJ_WRITE(klass, &RCLASSEXT_ORIGIN(ext), origin);
693 if (klass != origin) RCLASSEXT_ICLASS_IS_ORIGIN(RCLASS_EXT_WRITABLE(origin)) = true;
694}
695
696static inline void
697RICLASS_SET_ORIGIN_SHARED_MTBL(VALUE iclass)
698{
699 RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(RCLASS_EXT_PRIME(iclass)) = true;
700}
701
702static inline void
703RICLASS_WRITE_ORIGIN_SHARED_MTBL(VALUE iclass)
704{
705 RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(RCLASS_EXT_WRITABLE(iclass)) = true;
706}
707
708static inline bool
709RICLASS_OWNS_M_TBL_P(VALUE iclass)
710{
711 rb_classext_t *ext = RCLASS_EXT_READABLE(iclass);
712 return RCLASSEXT_ICLASS_IS_ORIGIN(ext) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext);
713}
714
715static inline bool
716RICLASS_FOR_REFINEMENT_P(VALUE iclass)
717{
718 return BUILTIN_TYPE(iclass) == T_ICLASS &&
719 RB_TYPE_P(RBASIC(iclass)->klass, T_MODULE) &&
720 FL_TEST_RAW(RBASIC(iclass)->klass, RMODULE_IS_REFINEMENT);
721}
722
723static inline void
724RCLASS_SET_INCLUDER(VALUE iclass, VALUE klass)
725{
727 RB_OBJ_WRITE(iclass, &RCLASS_INCLUDER(iclass), klass);
728}
729
730static inline void
731RCLASS_SET_MODULE_REFINEMENT_ICLASSES(VALUE iclass, VALUE table)
732{
734 RB_OBJ_WRITE(iclass, &RCLASS_MODULE_REFINEMENT_ICLASSES(iclass), table);
735}
736
737static inline void
738RCLASS_WRITE_SUPERCLASSES(VALUE klass, size_t depth, VALUE *superclasses, bool with_self)
739{
740 RUBY_ASSERT(depth <= RCLASS_MAX_SUPERCLASS_DEPTH);
741
742 rb_classext_t *ext = RCLASS_EXT_PRIME(klass);
743 RCLASSEXT_SUPERCLASS_DEPTH(ext) = depth;
744 RCLASSEXT_SUPERCLASSES(ext) = superclasses;
745 RCLASSEXT_SUPERCLASSES_WITH_SELF(ext) = with_self;
746}
747
748static inline void
749RCLASS_SET_SUBCLASSES(VALUE klass, VALUE subclasses)
750{
751 rb_classext_t *ext = RCLASS_EXT_PRIME(klass);
752 RB_OBJ_WRITE(klass, &RCLASSEXT_SUBCLASSES(ext), subclasses);
753}
754
755static inline void
756RCLASS_SET_CLASSPATH(VALUE klass, VALUE classpath, bool permanent)
757{
758 rb_classext_t *ext = RCLASS_EXT_READABLE(klass);
759 assert(BUILTIN_TYPE(klass) == T_CLASS || BUILTIN_TYPE(klass) == T_MODULE);
760 assert(classpath == 0 || BUILTIN_TYPE(classpath) == T_STRING);
761 assert(FL_TEST_RAW(classpath, RUBY_FL_SHAREABLE));
762
763 RB_OBJ_WRITE(klass, &(RCLASSEXT_CLASSPATH(ext)), classpath);
764 RCLASSEXT_PERMANENT_CLASSPATH(ext) = permanent;
765}
766
767static inline void
768RCLASS_WRITE_CLASSPATH(VALUE klass, VALUE classpath, bool permanent)
769{
770 rb_classext_t *ext = RCLASS_EXT_WRITABLE(klass);
771 assert(BUILTIN_TYPE(klass) == T_CLASS || BUILTIN_TYPE(klass) == T_MODULE);
772 assert(classpath == 0 || BUILTIN_TYPE(classpath) == T_STRING);
773 assert(!RB_FL_ABLE(classpath) || FL_TEST_RAW(classpath, RUBY_FL_SHAREABLE));
774
775 RB_OBJ_WRITE(klass, &(RCLASSEXT_CLASSPATH(ext)), classpath);
776 RCLASSEXT_PERMANENT_CLASSPATH(ext) = permanent;
777}
778
779static inline VALUE
780RCLASS_SET_ATTACHED_OBJECT(VALUE klass, VALUE attached_object)
781{
782 assert(RCLASS_SINGLETON_P(klass));
783
784 RB_OBJ_WRITE(klass, &RCLASS_EXT_PRIME(klass)->as.singleton_class.attached_object, attached_object);
785 return attached_object;
786}
787
788static inline void
789RCLASS_SET_MAX_IV_COUNT(VALUE klass, attr_index_t count)
790{
791 RUBY_ASSERT(klass != rb_cObject);
793
794 RCLASS_MAX_IV_COUNT(klass) = count;
795}
796
797static inline void
798RCLASS_SET_EXPECT_NO_IVAR(VALUE klass)
799{
800 RCLASS_EXT_PRIME(klass)->expect_no_ivar = true;
801}
802
803static inline bool
804RCLASS_EXPECT_NO_IVAR(VALUE klass)
805{
806 return RCLASS_EXT_PRIME(klass)->expect_no_ivar;
807}
808
809static inline bool
810RCLASS_INITIALIZED_P(VALUE klass)
811{
812 VM_ASSERT(RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_MODULE));
813 return FL_TEST_RAW(klass, RCLASS_IS_INITIALIZED);
814}
815
816#endif /* INTERNAL_CLASS_H */
static bool RB_FL_ABLE(VALUE obj)
Checks if the object is flaggable.
Definition fl_type.h:384
@ RUBY_FL_SHAREABLE
This flag has something to do with Ractor.
Definition fl_type.h:253
VALUE rb_class_super_of(VALUE klass)
Internal header for Objspace.
Definition class.c:464
VALUE rb_class_boot(VALUE)
A utility function that wraps class_alloc.
Definition class.c:776
VALUE rb_class_inherited(VALUE, VALUE)
Calls Class::inherited.
Definition class.c:1565
void rb_freeze_singleton_class(VALUE attached_object)
This is an implementation detail of RB_OBJ_FREEZE().
Definition class.c:3024
VALUE rb_singleton_class_get(VALUE obj)
Returns the singleton class of obj, or nil if obj is not a singleton object.
Definition class.c:3047
#define FL_SINGLETON
Old name of RUBY_FL_SINGLETON.
Definition fl_type.h:58
#define FL_UNSET_RAW
Old name of RB_FL_UNSET_RAW.
Definition fl_type.h:130
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
#define T_MODULE
Old name of RUBY_T_MODULE.
Definition value_type.h:70
#define T_ICLASS
Old name of RUBY_T_ICLASS.
Definition value_type.h:66
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:128
#define T_CLASS
Old name of RUBY_T_CLASS.
Definition value_type.h:58
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition value_type.h:85
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
Definition fl_type.h:126
VALUE rb_cObject
Object class.
Definition object.c:60
VALUE rb_cBasicObject
BasicObject class.
Definition object.c:58
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:492
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
#define RBIMPL_ASSUME(_)
Wraps (or simulates) __builtin_unreachable.
Definition assume.h:76
VALUE(* rb_alloc_func_t)(VALUE klass)
This is the type of functions that ruby calls when trying to allocate an object.
Definition vm.h:219
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
C99 shim for <stdbool.h>.
Ruby object's base components.
Definition rbasic.h:69
Definition class.h:91
VALUE subclasses
imemo_subclasses VALUE tracking this class's subclasses.
Definition class.h:53
Internal header for Class.
Definition class.h:31
Definition st.h:79
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static enum ruby_value_type RB_BUILTIN_TYPE(VALUE obj)
Queries the type of the object.
Definition value_type.h:182
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376
@ RUBY_T_MODULE
Definition value_type.h:118
@ RUBY_T_CLASS
Definition value_type.h:117