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