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