Ruby 3.5.0dev (2025-02-20 revision 34098b669c0cbc024cd08e686891f1dfe0a10aaf)
class.c (34098b669c0cbc024cd08e686891f1dfe0a10aaf)
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
81RUBY_EXTERN rb_serial_t ruby_vm_global_cvar_state;
82
84push_subclass_entry_to_list(VALUE super, VALUE klass)
85{
86 rb_subclass_entry_t *entry, *head;
87
89 entry->klass = klass;
90
91 head = RCLASS_SUBCLASSES(super);
92 if (!head) {
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
240{
241 size_t alloc_size = sizeof(struct RClass) + sizeof(rb_classext_t);
242
243 flags &= T_MASK;
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
280VALUE
282{
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
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
358VALUE
360{
361 Check_Type(super, T_CLASS);
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
372VALUE
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
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
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{
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
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: */
532VALUE
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
644VALUE
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`.
651VALUE
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
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
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
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
925}
926
927
938VALUE
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
949VALUE
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
970VALUE
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
979VALUE
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
1011VALUE
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
1017VALUE
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
1050VALUE
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
1058VALUE
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
1075VALUE
1077{
1078 return module_new(rb_cModule);
1079}
1080
1081VALUE
1083{
1084 return module_new(rb_cRefinement);
1085}
1086
1087// Kept for compatibility. Use rb_module_new() instead.
1088VALUE
1090{
1091 return rb_module_new();
1092}
1093
1094VALUE
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
1118VALUE
1119rb_define_module_under(VALUE outer, const char *name)
1120{
1121 return rb_define_module_id_under(outer, rb_intern(name));
1122}
1123
1124VALUE
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
1148VALUE
1149rb_include_class_new(VALUE module, VALUE super)
1150{
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
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{
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);
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 RCLASS_M_TBL_INIT(klass);
1430 rb_id_table_foreach(RCLASS_M_TBL(origin), cache_clear_refined_method, (void *)klass);
1431 rb_id_table_foreach(RCLASS_M_TBL(origin), move_refined_method, (void *)klass);
1432 return true;
1433 }
1434 return false;
1435}
1436
1437void
1439{
1440 int changed;
1441 bool klass_had_no_origin;
1442
1443 ensure_includable(klass, module);
1444 if (module_in_super_chain(klass, module))
1445 rb_raise(rb_eArgError, "cyclic prepend detected");
1446
1447 klass_had_no_origin = ensure_origin(klass);
1448 changed = do_include_modules_at(klass, klass, module, FALSE, false);
1449 RUBY_ASSERT(changed >= 0); // already checked for cyclic prepend above
1450 if (changed) {
1451 rb_vm_check_redefinition_by_prepend(klass);
1452 }
1453 if (RB_TYPE_P(klass, T_MODULE)) {
1454 rb_subclass_entry_t *iclass = RCLASS_SUBCLASSES(klass);
1455 // skip the placeholder subclass entry at the head of the list if it exists
1456 if (iclass) {
1457 RUBY_ASSERT(!iclass->klass);
1458 iclass = iclass->next;
1459 }
1460
1461 VALUE klass_origin = RCLASS_ORIGIN(klass);
1462 struct rb_id_table *klass_m_tbl = RCLASS_M_TBL(klass);
1463 struct rb_id_table *klass_origin_m_tbl = RCLASS_M_TBL(klass_origin);
1464 while (iclass) {
1465 /* During lazy sweeping, iclass->klass could be a dead object that
1466 * has not yet been swept. */
1467 if (!rb_objspace_garbage_object_p(iclass->klass)) {
1468 const VALUE subclass = iclass->klass;
1469 if (klass_had_no_origin && klass_origin_m_tbl == RCLASS_M_TBL(subclass)) {
1470 // backfill an origin iclass to handle refinements and future prepends
1471 rb_id_table_foreach(RCLASS_M_TBL(subclass), clear_module_cache_i, (void *)subclass);
1472 RCLASS_M_TBL(subclass) = klass_m_tbl;
1473 VALUE origin = rb_include_class_new(klass_origin, RCLASS_SUPER(subclass));
1474 RCLASS_SET_SUPER(subclass, origin);
1475 RCLASS_SET_INCLUDER(origin, RCLASS_INCLUDER(subclass));
1476 RCLASS_SET_ORIGIN(subclass, origin);
1477 RICLASS_SET_ORIGIN_SHARED_MTBL(origin);
1478 }
1479 include_modules_at(subclass, subclass, module, FALSE);
1480 }
1481
1482 iclass = iclass->next;
1483 }
1484 }
1485}
1486
1487/*
1488 * call-seq:
1489 * mod.included_modules -> array
1490 *
1491 * Returns the list of modules included or prepended in <i>mod</i>
1492 * or one of <i>mod</i>'s ancestors.
1493 *
1494 * module Sub
1495 * end
1496 *
1497 * module Mixin
1498 * prepend Sub
1499 * end
1500 *
1501 * module Outer
1502 * include Mixin
1503 * end
1504 *
1505 * Mixin.included_modules #=> [Sub]
1506 * Outer.included_modules #=> [Sub, Mixin]
1507 */
1508
1509VALUE
1511{
1512 VALUE ary = rb_ary_new();
1513 VALUE p;
1514 VALUE origin = RCLASS_ORIGIN(mod);
1515
1516 for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
1517 if (p != origin && RCLASS_ORIGIN(p) == p && BUILTIN_TYPE(p) == T_ICLASS) {
1518 VALUE m = METACLASS_OF(p);
1519 if (RB_TYPE_P(m, T_MODULE))
1520 rb_ary_push(ary, m);
1521 }
1522 }
1523 return ary;
1524}
1525
1526/*
1527 * call-seq:
1528 * mod.include?(module) -> true or false
1529 *
1530 * Returns <code>true</code> if <i>module</i> is included
1531 * or prepended in <i>mod</i> or one of <i>mod</i>'s ancestors.
1532 *
1533 * module A
1534 * end
1535 * class B
1536 * include A
1537 * end
1538 * class C < B
1539 * end
1540 * B.include?(A) #=> true
1541 * C.include?(A) #=> true
1542 * A.include?(A) #=> false
1543 */
1544
1545VALUE
1547{
1548 VALUE p;
1549
1550 Check_Type(mod2, T_MODULE);
1551 for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
1552 if (BUILTIN_TYPE(p) == T_ICLASS && !FL_TEST(p, RICLASS_IS_ORIGIN)) {
1553 if (METACLASS_OF(p) == mod2) return Qtrue;
1554 }
1555 }
1556 return Qfalse;
1557}
1558
1559/*
1560 * call-seq:
1561 * mod.ancestors -> array
1562 *
1563 * Returns a list of modules included/prepended in <i>mod</i>
1564 * (including <i>mod</i> itself).
1565 *
1566 * module Mod
1567 * include Math
1568 * include Comparable
1569 * prepend Enumerable
1570 * end
1571 *
1572 * Mod.ancestors #=> [Enumerable, Mod, Comparable, Math]
1573 * Math.ancestors #=> [Math]
1574 * Enumerable.ancestors #=> [Enumerable]
1575 */
1576
1577VALUE
1579{
1580 VALUE p, ary = rb_ary_new();
1581 VALUE refined_class = Qnil;
1582 if (BUILTIN_TYPE(mod) == T_MODULE && FL_TEST(mod, RMODULE_IS_REFINEMENT)) {
1583 refined_class = rb_refinement_module_get_refined_class(mod);
1584 }
1585
1586 for (p = mod; p; p = RCLASS_SUPER(p)) {
1587 if (p == refined_class) break;
1588 if (p != RCLASS_ORIGIN(p)) continue;
1589 if (BUILTIN_TYPE(p) == T_ICLASS) {
1590 rb_ary_push(ary, METACLASS_OF(p));
1591 }
1592 else {
1593 rb_ary_push(ary, p);
1594 }
1595 }
1596 return ary;
1597}
1598
1600{
1601 VALUE buffer;
1602 long count;
1603 long maxcount;
1604 bool immediate_only;
1605};
1606
1607static void
1608class_descendants_recursive(VALUE klass, VALUE v)
1609{
1610 struct subclass_traverse_data *data = (struct subclass_traverse_data *) v;
1611
1612 if (BUILTIN_TYPE(klass) == T_CLASS && !RCLASS_SINGLETON_P(klass)) {
1613 if (data->buffer && data->count < data->maxcount && !rb_objspace_garbage_object_p(klass)) {
1614 // assumes that this does not cause GC as long as the length does not exceed the capacity
1615 rb_ary_push(data->buffer, klass);
1616 }
1617 data->count++;
1618 if (!data->immediate_only) {
1619 rb_class_foreach_subclass(klass, class_descendants_recursive, v);
1620 }
1621 }
1622 else {
1623 rb_class_foreach_subclass(klass, class_descendants_recursive, v);
1624 }
1625}
1626
1627static VALUE
1628class_descendants(VALUE klass, bool immediate_only)
1629{
1630 struct subclass_traverse_data data = { Qfalse, 0, -1, immediate_only };
1631
1632 // estimate the count of subclasses
1633 rb_class_foreach_subclass(klass, class_descendants_recursive, (VALUE) &data);
1634
1635 // the following allocation may cause GC which may change the number of subclasses
1636 data.buffer = rb_ary_new_capa(data.count);
1637 data.maxcount = data.count;
1638 data.count = 0;
1639
1640 size_t gc_count = rb_gc_count();
1641
1642 // enumerate subclasses
1643 rb_class_foreach_subclass(klass, class_descendants_recursive, (VALUE) &data);
1644
1645 if (gc_count != rb_gc_count()) {
1646 rb_bug("GC must not occur during the subclass iteration of Class#descendants");
1647 }
1648
1649 return data.buffer;
1650}
1651
1652/*
1653 * call-seq:
1654 * subclasses -> array
1655 *
1656 * Returns an array of classes where the receiver is the
1657 * direct superclass of the class, excluding singleton classes.
1658 * The order of the returned array is not defined.
1659 *
1660 * class A; end
1661 * class B < A; end
1662 * class C < B; end
1663 * class D < A; end
1664 *
1665 * A.subclasses #=> [D, B]
1666 * B.subclasses #=> [C]
1667 * C.subclasses #=> []
1668 *
1669 * Anonymous subclasses (not associated with a constant) are
1670 * returned, too:
1671 *
1672 * c = Class.new(A)
1673 * A.subclasses # => [#<Class:0x00007f003c77bd78>, D, B]
1674 *
1675 * Note that the parent does not hold references to subclasses
1676 * and doesn't prevent them from being garbage collected. This
1677 * means that the subclass might disappear when all references
1678 * to it are dropped:
1679 *
1680 * # drop the reference to subclass, it can be garbage-collected now
1681 * c = nil
1682 *
1683 * A.subclasses
1684 * # It can be
1685 * # => [#<Class:0x00007f003c77bd78>, D, B]
1686 * # ...or just
1687 * # => [D, B]
1688 * # ...depending on whether garbage collector was run
1689 */
1690
1691VALUE
1693{
1694 return class_descendants(klass, true);
1695}
1696
1697/*
1698 * call-seq:
1699 * attached_object -> object
1700 *
1701 * Returns the object for which the receiver is the singleton class.
1702 *
1703 * Raises an TypeError if the class is not a singleton class.
1704 *
1705 * class Foo; end
1706 *
1707 * Foo.singleton_class.attached_object #=> Foo
1708 * Foo.attached_object #=> TypeError: `Foo' is not a singleton class
1709 * Foo.new.singleton_class.attached_object #=> #<Foo:0x000000010491a370>
1710 * TrueClass.attached_object #=> TypeError: `TrueClass' is not a singleton class
1711 * NilClass.attached_object #=> TypeError: `NilClass' is not a singleton class
1712 */
1713
1714VALUE
1716{
1717 if (!RCLASS_SINGLETON_P(klass)) {
1718 rb_raise(rb_eTypeError, "'%"PRIsVALUE"' is not a singleton class", klass);
1719 }
1720
1721 return RCLASS_ATTACHED_OBJECT(klass);
1722}
1723
1724static void
1725ins_methods_push(st_data_t name, st_data_t ary)
1726{
1727 rb_ary_push((VALUE)ary, ID2SYM((ID)name));
1728}
1729
1730static int
1731ins_methods_i(st_data_t name, st_data_t type, st_data_t ary)
1732{
1733 switch ((rb_method_visibility_t)type) {
1734 case METHOD_VISI_UNDEF:
1735 case METHOD_VISI_PRIVATE:
1736 break;
1737 default: /* everything but private */
1738 ins_methods_push(name, ary);
1739 break;
1740 }
1741 return ST_CONTINUE;
1742}
1743
1744static int
1745ins_methods_type_i(st_data_t name, st_data_t type, st_data_t ary, rb_method_visibility_t visi)
1746{
1747 if ((rb_method_visibility_t)type == visi) {
1748 ins_methods_push(name, ary);
1749 }
1750 return ST_CONTINUE;
1751}
1752
1753static int
1754ins_methods_prot_i(st_data_t name, st_data_t type, st_data_t ary)
1755{
1756 return ins_methods_type_i(name, type, ary, METHOD_VISI_PROTECTED);
1757}
1758
1759static int
1760ins_methods_priv_i(st_data_t name, st_data_t type, st_data_t ary)
1761{
1762 return ins_methods_type_i(name, type, ary, METHOD_VISI_PRIVATE);
1763}
1764
1765static int
1766ins_methods_pub_i(st_data_t name, st_data_t type, st_data_t ary)
1767{
1768 return ins_methods_type_i(name, type, ary, METHOD_VISI_PUBLIC);
1769}
1770
1771static int
1772ins_methods_undef_i(st_data_t name, st_data_t type, st_data_t ary)
1773{
1774 return ins_methods_type_i(name, type, ary, METHOD_VISI_UNDEF);
1775}
1776
1778 st_table *list;
1779 int recur;
1780};
1781
1782static enum rb_id_table_iterator_result
1783method_entry_i(ID key, VALUE value, void *data)
1784{
1785 const rb_method_entry_t *me = (const rb_method_entry_t *)value;
1786 struct method_entry_arg *arg = (struct method_entry_arg *)data;
1787 rb_method_visibility_t type;
1788
1789 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1790 VALUE owner = me->owner;
1791 me = rb_resolve_refined_method(Qnil, me);
1792 if (!me) return ID_TABLE_CONTINUE;
1793 if (!arg->recur && me->owner != owner) return ID_TABLE_CONTINUE;
1794 }
1795 if (!st_is_member(arg->list, key)) {
1796 if (UNDEFINED_METHOD_ENTRY_P(me)) {
1797 type = METHOD_VISI_UNDEF; /* none */
1798 }
1799 else {
1800 type = METHOD_ENTRY_VISI(me);
1801 RUBY_ASSERT(type != METHOD_VISI_UNDEF);
1802 }
1803 st_add_direct(arg->list, key, (st_data_t)type);
1804 }
1805 return ID_TABLE_CONTINUE;
1806}
1807
1808static void
1809add_instance_method_list(VALUE mod, struct method_entry_arg *me_arg)
1810{
1811 struct rb_id_table *m_tbl = RCLASS_M_TBL(mod);
1812 if (!m_tbl) return;
1813 rb_id_table_foreach(m_tbl, method_entry_i, me_arg);
1814}
1815
1816static bool
1817particular_class_p(VALUE mod)
1818{
1819 if (!mod) return false;
1820 if (RCLASS_SINGLETON_P(mod)) return true;
1821 if (BUILTIN_TYPE(mod) == T_ICLASS) return true;
1822 return false;
1823}
1824
1825static VALUE
1826class_instance_method_list(int argc, const VALUE *argv, VALUE mod, int obj, int (*func) (st_data_t, st_data_t, st_data_t))
1827{
1828 VALUE ary;
1829 int recur = TRUE, prepended = 0;
1830 struct method_entry_arg me_arg;
1831
1832 if (rb_check_arity(argc, 0, 1)) recur = RTEST(argv[0]);
1833
1834 me_arg.list = st_init_numtable();
1835 me_arg.recur = recur;
1836
1837 if (obj) {
1838 for (; particular_class_p(mod); mod = RCLASS_SUPER(mod)) {
1839 add_instance_method_list(mod, &me_arg);
1840 }
1841 }
1842
1843 if (!recur && RCLASS_ORIGIN(mod) != mod) {
1844 mod = RCLASS_ORIGIN(mod);
1845 prepended = 1;
1846 }
1847
1848 for (; mod; mod = RCLASS_SUPER(mod)) {
1849 add_instance_method_list(mod, &me_arg);
1850 if (BUILTIN_TYPE(mod) == T_ICLASS && !prepended) continue;
1851 if (!recur) break;
1852 }
1853 ary = rb_ary_new2(me_arg.list->num_entries);
1854 st_foreach(me_arg.list, func, ary);
1855 st_free_table(me_arg.list);
1856
1857 return ary;
1858}
1859
1860/*
1861 * call-seq:
1862 * mod.instance_methods(include_super=true) -> array
1863 *
1864 * Returns an array containing the names of the public and protected instance
1865 * methods in the receiver. For a module, these are the public and protected methods;
1866 * for a class, they are the instance (not singleton) methods. If the optional
1867 * parameter is <code>false</code>, the methods of any ancestors are not included.
1868 *
1869 * module A
1870 * def method1() end
1871 * end
1872 * class B
1873 * include A
1874 * def method2() end
1875 * end
1876 * class C < B
1877 * def method3() end
1878 * end
1879 *
1880 * A.instance_methods(false) #=> [:method1]
1881 * B.instance_methods(false) #=> [:method2]
1882 * B.instance_methods(true).include?(:method1) #=> true
1883 * C.instance_methods(false) #=> [:method3]
1884 * C.instance_methods.include?(:method2) #=> true
1885 *
1886 * Note that method visibility changes in the current class, as well as aliases,
1887 * are considered as methods of the current class by this method:
1888 *
1889 * class C < B
1890 * alias method4 method2
1891 * protected :method2
1892 * end
1893 * C.instance_methods(false).sort #=> [:method2, :method3, :method4]
1894 */
1895
1896VALUE
1897rb_class_instance_methods(int argc, const VALUE *argv, VALUE mod)
1898{
1899 return class_instance_method_list(argc, argv, mod, 0, ins_methods_i);
1900}
1901
1902/*
1903 * call-seq:
1904 * mod.protected_instance_methods(include_super=true) -> array
1905 *
1906 * Returns a list of the protected instance methods defined in
1907 * <i>mod</i>. If the optional parameter is <code>false</code>, the
1908 * methods of any ancestors are not included.
1909 */
1910
1911VALUE
1913{
1914 return class_instance_method_list(argc, argv, mod, 0, ins_methods_prot_i);
1915}
1916
1917/*
1918 * call-seq:
1919 * mod.private_instance_methods(include_super=true) -> array
1920 *
1921 * Returns a list of the private instance methods defined in
1922 * <i>mod</i>. If the optional parameter is <code>false</code>, the
1923 * methods of any ancestors are not included.
1924 *
1925 * module Mod
1926 * def method1() end
1927 * private :method1
1928 * def method2() end
1929 * end
1930 * Mod.instance_methods #=> [:method2]
1931 * Mod.private_instance_methods #=> [:method1]
1932 */
1933
1934VALUE
1936{
1937 return class_instance_method_list(argc, argv, mod, 0, ins_methods_priv_i);
1938}
1939
1940/*
1941 * call-seq:
1942 * mod.public_instance_methods(include_super=true) -> array
1943 *
1944 * Returns a list of the public instance methods defined in <i>mod</i>.
1945 * If the optional parameter is <code>false</code>, the methods of
1946 * any ancestors are not included.
1947 */
1948
1949VALUE
1951{
1952 return class_instance_method_list(argc, argv, mod, 0, ins_methods_pub_i);
1953}
1954
1955/*
1956 * call-seq:
1957 * mod.undefined_instance_methods -> array
1958 *
1959 * Returns a list of the undefined instance methods defined in <i>mod</i>.
1960 * The undefined methods of any ancestors are not included.
1961 */
1962
1963VALUE
1964rb_class_undefined_instance_methods(VALUE mod)
1965{
1966 VALUE include_super = Qfalse;
1967 return class_instance_method_list(1, &include_super, mod, 0, ins_methods_undef_i);
1968}
1969
1970/*
1971 * call-seq:
1972 * obj.methods(regular=true) -> array
1973 *
1974 * Returns a list of the names of public and protected methods of
1975 * <i>obj</i>. This will include all the methods accessible in
1976 * <i>obj</i>'s ancestors.
1977 * If the optional parameter is <code>false</code>, it
1978 * returns an array of <i>obj</i>'s public and protected singleton methods,
1979 * the array will not include methods in modules included in <i>obj</i>.
1980 *
1981 * class Klass
1982 * def klass_method()
1983 * end
1984 * end
1985 * k = Klass.new
1986 * k.methods[0..9] #=> [:klass_method, :nil?, :===,
1987 * # :==~, :!, :eql?
1988 * # :hash, :<=>, :class, :singleton_class]
1989 * k.methods.length #=> 56
1990 *
1991 * k.methods(false) #=> []
1992 * def k.singleton_method; end
1993 * k.methods(false) #=> [:singleton_method]
1994 *
1995 * module M123; def m123; end end
1996 * k.extend M123
1997 * k.methods(false) #=> [:singleton_method]
1998 */
1999
2000VALUE
2001rb_obj_methods(int argc, const VALUE *argv, VALUE obj)
2002{
2003 rb_check_arity(argc, 0, 1);
2004 if (argc > 0 && !RTEST(argv[0])) {
2005 return rb_obj_singleton_methods(argc, argv, obj);
2006 }
2007 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_i);
2008}
2009
2010/*
2011 * call-seq:
2012 * obj.protected_methods(all=true) -> array
2013 *
2014 * Returns the list of protected methods accessible to <i>obj</i>. If
2015 * the <i>all</i> parameter is set to <code>false</code>, only those methods
2016 * in the receiver will be listed.
2017 */
2018
2019VALUE
2020rb_obj_protected_methods(int argc, const VALUE *argv, VALUE obj)
2021{
2022 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_prot_i);
2023}
2024
2025/*
2026 * call-seq:
2027 * obj.private_methods(all=true) -> array
2028 *
2029 * Returns the list of private methods accessible to <i>obj</i>. If
2030 * the <i>all</i> parameter is set to <code>false</code>, only those methods
2031 * in the receiver will be listed.
2032 */
2033
2034VALUE
2035rb_obj_private_methods(int argc, const VALUE *argv, VALUE obj)
2036{
2037 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_priv_i);
2038}
2039
2040/*
2041 * call-seq:
2042 * obj.public_methods(all=true) -> array
2043 *
2044 * Returns the list of public methods accessible to <i>obj</i>. If
2045 * the <i>all</i> parameter is set to <code>false</code>, only those methods
2046 * in the receiver will be listed.
2047 */
2048
2049VALUE
2050rb_obj_public_methods(int argc, const VALUE *argv, VALUE obj)
2051{
2052 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_pub_i);
2053}
2054
2055/*
2056 * call-seq:
2057 * obj.singleton_methods(all=true) -> array
2058 *
2059 * Returns an array of the names of singleton methods for <i>obj</i>.
2060 * If the optional <i>all</i> parameter is true, the list will include
2061 * methods in modules included in <i>obj</i>.
2062 * Only public and protected singleton methods are returned.
2063 *
2064 * module Other
2065 * def three() end
2066 * end
2067 *
2068 * class Single
2069 * def Single.four() end
2070 * end
2071 *
2072 * a = Single.new
2073 *
2074 * def a.one()
2075 * end
2076 *
2077 * class << a
2078 * include Other
2079 * def two()
2080 * end
2081 * end
2082 *
2083 * Single.singleton_methods #=> [:four]
2084 * a.singleton_methods(false) #=> [:two, :one]
2085 * a.singleton_methods #=> [:two, :one, :three]
2086 */
2087
2088VALUE
2089rb_obj_singleton_methods(int argc, const VALUE *argv, VALUE obj)
2090{
2091 VALUE ary, klass, origin;
2092 struct method_entry_arg me_arg;
2093 struct rb_id_table *mtbl;
2094 int recur = TRUE;
2095
2096 if (rb_check_arity(argc, 0, 1)) recur = RTEST(argv[0]);
2097 if (RCLASS_SINGLETON_P(obj)) {
2098 rb_singleton_class(obj);
2099 }
2100 klass = CLASS_OF(obj);
2101 origin = RCLASS_ORIGIN(klass);
2102 me_arg.list = st_init_numtable();
2103 me_arg.recur = recur;
2104 if (klass && RCLASS_SINGLETON_P(klass)) {
2105 if ((mtbl = RCLASS_M_TBL(origin)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2106 klass = RCLASS_SUPER(klass);
2107 }
2108 if (recur) {
2109 while (klass && (RCLASS_SINGLETON_P(klass) || RB_TYPE_P(klass, T_ICLASS))) {
2110 if (klass != origin && (mtbl = RCLASS_M_TBL(klass)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2111 klass = RCLASS_SUPER(klass);
2112 }
2113 }
2114 ary = rb_ary_new2(me_arg.list->num_entries);
2115 st_foreach(me_arg.list, ins_methods_i, ary);
2116 st_free_table(me_arg.list);
2117
2118 return ary;
2119}
2120
2129#ifdef rb_define_method_id
2130#undef rb_define_method_id
2131#endif
2132void
2133rb_define_method_id(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc)
2134{
2135 rb_add_method_cfunc(klass, mid, func, argc, METHOD_VISI_PUBLIC);
2136}
2137
2138#ifdef rb_define_method
2139#undef rb_define_method
2140#endif
2141void
2142rb_define_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
2143{
2144 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PUBLIC);
2145}
2146
2147#ifdef rb_define_protected_method
2148#undef rb_define_protected_method
2149#endif
2150void
2151rb_define_protected_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
2152{
2153 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PROTECTED);
2154}
2155
2156#ifdef rb_define_private_method
2157#undef rb_define_private_method
2158#endif
2159void
2160rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
2161{
2162 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PRIVATE);
2163}
2164
2165void
2166rb_undef_method(VALUE klass, const char *name)
2167{
2168 rb_add_method(klass, rb_intern(name), VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2169}
2170
2171static enum rb_id_table_iterator_result
2172undef_method_i(ID name, VALUE value, void *data)
2173{
2174 VALUE klass = (VALUE)data;
2175 rb_add_method(klass, name, VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2176 return ID_TABLE_CONTINUE;
2177}
2178
2179void
2180rb_undef_methods_from(VALUE klass, VALUE super)
2181{
2182 struct rb_id_table *mtbl = RCLASS_M_TBL(super);
2183 if (mtbl) {
2184 rb_id_table_foreach(mtbl, undef_method_i, (void *)klass);
2185 }
2186}
2187
2196static inline VALUE
2197special_singleton_class_of(VALUE obj)
2198{
2199 switch (obj) {
2200 case Qnil: return rb_cNilClass;
2201 case Qfalse: return rb_cFalseClass;
2202 case Qtrue: return rb_cTrueClass;
2203 default: return Qnil;
2204 }
2205}
2206
2207VALUE
2208rb_special_singleton_class(VALUE obj)
2209{
2210 return special_singleton_class_of(obj);
2211}
2212
2222static VALUE
2223singleton_class_of(VALUE obj)
2224{
2225 VALUE klass;
2226
2227 switch (TYPE(obj)) {
2228 case T_FIXNUM:
2229 case T_BIGNUM:
2230 case T_FLOAT:
2231 case T_SYMBOL:
2232 rb_raise(rb_eTypeError, "can't define singleton");
2233
2234 case T_FALSE:
2235 case T_TRUE:
2236 case T_NIL:
2237 klass = special_singleton_class_of(obj);
2238 if (NIL_P(klass))
2239 rb_bug("unknown immediate %p", (void *)obj);
2240 return klass;
2241
2242 case T_STRING:
2243 if (CHILLED_STRING_P(obj)) {
2244 CHILLED_STRING_MUTATED(obj);
2245 }
2246 else if (FL_TEST_RAW(obj, RSTRING_FSTR)) {
2247 rb_raise(rb_eTypeError, "can't define singleton");
2248 }
2249 }
2250
2251 klass = METACLASS_OF(obj);
2252 if (!(RCLASS_SINGLETON_P(klass) &&
2253 RCLASS_ATTACHED_OBJECT(klass) == obj)) {
2254 klass = rb_make_metaclass(obj, klass);
2255 }
2256
2257 RB_FL_SET_RAW(klass, RB_OBJ_FROZEN_RAW(obj));
2258
2259 return klass;
2260}
2261
2262void
2264{
2265 /* should not propagate to meta-meta-class, and so on */
2266 if (!RCLASS_SINGLETON_P(x)) {
2267 VALUE klass = RBASIC_CLASS(x);
2268 if (klass && // no class when hidden from ObjectSpace
2270 OBJ_FREEZE(klass);
2271 }
2272 }
2273}
2274
2282VALUE
2284{
2285 VALUE klass;
2286
2287 if (SPECIAL_CONST_P(obj)) {
2288 return rb_special_singleton_class(obj);
2289 }
2290 klass = METACLASS_OF(obj);
2291 if (!RCLASS_SINGLETON_P(klass)) return Qnil;
2292 if (RCLASS_ATTACHED_OBJECT(klass) != obj) return Qnil;
2293 return klass;
2294}
2295
2296VALUE
2298{
2299 VALUE klass = singleton_class_of(obj);
2300
2301 /* ensures an exposed class belongs to its own eigenclass */
2302 if (RB_TYPE_P(obj, T_CLASS)) (void)ENSURE_EIGENCLASS(klass);
2303
2304 return klass;
2305}
2306
2316#ifdef rb_define_singleton_method
2317#undef rb_define_singleton_method
2318#endif
2319void
2320rb_define_singleton_method(VALUE obj, const char *name, VALUE (*func)(ANYARGS), int argc)
2321{
2322 rb_define_method(singleton_class_of(obj), name, func, argc);
2323}
2324
2325#ifdef rb_define_module_function
2326#undef rb_define_module_function
2327#endif
2328void
2329rb_define_module_function(VALUE module, const char *name, VALUE (*func)(ANYARGS), int argc)
2330{
2331 rb_define_private_method(module, name, func, argc);
2332 rb_define_singleton_method(module, name, func, argc);
2333}
2334
2335#ifdef rb_define_global_function
2336#undef rb_define_global_function
2337#endif
2338void
2339rb_define_global_function(const char *name, VALUE (*func)(ANYARGS), int argc)
2340{
2341 rb_define_module_function(rb_mKernel, name, func, argc);
2342}
2343
2344void
2345rb_define_alias(VALUE klass, const char *name1, const char *name2)
2346{
2347 rb_alias(klass, rb_intern(name1), rb_intern(name2));
2348}
2349
2350void
2351rb_define_attr(VALUE klass, const char *name, int read, int write)
2352{
2353 rb_attr(klass, rb_intern(name), read, write, FALSE);
2354}
2355
2356VALUE
2357rb_keyword_error_new(const char *error, VALUE keys)
2358{
2359 long i = 0, len = RARRAY_LEN(keys);
2360 VALUE error_message = rb_sprintf("%s keyword%.*s", error, len > 1, "s");
2361
2362 if (len > 0) {
2363 rb_str_cat_cstr(error_message, ": ");
2364 while (1) {
2365 const VALUE k = RARRAY_AREF(keys, i);
2366 rb_str_append(error_message, rb_inspect(k));
2367 if (++i >= len) break;
2368 rb_str_cat_cstr(error_message, ", ");
2369 }
2370 }
2371
2372 return rb_exc_new_str(rb_eArgError, error_message);
2373}
2374
2375NORETURN(static void rb_keyword_error(const char *error, VALUE keys));
2376static void
2377rb_keyword_error(const char *error, VALUE keys)
2378{
2379 rb_exc_raise(rb_keyword_error_new(error, keys));
2380}
2381
2382NORETURN(static void unknown_keyword_error(VALUE hash, const ID *table, int keywords));
2383static void
2384unknown_keyword_error(VALUE hash, const ID *table, int keywords)
2385{
2386 int i;
2387 for (i = 0; i < keywords; i++) {
2388 st_data_t key = ID2SYM(table[i]);
2389 rb_hash_stlike_delete(hash, &key, NULL);
2390 }
2391 rb_keyword_error("unknown", rb_hash_keys(hash));
2392}
2393
2394
2395static int
2396separate_symbol(st_data_t key, st_data_t value, st_data_t arg)
2397{
2398 VALUE *kwdhash = (VALUE *)arg;
2399 if (!SYMBOL_P(key)) kwdhash++;
2400 if (!*kwdhash) *kwdhash = rb_hash_new();
2401 rb_hash_aset(*kwdhash, (VALUE)key, (VALUE)value);
2402 return ST_CONTINUE;
2403}
2404
2405VALUE
2407{
2408 VALUE parthash[2] = {0, 0};
2409 VALUE hash = *orighash;
2410
2411 if (RHASH_EMPTY_P(hash)) {
2412 *orighash = 0;
2413 return hash;
2414 }
2415 rb_hash_foreach(hash, separate_symbol, (st_data_t)&parthash);
2416 *orighash = parthash[1];
2417 if (parthash[1] && RBASIC_CLASS(hash) != rb_cHash) {
2418 RBASIC_SET_CLASS(parthash[1], RBASIC_CLASS(hash));
2419 }
2420 return parthash[0];
2421}
2422
2423int
2424rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
2425{
2426 int i = 0, j;
2427 int rest = 0;
2428 VALUE missing = Qnil;
2429 st_data_t key;
2430
2431#define extract_kwarg(keyword, val) \
2432 (key = (st_data_t)(keyword), values ? \
2433 (rb_hash_stlike_delete(keyword_hash, &key, &(val)) || ((val) = Qundef, 0)) : \
2434 rb_hash_stlike_lookup(keyword_hash, key, NULL))
2435
2436 if (NIL_P(keyword_hash)) keyword_hash = 0;
2437
2438 if (optional < 0) {
2439 rest = 1;
2440 optional = -1-optional;
2441 }
2442 if (required) {
2443 for (; i < required; i++) {
2444 VALUE keyword = ID2SYM(table[i]);
2445 if (keyword_hash) {
2446 if (extract_kwarg(keyword, values[i])) {
2447 continue;
2448 }
2449 }
2450 if (NIL_P(missing)) missing = rb_ary_hidden_new(1);
2451 rb_ary_push(missing, keyword);
2452 }
2453 if (!NIL_P(missing)) {
2454 rb_keyword_error("missing", missing);
2455 }
2456 }
2457 j = i;
2458 if (optional && keyword_hash) {
2459 for (i = 0; i < optional; i++) {
2460 if (extract_kwarg(ID2SYM(table[required+i]), values[required+i])) {
2461 j++;
2462 }
2463 }
2464 }
2465 if (!rest && keyword_hash) {
2466 if (RHASH_SIZE(keyword_hash) > (unsigned int)(values ? 0 : j)) {
2467 unknown_keyword_error(keyword_hash, table, required+optional);
2468 }
2469 }
2470 if (values && !keyword_hash) {
2471 for (i = 0; i < required + optional; i++) {
2472 values[i] = Qundef;
2473 }
2474 }
2475 return j;
2476#undef extract_kwarg
2477}
2478
2480 int kw_flag;
2481 int n_lead;
2482 int n_opt;
2483 int n_trail;
2484 bool f_var;
2485 bool f_hash;
2486 bool f_block;
2487};
2488
2489static void
2490rb_scan_args_parse(int kw_flag, const char *fmt, struct rb_scan_args_t *arg)
2491{
2492 const char *p = fmt;
2493
2494 memset(arg, 0, sizeof(*arg));
2495 arg->kw_flag = kw_flag;
2496
2497 if (ISDIGIT(*p)) {
2498 arg->n_lead = *p - '0';
2499 p++;
2500 if (ISDIGIT(*p)) {
2501 arg->n_opt = *p - '0';
2502 p++;
2503 }
2504 }
2505 if (*p == '*') {
2506 arg->f_var = 1;
2507 p++;
2508 }
2509 if (ISDIGIT(*p)) {
2510 arg->n_trail = *p - '0';
2511 p++;
2512 }
2513 if (*p == ':') {
2514 arg->f_hash = 1;
2515 p++;
2516 }
2517 if (*p == '&') {
2518 arg->f_block = 1;
2519 p++;
2520 }
2521 if (*p != '\0') {
2522 rb_fatal("bad scan arg format: %s", fmt);
2523 }
2524}
2525
2526static int
2527rb_scan_args_assign(const struct rb_scan_args_t *arg, int argc, const VALUE *const argv, va_list vargs)
2528{
2529 int i, argi = 0;
2530 VALUE *var, hash = Qnil;
2531#define rb_scan_args_next_param() va_arg(vargs, VALUE *)
2532 const int kw_flag = arg->kw_flag;
2533 const int n_lead = arg->n_lead;
2534 const int n_opt = arg->n_opt;
2535 const int n_trail = arg->n_trail;
2536 const int n_mand = n_lead + n_trail;
2537 const bool f_var = arg->f_var;
2538 const bool f_hash = arg->f_hash;
2539 const bool f_block = arg->f_block;
2540
2541 /* capture an option hash - phase 1: pop from the argv */
2542 if (f_hash && argc > 0) {
2543 VALUE last = argv[argc - 1];
2544 if (rb_scan_args_keyword_p(kw_flag, last)) {
2545 hash = rb_hash_dup(last);
2546 argc--;
2547 }
2548 }
2549
2550 if (argc < n_mand) {
2551 goto argc_error;
2552 }
2553
2554 /* capture leading mandatory arguments */
2555 for (i = 0; i < n_lead; i++) {
2556 var = rb_scan_args_next_param();
2557 if (var) *var = argv[argi];
2558 argi++;
2559 }
2560 /* capture optional arguments */
2561 for (i = 0; i < n_opt; i++) {
2562 var = rb_scan_args_next_param();
2563 if (argi < argc - n_trail) {
2564 if (var) *var = argv[argi];
2565 argi++;
2566 }
2567 else {
2568 if (var) *var = Qnil;
2569 }
2570 }
2571 /* capture variable length arguments */
2572 if (f_var) {
2573 int n_var = argc - argi - n_trail;
2574
2575 var = rb_scan_args_next_param();
2576 if (0 < n_var) {
2577 if (var) *var = rb_ary_new_from_values(n_var, &argv[argi]);
2578 argi += n_var;
2579 }
2580 else {
2581 if (var) *var = rb_ary_new();
2582 }
2583 }
2584 /* capture trailing mandatory arguments */
2585 for (i = 0; i < n_trail; i++) {
2586 var = rb_scan_args_next_param();
2587 if (var) *var = argv[argi];
2588 argi++;
2589 }
2590 /* capture an option hash - phase 2: assignment */
2591 if (f_hash) {
2592 var = rb_scan_args_next_param();
2593 if (var) *var = hash;
2594 }
2595 /* capture iterator block */
2596 if (f_block) {
2597 var = rb_scan_args_next_param();
2598 if (rb_block_given_p()) {
2599 *var = rb_block_proc();
2600 }
2601 else {
2602 *var = Qnil;
2603 }
2604 }
2605
2606 if (argi == argc) {
2607 return argc;
2608 }
2609
2610 argc_error:
2611 return -(argc + 1);
2612#undef rb_scan_args_next_param
2613}
2614
2615static int
2616rb_scan_args_result(const struct rb_scan_args_t *const arg, int argc)
2617{
2618 const int n_lead = arg->n_lead;
2619 const int n_opt = arg->n_opt;
2620 const int n_trail = arg->n_trail;
2621 const int n_mand = n_lead + n_trail;
2622 const bool f_var = arg->f_var;
2623
2624 if (argc >= 0) {
2625 return argc;
2626 }
2627
2628 argc = -argc - 1;
2629 rb_error_arity(argc, n_mand, f_var ? UNLIMITED_ARGUMENTS : n_mand + n_opt);
2631}
2632
2633#undef rb_scan_args
2634int
2635rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...)
2636{
2637 va_list vargs;
2638 struct rb_scan_args_t arg;
2639 rb_scan_args_parse(RB_SCAN_ARGS_PASS_CALLED_KEYWORDS, fmt, &arg);
2640 va_start(vargs,fmt);
2641 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
2642 va_end(vargs);
2643 return rb_scan_args_result(&arg, argc);
2644}
2645
2646#undef rb_scan_args_kw
2647int
2648rb_scan_args_kw(int kw_flag, int argc, const VALUE *argv, const char *fmt, ...)
2649{
2650 va_list vargs;
2651 struct rb_scan_args_t arg;
2652 rb_scan_args_parse(kw_flag, fmt, &arg);
2653 va_start(vargs,fmt);
2654 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
2655 va_end(vargs);
2656 return rb_scan_args_result(&arg, argc);
2657}
2658
#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:883
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
User-defined flag.
Definition fl_type.h:329
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:1912
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
Definition class.c:1187
VALUE rb_refinement_new(void)
Creates a new, anonymous refinement.
Definition class.c:1082
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition class.c:980
VALUE rb_class_new(VALUE super)
Creates a new, anonymous class.
Definition class.c:359
static VALUE make_singleton_class(VALUE obj)
Creates a singleton class for obj.
Definition class.c:801
VALUE rb_singleton_class_clone(VALUE obj)
Clones a singleton class.
Definition class.c:645
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:1438
VALUE rb_class_subclasses(VALUE klass)
Queries the class's direct descendants.
Definition class.c:1692
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
Definition class.c:2297
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition class.c:1012
VALUE rb_class_attached_object(VALUE klass)
Returns the attached object for a singleton class.
Definition class.c:1715
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:2089
VALUE rb_module_new(void)
Creates a new, anonymous module.
Definition class.c:1076
#define META_CLASS_OF_CLASS_CLASS_P(k)
whether k is a meta^(n)-class of Class class
Definition class.c:721
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:1897
void rb_check_inheritable(VALUE super)
Asserts that the given class can derive a child class.
Definition class.c:344
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:1950
VALUE rb_class_boot(VALUE super)
A utility function that wraps class_alloc.
Definition class.c:281
VALUE rb_define_module(const char *name)
Defines a top-level module.
Definition class.c:1095
void rb_class_modify_check(VALUE klass)
Asserts that klass is not a frozen class.
Definition eval.c:418
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
void rb_singleton_class_attached(VALUE klass, VALUE obj)
Attaches a singleton class to its corresponding object.
Definition class.c:709
void rb_freeze_singleton_class(VALUE x)
This is an implementation detail of RB_OBJ_FREEZE().
Definition class.c:2263
VALUE rb_mod_included_modules(VALUE mod)
Queries the list of included modules.
Definition class.c:1510
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
VALUE rb_mod_ancestors(VALUE mod)
Queries the module's ancestors.
Definition class.c:1578
static VALUE make_metaclass(VALUE klass)
Creates a metaclass of klass
Definition class.c:766
static VALUE class_alloc(VALUE flags, VALUE klass)
Allocates a struct RClass for a new class.
Definition class.c:239
VALUE rb_class_inherited(VALUE super, VALUE klass)
Calls Class::inherited.
Definition class.c:971
VALUE rb_mod_include_p(VALUE mod, VALUE mod2)
Queries if the passed module is included by the module.
Definition class.c:1546
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:1935
#define ENSURE_EIGENCLASS(klass)
ensures klass belongs to its own eigenclass.
Definition class.c:752
VALUE rb_mod_init_copy(VALUE clone, VALUE orig)
The comment that comes with this function says :nodoc:.
Definition class.c:533
VALUE rb_define_module_under(VALUE outer, const char *name)
Defines a module under the namespace of outer.
Definition class.c:1119
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:2283
VALUE rb_define_module_id(ID id)
This is a very badly designed API that creates an anonymous module.
Definition class.c:1089
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
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition class.c:2345
VALUE rb_extract_keywords(VALUE *orighash)
Splits a hash into two.
Definition class.c:2406
void rb_define_attr(VALUE klass, const char *name, int read, int write)
Defines public accessor method(s) for an attribute.
Definition class.c:2351
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
Definition class.c:2166
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:2648
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:2635
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition eval.c:936
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
Definition class.c:2424
#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 FL_UNSET_RAW
Old name of RB_FL_UNSET_RAW.
Definition fl_type.h:134
#define OBJ_INIT_COPY(obj, orig)
Old name of RB_OBJ_INIT_COPY.
Definition object.h:41
#define ALLOC
Old name of RB_ALLOC.
Definition memory.h:400
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
#define xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
#define T_MASK
Old name of RUBY_T_MASK.
Definition value_type.h:68
#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:135
#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 CLASS_OF
Old name of rb_class_of.
Definition globals.h:203
#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:132
#define FL_SET
Old name of RB_FL_SET.
Definition fl_type.h:129
#define T_FALSE
Old name of RUBY_T_FALSE.
Definition value_type.h:61
#define Qtrue
Old name of RUBY_Qtrue.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define NIL_P
Old name of RB_NIL_P.
#define FL_WB_PROTECTED
Old name of RUBY_FL_WB_PROTECTED.
Definition fl_type.h:59
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
Definition value_type.h:80
#define T_CLASS
Old name of RUBY_T_CLASS.
Definition value_type.h:58
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition value_type.h:85
#define FL_TEST
Old name of RB_FL_TEST.
Definition fl_type.h:131
#define FL_FREEZE
Old name of RUBY_FL_FREEZE.
Definition fl_type.h:67
#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:130
#define SYMBOL_P
Old name of RB_SYMBOL_P.
Definition value_type.h:88
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition eval.c:675
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1430
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
VALUE rb_cClass
Class class.
Definition object.c:68
VALUE rb_mKernel
Kernel module.
Definition object.c:65
VALUE rb_cRefinement
Refinement class.
Definition object.c:69
VALUE rb_cNilClass
NilClass class.
Definition object.c:71
VALUE rb_cHash
Hash class.
Definition hash.c:113
VALUE rb_cFalseClass
FalseClass class.
Definition object.c:73
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:247
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
Definition object.c:680
VALUE rb_cBasicObject
BasicObject class.
Definition object.c:64
VALUE rb_cModule
Module class.
Definition object.c:67
VALUE rb_class_real(VALUE klass)
Finds a "real" class.
Definition object.c:237
VALUE rb_cTrueClass
TrueClass class.
Definition object.c:72
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
Definition gc.h:615
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:603
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1099
#define RGENGC_WB_PROTECTED_CLASS
This is a compile-time flag to enable/disable write barrier for struct RClass.
Definition gc.h:523
#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:845
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:3677
#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
VALUE rb_const_get(VALUE space, ID name)
Identical to rb_const_defined(), except it returns the actual defined value.
Definition variable.c:3135
void rb_const_set(VALUE space, ID name, VALUE val)
Names a constant.
Definition variable.c:3604
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:3141
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:336
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:3463
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
Definition variable.c:293
int rb_const_defined(VALUE space, ID name)
Queries if the constant is defined at the namespace.
Definition variable.c:3457
void rb_alias(VALUE klass, ID dst, ID src)
Resembles alias.
Definition vm_method.c:2282
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:1862
void rb_clear_constant_cache_for_id(ID id)
Clears the inline constant caches associated with a particular ID.
Definition vm_method.c:138
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
Definition symbol.h:284
int len
Length of the buffer.
Definition io.h:8
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
Definition memory.h:372
VALUE type(ANYARGS)
ANYARGS-ed function type.
void rb_hash_foreach(VALUE q, int_type *w, VALUE e)
Iteration over the given hash.
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:51
#define RARRAY_AREF(a, i)
Definition rarray.h:403
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
Definition rbasic.h:150
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
#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:80
Definition class.c:1777
Definition constant.h:33
CREF (Class REFerence)
Definition method.h:44
Definition class.h:36
Definition method.h:54
rb_cref_t * cref
class reference, should be marked
Definition method.h:136
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
Definition method.h:135
Internal header for Class.
Definition class.h:29
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