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