Ruby: class.c Source File

Ruby 4.0.6p0 (2026-07-14 revision 03b6d3f8898a28604fe6cb00eae3226b821168f4)

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