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