Ruby 3.5.0dev (2025-08-13 revision a9230e76ee19716c7d2e035be7bd1be9bdca2b59)
class.c (a9230e76ee19716c7d2e035be7bd1be9bdca2b59)
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(c, rb_id_table_create(0));
738}
739
740static void
741class_clear_method_table(VALUE c)
742{
743 RCLASS_WRITE_M_TBL(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(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 class_initialize_method_table(clone);
1057 rb_id_table_foreach(RCLASS_M_TBL(orig), clone_method_i, &arg);
1058 }
1059
1060 if (RCLASS_ORIGIN(orig) == orig) {
1061 rb_class_set_super(clone, RCLASS_SUPER(orig));
1062 }
1063 else {
1064 VALUE p = RCLASS_SUPER(orig);
1065 VALUE orig_origin = RCLASS_ORIGIN(orig);
1066 VALUE prev_clone_p = clone;
1067 VALUE origin_stack = rb_ary_hidden_new(2);
1068 VALUE origin[2];
1069 VALUE clone_p = 0;
1070 long origin_len;
1071 int add_subclass;
1072 VALUE clone_origin;
1073
1074 ensure_origin(clone);
1075 clone_origin = RCLASS_ORIGIN(clone);
1076
1077 while (p && p != orig_origin) {
1078 if (BUILTIN_TYPE(p) != T_ICLASS) {
1079 rb_bug("non iclass between module/class and origin");
1080 }
1081 clone_p = class_alloc(T_ICLASS, METACLASS_OF(p));
1082 RCLASS_SET_M_TBL(clone_p, RCLASS_M_TBL(p));
1083 rb_class_set_super(prev_clone_p, clone_p);
1084 prev_clone_p = clone_p;
1085 RCLASS_SET_CONST_TBL(clone_p, RCLASS_CONST_TBL(p), false);
1086 if (RB_TYPE_P(clone, T_CLASS)) {
1087 RCLASS_SET_INCLUDER(clone_p, clone);
1088 }
1089 add_subclass = TRUE;
1090 if (p != RCLASS_ORIGIN(p)) {
1091 origin[0] = clone_p;
1092 origin[1] = RCLASS_ORIGIN(p);
1093 rb_ary_cat(origin_stack, origin, 2);
1094 }
1095 else if ((origin_len = RARRAY_LEN(origin_stack)) > 1 &&
1096 RARRAY_AREF(origin_stack, origin_len - 1) == p) {
1097 RCLASS_WRITE_ORIGIN(RARRAY_AREF(origin_stack, (origin_len -= 2)), clone_p);
1098 RICLASS_WRITE_ORIGIN_SHARED_MTBL(clone_p);
1099 rb_ary_resize(origin_stack, origin_len);
1100 add_subclass = FALSE;
1101 }
1102 if (add_subclass) {
1103 rb_module_add_to_subclasses_list(METACLASS_OF(p), clone_p);
1104 }
1105 p = RCLASS_SUPER(p);
1106 }
1107
1108 if (p == orig_origin) {
1109 if (clone_p) {
1110 rb_class_set_super(clone_p, clone_origin);
1111 rb_class_set_super(clone_origin, RCLASS_SUPER(orig_origin));
1112 }
1113 copy_tables(clone_origin, orig_origin);
1114 if (RCLASS_M_TBL(orig_origin)) {
1115 struct clone_method_arg arg;
1116 arg.old_klass = orig;
1117 arg.new_klass = clone;
1118 class_initialize_method_table(clone_origin);
1119 rb_id_table_foreach(RCLASS_M_TBL(orig_origin), clone_method_i, &arg);
1120 }
1121 }
1122 else {
1123 rb_bug("no origin for class that has origin");
1124 }
1125
1126 rb_class_update_superclasses(clone);
1127 }
1128
1129 return clone;
1130}
1131
1132VALUE
1134{
1135 return rb_singleton_class_clone_and_attach(obj, Qundef);
1136}
1137
1138// Clone and return the singleton class of `obj` if it has been created and is attached to `obj`.
1139VALUE
1140rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach)
1141{
1142 const VALUE klass = METACLASS_OF(obj);
1143
1144 // Note that `rb_singleton_class()` can create situations where `klass` is
1145 // attached to an object other than `obj`. In which case `obj` does not have
1146 // a material singleton class attached yet and there is no singleton class
1147 // to clone.
1148 if (!(RCLASS_SINGLETON_P(klass) && RCLASS_ATTACHED_OBJECT(klass) == obj)) {
1149 // nothing to clone
1150 return klass;
1151 }
1152 else {
1153 /* copy singleton(unnamed) class */
1154 bool klass_of_clone_is_new;
1155 RUBY_ASSERT(RB_TYPE_P(klass, T_CLASS));
1156 VALUE clone = class_alloc(T_CLASS, 0);
1157
1158 if (BUILTIN_TYPE(obj) == T_CLASS) {
1159 klass_of_clone_is_new = true;
1160 RBASIC_SET_CLASS(clone, clone);
1161 }
1162 else {
1163 VALUE klass_metaclass_clone = rb_singleton_class_clone(klass);
1164 // When `METACLASS_OF(klass) == klass_metaclass_clone`, it means the
1165 // recursive call did not clone `METACLASS_OF(klass)`.
1166 klass_of_clone_is_new = (METACLASS_OF(klass) != klass_metaclass_clone);
1167 RBASIC_SET_CLASS(clone, klass_metaclass_clone);
1168 }
1169
1170 // initialize method table before any GC chance
1171 class_initialize_method_table(clone);
1172
1173 rb_class_set_super(clone, RCLASS_SUPER(klass));
1174 rb_fields_tbl_copy(clone, klass);
1175 if (RCLASS_CONST_TBL(klass)) {
1176 struct clone_const_arg arg;
1177 struct rb_id_table *table;
1178 arg.tbl = table = rb_id_table_create(0);
1179 arg.klass = clone;
1180 rb_id_table_foreach(RCLASS_CONST_TBL(klass), clone_const_i, &arg);
1181 RCLASS_SET_CONST_TBL(clone, table, false);
1182 }
1183 if (!UNDEF_P(attach)) {
1184 rb_singleton_class_attached(clone, attach);
1185 }
1186 {
1187 struct clone_method_arg arg;
1188 arg.old_klass = klass;
1189 arg.new_klass = clone;
1190 rb_id_table_foreach(RCLASS_M_TBL(klass), clone_method_i, &arg);
1191 }
1192 if (klass_of_clone_is_new) {
1193 rb_singleton_class_attached(METACLASS_OF(clone), clone);
1194 }
1195 FL_SET(clone, FL_SINGLETON);
1196
1197 return clone;
1198 }
1199}
1200
1201void
1203{
1204 if (RCLASS_SINGLETON_P(klass)) {
1205 RCLASS_SET_ATTACHED_OBJECT(klass, obj);
1206 }
1207}
1208
1214#define META_CLASS_OF_CLASS_CLASS_P(k) (METACLASS_OF(k) == (k))
1215
1216static int
1217rb_singleton_class_has_metaclass_p(VALUE sklass)
1218{
1219 return RCLASS_ATTACHED_OBJECT(METACLASS_OF(sklass)) == sklass;
1220}
1221
1222int
1223rb_singleton_class_internal_p(VALUE sklass)
1224{
1225 return (RB_TYPE_P(RCLASS_ATTACHED_OBJECT(sklass), T_CLASS) &&
1226 !rb_singleton_class_has_metaclass_p(sklass));
1227}
1228
1234#define HAVE_METACLASS_P(k) \
1235 (FL_TEST(METACLASS_OF(k), FL_SINGLETON) && \
1236 rb_singleton_class_has_metaclass_p(k))
1237
1245#define ENSURE_EIGENCLASS(klass) \
1246 (HAVE_METACLASS_P(klass) ? METACLASS_OF(klass) : make_metaclass(klass))
1247
1248
1258static inline VALUE
1260{
1261 VALUE super;
1262 VALUE metaclass = class_boot_namespaceable(Qundef, FL_TEST_RAW(klass, RCLASS_NAMESPACEABLE));
1263
1264 FL_SET(metaclass, FL_SINGLETON);
1265 rb_singleton_class_attached(metaclass, klass);
1266
1267 if (META_CLASS_OF_CLASS_CLASS_P(klass)) {
1268 SET_METACLASS_OF(klass, metaclass);
1269 SET_METACLASS_OF(metaclass, metaclass);
1270 }
1271 else {
1272 VALUE tmp = METACLASS_OF(klass); /* for a meta^(n)-class klass, tmp is meta^(n)-class of Class class */
1273 SET_METACLASS_OF(klass, metaclass);
1274 SET_METACLASS_OF(metaclass, ENSURE_EIGENCLASS(tmp));
1275 }
1276
1277 super = RCLASS_SUPER(klass);
1278 while (RB_TYPE_P(super, T_ICLASS)) super = RCLASS_SUPER(super);
1279 class_associate_super(metaclass, super ? ENSURE_EIGENCLASS(super) : rb_cClass, true);
1280 rb_class_set_initialized(klass);
1281
1282 // Full class ancestry may not have been filled until we reach here.
1283 rb_class_update_superclasses(METACLASS_OF(metaclass));
1284
1285 return metaclass;
1286}
1287
1294static inline VALUE
1296{
1297 VALUE orig_class = METACLASS_OF(obj);
1298 VALUE klass = class_boot_namespaceable(orig_class, FL_TEST_RAW(orig_class, RCLASS_NAMESPACEABLE));
1299
1300 FL_SET(klass, FL_SINGLETON);
1301 RBASIC_SET_CLASS(obj, klass);
1302 rb_singleton_class_attached(klass, obj);
1303 rb_yjit_invalidate_no_singleton_class(orig_class);
1304
1305 SET_METACLASS_OF(klass, METACLASS_OF(rb_class_real(orig_class)));
1306 return klass;
1307}
1308
1309
1310static VALUE
1311boot_defclass(const char *name, VALUE super)
1312{
1313 VALUE obj = rb_class_boot(super);
1314 ID id = rb_intern(name);
1315
1316 rb_const_set((rb_cObject ? rb_cObject : obj), id, obj);
1317 rb_vm_register_global_object(obj);
1318 return obj;
1319}
1320
1321/***********************************************************************
1322 *
1323 * Document-class: Refinement
1324 *
1325 * Refinement is a class of the +self+ (current context) inside +refine+
1326 * statement. It allows to import methods from other modules, see #import_methods.
1327 */
1328
1329#if 0 /* for RDoc */
1330/*
1331 * Document-method: Refinement#import_methods
1332 *
1333 * call-seq:
1334 * import_methods(module, ...) -> self
1335 *
1336 * Imports methods from modules. Unlike Module#include,
1337 * Refinement#import_methods copies methods and adds them into the refinement,
1338 * so the refinement is activated in the imported methods.
1339 *
1340 * Note that due to method copying, only methods defined in Ruby code can be imported.
1341 *
1342 * module StrUtils
1343 * def indent(level)
1344 * ' ' * level + self
1345 * end
1346 * end
1347 *
1348 * module M
1349 * refine String do
1350 * import_methods StrUtils
1351 * end
1352 * end
1353 *
1354 * using M
1355 * "foo".indent(3)
1356 * #=> " foo"
1357 *
1358 * module M
1359 * refine String do
1360 * import_methods Enumerable
1361 * # Can't import method which is not defined with Ruby code: Enumerable#drop
1362 * end
1363 * end
1364 *
1365 */
1366
1367static VALUE
1368refinement_import_methods(int argc, VALUE *argv, VALUE refinement)
1369{
1370}
1371# endif
1372
1392void
1393Init_class_hierarchy(void)
1394{
1395 rb_cBasicObject = boot_defclass("BasicObject", 0);
1396 rb_cObject = boot_defclass("Object", rb_cBasicObject);
1397 rb_vm_register_global_object(rb_cObject);
1398
1399 /* resolve class name ASAP for order-independence */
1400 rb_set_class_path_string(rb_cObject, rb_cObject, rb_fstring_lit("Object"));
1401
1402 rb_cModule = boot_defclass("Module", rb_cObject);
1403 rb_cClass = boot_defclass("Class", rb_cModule);
1404 rb_cRefinement = boot_defclass("Refinement", rb_cModule);
1405
1406#if 0 /* for RDoc */
1407 // we pretend it to be public, otherwise RDoc will ignore it
1408 rb_define_method(rb_cRefinement, "import_methods", refinement_import_methods, -1);
1409#endif
1410
1411 rb_const_set(rb_cObject, rb_intern_const("BasicObject"), rb_cBasicObject);
1412 RBASIC_SET_CLASS(rb_cClass, rb_cClass);
1413 RBASIC_SET_CLASS(rb_cModule, rb_cClass);
1414 RBASIC_SET_CLASS(rb_cObject, rb_cClass);
1415 RBASIC_SET_CLASS(rb_cRefinement, rb_cClass);
1416 RBASIC_SET_CLASS(rb_cBasicObject, rb_cClass);
1417
1419}
1420
1421
1432VALUE
1433rb_make_metaclass(VALUE obj, VALUE unused)
1434{
1435 if (BUILTIN_TYPE(obj) == T_CLASS) {
1436 return make_metaclass(obj);
1437 }
1438 else {
1439 return make_singleton_class(obj);
1440 }
1441}
1442
1443VALUE
1445{
1446 VALUE klass;
1447
1448 if (!super) super = rb_cObject;
1449 klass = rb_class_new(super);
1450 rb_make_metaclass(klass, METACLASS_OF(super));
1451
1452 return klass;
1453}
1454
1455
1464VALUE
1466{
1467 ID inherited;
1468 if (!super) super = rb_cObject;
1469 CONST_ID(inherited, "inherited");
1470 return rb_funcall(super, inherited, 1, klass);
1471}
1472
1473VALUE
1474rb_define_class(const char *name, VALUE super)
1475{
1476 VALUE klass;
1477 ID id;
1478 const rb_namespace_t *ns = rb_current_namespace();
1479
1480 id = rb_intern(name);
1481 if (NAMESPACE_OPTIONAL_P(ns)) {
1482 return rb_define_class_id_under(ns->ns_object, id, super);
1483 }
1484 if (rb_const_defined(rb_cObject, id)) {
1485 klass = rb_const_get(rb_cObject, id);
1486 if (!RB_TYPE_P(klass, T_CLASS)) {
1487 rb_raise(rb_eTypeError, "%s is not a class (%"PRIsVALUE")",
1488 name, rb_obj_class(klass));
1489 }
1490 if (rb_class_real(RCLASS_SUPER(klass)) != super) {
1491 rb_raise(rb_eTypeError, "superclass mismatch for class %s", name);
1492 }
1493
1494 /* Class may have been defined in Ruby and not pin-rooted */
1495 rb_vm_register_global_object(klass);
1496 return klass;
1497 }
1498 if (!super) {
1499 rb_raise(rb_eArgError, "no super class for '%s'", name);
1500 }
1501 klass = rb_define_class_id(id, super);
1502 rb_vm_register_global_object(klass);
1503 rb_const_set(rb_cObject, id, klass);
1504 rb_class_inherited(super, klass);
1505
1506 return klass;
1507}
1508
1509VALUE
1510rb_define_class_under(VALUE outer, const char *name, VALUE super)
1511{
1512 return rb_define_class_id_under(outer, rb_intern(name), super);
1513}
1514
1515VALUE
1516rb_define_class_id_under_no_pin(VALUE outer, ID id, VALUE super)
1517{
1518 VALUE klass;
1519
1520 if (rb_const_defined_at(outer, id)) {
1521 klass = rb_const_get_at(outer, id);
1522 if (!RB_TYPE_P(klass, T_CLASS)) {
1523 rb_raise(rb_eTypeError, "%"PRIsVALUE"::%"PRIsVALUE" is not a class"
1524 " (%"PRIsVALUE")",
1525 outer, rb_id2str(id), rb_obj_class(klass));
1526 }
1527 if (rb_class_real(RCLASS_SUPER(klass)) != super) {
1528 rb_raise(rb_eTypeError, "superclass mismatch for class "
1529 "%"PRIsVALUE"::%"PRIsVALUE""
1530 " (%"PRIsVALUE" is given but was %"PRIsVALUE")",
1531 outer, rb_id2str(id), RCLASS_SUPER(klass), super);
1532 }
1533
1534 return klass;
1535 }
1536 if (!super) {
1537 rb_raise(rb_eArgError, "no super class for '%"PRIsVALUE"::%"PRIsVALUE"'",
1538 rb_class_path(outer), rb_id2str(id));
1539 }
1540 klass = rb_define_class_id(id, super);
1541 rb_set_class_path_string(klass, outer, rb_id2str(id));
1542 rb_const_set(outer, id, klass);
1543 rb_class_inherited(super, klass);
1544
1545 return klass;
1546}
1547
1548VALUE
1550{
1551 VALUE klass = rb_define_class_id_under_no_pin(outer, id, super);
1552 rb_vm_register_global_object(klass);
1553 return klass;
1554}
1555
1556VALUE
1557rb_module_s_alloc(VALUE klass)
1558{
1559 VALUE mod = class_alloc(T_MODULE, klass);
1560 class_initialize_method_table(mod);
1561 return mod;
1562}
1563
1564static inline VALUE
1565module_new(VALUE klass)
1566{
1567 VALUE mdl = class_alloc(T_MODULE, klass);
1568 class_initialize_method_table(mdl);
1569 return (VALUE)mdl;
1570}
1571
1572VALUE
1574{
1575 return module_new(rb_cModule);
1576}
1577
1578VALUE
1580{
1581 return module_new(rb_cRefinement);
1582}
1583
1584// Kept for compatibility. Use rb_module_new() instead.
1585VALUE
1587{
1588 return rb_module_new();
1589}
1590
1591VALUE
1592rb_define_module(const char *name)
1593{
1594 VALUE module;
1595 ID id;
1596 const rb_namespace_t *ns = rb_current_namespace();
1597
1598 id = rb_intern(name);
1599 if (NAMESPACE_OPTIONAL_P(ns)) {
1600 return rb_define_module_id_under(ns->ns_object, id);
1601 }
1602 if (rb_const_defined(rb_cObject, id)) {
1603 module = rb_const_get(rb_cObject, id);
1604 if (!RB_TYPE_P(module, T_MODULE)) {
1605 rb_raise(rb_eTypeError, "%s is not a module (%"PRIsVALUE")",
1606 name, rb_obj_class(module));
1607 }
1608 /* Module may have been defined in Ruby and not pin-rooted */
1609 rb_vm_register_global_object(module);
1610 return module;
1611 }
1612 module = rb_module_new();
1613 rb_vm_register_global_object(module);
1614 rb_const_set(rb_cObject, id, module);
1615
1616 return module;
1617}
1618
1619VALUE
1620rb_define_module_under(VALUE outer, const char *name)
1621{
1622 return rb_define_module_id_under(outer, rb_intern(name));
1623}
1624
1625VALUE
1627{
1628 VALUE module;
1629
1630 if (rb_const_defined_at(outer, id)) {
1631 module = rb_const_get_at(outer, id);
1632 if (!RB_TYPE_P(module, T_MODULE)) {
1633 rb_raise(rb_eTypeError, "%"PRIsVALUE"::%"PRIsVALUE" is not a module"
1634 " (%"PRIsVALUE")",
1635 outer, rb_id2str(id), rb_obj_class(module));
1636 }
1637 /* Module may have been defined in Ruby and not pin-rooted */
1638 rb_vm_register_global_object(module);
1639 return module;
1640 }
1641 module = rb_module_new();
1642 rb_const_set(outer, id, module);
1643 rb_set_class_path_string(module, outer, rb_id2str(id));
1644 rb_vm_register_global_object(module);
1645
1646 return module;
1647}
1648
1649VALUE
1650rb_include_class_new(VALUE module, VALUE super)
1651{
1652 VALUE klass = class_alloc(T_ICLASS, rb_cClass);
1653
1654 RCLASS_SET_M_TBL(klass, RCLASS_WRITABLE_M_TBL(module));
1655
1656 RCLASS_SET_ORIGIN(klass, klass);
1657 if (BUILTIN_TYPE(module) == T_ICLASS) {
1658 module = METACLASS_OF(module);
1659 }
1660 RUBY_ASSERT(!RB_TYPE_P(module, T_ICLASS));
1661 if (RCLASS_WRITABLE_CONST_TBL(module)) {
1662 RCLASS_SET_CONST_TBL(klass, RCLASS_WRITABLE_CONST_TBL(module), true);
1663 }
1664 else {
1665 RCLASS_WRITE_CONST_TBL(module, rb_id_table_create(0), false);
1666 RCLASS_SET_CONST_TBL(klass, RCLASS_WRITABLE_CONST_TBL(module), true);
1667 }
1668
1669 RCLASS_SET_CVC_TBL(klass, RCLASS_WRITABLE_CVC_TBL(module));
1670
1671 class_associate_super(klass, super, true);
1672 RBASIC_SET_CLASS(klass, module);
1673
1674 return (VALUE)klass;
1675}
1676
1677static int include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super);
1678
1679static void
1680ensure_includable(VALUE klass, VALUE module)
1681{
1682 rb_class_modify_check(klass);
1683 Check_Type(module, T_MODULE);
1684 rb_class_set_initialized(module);
1685 if (!NIL_P(rb_refinement_module_get_refined_class(module))) {
1686 rb_raise(rb_eArgError, "refinement module is not allowed");
1687 }
1688}
1689
1690void
1692{
1693 int changed = 0;
1694
1695 ensure_includable(klass, module);
1696
1697 changed = include_modules_at(klass, RCLASS_ORIGIN(klass), module, TRUE);
1698 if (changed < 0)
1699 rb_raise(rb_eArgError, "cyclic include detected");
1700
1701 if (RB_TYPE_P(klass, T_MODULE)) {
1702 rb_subclass_entry_t *iclass = RCLASS_SUBCLASSES_FIRST(klass);
1703 while (iclass) {
1704 int do_include = 1;
1705 VALUE check_class = iclass->klass;
1706 /* During lazy sweeping, iclass->klass could be a dead object that
1707 * has not yet been swept. */
1708 if (!rb_objspace_garbage_object_p(check_class)) {
1709 while (check_class) {
1710 RUBY_ASSERT(!rb_objspace_garbage_object_p(check_class));
1711
1712 if (RB_TYPE_P(check_class, T_ICLASS) &&
1713 (METACLASS_OF(check_class) == module)) {
1714 do_include = 0;
1715 }
1716 check_class = RCLASS_SUPER(check_class);
1717 }
1718
1719 if (do_include) {
1720 include_modules_at(iclass->klass, RCLASS_ORIGIN(iclass->klass), module, TRUE);
1721 }
1722 }
1723
1724 iclass = iclass->next;
1725 }
1726 }
1727}
1728
1729static enum rb_id_table_iterator_result
1730add_refined_method_entry_i(ID key, VALUE value, void *data)
1731{
1732 rb_add_refined_method_entry((VALUE)data, key);
1733 return ID_TABLE_CONTINUE;
1734}
1735
1736static enum rb_id_table_iterator_result
1737clear_module_cache_i(ID id, VALUE val, void *data)
1738{
1739 VALUE klass = (VALUE)data;
1740 rb_clear_method_cache(klass, id);
1741 return ID_TABLE_CONTINUE;
1742}
1743
1744static bool
1745module_in_super_chain(const VALUE klass, VALUE module)
1746{
1747 struct rb_id_table *const klass_m_tbl = RCLASS_M_TBL(RCLASS_ORIGIN(klass));
1748 if (klass_m_tbl) {
1749 while (module) {
1750 if (klass_m_tbl == RCLASS_M_TBL(module))
1751 return true;
1752 module = RCLASS_SUPER(module);
1753 }
1754 }
1755 return false;
1756}
1757
1758// For each ID key in the class constant table, we're going to clear the VM's
1759// inline constant caches associated with it.
1760static enum rb_id_table_iterator_result
1761clear_constant_cache_i(ID id, VALUE value, void *data)
1762{
1764 return ID_TABLE_CONTINUE;
1765}
1766
1767static int
1768do_include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super, bool check_cyclic)
1769{
1770 VALUE p, iclass, origin_stack = 0;
1771 int method_changed = 0;
1772 long origin_len;
1773 VALUE klass_origin = RCLASS_ORIGIN(klass);
1774 VALUE original_klass = klass;
1775
1776 if (check_cyclic && module_in_super_chain(klass, module))
1777 return -1;
1778
1779 while (module) {
1780 int c_seen = FALSE;
1781 int superclass_seen = FALSE;
1782 struct rb_id_table *tbl;
1783
1784 if (klass == c) {
1785 c_seen = TRUE;
1786 }
1787 if (klass_origin != c || search_super) {
1788 /* ignore if the module included already in superclasses for include,
1789 * ignore if the module included before origin class for prepend
1790 */
1791 for (p = RCLASS_SUPER(klass); p; p = RCLASS_SUPER(p)) {
1792 int type = BUILTIN_TYPE(p);
1793 if (klass_origin == p && !search_super)
1794 break;
1795 if (c == p)
1796 c_seen = TRUE;
1797 if (type == T_ICLASS) {
1798 if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) {
1799 if (!superclass_seen && c_seen) {
1800 c = p; /* move insertion point */
1801 }
1802 goto skip;
1803 }
1804 }
1805 else if (type == T_CLASS) {
1806 superclass_seen = TRUE;
1807 }
1808 }
1809 }
1810
1811 VALUE super_class = RCLASS_SUPER(c);
1812
1813 // invalidate inline method cache
1814 RB_DEBUG_COUNTER_INC(cvar_include_invalidate);
1815 ruby_vm_global_cvar_state++;
1816 tbl = RCLASS_M_TBL(module);
1817 if (tbl && rb_id_table_size(tbl)) {
1818 if (search_super) { // include
1819 if (super_class && !RB_TYPE_P(super_class, T_MODULE)) {
1820 rb_id_table_foreach(tbl, clear_module_cache_i, (void *)super_class);
1821 }
1822 }
1823 else { // prepend
1824 if (!RB_TYPE_P(original_klass, T_MODULE)) {
1825 rb_id_table_foreach(tbl, clear_module_cache_i, (void *)original_klass);
1826 }
1827 }
1828 method_changed = 1;
1829 }
1830
1831 // setup T_ICLASS for the include/prepend module
1832 iclass = rb_include_class_new(module, super_class);
1833 c = rb_class_set_super(c, iclass);
1834 RCLASS_SET_INCLUDER(iclass, klass);
1835 if (module != RCLASS_ORIGIN(module)) {
1836 if (!origin_stack) origin_stack = rb_ary_hidden_new(2);
1837 VALUE origin[2] = {iclass, RCLASS_ORIGIN(module)};
1838 rb_ary_cat(origin_stack, origin, 2);
1839 }
1840 else if (origin_stack && (origin_len = RARRAY_LEN(origin_stack)) > 1 &&
1841 RARRAY_AREF(origin_stack, origin_len - 1) == module) {
1842 RCLASS_WRITE_ORIGIN(RARRAY_AREF(origin_stack, (origin_len -= 2)), iclass);
1843 RICLASS_WRITE_ORIGIN_SHARED_MTBL(iclass);
1844 rb_ary_resize(origin_stack, origin_len);
1845 }
1846
1847 VALUE m = module;
1848 if (BUILTIN_TYPE(m) == T_ICLASS) m = METACLASS_OF(m);
1849 rb_module_add_to_subclasses_list(m, iclass);
1850
1851 if (BUILTIN_TYPE(klass) == T_MODULE && FL_TEST(klass, RMODULE_IS_REFINEMENT)) {
1852 VALUE refined_class =
1853 rb_refinement_module_get_refined_class(klass);
1854
1855 rb_id_table_foreach(RCLASS_M_TBL(module), add_refined_method_entry_i, (void *)refined_class);
1857 }
1858
1859 tbl = RCLASS_CONST_TBL(module);
1860 if (tbl && rb_id_table_size(tbl))
1861 rb_id_table_foreach(tbl, clear_constant_cache_i, NULL);
1862 skip:
1863 module = RCLASS_SUPER(module);
1864 }
1865
1866 return method_changed;
1867}
1868
1869static int
1870include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super)
1871{
1872 return do_include_modules_at(klass, c, module, search_super, true);
1873}
1874
1875static enum rb_id_table_iterator_result
1876move_refined_method(ID key, VALUE value, void *data)
1877{
1878 rb_method_entry_t *me = (rb_method_entry_t *)value;
1879
1880 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1881 VALUE klass = (VALUE)data;
1882 struct rb_id_table *tbl = RCLASS_WRITABLE_M_TBL(klass);
1883
1884 if (me->def->body.refined.orig_me) {
1885 const rb_method_entry_t *orig_me = me->def->body.refined.orig_me, *new_me;
1886 RB_OBJ_WRITE(me, &me->def->body.refined.orig_me, NULL);
1887 new_me = rb_method_entry_clone(me);
1888 rb_method_table_insert(klass, tbl, key, new_me);
1889 rb_method_entry_copy(me, orig_me);
1890 return ID_TABLE_CONTINUE;
1891 }
1892 else {
1893 rb_method_table_insert(klass, tbl, key, me);
1894 return ID_TABLE_DELETE;
1895 }
1896 }
1897 else {
1898 return ID_TABLE_CONTINUE;
1899 }
1900}
1901
1902static enum rb_id_table_iterator_result
1903cache_clear_refined_method(ID key, VALUE value, void *data)
1904{
1905 rb_method_entry_t *me = (rb_method_entry_t *) value;
1906
1907 if (me->def->type == VM_METHOD_TYPE_REFINED && me->def->body.refined.orig_me) {
1908 VALUE klass = (VALUE)data;
1909 rb_clear_method_cache(klass, me->called_id);
1910 }
1911 // Refined method entries without an orig_me is going to stay in the method
1912 // table of klass, like before the move, so no need to clear the cache.
1913
1914 return ID_TABLE_CONTINUE;
1915}
1916
1917static bool
1918ensure_origin(VALUE klass)
1919{
1920 VALUE origin = RCLASS_ORIGIN(klass);
1921 if (origin == klass) {
1922 origin = class_alloc(T_ICLASS, klass);
1923 RCLASS_SET_M_TBL(origin, RCLASS_M_TBL(klass));
1924 rb_class_set_super(origin, RCLASS_SUPER(klass));
1925 rb_class_set_super(klass, origin); // writes origin into RCLASS_SUPER(klass)
1926 RCLASS_WRITE_ORIGIN(klass, origin);
1927
1928 // RCLASS_WRITE_ORIGIN marks origin as an origin, so this is the first
1929 // point that it sees M_TBL and may mark it
1930 rb_gc_writebarrier_remember(origin);
1931
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(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:1691
VALUE rb_refinement_new(void)
Creates a new, anonymous refinement.
Definition class.c:1579
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition class.c:1474
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:1295
VALUE rb_singleton_class_clone(VALUE obj)
Clones a singleton class.
Definition class.c:1133
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:1510
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:1573
#define META_CLASS_OF_CLASS_CLASS_P(k)
whether k is a meta^(n)-class of Class class
Definition class.c:1214
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:1592
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:1626
void rb_singleton_class_attached(VALUE klass, VALUE obj)
Attaches a singleton class to its corresponding object.
Definition class.c:1202
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:1549
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:1259
VALUE rb_class_inherited(VALUE super, VALUE klass)
Calls Class::inherited.
Definition class.c:1465
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:1245
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:1620
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:1586
VALUE rb_define_class_id(ID id, VALUE super)
This is a very badly designed API that creates an anonymous class.
Definition class.c:1444
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:1036
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:109
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:655
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:3757
#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:3549
void rb_const_set(VALUE space, ID name, VALUE val)
Names a constant.
Definition variable.c:4016
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:3555
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:422
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:3878
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
Definition variable.c:379
int rb_const_defined(VALUE space, ID name)
Queries if the constant is defined at the namespace.
Definition variable.c:3872
void rb_alias(VALUE klass, ID dst, ID src)
Resembles alias.
Definition vm_method.c:2606
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:2186
void rb_clear_constant_cache_for_id(ID id)
Clears the inline constant caches associated with a particular ID.
Definition vm_method.c:319
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