Ruby 4.1.0dev (2026-01-02 revision 0ec5678cd55aa0f2d900d028497bb444c4ce4b7b)
class.c (0ec5678cd55aa0f2d900d028497bb444c4ce4b7b)
1/**********************************************************************
2
3 class.c -
4
5 $Author$
6 created at: Tue Aug 10 15:05:44 JST 1993
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9
10**********************************************************************/
11
17#include "ruby/internal/config.h"
18#include <ctype.h>
19
20#include "constant.h"
21#include "debug_counter.h"
22#include "id_table.h"
23#include "internal.h"
24#include "internal/box.h"
25#include "internal/class.h"
26#include "internal/eval.h"
27#include "internal/hash.h"
28#include "internal/object.h"
29#include "internal/string.h"
30#include "internal/variable.h"
31#include "ruby/st.h"
32#include "vm_core.h"
33#include "ruby/ractor.h"
34#include "yjit.h"
35#include "zjit.h"
36
37/* Flags of T_CLASS
38 *
39 * 0: RCLASS_IS_ROOT
40 * The class has been added to the VM roots. Will always be marked and pinned.
41 * This is done for classes defined from C to allow storing them in global variables.
42 * 1: RUBY_FL_SINGLETON
43 * This class is a singleton class.
44 * 2: RCLASS_PRIME_CLASSEXT_PRIME_WRITABLE
45 * This class's prime classext is the only classext and writable from any boxes.
46 * If unset, the prime classext is writable only from the root box.
47 * 3: RCLASS_IS_INITIALIZED
48 * Class has been initialized.
49 * 4: RCLASS_BOXABLE
50 * Is a builtin class that may be boxed. It larger than a normal class.
51 */
52
53/* Flags of T_ICLASS
54 *
55 * 2: RCLASS_PRIME_CLASSEXT_PRIME_WRITABLE
56 * This module's prime classext is the only classext and writable from any boxes.
57 * If unset, the prime classext is writable only from the root box.
58 * 4: RCLASS_BOXABLE
59 * Is a builtin class that may be boxed. It larger than a normal class.
60 */
61
62/* Flags of T_MODULE
63 *
64 * 0: RCLASS_IS_ROOT
65 * The class has been added to the VM roots. Will always be marked and pinned.
66 * This is done for classes defined from C to allow storing them in global variables.
67 * 1: <reserved>
68 * Ensures that RUBY_FL_SINGLETON is never set on a T_MODULE. See `rb_class_real`.
69 * 2: RCLASS_PRIME_CLASSEXT_PRIME_WRITABLE
70 * This module's prime classext is the only classext and writable from any boxes.
71 * If unset, the prime classext is writable only from the root box.
72 * 3: RCLASS_IS_INITIALIZED
73 * Module has been initialized.
74 * 4: RCLASS_BOXABLE
75 * Is a builtin class that may be boxed. It larger than a normal class.
76 * 5: RMODULE_IS_REFINEMENT
77 * Module is used for refinements.
78 */
79
80#define METACLASS_OF(k) RBASIC(k)->klass
81#define SET_METACLASS_OF(k, cls) RBASIC_SET_CLASS(k, cls)
82
83static enum rb_id_table_iterator_result
84cvar_table_free_i(VALUE value, void *ctx)
85{
86 xfree((void *)value);
87 return ID_TABLE_CONTINUE;
88}
89
91rb_class_unlink_classext(VALUE klass, const rb_box_t *box)
92{
93 st_data_t ext;
94 st_data_t key = (st_data_t)box->box_object;
95 VALUE obj_id = rb_obj_id(klass);
96 st_delete(box->classext_cow_classes, &obj_id, 0);
97 st_delete(RCLASS_CLASSEXT_TBL(klass), &key, &ext);
98 return (rb_classext_t *)ext;
99}
100
101void
102rb_class_classext_free(VALUE klass, rb_classext_t *ext, bool is_prime)
103{
104 struct rb_id_table *tbl;
105
106 rb_id_table_free(RCLASSEXT_M_TBL(ext));
107
108 if (!RCLASSEXT_SHARED_CONST_TBL(ext) && (tbl = RCLASSEXT_CONST_TBL(ext)) != NULL) {
109 rb_free_const_table(tbl);
110 }
111
112 if ((tbl = RCLASSEXT_CVC_TBL(ext)) != NULL) {
113 rb_id_table_foreach_values(tbl, cvar_table_free_i, NULL);
114 rb_id_table_free(tbl);
115 }
116
117 rb_class_classext_free_subclasses(ext, klass, false);
118
119 if (RCLASSEXT_SUPERCLASSES_WITH_SELF(ext)) {
120 RUBY_ASSERT(is_prime); // superclasses should only be used on prime
121 xfree(RCLASSEXT_SUPERCLASSES(ext));
122 }
123
124 if (!is_prime) { // the prime classext will be freed with RClass
125 xfree(ext);
126 }
127}
128
129void
130rb_iclass_classext_free(VALUE klass, rb_classext_t *ext, bool is_prime)
131{
132 if (RCLASSEXT_ICLASS_IS_ORIGIN(ext) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext)) {
133 /* Method table is not shared for origin iclasses of classes */
134 rb_id_table_free(RCLASSEXT_M_TBL(ext));
135 }
136
137 if (RCLASSEXT_CALLABLE_M_TBL(ext) != NULL) {
138 rb_id_table_free(RCLASSEXT_CALLABLE_M_TBL(ext));
139 }
140
141 rb_class_classext_free_subclasses(ext, klass, false);
142
143 if (!is_prime) { // the prime classext will be freed with RClass
144 xfree(ext);
145 }
146}
147
148static void
149iclass_free_orphan_classext(VALUE klass, rb_classext_t *ext)
150{
151 if (RCLASSEXT_ICLASS_IS_ORIGIN(ext) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext)) {
152 /* Method table is not shared for origin iclasses of classes */
153 rb_id_table_free(RCLASSEXT_M_TBL(ext));
154 }
155
156 if (RCLASSEXT_CALLABLE_M_TBL(ext) != NULL) {
157 rb_id_table_free(RCLASSEXT_CALLABLE_M_TBL(ext));
158 }
159
160 rb_class_classext_free_subclasses(ext, klass, true); // replacing this classext with a newer one
161
162 xfree(ext);
163}
164
166 VALUE obj;
167 rb_classext_t *ext;
168};
169
170static int
171set_box_classext_update(st_data_t *key_ptr, st_data_t *val_ptr, st_data_t a, int existing)
172{
174
175 if (existing) {
176 if (LIKELY(BUILTIN_TYPE(args->obj) == T_ICLASS)) {
177 iclass_free_orphan_classext(args->obj, (rb_classext_t *)*val_ptr);
178 }
179 else {
180 rb_bug("Updating existing classext for non-iclass never happen");
181 }
182 }
183
184 *val_ptr = (st_data_t)args->ext;
185
186 return ST_CONTINUE;
187}
188
189void
190rb_class_set_box_classext(VALUE obj, const rb_box_t *box, rb_classext_t *ext)
191{
192 struct rb_class_set_box_classext_args args = {
193 .obj = obj,
194 .ext = ext,
195 };
196
197 VM_ASSERT(BOX_USER_P(box));
198
199 st_update(RCLASS_CLASSEXT_TBL(obj), (st_data_t)box->box_object, set_box_classext_update, (st_data_t)&args);
200 st_insert(box->classext_cow_classes, (st_data_t)rb_obj_id(obj), obj);
201
202 // FIXME: This is done here because this is the first time the objects in
203 // the classext are exposed via this class. It's likely that if GC
204 // compaction occurred between the VALUEs being copied in and this
205 // writebarrier trigger the values will be stale.
206 rb_gc_writebarrier_remember(obj);
207}
208
209RUBY_EXTERN rb_serial_t ruby_vm_global_cvar_state;
210
212 struct rb_id_table *tbl;
213 VALUE klass;
214};
215
216static enum rb_id_table_iterator_result
217duplicate_classext_id_table_i(ID key, VALUE value, void *data)
218{
219 struct rb_id_table *tbl = (struct rb_id_table *)data;
220 rb_id_table_insert(tbl, key, value);
221 return ID_TABLE_CONTINUE;
222}
223
224static enum rb_id_table_iterator_result
225duplicate_classext_m_tbl_i(ID key, VALUE value, void *data)
226{
227 struct duplicate_id_tbl_data *arg = (struct duplicate_id_tbl_data *)data;
229 rb_method_table_insert0(arg->klass, arg->tbl, key, me, false);
230 return ID_TABLE_CONTINUE;
231}
232
233static struct rb_id_table *
234duplicate_classext_m_tbl(struct rb_id_table *orig, VALUE klass, bool init_missing)
235{
236 struct rb_id_table *tbl;
237 if (!orig) {
238 if (init_missing)
239 return rb_id_table_create(0);
240 else
241 return NULL;
242 }
243 tbl = rb_id_table_create(rb_id_table_size(orig));
244 struct duplicate_id_tbl_data data = {
245 .tbl = tbl,
246 .klass = klass,
247 };
248 rb_id_table_foreach(orig, duplicate_classext_m_tbl_i, &data);
249 return tbl;
250}
251
252static struct rb_id_table *
253duplicate_classext_id_table(struct rb_id_table *orig, bool init_missing)
254{
255 struct rb_id_table *tbl;
256
257 if (!orig) {
258 if (init_missing)
259 return rb_id_table_create(0);
260 else
261 return NULL;
262 }
263 tbl = rb_id_table_create(rb_id_table_size(orig));
264 rb_id_table_foreach(orig, duplicate_classext_id_table_i, tbl);
265 return tbl;
266}
267
268static rb_const_entry_t *
269duplicate_classext_const_entry(rb_const_entry_t *src, VALUE klass)
270{
271 // See also: setup_const_entry (variable.c)
273
274 dst->flag = src->flag;
275 dst->line = src->line;
276 RB_OBJ_WRITE(klass, &dst->value, src->value);
277 RB_OBJ_WRITE(klass, &dst->file, src->file);
278
279 return dst;
280}
281
282static enum rb_id_table_iterator_result
283duplicate_classext_const_tbl_i(ID key, VALUE value, void *data)
284{
285 struct duplicate_id_tbl_data *arg = (struct duplicate_id_tbl_data *)data;
286 rb_const_entry_t *entry = duplicate_classext_const_entry((rb_const_entry_t *)value, arg->klass);
287
288 rb_id_table_insert(arg->tbl, key, (VALUE)entry);
289
290 return ID_TABLE_CONTINUE;
291}
292
293static struct rb_id_table *
294duplicate_classext_const_tbl(struct rb_id_table *src, VALUE klass)
295{
296 struct rb_id_table *dst;
297
298 if (!src)
299 return NULL;
300
301 dst = rb_id_table_create(rb_id_table_size(src));
302
303 struct duplicate_id_tbl_data data = {
304 .tbl = dst,
305 .klass = klass,
306 };
307 rb_id_table_foreach(src, duplicate_classext_const_tbl_i, (void *)&data);
308
309 return dst;
310}
311
312static VALUE
313box_subclasses_tbl_key(const rb_box_t *box)
314{
315 if (!box){
316 return 0;
317 }
318 return (VALUE)box->box_id;
319}
320
321static void
322duplicate_classext_subclasses(rb_classext_t *orig, rb_classext_t *copy)
323{
324 rb_subclass_anchor_t *anchor, *orig_anchor;
325 rb_subclass_entry_t *head, *cur, *cdr, *entry, *first = NULL;
326 rb_box_subclasses_t *box_subclasses;
327 struct st_table *tbl;
328
329 if (RCLASSEXT_SUBCLASSES(orig)) {
330 orig_anchor = RCLASSEXT_SUBCLASSES(orig);
331 box_subclasses = orig_anchor->box_subclasses;
332 tbl = ((rb_box_subclasses_t *)box_subclasses)->tbl;
333
335 anchor->box_subclasses = rb_box_subclasses_ref_inc(box_subclasses);
336
338 anchor->head = head;
339
340 RCLASSEXT_SUBCLASSES(copy) = anchor;
341
342 cur = head;
343 entry = orig_anchor->head;
344 RUBY_ASSERT(!entry->klass);
345 // The head entry has NULL klass always. See rb_class_foreach_subclass().
346 entry = entry->next;
347 while (entry) {
348 if (rb_objspace_garbage_object_p(entry->klass)) {
349 entry = entry->next;
350 continue;
351 }
353 cdr->klass = entry->klass;
354 cdr->prev = cur;
355 cur->next = cdr;
356 if (!first) {
357 VALUE box_id = box_subclasses_tbl_key(RCLASSEXT_BOX(copy));
358 first = cdr;
359 st_insert(tbl, box_id, (st_data_t)first);
360 }
361 cur = cdr;
362 entry = entry->next;
363 }
364 }
365
366 if (RCLASSEXT_BOX_SUPER_SUBCLASSES(orig))
367 RCLASSEXT_BOX_SUPER_SUBCLASSES(copy) = rb_box_subclasses_ref_inc(RCLASSEXT_BOX_SUPER_SUBCLASSES(orig));
368 if (RCLASSEXT_BOX_MODULE_SUBCLASSES(orig))
369 RCLASSEXT_BOX_MODULE_SUBCLASSES(copy) = rb_box_subclasses_ref_inc(RCLASSEXT_BOX_MODULE_SUBCLASSES(orig));
370}
371
372static void
373class_duplicate_iclass_classext(VALUE iclass, rb_classext_t *mod_ext, const rb_box_t *box)
374{
376
377 rb_classext_t *src = RCLASS_EXT_PRIME(iclass);
378 rb_classext_t *ext = RCLASS_EXT_TABLE_LOOKUP_INTERNAL(iclass, box);
379 int first_set = 0;
380
381 if (ext) {
382 // iclass classext for the ns is only for cc/callable_m_tbl if it's created earlier than module's one
383 rb_invalidate_method_caches(RCLASSEXT_CALLABLE_M_TBL(ext), RCLASSEXT_CC_TBL(ext));
384 }
385
386 ext = ZALLOC(rb_classext_t);
387
388 RCLASSEXT_BOX(ext) = box;
389
390 RCLASSEXT_SUPER(ext) = RCLASSEXT_SUPER(src);
391
392 // See also: rb_include_class_new()
393 if (RCLASSEXT_ICLASS_IS_ORIGIN(src) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(src)) {
394 RCLASSEXT_M_TBL(ext) = duplicate_classext_m_tbl(RCLASSEXT_M_TBL(src), iclass, true);
395 }
396 else {
397 RCLASSEXT_M_TBL(ext) = RCLASSEXT_M_TBL(mod_ext);
398 }
399
400 RCLASSEXT_CONST_TBL(ext) = RCLASSEXT_CONST_TBL(mod_ext);
401 RCLASSEXT_CVC_TBL(ext) = RCLASSEXT_CVC_TBL(mod_ext);
402
403 // Those are cache and should be recreated when methods are called
404 // RCLASSEXT_CALLABLE_M_TBL(ext) = NULL;
405 // RCLASSEXT_CC_TBL(ext) = NULL;
406
407 // subclasses, box_super_subclasses_tbl, box_module_subclasses_tbl
408 duplicate_classext_subclasses(src, ext);
409
410 RCLASSEXT_SET_ORIGIN(ext, iclass, RCLASSEXT_ORIGIN(src));
411 RCLASSEXT_ICLASS_IS_ORIGIN(ext) = RCLASSEXT_ICLASS_IS_ORIGIN(src);
412 RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext) = RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(src);
413
414 RCLASSEXT_SET_INCLUDER(ext, iclass, RCLASSEXT_INCLUDER(src));
415
416 VM_ASSERT(FL_TEST_RAW(iclass, RCLASS_BOXABLE));
417
418 first_set = RCLASS_SET_BOX_CLASSEXT(iclass, box, ext);
419 if (first_set) {
420 RCLASS_SET_PRIME_CLASSEXT_WRITABLE(iclass, false);
421 }
422}
423
425rb_class_duplicate_classext(rb_classext_t *orig, VALUE klass, const rb_box_t *box)
426{
427 VM_ASSERT(RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_MODULE) || RB_TYPE_P(klass, T_ICLASS));
428
430 bool dup_iclass = RB_TYPE_P(klass, T_MODULE) ? true : false;
431
432 RCLASSEXT_BOX(ext) = box;
433
434 RCLASSEXT_SUPER(ext) = RCLASSEXT_SUPER(orig);
435
436 RCLASSEXT_M_TBL(ext) = duplicate_classext_m_tbl(RCLASSEXT_M_TBL(orig), klass, dup_iclass);
437 RCLASSEXT_ICLASS_IS_ORIGIN(ext) = true;
438 RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext) = false;
439
440 if (orig->fields_obj) {
441 RB_OBJ_WRITE(klass, &ext->fields_obj, rb_imemo_fields_clone(orig->fields_obj));
442 }
443
444 if (RCLASSEXT_SHARED_CONST_TBL(orig)) {
445 RCLASSEXT_CONST_TBL(ext) = RCLASSEXT_CONST_TBL(orig);
446 RCLASSEXT_SHARED_CONST_TBL(ext) = true;
447 }
448 else {
449 RCLASSEXT_CONST_TBL(ext) = duplicate_classext_const_tbl(RCLASSEXT_CONST_TBL(orig), klass);
450 RCLASSEXT_SHARED_CONST_TBL(ext) = false;
451 }
452 /*
453 * callable_m_tbl is for `super` chain, and entries will be created when the super chain is called.
454 * so initially, it can be NULL and let it be created lazily.
455 * RCLASSEXT_CALLABLE_M_TBL(ext) = NULL;
456 *
457 * cc_tbl is for method inline cache, and method calls from different boxes never occur on
458 * the same code, so the copied classext should have a different cc_tbl from the prime one.
459 * RCLASSEXT_CC_TBL(copy) = NULL
460 */
461
462 RCLASSEXT_CVC_TBL(ext) = duplicate_classext_id_table(RCLASSEXT_CVC_TBL(orig), dup_iclass);
463
464 // subclasses, subclasses_index
465 duplicate_classext_subclasses(orig, ext);
466
467 RCLASSEXT_SET_ORIGIN(ext, klass, RCLASSEXT_ORIGIN(orig));
468 /*
469 * Members not copied to box's classext values
470 * * refined_class
471 * * as.class.allocator / as.singleton_class.attached_object
472 * * includer
473 * * max IV count
474 * * variation count
475 */
476 RCLASSEXT_PERMANENT_CLASSPATH(ext) = RCLASSEXT_PERMANENT_CLASSPATH(orig);
477 RCLASSEXT_CLONED(ext) = RCLASSEXT_CLONED(orig);
478 RCLASSEXT_CLASSPATH(ext) = RCLASSEXT_CLASSPATH(orig);
479
480 /* For the usual T_CLASS/T_MODULE, iclass flags are always false */
481
482 if (dup_iclass) {
483 VALUE iclass;
484 /*
485 * ICLASS has the same m_tbl/const_tbl/cvc_tbl with the included module.
486 * So the module's classext is copied, its tables should be also referred
487 * by the ICLASS's classext for the box.
488 */
489 rb_subclass_anchor_t *anchor = RCLASSEXT_SUBCLASSES(ext);
490 rb_subclass_entry_t *subclass_entry = anchor->head;
491 while (subclass_entry) {
492 if (subclass_entry->klass && RB_TYPE_P(subclass_entry->klass, T_ICLASS)) {
493 iclass = subclass_entry->klass;
494 VM_ASSERT(RB_TYPE_P(iclass, T_ICLASS));
495 if (RBASIC_CLASS(iclass) == klass) {
496 // Is the subclass an ICLASS including this module into another class
497 // If so we need to re-associate it under our box with the new ext
498 VM_ASSERT(FL_TEST_RAW(iclass, RCLASS_BOXABLE));
499 class_duplicate_iclass_classext(iclass, ext, box);
500 }
501 }
502 subclass_entry = subclass_entry->next;
503 }
504 }
505
506 return ext;
507}
508
509void
510rb_class_ensure_writable(VALUE klass)
511{
512 VM_ASSERT(RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_MODULE) || RB_TYPE_P(klass, T_ICLASS));
513 RCLASS_EXT_WRITABLE(klass);
514}
515
517 rb_class_classext_foreach_callback_func *func;
518 void * callback_arg;
519};
520
521static int
522class_classext_foreach_i(st_data_t key, st_data_t value, st_data_t arg)
523{
525 rb_class_classext_foreach_callback_func *func = foreach_arg->func;
526 func((rb_classext_t *)value, false, (VALUE)key, foreach_arg->callback_arg);
527 return ST_CONTINUE;
528}
529
530void
531rb_class_classext_foreach(VALUE klass, rb_class_classext_foreach_callback_func *func, void *arg)
532{
533 st_table *tbl = RCLASS_CLASSEXT_TBL(klass);
535 if (tbl) {
536 foreach_arg.func = func;
537 foreach_arg.callback_arg = arg;
538 rb_st_foreach(tbl, class_classext_foreach_i, (st_data_t)&foreach_arg);
539 }
540 func(RCLASS_EXT_PRIME(klass), true, (VALUE)NULL, arg);
541}
542
543VALUE
544rb_class_super_of(VALUE klass)
545{
546 return RCLASS_SUPER(klass);
547}
548
549VALUE
550rb_class_singleton_p(VALUE klass)
551{
552 return RCLASS_SINGLETON_P(klass);
553}
554
555unsigned char
556rb_class_variation_count(VALUE klass)
557{
558 return RCLASS_VARIATION_COUNT(klass);
559}
560
561static void
562push_subclass_entry_to_list(VALUE super, VALUE klass, bool is_module)
563{
564 rb_subclass_entry_t *entry, *head;
565 rb_subclass_anchor_t *anchor;
566 rb_box_subclasses_t *box_subclasses;
567 struct st_table *tbl;
568 const rb_box_t *box = rb_current_box();
569
571 entry->klass = klass;
572
573 RB_VM_LOCKING() {
574 anchor = RCLASS_WRITABLE_SUBCLASSES(super);
575 VM_ASSERT(anchor);
576 box_subclasses = (rb_box_subclasses_t *)anchor->box_subclasses;
577 VM_ASSERT(box_subclasses);
578 tbl = box_subclasses->tbl;
579 VM_ASSERT(tbl);
580
581 head = anchor->head;
582 if (head->next) {
583 head->next->prev = entry;
584 entry->next = head->next;
585 }
586 head->next = entry;
587 entry->prev = head;
588 st_insert(tbl, box_subclasses_tbl_key(box), (st_data_t)entry);
589 }
590
591 if (is_module) {
592 RCLASS_WRITE_BOX_MODULE_SUBCLASSES(klass, anchor->box_subclasses);
593 }
594 else {
595 RCLASS_WRITE_BOX_SUPER_SUBCLASSES(klass, anchor->box_subclasses);
596 }
597}
598
599void
600rb_class_subclass_add(VALUE super, VALUE klass)
601{
602 if (super && !UNDEF_P(super)) {
603 push_subclass_entry_to_list(super, klass, false);
604 }
605}
606
607static void
608rb_module_add_to_subclasses_list(VALUE module, VALUE iclass)
609{
610 if (module && !UNDEF_P(module)) {
611 push_subclass_entry_to_list(module, iclass, true);
612 }
613}
614
615static struct rb_subclass_entry *
616class_get_subclasses_for_ns(struct st_table *tbl, VALUE box_id)
617{
618 st_data_t value;
619 if (st_lookup(tbl, (st_data_t)box_id, &value)) {
620 return (struct rb_subclass_entry *)value;
621 }
622 return NULL;
623}
624
625static int
626remove_class_from_subclasses_replace_first_entry(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
627{
628 *value = arg;
629 return ST_CONTINUE;
630}
631
632static void
633remove_class_from_subclasses(struct st_table *tbl, VALUE box_id, VALUE klass)
634{
635 rb_subclass_entry_t *entry = class_get_subclasses_for_ns(tbl, box_id);
636 bool first_entry = true;
637 while (entry) {
638 if (entry->klass == klass) {
639 rb_subclass_entry_t *prev = entry->prev, *next = entry->next;
640
641 if (prev) {
642 prev->next = next;
643 }
644 if (next) {
645 next->prev = prev;
646 }
647
648 if (first_entry) {
649 if (next) {
650 st_update(tbl, box_id, remove_class_from_subclasses_replace_first_entry, (st_data_t)next);
651 }
652 else {
653 // no subclass entries in this ns after the deletion
654 st_delete(tbl, &box_id, NULL);
655 }
656 }
657
658 xfree(entry);
659
660 break;
661 }
662 else if (first_entry) {
663 first_entry = false;
664 }
665 entry = entry->next;
666 }
667}
668
669void
670rb_class_remove_from_super_subclasses(VALUE klass)
671{
672 rb_classext_t *ext = RCLASS_EXT_WRITABLE(klass);
673 rb_box_subclasses_t *box_subclasses = RCLASSEXT_BOX_SUPER_SUBCLASSES(ext);
674
675 if (!box_subclasses) return;
676 remove_class_from_subclasses(box_subclasses->tbl, box_subclasses_tbl_key(RCLASSEXT_BOX(ext)), klass);
677 rb_box_subclasses_ref_dec(box_subclasses);
678 RCLASSEXT_BOX_SUPER_SUBCLASSES(ext) = 0;
679}
680
681void
682rb_class_classext_free_subclasses(rb_classext_t *ext, VALUE klass, bool replacing)
683{
684 rb_subclass_anchor_t *anchor = RCLASSEXT_SUBCLASSES(ext);
685 struct st_table *tbl = anchor->box_subclasses->tbl;
686 VALUE box_id = box_subclasses_tbl_key(RCLASSEXT_BOX(ext));
687 rb_subclass_entry_t *next, *entry = anchor->head;
688
689 while (entry) {
690 next = entry->next;
691 xfree(entry);
692 entry = next;
693 }
694 VM_ASSERT(
695 rb_box_subclasses_ref_count(anchor->box_subclasses) > 0,
696 "box_subclasses refcount (%p) %ld", anchor->box_subclasses, rb_box_subclasses_ref_count(anchor->box_subclasses));
697 st_delete(tbl, &box_id, NULL);
698 rb_box_subclasses_ref_dec(anchor->box_subclasses);
699 xfree(anchor);
700
701 if (RCLASSEXT_BOX_SUPER_SUBCLASSES(ext)) {
702 rb_box_subclasses_t *box_sub = RCLASSEXT_BOX_SUPER_SUBCLASSES(ext);
703 if (!replacing) remove_class_from_subclasses(box_sub->tbl, box_id, klass);
704 rb_box_subclasses_ref_dec(box_sub);
705 }
706 if (RCLASSEXT_BOX_MODULE_SUBCLASSES(ext)) {
707 rb_box_subclasses_t *box_sub = RCLASSEXT_BOX_MODULE_SUBCLASSES(ext);
708 if (!replacing) remove_class_from_subclasses(box_sub->tbl, box_id, klass);
709 rb_box_subclasses_ref_dec(box_sub);
710 }
711}
712
713void
714rb_class_foreach_subclass(VALUE klass, void (*f)(VALUE, VALUE), VALUE arg)
715{
717 rb_subclass_entry_t *cur = RCLASS_SUBCLASSES_FIRST(klass);
718 /* do not be tempted to simplify this loop into a for loop, the order of
719 operations is important here if `f` modifies the linked list */
720 while (cur) {
721 VALUE curklass = cur->klass;
722 tmp = cur->next;
723 // do not trigger GC during f, otherwise the cur will become
724 // a dangling pointer if the subclass is collected
725 f(curklass, arg);
726 cur = tmp;
727 }
728}
729
730static void
731class_detach_subclasses(VALUE klass, VALUE arg)
732{
733 rb_class_remove_from_super_subclasses(klass);
734}
735
736static void
737class_switch_superclass(VALUE super, VALUE klass)
738{
739 RB_VM_LOCKING() {
740 class_detach_subclasses(klass, Qnil);
741 rb_class_subclass_add(super, klass);
742 }
743}
744
755static VALUE
756class_alloc0(enum ruby_value_type type, VALUE klass, bool boxable)
757{
758 rb_box_subclasses_t *box_subclasses;
759 rb_subclass_anchor_t *anchor;
760 const rb_box_t *box = rb_current_box();
761
762 if (!ruby_box_init_done) {
763 boxable = true;
764 }
765
766 size_t alloc_size = sizeof(struct RClass_and_rb_classext_t);
767 if (boxable) {
768 alloc_size = sizeof(struct RClass_boxable);
769 }
770
771 // class_alloc is supposed to return a new object that is not promoted yet.
772 // So, we need to avoid GC after NEWOBJ_OF.
773 // To achieve that, we allocate subclass lists before NEWOBJ_OF.
774 //
775 // TODO: Note that this could cause memory leak.
776 // If NEWOBJ_OF fails with out of memory, these buffers will leak.
777 box_subclasses = ZALLOC(rb_box_subclasses_t);
778 box_subclasses->refcount = 1;
779 box_subclasses->tbl = st_init_numtable();
781 anchor->box_subclasses = box_subclasses;
782 anchor->head = ZALLOC(rb_subclass_entry_t);
783
785
786 VALUE flags = type | FL_SHAREABLE;
788 if (boxable) flags |= RCLASS_BOXABLE;
789
790 NEWOBJ_OF(obj, struct RClass, klass, flags, alloc_size, 0);
791
792 obj->object_id = 0;
793
794 memset(RCLASS_EXT_PRIME(obj), 0, sizeof(rb_classext_t));
795
796 /* ZALLOC
797 RCLASS_CONST_TBL(obj) = 0;
798 RCLASS_M_TBL(obj) = 0;
799 RCLASS_FIELDS(obj) = 0;
800 RCLASS_SET_SUPER((VALUE)obj, 0);
801 */
802
803 if (boxable) {
804 ((struct RClass_boxable *)obj)->box_classext_tbl = NULL;
805 }
806
807 RCLASS_PRIME_BOX((VALUE)obj) = box;
808 // Classes/Modules defined in user boxes are
809 // writable directly because it exists only in a box.
810 RCLASS_SET_PRIME_CLASSEXT_WRITABLE((VALUE)obj, !boxable || BOX_USER_P(box));
811
812 RCLASS_SET_ORIGIN((VALUE)obj, (VALUE)obj);
813 RCLASS_SET_REFINED_CLASS((VALUE)obj, Qnil);
814
815 RCLASS_SET_SUBCLASSES((VALUE)obj, anchor);
816
817 return (VALUE)obj;
818}
819
820static VALUE
821class_alloc(enum ruby_value_type type, VALUE klass)
822{
823 bool boxable = rb_box_available() && BOX_ROOT_P(rb_current_box());
824 return class_alloc0(type, klass, boxable);
825}
826
827static VALUE
828class_associate_super(VALUE klass, VALUE super, bool init)
829{
830 if (super && !UNDEF_P(super)) {
831 class_switch_superclass(super, klass);
832 }
833 if (init) {
834 RCLASS_SET_SUPER(klass, super);
835 }
836 else {
837 RCLASS_WRITE_SUPER(klass, super);
838 }
839 rb_class_update_superclasses(klass);
840 return super;
841}
842
843VALUE
844rb_class_set_super(VALUE klass, VALUE super)
845{
846 return class_associate_super(klass, super, false);
847}
848
849static void
850class_initialize_method_table(VALUE c)
851{
852 // initialize the prime classext m_tbl
853 RCLASS_SET_M_TBL(c, rb_id_table_create(0));
854}
855
856static void
857class_clear_method_table(VALUE c)
858{
859 RCLASS_WRITE_M_TBL(c, rb_id_table_create(0));
860}
861
862static VALUE
863class_boot_boxable(VALUE super, bool boxable)
864{
865 VALUE klass = class_alloc0(T_CLASS, rb_cClass, boxable);
866
867 // initialize method table prior to class_associate_super()
868 // because class_associate_super() may cause GC and promote klass
869 class_initialize_method_table(klass);
870
871 class_associate_super(klass, super, true);
872 if (super && !UNDEF_P(super)) {
873 rb_class_set_initialized(klass);
874 }
875
876 return (VALUE)klass;
877}
878
888VALUE
890{
891 return class_boot_boxable(super, false);
892}
893
894static VALUE *
895class_superclasses_including_self(VALUE klass)
896{
897 if (RCLASS_SUPERCLASSES_WITH_SELF_P(klass))
898 return RCLASS_SUPERCLASSES(klass);
899
900 size_t depth = RCLASS_SUPERCLASS_DEPTH(klass);
901 VALUE *superclasses = xmalloc(sizeof(VALUE) * (depth + 1));
902 if (depth > 0)
903 memcpy(superclasses, RCLASS_SUPERCLASSES(klass), sizeof(VALUE) * depth);
904 superclasses[depth] = klass;
905
906 return superclasses;
907}
908
909void
910rb_class_update_superclasses(VALUE klass)
911{
912 VALUE *superclasses;
913 size_t super_depth;
914 VALUE super = RCLASS_SUPER(klass);
915
916 if (!RB_TYPE_P(klass, T_CLASS)) return;
917 if (UNDEF_P(super)) return;
918
919 // If the superclass array is already built
920 if (RCLASS_SUPERCLASSES(klass))
921 return;
922
923 // find the proper superclass
924 while (super != Qfalse && !RB_TYPE_P(super, T_CLASS)) {
925 super = RCLASS_SUPER(super);
926 }
927
928 // For BasicObject and uninitialized classes, depth=0 and ary=NULL
929 if (super == Qfalse)
930 return;
931
932 // Sometimes superclasses are set before the full ancestry tree is built
933 // This happens during metaclass construction
934 if (super != rb_cBasicObject && !RCLASS_SUPERCLASS_DEPTH(super)) {
935 rb_class_update_superclasses(super);
936
937 // If it is still unset we need to try later
938 if (!RCLASS_SUPERCLASS_DEPTH(super))
939 return;
940 }
941
942 super_depth = RCLASS_SUPERCLASS_DEPTH(super);
943 if (RCLASS_SUPERCLASSES_WITH_SELF_P(super)) {
944 superclasses = RCLASS_SUPERCLASSES(super);
945 }
946 else {
947 superclasses = class_superclasses_including_self(super);
948 RCLASS_WRITE_SUPERCLASSES(super, super_depth, superclasses, true);
949 }
950
951 size_t depth = super_depth == RCLASS_MAX_SUPERCLASS_DEPTH ? super_depth : super_depth + 1;
952 RCLASS_WRITE_SUPERCLASSES(klass, depth, superclasses, false);
953}
954
955void
957{
958 if (!RB_TYPE_P(super, T_CLASS)) {
959 rb_raise(rb_eTypeError, "superclass must be an instance of Class (given an instance of %"PRIsVALUE")",
960 rb_obj_class(super));
961 }
962 if (RCLASS_SINGLETON_P(super)) {
963 rb_raise(rb_eTypeError, "can't make subclass of singleton class");
964 }
965 if (super == rb_cClass) {
966 rb_raise(rb_eTypeError, "can't make subclass of Class");
967 }
968}
969
970VALUE
972{
973 Check_Type(super, T_CLASS);
975 VALUE klass = rb_class_boot(super);
976
977 if (super != rb_cObject && super != rb_cBasicObject) {
978 RCLASS_SET_MAX_IV_COUNT(klass, RCLASS_MAX_IV_COUNT(super));
979 }
980
981 RUBY_ASSERT(getenv("RUBY_BOX") || RCLASS_PRIME_CLASSEXT_WRITABLE_P(klass));
982
983 return klass;
984}
985
986VALUE
987rb_class_s_alloc(VALUE klass)
988{
989 return rb_class_boot(0);
990}
991
992static void
993clone_method(VALUE old_klass, VALUE new_klass, ID mid, const rb_method_entry_t *me)
994{
995 if (me->def->type == VM_METHOD_TYPE_ISEQ) {
996 rb_cref_t *new_cref = rb_vm_rewrite_cref(me->def->body.iseq.cref, old_klass, new_klass);
997 rb_add_method_iseq(new_klass, mid, me->def->body.iseq.iseqptr, new_cref, METHOD_ENTRY_VISI(me));
998 }
999 else {
1000 rb_method_entry_set(new_klass, mid, me, METHOD_ENTRY_VISI(me));
1001 }
1002}
1003
1005 VALUE new_klass;
1006 VALUE old_klass;
1007};
1008
1009static enum rb_id_table_iterator_result
1010clone_method_i(ID key, VALUE value, void *data)
1011{
1012 const struct clone_method_arg *arg = (struct clone_method_arg *)data;
1013 clone_method(arg->old_klass, arg->new_klass, key, (const rb_method_entry_t *)value);
1014 return ID_TABLE_CONTINUE;
1015}
1016
1018 VALUE klass;
1019 struct rb_id_table *tbl;
1020};
1021
1022static int
1023clone_const(ID key, const rb_const_entry_t *ce, struct clone_const_arg *arg)
1024{
1026 MEMCPY(nce, ce, rb_const_entry_t, 1);
1027 RB_OBJ_WRITTEN(arg->klass, Qundef, ce->value);
1028 RB_OBJ_WRITTEN(arg->klass, Qundef, ce->file);
1029
1030 rb_id_table_insert(arg->tbl, key, (VALUE)nce);
1031 return ID_TABLE_CONTINUE;
1032}
1033
1034static enum rb_id_table_iterator_result
1035clone_const_i(ID key, VALUE value, void *data)
1036{
1037 return clone_const(key, (const rb_const_entry_t *)value, data);
1038}
1039
1040static void
1041class_init_copy_check(VALUE clone, VALUE orig)
1042{
1043 if (orig == rb_cBasicObject) {
1044 rb_raise(rb_eTypeError, "can't copy the root class");
1045 }
1046 if (RCLASS_INITIALIZED_P(clone)) {
1047 rb_raise(rb_eTypeError, "already initialized class");
1048 }
1049 if (RCLASS_SINGLETON_P(orig)) {
1050 rb_raise(rb_eTypeError, "can't copy singleton class");
1051 }
1052}
1053
1055 VALUE clone;
1056 struct rb_id_table * new_table;
1057};
1058
1059static enum rb_id_table_iterator_result
1060cvc_table_copy(ID id, VALUE val, void *data)
1061{
1062 struct cvc_table_copy_ctx *ctx = (struct cvc_table_copy_ctx *)data;
1063 struct rb_cvar_class_tbl_entry * orig_entry;
1064 orig_entry = (struct rb_cvar_class_tbl_entry *)val;
1065
1066 struct rb_cvar_class_tbl_entry *ent;
1067
1068 ent = ALLOC(struct rb_cvar_class_tbl_entry);
1069 ent->class_value = ctx->clone;
1070 ent->cref = orig_entry->cref;
1071 ent->global_cvar_state = orig_entry->global_cvar_state;
1072 rb_id_table_insert(ctx->new_table, id, (VALUE)ent);
1073
1074 RB_OBJ_WRITTEN(ctx->clone, Qundef, ent->cref);
1075
1076 return ID_TABLE_CONTINUE;
1077}
1078
1079static void
1080copy_tables(VALUE clone, VALUE orig)
1081{
1082 if (RCLASS_CONST_TBL(clone)) {
1083 rb_free_const_table(RCLASS_CONST_TBL(clone));
1084 RCLASS_WRITE_CONST_TBL(clone, 0, false);
1085 }
1086 if (RCLASS_CVC_TBL(orig)) {
1087 struct rb_id_table *rb_cvc_tbl = RCLASS_CVC_TBL(orig);
1088 struct rb_id_table *rb_cvc_tbl_dup = rb_id_table_create(rb_id_table_size(rb_cvc_tbl));
1089
1090 struct cvc_table_copy_ctx ctx;
1091 ctx.clone = clone;
1092 ctx.new_table = rb_cvc_tbl_dup;
1093 rb_id_table_foreach(rb_cvc_tbl, cvc_table_copy, &ctx);
1094 RCLASS_WRITE_CVC_TBL(clone, rb_cvc_tbl_dup);
1095 }
1096 rb_id_table_free(RCLASS_M_TBL(clone));
1097 RCLASS_WRITE_M_TBL(clone, 0);
1098 if (!RB_TYPE_P(clone, T_ICLASS)) {
1099 rb_fields_tbl_copy(clone, orig);
1100 }
1101 if (RCLASS_CONST_TBL(orig)) {
1102 struct clone_const_arg arg;
1103 struct rb_id_table *const_tbl;
1104 struct rb_id_table *orig_tbl = RCLASS_CONST_TBL(orig);
1105 arg.tbl = const_tbl = rb_id_table_create(rb_id_table_size(orig_tbl));
1106 arg.klass = clone;
1107 rb_id_table_foreach(orig_tbl, clone_const_i, &arg);
1108 RCLASS_WRITE_CONST_TBL(clone, const_tbl, false);
1109 }
1110}
1111
1112static bool ensure_origin(VALUE klass);
1113
1114void
1115rb_class_set_initialized(VALUE klass)
1116{
1117 RUBY_ASSERT(RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_MODULE));
1118 FL_SET_RAW(klass, RCLASS_IS_INITIALIZED);
1119 /* no more re-initialization */
1120}
1121
1122void
1123rb_module_check_initializable(VALUE mod)
1124{
1125 if (RCLASS_INITIALIZED_P(mod)) {
1126 rb_raise(rb_eTypeError, "already initialized module");
1127 }
1128}
1129
1130/* :nodoc: */
1131VALUE
1133{
1134 /* Only class or module is valid here, but other classes may enter here and
1135 * only hit an exception on the OBJ_INIT_COPY checks
1136 */
1137 switch (BUILTIN_TYPE(clone)) {
1138 case T_CLASS:
1139 class_init_copy_check(clone, orig);
1140 break;
1141 case T_MODULE:
1142 rb_module_check_initializable(clone);
1143 break;
1144 default:
1145 break;
1146 }
1147 if (!OBJ_INIT_COPY(clone, orig)) return clone;
1148
1150 RUBY_ASSERT(BUILTIN_TYPE(clone) == BUILTIN_TYPE(orig));
1151
1152 rb_class_set_initialized(clone);
1153
1154 /* cloned flag is refer at constant inline cache
1155 * see vm_get_const_key_cref() in vm_insnhelper.c
1156 */
1157 RCLASS_SET_CLONED(clone, true);
1158 RCLASS_SET_CLONED(orig, true);
1159
1160 if (!RCLASS_SINGLETON_P(CLASS_OF(clone))) {
1161 RBASIC_SET_CLASS(clone, rb_singleton_class_clone(orig));
1162 rb_singleton_class_attached(METACLASS_OF(clone), (VALUE)clone);
1163 }
1164 if (BUILTIN_TYPE(clone) == T_CLASS) {
1165 RCLASS_SET_ALLOCATOR(clone, RCLASS_ALLOCATOR(orig));
1166 }
1167 copy_tables(clone, orig);
1168 if (RCLASS_M_TBL(orig)) {
1169 struct clone_method_arg arg;
1170 arg.old_klass = orig;
1171 arg.new_klass = clone;
1172 class_initialize_method_table(clone);
1173 rb_id_table_foreach(RCLASS_M_TBL(orig), clone_method_i, &arg);
1174 }
1175
1176 if (RCLASS_ORIGIN(orig) == orig) {
1177 rb_class_set_super(clone, RCLASS_SUPER(orig));
1178 }
1179 else {
1180 VALUE p = RCLASS_SUPER(orig);
1181 VALUE orig_origin = RCLASS_ORIGIN(orig);
1182 VALUE prev_clone_p = clone;
1183 VALUE origin_stack = rb_ary_hidden_new(2);
1184 VALUE origin[2];
1185 VALUE clone_p = 0;
1186 long origin_len;
1187 int add_subclass;
1188 VALUE clone_origin;
1189
1190 ensure_origin(clone);
1191 clone_origin = RCLASS_ORIGIN(clone);
1192
1193 while (p && p != orig_origin) {
1194 if (BUILTIN_TYPE(p) != T_ICLASS) {
1195 rb_bug("non iclass between module/class and origin");
1196 }
1197 clone_p = class_alloc(T_ICLASS, METACLASS_OF(p));
1198 RCLASS_SET_M_TBL(clone_p, RCLASS_M_TBL(p));
1199 rb_class_set_super(prev_clone_p, clone_p);
1200 prev_clone_p = clone_p;
1201 RCLASS_SET_CONST_TBL(clone_p, RCLASS_CONST_TBL(p), false);
1202 if (RB_TYPE_P(clone, T_CLASS)) {
1203 RCLASS_SET_INCLUDER(clone_p, clone);
1204 }
1205 add_subclass = TRUE;
1206 if (p != RCLASS_ORIGIN(p)) {
1207 origin[0] = clone_p;
1208 origin[1] = RCLASS_ORIGIN(p);
1209 rb_ary_cat(origin_stack, origin, 2);
1210 }
1211 else if ((origin_len = RARRAY_LEN(origin_stack)) > 1 &&
1212 RARRAY_AREF(origin_stack, origin_len - 1) == p) {
1213 RCLASS_WRITE_ORIGIN(RARRAY_AREF(origin_stack, (origin_len -= 2)), clone_p);
1214 RICLASS_WRITE_ORIGIN_SHARED_MTBL(clone_p);
1215 rb_ary_resize(origin_stack, origin_len);
1216 add_subclass = FALSE;
1217 }
1218 if (add_subclass) {
1219 rb_module_add_to_subclasses_list(METACLASS_OF(p), clone_p);
1220 }
1221 p = RCLASS_SUPER(p);
1222 }
1223
1224 if (p == orig_origin) {
1225 if (clone_p) {
1226 rb_class_set_super(clone_p, clone_origin);
1227 rb_class_set_super(clone_origin, RCLASS_SUPER(orig_origin));
1228 }
1229 copy_tables(clone_origin, orig_origin);
1230 if (RCLASS_M_TBL(orig_origin)) {
1231 struct clone_method_arg arg;
1232 arg.old_klass = orig;
1233 arg.new_klass = clone;
1234 class_initialize_method_table(clone_origin);
1235 rb_id_table_foreach(RCLASS_M_TBL(orig_origin), clone_method_i, &arg);
1236 }
1237 }
1238 else {
1239 rb_bug("no origin for class that has origin");
1240 }
1241
1242 rb_class_update_superclasses(clone);
1243 }
1244
1245 return clone;
1246}
1247
1248VALUE
1250{
1251 return rb_singleton_class_clone_and_attach(obj, Qundef);
1252}
1253
1254// Clone and return the singleton class of `obj` if it has been created and is attached to `obj`.
1255VALUE
1256rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach)
1257{
1258 const VALUE klass = METACLASS_OF(obj);
1259
1260 // Note that `rb_singleton_class()` can create situations where `klass` is
1261 // attached to an object other than `obj`. In which case `obj` does not have
1262 // a material singleton class attached yet and there is no singleton class
1263 // to clone.
1264 if (!(RCLASS_SINGLETON_P(klass) && RCLASS_ATTACHED_OBJECT(klass) == obj)) {
1265 // nothing to clone
1266 return klass;
1267 }
1268 else {
1269 /* copy singleton(unnamed) class */
1270 bool klass_of_clone_is_new;
1271 RUBY_ASSERT(RB_TYPE_P(klass, T_CLASS));
1272 VALUE clone = class_alloc(T_CLASS, 0);
1273
1274 if (BUILTIN_TYPE(obj) == T_CLASS) {
1275 klass_of_clone_is_new = true;
1276 RBASIC_SET_CLASS(clone, clone);
1277 }
1278 else {
1279 VALUE klass_metaclass_clone = rb_singleton_class_clone(klass);
1280 // When `METACLASS_OF(klass) == klass_metaclass_clone`, it means the
1281 // recursive call did not clone `METACLASS_OF(klass)`.
1282 klass_of_clone_is_new = (METACLASS_OF(klass) != klass_metaclass_clone);
1283 RBASIC_SET_CLASS(clone, klass_metaclass_clone);
1284 }
1285
1286 // initialize method table before any GC chance
1287 class_initialize_method_table(clone);
1288
1289 rb_class_set_super(clone, RCLASS_SUPER(klass));
1290 rb_fields_tbl_copy(clone, klass);
1291 if (RCLASS_CONST_TBL(klass)) {
1292 struct clone_const_arg arg;
1293 struct rb_id_table *table;
1294 arg.tbl = table = rb_id_table_create(rb_id_table_size(RCLASS_CONST_TBL(klass)));
1295 arg.klass = clone;
1296 rb_id_table_foreach(RCLASS_CONST_TBL(klass), clone_const_i, &arg);
1297 RCLASS_SET_CONST_TBL(clone, table, false);
1298 }
1299 if (!UNDEF_P(attach)) {
1300 rb_singleton_class_attached(clone, attach);
1301 }
1302 {
1303 struct clone_method_arg arg;
1304 arg.old_klass = klass;
1305 arg.new_klass = clone;
1306 rb_id_table_foreach(RCLASS_M_TBL(klass), clone_method_i, &arg);
1307 }
1308 if (klass_of_clone_is_new) {
1309 rb_singleton_class_attached(METACLASS_OF(clone), clone);
1310 }
1311 FL_SET(clone, FL_SINGLETON);
1312
1313 return clone;
1314 }
1315}
1316
1317void
1319{
1320 if (RCLASS_SINGLETON_P(klass)) {
1321 RCLASS_SET_ATTACHED_OBJECT(klass, obj);
1322 }
1323}
1324
1330#define META_CLASS_OF_CLASS_CLASS_P(k) (METACLASS_OF(k) == (k))
1331
1332static int
1333rb_singleton_class_has_metaclass_p(VALUE sklass)
1334{
1335 return RCLASS_ATTACHED_OBJECT(METACLASS_OF(sklass)) == sklass;
1336}
1337
1338int
1339rb_singleton_class_internal_p(VALUE sklass)
1340{
1341 return (RB_TYPE_P(RCLASS_ATTACHED_OBJECT(sklass), T_CLASS) &&
1342 !rb_singleton_class_has_metaclass_p(sklass));
1343}
1344
1350#define HAVE_METACLASS_P(k) \
1351 (FL_TEST(METACLASS_OF(k), FL_SINGLETON) && \
1352 rb_singleton_class_has_metaclass_p(k))
1353
1361#define ENSURE_EIGENCLASS(klass) \
1362 (HAVE_METACLASS_P(klass) ? METACLASS_OF(klass) : make_metaclass(klass))
1363
1364
1374static inline VALUE
1376{
1377 VALUE super;
1378 VALUE metaclass = class_boot_boxable(Qundef, FL_TEST_RAW(klass, RCLASS_BOXABLE));
1379
1380 FL_SET(metaclass, FL_SINGLETON);
1381 rb_singleton_class_attached(metaclass, klass);
1382
1383 if (META_CLASS_OF_CLASS_CLASS_P(klass)) {
1384 SET_METACLASS_OF(klass, metaclass);
1385 SET_METACLASS_OF(metaclass, metaclass);
1386 }
1387 else {
1388 VALUE tmp = METACLASS_OF(klass); /* for a meta^(n)-class klass, tmp is meta^(n)-class of Class class */
1389 SET_METACLASS_OF(klass, metaclass);
1390 SET_METACLASS_OF(metaclass, ENSURE_EIGENCLASS(tmp));
1391 }
1392
1393 super = RCLASS_SUPER(klass);
1394 while (RB_TYPE_P(super, T_ICLASS)) super = RCLASS_SUPER(super);
1395 class_associate_super(metaclass, super ? ENSURE_EIGENCLASS(super) : rb_cClass, true);
1396 rb_class_set_initialized(klass);
1397
1398 // Full class ancestry may not have been filled until we reach here.
1399 rb_class_update_superclasses(METACLASS_OF(metaclass));
1400
1401 return metaclass;
1402}
1403
1410static inline VALUE
1412{
1413 VALUE orig_class = METACLASS_OF(obj);
1414 VALUE klass = class_boot_boxable(orig_class, FL_TEST_RAW(orig_class, RCLASS_BOXABLE));
1415
1416 FL_SET(klass, FL_SINGLETON);
1417 RBASIC_SET_CLASS(obj, klass);
1418 rb_singleton_class_attached(klass, obj);
1419 rb_yjit_invalidate_no_singleton_class(orig_class);
1420 rb_zjit_invalidate_no_singleton_class(orig_class);
1421
1422 SET_METACLASS_OF(klass, METACLASS_OF(rb_class_real(orig_class)));
1423 return klass;
1424}
1425
1426
1427static VALUE
1428boot_defclass(const char *name, VALUE super)
1429{
1430 VALUE obj = rb_class_boot(super);
1431 ID id = rb_intern(name);
1432
1433 rb_const_set((rb_cObject ? rb_cObject : obj), id, obj);
1434 rb_vm_register_global_object(obj);
1435 return obj;
1436}
1437
1438/***********************************************************************
1439 *
1440 * Document-class: Refinement
1441 *
1442 * Refinement is a class of the +self+ (current context) inside +refine+
1443 * statement. It allows to import methods from other modules, see #import_methods.
1444 */
1445
1446#if 0 /* for RDoc */
1447/*
1448 * Document-method: Refinement#import_methods
1449 *
1450 * call-seq:
1451 * import_methods(module, ...) -> self
1452 *
1453 * Imports methods from modules. Unlike Module#include,
1454 * Refinement#import_methods copies methods and adds them into the refinement,
1455 * so the refinement is activated in the imported methods.
1456 *
1457 * Note that due to method copying, only methods defined in Ruby code can be imported.
1458 *
1459 * module StrUtils
1460 * def indent(level)
1461 * ' ' * level + self
1462 * end
1463 * end
1464 *
1465 * module M
1466 * refine String do
1467 * import_methods StrUtils
1468 * end
1469 * end
1470 *
1471 * using M
1472 * "foo".indent(3)
1473 * #=> " foo"
1474 *
1475 * module M
1476 * refine String do
1477 * import_methods Enumerable
1478 * # Can't import method which is not defined with Ruby code: Enumerable#drop
1479 * end
1480 * end
1481 *
1482 */
1483
1484static VALUE
1485refinement_import_methods(int argc, VALUE *argv, VALUE refinement)
1486{
1487}
1488# endif
1489
1509void
1510Init_class_hierarchy(void)
1511{
1512 rb_cBasicObject = boot_defclass("BasicObject", 0);
1513 rb_cObject = boot_defclass("Object", rb_cBasicObject);
1514 rb_vm_register_global_object(rb_cObject);
1515
1516 /* resolve class name ASAP for order-independence */
1517 rb_set_class_path_string(rb_cObject, rb_cObject, rb_fstring_lit("Object"));
1518
1519 rb_cModule = boot_defclass("Module", rb_cObject);
1520 rb_cClass = boot_defclass("Class", rb_cModule);
1521 rb_cRefinement = boot_defclass("Refinement", rb_cModule);
1522
1523#if 0 /* for RDoc */
1524 // we pretend it to be public, otherwise RDoc will ignore it
1525 rb_define_method(rb_cRefinement, "import_methods", refinement_import_methods, -1);
1526#endif
1527
1528 rb_const_set(rb_cObject, rb_intern_const("BasicObject"), rb_cBasicObject);
1529 RBASIC_SET_CLASS(rb_cClass, rb_cClass);
1530 RBASIC_SET_CLASS(rb_cModule, rb_cClass);
1531 RBASIC_SET_CLASS(rb_cObject, rb_cClass);
1532 RBASIC_SET_CLASS(rb_cRefinement, rb_cClass);
1533 RBASIC_SET_CLASS(rb_cBasicObject, rb_cClass);
1534
1536}
1537
1538
1549VALUE
1550rb_make_metaclass(VALUE obj, VALUE unused)
1551{
1552 if (BUILTIN_TYPE(obj) == T_CLASS) {
1553 return make_metaclass(obj);
1554 }
1555 else {
1556 return make_singleton_class(obj);
1557 }
1558}
1559
1560VALUE
1562{
1563 VALUE klass;
1564
1565 if (!super) super = rb_cObject;
1566 klass = rb_class_new(super);
1567 rb_make_metaclass(klass, METACLASS_OF(super));
1568
1569 return klass;
1570}
1571
1572
1581VALUE
1583{
1584 ID inherited;
1585 if (!super) super = rb_cObject;
1586 CONST_ID(inherited, "inherited");
1587 return rb_funcall(super, inherited, 1, klass);
1588}
1589
1590VALUE
1591rb_define_class(const char *name, VALUE super)
1592{
1593 VALUE klass;
1594 ID id = rb_intern(name);
1595
1596 if (rb_const_defined(rb_cObject, id)) {
1597 klass = rb_const_get(rb_cObject, id);
1598 if (!RB_TYPE_P(klass, T_CLASS)) {
1599 rb_raise(rb_eTypeError, "%s is not a class (%"PRIsVALUE")",
1600 name, rb_obj_class(klass));
1601 }
1602 if (rb_class_real(RCLASS_SUPER(klass)) != super) {
1603 rb_raise(rb_eTypeError, "superclass mismatch for class %s", name);
1604 }
1605
1606 /* Class may have been defined in Ruby and not pin-rooted */
1607 rb_vm_register_global_object(klass);
1608 return klass;
1609 }
1610 if (!super) {
1611 rb_raise(rb_eArgError, "no super class for '%s'", name);
1612 }
1613 klass = rb_define_class_id(id, super);
1614 rb_vm_register_global_object(klass);
1615 rb_const_set(rb_cObject, id, klass);
1616 rb_class_inherited(super, klass);
1617
1618 return klass;
1619}
1620
1621VALUE
1622rb_define_class_under(VALUE outer, const char *name, VALUE super)
1623{
1624 return rb_define_class_id_under(outer, rb_intern(name), super);
1625}
1626
1627VALUE
1628rb_define_class_id_under_no_pin(VALUE outer, ID id, VALUE super)
1629{
1630 VALUE klass;
1631
1632 if (rb_const_defined_at(outer, id)) {
1633 klass = rb_const_get_at(outer, id);
1634 if (!RB_TYPE_P(klass, T_CLASS)) {
1635 rb_raise(rb_eTypeError, "%"PRIsVALUE"::%"PRIsVALUE" is not a class"
1636 " (%"PRIsVALUE")",
1637 outer, rb_id2str(id), rb_obj_class(klass));
1638 }
1639 if (rb_class_real(RCLASS_SUPER(klass)) != super) {
1640 rb_raise(rb_eTypeError, "superclass mismatch for class "
1641 "%"PRIsVALUE"::%"PRIsVALUE""
1642 " (%"PRIsVALUE" is given but was %"PRIsVALUE")",
1643 outer, rb_id2str(id), RCLASS_SUPER(klass), super);
1644 }
1645
1646 return klass;
1647 }
1648 if (!super) {
1649 rb_raise(rb_eArgError, "no super class for '%"PRIsVALUE"::%"PRIsVALUE"'",
1650 rb_class_path(outer), rb_id2str(id));
1651 }
1652 klass = rb_define_class_id(id, super);
1653 rb_set_class_path_string(klass, outer, rb_id2str(id));
1654 rb_const_set(outer, id, klass);
1655 rb_class_inherited(super, klass);
1656
1657 return klass;
1658}
1659
1660VALUE
1662{
1663 VALUE klass = rb_define_class_id_under_no_pin(outer, id, super);
1664 rb_vm_register_global_object(klass);
1665 return klass;
1666}
1667
1668VALUE
1669rb_module_s_alloc(VALUE klass)
1670{
1671 VALUE mod = class_alloc(T_MODULE, klass);
1672 class_initialize_method_table(mod);
1673 return mod;
1674}
1675
1676static inline VALUE
1677module_new(VALUE klass)
1678{
1679 VALUE mdl = class_alloc(T_MODULE, klass);
1680 class_initialize_method_table(mdl);
1681 return (VALUE)mdl;
1682}
1683
1684VALUE
1686{
1687 return module_new(rb_cModule);
1688}
1689
1690VALUE
1692{
1693 return module_new(rb_cRefinement);
1694}
1695
1696// Kept for compatibility. Use rb_module_new() instead.
1697VALUE
1699{
1700 return rb_module_new();
1701}
1702
1703VALUE
1704rb_define_module(const char *name)
1705{
1706 VALUE module;
1707 ID id = rb_intern(name);
1708
1709 if (rb_const_defined(rb_cObject, id)) {
1710 module = rb_const_get(rb_cObject, id);
1711 if (!RB_TYPE_P(module, T_MODULE)) {
1712 rb_raise(rb_eTypeError, "%s is not a module (%"PRIsVALUE")",
1713 name, rb_obj_class(module));
1714 }
1715 /* Module may have been defined in Ruby and not pin-rooted */
1716 rb_vm_register_global_object(module);
1717 return module;
1718 }
1719 module = rb_module_new();
1720 rb_vm_register_global_object(module);
1721 rb_const_set(rb_cObject, id, module);
1722
1723 return module;
1724}
1725
1726VALUE
1727rb_define_module_under(VALUE outer, const char *name)
1728{
1729 return rb_define_module_id_under(outer, rb_intern(name));
1730}
1731
1732VALUE
1734{
1735 VALUE module;
1736
1737 if (rb_const_defined_at(outer, id)) {
1738 module = rb_const_get_at(outer, id);
1739 if (!RB_TYPE_P(module, T_MODULE)) {
1740 rb_raise(rb_eTypeError, "%"PRIsVALUE"::%"PRIsVALUE" is not a module"
1741 " (%"PRIsVALUE")",
1742 outer, rb_id2str(id), rb_obj_class(module));
1743 }
1744 /* Module may have been defined in Ruby and not pin-rooted */
1745 rb_vm_register_global_object(module);
1746 return module;
1747 }
1748 module = rb_module_new();
1749 rb_const_set(outer, id, module);
1750 rb_set_class_path_string(module, outer, rb_id2str(id));
1751 rb_vm_register_global_object(module);
1752
1753 return module;
1754}
1755
1756VALUE
1757rb_include_class_new(VALUE module, VALUE super)
1758{
1759 VALUE klass = class_alloc(T_ICLASS, rb_cClass);
1760
1761 RCLASS_SET_M_TBL(klass, RCLASS_WRITABLE_M_TBL(module));
1762
1763 RCLASS_SET_ORIGIN(klass, klass);
1764 if (BUILTIN_TYPE(module) == T_ICLASS) {
1765 module = METACLASS_OF(module);
1766 }
1767 RUBY_ASSERT(!RB_TYPE_P(module, T_ICLASS));
1768 if (RCLASS_WRITABLE_CONST_TBL(module)) {
1769 RCLASS_SET_CONST_TBL(klass, RCLASS_WRITABLE_CONST_TBL(module), true);
1770 }
1771 else {
1772 RCLASS_WRITE_CONST_TBL(module, rb_id_table_create(0), false);
1773 RCLASS_SET_CONST_TBL(klass, RCLASS_WRITABLE_CONST_TBL(module), true);
1774 }
1775
1776 RCLASS_SET_CVC_TBL(klass, RCLASS_WRITABLE_CVC_TBL(module));
1777
1778 class_associate_super(klass, super, true);
1779 RBASIC_SET_CLASS(klass, module);
1780
1781 return (VALUE)klass;
1782}
1783
1784static int include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super);
1785
1786static void
1787ensure_includable(VALUE klass, VALUE module)
1788{
1789 rb_class_modify_check(klass);
1790 Check_Type(module, T_MODULE);
1791 rb_class_set_initialized(module);
1792 if (!NIL_P(rb_refinement_module_get_refined_class(module))) {
1793 rb_raise(rb_eArgError, "refinement module is not allowed");
1794 }
1795}
1796
1797void
1799{
1800 int changed = 0;
1801
1802 ensure_includable(klass, module);
1803
1804 changed = include_modules_at(klass, RCLASS_ORIGIN(klass), module, TRUE);
1805 if (changed < 0)
1806 rb_raise(rb_eArgError, "cyclic include detected");
1807
1808 if (RB_TYPE_P(klass, T_MODULE)) {
1809 rb_subclass_entry_t *iclass = RCLASS_SUBCLASSES_FIRST(klass);
1810 while (iclass) {
1811 int do_include = 1;
1812 VALUE check_class = iclass->klass;
1813 /* During lazy sweeping, iclass->klass could be a dead object that
1814 * has not yet been swept. */
1815 if (!rb_objspace_garbage_object_p(check_class)) {
1816 while (check_class) {
1817 RUBY_ASSERT(!rb_objspace_garbage_object_p(check_class));
1818
1819 if (RB_TYPE_P(check_class, T_ICLASS) &&
1820 (METACLASS_OF(check_class) == module)) {
1821 do_include = 0;
1822 }
1823 check_class = RCLASS_SUPER(check_class);
1824 }
1825
1826 if (do_include) {
1827 include_modules_at(iclass->klass, RCLASS_ORIGIN(iclass->klass), module, TRUE);
1828 }
1829 }
1830
1831 iclass = iclass->next;
1832 }
1833 }
1834}
1835
1836static enum rb_id_table_iterator_result
1837add_refined_method_entry_i(ID key, VALUE value, void *data)
1838{
1839 rb_add_refined_method_entry((VALUE)data, key);
1840 return ID_TABLE_CONTINUE;
1841}
1842
1843static enum rb_id_table_iterator_result
1844clear_module_cache_i(ID id, VALUE val, void *data)
1845{
1846 VALUE klass = (VALUE)data;
1847 rb_clear_method_cache(klass, id);
1848 return ID_TABLE_CONTINUE;
1849}
1850
1851static bool
1852module_in_super_chain(const VALUE klass, VALUE module)
1853{
1854 struct rb_id_table *const klass_m_tbl = RCLASS_M_TBL(RCLASS_ORIGIN(klass));
1855 if (klass_m_tbl) {
1856 while (module) {
1857 if (klass_m_tbl == RCLASS_M_TBL(module))
1858 return true;
1859 module = RCLASS_SUPER(module);
1860 }
1861 }
1862 return false;
1863}
1864
1865// For each ID key in the class constant table, we're going to clear the VM's
1866// inline constant caches associated with it.
1867static enum rb_id_table_iterator_result
1868clear_constant_cache_i(ID id, VALUE value, void *data)
1869{
1871 return ID_TABLE_CONTINUE;
1872}
1873
1874static int
1875do_include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super, bool check_cyclic)
1876{
1877 VALUE p, iclass, origin_stack = 0;
1878 int method_changed = 0;
1879 long origin_len;
1880 VALUE klass_origin = RCLASS_ORIGIN(klass);
1881 VALUE original_klass = klass;
1882
1883 if (check_cyclic && module_in_super_chain(klass, module))
1884 return -1;
1885
1886 while (module) {
1887 int c_seen = FALSE;
1888 int superclass_seen = FALSE;
1889 struct rb_id_table *tbl;
1890
1891 if (klass == c) {
1892 c_seen = TRUE;
1893 }
1894 if (klass_origin != c || search_super) {
1895 /* ignore if the module included already in superclasses for include,
1896 * ignore if the module included before origin class for prepend
1897 */
1898 for (p = RCLASS_SUPER(klass); p; p = RCLASS_SUPER(p)) {
1899 int type = BUILTIN_TYPE(p);
1900 if (klass_origin == p && !search_super)
1901 break;
1902 if (c == p)
1903 c_seen = TRUE;
1904 if (type == T_ICLASS) {
1905 if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) {
1906 if (!superclass_seen && c_seen) {
1907 c = p; /* move insertion point */
1908 }
1909 goto skip;
1910 }
1911 }
1912 else if (type == T_CLASS) {
1913 superclass_seen = TRUE;
1914 }
1915 }
1916 }
1917
1918 VALUE super_class = RCLASS_SUPER(c);
1919
1920 // invalidate inline method cache
1921 RB_DEBUG_COUNTER_INC(cvar_include_invalidate);
1922 ruby_vm_global_cvar_state++;
1923 tbl = RCLASS_M_TBL(module);
1924 if (tbl && rb_id_table_size(tbl)) {
1925 if (search_super) { // include
1926 if (super_class && !RB_TYPE_P(super_class, T_MODULE)) {
1927 rb_id_table_foreach(tbl, clear_module_cache_i, (void *)super_class);
1928 }
1929 }
1930 else { // prepend
1931 if (!RB_TYPE_P(original_klass, T_MODULE)) {
1932 rb_id_table_foreach(tbl, clear_module_cache_i, (void *)original_klass);
1933 }
1934 }
1935 method_changed = 1;
1936 }
1937
1938 // setup T_ICLASS for the include/prepend module
1939 iclass = rb_include_class_new(module, super_class);
1940 c = rb_class_set_super(c, iclass);
1941 RCLASS_SET_INCLUDER(iclass, klass);
1942 if (module != RCLASS_ORIGIN(module)) {
1943 if (!origin_stack) origin_stack = rb_ary_hidden_new(2);
1944 VALUE origin[2] = {iclass, RCLASS_ORIGIN(module)};
1945 rb_ary_cat(origin_stack, origin, 2);
1946 }
1947 else if (origin_stack && (origin_len = RARRAY_LEN(origin_stack)) > 1 &&
1948 RARRAY_AREF(origin_stack, origin_len - 1) == module) {
1949 RCLASS_WRITE_ORIGIN(RARRAY_AREF(origin_stack, (origin_len -= 2)), iclass);
1950 RICLASS_WRITE_ORIGIN_SHARED_MTBL(iclass);
1951 rb_ary_resize(origin_stack, origin_len);
1952 }
1953
1954 VALUE m = module;
1955 if (BUILTIN_TYPE(m) == T_ICLASS) m = METACLASS_OF(m);
1956 rb_module_add_to_subclasses_list(m, iclass);
1957
1958 if (BUILTIN_TYPE(klass) == T_MODULE && FL_TEST(klass, RMODULE_IS_REFINEMENT)) {
1959 VALUE refined_class =
1960 rb_refinement_module_get_refined_class(klass);
1961
1962 rb_id_table_foreach(RCLASS_M_TBL(module), add_refined_method_entry_i, (void *)refined_class);
1964 }
1965
1966 tbl = RCLASS_CONST_TBL(module);
1967 if (tbl && rb_id_table_size(tbl))
1968 rb_id_table_foreach(tbl, clear_constant_cache_i, NULL);
1969 skip:
1970 module = RCLASS_SUPER(module);
1971 }
1972
1973 return method_changed;
1974}
1975
1976static int
1977include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super)
1978{
1979 return do_include_modules_at(klass, c, module, search_super, true);
1980}
1981
1982static enum rb_id_table_iterator_result
1983move_refined_method(ID key, VALUE value, void *data)
1984{
1985 rb_method_entry_t *me = (rb_method_entry_t *)value;
1986
1987 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1988 VALUE klass = (VALUE)data;
1989 struct rb_id_table *tbl = RCLASS_WRITABLE_M_TBL(klass);
1990
1991 if (me->def->body.refined.orig_me) {
1992 const rb_method_entry_t *orig_me = me->def->body.refined.orig_me, *new_me;
1993 RB_OBJ_WRITE(me, &me->def->body.refined.orig_me, NULL);
1994 new_me = rb_method_entry_clone(me);
1995 rb_method_table_insert(klass, tbl, key, new_me);
1996 rb_method_entry_copy(me, orig_me);
1997 return ID_TABLE_CONTINUE;
1998 }
1999 else {
2000 rb_method_table_insert(klass, tbl, key, me);
2001 return ID_TABLE_DELETE;
2002 }
2003 }
2004 else {
2005 return ID_TABLE_CONTINUE;
2006 }
2007}
2008
2009static enum rb_id_table_iterator_result
2010cache_clear_refined_method(ID key, VALUE value, void *data)
2011{
2012 rb_method_entry_t *me = (rb_method_entry_t *) value;
2013
2014 if (me->def->type == VM_METHOD_TYPE_REFINED && me->def->body.refined.orig_me) {
2015 VALUE klass = (VALUE)data;
2016 rb_clear_method_cache(klass, me->called_id);
2017 }
2018 // Refined method entries without an orig_me is going to stay in the method
2019 // table of klass, like before the move, so no need to clear the cache.
2020
2021 return ID_TABLE_CONTINUE;
2022}
2023
2024static bool
2025ensure_origin(VALUE klass)
2026{
2027 VALUE origin = RCLASS_ORIGIN(klass);
2028 if (origin == klass) {
2029 origin = class_alloc(T_ICLASS, klass);
2030 RCLASS_SET_M_TBL(origin, RCLASS_M_TBL(klass));
2031 rb_class_set_super(origin, RCLASS_SUPER(klass));
2032 rb_class_set_super(klass, origin); // writes origin into RCLASS_SUPER(klass)
2033 RCLASS_WRITE_ORIGIN(klass, origin);
2034
2035 // RCLASS_WRITE_ORIGIN marks origin as an origin, so this is the first
2036 // point that it sees M_TBL and may mark it
2037 rb_gc_writebarrier_remember(origin);
2038
2039 class_clear_method_table(klass);
2040 rb_id_table_foreach(RCLASS_M_TBL(origin), cache_clear_refined_method, (void *)klass);
2041 rb_id_table_foreach(RCLASS_M_TBL(origin), move_refined_method, (void *)klass);
2042 return true;
2043 }
2044 return false;
2045}
2046
2047void
2049{
2050 int changed;
2051 bool klass_had_no_origin;
2052
2053 ensure_includable(klass, module);
2054 if (module_in_super_chain(klass, module))
2055 rb_raise(rb_eArgError, "cyclic prepend detected");
2056
2057 klass_had_no_origin = ensure_origin(klass);
2058 changed = do_include_modules_at(klass, klass, module, FALSE, false);
2059 RUBY_ASSERT(changed >= 0); // already checked for cyclic prepend above
2060 if (changed) {
2061 rb_vm_check_redefinition_by_prepend(klass);
2062 }
2063 if (RB_TYPE_P(klass, T_MODULE)) {
2064 rb_subclass_entry_t *iclass = RCLASS_SUBCLASSES_FIRST(klass);
2065 VALUE klass_origin = RCLASS_ORIGIN(klass);
2066 struct rb_id_table *klass_m_tbl = RCLASS_M_TBL(klass);
2067 struct rb_id_table *klass_origin_m_tbl = RCLASS_M_TBL(klass_origin);
2068 while (iclass) {
2069 /* During lazy sweeping, iclass->klass could be a dead object that
2070 * has not yet been swept. */
2071 if (!rb_objspace_garbage_object_p(iclass->klass)) {
2072 const VALUE subclass = iclass->klass;
2073 if (klass_had_no_origin && klass_origin_m_tbl == RCLASS_M_TBL(subclass)) {
2074 // backfill an origin iclass to handle refinements and future prepends
2075 rb_id_table_foreach(RCLASS_M_TBL(subclass), clear_module_cache_i, (void *)subclass);
2076 RCLASS_WRITE_M_TBL(subclass, klass_m_tbl);
2077 VALUE origin = rb_include_class_new(klass_origin, RCLASS_SUPER(subclass));
2078 rb_class_set_super(subclass, origin);
2079 RCLASS_SET_INCLUDER(origin, RCLASS_INCLUDER(subclass));
2080 RCLASS_WRITE_ORIGIN(subclass, origin);
2081 RICLASS_SET_ORIGIN_SHARED_MTBL(origin);
2082 }
2083 include_modules_at(subclass, subclass, module, FALSE);
2084 }
2085
2086 iclass = iclass->next;
2087 }
2088 }
2089}
2090
2091/*
2092 * call-seq:
2093 * mod.included_modules -> array
2094 *
2095 * Returns the list of modules included or prepended in <i>mod</i>
2096 * or one of <i>mod</i>'s ancestors.
2097 *
2098 * module Sub
2099 * end
2100 *
2101 * module Mixin
2102 * prepend Sub
2103 * end
2104 *
2105 * module Outer
2106 * include Mixin
2107 * end
2108 *
2109 * Mixin.included_modules #=> [Sub]
2110 * Outer.included_modules #=> [Sub, Mixin]
2111 */
2112
2113VALUE
2115{
2116 VALUE ary = rb_ary_new();
2117 VALUE p;
2118 VALUE origin = RCLASS_ORIGIN(mod);
2119
2120 for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
2121 if (p != origin && RCLASS_ORIGIN(p) == p && BUILTIN_TYPE(p) == T_ICLASS) {
2122 VALUE m = METACLASS_OF(p);
2123 if (RB_TYPE_P(m, T_MODULE))
2124 rb_ary_push(ary, m);
2125 }
2126 }
2127 return ary;
2128}
2129
2130/*
2131 * call-seq:
2132 * mod.include?(module) -> true or false
2133 *
2134 * Returns <code>true</code> if <i>module</i> is included
2135 * or prepended in <i>mod</i> or one of <i>mod</i>'s ancestors.
2136 *
2137 * module A
2138 * end
2139 * class B
2140 * include A
2141 * end
2142 * class C < B
2143 * end
2144 * B.include?(A) #=> true
2145 * C.include?(A) #=> true
2146 * A.include?(A) #=> false
2147 */
2148
2149VALUE
2151{
2152 VALUE p;
2153
2154 Check_Type(mod2, T_MODULE);
2155 for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
2156 if (BUILTIN_TYPE(p) == T_ICLASS && !RICLASS_IS_ORIGIN_P(p)) {
2157 if (METACLASS_OF(p) == mod2) return Qtrue;
2158 }
2159 }
2160 return Qfalse;
2161}
2162
2163/*
2164 * call-seq:
2165 * mod.ancestors -> array
2166 *
2167 * Returns a list of modules included/prepended in <i>mod</i>
2168 * (including <i>mod</i> itself).
2169 *
2170 * module Mod
2171 * include Math
2172 * include Comparable
2173 * prepend Enumerable
2174 * end
2175 *
2176 * Mod.ancestors #=> [Enumerable, Mod, Comparable, Math]
2177 * Math.ancestors #=> [Math]
2178 * Enumerable.ancestors #=> [Enumerable]
2179 */
2180
2181VALUE
2183{
2184 VALUE p, ary = rb_ary_new();
2185 VALUE refined_class = Qnil;
2186 if (BUILTIN_TYPE(mod) == T_MODULE && FL_TEST(mod, RMODULE_IS_REFINEMENT)) {
2187 refined_class = rb_refinement_module_get_refined_class(mod);
2188 }
2189
2190 for (p = mod; p; p = RCLASS_SUPER(p)) {
2191 if (p == refined_class) break;
2192 if (p != RCLASS_ORIGIN(p)) continue;
2193 if (BUILTIN_TYPE(p) == T_ICLASS) {
2194 rb_ary_push(ary, METACLASS_OF(p));
2195 }
2196 else {
2197 rb_ary_push(ary, p);
2198 }
2199 }
2200 return ary;
2201}
2202
2204{
2205 VALUE buffer;
2206 long count;
2207 long maxcount;
2208 bool immediate_only;
2209};
2210
2211static void
2212class_descendants_recursive(VALUE klass, VALUE v)
2213{
2214 struct subclass_traverse_data *data = (struct subclass_traverse_data *) v;
2215
2216 if (BUILTIN_TYPE(klass) == T_CLASS && !RCLASS_SINGLETON_P(klass)) {
2217 if (data->buffer && data->count < data->maxcount && !rb_objspace_garbage_object_p(klass)) {
2218 // assumes that this does not cause GC as long as the length does not exceed the capacity
2219 rb_ary_push(data->buffer, klass);
2220 }
2221 data->count++;
2222 if (!data->immediate_only) {
2223 rb_class_foreach_subclass(klass, class_descendants_recursive, v);
2224 }
2225 }
2226 else {
2227 rb_class_foreach_subclass(klass, class_descendants_recursive, v);
2228 }
2229}
2230
2231static VALUE
2232class_descendants(VALUE klass, bool immediate_only)
2233{
2234 struct subclass_traverse_data data = { Qfalse, 0, -1, immediate_only };
2235
2236 // estimate the count of subclasses
2237 rb_class_foreach_subclass(klass, class_descendants_recursive, (VALUE) &data);
2238
2239 // the following allocation may cause GC which may change the number of subclasses
2240 data.buffer = rb_ary_new_capa(data.count);
2241 data.maxcount = data.count;
2242 data.count = 0;
2243
2244 size_t gc_count = rb_gc_count();
2245
2246 // enumerate subclasses
2247 rb_class_foreach_subclass(klass, class_descendants_recursive, (VALUE) &data);
2248
2249 if (gc_count != rb_gc_count()) {
2250 rb_bug("GC must not occur during the subclass iteration of Class#descendants");
2251 }
2252
2253 return data.buffer;
2254}
2255
2256/*
2257 * call-seq:
2258 * subclasses -> array
2259 *
2260 * Returns an array of classes where the receiver is the
2261 * direct superclass of the class, excluding singleton classes.
2262 * The order of the returned array is not defined.
2263 *
2264 * class A; end
2265 * class B < A; end
2266 * class C < B; end
2267 * class D < A; end
2268 *
2269 * A.subclasses #=> [D, B]
2270 * B.subclasses #=> [C]
2271 * C.subclasses #=> []
2272 *
2273 * Anonymous subclasses (not associated with a constant) are
2274 * returned, too:
2275 *
2276 * c = Class.new(A)
2277 * A.subclasses # => [#<Class:0x00007f003c77bd78>, D, B]
2278 *
2279 * Note that the parent does not hold references to subclasses
2280 * and doesn't prevent them from being garbage collected. This
2281 * means that the subclass might disappear when all references
2282 * to it are dropped:
2283 *
2284 * # drop the reference to subclass, it can be garbage-collected now
2285 * c = nil
2286 *
2287 * A.subclasses
2288 * # It can be
2289 * # => [#<Class:0x00007f003c77bd78>, D, B]
2290 * # ...or just
2291 * # => [D, B]
2292 * # ...depending on whether garbage collector was run
2293 */
2294
2295VALUE
2297{
2298 return class_descendants(klass, true);
2299}
2300
2301/*
2302 * call-seq:
2303 * attached_object -> object
2304 *
2305 * Returns the object for which the receiver is the singleton class.
2306 *
2307 * Raises an TypeError if the class is not a singleton class.
2308 *
2309 * class Foo; end
2310 *
2311 * Foo.singleton_class.attached_object #=> Foo
2312 * Foo.attached_object #=> TypeError: `Foo' is not a singleton class
2313 * Foo.new.singleton_class.attached_object #=> #<Foo:0x000000010491a370>
2314 * TrueClass.attached_object #=> TypeError: `TrueClass' is not a singleton class
2315 * NilClass.attached_object #=> TypeError: `NilClass' is not a singleton class
2316 */
2317
2318VALUE
2320{
2321 if (!RCLASS_SINGLETON_P(klass)) {
2322 rb_raise(rb_eTypeError, "'%"PRIsVALUE"' is not a singleton class", klass);
2323 }
2324
2325 return RCLASS_ATTACHED_OBJECT(klass);
2326}
2327
2328static void
2329ins_methods_push(st_data_t name, st_data_t ary)
2330{
2331 rb_ary_push((VALUE)ary, ID2SYM((ID)name));
2332}
2333
2334static int
2335ins_methods_i(st_data_t name, st_data_t type, st_data_t ary)
2336{
2337 switch ((rb_method_visibility_t)type) {
2338 case METHOD_VISI_UNDEF:
2339 case METHOD_VISI_PRIVATE:
2340 break;
2341 default: /* everything but private */
2342 ins_methods_push(name, ary);
2343 break;
2344 }
2345 return ST_CONTINUE;
2346}
2347
2348static int
2349ins_methods_type_i(st_data_t name, st_data_t type, st_data_t ary, rb_method_visibility_t visi)
2350{
2351 if ((rb_method_visibility_t)type == visi) {
2352 ins_methods_push(name, ary);
2353 }
2354 return ST_CONTINUE;
2355}
2356
2357static int
2358ins_methods_prot_i(st_data_t name, st_data_t type, st_data_t ary)
2359{
2360 return ins_methods_type_i(name, type, ary, METHOD_VISI_PROTECTED);
2361}
2362
2363static int
2364ins_methods_priv_i(st_data_t name, st_data_t type, st_data_t ary)
2365{
2366 return ins_methods_type_i(name, type, ary, METHOD_VISI_PRIVATE);
2367}
2368
2369static int
2370ins_methods_pub_i(st_data_t name, st_data_t type, st_data_t ary)
2371{
2372 return ins_methods_type_i(name, type, ary, METHOD_VISI_PUBLIC);
2373}
2374
2375static int
2376ins_methods_undef_i(st_data_t name, st_data_t type, st_data_t ary)
2377{
2378 return ins_methods_type_i(name, type, ary, METHOD_VISI_UNDEF);
2379}
2380
2382 st_table *list;
2383 int recur;
2384};
2385
2386static enum rb_id_table_iterator_result
2387method_entry_i(ID key, VALUE value, void *data)
2388{
2389 const rb_method_entry_t *me = (const rb_method_entry_t *)value;
2390 struct method_entry_arg *arg = (struct method_entry_arg *)data;
2391 rb_method_visibility_t type;
2392
2393 if (me->def->type == VM_METHOD_TYPE_REFINED) {
2394 VALUE owner = me->owner;
2395 me = rb_resolve_refined_method(Qnil, me);
2396 if (!me) return ID_TABLE_CONTINUE;
2397 if (!arg->recur && me->owner != owner) return ID_TABLE_CONTINUE;
2398 }
2399 if (!st_is_member(arg->list, key)) {
2400 if (UNDEFINED_METHOD_ENTRY_P(me)) {
2401 type = METHOD_VISI_UNDEF; /* none */
2402 }
2403 else {
2404 type = METHOD_ENTRY_VISI(me);
2405 RUBY_ASSERT(type != METHOD_VISI_UNDEF);
2406 }
2407 st_add_direct(arg->list, key, (st_data_t)type);
2408 }
2409 return ID_TABLE_CONTINUE;
2410}
2411
2412static void
2413add_instance_method_list(VALUE mod, struct method_entry_arg *me_arg)
2414{
2415 struct rb_id_table *m_tbl = RCLASS_M_TBL(mod);
2416 if (!m_tbl) return;
2417 rb_id_table_foreach(m_tbl, method_entry_i, me_arg);
2418}
2419
2420static bool
2421particular_class_p(VALUE mod)
2422{
2423 if (!mod) return false;
2424 if (RCLASS_SINGLETON_P(mod)) return true;
2425 if (BUILTIN_TYPE(mod) == T_ICLASS) return true;
2426 return false;
2427}
2428
2429static VALUE
2430class_instance_method_list(int argc, const VALUE *argv, VALUE mod, int obj, int (*func) (st_data_t, st_data_t, st_data_t))
2431{
2432 VALUE ary;
2433 int recur = TRUE, prepended = 0;
2434 struct method_entry_arg me_arg;
2435
2436 if (rb_check_arity(argc, 0, 1)) recur = RTEST(argv[0]);
2437
2438 me_arg.list = st_init_numtable();
2439 me_arg.recur = recur;
2440
2441 if (obj) {
2442 for (; particular_class_p(mod); mod = RCLASS_SUPER(mod)) {
2443 add_instance_method_list(mod, &me_arg);
2444 }
2445 }
2446
2447 if (!recur && RCLASS_ORIGIN(mod) != mod) {
2448 mod = RCLASS_ORIGIN(mod);
2449 prepended = 1;
2450 }
2451
2452 for (; mod; mod = RCLASS_SUPER(mod)) {
2453 add_instance_method_list(mod, &me_arg);
2454 if (BUILTIN_TYPE(mod) == T_ICLASS && !prepended) continue;
2455 if (!recur) break;
2456 }
2457 ary = rb_ary_new2(me_arg.list->num_entries);
2458 st_foreach(me_arg.list, func, ary);
2459 st_free_table(me_arg.list);
2460
2461 return ary;
2462}
2463
2464/*
2465 * call-seq:
2466 * mod.instance_methods(include_super=true) -> array
2467 *
2468 * Returns an array containing the names of the public and protected instance
2469 * methods in the receiver. For a module, these are the public and protected methods;
2470 * for a class, they are the instance (not singleton) methods. If the optional
2471 * parameter is <code>false</code>, the methods of any ancestors are not included.
2472 *
2473 * module A
2474 * def method1() end
2475 * end
2476 * class B
2477 * include A
2478 * def method2() end
2479 * end
2480 * class C < B
2481 * def method3() end
2482 * end
2483 *
2484 * A.instance_methods(false) #=> [:method1]
2485 * B.instance_methods(false) #=> [:method2]
2486 * B.instance_methods(true).include?(:method1) #=> true
2487 * C.instance_methods(false) #=> [:method3]
2488 * C.instance_methods.include?(:method2) #=> true
2489 *
2490 * Note that method visibility changes in the current class, as well as aliases,
2491 * are considered as methods of the current class by this method:
2492 *
2493 * class C < B
2494 * alias method4 method2
2495 * protected :method2
2496 * end
2497 * C.instance_methods(false).sort #=> [:method2, :method3, :method4]
2498 */
2499
2500VALUE
2501rb_class_instance_methods(int argc, const VALUE *argv, VALUE mod)
2502{
2503 return class_instance_method_list(argc, argv, mod, 0, ins_methods_i);
2504}
2505
2506/*
2507 * call-seq:
2508 * mod.protected_instance_methods(include_super=true) -> array
2509 *
2510 * Returns a list of the protected instance methods defined in
2511 * <i>mod</i>. If the optional parameter is <code>false</code>, the
2512 * methods of any ancestors are not included.
2513 */
2514
2515VALUE
2517{
2518 return class_instance_method_list(argc, argv, mod, 0, ins_methods_prot_i);
2519}
2520
2521/*
2522 * call-seq:
2523 * mod.private_instance_methods(include_super=true) -> array
2524 *
2525 * Returns a list of the private instance methods defined in
2526 * <i>mod</i>. If the optional parameter is <code>false</code>, the
2527 * methods of any ancestors are not included.
2528 *
2529 * module Mod
2530 * def method1() end
2531 * private :method1
2532 * def method2() end
2533 * end
2534 * Mod.instance_methods #=> [:method2]
2535 * Mod.private_instance_methods #=> [:method1]
2536 */
2537
2538VALUE
2540{
2541 return class_instance_method_list(argc, argv, mod, 0, ins_methods_priv_i);
2542}
2543
2544/*
2545 * call-seq:
2546 * mod.public_instance_methods(include_super=true) -> array
2547 *
2548 * Returns a list of the public instance methods defined in <i>mod</i>.
2549 * If the optional parameter is <code>false</code>, the methods of
2550 * any ancestors are not included.
2551 */
2552
2553VALUE
2555{
2556 return class_instance_method_list(argc, argv, mod, 0, ins_methods_pub_i);
2557}
2558
2559/*
2560 * call-seq:
2561 * mod.undefined_instance_methods -> array
2562 *
2563 * Returns a list of the undefined instance methods defined in <i>mod</i>.
2564 * The undefined methods of any ancestors are not included.
2565 */
2566
2567VALUE
2568rb_class_undefined_instance_methods(VALUE mod)
2569{
2570 VALUE include_super = Qfalse;
2571 return class_instance_method_list(1, &include_super, mod, 0, ins_methods_undef_i);
2572}
2573
2574/*
2575 * call-seq:
2576 * obj.methods(regular=true) -> array
2577 *
2578 * Returns a list of the names of public and protected methods of
2579 * <i>obj</i>. This will include all the methods accessible in
2580 * <i>obj</i>'s ancestors.
2581 * If the optional parameter is <code>false</code>, it
2582 * returns an array of <i>obj</i>'s public and protected singleton methods,
2583 * the array will not include methods in modules included in <i>obj</i>.
2584 *
2585 * class Klass
2586 * def klass_method()
2587 * end
2588 * end
2589 * k = Klass.new
2590 * k.methods[0..9] #=> [:klass_method, :nil?, :===,
2591 * # :==~, :!, :eql?
2592 * # :hash, :<=>, :class, :singleton_class]
2593 * k.methods.length #=> 56
2594 *
2595 * k.methods(false) #=> []
2596 * def k.singleton_method; end
2597 * k.methods(false) #=> [:singleton_method]
2598 *
2599 * module M123; def m123; end end
2600 * k.extend M123
2601 * k.methods(false) #=> [:singleton_method]
2602 */
2603
2604VALUE
2605rb_obj_methods(int argc, const VALUE *argv, VALUE obj)
2606{
2607 rb_check_arity(argc, 0, 1);
2608 if (argc > 0 && !RTEST(argv[0])) {
2609 return rb_obj_singleton_methods(argc, argv, obj);
2610 }
2611 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_i);
2612}
2613
2614/*
2615 * call-seq:
2616 * obj.protected_methods(all=true) -> array
2617 *
2618 * Returns the list of protected methods accessible to <i>obj</i>. If
2619 * the <i>all</i> parameter is set to <code>false</code>, only those methods
2620 * in the receiver will be listed.
2621 */
2622
2623VALUE
2624rb_obj_protected_methods(int argc, const VALUE *argv, VALUE obj)
2625{
2626 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_prot_i);
2627}
2628
2629/*
2630 * call-seq:
2631 * obj.private_methods(all=true) -> array
2632 *
2633 * Returns the list of private methods accessible to <i>obj</i>. If
2634 * the <i>all</i> parameter is set to <code>false</code>, only those methods
2635 * in the receiver will be listed.
2636 */
2637
2638VALUE
2639rb_obj_private_methods(int argc, const VALUE *argv, VALUE obj)
2640{
2641 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_priv_i);
2642}
2643
2644/*
2645 * call-seq:
2646 * obj.public_methods(all=true) -> array
2647 *
2648 * Returns the list of public methods accessible to <i>obj</i>. If
2649 * the <i>all</i> parameter is set to <code>false</code>, only those methods
2650 * in the receiver will be listed.
2651 */
2652
2653VALUE
2654rb_obj_public_methods(int argc, const VALUE *argv, VALUE obj)
2655{
2656 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_pub_i);
2657}
2658
2659/*
2660 * call-seq:
2661 * obj.singleton_methods(all=true) -> array
2662 *
2663 * Returns an array of the names of singleton methods for <i>obj</i>.
2664 * If the optional <i>all</i> parameter is true, the list will include
2665 * methods in modules included in <i>obj</i>.
2666 * Only public and protected singleton methods are returned.
2667 *
2668 * module Other
2669 * def three() end
2670 * end
2671 *
2672 * class Single
2673 * def Single.four() end
2674 * end
2675 *
2676 * a = Single.new
2677 *
2678 * def a.one()
2679 * end
2680 *
2681 * class << a
2682 * include Other
2683 * def two()
2684 * end
2685 * end
2686 *
2687 * Single.singleton_methods #=> [:four]
2688 * a.singleton_methods(false) #=> [:two, :one]
2689 * a.singleton_methods #=> [:two, :one, :three]
2690 */
2691
2692VALUE
2693rb_obj_singleton_methods(int argc, const VALUE *argv, VALUE obj)
2694{
2695 VALUE ary, klass, origin;
2696 struct method_entry_arg me_arg;
2697 struct rb_id_table *mtbl;
2698 int recur = TRUE;
2699
2700 if (rb_check_arity(argc, 0, 1)) recur = RTEST(argv[0]);
2701 if (RCLASS_SINGLETON_P(obj)) {
2702 rb_singleton_class(obj);
2703 }
2704 klass = CLASS_OF(obj);
2705 origin = RCLASS_ORIGIN(klass);
2706 me_arg.list = st_init_numtable();
2707 me_arg.recur = recur;
2708 if (klass && RCLASS_SINGLETON_P(klass)) {
2709 if ((mtbl = RCLASS_M_TBL(origin)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2710 klass = RCLASS_SUPER(klass);
2711 }
2712 if (recur) {
2713 while (klass && (RCLASS_SINGLETON_P(klass) || RB_TYPE_P(klass, T_ICLASS))) {
2714 if (klass != origin && (mtbl = RCLASS_M_TBL(klass)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2715 klass = RCLASS_SUPER(klass);
2716 }
2717 }
2718 ary = rb_ary_new2(me_arg.list->num_entries);
2719 st_foreach(me_arg.list, ins_methods_i, ary);
2720 st_free_table(me_arg.list);
2721
2722 return ary;
2723}
2724
2733#ifdef rb_define_method_id
2734#undef rb_define_method_id
2735#endif
2736void
2737rb_define_method_id(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc)
2738{
2739 rb_add_method_cfunc(klass, mid, func, argc, METHOD_VISI_PUBLIC);
2740}
2741
2742#ifdef rb_define_method
2743#undef rb_define_method
2744#endif
2745void
2746rb_define_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
2747{
2748 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PUBLIC);
2749}
2750
2751#ifdef rb_define_protected_method
2752#undef rb_define_protected_method
2753#endif
2754void
2755rb_define_protected_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
2756{
2757 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PROTECTED);
2758}
2759
2760#ifdef rb_define_private_method
2761#undef rb_define_private_method
2762#endif
2763void
2764rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
2765{
2766 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PRIVATE);
2767}
2768
2769void
2770rb_undef_method(VALUE klass, const char *name)
2771{
2772 rb_add_method(klass, rb_intern(name), VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2773}
2774
2775static enum rb_id_table_iterator_result
2776undef_method_i(ID name, VALUE value, void *data)
2777{
2778 VALUE klass = (VALUE)data;
2779 rb_add_method(klass, name, VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2780 return ID_TABLE_CONTINUE;
2781}
2782
2783void
2784rb_undef_methods_from(VALUE klass, VALUE super)
2785{
2786 struct rb_id_table *mtbl = RCLASS_M_TBL(super);
2787 if (mtbl) {
2788 rb_id_table_foreach(mtbl, undef_method_i, (void *)klass);
2789 }
2790}
2791
2800static inline VALUE
2801special_singleton_class_of(VALUE obj)
2802{
2803 switch (obj) {
2804 case Qnil: return rb_cNilClass;
2805 case Qfalse: return rb_cFalseClass;
2806 case Qtrue: return rb_cTrueClass;
2807 default: return Qnil;
2808 }
2809}
2810
2811VALUE
2812rb_special_singleton_class(VALUE obj)
2813{
2814 return special_singleton_class_of(obj);
2815}
2816
2826static VALUE
2827singleton_class_of(VALUE obj, bool ensure_eigenclass)
2828{
2829 VALUE klass;
2830
2831 switch (TYPE(obj)) {
2832 case T_FIXNUM:
2833 case T_BIGNUM:
2834 case T_FLOAT:
2835 case T_SYMBOL:
2836 rb_raise(rb_eTypeError, "can't define singleton");
2837
2838 case T_FALSE:
2839 case T_TRUE:
2840 case T_NIL:
2841 klass = special_singleton_class_of(obj);
2842 if (NIL_P(klass))
2843 rb_bug("unknown immediate %p", (void *)obj);
2844 return klass;
2845
2846 case T_STRING:
2847 if (CHILLED_STRING_P(obj)) {
2848 CHILLED_STRING_MUTATED(obj);
2849 }
2850 else if (FL_TEST_RAW(obj, RSTRING_FSTR)) {
2851 rb_raise(rb_eTypeError, "can't define singleton");
2852 }
2853 }
2854
2855 bool needs_lock = rb_multi_ractor_p() && rb_ractor_shareable_p(obj);
2856 unsigned int lev;
2857 if (needs_lock) {
2858 RB_VM_LOCK_ENTER_LEV(&lev);
2859 }
2860 {
2861 klass = METACLASS_OF(obj);
2862 if (!(RCLASS_SINGLETON_P(klass) &&
2863 RCLASS_ATTACHED_OBJECT(klass) == obj)) {
2864 klass = rb_make_metaclass(obj, klass);
2865 }
2866 RB_FL_SET_RAW(klass, RB_OBJ_FROZEN_RAW(obj));
2867 if (ensure_eigenclass && RB_TYPE_P(obj, T_CLASS)) {
2868 /* ensures an exposed class belongs to its own eigenclass */
2869 (void)ENSURE_EIGENCLASS(klass);
2870 }
2871 }
2872 if (needs_lock) {
2873 RB_VM_LOCK_LEAVE_LEV(&lev);
2874 }
2875
2876 return klass;
2877}
2878
2879void
2881{
2882 /* should not propagate to meta-meta-class, and so on */
2883 if (!RCLASS_SINGLETON_P(x)) {
2884 VALUE klass = RBASIC_CLASS(x);
2885 if (klass && // no class when hidden from ObjectSpace
2886 FL_TEST_RAW(klass, FL_SINGLETON) &&
2887 !OBJ_FROZEN_RAW(klass)) {
2888 OBJ_FREEZE(klass);
2889 }
2890 }
2891}
2892
2900VALUE
2902{
2903 VALUE klass;
2904
2905 if (SPECIAL_CONST_P(obj)) {
2906 return rb_special_singleton_class(obj);
2907 }
2908 klass = METACLASS_OF(obj);
2909 if (!RCLASS_SINGLETON_P(klass)) return Qnil;
2910 if (RCLASS_ATTACHED_OBJECT(klass) != obj) return Qnil;
2911 return klass;
2912}
2913
2914VALUE
2916{
2917 return singleton_class_of(obj, true);
2918}
2919
2929#ifdef rb_define_singleton_method
2930#undef rb_define_singleton_method
2931#endif
2932void
2933rb_define_singleton_method(VALUE obj, const char *name, VALUE (*func)(ANYARGS), int argc)
2934{
2935 rb_define_method(singleton_class_of(obj, false), name, func, argc);
2936}
2937
2938#ifdef rb_define_module_function
2939#undef rb_define_module_function
2940#endif
2941void
2942rb_define_module_function(VALUE module, const char *name, VALUE (*func)(ANYARGS), int argc)
2943{
2944 rb_define_private_method(module, name, func, argc);
2945 rb_define_singleton_method(module, name, func, argc);
2946}
2947
2948#ifdef rb_define_global_function
2949#undef rb_define_global_function
2950#endif
2951void
2952rb_define_global_function(const char *name, VALUE (*func)(ANYARGS), int argc)
2953{
2954 rb_define_module_function(rb_mKernel, name, func, argc);
2955}
2956
2957void
2958rb_define_alias(VALUE klass, const char *name1, const char *name2)
2959{
2960 rb_alias(klass, rb_intern(name1), rb_intern(name2));
2961}
2962
2963void
2964rb_define_attr(VALUE klass, const char *name, int read, int write)
2965{
2966 rb_attr(klass, rb_intern(name), read, write, FALSE);
2967}
2968
2969VALUE
2970rb_keyword_error_new(const char *error, VALUE keys)
2971{
2972 long i = 0, len = RARRAY_LEN(keys);
2973 VALUE error_message = rb_sprintf("%s keyword%.*s", error, len > 1, "s");
2974
2975 if (len > 0) {
2976 rb_str_cat_cstr(error_message, ": ");
2977 while (1) {
2978 const VALUE k = RARRAY_AREF(keys, i);
2979 rb_str_append(error_message, rb_inspect(k));
2980 if (++i >= len) break;
2981 rb_str_cat_cstr(error_message, ", ");
2982 }
2983 }
2984
2985 return rb_exc_new_str(rb_eArgError, error_message);
2986}
2987
2988NORETURN(static void rb_keyword_error(const char *error, VALUE keys));
2989static void
2990rb_keyword_error(const char *error, VALUE keys)
2991{
2992 rb_exc_raise(rb_keyword_error_new(error, keys));
2993}
2994
2995NORETURN(static void unknown_keyword_error(VALUE hash, const ID *table, int keywords));
2996static void
2997unknown_keyword_error(VALUE hash, const ID *table, int keywords)
2998{
2999 int i;
3000 for (i = 0; i < keywords; i++) {
3001 st_data_t key = ID2SYM(table[i]);
3002 rb_hash_stlike_delete(hash, &key, NULL);
3003 }
3004 rb_keyword_error("unknown", rb_hash_keys(hash));
3005}
3006
3007
3008static int
3009separate_symbol(st_data_t key, st_data_t value, st_data_t arg)
3010{
3011 VALUE *kwdhash = (VALUE *)arg;
3012 if (!SYMBOL_P(key)) kwdhash++;
3013 if (!*kwdhash) *kwdhash = rb_hash_new();
3014 rb_hash_aset(*kwdhash, (VALUE)key, (VALUE)value);
3015 return ST_CONTINUE;
3016}
3017
3018VALUE
3020{
3021 VALUE parthash[2] = {0, 0};
3022 VALUE hash = *orighash;
3023
3024 if (RHASH_EMPTY_P(hash)) {
3025 *orighash = 0;
3026 return hash;
3027 }
3028 rb_hash_foreach(hash, separate_symbol, (st_data_t)&parthash);
3029 *orighash = parthash[1];
3030 if (parthash[1] && RBASIC_CLASS(hash) != rb_cHash) {
3031 RBASIC_SET_CLASS(parthash[1], RBASIC_CLASS(hash));
3032 }
3033 return parthash[0];
3034}
3035
3036int
3037rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
3038{
3039 int i = 0, j;
3040 int rest = 0;
3041 VALUE missing = Qnil;
3042 st_data_t key;
3043
3044#define extract_kwarg(keyword, val) \
3045 (key = (st_data_t)(keyword), values ? \
3046 (rb_hash_stlike_delete(keyword_hash, &key, &(val)) || ((val) = Qundef, 0)) : \
3047 rb_hash_stlike_lookup(keyword_hash, key, NULL))
3048
3049 if (NIL_P(keyword_hash)) keyword_hash = 0;
3050
3051 if (optional < 0) {
3052 rest = 1;
3053 optional = -1-optional;
3054 }
3055 if (required) {
3056 for (; i < required; i++) {
3057 VALUE keyword = ID2SYM(table[i]);
3058 if (keyword_hash) {
3059 if (extract_kwarg(keyword, values[i])) {
3060 continue;
3061 }
3062 }
3063 if (NIL_P(missing)) missing = rb_ary_hidden_new(1);
3064 rb_ary_push(missing, keyword);
3065 }
3066 if (!NIL_P(missing)) {
3067 rb_keyword_error("missing", missing);
3068 }
3069 }
3070 j = i;
3071 if (optional && keyword_hash) {
3072 for (i = 0; i < optional; i++) {
3073 if (extract_kwarg(ID2SYM(table[required+i]), values[required+i])) {
3074 j++;
3075 }
3076 }
3077 }
3078 if (!rest && keyword_hash) {
3079 if (RHASH_SIZE(keyword_hash) > (unsigned int)(values ? 0 : j)) {
3080 unknown_keyword_error(keyword_hash, table, required+optional);
3081 }
3082 }
3083 if (values && !keyword_hash) {
3084 for (i = 0; i < required + optional; i++) {
3085 values[i] = Qundef;
3086 }
3087 }
3088 return j;
3089#undef extract_kwarg
3090}
3091
3093 int kw_flag;
3094 int n_lead;
3095 int n_opt;
3096 int n_trail;
3097 bool f_var;
3098 bool f_hash;
3099 bool f_block;
3100};
3101
3102static void
3103rb_scan_args_parse(int kw_flag, const char *fmt, struct rb_scan_args_t *arg)
3104{
3105 const char *p = fmt;
3106
3107 memset(arg, 0, sizeof(*arg));
3108 arg->kw_flag = kw_flag;
3109
3110 if (ISDIGIT(*p)) {
3111 arg->n_lead = *p - '0';
3112 p++;
3113 if (ISDIGIT(*p)) {
3114 arg->n_opt = *p - '0';
3115 p++;
3116 }
3117 }
3118 if (*p == '*') {
3119 arg->f_var = 1;
3120 p++;
3121 }
3122 if (ISDIGIT(*p)) {
3123 arg->n_trail = *p - '0';
3124 p++;
3125 }
3126 if (*p == ':') {
3127 arg->f_hash = 1;
3128 p++;
3129 }
3130 if (*p == '&') {
3131 arg->f_block = 1;
3132 p++;
3133 }
3134 if (*p != '\0') {
3135 rb_fatal("bad scan arg format: %s", fmt);
3136 }
3137}
3138
3139static int
3140rb_scan_args_assign(const struct rb_scan_args_t *arg, int argc, const VALUE *const argv, va_list vargs)
3141{
3142 int i, argi = 0;
3143 VALUE *var, hash = Qnil;
3144#define rb_scan_args_next_param() va_arg(vargs, VALUE *)
3145 const int kw_flag = arg->kw_flag;
3146 const int n_lead = arg->n_lead;
3147 const int n_opt = arg->n_opt;
3148 const int n_trail = arg->n_trail;
3149 const int n_mand = n_lead + n_trail;
3150 const bool f_var = arg->f_var;
3151 const bool f_hash = arg->f_hash;
3152 const bool f_block = arg->f_block;
3153
3154 /* capture an option hash - phase 1: pop from the argv */
3155 if (f_hash && argc > 0) {
3156 VALUE last = argv[argc - 1];
3157 if (rb_scan_args_keyword_p(kw_flag, last)) {
3158 hash = rb_hash_dup(last);
3159 argc--;
3160 }
3161 }
3162
3163 if (argc < n_mand) {
3164 goto argc_error;
3165 }
3166
3167 /* capture leading mandatory arguments */
3168 for (i = 0; i < n_lead; i++) {
3169 var = rb_scan_args_next_param();
3170 if (var) *var = argv[argi];
3171 argi++;
3172 }
3173 /* capture optional arguments */
3174 for (i = 0; i < n_opt; i++) {
3175 var = rb_scan_args_next_param();
3176 if (argi < argc - n_trail) {
3177 if (var) *var = argv[argi];
3178 argi++;
3179 }
3180 else {
3181 if (var) *var = Qnil;
3182 }
3183 }
3184 /* capture variable length arguments */
3185 if (f_var) {
3186 int n_var = argc - argi - n_trail;
3187
3188 var = rb_scan_args_next_param();
3189 if (0 < n_var) {
3190 if (var) *var = rb_ary_new_from_values(n_var, &argv[argi]);
3191 argi += n_var;
3192 }
3193 else {
3194 if (var) *var = rb_ary_new();
3195 }
3196 }
3197 /* capture trailing mandatory arguments */
3198 for (i = 0; i < n_trail; i++) {
3199 var = rb_scan_args_next_param();
3200 if (var) *var = argv[argi];
3201 argi++;
3202 }
3203 /* capture an option hash - phase 2: assignment */
3204 if (f_hash) {
3205 var = rb_scan_args_next_param();
3206 if (var) *var = hash;
3207 }
3208 /* capture iterator block */
3209 if (f_block) {
3210 var = rb_scan_args_next_param();
3211 if (rb_block_given_p()) {
3212 *var = rb_block_proc();
3213 }
3214 else {
3215 *var = Qnil;
3216 }
3217 }
3218
3219 if (argi == argc) {
3220 return argc;
3221 }
3222
3223 argc_error:
3224 return -(argc + 1);
3225#undef rb_scan_args_next_param
3226}
3227
3228static int
3229rb_scan_args_result(const struct rb_scan_args_t *const arg, int argc)
3230{
3231 const int n_lead = arg->n_lead;
3232 const int n_opt = arg->n_opt;
3233 const int n_trail = arg->n_trail;
3234 const int n_mand = n_lead + n_trail;
3235 const bool f_var = arg->f_var;
3236
3237 if (argc >= 0) {
3238 return argc;
3239 }
3240
3241 argc = -argc - 1;
3242 rb_error_arity(argc, n_mand, f_var ? UNLIMITED_ARGUMENTS : n_mand + n_opt);
3244}
3245
3246#undef rb_scan_args
3247int
3248rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...)
3249{
3250 va_list vargs;
3251 struct rb_scan_args_t arg;
3252 rb_scan_args_parse(RB_SCAN_ARGS_PASS_CALLED_KEYWORDS, fmt, &arg);
3253 va_start(vargs,fmt);
3254 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
3255 va_end(vargs);
3256 return rb_scan_args_result(&arg, argc);
3257}
3258
3259#undef rb_scan_args_kw
3260int
3261rb_scan_args_kw(int kw_flag, int argc, const VALUE *argv, const char *fmt, ...)
3262{
3263 va_list vargs;
3264 struct rb_scan_args_t arg;
3265 rb_scan_args_parse(kw_flag, fmt, &arg);
3266 va_start(vargs,fmt);
3267 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
3268 va_end(vargs);
3269 return rb_scan_args_result(&arg, argc);
3270}
3271
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_method_id(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
#define rb_define_protected_method(klass, mid, func, arity)
Defines klass#mid and makes it protected.
#define rb_define_module_function(klass, mid, func, arity)
Defines klass#mid and makes it a module function.
#define rb_define_private_method(klass, mid, func, arity)
Defines klass#mid and makes it private.
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition dllexport.h:45
static VALUE RB_OBJ_FROZEN_RAW(VALUE obj)
This is an implementation detail of RB_OBJ_FROZEN().
Definition fl_type.h:707
static void RB_FL_SET_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_SET().
Definition fl_type.h:552
VALUE rb_class_protected_instance_methods(int argc, const VALUE *argv, VALUE mod)
Identical to rb_class_instance_methods(), except it returns names of methods that are protected only.
Definition class.c:2516
static VALUE class_alloc0(enum ruby_value_type type, VALUE klass, bool boxable)
Allocates a struct RClass for a new class, iclass, or module.
Definition class.c:756
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
Definition class.c:1798
VALUE rb_refinement_new(void)
Creates a new, anonymous refinement.
Definition class.c:1691
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition class.c:1591
VALUE rb_class_new(VALUE super)
Creates a new, anonymous class.
Definition class.c:971
static VALUE make_singleton_class(VALUE obj)
Creates a singleton class for obj.
Definition class.c:1411
VALUE rb_singleton_class_clone(VALUE obj)
Clones a singleton class.
Definition class.c:1249
void rb_prepend_module(VALUE klass, VALUE module)
Identical to rb_include_module(), except it "prepends" the passed module to the klass,...
Definition class.c:2048
VALUE rb_class_subclasses(VALUE klass)
Queries the class's direct descendants.
Definition class.c:2296
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
Definition class.c:2915
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition class.c:1622
VALUE rb_class_attached_object(VALUE klass)
Returns the attached object for a singleton class.
Definition class.c:2319
VALUE rb_obj_singleton_methods(int argc, const VALUE *argv, VALUE obj)
Identical to rb_class_instance_methods(), except it returns names of singleton methods instead of ins...
Definition class.c:2693
VALUE rb_module_new(void)
Creates a new, anonymous module.
Definition class.c:1685
#define META_CLASS_OF_CLASS_CLASS_P(k)
whether k is a meta^(n)-class of Class class
Definition class.c:1330
VALUE rb_class_instance_methods(int argc, const VALUE *argv, VALUE mod)
Generates an array of symbols, which are the list of method names defined in the passed class.
Definition class.c:2501
void rb_check_inheritable(VALUE super)
Asserts that the given class can derive a child class.
Definition class.c:956
VALUE rb_class_public_instance_methods(int argc, const VALUE *argv, VALUE mod)
Identical to rb_class_instance_methods(), except it returns names of methods that are public only.
Definition class.c:2554
VALUE rb_class_boot(VALUE super)
A utility function that wraps class_alloc.
Definition class.c:889
VALUE rb_define_module(const char *name)
Defines a top-level module.
Definition class.c:1704
void rb_class_modify_check(VALUE klass)
Asserts that klass is not a frozen class.
Definition eval.c:421
VALUE rb_define_module_id_under(VALUE outer, ID id)
Identical to rb_define_module_under(), except it takes the name in ID instead of C's string.
Definition class.c:1733
void rb_singleton_class_attached(VALUE klass, VALUE obj)
Attaches a singleton class to its corresponding object.
Definition class.c:1318
void rb_freeze_singleton_class(VALUE x)
This is an implementation detail of RB_OBJ_FREEZE().
Definition class.c:2880
VALUE rb_mod_included_modules(VALUE mod)
Queries the list of included modules.
Definition class.c:2114
VALUE rb_define_class_id_under(VALUE outer, ID id, VALUE super)
Identical to rb_define_class_under(), except it takes the name in ID instead of C's string.
Definition class.c:1661
VALUE rb_mod_ancestors(VALUE mod)
Queries the module's ancestors.
Definition class.c:2182
static VALUE make_metaclass(VALUE klass)
Creates a metaclass of klass
Definition class.c:1375
VALUE rb_class_inherited(VALUE super, VALUE klass)
Calls Class::inherited.
Definition class.c:1582
VALUE rb_mod_include_p(VALUE mod, VALUE mod2)
Queries if the passed module is included by the module.
Definition class.c:2150
VALUE rb_class_private_instance_methods(int argc, const VALUE *argv, VALUE mod)
Identical to rb_class_instance_methods(), except it returns names of methods that are private only.
Definition class.c:2539
#define ENSURE_EIGENCLASS(klass)
ensures klass belongs to its own eigenclass.
Definition class.c:1361
VALUE rb_mod_init_copy(VALUE clone, VALUE orig)
The comment that comes with this function says :nodoc:.
Definition class.c:1132
VALUE rb_define_module_under(VALUE outer, const char *name)
Defines a module under the namespace of outer.
Definition class.c:1727
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:2901
VALUE rb_define_module_id(ID id)
This is a very badly designed API that creates an anonymous module.
Definition class.c:1698
VALUE rb_define_class_id(ID id, VALUE super)
This is a very badly designed API that creates an anonymous class.
Definition class.c:1561
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition class.c:2958
VALUE rb_extract_keywords(VALUE *orighash)
Splits a hash into two.
Definition class.c:3019
void rb_define_attr(VALUE klass, const char *name, int read, int write)
Defines public accessor method(s) for an attribute.
Definition class.c:2964
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
Definition class.c:2770
int rb_scan_args_kw(int kw_flag, int argc, const VALUE *argv, const char *fmt,...)
Identical to rb_scan_args(), except it also accepts kw_splat.
Definition class.c:3261
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
Definition class.c:3248
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition eval.c:1010
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
Definition class.c:3037
#define TYPE(_)
Old name of rb_type.
Definition value_type.h:108
#define FL_SINGLETON
Old name of RUBY_FL_SINGLETON.
Definition fl_type.h:58
#define OBJ_INIT_COPY(obj, orig)
Old name of RB_OBJ_INIT_COPY.
Definition object.h:41
#define ALLOC
Old name of RB_ALLOC.
Definition memory.h:400
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
#define xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
#define Qundef
Old name of RUBY_Qundef.
#define T_NIL
Old name of RUBY_T_NIL.
Definition value_type.h:72
#define T_FLOAT
Old name of RUBY_T_FLOAT.
Definition value_type.h:64
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
Definition value_type.h:57
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
Definition fl_type.h:133
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
Definition value_type.h:63
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
Definition assume.h:29
#define ZALLOC
Old name of RB_ZALLOC.
Definition memory.h:402
#define FL_SHAREABLE
Old name of RUBY_FL_SHAREABLE.
Definition fl_type.h:62
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:205
#define xmalloc
Old name of ruby_xmalloc.
Definition xmalloc.h:53
#define T_MODULE
Old name of RUBY_T_MODULE.
Definition value_type.h:70
#define ISDIGIT
Old name of rb_isdigit.
Definition ctype.h:93
#define T_TRUE
Old name of RUBY_T_TRUE.
Definition value_type.h:81
#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:130
#define FL_SET
Old name of RB_FL_SET.
Definition fl_type.h:127
#define T_FALSE
Old name of RUBY_T_FALSE.
Definition value_type.h:61
#define Qtrue
Old name of RUBY_Qtrue.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define NIL_P
Old name of RB_NIL_P.
#define FL_WB_PROTECTED
Old name of RUBY_FL_WB_PROTECTED.
Definition fl_type.h:59
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
Definition value_type.h:80
#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_TEST
Old name of RB_FL_TEST.
Definition fl_type.h:129
#define CONST_ID
Old name of RUBY_CONST_ID.
Definition symbol.h:47
#define rb_ary_new2
Old name of rb_ary_new_capa.
Definition array.h:657
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
Definition fl_type.h:128
#define SYMBOL_P
Old name of RB_SYMBOL_P.
Definition value_type.h:88
#define OBJ_FROZEN_RAW
Old name of RB_OBJ_FROZEN_RAW.
Definition fl_type.h:136
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition eval.c:653
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1418
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Identical to rb_exc_new_cstr(), except it takes a Ruby's string instead of C's.
Definition error.c:1469
VALUE rb_cClass
Class class.
Definition object.c:63
VALUE rb_mKernel
Kernel module.
Definition object.c:60
VALUE rb_cRefinement
Refinement class.
Definition object.c:64
VALUE rb_cNilClass
NilClass class.
Definition object.c:66
VALUE rb_cHash
Hash class.
Definition hash.c:109
VALUE rb_cFalseClass
FalseClass class.
Definition object.c:68
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:264
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
Definition object.c:686
VALUE rb_cBasicObject
BasicObject class.
Definition object.c:59
VALUE rb_cModule
Module class.
Definition object.c:62
VALUE rb_class_real(VALUE klass)
Finds a "real" class.
Definition object.c:255
VALUE rb_cTrueClass
TrueClass class.
Definition object.c:67
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
Definition gc.h:615
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:603
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1117
#define RGENGC_WB_PROTECTED_CLASS
This is a compile-time flag to enable/disable write barrier for struct RClass.
Definition gc.h:523
VALUE rb_ary_new_from_values(long n, const VALUE *elts)
Identical to rb_ary_new_from_args(), except how objects are passed.
VALUE rb_ary_cat(VALUE ary, const VALUE *train, long len)
Destructively appends multiple elements at the end of the array.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_resize(VALUE ary, long len)
Expands or shrinks the passed array to the passed length.
VALUE rb_ary_hidden_new(long capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
#define UNLIMITED_ARGUMENTS
This macro is used in conjunction with rb_check_arity().
Definition error.h:35
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition error.h:284
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
Definition proc.c:986
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
Definition string.c:3798
#define rb_str_cat_cstr(buf, str)
Identical to rb_str_cat(), except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1655
VALUE rb_const_get(VALUE space, ID name)
Identical to rb_const_defined(), except it returns the actual defined value.
Definition variable.c:3448
void rb_const_set(VALUE space, ID name, VALUE val)
Names a constant.
Definition variable.c:3926
VALUE rb_const_get_at(VALUE space, ID name)
Identical to rb_const_defined_at(), except it returns the actual defined value.
Definition variable.c:3454
void rb_set_class_path_string(VALUE klass, VALUE space, VALUE name)
Identical to rb_set_class_path(), except it accepts the name as Ruby's string instead of C's.
Definition variable.c:423
int rb_const_defined_at(VALUE space, ID name)
Identical to rb_const_defined(), except it doesn't look for parent classes.
Definition variable.c:3786
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
Definition variable.c:380
int rb_const_defined(VALUE space, ID name)
Queries if the constant is defined at the namespace.
Definition variable.c:3780
void rb_alias(VALUE klass, ID dst, ID src)
Resembles alias.
Definition vm_method.c:2698
void rb_attr(VALUE klass, ID name, int need_reader, int need_writer, int honour_visibility)
This function resembles now-deprecated Module#attr.
Definition vm_method.c:2278
void rb_clear_constant_cache_for_id(ID id)
Clears the inline constant caches associated with a particular ID.
Definition vm_method.c:329
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
Definition symbol.h:285
int len
Length of the buffer.
Definition io.h:8
static bool rb_ractor_shareable_p(VALUE obj)
Queries if multiple Ractors can share the passed object or not.
Definition ractor.h:249
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
Definition memory.h:372
VALUE type(ANYARGS)
ANYARGS-ed function type.
void rb_hash_foreach(VALUE q, int_type *w, VALUE e)
Iteration over the given hash.
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:51
#define RARRAY_AREF(a, i)
Definition rarray.h:403
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
Definition rbasic.h:166
#define RCLASS_SUPER
Just another name of rb_class_get_superclass.
Definition rclass.h:44
#define RHASH_SIZE(h)
Queries the size of the hash.
Definition rhash.h:69
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
Definition rhash.h:79
#define RB_SCAN_ARGS_PASS_CALLED_KEYWORDS
Same behaviour as rb_scan_args().
Definition scan_args.h:50
#define RTEST
This is an old name of RB_TEST.
#define ANYARGS
Functions declared using this macro take arbitrary arguments, including void.
Definition stdarg.h:64
Definition class.c:2381
Internal header for Ruby Box.
Definition box.h:14
Internal header for Class.
Definition class.h:30
Definition constant.h:33
CREF (Class REFerence)
Definition method.h:45
Definition class.h:72
Definition method.h:55
rb_cref_t * cref
class reference, should be marked
Definition method.h:144
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
Definition method.h:143
Definition class.h:65
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 void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
Definition value_type.h:433
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_value_type
C-level type of an object.
Definition value_type.h:113