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