Ruby: class.c Source File

Ruby 3.4.8p72 (2025-12-17 revision 995b59f66677d44767ce9faac6957e5543617ff9)

class.c
1/**********************************************************************
2
3 class.c -
4
5 $Author$
6 created at: Tue Aug 10 15:05:44 JST 1993
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9
10**********************************************************************/
11
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/class.h"
25#include "internal/eval.h"
26#include "internal/hash.h"
27#include "internal/object.h"
28#include "internal/string.h"
29#include "internal/variable.h"
30#include "ruby/st.h"
31#include "vm_core.h"
32#include "yjit.h"
33
34/* Flags of T_CLASS
35 *
36 * 0: RCLASS_IS_ROOT
37 * The class has been added to the VM roots. Will always be marked and pinned.
38 * This is done for classes defined from C to allow storing them in global variables.
39 * 1: RUBY_FL_SINGLETON
40 * This class is a singleton class.
41 * 2: RCLASS_SUPERCLASSES_INCLUDE_SELF
42 * The RCLASS_SUPERCLASSES contains the class as the last element.
43 * This means that this class owns the RCLASS_SUPERCLASSES list.
44 * if !SHAPE_IN_BASIC_FLAGS
45 * 4-19: SHAPE_FLAG_MASK
46 * Shape ID for the class.
47 * endif
48 */
49
50/* Flags of T_ICLASS
51 *
52 * 0: RICLASS_IS_ORIGIN
53 * 3: RICLASS_ORIGIN_SHARED_MTBL
54 * The T_ICLASS does not own the method table.
55 * if !SHAPE_IN_BASIC_FLAGS
56 * 4-19: SHAPE_FLAG_MASK
57 * Shape ID. This is set but not used.
58 * endif
59 */
60
61/* Flags of T_MODULE
62 *
63 * 0: RCLASS_IS_ROOT
64 * The class has been added to the VM roots. Will always be marked and pinned.
65 * This is done for classes defined from C to allow storing them in global variables.
66 * 1: RMODULE_ALLOCATED_BUT_NOT_INITIALIZED
67 * Module has not been initialized.
68 * 2: RCLASS_SUPERCLASSES_INCLUDE_SELF
69 * See RCLASS_SUPERCLASSES_INCLUDE_SELF in T_CLASS.
70 * 3: RMODULE_IS_REFINEMENT
71 * Module is used for refinements.
72 * if !SHAPE_IN_BASIC_FLAGS
73 * 4-19: SHAPE_FLAG_MASK
74 * Shape ID for the module.
75 * endif
76 */
77
78#define METACLASS_OF(k) RBASIC(k)->klass
79#define SET_METACLASS_OF(k, cls) RBASIC_SET_CLASS(k, cls)
80
81 RUBY_EXTERN rb_serial_t ruby_vm_global_cvar_state;
82
83static rb_subclass_entry_t *
84push_subclass_entry_to_list(VALUE super, VALUE klass)
85{
86 rb_subclass_entry_t *entry, *head;
87
88 entry = ZALLOC(rb_subclass_entry_t);
89 entry->klass = klass;
90
91 head = RCLASS_SUBCLASSES(super);
92 if (!head) {
93 head = ZALLOC(rb_subclass_entry_t);
94 RCLASS_SUBCLASSES(super) = head;
95 }
96 entry->next = head->next;
97 entry->prev = head;
98
99 if (head->next) {
100 head->next->prev = entry;
101 }
102 head->next = entry;
103
104 return entry;
105}
106
107void
108rb_class_subclass_add(VALUE super, VALUE klass)
109{
110 if (super && !UNDEF_P(super)) {
111 rb_subclass_entry_t *entry = push_subclass_entry_to_list(super, klass);
112 RCLASS_SUBCLASS_ENTRY(klass) = entry;
113 }
114}
115
116static void
117rb_module_add_to_subclasses_list(VALUE module, VALUE iclass)
118{
119 rb_subclass_entry_t *entry = push_subclass_entry_to_list(module, iclass);
120 RCLASS_MODULE_SUBCLASS_ENTRY(iclass) = entry;
121}
122
123void
124rb_class_remove_subclass_head(VALUE klass)
125{
126 rb_subclass_entry_t *head = RCLASS_SUBCLASSES(klass);
127
128 if (head) {
129 if (head->next) {
130 head->next->prev = NULL;
131 }
132 RCLASS_SUBCLASSES(klass) = NULL;
133 xfree(head);
134 }
135}
136
137void
138rb_class_remove_from_super_subclasses(VALUE klass)
139{
140 rb_subclass_entry_t *entry = RCLASS_SUBCLASS_ENTRY(klass);
141
142 if (entry) {
143 rb_subclass_entry_t *prev = entry->prev, *next = entry->next;
144
145 if (prev) {
146 prev->next = next;
147 }
148 if (next) {
149 next->prev = prev;
150 }
151
152 xfree(entry);
153 }
154
155 RCLASS_SUBCLASS_ENTRY(klass) = NULL;
156}
157
158void
159rb_class_remove_from_module_subclasses(VALUE klass)
160{
161 rb_subclass_entry_t *entry = RCLASS_MODULE_SUBCLASS_ENTRY(klass);
162
163 if (entry) {
164 rb_subclass_entry_t *prev = entry->prev, *next = entry->next;
165
166 if (prev) {
167 prev->next = next;
168 }
169 if (next) {
170 next->prev = prev;
171 }
172
173 xfree(entry);
174 }
175
176 RCLASS_MODULE_SUBCLASS_ENTRY(klass) = NULL;
177}
178
179void
180rb_class_foreach_subclass(VALUE klass, void (*f)(VALUE, VALUE), VALUE arg)
181{
182 // RCLASS_SUBCLASSES should always point to our head element which has NULL klass
183 rb_subclass_entry_t *cur = RCLASS_SUBCLASSES(klass);
184 // if we have a subclasses list, then the head is a placeholder with no valid
185 // class. So ignore it and use the next element in the list (if one exists)
186 if (cur) {
187 RUBY_ASSERT(!cur->klass);
188 cur = cur->next;
189 }
190
191 /* do not be tempted to simplify this loop into a for loop, the order of
192 operations is important here if `f` modifies the linked list */
193 while (cur) {
194 VALUE curklass = cur->klass;
195 cur = cur->next;
196 // do not trigger GC during f, otherwise the cur will become
197 // a dangling pointer if the subclass is collected
198 f(curklass, arg);
199 }
200}
201
202static void
203class_detach_subclasses(VALUE klass, VALUE arg)
204{
205 rb_class_remove_from_super_subclasses(klass);
206}
207
208void
209rb_class_detach_subclasses(VALUE klass)
210{
211 rb_class_foreach_subclass(klass, class_detach_subclasses, Qnil);
212}
213
214static void
215class_detach_module_subclasses(VALUE klass, VALUE arg)
216{
217 rb_class_remove_from_module_subclasses(klass);
218}
219
220void
221rb_class_detach_module_subclasses(VALUE klass)
222{
223 rb_class_foreach_subclass(klass, class_detach_module_subclasses, Qnil);
224}
225
238static VALUE
239class_alloc(VALUE flags, VALUE klass)
240{
241 size_t alloc_size = sizeof(struct RClass) + sizeof(rb_classext_t);
242
243 flags &= T_MASK;
244 if (RGENGC_WB_PROTECTED_CLASS) flags |= FL_WB_PROTECTED;
245 NEWOBJ_OF(obj, struct RClass, klass, flags, alloc_size, 0);
246
247 memset(RCLASS_EXT(obj), 0, sizeof(rb_classext_t));
248
249 /* ZALLOC
250 RCLASS_CONST_TBL(obj) = 0;
251 RCLASS_M_TBL(obj) = 0;
252 RCLASS_IV_INDEX_TBL(obj) = 0;
253 RCLASS_SET_SUPER((VALUE)obj, 0);
254 RCLASS_SUBCLASSES(obj) = NULL;
255 RCLASS_PARENT_SUBCLASSES(obj) = NULL;
256 RCLASS_MODULE_SUBCLASSES(obj) = NULL;
257 */
258 RCLASS_SET_ORIGIN((VALUE)obj, (VALUE)obj);
259 RB_OBJ_WRITE(obj, &RCLASS_REFINED_CLASS(obj), Qnil);
260 RCLASS_SET_ALLOCATOR((VALUE)obj, 0);
261
262 return (VALUE)obj;
263}
264
265static void
266RCLASS_M_TBL_INIT(VALUE c)
267{
268 RCLASS_M_TBL(c) = rb_id_table_create(0);
269}
270
280 VALUE
281rb_class_boot(VALUE super)
282{
283 VALUE klass = class_alloc(T_CLASS, rb_cClass);
284
285 RCLASS_SET_SUPER(klass, super);
286 RCLASS_M_TBL_INIT(klass);
287
288 return (VALUE)klass;
289}
290
291static VALUE *
292class_superclasses_including_self(VALUE klass)
293{
294 if (FL_TEST_RAW(klass, RCLASS_SUPERCLASSES_INCLUDE_SELF))
295 return RCLASS_SUPERCLASSES(klass);
296
297 size_t depth = RCLASS_SUPERCLASS_DEPTH(klass);
298 VALUE *superclasses = xmalloc(sizeof(VALUE) * (depth + 1));
299 if (depth > 0)
300 memcpy(superclasses, RCLASS_SUPERCLASSES(klass), sizeof(VALUE) * depth);
301 superclasses[depth] = klass;
302
303 RCLASS_SUPERCLASSES(klass) = superclasses;
304 FL_SET_RAW(klass, RCLASS_SUPERCLASSES_INCLUDE_SELF);
305 return superclasses;
306}
307
308void
309rb_class_update_superclasses(VALUE klass)
310{
311 VALUE super = RCLASS_SUPER(klass);
312
313 if (!RB_TYPE_P(klass, T_CLASS)) return;
314 if (UNDEF_P(super)) return;
315
316 // If the superclass array is already built
317 if (RCLASS_SUPERCLASSES(klass))
318 return;
319
320 // find the proper superclass
321 while (super != Qfalse && !RB_TYPE_P(super, T_CLASS)) {
322 super = RCLASS_SUPER(super);
323 }
324
325 // For BasicObject and uninitialized classes, depth=0 and ary=NULL
326 if (super == Qfalse)
327 return;
328
329 // Sometimes superclasses are set before the full ancestry tree is built
330 // This happens during metaclass construction
331 if (super != rb_cBasicObject && !RCLASS_SUPERCLASS_DEPTH(super)) {
332 rb_class_update_superclasses(super);
333
334 // If it is still unset we need to try later
335 if (!RCLASS_SUPERCLASS_DEPTH(super))
336 return;
337 }
338
339 RCLASS_SUPERCLASSES(klass) = class_superclasses_including_self(super);
340 RCLASS_SUPERCLASS_DEPTH(klass) = RCLASS_SUPERCLASS_DEPTH(super) + 1;
341}
342
343void
344rb_check_inheritable(VALUE super)
345{
346 if (!RB_TYPE_P(super, T_CLASS)) {
347 rb_raise(rb_eTypeError, "superclass must be an instance of Class (given an instance of %"PRIsVALUE")",
348 rb_obj_class(super));
349 }
350 if (RCLASS_SINGLETON_P(super)) {
351 rb_raise(rb_eTypeError, "can't make subclass of singleton class");
352 }
353 if (super == rb_cClass) {
354 rb_raise(rb_eTypeError, "can't make subclass of Class");
355 }
356}
357
358 VALUE
359rb_class_new(VALUE super)
360{
361 Check_Type(super, T_CLASS);
362 rb_check_inheritable(super);
363 VALUE klass = rb_class_boot(super);
364
365 if (super != rb_cObject && super != rb_cBasicObject) {
366 RCLASS_EXT(klass)->max_iv_count = RCLASS_EXT(super)->max_iv_count;
367 }
368
369 return klass;
370}
371
372 VALUE
373rb_class_s_alloc(VALUE klass)
374{
375 return rb_class_boot(0);
376}
377
378static void
379clone_method(VALUE old_klass, VALUE new_klass, ID mid, const rb_method_entry_t *me)
380{
381 if (me->def->type == VM_METHOD_TYPE_ISEQ) {
382 rb_cref_t *new_cref;
383 rb_vm_rewrite_cref(me->def->body.iseq.cref, old_klass, new_klass, &new_cref);
384 rb_add_method_iseq(new_klass, mid, me->def->body.iseq.iseqptr, new_cref, METHOD_ENTRY_VISI(me));
385 }
386 else {
387 rb_method_entry_set(new_klass, mid, me, METHOD_ENTRY_VISI(me));
388 }
389}
390
391 struct clone_method_arg {
392 VALUE new_klass;
393 VALUE old_klass;
394};
395
396static enum rb_id_table_iterator_result
397clone_method_i(ID key, VALUE value, void *data)
398{
399 const struct clone_method_arg *arg = (struct clone_method_arg *)data;
400 clone_method(arg->old_klass, arg->new_klass, key, (const rb_method_entry_t *)value);
401 return ID_TABLE_CONTINUE;
402}
403
404 struct clone_const_arg {
405 VALUE klass;
406 struct rb_id_table *tbl;
407};
408
409static int
410clone_const(ID key, const rb_const_entry_t *ce, struct clone_const_arg *arg)
411{
412 rb_const_entry_t *nce = ALLOC(rb_const_entry_t);
413 MEMCPY(nce, ce, rb_const_entry_t, 1);
414 RB_OBJ_WRITTEN(arg->klass, Qundef, ce->value);
415 RB_OBJ_WRITTEN(arg->klass, Qundef, ce->file);
416
417 rb_id_table_insert(arg->tbl, key, (VALUE)nce);
418 return ID_TABLE_CONTINUE;
419}
420
421static enum rb_id_table_iterator_result
422clone_const_i(ID key, VALUE value, void *data)
423{
424 return clone_const(key, (const rb_const_entry_t *)value, data);
425}
426
427static void
428class_init_copy_check(VALUE clone, VALUE orig)
429{
430 if (orig == rb_cBasicObject) {
431 rb_raise(rb_eTypeError, "can't copy the root class");
432 }
433 if (RCLASS_SUPER(clone) != 0 || clone == rb_cBasicObject) {
434 rb_raise(rb_eTypeError, "already initialized class");
435 }
436 if (RCLASS_SINGLETON_P(orig)) {
437 rb_raise(rb_eTypeError, "can't copy singleton class");
438 }
439}
440
441 struct cvc_table_copy_ctx {
442 VALUE clone;
443 struct rb_id_table * new_table;
444};
445
446static enum rb_id_table_iterator_result
447cvc_table_copy(ID id, VALUE val, void *data)
448{
449 struct cvc_table_copy_ctx *ctx = (struct cvc_table_copy_ctx *)data;
450 struct rb_cvar_class_tbl_entry * orig_entry;
451 orig_entry = (struct rb_cvar_class_tbl_entry *)val;
452
453 struct rb_cvar_class_tbl_entry *ent;
454
455 ent = ALLOC(struct rb_cvar_class_tbl_entry);
456 ent->class_value = ctx->clone;
457 ent->cref = orig_entry->cref;
458 ent->global_cvar_state = orig_entry->global_cvar_state;
459 rb_id_table_insert(ctx->new_table, id, (VALUE)ent);
460
461 RB_OBJ_WRITTEN(ctx->clone, Qundef, ent->cref);
462
463 return ID_TABLE_CONTINUE;
464}
465
466static void
467copy_tables(VALUE clone, VALUE orig)
468{
469 if (RCLASS_CONST_TBL(clone)) {
470 rb_free_const_table(RCLASS_CONST_TBL(clone));
471 RCLASS_CONST_TBL(clone) = 0;
472 }
473 if (RCLASS_CVC_TBL(orig)) {
474 struct rb_id_table *rb_cvc_tbl = RCLASS_CVC_TBL(orig);
475 struct rb_id_table *rb_cvc_tbl_dup = rb_id_table_create(rb_id_table_size(rb_cvc_tbl));
476
477 struct cvc_table_copy_ctx ctx;
478 ctx.clone = clone;
479 ctx.new_table = rb_cvc_tbl_dup;
480 rb_id_table_foreach(rb_cvc_tbl, cvc_table_copy, &ctx);
481 RCLASS_CVC_TBL(clone) = rb_cvc_tbl_dup;
482 }
483 rb_id_table_free(RCLASS_M_TBL(clone));
484 RCLASS_M_TBL(clone) = 0;
485 if (!RB_TYPE_P(clone, T_ICLASS)) {
486 st_data_t id;
487
488 rb_iv_tbl_copy(clone, orig);
489 CONST_ID(id, "__tmp_classpath__");
490 rb_attr_delete(clone, id);
491 CONST_ID(id, "__classpath__");
492 rb_attr_delete(clone, id);
493 }
494 if (RCLASS_CONST_TBL(orig)) {
495 struct clone_const_arg arg;
496
497 arg.tbl = RCLASS_CONST_TBL(clone) = rb_id_table_create(0);
498 arg.klass = clone;
499 rb_id_table_foreach(RCLASS_CONST_TBL(orig), clone_const_i, &arg);
500 }
501}
502
503static bool ensure_origin(VALUE klass);
504
508enum {RMODULE_ALLOCATED_BUT_NOT_INITIALIZED = RUBY_FL_USER1};
509
510static inline bool
511RMODULE_UNINITIALIZED(VALUE module)
512{
513 return FL_TEST_RAW(module, RMODULE_ALLOCATED_BUT_NOT_INITIALIZED);
514}
515
516void
517rb_module_set_initialized(VALUE mod)
518{
519 FL_UNSET_RAW(mod, RMODULE_ALLOCATED_BUT_NOT_INITIALIZED);
520 /* no more re-initialization */
521}
522
523void
524rb_module_check_initializable(VALUE mod)
525{
526 if (!RMODULE_UNINITIALIZED(mod)) {
527 rb_raise(rb_eTypeError, "already initialized module");
528 }
529}
530
531/* :nodoc: */
532 VALUE
533rb_mod_init_copy(VALUE clone, VALUE orig)
534{
535 switch (BUILTIN_TYPE(clone)) {
536 case T_CLASS:
537 case T_ICLASS:
538 class_init_copy_check(clone, orig);
539 break;
540 case T_MODULE:
541 rb_module_check_initializable(clone);
542 break;
543 default:
544 break;
545 }
546 if (!OBJ_INIT_COPY(clone, orig)) return clone;
547
548 /* cloned flag is refer at constant inline cache
549 * see vm_get_const_key_cref() in vm_insnhelper.c
550 */
551 RCLASS_EXT(clone)->cloned = true;
552 RCLASS_EXT(orig)->cloned = true;
553
554 if (!RCLASS_SINGLETON_P(CLASS_OF(clone))) {
555 RBASIC_SET_CLASS(clone, rb_singleton_class_clone(orig));
556 rb_singleton_class_attached(METACLASS_OF(clone), (VALUE)clone);
557 }
558 RCLASS_SET_ALLOCATOR(clone, RCLASS_ALLOCATOR(orig));
559 copy_tables(clone, orig);
560 if (RCLASS_M_TBL(orig)) {
561 struct clone_method_arg arg;
562 arg.old_klass = orig;
563 arg.new_klass = clone;
564 RCLASS_M_TBL_INIT(clone);
565 rb_id_table_foreach(RCLASS_M_TBL(orig), clone_method_i, &arg);
566 }
567
568 if (RCLASS_ORIGIN(orig) == orig) {
569 RCLASS_SET_SUPER(clone, RCLASS_SUPER(orig));
570 }
571 else {
572 VALUE p = RCLASS_SUPER(orig);
573 VALUE orig_origin = RCLASS_ORIGIN(orig);
574 VALUE prev_clone_p = clone;
575 VALUE origin_stack = rb_ary_hidden_new(2);
576 VALUE origin[2];
577 VALUE clone_p = 0;
578 long origin_len;
579 int add_subclass;
580 VALUE clone_origin;
581
582 ensure_origin(clone);
583 clone_origin = RCLASS_ORIGIN(clone);
584
585 while (p && p != orig_origin) {
586 if (BUILTIN_TYPE(p) != T_ICLASS) {
587 rb_bug("non iclass between module/class and origin");
588 }
589 clone_p = class_alloc(RBASIC(p)->flags, METACLASS_OF(p));
590 /* We should set the m_tbl right after allocation before anything
591 * that can trigger GC to avoid clone_p from becoming old and
592 * needing to fire write barriers. */
593 RCLASS_SET_M_TBL(clone_p, RCLASS_M_TBL(p));
594 RCLASS_SET_SUPER(prev_clone_p, clone_p);
595 prev_clone_p = clone_p;
596 RCLASS_CONST_TBL(clone_p) = RCLASS_CONST_TBL(p);
597 RCLASS_SET_ALLOCATOR(clone_p, RCLASS_ALLOCATOR(p));
598 if (RB_TYPE_P(clone, T_CLASS)) {
599 RCLASS_SET_INCLUDER(clone_p, clone);
600 }
601 add_subclass = TRUE;
602 if (p != RCLASS_ORIGIN(p)) {
603 origin[0] = clone_p;
604 origin[1] = RCLASS_ORIGIN(p);
605 rb_ary_cat(origin_stack, origin, 2);
606 }
607 else if ((origin_len = RARRAY_LEN(origin_stack)) > 1 &&
608 RARRAY_AREF(origin_stack, origin_len - 1) == p) {
609 RCLASS_SET_ORIGIN(RARRAY_AREF(origin_stack, (origin_len -= 2)), clone_p);
610 RICLASS_SET_ORIGIN_SHARED_MTBL(clone_p);
611 rb_ary_resize(origin_stack, origin_len);
612 add_subclass = FALSE;
613 }
614 if (add_subclass) {
615 rb_module_add_to_subclasses_list(METACLASS_OF(p), clone_p);
616 }
617 p = RCLASS_SUPER(p);
618 }
619
620 if (p == orig_origin) {
621 if (clone_p) {
622 RCLASS_SET_SUPER(clone_p, clone_origin);
623 RCLASS_SET_SUPER(clone_origin, RCLASS_SUPER(orig_origin));
624 }
625 copy_tables(clone_origin, orig_origin);
626 if (RCLASS_M_TBL(orig_origin)) {
627 struct clone_method_arg arg;
628 arg.old_klass = orig;
629 arg.new_klass = clone;
630 RCLASS_M_TBL_INIT(clone_origin);
631 rb_id_table_foreach(RCLASS_M_TBL(orig_origin), clone_method_i, &arg);
632 }
633 }
634 else {
635 rb_bug("no origin for class that has origin");
636 }
637
638 rb_class_update_superclasses(clone);
639 }
640
641 return clone;
642}
643
644 VALUE
645rb_singleton_class_clone(VALUE obj)
646{
647 return rb_singleton_class_clone_and_attach(obj, Qundef);
648}
649
650// Clone and return the singleton class of `obj` if it has been created and is attached to `obj`.
651 VALUE
652rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach)
653{
654 const VALUE klass = METACLASS_OF(obj);
655
656 // Note that `rb_singleton_class()` can create situations where `klass` is
657 // attached to an object other than `obj`. In which case `obj` does not have
658 // a material singleton class attached yet and there is no singleton class
659 // to clone.
660 if (!(RCLASS_SINGLETON_P(klass) && RCLASS_ATTACHED_OBJECT(klass) == obj)) {
661 // nothing to clone
662 return klass;
663 }
664 else {
665 /* copy singleton(unnamed) class */
666 bool klass_of_clone_is_new;
667 VALUE clone = class_alloc(RBASIC(klass)->flags, 0);
668
669 if (BUILTIN_TYPE(obj) == T_CLASS) {
670 klass_of_clone_is_new = true;
671 RBASIC_SET_CLASS(clone, clone);
672 }
673 else {
674 VALUE klass_metaclass_clone = rb_singleton_class_clone(klass);
675 // When `METACLASS_OF(klass) == klass_metaclass_clone`, it means the
676 // recursive call did not clone `METACLASS_OF(klass)`.
677 klass_of_clone_is_new = (METACLASS_OF(klass) != klass_metaclass_clone);
678 RBASIC_SET_CLASS(clone, klass_metaclass_clone);
679 }
680
681 RCLASS_SET_SUPER(clone, RCLASS_SUPER(klass));
682 rb_iv_tbl_copy(clone, klass);
683 if (RCLASS_CONST_TBL(klass)) {
684 struct clone_const_arg arg;
685 arg.tbl = RCLASS_CONST_TBL(clone) = rb_id_table_create(0);
686 arg.klass = clone;
687 rb_id_table_foreach(RCLASS_CONST_TBL(klass), clone_const_i, &arg);
688 }
689 if (!UNDEF_P(attach)) {
690 rb_singleton_class_attached(clone, attach);
691 }
692 RCLASS_M_TBL_INIT(clone);
693 {
694 struct clone_method_arg arg;
695 arg.old_klass = klass;
696 arg.new_klass = clone;
697 rb_id_table_foreach(RCLASS_M_TBL(klass), clone_method_i, &arg);
698 }
699 if (klass_of_clone_is_new) {
700 rb_singleton_class_attached(METACLASS_OF(clone), clone);
701 }
702 FL_SET(clone, FL_SINGLETON);
703
704 return clone;
705 }
706}
707
708void
709rb_singleton_class_attached(VALUE klass, VALUE obj)
710{
711 if (RCLASS_SINGLETON_P(klass)) {
712 RCLASS_SET_ATTACHED_OBJECT(klass, obj);
713 }
714}
715
721#define META_CLASS_OF_CLASS_CLASS_P(k) (METACLASS_OF(k) == (k))
722
723static int
724rb_singleton_class_has_metaclass_p(VALUE sklass)
725{
726 return RCLASS_ATTACHED_OBJECT(METACLASS_OF(sklass)) == sklass;
727}
728
729int
730rb_singleton_class_internal_p(VALUE sklass)
731{
732 return (RB_TYPE_P(RCLASS_ATTACHED_OBJECT(sklass), T_CLASS) &&
733 !rb_singleton_class_has_metaclass_p(sklass));
734}
735
741#define HAVE_METACLASS_P(k) \
742 (FL_TEST(METACLASS_OF(k), FL_SINGLETON) && \
743 rb_singleton_class_has_metaclass_p(k))
744
752#define ENSURE_EIGENCLASS(klass) \
753 (HAVE_METACLASS_P(klass) ? METACLASS_OF(klass) : make_metaclass(klass))
754
755
765static inline VALUE
766make_metaclass(VALUE klass)
767{
768 VALUE super;
769 VALUE metaclass = rb_class_boot(Qundef);
770
771 FL_SET(metaclass, FL_SINGLETON);
772 rb_singleton_class_attached(metaclass, klass);
773
774 if (META_CLASS_OF_CLASS_CLASS_P(klass)) {
775 SET_METACLASS_OF(klass, metaclass);
776 SET_METACLASS_OF(metaclass, metaclass);
777 }
778 else {
779 VALUE tmp = METACLASS_OF(klass); /* for a meta^(n)-class klass, tmp is meta^(n)-class of Class class */
780 SET_METACLASS_OF(klass, metaclass);
781 SET_METACLASS_OF(metaclass, ENSURE_EIGENCLASS(tmp));
782 }
783
784 super = RCLASS_SUPER(klass);
785 while (RB_TYPE_P(super, T_ICLASS)) super = RCLASS_SUPER(super);
786 RCLASS_SET_SUPER(metaclass, super ? ENSURE_EIGENCLASS(super) : rb_cClass);
787
788 // Full class ancestry may not have been filled until we reach here.
789 rb_class_update_superclasses(METACLASS_OF(metaclass));
790
791 return metaclass;
792}
793
800static inline VALUE
801make_singleton_class(VALUE obj)
802{
803 VALUE orig_class = METACLASS_OF(obj);
804 VALUE klass = rb_class_boot(orig_class);
805
806 FL_SET(klass, FL_SINGLETON);
807 RBASIC_SET_CLASS(obj, klass);
808 rb_singleton_class_attached(klass, obj);
809 rb_yjit_invalidate_no_singleton_class(orig_class);
810
811 SET_METACLASS_OF(klass, METACLASS_OF(rb_class_real(orig_class)));
812 return klass;
813}
814
815
816static VALUE
817boot_defclass(const char *name, VALUE super)
818{
819 VALUE obj = rb_class_boot(super);
820 ID id = rb_intern(name);
821
822 rb_const_set((rb_cObject ? rb_cObject : obj), id, obj);
823 rb_vm_register_global_object(obj);
824 return obj;
825}
826
827/***********************************************************************
828 *
829 * Document-class: Refinement
830 *
831 * Refinement is a class of the +self+ (current context) inside +refine+
832 * statement. It allows to import methods from other modules, see #import_methods.
833 */
834
835#if 0 /* for RDoc */
836/*
837 * Document-method: Refinement#import_methods
838 *
839 * call-seq:
840 * import_methods(module, ...) -> self
841 *
842 * Imports methods from modules. Unlike Module#include,
843 * Refinement#import_methods copies methods and adds them into the refinement,
844 * so the refinement is activated in the imported methods.
845 *
846 * Note that due to method copying, only methods defined in Ruby code can be imported.
847 *
848 * module StrUtils
849 * def indent(level)
850 * ' ' * level + self
851 * end
852 * end
853 *
854 * module M
855 * refine String do
856 * import_methods StrUtils
857 * end
858 * end
859 *
860 * using M
861 * "foo".indent(3)
862 * #=> " foo"
863 *
864 * module M
865 * refine String do
866 * import_methods Enumerable
867 * # Can't import method which is not defined with Ruby code: Enumerable#drop
868 * end
869 * end
870 *
871 */
872
873static VALUE
874refinement_import_methods(int argc, VALUE *argv, VALUE refinement)
875{
876}
877# endif
878
898void
899Init_class_hierarchy(void)
900{
901 rb_cBasicObject = boot_defclass("BasicObject", 0);
902 rb_cObject = boot_defclass("Object", rb_cBasicObject);
903 rb_vm_register_global_object(rb_cObject);
904
905 /* resolve class name ASAP for order-independence */
906 rb_set_class_path_string(rb_cObject, rb_cObject, rb_fstring_lit("Object"));
907
908 rb_cModule = boot_defclass("Module", rb_cObject);
909 rb_cClass = boot_defclass("Class", rb_cModule);
910 rb_cRefinement = boot_defclass("Refinement", rb_cModule);
911
912#if 0 /* for RDoc */
913 // we pretend it to be public, otherwise RDoc will ignore it
914 rb_define_method(rb_cRefinement, "import_methods", refinement_import_methods, -1);
915#endif
916
917 rb_const_set(rb_cObject, rb_intern_const("BasicObject"), rb_cBasicObject);
918 RBASIC_SET_CLASS(rb_cClass, rb_cClass);
919 RBASIC_SET_CLASS(rb_cModule, rb_cClass);
920 RBASIC_SET_CLASS(rb_cObject, rb_cClass);
921 RBASIC_SET_CLASS(rb_cRefinement, rb_cClass);
922 RBASIC_SET_CLASS(rb_cBasicObject, rb_cClass);
923
924 ENSURE_EIGENCLASS(rb_cRefinement);
925}
926
927
938 VALUE
939rb_make_metaclass(VALUE obj, VALUE unused)
940{
941 if (BUILTIN_TYPE(obj) == T_CLASS) {
942 return make_metaclass(obj);
943 }
944 else {
945 return make_singleton_class(obj);
946 }
947}
948
949 VALUE
950rb_define_class_id(ID id, VALUE super)
951{
952 VALUE klass;
953
954 if (!super) super = rb_cObject;
955 klass = rb_class_new(super);
956 rb_make_metaclass(klass, METACLASS_OF(super));
957
958 return klass;
959}
960
961
970 VALUE
971rb_class_inherited(VALUE super, VALUE klass)
972{
973 ID inherited;
974 if (!super) super = rb_cObject;
975 CONST_ID(inherited, "inherited");
976 return rb_funcall(super, inherited, 1, klass);
977}
978
979 VALUE
980rb_define_class(const char *name, VALUE super)
981{
982 VALUE klass;
983 ID id;
984
985 id = rb_intern(name);
986 if (rb_const_defined(rb_cObject, id)) {
987 klass = rb_const_get(rb_cObject, id);
988 if (!RB_TYPE_P(klass, T_CLASS)) {
989 rb_raise(rb_eTypeError, "%s is not a class (%"PRIsVALUE")",
990 name, rb_obj_class(klass));
991 }
992 if (rb_class_real(RCLASS_SUPER(klass)) != super) {
993 rb_raise(rb_eTypeError, "superclass mismatch for class %s", name);
994 }
995
996 /* Class may have been defined in Ruby and not pin-rooted */
997 rb_vm_register_global_object(klass);
998 return klass;
999 }
1000 if (!super) {
1001 rb_raise(rb_eArgError, "no super class for '%s'", name);
1002 }
1003 klass = rb_define_class_id(id, super);
1004 rb_vm_register_global_object(klass);
1005 rb_const_set(rb_cObject, id, klass);
1006 rb_class_inherited(super, klass);
1007
1008 return klass;
1009}
1010
1011 VALUE
1012rb_define_class_under(VALUE outer, const char *name, VALUE super)
1013{
1014 return rb_define_class_id_under(outer, rb_intern(name), super);
1015}
1016
1017 VALUE
1018rb_define_class_id_under_no_pin(VALUE outer, ID id, VALUE super)
1019{
1020 VALUE klass;
1021
1022 if (rb_const_defined_at(outer, id)) {
1023 klass = rb_const_get_at(outer, id);
1024 if (!RB_TYPE_P(klass, T_CLASS)) {
1025 rb_raise(rb_eTypeError, "%"PRIsVALUE"::%"PRIsVALUE" is not a class"
1026 " (%"PRIsVALUE")",
1027 outer, rb_id2str(id), rb_obj_class(klass));
1028 }
1029 if (rb_class_real(RCLASS_SUPER(klass)) != super) {
1030 rb_raise(rb_eTypeError, "superclass mismatch for class "
1031 "%"PRIsVALUE"::%"PRIsVALUE""
1032 " (%"PRIsVALUE" is given but was %"PRIsVALUE")",
1033 outer, rb_id2str(id), RCLASS_SUPER(klass), super);
1034 }
1035
1036 return klass;
1037 }
1038 if (!super) {
1039 rb_raise(rb_eArgError, "no super class for '%"PRIsVALUE"::%"PRIsVALUE"'",
1040 rb_class_path(outer), rb_id2str(id));
1041 }
1042 klass = rb_define_class_id(id, super);
1043 rb_set_class_path_string(klass, outer, rb_id2str(id));
1044 rb_const_set(outer, id, klass);
1045 rb_class_inherited(super, klass);
1046
1047 return klass;
1048}
1049
1050 VALUE
1051rb_define_class_id_under(VALUE outer, ID id, VALUE super)
1052{
1053 VALUE klass = rb_define_class_id_under_no_pin(outer, id, super);
1054 rb_vm_register_global_object(klass);
1055 return klass;
1056}
1057
1058 VALUE
1059rb_module_s_alloc(VALUE klass)
1060{
1061 VALUE mod = class_alloc(T_MODULE, klass);
1062 RCLASS_M_TBL_INIT(mod);
1063 FL_SET(mod, RMODULE_ALLOCATED_BUT_NOT_INITIALIZED);
1064 return mod;
1065}
1066
1067static inline VALUE
1068module_new(VALUE klass)
1069{
1070 VALUE mdl = class_alloc(T_MODULE, klass);
1071 RCLASS_M_TBL_INIT(mdl);
1072 return (VALUE)mdl;
1073}
1074
1075 VALUE
1076rb_module_new(void)
1077{
1078 return module_new(rb_cModule);
1079}
1080
1081 VALUE
1082rb_refinement_new(void)
1083{
1084 return module_new(rb_cRefinement);
1085}
1086
1087// Kept for compatibility. Use rb_module_new() instead.
1088 VALUE
1089rb_define_module_id(ID id)
1090{
1091 return rb_module_new();
1092}
1093
1094 VALUE
1095rb_define_module(const char *name)
1096{
1097 VALUE module;
1098 ID id;
1099
1100 id = rb_intern(name);
1101 if (rb_const_defined(rb_cObject, id)) {
1102 module = rb_const_get(rb_cObject, id);
1103 if (!RB_TYPE_P(module, T_MODULE)) {
1104 rb_raise(rb_eTypeError, "%s is not a module (%"PRIsVALUE")",
1105 name, rb_obj_class(module));
1106 }
1107 /* Module may have been defined in Ruby and not pin-rooted */
1108 rb_vm_register_global_object(module);
1109 return module;
1110 }
1111 module = rb_module_new();
1112 rb_vm_register_global_object(module);
1113 rb_const_set(rb_cObject, id, module);
1114
1115 return module;
1116}
1117
1118 VALUE
1119rb_define_module_under(VALUE outer, const char *name)
1120{
1121 return rb_define_module_id_under(outer, rb_intern(name));
1122}
1123
1124 VALUE
1125rb_define_module_id_under(VALUE outer, ID id)
1126{
1127 VALUE module;
1128
1129 if (rb_const_defined_at(outer, id)) {
1130 module = rb_const_get_at(outer, id);
1131 if (!RB_TYPE_P(module, T_MODULE)) {
1132 rb_raise(rb_eTypeError, "%"PRIsVALUE"::%"PRIsVALUE" is not a module"
1133 " (%"PRIsVALUE")",
1134 outer, rb_id2str(id), rb_obj_class(module));
1135 }
1136 /* Module may have been defined in Ruby and not pin-rooted */
1137 rb_vm_register_global_object(module);
1138 return module;
1139 }
1140 module = rb_module_new();
1141 rb_const_set(outer, id, module);
1142 rb_set_class_path_string(module, outer, rb_id2str(id));
1143 rb_vm_register_global_object(module);
1144
1145 return module;
1146}
1147
1148 VALUE
1149rb_include_class_new(VALUE module, VALUE super)
1150{
1151 VALUE klass = class_alloc(T_ICLASS, rb_cClass);
1152
1153 RCLASS_SET_M_TBL(klass, RCLASS_M_TBL(module));
1154
1155 RCLASS_SET_ORIGIN(klass, klass);
1156 if (BUILTIN_TYPE(module) == T_ICLASS) {
1157 module = METACLASS_OF(module);
1158 }
1159 RUBY_ASSERT(!RB_TYPE_P(module, T_ICLASS));
1160 if (!RCLASS_CONST_TBL(module)) {
1161 RCLASS_CONST_TBL(module) = rb_id_table_create(0);
1162 }
1163
1164 RCLASS_CVC_TBL(klass) = RCLASS_CVC_TBL(module);
1165 RCLASS_CONST_TBL(klass) = RCLASS_CONST_TBL(module);
1166
1167 RCLASS_SET_SUPER(klass, super);
1168 RBASIC_SET_CLASS(klass, module);
1169
1170 return (VALUE)klass;
1171}
1172
1173static int include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super);
1174
1175static void
1176ensure_includable(VALUE klass, VALUE module)
1177{
1178 rb_class_modify_check(klass);
1179 Check_Type(module, T_MODULE);
1180 rb_module_set_initialized(module);
1181 if (!NIL_P(rb_refinement_module_get_refined_class(module))) {
1182 rb_raise(rb_eArgError, "refinement module is not allowed");
1183 }
1184}
1185
1186void
1187rb_include_module(VALUE klass, VALUE module)
1188{
1189 int changed = 0;
1190
1191 ensure_includable(klass, module);
1192
1193 changed = include_modules_at(klass, RCLASS_ORIGIN(klass), module, TRUE);
1194 if (changed < 0)
1195 rb_raise(rb_eArgError, "cyclic include detected");
1196
1197 if (RB_TYPE_P(klass, T_MODULE)) {
1198 rb_subclass_entry_t *iclass = RCLASS_SUBCLASSES(klass);
1199 // skip the placeholder subclass entry at the head of the list
1200 if (iclass) {
1201 RUBY_ASSERT(!iclass->klass);
1202 iclass = iclass->next;
1203 }
1204
1205 while (iclass) {
1206 int do_include = 1;
1207 VALUE check_class = iclass->klass;
1208 /* During lazy sweeping, iclass->klass could be a dead object that
1209 * has not yet been swept. */
1210 if (!rb_objspace_garbage_object_p(check_class)) {
1211 while (check_class) {
1212 RUBY_ASSERT(!rb_objspace_garbage_object_p(check_class));
1213
1214 if (RB_TYPE_P(check_class, T_ICLASS) &&
1215 (METACLASS_OF(check_class) == module)) {
1216 do_include = 0;
1217 }
1218 check_class = RCLASS_SUPER(check_class);
1219 }
1220
1221 if (do_include) {
1222 include_modules_at(iclass->klass, RCLASS_ORIGIN(iclass->klass), module, TRUE);
1223 }
1224 }
1225
1226 iclass = iclass->next;
1227 }
1228 }
1229}
1230
1231static enum rb_id_table_iterator_result
1232add_refined_method_entry_i(ID key, VALUE value, void *data)
1233{
1234 rb_add_refined_method_entry((VALUE)data, key);
1235 return ID_TABLE_CONTINUE;
1236}
1237
1238static enum rb_id_table_iterator_result
1239clear_module_cache_i(ID id, VALUE val, void *data)
1240{
1241 VALUE klass = (VALUE)data;
1242 rb_clear_method_cache(klass, id);
1243 return ID_TABLE_CONTINUE;
1244}
1245
1246static bool
1247module_in_super_chain(const VALUE klass, VALUE module)
1248{
1249 struct rb_id_table *const klass_m_tbl = RCLASS_M_TBL(RCLASS_ORIGIN(klass));
1250 if (klass_m_tbl) {
1251 while (module) {
1252 if (klass_m_tbl == RCLASS_M_TBL(module))
1253 return true;
1254 module = RCLASS_SUPER(module);
1255 }
1256 }
1257 return false;
1258}
1259
1260// For each ID key in the class constant table, we're going to clear the VM's
1261// inline constant caches associated with it.
1262static enum rb_id_table_iterator_result
1263clear_constant_cache_i(ID id, VALUE value, void *data)
1264{
1265 rb_clear_constant_cache_for_id(id);
1266 return ID_TABLE_CONTINUE;
1267}
1268
1269static int
1270do_include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super, bool check_cyclic)
1271{
1272 VALUE p, iclass, origin_stack = 0;
1273 int method_changed = 0;
1274 long origin_len;
1275 VALUE klass_origin = RCLASS_ORIGIN(klass);
1276 VALUE original_klass = klass;
1277
1278 if (check_cyclic && module_in_super_chain(klass, module))
1279 return -1;
1280
1281 while (module) {
1282 int c_seen = FALSE;
1283 int superclass_seen = FALSE;
1284 struct rb_id_table *tbl;
1285
1286 if (klass == c) {
1287 c_seen = TRUE;
1288 }
1289 if (klass_origin != c || search_super) {
1290 /* ignore if the module included already in superclasses for include,
1291 * ignore if the module included before origin class for prepend
1292 */
1293 for (p = RCLASS_SUPER(klass); p; p = RCLASS_SUPER(p)) {
1294 int type = BUILTIN_TYPE(p);
1295 if (klass_origin == p && !search_super)
1296 break;
1297 if (c == p)
1298 c_seen = TRUE;
1299 if (type == T_ICLASS) {
1300 if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) {
1301 if (!superclass_seen && c_seen) {
1302 c = p; /* move insertion point */
1303 }
1304 goto skip;
1305 }
1306 }
1307 else if (type == T_CLASS) {
1308 superclass_seen = TRUE;
1309 }
1310 }
1311 }
1312
1313 VALUE super_class = RCLASS_SUPER(c);
1314
1315 // invalidate inline method cache
1316 RB_DEBUG_COUNTER_INC(cvar_include_invalidate);
1317 ruby_vm_global_cvar_state++;
1318 tbl = RCLASS_M_TBL(module);
1319 if (tbl && rb_id_table_size(tbl)) {
1320 if (search_super) { // include
1321 if (super_class && !RB_TYPE_P(super_class, T_MODULE)) {
1322 rb_id_table_foreach(tbl, clear_module_cache_i, (void *)super_class);
1323 }
1324 }
1325 else { // prepend
1326 if (!RB_TYPE_P(original_klass, T_MODULE)) {
1327 rb_id_table_foreach(tbl, clear_module_cache_i, (void *)original_klass);
1328 }
1329 }
1330 method_changed = 1;
1331 }
1332
1333 // setup T_ICLASS for the include/prepend module
1334 iclass = rb_include_class_new(module, super_class);
1335 c = RCLASS_SET_SUPER(c, iclass);
1336 RCLASS_SET_INCLUDER(iclass, klass);
1337 if (module != RCLASS_ORIGIN(module)) {
1338 if (!origin_stack) origin_stack = rb_ary_hidden_new(2);
1339 VALUE origin[2] = {iclass, RCLASS_ORIGIN(module)};
1340 rb_ary_cat(origin_stack, origin, 2);
1341 }
1342 else if (origin_stack && (origin_len = RARRAY_LEN(origin_stack)) > 1 &&
1343 RARRAY_AREF(origin_stack, origin_len - 1) == module) {
1344 RCLASS_SET_ORIGIN(RARRAY_AREF(origin_stack, (origin_len -= 2)), iclass);
1345 RICLASS_SET_ORIGIN_SHARED_MTBL(iclass);
1346 rb_ary_resize(origin_stack, origin_len);
1347 }
1348
1349 VALUE m = module;
1350 if (BUILTIN_TYPE(m) == T_ICLASS) m = METACLASS_OF(m);
1351 rb_module_add_to_subclasses_list(m, iclass);
1352
1353 if (BUILTIN_TYPE(klass) == T_MODULE && FL_TEST(klass, RMODULE_IS_REFINEMENT)) {
1354 VALUE refined_class =
1355 rb_refinement_module_get_refined_class(klass);
1356
1357 rb_id_table_foreach(RCLASS_M_TBL(module), add_refined_method_entry_i, (void *)refined_class);
1358 RUBY_ASSERT(BUILTIN_TYPE(c) == T_MODULE);
1359 }
1360
1361 tbl = RCLASS_CONST_TBL(module);
1362 if (tbl && rb_id_table_size(tbl))
1363 rb_id_table_foreach(tbl, clear_constant_cache_i, NULL);
1364 skip:
1365 module = RCLASS_SUPER(module);
1366 }
1367
1368 return method_changed;
1369}
1370
1371static int
1372include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super)
1373{
1374 return do_include_modules_at(klass, c, module, search_super, true);
1375}
1376
1377static enum rb_id_table_iterator_result
1378move_refined_method(ID key, VALUE value, void *data)
1379{
1380 rb_method_entry_t *me = (rb_method_entry_t *)value;
1381
1382 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1383 VALUE klass = (VALUE)data;
1384 struct rb_id_table *tbl = RCLASS_M_TBL(klass);
1385
1386 if (me->def->body.refined.orig_me) {
1387 const rb_method_entry_t *orig_me = me->def->body.refined.orig_me, *new_me;
1388 RB_OBJ_WRITE(me, &me->def->body.refined.orig_me, NULL);
1389 new_me = rb_method_entry_clone(me);
1390 rb_method_table_insert(klass, tbl, key, new_me);
1391 rb_method_entry_copy(me, orig_me);
1392 return ID_TABLE_CONTINUE;
1393 }
1394 else {
1395 rb_method_table_insert(klass, tbl, key, me);
1396 return ID_TABLE_DELETE;
1397 }
1398 }
1399 else {
1400 return ID_TABLE_CONTINUE;
1401 }
1402}
1403
1404static enum rb_id_table_iterator_result
1405cache_clear_refined_method(ID key, VALUE value, void *data)
1406{
1407 rb_method_entry_t *me = (rb_method_entry_t *) value;
1408
1409 if (me->def->type == VM_METHOD_TYPE_REFINED && me->def->body.refined.orig_me) {
1410 VALUE klass = (VALUE)data;
1411 rb_clear_method_cache(klass, me->called_id);
1412 }
1413 // Refined method entries without an orig_me is going to stay in the method
1414 // table of klass, like before the move, so no need to clear the cache.
1415
1416 return ID_TABLE_CONTINUE;
1417}
1418
1419static bool
1420ensure_origin(VALUE klass)
1421{
1422 VALUE origin = RCLASS_ORIGIN(klass);
1423 if (origin == klass) {
1424 origin = class_alloc(T_ICLASS, klass);
1425 RCLASS_SET_M_TBL(origin, RCLASS_M_TBL(klass));
1426 RCLASS_SET_SUPER(origin, RCLASS_SUPER(klass));
1427 RCLASS_SET_SUPER(klass, origin);
1428 RCLASS_SET_ORIGIN(klass, origin);
1429
1430 // RCLASS_SET_ORIGIN marks origin as an origin, so this is the first
1431 // point that it sees M_TBL and may mark it
1432 rb_gc_writebarrier_remember(origin);
1433
1434 RCLASS_M_TBL_INIT(klass);
1435 rb_id_table_foreach(RCLASS_M_TBL(origin), cache_clear_refined_method, (void *)klass);
1436 rb_id_table_foreach(RCLASS_M_TBL(origin), move_refined_method, (void *)klass);
1437 return true;
1438 }
1439 return false;
1440}
1441
1442void
1443rb_prepend_module(VALUE klass, VALUE module)
1444{
1445 int changed;
1446 bool klass_had_no_origin;
1447
1448 ensure_includable(klass, module);
1449 if (module_in_super_chain(klass, module))
1450 rb_raise(rb_eArgError, "cyclic prepend detected");
1451
1452 klass_had_no_origin = ensure_origin(klass);
1453 changed = do_include_modules_at(klass, klass, module, FALSE, false);
1454 RUBY_ASSERT(changed >= 0); // already checked for cyclic prepend above
1455 if (changed) {
1456 rb_vm_check_redefinition_by_prepend(klass);
1457 }
1458 if (RB_TYPE_P(klass, T_MODULE)) {
1459 rb_subclass_entry_t *iclass = RCLASS_SUBCLASSES(klass);
1460 // skip the placeholder subclass entry at the head of the list if it exists
1461 if (iclass) {
1462 RUBY_ASSERT(!iclass->klass);
1463 iclass = iclass->next;
1464 }
1465
1466 VALUE klass_origin = RCLASS_ORIGIN(klass);
1467 struct rb_id_table *klass_m_tbl = RCLASS_M_TBL(klass);
1468 struct rb_id_table *klass_origin_m_tbl = RCLASS_M_TBL(klass_origin);
1469 while (iclass) {
1470 /* During lazy sweeping, iclass->klass could be a dead object that
1471 * has not yet been swept. */
1472 if (!rb_objspace_garbage_object_p(iclass->klass)) {
1473 const VALUE subclass = iclass->klass;
1474 if (klass_had_no_origin && klass_origin_m_tbl == RCLASS_M_TBL(subclass)) {
1475 // backfill an origin iclass to handle refinements and future prepends
1476 rb_id_table_foreach(RCLASS_M_TBL(subclass), clear_module_cache_i, (void *)subclass);
1477 RCLASS_M_TBL(subclass) = klass_m_tbl;
1478 VALUE origin = rb_include_class_new(klass_origin, RCLASS_SUPER(subclass));
1479 RCLASS_SET_SUPER(subclass, origin);
1480 RCLASS_SET_INCLUDER(origin, RCLASS_INCLUDER(subclass));
1481 RCLASS_SET_ORIGIN(subclass, origin);
1482 RICLASS_SET_ORIGIN_SHARED_MTBL(origin);
1483 }
1484 include_modules_at(subclass, subclass, module, FALSE);
1485 }
1486
1487 iclass = iclass->next;
1488 }
1489 }
1490}
1491
1492/*
1493 * call-seq:
1494 * mod.included_modules -> array
1495 *
1496 * Returns the list of modules included or prepended in <i>mod</i>
1497 * or one of <i>mod</i>'s ancestors.
1498 *
1499 * module Sub
1500 * end
1501 *
1502 * module Mixin
1503 * prepend Sub
1504 * end
1505 *
1506 * module Outer
1507 * include Mixin
1508 * end
1509 *
1510 * Mixin.included_modules #=> [Sub]
1511 * Outer.included_modules #=> [Sub, Mixin]
1512 */
1513
1514 VALUE
1515rb_mod_included_modules(VALUE mod)
1516{
1517 VALUE ary = rb_ary_new();
1518 VALUE p;
1519 VALUE origin = RCLASS_ORIGIN(mod);
1520
1521 for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
1522 if (p != origin && RCLASS_ORIGIN(p) == p && BUILTIN_TYPE(p) == T_ICLASS) {
1523 VALUE m = METACLASS_OF(p);
1524 if (RB_TYPE_P(m, T_MODULE))
1525 rb_ary_push(ary, m);
1526 }
1527 }
1528 return ary;
1529}
1530
1531/*
1532 * call-seq:
1533 * mod.include?(module) -> true or false
1534 *
1535 * Returns <code>true</code> if <i>module</i> is included
1536 * or prepended in <i>mod</i> or one of <i>mod</i>'s ancestors.
1537 *
1538 * module A
1539 * end
1540 * class B
1541 * include A
1542 * end
1543 * class C < B
1544 * end
1545 * B.include?(A) #=> true
1546 * C.include?(A) #=> true
1547 * A.include?(A) #=> false
1548 */
1549
1550 VALUE
1551rb_mod_include_p(VALUE mod, VALUE mod2)
1552{
1553 VALUE p;
1554
1555 Check_Type(mod2, T_MODULE);
1556 for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
1557 if (BUILTIN_TYPE(p) == T_ICLASS && !FL_TEST(p, RICLASS_IS_ORIGIN)) {
1558 if (METACLASS_OF(p) == mod2) return Qtrue;
1559 }
1560 }
1561 return Qfalse;
1562}
1563
1564/*
1565 * call-seq:
1566 * mod.ancestors -> array
1567 *
1568 * Returns a list of modules included/prepended in <i>mod</i>
1569 * (including <i>mod</i> itself).
1570 *
1571 * module Mod
1572 * include Math
1573 * include Comparable
1574 * prepend Enumerable
1575 * end
1576 *
1577 * Mod.ancestors #=> [Enumerable, Mod, Comparable, Math]
1578 * Math.ancestors #=> [Math]
1579 * Enumerable.ancestors #=> [Enumerable]
1580 */
1581
1582 VALUE
1583rb_mod_ancestors(VALUE mod)
1584{
1585 VALUE p, ary = rb_ary_new();
1586 VALUE refined_class = Qnil;
1587 if (BUILTIN_TYPE(mod) == T_MODULE && FL_TEST(mod, RMODULE_IS_REFINEMENT)) {
1588 refined_class = rb_refinement_module_get_refined_class(mod);
1589 }
1590
1591 for (p = mod; p; p = RCLASS_SUPER(p)) {
1592 if (p == refined_class) break;
1593 if (p != RCLASS_ORIGIN(p)) continue;
1594 if (BUILTIN_TYPE(p) == T_ICLASS) {
1595 rb_ary_push(ary, METACLASS_OF(p));
1596 }
1597 else {
1598 rb_ary_push(ary, p);
1599 }
1600 }
1601 return ary;
1602}
1603
1604 struct subclass_traverse_data
1605{
1606 VALUE buffer;
1607 long count;
1608 long maxcount;
1609 bool immediate_only;
1610};
1611
1612static void
1613class_descendants_recursive(VALUE klass, VALUE v)
1614{
1615 struct subclass_traverse_data *data = (struct subclass_traverse_data *) v;
1616
1617 if (BUILTIN_TYPE(klass) == T_CLASS && !RCLASS_SINGLETON_P(klass)) {
1618 if (data->buffer && data->count < data->maxcount && !rb_objspace_garbage_object_p(klass)) {
1619 // assumes that this does not cause GC as long as the length does not exceed the capacity
1620 rb_ary_push(data->buffer, klass);
1621 }
1622 data->count++;
1623 if (!data->immediate_only) {
1624 rb_class_foreach_subclass(klass, class_descendants_recursive, v);
1625 }
1626 }
1627 else {
1628 rb_class_foreach_subclass(klass, class_descendants_recursive, v);
1629 }
1630}
1631
1632static VALUE
1633class_descendants(VALUE klass, bool immediate_only)
1634{
1635 struct subclass_traverse_data data = { Qfalse, 0, -1, immediate_only };
1636
1637 // estimate the count of subclasses
1638 rb_class_foreach_subclass(klass, class_descendants_recursive, (VALUE) &data);
1639
1640 // the following allocation may cause GC which may change the number of subclasses
1641 data.buffer = rb_ary_new_capa(data.count);
1642 data.maxcount = data.count;
1643 data.count = 0;
1644
1645 size_t gc_count = rb_gc_count();
1646
1647 // enumerate subclasses
1648 rb_class_foreach_subclass(klass, class_descendants_recursive, (VALUE) &data);
1649
1650 if (gc_count != rb_gc_count()) {
1651 rb_bug("GC must not occur during the subclass iteration of Class#descendants");
1652 }
1653
1654 return data.buffer;
1655}
1656
1657/*
1658 * call-seq:
1659 * subclasses -> array
1660 *
1661 * Returns an array of classes where the receiver is the
1662 * direct superclass of the class, excluding singleton classes.
1663 * The order of the returned array is not defined.
1664 *
1665 * class A; end
1666 * class B < A; end
1667 * class C < B; end
1668 * class D < A; end
1669 *
1670 * A.subclasses #=> [D, B]
1671 * B.subclasses #=> [C]
1672 * C.subclasses #=> []
1673 *
1674 * Anonymous subclasses (not associated with a constant) are
1675 * returned, too:
1676 *
1677 * c = Class.new(A)
1678 * A.subclasses # => [#<Class:0x00007f003c77bd78>, D, B]
1679 *
1680 * Note that the parent does not hold references to subclasses
1681 * and doesn't prevent them from being garbage collected. This
1682 * means that the subclass might disappear when all references
1683 * to it are dropped:
1684 *
1685 * # drop the reference to subclass, it can be garbage-collected now
1686 * c = nil
1687 *
1688 * A.subclasses
1689 * # It can be
1690 * # => [#<Class:0x00007f003c77bd78>, D, B]
1691 * # ...or just
1692 * # => [D, B]
1693 * # ...depending on whether garbage collector was run
1694 */
1695
1696 VALUE
1697rb_class_subclasses(VALUE klass)
1698{
1699 return class_descendants(klass, true);
1700}
1701
1702/*
1703 * call-seq:
1704 * attached_object -> object
1705 *
1706 * Returns the object for which the receiver is the singleton class.
1707 *
1708 * Raises an TypeError if the class is not a singleton class.
1709 *
1710 * class Foo; end
1711 *
1712 * Foo.singleton_class.attached_object #=> Foo
1713 * Foo.attached_object #=> TypeError: `Foo' is not a singleton class
1714 * Foo.new.singleton_class.attached_object #=> #<Foo:0x000000010491a370>
1715 * TrueClass.attached_object #=> TypeError: `TrueClass' is not a singleton class
1716 * NilClass.attached_object #=> TypeError: `NilClass' is not a singleton class
1717 */
1718
1719 VALUE
1720rb_class_attached_object(VALUE klass)
1721{
1722 if (!RCLASS_SINGLETON_P(klass)) {
1723 rb_raise(rb_eTypeError, "'%"PRIsVALUE"' is not a singleton class", klass);
1724 }
1725
1726 return RCLASS_ATTACHED_OBJECT(klass);
1727}
1728
1729static void
1730ins_methods_push(st_data_t name, st_data_t ary)
1731{
1732 rb_ary_push((VALUE)ary, ID2SYM((ID)name));
1733}
1734
1735static int
1736ins_methods_i(st_data_t name, st_data_t type, st_data_t ary)
1737{
1738 switch ((rb_method_visibility_t)type) {
1739 case METHOD_VISI_UNDEF:
1740 case METHOD_VISI_PRIVATE:
1741 break;
1742 default: /* everything but private */
1743 ins_methods_push(name, ary);
1744 break;
1745 }
1746 return ST_CONTINUE;
1747}
1748
1749static int
1750ins_methods_type_i(st_data_t name, st_data_t type, st_data_t ary, rb_method_visibility_t visi)
1751{
1752 if ((rb_method_visibility_t)type == visi) {
1753 ins_methods_push(name, ary);
1754 }
1755 return ST_CONTINUE;
1756}
1757
1758static int
1759ins_methods_prot_i(st_data_t name, st_data_t type, st_data_t ary)
1760{
1761 return ins_methods_type_i(name, type, ary, METHOD_VISI_PROTECTED);
1762}
1763
1764static int
1765ins_methods_priv_i(st_data_t name, st_data_t type, st_data_t ary)
1766{
1767 return ins_methods_type_i(name, type, ary, METHOD_VISI_PRIVATE);
1768}
1769
1770static int
1771ins_methods_pub_i(st_data_t name, st_data_t type, st_data_t ary)
1772{
1773 return ins_methods_type_i(name, type, ary, METHOD_VISI_PUBLIC);
1774}
1775
1776static int
1777ins_methods_undef_i(st_data_t name, st_data_t type, st_data_t ary)
1778{
1779 return ins_methods_type_i(name, type, ary, METHOD_VISI_UNDEF);
1780}
1781
1782 struct method_entry_arg {
1783 st_table *list;
1784 int recur;
1785};
1786
1787static enum rb_id_table_iterator_result
1788method_entry_i(ID key, VALUE value, void *data)
1789{
1790 const rb_method_entry_t *me = (const rb_method_entry_t *)value;
1791 struct method_entry_arg *arg = (struct method_entry_arg *)data;
1792 rb_method_visibility_t type;
1793
1794 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1795 VALUE owner = me->owner;
1796 me = rb_resolve_refined_method(Qnil, me);
1797 if (!me) return ID_TABLE_CONTINUE;
1798 if (!arg->recur && me->owner != owner) return ID_TABLE_CONTINUE;
1799 }
1800 if (!st_is_member(arg->list, key)) {
1801 if (UNDEFINED_METHOD_ENTRY_P(me)) {
1802 type = METHOD_VISI_UNDEF; /* none */
1803 }
1804 else {
1805 type = METHOD_ENTRY_VISI(me);
1806 RUBY_ASSERT(type != METHOD_VISI_UNDEF);
1807 }
1808 st_add_direct(arg->list, key, (st_data_t)type);
1809 }
1810 return ID_TABLE_CONTINUE;
1811}
1812
1813static void
1814add_instance_method_list(VALUE mod, struct method_entry_arg *me_arg)
1815{
1816 struct rb_id_table *m_tbl = RCLASS_M_TBL(mod);
1817 if (!m_tbl) return;
1818 rb_id_table_foreach(m_tbl, method_entry_i, me_arg);
1819}
1820
1821static bool
1822particular_class_p(VALUE mod)
1823{
1824 if (!mod) return false;
1825 if (RCLASS_SINGLETON_P(mod)) return true;
1826 if (BUILTIN_TYPE(mod) == T_ICLASS) return true;
1827 return false;
1828}
1829
1830static VALUE
1831class_instance_method_list(int argc, const VALUE *argv, VALUE mod, int obj, int (*func) (st_data_t, st_data_t, st_data_t))
1832{
1833 VALUE ary;
1834 int recur = TRUE, prepended = 0;
1835 struct method_entry_arg me_arg;
1836
1837 if (rb_check_arity(argc, 0, 1)) recur = RTEST(argv[0]);
1838
1839 me_arg.list = st_init_numtable();
1840 me_arg.recur = recur;
1841
1842 if (obj) {
1843 for (; particular_class_p(mod); mod = RCLASS_SUPER(mod)) {
1844 add_instance_method_list(mod, &me_arg);
1845 }
1846 }
1847
1848 if (!recur && RCLASS_ORIGIN(mod) != mod) {
1849 mod = RCLASS_ORIGIN(mod);
1850 prepended = 1;
1851 }
1852
1853 for (; mod; mod = RCLASS_SUPER(mod)) {
1854 add_instance_method_list(mod, &me_arg);
1855 if (BUILTIN_TYPE(mod) == T_ICLASS && !prepended) continue;
1856 if (!recur) break;
1857 }
1858 ary = rb_ary_new2(me_arg.list->num_entries);
1859 st_foreach(me_arg.list, func, ary);
1860 st_free_table(me_arg.list);
1861
1862 return ary;
1863}
1864
1865/*
1866 * call-seq:
1867 * mod.instance_methods(include_super=true) -> array
1868 *
1869 * Returns an array containing the names of the public and protected instance
1870 * methods in the receiver. For a module, these are the public and protected methods;
1871 * for a class, they are the instance (not singleton) methods. If the optional
1872 * parameter is <code>false</code>, the methods of any ancestors are not included.
1873 *
1874 * module A
1875 * def method1() end
1876 * end
1877 * class B
1878 * include A
1879 * def method2() end
1880 * end
1881 * class C < B
1882 * def method3() end
1883 * end
1884 *
1885 * A.instance_methods(false) #=> [:method1]
1886 * B.instance_methods(false) #=> [:method2]
1887 * B.instance_methods(true).include?(:method1) #=> true
1888 * C.instance_methods(false) #=> [:method3]
1889 * C.instance_methods.include?(:method2) #=> true
1890 *
1891 * Note that method visibility changes in the current class, as well as aliases,
1892 * are considered as methods of the current class by this method:
1893 *
1894 * class C < B
1895 * alias method4 method2
1896 * protected :method2
1897 * end
1898 * C.instance_methods(false).sort #=> [:method2, :method3, :method4]
1899 */
1900
1901 VALUE
1902rb_class_instance_methods(int argc, const VALUE *argv, VALUE mod)
1903{
1904 return class_instance_method_list(argc, argv, mod, 0, ins_methods_i);
1905}
1906
1907/*
1908 * call-seq:
1909 * mod.protected_instance_methods(include_super=true) -> array
1910 *
1911 * Returns a list of the protected instance methods defined in
1912 * <i>mod</i>. If the optional parameter is <code>false</code>, the
1913 * methods of any ancestors are not included.
1914 */
1915
1916 VALUE
1917rb_class_protected_instance_methods(int argc, const VALUE *argv, VALUE mod)
1918{
1919 return class_instance_method_list(argc, argv, mod, 0, ins_methods_prot_i);
1920}
1921
1922/*
1923 * call-seq:
1924 * mod.private_instance_methods(include_super=true) -> array
1925 *
1926 * Returns a list of the private instance methods defined in
1927 * <i>mod</i>. If the optional parameter is <code>false</code>, the
1928 * methods of any ancestors are not included.
1929 *
1930 * module Mod
1931 * def method1() end
1932 * private :method1
1933 * def method2() end
1934 * end
1935 * Mod.instance_methods #=> [:method2]
1936 * Mod.private_instance_methods #=> [:method1]
1937 */
1938
1939 VALUE
1940rb_class_private_instance_methods(int argc, const VALUE *argv, VALUE mod)
1941{
1942 return class_instance_method_list(argc, argv, mod, 0, ins_methods_priv_i);
1943}
1944
1945/*
1946 * call-seq:
1947 * mod.public_instance_methods(include_super=true) -> array
1948 *
1949 * Returns a list of the public instance methods defined in <i>mod</i>.
1950 * If the optional parameter is <code>false</code>, the methods of
1951 * any ancestors are not included.
1952 */
1953
1954 VALUE
1955rb_class_public_instance_methods(int argc, const VALUE *argv, VALUE mod)
1956{
1957 return class_instance_method_list(argc, argv, mod, 0, ins_methods_pub_i);
1958}
1959
1960/*
1961 * call-seq:
1962 * mod.undefined_instance_methods -> array
1963 *
1964 * Returns a list of the undefined instance methods defined in <i>mod</i>.
1965 * The undefined methods of any ancestors are not included.
1966 */
1967
1968 VALUE
1969rb_class_undefined_instance_methods(VALUE mod)
1970{
1971 VALUE include_super = Qfalse;
1972 return class_instance_method_list(1, &include_super, mod, 0, ins_methods_undef_i);
1973}
1974
1975/*
1976 * call-seq:
1977 * obj.methods(regular=true) -> array
1978 *
1979 * Returns a list of the names of public and protected methods of
1980 * <i>obj</i>. This will include all the methods accessible in
1981 * <i>obj</i>'s ancestors.
1982 * If the optional parameter is <code>false</code>, it
1983 * returns an array of <i>obj</i>'s public and protected singleton methods,
1984 * the array will not include methods in modules included in <i>obj</i>.
1985 *
1986 * class Klass
1987 * def klass_method()
1988 * end
1989 * end
1990 * k = Klass.new
1991 * k.methods[0..9] #=> [:klass_method, :nil?, :===,
1992 * # :==~, :!, :eql?
1993 * # :hash, :<=>, :class, :singleton_class]
1994 * k.methods.length #=> 56
1995 *
1996 * k.methods(false) #=> []
1997 * def k.singleton_method; end
1998 * k.methods(false) #=> [:singleton_method]
1999 *
2000 * module M123; def m123; end end
2001 * k.extend M123
2002 * k.methods(false) #=> [:singleton_method]
2003 */
2004
2005 VALUE
2006rb_obj_methods(int argc, const VALUE *argv, VALUE obj)
2007{
2008 rb_check_arity(argc, 0, 1);
2009 if (argc > 0 && !RTEST(argv[0])) {
2010 return rb_obj_singleton_methods(argc, argv, obj);
2011 }
2012 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_i);
2013}
2014
2015/*
2016 * call-seq:
2017 * obj.protected_methods(all=true) -> array
2018 *
2019 * Returns the list of protected methods accessible to <i>obj</i>. If
2020 * the <i>all</i> parameter is set to <code>false</code>, only those methods
2021 * in the receiver will be listed.
2022 */
2023
2024 VALUE
2025rb_obj_protected_methods(int argc, const VALUE *argv, VALUE obj)
2026{
2027 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_prot_i);
2028}
2029
2030/*
2031 * call-seq:
2032 * obj.private_methods(all=true) -> array
2033 *
2034 * Returns the list of private methods accessible to <i>obj</i>. If
2035 * the <i>all</i> parameter is set to <code>false</code>, only those methods
2036 * in the receiver will be listed.
2037 */
2038
2039 VALUE
2040rb_obj_private_methods(int argc, const VALUE *argv, VALUE obj)
2041{
2042 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_priv_i);
2043}
2044
2045/*
2046 * call-seq:
2047 * obj.public_methods(all=true) -> array
2048 *
2049 * Returns the list of public methods accessible to <i>obj</i>. If
2050 * the <i>all</i> parameter is set to <code>false</code>, only those methods
2051 * in the receiver will be listed.
2052 */
2053
2054 VALUE
2055rb_obj_public_methods(int argc, const VALUE *argv, VALUE obj)
2056{
2057 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_pub_i);
2058}
2059
2060/*
2061 * call-seq:
2062 * obj.singleton_methods(all=true) -> array
2063 *
2064 * Returns an array of the names of singleton methods for <i>obj</i>.
2065 * If the optional <i>all</i> parameter is true, the list will include
2066 * methods in modules included in <i>obj</i>.
2067 * Only public and protected singleton methods are returned.
2068 *
2069 * module Other
2070 * def three() end
2071 * end
2072 *
2073 * class Single
2074 * def Single.four() end
2075 * end
2076 *
2077 * a = Single.new
2078 *
2079 * def a.one()
2080 * end
2081 *
2082 * class << a
2083 * include Other
2084 * def two()
2085 * end
2086 * end
2087 *
2088 * Single.singleton_methods #=> [:four]
2089 * a.singleton_methods(false) #=> [:two, :one]
2090 * a.singleton_methods #=> [:two, :one, :three]
2091 */
2092
2093 VALUE
2094rb_obj_singleton_methods(int argc, const VALUE *argv, VALUE obj)
2095{
2096 VALUE ary, klass, origin;
2097 struct method_entry_arg me_arg;
2098 struct rb_id_table *mtbl;
2099 int recur = TRUE;
2100
2101 if (rb_check_arity(argc, 0, 1)) recur = RTEST(argv[0]);
2102 if (RCLASS_SINGLETON_P(obj)) {
2103 rb_singleton_class(obj);
2104 }
2105 klass = CLASS_OF(obj);
2106 origin = RCLASS_ORIGIN(klass);
2107 me_arg.list = st_init_numtable();
2108 me_arg.recur = recur;
2109 if (klass && RCLASS_SINGLETON_P(klass)) {
2110 if ((mtbl = RCLASS_M_TBL(origin)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2111 klass = RCLASS_SUPER(klass);
2112 }
2113 if (recur) {
2114 while (klass && (RCLASS_SINGLETON_P(klass) || RB_TYPE_P(klass, T_ICLASS))) {
2115 if (klass != origin && (mtbl = RCLASS_M_TBL(klass)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2116 klass = RCLASS_SUPER(klass);
2117 }
2118 }
2119 ary = rb_ary_new2(me_arg.list->num_entries);
2120 st_foreach(me_arg.list, ins_methods_i, ary);
2121 st_free_table(me_arg.list);
2122
2123 return ary;
2124}
2125
2134#ifdef rb_define_method_id
2135#undef rb_define_method_id
2136#endif
2137void
2138 rb_define_method_id(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc)
2139{
2140 rb_add_method_cfunc(klass, mid, func, argc, METHOD_VISI_PUBLIC);
2141}
2142
2143#ifdef rb_define_method
2144#undef rb_define_method
2145#endif
2146void
2147 rb_define_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
2148{
2149 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PUBLIC);
2150}
2151
2152#ifdef rb_define_protected_method
2153#undef rb_define_protected_method
2154#endif
2155void
2156 rb_define_protected_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
2157{
2158 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PROTECTED);
2159}
2160
2161#ifdef rb_define_private_method
2162#undef rb_define_private_method
2163#endif
2164void
2165 rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
2166{
2167 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PRIVATE);
2168}
2169
2170void
2171rb_undef_method(VALUE klass, const char *name)
2172{
2173 rb_add_method(klass, rb_intern(name), VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2174}
2175
2176static enum rb_id_table_iterator_result
2177undef_method_i(ID name, VALUE value, void *data)
2178{
2179 VALUE klass = (VALUE)data;
2180 rb_add_method(klass, name, VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2181 return ID_TABLE_CONTINUE;
2182}
2183
2184void
2185rb_undef_methods_from(VALUE klass, VALUE super)
2186{
2187 struct rb_id_table *mtbl = RCLASS_M_TBL(super);
2188 if (mtbl) {
2189 rb_id_table_foreach(mtbl, undef_method_i, (void *)klass);
2190 }
2191}
2192
2201static inline VALUE
2202special_singleton_class_of(VALUE obj)
2203{
2204 switch (obj) {
2205 case Qnil: return rb_cNilClass;
2206 case Qfalse: return rb_cFalseClass;
2207 case Qtrue: return rb_cTrueClass;
2208 default: return Qnil;
2209 }
2210}
2211
2212 VALUE
2213rb_special_singleton_class(VALUE obj)
2214{
2215 return special_singleton_class_of(obj);
2216}
2217
2227static VALUE
2228singleton_class_of(VALUE obj)
2229{
2230 VALUE klass;
2231
2232 switch (TYPE(obj)) {
2233 case T_FIXNUM:
2234 case T_BIGNUM:
2235 case T_FLOAT:
2236 case T_SYMBOL:
2237 rb_raise(rb_eTypeError, "can't define singleton");
2238
2239 case T_FALSE:
2240 case T_TRUE:
2241 case T_NIL:
2242 klass = special_singleton_class_of(obj);
2243 if (NIL_P(klass))
2244 rb_bug("unknown immediate %p", (void *)obj);
2245 return klass;
2246
2247 case T_STRING:
2248 if (CHILLED_STRING_P(obj)) {
2249 CHILLED_STRING_MUTATED(obj);
2250 }
2251 else if (FL_TEST_RAW(obj, RSTRING_FSTR)) {
2252 rb_raise(rb_eTypeError, "can't define singleton");
2253 }
2254 }
2255
2256 klass = METACLASS_OF(obj);
2257 if (!(RCLASS_SINGLETON_P(klass) &&
2258 RCLASS_ATTACHED_OBJECT(klass) == obj)) {
2259 klass = rb_make_metaclass(obj, klass);
2260 }
2261
2262 RB_FL_SET_RAW(klass, RB_OBJ_FROZEN_RAW(obj));
2263
2264 return klass;
2265}
2266
2267void
2268rb_freeze_singleton_class(VALUE x)
2269{
2270 /* should not propagate to meta-meta-class, and so on */
2271 if (!RCLASS_SINGLETON_P(x)) {
2272 VALUE klass = RBASIC_CLASS(x);
2273 if (klass && // no class when hidden from ObjectSpace
2274 FL_TEST(klass, (FL_SINGLETON|FL_FREEZE)) == FL_SINGLETON) {
2275 OBJ_FREEZE(klass);
2276 }
2277 }
2278}
2279
2287 VALUE
2288rb_singleton_class_get(VALUE obj)
2289{
2290 VALUE klass;
2291
2292 if (SPECIAL_CONST_P(obj)) {
2293 return rb_special_singleton_class(obj);
2294 }
2295 klass = METACLASS_OF(obj);
2296 if (!RCLASS_SINGLETON_P(klass)) return Qnil;
2297 if (RCLASS_ATTACHED_OBJECT(klass) != obj) return Qnil;
2298 return klass;
2299}
2300
2301 VALUE
2302rb_singleton_class(VALUE obj)
2303{
2304 VALUE klass = singleton_class_of(obj);
2305
2306 /* ensures an exposed class belongs to its own eigenclass */
2307 if (RB_TYPE_P(obj, T_CLASS)) (void)ENSURE_EIGENCLASS(klass);
2308
2309 return klass;
2310}
2311
2321#ifdef rb_define_singleton_method
2322#undef rb_define_singleton_method
2323#endif
2324void
2325 rb_define_singleton_method(VALUE obj, const char *name, VALUE (*func)(ANYARGS), int argc)
2326{
2327 rb_define_method(singleton_class_of(obj), name, func, argc);
2328}
2329
2330#ifdef rb_define_module_function
2331#undef rb_define_module_function
2332#endif
2333void
2334 rb_define_module_function(VALUE module, const char *name, VALUE (*func)(ANYARGS), int argc)
2335{
2336 rb_define_private_method(module, name, func, argc);
2337 rb_define_singleton_method(module, name, func, argc);
2338}
2339
2340#ifdef rb_define_global_function
2341#undef rb_define_global_function
2342#endif
2343void
2344 rb_define_global_function(const char *name, VALUE (*func)(ANYARGS), int argc)
2345{
2346 rb_define_module_function(rb_mKernel, name, func, argc);
2347}
2348
2349void
2350rb_define_alias(VALUE klass, const char *name1, const char *name2)
2351{
2352 rb_alias(klass, rb_intern(name1), rb_intern(name2));
2353}
2354
2355void
2356rb_define_attr(VALUE klass, const char *name, int read, int write)
2357{
2358 rb_attr(klass, rb_intern(name), read, write, FALSE);
2359}
2360
2361 VALUE
2362rb_keyword_error_new(const char *error, VALUE keys)
2363{
2364 long i = 0, len = RARRAY_LEN(keys);
2365 VALUE error_message = rb_sprintf("%s keyword%.*s", error, len > 1, "s");
2366
2367 if (len > 0) {
2368 rb_str_cat_cstr(error_message, ": ");
2369 while (1) {
2370 const VALUE k = RARRAY_AREF(keys, i);
2371 rb_str_append(error_message, rb_inspect(k));
2372 if (++i >= len) break;
2373 rb_str_cat_cstr(error_message, ", ");
2374 }
2375 }
2376
2377 return rb_exc_new_str(rb_eArgError, error_message);
2378}
2379
2380NORETURN(static void rb_keyword_error(const char *error, VALUE keys));
2381static void
2382rb_keyword_error(const char *error, VALUE keys)
2383{
2384 rb_exc_raise(rb_keyword_error_new(error, keys));
2385}
2386
2387NORETURN(static void unknown_keyword_error(VALUE hash, const ID *table, int keywords));
2388static void
2389unknown_keyword_error(VALUE hash, const ID *table, int keywords)
2390{
2391 int i;
2392 for (i = 0; i < keywords; i++) {
2393 st_data_t key = ID2SYM(table[i]);
2394 rb_hash_stlike_delete(hash, &key, NULL);
2395 }
2396 rb_keyword_error("unknown", rb_hash_keys(hash));
2397}
2398
2399
2400static int
2401separate_symbol(st_data_t key, st_data_t value, st_data_t arg)
2402{
2403 VALUE *kwdhash = (VALUE *)arg;
2404 if (!SYMBOL_P(key)) kwdhash++;
2405 if (!*kwdhash) *kwdhash = rb_hash_new();
2406 rb_hash_aset(*kwdhash, (VALUE)key, (VALUE)value);
2407 return ST_CONTINUE;
2408}
2409
2410 VALUE
2411rb_extract_keywords(VALUE *orighash)
2412{
2413 VALUE parthash[2] = {0, 0};
2414 VALUE hash = *orighash;
2415
2416 if (RHASH_EMPTY_P(hash)) {
2417 *orighash = 0;
2418 return hash;
2419 }
2420 rb_hash_foreach(hash, separate_symbol, (st_data_t)&parthash);
2421 *orighash = parthash[1];
2422 if (parthash[1] && RBASIC_CLASS(hash) != rb_cHash) {
2423 RBASIC_SET_CLASS(parthash[1], RBASIC_CLASS(hash));
2424 }
2425 return parthash[0];
2426}
2427
2428int
2429rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
2430{
2431 int i = 0, j;
2432 int rest = 0;
2433 VALUE missing = Qnil;
2434 st_data_t key;
2435
2436#define extract_kwarg(keyword, val) \
2437 (key = (st_data_t)(keyword), values ? \
2438 (rb_hash_stlike_delete(keyword_hash, &key, &(val)) || ((val) = Qundef, 0)) : \
2439 rb_hash_stlike_lookup(keyword_hash, key, NULL))
2440
2441 if (NIL_P(keyword_hash)) keyword_hash = 0;
2442
2443 if (optional < 0) {
2444 rest = 1;
2445 optional = -1-optional;
2446 }
2447 if (required) {
2448 for (; i < required; i++) {
2449 VALUE keyword = ID2SYM(table[i]);
2450 if (keyword_hash) {
2451 if (extract_kwarg(keyword, values[i])) {
2452 continue;
2453 }
2454 }
2455 if (NIL_P(missing)) missing = rb_ary_hidden_new(1);
2456 rb_ary_push(missing, keyword);
2457 }
2458 if (!NIL_P(missing)) {
2459 rb_keyword_error("missing", missing);
2460 }
2461 }
2462 j = i;
2463 if (optional && keyword_hash) {
2464 for (i = 0; i < optional; i++) {
2465 if (extract_kwarg(ID2SYM(table[required+i]), values[required+i])) {
2466 j++;
2467 }
2468 }
2469 }
2470 if (!rest && keyword_hash) {
2471 if (RHASH_SIZE(keyword_hash) > (unsigned int)(values ? 0 : j)) {
2472 unknown_keyword_error(keyword_hash, table, required+optional);
2473 }
2474 }
2475 if (values && !keyword_hash) {
2476 for (i = 0; i < required + optional; i++) {
2477 values[i] = Qundef;
2478 }
2479 }
2480 return j;
2481#undef extract_kwarg
2482}
2483
2484 struct rb_scan_args_t {
2485 int kw_flag;
2486 int n_lead;
2487 int n_opt;
2488 int n_trail;
2489 bool f_var;
2490 bool f_hash;
2491 bool f_block;
2492};
2493
2494static void
2495rb_scan_args_parse(int kw_flag, const char *fmt, struct rb_scan_args_t *arg)
2496{
2497 const char *p = fmt;
2498
2499 memset(arg, 0, sizeof(*arg));
2500 arg->kw_flag = kw_flag;
2501
2502 if (ISDIGIT(*p)) {
2503 arg->n_lead = *p - '0';
2504 p++;
2505 if (ISDIGIT(*p)) {
2506 arg->n_opt = *p - '0';
2507 p++;
2508 }
2509 }
2510 if (*p == '*') {
2511 arg->f_var = 1;
2512 p++;
2513 }
2514 if (ISDIGIT(*p)) {
2515 arg->n_trail = *p - '0';
2516 p++;
2517 }
2518 if (*p == ':') {
2519 arg->f_hash = 1;
2520 p++;
2521 }
2522 if (*p == '&') {
2523 arg->f_block = 1;
2524 p++;
2525 }
2526 if (*p != '\0') {
2527 rb_fatal("bad scan arg format: %s", fmt);
2528 }
2529}
2530
2531static int
2532rb_scan_args_assign(const struct rb_scan_args_t *arg, int argc, const VALUE *const argv, va_list vargs)
2533{
2534 int i, argi = 0;
2535 VALUE *var, hash = Qnil;
2536#define rb_scan_args_next_param() va_arg(vargs, VALUE *)
2537 const int kw_flag = arg->kw_flag;
2538 const int n_lead = arg->n_lead;
2539 const int n_opt = arg->n_opt;
2540 const int n_trail = arg->n_trail;
2541 const int n_mand = n_lead + n_trail;
2542 const bool f_var = arg->f_var;
2543 const bool f_hash = arg->f_hash;
2544 const bool f_block = arg->f_block;
2545
2546 /* capture an option hash - phase 1: pop from the argv */
2547 if (f_hash && argc > 0) {
2548 VALUE last = argv[argc - 1];
2549 if (rb_scan_args_keyword_p(kw_flag, last)) {
2550 hash = rb_hash_dup(last);
2551 argc--;
2552 }
2553 }
2554
2555 if (argc < n_mand) {
2556 goto argc_error;
2557 }
2558
2559 /* capture leading mandatory arguments */
2560 for (i = 0; i < n_lead; i++) {
2561 var = rb_scan_args_next_param();
2562 if (var) *var = argv[argi];
2563 argi++;
2564 }
2565 /* capture optional arguments */
2566 for (i = 0; i < n_opt; i++) {
2567 var = rb_scan_args_next_param();
2568 if (argi < argc - n_trail) {
2569 if (var) *var = argv[argi];
2570 argi++;
2571 }
2572 else {
2573 if (var) *var = Qnil;
2574 }
2575 }
2576 /* capture variable length arguments */
2577 if (f_var) {
2578 int n_var = argc - argi - n_trail;
2579
2580 var = rb_scan_args_next_param();
2581 if (0 < n_var) {
2582 if (var) *var = rb_ary_new_from_values(n_var, &argv[argi]);
2583 argi += n_var;
2584 }
2585 else {
2586 if (var) *var = rb_ary_new();
2587 }
2588 }
2589 /* capture trailing mandatory arguments */
2590 for (i = 0; i < n_trail; i++) {
2591 var = rb_scan_args_next_param();
2592 if (var) *var = argv[argi];
2593 argi++;
2594 }
2595 /* capture an option hash - phase 2: assignment */
2596 if (f_hash) {
2597 var = rb_scan_args_next_param();
2598 if (var) *var = hash;
2599 }
2600 /* capture iterator block */
2601 if (f_block) {
2602 var = rb_scan_args_next_param();
2603 if (rb_block_given_p()) {
2604 *var = rb_block_proc();
2605 }
2606 else {
2607 *var = Qnil;
2608 }
2609 }
2610
2611 if (argi == argc) {
2612 return argc;
2613 }
2614
2615 argc_error:
2616 return -(argc + 1);
2617#undef rb_scan_args_next_param
2618}
2619
2620static int
2621rb_scan_args_result(const struct rb_scan_args_t *const arg, int argc)
2622{
2623 const int n_lead = arg->n_lead;
2624 const int n_opt = arg->n_opt;
2625 const int n_trail = arg->n_trail;
2626 const int n_mand = n_lead + n_trail;
2627 const bool f_var = arg->f_var;
2628
2629 if (argc >= 0) {
2630 return argc;
2631 }
2632
2633 argc = -argc - 1;
2634 rb_error_arity(argc, n_mand, f_var ? UNLIMITED_ARGUMENTS : n_mand + n_opt);
2635 UNREACHABLE_RETURN(-1);
2636}
2637
2638#undef rb_scan_args
2639int
2640rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...)
2641{
2642 va_list vargs;
2643 struct rb_scan_args_t arg;
2644 rb_scan_args_parse(RB_SCAN_ARGS_PASS_CALLED_KEYWORDS, fmt, &arg);
2645 va_start(vargs,fmt);
2646 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
2647 va_end(vargs);
2648 return rb_scan_args_result(&arg, argc);
2649}
2650
2651#undef rb_scan_args_kw
2652int
2653rb_scan_args_kw(int kw_flag, int argc, const VALUE *argv, const char *fmt, ...)
2654{
2655 va_list vargs;
2656 struct rb_scan_args_t arg;
2657 rb_scan_args_parse(kw_flag, fmt, &arg);
2658 va_start(vargs,fmt);
2659 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
2660 va_end(vargs);
2661 return rb_scan_args_result(&arg, argc);
2662}
2663
RUBY_ASSERT
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
rb_define_method
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
Definition cxxanyargs.hpp:670
rb_define_method_id
#define rb_define_method_id(klass, mid, func, arity)
Defines klass#mid.
Definition cxxanyargs.hpp:673
rb_define_singleton_method
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
Definition cxxanyargs.hpp:685
rb_define_protected_method
#define rb_define_protected_method(klass, mid, func, arity)
Defines klass#mid and makes it protected.
Definition cxxanyargs.hpp:681
rb_define_module_function
#define rb_define_module_function(klass, mid, func, arity)
Defines klass#mid and makes it a module function.
Definition cxxanyargs.hpp:689
rb_define_private_method
#define rb_define_private_method(klass, mid, func, arity)
Defines klass#mid and makes it private.
Definition cxxanyargs.hpp:677
rb_define_global_function
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
Definition cxxanyargs.hpp:695
RUBY_EXTERN
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition dllexport.h:45
RB_OBJ_FROZEN_RAW
static VALUE RB_OBJ_FROZEN_RAW(VALUE obj)
This is an implementation detail of RB_OBJ_FROZEN().
Definition fl_type.h:883
RB_FL_SET_RAW
static void RB_FL_SET_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_SET().
Definition fl_type.h:606
RUBY_FL_USER1
@ RUBY_FL_USER1
User-defined flag.
Definition fl_type.h:329
rb_class_protected_instance_methods
VALUE rb_class_protected_instance_methods(int argc, const VALUE *argv, VALUE mod)
Identical to rb_class_instance_methods(), except it returns names of methods that are protected only.
Definition class.c:1917
rb_include_module
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
Definition class.c:1187
rb_refinement_new
VALUE rb_refinement_new(void)
Creates a new, anonymous refinement.
Definition class.c:1082
rb_define_class
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition class.c:980
rb_class_new
VALUE rb_class_new(VALUE super)
Creates a new, anonymous class.
Definition class.c:359
make_singleton_class
static VALUE make_singleton_class(VALUE obj)
Creates a singleton class for obj.
Definition class.c:801
rb_singleton_class_clone
VALUE rb_singleton_class_clone(VALUE obj)
Clones a singleton class.
Definition class.c:645
rb_prepend_module
void rb_prepend_module(VALUE klass, VALUE module)
Identical to rb_include_module(), except it "prepends" the passed module to the klass,...
Definition class.c:1443
rb_class_subclasses
VALUE rb_class_subclasses(VALUE klass)
Queries the class's direct descendants.
Definition class.c:1697
rb_singleton_class
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
Definition class.c:2302
rb_define_class_under
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition class.c:1012
rb_class_attached_object
VALUE rb_class_attached_object(VALUE klass)
Returns the attached object for a singleton class.
Definition class.c:1720
rb_obj_singleton_methods
VALUE rb_obj_singleton_methods(int argc, const VALUE *argv, VALUE obj)
Identical to rb_class_instance_methods(), except it returns names of singleton methods instead of ins...
Definition class.c:2094
rb_module_new
VALUE rb_module_new(void)
Creates a new, anonymous module.
Definition class.c:1076
META_CLASS_OF_CLASS_CLASS_P
#define META_CLASS_OF_CLASS_CLASS_P(k)
whether k is a meta^(n)-class of Class class
Definition class.c:721
rb_class_instance_methods
VALUE rb_class_instance_methods(int argc, const VALUE *argv, VALUE mod)
Generates an array of symbols, which are the list of method names defined in the passed class.
Definition class.c:1902
rb_check_inheritable
void rb_check_inheritable(VALUE super)
Asserts that the given class can derive a child class.
Definition class.c:344
rb_class_public_instance_methods
VALUE rb_class_public_instance_methods(int argc, const VALUE *argv, VALUE mod)
Identical to rb_class_instance_methods(), except it returns names of methods that are public only.
Definition class.c:1955
rb_class_boot
VALUE rb_class_boot(VALUE super)
A utility function that wraps class_alloc.
Definition class.c:281
rb_define_module
VALUE rb_define_module(const char *name)
Defines a top-level module.
Definition class.c:1095
rb_class_modify_check
void rb_class_modify_check(VALUE klass)
Asserts that klass is not a frozen class.
Definition eval.c:419
rb_define_module_id_under
VALUE rb_define_module_id_under(VALUE outer, ID id)
Identical to rb_define_module_under(), except it takes the name in ID instead of C's string.
Definition class.c:1125
rb_singleton_class_attached
void rb_singleton_class_attached(VALUE klass, VALUE obj)
Attaches a singleton class to its corresponding object.
Definition class.c:709
rb_freeze_singleton_class
void rb_freeze_singleton_class(VALUE x)
This is an implementation detail of RB_OBJ_FREEZE().
Definition class.c:2268
rb_mod_included_modules
VALUE rb_mod_included_modules(VALUE mod)
Queries the list of included modules.
Definition class.c:1515
rb_define_class_id_under
VALUE rb_define_class_id_under(VALUE outer, ID id, VALUE super)
Identical to rb_define_class_under(), except it takes the name in ID instead of C's string.
Definition class.c:1051
rb_mod_ancestors
VALUE rb_mod_ancestors(VALUE mod)
Queries the module's ancestors.
Definition class.c:1583
make_metaclass
static VALUE make_metaclass(VALUE klass)
Creates a metaclass of klass
Definition class.c:766
class_alloc
static VALUE class_alloc(VALUE flags, VALUE klass)
Allocates a struct RClass for a new class.
Definition class.c:239
rb_class_inherited
VALUE rb_class_inherited(VALUE super, VALUE klass)
Calls Class::inherited.
Definition class.c:971
rb_mod_include_p
VALUE rb_mod_include_p(VALUE mod, VALUE mod2)
Queries if the passed module is included by the module.
Definition class.c:1551
rb_class_private_instance_methods
VALUE rb_class_private_instance_methods(int argc, const VALUE *argv, VALUE mod)
Identical to rb_class_instance_methods(), except it returns names of methods that are private only.
Definition class.c:1940
ENSURE_EIGENCLASS
#define ENSURE_EIGENCLASS(klass)
ensures klass belongs to its own eigenclass.
Definition class.c:752
rb_mod_init_copy
VALUE rb_mod_init_copy(VALUE clone, VALUE orig)
The comment that comes with this function says :nodoc:.
Definition class.c:533
rb_define_module_under
VALUE rb_define_module_under(VALUE outer, const char *name)
Defines a module under the namespace of outer.
Definition class.c:1119
rb_singleton_class_get
VALUE rb_singleton_class_get(VALUE obj)
Returns the singleton class of obj, or nil if obj is not a singleton object.
Definition class.c:2288
rb_define_module_id
VALUE rb_define_module_id(ID id)
This is a very badly designed API that creates an anonymous module.
Definition class.c:1089
rb_define_class_id
VALUE rb_define_class_id(ID id, VALUE super)
This is a very badly designed API that creates an anonymous class.
Definition class.c:950
rb_define_alias
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition class.c:2350
rb_extract_keywords
VALUE rb_extract_keywords(VALUE *orighash)
Splits a hash into two.
Definition class.c:2411
rb_define_attr
void rb_define_attr(VALUE klass, const char *name, int read, int write)
Defines public accessor method(s) for an attribute.
Definition class.c:2356
rb_undef_method
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
Definition class.c:2171
rb_scan_args_kw
int rb_scan_args_kw(int kw_flag, int argc, const VALUE *argv, const char *fmt,...)
Identical to rb_scan_args(), except it also accepts kw_splat.
Definition class.c:2653
rb_scan_args
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
Definition class.c:2640
rb_block_given_p
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition eval.c:937
rb_get_kwargs
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
Definition class.c:2429
TYPE
#define TYPE(_)
Old name of rb_type.
Definition value_type.h:108
FL_SINGLETON
#define FL_SINGLETON
Old name of RUBY_FL_SINGLETON.
Definition fl_type.h:58
FL_UNSET_RAW
#define FL_UNSET_RAW
Old name of RB_FL_UNSET_RAW.
Definition fl_type.h:134
OBJ_INIT_COPY
#define OBJ_INIT_COPY(obj, orig)
Old name of RB_OBJ_INIT_COPY.
Definition object.h:41
ALLOC
#define ALLOC
Old name of RB_ALLOC.
Definition memory.h:400
T_STRING
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
xfree
#define xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
T_MASK
#define T_MASK
Old name of RUBY_T_MASK.
Definition value_type.h:68
Qundef
#define Qundef
Old name of RUBY_Qundef.
Definition special_consts.h:62
T_NIL
#define T_NIL
Old name of RUBY_T_NIL.
Definition value_type.h:72
T_FLOAT
#define T_FLOAT
Old name of RUBY_T_FLOAT.
Definition value_type.h:64
ID2SYM
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
T_BIGNUM
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
Definition value_type.h:57
SPECIAL_CONST_P
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
Definition special_consts.h:56
OBJ_FREEZE
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
Definition fl_type.h:135
T_FIXNUM
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
Definition value_type.h:63
UNREACHABLE_RETURN
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
Definition assume.h:29
ZALLOC
#define ZALLOC
Old name of RB_ZALLOC.
Definition memory.h:402
CLASS_OF
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:203
xmalloc
#define xmalloc
Old name of ruby_xmalloc.
Definition xmalloc.h:53
T_MODULE
#define T_MODULE
Old name of RUBY_T_MODULE.
Definition value_type.h:70
ISDIGIT
#define ISDIGIT
Old name of rb_isdigit.
Definition ctype.h:93
T_TRUE
#define T_TRUE
Old name of RUBY_T_TRUE.
Definition value_type.h:81
T_ICLASS
#define T_ICLASS
Old name of RUBY_T_ICLASS.
Definition value_type.h:66
FL_TEST_RAW
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:132
FL_SET
#define FL_SET
Old name of RB_FL_SET.
Definition fl_type.h:129
T_FALSE
#define T_FALSE
Old name of RUBY_T_FALSE.
Definition value_type.h:61
Qtrue
#define Qtrue
Old name of RUBY_Qtrue.
Definition special_consts.h:61
Qnil
#define Qnil
Old name of RUBY_Qnil.
Definition special_consts.h:60
Qfalse
#define Qfalse
Old name of RUBY_Qfalse.
Definition special_consts.h:59
NIL_P
#define NIL_P
Old name of RB_NIL_P.
Definition special_consts.h:55
FL_WB_PROTECTED
#define FL_WB_PROTECTED
Old name of RUBY_FL_WB_PROTECTED.
Definition fl_type.h:59
T_SYMBOL
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
Definition value_type.h:80
T_CLASS
#define T_CLASS
Old name of RUBY_T_CLASS.
Definition value_type.h:58
BUILTIN_TYPE
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition value_type.h:85
FL_TEST
#define FL_TEST
Old name of RB_FL_TEST.
Definition fl_type.h:131
FL_FREEZE
#define FL_FREEZE
Old name of RUBY_FL_FREEZE.
Definition fl_type.h:67
CONST_ID
#define CONST_ID
Old name of RUBY_CONST_ID.
Definition symbol.h:47
rb_ary_new2
#define rb_ary_new2
Old name of rb_ary_new_capa.
Definition array.h:657
FL_SET_RAW
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
Definition fl_type.h:130
SYMBOL_P
#define SYMBOL_P
Old name of RB_SYMBOL_P.
Definition value_type.h:88
rb_eTypeError
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1430
rb_exc_new_str
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Identical to rb_exc_new_cstr(), except it takes a Ruby's string instead of C's.
Definition error.c:1481
rb_cClass
VALUE rb_cClass
Class class.
Definition object.c:68
rb_mKernel
VALUE rb_mKernel
Kernel module.
Definition object.c:65
rb_cRefinement
VALUE rb_cRefinement
Refinement class.
Definition object.c:69
rb_cNilClass
VALUE rb_cNilClass
NilClass class.
Definition object.c:71
rb_cHash
VALUE rb_cHash
Hash class.
Definition hash.c:113
rb_cFalseClass
VALUE rb_cFalseClass
FalseClass class.
Definition object.c:73
rb_obj_class
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:247
rb_inspect
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
Definition object.c:680
rb_cBasicObject
VALUE rb_cBasicObject
BasicObject class.
Definition object.c:64
rb_cModule
VALUE rb_cModule
Module class.
Definition object.c:67
rb_class_real
VALUE rb_class_real(VALUE klass)
Finds a "real" class.
Definition object.c:237
rb_cTrueClass
VALUE rb_cTrueClass
TrueClass class.
Definition object.c:72
RB_OBJ_WRITTEN
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
Definition gc.h:615
RB_OBJ_WRITE
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:603
rb_funcall
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1099
RGENGC_WB_PROTECTED_CLASS
#define RGENGC_WB_PROTECTED_CLASS
This is a compile-time flag to enable/disable write barrier for struct RClass.
Definition gc.h:523
UNLIMITED_ARGUMENTS
#define UNLIMITED_ARGUMENTS
This macro is used in conjunction with rb_check_arity().
Definition error.h:35
rb_check_arity
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition error.h:284
rb_block_proc
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
Definition proc.c:839
rb_str_append
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
Definition string.c:3681
rb_str_cat_cstr
#define rb_str_cat_cstr(buf, str)
Identical to rb_str_cat(), except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1656
rb_const_get
VALUE rb_const_get(VALUE space, ID name)
Identical to rb_const_defined(), except it returns the actual defined value.
Definition variable.c:3231
rb_const_set
void rb_const_set(VALUE space, ID name, VALUE val)
Names a constant.
Definition variable.c:3703
rb_const_get_at
VALUE rb_const_get_at(VALUE space, ID name)
Identical to rb_const_defined_at(), except it returns the actual defined value.
Definition variable.c:3237
rb_set_class_path_string
void rb_set_class_path_string(VALUE klass, VALUE space, VALUE name)
Identical to rb_set_class_path(), except it accepts the name as Ruby's string instead of C's.
Definition variable.c:416
rb_const_defined_at
int rb_const_defined_at(VALUE space, ID name)
Identical to rb_const_defined(), except it doesn't look for parent classes.
Definition variable.c:3559
rb_class_path
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
Definition variable.c:373
rb_const_defined
int rb_const_defined(VALUE space, ID name)
Queries if the constant is defined at the namespace.
Definition variable.c:3553
rb_alias
void rb_alias(VALUE klass, ID dst, ID src)
Resembles alias.
Definition vm_method.c:2300
rb_attr
void rb_attr(VALUE klass, ID name, int need_reader, int need_writer, int honour_visibility)
This function resembles now-deprecated Module#attr.
Definition vm_method.c:1880
rb_clear_constant_cache_for_id
void rb_clear_constant_cache_for_id(ID id)
Clears the inline constant caches associated with a particular ID.
Definition vm_method.c:140
rb_intern_const
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
Definition symbol.h:284
len
int len
Length of the buffer.
Definition io.h:8
MEMCPY
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
Definition memory.h:372
ruby::backward::cxxanyargs::type
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition cxxanyargs.hpp:56
RARRAY_LEN
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:51
RARRAY_AREF
#define RARRAY_AREF(a, i)
Definition rarray.h:403
RBASIC_CLASS
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
Definition rbasic.h:153
RBASIC
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
RCLASS_SUPER
#define RCLASS_SUPER
Just another name of rb_class_get_superclass.
Definition rclass.h:44
RHASH_SIZE
#define RHASH_SIZE(h)
Queries the size of the hash.
Definition rhash.h:69
RHASH_EMPTY_P
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
Definition rhash.h:79
RB_SCAN_ARGS_PASS_CALLED_KEYWORDS
#define RB_SCAN_ARGS_PASS_CALLED_KEYWORDS
Same behaviour as rb_scan_args().
Definition scan_args.h:50
RTEST
#define RTEST
This is an old name of RB_TEST.
Definition special_consts.h:51
ANYARGS
#define ANYARGS
Functions declared using this macro take arbitrary arguments, including void.
Definition stdarg.h:64
RClass
Definition class.h:80
clone_const_arg
Definition class.c:404
clone_method_arg
Definition class.c:391
cvc_table_copy_ctx
Definition class.c:441
method_entry_arg
Definition class.c:1782
rb_classext_struct
Definition class.h:43
rb_const_entry_struct
Definition constant.h:33
rb_cref_struct
CREF (Class REFerence)
Definition method.h:44
rb_cvar_class_tbl_entry
Definition class.h:36
rb_id_table
Definition id_table.c:41
rb_method_entry_struct
Definition method.h:54
rb_method_iseq_struct::cref
rb_cref_t * cref
class reference, should be marked
Definition method.h:136
rb_method_iseq_struct::iseqptr
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
Definition method.h:135
rb_scan_args_t
Definition class.c:2484
rb_subclass_entry
Internal header for Class.
Definition class.h:29
st_table
Definition st.h:79
subclass_traverse_data
Definition class.c:1605
ID
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
Check_Type
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
Definition value_type.h:433
RB_TYPE_P
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376