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