Ruby 3.5.0dev (2025-07-22 revision e77eee96a3bd4ba737f6aee01acaffe795d7be60)
class.c (e77eee96a3bd4ba737f6aee01acaffe795d7be60)
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 rb_fields_tbl_copy(clone, orig);
984 }
985 if (RCLASS_CONST_TBL(orig)) {
986 struct clone_const_arg arg;
987 struct rb_id_table *const_tbl;
988 struct rb_id_table *orig_tbl = RCLASS_CONST_TBL(orig);
989 arg.tbl = const_tbl = rb_id_table_create(rb_id_table_size(orig_tbl));
990 arg.klass = clone;
991 rb_id_table_foreach(orig_tbl, clone_const_i, &arg);
992 RCLASS_WRITE_CONST_TBL(clone, const_tbl, false);
993 }
994}
995
996static bool ensure_origin(VALUE klass);
997
998void
999rb_class_set_initialized(VALUE klass)
1000{
1001 RUBY_ASSERT(RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_MODULE));
1002 FL_SET_RAW(klass, RCLASS_IS_INITIALIZED);
1003 /* no more re-initialization */
1004}
1005
1006void
1007rb_module_check_initializable(VALUE mod)
1008{
1009 if (RCLASS_INITIALIZED_P(mod)) {
1010 rb_raise(rb_eTypeError, "already initialized module");
1011 }
1012}
1013
1014/* :nodoc: */
1015VALUE
1017{
1018 /* Only class or module is valid here, but other classes may enter here and
1019 * only hit an exception on the OBJ_INIT_COPY checks
1020 */
1021 switch (BUILTIN_TYPE(clone)) {
1022 case T_CLASS:
1023 class_init_copy_check(clone, orig);
1024 break;
1025 case T_MODULE:
1026 rb_module_check_initializable(clone);
1027 break;
1028 default:
1029 break;
1030 }
1031 if (!OBJ_INIT_COPY(clone, orig)) return clone;
1032
1034 RUBY_ASSERT(BUILTIN_TYPE(clone) == BUILTIN_TYPE(orig));
1035
1036 rb_class_set_initialized(clone);
1037
1038 /* cloned flag is refer at constant inline cache
1039 * see vm_get_const_key_cref() in vm_insnhelper.c
1040 */
1041 RCLASS_SET_CLONED(clone, true);
1042 RCLASS_SET_CLONED(orig, true);
1043
1044 if (!RCLASS_SINGLETON_P(CLASS_OF(clone))) {
1045 RBASIC_SET_CLASS(clone, rb_singleton_class_clone(orig));
1046 rb_singleton_class_attached(METACLASS_OF(clone), (VALUE)clone);
1047 }
1048 if (BUILTIN_TYPE(clone) == T_CLASS) {
1049 RCLASS_SET_ALLOCATOR(clone, RCLASS_ALLOCATOR(orig));
1050 }
1051 copy_tables(clone, orig);
1052 if (RCLASS_M_TBL(orig)) {
1053 struct clone_method_arg arg;
1054 arg.old_klass = orig;
1055 arg.new_klass = clone;
1056 // TODO: use class_initialize_method_table() instead of RCLASS_SET_M_TBL_*
1057 // after RCLASS_SET_M_TBL is protected by write barrier
1058 RCLASS_SET_M_TBL_EVEN_WHEN_PROMOTED(clone, rb_id_table_create(0));
1059 rb_id_table_foreach(RCLASS_M_TBL(orig), clone_method_i, &arg);
1060 }
1061
1062 if (RCLASS_ORIGIN(orig) == orig) {
1063 rb_class_set_super(clone, RCLASS_SUPER(orig));
1064 }
1065 else {
1066 VALUE p = RCLASS_SUPER(orig);
1067 VALUE orig_origin = RCLASS_ORIGIN(orig);
1068 VALUE prev_clone_p = clone;
1069 VALUE origin_stack = rb_ary_hidden_new(2);
1070 VALUE origin[2];
1071 VALUE clone_p = 0;
1072 long origin_len;
1073 int add_subclass;
1074 VALUE clone_origin;
1075
1076 ensure_origin(clone);
1077 clone_origin = RCLASS_ORIGIN(clone);
1078
1079 while (p && p != orig_origin) {
1080 if (BUILTIN_TYPE(p) != T_ICLASS) {
1081 rb_bug("non iclass between module/class and origin");
1082 }
1083 clone_p = class_alloc(T_ICLASS, METACLASS_OF(p));
1084 /* We should set the m_tbl right after allocation before anything
1085 * that can trigger GC to avoid clone_p from becoming old and
1086 * needing to fire write barriers. */
1087 RCLASS_SET_M_TBL(clone_p, RCLASS_M_TBL(p));
1088 rb_class_set_super(prev_clone_p, clone_p);
1089 prev_clone_p = clone_p;
1090 RCLASS_SET_CONST_TBL(clone_p, RCLASS_CONST_TBL(p), false);
1091 if (RB_TYPE_P(clone, T_CLASS)) {
1092 RCLASS_SET_INCLUDER(clone_p, clone);
1093 }
1094 add_subclass = TRUE;
1095 if (p != RCLASS_ORIGIN(p)) {
1096 origin[0] = clone_p;
1097 origin[1] = RCLASS_ORIGIN(p);
1098 rb_ary_cat(origin_stack, origin, 2);
1099 }
1100 else if ((origin_len = RARRAY_LEN(origin_stack)) > 1 &&
1101 RARRAY_AREF(origin_stack, origin_len - 1) == p) {
1102 RCLASS_WRITE_ORIGIN(RARRAY_AREF(origin_stack, (origin_len -= 2)), clone_p);
1103 RICLASS_WRITE_ORIGIN_SHARED_MTBL(clone_p);
1104 rb_ary_resize(origin_stack, origin_len);
1105 add_subclass = FALSE;
1106 }
1107 if (add_subclass) {
1108 rb_module_add_to_subclasses_list(METACLASS_OF(p), clone_p);
1109 }
1110 p = RCLASS_SUPER(p);
1111 }
1112
1113 if (p == orig_origin) {
1114 if (clone_p) {
1115 rb_class_set_super(clone_p, clone_origin);
1116 rb_class_set_super(clone_origin, RCLASS_SUPER(orig_origin));
1117 }
1118 copy_tables(clone_origin, orig_origin);
1119 if (RCLASS_M_TBL(orig_origin)) {
1120 struct clone_method_arg arg;
1121 arg.old_klass = orig;
1122 arg.new_klass = clone;
1123 class_initialize_method_table(clone_origin);
1124 rb_id_table_foreach(RCLASS_M_TBL(orig_origin), clone_method_i, &arg);
1125 }
1126 }
1127 else {
1128 rb_bug("no origin for class that has origin");
1129 }
1130
1131 rb_class_update_superclasses(clone);
1132 }
1133
1134 return clone;
1135}
1136
1137VALUE
1139{
1140 return rb_singleton_class_clone_and_attach(obj, Qundef);
1141}
1142
1143// Clone and return the singleton class of `obj` if it has been created and is attached to `obj`.
1144VALUE
1145rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach)
1146{
1147 const VALUE klass = METACLASS_OF(obj);
1148
1149 // Note that `rb_singleton_class()` can create situations where `klass` is
1150 // attached to an object other than `obj`. In which case `obj` does not have
1151 // a material singleton class attached yet and there is no singleton class
1152 // to clone.
1153 if (!(RCLASS_SINGLETON_P(klass) && RCLASS_ATTACHED_OBJECT(klass) == obj)) {
1154 // nothing to clone
1155 return klass;
1156 }
1157 else {
1158 /* copy singleton(unnamed) class */
1159 bool klass_of_clone_is_new;
1160 RUBY_ASSERT(RB_TYPE_P(klass, T_CLASS));
1161 VALUE clone = class_alloc(T_CLASS, 0);
1162
1163 if (BUILTIN_TYPE(obj) == T_CLASS) {
1164 klass_of_clone_is_new = true;
1165 RBASIC_SET_CLASS(clone, clone);
1166 }
1167 else {
1168 VALUE klass_metaclass_clone = rb_singleton_class_clone(klass);
1169 // When `METACLASS_OF(klass) == klass_metaclass_clone`, it means the
1170 // recursive call did not clone `METACLASS_OF(klass)`.
1171 klass_of_clone_is_new = (METACLASS_OF(klass) != klass_metaclass_clone);
1172 RBASIC_SET_CLASS(clone, klass_metaclass_clone);
1173 }
1174
1175 // initialize method table before any GC chance
1176 class_initialize_method_table(clone);
1177
1178 rb_class_set_super(clone, RCLASS_SUPER(klass));
1179 rb_fields_tbl_copy(clone, klass);
1180 if (RCLASS_CONST_TBL(klass)) {
1181 struct clone_const_arg arg;
1182 struct rb_id_table *table;
1183 arg.tbl = table = rb_id_table_create(0);
1184 arg.klass = clone;
1185 rb_id_table_foreach(RCLASS_CONST_TBL(klass), clone_const_i, &arg);
1186 RCLASS_SET_CONST_TBL(clone, table, false);
1187 }
1188 if (!UNDEF_P(attach)) {
1189 rb_singleton_class_attached(clone, attach);
1190 }
1191 {
1192 struct clone_method_arg arg;
1193 arg.old_klass = klass;
1194 arg.new_klass = clone;
1195 rb_id_table_foreach(RCLASS_M_TBL(klass), clone_method_i, &arg);
1196 }
1197 if (klass_of_clone_is_new) {
1198 rb_singleton_class_attached(METACLASS_OF(clone), clone);
1199 }
1200 FL_SET(clone, FL_SINGLETON);
1201
1202 return clone;
1203 }
1204}
1205
1206void
1208{
1209 if (RCLASS_SINGLETON_P(klass)) {
1210 RCLASS_SET_ATTACHED_OBJECT(klass, obj);
1211 }
1212}
1213
1219#define META_CLASS_OF_CLASS_CLASS_P(k) (METACLASS_OF(k) == (k))
1220
1221static int
1222rb_singleton_class_has_metaclass_p(VALUE sklass)
1223{
1224 return RCLASS_ATTACHED_OBJECT(METACLASS_OF(sklass)) == sklass;
1225}
1226
1227int
1228rb_singleton_class_internal_p(VALUE sklass)
1229{
1230 return (RB_TYPE_P(RCLASS_ATTACHED_OBJECT(sklass), T_CLASS) &&
1231 !rb_singleton_class_has_metaclass_p(sklass));
1232}
1233
1239#define HAVE_METACLASS_P(k) \
1240 (FL_TEST(METACLASS_OF(k), FL_SINGLETON) && \
1241 rb_singleton_class_has_metaclass_p(k))
1242
1250#define ENSURE_EIGENCLASS(klass) \
1251 (HAVE_METACLASS_P(klass) ? METACLASS_OF(klass) : make_metaclass(klass))
1252
1253
1263static inline VALUE
1265{
1266 VALUE super;
1267 VALUE metaclass = class_boot_namespaceable(Qundef, FL_TEST_RAW(klass, RCLASS_NAMESPACEABLE));
1268
1269 FL_SET(metaclass, FL_SINGLETON);
1270 rb_singleton_class_attached(metaclass, klass);
1271
1272 if (META_CLASS_OF_CLASS_CLASS_P(klass)) {
1273 SET_METACLASS_OF(klass, metaclass);
1274 SET_METACLASS_OF(metaclass, metaclass);
1275 }
1276 else {
1277 VALUE tmp = METACLASS_OF(klass); /* for a meta^(n)-class klass, tmp is meta^(n)-class of Class class */
1278 SET_METACLASS_OF(klass, metaclass);
1279 SET_METACLASS_OF(metaclass, ENSURE_EIGENCLASS(tmp));
1280 }
1281
1282 super = RCLASS_SUPER(klass);
1283 while (RB_TYPE_P(super, T_ICLASS)) super = RCLASS_SUPER(super);
1284 class_associate_super(metaclass, super ? ENSURE_EIGENCLASS(super) : rb_cClass, true);
1285 rb_class_set_initialized(klass);
1286
1287 // Full class ancestry may not have been filled until we reach here.
1288 rb_class_update_superclasses(METACLASS_OF(metaclass));
1289
1290 return metaclass;
1291}
1292
1299static inline VALUE
1301{
1302 VALUE orig_class = METACLASS_OF(obj);
1303 VALUE klass = class_boot_namespaceable(orig_class, FL_TEST_RAW(orig_class, RCLASS_NAMESPACEABLE));
1304
1305 FL_SET(klass, FL_SINGLETON);
1306 RBASIC_SET_CLASS(obj, klass);
1307 rb_singleton_class_attached(klass, obj);
1308 rb_yjit_invalidate_no_singleton_class(orig_class);
1309
1310 SET_METACLASS_OF(klass, METACLASS_OF(rb_class_real(orig_class)));
1311 return klass;
1312}
1313
1314
1315static VALUE
1316boot_defclass(const char *name, VALUE super)
1317{
1318 VALUE obj = rb_class_boot(super);
1319 ID id = rb_intern(name);
1320
1321 rb_const_set((rb_cObject ? rb_cObject : obj), id, obj);
1322 rb_vm_register_global_object(obj);
1323 return obj;
1324}
1325
1326/***********************************************************************
1327 *
1328 * Document-class: Refinement
1329 *
1330 * Refinement is a class of the +self+ (current context) inside +refine+
1331 * statement. It allows to import methods from other modules, see #import_methods.
1332 */
1333
1334#if 0 /* for RDoc */
1335/*
1336 * Document-method: Refinement#import_methods
1337 *
1338 * call-seq:
1339 * import_methods(module, ...) -> self
1340 *
1341 * Imports methods from modules. Unlike Module#include,
1342 * Refinement#import_methods copies methods and adds them into the refinement,
1343 * so the refinement is activated in the imported methods.
1344 *
1345 * Note that due to method copying, only methods defined in Ruby code can be imported.
1346 *
1347 * module StrUtils
1348 * def indent(level)
1349 * ' ' * level + self
1350 * end
1351 * end
1352 *
1353 * module M
1354 * refine String do
1355 * import_methods StrUtils
1356 * end
1357 * end
1358 *
1359 * using M
1360 * "foo".indent(3)
1361 * #=> " foo"
1362 *
1363 * module M
1364 * refine String do
1365 * import_methods Enumerable
1366 * # Can't import method which is not defined with Ruby code: Enumerable#drop
1367 * end
1368 * end
1369 *
1370 */
1371
1372static VALUE
1373refinement_import_methods(int argc, VALUE *argv, VALUE refinement)
1374{
1375}
1376# endif
1377
1397void
1398Init_class_hierarchy(void)
1399{
1400 rb_cBasicObject = boot_defclass("BasicObject", 0);
1401 rb_cObject = boot_defclass("Object", rb_cBasicObject);
1402 rb_vm_register_global_object(rb_cObject);
1403
1404 /* resolve class name ASAP for order-independence */
1405 rb_set_class_path_string(rb_cObject, rb_cObject, rb_fstring_lit("Object"));
1406
1407 rb_cModule = boot_defclass("Module", rb_cObject);
1408 rb_cClass = boot_defclass("Class", rb_cModule);
1409 rb_cRefinement = boot_defclass("Refinement", rb_cModule);
1410
1411#if 0 /* for RDoc */
1412 // we pretend it to be public, otherwise RDoc will ignore it
1413 rb_define_method(rb_cRefinement, "import_methods", refinement_import_methods, -1);
1414#endif
1415
1416 rb_const_set(rb_cObject, rb_intern_const("BasicObject"), rb_cBasicObject);
1417 RBASIC_SET_CLASS(rb_cClass, rb_cClass);
1418 RBASIC_SET_CLASS(rb_cModule, rb_cClass);
1419 RBASIC_SET_CLASS(rb_cObject, rb_cClass);
1420 RBASIC_SET_CLASS(rb_cRefinement, rb_cClass);
1421 RBASIC_SET_CLASS(rb_cBasicObject, rb_cClass);
1422
1424}
1425
1426
1437VALUE
1438rb_make_metaclass(VALUE obj, VALUE unused)
1439{
1440 if (BUILTIN_TYPE(obj) == T_CLASS) {
1441 return make_metaclass(obj);
1442 }
1443 else {
1444 return make_singleton_class(obj);
1445 }
1446}
1447
1448VALUE
1450{
1451 VALUE klass;
1452
1453 if (!super) super = rb_cObject;
1454 klass = rb_class_new(super);
1455 rb_make_metaclass(klass, METACLASS_OF(super));
1456
1457 return klass;
1458}
1459
1460
1469VALUE
1471{
1472 ID inherited;
1473 if (!super) super = rb_cObject;
1474 CONST_ID(inherited, "inherited");
1475 return rb_funcall(super, inherited, 1, klass);
1476}
1477
1478VALUE
1479rb_define_class(const char *name, VALUE super)
1480{
1481 VALUE klass;
1482 ID id;
1483 const rb_namespace_t *ns = rb_current_namespace();
1484
1485 id = rb_intern(name);
1486 if (NAMESPACE_OPTIONAL_P(ns)) {
1487 return rb_define_class_id_under(ns->ns_object, id, super);
1488 }
1489 if (rb_const_defined(rb_cObject, id)) {
1490 klass = rb_const_get(rb_cObject, id);
1491 if (!RB_TYPE_P(klass, T_CLASS)) {
1492 rb_raise(rb_eTypeError, "%s is not a class (%"PRIsVALUE")",
1493 name, rb_obj_class(klass));
1494 }
1495 if (rb_class_real(RCLASS_SUPER(klass)) != super) {
1496 rb_raise(rb_eTypeError, "superclass mismatch for class %s", name);
1497 }
1498
1499 /* Class may have been defined in Ruby and not pin-rooted */
1500 rb_vm_register_global_object(klass);
1501 return klass;
1502 }
1503 if (!super) {
1504 rb_raise(rb_eArgError, "no super class for '%s'", name);
1505 }
1506 klass = rb_define_class_id(id, super);
1507 rb_vm_register_global_object(klass);
1508 rb_const_set(rb_cObject, id, klass);
1509 rb_class_inherited(super, klass);
1510
1511 return klass;
1512}
1513
1514VALUE
1515rb_define_class_under(VALUE outer, const char *name, VALUE super)
1516{
1517 return rb_define_class_id_under(outer, rb_intern(name), super);
1518}
1519
1520VALUE
1521rb_define_class_id_under_no_pin(VALUE outer, ID id, VALUE super)
1522{
1523 VALUE klass;
1524
1525 if (rb_const_defined_at(outer, id)) {
1526 klass = rb_const_get_at(outer, id);
1527 if (!RB_TYPE_P(klass, T_CLASS)) {
1528 rb_raise(rb_eTypeError, "%"PRIsVALUE"::%"PRIsVALUE" is not a class"
1529 " (%"PRIsVALUE")",
1530 outer, rb_id2str(id), rb_obj_class(klass));
1531 }
1532 if (rb_class_real(RCLASS_SUPER(klass)) != super) {
1533 rb_raise(rb_eTypeError, "superclass mismatch for class "
1534 "%"PRIsVALUE"::%"PRIsVALUE""
1535 " (%"PRIsVALUE" is given but was %"PRIsVALUE")",
1536 outer, rb_id2str(id), RCLASS_SUPER(klass), super);
1537 }
1538
1539 return klass;
1540 }
1541 if (!super) {
1542 rb_raise(rb_eArgError, "no super class for '%"PRIsVALUE"::%"PRIsVALUE"'",
1543 rb_class_path(outer), rb_id2str(id));
1544 }
1545 klass = rb_define_class_id(id, super);
1546 rb_set_class_path_string(klass, outer, rb_id2str(id));
1547 rb_const_set(outer, id, klass);
1548 rb_class_inherited(super, klass);
1549
1550 return klass;
1551}
1552
1553VALUE
1555{
1556 VALUE klass = rb_define_class_id_under_no_pin(outer, id, super);
1557 rb_vm_register_global_object(klass);
1558 return klass;
1559}
1560
1561VALUE
1562rb_module_s_alloc(VALUE klass)
1563{
1564 VALUE mod = class_alloc(T_MODULE, klass);
1565 class_initialize_method_table(mod);
1566 return mod;
1567}
1568
1569static inline VALUE
1570module_new(VALUE klass)
1571{
1572 VALUE mdl = class_alloc(T_MODULE, klass);
1573 class_initialize_method_table(mdl);
1574 return (VALUE)mdl;
1575}
1576
1577VALUE
1579{
1580 return module_new(rb_cModule);
1581}
1582
1583VALUE
1585{
1586 return module_new(rb_cRefinement);
1587}
1588
1589// Kept for compatibility. Use rb_module_new() instead.
1590VALUE
1592{
1593 return rb_module_new();
1594}
1595
1596VALUE
1597rb_define_module(const char *name)
1598{
1599 VALUE module;
1600 ID id;
1601 const rb_namespace_t *ns = rb_current_namespace();
1602
1603 id = rb_intern(name);
1604 if (NAMESPACE_OPTIONAL_P(ns)) {
1605 return rb_define_module_id_under(ns->ns_object, id);
1606 }
1607 if (rb_const_defined(rb_cObject, id)) {
1608 module = rb_const_get(rb_cObject, id);
1609 if (!RB_TYPE_P(module, T_MODULE)) {
1610 rb_raise(rb_eTypeError, "%s is not a module (%"PRIsVALUE")",
1611 name, rb_obj_class(module));
1612 }
1613 /* Module may have been defined in Ruby and not pin-rooted */
1614 rb_vm_register_global_object(module);
1615 return module;
1616 }
1617 module = rb_module_new();
1618 rb_vm_register_global_object(module);
1619 rb_const_set(rb_cObject, id, module);
1620
1621 return module;
1622}
1623
1624VALUE
1625rb_define_module_under(VALUE outer, const char *name)
1626{
1627 return rb_define_module_id_under(outer, rb_intern(name));
1628}
1629
1630VALUE
1632{
1633 VALUE module;
1634
1635 if (rb_const_defined_at(outer, id)) {
1636 module = rb_const_get_at(outer, id);
1637 if (!RB_TYPE_P(module, T_MODULE)) {
1638 rb_raise(rb_eTypeError, "%"PRIsVALUE"::%"PRIsVALUE" is not a module"
1639 " (%"PRIsVALUE")",
1640 outer, rb_id2str(id), rb_obj_class(module));
1641 }
1642 /* Module may have been defined in Ruby and not pin-rooted */
1643 rb_vm_register_global_object(module);
1644 return module;
1645 }
1646 module = rb_module_new();
1647 rb_const_set(outer, id, module);
1648 rb_set_class_path_string(module, outer, rb_id2str(id));
1649 rb_vm_register_global_object(module);
1650
1651 return module;
1652}
1653
1654VALUE
1655rb_include_class_new(VALUE module, VALUE super)
1656{
1657 VALUE klass = class_alloc(T_ICLASS, rb_cClass);
1658
1659 RCLASS_SET_M_TBL(klass, RCLASS_WRITABLE_M_TBL(module));
1660
1661 RCLASS_SET_ORIGIN(klass, klass);
1662 if (BUILTIN_TYPE(module) == T_ICLASS) {
1663 module = METACLASS_OF(module);
1664 }
1665 RUBY_ASSERT(!RB_TYPE_P(module, T_ICLASS));
1666 if (RCLASS_WRITABLE_CONST_TBL(module)) {
1667 RCLASS_SET_CONST_TBL(klass, RCLASS_WRITABLE_CONST_TBL(module), true);
1668 }
1669 else {
1670 RCLASS_WRITE_CONST_TBL(module, rb_id_table_create(0), false);
1671 RCLASS_SET_CONST_TBL(klass, RCLASS_WRITABLE_CONST_TBL(module), true);
1672 }
1673
1674 RCLASS_SET_CVC_TBL(klass, RCLASS_WRITABLE_CVC_TBL(module));
1675
1676 class_associate_super(klass, super, true);
1677 RBASIC_SET_CLASS(klass, module);
1678
1679 return (VALUE)klass;
1680}
1681
1682static int include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super);
1683
1684static void
1685ensure_includable(VALUE klass, VALUE module)
1686{
1687 rb_class_modify_check(klass);
1688 Check_Type(module, T_MODULE);
1689 rb_class_set_initialized(module);
1690 if (!NIL_P(rb_refinement_module_get_refined_class(module))) {
1691 rb_raise(rb_eArgError, "refinement module is not allowed");
1692 }
1693}
1694
1695void
1697{
1698 int changed = 0;
1699
1700 ensure_includable(klass, module);
1701
1702 changed = include_modules_at(klass, RCLASS_ORIGIN(klass), module, TRUE);
1703 if (changed < 0)
1704 rb_raise(rb_eArgError, "cyclic include detected");
1705
1706 if (RB_TYPE_P(klass, T_MODULE)) {
1707 rb_subclass_entry_t *iclass = RCLASS_SUBCLASSES_FIRST(klass);
1708 while (iclass) {
1709 int do_include = 1;
1710 VALUE check_class = iclass->klass;
1711 /* During lazy sweeping, iclass->klass could be a dead object that
1712 * has not yet been swept. */
1713 if (!rb_objspace_garbage_object_p(check_class)) {
1714 while (check_class) {
1715 RUBY_ASSERT(!rb_objspace_garbage_object_p(check_class));
1716
1717 if (RB_TYPE_P(check_class, T_ICLASS) &&
1718 (METACLASS_OF(check_class) == module)) {
1719 do_include = 0;
1720 }
1721 check_class = RCLASS_SUPER(check_class);
1722 }
1723
1724 if (do_include) {
1725 include_modules_at(iclass->klass, RCLASS_ORIGIN(iclass->klass), module, TRUE);
1726 }
1727 }
1728
1729 iclass = iclass->next;
1730 }
1731 }
1732}
1733
1734static enum rb_id_table_iterator_result
1735add_refined_method_entry_i(ID key, VALUE value, void *data)
1736{
1737 rb_add_refined_method_entry((VALUE)data, key);
1738 return ID_TABLE_CONTINUE;
1739}
1740
1741static enum rb_id_table_iterator_result
1742clear_module_cache_i(ID id, VALUE val, void *data)
1743{
1744 VALUE klass = (VALUE)data;
1745 rb_clear_method_cache(klass, id);
1746 return ID_TABLE_CONTINUE;
1747}
1748
1749static bool
1750module_in_super_chain(const VALUE klass, VALUE module)
1751{
1752 struct rb_id_table *const klass_m_tbl = RCLASS_M_TBL(RCLASS_ORIGIN(klass));
1753 if (klass_m_tbl) {
1754 while (module) {
1755 if (klass_m_tbl == RCLASS_M_TBL(module))
1756 return true;
1757 module = RCLASS_SUPER(module);
1758 }
1759 }
1760 return false;
1761}
1762
1763// For each ID key in the class constant table, we're going to clear the VM's
1764// inline constant caches associated with it.
1765static enum rb_id_table_iterator_result
1766clear_constant_cache_i(ID id, VALUE value, void *data)
1767{
1769 return ID_TABLE_CONTINUE;
1770}
1771
1772static int
1773do_include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super, bool check_cyclic)
1774{
1775 VALUE p, iclass, origin_stack = 0;
1776 int method_changed = 0;
1777 long origin_len;
1778 VALUE klass_origin = RCLASS_ORIGIN(klass);
1779 VALUE original_klass = klass;
1780
1781 if (check_cyclic && module_in_super_chain(klass, module))
1782 return -1;
1783
1784 while (module) {
1785 int c_seen = FALSE;
1786 int superclass_seen = FALSE;
1787 struct rb_id_table *tbl;
1788
1789 if (klass == c) {
1790 c_seen = TRUE;
1791 }
1792 if (klass_origin != c || search_super) {
1793 /* ignore if the module included already in superclasses for include,
1794 * ignore if the module included before origin class for prepend
1795 */
1796 for (p = RCLASS_SUPER(klass); p; p = RCLASS_SUPER(p)) {
1797 int type = BUILTIN_TYPE(p);
1798 if (klass_origin == p && !search_super)
1799 break;
1800 if (c == p)
1801 c_seen = TRUE;
1802 if (type == T_ICLASS) {
1803 if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) {
1804 if (!superclass_seen && c_seen) {
1805 c = p; /* move insertion point */
1806 }
1807 goto skip;
1808 }
1809 }
1810 else if (type == T_CLASS) {
1811 superclass_seen = TRUE;
1812 }
1813 }
1814 }
1815
1816 VALUE super_class = RCLASS_SUPER(c);
1817
1818 // invalidate inline method cache
1819 RB_DEBUG_COUNTER_INC(cvar_include_invalidate);
1820 ruby_vm_global_cvar_state++;
1821 tbl = RCLASS_M_TBL(module);
1822 if (tbl && rb_id_table_size(tbl)) {
1823 if (search_super) { // include
1824 if (super_class && !RB_TYPE_P(super_class, T_MODULE)) {
1825 rb_id_table_foreach(tbl, clear_module_cache_i, (void *)super_class);
1826 }
1827 }
1828 else { // prepend
1829 if (!RB_TYPE_P(original_klass, T_MODULE)) {
1830 rb_id_table_foreach(tbl, clear_module_cache_i, (void *)original_klass);
1831 }
1832 }
1833 method_changed = 1;
1834 }
1835
1836 // setup T_ICLASS for the include/prepend module
1837 iclass = rb_include_class_new(module, super_class);
1838 c = rb_class_set_super(c, iclass);
1839 RCLASS_SET_INCLUDER(iclass, klass);
1840 if (module != RCLASS_ORIGIN(module)) {
1841 if (!origin_stack) origin_stack = rb_ary_hidden_new(2);
1842 VALUE origin[2] = {iclass, RCLASS_ORIGIN(module)};
1843 rb_ary_cat(origin_stack, origin, 2);
1844 }
1845 else if (origin_stack && (origin_len = RARRAY_LEN(origin_stack)) > 1 &&
1846 RARRAY_AREF(origin_stack, origin_len - 1) == module) {
1847 RCLASS_WRITE_ORIGIN(RARRAY_AREF(origin_stack, (origin_len -= 2)), iclass);
1848 RICLASS_WRITE_ORIGIN_SHARED_MTBL(iclass);
1849 rb_ary_resize(origin_stack, origin_len);
1850 }
1851
1852 VALUE m = module;
1853 if (BUILTIN_TYPE(m) == T_ICLASS) m = METACLASS_OF(m);
1854 rb_module_add_to_subclasses_list(m, iclass);
1855
1856 if (BUILTIN_TYPE(klass) == T_MODULE && FL_TEST(klass, RMODULE_IS_REFINEMENT)) {
1857 VALUE refined_class =
1858 rb_refinement_module_get_refined_class(klass);
1859
1860 rb_id_table_foreach(RCLASS_M_TBL(module), add_refined_method_entry_i, (void *)refined_class);
1862 }
1863
1864 tbl = RCLASS_CONST_TBL(module);
1865 if (tbl && rb_id_table_size(tbl))
1866 rb_id_table_foreach(tbl, clear_constant_cache_i, NULL);
1867 skip:
1868 module = RCLASS_SUPER(module);
1869 }
1870
1871 return method_changed;
1872}
1873
1874static int
1875include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super)
1876{
1877 return do_include_modules_at(klass, c, module, search_super, true);
1878}
1879
1880static enum rb_id_table_iterator_result
1881move_refined_method(ID key, VALUE value, void *data)
1882{
1883 rb_method_entry_t *me = (rb_method_entry_t *)value;
1884
1885 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1886 VALUE klass = (VALUE)data;
1887 struct rb_id_table *tbl = RCLASS_WRITABLE_M_TBL(klass);
1888
1889 if (me->def->body.refined.orig_me) {
1890 const rb_method_entry_t *orig_me = me->def->body.refined.orig_me, *new_me;
1891 RB_OBJ_WRITE(me, &me->def->body.refined.orig_me, NULL);
1892 new_me = rb_method_entry_clone(me);
1893 rb_method_table_insert(klass, tbl, key, new_me);
1894 rb_method_entry_copy(me, orig_me);
1895 return ID_TABLE_CONTINUE;
1896 }
1897 else {
1898 rb_method_table_insert(klass, tbl, key, me);
1899 return ID_TABLE_DELETE;
1900 }
1901 }
1902 else {
1903 return ID_TABLE_CONTINUE;
1904 }
1905}
1906
1907static enum rb_id_table_iterator_result
1908cache_clear_refined_method(ID key, VALUE value, void *data)
1909{
1910 rb_method_entry_t *me = (rb_method_entry_t *) value;
1911
1912 if (me->def->type == VM_METHOD_TYPE_REFINED && me->def->body.refined.orig_me) {
1913 VALUE klass = (VALUE)data;
1914 rb_clear_method_cache(klass, me->called_id);
1915 }
1916 // Refined method entries without an orig_me is going to stay in the method
1917 // table of klass, like before the move, so no need to clear the cache.
1918
1919 return ID_TABLE_CONTINUE;
1920}
1921
1922static bool
1923ensure_origin(VALUE klass)
1924{
1925 VALUE origin = RCLASS_ORIGIN(klass);
1926 if (origin == klass) {
1927 origin = class_alloc(T_ICLASS, klass);
1928 RCLASS_SET_M_TBL(origin, RCLASS_M_TBL(klass));
1929 rb_class_set_super(origin, RCLASS_SUPER(klass));
1930 rb_class_set_super(klass, origin); // writes origin into RCLASS_SUPER(klass)
1931 RCLASS_WRITE_ORIGIN(klass, origin);
1932 class_clear_method_table(klass);
1933 rb_id_table_foreach(RCLASS_M_TBL(origin), cache_clear_refined_method, (void *)klass);
1934 rb_id_table_foreach(RCLASS_M_TBL(origin), move_refined_method, (void *)klass);
1935 return true;
1936 }
1937 return false;
1938}
1939
1940void
1942{
1943 int changed;
1944 bool klass_had_no_origin;
1945
1946 ensure_includable(klass, module);
1947 if (module_in_super_chain(klass, module))
1948 rb_raise(rb_eArgError, "cyclic prepend detected");
1949
1950 klass_had_no_origin = ensure_origin(klass);
1951 changed = do_include_modules_at(klass, klass, module, FALSE, false);
1952 RUBY_ASSERT(changed >= 0); // already checked for cyclic prepend above
1953 if (changed) {
1954 rb_vm_check_redefinition_by_prepend(klass);
1955 }
1956 if (RB_TYPE_P(klass, T_MODULE)) {
1957 rb_subclass_entry_t *iclass = RCLASS_SUBCLASSES_FIRST(klass);
1958 VALUE klass_origin = RCLASS_ORIGIN(klass);
1959 struct rb_id_table *klass_m_tbl = RCLASS_M_TBL(klass);
1960 struct rb_id_table *klass_origin_m_tbl = RCLASS_M_TBL(klass_origin);
1961 while (iclass) {
1962 /* During lazy sweeping, iclass->klass could be a dead object that
1963 * has not yet been swept. */
1964 if (!rb_objspace_garbage_object_p(iclass->klass)) {
1965 const VALUE subclass = iclass->klass;
1966 if (klass_had_no_origin && klass_origin_m_tbl == RCLASS_M_TBL(subclass)) {
1967 // backfill an origin iclass to handle refinements and future prepends
1968 rb_id_table_foreach(RCLASS_M_TBL(subclass), clear_module_cache_i, (void *)subclass);
1969 RCLASS_WRITE_M_TBL_EVEN_WHEN_PROMOTED(subclass, klass_m_tbl);
1970 VALUE origin = rb_include_class_new(klass_origin, RCLASS_SUPER(subclass));
1971 rb_class_set_super(subclass, origin);
1972 RCLASS_SET_INCLUDER(origin, RCLASS_INCLUDER(subclass));
1973 RCLASS_WRITE_ORIGIN(subclass, origin);
1974 RICLASS_SET_ORIGIN_SHARED_MTBL(origin);
1975 }
1976 include_modules_at(subclass, subclass, module, FALSE);
1977 }
1978
1979 iclass = iclass->next;
1980 }
1981 }
1982}
1983
1984/*
1985 * call-seq:
1986 * mod.included_modules -> array
1987 *
1988 * Returns the list of modules included or prepended in <i>mod</i>
1989 * or one of <i>mod</i>'s ancestors.
1990 *
1991 * module Sub
1992 * end
1993 *
1994 * module Mixin
1995 * prepend Sub
1996 * end
1997 *
1998 * module Outer
1999 * include Mixin
2000 * end
2001 *
2002 * Mixin.included_modules #=> [Sub]
2003 * Outer.included_modules #=> [Sub, Mixin]
2004 */
2005
2006VALUE
2008{
2009 VALUE ary = rb_ary_new();
2010 VALUE p;
2011 VALUE origin = RCLASS_ORIGIN(mod);
2012
2013 for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
2014 if (p != origin && RCLASS_ORIGIN(p) == p && BUILTIN_TYPE(p) == T_ICLASS) {
2015 VALUE m = METACLASS_OF(p);
2016 if (RB_TYPE_P(m, T_MODULE))
2017 rb_ary_push(ary, m);
2018 }
2019 }
2020 return ary;
2021}
2022
2023/*
2024 * call-seq:
2025 * mod.include?(module) -> true or false
2026 *
2027 * Returns <code>true</code> if <i>module</i> is included
2028 * or prepended in <i>mod</i> or one of <i>mod</i>'s ancestors.
2029 *
2030 * module A
2031 * end
2032 * class B
2033 * include A
2034 * end
2035 * class C < B
2036 * end
2037 * B.include?(A) #=> true
2038 * C.include?(A) #=> true
2039 * A.include?(A) #=> false
2040 */
2041
2042VALUE
2044{
2045 VALUE p;
2046
2047 Check_Type(mod2, T_MODULE);
2048 for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
2049 if (BUILTIN_TYPE(p) == T_ICLASS && !RICLASS_IS_ORIGIN_P(p)) {
2050 if (METACLASS_OF(p) == mod2) return Qtrue;
2051 }
2052 }
2053 return Qfalse;
2054}
2055
2056/*
2057 * call-seq:
2058 * mod.ancestors -> array
2059 *
2060 * Returns a list of modules included/prepended in <i>mod</i>
2061 * (including <i>mod</i> itself).
2062 *
2063 * module Mod
2064 * include Math
2065 * include Comparable
2066 * prepend Enumerable
2067 * end
2068 *
2069 * Mod.ancestors #=> [Enumerable, Mod, Comparable, Math]
2070 * Math.ancestors #=> [Math]
2071 * Enumerable.ancestors #=> [Enumerable]
2072 */
2073
2074VALUE
2076{
2077 VALUE p, ary = rb_ary_new();
2078 VALUE refined_class = Qnil;
2079 if (BUILTIN_TYPE(mod) == T_MODULE && FL_TEST(mod, RMODULE_IS_REFINEMENT)) {
2080 refined_class = rb_refinement_module_get_refined_class(mod);
2081 }
2082
2083 for (p = mod; p; p = RCLASS_SUPER(p)) {
2084 if (p == refined_class) break;
2085 if (p != RCLASS_ORIGIN(p)) continue;
2086 if (BUILTIN_TYPE(p) == T_ICLASS) {
2087 rb_ary_push(ary, METACLASS_OF(p));
2088 }
2089 else {
2090 rb_ary_push(ary, p);
2091 }
2092 }
2093 return ary;
2094}
2095
2097{
2098 VALUE buffer;
2099 long count;
2100 long maxcount;
2101 bool immediate_only;
2102};
2103
2104static void
2105class_descendants_recursive(VALUE klass, VALUE v)
2106{
2107 struct subclass_traverse_data *data = (struct subclass_traverse_data *) v;
2108
2109 if (BUILTIN_TYPE(klass) == T_CLASS && !RCLASS_SINGLETON_P(klass)) {
2110 if (data->buffer && data->count < data->maxcount && !rb_objspace_garbage_object_p(klass)) {
2111 // assumes that this does not cause GC as long as the length does not exceed the capacity
2112 rb_ary_push(data->buffer, klass);
2113 }
2114 data->count++;
2115 if (!data->immediate_only) {
2116 rb_class_foreach_subclass(klass, class_descendants_recursive, v);
2117 }
2118 }
2119 else {
2120 rb_class_foreach_subclass(klass, class_descendants_recursive, v);
2121 }
2122}
2123
2124static VALUE
2125class_descendants(VALUE klass, bool immediate_only)
2126{
2127 struct subclass_traverse_data data = { Qfalse, 0, -1, immediate_only };
2128
2129 // estimate the count of subclasses
2130 rb_class_foreach_subclass(klass, class_descendants_recursive, (VALUE) &data);
2131
2132 // the following allocation may cause GC which may change the number of subclasses
2133 data.buffer = rb_ary_new_capa(data.count);
2134 data.maxcount = data.count;
2135 data.count = 0;
2136
2137 size_t gc_count = rb_gc_count();
2138
2139 // enumerate subclasses
2140 rb_class_foreach_subclass(klass, class_descendants_recursive, (VALUE) &data);
2141
2142 if (gc_count != rb_gc_count()) {
2143 rb_bug("GC must not occur during the subclass iteration of Class#descendants");
2144 }
2145
2146 return data.buffer;
2147}
2148
2149/*
2150 * call-seq:
2151 * subclasses -> array
2152 *
2153 * Returns an array of classes where the receiver is the
2154 * direct superclass of the class, excluding singleton classes.
2155 * The order of the returned array is not defined.
2156 *
2157 * class A; end
2158 * class B < A; end
2159 * class C < B; end
2160 * class D < A; end
2161 *
2162 * A.subclasses #=> [D, B]
2163 * B.subclasses #=> [C]
2164 * C.subclasses #=> []
2165 *
2166 * Anonymous subclasses (not associated with a constant) are
2167 * returned, too:
2168 *
2169 * c = Class.new(A)
2170 * A.subclasses # => [#<Class:0x00007f003c77bd78>, D, B]
2171 *
2172 * Note that the parent does not hold references to subclasses
2173 * and doesn't prevent them from being garbage collected. This
2174 * means that the subclass might disappear when all references
2175 * to it are dropped:
2176 *
2177 * # drop the reference to subclass, it can be garbage-collected now
2178 * c = nil
2179 *
2180 * A.subclasses
2181 * # It can be
2182 * # => [#<Class:0x00007f003c77bd78>, D, B]
2183 * # ...or just
2184 * # => [D, B]
2185 * # ...depending on whether garbage collector was run
2186 */
2187
2188VALUE
2190{
2191 return class_descendants(klass, true);
2192}
2193
2194/*
2195 * call-seq:
2196 * attached_object -> object
2197 *
2198 * Returns the object for which the receiver is the singleton class.
2199 *
2200 * Raises an TypeError if the class is not a singleton class.
2201 *
2202 * class Foo; end
2203 *
2204 * Foo.singleton_class.attached_object #=> Foo
2205 * Foo.attached_object #=> TypeError: `Foo' is not a singleton class
2206 * Foo.new.singleton_class.attached_object #=> #<Foo:0x000000010491a370>
2207 * TrueClass.attached_object #=> TypeError: `TrueClass' is not a singleton class
2208 * NilClass.attached_object #=> TypeError: `NilClass' is not a singleton class
2209 */
2210
2211VALUE
2213{
2214 if (!RCLASS_SINGLETON_P(klass)) {
2215 rb_raise(rb_eTypeError, "'%"PRIsVALUE"' is not a singleton class", klass);
2216 }
2217
2218 return RCLASS_ATTACHED_OBJECT(klass);
2219}
2220
2221static void
2222ins_methods_push(st_data_t name, st_data_t ary)
2223{
2224 rb_ary_push((VALUE)ary, ID2SYM((ID)name));
2225}
2226
2227static int
2228ins_methods_i(st_data_t name, st_data_t type, st_data_t ary)
2229{
2230 switch ((rb_method_visibility_t)type) {
2231 case METHOD_VISI_UNDEF:
2232 case METHOD_VISI_PRIVATE:
2233 break;
2234 default: /* everything but private */
2235 ins_methods_push(name, ary);
2236 break;
2237 }
2238 return ST_CONTINUE;
2239}
2240
2241static int
2242ins_methods_type_i(st_data_t name, st_data_t type, st_data_t ary, rb_method_visibility_t visi)
2243{
2244 if ((rb_method_visibility_t)type == visi) {
2245 ins_methods_push(name, ary);
2246 }
2247 return ST_CONTINUE;
2248}
2249
2250static int
2251ins_methods_prot_i(st_data_t name, st_data_t type, st_data_t ary)
2252{
2253 return ins_methods_type_i(name, type, ary, METHOD_VISI_PROTECTED);
2254}
2255
2256static int
2257ins_methods_priv_i(st_data_t name, st_data_t type, st_data_t ary)
2258{
2259 return ins_methods_type_i(name, type, ary, METHOD_VISI_PRIVATE);
2260}
2261
2262static int
2263ins_methods_pub_i(st_data_t name, st_data_t type, st_data_t ary)
2264{
2265 return ins_methods_type_i(name, type, ary, METHOD_VISI_PUBLIC);
2266}
2267
2268static int
2269ins_methods_undef_i(st_data_t name, st_data_t type, st_data_t ary)
2270{
2271 return ins_methods_type_i(name, type, ary, METHOD_VISI_UNDEF);
2272}
2273
2275 st_table *list;
2276 int recur;
2277};
2278
2279static enum rb_id_table_iterator_result
2280method_entry_i(ID key, VALUE value, void *data)
2281{
2282 const rb_method_entry_t *me = (const rb_method_entry_t *)value;
2283 struct method_entry_arg *arg = (struct method_entry_arg *)data;
2284 rb_method_visibility_t type;
2285
2286 if (me->def->type == VM_METHOD_TYPE_REFINED) {
2287 VALUE owner = me->owner;
2288 me = rb_resolve_refined_method(Qnil, me);
2289 if (!me) return ID_TABLE_CONTINUE;
2290 if (!arg->recur && me->owner != owner) return ID_TABLE_CONTINUE;
2291 }
2292 if (!st_is_member(arg->list, key)) {
2293 if (UNDEFINED_METHOD_ENTRY_P(me)) {
2294 type = METHOD_VISI_UNDEF; /* none */
2295 }
2296 else {
2297 type = METHOD_ENTRY_VISI(me);
2298 RUBY_ASSERT(type != METHOD_VISI_UNDEF);
2299 }
2300 st_add_direct(arg->list, key, (st_data_t)type);
2301 }
2302 return ID_TABLE_CONTINUE;
2303}
2304
2305static void
2306add_instance_method_list(VALUE mod, struct method_entry_arg *me_arg)
2307{
2308 struct rb_id_table *m_tbl = RCLASS_M_TBL(mod);
2309 if (!m_tbl) return;
2310 rb_id_table_foreach(m_tbl, method_entry_i, me_arg);
2311}
2312
2313static bool
2314particular_class_p(VALUE mod)
2315{
2316 if (!mod) return false;
2317 if (RCLASS_SINGLETON_P(mod)) return true;
2318 if (BUILTIN_TYPE(mod) == T_ICLASS) return true;
2319 return false;
2320}
2321
2322static VALUE
2323class_instance_method_list(int argc, const VALUE *argv, VALUE mod, int obj, int (*func) (st_data_t, st_data_t, st_data_t))
2324{
2325 VALUE ary;
2326 int recur = TRUE, prepended = 0;
2327 struct method_entry_arg me_arg;
2328
2329 if (rb_check_arity(argc, 0, 1)) recur = RTEST(argv[0]);
2330
2331 me_arg.list = st_init_numtable();
2332 me_arg.recur = recur;
2333
2334 if (obj) {
2335 for (; particular_class_p(mod); mod = RCLASS_SUPER(mod)) {
2336 add_instance_method_list(mod, &me_arg);
2337 }
2338 }
2339
2340 if (!recur && RCLASS_ORIGIN(mod) != mod) {
2341 mod = RCLASS_ORIGIN(mod);
2342 prepended = 1;
2343 }
2344
2345 for (; mod; mod = RCLASS_SUPER(mod)) {
2346 add_instance_method_list(mod, &me_arg);
2347 if (BUILTIN_TYPE(mod) == T_ICLASS && !prepended) continue;
2348 if (!recur) break;
2349 }
2350 ary = rb_ary_new2(me_arg.list->num_entries);
2351 st_foreach(me_arg.list, func, ary);
2352 st_free_table(me_arg.list);
2353
2354 return ary;
2355}
2356
2357/*
2358 * call-seq:
2359 * mod.instance_methods(include_super=true) -> array
2360 *
2361 * Returns an array containing the names of the public and protected instance
2362 * methods in the receiver. For a module, these are the public and protected methods;
2363 * for a class, they are the instance (not singleton) methods. If the optional
2364 * parameter is <code>false</code>, the methods of any ancestors are not included.
2365 *
2366 * module A
2367 * def method1() end
2368 * end
2369 * class B
2370 * include A
2371 * def method2() end
2372 * end
2373 * class C < B
2374 * def method3() end
2375 * end
2376 *
2377 * A.instance_methods(false) #=> [:method1]
2378 * B.instance_methods(false) #=> [:method2]
2379 * B.instance_methods(true).include?(:method1) #=> true
2380 * C.instance_methods(false) #=> [:method3]
2381 * C.instance_methods.include?(:method2) #=> true
2382 *
2383 * Note that method visibility changes in the current class, as well as aliases,
2384 * are considered as methods of the current class by this method:
2385 *
2386 * class C < B
2387 * alias method4 method2
2388 * protected :method2
2389 * end
2390 * C.instance_methods(false).sort #=> [:method2, :method3, :method4]
2391 */
2392
2393VALUE
2394rb_class_instance_methods(int argc, const VALUE *argv, VALUE mod)
2395{
2396 return class_instance_method_list(argc, argv, mod, 0, ins_methods_i);
2397}
2398
2399/*
2400 * call-seq:
2401 * mod.protected_instance_methods(include_super=true) -> array
2402 *
2403 * Returns a list of the protected instance methods defined in
2404 * <i>mod</i>. If the optional parameter is <code>false</code>, the
2405 * methods of any ancestors are not included.
2406 */
2407
2408VALUE
2410{
2411 return class_instance_method_list(argc, argv, mod, 0, ins_methods_prot_i);
2412}
2413
2414/*
2415 * call-seq:
2416 * mod.private_instance_methods(include_super=true) -> array
2417 *
2418 * Returns a list of the private instance methods defined in
2419 * <i>mod</i>. If the optional parameter is <code>false</code>, the
2420 * methods of any ancestors are not included.
2421 *
2422 * module Mod
2423 * def method1() end
2424 * private :method1
2425 * def method2() end
2426 * end
2427 * Mod.instance_methods #=> [:method2]
2428 * Mod.private_instance_methods #=> [:method1]
2429 */
2430
2431VALUE
2433{
2434 return class_instance_method_list(argc, argv, mod, 0, ins_methods_priv_i);
2435}
2436
2437/*
2438 * call-seq:
2439 * mod.public_instance_methods(include_super=true) -> array
2440 *
2441 * Returns a list of the public instance methods defined in <i>mod</i>.
2442 * If the optional parameter is <code>false</code>, the methods of
2443 * any ancestors are not included.
2444 */
2445
2446VALUE
2448{
2449 return class_instance_method_list(argc, argv, mod, 0, ins_methods_pub_i);
2450}
2451
2452/*
2453 * call-seq:
2454 * mod.undefined_instance_methods -> array
2455 *
2456 * Returns a list of the undefined instance methods defined in <i>mod</i>.
2457 * The undefined methods of any ancestors are not included.
2458 */
2459
2460VALUE
2461rb_class_undefined_instance_methods(VALUE mod)
2462{
2463 VALUE include_super = Qfalse;
2464 return class_instance_method_list(1, &include_super, mod, 0, ins_methods_undef_i);
2465}
2466
2467/*
2468 * call-seq:
2469 * obj.methods(regular=true) -> array
2470 *
2471 * Returns a list of the names of public and protected methods of
2472 * <i>obj</i>. This will include all the methods accessible in
2473 * <i>obj</i>'s ancestors.
2474 * If the optional parameter is <code>false</code>, it
2475 * returns an array of <i>obj</i>'s public and protected singleton methods,
2476 * the array will not include methods in modules included in <i>obj</i>.
2477 *
2478 * class Klass
2479 * def klass_method()
2480 * end
2481 * end
2482 * k = Klass.new
2483 * k.methods[0..9] #=> [:klass_method, :nil?, :===,
2484 * # :==~, :!, :eql?
2485 * # :hash, :<=>, :class, :singleton_class]
2486 * k.methods.length #=> 56
2487 *
2488 * k.methods(false) #=> []
2489 * def k.singleton_method; end
2490 * k.methods(false) #=> [:singleton_method]
2491 *
2492 * module M123; def m123; end end
2493 * k.extend M123
2494 * k.methods(false) #=> [:singleton_method]
2495 */
2496
2497VALUE
2498rb_obj_methods(int argc, const VALUE *argv, VALUE obj)
2499{
2500 rb_check_arity(argc, 0, 1);
2501 if (argc > 0 && !RTEST(argv[0])) {
2502 return rb_obj_singleton_methods(argc, argv, obj);
2503 }
2504 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_i);
2505}
2506
2507/*
2508 * call-seq:
2509 * obj.protected_methods(all=true) -> array
2510 *
2511 * Returns the list of protected methods accessible to <i>obj</i>. If
2512 * the <i>all</i> parameter is set to <code>false</code>, only those methods
2513 * in the receiver will be listed.
2514 */
2515
2516VALUE
2517rb_obj_protected_methods(int argc, const VALUE *argv, VALUE obj)
2518{
2519 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_prot_i);
2520}
2521
2522/*
2523 * call-seq:
2524 * obj.private_methods(all=true) -> array
2525 *
2526 * Returns the list of private methods accessible to <i>obj</i>. If
2527 * the <i>all</i> parameter is set to <code>false</code>, only those methods
2528 * in the receiver will be listed.
2529 */
2530
2531VALUE
2532rb_obj_private_methods(int argc, const VALUE *argv, VALUE obj)
2533{
2534 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_priv_i);
2535}
2536
2537/*
2538 * call-seq:
2539 * obj.public_methods(all=true) -> array
2540 *
2541 * Returns the list of public methods accessible to <i>obj</i>. If
2542 * the <i>all</i> parameter is set to <code>false</code>, only those methods
2543 * in the receiver will be listed.
2544 */
2545
2546VALUE
2547rb_obj_public_methods(int argc, const VALUE *argv, VALUE obj)
2548{
2549 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_pub_i);
2550}
2551
2552/*
2553 * call-seq:
2554 * obj.singleton_methods(all=true) -> array
2555 *
2556 * Returns an array of the names of singleton methods for <i>obj</i>.
2557 * If the optional <i>all</i> parameter is true, the list will include
2558 * methods in modules included in <i>obj</i>.
2559 * Only public and protected singleton methods are returned.
2560 *
2561 * module Other
2562 * def three() end
2563 * end
2564 *
2565 * class Single
2566 * def Single.four() end
2567 * end
2568 *
2569 * a = Single.new
2570 *
2571 * def a.one()
2572 * end
2573 *
2574 * class << a
2575 * include Other
2576 * def two()
2577 * end
2578 * end
2579 *
2580 * Single.singleton_methods #=> [:four]
2581 * a.singleton_methods(false) #=> [:two, :one]
2582 * a.singleton_methods #=> [:two, :one, :three]
2583 */
2584
2585VALUE
2586rb_obj_singleton_methods(int argc, const VALUE *argv, VALUE obj)
2587{
2588 VALUE ary, klass, origin;
2589 struct method_entry_arg me_arg;
2590 struct rb_id_table *mtbl;
2591 int recur = TRUE;
2592
2593 if (rb_check_arity(argc, 0, 1)) recur = RTEST(argv[0]);
2594 if (RCLASS_SINGLETON_P(obj)) {
2595 rb_singleton_class(obj);
2596 }
2597 klass = CLASS_OF(obj);
2598 origin = RCLASS_ORIGIN(klass);
2599 me_arg.list = st_init_numtable();
2600 me_arg.recur = recur;
2601 if (klass && RCLASS_SINGLETON_P(klass)) {
2602 if ((mtbl = RCLASS_M_TBL(origin)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2603 klass = RCLASS_SUPER(klass);
2604 }
2605 if (recur) {
2606 while (klass && (RCLASS_SINGLETON_P(klass) || RB_TYPE_P(klass, T_ICLASS))) {
2607 if (klass != origin && (mtbl = RCLASS_M_TBL(klass)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2608 klass = RCLASS_SUPER(klass);
2609 }
2610 }
2611 ary = rb_ary_new2(me_arg.list->num_entries);
2612 st_foreach(me_arg.list, ins_methods_i, ary);
2613 st_free_table(me_arg.list);
2614
2615 return ary;
2616}
2617
2626#ifdef rb_define_method_id
2627#undef rb_define_method_id
2628#endif
2629void
2630rb_define_method_id(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc)
2631{
2632 rb_add_method_cfunc(klass, mid, func, argc, METHOD_VISI_PUBLIC);
2633}
2634
2635#ifdef rb_define_method
2636#undef rb_define_method
2637#endif
2638void
2639rb_define_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
2640{
2641 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PUBLIC);
2642}
2643
2644#ifdef rb_define_protected_method
2645#undef rb_define_protected_method
2646#endif
2647void
2648rb_define_protected_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
2649{
2650 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PROTECTED);
2651}
2652
2653#ifdef rb_define_private_method
2654#undef rb_define_private_method
2655#endif
2656void
2657rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
2658{
2659 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PRIVATE);
2660}
2661
2662void
2663rb_undef_method(VALUE klass, const char *name)
2664{
2665 rb_add_method(klass, rb_intern(name), VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2666}
2667
2668static enum rb_id_table_iterator_result
2669undef_method_i(ID name, VALUE value, void *data)
2670{
2671 VALUE klass = (VALUE)data;
2672 rb_add_method(klass, name, VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2673 return ID_TABLE_CONTINUE;
2674}
2675
2676void
2677rb_undef_methods_from(VALUE klass, VALUE super)
2678{
2679 struct rb_id_table *mtbl = RCLASS_M_TBL(super);
2680 if (mtbl) {
2681 rb_id_table_foreach(mtbl, undef_method_i, (void *)klass);
2682 }
2683}
2684
2693static inline VALUE
2694special_singleton_class_of(VALUE obj)
2695{
2696 switch (obj) {
2697 case Qnil: return rb_cNilClass;
2698 case Qfalse: return rb_cFalseClass;
2699 case Qtrue: return rb_cTrueClass;
2700 default: return Qnil;
2701 }
2702}
2703
2704VALUE
2705rb_special_singleton_class(VALUE obj)
2706{
2707 return special_singleton_class_of(obj);
2708}
2709
2719static VALUE
2720singleton_class_of(VALUE obj)
2721{
2722 VALUE klass;
2723
2724 switch (TYPE(obj)) {
2725 case T_FIXNUM:
2726 case T_BIGNUM:
2727 case T_FLOAT:
2728 case T_SYMBOL:
2729 rb_raise(rb_eTypeError, "can't define singleton");
2730
2731 case T_FALSE:
2732 case T_TRUE:
2733 case T_NIL:
2734 klass = special_singleton_class_of(obj);
2735 if (NIL_P(klass))
2736 rb_bug("unknown immediate %p", (void *)obj);
2737 return klass;
2738
2739 case T_STRING:
2740 if (CHILLED_STRING_P(obj)) {
2741 CHILLED_STRING_MUTATED(obj);
2742 }
2743 else if (FL_TEST_RAW(obj, RSTRING_FSTR)) {
2744 rb_raise(rb_eTypeError, "can't define singleton");
2745 }
2746 }
2747
2748 klass = METACLASS_OF(obj);
2749 if (!(RCLASS_SINGLETON_P(klass) &&
2750 RCLASS_ATTACHED_OBJECT(klass) == obj)) {
2751 klass = rb_make_metaclass(obj, klass);
2752 }
2753
2754 RB_FL_SET_RAW(klass, RB_OBJ_FROZEN_RAW(obj));
2755
2756 return klass;
2757}
2758
2759void
2761{
2762 /* should not propagate to meta-meta-class, and so on */
2763 if (!RCLASS_SINGLETON_P(x)) {
2764 VALUE klass = RBASIC_CLASS(x);
2765 if (klass && // no class when hidden from ObjectSpace
2766 FL_TEST_RAW(klass, FL_SINGLETON) &&
2767 !OBJ_FROZEN_RAW(klass)) {
2768 OBJ_FREEZE(klass);
2769 }
2770 }
2771}
2772
2780VALUE
2782{
2783 VALUE klass;
2784
2785 if (SPECIAL_CONST_P(obj)) {
2786 return rb_special_singleton_class(obj);
2787 }
2788 klass = METACLASS_OF(obj);
2789 if (!RCLASS_SINGLETON_P(klass)) return Qnil;
2790 if (RCLASS_ATTACHED_OBJECT(klass) != obj) return Qnil;
2791 return klass;
2792}
2793
2794VALUE
2796{
2797 VALUE klass = singleton_class_of(obj);
2798
2799 /* ensures an exposed class belongs to its own eigenclass */
2800 if (RB_TYPE_P(obj, T_CLASS)) (void)ENSURE_EIGENCLASS(klass);
2801
2802 return klass;
2803}
2804
2814#ifdef rb_define_singleton_method
2815#undef rb_define_singleton_method
2816#endif
2817void
2818rb_define_singleton_method(VALUE obj, const char *name, VALUE (*func)(ANYARGS), int argc)
2819{
2820 rb_define_method(singleton_class_of(obj), name, func, argc);
2821}
2822
2823#ifdef rb_define_module_function
2824#undef rb_define_module_function
2825#endif
2826void
2827rb_define_module_function(VALUE module, const char *name, VALUE (*func)(ANYARGS), int argc)
2828{
2829 rb_define_private_method(module, name, func, argc);
2830 rb_define_singleton_method(module, name, func, argc);
2831}
2832
2833#ifdef rb_define_global_function
2834#undef rb_define_global_function
2835#endif
2836void
2837rb_define_global_function(const char *name, VALUE (*func)(ANYARGS), int argc)
2838{
2839 rb_define_module_function(rb_mKernel, name, func, argc);
2840}
2841
2842void
2843rb_define_alias(VALUE klass, const char *name1, const char *name2)
2844{
2845 rb_alias(klass, rb_intern(name1), rb_intern(name2));
2846}
2847
2848void
2849rb_define_attr(VALUE klass, const char *name, int read, int write)
2850{
2851 rb_attr(klass, rb_intern(name), read, write, FALSE);
2852}
2853
2854VALUE
2855rb_keyword_error_new(const char *error, VALUE keys)
2856{
2857 long i = 0, len = RARRAY_LEN(keys);
2858 VALUE error_message = rb_sprintf("%s keyword%.*s", error, len > 1, "s");
2859
2860 if (len > 0) {
2861 rb_str_cat_cstr(error_message, ": ");
2862 while (1) {
2863 const VALUE k = RARRAY_AREF(keys, i);
2864 rb_str_append(error_message, rb_inspect(k));
2865 if (++i >= len) break;
2866 rb_str_cat_cstr(error_message, ", ");
2867 }
2868 }
2869
2870 return rb_exc_new_str(rb_eArgError, error_message);
2871}
2872
2873NORETURN(static void rb_keyword_error(const char *error, VALUE keys));
2874static void
2875rb_keyword_error(const char *error, VALUE keys)
2876{
2877 rb_exc_raise(rb_keyword_error_new(error, keys));
2878}
2879
2880NORETURN(static void unknown_keyword_error(VALUE hash, const ID *table, int keywords));
2881static void
2882unknown_keyword_error(VALUE hash, const ID *table, int keywords)
2883{
2884 int i;
2885 for (i = 0; i < keywords; i++) {
2886 st_data_t key = ID2SYM(table[i]);
2887 rb_hash_stlike_delete(hash, &key, NULL);
2888 }
2889 rb_keyword_error("unknown", rb_hash_keys(hash));
2890}
2891
2892
2893static int
2894separate_symbol(st_data_t key, st_data_t value, st_data_t arg)
2895{
2896 VALUE *kwdhash = (VALUE *)arg;
2897 if (!SYMBOL_P(key)) kwdhash++;
2898 if (!*kwdhash) *kwdhash = rb_hash_new();
2899 rb_hash_aset(*kwdhash, (VALUE)key, (VALUE)value);
2900 return ST_CONTINUE;
2901}
2902
2903VALUE
2905{
2906 VALUE parthash[2] = {0, 0};
2907 VALUE hash = *orighash;
2908
2909 if (RHASH_EMPTY_P(hash)) {
2910 *orighash = 0;
2911 return hash;
2912 }
2913 rb_hash_foreach(hash, separate_symbol, (st_data_t)&parthash);
2914 *orighash = parthash[1];
2915 if (parthash[1] && RBASIC_CLASS(hash) != rb_cHash) {
2916 RBASIC_SET_CLASS(parthash[1], RBASIC_CLASS(hash));
2917 }
2918 return parthash[0];
2919}
2920
2921int
2922rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
2923{
2924 int i = 0, j;
2925 int rest = 0;
2926 VALUE missing = Qnil;
2927 st_data_t key;
2928
2929#define extract_kwarg(keyword, val) \
2930 (key = (st_data_t)(keyword), values ? \
2931 (rb_hash_stlike_delete(keyword_hash, &key, &(val)) || ((val) = Qundef, 0)) : \
2932 rb_hash_stlike_lookup(keyword_hash, key, NULL))
2933
2934 if (NIL_P(keyword_hash)) keyword_hash = 0;
2935
2936 if (optional < 0) {
2937 rest = 1;
2938 optional = -1-optional;
2939 }
2940 if (required) {
2941 for (; i < required; i++) {
2942 VALUE keyword = ID2SYM(table[i]);
2943 if (keyword_hash) {
2944 if (extract_kwarg(keyword, values[i])) {
2945 continue;
2946 }
2947 }
2948 if (NIL_P(missing)) missing = rb_ary_hidden_new(1);
2949 rb_ary_push(missing, keyword);
2950 }
2951 if (!NIL_P(missing)) {
2952 rb_keyword_error("missing", missing);
2953 }
2954 }
2955 j = i;
2956 if (optional && keyword_hash) {
2957 for (i = 0; i < optional; i++) {
2958 if (extract_kwarg(ID2SYM(table[required+i]), values[required+i])) {
2959 j++;
2960 }
2961 }
2962 }
2963 if (!rest && keyword_hash) {
2964 if (RHASH_SIZE(keyword_hash) > (unsigned int)(values ? 0 : j)) {
2965 unknown_keyword_error(keyword_hash, table, required+optional);
2966 }
2967 }
2968 if (values && !keyword_hash) {
2969 for (i = 0; i < required + optional; i++) {
2970 values[i] = Qundef;
2971 }
2972 }
2973 return j;
2974#undef extract_kwarg
2975}
2976
2978 int kw_flag;
2979 int n_lead;
2980 int n_opt;
2981 int n_trail;
2982 bool f_var;
2983 bool f_hash;
2984 bool f_block;
2985};
2986
2987static void
2988rb_scan_args_parse(int kw_flag, const char *fmt, struct rb_scan_args_t *arg)
2989{
2990 const char *p = fmt;
2991
2992 memset(arg, 0, sizeof(*arg));
2993 arg->kw_flag = kw_flag;
2994
2995 if (ISDIGIT(*p)) {
2996 arg->n_lead = *p - '0';
2997 p++;
2998 if (ISDIGIT(*p)) {
2999 arg->n_opt = *p - '0';
3000 p++;
3001 }
3002 }
3003 if (*p == '*') {
3004 arg->f_var = 1;
3005 p++;
3006 }
3007 if (ISDIGIT(*p)) {
3008 arg->n_trail = *p - '0';
3009 p++;
3010 }
3011 if (*p == ':') {
3012 arg->f_hash = 1;
3013 p++;
3014 }
3015 if (*p == '&') {
3016 arg->f_block = 1;
3017 p++;
3018 }
3019 if (*p != '\0') {
3020 rb_fatal("bad scan arg format: %s", fmt);
3021 }
3022}
3023
3024static int
3025rb_scan_args_assign(const struct rb_scan_args_t *arg, int argc, const VALUE *const argv, va_list vargs)
3026{
3027 int i, argi = 0;
3028 VALUE *var, hash = Qnil;
3029#define rb_scan_args_next_param() va_arg(vargs, VALUE *)
3030 const int kw_flag = arg->kw_flag;
3031 const int n_lead = arg->n_lead;
3032 const int n_opt = arg->n_opt;
3033 const int n_trail = arg->n_trail;
3034 const int n_mand = n_lead + n_trail;
3035 const bool f_var = arg->f_var;
3036 const bool f_hash = arg->f_hash;
3037 const bool f_block = arg->f_block;
3038
3039 /* capture an option hash - phase 1: pop from the argv */
3040 if (f_hash && argc > 0) {
3041 VALUE last = argv[argc - 1];
3042 if (rb_scan_args_keyword_p(kw_flag, last)) {
3043 hash = rb_hash_dup(last);
3044 argc--;
3045 }
3046 }
3047
3048 if (argc < n_mand) {
3049 goto argc_error;
3050 }
3051
3052 /* capture leading mandatory arguments */
3053 for (i = 0; i < n_lead; i++) {
3054 var = rb_scan_args_next_param();
3055 if (var) *var = argv[argi];
3056 argi++;
3057 }
3058 /* capture optional arguments */
3059 for (i = 0; i < n_opt; i++) {
3060 var = rb_scan_args_next_param();
3061 if (argi < argc - n_trail) {
3062 if (var) *var = argv[argi];
3063 argi++;
3064 }
3065 else {
3066 if (var) *var = Qnil;
3067 }
3068 }
3069 /* capture variable length arguments */
3070 if (f_var) {
3071 int n_var = argc - argi - n_trail;
3072
3073 var = rb_scan_args_next_param();
3074 if (0 < n_var) {
3075 if (var) *var = rb_ary_new_from_values(n_var, &argv[argi]);
3076 argi += n_var;
3077 }
3078 else {
3079 if (var) *var = rb_ary_new();
3080 }
3081 }
3082 /* capture trailing mandatory arguments */
3083 for (i = 0; i < n_trail; i++) {
3084 var = rb_scan_args_next_param();
3085 if (var) *var = argv[argi];
3086 argi++;
3087 }
3088 /* capture an option hash - phase 2: assignment */
3089 if (f_hash) {
3090 var = rb_scan_args_next_param();
3091 if (var) *var = hash;
3092 }
3093 /* capture iterator block */
3094 if (f_block) {
3095 var = rb_scan_args_next_param();
3096 if (rb_block_given_p()) {
3097 *var = rb_block_proc();
3098 }
3099 else {
3100 *var = Qnil;
3101 }
3102 }
3103
3104 if (argi == argc) {
3105 return argc;
3106 }
3107
3108 argc_error:
3109 return -(argc + 1);
3110#undef rb_scan_args_next_param
3111}
3112
3113static int
3114rb_scan_args_result(const struct rb_scan_args_t *const arg, int argc)
3115{
3116 const int n_lead = arg->n_lead;
3117 const int n_opt = arg->n_opt;
3118 const int n_trail = arg->n_trail;
3119 const int n_mand = n_lead + n_trail;
3120 const bool f_var = arg->f_var;
3121
3122 if (argc >= 0) {
3123 return argc;
3124 }
3125
3126 argc = -argc - 1;
3127 rb_error_arity(argc, n_mand, f_var ? UNLIMITED_ARGUMENTS : n_mand + n_opt);
3129}
3130
3131#undef rb_scan_args
3132int
3133rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...)
3134{
3135 va_list vargs;
3136 struct rb_scan_args_t arg;
3137 rb_scan_args_parse(RB_SCAN_ARGS_PASS_CALLED_KEYWORDS, fmt, &arg);
3138 va_start(vargs,fmt);
3139 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
3140 va_end(vargs);
3141 return rb_scan_args_result(&arg, argc);
3142}
3143
3144#undef rb_scan_args_kw
3145int
3146rb_scan_args_kw(int kw_flag, int argc, const VALUE *argv, const char *fmt, ...)
3147{
3148 va_list vargs;
3149 struct rb_scan_args_t arg;
3150 rb_scan_args_parse(kw_flag, fmt, &arg);
3151 va_start(vargs,fmt);
3152 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
3153 va_end(vargs);
3154 return rb_scan_args_result(&arg, argc);
3155}
3156
#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:2409
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
Definition class.c:1696
VALUE rb_refinement_new(void)
Creates a new, anonymous refinement.
Definition class.c:1584
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition class.c:1479
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:1300
VALUE rb_singleton_class_clone(VALUE obj)
Clones a singleton class.
Definition class.c:1138
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:1941
VALUE rb_class_subclasses(VALUE klass)
Queries the class's direct descendants.
Definition class.c:2189
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
Definition class.c:2795
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition class.c:1515
VALUE rb_class_attached_object(VALUE klass)
Returns the attached object for a singleton class.
Definition class.c:2212
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:2586
VALUE rb_module_new(void)
Creates a new, anonymous module.
Definition class.c:1578
#define META_CLASS_OF_CLASS_CLASS_P(k)
whether k is a meta^(n)-class of Class class
Definition class.c:1219
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:2394
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:2447
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:1597
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:1631
void rb_singleton_class_attached(VALUE klass, VALUE obj)
Attaches a singleton class to its corresponding object.
Definition class.c:1207
void rb_freeze_singleton_class(VALUE x)
This is an implementation detail of RB_OBJ_FREEZE().
Definition class.c:2760
VALUE rb_mod_included_modules(VALUE mod)
Queries the list of included modules.
Definition class.c:2007
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:1554
VALUE rb_mod_ancestors(VALUE mod)
Queries the module's ancestors.
Definition class.c:2075
static VALUE make_metaclass(VALUE klass)
Creates a metaclass of klass
Definition class.c:1264
VALUE rb_class_inherited(VALUE super, VALUE klass)
Calls Class::inherited.
Definition class.c:1470
VALUE rb_mod_include_p(VALUE mod, VALUE mod2)
Queries if the passed module is included by the module.
Definition class.c:2043
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:2432
#define ENSURE_EIGENCLASS(klass)
ensures klass belongs to its own eigenclass.
Definition class.c:1250
VALUE rb_mod_init_copy(VALUE clone, VALUE orig)
The comment that comes with this function says :nodoc:.
Definition class.c:1016
VALUE rb_define_module_under(VALUE outer, const char *name)
Defines a module under the namespace of outer.
Definition class.c:1625
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:2781
VALUE rb_define_module_id(ID id)
This is a very badly designed API that creates an anonymous module.
Definition class.c:1591
VALUE rb_define_class_id(ID id, VALUE super)
This is a very badly designed API that creates an anonymous class.
Definition class.c:1449
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition class.c:2843
VALUE rb_extract_keywords(VALUE *orighash)
Splits a hash into two.
Definition class.c:2904
void rb_define_attr(VALUE klass, const char *name, int read, int write)
Defines public accessor method(s) for an attribute.
Definition class.c:2849
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
Definition class.c:2663
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:3146
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:3133
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:2922
#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:206
#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:3756
#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:3602
void rb_const_set(VALUE space, ID name, VALUE val)
Names a constant.
Definition variable.c:4069
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:3608
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:421
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:3931
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
Definition variable.c:378
int rb_const_defined(VALUE space, ID name)
Queries if the constant is defined at the namespace.
Definition variable.c:3925
void rb_alias(VALUE klass, ID dst, ID src)
Resembles alias.
Definition vm_method.c:2425
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:2005
void rb_clear_constant_cache_for_id(ID id)
Clears the inline constant caches associated with a particular ID.
Definition vm_method.c:139
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:2274
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