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