Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)
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
961rb_define_class_id_under_no_pin(VALUE outer, ID id, VALUE super)
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
979 return klass;
980 }
981 if (!super) {
982 rb_raise(rb_eArgError, "no super class for `%"PRIsVALUE"::%"PRIsVALUE"'",
983 rb_class_path(outer), rb_id2str(id));
984 }
985 klass = rb_define_class_id(id, super);
986 rb_set_class_path_string(klass, outer, rb_id2str(id));
987 rb_const_set(outer, id, klass);
988 rb_class_inherited(super, klass);
989
990 return klass;
991}
992
993VALUE
995{
996 VALUE klass = rb_define_class_id_under_no_pin(outer, id, super);
997 rb_vm_add_root_module(klass);
998 return klass;
999}
1000
1001VALUE
1002rb_module_s_alloc(VALUE klass)
1003{
1004 VALUE mod = class_alloc(T_MODULE, klass);
1005 RCLASS_M_TBL_INIT(mod);
1006 FL_SET(mod, RMODULE_ALLOCATED_BUT_NOT_INITIALIZED);
1007 return mod;
1008}
1009
1010static inline VALUE
1011module_new(VALUE klass)
1012{
1013 VALUE mdl = class_alloc(T_MODULE, klass);
1014 RCLASS_M_TBL_INIT(mdl);
1015 return (VALUE)mdl;
1016}
1017
1018VALUE
1020{
1021 return module_new(rb_cModule);
1022}
1023
1024VALUE
1026{
1027 return module_new(rb_cRefinement);
1028}
1029
1030// Kept for compatibility. Use rb_module_new() instead.
1031VALUE
1033{
1034 return rb_module_new();
1035}
1036
1037VALUE
1038rb_define_module(const char *name)
1039{
1040 VALUE module;
1041 ID id;
1042
1043 id = rb_intern(name);
1044 if (rb_const_defined(rb_cObject, id)) {
1045 module = rb_const_get(rb_cObject, id);
1046 if (!RB_TYPE_P(module, T_MODULE)) {
1047 rb_raise(rb_eTypeError, "%s is not a module (%"PRIsVALUE")",
1048 name, rb_obj_class(module));
1049 }
1050 /* Module may have been defined in Ruby and not pin-rooted */
1051 rb_vm_add_root_module(module);
1052 return module;
1053 }
1054 module = rb_module_new();
1055 rb_vm_add_root_module(module);
1056 rb_const_set(rb_cObject, id, module);
1057
1058 return module;
1059}
1060
1061VALUE
1062rb_define_module_under(VALUE outer, const char *name)
1063{
1064 return rb_define_module_id_under(outer, rb_intern(name));
1065}
1066
1067VALUE
1069{
1070 VALUE module;
1071
1072 if (rb_const_defined_at(outer, id)) {
1073 module = rb_const_get_at(outer, id);
1074 if (!RB_TYPE_P(module, T_MODULE)) {
1075 rb_raise(rb_eTypeError, "%"PRIsVALUE"::%"PRIsVALUE" is not a module"
1076 " (%"PRIsVALUE")",
1077 outer, rb_id2str(id), rb_obj_class(module));
1078 }
1079 /* Module may have been defined in Ruby and not pin-rooted */
1080 rb_gc_register_mark_object(module);
1081 return module;
1082 }
1083 module = rb_module_new();
1084 rb_const_set(outer, id, module);
1085 rb_set_class_path_string(module, outer, rb_id2str(id));
1086 rb_gc_register_mark_object(module);
1087
1088 return module;
1089}
1090
1091VALUE
1092rb_include_class_new(VALUE module, VALUE super)
1093{
1095
1096 RCLASS_M_TBL(klass) = RCLASS_M_TBL(module);
1097
1098 RCLASS_SET_ORIGIN(klass, klass);
1099 if (BUILTIN_TYPE(module) == T_ICLASS) {
1100 module = METACLASS_OF(module);
1101 }
1102 RUBY_ASSERT(!RB_TYPE_P(module, T_ICLASS));
1103 if (!RCLASS_CONST_TBL(module)) {
1104 RCLASS_CONST_TBL(module) = rb_id_table_create(0);
1105 }
1106
1107 RCLASS_CVC_TBL(klass) = RCLASS_CVC_TBL(module);
1108 RCLASS_CONST_TBL(klass) = RCLASS_CONST_TBL(module);
1109
1110 RCLASS_SET_SUPER(klass, super);
1111 RBASIC_SET_CLASS(klass, module);
1112
1113 return (VALUE)klass;
1114}
1115
1116static int include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super);
1117
1118static void
1119ensure_includable(VALUE klass, VALUE module)
1120{
1121 rb_class_modify_check(klass);
1122 Check_Type(module, T_MODULE);
1123 rb_module_set_initialized(module);
1124 if (!NIL_P(rb_refinement_module_get_refined_class(module))) {
1125 rb_raise(rb_eArgError, "refinement module is not allowed");
1126 }
1127}
1128
1129void
1131{
1132 int changed = 0;
1133
1134 ensure_includable(klass, module);
1135
1136 changed = include_modules_at(klass, RCLASS_ORIGIN(klass), module, TRUE);
1137 if (changed < 0)
1138 rb_raise(rb_eArgError, "cyclic include detected");
1139
1140 if (RB_TYPE_P(klass, T_MODULE)) {
1141 rb_subclass_entry_t *iclass = RCLASS_SUBCLASSES(klass);
1142 // skip the placeholder subclass entry at the head of the list
1143 if (iclass) {
1144 RUBY_ASSERT(!iclass->klass);
1145 iclass = iclass->next;
1146 }
1147
1148 int do_include = 1;
1149 while (iclass) {
1150 VALUE check_class = iclass->klass;
1151 /* During lazy sweeping, iclass->klass could be a dead object that
1152 * has not yet been swept. */
1153 if (!rb_objspace_garbage_object_p(check_class)) {
1154 while (check_class) {
1155 RUBY_ASSERT(!rb_objspace_garbage_object_p(check_class));
1156
1157 if (RB_TYPE_P(check_class, T_ICLASS) &&
1158 (METACLASS_OF(check_class) == module)) {
1159 do_include = 0;
1160 }
1161 check_class = RCLASS_SUPER(check_class);
1162 }
1163
1164 if (do_include) {
1165 include_modules_at(iclass->klass, RCLASS_ORIGIN(iclass->klass), module, TRUE);
1166 }
1167 }
1168
1169 iclass = iclass->next;
1170 }
1171 }
1172}
1173
1174static enum rb_id_table_iterator_result
1175add_refined_method_entry_i(ID key, VALUE value, void *data)
1176{
1177 rb_add_refined_method_entry((VALUE)data, key);
1178 return ID_TABLE_CONTINUE;
1179}
1180
1181static enum rb_id_table_iterator_result
1182clear_module_cache_i(ID id, VALUE val, void *data)
1183{
1184 VALUE klass = (VALUE)data;
1185 rb_clear_method_cache(klass, id);
1186 return ID_TABLE_CONTINUE;
1187}
1188
1189static bool
1190module_in_super_chain(const VALUE klass, VALUE module)
1191{
1192 struct rb_id_table *const klass_m_tbl = RCLASS_M_TBL(RCLASS_ORIGIN(klass));
1193 if (klass_m_tbl) {
1194 while (module) {
1195 if (klass_m_tbl == RCLASS_M_TBL(module))
1196 return true;
1197 module = RCLASS_SUPER(module);
1198 }
1199 }
1200 return false;
1201}
1202
1203// For each ID key in the class constant table, we're going to clear the VM's
1204// inline constant caches associated with it.
1205static enum rb_id_table_iterator_result
1206clear_constant_cache_i(ID id, VALUE value, void *data)
1207{
1209 return ID_TABLE_CONTINUE;
1210}
1211
1212static int
1213do_include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super, bool check_cyclic)
1214{
1215 VALUE p, iclass, origin_stack = 0;
1216 int method_changed = 0, add_subclass;
1217 long origin_len;
1218 VALUE klass_origin = RCLASS_ORIGIN(klass);
1219 VALUE original_klass = klass;
1220
1221 if (check_cyclic && module_in_super_chain(klass, module))
1222 return -1;
1223
1224 while (module) {
1225 int c_seen = FALSE;
1226 int superclass_seen = FALSE;
1227 struct rb_id_table *tbl;
1228
1229 if (klass == c) {
1230 c_seen = TRUE;
1231 }
1232 if (klass_origin != c || search_super) {
1233 /* ignore if the module included already in superclasses for include,
1234 * ignore if the module included before origin class for prepend
1235 */
1236 for (p = RCLASS_SUPER(klass); p; p = RCLASS_SUPER(p)) {
1237 int type = BUILTIN_TYPE(p);
1238 if (klass_origin == p && !search_super)
1239 break;
1240 if (c == p)
1241 c_seen = TRUE;
1242 if (type == T_ICLASS) {
1243 if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) {
1244 if (!superclass_seen && c_seen) {
1245 c = p; /* move insertion point */
1246 }
1247 goto skip;
1248 }
1249 }
1250 else if (type == T_CLASS) {
1251 superclass_seen = TRUE;
1252 }
1253 }
1254 }
1255
1256 VALUE super_class = RCLASS_SUPER(c);
1257
1258 // invalidate inline method cache
1259 RB_DEBUG_COUNTER_INC(cvar_include_invalidate);
1260 ruby_vm_global_cvar_state++;
1261 tbl = RCLASS_M_TBL(module);
1262 if (tbl && rb_id_table_size(tbl)) {
1263 if (search_super) { // include
1264 if (super_class && !RB_TYPE_P(super_class, T_MODULE)) {
1265 rb_id_table_foreach(tbl, clear_module_cache_i, (void *)super_class);
1266 }
1267 }
1268 else { // prepend
1269 if (!RB_TYPE_P(original_klass, T_MODULE)) {
1270 rb_id_table_foreach(tbl, clear_module_cache_i, (void *)original_klass);
1271 }
1272 }
1273 method_changed = 1;
1274 }
1275
1276 // setup T_ICLASS for the include/prepend module
1277 iclass = rb_include_class_new(module, super_class);
1278 c = RCLASS_SET_SUPER(c, iclass);
1279 RCLASS_SET_INCLUDER(iclass, klass);
1280 add_subclass = TRUE;
1281 if (module != RCLASS_ORIGIN(module)) {
1282 if (!origin_stack) origin_stack = rb_ary_hidden_new(2);
1283 VALUE origin[2] = {iclass, RCLASS_ORIGIN(module)};
1284 rb_ary_cat(origin_stack, origin, 2);
1285 }
1286 else if (origin_stack && (origin_len = RARRAY_LEN(origin_stack)) > 1 &&
1287 RARRAY_AREF(origin_stack, origin_len - 1) == module) {
1288 RCLASS_SET_ORIGIN(RARRAY_AREF(origin_stack, (origin_len -= 2)), iclass);
1289 RICLASS_SET_ORIGIN_SHARED_MTBL(iclass);
1290 rb_ary_resize(origin_stack, origin_len);
1291 add_subclass = FALSE;
1292 }
1293
1294 if (add_subclass) {
1295 VALUE m = module;
1296 if (BUILTIN_TYPE(m) == T_ICLASS) m = METACLASS_OF(m);
1297 rb_module_add_to_subclasses_list(m, iclass);
1298 }
1299
1300 if (BUILTIN_TYPE(klass) == T_MODULE && FL_TEST(klass, RMODULE_IS_REFINEMENT)) {
1301 VALUE refined_class =
1302 rb_refinement_module_get_refined_class(klass);
1303
1304 rb_id_table_foreach(RCLASS_M_TBL(module), add_refined_method_entry_i, (void *)refined_class);
1306 }
1307
1308 tbl = RCLASS_CONST_TBL(module);
1309 if (tbl && rb_id_table_size(tbl))
1310 rb_id_table_foreach(tbl, clear_constant_cache_i, NULL);
1311 skip:
1312 module = RCLASS_SUPER(module);
1313 }
1314
1315 return method_changed;
1316}
1317
1318static int
1319include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super)
1320{
1321 return do_include_modules_at(klass, c, module, search_super, true);
1322}
1323
1324static enum rb_id_table_iterator_result
1325move_refined_method(ID key, VALUE value, void *data)
1326{
1327 rb_method_entry_t *me = (rb_method_entry_t *)value;
1328
1329 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1330 VALUE klass = (VALUE)data;
1331 struct rb_id_table *tbl = RCLASS_M_TBL(klass);
1332
1333 if (me->def->body.refined.orig_me) {
1334 const rb_method_entry_t *orig_me = me->def->body.refined.orig_me, *new_me;
1335 RB_OBJ_WRITE(me, &me->def->body.refined.orig_me, NULL);
1336 new_me = rb_method_entry_clone(me);
1337 rb_method_table_insert(klass, tbl, key, new_me);
1338 rb_method_entry_copy(me, orig_me);
1339 return ID_TABLE_CONTINUE;
1340 }
1341 else {
1342 rb_method_table_insert(klass, tbl, key, me);
1343 return ID_TABLE_DELETE;
1344 }
1345 }
1346 else {
1347 return ID_TABLE_CONTINUE;
1348 }
1349}
1350
1351static enum rb_id_table_iterator_result
1352cache_clear_refined_method(ID key, VALUE value, void *data)
1353{
1354 rb_method_entry_t *me = (rb_method_entry_t *) value;
1355
1356 if (me->def->type == VM_METHOD_TYPE_REFINED && me->def->body.refined.orig_me) {
1357 VALUE klass = (VALUE)data;
1358 rb_clear_method_cache(klass, me->called_id);
1359 }
1360 // Refined method entries without an orig_me is going to stay in the method
1361 // table of klass, like before the move, so no need to clear the cache.
1362
1363 return ID_TABLE_CONTINUE;
1364}
1365
1366static bool
1367ensure_origin(VALUE klass)
1368{
1369 VALUE origin = RCLASS_ORIGIN(klass);
1370 if (origin == klass) {
1371 origin = class_alloc(T_ICLASS, klass);
1372 RCLASS_SET_SUPER(origin, RCLASS_SUPER(klass));
1373 RCLASS_SET_SUPER(klass, origin);
1374 RCLASS_SET_ORIGIN(klass, origin);
1375 RCLASS_M_TBL(origin) = RCLASS_M_TBL(klass);
1376 RCLASS_M_TBL_INIT(klass);
1377 rb_id_table_foreach(RCLASS_M_TBL(origin), cache_clear_refined_method, (void *)klass);
1378 rb_id_table_foreach(RCLASS_M_TBL(origin), move_refined_method, (void *)klass);
1379 return true;
1380 }
1381 return false;
1382}
1383
1384void
1386{
1387 int changed;
1388 bool klass_had_no_origin;
1389
1390 ensure_includable(klass, module);
1391 if (module_in_super_chain(klass, module))
1392 rb_raise(rb_eArgError, "cyclic prepend detected");
1393
1394 klass_had_no_origin = ensure_origin(klass);
1395 changed = do_include_modules_at(klass, klass, module, FALSE, false);
1396 RUBY_ASSERT(changed >= 0); // already checked for cyclic prepend above
1397 if (changed) {
1398 rb_vm_check_redefinition_by_prepend(klass);
1399 }
1400 if (RB_TYPE_P(klass, T_MODULE)) {
1401 rb_subclass_entry_t *iclass = RCLASS_SUBCLASSES(klass);
1402 // skip the placeholder subclass entry at the head of the list if it exists
1403 if (iclass) {
1404 RUBY_ASSERT(!iclass->klass);
1405 iclass = iclass->next;
1406 }
1407
1408 VALUE klass_origin = RCLASS_ORIGIN(klass);
1409 struct rb_id_table *klass_m_tbl = RCLASS_M_TBL(klass);
1410 struct rb_id_table *klass_origin_m_tbl = RCLASS_M_TBL(klass_origin);
1411 while (iclass) {
1412 /* During lazy sweeping, iclass->klass could be a dead object that
1413 * has not yet been swept. */
1414 if (!rb_objspace_garbage_object_p(iclass->klass)) {
1415 const VALUE subclass = iclass->klass;
1416 if (klass_had_no_origin && klass_origin_m_tbl == RCLASS_M_TBL(subclass)) {
1417 // backfill an origin iclass to handle refinements and future prepends
1418 rb_id_table_foreach(RCLASS_M_TBL(subclass), clear_module_cache_i, (void *)subclass);
1419 RCLASS_M_TBL(subclass) = klass_m_tbl;
1420 VALUE origin = rb_include_class_new(klass_origin, RCLASS_SUPER(subclass));
1421 RCLASS_SET_SUPER(subclass, origin);
1422 RCLASS_SET_INCLUDER(origin, RCLASS_INCLUDER(subclass));
1423 RCLASS_SET_ORIGIN(subclass, origin);
1424 RICLASS_SET_ORIGIN_SHARED_MTBL(origin);
1425 }
1426 include_modules_at(subclass, subclass, module, FALSE);
1427 }
1428
1429 iclass = iclass->next;
1430 }
1431 }
1432}
1433
1434/*
1435 * call-seq:
1436 * mod.included_modules -> array
1437 *
1438 * Returns the list of modules included or prepended in <i>mod</i>
1439 * or one of <i>mod</i>'s ancestors.
1440 *
1441 * module Sub
1442 * end
1443 *
1444 * module Mixin
1445 * prepend Sub
1446 * end
1447 *
1448 * module Outer
1449 * include Mixin
1450 * end
1451 *
1452 * Mixin.included_modules #=> [Sub]
1453 * Outer.included_modules #=> [Sub, Mixin]
1454 */
1455
1456VALUE
1458{
1459 VALUE ary = rb_ary_new();
1460 VALUE p;
1461 VALUE origin = RCLASS_ORIGIN(mod);
1462
1463 for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
1464 if (p != origin && RCLASS_ORIGIN(p) == p && BUILTIN_TYPE(p) == T_ICLASS) {
1465 VALUE m = METACLASS_OF(p);
1466 if (RB_TYPE_P(m, T_MODULE))
1467 rb_ary_push(ary, m);
1468 }
1469 }
1470 return ary;
1471}
1472
1473/*
1474 * call-seq:
1475 * mod.include?(module) -> true or false
1476 *
1477 * Returns <code>true</code> if <i>module</i> is included
1478 * or prepended in <i>mod</i> or one of <i>mod</i>'s ancestors.
1479 *
1480 * module A
1481 * end
1482 * class B
1483 * include A
1484 * end
1485 * class C < B
1486 * end
1487 * B.include?(A) #=> true
1488 * C.include?(A) #=> true
1489 * A.include?(A) #=> false
1490 */
1491
1492VALUE
1494{
1495 VALUE p;
1496
1497 Check_Type(mod2, T_MODULE);
1498 for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
1499 if (BUILTIN_TYPE(p) == T_ICLASS && !FL_TEST(p, RICLASS_IS_ORIGIN)) {
1500 if (METACLASS_OF(p) == mod2) return Qtrue;
1501 }
1502 }
1503 return Qfalse;
1504}
1505
1506/*
1507 * call-seq:
1508 * mod.ancestors -> array
1509 *
1510 * Returns a list of modules included/prepended in <i>mod</i>
1511 * (including <i>mod</i> itself).
1512 *
1513 * module Mod
1514 * include Math
1515 * include Comparable
1516 * prepend Enumerable
1517 * end
1518 *
1519 * Mod.ancestors #=> [Enumerable, Mod, Comparable, Math]
1520 * Math.ancestors #=> [Math]
1521 * Enumerable.ancestors #=> [Enumerable]
1522 */
1523
1524VALUE
1526{
1527 VALUE p, ary = rb_ary_new();
1528 VALUE refined_class = Qnil;
1529 if (BUILTIN_TYPE(mod) == T_MODULE && FL_TEST(mod, RMODULE_IS_REFINEMENT)) {
1530 refined_class = rb_refinement_module_get_refined_class(mod);
1531 }
1532
1533 for (p = mod; p; p = RCLASS_SUPER(p)) {
1534 if (p == refined_class) break;
1535 if (p != RCLASS_ORIGIN(p)) continue;
1536 if (BUILTIN_TYPE(p) == T_ICLASS) {
1537 rb_ary_push(ary, METACLASS_OF(p));
1538 }
1539 else {
1540 rb_ary_push(ary, p);
1541 }
1542 }
1543 return ary;
1544}
1545
1547{
1548 VALUE buffer;
1549 long count;
1550 long maxcount;
1551 bool immediate_only;
1552};
1553
1554static void
1555class_descendants_recursive(VALUE klass, VALUE v)
1556{
1557 struct subclass_traverse_data *data = (struct subclass_traverse_data *) v;
1558
1559 if (BUILTIN_TYPE(klass) == T_CLASS && !FL_TEST(klass, FL_SINGLETON)) {
1560 if (data->buffer && data->count < data->maxcount && !rb_objspace_garbage_object_p(klass)) {
1561 // assumes that this does not cause GC as long as the length does not exceed the capacity
1562 rb_ary_push(data->buffer, klass);
1563 }
1564 data->count++;
1565 if (!data->immediate_only) {
1566 rb_class_foreach_subclass(klass, class_descendants_recursive, v);
1567 }
1568 }
1569 else {
1570 rb_class_foreach_subclass(klass, class_descendants_recursive, v);
1571 }
1572}
1573
1574static VALUE
1575class_descendants(VALUE klass, bool immediate_only)
1576{
1577 struct subclass_traverse_data data = { Qfalse, 0, -1, immediate_only };
1578
1579 // estimate the count of subclasses
1580 rb_class_foreach_subclass(klass, class_descendants_recursive, (VALUE) &data);
1581
1582 // the following allocation may cause GC which may change the number of subclasses
1583 data.buffer = rb_ary_new_capa(data.count);
1584 data.maxcount = data.count;
1585 data.count = 0;
1586
1587 size_t gc_count = rb_gc_count();
1588
1589 // enumerate subclasses
1590 rb_class_foreach_subclass(klass, class_descendants_recursive, (VALUE) &data);
1591
1592 if (gc_count != rb_gc_count()) {
1593 rb_bug("GC must not occur during the subclass iteration of Class#descendants");
1594 }
1595
1596 return data.buffer;
1597}
1598
1599/*
1600 * call-seq:
1601 * subclasses -> array
1602 *
1603 * Returns an array of classes where the receiver is the
1604 * direct superclass of the class, excluding singleton classes.
1605 * The order of the returned array is not defined.
1606 *
1607 * class A; end
1608 * class B < A; end
1609 * class C < B; end
1610 * class D < A; end
1611 *
1612 * A.subclasses #=> [D, B]
1613 * B.subclasses #=> [C]
1614 * C.subclasses #=> []
1615 *
1616 * Anonymous subclasses (not associated with a constant) are
1617 * returned, too:
1618 *
1619 * c = Class.new(A)
1620 * A.subclasses # => [#<Class:0x00007f003c77bd78>, D, B]
1621 *
1622 * Note that the parent does not hold references to subclasses
1623 * and doesn't prevent them from being garbage collected. This
1624 * means that the subclass might disappear when all references
1625 * to it are dropped:
1626 *
1627 * # drop the reference to subclass, it can be garbage-collected now
1628 * c = nil
1629 *
1630 * A.subclasses
1631 * # It can be
1632 * # => [#<Class:0x00007f003c77bd78>, D, B]
1633 * # ...or just
1634 * # => [D, B]
1635 * # ...depending on whether garbage collector was run
1636 */
1637
1638VALUE
1640{
1641 return class_descendants(klass, true);
1642}
1643
1644/*
1645 * call-seq:
1646 * attached_object -> object
1647 *
1648 * Returns the object for which the receiver is the singleton class.
1649 *
1650 * Raises an TypeError if the class is not a singleton class.
1651 *
1652 * class Foo; end
1653 *
1654 * Foo.singleton_class.attached_object #=> Foo
1655 * Foo.attached_object #=> TypeError: `Foo' is not a singleton class
1656 * Foo.new.singleton_class.attached_object #=> #<Foo:0x000000010491a370>
1657 * TrueClass.attached_object #=> TypeError: `TrueClass' is not a singleton class
1658 * NilClass.attached_object #=> TypeError: `NilClass' is not a singleton class
1659 */
1660
1661VALUE
1663{
1664 if (!FL_TEST(klass, FL_SINGLETON)) {
1665 rb_raise(rb_eTypeError, "`%"PRIsVALUE"' is not a singleton class", klass);
1666 }
1667
1668 return rb_attr_get(klass, id_attached);
1669}
1670
1671static void
1672ins_methods_push(st_data_t name, st_data_t ary)
1673{
1674 rb_ary_push((VALUE)ary, ID2SYM((ID)name));
1675}
1676
1677static int
1678ins_methods_i(st_data_t name, st_data_t type, st_data_t ary)
1679{
1680 switch ((rb_method_visibility_t)type) {
1681 case METHOD_VISI_UNDEF:
1682 case METHOD_VISI_PRIVATE:
1683 break;
1684 default: /* everything but private */
1685 ins_methods_push(name, ary);
1686 break;
1687 }
1688 return ST_CONTINUE;
1689}
1690
1691static int
1692ins_methods_type_i(st_data_t name, st_data_t type, st_data_t ary, rb_method_visibility_t visi)
1693{
1694 if ((rb_method_visibility_t)type == visi) {
1695 ins_methods_push(name, ary);
1696 }
1697 return ST_CONTINUE;
1698}
1699
1700static int
1701ins_methods_prot_i(st_data_t name, st_data_t type, st_data_t ary)
1702{
1703 return ins_methods_type_i(name, type, ary, METHOD_VISI_PROTECTED);
1704}
1705
1706static int
1707ins_methods_priv_i(st_data_t name, st_data_t type, st_data_t ary)
1708{
1709 return ins_methods_type_i(name, type, ary, METHOD_VISI_PRIVATE);
1710}
1711
1712static int
1713ins_methods_pub_i(st_data_t name, st_data_t type, st_data_t ary)
1714{
1715 return ins_methods_type_i(name, type, ary, METHOD_VISI_PUBLIC);
1716}
1717
1718static int
1719ins_methods_undef_i(st_data_t name, st_data_t type, st_data_t ary)
1720{
1721 return ins_methods_type_i(name, type, ary, METHOD_VISI_UNDEF);
1722}
1723
1725 st_table *list;
1726 int recur;
1727};
1728
1729static enum rb_id_table_iterator_result
1730method_entry_i(ID key, VALUE value, void *data)
1731{
1732 const rb_method_entry_t *me = (const rb_method_entry_t *)value;
1733 struct method_entry_arg *arg = (struct method_entry_arg *)data;
1734 rb_method_visibility_t type;
1735
1736 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1737 VALUE owner = me->owner;
1738 me = rb_resolve_refined_method(Qnil, me);
1739 if (!me) return ID_TABLE_CONTINUE;
1740 if (!arg->recur && me->owner != owner) return ID_TABLE_CONTINUE;
1741 }
1742 if (!st_is_member(arg->list, key)) {
1743 if (UNDEFINED_METHOD_ENTRY_P(me)) {
1744 type = METHOD_VISI_UNDEF; /* none */
1745 }
1746 else {
1747 type = METHOD_ENTRY_VISI(me);
1748 RUBY_ASSERT(type != METHOD_VISI_UNDEF);
1749 }
1750 st_add_direct(arg->list, key, (st_data_t)type);
1751 }
1752 return ID_TABLE_CONTINUE;
1753}
1754
1755static void
1756add_instance_method_list(VALUE mod, struct method_entry_arg *me_arg)
1757{
1758 struct rb_id_table *m_tbl = RCLASS_M_TBL(mod);
1759 if (!m_tbl) return;
1760 rb_id_table_foreach(m_tbl, method_entry_i, me_arg);
1761}
1762
1763static bool
1764particular_class_p(VALUE mod)
1765{
1766 if (!mod) return false;
1767 if (FL_TEST(mod, FL_SINGLETON)) return true;
1768 if (BUILTIN_TYPE(mod) == T_ICLASS) return true;
1769 return false;
1770}
1771
1772static VALUE
1773class_instance_method_list(int argc, const VALUE *argv, VALUE mod, int obj, int (*func) (st_data_t, st_data_t, st_data_t))
1774{
1775 VALUE ary;
1776 int recur = TRUE, prepended = 0;
1777 struct method_entry_arg me_arg;
1778
1779 if (rb_check_arity(argc, 0, 1)) recur = RTEST(argv[0]);
1780
1781 me_arg.list = st_init_numtable();
1782 me_arg.recur = recur;
1783
1784 if (obj) {
1785 for (; particular_class_p(mod); mod = RCLASS_SUPER(mod)) {
1786 add_instance_method_list(mod, &me_arg);
1787 }
1788 }
1789
1790 if (!recur && RCLASS_ORIGIN(mod) != mod) {
1791 mod = RCLASS_ORIGIN(mod);
1792 prepended = 1;
1793 }
1794
1795 for (; mod; mod = RCLASS_SUPER(mod)) {
1796 add_instance_method_list(mod, &me_arg);
1797 if (BUILTIN_TYPE(mod) == T_ICLASS && !prepended) continue;
1798 if (!recur) break;
1799 }
1800 ary = rb_ary_new2(me_arg.list->num_entries);
1801 st_foreach(me_arg.list, func, ary);
1802 st_free_table(me_arg.list);
1803
1804 return ary;
1805}
1806
1807/*
1808 * call-seq:
1809 * mod.instance_methods(include_super=true) -> array
1810 *
1811 * Returns an array containing the names of the public and protected instance
1812 * methods in the receiver. For a module, these are the public and protected methods;
1813 * for a class, they are the instance (not singleton) methods. If the optional
1814 * parameter is <code>false</code>, the methods of any ancestors are not included.
1815 *
1816 * module A
1817 * def method1() end
1818 * end
1819 * class B
1820 * include A
1821 * def method2() end
1822 * end
1823 * class C < B
1824 * def method3() end
1825 * end
1826 *
1827 * A.instance_methods(false) #=> [:method1]
1828 * B.instance_methods(false) #=> [:method2]
1829 * B.instance_methods(true).include?(:method1) #=> true
1830 * C.instance_methods(false) #=> [:method3]
1831 * C.instance_methods.include?(:method2) #=> true
1832 *
1833 * Note that method visibility changes in the current class, as well as aliases,
1834 * are considered as methods of the current class by this method:
1835 *
1836 * class C < B
1837 * alias method4 method2
1838 * protected :method2
1839 * end
1840 * C.instance_methods(false).sort #=> [:method2, :method3, :method4]
1841 */
1842
1843VALUE
1844rb_class_instance_methods(int argc, const VALUE *argv, VALUE mod)
1845{
1846 return class_instance_method_list(argc, argv, mod, 0, ins_methods_i);
1847}
1848
1849/*
1850 * call-seq:
1851 * mod.protected_instance_methods(include_super=true) -> array
1852 *
1853 * Returns a list of the protected instance methods defined in
1854 * <i>mod</i>. If the optional parameter is <code>false</code>, the
1855 * methods of any ancestors are not included.
1856 */
1857
1858VALUE
1860{
1861 return class_instance_method_list(argc, argv, mod, 0, ins_methods_prot_i);
1862}
1863
1864/*
1865 * call-seq:
1866 * mod.private_instance_methods(include_super=true) -> array
1867 *
1868 * Returns a list of the private instance methods defined in
1869 * <i>mod</i>. If the optional parameter is <code>false</code>, the
1870 * methods of any ancestors are not included.
1871 *
1872 * module Mod
1873 * def method1() end
1874 * private :method1
1875 * def method2() end
1876 * end
1877 * Mod.instance_methods #=> [:method2]
1878 * Mod.private_instance_methods #=> [:method1]
1879 */
1880
1881VALUE
1883{
1884 return class_instance_method_list(argc, argv, mod, 0, ins_methods_priv_i);
1885}
1886
1887/*
1888 * call-seq:
1889 * mod.public_instance_methods(include_super=true) -> array
1890 *
1891 * Returns a list of the public instance methods defined in <i>mod</i>.
1892 * If the optional parameter is <code>false</code>, the methods of
1893 * any ancestors are not included.
1894 */
1895
1896VALUE
1898{
1899 return class_instance_method_list(argc, argv, mod, 0, ins_methods_pub_i);
1900}
1901
1902/*
1903 * call-seq:
1904 * mod.undefined_instance_methods -> array
1905 *
1906 * Returns a list of the undefined instance methods defined in <i>mod</i>.
1907 * The undefined methods of any ancestors are not included.
1908 */
1909
1910VALUE
1911rb_class_undefined_instance_methods(VALUE mod)
1912{
1913 VALUE include_super = Qfalse;
1914 return class_instance_method_list(1, &include_super, mod, 0, ins_methods_undef_i);
1915}
1916
1917/*
1918 * call-seq:
1919 * obj.methods(regular=true) -> array
1920 *
1921 * Returns a list of the names of public and protected methods of
1922 * <i>obj</i>. This will include all the methods accessible in
1923 * <i>obj</i>'s ancestors.
1924 * If the optional parameter is <code>false</code>, it
1925 * returns an array of <i>obj</i>'s public and protected singleton methods,
1926 * the array will not include methods in modules included in <i>obj</i>.
1927 *
1928 * class Klass
1929 * def klass_method()
1930 * end
1931 * end
1932 * k = Klass.new
1933 * k.methods[0..9] #=> [:klass_method, :nil?, :===,
1934 * # :==~, :!, :eql?
1935 * # :hash, :<=>, :class, :singleton_class]
1936 * k.methods.length #=> 56
1937 *
1938 * k.methods(false) #=> []
1939 * def k.singleton_method; end
1940 * k.methods(false) #=> [:singleton_method]
1941 *
1942 * module M123; def m123; end end
1943 * k.extend M123
1944 * k.methods(false) #=> [:singleton_method]
1945 */
1946
1947VALUE
1948rb_obj_methods(int argc, const VALUE *argv, VALUE obj)
1949{
1950 rb_check_arity(argc, 0, 1);
1951 if (argc > 0 && !RTEST(argv[0])) {
1952 return rb_obj_singleton_methods(argc, argv, obj);
1953 }
1954 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_i);
1955}
1956
1957/*
1958 * call-seq:
1959 * obj.protected_methods(all=true) -> array
1960 *
1961 * Returns the list of protected methods accessible to <i>obj</i>. If
1962 * the <i>all</i> parameter is set to <code>false</code>, only those methods
1963 * in the receiver will be listed.
1964 */
1965
1966VALUE
1967rb_obj_protected_methods(int argc, const VALUE *argv, VALUE obj)
1968{
1969 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_prot_i);
1970}
1971
1972/*
1973 * call-seq:
1974 * obj.private_methods(all=true) -> array
1975 *
1976 * Returns the list of private methods accessible to <i>obj</i>. If
1977 * the <i>all</i> parameter is set to <code>false</code>, only those methods
1978 * in the receiver will be listed.
1979 */
1980
1981VALUE
1982rb_obj_private_methods(int argc, const VALUE *argv, VALUE obj)
1983{
1984 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_priv_i);
1985}
1986
1987/*
1988 * call-seq:
1989 * obj.public_methods(all=true) -> array
1990 *
1991 * Returns the list of public methods accessible to <i>obj</i>. If
1992 * the <i>all</i> parameter is set to <code>false</code>, only those methods
1993 * in the receiver will be listed.
1994 */
1995
1996VALUE
1997rb_obj_public_methods(int argc, const VALUE *argv, VALUE obj)
1998{
1999 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_pub_i);
2000}
2001
2002/*
2003 * call-seq:
2004 * obj.singleton_methods(all=true) -> array
2005 *
2006 * Returns an array of the names of singleton methods for <i>obj</i>.
2007 * If the optional <i>all</i> parameter is true, the list will include
2008 * methods in modules included in <i>obj</i>.
2009 * Only public and protected singleton methods are returned.
2010 *
2011 * module Other
2012 * def three() end
2013 * end
2014 *
2015 * class Single
2016 * def Single.four() end
2017 * end
2018 *
2019 * a = Single.new
2020 *
2021 * def a.one()
2022 * end
2023 *
2024 * class << a
2025 * include Other
2026 * def two()
2027 * end
2028 * end
2029 *
2030 * Single.singleton_methods #=> [:four]
2031 * a.singleton_methods(false) #=> [:two, :one]
2032 * a.singleton_methods #=> [:two, :one, :three]
2033 */
2034
2035VALUE
2036rb_obj_singleton_methods(int argc, const VALUE *argv, VALUE obj)
2037{
2038 VALUE ary, klass, origin;
2039 struct method_entry_arg me_arg;
2040 struct rb_id_table *mtbl;
2041 int recur = TRUE;
2042
2043 if (rb_check_arity(argc, 0, 1)) recur = RTEST(argv[0]);
2044 if (RB_TYPE_P(obj, T_CLASS) && FL_TEST(obj, FL_SINGLETON)) {
2045 rb_singleton_class(obj);
2046 }
2047 klass = CLASS_OF(obj);
2048 origin = RCLASS_ORIGIN(klass);
2049 me_arg.list = st_init_numtable();
2050 me_arg.recur = recur;
2051 if (klass && FL_TEST(klass, FL_SINGLETON)) {
2052 if ((mtbl = RCLASS_M_TBL(origin)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2053 klass = RCLASS_SUPER(klass);
2054 }
2055 if (recur) {
2056 while (klass && (FL_TEST(klass, FL_SINGLETON) || RB_TYPE_P(klass, T_ICLASS))) {
2057 if (klass != origin && (mtbl = RCLASS_M_TBL(klass)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2058 klass = RCLASS_SUPER(klass);
2059 }
2060 }
2061 ary = rb_ary_new2(me_arg.list->num_entries);
2062 st_foreach(me_arg.list, ins_methods_i, ary);
2063 st_free_table(me_arg.list);
2064
2065 return ary;
2066}
2067
2076#ifdef rb_define_method_id
2077#undef rb_define_method_id
2078#endif
2079void
2080rb_define_method_id(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc)
2081{
2082 rb_add_method_cfunc(klass, mid, func, argc, METHOD_VISI_PUBLIC);
2083}
2084
2085#ifdef rb_define_method
2086#undef rb_define_method
2087#endif
2088void
2089rb_define_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
2090{
2091 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PUBLIC);
2092}
2093
2094#ifdef rb_define_protected_method
2095#undef rb_define_protected_method
2096#endif
2097void
2098rb_define_protected_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
2099{
2100 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PROTECTED);
2101}
2102
2103#ifdef rb_define_private_method
2104#undef rb_define_private_method
2105#endif
2106void
2107rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
2108{
2109 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PRIVATE);
2110}
2111
2112void
2113rb_undef_method(VALUE klass, const char *name)
2114{
2115 rb_add_method(klass, rb_intern(name), VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2116}
2117
2118static enum rb_id_table_iterator_result
2119undef_method_i(ID name, VALUE value, void *data)
2120{
2121 VALUE klass = (VALUE)data;
2122 rb_add_method(klass, name, VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2123 return ID_TABLE_CONTINUE;
2124}
2125
2126void
2127rb_undef_methods_from(VALUE klass, VALUE super)
2128{
2129 struct rb_id_table *mtbl = RCLASS_M_TBL(super);
2130 if (mtbl) {
2131 rb_id_table_foreach(mtbl, undef_method_i, (void *)klass);
2132 }
2133}
2134
2143static inline VALUE
2144special_singleton_class_of(VALUE obj)
2145{
2146 switch (obj) {
2147 case Qnil: return rb_cNilClass;
2148 case Qfalse: return rb_cFalseClass;
2149 case Qtrue: return rb_cTrueClass;
2150 default: return Qnil;
2151 }
2152}
2153
2154VALUE
2155rb_special_singleton_class(VALUE obj)
2156{
2157 return special_singleton_class_of(obj);
2158}
2159
2169static VALUE
2170singleton_class_of(VALUE obj)
2171{
2172 VALUE klass;
2173
2174 switch (TYPE(obj)) {
2175 case T_FIXNUM:
2176 case T_BIGNUM:
2177 case T_FLOAT:
2178 case T_SYMBOL:
2179 rb_raise(rb_eTypeError, "can't define singleton");
2180
2181 case T_FALSE:
2182 case T_TRUE:
2183 case T_NIL:
2184 klass = special_singleton_class_of(obj);
2185 if (NIL_P(klass))
2186 rb_bug("unknown immediate %p", (void *)obj);
2187 return klass;
2188
2189 case T_STRING:
2190 if (FL_TEST_RAW(obj, RSTRING_FSTR)) {
2191 rb_raise(rb_eTypeError, "can't define singleton");
2192 }
2193 }
2194
2195 klass = METACLASS_OF(obj);
2196 if (!(FL_TEST(klass, FL_SINGLETON) &&
2197 rb_attr_get(klass, id_attached) == obj)) {
2198 klass = rb_make_metaclass(obj, klass);
2199 }
2200
2201 RB_FL_SET_RAW(klass, RB_OBJ_FROZEN_RAW(obj));
2202
2203 return klass;
2204}
2205
2206void
2208{
2209 /* should not propagate to meta-meta-class, and so on */
2210 if (!(RBASIC(x)->flags & FL_SINGLETON)) {
2211 VALUE klass = RBASIC_CLASS(x);
2212 if (klass && // no class when hidden from ObjectSpace
2214 OBJ_FREEZE_RAW(klass);
2215 }
2216 }
2217}
2218
2226VALUE
2228{
2229 VALUE klass;
2230
2231 if (SPECIAL_CONST_P(obj)) {
2232 return rb_special_singleton_class(obj);
2233 }
2234 klass = METACLASS_OF(obj);
2235 if (!FL_TEST(klass, FL_SINGLETON)) return Qnil;
2236 if (rb_attr_get(klass, id_attached) != obj) return Qnil;
2237 return klass;
2238}
2239
2240VALUE
2242{
2243 VALUE klass = singleton_class_of(obj);
2244
2245 /* ensures an exposed class belongs to its own eigenclass */
2246 if (RB_TYPE_P(obj, T_CLASS)) (void)ENSURE_EIGENCLASS(klass);
2247
2248 return klass;
2249}
2250
2260#ifdef rb_define_singleton_method
2261#undef rb_define_singleton_method
2262#endif
2263void
2264rb_define_singleton_method(VALUE obj, const char *name, VALUE (*func)(ANYARGS), int argc)
2265{
2266 rb_define_method(singleton_class_of(obj), name, func, argc);
2267}
2268
2269#ifdef rb_define_module_function
2270#undef rb_define_module_function
2271#endif
2272void
2273rb_define_module_function(VALUE module, const char *name, VALUE (*func)(ANYARGS), int argc)
2274{
2275 rb_define_private_method(module, name, func, argc);
2276 rb_define_singleton_method(module, name, func, argc);
2277}
2278
2279#ifdef rb_define_global_function
2280#undef rb_define_global_function
2281#endif
2282void
2283rb_define_global_function(const char *name, VALUE (*func)(ANYARGS), int argc)
2284{
2285 rb_define_module_function(rb_mKernel, name, func, argc);
2286}
2287
2288void
2289rb_define_alias(VALUE klass, const char *name1, const char *name2)
2290{
2291 rb_alias(klass, rb_intern(name1), rb_intern(name2));
2292}
2293
2294void
2295rb_define_attr(VALUE klass, const char *name, int read, int write)
2296{
2297 rb_attr(klass, rb_intern(name), read, write, FALSE);
2298}
2299
2300MJIT_FUNC_EXPORTED VALUE
2301rb_keyword_error_new(const char *error, VALUE keys)
2302{
2303 long i = 0, len = RARRAY_LEN(keys);
2304 VALUE error_message = rb_sprintf("%s keyword%.*s", error, len > 1, "s");
2305
2306 if (len > 0) {
2307 rb_str_cat_cstr(error_message, ": ");
2308 while (1) {
2309 const VALUE k = RARRAY_AREF(keys, i);
2310 rb_str_append(error_message, rb_inspect(k));
2311 if (++i >= len) break;
2312 rb_str_cat_cstr(error_message, ", ");
2313 }
2314 }
2315
2316 return rb_exc_new_str(rb_eArgError, error_message);
2317}
2318
2319NORETURN(static void rb_keyword_error(const char *error, VALUE keys));
2320static void
2321rb_keyword_error(const char *error, VALUE keys)
2322{
2323 rb_exc_raise(rb_keyword_error_new(error, keys));
2324}
2325
2326NORETURN(static void unknown_keyword_error(VALUE hash, const ID *table, int keywords));
2327static void
2328unknown_keyword_error(VALUE hash, const ID *table, int keywords)
2329{
2330 int i;
2331 for (i = 0; i < keywords; i++) {
2332 st_data_t key = ID2SYM(table[i]);
2333 rb_hash_stlike_delete(hash, &key, NULL);
2334 }
2335 rb_keyword_error("unknown", rb_hash_keys(hash));
2336}
2337
2338
2339static int
2340separate_symbol(st_data_t key, st_data_t value, st_data_t arg)
2341{
2342 VALUE *kwdhash = (VALUE *)arg;
2343 if (!SYMBOL_P(key)) kwdhash++;
2344 if (!*kwdhash) *kwdhash = rb_hash_new();
2345 rb_hash_aset(*kwdhash, (VALUE)key, (VALUE)value);
2346 return ST_CONTINUE;
2347}
2348
2349VALUE
2351{
2352 VALUE parthash[2] = {0, 0};
2353 VALUE hash = *orighash;
2354
2355 if (RHASH_EMPTY_P(hash)) {
2356 *orighash = 0;
2357 return hash;
2358 }
2359 rb_hash_foreach(hash, separate_symbol, (st_data_t)&parthash);
2360 *orighash = parthash[1];
2361 if (parthash[1] && RBASIC_CLASS(hash) != rb_cHash) {
2362 RBASIC_SET_CLASS(parthash[1], RBASIC_CLASS(hash));
2363 }
2364 return parthash[0];
2365}
2366
2367int
2368rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
2369{
2370 int i = 0, j;
2371 int rest = 0;
2372 VALUE missing = Qnil;
2373 st_data_t key;
2374
2375#define extract_kwarg(keyword, val) \
2376 (key = (st_data_t)(keyword), values ? \
2377 (rb_hash_stlike_delete(keyword_hash, &key, &(val)) || ((val) = Qundef, 0)) : \
2378 rb_hash_stlike_lookup(keyword_hash, key, NULL))
2379
2380 if (NIL_P(keyword_hash)) keyword_hash = 0;
2381
2382 if (optional < 0) {
2383 rest = 1;
2384 optional = -1-optional;
2385 }
2386 if (required) {
2387 for (; i < required; i++) {
2388 VALUE keyword = ID2SYM(table[i]);
2389 if (keyword_hash) {
2390 if (extract_kwarg(keyword, values[i])) {
2391 continue;
2392 }
2393 }
2394 if (NIL_P(missing)) missing = rb_ary_hidden_new(1);
2395 rb_ary_push(missing, keyword);
2396 }
2397 if (!NIL_P(missing)) {
2398 rb_keyword_error("missing", missing);
2399 }
2400 }
2401 j = i;
2402 if (optional && keyword_hash) {
2403 for (i = 0; i < optional; i++) {
2404 if (extract_kwarg(ID2SYM(table[required+i]), values[required+i])) {
2405 j++;
2406 }
2407 }
2408 }
2409 if (!rest && keyword_hash) {
2410 if (RHASH_SIZE(keyword_hash) > (unsigned int)(values ? 0 : j)) {
2411 unknown_keyword_error(keyword_hash, table, required+optional);
2412 }
2413 }
2414 if (values && !keyword_hash) {
2415 for (i = 0; i < required + optional; i++) {
2416 values[i] = Qundef;
2417 }
2418 }
2419 return j;
2420#undef extract_kwarg
2421}
2422
2424 int kw_flag;
2425 int n_lead;
2426 int n_opt;
2427 int n_trail;
2428 bool f_var;
2429 bool f_hash;
2430 bool f_block;
2431};
2432
2433static void
2434rb_scan_args_parse(int kw_flag, const char *fmt, struct rb_scan_args_t *arg)
2435{
2436 const char *p = fmt;
2437
2438 memset(arg, 0, sizeof(*arg));
2439 arg->kw_flag = kw_flag;
2440
2441 if (ISDIGIT(*p)) {
2442 arg->n_lead = *p - '0';
2443 p++;
2444 if (ISDIGIT(*p)) {
2445 arg->n_opt = *p - '0';
2446 p++;
2447 }
2448 }
2449 if (*p == '*') {
2450 arg->f_var = 1;
2451 p++;
2452 }
2453 if (ISDIGIT(*p)) {
2454 arg->n_trail = *p - '0';
2455 p++;
2456 }
2457 if (*p == ':') {
2458 arg->f_hash = 1;
2459 p++;
2460 }
2461 if (*p == '&') {
2462 arg->f_block = 1;
2463 p++;
2464 }
2465 if (*p != '\0') {
2466 rb_fatal("bad scan arg format: %s", fmt);
2467 }
2468}
2469
2470static int
2471rb_scan_args_assign(const struct rb_scan_args_t *arg, int argc, const VALUE *const argv, va_list vargs)
2472{
2473 int i, argi = 0;
2474 VALUE *var, hash = Qnil;
2475#define rb_scan_args_next_param() va_arg(vargs, VALUE *)
2476 const int kw_flag = arg->kw_flag;
2477 const int n_lead = arg->n_lead;
2478 const int n_opt = arg->n_opt;
2479 const int n_trail = arg->n_trail;
2480 const int n_mand = n_lead + n_trail;
2481 const bool f_var = arg->f_var;
2482 const bool f_hash = arg->f_hash;
2483 const bool f_block = arg->f_block;
2484
2485 /* capture an option hash - phase 1: pop from the argv */
2486 if (f_hash && argc > 0) {
2487 VALUE last = argv[argc - 1];
2488 if (rb_scan_args_keyword_p(kw_flag, last)) {
2489 hash = rb_hash_dup(last);
2490 argc--;
2491 }
2492 }
2493
2494 if (argc < n_mand) {
2495 goto argc_error;
2496 }
2497
2498 /* capture leading mandatory arguments */
2499 for (i = 0; i < n_lead; i++) {
2500 var = rb_scan_args_next_param();
2501 if (var) *var = argv[argi];
2502 argi++;
2503 }
2504 /* capture optional arguments */
2505 for (i = 0; i < n_opt; i++) {
2506 var = rb_scan_args_next_param();
2507 if (argi < argc - n_trail) {
2508 if (var) *var = argv[argi];
2509 argi++;
2510 }
2511 else {
2512 if (var) *var = Qnil;
2513 }
2514 }
2515 /* capture variable length arguments */
2516 if (f_var) {
2517 int n_var = argc - argi - n_trail;
2518
2519 var = rb_scan_args_next_param();
2520 if (0 < n_var) {
2521 if (var) *var = rb_ary_new_from_values(n_var, &argv[argi]);
2522 argi += n_var;
2523 }
2524 else {
2525 if (var) *var = rb_ary_new();
2526 }
2527 }
2528 /* capture trailing mandatory arguments */
2529 for (i = 0; i < n_trail; i++) {
2530 var = rb_scan_args_next_param();
2531 if (var) *var = argv[argi];
2532 argi++;
2533 }
2534 /* capture an option hash - phase 2: assignment */
2535 if (f_hash) {
2536 var = rb_scan_args_next_param();
2537 if (var) *var = hash;
2538 }
2539 /* capture iterator block */
2540 if (f_block) {
2541 var = rb_scan_args_next_param();
2542 if (rb_block_given_p()) {
2543 *var = rb_block_proc();
2544 }
2545 else {
2546 *var = Qnil;
2547 }
2548 }
2549
2550 if (argi == argc) {
2551 return argc;
2552 }
2553
2554 argc_error:
2555 return -(argc + 1);
2556#undef rb_scan_args_next_param
2557}
2558
2559static int
2560rb_scan_args_result(const struct rb_scan_args_t *const arg, int argc)
2561{
2562 const int n_lead = arg->n_lead;
2563 const int n_opt = arg->n_opt;
2564 const int n_trail = arg->n_trail;
2565 const int n_mand = n_lead + n_trail;
2566 const bool f_var = arg->f_var;
2567
2568 if (argc >= 0) {
2569 return argc;
2570 }
2571
2572 argc = -argc - 1;
2573 rb_error_arity(argc, n_mand, f_var ? UNLIMITED_ARGUMENTS : n_mand + n_opt);
2575}
2576
2577#undef rb_scan_args
2578int
2579rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...)
2580{
2581 va_list vargs;
2582 struct rb_scan_args_t arg;
2583 rb_scan_args_parse(RB_SCAN_ARGS_PASS_CALLED_KEYWORDS, fmt, &arg);
2584 va_start(vargs,fmt);
2585 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
2586 va_end(vargs);
2587 return rb_scan_args_result(&arg, argc);
2588}
2589
2590#undef rb_scan_args_kw
2591int
2592rb_scan_args_kw(int kw_flag, int argc, const VALUE *argv, const char *fmt, ...)
2593{
2594 va_list vargs;
2595 struct rb_scan_args_t arg;
2596 rb_scan_args_parse(kw_flag, fmt, &arg);
2597 va_start(vargs,fmt);
2598 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
2599 va_end(vargs);
2600 return rb_scan_args_result(&arg, argc);
2601}
2602
#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:1859
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
Definition class.c:1130
VALUE rb_refinement_new(void)
Creates a new, anonymous refinement.
Definition class.c:1025
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:1385
VALUE rb_class_subclasses(VALUE klass)
Queries the class's direct descendants.
Definition class.c:1639
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
Definition class.c:2241
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:1662
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:2036
VALUE rb_module_new(void)
Creates a new, anonymous module.
Definition class.c:1019
#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:1844
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:1897
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:1038
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:1068
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:2207
VALUE rb_mod_included_modules(VALUE mod)
Queries the list of included modules.
Definition class.c:1457
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:994
VALUE rb_mod_ancestors(VALUE mod)
Queries the module's ancestors.
Definition class.c:1525
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:1493
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:1882
#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:1062
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:2227
VALUE rb_define_module_id(ID id)
This is a very badly designed API that creates an anonymous module.
Definition class.c:1032
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:2289
VALUE rb_extract_keywords(VALUE *orighash)
Splits a hash into two.
Definition class.c:2350
void rb_define_attr(VALUE klass, const char *name, int read, int write)
Defines public accessor method(s) for an attribute.
Definition class.c:2295
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
Definition class.c:2113
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:2592
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:2579
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:2368
#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:191
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
Definition object.c:601
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:181
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:1724
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