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