Ruby 3.5.0dev (2025-06-26 revision 4b1de7378d038109d85b5ab8b817de13c1217a3f)
class.c (4b1de7378d038109d85b5ab8b817de13c1217a3f)
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/namespace.h"
28#include "internal/object.h"
29#include "internal/string.h"
30#include "internal/variable.h"
31#include "ruby/st.h"
32#include "vm_core.h"
33#include "yjit.h"
34
35/* Flags of T_CLASS
36 *
37 * 0: RCLASS_IS_ROOT
38 * The class has been added to the VM roots. Will always be marked and pinned.
39 * This is done for classes defined from C to allow storing them in global variables.
40 * 1: RUBY_FL_SINGLETON
41 * This class is a singleton class.
42 * 2: RCLASS_PRIME_CLASSEXT_PRIME_WRITABLE
43 * This class's prime classext is the only classext and writable from any namespaces.
44 * If unset, the prime classext is writable only from the root namespace.
45 * 3: RCLASS_IS_INITIALIZED
46 * Class has been initialized.
47 * 4: RCLASS_NAMESPACEABLE
48 * Is a builtin class that may be namespaced. It larger than a normal class.
49 */
50
51/* Flags of T_ICLASS
52 *
53 * 2: RCLASS_PRIME_CLASSEXT_PRIME_WRITABLE
54 * This module's prime classext is the only classext and writable from any namespaces.
55 * If unset, the prime classext is writable only from the root namespace.
56 * 4: RCLASS_NAMESPACEABLE
57 * Is a builtin class that may be namespaced. It larger than a normal class.
58 */
59
60/* Flags of T_MODULE
61 *
62 * 0: RCLASS_IS_ROOT
63 * The class has been added to the VM roots. Will always be marked and pinned.
64 * This is done for classes defined from C to allow storing them in global variables.
65 * 1: RMODULE_IS_REFINEMENT
66 * Module is used for refinements.
67 * 2: RCLASS_PRIME_CLASSEXT_PRIME_WRITABLE
68 * This module's prime classext is the only classext and writable from any namespaces.
69 * If unset, the prime classext is writable only from the root namespace.
70 * 3: RCLASS_IS_INITIALIZED
71 * Module has been initialized.
72 * 4: RCLASS_NAMESPACEABLE
73 * Is a builtin class that may be namespaced. It larger than a normal class.
74 */
75
76#define METACLASS_OF(k) RBASIC(k)->klass
77#define SET_METACLASS_OF(k, cls) RBASIC_SET_CLASS(k, cls)
78
79RUBY_EXTERN rb_serial_t ruby_vm_global_cvar_state;
80
82 struct rb_id_table *tbl;
83 VALUE klass;
84};
85
86static enum rb_id_table_iterator_result
87duplicate_classext_id_table_i(ID key, VALUE value, void *data)
88{
89 struct rb_id_table *tbl = (struct rb_id_table *)data;
90 rb_id_table_insert(tbl, key, value);
91 return ID_TABLE_CONTINUE;
92}
93
94static enum rb_id_table_iterator_result
95duplicate_classext_m_tbl_i(ID key, VALUE value, void *data)
96{
97 struct duplicate_id_tbl_data *arg = (struct duplicate_id_tbl_data *)data;
99 rb_method_table_insert0(arg->klass, arg->tbl, key, me, false);
100 return ID_TABLE_CONTINUE;
101}
102
103static struct rb_id_table *
104duplicate_classext_m_tbl(struct rb_id_table *orig, VALUE klass, bool init_missing)
105{
106 struct rb_id_table *tbl;
107 if (!orig) {
108 if (init_missing)
109 return rb_id_table_create(0);
110 else
111 return NULL;
112 }
113 tbl = rb_id_table_create(rb_id_table_size(orig));
114 struct duplicate_id_tbl_data data = {
115 .tbl = tbl,
116 .klass = klass,
117 };
118 rb_id_table_foreach(orig, duplicate_classext_m_tbl_i, &data);
119 return tbl;
120}
121
122static struct rb_id_table *
123duplicate_classext_id_table(struct rb_id_table *orig, bool init_missing)
124{
125 struct rb_id_table *tbl;
126
127 if (!orig) {
128 if (init_missing)
129 return rb_id_table_create(0);
130 else
131 return NULL;
132 }
133 tbl = rb_id_table_create(rb_id_table_size(orig));
134 rb_id_table_foreach(orig, duplicate_classext_id_table_i, tbl);
135 return tbl;
136}
137
138static rb_const_entry_t *
139duplicate_classext_const_entry(rb_const_entry_t *src, VALUE klass)
140{
141 // See also: setup_const_entry (variable.c)
143
144 dst->flag = src->flag;
145 dst->line = src->line;
146 RB_OBJ_WRITE(klass, &dst->value, src->value);
147 RB_OBJ_WRITE(klass, &dst->file, src->file);
148
149 return dst;
150}
151
152static enum rb_id_table_iterator_result
153duplicate_classext_const_tbl_i(ID key, VALUE value, void *data)
154{
155 struct duplicate_id_tbl_data *arg = (struct duplicate_id_tbl_data *)data;
156 rb_const_entry_t *entry = duplicate_classext_const_entry((rb_const_entry_t *)value, arg->klass);
157
158 rb_id_table_insert(arg->tbl, key, (VALUE)entry);
159
160 return ID_TABLE_CONTINUE;
161}
162
163static struct rb_id_table *
164duplicate_classext_const_tbl(struct rb_id_table *src, VALUE klass)
165{
166 struct rb_id_table *dst;
167
168 if (!src)
169 return NULL;
170
171 dst = rb_id_table_create(rb_id_table_size(src));
172
173 struct duplicate_id_tbl_data data = {
174 .tbl = dst,
175 .klass = klass,
176 };
177 rb_id_table_foreach(src, duplicate_classext_const_tbl_i, (void *)&data);
178
179 return dst;
180}
181
182static VALUE
183namespace_subclasses_tbl_key(const rb_namespace_t *ns)
184{
185 if (!ns){
186 return 0;
187 }
188 return (VALUE)ns->ns_id;
189}
190
191static void
192duplicate_classext_subclasses(rb_classext_t *orig, rb_classext_t *copy)
193{
194 rb_subclass_anchor_t *anchor, *orig_anchor;
195 rb_subclass_entry_t *head, *cur, *cdr, *entry, *first = NULL;
196 rb_ns_subclasses_t *ns_subclasses;
197 struct st_table *tbl;
198
199 if (RCLASSEXT_SUBCLASSES(orig)) {
200 orig_anchor = RCLASSEXT_SUBCLASSES(orig);
201 ns_subclasses = orig_anchor->ns_subclasses;
202 tbl = ((rb_ns_subclasses_t *)ns_subclasses)->tbl;
203
205 anchor->ns_subclasses = rb_ns_subclasses_ref_inc(ns_subclasses);
206
208 anchor->head = head;
209
210 RCLASSEXT_SUBCLASSES(copy) = anchor;
211
212 cur = head;
213 entry = orig_anchor->head;
214 RUBY_ASSERT(!entry->klass);
215 // The head entry has NULL klass always. See rb_class_foreach_subclass().
216 entry = entry->next;
217 while (entry) {
218 if (rb_objspace_garbage_object_p(entry->klass)) {
219 entry = entry->next;
220 continue;
221 }
223 cdr->klass = entry->klass;
224 cdr->prev = cur;
225 cur->next = cdr;
226 if (!first) {
227 VALUE ns_id = namespace_subclasses_tbl_key(RCLASSEXT_NS(copy));
228 first = cdr;
229 st_insert(tbl, ns_id, (st_data_t)first);
230 }
231 cur = cdr;
232 entry = entry->next;
233 }
234 }
235
236 if (RCLASSEXT_NS_SUPER_SUBCLASSES(orig))
237 RCLASSEXT_NS_SUPER_SUBCLASSES(copy) = rb_ns_subclasses_ref_inc(RCLASSEXT_NS_SUPER_SUBCLASSES(orig));
238 if (RCLASSEXT_NS_MODULE_SUBCLASSES(orig))
239 RCLASSEXT_NS_MODULE_SUBCLASSES(copy) = rb_ns_subclasses_ref_inc(RCLASSEXT_NS_MODULE_SUBCLASSES(orig));
240}
241
242static void
243class_duplicate_iclass_classext(VALUE iclass, rb_classext_t *mod_ext, const rb_namespace_t *ns)
244{
246
247 rb_classext_t *src = RCLASS_EXT_PRIME(iclass);
248 rb_classext_t *ext = RCLASS_EXT_TABLE_LOOKUP_INTERNAL(iclass, ns);
249 int first_set = 0;
250
251 if (ext) {
252 // iclass classext for the ns is only for cc/callable_m_tbl if it's created earlier than module's one
253 rb_invalidate_method_caches(RCLASSEXT_CALLABLE_M_TBL(ext), RCLASSEXT_CC_TBL(ext));
254 }
255
256 ext = ZALLOC(rb_classext_t);
257
258 RCLASSEXT_NS(ext) = ns;
259
260 RCLASSEXT_SUPER(ext) = RCLASSEXT_SUPER(src);
261
262 // See also: rb_include_class_new()
263 if (RCLASSEXT_ICLASS_IS_ORIGIN(src) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(src)) {
264 RCLASSEXT_M_TBL(ext) = duplicate_classext_m_tbl(RCLASSEXT_M_TBL(src), iclass, true);
265 }
266 else {
267 RCLASSEXT_M_TBL(ext) = RCLASSEXT_M_TBL(mod_ext);
268 }
269
270 RCLASSEXT_CONST_TBL(ext) = RCLASSEXT_CONST_TBL(mod_ext);
271 RCLASSEXT_CVC_TBL(ext) = RCLASSEXT_CVC_TBL(mod_ext);
272
273 // Those are cache and should be recreated when methods are called
274 // RCLASSEXT_CALLABLE_M_TBL(ext) = NULL;
275 // RCLASSEXT_CC_TBL(ext) = NULL;
276
277 // subclasses, namespace_super_subclasses_tbl, namespace_module_subclasses_tbl
278 duplicate_classext_subclasses(src, ext);
279
280 RCLASSEXT_SET_ORIGIN(ext, iclass, RCLASSEXT_ORIGIN(src));
281 RCLASSEXT_ICLASS_IS_ORIGIN(ext) = RCLASSEXT_ICLASS_IS_ORIGIN(src);
282 RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext) = RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(src);
283
284 RCLASSEXT_SET_INCLUDER(ext, iclass, RCLASSEXT_INCLUDER(src));
285
286 first_set = RCLASS_SET_NAMESPACE_CLASSEXT(iclass, ns, ext);
287 if (first_set) {
288 RCLASS_SET_PRIME_CLASSEXT_WRITABLE(iclass, false);
289 }
290}
291
293rb_class_duplicate_classext(rb_classext_t *orig, VALUE klass, const rb_namespace_t *ns)
294{
295 VM_ASSERT(RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_MODULE) || RB_TYPE_P(klass, T_ICLASS));
296
298 bool dup_iclass = RB_TYPE_P(klass, T_MODULE) ? true : false;
299
300 RCLASSEXT_NS(ext) = ns;
301
302 RCLASSEXT_SUPER(ext) = RCLASSEXT_SUPER(orig);
303
304 RCLASSEXT_M_TBL(ext) = duplicate_classext_m_tbl(RCLASSEXT_M_TBL(orig), klass, dup_iclass);
305
306 if (orig->fields_obj) {
307 RB_OBJ_WRITE(klass, &ext->fields_obj, rb_imemo_fields_clone(orig->fields_obj));
308 }
309
310 if (RCLASSEXT_SHARED_CONST_TBL(orig)) {
311 RCLASSEXT_CONST_TBL(ext) = RCLASSEXT_CONST_TBL(orig);
312 RCLASSEXT_SHARED_CONST_TBL(ext) = true;
313 }
314 else {
315 RCLASSEXT_CONST_TBL(ext) = duplicate_classext_const_tbl(RCLASSEXT_CONST_TBL(orig), klass);
316 RCLASSEXT_SHARED_CONST_TBL(ext) = false;
317 }
318 /*
319 * callable_m_tbl is for `super` chain, and entries will be created when the super chain is called.
320 * so initially, it can be NULL and let it be created lazily.
321 * RCLASSEXT_CALLABLE_M_TBL(ext) = NULL;
322 *
323 * cc_tbl is for method inline cache, and method calls from different namespaces never occur on
324 * the same code, so the copied classext should have a different cc_tbl from the prime one.
325 * RCLASSEXT_CC_TBL(copy) = NULL
326 */
327
328 RCLASSEXT_CVC_TBL(ext) = duplicate_classext_id_table(RCLASSEXT_CVC_TBL(orig), dup_iclass);
329
330 // subclasses, subclasses_index
331 duplicate_classext_subclasses(orig, ext);
332
333 RCLASSEXT_SET_ORIGIN(ext, klass, RCLASSEXT_ORIGIN(orig));
334 /*
335 * Members not copied to namespace classext values
336 * * refined_class
337 * * as.class.allocator / as.singleton_class.attached_object
338 * * includer
339 * * max IV count
340 * * variation count
341 */
342 RCLASSEXT_PERMANENT_CLASSPATH(ext) = RCLASSEXT_PERMANENT_CLASSPATH(orig);
343 RCLASSEXT_CLONED(ext) = RCLASSEXT_CLONED(orig);
344 RCLASSEXT_CLASSPATH(ext) = RCLASSEXT_CLASSPATH(orig);
345
346 /* For the usual T_CLASS/T_MODULE, iclass flags are always false */
347
348 if (dup_iclass) {
349 VALUE iclass;
350 /*
351 * ICLASS has the same m_tbl/const_tbl/cvc_tbl with the included module.
352 * So the module's classext is copied, its tables should be also referred
353 * by the ICLASS's classext for the namespace.
354 */
355 rb_subclass_anchor_t *anchor = RCLASSEXT_SUBCLASSES(ext);
356 rb_subclass_entry_t *subclass_entry = anchor->head;
357 while (subclass_entry) {
358 if (subclass_entry->klass && RB_TYPE_P(subclass_entry->klass, T_ICLASS)) {
359 iclass = subclass_entry->klass;
360 if (RBASIC_CLASS(iclass) == klass) {
361 // Is the subclass an ICLASS including this module into another class
362 // If so we need to re-associate it under our namespace with the new ext
363 class_duplicate_iclass_classext(iclass, ext, ns);
364 }
365 }
366 subclass_entry = subclass_entry->next;
367 }
368 }
369
370 return ext;
371}
372
373void
374rb_class_ensure_writable(VALUE klass)
375{
376 VM_ASSERT(RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_MODULE) || RB_TYPE_P(klass, T_ICLASS));
377 RCLASS_EXT_WRITABLE(klass);
378}
379
381 rb_class_classext_foreach_callback_func *func;
382 void * callback_arg;
383};
384
385static int
386class_classext_foreach_i(st_data_t key, st_data_t value, st_data_t arg)
387{
389 rb_class_classext_foreach_callback_func *func = foreach_arg->func;
390 func((rb_classext_t *)value, false, (VALUE)key, foreach_arg->callback_arg);
391 return ST_CONTINUE;
392}
393
394void
395rb_class_classext_foreach(VALUE klass, rb_class_classext_foreach_callback_func *func, void *arg)
396{
397 st_table *tbl = RCLASS_CLASSEXT_TBL(klass);
399 if (tbl) {
400 foreach_arg.func = func;
401 foreach_arg.callback_arg = arg;
402 rb_st_foreach(tbl, class_classext_foreach_i, (st_data_t)&foreach_arg);
403 }
404 func(RCLASS_EXT_PRIME(klass), true, (VALUE)NULL, arg);
405}
406
407VALUE
408rb_class_super_of(VALUE klass)
409{
410 return RCLASS_SUPER(klass);
411}
412
413VALUE
414rb_class_singleton_p(VALUE klass)
415{
416 return RCLASS_SINGLETON_P(klass);
417}
418
419unsigned char
420rb_class_variation_count(VALUE klass)
421{
422 return RCLASS_VARIATION_COUNT(klass);
423}
424
425static void
426push_subclass_entry_to_list(VALUE super, VALUE klass, bool is_module)
427{
428 rb_subclass_entry_t *entry, *head;
429 rb_subclass_anchor_t *anchor;
430 rb_ns_subclasses_t *ns_subclasses;
431 struct st_table *tbl;
432 const rb_namespace_t *ns = rb_current_namespace();
433
435 entry->klass = klass;
436
437 RB_VM_LOCKING() {
438 anchor = RCLASS_WRITABLE_SUBCLASSES(super);
439 VM_ASSERT(anchor);
440 ns_subclasses = (rb_ns_subclasses_t *)anchor->ns_subclasses;
441 VM_ASSERT(ns_subclasses);
442 tbl = ns_subclasses->tbl;
443 VM_ASSERT(tbl);
444
445 head = anchor->head;
446 if (head->next) {
447 head->next->prev = entry;
448 entry->next = head->next;
449 }
450 head->next = entry;
451 entry->prev = head;
452 st_insert(tbl, namespace_subclasses_tbl_key(ns), (st_data_t)entry);
453 }
454
455 if (is_module) {
456 RCLASS_WRITE_NS_MODULE_SUBCLASSES(klass, anchor->ns_subclasses);
457 }
458 else {
459 RCLASS_WRITE_NS_SUPER_SUBCLASSES(klass, anchor->ns_subclasses);
460 }
461}
462
463void
464rb_class_subclass_add(VALUE super, VALUE klass)
465{
466 if (super && !UNDEF_P(super)) {
467 push_subclass_entry_to_list(super, klass, false);
468 }
469}
470
471static void
472rb_module_add_to_subclasses_list(VALUE module, VALUE iclass)
473{
474 if (module && !UNDEF_P(module)) {
475 push_subclass_entry_to_list(module, iclass, true);
476 }
477}
478
479void
480rb_class_remove_subclass_head(VALUE klass) // TODO: check this is still used and required
481{
482 rb_classext_t *ext = RCLASS_EXT_WRITABLE(klass);
483 rb_class_classext_free_subclasses(ext, klass);
484}
485
486static struct rb_subclass_entry *
487class_get_subclasses_for_ns(struct st_table *tbl, VALUE ns_id)
488{
489 st_data_t value;
490 if (st_lookup(tbl, (st_data_t)ns_id, &value)) {
491 return (struct rb_subclass_entry *)value;
492 }
493 return NULL;
494}
495
496static void
497remove_class_from_subclasses(struct st_table *tbl, VALUE ns_id, VALUE klass)
498{
499 rb_subclass_entry_t *entry = class_get_subclasses_for_ns(tbl, ns_id);
500 bool first_entry = true;
501 while (entry) {
502 if (entry->klass == klass) {
503 rb_subclass_entry_t *prev = entry->prev, *next = entry->next;
504
505 if (prev) {
506 prev->next = next;
507 }
508 if (next) {
509 next->prev = prev;
510 }
511
512 xfree(entry);
513
514 if (first_entry) {
515 if (next) {
516 st_insert(tbl, ns_id, (st_data_t)next);
517 }
518 else {
519 // no subclass entries in this ns
520 st_delete(tbl, &ns_id, NULL);
521 }
522 }
523 break;
524 }
525 else if (first_entry) {
526 first_entry = false;
527 }
528 entry = entry->next;
529 }
530}
531
532void
533rb_class_remove_from_super_subclasses(VALUE klass)
534{
535 rb_classext_t *ext = RCLASS_EXT_WRITABLE(klass);
536 rb_ns_subclasses_t *ns_subclasses = RCLASSEXT_NS_SUPER_SUBCLASSES(ext);
537
538 if (!ns_subclasses) return;
539 remove_class_from_subclasses(ns_subclasses->tbl, namespace_subclasses_tbl_key(RCLASSEXT_NS(ext)), klass);
540 rb_ns_subclasses_ref_dec(ns_subclasses);
541 RCLASSEXT_NS_SUPER_SUBCLASSES(ext) = 0;
542}
543
544void
545rb_class_remove_from_module_subclasses(VALUE klass)
546{
547 rb_classext_t *ext = RCLASS_EXT_WRITABLE(klass);
548 rb_ns_subclasses_t *ns_subclasses = RCLASSEXT_NS_MODULE_SUBCLASSES(ext);
549
550 if (!ns_subclasses) return;
551 remove_class_from_subclasses(ns_subclasses->tbl, namespace_subclasses_tbl_key(RCLASSEXT_NS(ext)), klass);
552 rb_ns_subclasses_ref_dec(ns_subclasses);
553 RCLASSEXT_NS_MODULE_SUBCLASSES(ext) = 0;
554}
555
556void
557rb_class_classext_free_subclasses(rb_classext_t *ext, VALUE klass)
558{
559 rb_subclass_anchor_t *anchor = RCLASSEXT_SUBCLASSES(ext);
560 struct st_table *tbl = anchor->ns_subclasses->tbl;
561 VALUE ns_id = namespace_subclasses_tbl_key(RCLASSEXT_NS(ext));
562 rb_subclass_entry_t *next, *entry = anchor->head;
563
564 while (entry) {
565 next = entry->next;
566 xfree(entry);
567 entry = next;
568 }
569 VM_ASSERT(
570 rb_ns_subclasses_ref_count(anchor->ns_subclasses) > 0,
571 "ns_subclasses refcount (%p) %ld", anchor->ns_subclasses, rb_ns_subclasses_ref_count(anchor->ns_subclasses));
572 st_delete(tbl, &ns_id, NULL);
573 rb_ns_subclasses_ref_dec(anchor->ns_subclasses);
574 xfree(anchor);
575
576 if (RCLASSEXT_NS_SUPER_SUBCLASSES(ext)) {
577 rb_ns_subclasses_t *ns_sub = RCLASSEXT_NS_SUPER_SUBCLASSES(ext);
578 remove_class_from_subclasses(ns_sub->tbl, ns_id, klass);
579 rb_ns_subclasses_ref_dec(ns_sub);
580 }
581 if (RCLASSEXT_NS_MODULE_SUBCLASSES(ext)) {
582 rb_ns_subclasses_t *ns_sub = RCLASSEXT_NS_MODULE_SUBCLASSES(ext);
583 remove_class_from_subclasses(ns_sub->tbl, ns_id, klass);
584 rb_ns_subclasses_ref_dec(ns_sub);
585 }
586}
587
588void
589rb_class_foreach_subclass(VALUE klass, void (*f)(VALUE, VALUE), VALUE arg)
590{
592 rb_subclass_entry_t *cur = RCLASS_SUBCLASSES_FIRST(klass);
593 /* do not be tempted to simplify this loop into a for loop, the order of
594 operations is important here if `f` modifies the linked list */
595 while (cur) {
596 VALUE curklass = cur->klass;
597 tmp = cur->next;
598 // do not trigger GC during f, otherwise the cur will become
599 // a dangling pointer if the subclass is collected
600 f(curklass, arg);
601 cur = tmp;
602 }
603}
604
605static void
606class_detach_subclasses(VALUE klass, VALUE arg)
607{
608 rb_class_remove_from_super_subclasses(klass);
609}
610
611void
612rb_class_detach_subclasses(VALUE klass)
613{
614 rb_class_foreach_subclass(klass, class_detach_subclasses, Qnil);
615}
616
617static void
618class_detach_module_subclasses(VALUE klass, VALUE arg)
619{
620 rb_class_remove_from_module_subclasses(klass);
621}
622
623void
624rb_class_detach_module_subclasses(VALUE klass)
625{
626 rb_class_foreach_subclass(klass, class_detach_module_subclasses, Qnil);
627}
628
629static void
630class_switch_superclass(VALUE super, VALUE klass)
631{
632 class_detach_subclasses(klass, Qnil);
633 rb_class_subclass_add(super, klass);
634}
635
646static VALUE
647class_alloc0(enum ruby_value_type type, VALUE klass, bool namespaceable)
648{
649 rb_ns_subclasses_t *ns_subclasses;
650 rb_subclass_anchor_t *anchor;
651 const rb_namespace_t *ns = rb_definition_namespace();
652
653 if (!ruby_namespace_init_done) {
654 namespaceable = true;
655 }
656
657 size_t alloc_size = sizeof(struct RClass_and_rb_classext_t);
658 if (namespaceable) {
659 alloc_size = sizeof(struct RClass_namespaceable);
660 }
661
662 // class_alloc is supposed to return a new object that is not promoted yet.
663 // So, we need to avoid GC after NEWOBJ_OF.
664 // To achieve that, we allocate subclass lists before NEWOBJ_OF.
665 //
666 // TODO: Note that this could cause memory leak.
667 // If NEWOBJ_OF fails with out of memory, these buffers will leak.
668 ns_subclasses = ZALLOC(rb_ns_subclasses_t);
669 ns_subclasses->refcount = 1;
670 ns_subclasses->tbl = st_init_numtable();
672 anchor->ns_subclasses = ns_subclasses;
673 anchor->head = ZALLOC(rb_subclass_entry_t);
674
676
677 VALUE flags = type;
679 if (namespaceable) flags |= RCLASS_NAMESPACEABLE;
680
681 NEWOBJ_OF(obj, struct RClass, klass, flags, alloc_size, 0);
682
683 memset(RCLASS_EXT_PRIME(obj), 0, sizeof(rb_classext_t));
684
685 /* ZALLOC
686 RCLASS_CONST_TBL(obj) = 0;
687 RCLASS_M_TBL(obj) = 0;
688 RCLASS_FIELDS(obj) = 0;
689 RCLASS_SET_SUPER((VALUE)obj, 0);
690 */
691
692 RCLASS_PRIME_NS((VALUE)obj) = ns;
693 // Classes/Modules defined in user namespaces are
694 // writable directly because it exists only in a namespace.
695 RCLASS_SET_PRIME_CLASSEXT_WRITABLE((VALUE)obj, !namespaceable || NAMESPACE_USER_P(ns));
696
697 RCLASS_SET_ORIGIN((VALUE)obj, (VALUE)obj);
698 RCLASS_SET_REFINED_CLASS((VALUE)obj, Qnil);
699
700 RCLASS_SET_SUBCLASSES((VALUE)obj, anchor);
701
702 return (VALUE)obj;
703}
704
705static VALUE
706class_alloc(enum ruby_value_type type, VALUE klass)
707{
708 return class_alloc0(type, klass, false);
709}
710
711static VALUE
712class_associate_super(VALUE klass, VALUE super, bool init)
713{
714 if (super && !UNDEF_P(super)) {
715 class_switch_superclass(super, klass);
716 }
717 if (init) {
718 RCLASS_SET_SUPER(klass, super);
719 }
720 else {
721 RCLASS_WRITE_SUPER(klass, super);
722 }
723 rb_class_update_superclasses(klass);
724 return super;
725}
726
727VALUE
728rb_class_set_super(VALUE klass, VALUE super)
729{
730 return class_associate_super(klass, super, false);
731}
732
733static void
734class_initialize_method_table(VALUE c)
735{
736 // initialize the prime classext m_tbl
737 RCLASS_SET_M_TBL_EVEN_WHEN_PROMOTED(c, rb_id_table_create(0));
738}
739
740static void
741class_clear_method_table(VALUE c)
742{
743 RCLASS_WRITE_M_TBL_EVEN_WHEN_PROMOTED(c, rb_id_table_create(0));
744}
745
746static VALUE
747class_boot_namespaceable(VALUE super, bool namespaceable)
748{
749 VALUE klass = class_alloc0(T_CLASS, rb_cClass, namespaceable);
750
751 // initialize method table prior to class_associate_super()
752 // because class_associate_super() may cause GC and promote klass
753 class_initialize_method_table(klass);
754
755 class_associate_super(klass, super, true);
756 if (super && !UNDEF_P(super)) {
757 rb_class_set_initialized(klass);
758 }
759
760 return (VALUE)klass;
761}
762
772VALUE
774{
775 return class_boot_namespaceable(super, false);
776}
777
778static VALUE *
779class_superclasses_including_self(VALUE klass)
780{
781 if (RCLASS_SUPERCLASSES_WITH_SELF_P(klass))
782 return RCLASS_SUPERCLASSES(klass);
783
784 size_t depth = RCLASS_SUPERCLASS_DEPTH(klass);
785 VALUE *superclasses = xmalloc(sizeof(VALUE) * (depth + 1));
786 if (depth > 0)
787 memcpy(superclasses, RCLASS_SUPERCLASSES(klass), sizeof(VALUE) * depth);
788 superclasses[depth] = klass;
789
790 return superclasses;
791}
792
793void
794rb_class_update_superclasses(VALUE klass)
795{
796 VALUE *superclasses;
797 size_t super_depth;
798 VALUE super = RCLASS_SUPER(klass);
799
800 if (!RB_TYPE_P(klass, T_CLASS)) return;
801 if (UNDEF_P(super)) return;
802
803 // If the superclass array is already built
804 if (RCLASS_SUPERCLASSES(klass))
805 return;
806
807 // find the proper superclass
808 while (super != Qfalse && !RB_TYPE_P(super, T_CLASS)) {
809 super = RCLASS_SUPER(super);
810 }
811
812 // For BasicObject and uninitialized classes, depth=0 and ary=NULL
813 if (super == Qfalse)
814 return;
815
816 // Sometimes superclasses are set before the full ancestry tree is built
817 // This happens during metaclass construction
818 if (super != rb_cBasicObject && !RCLASS_SUPERCLASS_DEPTH(super)) {
819 rb_class_update_superclasses(super);
820
821 // If it is still unset we need to try later
822 if (!RCLASS_SUPERCLASS_DEPTH(super))
823 return;
824 }
825
826 super_depth = RCLASS_SUPERCLASS_DEPTH(super);
827 if (RCLASS_SUPERCLASSES_WITH_SELF_P(super)) {
828 superclasses = RCLASS_SUPERCLASSES(super);
829 }
830 else {
831 superclasses = class_superclasses_including_self(super);
832 RCLASS_WRITE_SUPERCLASSES(super, super_depth, superclasses, true);
833 }
834
835 size_t depth = super_depth == RCLASS_MAX_SUPERCLASS_DEPTH ? super_depth : super_depth + 1;
836 RCLASS_WRITE_SUPERCLASSES(klass, depth, superclasses, false);
837}
838
839void
841{
842 if (!RB_TYPE_P(super, T_CLASS)) {
843 rb_raise(rb_eTypeError, "superclass must be an instance of Class (given an instance of %"PRIsVALUE")",
844 rb_obj_class(super));
845 }
846 if (RCLASS_SINGLETON_P(super)) {
847 rb_raise(rb_eTypeError, "can't make subclass of singleton class");
848 }
849 if (super == rb_cClass) {
850 rb_raise(rb_eTypeError, "can't make subclass of Class");
851 }
852}
853
854VALUE
856{
857 Check_Type(super, T_CLASS);
859 VALUE klass = rb_class_boot(super);
860
861 if (super != rb_cObject && super != rb_cBasicObject) {
862 RCLASS_SET_MAX_IV_COUNT(klass, RCLASS_MAX_IV_COUNT(super));
863 }
864
865 RUBY_ASSERT(getenv("RUBY_NAMESPACE") || RCLASS_PRIME_CLASSEXT_WRITABLE_P(klass));
866
867 return klass;
868}
869
870VALUE
871rb_class_s_alloc(VALUE klass)
872{
873 return rb_class_boot(0);
874}
875
876static void
877clone_method(VALUE old_klass, VALUE new_klass, ID mid, const rb_method_entry_t *me)
878{
879 if (me->def->type == VM_METHOD_TYPE_ISEQ) {
880 rb_cref_t *new_cref = rb_vm_rewrite_cref(me->def->body.iseq.cref, old_klass, new_klass);
881 rb_add_method_iseq(new_klass, mid, me->def->body.iseq.iseqptr, new_cref, METHOD_ENTRY_VISI(me));
882 }
883 else {
884 rb_method_entry_set(new_klass, mid, me, METHOD_ENTRY_VISI(me));
885 }
886}
887
889 VALUE new_klass;
890 VALUE old_klass;
891};
892
893static enum rb_id_table_iterator_result
894clone_method_i(ID key, VALUE value, void *data)
895{
896 const struct clone_method_arg *arg = (struct clone_method_arg *)data;
897 clone_method(arg->old_klass, arg->new_klass, key, (const rb_method_entry_t *)value);
898 return ID_TABLE_CONTINUE;
899}
900
902 VALUE klass;
903 struct rb_id_table *tbl;
904};
905
906static int
907clone_const(ID key, const rb_const_entry_t *ce, struct clone_const_arg *arg)
908{
910 MEMCPY(nce, ce, rb_const_entry_t, 1);
911 RB_OBJ_WRITTEN(arg->klass, Qundef, ce->value);
912 RB_OBJ_WRITTEN(arg->klass, Qundef, ce->file);
913
914 rb_id_table_insert(arg->tbl, key, (VALUE)nce);
915 return ID_TABLE_CONTINUE;
916}
917
918static enum rb_id_table_iterator_result
919clone_const_i(ID key, VALUE value, void *data)
920{
921 return clone_const(key, (const rb_const_entry_t *)value, data);
922}
923
924static void
925class_init_copy_check(VALUE clone, VALUE orig)
926{
927 if (orig == rb_cBasicObject) {
928 rb_raise(rb_eTypeError, "can't copy the root class");
929 }
930 if (RCLASS_INITIALIZED_P(clone)) {
931 rb_raise(rb_eTypeError, "already initialized class");
932 }
933 if (RCLASS_SINGLETON_P(orig)) {
934 rb_raise(rb_eTypeError, "can't copy singleton class");
935 }
936}
937
939 VALUE clone;
940 struct rb_id_table * new_table;
941};
942
943static enum rb_id_table_iterator_result
944cvc_table_copy(ID id, VALUE val, void *data)
945{
946 struct cvc_table_copy_ctx *ctx = (struct cvc_table_copy_ctx *)data;
947 struct rb_cvar_class_tbl_entry * orig_entry;
948 orig_entry = (struct rb_cvar_class_tbl_entry *)val;
949
950 struct rb_cvar_class_tbl_entry *ent;
951
952 ent = ALLOC(struct rb_cvar_class_tbl_entry);
953 ent->class_value = ctx->clone;
954 ent->cref = orig_entry->cref;
955 ent->global_cvar_state = orig_entry->global_cvar_state;
956 rb_id_table_insert(ctx->new_table, id, (VALUE)ent);
957
958 RB_OBJ_WRITTEN(ctx->clone, Qundef, ent->cref);
959
960 return ID_TABLE_CONTINUE;
961}
962
963static void
964copy_tables(VALUE clone, VALUE orig)
965{
966 if (RCLASS_CONST_TBL(clone)) {
967 rb_free_const_table(RCLASS_CONST_TBL(clone));
968 RCLASS_WRITE_CONST_TBL(clone, 0, false);
969 }
970 if (RCLASS_CVC_TBL(orig)) {
971 struct rb_id_table *rb_cvc_tbl = RCLASS_CVC_TBL(orig);
972 struct rb_id_table *rb_cvc_tbl_dup = rb_id_table_create(rb_id_table_size(rb_cvc_tbl));
973
974 struct cvc_table_copy_ctx ctx;
975 ctx.clone = clone;
976 ctx.new_table = rb_cvc_tbl_dup;
977 rb_id_table_foreach(rb_cvc_tbl, cvc_table_copy, &ctx);
978 RCLASS_WRITE_CVC_TBL(clone, rb_cvc_tbl_dup);
979 }
980 rb_id_table_free(RCLASS_M_TBL(clone));
981 RCLASS_WRITE_M_TBL_EVEN_WHEN_PROMOTED(clone, 0);
982 if (!RB_TYPE_P(clone, T_ICLASS)) {
983 st_data_t id;
984
985 rb_fields_tbl_copy(clone, orig);
986 CONST_ID(id, "__tmp_classpath__");
987 rb_attr_delete(clone, id);
988 CONST_ID(id, "__classpath__");
989 rb_attr_delete(clone, id);
990 }
991 if (RCLASS_CONST_TBL(orig)) {
992 struct clone_const_arg arg;
993 struct rb_id_table *const_tbl;
994 arg.tbl = const_tbl = rb_id_table_create(0);
995 arg.klass = clone;
996 rb_id_table_foreach(RCLASS_CONST_TBL(orig), clone_const_i, &arg);
997 RCLASS_WRITE_CONST_TBL(clone, const_tbl, false);
998 }
999}
1000
1001static bool ensure_origin(VALUE klass);
1002
1003void
1004rb_class_set_initialized(VALUE klass)
1005{
1006 RUBY_ASSERT(RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_MODULE));
1007 FL_SET_RAW(klass, RCLASS_IS_INITIALIZED);
1008 /* no more re-initialization */
1009}
1010
1011void
1012rb_module_check_initializable(VALUE mod)
1013{
1014 if (RCLASS_INITIALIZED_P(mod)) {
1015 rb_raise(rb_eTypeError, "already initialized module");
1016 }
1017}
1018
1019/* :nodoc: */
1020VALUE
1022{
1023 /* Only class or module is valid here, but other classes may enter here and
1024 * only hit an exception on the OBJ_INIT_COPY checks
1025 */
1026 switch (BUILTIN_TYPE(clone)) {
1027 case T_CLASS:
1028 class_init_copy_check(clone, orig);
1029 break;
1030 case T_MODULE:
1031 rb_module_check_initializable(clone);
1032 break;
1033 default:
1034 break;
1035 }
1036 if (!OBJ_INIT_COPY(clone, orig)) return clone;
1037
1039 RUBY_ASSERT(BUILTIN_TYPE(clone) == BUILTIN_TYPE(orig));
1040
1041 rb_class_set_initialized(clone);
1042
1043 /* cloned flag is refer at constant inline cache
1044 * see vm_get_const_key_cref() in vm_insnhelper.c
1045 */
1046 RCLASS_SET_CLONED(clone, true);
1047 RCLASS_SET_CLONED(orig, true);
1048
1049 if (!RCLASS_SINGLETON_P(CLASS_OF(clone))) {
1050 RBASIC_SET_CLASS(clone, rb_singleton_class_clone(orig));
1051 rb_singleton_class_attached(METACLASS_OF(clone), (VALUE)clone);
1052 }
1053 if (BUILTIN_TYPE(clone) == T_CLASS) {
1054 RCLASS_SET_ALLOCATOR(clone, RCLASS_ALLOCATOR(orig));
1055 }
1056 copy_tables(clone, orig);
1057 if (RCLASS_M_TBL(orig)) {
1058 struct clone_method_arg arg;
1059 arg.old_klass = orig;
1060 arg.new_klass = clone;
1061 // TODO: use class_initialize_method_table() instead of RCLASS_SET_M_TBL_*
1062 // after RCLASS_SET_M_TBL is protected by write barrier
1063 RCLASS_SET_M_TBL_EVEN_WHEN_PROMOTED(clone, rb_id_table_create(0));
1064 rb_id_table_foreach(RCLASS_M_TBL(orig), clone_method_i, &arg);
1065 }
1066
1067 if (RCLASS_ORIGIN(orig) == orig) {
1068 rb_class_set_super(clone, RCLASS_SUPER(orig));
1069 }
1070 else {
1071 VALUE p = RCLASS_SUPER(orig);
1072 VALUE orig_origin = RCLASS_ORIGIN(orig);
1073 VALUE prev_clone_p = clone;
1074 VALUE origin_stack = rb_ary_hidden_new(2);
1075 VALUE origin[2];
1076 VALUE clone_p = 0;
1077 long origin_len;
1078 int add_subclass;
1079 VALUE clone_origin;
1080
1081 ensure_origin(clone);
1082 clone_origin = RCLASS_ORIGIN(clone);
1083
1084 while (p && p != orig_origin) {
1085 if (BUILTIN_TYPE(p) != T_ICLASS) {
1086 rb_bug("non iclass between module/class and origin");
1087 }
1088 clone_p = class_alloc(T_ICLASS, METACLASS_OF(p));
1089 /* We should set the m_tbl right after allocation before anything
1090 * that can trigger GC to avoid clone_p from becoming old and
1091 * needing to fire write barriers. */
1092 RCLASS_SET_M_TBL(clone_p, RCLASS_M_TBL(p));
1093 rb_class_set_super(prev_clone_p, clone_p);
1094 prev_clone_p = clone_p;
1095 RCLASS_SET_CONST_TBL(clone_p, RCLASS_CONST_TBL(p), false);
1096 if (RB_TYPE_P(clone, T_CLASS)) {
1097 RCLASS_SET_INCLUDER(clone_p, clone);
1098 }
1099 add_subclass = TRUE;
1100 if (p != RCLASS_ORIGIN(p)) {
1101 origin[0] = clone_p;
1102 origin[1] = RCLASS_ORIGIN(p);
1103 rb_ary_cat(origin_stack, origin, 2);
1104 }
1105 else if ((origin_len = RARRAY_LEN(origin_stack)) > 1 &&
1106 RARRAY_AREF(origin_stack, origin_len - 1) == p) {
1107 RCLASS_WRITE_ORIGIN(RARRAY_AREF(origin_stack, (origin_len -= 2)), clone_p);
1108 RICLASS_WRITE_ORIGIN_SHARED_MTBL(clone_p);
1109 rb_ary_resize(origin_stack, origin_len);
1110 add_subclass = FALSE;
1111 }
1112 if (add_subclass) {
1113 rb_module_add_to_subclasses_list(METACLASS_OF(p), clone_p);
1114 }
1115 p = RCLASS_SUPER(p);
1116 }
1117
1118 if (p == orig_origin) {
1119 if (clone_p) {
1120 rb_class_set_super(clone_p, clone_origin);
1121 rb_class_set_super(clone_origin, RCLASS_SUPER(orig_origin));
1122 }
1123 copy_tables(clone_origin, orig_origin);
1124 if (RCLASS_M_TBL(orig_origin)) {
1125 struct clone_method_arg arg;
1126 arg.old_klass = orig;
1127 arg.new_klass = clone;
1128 class_initialize_method_table(clone_origin);
1129 rb_id_table_foreach(RCLASS_M_TBL(orig_origin), clone_method_i, &arg);
1130 }
1131 }
1132 else {
1133 rb_bug("no origin for class that has origin");
1134 }
1135
1136 rb_class_update_superclasses(clone);
1137 }
1138
1139 return clone;
1140}
1141
1142VALUE
1144{
1145 return rb_singleton_class_clone_and_attach(obj, Qundef);
1146}
1147
1148// Clone and return the singleton class of `obj` if it has been created and is attached to `obj`.
1149VALUE
1150rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach)
1151{
1152 const VALUE klass = METACLASS_OF(obj);
1153
1154 // Note that `rb_singleton_class()` can create situations where `klass` is
1155 // attached to an object other than `obj`. In which case `obj` does not have
1156 // a material singleton class attached yet and there is no singleton class
1157 // to clone.
1158 if (!(RCLASS_SINGLETON_P(klass) && RCLASS_ATTACHED_OBJECT(klass) == obj)) {
1159 // nothing to clone
1160 return klass;
1161 }
1162 else {
1163 /* copy singleton(unnamed) class */
1164 bool klass_of_clone_is_new;
1165 RUBY_ASSERT(RB_TYPE_P(klass, T_CLASS));
1166 VALUE clone = class_alloc(T_CLASS, 0);
1167
1168 if (BUILTIN_TYPE(obj) == T_CLASS) {
1169 klass_of_clone_is_new = true;
1170 RBASIC_SET_CLASS(clone, clone);
1171 }
1172 else {
1173 VALUE klass_metaclass_clone = rb_singleton_class_clone(klass);
1174 // When `METACLASS_OF(klass) == klass_metaclass_clone`, it means the
1175 // recursive call did not clone `METACLASS_OF(klass)`.
1176 klass_of_clone_is_new = (METACLASS_OF(klass) != klass_metaclass_clone);
1177 RBASIC_SET_CLASS(clone, klass_metaclass_clone);
1178 }
1179
1180 // initialize method table before any GC chance
1181 class_initialize_method_table(clone);
1182
1183 rb_class_set_super(clone, RCLASS_SUPER(klass));
1184 rb_fields_tbl_copy(clone, klass);
1185 if (RCLASS_CONST_TBL(klass)) {
1186 struct clone_const_arg arg;
1187 struct rb_id_table *table;
1188 arg.tbl = table = rb_id_table_create(0);
1189 arg.klass = clone;
1190 rb_id_table_foreach(RCLASS_CONST_TBL(klass), clone_const_i, &arg);
1191 RCLASS_SET_CONST_TBL(clone, table, false);
1192 }
1193 if (!UNDEF_P(attach)) {
1194 rb_singleton_class_attached(clone, attach);
1195 }
1196 {
1197 struct clone_method_arg arg;
1198 arg.old_klass = klass;
1199 arg.new_klass = clone;
1200 rb_id_table_foreach(RCLASS_M_TBL(klass), clone_method_i, &arg);
1201 }
1202 if (klass_of_clone_is_new) {
1203 rb_singleton_class_attached(METACLASS_OF(clone), clone);
1204 }
1205 FL_SET(clone, FL_SINGLETON);
1206
1207 return clone;
1208 }
1209}
1210
1211void
1213{
1214 if (RCLASS_SINGLETON_P(klass)) {
1215 RCLASS_SET_ATTACHED_OBJECT(klass, obj);
1216 }
1217}
1218
1224#define META_CLASS_OF_CLASS_CLASS_P(k) (METACLASS_OF(k) == (k))
1225
1226static int
1227rb_singleton_class_has_metaclass_p(VALUE sklass)
1228{
1229 return RCLASS_ATTACHED_OBJECT(METACLASS_OF(sklass)) == sklass;
1230}
1231
1232int
1233rb_singleton_class_internal_p(VALUE sklass)
1234{
1235 return (RB_TYPE_P(RCLASS_ATTACHED_OBJECT(sklass), T_CLASS) &&
1236 !rb_singleton_class_has_metaclass_p(sklass));
1237}
1238
1244#define HAVE_METACLASS_P(k) \
1245 (FL_TEST(METACLASS_OF(k), FL_SINGLETON) && \
1246 rb_singleton_class_has_metaclass_p(k))
1247
1255#define ENSURE_EIGENCLASS(klass) \
1256 (HAVE_METACLASS_P(klass) ? METACLASS_OF(klass) : make_metaclass(klass))
1257
1258
1268static inline VALUE
1270{
1271 VALUE super;
1272 VALUE metaclass = class_boot_namespaceable(Qundef, FL_TEST_RAW(klass, RCLASS_NAMESPACEABLE));
1273
1274 FL_SET(metaclass, FL_SINGLETON);
1275 rb_singleton_class_attached(metaclass, klass);
1276
1277 if (META_CLASS_OF_CLASS_CLASS_P(klass)) {
1278 SET_METACLASS_OF(klass, metaclass);
1279 SET_METACLASS_OF(metaclass, metaclass);
1280 }
1281 else {
1282 VALUE tmp = METACLASS_OF(klass); /* for a meta^(n)-class klass, tmp is meta^(n)-class of Class class */
1283 SET_METACLASS_OF(klass, metaclass);
1284 SET_METACLASS_OF(metaclass, ENSURE_EIGENCLASS(tmp));
1285 }
1286
1287 super = RCLASS_SUPER(klass);
1288 while (RB_TYPE_P(super, T_ICLASS)) super = RCLASS_SUPER(super);
1289 class_associate_super(metaclass, super ? ENSURE_EIGENCLASS(super) : rb_cClass, true);
1290 rb_class_set_initialized(klass);
1291
1292 // Full class ancestry may not have been filled until we reach here.
1293 rb_class_update_superclasses(METACLASS_OF(metaclass));
1294
1295 return metaclass;
1296}
1297
1304static inline VALUE
1306{
1307 VALUE orig_class = METACLASS_OF(obj);
1308 VALUE klass = class_boot_namespaceable(orig_class, FL_TEST_RAW(orig_class, RCLASS_NAMESPACEABLE));
1309
1310 FL_SET(klass, FL_SINGLETON);
1311 RBASIC_SET_CLASS(obj, klass);
1312 rb_singleton_class_attached(klass, obj);
1313 rb_yjit_invalidate_no_singleton_class(orig_class);
1314
1315 SET_METACLASS_OF(klass, METACLASS_OF(rb_class_real(orig_class)));
1316 return klass;
1317}
1318
1319
1320static VALUE
1321boot_defclass(const char *name, VALUE super)
1322{
1323 VALUE obj = rb_class_boot(super);
1324 ID id = rb_intern(name);
1325
1326 rb_const_set((rb_cObject ? rb_cObject : obj), id, obj);
1327 rb_vm_register_global_object(obj);
1328 return obj;
1329}
1330
1331/***********************************************************************
1332 *
1333 * Document-class: Refinement
1334 *
1335 * Refinement is a class of the +self+ (current context) inside +refine+
1336 * statement. It allows to import methods from other modules, see #import_methods.
1337 */
1338
1339#if 0 /* for RDoc */
1340/*
1341 * Document-method: Refinement#import_methods
1342 *
1343 * call-seq:
1344 * import_methods(module, ...) -> self
1345 *
1346 * Imports methods from modules. Unlike Module#include,
1347 * Refinement#import_methods copies methods and adds them into the refinement,
1348 * so the refinement is activated in the imported methods.
1349 *
1350 * Note that due to method copying, only methods defined in Ruby code can be imported.
1351 *
1352 * module StrUtils
1353 * def indent(level)
1354 * ' ' * level + self
1355 * end
1356 * end
1357 *
1358 * module M
1359 * refine String do
1360 * import_methods StrUtils
1361 * end
1362 * end
1363 *
1364 * using M
1365 * "foo".indent(3)
1366 * #=> " foo"
1367 *
1368 * module M
1369 * refine String do
1370 * import_methods Enumerable
1371 * # Can't import method which is not defined with Ruby code: Enumerable#drop
1372 * end
1373 * end
1374 *
1375 */
1376
1377static VALUE
1378refinement_import_methods(int argc, VALUE *argv, VALUE refinement)
1379{
1380}
1381# endif
1382
1402void
1403Init_class_hierarchy(void)
1404{
1405 rb_cBasicObject = boot_defclass("BasicObject", 0);
1406 rb_cObject = boot_defclass("Object", rb_cBasicObject);
1407 rb_vm_register_global_object(rb_cObject);
1408
1409 /* resolve class name ASAP for order-independence */
1410 rb_set_class_path_string(rb_cObject, rb_cObject, rb_fstring_lit("Object"));
1411
1412 rb_cModule = boot_defclass("Module", rb_cObject);
1413 rb_cClass = boot_defclass("Class", rb_cModule);
1414 rb_cRefinement = boot_defclass("Refinement", rb_cModule);
1415
1416#if 0 /* for RDoc */
1417 // we pretend it to be public, otherwise RDoc will ignore it
1418 rb_define_method(rb_cRefinement, "import_methods", refinement_import_methods, -1);
1419#endif
1420
1421 rb_const_set(rb_cObject, rb_intern_const("BasicObject"), rb_cBasicObject);
1422 RBASIC_SET_CLASS(rb_cClass, rb_cClass);
1423 RBASIC_SET_CLASS(rb_cModule, rb_cClass);
1424 RBASIC_SET_CLASS(rb_cObject, rb_cClass);
1425 RBASIC_SET_CLASS(rb_cRefinement, rb_cClass);
1426 RBASIC_SET_CLASS(rb_cBasicObject, rb_cClass);
1427
1429}
1430
1431
1442VALUE
1443rb_make_metaclass(VALUE obj, VALUE unused)
1444{
1445 if (BUILTIN_TYPE(obj) == T_CLASS) {
1446 return make_metaclass(obj);
1447 }
1448 else {
1449 return make_singleton_class(obj);
1450 }
1451}
1452
1453VALUE
1455{
1456 VALUE klass;
1457
1458 if (!super) super = rb_cObject;
1459 klass = rb_class_new(super);
1460 rb_make_metaclass(klass, METACLASS_OF(super));
1461
1462 return klass;
1463}
1464
1465
1474VALUE
1476{
1477 ID inherited;
1478 if (!super) super = rb_cObject;
1479 CONST_ID(inherited, "inherited");
1480 return rb_funcall(super, inherited, 1, klass);
1481}
1482
1483VALUE
1484rb_define_class(const char *name, VALUE super)
1485{
1486 VALUE klass;
1487 ID id;
1488 const rb_namespace_t *ns = rb_current_namespace();
1489
1490 id = rb_intern(name);
1491 if (NAMESPACE_OPTIONAL_P(ns)) {
1492 return rb_define_class_id_under(ns->ns_object, id, super);
1493 }
1494 if (rb_const_defined(rb_cObject, id)) {
1495 klass = rb_const_get(rb_cObject, id);
1496 if (!RB_TYPE_P(klass, T_CLASS)) {
1497 rb_raise(rb_eTypeError, "%s is not a class (%"PRIsVALUE")",
1498 name, rb_obj_class(klass));
1499 }
1500 if (rb_class_real(RCLASS_SUPER(klass)) != super) {
1501 rb_raise(rb_eTypeError, "superclass mismatch for class %s", name);
1502 }
1503
1504 /* Class may have been defined in Ruby and not pin-rooted */
1505 rb_vm_register_global_object(klass);
1506 return klass;
1507 }
1508 if (!super) {
1509 rb_raise(rb_eArgError, "no super class for '%s'", name);
1510 }
1511 klass = rb_define_class_id(id, super);
1512 rb_vm_register_global_object(klass);
1513 rb_const_set(rb_cObject, id, klass);
1514 rb_class_inherited(super, klass);
1515
1516 return klass;
1517}
1518
1519VALUE
1520rb_define_class_under(VALUE outer, const char *name, VALUE super)
1521{
1522 return rb_define_class_id_under(outer, rb_intern(name), super);
1523}
1524
1525VALUE
1526rb_define_class_id_under_no_pin(VALUE outer, ID id, VALUE super)
1527{
1528 VALUE klass;
1529
1530 if (rb_const_defined_at(outer, id)) {
1531 klass = rb_const_get_at(outer, id);
1532 if (!RB_TYPE_P(klass, T_CLASS)) {
1533 rb_raise(rb_eTypeError, "%"PRIsVALUE"::%"PRIsVALUE" is not a class"
1534 " (%"PRIsVALUE")",
1535 outer, rb_id2str(id), rb_obj_class(klass));
1536 }
1537 if (rb_class_real(RCLASS_SUPER(klass)) != super) {
1538 rb_raise(rb_eTypeError, "superclass mismatch for class "
1539 "%"PRIsVALUE"::%"PRIsVALUE""
1540 " (%"PRIsVALUE" is given but was %"PRIsVALUE")",
1541 outer, rb_id2str(id), RCLASS_SUPER(klass), super);
1542 }
1543
1544 return klass;
1545 }
1546 if (!super) {
1547 rb_raise(rb_eArgError, "no super class for '%"PRIsVALUE"::%"PRIsVALUE"'",
1548 rb_class_path(outer), rb_id2str(id));
1549 }
1550 klass = rb_define_class_id(id, super);
1551 rb_set_class_path_string(klass, outer, rb_id2str(id));
1552 rb_const_set(outer, id, klass);
1553 rb_class_inherited(super, klass);
1554
1555 return klass;
1556}
1557
1558VALUE
1560{
1561 VALUE klass = rb_define_class_id_under_no_pin(outer, id, super);
1562 rb_vm_register_global_object(klass);
1563 return klass;
1564}
1565
1566VALUE
1567rb_module_s_alloc(VALUE klass)
1568{
1569 VALUE mod = class_alloc(T_MODULE, klass);
1570 class_initialize_method_table(mod);
1571 return mod;
1572}
1573
1574static inline VALUE
1575module_new(VALUE klass)
1576{
1577 VALUE mdl = class_alloc(T_MODULE, klass);
1578 class_initialize_method_table(mdl);
1579 return (VALUE)mdl;
1580}
1581
1582VALUE
1584{
1585 return module_new(rb_cModule);
1586}
1587
1588VALUE
1590{
1591 return module_new(rb_cRefinement);
1592}
1593
1594// Kept for compatibility. Use rb_module_new() instead.
1595VALUE
1597{
1598 return rb_module_new();
1599}
1600
1601VALUE
1602rb_define_module(const char *name)
1603{
1604 VALUE module;
1605 ID id;
1606 const rb_namespace_t *ns = rb_current_namespace();
1607
1608 id = rb_intern(name);
1609 if (NAMESPACE_OPTIONAL_P(ns)) {
1610 return rb_define_module_id_under(ns->ns_object, id);
1611 }
1612 if (rb_const_defined(rb_cObject, id)) {
1613 module = rb_const_get(rb_cObject, id);
1614 if (!RB_TYPE_P(module, T_MODULE)) {
1615 rb_raise(rb_eTypeError, "%s is not a module (%"PRIsVALUE")",
1616 name, rb_obj_class(module));
1617 }
1618 /* Module may have been defined in Ruby and not pin-rooted */
1619 rb_vm_register_global_object(module);
1620 return module;
1621 }
1622 module = rb_module_new();
1623 rb_vm_register_global_object(module);
1624 rb_const_set(rb_cObject, id, module);
1625
1626 return module;
1627}
1628
1629VALUE
1630rb_define_module_under(VALUE outer, const char *name)
1631{
1632 return rb_define_module_id_under(outer, rb_intern(name));
1633}
1634
1635VALUE
1637{
1638 VALUE module;
1639
1640 if (rb_const_defined_at(outer, id)) {
1641 module = rb_const_get_at(outer, id);
1642 if (!RB_TYPE_P(module, T_MODULE)) {
1643 rb_raise(rb_eTypeError, "%"PRIsVALUE"::%"PRIsVALUE" is not a module"
1644 " (%"PRIsVALUE")",
1645 outer, rb_id2str(id), rb_obj_class(module));
1646 }
1647 /* Module may have been defined in Ruby and not pin-rooted */
1648 rb_vm_register_global_object(module);
1649 return module;
1650 }
1651 module = rb_module_new();
1652 rb_const_set(outer, id, module);
1653 rb_set_class_path_string(module, outer, rb_id2str(id));
1654 rb_vm_register_global_object(module);
1655
1656 return module;
1657}
1658
1659VALUE
1660rb_include_class_new(VALUE module, VALUE super)
1661{
1662 VALUE klass = class_alloc(T_ICLASS, rb_cClass);
1663
1664 RCLASS_SET_M_TBL(klass, RCLASS_WRITABLE_M_TBL(module));
1665
1666 RCLASS_SET_ORIGIN(klass, klass);
1667 if (BUILTIN_TYPE(module) == T_ICLASS) {
1668 module = METACLASS_OF(module);
1669 }
1670 RUBY_ASSERT(!RB_TYPE_P(module, T_ICLASS));
1671 if (RCLASS_WRITABLE_CONST_TBL(module)) {
1672 RCLASS_SET_CONST_TBL(klass, RCLASS_WRITABLE_CONST_TBL(module), true);
1673 }
1674 else {
1675 RCLASS_WRITE_CONST_TBL(module, rb_id_table_create(0), false);
1676 RCLASS_SET_CONST_TBL(klass, RCLASS_WRITABLE_CONST_TBL(module), true);
1677 }
1678
1679 RCLASS_SET_CVC_TBL(klass, RCLASS_WRITABLE_CVC_TBL(module));
1680
1681 class_associate_super(klass, super, true);
1682 RBASIC_SET_CLASS(klass, module);
1683
1684 return (VALUE)klass;
1685}
1686
1687static int include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super);
1688
1689static void
1690ensure_includable(VALUE klass, VALUE module)
1691{
1692 rb_class_modify_check(klass);
1693 Check_Type(module, T_MODULE);
1694 rb_class_set_initialized(module);
1695 if (!NIL_P(rb_refinement_module_get_refined_class(module))) {
1696 rb_raise(rb_eArgError, "refinement module is not allowed");
1697 }
1698}
1699
1700void
1702{
1703 int changed = 0;
1704
1705 ensure_includable(klass, module);
1706
1707 changed = include_modules_at(klass, RCLASS_ORIGIN(klass), module, TRUE);
1708 if (changed < 0)
1709 rb_raise(rb_eArgError, "cyclic include detected");
1710
1711 if (RB_TYPE_P(klass, T_MODULE)) {
1712 rb_subclass_entry_t *iclass = RCLASS_SUBCLASSES_FIRST(klass);
1713 while (iclass) {
1714 int do_include = 1;
1715 VALUE check_class = iclass->klass;
1716 /* During lazy sweeping, iclass->klass could be a dead object that
1717 * has not yet been swept. */
1718 if (!rb_objspace_garbage_object_p(check_class)) {
1719 while (check_class) {
1720 RUBY_ASSERT(!rb_objspace_garbage_object_p(check_class));
1721
1722 if (RB_TYPE_P(check_class, T_ICLASS) &&
1723 (METACLASS_OF(check_class) == module)) {
1724 do_include = 0;
1725 }
1726 check_class = RCLASS_SUPER(check_class);
1727 }
1728
1729 if (do_include) {
1730 include_modules_at(iclass->klass, RCLASS_ORIGIN(iclass->klass), module, TRUE);
1731 }
1732 }
1733
1734 iclass = iclass->next;
1735 }
1736 }
1737}
1738
1739static enum rb_id_table_iterator_result
1740add_refined_method_entry_i(ID key, VALUE value, void *data)
1741{
1742 rb_add_refined_method_entry((VALUE)data, key);
1743 return ID_TABLE_CONTINUE;
1744}
1745
1746static enum rb_id_table_iterator_result
1747clear_module_cache_i(ID id, VALUE val, void *data)
1748{
1749 VALUE klass = (VALUE)data;
1750 rb_clear_method_cache(klass, id);
1751 return ID_TABLE_CONTINUE;
1752}
1753
1754static bool
1755module_in_super_chain(const VALUE klass, VALUE module)
1756{
1757 struct rb_id_table *const klass_m_tbl = RCLASS_M_TBL(RCLASS_ORIGIN(klass));
1758 if (klass_m_tbl) {
1759 while (module) {
1760 if (klass_m_tbl == RCLASS_M_TBL(module))
1761 return true;
1762 module = RCLASS_SUPER(module);
1763 }
1764 }
1765 return false;
1766}
1767
1768// For each ID key in the class constant table, we're going to clear the VM's
1769// inline constant caches associated with it.
1770static enum rb_id_table_iterator_result
1771clear_constant_cache_i(ID id, VALUE value, void *data)
1772{
1774 return ID_TABLE_CONTINUE;
1775}
1776
1777static int
1778do_include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super, bool check_cyclic)
1779{
1780 VALUE p, iclass, origin_stack = 0;
1781 int method_changed = 0;
1782 long origin_len;
1783 VALUE klass_origin = RCLASS_ORIGIN(klass);
1784 VALUE original_klass = klass;
1785
1786 if (check_cyclic && module_in_super_chain(klass, module))
1787 return -1;
1788
1789 while (module) {
1790 int c_seen = FALSE;
1791 int superclass_seen = FALSE;
1792 struct rb_id_table *tbl;
1793
1794 if (klass == c) {
1795 c_seen = TRUE;
1796 }
1797 if (klass_origin != c || search_super) {
1798 /* ignore if the module included already in superclasses for include,
1799 * ignore if the module included before origin class for prepend
1800 */
1801 for (p = RCLASS_SUPER(klass); p; p = RCLASS_SUPER(p)) {
1802 int type = BUILTIN_TYPE(p);
1803 if (klass_origin == p && !search_super)
1804 break;
1805 if (c == p)
1806 c_seen = TRUE;
1807 if (type == T_ICLASS) {
1808 if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) {
1809 if (!superclass_seen && c_seen) {
1810 c = p; /* move insertion point */
1811 }
1812 goto skip;
1813 }
1814 }
1815 else if (type == T_CLASS) {
1816 superclass_seen = TRUE;
1817 }
1818 }
1819 }
1820
1821 VALUE super_class = RCLASS_SUPER(c);
1822
1823 // invalidate inline method cache
1824 RB_DEBUG_COUNTER_INC(cvar_include_invalidate);
1825 ruby_vm_global_cvar_state++;
1826 tbl = RCLASS_M_TBL(module);
1827 if (tbl && rb_id_table_size(tbl)) {
1828 if (search_super) { // include
1829 if (super_class && !RB_TYPE_P(super_class, T_MODULE)) {
1830 rb_id_table_foreach(tbl, clear_module_cache_i, (void *)super_class);
1831 }
1832 }
1833 else { // prepend
1834 if (!RB_TYPE_P(original_klass, T_MODULE)) {
1835 rb_id_table_foreach(tbl, clear_module_cache_i, (void *)original_klass);
1836 }
1837 }
1838 method_changed = 1;
1839 }
1840
1841 // setup T_ICLASS for the include/prepend module
1842 iclass = rb_include_class_new(module, super_class);
1843 c = rb_class_set_super(c, iclass);
1844 RCLASS_SET_INCLUDER(iclass, klass);
1845 if (module != RCLASS_ORIGIN(module)) {
1846 if (!origin_stack) origin_stack = rb_ary_hidden_new(2);
1847 VALUE origin[2] = {iclass, RCLASS_ORIGIN(module)};
1848 rb_ary_cat(origin_stack, origin, 2);
1849 }
1850 else if (origin_stack && (origin_len = RARRAY_LEN(origin_stack)) > 1 &&
1851 RARRAY_AREF(origin_stack, origin_len - 1) == module) {
1852 RCLASS_WRITE_ORIGIN(RARRAY_AREF(origin_stack, (origin_len -= 2)), iclass);
1853 RICLASS_WRITE_ORIGIN_SHARED_MTBL(iclass);
1854 rb_ary_resize(origin_stack, origin_len);
1855 }
1856
1857 VALUE m = module;
1858 if (BUILTIN_TYPE(m) == T_ICLASS) m = METACLASS_OF(m);
1859 rb_module_add_to_subclasses_list(m, iclass);
1860
1861 if (BUILTIN_TYPE(klass) == T_MODULE && FL_TEST(klass, RMODULE_IS_REFINEMENT)) {
1862 VALUE refined_class =
1863 rb_refinement_module_get_refined_class(klass);
1864
1865 rb_id_table_foreach(RCLASS_M_TBL(module), add_refined_method_entry_i, (void *)refined_class);
1867 }
1868
1869 tbl = RCLASS_CONST_TBL(module);
1870 if (tbl && rb_id_table_size(tbl))
1871 rb_id_table_foreach(tbl, clear_constant_cache_i, NULL);
1872 skip:
1873 module = RCLASS_SUPER(module);
1874 }
1875
1876 return method_changed;
1877}
1878
1879static int
1880include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super)
1881{
1882 return do_include_modules_at(klass, c, module, search_super, true);
1883}
1884
1885static enum rb_id_table_iterator_result
1886move_refined_method(ID key, VALUE value, void *data)
1887{
1888 rb_method_entry_t *me = (rb_method_entry_t *)value;
1889
1890 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1891 VALUE klass = (VALUE)data;
1892 struct rb_id_table *tbl = RCLASS_WRITABLE_M_TBL(klass);
1893
1894 if (me->def->body.refined.orig_me) {
1895 const rb_method_entry_t *orig_me = me->def->body.refined.orig_me, *new_me;
1896 RB_OBJ_WRITE(me, &me->def->body.refined.orig_me, NULL);
1897 new_me = rb_method_entry_clone(me);
1898 rb_method_table_insert(klass, tbl, key, new_me);
1899 rb_method_entry_copy(me, orig_me);
1900 return ID_TABLE_CONTINUE;
1901 }
1902 else {
1903 rb_method_table_insert(klass, tbl, key, me);
1904 return ID_TABLE_DELETE;
1905 }
1906 }
1907 else {
1908 return ID_TABLE_CONTINUE;
1909 }
1910}
1911
1912static enum rb_id_table_iterator_result
1913cache_clear_refined_method(ID key, VALUE value, void *data)
1914{
1915 rb_method_entry_t *me = (rb_method_entry_t *) value;
1916
1917 if (me->def->type == VM_METHOD_TYPE_REFINED && me->def->body.refined.orig_me) {
1918 VALUE klass = (VALUE)data;
1919 rb_clear_method_cache(klass, me->called_id);
1920 }
1921 // Refined method entries without an orig_me is going to stay in the method
1922 // table of klass, like before the move, so no need to clear the cache.
1923
1924 return ID_TABLE_CONTINUE;
1925}
1926
1927static bool
1928ensure_origin(VALUE klass)
1929{
1930 VALUE origin = RCLASS_ORIGIN(klass);
1931 if (origin == klass) {
1932 origin = class_alloc(T_ICLASS, klass);
1933 RCLASS_SET_M_TBL(origin, RCLASS_M_TBL(klass));
1934 rb_class_set_super(origin, RCLASS_SUPER(klass));
1935 rb_class_set_super(klass, origin); // writes origin into RCLASS_SUPER(klass)
1936 RCLASS_WRITE_ORIGIN(klass, origin);
1937 class_clear_method_table(klass);
1938 rb_id_table_foreach(RCLASS_M_TBL(origin), cache_clear_refined_method, (void *)klass);
1939 rb_id_table_foreach(RCLASS_M_TBL(origin), move_refined_method, (void *)klass);
1940 return true;
1941 }
1942 return false;
1943}
1944
1945void
1947{
1948 int changed;
1949 bool klass_had_no_origin;
1950
1951 ensure_includable(klass, module);
1952 if (module_in_super_chain(klass, module))
1953 rb_raise(rb_eArgError, "cyclic prepend detected");
1954
1955 klass_had_no_origin = ensure_origin(klass);
1956 changed = do_include_modules_at(klass, klass, module, FALSE, false);
1957 RUBY_ASSERT(changed >= 0); // already checked for cyclic prepend above
1958 if (changed) {
1959 rb_vm_check_redefinition_by_prepend(klass);
1960 }
1961 if (RB_TYPE_P(klass, T_MODULE)) {
1962 rb_subclass_entry_t *iclass = RCLASS_SUBCLASSES_FIRST(klass);
1963 VALUE klass_origin = RCLASS_ORIGIN(klass);
1964 struct rb_id_table *klass_m_tbl = RCLASS_M_TBL(klass);
1965 struct rb_id_table *klass_origin_m_tbl = RCLASS_M_TBL(klass_origin);
1966 while (iclass) {
1967 /* During lazy sweeping, iclass->klass could be a dead object that
1968 * has not yet been swept. */
1969 if (!rb_objspace_garbage_object_p(iclass->klass)) {
1970 const VALUE subclass = iclass->klass;
1971 if (klass_had_no_origin && klass_origin_m_tbl == RCLASS_M_TBL(subclass)) {
1972 // backfill an origin iclass to handle refinements and future prepends
1973 rb_id_table_foreach(RCLASS_M_TBL(subclass), clear_module_cache_i, (void *)subclass);
1974 RCLASS_WRITE_M_TBL_EVEN_WHEN_PROMOTED(subclass, klass_m_tbl);
1975 VALUE origin = rb_include_class_new(klass_origin, RCLASS_SUPER(subclass));
1976 rb_class_set_super(subclass, origin);
1977 RCLASS_SET_INCLUDER(origin, RCLASS_INCLUDER(subclass));
1978 RCLASS_WRITE_ORIGIN(subclass, origin);
1979 RICLASS_SET_ORIGIN_SHARED_MTBL(origin);
1980 }
1981 include_modules_at(subclass, subclass, module, FALSE);
1982 }
1983
1984 iclass = iclass->next;
1985 }
1986 }
1987}
1988
1989/*
1990 * call-seq:
1991 * mod.included_modules -> array
1992 *
1993 * Returns the list of modules included or prepended in <i>mod</i>
1994 * or one of <i>mod</i>'s ancestors.
1995 *
1996 * module Sub
1997 * end
1998 *
1999 * module Mixin
2000 * prepend Sub
2001 * end
2002 *
2003 * module Outer
2004 * include Mixin
2005 * end
2006 *
2007 * Mixin.included_modules #=> [Sub]
2008 * Outer.included_modules #=> [Sub, Mixin]
2009 */
2010
2011VALUE
2013{
2014 VALUE ary = rb_ary_new();
2015 VALUE p;
2016 VALUE origin = RCLASS_ORIGIN(mod);
2017
2018 for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
2019 if (p != origin && RCLASS_ORIGIN(p) == p && BUILTIN_TYPE(p) == T_ICLASS) {
2020 VALUE m = METACLASS_OF(p);
2021 if (RB_TYPE_P(m, T_MODULE))
2022 rb_ary_push(ary, m);
2023 }
2024 }
2025 return ary;
2026}
2027
2028/*
2029 * call-seq:
2030 * mod.include?(module) -> true or false
2031 *
2032 * Returns <code>true</code> if <i>module</i> is included
2033 * or prepended in <i>mod</i> or one of <i>mod</i>'s ancestors.
2034 *
2035 * module A
2036 * end
2037 * class B
2038 * include A
2039 * end
2040 * class C < B
2041 * end
2042 * B.include?(A) #=> true
2043 * C.include?(A) #=> true
2044 * A.include?(A) #=> false
2045 */
2046
2047VALUE
2049{
2050 VALUE p;
2051
2052 Check_Type(mod2, T_MODULE);
2053 for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
2054 if (BUILTIN_TYPE(p) == T_ICLASS && !RICLASS_IS_ORIGIN_P(p)) {
2055 if (METACLASS_OF(p) == mod2) return Qtrue;
2056 }
2057 }
2058 return Qfalse;
2059}
2060
2061/*
2062 * call-seq:
2063 * mod.ancestors -> array
2064 *
2065 * Returns a list of modules included/prepended in <i>mod</i>
2066 * (including <i>mod</i> itself).
2067 *
2068 * module Mod
2069 * include Math
2070 * include Comparable
2071 * prepend Enumerable
2072 * end
2073 *
2074 * Mod.ancestors #=> [Enumerable, Mod, Comparable, Math]
2075 * Math.ancestors #=> [Math]
2076 * Enumerable.ancestors #=> [Enumerable]
2077 */
2078
2079VALUE
2081{
2082 VALUE p, ary = rb_ary_new();
2083 VALUE refined_class = Qnil;
2084 if (BUILTIN_TYPE(mod) == T_MODULE && FL_TEST(mod, RMODULE_IS_REFINEMENT)) {
2085 refined_class = rb_refinement_module_get_refined_class(mod);
2086 }
2087
2088 for (p = mod; p; p = RCLASS_SUPER(p)) {
2089 if (p == refined_class) break;
2090 if (p != RCLASS_ORIGIN(p)) continue;
2091 if (BUILTIN_TYPE(p) == T_ICLASS) {
2092 rb_ary_push(ary, METACLASS_OF(p));
2093 }
2094 else {
2095 rb_ary_push(ary, p);
2096 }
2097 }
2098 return ary;
2099}
2100
2102{
2103 VALUE buffer;
2104 long count;
2105 long maxcount;
2106 bool immediate_only;
2107};
2108
2109static void
2110class_descendants_recursive(VALUE klass, VALUE v)
2111{
2112 struct subclass_traverse_data *data = (struct subclass_traverse_data *) v;
2113
2114 if (BUILTIN_TYPE(klass) == T_CLASS && !RCLASS_SINGLETON_P(klass)) {
2115 if (data->buffer && data->count < data->maxcount && !rb_objspace_garbage_object_p(klass)) {
2116 // assumes that this does not cause GC as long as the length does not exceed the capacity
2117 rb_ary_push(data->buffer, klass);
2118 }
2119 data->count++;
2120 if (!data->immediate_only) {
2121 rb_class_foreach_subclass(klass, class_descendants_recursive, v);
2122 }
2123 }
2124 else {
2125 rb_class_foreach_subclass(klass, class_descendants_recursive, v);
2126 }
2127}
2128
2129static VALUE
2130class_descendants(VALUE klass, bool immediate_only)
2131{
2132 struct subclass_traverse_data data = { Qfalse, 0, -1, immediate_only };
2133
2134 // estimate the count of subclasses
2135 rb_class_foreach_subclass(klass, class_descendants_recursive, (VALUE) &data);
2136
2137 // the following allocation may cause GC which may change the number of subclasses
2138 data.buffer = rb_ary_new_capa(data.count);
2139 data.maxcount = data.count;
2140 data.count = 0;
2141
2142 size_t gc_count = rb_gc_count();
2143
2144 // enumerate subclasses
2145 rb_class_foreach_subclass(klass, class_descendants_recursive, (VALUE) &data);
2146
2147 if (gc_count != rb_gc_count()) {
2148 rb_bug("GC must not occur during the subclass iteration of Class#descendants");
2149 }
2150
2151 return data.buffer;
2152}
2153
2154/*
2155 * call-seq:
2156 * subclasses -> array
2157 *
2158 * Returns an array of classes where the receiver is the
2159 * direct superclass of the class, excluding singleton classes.
2160 * The order of the returned array is not defined.
2161 *
2162 * class A; end
2163 * class B < A; end
2164 * class C < B; end
2165 * class D < A; end
2166 *
2167 * A.subclasses #=> [D, B]
2168 * B.subclasses #=> [C]
2169 * C.subclasses #=> []
2170 *
2171 * Anonymous subclasses (not associated with a constant) are
2172 * returned, too:
2173 *
2174 * c = Class.new(A)
2175 * A.subclasses # => [#<Class:0x00007f003c77bd78>, D, B]
2176 *
2177 * Note that the parent does not hold references to subclasses
2178 * and doesn't prevent them from being garbage collected. This
2179 * means that the subclass might disappear when all references
2180 * to it are dropped:
2181 *
2182 * # drop the reference to subclass, it can be garbage-collected now
2183 * c = nil
2184 *
2185 * A.subclasses
2186 * # It can be
2187 * # => [#<Class:0x00007f003c77bd78>, D, B]
2188 * # ...or just
2189 * # => [D, B]
2190 * # ...depending on whether garbage collector was run
2191 */
2192
2193VALUE
2195{
2196 return class_descendants(klass, true);
2197}
2198
2199/*
2200 * call-seq:
2201 * attached_object -> object
2202 *
2203 * Returns the object for which the receiver is the singleton class.
2204 *
2205 * Raises an TypeError if the class is not a singleton class.
2206 *
2207 * class Foo; end
2208 *
2209 * Foo.singleton_class.attached_object #=> Foo
2210 * Foo.attached_object #=> TypeError: `Foo' is not a singleton class
2211 * Foo.new.singleton_class.attached_object #=> #<Foo:0x000000010491a370>
2212 * TrueClass.attached_object #=> TypeError: `TrueClass' is not a singleton class
2213 * NilClass.attached_object #=> TypeError: `NilClass' is not a singleton class
2214 */
2215
2216VALUE
2218{
2219 if (!RCLASS_SINGLETON_P(klass)) {
2220 rb_raise(rb_eTypeError, "'%"PRIsVALUE"' is not a singleton class", klass);
2221 }
2222
2223 return RCLASS_ATTACHED_OBJECT(klass);
2224}
2225
2226static void
2227ins_methods_push(st_data_t name, st_data_t ary)
2228{
2229 rb_ary_push((VALUE)ary, ID2SYM((ID)name));
2230}
2231
2232static int
2233ins_methods_i(st_data_t name, st_data_t type, st_data_t ary)
2234{
2235 switch ((rb_method_visibility_t)type) {
2236 case METHOD_VISI_UNDEF:
2237 case METHOD_VISI_PRIVATE:
2238 break;
2239 default: /* everything but private */
2240 ins_methods_push(name, ary);
2241 break;
2242 }
2243 return ST_CONTINUE;
2244}
2245
2246static int
2247ins_methods_type_i(st_data_t name, st_data_t type, st_data_t ary, rb_method_visibility_t visi)
2248{
2249 if ((rb_method_visibility_t)type == visi) {
2250 ins_methods_push(name, ary);
2251 }
2252 return ST_CONTINUE;
2253}
2254
2255static int
2256ins_methods_prot_i(st_data_t name, st_data_t type, st_data_t ary)
2257{
2258 return ins_methods_type_i(name, type, ary, METHOD_VISI_PROTECTED);
2259}
2260
2261static int
2262ins_methods_priv_i(st_data_t name, st_data_t type, st_data_t ary)
2263{
2264 return ins_methods_type_i(name, type, ary, METHOD_VISI_PRIVATE);
2265}
2266
2267static int
2268ins_methods_pub_i(st_data_t name, st_data_t type, st_data_t ary)
2269{
2270 return ins_methods_type_i(name, type, ary, METHOD_VISI_PUBLIC);
2271}
2272
2273static int
2274ins_methods_undef_i(st_data_t name, st_data_t type, st_data_t ary)
2275{
2276 return ins_methods_type_i(name, type, ary, METHOD_VISI_UNDEF);
2277}
2278
2280 st_table *list;
2281 int recur;
2282};
2283
2284static enum rb_id_table_iterator_result
2285method_entry_i(ID key, VALUE value, void *data)
2286{
2287 const rb_method_entry_t *me = (const rb_method_entry_t *)value;
2288 struct method_entry_arg *arg = (struct method_entry_arg *)data;
2289 rb_method_visibility_t type;
2290
2291 if (me->def->type == VM_METHOD_TYPE_REFINED) {
2292 VALUE owner = me->owner;
2293 me = rb_resolve_refined_method(Qnil, me);
2294 if (!me) return ID_TABLE_CONTINUE;
2295 if (!arg->recur && me->owner != owner) return ID_TABLE_CONTINUE;
2296 }
2297 if (!st_is_member(arg->list, key)) {
2298 if (UNDEFINED_METHOD_ENTRY_P(me)) {
2299 type = METHOD_VISI_UNDEF; /* none */
2300 }
2301 else {
2302 type = METHOD_ENTRY_VISI(me);
2303 RUBY_ASSERT(type != METHOD_VISI_UNDEF);
2304 }
2305 st_add_direct(arg->list, key, (st_data_t)type);
2306 }
2307 return ID_TABLE_CONTINUE;
2308}
2309
2310static void
2311add_instance_method_list(VALUE mod, struct method_entry_arg *me_arg)
2312{
2313 struct rb_id_table *m_tbl = RCLASS_M_TBL(mod);
2314 if (!m_tbl) return;
2315 rb_id_table_foreach(m_tbl, method_entry_i, me_arg);
2316}
2317
2318static bool
2319particular_class_p(VALUE mod)
2320{
2321 if (!mod) return false;
2322 if (RCLASS_SINGLETON_P(mod)) return true;
2323 if (BUILTIN_TYPE(mod) == T_ICLASS) return true;
2324 return false;
2325}
2326
2327static VALUE
2328class_instance_method_list(int argc, const VALUE *argv, VALUE mod, int obj, int (*func) (st_data_t, st_data_t, st_data_t))
2329{
2330 VALUE ary;
2331 int recur = TRUE, prepended = 0;
2332 struct method_entry_arg me_arg;
2333
2334 if (rb_check_arity(argc, 0, 1)) recur = RTEST(argv[0]);
2335
2336 me_arg.list = st_init_numtable();
2337 me_arg.recur = recur;
2338
2339 if (obj) {
2340 for (; particular_class_p(mod); mod = RCLASS_SUPER(mod)) {
2341 add_instance_method_list(mod, &me_arg);
2342 }
2343 }
2344
2345 if (!recur && RCLASS_ORIGIN(mod) != mod) {
2346 mod = RCLASS_ORIGIN(mod);
2347 prepended = 1;
2348 }
2349
2350 for (; mod; mod = RCLASS_SUPER(mod)) {
2351 add_instance_method_list(mod, &me_arg);
2352 if (BUILTIN_TYPE(mod) == T_ICLASS && !prepended) continue;
2353 if (!recur) break;
2354 }
2355 ary = rb_ary_new2(me_arg.list->num_entries);
2356 st_foreach(me_arg.list, func, ary);
2357 st_free_table(me_arg.list);
2358
2359 return ary;
2360}
2361
2362/*
2363 * call-seq:
2364 * mod.instance_methods(include_super=true) -> array
2365 *
2366 * Returns an array containing the names of the public and protected instance
2367 * methods in the receiver. For a module, these are the public and protected methods;
2368 * for a class, they are the instance (not singleton) methods. If the optional
2369 * parameter is <code>false</code>, the methods of any ancestors are not included.
2370 *
2371 * module A
2372 * def method1() end
2373 * end
2374 * class B
2375 * include A
2376 * def method2() end
2377 * end
2378 * class C < B
2379 * def method3() end
2380 * end
2381 *
2382 * A.instance_methods(false) #=> [:method1]
2383 * B.instance_methods(false) #=> [:method2]
2384 * B.instance_methods(true).include?(:method1) #=> true
2385 * C.instance_methods(false) #=> [:method3]
2386 * C.instance_methods.include?(:method2) #=> true
2387 *
2388 * Note that method visibility changes in the current class, as well as aliases,
2389 * are considered as methods of the current class by this method:
2390 *
2391 * class C < B
2392 * alias method4 method2
2393 * protected :method2
2394 * end
2395 * C.instance_methods(false).sort #=> [:method2, :method3, :method4]
2396 */
2397
2398VALUE
2399rb_class_instance_methods(int argc, const VALUE *argv, VALUE mod)
2400{
2401 return class_instance_method_list(argc, argv, mod, 0, ins_methods_i);
2402}
2403
2404/*
2405 * call-seq:
2406 * mod.protected_instance_methods(include_super=true) -> array
2407 *
2408 * Returns a list of the protected instance methods defined in
2409 * <i>mod</i>. If the optional parameter is <code>false</code>, the
2410 * methods of any ancestors are not included.
2411 */
2412
2413VALUE
2415{
2416 return class_instance_method_list(argc, argv, mod, 0, ins_methods_prot_i);
2417}
2418
2419/*
2420 * call-seq:
2421 * mod.private_instance_methods(include_super=true) -> array
2422 *
2423 * Returns a list of the private instance methods defined in
2424 * <i>mod</i>. If the optional parameter is <code>false</code>, the
2425 * methods of any ancestors are not included.
2426 *
2427 * module Mod
2428 * def method1() end
2429 * private :method1
2430 * def method2() end
2431 * end
2432 * Mod.instance_methods #=> [:method2]
2433 * Mod.private_instance_methods #=> [:method1]
2434 */
2435
2436VALUE
2438{
2439 return class_instance_method_list(argc, argv, mod, 0, ins_methods_priv_i);
2440}
2441
2442/*
2443 * call-seq:
2444 * mod.public_instance_methods(include_super=true) -> array
2445 *
2446 * Returns a list of the public instance methods defined in <i>mod</i>.
2447 * If the optional parameter is <code>false</code>, the methods of
2448 * any ancestors are not included.
2449 */
2450
2451VALUE
2453{
2454 return class_instance_method_list(argc, argv, mod, 0, ins_methods_pub_i);
2455}
2456
2457/*
2458 * call-seq:
2459 * mod.undefined_instance_methods -> array
2460 *
2461 * Returns a list of the undefined instance methods defined in <i>mod</i>.
2462 * The undefined methods of any ancestors are not included.
2463 */
2464
2465VALUE
2466rb_class_undefined_instance_methods(VALUE mod)
2467{
2468 VALUE include_super = Qfalse;
2469 return class_instance_method_list(1, &include_super, mod, 0, ins_methods_undef_i);
2470}
2471
2472/*
2473 * call-seq:
2474 * obj.methods(regular=true) -> array
2475 *
2476 * Returns a list of the names of public and protected methods of
2477 * <i>obj</i>. This will include all the methods accessible in
2478 * <i>obj</i>'s ancestors.
2479 * If the optional parameter is <code>false</code>, it
2480 * returns an array of <i>obj</i>'s public and protected singleton methods,
2481 * the array will not include methods in modules included in <i>obj</i>.
2482 *
2483 * class Klass
2484 * def klass_method()
2485 * end
2486 * end
2487 * k = Klass.new
2488 * k.methods[0..9] #=> [:klass_method, :nil?, :===,
2489 * # :==~, :!, :eql?
2490 * # :hash, :<=>, :class, :singleton_class]
2491 * k.methods.length #=> 56
2492 *
2493 * k.methods(false) #=> []
2494 * def k.singleton_method; end
2495 * k.methods(false) #=> [:singleton_method]
2496 *
2497 * module M123; def m123; end end
2498 * k.extend M123
2499 * k.methods(false) #=> [:singleton_method]
2500 */
2501
2502VALUE
2503rb_obj_methods(int argc, const VALUE *argv, VALUE obj)
2504{
2505 rb_check_arity(argc, 0, 1);
2506 if (argc > 0 && !RTEST(argv[0])) {
2507 return rb_obj_singleton_methods(argc, argv, obj);
2508 }
2509 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_i);
2510}
2511
2512/*
2513 * call-seq:
2514 * obj.protected_methods(all=true) -> array
2515 *
2516 * Returns the list of protected methods accessible to <i>obj</i>. If
2517 * the <i>all</i> parameter is set to <code>false</code>, only those methods
2518 * in the receiver will be listed.
2519 */
2520
2521VALUE
2522rb_obj_protected_methods(int argc, const VALUE *argv, VALUE obj)
2523{
2524 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_prot_i);
2525}
2526
2527/*
2528 * call-seq:
2529 * obj.private_methods(all=true) -> array
2530 *
2531 * Returns the list of private methods accessible to <i>obj</i>. If
2532 * the <i>all</i> parameter is set to <code>false</code>, only those methods
2533 * in the receiver will be listed.
2534 */
2535
2536VALUE
2537rb_obj_private_methods(int argc, const VALUE *argv, VALUE obj)
2538{
2539 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_priv_i);
2540}
2541
2542/*
2543 * call-seq:
2544 * obj.public_methods(all=true) -> array
2545 *
2546 * Returns the list of public methods accessible to <i>obj</i>. If
2547 * the <i>all</i> parameter is set to <code>false</code>, only those methods
2548 * in the receiver will be listed.
2549 */
2550
2551VALUE
2552rb_obj_public_methods(int argc, const VALUE *argv, VALUE obj)
2553{
2554 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_pub_i);
2555}
2556
2557/*
2558 * call-seq:
2559 * obj.singleton_methods(all=true) -> array
2560 *
2561 * Returns an array of the names of singleton methods for <i>obj</i>.
2562 * If the optional <i>all</i> parameter is true, the list will include
2563 * methods in modules included in <i>obj</i>.
2564 * Only public and protected singleton methods are returned.
2565 *
2566 * module Other
2567 * def three() end
2568 * end
2569 *
2570 * class Single
2571 * def Single.four() end
2572 * end
2573 *
2574 * a = Single.new
2575 *
2576 * def a.one()
2577 * end
2578 *
2579 * class << a
2580 * include Other
2581 * def two()
2582 * end
2583 * end
2584 *
2585 * Single.singleton_methods #=> [:four]
2586 * a.singleton_methods(false) #=> [:two, :one]
2587 * a.singleton_methods #=> [:two, :one, :three]
2588 */
2589
2590VALUE
2591rb_obj_singleton_methods(int argc, const VALUE *argv, VALUE obj)
2592{
2593 VALUE ary, klass, origin;
2594 struct method_entry_arg me_arg;
2595 struct rb_id_table *mtbl;
2596 int recur = TRUE;
2597
2598 if (rb_check_arity(argc, 0, 1)) recur = RTEST(argv[0]);
2599 if (RCLASS_SINGLETON_P(obj)) {
2600 rb_singleton_class(obj);
2601 }
2602 klass = CLASS_OF(obj);
2603 origin = RCLASS_ORIGIN(klass);
2604 me_arg.list = st_init_numtable();
2605 me_arg.recur = recur;
2606 if (klass && RCLASS_SINGLETON_P(klass)) {
2607 if ((mtbl = RCLASS_M_TBL(origin)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2608 klass = RCLASS_SUPER(klass);
2609 }
2610 if (recur) {
2611 while (klass && (RCLASS_SINGLETON_P(klass) || RB_TYPE_P(klass, T_ICLASS))) {
2612 if (klass != origin && (mtbl = RCLASS_M_TBL(klass)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2613 klass = RCLASS_SUPER(klass);
2614 }
2615 }
2616 ary = rb_ary_new2(me_arg.list->num_entries);
2617 st_foreach(me_arg.list, ins_methods_i, ary);
2618 st_free_table(me_arg.list);
2619
2620 return ary;
2621}
2622
2631#ifdef rb_define_method_id
2632#undef rb_define_method_id
2633#endif
2634void
2635rb_define_method_id(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc)
2636{
2637 rb_add_method_cfunc(klass, mid, func, argc, METHOD_VISI_PUBLIC);
2638}
2639
2640#ifdef rb_define_method
2641#undef rb_define_method
2642#endif
2643void
2644rb_define_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
2645{
2646 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PUBLIC);
2647}
2648
2649#ifdef rb_define_protected_method
2650#undef rb_define_protected_method
2651#endif
2652void
2653rb_define_protected_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
2654{
2655 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PROTECTED);
2656}
2657
2658#ifdef rb_define_private_method
2659#undef rb_define_private_method
2660#endif
2661void
2662rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
2663{
2664 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PRIVATE);
2665}
2666
2667void
2668rb_undef_method(VALUE klass, const char *name)
2669{
2670 rb_add_method(klass, rb_intern(name), VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2671}
2672
2673static enum rb_id_table_iterator_result
2674undef_method_i(ID name, VALUE value, void *data)
2675{
2676 VALUE klass = (VALUE)data;
2677 rb_add_method(klass, name, VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2678 return ID_TABLE_CONTINUE;
2679}
2680
2681void
2682rb_undef_methods_from(VALUE klass, VALUE super)
2683{
2684 struct rb_id_table *mtbl = RCLASS_M_TBL(super);
2685 if (mtbl) {
2686 rb_id_table_foreach(mtbl, undef_method_i, (void *)klass);
2687 }
2688}
2689
2698static inline VALUE
2699special_singleton_class_of(VALUE obj)
2700{
2701 switch (obj) {
2702 case Qnil: return rb_cNilClass;
2703 case Qfalse: return rb_cFalseClass;
2704 case Qtrue: return rb_cTrueClass;
2705 default: return Qnil;
2706 }
2707}
2708
2709VALUE
2710rb_special_singleton_class(VALUE obj)
2711{
2712 return special_singleton_class_of(obj);
2713}
2714
2724static VALUE
2725singleton_class_of(VALUE obj)
2726{
2727 VALUE klass;
2728
2729 switch (TYPE(obj)) {
2730 case T_FIXNUM:
2731 case T_BIGNUM:
2732 case T_FLOAT:
2733 case T_SYMBOL:
2734 rb_raise(rb_eTypeError, "can't define singleton");
2735
2736 case T_FALSE:
2737 case T_TRUE:
2738 case T_NIL:
2739 klass = special_singleton_class_of(obj);
2740 if (NIL_P(klass))
2741 rb_bug("unknown immediate %p", (void *)obj);
2742 return klass;
2743
2744 case T_STRING:
2745 if (CHILLED_STRING_P(obj)) {
2746 CHILLED_STRING_MUTATED(obj);
2747 }
2748 else if (FL_TEST_RAW(obj, RSTRING_FSTR)) {
2749 rb_raise(rb_eTypeError, "can't define singleton");
2750 }
2751 }
2752
2753 klass = METACLASS_OF(obj);
2754 if (!(RCLASS_SINGLETON_P(klass) &&
2755 RCLASS_ATTACHED_OBJECT(klass) == obj)) {
2756 klass = rb_make_metaclass(obj, klass);
2757 }
2758
2759 RB_FL_SET_RAW(klass, RB_OBJ_FROZEN_RAW(obj));
2760
2761 return klass;
2762}
2763
2764void
2766{
2767 /* should not propagate to meta-meta-class, and so on */
2768 if (!RCLASS_SINGLETON_P(x)) {
2769 VALUE klass = RBASIC_CLASS(x);
2770 if (klass && // no class when hidden from ObjectSpace
2771 FL_TEST_RAW(klass, FL_SINGLETON) &&
2772 !OBJ_FROZEN_RAW(klass)) {
2773 OBJ_FREEZE(klass);
2774 }
2775 }
2776}
2777
2785VALUE
2787{
2788 VALUE klass;
2789
2790 if (SPECIAL_CONST_P(obj)) {
2791 return rb_special_singleton_class(obj);
2792 }
2793 klass = METACLASS_OF(obj);
2794 if (!RCLASS_SINGLETON_P(klass)) return Qnil;
2795 if (RCLASS_ATTACHED_OBJECT(klass) != obj) return Qnil;
2796 return klass;
2797}
2798
2799VALUE
2801{
2802 VALUE klass = singleton_class_of(obj);
2803
2804 /* ensures an exposed class belongs to its own eigenclass */
2805 if (RB_TYPE_P(obj, T_CLASS)) (void)ENSURE_EIGENCLASS(klass);
2806
2807 return klass;
2808}
2809
2819#ifdef rb_define_singleton_method
2820#undef rb_define_singleton_method
2821#endif
2822void
2823rb_define_singleton_method(VALUE obj, const char *name, VALUE (*func)(ANYARGS), int argc)
2824{
2825 rb_define_method(singleton_class_of(obj), name, func, argc);
2826}
2827
2828#ifdef rb_define_module_function
2829#undef rb_define_module_function
2830#endif
2831void
2832rb_define_module_function(VALUE module, const char *name, VALUE (*func)(ANYARGS), int argc)
2833{
2834 rb_define_private_method(module, name, func, argc);
2835 rb_define_singleton_method(module, name, func, argc);
2836}
2837
2838#ifdef rb_define_global_function
2839#undef rb_define_global_function
2840#endif
2841void
2842rb_define_global_function(const char *name, VALUE (*func)(ANYARGS), int argc)
2843{
2844 rb_define_module_function(rb_mKernel, name, func, argc);
2845}
2846
2847void
2848rb_define_alias(VALUE klass, const char *name1, const char *name2)
2849{
2850 rb_alias(klass, rb_intern(name1), rb_intern(name2));
2851}
2852
2853void
2854rb_define_attr(VALUE klass, const char *name, int read, int write)
2855{
2856 rb_attr(klass, rb_intern(name), read, write, FALSE);
2857}
2858
2859VALUE
2860rb_keyword_error_new(const char *error, VALUE keys)
2861{
2862 long i = 0, len = RARRAY_LEN(keys);
2863 VALUE error_message = rb_sprintf("%s keyword%.*s", error, len > 1, "s");
2864
2865 if (len > 0) {
2866 rb_str_cat_cstr(error_message, ": ");
2867 while (1) {
2868 const VALUE k = RARRAY_AREF(keys, i);
2869 rb_str_append(error_message, rb_inspect(k));
2870 if (++i >= len) break;
2871 rb_str_cat_cstr(error_message, ", ");
2872 }
2873 }
2874
2875 return rb_exc_new_str(rb_eArgError, error_message);
2876}
2877
2878NORETURN(static void rb_keyword_error(const char *error, VALUE keys));
2879static void
2880rb_keyword_error(const char *error, VALUE keys)
2881{
2882 rb_exc_raise(rb_keyword_error_new(error, keys));
2883}
2884
2885NORETURN(static void unknown_keyword_error(VALUE hash, const ID *table, int keywords));
2886static void
2887unknown_keyword_error(VALUE hash, const ID *table, int keywords)
2888{
2889 int i;
2890 for (i = 0; i < keywords; i++) {
2891 st_data_t key = ID2SYM(table[i]);
2892 rb_hash_stlike_delete(hash, &key, NULL);
2893 }
2894 rb_keyword_error("unknown", rb_hash_keys(hash));
2895}
2896
2897
2898static int
2899separate_symbol(st_data_t key, st_data_t value, st_data_t arg)
2900{
2901 VALUE *kwdhash = (VALUE *)arg;
2902 if (!SYMBOL_P(key)) kwdhash++;
2903 if (!*kwdhash) *kwdhash = rb_hash_new();
2904 rb_hash_aset(*kwdhash, (VALUE)key, (VALUE)value);
2905 return ST_CONTINUE;
2906}
2907
2908VALUE
2910{
2911 VALUE parthash[2] = {0, 0};
2912 VALUE hash = *orighash;
2913
2914 if (RHASH_EMPTY_P(hash)) {
2915 *orighash = 0;
2916 return hash;
2917 }
2918 rb_hash_foreach(hash, separate_symbol, (st_data_t)&parthash);
2919 *orighash = parthash[1];
2920 if (parthash[1] && RBASIC_CLASS(hash) != rb_cHash) {
2921 RBASIC_SET_CLASS(parthash[1], RBASIC_CLASS(hash));
2922 }
2923 return parthash[0];
2924}
2925
2926int
2927rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
2928{
2929 int i = 0, j;
2930 int rest = 0;
2931 VALUE missing = Qnil;
2932 st_data_t key;
2933
2934#define extract_kwarg(keyword, val) \
2935 (key = (st_data_t)(keyword), values ? \
2936 (rb_hash_stlike_delete(keyword_hash, &key, &(val)) || ((val) = Qundef, 0)) : \
2937 rb_hash_stlike_lookup(keyword_hash, key, NULL))
2938
2939 if (NIL_P(keyword_hash)) keyword_hash = 0;
2940
2941 if (optional < 0) {
2942 rest = 1;
2943 optional = -1-optional;
2944 }
2945 if (required) {
2946 for (; i < required; i++) {
2947 VALUE keyword = ID2SYM(table[i]);
2948 if (keyword_hash) {
2949 if (extract_kwarg(keyword, values[i])) {
2950 continue;
2951 }
2952 }
2953 if (NIL_P(missing)) missing = rb_ary_hidden_new(1);
2954 rb_ary_push(missing, keyword);
2955 }
2956 if (!NIL_P(missing)) {
2957 rb_keyword_error("missing", missing);
2958 }
2959 }
2960 j = i;
2961 if (optional && keyword_hash) {
2962 for (i = 0; i < optional; i++) {
2963 if (extract_kwarg(ID2SYM(table[required+i]), values[required+i])) {
2964 j++;
2965 }
2966 }
2967 }
2968 if (!rest && keyword_hash) {
2969 if (RHASH_SIZE(keyword_hash) > (unsigned int)(values ? 0 : j)) {
2970 unknown_keyword_error(keyword_hash, table, required+optional);
2971 }
2972 }
2973 if (values && !keyword_hash) {
2974 for (i = 0; i < required + optional; i++) {
2975 values[i] = Qundef;
2976 }
2977 }
2978 return j;
2979#undef extract_kwarg
2980}
2981
2983 int kw_flag;
2984 int n_lead;
2985 int n_opt;
2986 int n_trail;
2987 bool f_var;
2988 bool f_hash;
2989 bool f_block;
2990};
2991
2992static void
2993rb_scan_args_parse(int kw_flag, const char *fmt, struct rb_scan_args_t *arg)
2994{
2995 const char *p = fmt;
2996
2997 memset(arg, 0, sizeof(*arg));
2998 arg->kw_flag = kw_flag;
2999
3000 if (ISDIGIT(*p)) {
3001 arg->n_lead = *p - '0';
3002 p++;
3003 if (ISDIGIT(*p)) {
3004 arg->n_opt = *p - '0';
3005 p++;
3006 }
3007 }
3008 if (*p == '*') {
3009 arg->f_var = 1;
3010 p++;
3011 }
3012 if (ISDIGIT(*p)) {
3013 arg->n_trail = *p - '0';
3014 p++;
3015 }
3016 if (*p == ':') {
3017 arg->f_hash = 1;
3018 p++;
3019 }
3020 if (*p == '&') {
3021 arg->f_block = 1;
3022 p++;
3023 }
3024 if (*p != '\0') {
3025 rb_fatal("bad scan arg format: %s", fmt);
3026 }
3027}
3028
3029static int
3030rb_scan_args_assign(const struct rb_scan_args_t *arg, int argc, const VALUE *const argv, va_list vargs)
3031{
3032 int i, argi = 0;
3033 VALUE *var, hash = Qnil;
3034#define rb_scan_args_next_param() va_arg(vargs, VALUE *)
3035 const int kw_flag = arg->kw_flag;
3036 const int n_lead = arg->n_lead;
3037 const int n_opt = arg->n_opt;
3038 const int n_trail = arg->n_trail;
3039 const int n_mand = n_lead + n_trail;
3040 const bool f_var = arg->f_var;
3041 const bool f_hash = arg->f_hash;
3042 const bool f_block = arg->f_block;
3043
3044 /* capture an option hash - phase 1: pop from the argv */
3045 if (f_hash && argc > 0) {
3046 VALUE last = argv[argc - 1];
3047 if (rb_scan_args_keyword_p(kw_flag, last)) {
3048 hash = rb_hash_dup(last);
3049 argc--;
3050 }
3051 }
3052
3053 if (argc < n_mand) {
3054 goto argc_error;
3055 }
3056
3057 /* capture leading mandatory arguments */
3058 for (i = 0; i < n_lead; i++) {
3059 var = rb_scan_args_next_param();
3060 if (var) *var = argv[argi];
3061 argi++;
3062 }
3063 /* capture optional arguments */
3064 for (i = 0; i < n_opt; i++) {
3065 var = rb_scan_args_next_param();
3066 if (argi < argc - n_trail) {
3067 if (var) *var = argv[argi];
3068 argi++;
3069 }
3070 else {
3071 if (var) *var = Qnil;
3072 }
3073 }
3074 /* capture variable length arguments */
3075 if (f_var) {
3076 int n_var = argc - argi - n_trail;
3077
3078 var = rb_scan_args_next_param();
3079 if (0 < n_var) {
3080 if (var) *var = rb_ary_new_from_values(n_var, &argv[argi]);
3081 argi += n_var;
3082 }
3083 else {
3084 if (var) *var = rb_ary_new();
3085 }
3086 }
3087 /* capture trailing mandatory arguments */
3088 for (i = 0; i < n_trail; i++) {
3089 var = rb_scan_args_next_param();
3090 if (var) *var = argv[argi];
3091 argi++;
3092 }
3093 /* capture an option hash - phase 2: assignment */
3094 if (f_hash) {
3095 var = rb_scan_args_next_param();
3096 if (var) *var = hash;
3097 }
3098 /* capture iterator block */
3099 if (f_block) {
3100 var = rb_scan_args_next_param();
3101 if (rb_block_given_p()) {
3102 *var = rb_block_proc();
3103 }
3104 else {
3105 *var = Qnil;
3106 }
3107 }
3108
3109 if (argi == argc) {
3110 return argc;
3111 }
3112
3113 argc_error:
3114 return -(argc + 1);
3115#undef rb_scan_args_next_param
3116}
3117
3118static int
3119rb_scan_args_result(const struct rb_scan_args_t *const arg, int argc)
3120{
3121 const int n_lead = arg->n_lead;
3122 const int n_opt = arg->n_opt;
3123 const int n_trail = arg->n_trail;
3124 const int n_mand = n_lead + n_trail;
3125 const bool f_var = arg->f_var;
3126
3127 if (argc >= 0) {
3128 return argc;
3129 }
3130
3131 argc = -argc - 1;
3132 rb_error_arity(argc, n_mand, f_var ? UNLIMITED_ARGUMENTS : n_mand + n_opt);
3134}
3135
3136#undef rb_scan_args
3137int
3138rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...)
3139{
3140 va_list vargs;
3141 struct rb_scan_args_t arg;
3142 rb_scan_args_parse(RB_SCAN_ARGS_PASS_CALLED_KEYWORDS, fmt, &arg);
3143 va_start(vargs,fmt);
3144 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
3145 va_end(vargs);
3146 return rb_scan_args_result(&arg, argc);
3147}
3148
3149#undef rb_scan_args_kw
3150int
3151rb_scan_args_kw(int kw_flag, int argc, const VALUE *argv, const char *fmt, ...)
3152{
3153 va_list vargs;
3154 struct rb_scan_args_t arg;
3155 rb_scan_args_parse(kw_flag, fmt, &arg);
3156 va_start(vargs,fmt);
3157 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
3158 va_end(vargs);
3159 return rb_scan_args_result(&arg, argc);
3160}
3161
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
#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:877
static void RB_FL_SET_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_SET().
Definition fl_type.h:600
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:2414
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
Definition class.c:1701
VALUE rb_refinement_new(void)
Creates a new, anonymous refinement.
Definition class.c:1589
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition class.c:1484
VALUE rb_class_new(VALUE super)
Creates a new, anonymous class.
Definition class.c:855
static VALUE make_singleton_class(VALUE obj)
Creates a singleton class for obj.
Definition class.c:1305
VALUE rb_singleton_class_clone(VALUE obj)
Clones a singleton class.
Definition class.c:1143
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:1946
VALUE rb_class_subclasses(VALUE klass)
Queries the class's direct descendants.
Definition class.c:2194
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
Definition class.c:2800
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition class.c:1520
VALUE rb_class_attached_object(VALUE klass)
Returns the attached object for a singleton class.
Definition class.c:2217
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:2591
VALUE rb_module_new(void)
Creates a new, anonymous module.
Definition class.c:1583
#define META_CLASS_OF_CLASS_CLASS_P(k)
whether k is a meta^(n)-class of Class class
Definition class.c:1224
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:2399
void rb_check_inheritable(VALUE super)
Asserts that the given class can derive a child class.
Definition class.c:840
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:2452
static VALUE class_alloc0(enum ruby_value_type type, VALUE klass, bool namespaceable)
Allocates a struct RClass for a new class, iclass, or module.
Definition class.c:647
VALUE rb_class_boot(VALUE super)
A utility function that wraps class_alloc.
Definition class.c:773
VALUE rb_define_module(const char *name)
Defines a top-level module.
Definition class.c:1602
void rb_class_modify_check(VALUE klass)
Asserts that klass is not a frozen class.
Definition eval.c:420
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:1636
void rb_singleton_class_attached(VALUE klass, VALUE obj)
Attaches a singleton class to its corresponding object.
Definition class.c:1212
void rb_freeze_singleton_class(VALUE x)
This is an implementation detail of RB_OBJ_FREEZE().
Definition class.c:2765
VALUE rb_mod_included_modules(VALUE mod)
Queries the list of included modules.
Definition class.c:2012
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:1559
VALUE rb_mod_ancestors(VALUE mod)
Queries the module's ancestors.
Definition class.c:2080
static VALUE make_metaclass(VALUE klass)
Creates a metaclass of klass
Definition class.c:1269
VALUE rb_class_inherited(VALUE super, VALUE klass)
Calls Class::inherited.
Definition class.c:1475
VALUE rb_mod_include_p(VALUE mod, VALUE mod2)
Queries if the passed module is included by the module.
Definition class.c:2048
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:2437
#define ENSURE_EIGENCLASS(klass)
ensures klass belongs to its own eigenclass.
Definition class.c:1255
VALUE rb_mod_init_copy(VALUE clone, VALUE orig)
The comment that comes with this function says :nodoc:.
Definition class.c:1021
VALUE rb_define_module_under(VALUE outer, const char *name)
Defines a module under the namespace of outer.
Definition class.c:1630
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:2786
VALUE rb_define_module_id(ID id)
This is a very badly designed API that creates an anonymous module.
Definition class.c:1596
VALUE rb_define_class_id(ID id, VALUE super)
This is a very badly designed API that creates an anonymous class.
Definition class.c:1454
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition class.c:2848
VALUE rb_extract_keywords(VALUE *orighash)
Splits a hash into two.
Definition class.c:2909
void rb_define_attr(VALUE klass, const char *name, int read, int write)
Defines public accessor method(s) for an attribute.
Definition class.c:2854
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
Definition class.c:2668
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:3151
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:3138
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition eval.c:943
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
Definition class.c:2927
#define TYPE(_)
Old name of rb_type.
Definition value_type.h:108
#define FL_SINGLETON
Old name of RUBY_FL_SINGLETON.
Definition fl_type.h:58
#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:400
#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 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
Old name of RB_OBJ_FREEZE.
Definition fl_type.h:134
#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:402
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:205
#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:131
#define FL_SET
Old name of RB_FL_SET.
Definition fl_type.h:128
#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:130
#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:657
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
Definition fl_type.h:129
#define SYMBOL_P
Old name of RB_SYMBOL_P.
Definition value_type.h:88
#define OBJ_FROZEN_RAW
Old name of RB_OBJ_FROZEN_RAW.
Definition fl_type.h:137
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition eval.c:682
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1430
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:1481
VALUE rb_cClass
Class class.
Definition object.c:64
VALUE rb_mKernel
Kernel module.
Definition object.c:61
VALUE rb_cRefinement
Refinement class.
Definition object.c:65
VALUE rb_cNilClass
NilClass class.
Definition object.c:67
VALUE rb_cHash
Hash class.
Definition hash.c:113
VALUE rb_cFalseClass
FalseClass class.
Definition object.c:69
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:243
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
Definition object.c:657
VALUE rb_cBasicObject
BasicObject class.
Definition object.c:60
VALUE rb_cModule
Module class.
Definition object.c:63
VALUE rb_class_real(VALUE klass)
Finds a "real" class.
Definition object.c:233
VALUE rb_cTrueClass
TrueClass class.
Definition object.c:68
#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:615
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:603
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1117
#define RGENGC_WB_PROTECTED_CLASS
This is a compile-time flag to enable/disable write barrier for struct RClass.
Definition gc.h:523
VALUE rb_ary_new_from_values(long n, const VALUE *elts)
Identical to rb_ary_new_from_args(), except how objects are passed.
VALUE rb_ary_cat(VALUE ary, const VALUE *train, long len)
Destructively appends multiple elements at the end of the array.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_resize(VALUE ary, long len)
Expands or shrinks the passed array to the passed length.
VALUE rb_ary_hidden_new(long capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
#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:284
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
Definition proc.c:847
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:4102
#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:3623
void rb_const_set(VALUE space, ID name, VALUE val)
Names a constant.
Definition variable.c:4090
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:3629
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:417
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:3952
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
Definition variable.c:374
int rb_const_defined(VALUE space, ID name)
Queries if the constant is defined at the namespace.
Definition variable.c:3946
void rb_alias(VALUE klass, ID dst, ID src)
Resembles alias.
Definition vm_method.c:2423
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:2003
void rb_clear_constant_cache_for_id(ID id)
Clears the inline constant caches associated with a particular ID.
Definition vm_method.c:138
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
Definition symbol.h:284
int len
Length of the buffer.
Definition io.h:8
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
Definition memory.h:372
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:163
#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.c:2279
Definition constant.h:33
CREF (Class REFerence)
Definition method.h:45
Definition class.h:72
Definition method.h:55
rb_cref_t * cref
class reference, should be marked
Definition method.h:137
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
Definition method.h:136
Internal header for Namespace.
Definition namespace.h:14
Internal header for Class.
Definition class.h:30
Definition class.h:65
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:433
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376
ruby_value_type
C-level type of an object.
Definition value_type.h:113