Ruby: class.c Source File

Ruby 4.0.5p0 (2026-05-20 revision 64336ffd0ee9e1f4c05891695a3d7b49cb709721)

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