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