Ruby: class.c Source File

Ruby 3.3.4p94 (2024-07-09 revision be1089c8ec5ba40e09b1553e36b3174bf4014d9d)

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
72 RUBY_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
230class_alloc(VALUE flags, VALUE klass)
231{
232 size_t alloc_size = sizeof(struct RClass) + sizeof(rb_classext_t);
233
234 flags &= T_MASK;
235 if (RGENGC_WB_PROTECTED_CLASS) flags |= FL_WB_PROTECTED;
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
271 VALUE
272rb_class_boot(VALUE super)
273{
274 VALUE klass = class_alloc(T_CLASS, rb_cClass);
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
335rb_check_inheritable(VALUE super)
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
349 VALUE
350rb_class_new(VALUE super)
351{
352 Check_Type(super, T_CLASS);
353 rb_check_inheritable(super);
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
363 VALUE
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
382 struct clone_method_arg {
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
395 struct clone_const_arg {
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
432 struct cvc_table_copy_ctx {
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: */
523 VALUE
524rb_mod_init_copy(VALUE clone, VALUE orig)
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
635 VALUE
636rb_singleton_class_clone(VALUE obj)
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`.
642 VALUE
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
700rb_singleton_class_attached(VALUE klass, VALUE obj)
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
757make_metaclass(VALUE klass)
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
792make_singleton_class(VALUE obj)
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
914 ENSURE_EIGENCLASS(rb_cRefinement);
915}
916
917
928 VALUE
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
939 VALUE
940rb_define_class_id(ID id, VALUE super)
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
960 VALUE
961rb_class_inherited(VALUE super, VALUE klass)
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
969 VALUE
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
1001 VALUE
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
1007 VALUE
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
1040 VALUE
1041rb_define_class_id_under(VALUE outer, ID id, VALUE super)
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
1048 VALUE
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
1065 VALUE
1066rb_module_new(void)
1067{
1068 return module_new(rb_cModule);
1069}
1070
1071 VALUE
1072rb_refinement_new(void)
1073{
1074 return module_new(rb_cRefinement);
1075}
1076
1077// Kept for compatibility. Use rb_module_new() instead.
1078 VALUE
1079rb_define_module_id(ID id)
1080{
1081 return rb_module_new();
1082}
1083
1084 VALUE
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
1108 VALUE
1109rb_define_module_under(VALUE outer, const char *name)
1110{
1111 return rb_define_module_id_under(outer, rb_intern(name));
1112}
1113
1114 VALUE
1115rb_define_module_id_under(VALUE outer, ID id)
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
1138 VALUE
1139rb_include_class_new(VALUE module, VALUE super)
1140{
1141 VALUE klass = class_alloc(T_ICLASS, rb_cClass);
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
1177rb_include_module(VALUE klass, VALUE module)
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 int do_include = 1;
1196 while (iclass) {
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{
1255 rb_clear_constant_cache_for_id(id);
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 add_subclass = TRUE;
1328 if (module != RCLASS_ORIGIN(module)) {
1329 if (!origin_stack) origin_stack = rb_ary_hidden_new(2);
1330 VALUE origin[2] = {iclass, RCLASS_ORIGIN(module)};
1331 rb_ary_cat(origin_stack, origin, 2);
1332 }
1333 else if (origin_stack && (origin_len = RARRAY_LEN(origin_stack)) > 1 &&
1334 RARRAY_AREF(origin_stack, origin_len - 1) == module) {
1335 RCLASS_SET_ORIGIN(RARRAY_AREF(origin_stack, (origin_len -= 2)), iclass);
1336 RICLASS_SET_ORIGIN_SHARED_MTBL(iclass);
1337 rb_ary_resize(origin_stack, origin_len);
1338 add_subclass = FALSE;
1339 }
1340
1341 if (add_subclass) {
1342 VALUE m = module;
1343 if (BUILTIN_TYPE(m) == T_ICLASS) m = METACLASS_OF(m);
1344 rb_module_add_to_subclasses_list(m, iclass);
1345 }
1346
1347 if (BUILTIN_TYPE(klass) == T_MODULE && FL_TEST(klass, RMODULE_IS_REFINEMENT)) {
1348 VALUE refined_class =
1349 rb_refinement_module_get_refined_class(klass);
1350
1351 rb_id_table_foreach(RCLASS_M_TBL(module), add_refined_method_entry_i, (void *)refined_class);
1352 RUBY_ASSERT(BUILTIN_TYPE(c) == T_MODULE);
1353 }
1354
1355 tbl = RCLASS_CONST_TBL(module);
1356 if (tbl && rb_id_table_size(tbl))
1357 rb_id_table_foreach(tbl, clear_constant_cache_i, NULL);
1358 skip:
1359 module = RCLASS_SUPER(module);
1360 }
1361
1362 return method_changed;
1363}
1364
1365static int
1366include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super)
1367{
1368 return do_include_modules_at(klass, c, module, search_super, true);
1369}
1370
1371static enum rb_id_table_iterator_result
1372move_refined_method(ID key, VALUE value, void *data)
1373{
1374 rb_method_entry_t *me = (rb_method_entry_t *)value;
1375
1376 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1377 VALUE klass = (VALUE)data;
1378 struct rb_id_table *tbl = RCLASS_M_TBL(klass);
1379
1380 if (me->def->body.refined.orig_me) {
1381 const rb_method_entry_t *orig_me = me->def->body.refined.orig_me, *new_me;
1382 RB_OBJ_WRITE(me, &me->def->body.refined.orig_me, NULL);
1383 new_me = rb_method_entry_clone(me);
1384 rb_method_table_insert(klass, tbl, key, new_me);
1385 rb_method_entry_copy(me, orig_me);
1386 return ID_TABLE_CONTINUE;
1387 }
1388 else {
1389 rb_method_table_insert(klass, tbl, key, me);
1390 return ID_TABLE_DELETE;
1391 }
1392 }
1393 else {
1394 return ID_TABLE_CONTINUE;
1395 }
1396}
1397
1398static enum rb_id_table_iterator_result
1399cache_clear_refined_method(ID key, VALUE value, void *data)
1400{
1401 rb_method_entry_t *me = (rb_method_entry_t *) value;
1402
1403 if (me->def->type == VM_METHOD_TYPE_REFINED && me->def->body.refined.orig_me) {
1404 VALUE klass = (VALUE)data;
1405 rb_clear_method_cache(klass, me->called_id);
1406 }
1407 // Refined method entries without an orig_me is going to stay in the method
1408 // table of klass, like before the move, so no need to clear the cache.
1409
1410 return ID_TABLE_CONTINUE;
1411}
1412
1413static bool
1414ensure_origin(VALUE klass)
1415{
1416 VALUE origin = RCLASS_ORIGIN(klass);
1417 if (origin == klass) {
1418 origin = class_alloc(T_ICLASS, klass);
1419 RCLASS_SET_M_TBL(origin, RCLASS_M_TBL(klass));
1420 RCLASS_SET_SUPER(origin, RCLASS_SUPER(klass));
1421 RCLASS_SET_SUPER(klass, origin);
1422 RCLASS_SET_ORIGIN(klass, origin);
1423 RCLASS_M_TBL_INIT(klass);
1424 rb_id_table_foreach(RCLASS_M_TBL(origin), cache_clear_refined_method, (void *)klass);
1425 rb_id_table_foreach(RCLASS_M_TBL(origin), move_refined_method, (void *)klass);
1426 return true;
1427 }
1428 return false;
1429}
1430
1431void
1432rb_prepend_module(VALUE klass, VALUE module)
1433{
1434 int changed;
1435 bool klass_had_no_origin;
1436
1437 ensure_includable(klass, module);
1438 if (module_in_super_chain(klass, module))
1439 rb_raise(rb_eArgError, "cyclic prepend detected");
1440
1441 klass_had_no_origin = ensure_origin(klass);
1442 changed = do_include_modules_at(klass, klass, module, FALSE, false);
1443 RUBY_ASSERT(changed >= 0); // already checked for cyclic prepend above
1444 if (changed) {
1445 rb_vm_check_redefinition_by_prepend(klass);
1446 }
1447 if (RB_TYPE_P(klass, T_MODULE)) {
1448 rb_subclass_entry_t *iclass = RCLASS_SUBCLASSES(klass);
1449 // skip the placeholder subclass entry at the head of the list if it exists
1450 if (iclass) {
1451 RUBY_ASSERT(!iclass->klass);
1452 iclass = iclass->next;
1453 }
1454
1455 VALUE klass_origin = RCLASS_ORIGIN(klass);
1456 struct rb_id_table *klass_m_tbl = RCLASS_M_TBL(klass);
1457 struct rb_id_table *klass_origin_m_tbl = RCLASS_M_TBL(klass_origin);
1458 while (iclass) {
1459 /* During lazy sweeping, iclass->klass could be a dead object that
1460 * has not yet been swept. */
1461 if (!rb_objspace_garbage_object_p(iclass->klass)) {
1462 const VALUE subclass = iclass->klass;
1463 if (klass_had_no_origin && klass_origin_m_tbl == RCLASS_M_TBL(subclass)) {
1464 // backfill an origin iclass to handle refinements and future prepends
1465 rb_id_table_foreach(RCLASS_M_TBL(subclass), clear_module_cache_i, (void *)subclass);
1466 RCLASS_M_TBL(subclass) = klass_m_tbl;
1467 VALUE origin = rb_include_class_new(klass_origin, RCLASS_SUPER(subclass));
1468 RCLASS_SET_SUPER(subclass, origin);
1469 RCLASS_SET_INCLUDER(origin, RCLASS_INCLUDER(subclass));
1470 RCLASS_SET_ORIGIN(subclass, origin);
1471 RICLASS_SET_ORIGIN_SHARED_MTBL(origin);
1472 }
1473 include_modules_at(subclass, subclass, module, FALSE);
1474 }
1475
1476 iclass = iclass->next;
1477 }
1478 }
1479}
1480
1481/*
1482 * call-seq:
1483 * mod.included_modules -> array
1484 *
1485 * Returns the list of modules included or prepended in <i>mod</i>
1486 * or one of <i>mod</i>'s ancestors.
1487 *
1488 * module Sub
1489 * end
1490 *
1491 * module Mixin
1492 * prepend Sub
1493 * end
1494 *
1495 * module Outer
1496 * include Mixin
1497 * end
1498 *
1499 * Mixin.included_modules #=> [Sub]
1500 * Outer.included_modules #=> [Sub, Mixin]
1501 */
1502
1503 VALUE
1504rb_mod_included_modules(VALUE mod)
1505{
1506 VALUE ary = rb_ary_new();
1507 VALUE p;
1508 VALUE origin = RCLASS_ORIGIN(mod);
1509
1510 for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
1511 if (p != origin && RCLASS_ORIGIN(p) == p && BUILTIN_TYPE(p) == T_ICLASS) {
1512 VALUE m = METACLASS_OF(p);
1513 if (RB_TYPE_P(m, T_MODULE))
1514 rb_ary_push(ary, m);
1515 }
1516 }
1517 return ary;
1518}
1519
1520/*
1521 * call-seq:
1522 * mod.include?(module) -> true or false
1523 *
1524 * Returns <code>true</code> if <i>module</i> is included
1525 * or prepended in <i>mod</i> or one of <i>mod</i>'s ancestors.
1526 *
1527 * module A
1528 * end
1529 * class B
1530 * include A
1531 * end
1532 * class C < B
1533 * end
1534 * B.include?(A) #=> true
1535 * C.include?(A) #=> true
1536 * A.include?(A) #=> false
1537 */
1538
1539 VALUE
1540rb_mod_include_p(VALUE mod, VALUE mod2)
1541{
1542 VALUE p;
1543
1544 Check_Type(mod2, T_MODULE);
1545 for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
1546 if (BUILTIN_TYPE(p) == T_ICLASS && !FL_TEST(p, RICLASS_IS_ORIGIN)) {
1547 if (METACLASS_OF(p) == mod2) return Qtrue;
1548 }
1549 }
1550 return Qfalse;
1551}
1552
1553/*
1554 * call-seq:
1555 * mod.ancestors -> array
1556 *
1557 * Returns a list of modules included/prepended in <i>mod</i>
1558 * (including <i>mod</i> itself).
1559 *
1560 * module Mod
1561 * include Math
1562 * include Comparable
1563 * prepend Enumerable
1564 * end
1565 *
1566 * Mod.ancestors #=> [Enumerable, Mod, Comparable, Math]
1567 * Math.ancestors #=> [Math]
1568 * Enumerable.ancestors #=> [Enumerable]
1569 */
1570
1571 VALUE
1572rb_mod_ancestors(VALUE mod)
1573{
1574 VALUE p, ary = rb_ary_new();
1575 VALUE refined_class = Qnil;
1576 if (BUILTIN_TYPE(mod) == T_MODULE && FL_TEST(mod, RMODULE_IS_REFINEMENT)) {
1577 refined_class = rb_refinement_module_get_refined_class(mod);
1578 }
1579
1580 for (p = mod; p; p = RCLASS_SUPER(p)) {
1581 if (p == refined_class) break;
1582 if (p != RCLASS_ORIGIN(p)) continue;
1583 if (BUILTIN_TYPE(p) == T_ICLASS) {
1584 rb_ary_push(ary, METACLASS_OF(p));
1585 }
1586 else {
1587 rb_ary_push(ary, p);
1588 }
1589 }
1590 return ary;
1591}
1592
1593 struct subclass_traverse_data
1594{
1595 VALUE buffer;
1596 long count;
1597 long maxcount;
1598 bool immediate_only;
1599};
1600
1601static void
1602class_descendants_recursive(VALUE klass, VALUE v)
1603{
1604 struct subclass_traverse_data *data = (struct subclass_traverse_data *) v;
1605
1606 if (BUILTIN_TYPE(klass) == T_CLASS && !FL_TEST(klass, FL_SINGLETON)) {
1607 if (data->buffer && data->count < data->maxcount && !rb_objspace_garbage_object_p(klass)) {
1608 // assumes that this does not cause GC as long as the length does not exceed the capacity
1609 rb_ary_push(data->buffer, klass);
1610 }
1611 data->count++;
1612 if (!data->immediate_only) {
1613 rb_class_foreach_subclass(klass, class_descendants_recursive, v);
1614 }
1615 }
1616 else {
1617 rb_class_foreach_subclass(klass, class_descendants_recursive, v);
1618 }
1619}
1620
1621static VALUE
1622class_descendants(VALUE klass, bool immediate_only)
1623{
1624 struct subclass_traverse_data data = { Qfalse, 0, -1, immediate_only };
1625
1626 // estimate the count of subclasses
1627 rb_class_foreach_subclass(klass, class_descendants_recursive, (VALUE) &data);
1628
1629 // the following allocation may cause GC which may change the number of subclasses
1630 data.buffer = rb_ary_new_capa(data.count);
1631 data.maxcount = data.count;
1632 data.count = 0;
1633
1634 size_t gc_count = rb_gc_count();
1635
1636 // enumerate subclasses
1637 rb_class_foreach_subclass(klass, class_descendants_recursive, (VALUE) &data);
1638
1639 if (gc_count != rb_gc_count()) {
1640 rb_bug("GC must not occur during the subclass iteration of Class#descendants");
1641 }
1642
1643 return data.buffer;
1644}
1645
1646/*
1647 * call-seq:
1648 * subclasses -> array
1649 *
1650 * Returns an array of classes where the receiver is the
1651 * direct superclass of the class, excluding singleton classes.
1652 * The order of the returned array is not defined.
1653 *
1654 * class A; end
1655 * class B < A; end
1656 * class C < B; end
1657 * class D < A; end
1658 *
1659 * A.subclasses #=> [D, B]
1660 * B.subclasses #=> [C]
1661 * C.subclasses #=> []
1662 *
1663 * Anonymous subclasses (not associated with a constant) are
1664 * returned, too:
1665 *
1666 * c = Class.new(A)
1667 * A.subclasses # => [#<Class:0x00007f003c77bd78>, D, B]
1668 *
1669 * Note that the parent does not hold references to subclasses
1670 * and doesn't prevent them from being garbage collected. This
1671 * means that the subclass might disappear when all references
1672 * to it are dropped:
1673 *
1674 * # drop the reference to subclass, it can be garbage-collected now
1675 * c = nil
1676 *
1677 * A.subclasses
1678 * # It can be
1679 * # => [#<Class:0x00007f003c77bd78>, D, B]
1680 * # ...or just
1681 * # => [D, B]
1682 * # ...depending on whether garbage collector was run
1683 */
1684
1685 VALUE
1686rb_class_subclasses(VALUE klass)
1687{
1688 return class_descendants(klass, true);
1689}
1690
1691/*
1692 * call-seq:
1693 * attached_object -> object
1694 *
1695 * Returns the object for which the receiver is the singleton class.
1696 *
1697 * Raises an TypeError if the class is not a singleton class.
1698 *
1699 * class Foo; end
1700 *
1701 * Foo.singleton_class.attached_object #=> Foo
1702 * Foo.attached_object #=> TypeError: `Foo' is not a singleton class
1703 * Foo.new.singleton_class.attached_object #=> #<Foo:0x000000010491a370>
1704 * TrueClass.attached_object #=> TypeError: `TrueClass' is not a singleton class
1705 * NilClass.attached_object #=> TypeError: `NilClass' is not a singleton class
1706 */
1707
1708 VALUE
1709rb_class_attached_object(VALUE klass)
1710{
1711 if (!FL_TEST(klass, FL_SINGLETON)) {
1712 rb_raise(rb_eTypeError, "`%"PRIsVALUE"' is not a singleton class", klass);
1713 }
1714
1715 return RCLASS_ATTACHED_OBJECT(klass);
1716}
1717
1718static void
1719ins_methods_push(st_data_t name, st_data_t ary)
1720{
1721 rb_ary_push((VALUE)ary, ID2SYM((ID)name));
1722}
1723
1724static int
1725ins_methods_i(st_data_t name, st_data_t type, st_data_t ary)
1726{
1727 switch ((rb_method_visibility_t)type) {
1728 case METHOD_VISI_UNDEF:
1729 case METHOD_VISI_PRIVATE:
1730 break;
1731 default: /* everything but private */
1732 ins_methods_push(name, ary);
1733 break;
1734 }
1735 return ST_CONTINUE;
1736}
1737
1738static int
1739ins_methods_type_i(st_data_t name, st_data_t type, st_data_t ary, rb_method_visibility_t visi)
1740{
1741 if ((rb_method_visibility_t)type == visi) {
1742 ins_methods_push(name, ary);
1743 }
1744 return ST_CONTINUE;
1745}
1746
1747static int
1748ins_methods_prot_i(st_data_t name, st_data_t type, st_data_t ary)
1749{
1750 return ins_methods_type_i(name, type, ary, METHOD_VISI_PROTECTED);
1751}
1752
1753static int
1754ins_methods_priv_i(st_data_t name, st_data_t type, st_data_t ary)
1755{
1756 return ins_methods_type_i(name, type, ary, METHOD_VISI_PRIVATE);
1757}
1758
1759static int
1760ins_methods_pub_i(st_data_t name, st_data_t type, st_data_t ary)
1761{
1762 return ins_methods_type_i(name, type, ary, METHOD_VISI_PUBLIC);
1763}
1764
1765static int
1766ins_methods_undef_i(st_data_t name, st_data_t type, st_data_t ary)
1767{
1768 return ins_methods_type_i(name, type, ary, METHOD_VISI_UNDEF);
1769}
1770
1771 struct method_entry_arg {
1772 st_table *list;
1773 int recur;
1774};
1775
1776static enum rb_id_table_iterator_result
1777method_entry_i(ID key, VALUE value, void *data)
1778{
1779 const rb_method_entry_t *me = (const rb_method_entry_t *)value;
1780 struct method_entry_arg *arg = (struct method_entry_arg *)data;
1781 rb_method_visibility_t type;
1782
1783 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1784 VALUE owner = me->owner;
1785 me = rb_resolve_refined_method(Qnil, me);
1786 if (!me) return ID_TABLE_CONTINUE;
1787 if (!arg->recur && me->owner != owner) return ID_TABLE_CONTINUE;
1788 }
1789 if (!st_is_member(arg->list, key)) {
1790 if (UNDEFINED_METHOD_ENTRY_P(me)) {
1791 type = METHOD_VISI_UNDEF; /* none */
1792 }
1793 else {
1794 type = METHOD_ENTRY_VISI(me);
1795 RUBY_ASSERT(type != METHOD_VISI_UNDEF);
1796 }
1797 st_add_direct(arg->list, key, (st_data_t)type);
1798 }
1799 return ID_TABLE_CONTINUE;
1800}
1801
1802static void
1803add_instance_method_list(VALUE mod, struct method_entry_arg *me_arg)
1804{
1805 struct rb_id_table *m_tbl = RCLASS_M_TBL(mod);
1806 if (!m_tbl) return;
1807 rb_id_table_foreach(m_tbl, method_entry_i, me_arg);
1808}
1809
1810static bool
1811particular_class_p(VALUE mod)
1812{
1813 if (!mod) return false;
1814 if (FL_TEST(mod, FL_SINGLETON)) return true;
1815 if (BUILTIN_TYPE(mod) == T_ICLASS) return true;
1816 return false;
1817}
1818
1819static VALUE
1820class_instance_method_list(int argc, const VALUE *argv, VALUE mod, int obj, int (*func) (st_data_t, st_data_t, st_data_t))
1821{
1822 VALUE ary;
1823 int recur = TRUE, prepended = 0;
1824 struct method_entry_arg me_arg;
1825
1826 if (rb_check_arity(argc, 0, 1)) recur = RTEST(argv[0]);
1827
1828 me_arg.list = st_init_numtable();
1829 me_arg.recur = recur;
1830
1831 if (obj) {
1832 for (; particular_class_p(mod); mod = RCLASS_SUPER(mod)) {
1833 add_instance_method_list(mod, &me_arg);
1834 }
1835 }
1836
1837 if (!recur && RCLASS_ORIGIN(mod) != mod) {
1838 mod = RCLASS_ORIGIN(mod);
1839 prepended = 1;
1840 }
1841
1842 for (; mod; mod = RCLASS_SUPER(mod)) {
1843 add_instance_method_list(mod, &me_arg);
1844 if (BUILTIN_TYPE(mod) == T_ICLASS && !prepended) continue;
1845 if (!recur) break;
1846 }
1847 ary = rb_ary_new2(me_arg.list->num_entries);
1848 st_foreach(me_arg.list, func, ary);
1849 st_free_table(me_arg.list);
1850
1851 return ary;
1852}
1853
1854/*
1855 * call-seq:
1856 * mod.instance_methods(include_super=true) -> array
1857 *
1858 * Returns an array containing the names of the public and protected instance
1859 * methods in the receiver. For a module, these are the public and protected methods;
1860 * for a class, they are the instance (not singleton) methods. If the optional
1861 * parameter is <code>false</code>, the methods of any ancestors are not included.
1862 *
1863 * module A
1864 * def method1() end
1865 * end
1866 * class B
1867 * include A
1868 * def method2() end
1869 * end
1870 * class C < B
1871 * def method3() end
1872 * end
1873 *
1874 * A.instance_methods(false) #=> [:method1]
1875 * B.instance_methods(false) #=> [:method2]
1876 * B.instance_methods(true).include?(:method1) #=> true
1877 * C.instance_methods(false) #=> [:method3]
1878 * C.instance_methods.include?(:method2) #=> true
1879 *
1880 * Note that method visibility changes in the current class, as well as aliases,
1881 * are considered as methods of the current class by this method:
1882 *
1883 * class C < B
1884 * alias method4 method2
1885 * protected :method2
1886 * end
1887 * C.instance_methods(false).sort #=> [:method2, :method3, :method4]
1888 */
1889
1890 VALUE
1891rb_class_instance_methods(int argc, const VALUE *argv, VALUE mod)
1892{
1893 return class_instance_method_list(argc, argv, mod, 0, ins_methods_i);
1894}
1895
1896/*
1897 * call-seq:
1898 * mod.protected_instance_methods(include_super=true) -> array
1899 *
1900 * Returns a list of the protected instance methods defined in
1901 * <i>mod</i>. If the optional parameter is <code>false</code>, the
1902 * methods of any ancestors are not included.
1903 */
1904
1905 VALUE
1906rb_class_protected_instance_methods(int argc, const VALUE *argv, VALUE mod)
1907{
1908 return class_instance_method_list(argc, argv, mod, 0, ins_methods_prot_i);
1909}
1910
1911/*
1912 * call-seq:
1913 * mod.private_instance_methods(include_super=true) -> array
1914 *
1915 * Returns a list of the private instance methods defined in
1916 * <i>mod</i>. If the optional parameter is <code>false</code>, the
1917 * methods of any ancestors are not included.
1918 *
1919 * module Mod
1920 * def method1() end
1921 * private :method1
1922 * def method2() end
1923 * end
1924 * Mod.instance_methods #=> [:method2]
1925 * Mod.private_instance_methods #=> [:method1]
1926 */
1927
1928 VALUE
1929rb_class_private_instance_methods(int argc, const VALUE *argv, VALUE mod)
1930{
1931 return class_instance_method_list(argc, argv, mod, 0, ins_methods_priv_i);
1932}
1933
1934/*
1935 * call-seq:
1936 * mod.public_instance_methods(include_super=true) -> array
1937 *
1938 * Returns a list of the public instance methods defined in <i>mod</i>.
1939 * If the optional parameter is <code>false</code>, the methods of
1940 * any ancestors are not included.
1941 */
1942
1943 VALUE
1944rb_class_public_instance_methods(int argc, const VALUE *argv, VALUE mod)
1945{
1946 return class_instance_method_list(argc, argv, mod, 0, ins_methods_pub_i);
1947}
1948
1949/*
1950 * call-seq:
1951 * mod.undefined_instance_methods -> array
1952 *
1953 * Returns a list of the undefined instance methods defined in <i>mod</i>.
1954 * The undefined methods of any ancestors are not included.
1955 */
1956
1957 VALUE
1958rb_class_undefined_instance_methods(VALUE mod)
1959{
1960 VALUE include_super = Qfalse;
1961 return class_instance_method_list(1, &include_super, mod, 0, ins_methods_undef_i);
1962}
1963
1964/*
1965 * call-seq:
1966 * obj.methods(regular=true) -> array
1967 *
1968 * Returns a list of the names of public and protected methods of
1969 * <i>obj</i>. This will include all the methods accessible in
1970 * <i>obj</i>'s ancestors.
1971 * If the optional parameter is <code>false</code>, it
1972 * returns an array of <i>obj</i>'s public and protected singleton methods,
1973 * the array will not include methods in modules included in <i>obj</i>.
1974 *
1975 * class Klass
1976 * def klass_method()
1977 * end
1978 * end
1979 * k = Klass.new
1980 * k.methods[0..9] #=> [:klass_method, :nil?, :===,
1981 * # :==~, :!, :eql?
1982 * # :hash, :<=>, :class, :singleton_class]
1983 * k.methods.length #=> 56
1984 *
1985 * k.methods(false) #=> []
1986 * def k.singleton_method; end
1987 * k.methods(false) #=> [:singleton_method]
1988 *
1989 * module M123; def m123; end end
1990 * k.extend M123
1991 * k.methods(false) #=> [:singleton_method]
1992 */
1993
1994 VALUE
1995rb_obj_methods(int argc, const VALUE *argv, VALUE obj)
1996{
1997 rb_check_arity(argc, 0, 1);
1998 if (argc > 0 && !RTEST(argv[0])) {
1999 return rb_obj_singleton_methods(argc, argv, obj);
2000 }
2001 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_i);
2002}
2003
2004/*
2005 * call-seq:
2006 * obj.protected_methods(all=true) -> array
2007 *
2008 * Returns the list of protected methods accessible to <i>obj</i>. If
2009 * the <i>all</i> parameter is set to <code>false</code>, only those methods
2010 * in the receiver will be listed.
2011 */
2012
2013 VALUE
2014rb_obj_protected_methods(int argc, const VALUE *argv, VALUE obj)
2015{
2016 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_prot_i);
2017}
2018
2019/*
2020 * call-seq:
2021 * obj.private_methods(all=true) -> array
2022 *
2023 * Returns the list of private methods accessible to <i>obj</i>. If
2024 * the <i>all</i> parameter is set to <code>false</code>, only those methods
2025 * in the receiver will be listed.
2026 */
2027
2028 VALUE
2029rb_obj_private_methods(int argc, const VALUE *argv, VALUE obj)
2030{
2031 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_priv_i);
2032}
2033
2034/*
2035 * call-seq:
2036 * obj.public_methods(all=true) -> array
2037 *
2038 * Returns the list of public methods accessible to <i>obj</i>. If
2039 * the <i>all</i> parameter is set to <code>false</code>, only those methods
2040 * in the receiver will be listed.
2041 */
2042
2043 VALUE
2044rb_obj_public_methods(int argc, const VALUE *argv, VALUE obj)
2045{
2046 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_pub_i);
2047}
2048
2049/*
2050 * call-seq:
2051 * obj.singleton_methods(all=true) -> array
2052 *
2053 * Returns an array of the names of singleton methods for <i>obj</i>.
2054 * If the optional <i>all</i> parameter is true, the list will include
2055 * methods in modules included in <i>obj</i>.
2056 * Only public and protected singleton methods are returned.
2057 *
2058 * module Other
2059 * def three() end
2060 * end
2061 *
2062 * class Single
2063 * def Single.four() end
2064 * end
2065 *
2066 * a = Single.new
2067 *
2068 * def a.one()
2069 * end
2070 *
2071 * class << a
2072 * include Other
2073 * def two()
2074 * end
2075 * end
2076 *
2077 * Single.singleton_methods #=> [:four]
2078 * a.singleton_methods(false) #=> [:two, :one]
2079 * a.singleton_methods #=> [:two, :one, :three]
2080 */
2081
2082 VALUE
2083rb_obj_singleton_methods(int argc, const VALUE *argv, VALUE obj)
2084{
2085 VALUE ary, klass, origin;
2086 struct method_entry_arg me_arg;
2087 struct rb_id_table *mtbl;
2088 int recur = TRUE;
2089
2090 if (rb_check_arity(argc, 0, 1)) recur = RTEST(argv[0]);
2091 if (RB_TYPE_P(obj, T_CLASS) && FL_TEST(obj, FL_SINGLETON)) {
2092 rb_singleton_class(obj);
2093 }
2094 klass = CLASS_OF(obj);
2095 origin = RCLASS_ORIGIN(klass);
2096 me_arg.list = st_init_numtable();
2097 me_arg.recur = recur;
2098 if (klass && FL_TEST(klass, FL_SINGLETON)) {
2099 if ((mtbl = RCLASS_M_TBL(origin)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2100 klass = RCLASS_SUPER(klass);
2101 }
2102 if (recur) {
2103 while (klass && (FL_TEST(klass, FL_SINGLETON) || RB_TYPE_P(klass, T_ICLASS))) {
2104 if (klass != origin && (mtbl = RCLASS_M_TBL(klass)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2105 klass = RCLASS_SUPER(klass);
2106 }
2107 }
2108 ary = rb_ary_new2(me_arg.list->num_entries);
2109 st_foreach(me_arg.list, ins_methods_i, ary);
2110 st_free_table(me_arg.list);
2111
2112 return ary;
2113}
2114
2123#ifdef rb_define_method_id
2124#undef rb_define_method_id
2125#endif
2126void
2127 rb_define_method_id(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc)
2128{
2129 rb_add_method_cfunc(klass, mid, func, argc, METHOD_VISI_PUBLIC);
2130}
2131
2132#ifdef rb_define_method
2133#undef rb_define_method
2134#endif
2135void
2136 rb_define_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
2137{
2138 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PUBLIC);
2139}
2140
2141#ifdef rb_define_protected_method
2142#undef rb_define_protected_method
2143#endif
2144void
2145 rb_define_protected_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
2146{
2147 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PROTECTED);
2148}
2149
2150#ifdef rb_define_private_method
2151#undef rb_define_private_method
2152#endif
2153void
2154 rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
2155{
2156 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PRIVATE);
2157}
2158
2159void
2160rb_undef_method(VALUE klass, const char *name)
2161{
2162 rb_add_method(klass, rb_intern(name), VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2163}
2164
2165static enum rb_id_table_iterator_result
2166undef_method_i(ID name, VALUE value, void *data)
2167{
2168 VALUE klass = (VALUE)data;
2169 rb_add_method(klass, name, VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2170 return ID_TABLE_CONTINUE;
2171}
2172
2173void
2174rb_undef_methods_from(VALUE klass, VALUE super)
2175{
2176 struct rb_id_table *mtbl = RCLASS_M_TBL(super);
2177 if (mtbl) {
2178 rb_id_table_foreach(mtbl, undef_method_i, (void *)klass);
2179 }
2180}
2181
2190static inline VALUE
2191special_singleton_class_of(VALUE obj)
2192{
2193 switch (obj) {
2194 case Qnil: return rb_cNilClass;
2195 case Qfalse: return rb_cFalseClass;
2196 case Qtrue: return rb_cTrueClass;
2197 default: return Qnil;
2198 }
2199}
2200
2201 VALUE
2202rb_special_singleton_class(VALUE obj)
2203{
2204 return special_singleton_class_of(obj);
2205}
2206
2216static VALUE
2217singleton_class_of(VALUE obj)
2218{
2219 VALUE klass;
2220
2221 switch (TYPE(obj)) {
2222 case T_FIXNUM:
2223 case T_BIGNUM:
2224 case T_FLOAT:
2225 case T_SYMBOL:
2226 rb_raise(rb_eTypeError, "can't define singleton");
2227
2228 case T_FALSE:
2229 case T_TRUE:
2230 case T_NIL:
2231 klass = special_singleton_class_of(obj);
2232 if (NIL_P(klass))
2233 rb_bug("unknown immediate %p", (void *)obj);
2234 return klass;
2235
2236 case T_STRING:
2237 if (FL_TEST_RAW(obj, RSTRING_FSTR)) {
2238 rb_raise(rb_eTypeError, "can't define singleton");
2239 }
2240 }
2241
2242 klass = METACLASS_OF(obj);
2243 if (!(FL_TEST(klass, FL_SINGLETON) &&
2244 RCLASS_ATTACHED_OBJECT(klass) == obj)) {
2245 klass = rb_make_metaclass(obj, klass);
2246 }
2247
2248 RB_FL_SET_RAW(klass, RB_OBJ_FROZEN_RAW(obj));
2249
2250 return klass;
2251}
2252
2253void
2254rb_freeze_singleton_class(VALUE x)
2255{
2256 /* should not propagate to meta-meta-class, and so on */
2257 if (!(RBASIC(x)->flags & FL_SINGLETON)) {
2258 VALUE klass = RBASIC_CLASS(x);
2259 if (klass && // no class when hidden from ObjectSpace
2260 FL_TEST(klass, (FL_SINGLETON|FL_FREEZE)) == FL_SINGLETON) {
2261 OBJ_FREEZE_RAW(klass);
2262 }
2263 }
2264}
2265
2273 VALUE
2274rb_singleton_class_get(VALUE obj)
2275{
2276 VALUE klass;
2277
2278 if (SPECIAL_CONST_P(obj)) {
2279 return rb_special_singleton_class(obj);
2280 }
2281 klass = METACLASS_OF(obj);
2282 if (!FL_TEST(klass, FL_SINGLETON)) return Qnil;
2283 if (RCLASS_ATTACHED_OBJECT(klass) != obj) return Qnil;
2284 return klass;
2285}
2286
2287 VALUE
2288rb_singleton_class(VALUE obj)
2289{
2290 VALUE klass = singleton_class_of(obj);
2291
2292 /* ensures an exposed class belongs to its own eigenclass */
2293 if (RB_TYPE_P(obj, T_CLASS)) (void)ENSURE_EIGENCLASS(klass);
2294
2295 return klass;
2296}
2297
2307#ifdef rb_define_singleton_method
2308#undef rb_define_singleton_method
2309#endif
2310void
2311 rb_define_singleton_method(VALUE obj, const char *name, VALUE (*func)(ANYARGS), int argc)
2312{
2313 rb_define_method(singleton_class_of(obj), name, func, argc);
2314}
2315
2316#ifdef rb_define_module_function
2317#undef rb_define_module_function
2318#endif
2319void
2320 rb_define_module_function(VALUE module, const char *name, VALUE (*func)(ANYARGS), int argc)
2321{
2322 rb_define_private_method(module, name, func, argc);
2323 rb_define_singleton_method(module, name, func, argc);
2324}
2325
2326#ifdef rb_define_global_function
2327#undef rb_define_global_function
2328#endif
2329void
2330 rb_define_global_function(const char *name, VALUE (*func)(ANYARGS), int argc)
2331{
2332 rb_define_module_function(rb_mKernel, name, func, argc);
2333}
2334
2335void
2336rb_define_alias(VALUE klass, const char *name1, const char *name2)
2337{
2338 rb_alias(klass, rb_intern(name1), rb_intern(name2));
2339}
2340
2341void
2342rb_define_attr(VALUE klass, const char *name, int read, int write)
2343{
2344 rb_attr(klass, rb_intern(name), read, write, FALSE);
2345}
2346
2347 VALUE
2348rb_keyword_error_new(const char *error, VALUE keys)
2349{
2350 long i = 0, len = RARRAY_LEN(keys);
2351 VALUE error_message = rb_sprintf("%s keyword%.*s", error, len > 1, "s");
2352
2353 if (len > 0) {
2354 rb_str_cat_cstr(error_message, ": ");
2355 while (1) {
2356 const VALUE k = RARRAY_AREF(keys, i);
2357 rb_str_append(error_message, rb_inspect(k));
2358 if (++i >= len) break;
2359 rb_str_cat_cstr(error_message, ", ");
2360 }
2361 }
2362
2363 return rb_exc_new_str(rb_eArgError, error_message);
2364}
2365
2366NORETURN(static void rb_keyword_error(const char *error, VALUE keys));
2367static void
2368rb_keyword_error(const char *error, VALUE keys)
2369{
2370 rb_exc_raise(rb_keyword_error_new(error, keys));
2371}
2372
2373NORETURN(static void unknown_keyword_error(VALUE hash, const ID *table, int keywords));
2374static void
2375unknown_keyword_error(VALUE hash, const ID *table, int keywords)
2376{
2377 int i;
2378 for (i = 0; i < keywords; i++) {
2379 st_data_t key = ID2SYM(table[i]);
2380 rb_hash_stlike_delete(hash, &key, NULL);
2381 }
2382 rb_keyword_error("unknown", rb_hash_keys(hash));
2383}
2384
2385
2386static int
2387separate_symbol(st_data_t key, st_data_t value, st_data_t arg)
2388{
2389 VALUE *kwdhash = (VALUE *)arg;
2390 if (!SYMBOL_P(key)) kwdhash++;
2391 if (!*kwdhash) *kwdhash = rb_hash_new();
2392 rb_hash_aset(*kwdhash, (VALUE)key, (VALUE)value);
2393 return ST_CONTINUE;
2394}
2395
2396 VALUE
2397rb_extract_keywords(VALUE *orighash)
2398{
2399 VALUE parthash[2] = {0, 0};
2400 VALUE hash = *orighash;
2401
2402 if (RHASH_EMPTY_P(hash)) {
2403 *orighash = 0;
2404 return hash;
2405 }
2406 rb_hash_foreach(hash, separate_symbol, (st_data_t)&parthash);
2407 *orighash = parthash[1];
2408 if (parthash[1] && RBASIC_CLASS(hash) != rb_cHash) {
2409 RBASIC_SET_CLASS(parthash[1], RBASIC_CLASS(hash));
2410 }
2411 return parthash[0];
2412}
2413
2414int
2415rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
2416{
2417 int i = 0, j;
2418 int rest = 0;
2419 VALUE missing = Qnil;
2420 st_data_t key;
2421
2422#define extract_kwarg(keyword, val) \
2423 (key = (st_data_t)(keyword), values ? \
2424 (rb_hash_stlike_delete(keyword_hash, &key, &(val)) || ((val) = Qundef, 0)) : \
2425 rb_hash_stlike_lookup(keyword_hash, key, NULL))
2426
2427 if (NIL_P(keyword_hash)) keyword_hash = 0;
2428
2429 if (optional < 0) {
2430 rest = 1;
2431 optional = -1-optional;
2432 }
2433 if (required) {
2434 for (; i < required; i++) {
2435 VALUE keyword = ID2SYM(table[i]);
2436 if (keyword_hash) {
2437 if (extract_kwarg(keyword, values[i])) {
2438 continue;
2439 }
2440 }
2441 if (NIL_P(missing)) missing = rb_ary_hidden_new(1);
2442 rb_ary_push(missing, keyword);
2443 }
2444 if (!NIL_P(missing)) {
2445 rb_keyword_error("missing", missing);
2446 }
2447 }
2448 j = i;
2449 if (optional && keyword_hash) {
2450 for (i = 0; i < optional; i++) {
2451 if (extract_kwarg(ID2SYM(table[required+i]), values[required+i])) {
2452 j++;
2453 }
2454 }
2455 }
2456 if (!rest && keyword_hash) {
2457 if (RHASH_SIZE(keyword_hash) > (unsigned int)(values ? 0 : j)) {
2458 unknown_keyword_error(keyword_hash, table, required+optional);
2459 }
2460 }
2461 if (values && !keyword_hash) {
2462 for (i = 0; i < required + optional; i++) {
2463 values[i] = Qundef;
2464 }
2465 }
2466 return j;
2467#undef extract_kwarg
2468}
2469
2470 struct rb_scan_args_t {
2471 int kw_flag;
2472 int n_lead;
2473 int n_opt;
2474 int n_trail;
2475 bool f_var;
2476 bool f_hash;
2477 bool f_block;
2478};
2479
2480static void
2481rb_scan_args_parse(int kw_flag, const char *fmt, struct rb_scan_args_t *arg)
2482{
2483 const char *p = fmt;
2484
2485 memset(arg, 0, sizeof(*arg));
2486 arg->kw_flag = kw_flag;
2487
2488 if (ISDIGIT(*p)) {
2489 arg->n_lead = *p - '0';
2490 p++;
2491 if (ISDIGIT(*p)) {
2492 arg->n_opt = *p - '0';
2493 p++;
2494 }
2495 }
2496 if (*p == '*') {
2497 arg->f_var = 1;
2498 p++;
2499 }
2500 if (ISDIGIT(*p)) {
2501 arg->n_trail = *p - '0';
2502 p++;
2503 }
2504 if (*p == ':') {
2505 arg->f_hash = 1;
2506 p++;
2507 }
2508 if (*p == '&') {
2509 arg->f_block = 1;
2510 p++;
2511 }
2512 if (*p != '\0') {
2513 rb_fatal("bad scan arg format: %s", fmt);
2514 }
2515}
2516
2517static int
2518rb_scan_args_assign(const struct rb_scan_args_t *arg, int argc, const VALUE *const argv, va_list vargs)
2519{
2520 int i, argi = 0;
2521 VALUE *var, hash = Qnil;
2522#define rb_scan_args_next_param() va_arg(vargs, VALUE *)
2523 const int kw_flag = arg->kw_flag;
2524 const int n_lead = arg->n_lead;
2525 const int n_opt = arg->n_opt;
2526 const int n_trail = arg->n_trail;
2527 const int n_mand = n_lead + n_trail;
2528 const bool f_var = arg->f_var;
2529 const bool f_hash = arg->f_hash;
2530 const bool f_block = arg->f_block;
2531
2532 /* capture an option hash - phase 1: pop from the argv */
2533 if (f_hash && argc > 0) {
2534 VALUE last = argv[argc - 1];
2535 if (rb_scan_args_keyword_p(kw_flag, last)) {
2536 hash = rb_hash_dup(last);
2537 argc--;
2538 }
2539 }
2540
2541 if (argc < n_mand) {
2542 goto argc_error;
2543 }
2544
2545 /* capture leading mandatory arguments */
2546 for (i = 0; i < n_lead; i++) {
2547 var = rb_scan_args_next_param();
2548 if (var) *var = argv[argi];
2549 argi++;
2550 }
2551 /* capture optional arguments */
2552 for (i = 0; i < n_opt; i++) {
2553 var = rb_scan_args_next_param();
2554 if (argi < argc - n_trail) {
2555 if (var) *var = argv[argi];
2556 argi++;
2557 }
2558 else {
2559 if (var) *var = Qnil;
2560 }
2561 }
2562 /* capture variable length arguments */
2563 if (f_var) {
2564 int n_var = argc - argi - n_trail;
2565
2566 var = rb_scan_args_next_param();
2567 if (0 < n_var) {
2568 if (var) *var = rb_ary_new_from_values(n_var, &argv[argi]);
2569 argi += n_var;
2570 }
2571 else {
2572 if (var) *var = rb_ary_new();
2573 }
2574 }
2575 /* capture trailing mandatory arguments */
2576 for (i = 0; i < n_trail; i++) {
2577 var = rb_scan_args_next_param();
2578 if (var) *var = argv[argi];
2579 argi++;
2580 }
2581 /* capture an option hash - phase 2: assignment */
2582 if (f_hash) {
2583 var = rb_scan_args_next_param();
2584 if (var) *var = hash;
2585 }
2586 /* capture iterator block */
2587 if (f_block) {
2588 var = rb_scan_args_next_param();
2589 if (rb_block_given_p()) {
2590 *var = rb_block_proc();
2591 }
2592 else {
2593 *var = Qnil;
2594 }
2595 }
2596
2597 if (argi == argc) {
2598 return argc;
2599 }
2600
2601 argc_error:
2602 return -(argc + 1);
2603#undef rb_scan_args_next_param
2604}
2605
2606static int
2607rb_scan_args_result(const struct rb_scan_args_t *const arg, int argc)
2608{
2609 const int n_lead = arg->n_lead;
2610 const int n_opt = arg->n_opt;
2611 const int n_trail = arg->n_trail;
2612 const int n_mand = n_lead + n_trail;
2613 const bool f_var = arg->f_var;
2614
2615 if (argc >= 0) {
2616 return argc;
2617 }
2618
2619 argc = -argc - 1;
2620 rb_error_arity(argc, n_mand, f_var ? UNLIMITED_ARGUMENTS : n_mand + n_opt);
2621 UNREACHABLE_RETURN(-1);
2622}
2623
2624#undef rb_scan_args
2625int
2626rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...)
2627{
2628 va_list vargs;
2629 struct rb_scan_args_t arg;
2630 rb_scan_args_parse(RB_SCAN_ARGS_PASS_CALLED_KEYWORDS, fmt, &arg);
2631 va_start(vargs,fmt);
2632 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
2633 va_end(vargs);
2634 return rb_scan_args_result(&arg, argc);
2635}
2636
2637#undef rb_scan_args_kw
2638int
2639rb_scan_args_kw(int kw_flag, int argc, const VALUE *argv, const char *fmt, ...)
2640{
2641 va_list vargs;
2642 struct rb_scan_args_t arg;
2643 rb_scan_args_parse(kw_flag, fmt, &arg);
2644 va_start(vargs,fmt);
2645 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
2646 va_end(vargs);
2647 return rb_scan_args_result(&arg, argc);
2648}
2649
RUBY_ASSERT
#define RUBY_ASSERT(expr)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:177
rb_define_method
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
Definition cxxanyargs.hpp:670
rb_define_method_id
#define rb_define_method_id(klass, mid, func, arity)
Defines klass#mid.
Definition cxxanyargs.hpp:673
rb_define_singleton_method
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
Definition cxxanyargs.hpp:685
rb_define_protected_method
#define rb_define_protected_method(klass, mid, func, arity)
Defines klass#mid and makes it protected.
Definition cxxanyargs.hpp:681
rb_define_module_function
#define rb_define_module_function(klass, mid, func, arity)
Defines klass#mid and makes it a module function.
Definition cxxanyargs.hpp:689
rb_define_private_method
#define rb_define_private_method(klass, mid, func, arity)
Defines klass#mid and makes it private.
Definition cxxanyargs.hpp:677
rb_define_global_function
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
Definition cxxanyargs.hpp:695
RUBY_EXTERN
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition dllexport.h:45
RB_OBJ_FROZEN_RAW
static VALUE RB_OBJ_FROZEN_RAW(VALUE obj)
This is an implementation detail of RB_OBJ_FROZEN().
Definition fl_type.h:883
RB_FL_SET_RAW
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
@ RUBY_FL_USER1
User-defined flag.
Definition fl_type.h:329
rb_class_protected_instance_methods
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:1906
rb_include_module
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
Definition class.c:1177
rb_refinement_new
VALUE rb_refinement_new(void)
Creates a new, anonymous refinement.
Definition class.c:1072
rb_define_class
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition class.c:970
rb_class_new
VALUE rb_class_new(VALUE super)
Creates a new, anonymous class.
Definition class.c:350
make_singleton_class
static VALUE make_singleton_class(VALUE obj)
Creates a singleton class for obj.
Definition class.c:792
rb_singleton_class_clone
VALUE rb_singleton_class_clone(VALUE obj)
Clones a singleton class.
Definition class.c:636
rb_prepend_module
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:1432
rb_class_subclasses
VALUE rb_class_subclasses(VALUE klass)
Queries the class's direct descendants.
Definition class.c:1686
rb_singleton_class
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
Definition class.c:2288
rb_define_class_under
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition class.c:1002
rb_class_attached_object
VALUE rb_class_attached_object(VALUE klass)
Returns the attached object for a singleton class.
Definition class.c:1709
rb_obj_singleton_methods
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:2083
rb_module_new
VALUE rb_module_new(void)
Creates a new, anonymous module.
Definition class.c:1066
META_CLASS_OF_CLASS_CLASS_P
#define META_CLASS_OF_CLASS_CLASS_P(k)
whether k is a meta^(n)-class of Class class
Definition class.c:712
rb_class_instance_methods
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:1891
rb_check_inheritable
void rb_check_inheritable(VALUE super)
Asserts that the given class can derive a child class.
Definition class.c:335
rb_class_public_instance_methods
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:1944
rb_class_boot
VALUE rb_class_boot(VALUE super)
A utility function that wraps class_alloc.
Definition class.c:272
rb_define_module
VALUE rb_define_module(const char *name)
Defines a top-level module.
Definition class.c:1085
rb_class_modify_check
void rb_class_modify_check(VALUE klass)
Asserts that klass is not a frozen class.
Definition eval.c:429
rb_define_module_id_under
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
rb_singleton_class_attached
void rb_singleton_class_attached(VALUE klass, VALUE obj)
Attaches a singleton class to its corresponding object.
Definition class.c:700
rb_freeze_singleton_class
void rb_freeze_singleton_class(VALUE x)
This is an implementation detail of RB_OBJ_FREEZE().
Definition class.c:2254
rb_mod_included_modules
VALUE rb_mod_included_modules(VALUE mod)
Queries the list of included modules.
Definition class.c:1504
rb_define_class_id_under
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
rb_mod_ancestors
VALUE rb_mod_ancestors(VALUE mod)
Queries the module's ancestors.
Definition class.c:1572
make_metaclass
static VALUE make_metaclass(VALUE klass)
Creates a metaclass of klass.
Definition class.c:757
class_alloc
static VALUE class_alloc(VALUE flags, VALUE klass)
Allocates a struct RClass for a new class.
Definition class.c:230
rb_class_inherited
VALUE rb_class_inherited(VALUE super, VALUE klass)
Calls Class::inherited.
Definition class.c:961
rb_mod_include_p
VALUE rb_mod_include_p(VALUE mod, VALUE mod2)
Queries if the passed module is included by the module.
Definition class.c:1540
rb_class_private_instance_methods
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:1929
ENSURE_EIGENCLASS
#define ENSURE_EIGENCLASS(klass)
ensures klass belongs to its own eigenclass.
Definition class.c:743
rb_mod_init_copy
VALUE rb_mod_init_copy(VALUE clone, VALUE orig)
The comment that comes with this function says :nodoc:.
Definition class.c:524
rb_define_module_under
VALUE rb_define_module_under(VALUE outer, const char *name)
Defines a module under the namespace of outer.
Definition class.c:1109
rb_singleton_class_get
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:2274
rb_define_module_id
VALUE rb_define_module_id(ID id)
This is a very badly designed API that creates an anonymous module.
Definition class.c:1079
rb_define_class_id
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
rb_define_alias
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition class.c:2336
rb_extract_keywords
VALUE rb_extract_keywords(VALUE *orighash)
Splits a hash into two.
Definition class.c:2397
rb_define_attr
void rb_define_attr(VALUE klass, const char *name, int read, int write)
Defines public accessor method(s) for an attribute.
Definition class.c:2342
rb_undef_method
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
Definition class.c:2160
rb_scan_args_kw
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:2639
rb_scan_args
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:2626
rb_block_given_p
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition eval.c:866
rb_get_kwargs
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
Definition class.c:2415
TYPE
#define TYPE(_)
Old name of rb_type.
Definition value_type.h:107
NEWOBJ_OF
#define NEWOBJ_OF
Old name of RB_NEWOBJ_OF.
Definition newobj.h:61
FL_SINGLETON
#define FL_SINGLETON
Old name of RUBY_FL_SINGLETON.
Definition fl_type.h:58
FL_UNSET_RAW
#define FL_UNSET_RAW
Old name of RB_FL_UNSET_RAW.
Definition fl_type.h:134
OBJ_INIT_COPY
#define OBJ_INIT_COPY(obj, orig)
Old name of RB_OBJ_INIT_COPY.
Definition object.h:41
ALLOC
#define ALLOC
Old name of RB_ALLOC.
Definition memory.h:394
T_STRING
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
xfree
#define xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
T_MASK
#define T_MASK
Old name of RUBY_T_MASK.
Definition value_type.h:68
Qundef
#define Qundef
Old name of RUBY_Qundef.
Definition special_consts.h:62
T_NIL
#define T_NIL
Old name of RUBY_T_NIL.
Definition value_type.h:72
T_FLOAT
#define T_FLOAT
Old name of RUBY_T_FLOAT.
Definition value_type.h:64
ID2SYM
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
T_BIGNUM
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
Definition value_type.h:57
SPECIAL_CONST_P
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
Definition special_consts.h:56
OBJ_FREEZE_RAW
#define OBJ_FREEZE_RAW
Old name of RB_OBJ_FREEZE_RAW.
Definition fl_type.h:136
T_FIXNUM
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
Definition value_type.h:63
UNREACHABLE_RETURN
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
Definition assume.h:29
ZALLOC
#define ZALLOC
Old name of RB_ZALLOC.
Definition memory.h:396
CLASS_OF
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:203
xmalloc
#define xmalloc
Old name of ruby_xmalloc.
Definition xmalloc.h:53
T_MODULE
#define T_MODULE
Old name of RUBY_T_MODULE.
Definition value_type.h:70
ISDIGIT
#define ISDIGIT
Old name of rb_isdigit.
Definition ctype.h:93
T_TRUE
#define T_TRUE
Old name of RUBY_T_TRUE.
Definition value_type.h:81
T_ICLASS
#define T_ICLASS
Old name of RUBY_T_ICLASS.
Definition value_type.h:66
FL_TEST_RAW
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:132
FL_SET
#define FL_SET
Old name of RB_FL_SET.
Definition fl_type.h:129
T_FALSE
#define T_FALSE
Old name of RUBY_T_FALSE.
Definition value_type.h:61
Qtrue
#define Qtrue
Old name of RUBY_Qtrue.
Definition special_consts.h:61
Qnil
#define Qnil
Old name of RUBY_Qnil.
Definition special_consts.h:60
Qfalse
#define Qfalse
Old name of RUBY_Qfalse.
Definition special_consts.h:59
NIL_P
#define NIL_P
Old name of RB_NIL_P.
Definition special_consts.h:55
FL_WB_PROTECTED
#define FL_WB_PROTECTED
Old name of RUBY_FL_WB_PROTECTED.
Definition fl_type.h:59
T_SYMBOL
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
Definition value_type.h:80
T_CLASS
#define T_CLASS
Old name of RUBY_T_CLASS.
Definition value_type.h:58
BUILTIN_TYPE
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition value_type.h:85
FL_TEST
#define FL_TEST
Old name of RB_FL_TEST.
Definition fl_type.h:131
FL_FREEZE
#define FL_FREEZE
Old name of RUBY_FL_FREEZE.
Definition fl_type.h:67
CONST_ID
#define CONST_ID
Old name of RUBY_CONST_ID.
Definition symbol.h:47
rb_ary_new2
#define rb_ary_new2
Old name of rb_ary_new_capa.
Definition array.h:651
FL_SET_RAW
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
Definition fl_type.h:130
SYMBOL_P
#define SYMBOL_P
Old name of RB_SYMBOL_P.
Definition value_type.h:88
rb_eTypeError
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1344
rb_exc_new_str
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
rb_cClass
VALUE rb_cClass
Class class.
Definition object.c:66
rb_mKernel
VALUE rb_mKernel
Kernel module.
Definition object.c:63
rb_cRefinement
VALUE rb_cRefinement
Refinement class.
Definition object.c:67
rb_cNilClass
VALUE rb_cNilClass
NilClass class.
Definition object.c:69
rb_cHash
VALUE rb_cHash
Hash class.
Definition hash.c:110
rb_cFalseClass
VALUE rb_cFalseClass
FalseClass class.
Definition object.c:71
rb_obj_class
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:215
rb_inspect
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
Definition object.c:645
rb_cBasicObject
VALUE rb_cBasicObject
BasicObject class.
Definition object.c:62
rb_cModule
VALUE rb_cModule
Module class.
Definition object.c:65
rb_class_real
VALUE rb_class_real(VALUE klass)
Finds a "real" class.
Definition object.c:205
rb_cTrueClass
VALUE rb_cTrueClass
TrueClass class.
Definition object.c:70
RB_OBJ_WRITTEN
#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
RB_OBJ_WRITE
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:619
rb_funcall
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1121
RGENGC_WB_PROTECTED_CLASS
#define RGENGC_WB_PROTECTED_CLASS
This is a compile-time flag to enable/disable write barrier for struct RClass.
Definition gc.h:539
UNLIMITED_ARGUMENTS
#define UNLIMITED_ARGUMENTS
This macro is used in conjunction with rb_check_arity().
Definition error.h:35
rb_check_arity
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
rb_block_proc
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
Definition proc.c:807
rb_str_append
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:3409
rb_str_cat_cstr
#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
rb_const_get
VALUE rb_const_get(VALUE space, ID name)
Identical to rb_const_defined(), except it returns the actual defined value.
Definition variable.c:3141
rb_const_set
void rb_const_set(VALUE space, ID name, VALUE val)
Names a constant.
Definition variable.c:3596
rb_const_get_at
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
rb_set_class_path_string
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
rb_const_defined_at
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
rb_class_path
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
Definition variable.c:283
rb_const_defined
int rb_const_defined(VALUE space, ID name)
Queries if the constant is defined at the namespace.
Definition variable.c:3449
rb_alias
void rb_alias(VALUE klass, ID dst, ID src)
Resembles alias.
Definition vm_method.c:2272
rb_attr
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
rb_clear_constant_cache_for_id
void rb_clear_constant_cache_for_id(ID id)
Clears the inline constant caches associated with a particular ID.
Definition vm_method.c:141
rb_intern_const
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
Definition symbol.h:276
len
int len
Length of the buffer.
Definition io.h:8
MEMCPY
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
Definition memory.h:366
ruby::backward::cxxanyargs::type
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition cxxanyargs.hpp:56
ruby::backward::cxxanyargs::rb_hash_foreach
void rb_hash_foreach(VALUE q, int_type *w, VALUE e)
Iteration over the given hash.
Definition cxxanyargs.hpp:482
RARRAY_LEN
#define RARRAY_LEN
Just another name of rb_array_len
Definition rarray.h:51
RARRAY_AREF
#define RARRAY_AREF(a, i)
Definition rarray.h:403
RBASIC_CLASS
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
Definition rbasic.h:152
RBASIC
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
RCLASS_SUPER
#define RCLASS_SUPER
Just another name of rb_class_get_superclass
Definition rclass.h:44
RHASH_SIZE
#define RHASH_SIZE(h)
Queries the size of the hash.
Definition rhash.h:69
RHASH_EMPTY_P
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
Definition rhash.h:79
RB_SCAN_ARGS_PASS_CALLED_KEYWORDS
#define RB_SCAN_ARGS_PASS_CALLED_KEYWORDS
Same behaviour as rb_scan_args().
Definition scan_args.h:50
RTEST
#define RTEST
This is an old name of RB_TEST.
Definition special_consts.h:51
ANYARGS
#define ANYARGS
Functions declared using this macro take arbitrary arguments, including void.
Definition stdarg.h:64
RClass
Definition class.h:80
clone_const_arg
Definition class.c:395
clone_method_arg
Definition class.c:382
cvc_table_copy_ctx
Definition class.c:432
method_entry_arg
Definition class.c:1771
rb_classext_struct
Definition class.h:43
rb_const_entry_struct
Definition constant.h:33
rb_cref_struct
CREF (Class REFerence)
Definition method.h:44
rb_cvar_class_tbl_entry
Definition class.h:36
rb_id_table
Definition id_table.c:41
rb_method_entry_struct
Definition method.h:54
rb_method_iseq_struct::cref
rb_cref_t * cref
class reference, should be marked
Definition method.h:136
rb_method_iseq_struct::iseqptr
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
Definition method.h:135
rb_scan_args_t
Definition class.c:2470
rb_subclass_entry
Internal header for Class.
Definition class.h:29
st_table
Definition st.h:79
subclass_traverse_data
Definition class.c:1594
ID
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
Check_Type
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