Ruby 4.0.0dev (2025-12-25 revision 6a66129d6c289b0da99cd89592f5ee948da6f381)
class.c (6a66129d6c289b0da99cd89592f5ee948da6f381)
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 if (RBASIC_CLASS(iclass) == klass) {
495 // Is the subclass an ICLASS including this module into another class
496 // If so we need to re-associate it under our box with the new ext
497 VM_ASSERT(FL_TEST_RAW(iclass, RCLASS_BOXABLE));
498 class_duplicate_iclass_classext(iclass, ext, box);
499 }
500 }
501 subclass_entry = subclass_entry->next;
502 }
503 }
504
505 return ext;
506}
507
508void
509rb_class_ensure_writable(VALUE klass)
510{
511 VM_ASSERT(RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_MODULE) || RB_TYPE_P(klass, T_ICLASS));
512 RCLASS_EXT_WRITABLE(klass);
513}
514
516 rb_class_classext_foreach_callback_func *func;
517 void * callback_arg;
518};
519
520static int
521class_classext_foreach_i(st_data_t key, st_data_t value, st_data_t arg)
522{
524 rb_class_classext_foreach_callback_func *func = foreach_arg->func;
525 func((rb_classext_t *)value, false, (VALUE)key, foreach_arg->callback_arg);
526 return ST_CONTINUE;
527}
528
529void
530rb_class_classext_foreach(VALUE klass, rb_class_classext_foreach_callback_func *func, void *arg)
531{
532 st_table *tbl = RCLASS_CLASSEXT_TBL(klass);
534 if (tbl) {
535 foreach_arg.func = func;
536 foreach_arg.callback_arg = arg;
537 rb_st_foreach(tbl, class_classext_foreach_i, (st_data_t)&foreach_arg);
538 }
539 func(RCLASS_EXT_PRIME(klass), true, (VALUE)NULL, arg);
540}
541
542VALUE
543rb_class_super_of(VALUE klass)
544{
545 return RCLASS_SUPER(klass);
546}
547
548VALUE
549rb_class_singleton_p(VALUE klass)
550{
551 return RCLASS_SINGLETON_P(klass);
552}
553
554unsigned char
555rb_class_variation_count(VALUE klass)
556{
557 return RCLASS_VARIATION_COUNT(klass);
558}
559
560static void
561push_subclass_entry_to_list(VALUE super, VALUE klass, bool is_module)
562{
563 rb_subclass_entry_t *entry, *head;
564 rb_subclass_anchor_t *anchor;
565 rb_box_subclasses_t *box_subclasses;
566 struct st_table *tbl;
567 const rb_box_t *box = rb_current_box();
568
570 entry->klass = klass;
571
572 RB_VM_LOCKING() {
573 anchor = RCLASS_WRITABLE_SUBCLASSES(super);
574 VM_ASSERT(anchor);
575 box_subclasses = (rb_box_subclasses_t *)anchor->box_subclasses;
576 VM_ASSERT(box_subclasses);
577 tbl = box_subclasses->tbl;
578 VM_ASSERT(tbl);
579
580 head = anchor->head;
581 if (head->next) {
582 head->next->prev = entry;
583 entry->next = head->next;
584 }
585 head->next = entry;
586 entry->prev = head;
587 st_insert(tbl, box_subclasses_tbl_key(box), (st_data_t)entry);
588 }
589
590 if (is_module) {
591 RCLASS_WRITE_BOX_MODULE_SUBCLASSES(klass, anchor->box_subclasses);
592 }
593 else {
594 RCLASS_WRITE_BOX_SUPER_SUBCLASSES(klass, anchor->box_subclasses);
595 }
596}
597
598void
599rb_class_subclass_add(VALUE super, VALUE klass)
600{
601 if (super && !UNDEF_P(super)) {
602 push_subclass_entry_to_list(super, klass, false);
603 }
604}
605
606static void
607rb_module_add_to_subclasses_list(VALUE module, VALUE iclass)
608{
609 if (module && !UNDEF_P(module)) {
610 push_subclass_entry_to_list(module, iclass, true);
611 }
612}
613
614static struct rb_subclass_entry *
615class_get_subclasses_for_ns(struct st_table *tbl, VALUE box_id)
616{
617 st_data_t value;
618 if (st_lookup(tbl, (st_data_t)box_id, &value)) {
619 return (struct rb_subclass_entry *)value;
620 }
621 return NULL;
622}
623
624static int
625remove_class_from_subclasses_replace_first_entry(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
626{
627 *value = arg;
628 return ST_CONTINUE;
629}
630
631static void
632remove_class_from_subclasses(struct st_table *tbl, VALUE box_id, VALUE klass)
633{
634 rb_subclass_entry_t *entry = class_get_subclasses_for_ns(tbl, box_id);
635 bool first_entry = true;
636 while (entry) {
637 if (entry->klass == klass) {
638 rb_subclass_entry_t *prev = entry->prev, *next = entry->next;
639
640 if (prev) {
641 prev->next = next;
642 }
643 if (next) {
644 next->prev = prev;
645 }
646
647 if (first_entry) {
648 if (next) {
649 st_update(tbl, box_id, remove_class_from_subclasses_replace_first_entry, (st_data_t)next);
650 }
651 else {
652 // no subclass entries in this ns after the deletion
653 st_delete(tbl, &box_id, NULL);
654 }
655 }
656
657 xfree(entry);
658
659 break;
660 }
661 else if (first_entry) {
662 first_entry = false;
663 }
664 entry = entry->next;
665 }
666}
667
668void
669rb_class_remove_from_super_subclasses(VALUE klass)
670{
671 rb_classext_t *ext = RCLASS_EXT_WRITABLE(klass);
672 rb_box_subclasses_t *box_subclasses = RCLASSEXT_BOX_SUPER_SUBCLASSES(ext);
673
674 if (!box_subclasses) return;
675 remove_class_from_subclasses(box_subclasses->tbl, box_subclasses_tbl_key(RCLASSEXT_BOX(ext)), klass);
676 rb_box_subclasses_ref_dec(box_subclasses);
677 RCLASSEXT_BOX_SUPER_SUBCLASSES(ext) = 0;
678}
679
680void
681rb_class_classext_free_subclasses(rb_classext_t *ext, VALUE klass, bool replacing)
682{
683 rb_subclass_anchor_t *anchor = RCLASSEXT_SUBCLASSES(ext);
684 struct st_table *tbl = anchor->box_subclasses->tbl;
685 VALUE box_id = box_subclasses_tbl_key(RCLASSEXT_BOX(ext));
686 rb_subclass_entry_t *next, *entry = anchor->head;
687
688 while (entry) {
689 next = entry->next;
690 xfree(entry);
691 entry = next;
692 }
693 VM_ASSERT(
694 rb_box_subclasses_ref_count(anchor->box_subclasses) > 0,
695 "box_subclasses refcount (%p) %ld", anchor->box_subclasses, rb_box_subclasses_ref_count(anchor->box_subclasses));
696 st_delete(tbl, &box_id, NULL);
697 rb_box_subclasses_ref_dec(anchor->box_subclasses);
698 xfree(anchor);
699
700 if (RCLASSEXT_BOX_SUPER_SUBCLASSES(ext)) {
701 rb_box_subclasses_t *box_sub = RCLASSEXT_BOX_SUPER_SUBCLASSES(ext);
702 if (!replacing) remove_class_from_subclasses(box_sub->tbl, box_id, klass);
703 rb_box_subclasses_ref_dec(box_sub);
704 }
705 if (RCLASSEXT_BOX_MODULE_SUBCLASSES(ext)) {
706 rb_box_subclasses_t *box_sub = RCLASSEXT_BOX_MODULE_SUBCLASSES(ext);
707 if (!replacing) remove_class_from_subclasses(box_sub->tbl, box_id, klass);
708 rb_box_subclasses_ref_dec(box_sub);
709 }
710}
711
712void
713rb_class_foreach_subclass(VALUE klass, void (*f)(VALUE, VALUE), VALUE arg)
714{
716 rb_subclass_entry_t *cur = RCLASS_SUBCLASSES_FIRST(klass);
717 /* do not be tempted to simplify this loop into a for loop, the order of
718 operations is important here if `f` modifies the linked list */
719 while (cur) {
720 VALUE curklass = cur->klass;
721 tmp = cur->next;
722 // do not trigger GC during f, otherwise the cur will become
723 // a dangling pointer if the subclass is collected
724 f(curklass, arg);
725 cur = tmp;
726 }
727}
728
729static void
730class_detach_subclasses(VALUE klass, VALUE arg)
731{
732 rb_class_remove_from_super_subclasses(klass);
733}
734
735static void
736class_switch_superclass(VALUE super, VALUE klass)
737{
738 RB_VM_LOCKING() {
739 class_detach_subclasses(klass, Qnil);
740 rb_class_subclass_add(super, klass);
741 }
742}
743
754static VALUE
755class_alloc0(enum ruby_value_type type, VALUE klass, bool boxable)
756{
757 rb_box_subclasses_t *box_subclasses;
758 rb_subclass_anchor_t *anchor;
759 const rb_box_t *box = rb_current_box();
760
761 if (!ruby_box_init_done) {
762 boxable = true;
763 }
764
765 size_t alloc_size = sizeof(struct RClass_and_rb_classext_t);
766 if (boxable) {
767 alloc_size = sizeof(struct RClass_boxable);
768 }
769
770 // class_alloc is supposed to return a new object that is not promoted yet.
771 // So, we need to avoid GC after NEWOBJ_OF.
772 // To achieve that, we allocate subclass lists before NEWOBJ_OF.
773 //
774 // TODO: Note that this could cause memory leak.
775 // If NEWOBJ_OF fails with out of memory, these buffers will leak.
776 box_subclasses = ZALLOC(rb_box_subclasses_t);
777 box_subclasses->refcount = 1;
778 box_subclasses->tbl = st_init_numtable();
780 anchor->box_subclasses = box_subclasses;
781 anchor->head = ZALLOC(rb_subclass_entry_t);
782
784
785 VALUE flags = type | FL_SHAREABLE;
787 if (boxable) flags |= RCLASS_BOXABLE;
788
789 NEWOBJ_OF(obj, struct RClass, klass, flags, alloc_size, 0);
790
791 obj->object_id = 0;
792
793 memset(RCLASS_EXT_PRIME(obj), 0, sizeof(rb_classext_t));
794
795 /* ZALLOC
796 RCLASS_CONST_TBL(obj) = 0;
797 RCLASS_M_TBL(obj) = 0;
798 RCLASS_FIELDS(obj) = 0;
799 RCLASS_SET_SUPER((VALUE)obj, 0);
800 */
801
802 if (boxable) {
803 ((struct RClass_boxable *)obj)->box_classext_tbl = NULL;
804 }
805
806 RCLASS_PRIME_BOX((VALUE)obj) = box;
807 // Classes/Modules defined in user boxes are
808 // writable directly because it exists only in a box.
809 RCLASS_SET_PRIME_CLASSEXT_WRITABLE((VALUE)obj, !boxable || BOX_USER_P(box));
810
811 RCLASS_SET_ORIGIN((VALUE)obj, (VALUE)obj);
812 RCLASS_SET_REFINED_CLASS((VALUE)obj, Qnil);
813
814 RCLASS_SET_SUBCLASSES((VALUE)obj, anchor);
815
816 return (VALUE)obj;
817}
818
819static VALUE
820class_alloc(enum ruby_value_type type, VALUE klass)
821{
822 return class_alloc0(type, klass, false);
823}
824
825static VALUE
826class_associate_super(VALUE klass, VALUE super, bool init)
827{
828 if (super && !UNDEF_P(super)) {
829 class_switch_superclass(super, klass);
830 }
831 if (init) {
832 RCLASS_SET_SUPER(klass, super);
833 }
834 else {
835 RCLASS_WRITE_SUPER(klass, super);
836 }
837 rb_class_update_superclasses(klass);
838 return super;
839}
840
841VALUE
842rb_class_set_super(VALUE klass, VALUE super)
843{
844 return class_associate_super(klass, super, false);
845}
846
847static void
848class_initialize_method_table(VALUE c)
849{
850 // initialize the prime classext m_tbl
851 RCLASS_SET_M_TBL(c, rb_id_table_create(0));
852}
853
854static void
855class_clear_method_table(VALUE c)
856{
857 RCLASS_WRITE_M_TBL(c, rb_id_table_create(0));
858}
859
860static VALUE
861class_boot_boxable(VALUE super, bool boxable)
862{
863 VALUE klass = class_alloc0(T_CLASS, rb_cClass, boxable);
864
865 // initialize method table prior to class_associate_super()
866 // because class_associate_super() may cause GC and promote klass
867 class_initialize_method_table(klass);
868
869 class_associate_super(klass, super, true);
870 if (super && !UNDEF_P(super)) {
871 rb_class_set_initialized(klass);
872 }
873
874 return (VALUE)klass;
875}
876
886VALUE
888{
889 return class_boot_boxable(super, false);
890}
891
892static VALUE *
893class_superclasses_including_self(VALUE klass)
894{
895 if (RCLASS_SUPERCLASSES_WITH_SELF_P(klass))
896 return RCLASS_SUPERCLASSES(klass);
897
898 size_t depth = RCLASS_SUPERCLASS_DEPTH(klass);
899 VALUE *superclasses = xmalloc(sizeof(VALUE) * (depth + 1));
900 if (depth > 0)
901 memcpy(superclasses, RCLASS_SUPERCLASSES(klass), sizeof(VALUE) * depth);
902 superclasses[depth] = klass;
903
904 return superclasses;
905}
906
907void
908rb_class_update_superclasses(VALUE klass)
909{
910 VALUE *superclasses;
911 size_t super_depth;
912 VALUE super = RCLASS_SUPER(klass);
913
914 if (!RB_TYPE_P(klass, T_CLASS)) return;
915 if (UNDEF_P(super)) return;
916
917 // If the superclass array is already built
918 if (RCLASS_SUPERCLASSES(klass))
919 return;
920
921 // find the proper superclass
922 while (super != Qfalse && !RB_TYPE_P(super, T_CLASS)) {
923 super = RCLASS_SUPER(super);
924 }
925
926 // For BasicObject and uninitialized classes, depth=0 and ary=NULL
927 if (super == Qfalse)
928 return;
929
930 // Sometimes superclasses are set before the full ancestry tree is built
931 // This happens during metaclass construction
932 if (super != rb_cBasicObject && !RCLASS_SUPERCLASS_DEPTH(super)) {
933 rb_class_update_superclasses(super);
934
935 // If it is still unset we need to try later
936 if (!RCLASS_SUPERCLASS_DEPTH(super))
937 return;
938 }
939
940 super_depth = RCLASS_SUPERCLASS_DEPTH(super);
941 if (RCLASS_SUPERCLASSES_WITH_SELF_P(super)) {
942 superclasses = RCLASS_SUPERCLASSES(super);
943 }
944 else {
945 superclasses = class_superclasses_including_self(super);
946 RCLASS_WRITE_SUPERCLASSES(super, super_depth, superclasses, true);
947 }
948
949 size_t depth = super_depth == RCLASS_MAX_SUPERCLASS_DEPTH ? super_depth : super_depth + 1;
950 RCLASS_WRITE_SUPERCLASSES(klass, depth, superclasses, false);
951}
952
953void
955{
956 if (!RB_TYPE_P(super, T_CLASS)) {
957 rb_raise(rb_eTypeError, "superclass must be an instance of Class (given an instance of %"PRIsVALUE")",
958 rb_obj_class(super));
959 }
960 if (RCLASS_SINGLETON_P(super)) {
961 rb_raise(rb_eTypeError, "can't make subclass of singleton class");
962 }
963 if (super == rb_cClass) {
964 rb_raise(rb_eTypeError, "can't make subclass of Class");
965 }
966}
967
968VALUE
970{
971 Check_Type(super, T_CLASS);
973 VALUE klass = rb_class_boot(super);
974
975 if (super != rb_cObject && super != rb_cBasicObject) {
976 RCLASS_SET_MAX_IV_COUNT(klass, RCLASS_MAX_IV_COUNT(super));
977 }
978
979 RUBY_ASSERT(getenv("RUBY_BOX") || RCLASS_PRIME_CLASSEXT_WRITABLE_P(klass));
980
981 return klass;
982}
983
984VALUE
985rb_class_s_alloc(VALUE klass)
986{
987 return rb_class_boot(0);
988}
989
990static void
991clone_method(VALUE old_klass, VALUE new_klass, ID mid, const rb_method_entry_t *me)
992{
993 if (me->def->type == VM_METHOD_TYPE_ISEQ) {
994 rb_cref_t *new_cref = rb_vm_rewrite_cref(me->def->body.iseq.cref, old_klass, new_klass);
995 rb_add_method_iseq(new_klass, mid, me->def->body.iseq.iseqptr, new_cref, METHOD_ENTRY_VISI(me));
996 }
997 else {
998 rb_method_entry_set(new_klass, mid, me, METHOD_ENTRY_VISI(me));
999 }
1000}
1001
1003 VALUE new_klass;
1004 VALUE old_klass;
1005};
1006
1007static enum rb_id_table_iterator_result
1008clone_method_i(ID key, VALUE value, void *data)
1009{
1010 const struct clone_method_arg *arg = (struct clone_method_arg *)data;
1011 clone_method(arg->old_klass, arg->new_klass, key, (const rb_method_entry_t *)value);
1012 return ID_TABLE_CONTINUE;
1013}
1014
1016 VALUE klass;
1017 struct rb_id_table *tbl;
1018};
1019
1020static int
1021clone_const(ID key, const rb_const_entry_t *ce, struct clone_const_arg *arg)
1022{
1024 MEMCPY(nce, ce, rb_const_entry_t, 1);
1025 RB_OBJ_WRITTEN(arg->klass, Qundef, ce->value);
1026 RB_OBJ_WRITTEN(arg->klass, Qundef, ce->file);
1027
1028 rb_id_table_insert(arg->tbl, key, (VALUE)nce);
1029 return ID_TABLE_CONTINUE;
1030}
1031
1032static enum rb_id_table_iterator_result
1033clone_const_i(ID key, VALUE value, void *data)
1034{
1035 return clone_const(key, (const rb_const_entry_t *)value, data);
1036}
1037
1038static void
1039class_init_copy_check(VALUE clone, VALUE orig)
1040{
1041 if (orig == rb_cBasicObject) {
1042 rb_raise(rb_eTypeError, "can't copy the root class");
1043 }
1044 if (RCLASS_INITIALIZED_P(clone)) {
1045 rb_raise(rb_eTypeError, "already initialized class");
1046 }
1047 if (RCLASS_SINGLETON_P(orig)) {
1048 rb_raise(rb_eTypeError, "can't copy singleton class");
1049 }
1050}
1051
1053 VALUE clone;
1054 struct rb_id_table * new_table;
1055};
1056
1057static enum rb_id_table_iterator_result
1058cvc_table_copy(ID id, VALUE val, void *data)
1059{
1060 struct cvc_table_copy_ctx *ctx = (struct cvc_table_copy_ctx *)data;
1061 struct rb_cvar_class_tbl_entry * orig_entry;
1062 orig_entry = (struct rb_cvar_class_tbl_entry *)val;
1063
1064 struct rb_cvar_class_tbl_entry *ent;
1065
1066 ent = ALLOC(struct rb_cvar_class_tbl_entry);
1067 ent->class_value = ctx->clone;
1068 ent->cref = orig_entry->cref;
1069 ent->global_cvar_state = orig_entry->global_cvar_state;
1070 rb_id_table_insert(ctx->new_table, id, (VALUE)ent);
1071
1072 RB_OBJ_WRITTEN(ctx->clone, Qundef, ent->cref);
1073
1074 return ID_TABLE_CONTINUE;
1075}
1076
1077static void
1078copy_tables(VALUE clone, VALUE orig)
1079{
1080 if (RCLASS_CONST_TBL(clone)) {
1081 rb_free_const_table(RCLASS_CONST_TBL(clone));
1082 RCLASS_WRITE_CONST_TBL(clone, 0, false);
1083 }
1084 if (RCLASS_CVC_TBL(orig)) {
1085 struct rb_id_table *rb_cvc_tbl = RCLASS_CVC_TBL(orig);
1086 struct rb_id_table *rb_cvc_tbl_dup = rb_id_table_create(rb_id_table_size(rb_cvc_tbl));
1087
1088 struct cvc_table_copy_ctx ctx;
1089 ctx.clone = clone;
1090 ctx.new_table = rb_cvc_tbl_dup;
1091 rb_id_table_foreach(rb_cvc_tbl, cvc_table_copy, &ctx);
1092 RCLASS_WRITE_CVC_TBL(clone, rb_cvc_tbl_dup);
1093 }
1094 rb_id_table_free(RCLASS_M_TBL(clone));
1095 RCLASS_WRITE_M_TBL(clone, 0);
1096 if (!RB_TYPE_P(clone, T_ICLASS)) {
1097 rb_fields_tbl_copy(clone, orig);
1098 }
1099 if (RCLASS_CONST_TBL(orig)) {
1100 struct clone_const_arg arg;
1101 struct rb_id_table *const_tbl;
1102 struct rb_id_table *orig_tbl = RCLASS_CONST_TBL(orig);
1103 arg.tbl = const_tbl = rb_id_table_create(rb_id_table_size(orig_tbl));
1104 arg.klass = clone;
1105 rb_id_table_foreach(orig_tbl, clone_const_i, &arg);
1106 RCLASS_WRITE_CONST_TBL(clone, const_tbl, false);
1107 }
1108}
1109
1110static bool ensure_origin(VALUE klass);
1111
1112void
1113rb_class_set_initialized(VALUE klass)
1114{
1115 RUBY_ASSERT(RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_MODULE));
1116 FL_SET_RAW(klass, RCLASS_IS_INITIALIZED);
1117 /* no more re-initialization */
1118}
1119
1120void
1121rb_module_check_initializable(VALUE mod)
1122{
1123 if (RCLASS_INITIALIZED_P(mod)) {
1124 rb_raise(rb_eTypeError, "already initialized module");
1125 }
1126}
1127
1128/* :nodoc: */
1129VALUE
1131{
1132 /* Only class or module is valid here, but other classes may enter here and
1133 * only hit an exception on the OBJ_INIT_COPY checks
1134 */
1135 switch (BUILTIN_TYPE(clone)) {
1136 case T_CLASS:
1137 class_init_copy_check(clone, orig);
1138 break;
1139 case T_MODULE:
1140 rb_module_check_initializable(clone);
1141 break;
1142 default:
1143 break;
1144 }
1145 if (!OBJ_INIT_COPY(clone, orig)) return clone;
1146
1148 RUBY_ASSERT(BUILTIN_TYPE(clone) == BUILTIN_TYPE(orig));
1149
1150 rb_class_set_initialized(clone);
1151
1152 /* cloned flag is refer at constant inline cache
1153 * see vm_get_const_key_cref() in vm_insnhelper.c
1154 */
1155 RCLASS_SET_CLONED(clone, true);
1156 RCLASS_SET_CLONED(orig, true);
1157
1158 if (!RCLASS_SINGLETON_P(CLASS_OF(clone))) {
1159 RBASIC_SET_CLASS(clone, rb_singleton_class_clone(orig));
1160 rb_singleton_class_attached(METACLASS_OF(clone), (VALUE)clone);
1161 }
1162 if (BUILTIN_TYPE(clone) == T_CLASS) {
1163 RCLASS_SET_ALLOCATOR(clone, RCLASS_ALLOCATOR(orig));
1164 }
1165 copy_tables(clone, orig);
1166 if (RCLASS_M_TBL(orig)) {
1167 struct clone_method_arg arg;
1168 arg.old_klass = orig;
1169 arg.new_klass = clone;
1170 class_initialize_method_table(clone);
1171 rb_id_table_foreach(RCLASS_M_TBL(orig), clone_method_i, &arg);
1172 }
1173
1174 if (RCLASS_ORIGIN(orig) == orig) {
1175 rb_class_set_super(clone, RCLASS_SUPER(orig));
1176 }
1177 else {
1178 VALUE p = RCLASS_SUPER(orig);
1179 VALUE orig_origin = RCLASS_ORIGIN(orig);
1180 VALUE prev_clone_p = clone;
1181 VALUE origin_stack = rb_ary_hidden_new(2);
1182 VALUE origin[2];
1183 VALUE clone_p = 0;
1184 long origin_len;
1185 int add_subclass;
1186 VALUE clone_origin;
1187
1188 ensure_origin(clone);
1189 clone_origin = RCLASS_ORIGIN(clone);
1190
1191 while (p && p != orig_origin) {
1192 if (BUILTIN_TYPE(p) != T_ICLASS) {
1193 rb_bug("non iclass between module/class and origin");
1194 }
1195 clone_p = class_alloc(T_ICLASS, METACLASS_OF(p));
1196 RCLASS_SET_M_TBL(clone_p, RCLASS_M_TBL(p));
1197 rb_class_set_super(prev_clone_p, clone_p);
1198 prev_clone_p = clone_p;
1199 RCLASS_SET_CONST_TBL(clone_p, RCLASS_CONST_TBL(p), false);
1200 if (RB_TYPE_P(clone, T_CLASS)) {
1201 RCLASS_SET_INCLUDER(clone_p, clone);
1202 }
1203 add_subclass = TRUE;
1204 if (p != RCLASS_ORIGIN(p)) {
1205 origin[0] = clone_p;
1206 origin[1] = RCLASS_ORIGIN(p);
1207 rb_ary_cat(origin_stack, origin, 2);
1208 }
1209 else if ((origin_len = RARRAY_LEN(origin_stack)) > 1 &&
1210 RARRAY_AREF(origin_stack, origin_len - 1) == p) {
1211 RCLASS_WRITE_ORIGIN(RARRAY_AREF(origin_stack, (origin_len -= 2)), clone_p);
1212 RICLASS_WRITE_ORIGIN_SHARED_MTBL(clone_p);
1213 rb_ary_resize(origin_stack, origin_len);
1214 add_subclass = FALSE;
1215 }
1216 if (add_subclass) {
1217 rb_module_add_to_subclasses_list(METACLASS_OF(p), clone_p);
1218 }
1219 p = RCLASS_SUPER(p);
1220 }
1221
1222 if (p == orig_origin) {
1223 if (clone_p) {
1224 rb_class_set_super(clone_p, clone_origin);
1225 rb_class_set_super(clone_origin, RCLASS_SUPER(orig_origin));
1226 }
1227 copy_tables(clone_origin, orig_origin);
1228 if (RCLASS_M_TBL(orig_origin)) {
1229 struct clone_method_arg arg;
1230 arg.old_klass = orig;
1231 arg.new_klass = clone;
1232 class_initialize_method_table(clone_origin);
1233 rb_id_table_foreach(RCLASS_M_TBL(orig_origin), clone_method_i, &arg);
1234 }
1235 }
1236 else {
1237 rb_bug("no origin for class that has origin");
1238 }
1239
1240 rb_class_update_superclasses(clone);
1241 }
1242
1243 return clone;
1244}
1245
1246VALUE
1248{
1249 return rb_singleton_class_clone_and_attach(obj, Qundef);
1250}
1251
1252// Clone and return the singleton class of `obj` if it has been created and is attached to `obj`.
1253VALUE
1254rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach)
1255{
1256 const VALUE klass = METACLASS_OF(obj);
1257
1258 // Note that `rb_singleton_class()` can create situations where `klass` is
1259 // attached to an object other than `obj`. In which case `obj` does not have
1260 // a material singleton class attached yet and there is no singleton class
1261 // to clone.
1262 if (!(RCLASS_SINGLETON_P(klass) && RCLASS_ATTACHED_OBJECT(klass) == obj)) {
1263 // nothing to clone
1264 return klass;
1265 }
1266 else {
1267 /* copy singleton(unnamed) class */
1268 bool klass_of_clone_is_new;
1269 RUBY_ASSERT(RB_TYPE_P(klass, T_CLASS));
1270 VALUE clone = class_alloc(T_CLASS, 0);
1271
1272 if (BUILTIN_TYPE(obj) == T_CLASS) {
1273 klass_of_clone_is_new = true;
1274 RBASIC_SET_CLASS(clone, clone);
1275 }
1276 else {
1277 VALUE klass_metaclass_clone = rb_singleton_class_clone(klass);
1278 // When `METACLASS_OF(klass) == klass_metaclass_clone`, it means the
1279 // recursive call did not clone `METACLASS_OF(klass)`.
1280 klass_of_clone_is_new = (METACLASS_OF(klass) != klass_metaclass_clone);
1281 RBASIC_SET_CLASS(clone, klass_metaclass_clone);
1282 }
1283
1284 // initialize method table before any GC chance
1285 class_initialize_method_table(clone);
1286
1287 rb_class_set_super(clone, RCLASS_SUPER(klass));
1288 rb_fields_tbl_copy(clone, klass);
1289 if (RCLASS_CONST_TBL(klass)) {
1290 struct clone_const_arg arg;
1291 struct rb_id_table *table;
1292 arg.tbl = table = rb_id_table_create(rb_id_table_size(RCLASS_CONST_TBL(klass)));
1293 arg.klass = clone;
1294 rb_id_table_foreach(RCLASS_CONST_TBL(klass), clone_const_i, &arg);
1295 RCLASS_SET_CONST_TBL(clone, table, false);
1296 }
1297 if (!UNDEF_P(attach)) {
1298 rb_singleton_class_attached(clone, attach);
1299 }
1300 {
1301 struct clone_method_arg arg;
1302 arg.old_klass = klass;
1303 arg.new_klass = clone;
1304 rb_id_table_foreach(RCLASS_M_TBL(klass), clone_method_i, &arg);
1305 }
1306 if (klass_of_clone_is_new) {
1307 rb_singleton_class_attached(METACLASS_OF(clone), clone);
1308 }
1309 FL_SET(clone, FL_SINGLETON);
1310
1311 return clone;
1312 }
1313}
1314
1315void
1317{
1318 if (RCLASS_SINGLETON_P(klass)) {
1319 RCLASS_SET_ATTACHED_OBJECT(klass, obj);
1320 }
1321}
1322
1328#define META_CLASS_OF_CLASS_CLASS_P(k) (METACLASS_OF(k) == (k))
1329
1330static int
1331rb_singleton_class_has_metaclass_p(VALUE sklass)
1332{
1333 return RCLASS_ATTACHED_OBJECT(METACLASS_OF(sklass)) == sklass;
1334}
1335
1336int
1337rb_singleton_class_internal_p(VALUE sklass)
1338{
1339 return (RB_TYPE_P(RCLASS_ATTACHED_OBJECT(sklass), T_CLASS) &&
1340 !rb_singleton_class_has_metaclass_p(sklass));
1341}
1342
1348#define HAVE_METACLASS_P(k) \
1349 (FL_TEST(METACLASS_OF(k), FL_SINGLETON) && \
1350 rb_singleton_class_has_metaclass_p(k))
1351
1359#define ENSURE_EIGENCLASS(klass) \
1360 (HAVE_METACLASS_P(klass) ? METACLASS_OF(klass) : make_metaclass(klass))
1361
1362
1372static inline VALUE
1374{
1375 VALUE super;
1376 VALUE metaclass = class_boot_boxable(Qundef, FL_TEST_RAW(klass, RCLASS_BOXABLE));
1377
1378 FL_SET(metaclass, FL_SINGLETON);
1379 rb_singleton_class_attached(metaclass, klass);
1380
1381 if (META_CLASS_OF_CLASS_CLASS_P(klass)) {
1382 SET_METACLASS_OF(klass, metaclass);
1383 SET_METACLASS_OF(metaclass, metaclass);
1384 }
1385 else {
1386 VALUE tmp = METACLASS_OF(klass); /* for a meta^(n)-class klass, tmp is meta^(n)-class of Class class */
1387 SET_METACLASS_OF(klass, metaclass);
1388 SET_METACLASS_OF(metaclass, ENSURE_EIGENCLASS(tmp));
1389 }
1390
1391 super = RCLASS_SUPER(klass);
1392 while (RB_TYPE_P(super, T_ICLASS)) super = RCLASS_SUPER(super);
1393 class_associate_super(metaclass, super ? ENSURE_EIGENCLASS(super) : rb_cClass, true);
1394 rb_class_set_initialized(klass);
1395
1396 // Full class ancestry may not have been filled until we reach here.
1397 rb_class_update_superclasses(METACLASS_OF(metaclass));
1398
1399 return metaclass;
1400}
1401
1408static inline VALUE
1410{
1411 VALUE orig_class = METACLASS_OF(obj);
1412 VALUE klass = class_boot_boxable(orig_class, FL_TEST_RAW(orig_class, RCLASS_BOXABLE));
1413
1414 FL_SET(klass, FL_SINGLETON);
1415 RBASIC_SET_CLASS(obj, klass);
1416 rb_singleton_class_attached(klass, obj);
1417 rb_yjit_invalidate_no_singleton_class(orig_class);
1418 rb_zjit_invalidate_no_singleton_class(orig_class);
1419
1420 SET_METACLASS_OF(klass, METACLASS_OF(rb_class_real(orig_class)));
1421 return klass;
1422}
1423
1424
1425static VALUE
1426boot_defclass(const char *name, VALUE super)
1427{
1428 VALUE obj = rb_class_boot(super);
1429 ID id = rb_intern(name);
1430
1431 rb_const_set((rb_cObject ? rb_cObject : obj), id, obj);
1432 rb_vm_register_global_object(obj);
1433 return obj;
1434}
1435
1436/***********************************************************************
1437 *
1438 * Document-class: Refinement
1439 *
1440 * Refinement is a class of the +self+ (current context) inside +refine+
1441 * statement. It allows to import methods from other modules, see #import_methods.
1442 */
1443
1444#if 0 /* for RDoc */
1445/*
1446 * Document-method: Refinement#import_methods
1447 *
1448 * call-seq:
1449 * import_methods(module, ...) -> self
1450 *
1451 * Imports methods from modules. Unlike Module#include,
1452 * Refinement#import_methods copies methods and adds them into the refinement,
1453 * so the refinement is activated in the imported methods.
1454 *
1455 * Note that due to method copying, only methods defined in Ruby code can be imported.
1456 *
1457 * module StrUtils
1458 * def indent(level)
1459 * ' ' * level + self
1460 * end
1461 * end
1462 *
1463 * module M
1464 * refine String do
1465 * import_methods StrUtils
1466 * end
1467 * end
1468 *
1469 * using M
1470 * "foo".indent(3)
1471 * #=> " foo"
1472 *
1473 * module M
1474 * refine String do
1475 * import_methods Enumerable
1476 * # Can't import method which is not defined with Ruby code: Enumerable#drop
1477 * end
1478 * end
1479 *
1480 */
1481
1482static VALUE
1483refinement_import_methods(int argc, VALUE *argv, VALUE refinement)
1484{
1485}
1486# endif
1487
1507void
1508Init_class_hierarchy(void)
1509{
1510 rb_cBasicObject = boot_defclass("BasicObject", 0);
1511 rb_cObject = boot_defclass("Object", rb_cBasicObject);
1512 rb_vm_register_global_object(rb_cObject);
1513
1514 /* resolve class name ASAP for order-independence */
1515 rb_set_class_path_string(rb_cObject, rb_cObject, rb_fstring_lit("Object"));
1516
1517 rb_cModule = boot_defclass("Module", rb_cObject);
1518 rb_cClass = boot_defclass("Class", rb_cModule);
1519 rb_cRefinement = boot_defclass("Refinement", rb_cModule);
1520
1521#if 0 /* for RDoc */
1522 // we pretend it to be public, otherwise RDoc will ignore it
1523 rb_define_method(rb_cRefinement, "import_methods", refinement_import_methods, -1);
1524#endif
1525
1526 rb_const_set(rb_cObject, rb_intern_const("BasicObject"), rb_cBasicObject);
1527 RBASIC_SET_CLASS(rb_cClass, rb_cClass);
1528 RBASIC_SET_CLASS(rb_cModule, rb_cClass);
1529 RBASIC_SET_CLASS(rb_cObject, rb_cClass);
1530 RBASIC_SET_CLASS(rb_cRefinement, rb_cClass);
1531 RBASIC_SET_CLASS(rb_cBasicObject, rb_cClass);
1532
1534}
1535
1536
1547VALUE
1548rb_make_metaclass(VALUE obj, VALUE unused)
1549{
1550 if (BUILTIN_TYPE(obj) == T_CLASS) {
1551 return make_metaclass(obj);
1552 }
1553 else {
1554 return make_singleton_class(obj);
1555 }
1556}
1557
1558VALUE
1560{
1561 VALUE klass;
1562
1563 if (!super) super = rb_cObject;
1564 klass = rb_class_new(super);
1565 rb_make_metaclass(klass, METACLASS_OF(super));
1566
1567 return klass;
1568}
1569
1570
1579VALUE
1581{
1582 ID inherited;
1583 if (!super) super = rb_cObject;
1584 CONST_ID(inherited, "inherited");
1585 return rb_funcall(super, inherited, 1, klass);
1586}
1587
1588VALUE
1589rb_define_class(const char *name, VALUE super)
1590{
1591 VALUE klass;
1592 ID id = rb_intern(name);
1593
1594 if (rb_const_defined(rb_cObject, id)) {
1595 klass = rb_const_get(rb_cObject, id);
1596 if (!RB_TYPE_P(klass, T_CLASS)) {
1597 rb_raise(rb_eTypeError, "%s is not a class (%"PRIsVALUE")",
1598 name, rb_obj_class(klass));
1599 }
1600 if (rb_class_real(RCLASS_SUPER(klass)) != super) {
1601 rb_raise(rb_eTypeError, "superclass mismatch for class %s", name);
1602 }
1603
1604 /* Class may have been defined in Ruby and not pin-rooted */
1605 rb_vm_register_global_object(klass);
1606 return klass;
1607 }
1608 if (!super) {
1609 rb_raise(rb_eArgError, "no super class for '%s'", name);
1610 }
1611 klass = rb_define_class_id(id, super);
1612 rb_vm_register_global_object(klass);
1613 rb_const_set(rb_cObject, id, klass);
1614 rb_class_inherited(super, klass);
1615
1616 return klass;
1617}
1618
1619VALUE
1620rb_define_class_under(VALUE outer, const char *name, VALUE super)
1621{
1622 return rb_define_class_id_under(outer, rb_intern(name), super);
1623}
1624
1625VALUE
1626rb_define_class_id_under_no_pin(VALUE outer, ID id, VALUE super)
1627{
1628 VALUE klass;
1629
1630 if (rb_const_defined_at(outer, id)) {
1631 klass = rb_const_get_at(outer, id);
1632 if (!RB_TYPE_P(klass, T_CLASS)) {
1633 rb_raise(rb_eTypeError, "%"PRIsVALUE"::%"PRIsVALUE" is not a class"
1634 " (%"PRIsVALUE")",
1635 outer, rb_id2str(id), rb_obj_class(klass));
1636 }
1637 if (rb_class_real(RCLASS_SUPER(klass)) != super) {
1638 rb_raise(rb_eTypeError, "superclass mismatch for class "
1639 "%"PRIsVALUE"::%"PRIsVALUE""
1640 " (%"PRIsVALUE" is given but was %"PRIsVALUE")",
1641 outer, rb_id2str(id), RCLASS_SUPER(klass), super);
1642 }
1643
1644 return klass;
1645 }
1646 if (!super) {
1647 rb_raise(rb_eArgError, "no super class for '%"PRIsVALUE"::%"PRIsVALUE"'",
1648 rb_class_path(outer), rb_id2str(id));
1649 }
1650 klass = rb_define_class_id(id, super);
1651 rb_set_class_path_string(klass, outer, rb_id2str(id));
1652 rb_const_set(outer, id, klass);
1653 rb_class_inherited(super, klass);
1654
1655 return klass;
1656}
1657
1658VALUE
1660{
1661 VALUE klass = rb_define_class_id_under_no_pin(outer, id, super);
1662 rb_vm_register_global_object(klass);
1663 return klass;
1664}
1665
1666VALUE
1667rb_module_s_alloc(VALUE klass)
1668{
1669 VALUE mod = class_alloc(T_MODULE, klass);
1670 class_initialize_method_table(mod);
1671 return mod;
1672}
1673
1674static inline VALUE
1675module_new(VALUE klass)
1676{
1677 VALUE mdl = class_alloc(T_MODULE, klass);
1678 class_initialize_method_table(mdl);
1679 return (VALUE)mdl;
1680}
1681
1682VALUE
1684{
1685 return module_new(rb_cModule);
1686}
1687
1688VALUE
1690{
1691 return module_new(rb_cRefinement);
1692}
1693
1694// Kept for compatibility. Use rb_module_new() instead.
1695VALUE
1697{
1698 return rb_module_new();
1699}
1700
1701VALUE
1702rb_define_module(const char *name)
1703{
1704 VALUE module;
1705 ID id = rb_intern(name);
1706
1707 if (rb_const_defined(rb_cObject, id)) {
1708 module = rb_const_get(rb_cObject, id);
1709 if (!RB_TYPE_P(module, T_MODULE)) {
1710 rb_raise(rb_eTypeError, "%s is not a module (%"PRIsVALUE")",
1711 name, rb_obj_class(module));
1712 }
1713 /* Module may have been defined in Ruby and not pin-rooted */
1714 rb_vm_register_global_object(module);
1715 return module;
1716 }
1717 module = rb_module_new();
1718 rb_vm_register_global_object(module);
1719 rb_const_set(rb_cObject, id, module);
1720
1721 return module;
1722}
1723
1724VALUE
1725rb_define_module_under(VALUE outer, const char *name)
1726{
1727 return rb_define_module_id_under(outer, rb_intern(name));
1728}
1729
1730VALUE
1732{
1733 VALUE module;
1734
1735 if (rb_const_defined_at(outer, id)) {
1736 module = rb_const_get_at(outer, id);
1737 if (!RB_TYPE_P(module, T_MODULE)) {
1738 rb_raise(rb_eTypeError, "%"PRIsVALUE"::%"PRIsVALUE" is not a module"
1739 " (%"PRIsVALUE")",
1740 outer, rb_id2str(id), rb_obj_class(module));
1741 }
1742 /* Module may have been defined in Ruby and not pin-rooted */
1743 rb_vm_register_global_object(module);
1744 return module;
1745 }
1746 module = rb_module_new();
1747 rb_const_set(outer, id, module);
1748 rb_set_class_path_string(module, outer, rb_id2str(id));
1749 rb_vm_register_global_object(module);
1750
1751 return module;
1752}
1753
1754VALUE
1755rb_include_class_new(VALUE module, VALUE super)
1756{
1757 VALUE klass = class_alloc(T_ICLASS, rb_cClass);
1758
1759 RCLASS_SET_M_TBL(klass, RCLASS_WRITABLE_M_TBL(module));
1760
1761 RCLASS_SET_ORIGIN(klass, klass);
1762 if (BUILTIN_TYPE(module) == T_ICLASS) {
1763 module = METACLASS_OF(module);
1764 }
1765 RUBY_ASSERT(!RB_TYPE_P(module, T_ICLASS));
1766 if (RCLASS_WRITABLE_CONST_TBL(module)) {
1767 RCLASS_SET_CONST_TBL(klass, RCLASS_WRITABLE_CONST_TBL(module), true);
1768 }
1769 else {
1770 RCLASS_WRITE_CONST_TBL(module, rb_id_table_create(0), false);
1771 RCLASS_SET_CONST_TBL(klass, RCLASS_WRITABLE_CONST_TBL(module), true);
1772 }
1773
1774 RCLASS_SET_CVC_TBL(klass, RCLASS_WRITABLE_CVC_TBL(module));
1775
1776 class_associate_super(klass, super, true);
1777 RBASIC_SET_CLASS(klass, module);
1778
1779 return (VALUE)klass;
1780}
1781
1782static int include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super);
1783
1784static void
1785ensure_includable(VALUE klass, VALUE module)
1786{
1787 rb_class_modify_check(klass);
1788 Check_Type(module, T_MODULE);
1789 rb_class_set_initialized(module);
1790 if (!NIL_P(rb_refinement_module_get_refined_class(module))) {
1791 rb_raise(rb_eArgError, "refinement module is not allowed");
1792 }
1793}
1794
1795void
1797{
1798 int changed = 0;
1799
1800 ensure_includable(klass, module);
1801
1802 changed = include_modules_at(klass, RCLASS_ORIGIN(klass), module, TRUE);
1803 if (changed < 0)
1804 rb_raise(rb_eArgError, "cyclic include detected");
1805
1806 if (RB_TYPE_P(klass, T_MODULE)) {
1807 rb_subclass_entry_t *iclass = RCLASS_SUBCLASSES_FIRST(klass);
1808 while (iclass) {
1809 int do_include = 1;
1810 VALUE check_class = iclass->klass;
1811 /* During lazy sweeping, iclass->klass could be a dead object that
1812 * has not yet been swept. */
1813 if (!rb_objspace_garbage_object_p(check_class)) {
1814 while (check_class) {
1815 RUBY_ASSERT(!rb_objspace_garbage_object_p(check_class));
1816
1817 if (RB_TYPE_P(check_class, T_ICLASS) &&
1818 (METACLASS_OF(check_class) == module)) {
1819 do_include = 0;
1820 }
1821 check_class = RCLASS_SUPER(check_class);
1822 }
1823
1824 if (do_include) {
1825 include_modules_at(iclass->klass, RCLASS_ORIGIN(iclass->klass), module, TRUE);
1826 }
1827 }
1828
1829 iclass = iclass->next;
1830 }
1831 }
1832}
1833
1834static enum rb_id_table_iterator_result
1835add_refined_method_entry_i(ID key, VALUE value, void *data)
1836{
1837 rb_add_refined_method_entry((VALUE)data, key);
1838 return ID_TABLE_CONTINUE;
1839}
1840
1841static enum rb_id_table_iterator_result
1842clear_module_cache_i(ID id, VALUE val, void *data)
1843{
1844 VALUE klass = (VALUE)data;
1845 rb_clear_method_cache(klass, id);
1846 return ID_TABLE_CONTINUE;
1847}
1848
1849static bool
1850module_in_super_chain(const VALUE klass, VALUE module)
1851{
1852 struct rb_id_table *const klass_m_tbl = RCLASS_M_TBL(RCLASS_ORIGIN(klass));
1853 if (klass_m_tbl) {
1854 while (module) {
1855 if (klass_m_tbl == RCLASS_M_TBL(module))
1856 return true;
1857 module = RCLASS_SUPER(module);
1858 }
1859 }
1860 return false;
1861}
1862
1863// For each ID key in the class constant table, we're going to clear the VM's
1864// inline constant caches associated with it.
1865static enum rb_id_table_iterator_result
1866clear_constant_cache_i(ID id, VALUE value, void *data)
1867{
1869 return ID_TABLE_CONTINUE;
1870}
1871
1872static int
1873do_include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super, bool check_cyclic)
1874{
1875 VALUE p, iclass, origin_stack = 0;
1876 int method_changed = 0;
1877 long origin_len;
1878 VALUE klass_origin = RCLASS_ORIGIN(klass);
1879 VALUE original_klass = klass;
1880
1881 if (check_cyclic && module_in_super_chain(klass, module))
1882 return -1;
1883
1884 while (module) {
1885 int c_seen = FALSE;
1886 int superclass_seen = FALSE;
1887 struct rb_id_table *tbl;
1888
1889 if (klass == c) {
1890 c_seen = TRUE;
1891 }
1892 if (klass_origin != c || search_super) {
1893 /* ignore if the module included already in superclasses for include,
1894 * ignore if the module included before origin class for prepend
1895 */
1896 for (p = RCLASS_SUPER(klass); p; p = RCLASS_SUPER(p)) {
1897 int type = BUILTIN_TYPE(p);
1898 if (klass_origin == p && !search_super)
1899 break;
1900 if (c == p)
1901 c_seen = TRUE;
1902 if (type == T_ICLASS) {
1903 if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) {
1904 if (!superclass_seen && c_seen) {
1905 c = p; /* move insertion point */
1906 }
1907 goto skip;
1908 }
1909 }
1910 else if (type == T_CLASS) {
1911 superclass_seen = TRUE;
1912 }
1913 }
1914 }
1915
1916 VALUE super_class = RCLASS_SUPER(c);
1917
1918 // invalidate inline method cache
1919 RB_DEBUG_COUNTER_INC(cvar_include_invalidate);
1920 ruby_vm_global_cvar_state++;
1921 tbl = RCLASS_M_TBL(module);
1922 if (tbl && rb_id_table_size(tbl)) {
1923 if (search_super) { // include
1924 if (super_class && !RB_TYPE_P(super_class, T_MODULE)) {
1925 rb_id_table_foreach(tbl, clear_module_cache_i, (void *)super_class);
1926 }
1927 }
1928 else { // prepend
1929 if (!RB_TYPE_P(original_klass, T_MODULE)) {
1930 rb_id_table_foreach(tbl, clear_module_cache_i, (void *)original_klass);
1931 }
1932 }
1933 method_changed = 1;
1934 }
1935
1936 // setup T_ICLASS for the include/prepend module
1937 iclass = rb_include_class_new(module, super_class);
1938 c = rb_class_set_super(c, iclass);
1939 RCLASS_SET_INCLUDER(iclass, klass);
1940 if (module != RCLASS_ORIGIN(module)) {
1941 if (!origin_stack) origin_stack = rb_ary_hidden_new(2);
1942 VALUE origin[2] = {iclass, RCLASS_ORIGIN(module)};
1943 rb_ary_cat(origin_stack, origin, 2);
1944 }
1945 else if (origin_stack && (origin_len = RARRAY_LEN(origin_stack)) > 1 &&
1946 RARRAY_AREF(origin_stack, origin_len - 1) == module) {
1947 RCLASS_WRITE_ORIGIN(RARRAY_AREF(origin_stack, (origin_len -= 2)), iclass);
1948 RICLASS_WRITE_ORIGIN_SHARED_MTBL(iclass);
1949 rb_ary_resize(origin_stack, origin_len);
1950 }
1951
1952 VALUE m = module;
1953 if (BUILTIN_TYPE(m) == T_ICLASS) m = METACLASS_OF(m);
1954 rb_module_add_to_subclasses_list(m, iclass);
1955
1956 if (BUILTIN_TYPE(klass) == T_MODULE && FL_TEST(klass, RMODULE_IS_REFINEMENT)) {
1957 VALUE refined_class =
1958 rb_refinement_module_get_refined_class(klass);
1959
1960 rb_id_table_foreach(RCLASS_M_TBL(module), add_refined_method_entry_i, (void *)refined_class);
1962 }
1963
1964 tbl = RCLASS_CONST_TBL(module);
1965 if (tbl && rb_id_table_size(tbl))
1966 rb_id_table_foreach(tbl, clear_constant_cache_i, NULL);
1967 skip:
1968 module = RCLASS_SUPER(module);
1969 }
1970
1971 return method_changed;
1972}
1973
1974static int
1975include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super)
1976{
1977 return do_include_modules_at(klass, c, module, search_super, true);
1978}
1979
1980static enum rb_id_table_iterator_result
1981move_refined_method(ID key, VALUE value, void *data)
1982{
1983 rb_method_entry_t *me = (rb_method_entry_t *)value;
1984
1985 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1986 VALUE klass = (VALUE)data;
1987 struct rb_id_table *tbl = RCLASS_WRITABLE_M_TBL(klass);
1988
1989 if (me->def->body.refined.orig_me) {
1990 const rb_method_entry_t *orig_me = me->def->body.refined.orig_me, *new_me;
1991 RB_OBJ_WRITE(me, &me->def->body.refined.orig_me, NULL);
1992 new_me = rb_method_entry_clone(me);
1993 rb_method_table_insert(klass, tbl, key, new_me);
1994 rb_method_entry_copy(me, orig_me);
1995 return ID_TABLE_CONTINUE;
1996 }
1997 else {
1998 rb_method_table_insert(klass, tbl, key, me);
1999 return ID_TABLE_DELETE;
2000 }
2001 }
2002 else {
2003 return ID_TABLE_CONTINUE;
2004 }
2005}
2006
2007static enum rb_id_table_iterator_result
2008cache_clear_refined_method(ID key, VALUE value, void *data)
2009{
2010 rb_method_entry_t *me = (rb_method_entry_t *) value;
2011
2012 if (me->def->type == VM_METHOD_TYPE_REFINED && me->def->body.refined.orig_me) {
2013 VALUE klass = (VALUE)data;
2014 rb_clear_method_cache(klass, me->called_id);
2015 }
2016 // Refined method entries without an orig_me is going to stay in the method
2017 // table of klass, like before the move, so no need to clear the cache.
2018
2019 return ID_TABLE_CONTINUE;
2020}
2021
2022static bool
2023ensure_origin(VALUE klass)
2024{
2025 VALUE origin = RCLASS_ORIGIN(klass);
2026 if (origin == klass) {
2027 origin = class_alloc(T_ICLASS, klass);
2028 RCLASS_SET_M_TBL(origin, RCLASS_M_TBL(klass));
2029 rb_class_set_super(origin, RCLASS_SUPER(klass));
2030 rb_class_set_super(klass, origin); // writes origin into RCLASS_SUPER(klass)
2031 RCLASS_WRITE_ORIGIN(klass, origin);
2032
2033 // RCLASS_WRITE_ORIGIN marks origin as an origin, so this is the first
2034 // point that it sees M_TBL and may mark it
2035 rb_gc_writebarrier_remember(origin);
2036
2037 class_clear_method_table(klass);
2038 rb_id_table_foreach(RCLASS_M_TBL(origin), cache_clear_refined_method, (void *)klass);
2039 rb_id_table_foreach(RCLASS_M_TBL(origin), move_refined_method, (void *)klass);
2040 return true;
2041 }
2042 return false;
2043}
2044
2045void
2047{
2048 int changed;
2049 bool klass_had_no_origin;
2050
2051 ensure_includable(klass, module);
2052 if (module_in_super_chain(klass, module))
2053 rb_raise(rb_eArgError, "cyclic prepend detected");
2054
2055 klass_had_no_origin = ensure_origin(klass);
2056 changed = do_include_modules_at(klass, klass, module, FALSE, false);
2057 RUBY_ASSERT(changed >= 0); // already checked for cyclic prepend above
2058 if (changed) {
2059 rb_vm_check_redefinition_by_prepend(klass);
2060 }
2061 if (RB_TYPE_P(klass, T_MODULE)) {
2062 rb_subclass_entry_t *iclass = RCLASS_SUBCLASSES_FIRST(klass);
2063 VALUE klass_origin = RCLASS_ORIGIN(klass);
2064 struct rb_id_table *klass_m_tbl = RCLASS_M_TBL(klass);
2065 struct rb_id_table *klass_origin_m_tbl = RCLASS_M_TBL(klass_origin);
2066 while (iclass) {
2067 /* During lazy sweeping, iclass->klass could be a dead object that
2068 * has not yet been swept. */
2069 if (!rb_objspace_garbage_object_p(iclass->klass)) {
2070 const VALUE subclass = iclass->klass;
2071 if (klass_had_no_origin && klass_origin_m_tbl == RCLASS_M_TBL(subclass)) {
2072 // backfill an origin iclass to handle refinements and future prepends
2073 rb_id_table_foreach(RCLASS_M_TBL(subclass), clear_module_cache_i, (void *)subclass);
2074 RCLASS_WRITE_M_TBL(subclass, klass_m_tbl);
2075 VALUE origin = rb_include_class_new(klass_origin, RCLASS_SUPER(subclass));
2076 rb_class_set_super(subclass, origin);
2077 RCLASS_SET_INCLUDER(origin, RCLASS_INCLUDER(subclass));
2078 RCLASS_WRITE_ORIGIN(subclass, origin);
2079 RICLASS_SET_ORIGIN_SHARED_MTBL(origin);
2080 }
2081 include_modules_at(subclass, subclass, module, FALSE);
2082 }
2083
2084 iclass = iclass->next;
2085 }
2086 }
2087}
2088
2089/*
2090 * call-seq:
2091 * mod.included_modules -> array
2092 *
2093 * Returns the list of modules included or prepended in <i>mod</i>
2094 * or one of <i>mod</i>'s ancestors.
2095 *
2096 * module Sub
2097 * end
2098 *
2099 * module Mixin
2100 * prepend Sub
2101 * end
2102 *
2103 * module Outer
2104 * include Mixin
2105 * end
2106 *
2107 * Mixin.included_modules #=> [Sub]
2108 * Outer.included_modules #=> [Sub, Mixin]
2109 */
2110
2111VALUE
2113{
2114 VALUE ary = rb_ary_new();
2115 VALUE p;
2116 VALUE origin = RCLASS_ORIGIN(mod);
2117
2118 for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
2119 if (p != origin && RCLASS_ORIGIN(p) == p && BUILTIN_TYPE(p) == T_ICLASS) {
2120 VALUE m = METACLASS_OF(p);
2121 if (RB_TYPE_P(m, T_MODULE))
2122 rb_ary_push(ary, m);
2123 }
2124 }
2125 return ary;
2126}
2127
2128/*
2129 * call-seq:
2130 * mod.include?(module) -> true or false
2131 *
2132 * Returns <code>true</code> if <i>module</i> is included
2133 * or prepended in <i>mod</i> or one of <i>mod</i>'s ancestors.
2134 *
2135 * module A
2136 * end
2137 * class B
2138 * include A
2139 * end
2140 * class C < B
2141 * end
2142 * B.include?(A) #=> true
2143 * C.include?(A) #=> true
2144 * A.include?(A) #=> false
2145 */
2146
2147VALUE
2149{
2150 VALUE p;
2151
2152 Check_Type(mod2, T_MODULE);
2153 for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
2154 if (BUILTIN_TYPE(p) == T_ICLASS && !RICLASS_IS_ORIGIN_P(p)) {
2155 if (METACLASS_OF(p) == mod2) return Qtrue;
2156 }
2157 }
2158 return Qfalse;
2159}
2160
2161/*
2162 * call-seq:
2163 * mod.ancestors -> array
2164 *
2165 * Returns a list of modules included/prepended in <i>mod</i>
2166 * (including <i>mod</i> itself).
2167 *
2168 * module Mod
2169 * include Math
2170 * include Comparable
2171 * prepend Enumerable
2172 * end
2173 *
2174 * Mod.ancestors #=> [Enumerable, Mod, Comparable, Math]
2175 * Math.ancestors #=> [Math]
2176 * Enumerable.ancestors #=> [Enumerable]
2177 */
2178
2179VALUE
2181{
2182 VALUE p, ary = rb_ary_new();
2183 VALUE refined_class = Qnil;
2184 if (BUILTIN_TYPE(mod) == T_MODULE && FL_TEST(mod, RMODULE_IS_REFINEMENT)) {
2185 refined_class = rb_refinement_module_get_refined_class(mod);
2186 }
2187
2188 for (p = mod; p; p = RCLASS_SUPER(p)) {
2189 if (p == refined_class) break;
2190 if (p != RCLASS_ORIGIN(p)) continue;
2191 if (BUILTIN_TYPE(p) == T_ICLASS) {
2192 rb_ary_push(ary, METACLASS_OF(p));
2193 }
2194 else {
2195 rb_ary_push(ary, p);
2196 }
2197 }
2198 return ary;
2199}
2200
2202{
2203 VALUE buffer;
2204 long count;
2205 long maxcount;
2206 bool immediate_only;
2207};
2208
2209static void
2210class_descendants_recursive(VALUE klass, VALUE v)
2211{
2212 struct subclass_traverse_data *data = (struct subclass_traverse_data *) v;
2213
2214 if (BUILTIN_TYPE(klass) == T_CLASS && !RCLASS_SINGLETON_P(klass)) {
2215 if (data->buffer && data->count < data->maxcount && !rb_objspace_garbage_object_p(klass)) {
2216 // assumes that this does not cause GC as long as the length does not exceed the capacity
2217 rb_ary_push(data->buffer, klass);
2218 }
2219 data->count++;
2220 if (!data->immediate_only) {
2221 rb_class_foreach_subclass(klass, class_descendants_recursive, v);
2222 }
2223 }
2224 else {
2225 rb_class_foreach_subclass(klass, class_descendants_recursive, v);
2226 }
2227}
2228
2229static VALUE
2230class_descendants(VALUE klass, bool immediate_only)
2231{
2232 struct subclass_traverse_data data = { Qfalse, 0, -1, immediate_only };
2233
2234 // estimate the count of subclasses
2235 rb_class_foreach_subclass(klass, class_descendants_recursive, (VALUE) &data);
2236
2237 // the following allocation may cause GC which may change the number of subclasses
2238 data.buffer = rb_ary_new_capa(data.count);
2239 data.maxcount = data.count;
2240 data.count = 0;
2241
2242 size_t gc_count = rb_gc_count();
2243
2244 // enumerate subclasses
2245 rb_class_foreach_subclass(klass, class_descendants_recursive, (VALUE) &data);
2246
2247 if (gc_count != rb_gc_count()) {
2248 rb_bug("GC must not occur during the subclass iteration of Class#descendants");
2249 }
2250
2251 return data.buffer;
2252}
2253
2254/*
2255 * call-seq:
2256 * subclasses -> array
2257 *
2258 * Returns an array of classes where the receiver is the
2259 * direct superclass of the class, excluding singleton classes.
2260 * The order of the returned array is not defined.
2261 *
2262 * class A; end
2263 * class B < A; end
2264 * class C < B; end
2265 * class D < A; end
2266 *
2267 * A.subclasses #=> [D, B]
2268 * B.subclasses #=> [C]
2269 * C.subclasses #=> []
2270 *
2271 * Anonymous subclasses (not associated with a constant) are
2272 * returned, too:
2273 *
2274 * c = Class.new(A)
2275 * A.subclasses # => [#<Class:0x00007f003c77bd78>, D, B]
2276 *
2277 * Note that the parent does not hold references to subclasses
2278 * and doesn't prevent them from being garbage collected. This
2279 * means that the subclass might disappear when all references
2280 * to it are dropped:
2281 *
2282 * # drop the reference to subclass, it can be garbage-collected now
2283 * c = nil
2284 *
2285 * A.subclasses
2286 * # It can be
2287 * # => [#<Class:0x00007f003c77bd78>, D, B]
2288 * # ...or just
2289 * # => [D, B]
2290 * # ...depending on whether garbage collector was run
2291 */
2292
2293VALUE
2295{
2296 return class_descendants(klass, true);
2297}
2298
2299/*
2300 * call-seq:
2301 * attached_object -> object
2302 *
2303 * Returns the object for which the receiver is the singleton class.
2304 *
2305 * Raises an TypeError if the class is not a singleton class.
2306 *
2307 * class Foo; end
2308 *
2309 * Foo.singleton_class.attached_object #=> Foo
2310 * Foo.attached_object #=> TypeError: `Foo' is not a singleton class
2311 * Foo.new.singleton_class.attached_object #=> #<Foo:0x000000010491a370>
2312 * TrueClass.attached_object #=> TypeError: `TrueClass' is not a singleton class
2313 * NilClass.attached_object #=> TypeError: `NilClass' is not a singleton class
2314 */
2315
2316VALUE
2318{
2319 if (!RCLASS_SINGLETON_P(klass)) {
2320 rb_raise(rb_eTypeError, "'%"PRIsVALUE"' is not a singleton class", klass);
2321 }
2322
2323 return RCLASS_ATTACHED_OBJECT(klass);
2324}
2325
2326static void
2327ins_methods_push(st_data_t name, st_data_t ary)
2328{
2329 rb_ary_push((VALUE)ary, ID2SYM((ID)name));
2330}
2331
2332static int
2333ins_methods_i(st_data_t name, st_data_t type, st_data_t ary)
2334{
2335 switch ((rb_method_visibility_t)type) {
2336 case METHOD_VISI_UNDEF:
2337 case METHOD_VISI_PRIVATE:
2338 break;
2339 default: /* everything but private */
2340 ins_methods_push(name, ary);
2341 break;
2342 }
2343 return ST_CONTINUE;
2344}
2345
2346static int
2347ins_methods_type_i(st_data_t name, st_data_t type, st_data_t ary, rb_method_visibility_t visi)
2348{
2349 if ((rb_method_visibility_t)type == visi) {
2350 ins_methods_push(name, ary);
2351 }
2352 return ST_CONTINUE;
2353}
2354
2355static int
2356ins_methods_prot_i(st_data_t name, st_data_t type, st_data_t ary)
2357{
2358 return ins_methods_type_i(name, type, ary, METHOD_VISI_PROTECTED);
2359}
2360
2361static int
2362ins_methods_priv_i(st_data_t name, st_data_t type, st_data_t ary)
2363{
2364 return ins_methods_type_i(name, type, ary, METHOD_VISI_PRIVATE);
2365}
2366
2367static int
2368ins_methods_pub_i(st_data_t name, st_data_t type, st_data_t ary)
2369{
2370 return ins_methods_type_i(name, type, ary, METHOD_VISI_PUBLIC);
2371}
2372
2373static int
2374ins_methods_undef_i(st_data_t name, st_data_t type, st_data_t ary)
2375{
2376 return ins_methods_type_i(name, type, ary, METHOD_VISI_UNDEF);
2377}
2378
2380 st_table *list;
2381 int recur;
2382};
2383
2384static enum rb_id_table_iterator_result
2385method_entry_i(ID key, VALUE value, void *data)
2386{
2387 const rb_method_entry_t *me = (const rb_method_entry_t *)value;
2388 struct method_entry_arg *arg = (struct method_entry_arg *)data;
2389 rb_method_visibility_t type;
2390
2391 if (me->def->type == VM_METHOD_TYPE_REFINED) {
2392 VALUE owner = me->owner;
2393 me = rb_resolve_refined_method(Qnil, me);
2394 if (!me) return ID_TABLE_CONTINUE;
2395 if (!arg->recur && me->owner != owner) return ID_TABLE_CONTINUE;
2396 }
2397 if (!st_is_member(arg->list, key)) {
2398 if (UNDEFINED_METHOD_ENTRY_P(me)) {
2399 type = METHOD_VISI_UNDEF; /* none */
2400 }
2401 else {
2402 type = METHOD_ENTRY_VISI(me);
2403 RUBY_ASSERT(type != METHOD_VISI_UNDEF);
2404 }
2405 st_add_direct(arg->list, key, (st_data_t)type);
2406 }
2407 return ID_TABLE_CONTINUE;
2408}
2409
2410static void
2411add_instance_method_list(VALUE mod, struct method_entry_arg *me_arg)
2412{
2413 struct rb_id_table *m_tbl = RCLASS_M_TBL(mod);
2414 if (!m_tbl) return;
2415 rb_id_table_foreach(m_tbl, method_entry_i, me_arg);
2416}
2417
2418static bool
2419particular_class_p(VALUE mod)
2420{
2421 if (!mod) return false;
2422 if (RCLASS_SINGLETON_P(mod)) return true;
2423 if (BUILTIN_TYPE(mod) == T_ICLASS) return true;
2424 return false;
2425}
2426
2427static VALUE
2428class_instance_method_list(int argc, const VALUE *argv, VALUE mod, int obj, int (*func) (st_data_t, st_data_t, st_data_t))
2429{
2430 VALUE ary;
2431 int recur = TRUE, prepended = 0;
2432 struct method_entry_arg me_arg;
2433
2434 if (rb_check_arity(argc, 0, 1)) recur = RTEST(argv[0]);
2435
2436 me_arg.list = st_init_numtable();
2437 me_arg.recur = recur;
2438
2439 if (obj) {
2440 for (; particular_class_p(mod); mod = RCLASS_SUPER(mod)) {
2441 add_instance_method_list(mod, &me_arg);
2442 }
2443 }
2444
2445 if (!recur && RCLASS_ORIGIN(mod) != mod) {
2446 mod = RCLASS_ORIGIN(mod);
2447 prepended = 1;
2448 }
2449
2450 for (; mod; mod = RCLASS_SUPER(mod)) {
2451 add_instance_method_list(mod, &me_arg);
2452 if (BUILTIN_TYPE(mod) == T_ICLASS && !prepended) continue;
2453 if (!recur) break;
2454 }
2455 ary = rb_ary_new2(me_arg.list->num_entries);
2456 st_foreach(me_arg.list, func, ary);
2457 st_free_table(me_arg.list);
2458
2459 return ary;
2460}
2461
2462/*
2463 * call-seq:
2464 * mod.instance_methods(include_super=true) -> array
2465 *
2466 * Returns an array containing the names of the public and protected instance
2467 * methods in the receiver. For a module, these are the public and protected methods;
2468 * for a class, they are the instance (not singleton) methods. If the optional
2469 * parameter is <code>false</code>, the methods of any ancestors are not included.
2470 *
2471 * module A
2472 * def method1() end
2473 * end
2474 * class B
2475 * include A
2476 * def method2() end
2477 * end
2478 * class C < B
2479 * def method3() end
2480 * end
2481 *
2482 * A.instance_methods(false) #=> [:method1]
2483 * B.instance_methods(false) #=> [:method2]
2484 * B.instance_methods(true).include?(:method1) #=> true
2485 * C.instance_methods(false) #=> [:method3]
2486 * C.instance_methods.include?(:method2) #=> true
2487 *
2488 * Note that method visibility changes in the current class, as well as aliases,
2489 * are considered as methods of the current class by this method:
2490 *
2491 * class C < B
2492 * alias method4 method2
2493 * protected :method2
2494 * end
2495 * C.instance_methods(false).sort #=> [:method2, :method3, :method4]
2496 */
2497
2498VALUE
2499rb_class_instance_methods(int argc, const VALUE *argv, VALUE mod)
2500{
2501 return class_instance_method_list(argc, argv, mod, 0, ins_methods_i);
2502}
2503
2504/*
2505 * call-seq:
2506 * mod.protected_instance_methods(include_super=true) -> array
2507 *
2508 * Returns a list of the protected instance methods defined in
2509 * <i>mod</i>. If the optional parameter is <code>false</code>, the
2510 * methods of any ancestors are not included.
2511 */
2512
2513VALUE
2515{
2516 return class_instance_method_list(argc, argv, mod, 0, ins_methods_prot_i);
2517}
2518
2519/*
2520 * call-seq:
2521 * mod.private_instance_methods(include_super=true) -> array
2522 *
2523 * Returns a list of the private instance methods defined in
2524 * <i>mod</i>. If the optional parameter is <code>false</code>, the
2525 * methods of any ancestors are not included.
2526 *
2527 * module Mod
2528 * def method1() end
2529 * private :method1
2530 * def method2() end
2531 * end
2532 * Mod.instance_methods #=> [:method2]
2533 * Mod.private_instance_methods #=> [:method1]
2534 */
2535
2536VALUE
2538{
2539 return class_instance_method_list(argc, argv, mod, 0, ins_methods_priv_i);
2540}
2541
2542/*
2543 * call-seq:
2544 * mod.public_instance_methods(include_super=true) -> array
2545 *
2546 * Returns a list of the public instance methods defined in <i>mod</i>.
2547 * If the optional parameter is <code>false</code>, the methods of
2548 * any ancestors are not included.
2549 */
2550
2551VALUE
2553{
2554 return class_instance_method_list(argc, argv, mod, 0, ins_methods_pub_i);
2555}
2556
2557/*
2558 * call-seq:
2559 * mod.undefined_instance_methods -> array
2560 *
2561 * Returns a list of the undefined instance methods defined in <i>mod</i>.
2562 * The undefined methods of any ancestors are not included.
2563 */
2564
2565VALUE
2566rb_class_undefined_instance_methods(VALUE mod)
2567{
2568 VALUE include_super = Qfalse;
2569 return class_instance_method_list(1, &include_super, mod, 0, ins_methods_undef_i);
2570}
2571
2572/*
2573 * call-seq:
2574 * obj.methods(regular=true) -> array
2575 *
2576 * Returns a list of the names of public and protected methods of
2577 * <i>obj</i>. This will include all the methods accessible in
2578 * <i>obj</i>'s ancestors.
2579 * If the optional parameter is <code>false</code>, it
2580 * returns an array of <i>obj</i>'s public and protected singleton methods,
2581 * the array will not include methods in modules included in <i>obj</i>.
2582 *
2583 * class Klass
2584 * def klass_method()
2585 * end
2586 * end
2587 * k = Klass.new
2588 * k.methods[0..9] #=> [:klass_method, :nil?, :===,
2589 * # :==~, :!, :eql?
2590 * # :hash, :<=>, :class, :singleton_class]
2591 * k.methods.length #=> 56
2592 *
2593 * k.methods(false) #=> []
2594 * def k.singleton_method; end
2595 * k.methods(false) #=> [:singleton_method]
2596 *
2597 * module M123; def m123; end end
2598 * k.extend M123
2599 * k.methods(false) #=> [:singleton_method]
2600 */
2601
2602VALUE
2603rb_obj_methods(int argc, const VALUE *argv, VALUE obj)
2604{
2605 rb_check_arity(argc, 0, 1);
2606 if (argc > 0 && !RTEST(argv[0])) {
2607 return rb_obj_singleton_methods(argc, argv, obj);
2608 }
2609 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_i);
2610}
2611
2612/*
2613 * call-seq:
2614 * obj.protected_methods(all=true) -> array
2615 *
2616 * Returns the list of protected methods accessible to <i>obj</i>. If
2617 * the <i>all</i> parameter is set to <code>false</code>, only those methods
2618 * in the receiver will be listed.
2619 */
2620
2621VALUE
2622rb_obj_protected_methods(int argc, const VALUE *argv, VALUE obj)
2623{
2624 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_prot_i);
2625}
2626
2627/*
2628 * call-seq:
2629 * obj.private_methods(all=true) -> array
2630 *
2631 * Returns the list of private methods accessible to <i>obj</i>. If
2632 * the <i>all</i> parameter is set to <code>false</code>, only those methods
2633 * in the receiver will be listed.
2634 */
2635
2636VALUE
2637rb_obj_private_methods(int argc, const VALUE *argv, VALUE obj)
2638{
2639 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_priv_i);
2640}
2641
2642/*
2643 * call-seq:
2644 * obj.public_methods(all=true) -> array
2645 *
2646 * Returns the list of public methods accessible to <i>obj</i>. If
2647 * the <i>all</i> parameter is set to <code>false</code>, only those methods
2648 * in the receiver will be listed.
2649 */
2650
2651VALUE
2652rb_obj_public_methods(int argc, const VALUE *argv, VALUE obj)
2653{
2654 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_pub_i);
2655}
2656
2657/*
2658 * call-seq:
2659 * obj.singleton_methods(all=true) -> array
2660 *
2661 * Returns an array of the names of singleton methods for <i>obj</i>.
2662 * If the optional <i>all</i> parameter is true, the list will include
2663 * methods in modules included in <i>obj</i>.
2664 * Only public and protected singleton methods are returned.
2665 *
2666 * module Other
2667 * def three() end
2668 * end
2669 *
2670 * class Single
2671 * def Single.four() end
2672 * end
2673 *
2674 * a = Single.new
2675 *
2676 * def a.one()
2677 * end
2678 *
2679 * class << a
2680 * include Other
2681 * def two()
2682 * end
2683 * end
2684 *
2685 * Single.singleton_methods #=> [:four]
2686 * a.singleton_methods(false) #=> [:two, :one]
2687 * a.singleton_methods #=> [:two, :one, :three]
2688 */
2689
2690VALUE
2691rb_obj_singleton_methods(int argc, const VALUE *argv, VALUE obj)
2692{
2693 VALUE ary, klass, origin;
2694 struct method_entry_arg me_arg;
2695 struct rb_id_table *mtbl;
2696 int recur = TRUE;
2697
2698 if (rb_check_arity(argc, 0, 1)) recur = RTEST(argv[0]);
2699 if (RCLASS_SINGLETON_P(obj)) {
2700 rb_singleton_class(obj);
2701 }
2702 klass = CLASS_OF(obj);
2703 origin = RCLASS_ORIGIN(klass);
2704 me_arg.list = st_init_numtable();
2705 me_arg.recur = recur;
2706 if (klass && RCLASS_SINGLETON_P(klass)) {
2707 if ((mtbl = RCLASS_M_TBL(origin)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2708 klass = RCLASS_SUPER(klass);
2709 }
2710 if (recur) {
2711 while (klass && (RCLASS_SINGLETON_P(klass) || RB_TYPE_P(klass, T_ICLASS))) {
2712 if (klass != origin && (mtbl = RCLASS_M_TBL(klass)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2713 klass = RCLASS_SUPER(klass);
2714 }
2715 }
2716 ary = rb_ary_new2(me_arg.list->num_entries);
2717 st_foreach(me_arg.list, ins_methods_i, ary);
2718 st_free_table(me_arg.list);
2719
2720 return ary;
2721}
2722
2731#ifdef rb_define_method_id
2732#undef rb_define_method_id
2733#endif
2734void
2735rb_define_method_id(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc)
2736{
2737 rb_add_method_cfunc(klass, mid, func, argc, METHOD_VISI_PUBLIC);
2738}
2739
2740#ifdef rb_define_method
2741#undef rb_define_method
2742#endif
2743void
2744rb_define_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
2745{
2746 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PUBLIC);
2747}
2748
2749#ifdef rb_define_protected_method
2750#undef rb_define_protected_method
2751#endif
2752void
2753rb_define_protected_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
2754{
2755 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PROTECTED);
2756}
2757
2758#ifdef rb_define_private_method
2759#undef rb_define_private_method
2760#endif
2761void
2762rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
2763{
2764 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PRIVATE);
2765}
2766
2767void
2768rb_undef_method(VALUE klass, const char *name)
2769{
2770 rb_add_method(klass, rb_intern(name), VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2771}
2772
2773static enum rb_id_table_iterator_result
2774undef_method_i(ID name, VALUE value, void *data)
2775{
2776 VALUE klass = (VALUE)data;
2777 rb_add_method(klass, name, VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2778 return ID_TABLE_CONTINUE;
2779}
2780
2781void
2782rb_undef_methods_from(VALUE klass, VALUE super)
2783{
2784 struct rb_id_table *mtbl = RCLASS_M_TBL(super);
2785 if (mtbl) {
2786 rb_id_table_foreach(mtbl, undef_method_i, (void *)klass);
2787 }
2788}
2789
2798static inline VALUE
2799special_singleton_class_of(VALUE obj)
2800{
2801 switch (obj) {
2802 case Qnil: return rb_cNilClass;
2803 case Qfalse: return rb_cFalseClass;
2804 case Qtrue: return rb_cTrueClass;
2805 default: return Qnil;
2806 }
2807}
2808
2809VALUE
2810rb_special_singleton_class(VALUE obj)
2811{
2812 return special_singleton_class_of(obj);
2813}
2814
2824static VALUE
2825singleton_class_of(VALUE obj, bool ensure_eigenclass)
2826{
2827 VALUE klass;
2828
2829 switch (TYPE(obj)) {
2830 case T_FIXNUM:
2831 case T_BIGNUM:
2832 case T_FLOAT:
2833 case T_SYMBOL:
2834 rb_raise(rb_eTypeError, "can't define singleton");
2835
2836 case T_FALSE:
2837 case T_TRUE:
2838 case T_NIL:
2839 klass = special_singleton_class_of(obj);
2840 if (NIL_P(klass))
2841 rb_bug("unknown immediate %p", (void *)obj);
2842 return klass;
2843
2844 case T_STRING:
2845 if (CHILLED_STRING_P(obj)) {
2846 CHILLED_STRING_MUTATED(obj);
2847 }
2848 else if (FL_TEST_RAW(obj, RSTRING_FSTR)) {
2849 rb_raise(rb_eTypeError, "can't define singleton");
2850 }
2851 }
2852
2853 bool needs_lock = rb_multi_ractor_p() && rb_ractor_shareable_p(obj);
2854 unsigned int lev;
2855 if (needs_lock) {
2856 RB_VM_LOCK_ENTER_LEV(&lev);
2857 }
2858 {
2859 klass = METACLASS_OF(obj);
2860 if (!(RCLASS_SINGLETON_P(klass) &&
2861 RCLASS_ATTACHED_OBJECT(klass) == obj)) {
2862 klass = rb_make_metaclass(obj, klass);
2863 }
2864 RB_FL_SET_RAW(klass, RB_OBJ_FROZEN_RAW(obj));
2865 if (ensure_eigenclass && RB_TYPE_P(obj, T_CLASS)) {
2866 /* ensures an exposed class belongs to its own eigenclass */
2867 (void)ENSURE_EIGENCLASS(klass);
2868 }
2869 }
2870 if (needs_lock) {
2871 RB_VM_LOCK_LEAVE_LEV(&lev);
2872 }
2873
2874 return klass;
2875}
2876
2877void
2879{
2880 /* should not propagate to meta-meta-class, and so on */
2881 if (!RCLASS_SINGLETON_P(x)) {
2882 VALUE klass = RBASIC_CLASS(x);
2883 if (klass && // no class when hidden from ObjectSpace
2884 FL_TEST_RAW(klass, FL_SINGLETON) &&
2885 !OBJ_FROZEN_RAW(klass)) {
2886 OBJ_FREEZE(klass);
2887 }
2888 }
2889}
2890
2898VALUE
2900{
2901 VALUE klass;
2902
2903 if (SPECIAL_CONST_P(obj)) {
2904 return rb_special_singleton_class(obj);
2905 }
2906 klass = METACLASS_OF(obj);
2907 if (!RCLASS_SINGLETON_P(klass)) return Qnil;
2908 if (RCLASS_ATTACHED_OBJECT(klass) != obj) return Qnil;
2909 return klass;
2910}
2911
2912VALUE
2914{
2915 return singleton_class_of(obj, true);
2916}
2917
2927#ifdef rb_define_singleton_method
2928#undef rb_define_singleton_method
2929#endif
2930void
2931rb_define_singleton_method(VALUE obj, const char *name, VALUE (*func)(ANYARGS), int argc)
2932{
2933 rb_define_method(singleton_class_of(obj, false), name, func, argc);
2934}
2935
2936#ifdef rb_define_module_function
2937#undef rb_define_module_function
2938#endif
2939void
2940rb_define_module_function(VALUE module, const char *name, VALUE (*func)(ANYARGS), int argc)
2941{
2942 rb_define_private_method(module, name, func, argc);
2943 rb_define_singleton_method(module, name, func, argc);
2944}
2945
2946#ifdef rb_define_global_function
2947#undef rb_define_global_function
2948#endif
2949void
2950rb_define_global_function(const char *name, VALUE (*func)(ANYARGS), int argc)
2951{
2952 rb_define_module_function(rb_mKernel, name, func, argc);
2953}
2954
2955void
2956rb_define_alias(VALUE klass, const char *name1, const char *name2)
2957{
2958 rb_alias(klass, rb_intern(name1), rb_intern(name2));
2959}
2960
2961void
2962rb_define_attr(VALUE klass, const char *name, int read, int write)
2963{
2964 rb_attr(klass, rb_intern(name), read, write, FALSE);
2965}
2966
2967VALUE
2968rb_keyword_error_new(const char *error, VALUE keys)
2969{
2970 long i = 0, len = RARRAY_LEN(keys);
2971 VALUE error_message = rb_sprintf("%s keyword%.*s", error, len > 1, "s");
2972
2973 if (len > 0) {
2974 rb_str_cat_cstr(error_message, ": ");
2975 while (1) {
2976 const VALUE k = RARRAY_AREF(keys, i);
2977 rb_str_append(error_message, rb_inspect(k));
2978 if (++i >= len) break;
2979 rb_str_cat_cstr(error_message, ", ");
2980 }
2981 }
2982
2983 return rb_exc_new_str(rb_eArgError, error_message);
2984}
2985
2986NORETURN(static void rb_keyword_error(const char *error, VALUE keys));
2987static void
2988rb_keyword_error(const char *error, VALUE keys)
2989{
2990 rb_exc_raise(rb_keyword_error_new(error, keys));
2991}
2992
2993NORETURN(static void unknown_keyword_error(VALUE hash, const ID *table, int keywords));
2994static void
2995unknown_keyword_error(VALUE hash, const ID *table, int keywords)
2996{
2997 int i;
2998 for (i = 0; i < keywords; i++) {
2999 st_data_t key = ID2SYM(table[i]);
3000 rb_hash_stlike_delete(hash, &key, NULL);
3001 }
3002 rb_keyword_error("unknown", rb_hash_keys(hash));
3003}
3004
3005
3006static int
3007separate_symbol(st_data_t key, st_data_t value, st_data_t arg)
3008{
3009 VALUE *kwdhash = (VALUE *)arg;
3010 if (!SYMBOL_P(key)) kwdhash++;
3011 if (!*kwdhash) *kwdhash = rb_hash_new();
3012 rb_hash_aset(*kwdhash, (VALUE)key, (VALUE)value);
3013 return ST_CONTINUE;
3014}
3015
3016VALUE
3018{
3019 VALUE parthash[2] = {0, 0};
3020 VALUE hash = *orighash;
3021
3022 if (RHASH_EMPTY_P(hash)) {
3023 *orighash = 0;
3024 return hash;
3025 }
3026 rb_hash_foreach(hash, separate_symbol, (st_data_t)&parthash);
3027 *orighash = parthash[1];
3028 if (parthash[1] && RBASIC_CLASS(hash) != rb_cHash) {
3029 RBASIC_SET_CLASS(parthash[1], RBASIC_CLASS(hash));
3030 }
3031 return parthash[0];
3032}
3033
3034int
3035rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
3036{
3037 int i = 0, j;
3038 int rest = 0;
3039 VALUE missing = Qnil;
3040 st_data_t key;
3041
3042#define extract_kwarg(keyword, val) \
3043 (key = (st_data_t)(keyword), values ? \
3044 (rb_hash_stlike_delete(keyword_hash, &key, &(val)) || ((val) = Qundef, 0)) : \
3045 rb_hash_stlike_lookup(keyword_hash, key, NULL))
3046
3047 if (NIL_P(keyword_hash)) keyword_hash = 0;
3048
3049 if (optional < 0) {
3050 rest = 1;
3051 optional = -1-optional;
3052 }
3053 if (required) {
3054 for (; i < required; i++) {
3055 VALUE keyword = ID2SYM(table[i]);
3056 if (keyword_hash) {
3057 if (extract_kwarg(keyword, values[i])) {
3058 continue;
3059 }
3060 }
3061 if (NIL_P(missing)) missing = rb_ary_hidden_new(1);
3062 rb_ary_push(missing, keyword);
3063 }
3064 if (!NIL_P(missing)) {
3065 rb_keyword_error("missing", missing);
3066 }
3067 }
3068 j = i;
3069 if (optional && keyword_hash) {
3070 for (i = 0; i < optional; i++) {
3071 if (extract_kwarg(ID2SYM(table[required+i]), values[required+i])) {
3072 j++;
3073 }
3074 }
3075 }
3076 if (!rest && keyword_hash) {
3077 if (RHASH_SIZE(keyword_hash) > (unsigned int)(values ? 0 : j)) {
3078 unknown_keyword_error(keyword_hash, table, required+optional);
3079 }
3080 }
3081 if (values && !keyword_hash) {
3082 for (i = 0; i < required + optional; i++) {
3083 values[i] = Qundef;
3084 }
3085 }
3086 return j;
3087#undef extract_kwarg
3088}
3089
3091 int kw_flag;
3092 int n_lead;
3093 int n_opt;
3094 int n_trail;
3095 bool f_var;
3096 bool f_hash;
3097 bool f_block;
3098};
3099
3100static void
3101rb_scan_args_parse(int kw_flag, const char *fmt, struct rb_scan_args_t *arg)
3102{
3103 const char *p = fmt;
3104
3105 memset(arg, 0, sizeof(*arg));
3106 arg->kw_flag = kw_flag;
3107
3108 if (ISDIGIT(*p)) {
3109 arg->n_lead = *p - '0';
3110 p++;
3111 if (ISDIGIT(*p)) {
3112 arg->n_opt = *p - '0';
3113 p++;
3114 }
3115 }
3116 if (*p == '*') {
3117 arg->f_var = 1;
3118 p++;
3119 }
3120 if (ISDIGIT(*p)) {
3121 arg->n_trail = *p - '0';
3122 p++;
3123 }
3124 if (*p == ':') {
3125 arg->f_hash = 1;
3126 p++;
3127 }
3128 if (*p == '&') {
3129 arg->f_block = 1;
3130 p++;
3131 }
3132 if (*p != '\0') {
3133 rb_fatal("bad scan arg format: %s", fmt);
3134 }
3135}
3136
3137static int
3138rb_scan_args_assign(const struct rb_scan_args_t *arg, int argc, const VALUE *const argv, va_list vargs)
3139{
3140 int i, argi = 0;
3141 VALUE *var, hash = Qnil;
3142#define rb_scan_args_next_param() va_arg(vargs, VALUE *)
3143 const int kw_flag = arg->kw_flag;
3144 const int n_lead = arg->n_lead;
3145 const int n_opt = arg->n_opt;
3146 const int n_trail = arg->n_trail;
3147 const int n_mand = n_lead + n_trail;
3148 const bool f_var = arg->f_var;
3149 const bool f_hash = arg->f_hash;
3150 const bool f_block = arg->f_block;
3151
3152 /* capture an option hash - phase 1: pop from the argv */
3153 if (f_hash && argc > 0) {
3154 VALUE last = argv[argc - 1];
3155 if (rb_scan_args_keyword_p(kw_flag, last)) {
3156 hash = rb_hash_dup(last);
3157 argc--;
3158 }
3159 }
3160
3161 if (argc < n_mand) {
3162 goto argc_error;
3163 }
3164
3165 /* capture leading mandatory arguments */
3166 for (i = 0; i < n_lead; i++) {
3167 var = rb_scan_args_next_param();
3168 if (var) *var = argv[argi];
3169 argi++;
3170 }
3171 /* capture optional arguments */
3172 for (i = 0; i < n_opt; i++) {
3173 var = rb_scan_args_next_param();
3174 if (argi < argc - n_trail) {
3175 if (var) *var = argv[argi];
3176 argi++;
3177 }
3178 else {
3179 if (var) *var = Qnil;
3180 }
3181 }
3182 /* capture variable length arguments */
3183 if (f_var) {
3184 int n_var = argc - argi - n_trail;
3185
3186 var = rb_scan_args_next_param();
3187 if (0 < n_var) {
3188 if (var) *var = rb_ary_new_from_values(n_var, &argv[argi]);
3189 argi += n_var;
3190 }
3191 else {
3192 if (var) *var = rb_ary_new();
3193 }
3194 }
3195 /* capture trailing mandatory arguments */
3196 for (i = 0; i < n_trail; i++) {
3197 var = rb_scan_args_next_param();
3198 if (var) *var = argv[argi];
3199 argi++;
3200 }
3201 /* capture an option hash - phase 2: assignment */
3202 if (f_hash) {
3203 var = rb_scan_args_next_param();
3204 if (var) *var = hash;
3205 }
3206 /* capture iterator block */
3207 if (f_block) {
3208 var = rb_scan_args_next_param();
3209 if (rb_block_given_p()) {
3210 *var = rb_block_proc();
3211 }
3212 else {
3213 *var = Qnil;
3214 }
3215 }
3216
3217 if (argi == argc) {
3218 return argc;
3219 }
3220
3221 argc_error:
3222 return -(argc + 1);
3223#undef rb_scan_args_next_param
3224}
3225
3226static int
3227rb_scan_args_result(const struct rb_scan_args_t *const arg, int argc)
3228{
3229 const int n_lead = arg->n_lead;
3230 const int n_opt = arg->n_opt;
3231 const int n_trail = arg->n_trail;
3232 const int n_mand = n_lead + n_trail;
3233 const bool f_var = arg->f_var;
3234
3235 if (argc >= 0) {
3236 return argc;
3237 }
3238
3239 argc = -argc - 1;
3240 rb_error_arity(argc, n_mand, f_var ? UNLIMITED_ARGUMENTS : n_mand + n_opt);
3242}
3243
3244#undef rb_scan_args
3245int
3246rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...)
3247{
3248 va_list vargs;
3249 struct rb_scan_args_t arg;
3250 rb_scan_args_parse(RB_SCAN_ARGS_PASS_CALLED_KEYWORDS, fmt, &arg);
3251 va_start(vargs,fmt);
3252 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
3253 va_end(vargs);
3254 return rb_scan_args_result(&arg, argc);
3255}
3256
3257#undef rb_scan_args_kw
3258int
3259rb_scan_args_kw(int kw_flag, int argc, const VALUE *argv, const char *fmt, ...)
3260{
3261 va_list vargs;
3262 struct rb_scan_args_t arg;
3263 rb_scan_args_parse(kw_flag, fmt, &arg);
3264 va_start(vargs,fmt);
3265 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
3266 va_end(vargs);
3267 return rb_scan_args_result(&arg, argc);
3268}
3269
#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:877
static void RB_FL_SET_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_SET().
Definition fl_type.h:600
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:2514
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:755
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
Definition class.c:1796
VALUE rb_refinement_new(void)
Creates a new, anonymous refinement.
Definition class.c:1689
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition class.c:1589
VALUE rb_class_new(VALUE super)
Creates a new, anonymous class.
Definition class.c:969
static VALUE make_singleton_class(VALUE obj)
Creates a singleton class for obj.
Definition class.c:1409
VALUE rb_singleton_class_clone(VALUE obj)
Clones a singleton class.
Definition class.c:1247
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:2046
VALUE rb_class_subclasses(VALUE klass)
Queries the class's direct descendants.
Definition class.c:2294
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
Definition class.c:2913
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition class.c:1620
VALUE rb_class_attached_object(VALUE klass)
Returns the attached object for a singleton class.
Definition class.c:2317
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:2691
VALUE rb_module_new(void)
Creates a new, anonymous module.
Definition class.c:1683
#define META_CLASS_OF_CLASS_CLASS_P(k)
whether k is a meta^(n)-class of Class class
Definition class.c:1328
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:2499
void rb_check_inheritable(VALUE super)
Asserts that the given class can derive a child class.
Definition class.c:954
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:2552
VALUE rb_class_boot(VALUE super)
A utility function that wraps class_alloc.
Definition class.c:887
VALUE rb_define_module(const char *name)
Defines a top-level module.
Definition class.c:1702
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:1731
void rb_singleton_class_attached(VALUE klass, VALUE obj)
Attaches a singleton class to its corresponding object.
Definition class.c:1316
void rb_freeze_singleton_class(VALUE x)
This is an implementation detail of RB_OBJ_FREEZE().
Definition class.c:2878
VALUE rb_mod_included_modules(VALUE mod)
Queries the list of included modules.
Definition class.c:2112
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:1659
VALUE rb_mod_ancestors(VALUE mod)
Queries the module's ancestors.
Definition class.c:2180
static VALUE make_metaclass(VALUE klass)
Creates a metaclass of klass
Definition class.c:1373
VALUE rb_class_inherited(VALUE super, VALUE klass)
Calls Class::inherited.
Definition class.c:1580
VALUE rb_mod_include_p(VALUE mod, VALUE mod2)
Queries if the passed module is included by the module.
Definition class.c:2148
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:2537
#define ENSURE_EIGENCLASS(klass)
ensures klass belongs to its own eigenclass.
Definition class.c:1359
VALUE rb_mod_init_copy(VALUE clone, VALUE orig)
The comment that comes with this function says :nodoc:.
Definition class.c:1130
VALUE rb_define_module_under(VALUE outer, const char *name)
Defines a module under the namespace of outer.
Definition class.c:1725
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:2899
VALUE rb_define_module_id(ID id)
This is a very badly designed API that creates an anonymous module.
Definition class.c:1696
VALUE rb_define_class_id(ID id, VALUE super)
This is a very badly designed API that creates an anonymous class.
Definition class.c:1559
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition class.c:2956
VALUE rb_extract_keywords(VALUE *orighash)
Splits a hash into two.
Definition class.c:3017
void rb_define_attr(VALUE klass, const char *name, int read, int write)
Defines public accessor method(s) for an attribute.
Definition class.c:2962
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
Definition class.c:2768
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:3259
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:3246
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:3035
#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:134
#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:63
#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:131
#define FL_SET
Old name of RB_FL_SET.
Definition fl_type.h:128
#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:130
#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:129
#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:137
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:1431
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:1482
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:983
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:3797
#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:2697
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:2277
void rb_clear_constant_cache_for_id(ID id)
Clears the inline constant caches associated with a particular ID.
Definition vm_method.c:332
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:2379
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