Ruby 3.2.2p53 (2023-03-30 revision e51014f9c05aa65cbf203442d37fef7c12390015)
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
407static void
408copy_tables(VALUE clone, VALUE orig)
409{
410 if (RCLASS_CONST_TBL(clone)) {
411 rb_free_const_table(RCLASS_CONST_TBL(clone));
412 RCLASS_CONST_TBL(clone) = 0;
413 }
414 RCLASS_M_TBL(clone) = 0;
415 if (!RB_TYPE_P(clone, T_ICLASS)) {
416 st_data_t id;
417
418 rb_iv_tbl_copy(clone, orig);
419 CONST_ID(id, "__tmp_classpath__");
420 rb_attr_delete(clone, id);
421 CONST_ID(id, "__classpath__");
422 rb_attr_delete(clone, id);
423 }
424 if (RCLASS_CONST_TBL(orig)) {
425 struct clone_const_arg arg;
426
427 arg.tbl = RCLASS_CONST_TBL(clone) = rb_id_table_create(0);
428 arg.klass = clone;
429 rb_id_table_foreach(RCLASS_CONST_TBL(orig), clone_const_i, &arg);
430 }
431}
432
433static bool ensure_origin(VALUE klass);
434
438enum {RMODULE_ALLOCATED_BUT_NOT_INITIALIZED = RUBY_FL_USER5};
439
440static inline bool
441RMODULE_UNINITIALIZED(VALUE module)
442{
443 return FL_TEST_RAW(module, RMODULE_ALLOCATED_BUT_NOT_INITIALIZED);
444}
445
446void
447rb_module_set_initialized(VALUE mod)
448{
449 FL_UNSET_RAW(mod, RMODULE_ALLOCATED_BUT_NOT_INITIALIZED);
450 /* no more re-initialization */
451}
452
453void
454rb_module_check_initializable(VALUE mod)
455{
456 if (!RMODULE_UNINITIALIZED(mod)) {
457 rb_raise(rb_eTypeError, "already initialized module");
458 }
459}
460
461/* :nodoc: */
462VALUE
464{
465 switch (BUILTIN_TYPE(clone)) {
466 case T_CLASS:
467 case T_ICLASS:
468 class_init_copy_check(clone, orig);
469 break;
470 case T_MODULE:
471 rb_module_check_initializable(clone);
472 break;
473 default:
474 break;
475 }
476 if (!OBJ_INIT_COPY(clone, orig)) return clone;
477
478 /* cloned flag is refer at constant inline cache
479 * see vm_get_const_key_cref() in vm_insnhelper.c
480 */
481 FL_SET(clone, RCLASS_CLONED);
482 FL_SET(orig , RCLASS_CLONED);
483
484 if (!FL_TEST(CLASS_OF(clone), FL_SINGLETON)) {
485 RBASIC_SET_CLASS(clone, rb_singleton_class_clone(orig));
486 rb_singleton_class_attached(METACLASS_OF(clone), (VALUE)clone);
487 }
488 RCLASS_ALLOCATOR(clone) = RCLASS_ALLOCATOR(orig);
489 copy_tables(clone, orig);
490 if (RCLASS_M_TBL(orig)) {
491 struct clone_method_arg arg;
492 arg.old_klass = orig;
493 arg.new_klass = clone;
494 RCLASS_M_TBL_INIT(clone);
495 rb_id_table_foreach(RCLASS_M_TBL(orig), clone_method_i, &arg);
496 }
497
498 if (RCLASS_ORIGIN(orig) == orig) {
499 RCLASS_SET_SUPER(clone, RCLASS_SUPER(orig));
500 }
501 else {
502 VALUE p = RCLASS_SUPER(orig);
503 VALUE orig_origin = RCLASS_ORIGIN(orig);
504 VALUE prev_clone_p = clone;
505 VALUE origin_stack = rb_ary_hidden_new(2);
506 VALUE origin[2];
507 VALUE clone_p = 0;
508 long origin_len;
509 int add_subclass;
510 VALUE clone_origin;
511
512 ensure_origin(clone);
513 clone_origin = RCLASS_ORIGIN(clone);
514
515 while (p && p != orig_origin) {
516 if (BUILTIN_TYPE(p) != T_ICLASS) {
517 rb_bug("non iclass between module/class and origin");
518 }
519 clone_p = class_alloc(RBASIC(p)->flags, METACLASS_OF(p));
520 RCLASS_SET_SUPER(prev_clone_p, clone_p);
521 prev_clone_p = clone_p;
522 RCLASS_M_TBL(clone_p) = RCLASS_M_TBL(p);
523 RCLASS_CONST_TBL(clone_p) = RCLASS_CONST_TBL(p);
524 RCLASS_ALLOCATOR(clone_p) = RCLASS_ALLOCATOR(p);
525 if (RB_TYPE_P(clone, T_CLASS)) {
526 RCLASS_SET_INCLUDER(clone_p, clone);
527 }
528 add_subclass = TRUE;
529 if (p != RCLASS_ORIGIN(p)) {
530 origin[0] = clone_p;
531 origin[1] = RCLASS_ORIGIN(p);
532 rb_ary_cat(origin_stack, origin, 2);
533 }
534 else if ((origin_len = RARRAY_LEN(origin_stack)) > 1 &&
535 RARRAY_AREF(origin_stack, origin_len - 1) == p) {
536 RCLASS_SET_ORIGIN(RARRAY_AREF(origin_stack, (origin_len -= 2)), clone_p);
537 RICLASS_SET_ORIGIN_SHARED_MTBL(clone_p);
538 rb_ary_resize(origin_stack, origin_len);
539 add_subclass = FALSE;
540 }
541 if (add_subclass) {
542 rb_module_add_to_subclasses_list(METACLASS_OF(p), clone_p);
543 }
544 p = RCLASS_SUPER(p);
545 }
546
547 if (p == orig_origin) {
548 if (clone_p) {
549 RCLASS_SET_SUPER(clone_p, clone_origin);
550 RCLASS_SET_SUPER(clone_origin, RCLASS_SUPER(orig_origin));
551 }
552 copy_tables(clone_origin, orig_origin);
553 if (RCLASS_M_TBL(orig_origin)) {
554 struct clone_method_arg arg;
555 arg.old_klass = orig;
556 arg.new_klass = clone;
557 RCLASS_M_TBL_INIT(clone_origin);
558 rb_id_table_foreach(RCLASS_M_TBL(orig_origin), clone_method_i, &arg);
559 }
560 }
561 else {
562 rb_bug("no origin for class that has origin");
563 }
564
565 rb_class_update_superclasses(clone);
566 }
567
568 return clone;
569}
570
571VALUE
573{
574 return rb_singleton_class_clone_and_attach(obj, Qundef);
575}
576
577// Clone and return the singleton class of `obj` if it has been created and is attached to `obj`.
578VALUE
579rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach)
580{
581 const VALUE klass = METACLASS_OF(obj);
582
583 // Note that `rb_singleton_class()` can create situations where `klass` is
584 // attached to an object other than `obj`. In which case `obj` does not have
585 // a material singleton class attached yet and there is no singleton class
586 // to clone.
587 if (!(FL_TEST(klass, FL_SINGLETON) && rb_attr_get(klass, id_attached) == obj)) {
588 // nothing to clone
589 return klass;
590 }
591 else {
592 /* copy singleton(unnamed) class */
593 bool klass_of_clone_is_new;
594 VALUE clone = class_alloc(RBASIC(klass)->flags, 0);
595
596 if (BUILTIN_TYPE(obj) == T_CLASS) {
597 klass_of_clone_is_new = true;
598 RBASIC_SET_CLASS(clone, clone);
599 }
600 else {
601 VALUE klass_metaclass_clone = rb_singleton_class_clone(klass);
602 // When `METACLASS_OF(klass) == klass_metaclass_clone`, it means the
603 // recursive call did not clone `METACLASS_OF(klass)`.
604 klass_of_clone_is_new = (METACLASS_OF(klass) != klass_metaclass_clone);
605 RBASIC_SET_CLASS(clone, klass_metaclass_clone);
606 }
607
608 RCLASS_SET_SUPER(clone, RCLASS_SUPER(klass));
609 RCLASS_ALLOCATOR(clone) = RCLASS_ALLOCATOR(klass);
610 rb_iv_tbl_copy(clone, klass);
611 if (RCLASS_CONST_TBL(klass)) {
612 struct clone_const_arg arg;
613 arg.tbl = RCLASS_CONST_TBL(clone) = rb_id_table_create(0);
614 arg.klass = clone;
615 rb_id_table_foreach(RCLASS_CONST_TBL(klass), clone_const_i, &arg);
616 }
617 if (!UNDEF_P(attach)) {
618 rb_singleton_class_attached(clone, attach);
619 }
620 RCLASS_M_TBL_INIT(clone);
621 {
622 struct clone_method_arg arg;
623 arg.old_klass = klass;
624 arg.new_klass = clone;
625 rb_id_table_foreach(RCLASS_M_TBL(klass), clone_method_i, &arg);
626 }
627 if (klass_of_clone_is_new) {
628 rb_singleton_class_attached(METACLASS_OF(clone), clone);
629 }
630 FL_SET(clone, FL_SINGLETON);
631
632 return clone;
633 }
634}
635
636void
638{
639 if (FL_TEST(klass, FL_SINGLETON)) {
640 rb_class_ivar_set(klass, id_attached, obj);
641 }
642}
643
649#define META_CLASS_OF_CLASS_CLASS_P(k) (METACLASS_OF(k) == (k))
650
651static int
652rb_singleton_class_has_metaclass_p(VALUE sklass)
653{
654 return rb_attr_get(METACLASS_OF(sklass), id_attached) == sklass;
655}
656
657int
658rb_singleton_class_internal_p(VALUE sklass)
659{
660 return (RB_TYPE_P(rb_attr_get(sklass, id_attached), T_CLASS) &&
661 !rb_singleton_class_has_metaclass_p(sklass));
662}
663
669#define HAVE_METACLASS_P(k) \
670 (FL_TEST(METACLASS_OF(k), FL_SINGLETON) && \
671 rb_singleton_class_has_metaclass_p(k))
672
680#define ENSURE_EIGENCLASS(klass) \
681 (HAVE_METACLASS_P(klass) ? METACLASS_OF(klass) : make_metaclass(klass))
682
683
693static inline VALUE
695{
696 VALUE super;
697 VALUE metaclass = rb_class_boot(Qundef);
698
699 FL_SET(metaclass, FL_SINGLETON);
700 rb_singleton_class_attached(metaclass, klass);
701
702 if (META_CLASS_OF_CLASS_CLASS_P(klass)) {
703 SET_METACLASS_OF(klass, metaclass);
704 SET_METACLASS_OF(metaclass, metaclass);
705 }
706 else {
707 VALUE tmp = METACLASS_OF(klass); /* for a meta^(n)-class klass, tmp is meta^(n)-class of Class class */
708 SET_METACLASS_OF(klass, metaclass);
709 SET_METACLASS_OF(metaclass, ENSURE_EIGENCLASS(tmp));
710 }
711
712 super = RCLASS_SUPER(klass);
713 while (RB_TYPE_P(super, T_ICLASS)) super = RCLASS_SUPER(super);
714 RCLASS_SET_SUPER(metaclass, super ? ENSURE_EIGENCLASS(super) : rb_cClass);
715
716 // Full class ancestry may not have been filled until we reach here.
717 rb_class_update_superclasses(METACLASS_OF(metaclass));
718
719 return metaclass;
720}
721
728static inline VALUE
730{
731 VALUE orig_class = METACLASS_OF(obj);
732 VALUE klass = rb_class_boot(orig_class);
733
734 FL_SET(klass, FL_SINGLETON);
735 RBASIC_SET_CLASS(obj, klass);
736 rb_singleton_class_attached(klass, obj);
737
738 SET_METACLASS_OF(klass, METACLASS_OF(rb_class_real(orig_class)));
739 return klass;
740}
741
742
743static VALUE
744boot_defclass(const char *name, VALUE super)
745{
746 VALUE obj = rb_class_boot(super);
747 ID id = rb_intern(name);
748
749 rb_const_set((rb_cObject ? rb_cObject : obj), id, obj);
750 rb_vm_add_root_module(obj);
751 return obj;
752}
753
754/***********************************************************************
755 *
756 * Document-class: Refinement
757 *
758 * Refinement is a class of the +self+ (current context) inside +refine+
759 * statement. It allows to import methods from other modules, see #import_methods.
760 */
761
762#if 0 /* for RDoc */
763/*
764 * Document-method: Refinement#import_methods
765 *
766 * call-seq:
767 * import_methods(module, ...) -> self
768 *
769 * Imports methods from modules. Unlike Module#include,
770 * Refinement#import_methods copies methods and adds them into the refinement,
771 * so the refinement is activated in the imported methods.
772 *
773 * Note that due to method copying, only methods defined in Ruby code can be imported.
774 *
775 * module StrUtils
776 * def indent(level)
777 * ' ' * level + self
778 * end
779 * end
780 *
781 * module M
782 * refine String do
783 * import_methods StrUtils
784 * end
785 * end
786 *
787 * using M
788 * "foo".indent(3)
789 * #=> " foo"
790 *
791 * module M
792 * refine String do
793 * import_methods Enumerable
794 * # Can't import method which is not defined with Ruby code: Enumerable#drop
795 * end
796 * end
797 *
798 */
799
800static VALUE
801refinement_import_methods(int argc, VALUE *argv, VALUE refinement)
802{
803}
804# endif
805
806void
808{
809 rb_cBasicObject = boot_defclass("BasicObject", 0);
810 rb_cObject = boot_defclass("Object", rb_cBasicObject);
811 rb_gc_register_mark_object(rb_cObject);
812
813 /* resolve class name ASAP for order-independence */
814 rb_set_class_path_string(rb_cObject, rb_cObject, rb_fstring_lit("Object"));
815
816 rb_cModule = boot_defclass("Module", rb_cObject);
817 rb_cClass = boot_defclass("Class", rb_cModule);
818 rb_cRefinement = boot_defclass("Refinement", rb_cModule);
819
820#if 0 /* for RDoc */
821 // we pretend it to be public, otherwise RDoc will ignore it
822 rb_define_method(rb_cRefinement, "import_methods", refinement_import_methods, -1);
823#endif
824
825 rb_const_set(rb_cObject, rb_intern_const("BasicObject"), rb_cBasicObject);
826 RBASIC_SET_CLASS(rb_cClass, rb_cClass);
827 RBASIC_SET_CLASS(rb_cModule, rb_cClass);
828 RBASIC_SET_CLASS(rb_cObject, rb_cClass);
829 RBASIC_SET_CLASS(rb_cRefinement, rb_cClass);
830 RBASIC_SET_CLASS(rb_cBasicObject, rb_cClass);
831
833}
834
835
846VALUE
847rb_make_metaclass(VALUE obj, VALUE unused)
848{
849 if (BUILTIN_TYPE(obj) == T_CLASS) {
850 return make_metaclass(obj);
851 }
852 else {
853 return make_singleton_class(obj);
854 }
855}
856
857VALUE
859{
860 VALUE klass;
861
862 if (!super) super = rb_cObject;
863 klass = rb_class_new(super);
864 rb_make_metaclass(klass, METACLASS_OF(super));
865
866 return klass;
867}
868
869
878MJIT_FUNC_EXPORTED VALUE
880{
881 ID inherited;
882 if (!super) super = rb_cObject;
883 CONST_ID(inherited, "inherited");
884 return rb_funcall(super, inherited, 1, klass);
885}
886
887VALUE
888rb_define_class(const char *name, VALUE super)
889{
890 VALUE klass;
891 ID id;
892
893 id = rb_intern(name);
894 if (rb_const_defined(rb_cObject, id)) {
895 klass = rb_const_get(rb_cObject, id);
896 if (!RB_TYPE_P(klass, T_CLASS)) {
897 rb_raise(rb_eTypeError, "%s is not a class (%"PRIsVALUE")",
898 name, rb_obj_class(klass));
899 }
900 if (rb_class_real(RCLASS_SUPER(klass)) != super) {
901 rb_raise(rb_eTypeError, "superclass mismatch for class %s", name);
902 }
903
904 /* Class may have been defined in Ruby and not pin-rooted */
905 rb_vm_add_root_module(klass);
906 return klass;
907 }
908 if (!super) {
909 rb_raise(rb_eArgError, "no super class for `%s'", name);
910 }
911 klass = rb_define_class_id(id, super);
912 rb_vm_add_root_module(klass);
913 rb_const_set(rb_cObject, id, klass);
914 rb_class_inherited(super, klass);
915
916 return klass;
917}
918
919VALUE
920rb_define_class_under(VALUE outer, const char *name, VALUE super)
921{
922 return rb_define_class_id_under(outer, rb_intern(name), super);
923}
924
925VALUE
927{
928 VALUE klass;
929
930 if (rb_const_defined_at(outer, id)) {
931 klass = rb_const_get_at(outer, id);
932 if (!RB_TYPE_P(klass, T_CLASS)) {
933 rb_raise(rb_eTypeError, "%"PRIsVALUE"::%"PRIsVALUE" is not a class"
934 " (%"PRIsVALUE")",
935 outer, rb_id2str(id), rb_obj_class(klass));
936 }
937 if (rb_class_real(RCLASS_SUPER(klass)) != super) {
938 rb_raise(rb_eTypeError, "superclass mismatch for class "
939 "%"PRIsVALUE"::%"PRIsVALUE""
940 " (%"PRIsVALUE" is given but was %"PRIsVALUE")",
941 outer, rb_id2str(id), RCLASS_SUPER(klass), super);
942 }
943 /* Class may have been defined in Ruby and not pin-rooted */
944 rb_vm_add_root_module(klass);
945
946 return klass;
947 }
948 if (!super) {
949 rb_raise(rb_eArgError, "no super class for `%"PRIsVALUE"::%"PRIsVALUE"'",
950 rb_class_path(outer), rb_id2str(id));
951 }
952 klass = rb_define_class_id(id, super);
953 rb_set_class_path_string(klass, outer, rb_id2str(id));
954 rb_const_set(outer, id, klass);
955 rb_class_inherited(super, klass);
956 rb_vm_add_root_module(klass);
957
958 return klass;
959}
960
961VALUE
962rb_module_s_alloc(VALUE klass)
963{
964 VALUE mod = class_alloc(T_MODULE, klass);
965 RCLASS_M_TBL_INIT(mod);
966 FL_SET(mod, RMODULE_ALLOCATED_BUT_NOT_INITIALIZED);
967 return mod;
968}
969
970static inline VALUE
971module_new(VALUE klass)
972{
973 VALUE mdl = class_alloc(T_MODULE, klass);
974 RCLASS_M_TBL_INIT(mdl);
975 return (VALUE)mdl;
976}
977
978VALUE
980{
981 return module_new(rb_cModule);
982}
983
984VALUE
986{
987 return module_new(rb_cRefinement);
988}
989
990// Kept for compatibility. Use rb_module_new() instead.
991VALUE
993{
994 return rb_module_new();
995}
996
997VALUE
998rb_define_module(const char *name)
999{
1000 VALUE module;
1001 ID id;
1002
1003 id = rb_intern(name);
1004 if (rb_const_defined(rb_cObject, id)) {
1005 module = rb_const_get(rb_cObject, id);
1006 if (!RB_TYPE_P(module, T_MODULE)) {
1007 rb_raise(rb_eTypeError, "%s is not a module (%"PRIsVALUE")",
1008 name, rb_obj_class(module));
1009 }
1010 /* Module may have been defined in Ruby and not pin-rooted */
1011 rb_vm_add_root_module(module);
1012 return module;
1013 }
1014 module = rb_module_new();
1015 rb_vm_add_root_module(module);
1016 rb_const_set(rb_cObject, id, module);
1017
1018 return module;
1019}
1020
1021VALUE
1022rb_define_module_under(VALUE outer, const char *name)
1023{
1024 return rb_define_module_id_under(outer, rb_intern(name));
1025}
1026
1027VALUE
1029{
1030 VALUE module;
1031
1032 if (rb_const_defined_at(outer, id)) {
1033 module = rb_const_get_at(outer, id);
1034 if (!RB_TYPE_P(module, T_MODULE)) {
1035 rb_raise(rb_eTypeError, "%"PRIsVALUE"::%"PRIsVALUE" is not a module"
1036 " (%"PRIsVALUE")",
1037 outer, rb_id2str(id), rb_obj_class(module));
1038 }
1039 /* Module may have been defined in Ruby and not pin-rooted */
1040 rb_gc_register_mark_object(module);
1041 return module;
1042 }
1043 module = rb_module_new();
1044 rb_const_set(outer, id, module);
1045 rb_set_class_path_string(module, outer, rb_id2str(id));
1046 rb_gc_register_mark_object(module);
1047
1048 return module;
1049}
1050
1051VALUE
1052rb_include_class_new(VALUE module, VALUE super)
1053{
1055
1056 RCLASS_M_TBL(klass) = RCLASS_M_TBL(module);
1057
1058 RCLASS_SET_ORIGIN(klass, klass);
1059 if (BUILTIN_TYPE(module) == T_ICLASS) {
1060 module = METACLASS_OF(module);
1061 }
1062 RUBY_ASSERT(!RB_TYPE_P(module, T_ICLASS));
1063 if (!RCLASS_CONST_TBL(module)) {
1064 RCLASS_CONST_TBL(module) = rb_id_table_create(0);
1065 }
1066
1067 RCLASS_CVC_TBL(klass) = RCLASS_CVC_TBL(module);
1068 RCLASS_CONST_TBL(klass) = RCLASS_CONST_TBL(module);
1069
1070 RCLASS_SET_SUPER(klass, super);
1071 RBASIC_SET_CLASS(klass, module);
1072
1073 return (VALUE)klass;
1074}
1075
1076static int include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super);
1077
1078static void
1079ensure_includable(VALUE klass, VALUE module)
1080{
1081 rb_class_modify_check(klass);
1082 Check_Type(module, T_MODULE);
1083 rb_module_set_initialized(module);
1084 if (!NIL_P(rb_refinement_module_get_refined_class(module))) {
1085 rb_raise(rb_eArgError, "refinement module is not allowed");
1086 }
1087}
1088
1089void
1091{
1092 int changed = 0;
1093
1094 ensure_includable(klass, module);
1095
1096 changed = include_modules_at(klass, RCLASS_ORIGIN(klass), module, TRUE);
1097 if (changed < 0)
1098 rb_raise(rb_eArgError, "cyclic include detected");
1099
1100 if (RB_TYPE_P(klass, T_MODULE)) {
1101 rb_subclass_entry_t *iclass = RCLASS_SUBCLASSES(klass);
1102 // skip the placeholder subclass entry at the head of the list
1103 if (iclass) {
1104 RUBY_ASSERT(!iclass->klass);
1105 iclass = iclass->next;
1106 }
1107
1108 int do_include = 1;
1109 while (iclass) {
1110 VALUE check_class = iclass->klass;
1111 /* During lazy sweeping, iclass->klass could be a dead object that
1112 * has not yet been swept. */
1113 if (!rb_objspace_garbage_object_p(check_class)) {
1114 while (check_class) {
1115 RUBY_ASSERT(!rb_objspace_garbage_object_p(check_class));
1116
1117 if (RB_TYPE_P(check_class, T_ICLASS) &&
1118 (METACLASS_OF(check_class) == module)) {
1119 do_include = 0;
1120 }
1121 check_class = RCLASS_SUPER(check_class);
1122 }
1123
1124 if (do_include) {
1125 include_modules_at(iclass->klass, RCLASS_ORIGIN(iclass->klass), module, TRUE);
1126 }
1127 }
1128
1129 iclass = iclass->next;
1130 }
1131 }
1132}
1133
1134static enum rb_id_table_iterator_result
1135add_refined_method_entry_i(ID key, VALUE value, void *data)
1136{
1137 rb_add_refined_method_entry((VALUE)data, key);
1138 return ID_TABLE_CONTINUE;
1139}
1140
1141static enum rb_id_table_iterator_result
1142clear_module_cache_i(ID id, VALUE val, void *data)
1143{
1144 VALUE klass = (VALUE)data;
1145 rb_clear_method_cache(klass, id);
1146 return ID_TABLE_CONTINUE;
1147}
1148
1149static bool
1150module_in_super_chain(const VALUE klass, VALUE module)
1151{
1152 struct rb_id_table *const klass_m_tbl = RCLASS_M_TBL(RCLASS_ORIGIN(klass));
1153 if (klass_m_tbl) {
1154 while (module) {
1155 if (klass_m_tbl == RCLASS_M_TBL(module))
1156 return true;
1157 module = RCLASS_SUPER(module);
1158 }
1159 }
1160 return false;
1161}
1162
1163// For each ID key in the class constant table, we're going to clear the VM's
1164// inline constant caches associated with it.
1165static enum rb_id_table_iterator_result
1166clear_constant_cache_i(ID id, VALUE value, void *data)
1167{
1169 return ID_TABLE_CONTINUE;
1170}
1171
1172static int
1173do_include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super, bool check_cyclic)
1174{
1175 VALUE p, iclass, origin_stack = 0;
1176 int method_changed = 0, add_subclass;
1177 long origin_len;
1178 VALUE klass_origin = RCLASS_ORIGIN(klass);
1179 VALUE original_klass = klass;
1180
1181 if (check_cyclic && module_in_super_chain(klass, module))
1182 return -1;
1183
1184 while (module) {
1185 int c_seen = FALSE;
1186 int superclass_seen = FALSE;
1187 struct rb_id_table *tbl;
1188
1189 if (klass == c) {
1190 c_seen = TRUE;
1191 }
1192 if (klass_origin != c || search_super) {
1193 /* ignore if the module included already in superclasses for include,
1194 * ignore if the module included before origin class for prepend
1195 */
1196 for (p = RCLASS_SUPER(klass); p; p = RCLASS_SUPER(p)) {
1197 int type = BUILTIN_TYPE(p);
1198 if (klass_origin == p && !search_super)
1199 break;
1200 if (c == p)
1201 c_seen = TRUE;
1202 if (type == T_ICLASS) {
1203 if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) {
1204 if (!superclass_seen && c_seen) {
1205 c = p; /* move insertion point */
1206 }
1207 goto skip;
1208 }
1209 }
1210 else if (type == T_CLASS) {
1211 superclass_seen = TRUE;
1212 }
1213 }
1214 }
1215
1216 VALUE super_class = RCLASS_SUPER(c);
1217
1218 // invalidate inline method cache
1219 RB_DEBUG_COUNTER_INC(cvar_include_invalidate);
1220 ruby_vm_global_cvar_state++;
1221 tbl = RCLASS_M_TBL(module);
1222 if (tbl && rb_id_table_size(tbl)) {
1223 if (search_super) { // include
1224 if (super_class && !RB_TYPE_P(super_class, T_MODULE)) {
1225 rb_id_table_foreach(tbl, clear_module_cache_i, (void *)super_class);
1226 }
1227 }
1228 else { // prepend
1229 if (!RB_TYPE_P(original_klass, T_MODULE)) {
1230 rb_id_table_foreach(tbl, clear_module_cache_i, (void *)original_klass);
1231 }
1232 }
1233 method_changed = 1;
1234 }
1235
1236 // setup T_ICLASS for the include/prepend module
1237 iclass = rb_include_class_new(module, super_class);
1238 c = RCLASS_SET_SUPER(c, iclass);
1239 RCLASS_SET_INCLUDER(iclass, klass);
1240 add_subclass = TRUE;
1241 if (module != RCLASS_ORIGIN(module)) {
1242 if (!origin_stack) origin_stack = rb_ary_hidden_new(2);
1243 VALUE origin[2] = {iclass, RCLASS_ORIGIN(module)};
1244 rb_ary_cat(origin_stack, origin, 2);
1245 }
1246 else if (origin_stack && (origin_len = RARRAY_LEN(origin_stack)) > 1 &&
1247 RARRAY_AREF(origin_stack, origin_len - 1) == module) {
1248 RCLASS_SET_ORIGIN(RARRAY_AREF(origin_stack, (origin_len -= 2)), iclass);
1249 RICLASS_SET_ORIGIN_SHARED_MTBL(iclass);
1250 rb_ary_resize(origin_stack, origin_len);
1251 add_subclass = FALSE;
1252 }
1253
1254 if (add_subclass) {
1255 VALUE m = module;
1256 if (BUILTIN_TYPE(m) == T_ICLASS) m = METACLASS_OF(m);
1257 rb_module_add_to_subclasses_list(m, iclass);
1258 }
1259
1260 if (BUILTIN_TYPE(klass) == T_MODULE && FL_TEST(klass, RMODULE_IS_REFINEMENT)) {
1261 VALUE refined_class =
1262 rb_refinement_module_get_refined_class(klass);
1263
1264 rb_id_table_foreach(RCLASS_M_TBL(module), add_refined_method_entry_i, (void *)refined_class);
1266 }
1267
1268 tbl = RCLASS_CONST_TBL(module);
1269 if (tbl && rb_id_table_size(tbl))
1270 rb_id_table_foreach(tbl, clear_constant_cache_i, NULL);
1271 skip:
1272 module = RCLASS_SUPER(module);
1273 }
1274
1275 return method_changed;
1276}
1277
1278static int
1279include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super)
1280{
1281 return do_include_modules_at(klass, c, module, search_super, true);
1282}
1283
1284static enum rb_id_table_iterator_result
1285move_refined_method(ID key, VALUE value, void *data)
1286{
1287 rb_method_entry_t *me = (rb_method_entry_t *)value;
1288
1289 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1290 VALUE klass = (VALUE)data;
1291 struct rb_id_table *tbl = RCLASS_M_TBL(klass);
1292
1293 if (me->def->body.refined.orig_me) {
1294 const rb_method_entry_t *orig_me = me->def->body.refined.orig_me, *new_me;
1295 RB_OBJ_WRITE(me, &me->def->body.refined.orig_me, NULL);
1296 new_me = rb_method_entry_clone(me);
1297 rb_method_table_insert(klass, tbl, key, new_me);
1298 rb_method_entry_copy(me, orig_me);
1299 return ID_TABLE_CONTINUE;
1300 }
1301 else {
1302 rb_method_table_insert(klass, tbl, key, me);
1303 return ID_TABLE_DELETE;
1304 }
1305 }
1306 else {
1307 return ID_TABLE_CONTINUE;
1308 }
1309}
1310
1311static enum rb_id_table_iterator_result
1312cache_clear_refined_method(ID key, VALUE value, void *data)
1313{
1314 rb_method_entry_t *me = (rb_method_entry_t *) value;
1315
1316 if (me->def->type == VM_METHOD_TYPE_REFINED && me->def->body.refined.orig_me) {
1317 VALUE klass = (VALUE)data;
1318 rb_clear_method_cache(klass, me->called_id);
1319 }
1320 // Refined method entries without an orig_me is going to stay in the method
1321 // table of klass, like before the move, so no need to clear the cache.
1322
1323 return ID_TABLE_CONTINUE;
1324}
1325
1326static bool
1327ensure_origin(VALUE klass)
1328{
1329 VALUE origin = RCLASS_ORIGIN(klass);
1330 if (origin == klass) {
1331 origin = class_alloc(T_ICLASS, klass);
1332 RCLASS_SET_SUPER(origin, RCLASS_SUPER(klass));
1333 RCLASS_SET_SUPER(klass, origin);
1334 RCLASS_SET_ORIGIN(klass, origin);
1335 RCLASS_M_TBL(origin) = RCLASS_M_TBL(klass);
1336 RCLASS_M_TBL_INIT(klass);
1337 rb_id_table_foreach(RCLASS_M_TBL(origin), cache_clear_refined_method, (void *)klass);
1338 rb_id_table_foreach(RCLASS_M_TBL(origin), move_refined_method, (void *)klass);
1339 return true;
1340 }
1341 return false;
1342}
1343
1344void
1346{
1347 int changed;
1348 bool klass_had_no_origin;
1349
1350 ensure_includable(klass, module);
1351 if (module_in_super_chain(klass, module))
1352 rb_raise(rb_eArgError, "cyclic prepend detected");
1353
1354 klass_had_no_origin = ensure_origin(klass);
1355 changed = do_include_modules_at(klass, klass, module, FALSE, false);
1356 RUBY_ASSERT(changed >= 0); // already checked for cyclic prepend above
1357 if (changed) {
1358 rb_vm_check_redefinition_by_prepend(klass);
1359 }
1360 if (RB_TYPE_P(klass, T_MODULE)) {
1361 rb_subclass_entry_t *iclass = RCLASS_SUBCLASSES(klass);
1362 // skip the placeholder subclass entry at the head of the list if it exists
1363 if (iclass) {
1364 RUBY_ASSERT(!iclass->klass);
1365 iclass = iclass->next;
1366 }
1367
1368 VALUE klass_origin = RCLASS_ORIGIN(klass);
1369 struct rb_id_table *klass_m_tbl = RCLASS_M_TBL(klass);
1370 struct rb_id_table *klass_origin_m_tbl = RCLASS_M_TBL(klass_origin);
1371 while (iclass) {
1372 /* During lazy sweeping, iclass->klass could be a dead object that
1373 * has not yet been swept. */
1374 if (!rb_objspace_garbage_object_p(iclass->klass)) {
1375 const VALUE subclass = iclass->klass;
1376 if (klass_had_no_origin && klass_origin_m_tbl == RCLASS_M_TBL(subclass)) {
1377 // backfill an origin iclass to handle refinements and future prepends
1378 rb_id_table_foreach(RCLASS_M_TBL(subclass), clear_module_cache_i, (void *)subclass);
1379 RCLASS_M_TBL(subclass) = klass_m_tbl;
1380 VALUE origin = rb_include_class_new(klass_origin, RCLASS_SUPER(subclass));
1381 RCLASS_SET_SUPER(subclass, origin);
1382 RCLASS_SET_INCLUDER(origin, RCLASS_INCLUDER(subclass));
1383 RCLASS_SET_ORIGIN(subclass, origin);
1384 RICLASS_SET_ORIGIN_SHARED_MTBL(origin);
1385 }
1386 include_modules_at(subclass, subclass, module, FALSE);
1387 }
1388
1389 iclass = iclass->next;
1390 }
1391 }
1392}
1393
1394/*
1395 * call-seq:
1396 * mod.included_modules -> array
1397 *
1398 * Returns the list of modules included or prepended in <i>mod</i>
1399 * or one of <i>mod</i>'s ancestors.
1400 *
1401 * module Sub
1402 * end
1403 *
1404 * module Mixin
1405 * prepend Sub
1406 * end
1407 *
1408 * module Outer
1409 * include Mixin
1410 * end
1411 *
1412 * Mixin.included_modules #=> [Sub]
1413 * Outer.included_modules #=> [Sub, Mixin]
1414 */
1415
1416VALUE
1418{
1419 VALUE ary = rb_ary_new();
1420 VALUE p;
1421 VALUE origin = RCLASS_ORIGIN(mod);
1422
1423 for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
1424 if (p != origin && RCLASS_ORIGIN(p) == p && BUILTIN_TYPE(p) == T_ICLASS) {
1425 VALUE m = METACLASS_OF(p);
1426 if (RB_TYPE_P(m, T_MODULE))
1427 rb_ary_push(ary, m);
1428 }
1429 }
1430 return ary;
1431}
1432
1433/*
1434 * call-seq:
1435 * mod.include?(module) -> true or false
1436 *
1437 * Returns <code>true</code> if <i>module</i> is included
1438 * or prepended in <i>mod</i> or one of <i>mod</i>'s ancestors.
1439 *
1440 * module A
1441 * end
1442 * class B
1443 * include A
1444 * end
1445 * class C < B
1446 * end
1447 * B.include?(A) #=> true
1448 * C.include?(A) #=> true
1449 * A.include?(A) #=> false
1450 */
1451
1452VALUE
1454{
1455 VALUE p;
1456
1457 Check_Type(mod2, T_MODULE);
1458 for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
1459 if (BUILTIN_TYPE(p) == T_ICLASS && !FL_TEST(p, RICLASS_IS_ORIGIN)) {
1460 if (METACLASS_OF(p) == mod2) return Qtrue;
1461 }
1462 }
1463 return Qfalse;
1464}
1465
1466/*
1467 * call-seq:
1468 * mod.ancestors -> array
1469 *
1470 * Returns a list of modules included/prepended in <i>mod</i>
1471 * (including <i>mod</i> itself).
1472 *
1473 * module Mod
1474 * include Math
1475 * include Comparable
1476 * prepend Enumerable
1477 * end
1478 *
1479 * Mod.ancestors #=> [Enumerable, Mod, Comparable, Math]
1480 * Math.ancestors #=> [Math]
1481 * Enumerable.ancestors #=> [Enumerable]
1482 */
1483
1484VALUE
1486{
1487 VALUE p, ary = rb_ary_new();
1488 VALUE refined_class = Qnil;
1489 if (BUILTIN_TYPE(mod) == T_MODULE && FL_TEST(mod, RMODULE_IS_REFINEMENT)) {
1490 refined_class = rb_refinement_module_get_refined_class(mod);
1491 }
1492
1493 for (p = mod; p; p = RCLASS_SUPER(p)) {
1494 if (p == refined_class) break;
1495 if (p != RCLASS_ORIGIN(p)) continue;
1496 if (BUILTIN_TYPE(p) == T_ICLASS) {
1497 rb_ary_push(ary, METACLASS_OF(p));
1498 }
1499 else {
1500 rb_ary_push(ary, p);
1501 }
1502 }
1503 return ary;
1504}
1505
1507{
1508 VALUE buffer;
1509 long count;
1510 long maxcount;
1511 bool immediate_only;
1512};
1513
1514static void
1515class_descendants_recursive(VALUE klass, VALUE v)
1516{
1517 struct subclass_traverse_data *data = (struct subclass_traverse_data *) v;
1518
1519 if (BUILTIN_TYPE(klass) == T_CLASS && !FL_TEST(klass, FL_SINGLETON)) {
1520 if (data->buffer && data->count < data->maxcount && !rb_objspace_garbage_object_p(klass)) {
1521 // assumes that this does not cause GC as long as the length does not exceed the capacity
1522 rb_ary_push(data->buffer, klass);
1523 }
1524 data->count++;
1525 if (!data->immediate_only) {
1526 rb_class_foreach_subclass(klass, class_descendants_recursive, v);
1527 }
1528 }
1529 else {
1530 rb_class_foreach_subclass(klass, class_descendants_recursive, v);
1531 }
1532}
1533
1534static VALUE
1535class_descendants(VALUE klass, bool immediate_only)
1536{
1537 struct subclass_traverse_data data = { Qfalse, 0, -1, immediate_only };
1538
1539 // estimate the count of subclasses
1540 rb_class_foreach_subclass(klass, class_descendants_recursive, (VALUE) &data);
1541
1542 // the following allocation may cause GC which may change the number of subclasses
1543 data.buffer = rb_ary_new_capa(data.count);
1544 data.maxcount = data.count;
1545 data.count = 0;
1546
1547 size_t gc_count = rb_gc_count();
1548
1549 // enumerate subclasses
1550 rb_class_foreach_subclass(klass, class_descendants_recursive, (VALUE) &data);
1551
1552 if (gc_count != rb_gc_count()) {
1553 rb_bug("GC must not occur during the subclass iteration of Class#descendants");
1554 }
1555
1556 return data.buffer;
1557}
1558
1559/*
1560 * call-seq:
1561 * subclasses -> array
1562 *
1563 * Returns an array of classes where the receiver is the
1564 * direct superclass of the class, excluding singleton classes.
1565 * The order of the returned array is not defined.
1566 *
1567 * class A; end
1568 * class B < A; end
1569 * class C < B; end
1570 * class D < A; end
1571 *
1572 * A.subclasses #=> [D, B]
1573 * B.subclasses #=> [C]
1574 * C.subclasses #=> []
1575 *
1576 * Anonymous subclasses (not associated with a constant) are
1577 * returned, too:
1578 *
1579 * c = Class.new(A)
1580 * A.subclasses # => [#<Class:0x00007f003c77bd78>, D, B]
1581 *
1582 * Note that the parent does not hold references to subclasses
1583 * and doesn't prevent them from being garbage collected. This
1584 * means that the subclass might disappear when all references
1585 * to it are dropped:
1586 *
1587 * # drop the reference to subclass, it can be garbage-collected now
1588 * c = nil
1589 *
1590 * A.subclasses
1591 * # It can be
1592 * # => [#<Class:0x00007f003c77bd78>, D, B]
1593 * # ...or just
1594 * # => [D, B]
1595 * # ...depending on whether garbage collector was run
1596 */
1597
1598VALUE
1600{
1601 return class_descendants(klass, true);
1602}
1603
1604/*
1605 * call-seq:
1606 * attached_object -> object
1607 *
1608 * Returns the object for which the receiver is the singleton class.
1609 *
1610 * Raises an TypeError if the class is not a singleton class.
1611 *
1612 * class Foo; end
1613 *
1614 * Foo.singleton_class.attached_object #=> Foo
1615 * Foo.attached_object #=> TypeError: `Foo' is not a singleton class
1616 * Foo.new.singleton_class.attached_object #=> #<Foo:0x000000010491a370>
1617 * TrueClass.attached_object #=> TypeError: `TrueClass' is not a singleton class
1618 * NilClass.attached_object #=> TypeError: `NilClass' is not a singleton class
1619 */
1620
1621VALUE
1623{
1624 if (!FL_TEST(klass, FL_SINGLETON)) {
1625 rb_raise(rb_eTypeError, "`%"PRIsVALUE"' is not a singleton class", klass);
1626 }
1627
1628 return rb_attr_get(klass, id_attached);
1629}
1630
1631static void
1632ins_methods_push(st_data_t name, st_data_t ary)
1633{
1634 rb_ary_push((VALUE)ary, ID2SYM((ID)name));
1635}
1636
1637static int
1638ins_methods_i(st_data_t name, st_data_t type, st_data_t ary)
1639{
1640 switch ((rb_method_visibility_t)type) {
1641 case METHOD_VISI_UNDEF:
1642 case METHOD_VISI_PRIVATE:
1643 break;
1644 default: /* everything but private */
1645 ins_methods_push(name, ary);
1646 break;
1647 }
1648 return ST_CONTINUE;
1649}
1650
1651static int
1652ins_methods_type_i(st_data_t name, st_data_t type, st_data_t ary, rb_method_visibility_t visi)
1653{
1654 if ((rb_method_visibility_t)type == visi) {
1655 ins_methods_push(name, ary);
1656 }
1657 return ST_CONTINUE;
1658}
1659
1660static int
1661ins_methods_prot_i(st_data_t name, st_data_t type, st_data_t ary)
1662{
1663 return ins_methods_type_i(name, type, ary, METHOD_VISI_PROTECTED);
1664}
1665
1666static int
1667ins_methods_priv_i(st_data_t name, st_data_t type, st_data_t ary)
1668{
1669 return ins_methods_type_i(name, type, ary, METHOD_VISI_PRIVATE);
1670}
1671
1672static int
1673ins_methods_pub_i(st_data_t name, st_data_t type, st_data_t ary)
1674{
1675 return ins_methods_type_i(name, type, ary, METHOD_VISI_PUBLIC);
1676}
1677
1678static int
1679ins_methods_undef_i(st_data_t name, st_data_t type, st_data_t ary)
1680{
1681 return ins_methods_type_i(name, type, ary, METHOD_VISI_UNDEF);
1682}
1683
1685 st_table *list;
1686 int recur;
1687};
1688
1689static enum rb_id_table_iterator_result
1690method_entry_i(ID key, VALUE value, void *data)
1691{
1692 const rb_method_entry_t *me = (const rb_method_entry_t *)value;
1693 struct method_entry_arg *arg = (struct method_entry_arg *)data;
1694 rb_method_visibility_t type;
1695
1696 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1697 VALUE owner = me->owner;
1698 me = rb_resolve_refined_method(Qnil, me);
1699 if (!me) return ID_TABLE_CONTINUE;
1700 if (!arg->recur && me->owner != owner) return ID_TABLE_CONTINUE;
1701 }
1702 if (!st_is_member(arg->list, key)) {
1703 if (UNDEFINED_METHOD_ENTRY_P(me)) {
1704 type = METHOD_VISI_UNDEF; /* none */
1705 }
1706 else {
1707 type = METHOD_ENTRY_VISI(me);
1708 RUBY_ASSERT(type != METHOD_VISI_UNDEF);
1709 }
1710 st_add_direct(arg->list, key, (st_data_t)type);
1711 }
1712 return ID_TABLE_CONTINUE;
1713}
1714
1715static void
1716add_instance_method_list(VALUE mod, struct method_entry_arg *me_arg)
1717{
1718 struct rb_id_table *m_tbl = RCLASS_M_TBL(mod);
1719 if (!m_tbl) return;
1720 rb_id_table_foreach(m_tbl, method_entry_i, me_arg);
1721}
1722
1723static bool
1724particular_class_p(VALUE mod)
1725{
1726 if (!mod) return false;
1727 if (FL_TEST(mod, FL_SINGLETON)) return true;
1728 if (BUILTIN_TYPE(mod) == T_ICLASS) return true;
1729 return false;
1730}
1731
1732static VALUE
1733class_instance_method_list(int argc, const VALUE *argv, VALUE mod, int obj, int (*func) (st_data_t, st_data_t, st_data_t))
1734{
1735 VALUE ary;
1736 int recur = TRUE, prepended = 0;
1737 struct method_entry_arg me_arg;
1738
1739 if (rb_check_arity(argc, 0, 1)) recur = RTEST(argv[0]);
1740
1741 me_arg.list = st_init_numtable();
1742 me_arg.recur = recur;
1743
1744 if (obj) {
1745 for (; particular_class_p(mod); mod = RCLASS_SUPER(mod)) {
1746 add_instance_method_list(mod, &me_arg);
1747 }
1748 }
1749
1750 if (!recur && RCLASS_ORIGIN(mod) != mod) {
1751 mod = RCLASS_ORIGIN(mod);
1752 prepended = 1;
1753 }
1754
1755 for (; mod; mod = RCLASS_SUPER(mod)) {
1756 add_instance_method_list(mod, &me_arg);
1757 if (BUILTIN_TYPE(mod) == T_ICLASS && !prepended) continue;
1758 if (!recur) break;
1759 }
1760 ary = rb_ary_new2(me_arg.list->num_entries);
1761 st_foreach(me_arg.list, func, ary);
1762 st_free_table(me_arg.list);
1763
1764 return ary;
1765}
1766
1767/*
1768 * call-seq:
1769 * mod.instance_methods(include_super=true) -> array
1770 *
1771 * Returns an array containing the names of the public and protected instance
1772 * methods in the receiver. For a module, these are the public and protected methods;
1773 * for a class, they are the instance (not singleton) methods. If the optional
1774 * parameter is <code>false</code>, the methods of any ancestors are not included.
1775 *
1776 * module A
1777 * def method1() end
1778 * end
1779 * class B
1780 * include A
1781 * def method2() end
1782 * end
1783 * class C < B
1784 * def method3() end
1785 * end
1786 *
1787 * A.instance_methods(false) #=> [:method1]
1788 * B.instance_methods(false) #=> [:method2]
1789 * B.instance_methods(true).include?(:method1) #=> true
1790 * C.instance_methods(false) #=> [:method3]
1791 * C.instance_methods.include?(:method2) #=> true
1792 *
1793 * Note that method visibility changes in the current class, as well as aliases,
1794 * are considered as methods of the current class by this method:
1795 *
1796 * class C < B
1797 * alias method4 method2
1798 * protected :method2
1799 * end
1800 * C.instance_methods(false).sort #=> [:method2, :method3, :method4]
1801 */
1802
1803VALUE
1804rb_class_instance_methods(int argc, const VALUE *argv, VALUE mod)
1805{
1806 return class_instance_method_list(argc, argv, mod, 0, ins_methods_i);
1807}
1808
1809/*
1810 * call-seq:
1811 * mod.protected_instance_methods(include_super=true) -> array
1812 *
1813 * Returns a list of the protected instance methods defined in
1814 * <i>mod</i>. If the optional parameter is <code>false</code>, the
1815 * methods of any ancestors are not included.
1816 */
1817
1818VALUE
1820{
1821 return class_instance_method_list(argc, argv, mod, 0, ins_methods_prot_i);
1822}
1823
1824/*
1825 * call-seq:
1826 * mod.private_instance_methods(include_super=true) -> array
1827 *
1828 * Returns a list of the private instance methods defined in
1829 * <i>mod</i>. If the optional parameter is <code>false</code>, the
1830 * methods of any ancestors are not included.
1831 *
1832 * module Mod
1833 * def method1() end
1834 * private :method1
1835 * def method2() end
1836 * end
1837 * Mod.instance_methods #=> [:method2]
1838 * Mod.private_instance_methods #=> [:method1]
1839 */
1840
1841VALUE
1843{
1844 return class_instance_method_list(argc, argv, mod, 0, ins_methods_priv_i);
1845}
1846
1847/*
1848 * call-seq:
1849 * mod.public_instance_methods(include_super=true) -> array
1850 *
1851 * Returns a list of the public instance methods defined in <i>mod</i>.
1852 * If the optional parameter is <code>false</code>, the methods of
1853 * any ancestors are not included.
1854 */
1855
1856VALUE
1858{
1859 return class_instance_method_list(argc, argv, mod, 0, ins_methods_pub_i);
1860}
1861
1862/*
1863 * call-seq:
1864 * mod.undefined_instance_methods -> array
1865 *
1866 * Returns a list of the undefined instance methods defined in <i>mod</i>.
1867 * The undefined methods of any ancestors are not included.
1868 */
1869
1870VALUE
1871rb_class_undefined_instance_methods(VALUE mod)
1872{
1873 VALUE include_super = Qfalse;
1874 return class_instance_method_list(1, &include_super, mod, 0, ins_methods_undef_i);
1875}
1876
1877/*
1878 * call-seq:
1879 * obj.methods(regular=true) -> array
1880 *
1881 * Returns a list of the names of public and protected methods of
1882 * <i>obj</i>. This will include all the methods accessible in
1883 * <i>obj</i>'s ancestors.
1884 * If the optional parameter is <code>false</code>, it
1885 * returns an array of <i>obj</i>'s public and protected singleton methods,
1886 * the array will not include methods in modules included in <i>obj</i>.
1887 *
1888 * class Klass
1889 * def klass_method()
1890 * end
1891 * end
1892 * k = Klass.new
1893 * k.methods[0..9] #=> [:klass_method, :nil?, :===,
1894 * # :==~, :!, :eql?
1895 * # :hash, :<=>, :class, :singleton_class]
1896 * k.methods.length #=> 56
1897 *
1898 * k.methods(false) #=> []
1899 * def k.singleton_method; end
1900 * k.methods(false) #=> [:singleton_method]
1901 *
1902 * module M123; def m123; end end
1903 * k.extend M123
1904 * k.methods(false) #=> [:singleton_method]
1905 */
1906
1907VALUE
1908rb_obj_methods(int argc, const VALUE *argv, VALUE obj)
1909{
1910 rb_check_arity(argc, 0, 1);
1911 if (argc > 0 && !RTEST(argv[0])) {
1912 return rb_obj_singleton_methods(argc, argv, obj);
1913 }
1914 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_i);
1915}
1916
1917/*
1918 * call-seq:
1919 * obj.protected_methods(all=true) -> array
1920 *
1921 * Returns the list of protected methods accessible to <i>obj</i>. If
1922 * the <i>all</i> parameter is set to <code>false</code>, only those methods
1923 * in the receiver will be listed.
1924 */
1925
1926VALUE
1927rb_obj_protected_methods(int argc, const VALUE *argv, VALUE obj)
1928{
1929 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_prot_i);
1930}
1931
1932/*
1933 * call-seq:
1934 * obj.private_methods(all=true) -> array
1935 *
1936 * Returns the list of private methods accessible to <i>obj</i>. If
1937 * the <i>all</i> parameter is set to <code>false</code>, only those methods
1938 * in the receiver will be listed.
1939 */
1940
1941VALUE
1942rb_obj_private_methods(int argc, const VALUE *argv, VALUE obj)
1943{
1944 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_priv_i);
1945}
1946
1947/*
1948 * call-seq:
1949 * obj.public_methods(all=true) -> array
1950 *
1951 * Returns the list of public methods accessible to <i>obj</i>. If
1952 * the <i>all</i> parameter is set to <code>false</code>, only those methods
1953 * in the receiver will be listed.
1954 */
1955
1956VALUE
1957rb_obj_public_methods(int argc, const VALUE *argv, VALUE obj)
1958{
1959 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_pub_i);
1960}
1961
1962/*
1963 * call-seq:
1964 * obj.singleton_methods(all=true) -> array
1965 *
1966 * Returns an array of the names of singleton methods for <i>obj</i>.
1967 * If the optional <i>all</i> parameter is true, the list will include
1968 * methods in modules included in <i>obj</i>.
1969 * Only public and protected singleton methods are returned.
1970 *
1971 * module Other
1972 * def three() end
1973 * end
1974 *
1975 * class Single
1976 * def Single.four() end
1977 * end
1978 *
1979 * a = Single.new
1980 *
1981 * def a.one()
1982 * end
1983 *
1984 * class << a
1985 * include Other
1986 * def two()
1987 * end
1988 * end
1989 *
1990 * Single.singleton_methods #=> [:four]
1991 * a.singleton_methods(false) #=> [:two, :one]
1992 * a.singleton_methods #=> [:two, :one, :three]
1993 */
1994
1995VALUE
1996rb_obj_singleton_methods(int argc, const VALUE *argv, VALUE obj)
1997{
1998 VALUE ary, klass, origin;
1999 struct method_entry_arg me_arg;
2000 struct rb_id_table *mtbl;
2001 int recur = TRUE;
2002
2003 if (rb_check_arity(argc, 0, 1)) recur = RTEST(argv[0]);
2004 if (RB_TYPE_P(obj, T_CLASS) && FL_TEST(obj, FL_SINGLETON)) {
2005 rb_singleton_class(obj);
2006 }
2007 klass = CLASS_OF(obj);
2008 origin = RCLASS_ORIGIN(klass);
2009 me_arg.list = st_init_numtable();
2010 me_arg.recur = recur;
2011 if (klass && FL_TEST(klass, FL_SINGLETON)) {
2012 if ((mtbl = RCLASS_M_TBL(origin)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2013 klass = RCLASS_SUPER(klass);
2014 }
2015 if (recur) {
2016 while (klass && (FL_TEST(klass, FL_SINGLETON) || RB_TYPE_P(klass, T_ICLASS))) {
2017 if (klass != origin && (mtbl = RCLASS_M_TBL(klass)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2018 klass = RCLASS_SUPER(klass);
2019 }
2020 }
2021 ary = rb_ary_new2(me_arg.list->num_entries);
2022 st_foreach(me_arg.list, ins_methods_i, ary);
2023 st_free_table(me_arg.list);
2024
2025 return ary;
2026}
2027
2036#ifdef rb_define_method_id
2037#undef rb_define_method_id
2038#endif
2039void
2040rb_define_method_id(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc)
2041{
2042 rb_add_method_cfunc(klass, mid, func, argc, METHOD_VISI_PUBLIC);
2043}
2044
2045#ifdef rb_define_method
2046#undef rb_define_method
2047#endif
2048void
2049rb_define_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
2050{
2051 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PUBLIC);
2052}
2053
2054#ifdef rb_define_protected_method
2055#undef rb_define_protected_method
2056#endif
2057void
2058rb_define_protected_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
2059{
2060 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PROTECTED);
2061}
2062
2063#ifdef rb_define_private_method
2064#undef rb_define_private_method
2065#endif
2066void
2067rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
2068{
2069 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PRIVATE);
2070}
2071
2072void
2073rb_undef_method(VALUE klass, const char *name)
2074{
2075 rb_add_method(klass, rb_intern(name), VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2076}
2077
2078static enum rb_id_table_iterator_result
2079undef_method_i(ID name, VALUE value, void *data)
2080{
2081 VALUE klass = (VALUE)data;
2082 rb_add_method(klass, name, VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2083 return ID_TABLE_CONTINUE;
2084}
2085
2086void
2087rb_undef_methods_from(VALUE klass, VALUE super)
2088{
2089 struct rb_id_table *mtbl = RCLASS_M_TBL(super);
2090 if (mtbl) {
2091 rb_id_table_foreach(mtbl, undef_method_i, (void *)klass);
2092 }
2093}
2094
2103static inline VALUE
2104special_singleton_class_of(VALUE obj)
2105{
2106 switch (obj) {
2107 case Qnil: return rb_cNilClass;
2108 case Qfalse: return rb_cFalseClass;
2109 case Qtrue: return rb_cTrueClass;
2110 default: return Qnil;
2111 }
2112}
2113
2114VALUE
2115rb_special_singleton_class(VALUE obj)
2116{
2117 return special_singleton_class_of(obj);
2118}
2119
2129static VALUE
2130singleton_class_of(VALUE obj)
2131{
2132 VALUE klass;
2133
2134 switch (TYPE(obj)) {
2135 case T_FIXNUM:
2136 case T_BIGNUM:
2137 case T_FLOAT:
2138 case T_SYMBOL:
2139 rb_raise(rb_eTypeError, "can't define singleton");
2140
2141 case T_FALSE:
2142 case T_TRUE:
2143 case T_NIL:
2144 klass = special_singleton_class_of(obj);
2145 if (NIL_P(klass))
2146 rb_bug("unknown immediate %p", (void *)obj);
2147 return klass;
2148
2149 case T_STRING:
2150 if (FL_TEST_RAW(obj, RSTRING_FSTR)) {
2151 rb_raise(rb_eTypeError, "can't define singleton");
2152 }
2153 }
2154
2155 klass = METACLASS_OF(obj);
2156 if (!(FL_TEST(klass, FL_SINGLETON) &&
2157 rb_attr_get(klass, id_attached) == obj)) {
2158 klass = rb_make_metaclass(obj, klass);
2159 }
2160
2161 RB_FL_SET_RAW(klass, RB_OBJ_FROZEN_RAW(obj));
2162
2163 return klass;
2164}
2165
2166void
2168{
2169 /* should not propagate to meta-meta-class, and so on */
2170 if (!(RBASIC(x)->flags & FL_SINGLETON)) {
2171 VALUE klass = RBASIC_CLASS(x);
2172 if (klass && // no class when hidden from ObjectSpace
2174 OBJ_FREEZE_RAW(klass);
2175 }
2176 }
2177}
2178
2186VALUE
2188{
2189 VALUE klass;
2190
2191 if (SPECIAL_CONST_P(obj)) {
2192 return rb_special_singleton_class(obj);
2193 }
2194 klass = METACLASS_OF(obj);
2195 if (!FL_TEST(klass, FL_SINGLETON)) return Qnil;
2196 if (rb_attr_get(klass, id_attached) != obj) return Qnil;
2197 return klass;
2198}
2199
2200VALUE
2202{
2203 VALUE klass = singleton_class_of(obj);
2204
2205 /* ensures an exposed class belongs to its own eigenclass */
2206 if (RB_TYPE_P(obj, T_CLASS)) (void)ENSURE_EIGENCLASS(klass);
2207
2208 return klass;
2209}
2210
2220#ifdef rb_define_singleton_method
2221#undef rb_define_singleton_method
2222#endif
2223void
2224rb_define_singleton_method(VALUE obj, const char *name, VALUE (*func)(ANYARGS), int argc)
2225{
2226 rb_define_method(singleton_class_of(obj), name, func, argc);
2227}
2228
2229#ifdef rb_define_module_function
2230#undef rb_define_module_function
2231#endif
2232void
2233rb_define_module_function(VALUE module, const char *name, VALUE (*func)(ANYARGS), int argc)
2234{
2235 rb_define_private_method(module, name, func, argc);
2236 rb_define_singleton_method(module, name, func, argc);
2237}
2238
2239#ifdef rb_define_global_function
2240#undef rb_define_global_function
2241#endif
2242void
2243rb_define_global_function(const char *name, VALUE (*func)(ANYARGS), int argc)
2244{
2245 rb_define_module_function(rb_mKernel, name, func, argc);
2246}
2247
2248void
2249rb_define_alias(VALUE klass, const char *name1, const char *name2)
2250{
2251 rb_alias(klass, rb_intern(name1), rb_intern(name2));
2252}
2253
2254void
2255rb_define_attr(VALUE klass, const char *name, int read, int write)
2256{
2257 rb_attr(klass, rb_intern(name), read, write, FALSE);
2258}
2259
2260MJIT_FUNC_EXPORTED VALUE
2261rb_keyword_error_new(const char *error, VALUE keys)
2262{
2263 long i = 0, len = RARRAY_LEN(keys);
2264 VALUE error_message = rb_sprintf("%s keyword%.*s", error, len > 1, "s");
2265
2266 if (len > 0) {
2267 rb_str_cat_cstr(error_message, ": ");
2268 while (1) {
2269 const VALUE k = RARRAY_AREF(keys, i);
2270 rb_str_append(error_message, rb_inspect(k));
2271 if (++i >= len) break;
2272 rb_str_cat_cstr(error_message, ", ");
2273 }
2274 }
2275
2276 return rb_exc_new_str(rb_eArgError, error_message);
2277}
2278
2279NORETURN(static void rb_keyword_error(const char *error, VALUE keys));
2280static void
2281rb_keyword_error(const char *error, VALUE keys)
2282{
2283 rb_exc_raise(rb_keyword_error_new(error, keys));
2284}
2285
2286NORETURN(static void unknown_keyword_error(VALUE hash, const ID *table, int keywords));
2287static void
2288unknown_keyword_error(VALUE hash, const ID *table, int keywords)
2289{
2290 int i;
2291 for (i = 0; i < keywords; i++) {
2292 st_data_t key = ID2SYM(table[i]);
2293 rb_hash_stlike_delete(hash, &key, NULL);
2294 }
2295 rb_keyword_error("unknown", rb_hash_keys(hash));
2296}
2297
2298
2299static int
2300separate_symbol(st_data_t key, st_data_t value, st_data_t arg)
2301{
2302 VALUE *kwdhash = (VALUE *)arg;
2303 if (!SYMBOL_P(key)) kwdhash++;
2304 if (!*kwdhash) *kwdhash = rb_hash_new();
2305 rb_hash_aset(*kwdhash, (VALUE)key, (VALUE)value);
2306 return ST_CONTINUE;
2307}
2308
2309VALUE
2311{
2312 VALUE parthash[2] = {0, 0};
2313 VALUE hash = *orighash;
2314
2315 if (RHASH_EMPTY_P(hash)) {
2316 *orighash = 0;
2317 return hash;
2318 }
2319 rb_hash_foreach(hash, separate_symbol, (st_data_t)&parthash);
2320 *orighash = parthash[1];
2321 if (parthash[1] && RBASIC_CLASS(hash) != rb_cHash) {
2322 RBASIC_SET_CLASS(parthash[1], RBASIC_CLASS(hash));
2323 }
2324 return parthash[0];
2325}
2326
2327int
2328rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
2329{
2330 int i = 0, j;
2331 int rest = 0;
2332 VALUE missing = Qnil;
2333 st_data_t key;
2334
2335#define extract_kwarg(keyword, val) \
2336 (key = (st_data_t)(keyword), values ? \
2337 (rb_hash_stlike_delete(keyword_hash, &key, &(val)) || ((val) = Qundef, 0)) : \
2338 rb_hash_stlike_lookup(keyword_hash, key, NULL))
2339
2340 if (NIL_P(keyword_hash)) keyword_hash = 0;
2341
2342 if (optional < 0) {
2343 rest = 1;
2344 optional = -1-optional;
2345 }
2346 if (required) {
2347 for (; i < required; i++) {
2348 VALUE keyword = ID2SYM(table[i]);
2349 if (keyword_hash) {
2350 if (extract_kwarg(keyword, values[i])) {
2351 continue;
2352 }
2353 }
2354 if (NIL_P(missing)) missing = rb_ary_hidden_new(1);
2355 rb_ary_push(missing, keyword);
2356 }
2357 if (!NIL_P(missing)) {
2358 rb_keyword_error("missing", missing);
2359 }
2360 }
2361 j = i;
2362 if (optional && keyword_hash) {
2363 for (i = 0; i < optional; i++) {
2364 if (extract_kwarg(ID2SYM(table[required+i]), values[required+i])) {
2365 j++;
2366 }
2367 }
2368 }
2369 if (!rest && keyword_hash) {
2370 if (RHASH_SIZE(keyword_hash) > (unsigned int)(values ? 0 : j)) {
2371 unknown_keyword_error(keyword_hash, table, required+optional);
2372 }
2373 }
2374 if (values && !keyword_hash) {
2375 for (i = 0; i < required + optional; i++) {
2376 values[i] = Qundef;
2377 }
2378 }
2379 return j;
2380#undef extract_kwarg
2381}
2382
2384 int kw_flag;
2385 int n_lead;
2386 int n_opt;
2387 int n_trail;
2388 bool f_var;
2389 bool f_hash;
2390 bool f_block;
2391};
2392
2393static void
2394rb_scan_args_parse(int kw_flag, const char *fmt, struct rb_scan_args_t *arg)
2395{
2396 const char *p = fmt;
2397
2398 memset(arg, 0, sizeof(*arg));
2399 arg->kw_flag = kw_flag;
2400
2401 if (ISDIGIT(*p)) {
2402 arg->n_lead = *p - '0';
2403 p++;
2404 if (ISDIGIT(*p)) {
2405 arg->n_opt = *p - '0';
2406 p++;
2407 }
2408 }
2409 if (*p == '*') {
2410 arg->f_var = 1;
2411 p++;
2412 }
2413 if (ISDIGIT(*p)) {
2414 arg->n_trail = *p - '0';
2415 p++;
2416 }
2417 if (*p == ':') {
2418 arg->f_hash = 1;
2419 p++;
2420 }
2421 if (*p == '&') {
2422 arg->f_block = 1;
2423 p++;
2424 }
2425 if (*p != '\0') {
2426 rb_fatal("bad scan arg format: %s", fmt);
2427 }
2428}
2429
2430static int
2431rb_scan_args_assign(const struct rb_scan_args_t *arg, int argc, const VALUE *const argv, va_list vargs)
2432{
2433 int i, argi = 0;
2434 VALUE *var, hash = Qnil;
2435#define rb_scan_args_next_param() va_arg(vargs, VALUE *)
2436 const int kw_flag = arg->kw_flag;
2437 const int n_lead = arg->n_lead;
2438 const int n_opt = arg->n_opt;
2439 const int n_trail = arg->n_trail;
2440 const int n_mand = n_lead + n_trail;
2441 const bool f_var = arg->f_var;
2442 const bool f_hash = arg->f_hash;
2443 const bool f_block = arg->f_block;
2444
2445 /* capture an option hash - phase 1: pop from the argv */
2446 if (f_hash && argc > 0) {
2447 VALUE last = argv[argc - 1];
2448 if (rb_scan_args_keyword_p(kw_flag, last)) {
2449 hash = rb_hash_dup(last);
2450 argc--;
2451 }
2452 }
2453
2454 if (argc < n_mand) {
2455 goto argc_error;
2456 }
2457
2458 /* capture leading mandatory arguments */
2459 for (i = 0; i < n_lead; i++) {
2460 var = rb_scan_args_next_param();
2461 if (var) *var = argv[argi];
2462 argi++;
2463 }
2464 /* capture optional arguments */
2465 for (i = 0; i < n_opt; i++) {
2466 var = rb_scan_args_next_param();
2467 if (argi < argc - n_trail) {
2468 if (var) *var = argv[argi];
2469 argi++;
2470 }
2471 else {
2472 if (var) *var = Qnil;
2473 }
2474 }
2475 /* capture variable length arguments */
2476 if (f_var) {
2477 int n_var = argc - argi - n_trail;
2478
2479 var = rb_scan_args_next_param();
2480 if (0 < n_var) {
2481 if (var) *var = rb_ary_new_from_values(n_var, &argv[argi]);
2482 argi += n_var;
2483 }
2484 else {
2485 if (var) *var = rb_ary_new();
2486 }
2487 }
2488 /* capture trailing mandatory arguments */
2489 for (i = 0; i < n_trail; i++) {
2490 var = rb_scan_args_next_param();
2491 if (var) *var = argv[argi];
2492 argi++;
2493 }
2494 /* capture an option hash - phase 2: assignment */
2495 if (f_hash) {
2496 var = rb_scan_args_next_param();
2497 if (var) *var = hash;
2498 }
2499 /* capture iterator block */
2500 if (f_block) {
2501 var = rb_scan_args_next_param();
2502 if (rb_block_given_p()) {
2503 *var = rb_block_proc();
2504 }
2505 else {
2506 *var = Qnil;
2507 }
2508 }
2509
2510 if (argi == argc) {
2511 return argc;
2512 }
2513
2514 argc_error:
2515 return -(argc + 1);
2516#undef rb_scan_args_next_param
2517}
2518
2519static int
2520rb_scan_args_result(const struct rb_scan_args_t *const arg, int argc)
2521{
2522 const int n_lead = arg->n_lead;
2523 const int n_opt = arg->n_opt;
2524 const int n_trail = arg->n_trail;
2525 const int n_mand = n_lead + n_trail;
2526 const bool f_var = arg->f_var;
2527
2528 if (argc >= 0) {
2529 return argc;
2530 }
2531
2532 argc = -argc - 1;
2533 rb_error_arity(argc, n_mand, f_var ? UNLIMITED_ARGUMENTS : n_mand + n_opt);
2535}
2536
2537#undef rb_scan_args
2538int
2539rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...)
2540{
2541 va_list vargs;
2542 struct rb_scan_args_t arg;
2543 rb_scan_args_parse(RB_SCAN_ARGS_PASS_CALLED_KEYWORDS, fmt, &arg);
2544 va_start(vargs,fmt);
2545 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
2546 va_end(vargs);
2547 return rb_scan_args_result(&arg, argc);
2548}
2549
2550#undef rb_scan_args_kw
2551int
2552rb_scan_args_kw(int kw_flag, int argc, const VALUE *argv, const char *fmt, ...)
2553{
2554 va_list vargs;
2555 struct rb_scan_args_t arg;
2556 rb_scan_args_parse(kw_flag, fmt, &arg);
2557 va_start(vargs,fmt);
2558 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
2559 va_end(vargs);
2560 return rb_scan_args_result(&arg, argc);
2561}
2562
#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
@ 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:1819
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
Definition class.c:1090
VALUE rb_refinement_new(void)
Creates a new, anonymous refinement.
Definition class.c:985
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition class.c:888
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:729
VALUE rb_singleton_class_clone(VALUE obj)
Clones a singleton class.
Definition class.c:572
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:1345
VALUE rb_class_subclasses(VALUE klass)
Queries the class's direct descendants.
Definition class.c:1599
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
Definition class.c:2201
void Init_class_hierarchy(void)
Internal header aggregating init functions.
Definition class.c:807
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition class.c:920
VALUE rb_class_attached_object(VALUE klass)
Returns the attached object for a singleton class.
Definition class.c:1622
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:1996
VALUE rb_module_new(void)
Creates a new, anonymous module.
Definition class.c:979
#define META_CLASS_OF_CLASS_CLASS_P(k)
whether k is a meta^(n)-class of Class class
Definition class.c:649
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:1804
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:1857
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:998
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:1028
void rb_singleton_class_attached(VALUE klass, VALUE obj)
Attaches a singleton class to its corresponding object.
Definition class.c:637
void rb_freeze_singleton_class(VALUE x)
This is an implementation detail of RB_OBJ_FREEZE().
Definition class.c:2167
VALUE rb_mod_included_modules(VALUE mod)
Queries the list of included modules.
Definition class.c:1417
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:926
VALUE rb_mod_ancestors(VALUE mod)
Queries the module's ancestors.
Definition class.c:1485
static VALUE make_metaclass(VALUE klass)
Creates a metaclass of klass.
Definition class.c:694
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:879
VALUE rb_mod_include_p(VALUE mod, VALUE mod2)
Queries if the passed module is included by the module.
Definition class.c:1453
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:1842
#define ENSURE_EIGENCLASS(klass)
ensures klass belongs to its own eigenclass.
Definition class.c:680
VALUE rb_mod_init_copy(VALUE clone, VALUE orig)
The comment that comes with this function says :nodoc:.
Definition class.c:463
VALUE rb_define_module_under(VALUE outer, const char *name)
Defines a module under the namespace of outer.
Definition class.c:1022
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:2187
VALUE rb_define_module_id(ID id)
This is a very badly designed API that creates an anonymous module.
Definition class.c:992
VALUE rb_define_class_id(ID id, VALUE super)
This is a very badly designed API that creates an anonymous class.
Definition class.c:858
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition class.c:2249
VALUE rb_extract_keywords(VALUE *orighash)
Splits a hash into two.
Definition class.c:2310
void rb_define_attr(VALUE klass, const char *name, int read, int write)
Defines public accessor method(s) for an attribute.
Definition class.c:2255
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
Definition class.c:2073
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition eval.c:864
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
Definition class.c:2328
#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:3148
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition eval.c:684
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:3199
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_cObject
Documented in include/ruby/internal/globals.h.
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
#define UNLIMITED_ARGUMENTS
This macro is used in conjunction with rb_check_arity().
Definition error.h:35
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:3324
#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:2158
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:1738
void rb_clear_constant_cache_for_id(ID id)
Clears the inline constant caches associated with a particular ID.
Definition vm_method.c:142
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
#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:62
Definition class.c:1684
Definition constant.h:33
CREF (Class REFerence)
Definition method.h:44
Definition method.h:54
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
Definition method.h:134
rb_cref_t * cref
class reference, should be marked
Definition method.h:135
Internal header for Class.
Definition class.h:23
Definition st.h:79
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52