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