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