Ruby 4.0.7p0 (2026-09-15 revision 229531a6cfbf07e3caef30dbac24a2a3f3fed482)
class.c
1/**********************************************************************
2
3 class.c -
4
5 $Author$
6 created at: Tue Aug 10 15:05:44 JST 1993
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9
10**********************************************************************/
11
16
17#include "ruby/internal/config.h"
18#include <ctype.h>
19
20#include "constant.h"
21#include "debug_counter.h"
22#include "id_table.h"
23#include "internal.h"
24#include "internal/box.h"
25#include "internal/class.h"
26#include "internal/eval.h"
27#include "internal/hash.h"
28#include "internal/object.h"
29#include "internal/string.h"
30#include "internal/variable.h"
31#include "ruby/st.h"
32#include "vm_core.h"
33#include "ruby/ractor.h"
34#include "yjit.h"
35#include "zjit.h"
36
37/* Flags of T_CLASS
38 *
39 * 0: RCLASS_IS_ROOT
40 * The class has been added to the VM roots. Will always be marked and pinned.
41 * This is done for classes defined from C to allow storing them in global variables.
42 * 1: RUBY_FL_SINGLETON
43 * This class is a singleton class.
44 * 2: RCLASS_PRIME_CLASSEXT_PRIME_WRITABLE
45 * This class's prime classext is the only classext and writable from any boxes.
46 * If unset, the prime classext is writable only from the root box.
47 * 3: RCLASS_IS_INITIALIZED
48 * Class has been initialized.
49 * 4: RCLASS_BOXABLE
50 * Is a builtin class that may be boxed. It larger than a normal class.
51 */
52
53/* Flags of T_ICLASS
54 *
55 * 2: RCLASS_PRIME_CLASSEXT_PRIME_WRITABLE
56 * This module's prime classext is the only classext and writable from any boxes.
57 * If unset, the prime classext is writable only from the root box.
58 * 4: RCLASS_BOXABLE
59 * Is a builtin class that may be boxed. It larger than a normal class.
60 */
61
62/* Flags of T_MODULE
63 *
64 * 0: RCLASS_IS_ROOT
65 * The class has been added to the VM roots. Will always be marked and pinned.
66 * This is done for classes defined from C to allow storing them in global variables.
67 * 1: <reserved>
68 * Ensures that RUBY_FL_SINGLETON is never set on a T_MODULE. See `rb_class_real`.
69 * 2: RCLASS_PRIME_CLASSEXT_PRIME_WRITABLE
70 * This module's prime classext is the only classext and writable from any boxes.
71 * If unset, the prime classext is writable only from the root box.
72 * 3: RCLASS_IS_INITIALIZED
73 * Module has been initialized.
74 * 4: RCLASS_BOXABLE
75 * Is a builtin class that may be boxed. It larger than a normal class.
76 * 5: RMODULE_IS_REFINEMENT
77 * Module is used for refinements.
78 */
79
80#define METACLASS_OF(k) RBASIC(k)->klass
81#define SET_METACLASS_OF(k, cls) RBASIC_SET_CLASS(k, cls)
82
83static void rb_class_remove_from_super_subclasses(VALUE klass);
84static void rb_class_remove_from_module_subclasses(VALUE klass);
85static void rb_class_classext_free_subclasses(rb_classext_t *ext);
86
87rb_classext_t *
88rb_class_unlink_classext(VALUE klass, const rb_box_t *box)
89{
90 st_data_t ext;
91 st_data_t key = (st_data_t)box->box_object;
92 st_delete(box->classext_cow_classes, &klass, 0);
93 st_delete(RCLASS_CLASSEXT_TBL(klass), &key, &ext);
94 return (rb_classext_t *)ext;
95}
96
97void
98rb_class_classext_free(VALUE klass, rb_classext_t *ext, bool is_prime)
99{
100 struct rb_id_table *tbl;
101
102 rb_id_table_free(RCLASSEXT_M_TBL(ext));
103
104 if (!RCLASSEXT_SHARED_CONST_TBL(ext) && (tbl = RCLASSEXT_CONST_TBL(ext)) != NULL) {
105 rb_free_const_table(tbl);
106 }
107
108 if (is_prime) {
109 rb_class_remove_from_super_subclasses(klass);
110 rb_class_classext_free_subclasses(ext);
111 }
112
113 if (RCLASSEXT_SUPERCLASSES_WITH_SELF(ext)) {
114 RUBY_ASSERT(is_prime); // superclasses should only be used on prime
115 xfree(RCLASSEXT_SUPERCLASSES(ext));
116 }
117
118 if (!is_prime) { // the prime classext will be freed with RClass
119 xfree(ext);
120 }
121}
122
123void
124rb_iclass_classext_free(VALUE klass, rb_classext_t *ext, bool is_prime)
125{
126 if (RCLASSEXT_ICLASS_IS_ORIGIN(ext) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext)) {
127 /* Method table is not shared for origin iclasses of classes */
128 rb_id_table_free(RCLASSEXT_M_TBL(ext));
129 }
130
131 if (RCLASSEXT_CALLABLE_M_TBL(ext) != NULL) {
132 rb_id_table_free(RCLASSEXT_CALLABLE_M_TBL(ext));
133 }
134
135 if (is_prime) {
136 rb_class_remove_from_super_subclasses(klass);
137 rb_class_remove_from_module_subclasses(klass);
138 }
139
140 if (!is_prime) { // the prime classext will be freed with RClass
141 xfree(ext);
142 }
143}
144
145static void
146iclass_free_orphan_classext(VALUE klass, rb_classext_t *ext)
147{
148 if (RCLASSEXT_ICLASS_IS_ORIGIN(ext) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext)) {
149 /* Method table is not shared for origin iclasses of classes */
150 rb_id_table_free(RCLASSEXT_M_TBL(ext));
151 }
152
153 if (RCLASSEXT_CALLABLE_M_TBL(ext) != NULL) {
154 rb_id_table_free(RCLASSEXT_CALLABLE_M_TBL(ext));
155 }
156
157 xfree(ext);
158}
159
161 VALUE obj;
162 rb_classext_t *ext;
163};
164
165static int
166set_box_classext_update(st_data_t *key_ptr, st_data_t *val_ptr, st_data_t a, int existing)
167{
169
170 if (existing) {
171 if (LIKELY(BUILTIN_TYPE(args->obj) == T_ICLASS)) {
172 iclass_free_orphan_classext(args->obj, (rb_classext_t *)*val_ptr);
173 }
174 else {
175 rb_bug("Updating existing classext for non-iclass never happen");
176 }
177 }
178
179 *val_ptr = (st_data_t)args->ext;
180
181 return ST_CONTINUE;
182}
183
184void
185rb_class_set_box_classext(VALUE obj, const rb_box_t *box, rb_classext_t *ext)
186{
187 struct rb_class_set_box_classext_args args = {
188 .obj = obj,
189 .ext = ext,
190 };
191
192 VM_ASSERT(BOX_MUTABLE_P(box));
193
194 st_update(RCLASS_CLASSEXT_TBL(obj), (st_data_t)box->box_object, set_box_classext_update, (st_data_t)&args);
195
196 // The classext references are now visible via the classext table,
197 // so we must issue the write barrier before any further allocations
198 // (e.g. st_insert below) that could trigger GC.
199 rb_gc_writebarrier_remember(obj);
200
201 st_insert(box->classext_cow_classes, (st_data_t)obj, 0);
202}
203
204RUBY_EXTERN rb_serial_t ruby_vm_global_cvar_state;
205
207 struct rb_id_table *tbl;
208 VALUE klass;
209};
210
211static enum rb_id_table_iterator_result
212duplicate_classext_m_tbl_i(ID key, VALUE value, void *data)
213{
214 struct duplicate_id_tbl_data *arg = (struct duplicate_id_tbl_data *)data;
215 rb_method_entry_t *me = (rb_method_entry_t *)value;
216 rb_method_table_insert0(arg->klass, arg->tbl, key, me, false);
217 return ID_TABLE_CONTINUE;
218}
219
220static struct rb_id_table *
221duplicate_classext_m_tbl(struct rb_id_table *orig, VALUE klass, bool init_missing)
222{
223 struct rb_id_table *tbl;
224 if (!orig) {
225 if (init_missing)
226 return rb_id_table_create(0);
227 else
228 return NULL;
229 }
230 tbl = rb_id_table_create(rb_id_table_size(orig));
231 struct duplicate_id_tbl_data data = {
232 .tbl = tbl,
233 .klass = klass,
234 };
235 rb_id_table_foreach(orig, duplicate_classext_m_tbl_i, &data);
236 return tbl;
237}
238
239static rb_const_entry_t *
240duplicate_classext_const_entry(rb_const_entry_t *src, VALUE klass)
241{
242 // See also: setup_const_entry (variable.c)
243 rb_const_entry_t *dst = ZALLOC(rb_const_entry_t);
244
245 dst->flag = src->flag;
246 dst->line = src->line;
247 RB_OBJ_WRITE(klass, &dst->value, src->value);
248 RB_OBJ_WRITE(klass, &dst->file, src->file);
249
250 return dst;
251}
252
253static enum rb_id_table_iterator_result
254duplicate_classext_const_tbl_i(ID key, VALUE value, void *data)
255{
256 struct duplicate_id_tbl_data *arg = (struct duplicate_id_tbl_data *)data;
257 rb_const_entry_t *entry = duplicate_classext_const_entry((rb_const_entry_t *)value, arg->klass);
258
259 rb_id_table_insert(arg->tbl, key, (VALUE)entry);
260
261 return ID_TABLE_CONTINUE;
262}
263
264static struct rb_id_table *
265duplicate_classext_const_tbl(struct rb_id_table *src, VALUE klass)
266{
267 struct rb_id_table *dst;
268
269 if (!src)
270 return NULL;
271
272 dst = rb_id_table_create(rb_id_table_size(src));
273
274 struct duplicate_id_tbl_data data = {
275 .tbl = dst,
276 .klass = klass,
277 };
278 rb_id_table_foreach(src, duplicate_classext_const_tbl_i, (void *)&data);
279
280 return dst;
281}
282
283static void
284class_duplicate_iclass_classext(VALUE iclass, rb_classext_t *mod_ext, const rb_box_t *box)
285{
286 RUBY_ASSERT(RB_TYPE_P(iclass, T_ICLASS));
287
288 rb_classext_t *src = RCLASS_EXT_PRIME(iclass);
289 rb_classext_t *ext = RCLASS_EXT_TABLE_LOOKUP_INTERNAL(iclass, box);
290 int first_set = 0;
291
292 if (ext) {
293 // iclass classext for the ns is only for cc/callable_m_tbl if it's created earlier than module's one
294 rb_invalidate_method_caches(RCLASSEXT_CALLABLE_M_TBL(ext), RCLASSEXT_CC_TBL(ext));
295 }
296
297 ext = ZALLOC(rb_classext_t);
298
299 RCLASSEXT_BOX(ext) = box;
300
301 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
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{
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);
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
463VALUE
464rb_class_super_of(VALUE klass)
465{
466 return RCLASS_SUPER(klass);
467}
468
469VALUE
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
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
663
664 VALUE flags = type | FL_SHAREABLE;
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
724VALUE
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
769VALUE
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
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
851VALUE
853{
854 Check_Type(super, T_CLASS);
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
867VALUE
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
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
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
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: */
1017VALUE
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 if (RB_TYPE_P(clone, T_CLASS)) {
1132 VALUE super = RCLASS_SUPER(clone);
1133 if (super && RB_TYPE_P(super, T_ICLASS)) {
1134 class_switch_superclass(rb_class_superclass(clone), clone);
1135 }
1136 }
1137
1138 return clone;
1139}
1140
1141VALUE
1143{
1144 return rb_singleton_class_clone_and_attach(obj, Qundef);
1145}
1146
1147// Clone and return the singleton class of `obj` if it has been created and is attached to `obj`.
1148VALUE
1149rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach)
1150{
1151 const VALUE klass = METACLASS_OF(obj);
1152
1153 // Note that `rb_singleton_class()` can create situations where `klass` is
1154 // attached to an object other than `obj`. In which case `obj` does not have
1155 // a material singleton class attached yet and there is no singleton class
1156 // to clone.
1157 if (!(RCLASS_SINGLETON_P(klass) && RCLASS_ATTACHED_OBJECT(klass) == obj)) {
1158 // nothing to clone
1159 return klass;
1160 }
1161 else {
1162 /* copy singleton(unnamed) class */
1163 bool klass_of_clone_is_new;
1164 RUBY_ASSERT(RB_TYPE_P(klass, T_CLASS));
1165 VALUE clone = class_alloc(T_CLASS, 0);
1166
1167 if (BUILTIN_TYPE(obj) == T_CLASS) {
1168 klass_of_clone_is_new = true;
1169 RBASIC_SET_CLASS(clone, clone);
1170 }
1171 else {
1172 VALUE klass_metaclass_clone = rb_singleton_class_clone(klass);
1173 // When `METACLASS_OF(klass) == klass_metaclass_clone`, it means the
1174 // recursive call did not clone `METACLASS_OF(klass)`.
1175 klass_of_clone_is_new = (METACLASS_OF(klass) != klass_metaclass_clone);
1176 RBASIC_SET_CLASS(clone, klass_metaclass_clone);
1177 }
1178
1179 // initialize method table before any GC chance
1180 class_initialize_method_table(clone);
1181
1182 rb_class_set_super(clone, RCLASS_SUPER(klass));
1183 rb_fields_tbl_copy(clone, klass);
1184 if (RCLASS_CONST_TBL(klass)) {
1185 struct clone_const_arg arg;
1186 struct rb_id_table *table;
1187 arg.tbl = table = rb_id_table_create(rb_id_table_size(RCLASS_CONST_TBL(klass)));
1188 arg.klass = clone;
1189 rb_id_table_foreach(RCLASS_CONST_TBL(klass), clone_const_i, &arg);
1190 RCLASS_SET_CONST_TBL(clone, table, false);
1191 }
1192 if (!UNDEF_P(attach)) {
1193 rb_singleton_class_attached(clone, attach);
1194 }
1195 {
1196 struct clone_method_arg arg;
1197 arg.old_klass = klass;
1198 arg.new_klass = clone;
1199 rb_id_table_foreach(RCLASS_M_TBL(klass), clone_method_i, &arg);
1200 }
1201 if (klass_of_clone_is_new) {
1202 rb_singleton_class_attached(METACLASS_OF(clone), clone);
1203 }
1204 FL_SET(clone, FL_SINGLETON);
1205
1206 return clone;
1207 }
1208}
1209
1210void
1212{
1213 if (RCLASS_SINGLETON_P(klass)) {
1214 RCLASS_SET_ATTACHED_OBJECT(klass, obj);
1215 }
1216}
1217
1223#define META_CLASS_OF_CLASS_CLASS_P(k) (METACLASS_OF(k) == (k))
1224
1225static int
1226rb_singleton_class_has_metaclass_p(VALUE sklass)
1227{
1228 return RCLASS_ATTACHED_OBJECT(METACLASS_OF(sklass)) == sklass;
1229}
1230
1231int
1232rb_singleton_class_internal_p(VALUE sklass)
1233{
1234 return (RB_TYPE_P(RCLASS_ATTACHED_OBJECT(sklass), T_CLASS) &&
1235 !rb_singleton_class_has_metaclass_p(sklass));
1236}
1237
1243#define HAVE_METACLASS_P(k) \
1244 (FL_TEST(METACLASS_OF(k), FL_SINGLETON) && \
1245 rb_singleton_class_has_metaclass_p(k))
1246
1254#define ENSURE_EIGENCLASS(klass) \
1255 (HAVE_METACLASS_P(klass) ? METACLASS_OF(klass) : make_metaclass(klass))
1256
1257
1267static inline VALUE
1269{
1270 VALUE super;
1271 VALUE metaclass = class_boot_boxable(Qundef, FL_TEST_RAW(klass, RCLASS_BOXABLE));
1272
1273 FL_SET(metaclass, FL_SINGLETON);
1274 rb_singleton_class_attached(metaclass, klass);
1275
1276 if (META_CLASS_OF_CLASS_CLASS_P(klass)) {
1277 SET_METACLASS_OF(klass, metaclass);
1278 SET_METACLASS_OF(metaclass, metaclass);
1279 }
1280 else {
1281 VALUE tmp = METACLASS_OF(klass); /* for a meta^(n)-class klass, tmp is meta^(n)-class of Class class */
1282 SET_METACLASS_OF(klass, metaclass);
1283 SET_METACLASS_OF(metaclass, ENSURE_EIGENCLASS(tmp));
1284 }
1285
1286 super = RCLASS_SUPER(klass);
1287 while (RB_TYPE_P(super, T_ICLASS)) super = RCLASS_SUPER(super);
1288 class_associate_super(metaclass, super ? ENSURE_EIGENCLASS(super) : rb_cClass, true);
1289 rb_class_set_initialized(klass);
1290
1291 // Full class ancestry may not have been filled until we reach here.
1292 rb_class_update_superclasses(METACLASS_OF(metaclass));
1293
1294 return metaclass;
1295}
1296
1303static inline VALUE
1305{
1306 VALUE orig_class = METACLASS_OF(obj);
1307 VALUE klass = class_boot_boxable(orig_class, FL_TEST_RAW(orig_class, RCLASS_BOXABLE));
1308
1309 FL_SET(klass, FL_SINGLETON);
1310 RBASIC_SET_CLASS(obj, klass);
1311 rb_singleton_class_attached(klass, obj);
1312 rb_yjit_invalidate_no_singleton_class(orig_class);
1313 rb_zjit_invalidate_no_singleton_class(orig_class);
1314
1315 SET_METACLASS_OF(klass, METACLASS_OF(rb_class_real(orig_class)));
1316 return klass;
1317}
1318
1319
1320static VALUE
1321boot_defclass(const char *name, VALUE super)
1322{
1323 VALUE obj = rb_class_boot(super);
1324 ID id = rb_intern(name);
1325
1326 rb_const_set((rb_cObject ? rb_cObject : obj), id, obj);
1327 rb_vm_register_global_object(obj);
1328 return obj;
1329}
1330
1331/***********************************************************************
1332 *
1333 * Document-class: Refinement
1334 *
1335 * Refinement is a class of the +self+ (current context) inside +refine+
1336 * statement. It allows to import methods from other modules, see #import_methods.
1337 */
1338
1339#if 0 /* for RDoc */
1340/*
1341 * Document-method: Refinement#import_methods
1342 *
1343 * call-seq:
1344 * import_methods(module, ...) -> self
1345 *
1346 * Imports methods from modules. Unlike Module#include,
1347 * Refinement#import_methods copies methods and adds them into the refinement,
1348 * so the refinement is activated in the imported methods.
1349 *
1350 * Note that due to method copying, only methods defined in Ruby code can be imported.
1351 *
1352 * module StrUtils
1353 * def indent(level)
1354 * ' ' * level + self
1355 * end
1356 * end
1357 *
1358 * module M
1359 * refine String do
1360 * import_methods StrUtils
1361 * end
1362 * end
1363 *
1364 * using M
1365 * "foo".indent(3)
1366 * #=> " foo"
1367 *
1368 * module M
1369 * refine String do
1370 * import_methods Enumerable
1371 * # Can't import method which is not defined with Ruby code: Enumerable#drop
1372 * end
1373 * end
1374 *
1375 */
1376
1377static VALUE
1378refinement_import_methods(int argc, VALUE *argv, VALUE refinement)
1379{
1380}
1381# endif
1382
1401
1402void
1403Init_class_hierarchy(void)
1404{
1405 rb_cBasicObject = boot_defclass("BasicObject", 0);
1406 rb_cObject = boot_defclass("Object", rb_cBasicObject);
1407 rb_vm_register_global_object(rb_cObject);
1408
1409 /* resolve class name ASAP for order-independence */
1410 rb_set_class_path_string(rb_cObject, rb_cObject, rb_fstring_lit("Object"));
1411
1412 rb_cModule = boot_defclass("Module", rb_cObject);
1413 rb_cClass = boot_defclass("Class", rb_cModule);
1414 rb_cRefinement = boot_defclass("Refinement", rb_cModule);
1415
1416#if 0 /* for RDoc */
1417 // we pretend it to be public, otherwise RDoc will ignore it
1418 rb_define_method(rb_cRefinement, "import_methods", refinement_import_methods, -1);
1419#endif
1420
1421 rb_const_set(rb_cObject, rb_intern_const("BasicObject"), rb_cBasicObject);
1422 RBASIC_SET_CLASS(rb_cClass, rb_cClass);
1423 RBASIC_SET_CLASS(rb_cModule, rb_cClass);
1424 RBASIC_SET_CLASS(rb_cObject, rb_cClass);
1425 RBASIC_SET_CLASS(rb_cRefinement, rb_cClass);
1426 RBASIC_SET_CLASS(rb_cBasicObject, rb_cClass);
1427
1429}
1430
1431
1442VALUE
1443rb_make_metaclass(VALUE obj, VALUE unused)
1444{
1445 if (BUILTIN_TYPE(obj) == T_CLASS) {
1446 return make_metaclass(obj);
1447 }
1448 else {
1449 return make_singleton_class(obj);
1450 }
1451}
1452
1453VALUE
1455{
1456 VALUE klass;
1457
1458 if (!super) super = rb_cObject;
1459 klass = rb_class_new(super);
1460 rb_make_metaclass(klass, METACLASS_OF(super));
1461
1462 return klass;
1463}
1464
1465
1474VALUE
1476{
1477 ID inherited;
1478 if (!super) super = rb_cObject;
1479 CONST_ID(inherited, "inherited");
1480 return rb_funcall(super, inherited, 1, klass);
1481}
1482
1483VALUE
1484rb_define_class(const char *name, VALUE super)
1485{
1486 VALUE klass;
1487 ID id = rb_intern(name);
1488
1489 if (rb_const_defined(rb_cObject, id)) {
1490 klass = rb_const_get(rb_cObject, id);
1491 if (!RB_TYPE_P(klass, T_CLASS)) {
1492 rb_raise(rb_eTypeError, "%s is not a class (%"PRIsVALUE")",
1493 name, rb_obj_class(klass));
1494 }
1495 if (rb_class_real(RCLASS_SUPER(klass)) != super) {
1496 rb_raise(rb_eTypeError, "superclass mismatch for class %s", name);
1497 }
1498
1499 /* Class may have been defined in Ruby and not pin-rooted */
1500 rb_vm_register_global_object(klass);
1501 return klass;
1502 }
1503 if (!super) {
1504 rb_raise(rb_eArgError, "no super class for '%s'", name);
1505 }
1506 klass = rb_define_class_id(id, super);
1507 rb_vm_register_global_object(klass);
1508 rb_const_set(rb_cObject, id, klass);
1509 rb_class_inherited(super, klass);
1510
1511 return klass;
1512}
1513
1514VALUE
1515rb_define_class_under(VALUE outer, const char *name, VALUE super)
1516{
1517 return rb_define_class_id_under(outer, rb_intern(name), super);
1518}
1519
1520VALUE
1521rb_define_class_id_under_no_pin(VALUE outer, ID id, VALUE super)
1522{
1523 VALUE klass;
1524
1525 if (rb_const_defined_at(outer, id)) {
1526 klass = rb_const_get_at(outer, id);
1527 if (!RB_TYPE_P(klass, T_CLASS)) {
1528 rb_raise(rb_eTypeError, "%"PRIsVALUE"::%"PRIsVALUE" is not a class"
1529 " (%"PRIsVALUE")",
1530 outer, rb_id2str(id), rb_obj_class(klass));
1531 }
1532 if (rb_class_real(RCLASS_SUPER(klass)) != super) {
1533 rb_raise(rb_eTypeError, "superclass mismatch for class "
1534 "%"PRIsVALUE"::%"PRIsVALUE""
1535 " (%"PRIsVALUE" is given but was %"PRIsVALUE")",
1536 outer, rb_id2str(id), RCLASS_SUPER(klass), super);
1537 }
1538
1539 return klass;
1540 }
1541 if (!super) {
1542 rb_raise(rb_eArgError, "no super class for '%"PRIsVALUE"::%"PRIsVALUE"'",
1543 rb_class_path(outer), rb_id2str(id));
1544 }
1545 klass = rb_define_class_id(id, super);
1546 rb_set_class_path_string(klass, outer, rb_id2str(id));
1547 rb_const_set(outer, id, klass);
1548 rb_class_inherited(super, klass);
1549
1550 return klass;
1551}
1552
1553VALUE
1555{
1556 VALUE klass = rb_define_class_id_under_no_pin(outer, id, super);
1557 rb_vm_register_global_object(klass);
1558 return klass;
1559}
1560
1561VALUE
1562rb_module_s_alloc(VALUE klass)
1563{
1564 VALUE mod = class_alloc(T_MODULE, klass);
1565 class_initialize_method_table(mod);
1566 return mod;
1567}
1568
1569static inline VALUE
1570module_new(VALUE klass)
1571{
1572 VALUE mdl = class_alloc(T_MODULE, klass);
1573 class_initialize_method_table(mdl);
1574 return (VALUE)mdl;
1575}
1576
1577VALUE
1579{
1580 return module_new(rb_cModule);
1581}
1582
1583VALUE
1585{
1586 return module_new(rb_cRefinement);
1587}
1588
1589// Kept for compatibility. Use rb_module_new() instead.
1590VALUE
1592{
1593 return rb_module_new();
1594}
1595
1596VALUE
1597rb_define_module(const char *name)
1598{
1599 VALUE module;
1600 ID id = rb_intern(name);
1601
1602 if (rb_const_defined(rb_cObject, id)) {
1603 module = rb_const_get(rb_cObject, id);
1604 if (!RB_TYPE_P(module, T_MODULE)) {
1605 rb_raise(rb_eTypeError, "%s is not a module (%"PRIsVALUE")",
1606 name, rb_obj_class(module));
1607 }
1608 /* Module may have been defined in Ruby and not pin-rooted */
1609 rb_vm_register_global_object(module);
1610 return module;
1611 }
1612 module = rb_module_new();
1613 rb_vm_register_global_object(module);
1614 rb_const_set(rb_cObject, id, module);
1615
1616 return module;
1617}
1618
1619VALUE
1620rb_define_module_under(VALUE outer, const char *name)
1621{
1622 return rb_define_module_id_under(outer, rb_intern(name));
1623}
1624
1625VALUE
1627{
1628 VALUE module;
1629
1630 if (rb_const_defined_at(outer, id)) {
1631 module = rb_const_get_at(outer, id);
1632 if (!RB_TYPE_P(module, T_MODULE)) {
1633 rb_raise(rb_eTypeError, "%"PRIsVALUE"::%"PRIsVALUE" is not a module"
1634 " (%"PRIsVALUE")",
1635 outer, rb_id2str(id), rb_obj_class(module));
1636 }
1637 /* Module may have been defined in Ruby and not pin-rooted */
1638 rb_vm_register_global_object(module);
1639 return module;
1640 }
1641 module = rb_module_new();
1642 rb_const_set(outer, id, module);
1643 rb_set_class_path_string(module, outer, rb_id2str(id));
1644 rb_vm_register_global_object(module);
1645
1646 return module;
1647}
1648
1649VALUE
1650rb_include_class_new(VALUE module, VALUE super)
1651{
1652 VALUE klass = class_alloc(T_ICLASS, rb_cClass);
1653
1654 RCLASS_SET_M_TBL(klass, RCLASS_WRITABLE_M_TBL(module));
1655
1656 RCLASS_SET_ORIGIN(klass, klass);
1657 if (BUILTIN_TYPE(module) == T_ICLASS) {
1658 module = METACLASS_OF(module);
1659 }
1660 RUBY_ASSERT(!RB_TYPE_P(module, T_ICLASS));
1661 if (RCLASS_WRITABLE_CONST_TBL(module)) {
1662 RCLASS_SET_CONST_TBL(klass, RCLASS_WRITABLE_CONST_TBL(module), true);
1663 }
1664 else {
1665 RCLASS_WRITE_CONST_TBL(module, rb_id_table_create(0), false);
1666 RCLASS_SET_CONST_TBL(klass, RCLASS_WRITABLE_CONST_TBL(module), true);
1667 }
1668
1669 RCLASS_SET_CVC_TBL(klass, RCLASS_WRITABLE_CVC_TBL(module));
1670
1671 class_associate_super(klass, super, true);
1672 RBASIC_SET_CLASS(klass, module);
1673
1674 return (VALUE)klass;
1675}
1676
1677static int include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super);
1678
1679static void
1680ensure_includable(VALUE klass, VALUE module)
1681{
1682 rb_class_modify_check(klass);
1683 Check_Type(module, T_MODULE);
1684 rb_class_set_initialized(module);
1685 if (!NIL_P(rb_refinement_module_get_refined_class(module))) {
1686 rb_raise(rb_eArgError, "refinement module is not allowed");
1687 }
1688}
1689
1690void
1692{
1693 int changed = 0;
1694
1695 ensure_includable(klass, module);
1696
1697 changed = include_modules_at(klass, RCLASS_ORIGIN(klass), module, TRUE);
1698 if (changed < 0)
1699 rb_raise(rb_eArgError, "cyclic include detected");
1700
1701 if (RB_TYPE_P(klass, T_MODULE)) {
1702 rb_subclass_entry_t *iclass = RCLASS_SUBCLASSES_FIRST(klass);
1703 while (iclass) {
1704 int do_include = 1;
1705 VALUE check_class = iclass->klass;
1706 /* During lazy sweeping, iclass->klass could be a dead object that
1707 * has not yet been swept. */
1708 if (!rb_objspace_garbage_object_p(check_class)) {
1709 while (check_class) {
1710 RUBY_ASSERT(!rb_objspace_garbage_object_p(check_class));
1711
1712 if (RB_TYPE_P(check_class, T_ICLASS) &&
1713 (METACLASS_OF(check_class) == module)) {
1714 do_include = 0;
1715 }
1716 check_class = RCLASS_SUPER(check_class);
1717 }
1718
1719 if (do_include) {
1720 include_modules_at(iclass->klass, RCLASS_ORIGIN(iclass->klass), module, TRUE);
1721 }
1722 }
1723
1724 iclass = iclass->next;
1725 }
1726 }
1727}
1728
1729static enum rb_id_table_iterator_result
1730add_refined_method_entry_i(ID key, VALUE value, void *data)
1731{
1732 rb_add_refined_method_entry((VALUE)data, key);
1733 return ID_TABLE_CONTINUE;
1734}
1735
1736static enum rb_id_table_iterator_result
1737clear_module_cache_i(ID id, VALUE val, void *data)
1738{
1739 VALUE klass = (VALUE)data;
1740 rb_clear_method_cache(klass, id);
1741 return ID_TABLE_CONTINUE;
1742}
1743
1744static bool
1745module_in_super_chain(const VALUE klass, VALUE module)
1746{
1747 struct rb_id_table *const klass_m_tbl = RCLASS_M_TBL(RCLASS_ORIGIN(klass));
1748 if (klass_m_tbl) {
1749 while (module) {
1750 if (klass_m_tbl == RCLASS_M_TBL(module))
1751 return true;
1752 module = RCLASS_SUPER(module);
1753 }
1754 }
1755 return false;
1756}
1757
1758// For each ID key in the class constant table, we're going to clear the VM's
1759// inline constant caches associated with it.
1760static enum rb_id_table_iterator_result
1761clear_constant_cache_i(ID id, VALUE value, void *data)
1762{
1764 return ID_TABLE_CONTINUE;
1765}
1766
1767static int
1768do_include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super, bool check_cyclic)
1769{
1770 VALUE p, iclass, origin_stack = 0;
1771 int method_changed = 0;
1772 long origin_len;
1773 VALUE klass_origin = RCLASS_ORIGIN(klass);
1774 VALUE original_klass = klass;
1775
1776 if (check_cyclic && module_in_super_chain(klass, module))
1777 return -1;
1778
1779 while (module) {
1780 int c_seen = FALSE;
1781 int superclass_seen = FALSE;
1782 struct rb_id_table *tbl;
1783
1784 if (klass == c) {
1785 c_seen = TRUE;
1786 }
1787 if (klass_origin != c || search_super) {
1788 /* ignore if the module included already in superclasses for include,
1789 * ignore if the module included before origin class for prepend
1790 */
1791 for (p = RCLASS_SUPER(klass); p; p = RCLASS_SUPER(p)) {
1792 int type = BUILTIN_TYPE(p);
1793 if (klass_origin == p && !search_super)
1794 break;
1795 if (c == p)
1796 c_seen = TRUE;
1797 if (type == T_ICLASS) {
1798 if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) {
1799 if (!superclass_seen && c_seen) {
1800 c = p; /* move insertion point */
1801 }
1802 goto skip;
1803 }
1804 }
1805 else if (type == T_CLASS) {
1806 superclass_seen = TRUE;
1807 }
1808 }
1809 }
1810
1811 VALUE super_class = RCLASS_SUPER(c);
1812
1813 // invalidate inline method cache
1814 RB_DEBUG_COUNTER_INC(cvar_include_invalidate);
1815 ruby_vm_global_cvar_state++;
1816 tbl = RCLASS_M_TBL(module);
1817 if (tbl && rb_id_table_size(tbl)) {
1818 if (search_super) { // include
1819 if (super_class && !RB_TYPE_P(super_class, T_MODULE)) {
1820 rb_id_table_foreach(tbl, clear_module_cache_i, (void *)super_class);
1821 }
1822 }
1823 else { // prepend
1824 if (!RB_TYPE_P(original_klass, T_MODULE)) {
1825 rb_id_table_foreach(tbl, clear_module_cache_i, (void *)original_klass);
1826 }
1827 }
1828 method_changed = 1;
1829 }
1830
1831 // setup T_ICLASS for the include/prepend module
1832 iclass = rb_include_class_new(module, super_class);
1833 c = rb_class_set_super(c, iclass);
1834 RCLASS_SET_INCLUDER(iclass, klass);
1835 if (module != RCLASS_ORIGIN(module)) {
1836 if (!origin_stack) origin_stack = rb_ary_hidden_new(2);
1837 VALUE origin[2] = {iclass, RCLASS_ORIGIN(module)};
1838 rb_ary_cat(origin_stack, origin, 2);
1839 }
1840 else if (origin_stack && (origin_len = RARRAY_LEN(origin_stack)) > 1 &&
1841 RARRAY_AREF(origin_stack, origin_len - 1) == module) {
1842 RCLASS_WRITE_ORIGIN(RARRAY_AREF(origin_stack, (origin_len -= 2)), iclass);
1843 RICLASS_WRITE_ORIGIN_SHARED_MTBL(iclass);
1844 rb_ary_resize(origin_stack, origin_len);
1845 }
1846
1847 VALUE m = module;
1848 if (BUILTIN_TYPE(m) == T_ICLASS) m = METACLASS_OF(m);
1849 rb_module_add_to_subclasses_list(m, iclass);
1850
1851 if (BUILTIN_TYPE(klass) == T_MODULE && FL_TEST(klass, RMODULE_IS_REFINEMENT)) {
1852 VALUE refined_class =
1853 rb_refinement_module_get_refined_class(klass);
1854
1855 rb_id_table_foreach(RCLASS_M_TBL(module), add_refined_method_entry_i, (void *)refined_class);
1857 }
1858
1859 tbl = RCLASS_CONST_TBL(module);
1860 if (tbl && rb_id_table_size(tbl))
1861 rb_id_table_foreach(tbl, clear_constant_cache_i, NULL);
1862 skip:
1863 module = RCLASS_SUPER(module);
1864 }
1865
1866 return method_changed;
1867}
1868
1869static int
1870include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super)
1871{
1872 return do_include_modules_at(klass, c, module, search_super, true);
1873}
1874
1875static enum rb_id_table_iterator_result
1876move_refined_method(ID key, VALUE value, void *data)
1877{
1878 rb_method_entry_t *me = (rb_method_entry_t *)value;
1879
1880 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1881 VALUE klass = (VALUE)data;
1882 struct rb_id_table *tbl = RCLASS_WRITABLE_M_TBL(klass);
1883
1884 if (me->def->body.refined.orig_me) {
1885 const rb_method_entry_t *orig_me = me->def->body.refined.orig_me, *new_me;
1886 RB_OBJ_WRITE(me, &me->def->body.refined.orig_me, NULL);
1887 new_me = rb_method_entry_clone(me);
1888 rb_method_table_insert(klass, tbl, key, new_me);
1889 rb_method_entry_copy(me, orig_me);
1890 return ID_TABLE_CONTINUE;
1891 }
1892 else {
1893 rb_method_table_insert(klass, tbl, key, me);
1894 return ID_TABLE_DELETE;
1895 }
1896 }
1897 else {
1898 return ID_TABLE_CONTINUE;
1899 }
1900}
1901
1902static enum rb_id_table_iterator_result
1903cache_clear_refined_method(ID key, VALUE value, void *data)
1904{
1905 rb_method_entry_t *me = (rb_method_entry_t *) value;
1906
1907 if (me->def->type == VM_METHOD_TYPE_REFINED && me->def->body.refined.orig_me) {
1908 VALUE klass = (VALUE)data;
1909 rb_clear_method_cache(klass, me->called_id);
1910 }
1911 // Refined method entries without an orig_me is going to stay in the method
1912 // table of klass, like before the move, so no need to clear the cache.
1913
1914 return ID_TABLE_CONTINUE;
1915}
1916
1917static bool
1918ensure_origin(VALUE klass)
1919{
1920 VALUE origin = RCLASS_ORIGIN(klass);
1921 if (origin == klass) {
1922 origin = class_alloc(T_ICLASS, klass);
1923 RCLASS_SET_M_TBL(origin, RCLASS_M_TBL(klass));
1924 rb_class_set_super(origin, RCLASS_SUPER(klass));
1925 rb_class_set_super(klass, origin); // writes origin into RCLASS_SUPER(klass)
1926 RCLASS_WRITE_ORIGIN(klass, origin);
1927
1928 // RCLASS_WRITE_ORIGIN marks origin as an origin, so this is the first
1929 // point that it sees M_TBL and may mark it
1930 rb_gc_writebarrier_remember(origin);
1931
1932 class_clear_method_table(klass);
1933 rb_id_table_foreach(RCLASS_M_TBL(origin), cache_clear_refined_method, (void *)klass);
1934 rb_id_table_foreach(RCLASS_M_TBL(origin), move_refined_method, (void *)klass);
1935 return true;
1936 }
1937 return false;
1938}
1939
1940void
1942{
1943 int changed;
1944 bool klass_had_no_origin;
1945
1946 ensure_includable(klass, module);
1947 if (module_in_super_chain(klass, module))
1948 rb_raise(rb_eArgError, "cyclic prepend detected");
1949
1950 klass_had_no_origin = ensure_origin(klass);
1951 changed = do_include_modules_at(klass, klass, module, FALSE, false);
1952 RUBY_ASSERT(changed >= 0); // already checked for cyclic prepend above
1953 if (changed) {
1954 rb_vm_check_redefinition_by_prepend(klass);
1955 }
1956 if (RB_TYPE_P(klass, T_MODULE)) {
1957 rb_subclass_entry_t *iclass = RCLASS_SUBCLASSES_FIRST(klass);
1958 VALUE klass_origin = RCLASS_ORIGIN(klass);
1959 struct rb_id_table *klass_m_tbl = RCLASS_M_TBL(klass);
1960 struct rb_id_table *klass_origin_m_tbl = RCLASS_M_TBL(klass_origin);
1961 VALUE new_origins = 0;
1962 while (iclass) {
1963 /* During lazy sweeping, iclass->klass could be a dead object that
1964 * has not yet been swept. */
1965 if (!rb_objspace_garbage_object_p(iclass->klass)) {
1966 const VALUE subclass = iclass->klass;
1967 if (klass_had_no_origin && klass_origin_m_tbl == RCLASS_M_TBL(subclass)) {
1968 // backfill an origin iclass to handle refinements and future prepends
1969 rb_id_table_foreach(RCLASS_M_TBL(subclass), clear_module_cache_i, (void *)subclass);
1970 RCLASS_WRITE_M_TBL(subclass, klass_m_tbl);
1971 VALUE origin = rb_include_class_new(klass_origin, RCLASS_SUPER(subclass));
1972 rb_class_set_super(subclass, origin);
1973 RCLASS_SET_INCLUDER(origin, RCLASS_INCLUDER(subclass));
1974 RCLASS_WRITE_ORIGIN(subclass, origin);
1975 RICLASS_SET_ORIGIN_SHARED_MTBL(origin);
1976 if (!new_origins) new_origins = rb_ary_hidden_new(1);
1977 rb_ary_push(new_origins, origin);
1978 }
1979 include_modules_at(subclass, subclass, module, FALSE);
1980 }
1981
1982 iclass = iclass->next;
1983 }
1984 /* Register after the loop. Registering during it would visit the
1985 * new iclass and prepend module into it a second time. */
1986 if (new_origins) {
1987 for (long i = 0; i < RARRAY_LEN(new_origins); i++) {
1988 rb_module_add_to_subclasses_list(klass, RARRAY_AREF(new_origins, i));
1989 }
1990 }
1991 RB_GC_GUARD(new_origins);
1992 }
1993}
1994
1995/*
1996 * call-seq:
1997 * mod.included_modules -> array
1998 *
1999 * Returns the list of modules included or prepended in <i>mod</i>
2000 * or one of <i>mod</i>'s ancestors.
2001 *
2002 * module Sub
2003 * end
2004 *
2005 * module Mixin
2006 * prepend Sub
2007 * end
2008 *
2009 * module Outer
2010 * include Mixin
2011 * end
2012 *
2013 * Mixin.included_modules #=> [Sub]
2014 * Outer.included_modules #=> [Sub, Mixin]
2015 */
2016
2017VALUE
2019{
2020 VALUE ary = rb_ary_new();
2021 VALUE p;
2022 VALUE origin = RCLASS_ORIGIN(mod);
2023
2024 for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
2025 if (p != origin && RCLASS_ORIGIN(p) == p && BUILTIN_TYPE(p) == T_ICLASS) {
2026 VALUE m = METACLASS_OF(p);
2027 if (RB_TYPE_P(m, T_MODULE))
2028 rb_ary_push(ary, m);
2029 }
2030 }
2031 return ary;
2032}
2033
2034/*
2035 * call-seq:
2036 * mod.include?(module) -> true or false
2037 *
2038 * Returns <code>true</code> if <i>module</i> is included
2039 * or prepended in <i>mod</i> or one of <i>mod</i>'s ancestors.
2040 *
2041 * module A
2042 * end
2043 * class B
2044 * include A
2045 * end
2046 * class C < B
2047 * end
2048 * B.include?(A) #=> true
2049 * C.include?(A) #=> true
2050 * A.include?(A) #=> false
2051 */
2052
2053VALUE
2055{
2056 VALUE p;
2057
2058 Check_Type(mod2, T_MODULE);
2059 for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
2060 if (BUILTIN_TYPE(p) == T_ICLASS && !RICLASS_IS_ORIGIN_P(p)) {
2061 if (METACLASS_OF(p) == mod2) return Qtrue;
2062 }
2063 }
2064 return Qfalse;
2065}
2066
2067/*
2068 * call-seq:
2069 * mod.ancestors -> array
2070 *
2071 * Returns a list of modules included/prepended in <i>mod</i>
2072 * (including <i>mod</i> itself).
2073 *
2074 * module Mod
2075 * include Math
2076 * include Comparable
2077 * prepend Enumerable
2078 * end
2079 *
2080 * Mod.ancestors #=> [Enumerable, Mod, Comparable, Math]
2081 * Math.ancestors #=> [Math]
2082 * Enumerable.ancestors #=> [Enumerable]
2083 */
2084
2085VALUE
2087{
2088 VALUE p, ary = rb_ary_new();
2089 VALUE refined_class = Qnil;
2090 if (BUILTIN_TYPE(mod) == T_MODULE && FL_TEST(mod, RMODULE_IS_REFINEMENT)) {
2091 refined_class = rb_refinement_module_get_refined_class(mod);
2092 }
2093
2094 for (p = mod; p; p = RCLASS_SUPER(p)) {
2095 if (p == refined_class) break;
2096 if (p != RCLASS_ORIGIN(p)) continue;
2097 if (BUILTIN_TYPE(p) == T_ICLASS) {
2098 rb_ary_push(ary, METACLASS_OF(p));
2099 }
2100 else {
2101 rb_ary_push(ary, p);
2102 }
2103 }
2104 return ary;
2105}
2106
2108{
2109 VALUE buffer;
2110 long count;
2111 long maxcount;
2112 bool immediate_only;
2113};
2114
2115static void
2116class_descendants_recursive(VALUE klass, VALUE v)
2117{
2118 struct subclass_traverse_data *data = (struct subclass_traverse_data *) v;
2119
2120 if (RB_TYPE_P(klass, T_ICLASS)) return; // skip refinement ICLASSes
2121
2122 if (!RCLASS_SINGLETON_P(klass)) {
2123 if (data->buffer && data->count < data->maxcount && !rb_objspace_garbage_object_p(klass)) {
2124 // assumes that this does not cause GC as long as the length does not exceed the capacity
2125 rb_ary_push(data->buffer, klass);
2126 }
2127 data->count++;
2128 if (data->immediate_only) return;
2129 }
2130 rb_class_foreach_subclass(klass, class_descendants_recursive, v);
2131}
2132
2133static VALUE
2134class_descendants(VALUE klass, bool immediate_only)
2135{
2136 struct subclass_traverse_data data = { Qfalse, 0, -1, immediate_only };
2137
2138 // estimate the count of subclasses
2139 rb_class_foreach_subclass(klass, class_descendants_recursive, (VALUE) &data);
2140
2141 // the following allocation may cause GC which may change the number of subclasses
2142 data.buffer = rb_ary_new_capa(data.count);
2143 data.maxcount = data.count;
2144 data.count = 0;
2145
2146 size_t gc_count = rb_gc_count();
2147
2148 // enumerate subclasses
2149 rb_class_foreach_subclass(klass, class_descendants_recursive, (VALUE) &data);
2150
2151 if (gc_count != rb_gc_count()) {
2152 rb_bug("GC must not occur during the subclass iteration of Class#descendants");
2153 }
2154
2155 return data.buffer;
2156}
2157
2158/*
2159 * call-seq:
2160 * subclasses -> array
2161 *
2162 * Returns an array of classes where the receiver is the
2163 * direct superclass of the class, excluding singleton classes.
2164 * The order of the returned array is not defined.
2165 *
2166 * class A; end
2167 * class B < A; end
2168 * class C < B; end
2169 * class D < A; end
2170 *
2171 * A.subclasses #=> [D, B]
2172 * B.subclasses #=> [C]
2173 * C.subclasses #=> []
2174 *
2175 * Anonymous subclasses (not associated with a constant) are
2176 * returned, too:
2177 *
2178 * c = Class.new(A)
2179 * A.subclasses # => [#<Class:0x00007f003c77bd78>, D, B]
2180 *
2181 * Note that the parent does not hold references to subclasses
2182 * and doesn't prevent them from being garbage collected. This
2183 * means that the subclass might disappear when all references
2184 * to it are dropped:
2185 *
2186 * # drop the reference to subclass, it can be garbage-collected now
2187 * c = nil
2188 *
2189 * A.subclasses
2190 * # It can be
2191 * # => [#<Class:0x00007f003c77bd78>, D, B]
2192 * # ...or just
2193 * # => [D, B]
2194 * # ...depending on whether garbage collector was run
2195 */
2196
2197VALUE
2199{
2200 return class_descendants(klass, true);
2201}
2202
2203/*
2204 * call-seq:
2205 * attached_object -> object
2206 *
2207 * Returns the object for which the receiver is the singleton class.
2208 *
2209 * Raises an TypeError if the class is not a singleton class.
2210 *
2211 * class Foo; end
2212 *
2213 * Foo.singleton_class.attached_object #=> Foo
2214 * Foo.attached_object #=> TypeError: `Foo' is not a singleton class
2215 * Foo.new.singleton_class.attached_object #=> #<Foo:0x000000010491a370>
2216 * TrueClass.attached_object #=> TypeError: `TrueClass' is not a singleton class
2217 * NilClass.attached_object #=> TypeError: `NilClass' is not a singleton class
2218 */
2219
2220VALUE
2222{
2223 if (!RCLASS_SINGLETON_P(klass)) {
2224 rb_raise(rb_eTypeError, "'%"PRIsVALUE"' is not a singleton class", klass);
2225 }
2226
2227 return RCLASS_ATTACHED_OBJECT(klass);
2228}
2229
2230static void
2231ins_methods_push(st_data_t name, st_data_t ary)
2232{
2233 rb_ary_push((VALUE)ary, ID2SYM((ID)name));
2234}
2235
2236static int
2237ins_methods_i(st_data_t name, st_data_t type, st_data_t ary)
2238{
2239 switch ((rb_method_visibility_t)type) {
2240 case METHOD_VISI_UNDEF:
2241 case METHOD_VISI_PRIVATE:
2242 break;
2243 default: /* everything but private */
2244 ins_methods_push(name, ary);
2245 break;
2246 }
2247 return ST_CONTINUE;
2248}
2249
2250static int
2251ins_methods_type_i(st_data_t name, st_data_t type, st_data_t ary, rb_method_visibility_t visi)
2252{
2253 if ((rb_method_visibility_t)type == visi) {
2254 ins_methods_push(name, ary);
2255 }
2256 return ST_CONTINUE;
2257}
2258
2259static int
2260ins_methods_prot_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_PROTECTED);
2263}
2264
2265static int
2266ins_methods_priv_i(st_data_t name, st_data_t type, st_data_t ary)
2267{
2268 return ins_methods_type_i(name, type, ary, METHOD_VISI_PRIVATE);
2269}
2270
2271static int
2272ins_methods_pub_i(st_data_t name, st_data_t type, st_data_t ary)
2273{
2274 return ins_methods_type_i(name, type, ary, METHOD_VISI_PUBLIC);
2275}
2276
2277static int
2278ins_methods_undef_i(st_data_t name, st_data_t type, st_data_t ary)
2279{
2280 return ins_methods_type_i(name, type, ary, METHOD_VISI_UNDEF);
2281}
2282
2284 st_table *list;
2285 int recur;
2286};
2287
2288static enum rb_id_table_iterator_result
2289method_entry_i(ID key, VALUE value, void *data)
2290{
2291 const rb_method_entry_t *me = (const rb_method_entry_t *)value;
2292 struct method_entry_arg *arg = (struct method_entry_arg *)data;
2293 rb_method_visibility_t type;
2294
2295 if (me->def->type == VM_METHOD_TYPE_REFINED) {
2296 VALUE owner = me->owner;
2297 me = rb_resolve_refined_method(Qnil, me);
2298 if (!me) return ID_TABLE_CONTINUE;
2299 if (!arg->recur && me->owner != owner) return ID_TABLE_CONTINUE;
2300 }
2301 if (!st_is_member(arg->list, key)) {
2302 if (UNDEFINED_METHOD_ENTRY_P(me)) {
2303 type = METHOD_VISI_UNDEF; /* none */
2304 }
2305 else {
2306 type = METHOD_ENTRY_VISI(me);
2307 RUBY_ASSERT(type != METHOD_VISI_UNDEF);
2308 }
2309 st_add_direct(arg->list, key, (st_data_t)type);
2310 }
2311 return ID_TABLE_CONTINUE;
2312}
2313
2314static void
2315add_instance_method_list(VALUE mod, struct method_entry_arg *me_arg)
2316{
2317 struct rb_id_table *m_tbl = RCLASS_M_TBL(mod);
2318 if (!m_tbl) return;
2319 rb_id_table_foreach(m_tbl, method_entry_i, me_arg);
2320}
2321
2322static bool
2323particular_class_p(VALUE mod)
2324{
2325 if (!mod) return false;
2326 if (RCLASS_SINGLETON_P(mod)) return true;
2327 if (BUILTIN_TYPE(mod) == T_ICLASS) return true;
2328 return false;
2329}
2330
2331static VALUE
2332class_instance_method_list(int argc, const VALUE *argv, VALUE mod, int obj, int (*func) (st_data_t, st_data_t, st_data_t))
2333{
2334 VALUE ary;
2335 int recur = TRUE, prepended = 0;
2336 struct method_entry_arg me_arg;
2337
2338 if (rb_check_arity(argc, 0, 1)) recur = RTEST(argv[0]);
2339
2340 me_arg.list = st_init_numtable();
2341 me_arg.recur = recur;
2342
2343 if (obj) {
2344 for (; particular_class_p(mod); mod = RCLASS_SUPER(mod)) {
2345 add_instance_method_list(mod, &me_arg);
2346 }
2347 }
2348
2349 if (!recur && RCLASS_ORIGIN(mod) != mod) {
2350 mod = RCLASS_ORIGIN(mod);
2351 prepended = 1;
2352 }
2353
2354 for (; mod; mod = RCLASS_SUPER(mod)) {
2355 add_instance_method_list(mod, &me_arg);
2356 if (BUILTIN_TYPE(mod) == T_ICLASS && !prepended) continue;
2357 if (!recur) break;
2358 }
2359 ary = rb_ary_new2(me_arg.list->num_entries);
2360 st_foreach(me_arg.list, func, ary);
2361 st_free_table(me_arg.list);
2362
2363 return ary;
2364}
2365
2366/*
2367 * call-seq:
2368 * mod.instance_methods(include_super=true) -> array
2369 *
2370 * Returns an array containing the names of the public and protected instance
2371 * methods in the receiver. For a module, these are the public and protected methods;
2372 * for a class, they are the instance (not singleton) methods. If the optional
2373 * parameter is <code>false</code>, the methods of any ancestors are not included.
2374 *
2375 * module A
2376 * def method1() end
2377 * end
2378 * class B
2379 * include A
2380 * def method2() end
2381 * end
2382 * class C < B
2383 * def method3() end
2384 * end
2385 *
2386 * A.instance_methods(false) #=> [:method1]
2387 * B.instance_methods(false) #=> [:method2]
2388 * B.instance_methods(true).include?(:method1) #=> true
2389 * C.instance_methods(false) #=> [:method3]
2390 * C.instance_methods.include?(:method2) #=> true
2391 *
2392 * Note that method visibility changes in the current class, as well as aliases,
2393 * are considered as methods of the current class by this method:
2394 *
2395 * class C < B
2396 * alias method4 method2
2397 * protected :method2
2398 * end
2399 * C.instance_methods(false).sort #=> [:method2, :method3, :method4]
2400 */
2401
2402VALUE
2403rb_class_instance_methods(int argc, const VALUE *argv, VALUE mod)
2404{
2405 return class_instance_method_list(argc, argv, mod, 0, ins_methods_i);
2406}
2407
2408/*
2409 * call-seq:
2410 * mod.protected_instance_methods(include_super=true) -> array
2411 *
2412 * Returns a list of the protected instance methods defined in
2413 * <i>mod</i>. If the optional parameter is <code>false</code>, the
2414 * methods of any ancestors are not included.
2415 */
2416
2417VALUE
2419{
2420 return class_instance_method_list(argc, argv, mod, 0, ins_methods_prot_i);
2421}
2422
2423/*
2424 * call-seq:
2425 * mod.private_instance_methods(include_super=true) -> array
2426 *
2427 * Returns a list of the private instance methods defined in
2428 * <i>mod</i>. If the optional parameter is <code>false</code>, the
2429 * methods of any ancestors are not included.
2430 *
2431 * module Mod
2432 * def method1() end
2433 * private :method1
2434 * def method2() end
2435 * end
2436 * Mod.instance_methods #=> [:method2]
2437 * Mod.private_instance_methods #=> [:method1]
2438 */
2439
2440VALUE
2442{
2443 return class_instance_method_list(argc, argv, mod, 0, ins_methods_priv_i);
2444}
2445
2446/*
2447 * call-seq:
2448 * mod.public_instance_methods(include_super=true) -> array
2449 *
2450 * Returns a list of the public instance methods defined in <i>mod</i>.
2451 * If the optional parameter is <code>false</code>, the methods of
2452 * any ancestors are not included.
2453 */
2454
2455VALUE
2457{
2458 return class_instance_method_list(argc, argv, mod, 0, ins_methods_pub_i);
2459}
2460
2461/*
2462 * call-seq:
2463 * mod.undefined_instance_methods -> array
2464 *
2465 * Returns a list of the undefined instance methods defined in <i>mod</i>.
2466 * The undefined methods of any ancestors are not included.
2467 */
2468
2469VALUE
2470rb_class_undefined_instance_methods(VALUE mod)
2471{
2472 VALUE include_super = Qfalse;
2473 return class_instance_method_list(1, &include_super, mod, 0, ins_methods_undef_i);
2474}
2475
2476/*
2477 * call-seq:
2478 * obj.methods(regular=true) -> array
2479 *
2480 * Returns a list of the names of public and protected methods of
2481 * <i>obj</i>. This will include all the methods accessible in
2482 * <i>obj</i>'s ancestors.
2483 * If the optional parameter is <code>false</code>, it
2484 * returns an array of <i>obj</i>'s public and protected singleton methods,
2485 * the array will not include methods in modules included in <i>obj</i>.
2486 *
2487 * class Klass
2488 * def klass_method()
2489 * end
2490 * end
2491 * k = Klass.new
2492 * k.methods[0..9] #=> [:klass_method, :nil?, :===,
2493 * # :==~, :!, :eql?
2494 * # :hash, :<=>, :class, :singleton_class]
2495 * k.methods.length #=> 56
2496 *
2497 * k.methods(false) #=> []
2498 * def k.singleton_method; end
2499 * k.methods(false) #=> [:singleton_method]
2500 *
2501 * module M123; def m123; end end
2502 * k.extend M123
2503 * k.methods(false) #=> [:singleton_method]
2504 */
2505
2506VALUE
2507rb_obj_methods(int argc, const VALUE *argv, VALUE obj)
2508{
2509 rb_check_arity(argc, 0, 1);
2510 if (argc > 0 && !RTEST(argv[0])) {
2511 return rb_obj_singleton_methods(argc, argv, obj);
2512 }
2513 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_i);
2514}
2515
2516/*
2517 * call-seq:
2518 * obj.protected_methods(all=true) -> array
2519 *
2520 * Returns the list of protected methods accessible to <i>obj</i>. If
2521 * the <i>all</i> parameter is set to <code>false</code>, only those methods
2522 * in the receiver will be listed.
2523 */
2524
2525VALUE
2526rb_obj_protected_methods(int argc, const VALUE *argv, VALUE obj)
2527{
2528 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_prot_i);
2529}
2530
2531/*
2532 * call-seq:
2533 * obj.private_methods(all=true) -> array
2534 *
2535 * Returns the list of private methods accessible to <i>obj</i>. If
2536 * the <i>all</i> parameter is set to <code>false</code>, only those methods
2537 * in the receiver will be listed.
2538 */
2539
2540VALUE
2541rb_obj_private_methods(int argc, const VALUE *argv, VALUE obj)
2542{
2543 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_priv_i);
2544}
2545
2546/*
2547 * call-seq:
2548 * obj.public_methods(all=true) -> array
2549 *
2550 * Returns the list of public methods accessible to <i>obj</i>. If
2551 * the <i>all</i> parameter is set to <code>false</code>, only those methods
2552 * in the receiver will be listed.
2553 */
2554
2555VALUE
2556rb_obj_public_methods(int argc, const VALUE *argv, VALUE obj)
2557{
2558 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_pub_i);
2559}
2560
2561/*
2562 * call-seq:
2563 * obj.singleton_methods(all=true) -> array
2564 *
2565 * Returns an array of the names of singleton methods for <i>obj</i>.
2566 * If the optional <i>all</i> parameter is true, the list will include
2567 * methods in modules included in <i>obj</i>.
2568 * Only public and protected singleton methods are returned.
2569 *
2570 * module Other
2571 * def three() end
2572 * end
2573 *
2574 * class Single
2575 * def Single.four() end
2576 * end
2577 *
2578 * a = Single.new
2579 *
2580 * def a.one()
2581 * end
2582 *
2583 * class << a
2584 * include Other
2585 * def two()
2586 * end
2587 * end
2588 *
2589 * Single.singleton_methods #=> [:four]
2590 * a.singleton_methods(false) #=> [:two, :one]
2591 * a.singleton_methods #=> [:two, :one, :three]
2592 */
2593
2594VALUE
2595rb_obj_singleton_methods(int argc, const VALUE *argv, VALUE obj)
2596{
2597 VALUE ary, klass, origin;
2598 struct method_entry_arg me_arg;
2599 struct rb_id_table *mtbl;
2600 int recur = TRUE;
2601
2602 if (rb_check_arity(argc, 0, 1)) recur = RTEST(argv[0]);
2603 if (RCLASS_SINGLETON_P(obj)) {
2604 rb_singleton_class(obj);
2605 }
2606 klass = CLASS_OF(obj);
2607 origin = RCLASS_ORIGIN(klass);
2608 me_arg.list = st_init_numtable();
2609 me_arg.recur = recur;
2610 if (klass && RCLASS_SINGLETON_P(klass)) {
2611 if ((mtbl = RCLASS_M_TBL(origin)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2612 klass = RCLASS_SUPER(klass);
2613 }
2614 if (recur) {
2615 while (klass && (RCLASS_SINGLETON_P(klass) || RB_TYPE_P(klass, T_ICLASS))) {
2616 if (klass != origin && (mtbl = RCLASS_M_TBL(klass)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2617 klass = RCLASS_SUPER(klass);
2618 }
2619 }
2620 ary = rb_ary_new2(me_arg.list->num_entries);
2621 st_foreach(me_arg.list, ins_methods_i, ary);
2622 st_free_table(me_arg.list);
2623
2624 return ary;
2625}
2626
2634
2635#ifdef rb_define_method_id
2636#undef rb_define_method_id
2637#endif
2638void
2639rb_define_method_id(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc)
2640{
2641 rb_add_method_cfunc(klass, mid, func, argc, METHOD_VISI_PUBLIC);
2642}
2643
2644#ifdef rb_define_method
2645#undef rb_define_method
2646#endif
2647void
2648rb_define_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_PUBLIC);
2651}
2652
2653#ifdef rb_define_protected_method
2654#undef rb_define_protected_method
2655#endif
2656void
2657rb_define_protected_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
2658{
2659 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PROTECTED);
2660}
2661
2662#ifdef rb_define_private_method
2663#undef rb_define_private_method
2664#endif
2665void
2666rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
2667{
2668 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PRIVATE);
2669}
2670
2671void
2672rb_undef_method(VALUE klass, const char *name)
2673{
2674 rb_add_method(klass, rb_intern(name), VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2675}
2676
2677static enum rb_id_table_iterator_result
2678undef_method_i(ID name, VALUE value, void *data)
2679{
2680 VALUE klass = (VALUE)data;
2681 rb_add_method(klass, name, VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2682 return ID_TABLE_CONTINUE;
2683}
2684
2685void
2686rb_undef_methods_from(VALUE klass, VALUE super)
2687{
2688 struct rb_id_table *mtbl = RCLASS_M_TBL(super);
2689 if (mtbl) {
2690 rb_id_table_foreach(mtbl, undef_method_i, (void *)klass);
2691 }
2692}
2693
2701
2702static inline VALUE
2703special_singleton_class_of(VALUE obj)
2704{
2705 switch (obj) {
2706 case Qnil: return rb_cNilClass;
2707 case Qfalse: return rb_cFalseClass;
2708 case Qtrue: return rb_cTrueClass;
2709 default: return Qnil;
2710 }
2711}
2712
2713VALUE
2714rb_special_singleton_class(VALUE obj)
2715{
2716 return special_singleton_class_of(obj);
2717}
2718
2728static VALUE
2729singleton_class_of(VALUE obj, bool ensure_eigenclass)
2730{
2731 VALUE klass;
2732
2733 switch (TYPE(obj)) {
2734 case T_FIXNUM:
2735 case T_BIGNUM:
2736 case T_FLOAT:
2737 case T_SYMBOL:
2738 rb_raise(rb_eTypeError, "can't define singleton");
2739
2740 case T_FALSE:
2741 case T_TRUE:
2742 case T_NIL:
2743 klass = special_singleton_class_of(obj);
2744 if (NIL_P(klass))
2745 rb_bug("unknown immediate %p", (void *)obj);
2746 return klass;
2747
2748 case T_STRING:
2749 if (CHILLED_STRING_P(obj)) {
2750 CHILLED_STRING_MUTATED(obj);
2751 }
2752 else if (FL_TEST_RAW(obj, RSTRING_FSTR)) {
2753 rb_raise(rb_eTypeError, "can't define singleton");
2754 }
2755 }
2756
2757 bool needs_lock = rb_multi_ractor_p() && rb_ractor_shareable_p(obj);
2758 unsigned int lev;
2759 if (needs_lock) {
2760 RB_VM_LOCK_ENTER_LEV(&lev);
2761 }
2762 {
2763 klass = METACLASS_OF(obj);
2764 if (!(RCLASS_SINGLETON_P(klass) &&
2765 RCLASS_ATTACHED_OBJECT(klass) == obj)) {
2766 klass = rb_make_metaclass(obj, klass);
2767 }
2768 RB_FL_SET_RAW(klass, RB_OBJ_FROZEN_RAW(obj));
2769 if (ensure_eigenclass && RB_TYPE_P(obj, T_CLASS)) {
2770 /* ensures an exposed class belongs to its own eigenclass */
2771 (void)ENSURE_EIGENCLASS(klass);
2772 }
2773 }
2774 if (needs_lock) {
2775 RB_VM_LOCK_LEAVE_LEV(&lev);
2776 }
2777
2778 return klass;
2779}
2780
2781void
2783{
2784 /* should not propagate to meta-meta-class, and so on */
2785 if (!RCLASS_SINGLETON_P(x)) {
2786 VALUE klass = RBASIC_CLASS(x);
2787 if (klass && // no class when hidden from ObjectSpace
2788 FL_TEST_RAW(klass, FL_SINGLETON) &&
2789 !OBJ_FROZEN_RAW(klass)) {
2790 OBJ_FREEZE(klass);
2791 }
2792 }
2793}
2794
2802VALUE
2804{
2805 VALUE klass;
2806
2807 if (SPECIAL_CONST_P(obj)) {
2808 return rb_special_singleton_class(obj);
2809 }
2810 klass = METACLASS_OF(obj);
2811 if (!RCLASS_SINGLETON_P(klass)) return Qnil;
2812 if (RCLASS_ATTACHED_OBJECT(klass) != obj) return Qnil;
2813 return klass;
2814}
2815
2816VALUE
2818{
2819 return singleton_class_of(obj, true);
2820}
2821
2825
2830
2831#ifdef rb_define_singleton_method
2832#undef rb_define_singleton_method
2833#endif
2834void
2835rb_define_singleton_method(VALUE obj, const char *name, VALUE (*func)(ANYARGS), int argc)
2836{
2837 rb_define_method(singleton_class_of(obj, false), name, func, argc);
2838}
2839
2840#ifdef rb_define_module_function
2841#undef rb_define_module_function
2842#endif
2843void
2844rb_define_module_function(VALUE module, const char *name, VALUE (*func)(ANYARGS), int argc)
2845{
2846 rb_define_private_method(module, name, func, argc);
2847 rb_define_singleton_method(module, name, func, argc);
2848}
2849
2850#ifdef rb_define_global_function
2851#undef rb_define_global_function
2852#endif
2853void
2854rb_define_global_function(const char *name, VALUE (*func)(ANYARGS), int argc)
2855{
2856 rb_define_module_function(rb_mKernel, name, func, argc);
2857}
2858
2859void
2860rb_define_alias(VALUE klass, const char *name1, const char *name2)
2861{
2862 rb_alias(klass, rb_intern(name1), rb_intern(name2));
2863}
2864
2865void
2866rb_define_attr(VALUE klass, const char *name, int read, int write)
2867{
2868 rb_attr(klass, rb_intern(name), read, write, FALSE);
2869}
2870
2871VALUE
2872rb_keyword_error_new(const char *error, VALUE keys)
2873{
2874 long i = 0, len = RARRAY_LEN(keys);
2875 VALUE error_message = rb_sprintf("%s keyword%.*s", error, len > 1, "s");
2876
2877 if (len > 0) {
2878 rb_str_cat_cstr(error_message, ": ");
2879 while (1) {
2880 const VALUE k = RARRAY_AREF(keys, i);
2881 rb_str_append(error_message, rb_inspect(k));
2882 if (++i >= len) break;
2883 rb_str_cat_cstr(error_message, ", ");
2884 }
2885 }
2886
2887 return rb_exc_new_str(rb_eArgError, error_message);
2888}
2889
2890NORETURN(static void rb_keyword_error(const char *error, VALUE keys));
2891static void
2892rb_keyword_error(const char *error, VALUE keys)
2893{
2894 rb_exc_raise(rb_keyword_error_new(error, keys));
2895}
2896
2897NORETURN(static void unknown_keyword_error(VALUE hash, const ID *table, int keywords));
2898static void
2899unknown_keyword_error(VALUE hash, const ID *table, int keywords)
2900{
2901 int i;
2902 for (i = 0; i < keywords; i++) {
2903 st_data_t key = ID2SYM(table[i]);
2904 rb_hash_stlike_delete(hash, &key, NULL);
2905 }
2906 rb_keyword_error("unknown", rb_hash_keys(hash));
2907}
2908
2909
2910static int
2911separate_symbol(st_data_t key, st_data_t value, st_data_t arg)
2912{
2913 VALUE *kwdhash = (VALUE *)arg;
2914 if (!SYMBOL_P(key)) kwdhash++;
2915 if (!*kwdhash) *kwdhash = rb_hash_new();
2916 rb_hash_aset(*kwdhash, (VALUE)key, (VALUE)value);
2917 return ST_CONTINUE;
2918}
2919
2920VALUE
2922{
2923 VALUE parthash[2] = {0, 0};
2924 VALUE hash = *orighash;
2925
2926 if (RHASH_EMPTY_P(hash)) {
2927 *orighash = 0;
2928 return hash;
2929 }
2930 rb_hash_foreach(hash, separate_symbol, (st_data_t)&parthash);
2931 *orighash = parthash[1];
2932 if (parthash[1] && RBASIC_CLASS(hash) != rb_cHash) {
2933 RBASIC_SET_CLASS(parthash[1], RBASIC_CLASS(hash));
2934 }
2935 return parthash[0];
2936}
2937
2938int
2939rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
2940{
2941 int i = 0, j;
2942 int rest = 0;
2943 VALUE missing = Qnil;
2944 st_data_t key;
2945
2946#define extract_kwarg(keyword, val) \
2947 (key = (st_data_t)(keyword), values ? \
2948 (rb_hash_stlike_delete(keyword_hash, &key, &(val)) || ((val) = Qundef, 0)) : \
2949 rb_hash_stlike_lookup(keyword_hash, key, NULL))
2950
2951 if (NIL_P(keyword_hash)) keyword_hash = 0;
2952
2953 if (optional < 0) {
2954 rest = 1;
2955 optional = -1-optional;
2956 }
2957 if (required) {
2958 for (; i < required; i++) {
2959 VALUE keyword = ID2SYM(table[i]);
2960 if (keyword_hash) {
2961 if (extract_kwarg(keyword, values[i])) {
2962 continue;
2963 }
2964 }
2965 if (NIL_P(missing)) missing = rb_ary_hidden_new(1);
2966 rb_ary_push(missing, keyword);
2967 }
2968 if (!NIL_P(missing)) {
2969 rb_keyword_error("missing", missing);
2970 }
2971 }
2972 j = i;
2973 if (optional && keyword_hash) {
2974 for (i = 0; i < optional; i++) {
2975 if (extract_kwarg(ID2SYM(table[required+i]), values[required+i])) {
2976 j++;
2977 }
2978 }
2979 }
2980 if (!rest && keyword_hash) {
2981 if (RHASH_SIZE(keyword_hash) > (unsigned int)(values ? 0 : j)) {
2982 unknown_keyword_error(keyword_hash, table, required+optional);
2983 }
2984 }
2985 if (values && !keyword_hash) {
2986 for (i = 0; i < required + optional; i++) {
2987 values[i] = Qundef;
2988 }
2989 }
2990 return j;
2991#undef extract_kwarg
2992}
2993
2995 int kw_flag;
2996 int n_lead;
2997 int n_opt;
2998 int n_trail;
2999 bool f_var;
3000 bool f_hash;
3001 bool f_block;
3002};
3003
3004static void
3005rb_scan_args_parse(int kw_flag, const char *fmt, struct rb_scan_args_t *arg)
3006{
3007 const char *p = fmt;
3008
3009 memset(arg, 0, sizeof(*arg));
3010 arg->kw_flag = kw_flag;
3011
3012 if (ISDIGIT(*p)) {
3013 arg->n_lead = *p - '0';
3014 p++;
3015 if (ISDIGIT(*p)) {
3016 arg->n_opt = *p - '0';
3017 p++;
3018 }
3019 }
3020 if (*p == '*') {
3021 arg->f_var = 1;
3022 p++;
3023 }
3024 if (ISDIGIT(*p)) {
3025 arg->n_trail = *p - '0';
3026 p++;
3027 }
3028 if (*p == ':') {
3029 arg->f_hash = 1;
3030 p++;
3031 }
3032 if (*p == '&') {
3033 arg->f_block = 1;
3034 p++;
3035 }
3036 if (*p != '\0') {
3037 rb_fatal("bad scan arg format: %s", fmt);
3038 }
3039}
3040
3041static int
3042rb_scan_args_assign(const struct rb_scan_args_t *arg, int argc, const VALUE *const argv, va_list vargs)
3043{
3044 int i, argi = 0;
3045 VALUE *var, hash = Qnil;
3046#define rb_scan_args_next_param() va_arg(vargs, VALUE *)
3047 const int kw_flag = arg->kw_flag;
3048 const int n_lead = arg->n_lead;
3049 const int n_opt = arg->n_opt;
3050 const int n_trail = arg->n_trail;
3051 const int n_mand = n_lead + n_trail;
3052 const bool f_var = arg->f_var;
3053 const bool f_hash = arg->f_hash;
3054 const bool f_block = arg->f_block;
3055
3056 /* capture an option hash - phase 1: pop from the argv */
3057 if (f_hash && argc > 0) {
3058 VALUE last = argv[argc - 1];
3059 if (rb_scan_args_keyword_p(kw_flag, last)) {
3060 hash = rb_hash_dup(last);
3061 argc--;
3062 }
3063 }
3064
3065 if (argc < n_mand) {
3066 goto argc_error;
3067 }
3068
3069 /* capture leading mandatory arguments */
3070 for (i = 0; i < n_lead; i++) {
3071 var = rb_scan_args_next_param();
3072 if (var) *var = argv[argi];
3073 argi++;
3074 }
3075 /* capture optional arguments */
3076 for (i = 0; i < n_opt; i++) {
3077 var = rb_scan_args_next_param();
3078 if (argi < argc - n_trail) {
3079 if (var) *var = argv[argi];
3080 argi++;
3081 }
3082 else {
3083 if (var) *var = Qnil;
3084 }
3085 }
3086 /* capture variable length arguments */
3087 if (f_var) {
3088 int n_var = argc - argi - n_trail;
3089
3090 var = rb_scan_args_next_param();
3091 if (0 < n_var) {
3092 if (var) *var = rb_ary_new_from_values(n_var, &argv[argi]);
3093 argi += n_var;
3094 }
3095 else {
3096 if (var) *var = rb_ary_new();
3097 }
3098 }
3099 /* capture trailing mandatory arguments */
3100 for (i = 0; i < n_trail; i++) {
3101 var = rb_scan_args_next_param();
3102 if (var) *var = argv[argi];
3103 argi++;
3104 }
3105 /* capture an option hash - phase 2: assignment */
3106 if (f_hash) {
3107 var = rb_scan_args_next_param();
3108 if (var) *var = hash;
3109 }
3110 /* capture iterator block */
3111 if (f_block) {
3112 var = rb_scan_args_next_param();
3113 if (rb_block_given_p()) {
3114 *var = rb_block_proc();
3115 }
3116 else {
3117 *var = Qnil;
3118 }
3119 }
3120
3121 if (argi == argc) {
3122 return argc;
3123 }
3124
3125 argc_error:
3126 return -(argc + 1);
3127#undef rb_scan_args_next_param
3128}
3129
3130static int
3131rb_scan_args_result(const struct rb_scan_args_t *const arg, int argc)
3132{
3133 const int n_lead = arg->n_lead;
3134 const int n_opt = arg->n_opt;
3135 const int n_trail = arg->n_trail;
3136 const int n_mand = n_lead + n_trail;
3137 const bool f_var = arg->f_var;
3138
3139 if (argc >= 0) {
3140 return argc;
3141 }
3142
3143 argc = -argc - 1;
3144 rb_error_arity(argc, n_mand, f_var ? UNLIMITED_ARGUMENTS : n_mand + n_opt);
3146}
3147
3148#undef rb_scan_args
3149int
3150rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...)
3151{
3152 va_list vargs;
3153 struct rb_scan_args_t arg;
3154 rb_scan_args_parse(RB_SCAN_ARGS_PASS_CALLED_KEYWORDS, fmt, &arg);
3155 va_start(vargs,fmt);
3156 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
3157 va_end(vargs);
3158 return rb_scan_args_result(&arg, argc);
3159}
3160
3161#undef rb_scan_args_kw
3162int
3163rb_scan_args_kw(int kw_flag, int argc, const VALUE *argv, const char *fmt, ...)
3164{
3165 va_list vargs;
3166 struct rb_scan_args_t arg;
3167 rb_scan_args_parse(kw_flag, fmt, &arg);
3168 va_start(vargs,fmt);
3169 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
3170 va_end(vargs);
3171 return rb_scan_args_result(&arg, argc);
3172}
3173
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
#define ISDIGIT
@old{rb_isdigit}
Definition ctype.h:93
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_method_id(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
#define rb_define_protected_method(klass, mid, func, arity)
Defines klass#mid and makes it protected.
#define rb_define_module_function(klass, mid, func, arity)
Defines klass#mid and makes it a module function.
#define rb_define_private_method(klass, mid, func, arity)
Defines klass#mid and makes it private.
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition dllexport.h:45
VALUE rb_class_protected_instance_methods(int argc, const VALUE *argv, VALUE mod)
Identical to rb_class_instance_methods(), except it returns names of methods that are protected only.
Definition class.c:2418
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
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
Definition class.c:1691
VALUE rb_refinement_new(void)
Creates a new, anonymous refinement.
Definition class.c:1584
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition class.c:1484
VALUE rb_class_new(VALUE super)
Creates a new, anonymous class.
Definition class.c:852
static VALUE make_singleton_class(VALUE obj)
Creates a singleton class for obj.
Definition class.c:1304
VALUE rb_singleton_class_clone(VALUE obj)
Clones a singleton class.
Definition class.c:1142
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:1941
VALUE rb_class_subclasses(VALUE klass)
Queries the class's direct descendants.
Definition class.c:2198
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
Definition class.c:2817
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition class.c:1515
VALUE rb_class_attached_object(VALUE klass)
Returns the attached object for a singleton class.
Definition class.c:2221
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:2595
VALUE rb_module_new(void)
Creates a new, anonymous module.
Definition class.c:1578
#define META_CLASS_OF_CLASS_CLASS_P(k)
whether k is a meta^(n)-class of Class class
Definition class.c:1223
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:2403
void rb_check_inheritable(VALUE super)
Asserts that the given class can derive a child class.
Definition class.c:837
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:2456
VALUE rb_class_boot(VALUE super)
A utility function that wraps class_alloc.
Definition class.c:770
VALUE rb_define_module(const char *name)
Defines a top-level module.
Definition class.c:1597
void rb_class_modify_check(VALUE klass)
Asserts that klass is not a frozen class.
Definition eval.c:432
VALUE rb_define_module_id_under(VALUE outer, ID id)
Identical to rb_define_module_under(), except it takes the name in ID instead of C's string.
Definition class.c:1626
void rb_singleton_class_attached(VALUE klass, VALUE obj)
Attaches a singleton class to its corresponding object.
Definition class.c:1211
void rb_freeze_singleton_class(VALUE x)
This is an implementation detail of RB_OBJ_FREEZE().
Definition class.c:2782
VALUE rb_mod_included_modules(VALUE mod)
Queries the list of included modules.
Definition class.c:2018
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:1554
VALUE rb_mod_ancestors(VALUE mod)
Queries the module's ancestors.
Definition class.c:2086
static VALUE make_metaclass(VALUE klass)
Creates a metaclass of klass.
Definition class.c:1268
VALUE rb_class_inherited(VALUE super, VALUE klass)
Calls Class::inherited.
Definition class.c:1475
VALUE rb_mod_include_p(VALUE mod, VALUE mod2)
Queries if the passed module is included by the module.
Definition class.c:2054
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:2441
#define ENSURE_EIGENCLASS(klass)
ensures klass belongs to its own eigenclass.
Definition class.c:1254
VALUE rb_mod_init_copy(VALUE clone, VALUE orig)
The comment that comes with this function says :nodoc:.
Definition class.c:1018
VALUE rb_define_module_under(VALUE outer, const char *name)
Defines a module under the namespace of outer.
Definition class.c:1620
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:2803
VALUE rb_define_module_id(ID id)
This is a very badly designed API that creates an anonymous module.
Definition class.c:1591
VALUE rb_define_class_id(ID id, VALUE super)
This is a very badly designed API that creates an anonymous class.
Definition class.c:1454
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition class.c:2860
VALUE rb_extract_keywords(VALUE *orighash)
Splits a hash into two.
Definition class.c:2921
void rb_define_attr(VALUE klass, const char *name, int read, int write)
Defines public accessor method(s) for an attribute.
Definition class.c:2866
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
Definition class.c:2672
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:3163
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:3150
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition eval.c:1021
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
Definition class.c:2939
#define TYPE(_)
Old name of rb_type.
Definition value_type.h:108
#define FL_SINGLETON
Old name of RUBY_FL_SINGLETON.
Definition fl_type.h:58
#define OBJ_INIT_COPY(obj, orig)
Old name of RB_OBJ_INIT_COPY.
Definition object.h:41
#define ALLOC
Old name of RB_ALLOC.
Definition memory.h:400
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
#define xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
#define Qundef
Old name of RUBY_Qundef.
#define T_NIL
Old name of RUBY_T_NIL.
Definition value_type.h:72
#define T_FLOAT
Old name of RUBY_T_FLOAT.
Definition value_type.h:64
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
Definition value_type.h:57
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
Definition fl_type.h:134
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
Definition value_type.h:63
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
Definition assume.h:29
#define ZALLOC
Old name of RB_ZALLOC.
Definition memory.h:402
#define FL_SHAREABLE
Old name of RUBY_FL_SHAREABLE.
Definition fl_type.h:63
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:205
#define xmalloc
Old name of ruby_xmalloc.
Definition xmalloc.h:53
#define T_MODULE
Old name of RUBY_T_MODULE.
Definition value_type.h:70
#define T_TRUE
Old name of RUBY_T_TRUE.
Definition value_type.h:81
#define T_ICLASS
Old name of RUBY_T_ICLASS.
Definition value_type.h:66
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:131
#define FL_SET
Old name of RB_FL_SET.
Definition fl_type.h:128
#define T_FALSE
Old name of RUBY_T_FALSE.
Definition value_type.h:61
#define Qtrue
Old name of RUBY_Qtrue.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define NIL_P
Old name of RB_NIL_P.
#define FL_WB_PROTECTED
Old name of RUBY_FL_WB_PROTECTED.
Definition fl_type.h:59
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
Definition value_type.h:80
#define T_CLASS
Old name of RUBY_T_CLASS.
Definition value_type.h:58
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition value_type.h:85
#define FL_TEST
Old name of RB_FL_TEST.
Definition fl_type.h:130
#define CONST_ID
Old name of RUBY_CONST_ID.
Definition symbol.h:47
#define rb_ary_new2
Old name of rb_ary_new_capa.
Definition array.h:657
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
Definition fl_type.h:129
#define SYMBOL_P
Old name of RB_SYMBOL_P.
Definition value_type.h:88
#define OBJ_FROZEN_RAW
Old name of RB_OBJ_FROZEN_RAW.
Definition fl_type.h:137
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition eval.c:664
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1431
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Identical to rb_exc_new_cstr(), except it takes a Ruby's string instead of C's.
Definition error.c:1482
VALUE rb_cClass
Class class.
Definition object.c:63
VALUE rb_class_superclass(VALUE klass)
Queries the parent of the given class.
Definition object.c:2264
VALUE rb_mKernel
Kernel module.
Definition object.c:60
VALUE rb_cRefinement
Refinement class.
Definition object.c:64
VALUE rb_cNilClass
NilClass class.
Definition object.c:66
VALUE rb_cHash
Hash class.
Definition hash.c:109
VALUE rb_cFalseClass
FalseClass class.
Definition object.c:68
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:264
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
Definition object.c:686
VALUE rb_cBasicObject
BasicObject class.
Definition object.c:59
VALUE rb_cModule
Module class.
Definition object.c:62
VALUE rb_class_real(VALUE klass)
Finds a "real" class.
Definition object.c:255
VALUE rb_cTrueClass
TrueClass class.
Definition object.c:67
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
Definition gc.h:615
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:603
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1117
#define RGENGC_WB_PROTECTED_CLASS
This is a compile-time flag to enable/disable write barrier for struct RClass.
Definition gc.h:523
VALUE rb_ary_new_from_values(long n, const VALUE *elts)
Identical to rb_ary_new_from_args(), except how objects are passed.
VALUE rb_ary_cat(VALUE ary, const VALUE *train, long len)
Destructively appends multiple elements at the end of the array.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_resize(VALUE ary, long len)
Expands or shrinks the passed array to the passed length.
VALUE rb_ary_hidden_new(long capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
#define UNLIMITED_ARGUMENTS
This macro is used in conjunction with rb_check_arity().
Definition error.h:35
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
Definition proc.c:983
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
Definition string.c:3799
#define rb_str_cat_cstr(buf, str)
Identical to rb_str_cat(), except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1657
VALUE rb_const_get(VALUE space, ID name)
Identical to rb_const_defined(), except it returns the actual defined value.
Definition variable.c:3505
void rb_const_set(VALUE space, ID name, VALUE val)
Names a constant.
Definition variable.c:3983
VALUE rb_const_get_at(VALUE space, ID name)
Identical to rb_const_defined_at(), except it returns the actual defined value.
Definition variable.c:3511
void rb_set_class_path_string(VALUE klass, VALUE space, VALUE name)
Identical to rb_set_class_path(), except it accepts the name as Ruby's string instead of C's.
Definition variable.c:423
int rb_const_defined_at(VALUE space, ID name)
Identical to rb_const_defined(), except it doesn't look for parent classes.
Definition variable.c:3843
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
Definition variable.c:380
int rb_const_defined(VALUE space, ID name)
Queries if the constant is defined at the namespace.
Definition variable.c:3837
void rb_alias(VALUE klass, ID dst, ID src)
Resembles alias.
Definition vm_method.c:2766
void rb_attr(VALUE klass, ID name, int need_reader, int need_writer, int honour_visibility)
This function resembles now-deprecated Module#attr.
Definition vm_method.c:2346
void rb_clear_constant_cache_for_id(ID id)
Clears the inline constant caches associated with a particular ID.
Definition vm_method.c:340
int len
Length of the buffer.
Definition io.h:8
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
Definition memory.h:372
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:167
VALUE type(ANYARGS)
ANYARGS-ed function type.
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:51
#define RARRAY_AREF(a, i)
Definition rarray.h:403
#define RCLASS_SUPER
Just another name of rb_class_get_superclass.
Definition rclass.h:44
#define RHASH_SIZE(h)
Queries the size of the hash.
Definition rhash.h:69
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
Definition rhash.h:79
#define RB_SCAN_ARGS_PASS_CALLED_KEYWORDS
Same behaviour as rb_scan_args().
Definition scan_args.h:50
#define RTEST
This is an old name of RB_TEST.
#define ANYARGS
Functions declared using this macro take arbitrary arguments, including void.
Definition stdarg.h:64
Definition class.c:2283
Definition class.h:37
Definition st.h:79
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
ruby_value_type
C-level type of an object.
Definition value_type.h:113