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