Ruby  3.1.4p223 (2023-03-30 revision HEAD)
class.c
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/object.h"
28 #include "internal/string.h"
29 #include "internal/variable.h"
30 #include "ruby/st.h"
31 #include "vm_core.h"
32 
33 #define id_attached id__attached__
34 
35 #define METACLASS_OF(k) RBASIC(k)->klass
36 #define SET_METACLASS_OF(k, cls) RBASIC_SET_CLASS(k, cls)
37 
38 RUBY_EXTERN rb_serial_t ruby_vm_global_cvar_state;
39 
40 static rb_subclass_entry_t *
41 push_subclass_entry_to_list(VALUE super, VALUE klass)
42 {
43  rb_subclass_entry_t *entry, *head;
44 
45  entry = ZALLOC(rb_subclass_entry_t);
46  entry->klass = klass;
47 
48  head = RCLASS_SUBCLASSES(super);
49  if (!head) {
51  RCLASS_SUBCLASSES(super) = head;
52  }
53  entry->next = head->next;
54  entry->prev = head;
55 
56  if (head->next) {
57  head->next->prev = entry;
58  }
59  head->next = entry;
60 
61  return entry;
62 }
63 
64 void
65 rb_class_subclass_add(VALUE super, VALUE klass)
66 {
67  if (super && super != Qundef) {
68  rb_subclass_entry_t *entry = push_subclass_entry_to_list(super, klass);
69  RCLASS_SUBCLASS_ENTRY(klass) = entry;
70  }
71 }
72 
73 static void
74 rb_module_add_to_subclasses_list(VALUE module, VALUE iclass)
75 {
76  rb_subclass_entry_t *entry = push_subclass_entry_to_list(module, iclass);
77  RCLASS_MODULE_SUBCLASS_ENTRY(iclass) = entry;
78 }
79 
80 void
81 rb_class_remove_subclass_head(VALUE klass)
82 {
83  rb_subclass_entry_t *head = RCLASS_SUBCLASSES(klass);
84 
85  if (head) {
86  if (head->next) {
87  head->next->prev = NULL;
88  }
89  RCLASS_SUBCLASSES(klass) = NULL;
90  xfree(head);
91  }
92 }
93 
94 void
95 rb_class_remove_from_super_subclasses(VALUE klass)
96 {
97  rb_subclass_entry_t *entry = RCLASS_SUBCLASS_ENTRY(klass);
98 
99  if (entry) {
100  rb_subclass_entry_t *prev = entry->prev, *next = entry->next;
101 
102  if (prev) {
103  prev->next = next;
104  }
105  if (next) {
106  next->prev = prev;
107  }
108 
109  xfree(entry);
110  }
111 
112  RCLASS_SUBCLASS_ENTRY(klass) = NULL;
113 }
114 
115 void
116 rb_class_remove_from_module_subclasses(VALUE klass)
117 {
118  rb_subclass_entry_t *entry = RCLASS_MODULE_SUBCLASS_ENTRY(klass);
119 
120  if (entry) {
121  rb_subclass_entry_t *prev = entry->prev, *next = entry->next;
122 
123  if (prev) {
124  prev->next = next;
125  }
126  if (next) {
127  next->prev = prev;
128  }
129 
130  xfree(entry);
131  }
132 
133  RCLASS_MODULE_SUBCLASS_ENTRY(klass) = NULL;
134 }
135 
136 void
137 rb_class_foreach_subclass(VALUE klass, void (*f)(VALUE, VALUE), VALUE arg)
138 {
139  // RCLASS_SUBCLASSES should always point to our head element which has NULL klass
140  rb_subclass_entry_t *cur = RCLASS_SUBCLASSES(klass);
141  // if we have a subclasses list, then the head is a placeholder with no valid
142  // class. So ignore it and use the next element in the list (if one exists)
143  if (cur) {
144  RUBY_ASSERT(!cur->klass);
145  cur = cur->next;
146  }
147 
148  /* do not be tempted to simplify this loop into a for loop, the order of
149  operations is important here if `f` modifies the linked list */
150  while (cur) {
151  VALUE curklass = cur->klass;
152  cur = cur->next;
153  // do not trigger GC during f, otherwise the cur will become
154  // a dangling pointer if the subclass is collected
155  f(curklass, arg);
156  }
157 }
158 
159 static void
160 class_detach_subclasses(VALUE klass, VALUE arg)
161 {
162  rb_class_remove_from_super_subclasses(klass);
163 }
164 
165 void
166 rb_class_detach_subclasses(VALUE klass)
167 {
168  rb_class_foreach_subclass(klass, class_detach_subclasses, Qnil);
169 }
170 
171 static void
172 class_detach_module_subclasses(VALUE klass, VALUE arg)
173 {
174  rb_class_remove_from_module_subclasses(klass);
175 }
176 
177 void
178 rb_class_detach_module_subclasses(VALUE klass)
179 {
180  rb_class_foreach_subclass(klass, class_detach_module_subclasses, Qnil);
181 }
182 
195 static VALUE
196 class_alloc(VALUE flags, VALUE klass)
197 {
198  size_t alloc_size = sizeof(struct RClass);
199 
200 #if USE_RVARGC
201  alloc_size += sizeof(rb_classext_t);
202 #endif
203 
204  flags &= T_MASK;
205  flags |= FL_PROMOTED1 /* start from age == 2 */;
207  RVARGC_NEWOBJ_OF(obj, struct RClass, klass, flags, alloc_size);
208 
209 #if USE_RVARGC
210  memset(RCLASS_EXT(obj), 0, sizeof(rb_classext_t));
211 #else
212  obj->ptr = ZALLOC(rb_classext_t);
213 #endif
214 
215  /* ZALLOC
216  RCLASS_IV_TBL(obj) = 0;
217  RCLASS_CONST_TBL(obj) = 0;
218  RCLASS_M_TBL(obj) = 0;
219  RCLASS_IV_INDEX_TBL(obj) = 0;
220  RCLASS_SET_SUPER((VALUE)obj, 0);
221  RCLASS_SUBCLASSES(obj) = NULL;
222  RCLASS_PARENT_SUBCLASSES(obj) = NULL;
223  RCLASS_MODULE_SUBCLASSES(obj) = NULL;
224  */
225  RCLASS_SET_ORIGIN((VALUE)obj, (VALUE)obj);
226  RCLASS_SERIAL(obj) = rb_next_class_serial();
227  RB_OBJ_WRITE(obj, &RCLASS_REFINED_CLASS(obj), Qnil);
228  RCLASS_ALLOCATOR(obj) = 0;
229 
230  return (VALUE)obj;
231 }
232 
233 static void
234 RCLASS_M_TBL_INIT(VALUE c)
235 {
236  RCLASS_M_TBL(c) = rb_id_table_create(0);
237 }
238 
248 VALUE
250 {
252 
253  RCLASS_SET_SUPER(klass, super);
254  RCLASS_M_TBL_INIT(klass);
255 
256  return (VALUE)klass;
257 }
258 
259 void
261 {
262  if (!RB_TYPE_P(super, T_CLASS)) {
263  rb_raise(rb_eTypeError, "superclass must be an instance of Class (given an instance of %"PRIsVALUE")",
264  rb_obj_class(super));
265  }
266  if (RBASIC(super)->flags & FL_SINGLETON) {
267  rb_raise(rb_eTypeError, "can't make subclass of singleton class");
268  }
269  if (super == rb_cClass) {
270  rb_raise(rb_eTypeError, "can't make subclass of Class");
271  }
272 }
273 
274 VALUE
276 {
277  Check_Type(super, T_CLASS);
278  rb_check_inheritable(super);
279  return rb_class_boot(super);
280 }
281 
282 VALUE
283 rb_class_s_alloc(VALUE klass)
284 {
285  return rb_class_boot(0);
286 }
287 
288 static void
289 clone_method(VALUE old_klass, VALUE new_klass, ID mid, const rb_method_entry_t *me)
290 {
291  if (me->def->type == VM_METHOD_TYPE_ISEQ) {
292  rb_cref_t *new_cref;
293  rb_vm_rewrite_cref(me->def->body.iseq.cref, old_klass, new_klass, &new_cref);
294  rb_add_method_iseq(new_klass, mid, me->def->body.iseq.iseqptr, new_cref, METHOD_ENTRY_VISI(me));
295  }
296  else {
297  rb_method_entry_set(new_klass, mid, me, METHOD_ENTRY_VISI(me));
298  }
299 }
300 
302  VALUE new_klass;
303  VALUE old_klass;
304 };
305 
306 static enum rb_id_table_iterator_result
307 clone_method_i(ID key, VALUE value, void *data)
308 {
309  const struct clone_method_arg *arg = (struct clone_method_arg *)data;
310  clone_method(arg->old_klass, arg->new_klass, key, (const rb_method_entry_t *)value);
311  return ID_TABLE_CONTINUE;
312 }
313 
315  VALUE klass;
316  struct rb_id_table *tbl;
317 };
318 
319 static int
320 clone_const(ID key, const rb_const_entry_t *ce, struct clone_const_arg *arg)
321 {
323  MEMCPY(nce, ce, rb_const_entry_t, 1);
324  RB_OBJ_WRITTEN(arg->klass, Qundef, ce->value);
325  RB_OBJ_WRITTEN(arg->klass, Qundef, ce->file);
326 
327  rb_id_table_insert(arg->tbl, key, (VALUE)nce);
328  return ID_TABLE_CONTINUE;
329 }
330 
331 static enum rb_id_table_iterator_result
332 clone_const_i(ID key, VALUE value, void *data)
333 {
334  return clone_const(key, (const rb_const_entry_t *)value, data);
335 }
336 
337 static void
338 class_init_copy_check(VALUE clone, VALUE orig)
339 {
340  if (orig == rb_cBasicObject) {
341  rb_raise(rb_eTypeError, "can't copy the root class");
342  }
343  if (RCLASS_SUPER(clone) != 0 || clone == rb_cBasicObject) {
344  rb_raise(rb_eTypeError, "already initialized class");
345  }
346  if (FL_TEST(orig, FL_SINGLETON)) {
347  rb_raise(rb_eTypeError, "can't copy singleton class");
348  }
349 }
350 
351 static void
352 copy_tables(VALUE clone, VALUE orig)
353 {
354  if (RCLASS_IV_TBL(clone)) {
355  st_free_table(RCLASS_IV_TBL(clone));
356  RCLASS_IV_TBL(clone) = 0;
357  }
358  if (RCLASS_CONST_TBL(clone)) {
359  rb_free_const_table(RCLASS_CONST_TBL(clone));
360  RCLASS_CONST_TBL(clone) = 0;
361  }
362  RCLASS_M_TBL(clone) = 0;
363  if (RCLASS_IV_TBL(orig)) {
364  st_data_t id;
365 
366  rb_iv_tbl_copy(clone, orig);
367  CONST_ID(id, "__tmp_classpath__");
368  st_delete(RCLASS_IV_TBL(clone), &id, 0);
369  CONST_ID(id, "__classpath__");
370  st_delete(RCLASS_IV_TBL(clone), &id, 0);
371  CONST_ID(id, "__classid__");
372  st_delete(RCLASS_IV_TBL(clone), &id, 0);
373  }
374  if (RCLASS_CONST_TBL(orig)) {
375  struct clone_const_arg arg;
376 
377  arg.tbl = RCLASS_CONST_TBL(clone) = rb_id_table_create(0);
378  arg.klass = clone;
379  rb_id_table_foreach(RCLASS_CONST_TBL(orig), clone_const_i, &arg);
380  }
381 }
382 
383 static bool ensure_origin(VALUE klass);
384 
388 enum {RMODULE_ALLOCATED_BUT_NOT_INITIALIZED = RUBY_FL_USER5};
389 
390 static inline bool
391 RMODULE_UNINITIALIZED(VALUE module)
392 {
393  return FL_TEST_RAW(module, RMODULE_ALLOCATED_BUT_NOT_INITIALIZED);
394 }
395 
396 void
397 rb_module_set_initialized(VALUE mod)
398 {
399  FL_UNSET_RAW(mod, RMODULE_ALLOCATED_BUT_NOT_INITIALIZED);
400  /* no more re-initialization */
401 }
402 
403 void
404 rb_module_check_initializable(VALUE mod)
405 {
406  if (!RMODULE_UNINITIALIZED(mod)) {
407  rb_raise(rb_eTypeError, "already initialized module");
408  }
409  RB_OBJ_WRITE(mod, &RCLASS(mod)->super, 0);
410 }
411 
412 /* :nodoc: */
413 VALUE
415 {
416  switch (BUILTIN_TYPE(clone)) {
417  case T_CLASS:
418  case T_ICLASS:
419  class_init_copy_check(clone, orig);
420  break;
421  case T_MODULE:
422  rb_module_check_initializable(clone);
423  break;
424  default:
425  break;
426  }
427  if (!OBJ_INIT_COPY(clone, orig)) return clone;
428 
429  /* cloned flag is refer at constant inline cache
430  * see vm_get_const_key_cref() in vm_insnhelper.c
431  */
432  FL_SET(clone, RCLASS_CLONED);
433  FL_SET(orig , RCLASS_CLONED);
434 
435  if (!FL_TEST(CLASS_OF(clone), FL_SINGLETON)) {
436  RBASIC_SET_CLASS(clone, rb_singleton_class_clone(orig));
437  rb_singleton_class_attached(RBASIC(clone)->klass, (VALUE)clone);
438  }
439  RCLASS_ALLOCATOR(clone) = RCLASS_ALLOCATOR(orig);
440  copy_tables(clone, orig);
441  if (RCLASS_M_TBL(orig)) {
442  struct clone_method_arg arg;
443  arg.old_klass = orig;
444  arg.new_klass = clone;
445  RCLASS_M_TBL_INIT(clone);
446  rb_id_table_foreach(RCLASS_M_TBL(orig), clone_method_i, &arg);
447  }
448 
449  if (RCLASS_ORIGIN(orig) == orig) {
450  RCLASS_SET_SUPER(clone, RCLASS_SUPER(orig));
451  }
452  else {
453  VALUE p = RCLASS_SUPER(orig);
454  VALUE orig_origin = RCLASS_ORIGIN(orig);
455  VALUE prev_clone_p = clone;
456  VALUE origin_stack = rb_ary_tmp_new(2);
457  VALUE origin[2];
458  VALUE clone_p = 0;
459  long origin_len;
460  int add_subclass;
461  VALUE clone_origin;
462 
463  ensure_origin(clone);
464  clone_origin = RCLASS_ORIGIN(clone);
465 
466  while (p && p != orig_origin) {
467  if (BUILTIN_TYPE(p) != T_ICLASS) {
468  rb_bug("non iclass between module/class and origin");
469  }
470  clone_p = class_alloc(RBASIC(p)->flags, RBASIC(p)->klass);
471  RCLASS_SET_SUPER(prev_clone_p, clone_p);
472  prev_clone_p = clone_p;
473  RCLASS_M_TBL(clone_p) = RCLASS_M_TBL(p);
474  RCLASS_CONST_TBL(clone_p) = RCLASS_CONST_TBL(p);
475  RCLASS_IV_TBL(clone_p) = RCLASS_IV_TBL(p);
476  RCLASS_ALLOCATOR(clone_p) = RCLASS_ALLOCATOR(p);
477  if (RB_TYPE_P(clone, T_CLASS)) {
478  RCLASS_SET_INCLUDER(clone_p, clone);
479  }
480  add_subclass = TRUE;
481  if (p != RCLASS_ORIGIN(p)) {
482  origin[0] = clone_p;
483  origin[1] = RCLASS_ORIGIN(p);
484  rb_ary_cat(origin_stack, origin, 2);
485  }
486  else if ((origin_len = RARRAY_LEN(origin_stack)) > 1 &&
487  RARRAY_AREF(origin_stack, origin_len - 1) == p) {
488  RCLASS_SET_ORIGIN(RARRAY_AREF(origin_stack, (origin_len -= 2)), clone_p);
489  RICLASS_SET_ORIGIN_SHARED_MTBL(clone_p);
490  rb_ary_resize(origin_stack, origin_len);
491  add_subclass = FALSE;
492  }
493  if (add_subclass) {
494  rb_module_add_to_subclasses_list(RBASIC(p)->klass, clone_p);
495  }
496  p = RCLASS_SUPER(p);
497  }
498 
499  if (p == orig_origin) {
500  if (clone_p) {
501  RCLASS_SET_SUPER(clone_p, clone_origin);
502  RCLASS_SET_SUPER(clone_origin, RCLASS_SUPER(orig_origin));
503  }
504  copy_tables(clone_origin, orig_origin);
505  if (RCLASS_M_TBL(orig_origin)) {
506  struct clone_method_arg arg;
507  arg.old_klass = orig;
508  arg.new_klass = clone;
509  RCLASS_M_TBL_INIT(clone_origin);
510  rb_id_table_foreach(RCLASS_M_TBL(orig_origin), clone_method_i, &arg);
511  }
512  }
513  else {
514  rb_bug("no origin for class that has origin");
515  }
516  }
517 
518  return clone;
519 }
520 
521 VALUE
523 {
524  return rb_singleton_class_clone_and_attach(obj, Qundef);
525 }
526 
527 // Clone and return the singleton class of `obj` if it has been created and is attached to `obj`.
528 VALUE
529 rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach)
530 {
531  const VALUE klass = RBASIC(obj)->klass;
532 
533  // Note that `rb_singleton_class()` can create situations where `klass` is
534  // attached to an object other than `obj`. In which case `obj` does not have
535  // a material singleton class attached yet and there is no singleton class
536  // to clone.
537  if (!(FL_TEST(klass, FL_SINGLETON) && rb_attr_get(klass, id_attached) == obj)) {
538  // nothing to clone
539  return klass;
540  }
541  else {
542  /* copy singleton(unnamed) class */
543  bool klass_of_clone_is_new;
544  VALUE clone = class_alloc(RBASIC(klass)->flags, 0);
545 
546  if (BUILTIN_TYPE(obj) == T_CLASS) {
547  klass_of_clone_is_new = true;
548  RBASIC_SET_CLASS(clone, clone);
549  }
550  else {
551  VALUE klass_metaclass_clone = rb_singleton_class_clone(klass);
552  // When `METACLASS_OF(klass) == klass_metaclass_clone`, it means the
553  // recursive call did not clone `METACLASS_OF(klass)`.
554  klass_of_clone_is_new = (METACLASS_OF(klass) != klass_metaclass_clone);
555  RBASIC_SET_CLASS(clone, klass_metaclass_clone);
556  }
557 
558  RCLASS_SET_SUPER(clone, RCLASS_SUPER(klass));
559  RCLASS_ALLOCATOR(clone) = RCLASS_ALLOCATOR(klass);
560  if (RCLASS_IV_TBL(klass)) {
561  rb_iv_tbl_copy(clone, klass);
562  }
563  if (RCLASS_CONST_TBL(klass)) {
564  struct clone_const_arg arg;
565  arg.tbl = RCLASS_CONST_TBL(clone) = rb_id_table_create(0);
566  arg.klass = clone;
567  rb_id_table_foreach(RCLASS_CONST_TBL(klass), clone_const_i, &arg);
568  }
569  if (attach != Qundef) {
570  rb_singleton_class_attached(clone, attach);
571  }
572  RCLASS_M_TBL_INIT(clone);
573  {
574  struct clone_method_arg arg;
575  arg.old_klass = klass;
576  arg.new_klass = clone;
577  rb_id_table_foreach(RCLASS_M_TBL(klass), clone_method_i, &arg);
578  }
579  if (klass_of_clone_is_new) {
580  rb_singleton_class_attached(RBASIC(clone)->klass, clone);
581  }
582  FL_SET(clone, FL_SINGLETON);
583 
584  return clone;
585  }
586 }
587 
588 void
590 {
591  if (FL_TEST(klass, FL_SINGLETON)) {
592  rb_class_ivar_set(klass, id_attached, obj);
593  }
594 }
595 
601 #define META_CLASS_OF_CLASS_CLASS_P(k) (METACLASS_OF(k) == (k))
602 
603 static int
604 rb_singleton_class_has_metaclass_p(VALUE sklass)
605 {
606  return rb_attr_get(METACLASS_OF(sklass), id_attached) == sklass;
607 }
608 
609 int
610 rb_singleton_class_internal_p(VALUE sklass)
611 {
612  return (RB_TYPE_P(rb_attr_get(sklass, id_attached), T_CLASS) &&
613  !rb_singleton_class_has_metaclass_p(sklass));
614 }
615 
621 #define HAVE_METACLASS_P(k) \
622  (FL_TEST(METACLASS_OF(k), FL_SINGLETON) && \
623  rb_singleton_class_has_metaclass_p(k))
624 
632 #define ENSURE_EIGENCLASS(klass) \
633  (HAVE_METACLASS_P(klass) ? METACLASS_OF(klass) : make_metaclass(klass))
634 
635 
645 static inline VALUE
647 {
648  VALUE super;
649  VALUE metaclass = rb_class_boot(Qundef);
650 
651  FL_SET(metaclass, FL_SINGLETON);
652  rb_singleton_class_attached(metaclass, klass);
653 
654  if (META_CLASS_OF_CLASS_CLASS_P(klass)) {
655  SET_METACLASS_OF(klass, metaclass);
656  SET_METACLASS_OF(metaclass, metaclass);
657  }
658  else {
659  VALUE tmp = METACLASS_OF(klass); /* for a meta^(n)-class klass, tmp is meta^(n)-class of Class class */
660  SET_METACLASS_OF(klass, metaclass);
661  SET_METACLASS_OF(metaclass, ENSURE_EIGENCLASS(tmp));
662  }
663 
664  super = RCLASS_SUPER(klass);
665  while (RB_TYPE_P(super, T_ICLASS)) super = RCLASS_SUPER(super);
666  RCLASS_SET_SUPER(metaclass, super ? ENSURE_EIGENCLASS(super) : rb_cClass);
667 
668  return metaclass;
669 }
670 
677 static inline VALUE
679 {
680  VALUE orig_class = RBASIC(obj)->klass;
681  VALUE klass = rb_class_boot(orig_class);
682 
683  FL_SET(klass, FL_SINGLETON);
684  RBASIC_SET_CLASS(obj, klass);
685  rb_singleton_class_attached(klass, obj);
686 
687  SET_METACLASS_OF(klass, METACLASS_OF(rb_class_real(orig_class)));
688  return klass;
689 }
690 
691 
692 static VALUE
693 boot_defclass(const char *name, VALUE super)
694 {
695  VALUE obj = rb_class_boot(super);
696  ID id = rb_intern(name);
697 
698  rb_const_set((rb_cObject ? rb_cObject : obj), id, obj);
699  rb_vm_add_root_module(obj);
700  return obj;
701 }
702 
703 /***********************************************************************
704  *
705  * Document-class: Refinement
706  *
707  * Refinement is a class of the +self+ (current context) inside +refine+
708  * statement. It allows to import methods from other modules, see #import_methods.
709  */
710 
711 #if 0 /* for RDoc */
712 /*
713  * Document-method: Refinement#import_methods
714  *
715  * call-seq:
716  * import_methods(module, ...) -> self
717  *
718  * Imports methods from modules. Unlike Module#include,
719  * Refinement#import_methods copies methods and adds them into the refinement,
720  * so the refinement is activated in the imported methods.
721  *
722  * Note that due to method copying, only methods defined in Ruby code can be imported.
723  *
724  * module StrUtils
725  * def indent(level)
726  * ' ' * level + self
727  * end
728  * end
729  *
730  * module M
731  * refine String do
732  * import_methods StrUtils
733  * end
734  * end
735  *
736  * using M
737  * "foo".indent(3)
738  * #=> " foo"
739  *
740  * module M
741  * refine String do
742  * import_methods Enumerable
743  * # Can't import method which is not defined with Ruby code: Enumerable#drop
744  * end
745  * end
746  *
747  */
748 
749 static VALUE
750 refinement_import_methods(int argc, VALUE *argv, VALUE refinement)
751 {
752 }
753 # endif
754 
755 void
757 {
758  rb_cBasicObject = boot_defclass("BasicObject", 0);
759  rb_cObject = boot_defclass("Object", rb_cBasicObject);
760  rb_gc_register_mark_object(rb_cObject);
761 
762  /* resolve class name ASAP for order-independence */
763  rb_set_class_path_string(rb_cObject, rb_cObject, rb_fstring_lit("Object"));
764 
765  rb_cModule = boot_defclass("Module", rb_cObject);
766  rb_cClass = boot_defclass("Class", rb_cModule);
767  rb_cRefinement = boot_defclass("Refinement", rb_cModule);
768 
769 #if 0 /* for RDoc */
770  // we pretend it to be public, otherwise RDoc will ignore it
771  rb_define_method(rb_cRefinement, "import_methods", refinement_import_methods, -1);
772 #endif
773 
774  rb_const_set(rb_cObject, rb_intern_const("BasicObject"), rb_cBasicObject);
775  RBASIC_SET_CLASS(rb_cClass, rb_cClass);
776  RBASIC_SET_CLASS(rb_cModule, rb_cClass);
777  RBASIC_SET_CLASS(rb_cObject, rb_cClass);
778  RBASIC_SET_CLASS(rb_cRefinement, rb_cClass);
779  RBASIC_SET_CLASS(rb_cBasicObject, rb_cClass);
780 
782 }
783 
784 
795 VALUE
796 rb_make_metaclass(VALUE obj, VALUE unused)
797 {
798  if (BUILTIN_TYPE(obj) == T_CLASS) {
799  return make_metaclass(obj);
800  }
801  else {
802  return make_singleton_class(obj);
803  }
804 }
805 
806 VALUE
808 {
809  VALUE klass;
810 
811  if (!super) super = rb_cObject;
812  klass = rb_class_new(super);
813  rb_make_metaclass(klass, RBASIC(super)->klass);
814 
815  return klass;
816 }
817 
818 
827 MJIT_FUNC_EXPORTED VALUE
829 {
830  ID inherited;
831  if (!super) super = rb_cObject;
832  CONST_ID(inherited, "inherited");
833  return rb_funcall(super, inherited, 1, klass);
834 }
835 
836 VALUE
837 rb_define_class(const char *name, VALUE super)
838 {
839  VALUE klass;
840  ID id;
841 
842  id = rb_intern(name);
843  if (rb_const_defined(rb_cObject, id)) {
844  klass = rb_const_get(rb_cObject, id);
845  if (!RB_TYPE_P(klass, T_CLASS)) {
846  rb_raise(rb_eTypeError, "%s is not a class (%"PRIsVALUE")",
847  name, rb_obj_class(klass));
848  }
849  if (rb_class_real(RCLASS_SUPER(klass)) != super) {
850  rb_raise(rb_eTypeError, "superclass mismatch for class %s", name);
851  }
852 
853  /* Class may have been defined in Ruby and not pin-rooted */
854  rb_vm_add_root_module(klass);
855  return klass;
856  }
857  if (!super) {
858  rb_raise(rb_eArgError, "no super class for `%s'", name);
859  }
860  klass = rb_define_class_id(id, super);
861  rb_vm_add_root_module(klass);
862  rb_const_set(rb_cObject, id, klass);
863  rb_class_inherited(super, klass);
864 
865  return klass;
866 }
867 
868 VALUE
869 rb_define_class_under(VALUE outer, const char *name, VALUE super)
870 {
871  return rb_define_class_id_under(outer, rb_intern(name), super);
872 }
873 
874 VALUE
876 {
877  VALUE klass;
878 
879  if (rb_const_defined_at(outer, id)) {
880  klass = rb_const_get_at(outer, id);
881  if (!RB_TYPE_P(klass, T_CLASS)) {
882  rb_raise(rb_eTypeError, "%"PRIsVALUE"::%"PRIsVALUE" is not a class"
883  " (%"PRIsVALUE")",
884  outer, rb_id2str(id), rb_obj_class(klass));
885  }
886  if (rb_class_real(RCLASS_SUPER(klass)) != super) {
887  rb_raise(rb_eTypeError, "superclass mismatch for class "
888  "%"PRIsVALUE"::%"PRIsVALUE""
889  " (%"PRIsVALUE" is given but was %"PRIsVALUE")",
890  outer, rb_id2str(id), RCLASS_SUPER(klass), super);
891  }
892  /* Class may have been defined in Ruby and not pin-rooted */
893  rb_vm_add_root_module(klass);
894 
895  return klass;
896  }
897  if (!super) {
898  rb_raise(rb_eArgError, "no super class for `%"PRIsVALUE"::%"PRIsVALUE"'",
899  rb_class_path(outer), rb_id2str(id));
900  }
901  klass = rb_define_class_id(id, super);
902  rb_set_class_path_string(klass, outer, rb_id2str(id));
903  rb_const_set(outer, id, klass);
904  rb_class_inherited(super, klass);
905  rb_vm_add_root_module(klass);
906 
907  return klass;
908 }
909 
910 VALUE
911 rb_module_s_alloc(VALUE klass)
912 {
913  VALUE mod = class_alloc(T_MODULE, klass);
914  RCLASS_M_TBL_INIT(mod);
915  FL_SET(mod, RMODULE_ALLOCATED_BUT_NOT_INITIALIZED);
916  RB_OBJ_WRITE(mod, &RCLASS(mod)->super, 0);
917  return mod;
918 }
919 
920 static inline VALUE
921 module_new(VALUE klass)
922 {
923  VALUE mdl = class_alloc(T_MODULE, klass);
924  RCLASS_M_TBL_INIT(mdl);
925  return (VALUE)mdl;
926 }
927 
928 VALUE
930 {
931  return module_new(rb_cModule);
932 }
933 
934 VALUE
936 {
937  return module_new(rb_cRefinement);
938 }
939 
940 // Kept for compatibility. Use rb_module_new() instead.
941 VALUE
943 {
944  return rb_module_new();
945 }
946 
947 VALUE
948 rb_define_module(const char *name)
949 {
950  VALUE module;
951  ID id;
952 
953  id = rb_intern(name);
954  if (rb_const_defined(rb_cObject, id)) {
955  module = rb_const_get(rb_cObject, id);
956  if (!RB_TYPE_P(module, T_MODULE)) {
957  rb_raise(rb_eTypeError, "%s is not a module (%"PRIsVALUE")",
958  name, rb_obj_class(module));
959  }
960  /* Module may have been defined in Ruby and not pin-rooted */
961  rb_vm_add_root_module(module);
962  return module;
963  }
964  module = rb_module_new();
965  rb_vm_add_root_module(module);
966  rb_const_set(rb_cObject, id, module);
967 
968  return module;
969 }
970 
971 VALUE
972 rb_define_module_under(VALUE outer, const char *name)
973 {
974  return rb_define_module_id_under(outer, rb_intern(name));
975 }
976 
977 VALUE
979 {
980  VALUE module;
981 
982  if (rb_const_defined_at(outer, id)) {
983  module = rb_const_get_at(outer, id);
984  if (!RB_TYPE_P(module, T_MODULE)) {
985  rb_raise(rb_eTypeError, "%"PRIsVALUE"::%"PRIsVALUE" is not a module"
986  " (%"PRIsVALUE")",
987  outer, rb_id2str(id), rb_obj_class(module));
988  }
989  /* Module may have been defined in Ruby and not pin-rooted */
991  return module;
992  }
993  module = rb_module_new();
994  rb_const_set(outer, id, module);
995  rb_set_class_path_string(module, outer, rb_id2str(id));
997 
998  return module;
999 }
1000 
1001 VALUE
1002 rb_include_class_new(VALUE module, VALUE super)
1003 {
1004  VALUE klass = class_alloc(T_ICLASS, rb_cClass);
1005 
1006  RCLASS_M_TBL(klass) = RCLASS_M_TBL(module);
1007 
1008  RCLASS_SET_ORIGIN(klass, klass);
1009  if (BUILTIN_TYPE(module) == T_ICLASS) {
1010  module = RBASIC(module)->klass;
1011  }
1012  RUBY_ASSERT(!RB_TYPE_P(module, T_ICLASS));
1013  if (!RCLASS_IV_TBL(module)) {
1014  RCLASS_IV_TBL(module) = st_init_numtable();
1015  }
1016  if (!RCLASS_CONST_TBL(module)) {
1017  RCLASS_CONST_TBL(module) = rb_id_table_create(0);
1018  }
1019  RCLASS_IV_TBL(klass) = RCLASS_IV_TBL(module);
1020  RCLASS_CVC_TBL(klass) = RCLASS_CVC_TBL(module);
1021  RCLASS_CONST_TBL(klass) = RCLASS_CONST_TBL(module);
1022 
1023  RCLASS_SET_SUPER(klass, super);
1024  RBASIC_SET_CLASS(klass, module);
1025 
1026  return (VALUE)klass;
1027 }
1028 
1029 static int include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super);
1030 
1031 static void
1032 ensure_includable(VALUE klass, VALUE module)
1033 {
1034  rb_class_modify_check(klass);
1035  Check_Type(module, T_MODULE);
1036  rb_module_set_initialized(module);
1037  if (!NIL_P(rb_refinement_module_get_refined_class(module))) {
1038  rb_raise(rb_eArgError, "refinement module is not allowed");
1039  }
1040 }
1041 
1042 void
1044 {
1045  int changed = 0;
1046 
1047  ensure_includable(klass, module);
1048 
1049  changed = include_modules_at(klass, RCLASS_ORIGIN(klass), module, TRUE);
1050  if (changed < 0)
1051  rb_raise(rb_eArgError, "cyclic include detected");
1052 
1053  if (RB_TYPE_P(klass, T_MODULE)) {
1054  rb_subclass_entry_t *iclass = RCLASS_SUBCLASSES(klass);
1055  // skip the placeholder subclass entry at the head of the list
1056  if (iclass && !iclass->klass) {
1057  iclass = iclass->next;
1058  }
1059 
1060  int do_include = 1;
1061  while (iclass) {
1062  VALUE check_class = iclass->klass;
1063  /* During lazy sweeping, iclass->klass could be a dead object that
1064  * has not yet been swept. */
1065  if (!rb_objspace_garbage_object_p(check_class)) {
1066  while (check_class) {
1067  RUBY_ASSERT(!rb_objspace_garbage_object_p(check_class));
1068 
1069  if (RB_TYPE_P(check_class, T_ICLASS) &&
1070  (RBASIC(check_class)->klass == module)) {
1071  do_include = 0;
1072  }
1073  check_class = RCLASS_SUPER(check_class);
1074  }
1075 
1076  if (do_include) {
1077  include_modules_at(iclass->klass, RCLASS_ORIGIN(iclass->klass), module, TRUE);
1078  }
1079  }
1080 
1081  iclass = iclass->next;
1082  }
1083  }
1084 }
1085 
1086 static enum rb_id_table_iterator_result
1087 add_refined_method_entry_i(ID key, VALUE value, void *data)
1088 {
1089  rb_add_refined_method_entry((VALUE)data, key);
1090  return ID_TABLE_CONTINUE;
1091 }
1092 
1093 static enum rb_id_table_iterator_result
1094 clear_module_cache_i(ID id, VALUE val, void *data)
1095 {
1096  VALUE klass = (VALUE)data;
1097  rb_clear_method_cache(klass, id);
1098  return ID_TABLE_CONTINUE;
1099 }
1100 
1101 static bool
1102 module_in_super_chain(const VALUE klass, VALUE module)
1103 {
1104  struct rb_id_table *const klass_m_tbl = RCLASS_M_TBL(RCLASS_ORIGIN(klass));
1105  if (klass_m_tbl) {
1106  while (module) {
1107  if (klass_m_tbl == RCLASS_M_TBL(module))
1108  return true;
1109  module = RCLASS_SUPER(module);
1110  }
1111  }
1112  return false;
1113 }
1114 
1115 static int
1116 do_include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super, bool check_cyclic)
1117 {
1118  VALUE p, iclass, origin_stack = 0;
1119  int method_changed = 0, constant_changed = 0, add_subclass;
1120  long origin_len;
1121  VALUE klass_origin = RCLASS_ORIGIN(klass);
1122  VALUE original_klass = klass;
1123 
1124  if (check_cyclic && module_in_super_chain(klass, module))
1125  return -1;
1126 
1127  while (module) {
1128  int c_seen = FALSE;
1129  int superclass_seen = FALSE;
1130  struct rb_id_table *tbl;
1131 
1132  if (klass == c) {
1133  c_seen = TRUE;
1134  }
1135  if (klass_origin != c || search_super) {
1136  /* ignore if the module included already in superclasses for include,
1137  * ignore if the module included before origin class for prepend
1138  */
1139  for (p = RCLASS_SUPER(klass); p; p = RCLASS_SUPER(p)) {
1140  int type = BUILTIN_TYPE(p);
1141  if (klass_origin == p && !search_super)
1142  break;
1143  if (c == p)
1144  c_seen = TRUE;
1145  if (type == T_ICLASS) {
1146  if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) {
1147  if (!superclass_seen && c_seen) {
1148  c = p; /* move insertion point */
1149  }
1150  goto skip;
1151  }
1152  }
1153  else if (type == T_CLASS) {
1154  superclass_seen = TRUE;
1155  }
1156  }
1157  }
1158 
1159  VALUE super_class = RCLASS_SUPER(c);
1160 
1161  // invalidate inline method cache
1162  RB_DEBUG_COUNTER_INC(cvar_include_invalidate);
1163  ruby_vm_global_cvar_state++;
1164  tbl = RCLASS_M_TBL(module);
1165  if (tbl && rb_id_table_size(tbl)) {
1166  if (search_super) { // include
1167  if (super_class && !RB_TYPE_P(super_class, T_MODULE)) {
1168  rb_id_table_foreach(tbl, clear_module_cache_i, (void *)super_class);
1169  }
1170  }
1171  else { // prepend
1172  if (!RB_TYPE_P(original_klass, T_MODULE)) {
1173  rb_id_table_foreach(tbl, clear_module_cache_i, (void *)original_klass);
1174  }
1175  }
1176  method_changed = 1;
1177  }
1178 
1179  // setup T_ICLASS for the include/prepend module
1180  iclass = rb_include_class_new(module, super_class);
1181  c = RCLASS_SET_SUPER(c, iclass);
1182  RCLASS_SET_INCLUDER(iclass, klass);
1183  add_subclass = TRUE;
1184  if (module != RCLASS_ORIGIN(module)) {
1185  if (!origin_stack) origin_stack = rb_ary_tmp_new(2);
1186  VALUE origin[2] = {iclass, RCLASS_ORIGIN(module)};
1187  rb_ary_cat(origin_stack, origin, 2);
1188  }
1189  else if (origin_stack && (origin_len = RARRAY_LEN(origin_stack)) > 1 &&
1190  RARRAY_AREF(origin_stack, origin_len - 1) == module) {
1191  RCLASS_SET_ORIGIN(RARRAY_AREF(origin_stack, (origin_len -= 2)), iclass);
1192  RICLASS_SET_ORIGIN_SHARED_MTBL(iclass);
1193  rb_ary_resize(origin_stack, origin_len);
1194  add_subclass = FALSE;
1195  }
1196 
1197  if (add_subclass) {
1198  VALUE m = module;
1199  if (BUILTIN_TYPE(m) == T_ICLASS) m = RBASIC(m)->klass;
1200  rb_module_add_to_subclasses_list(m, iclass);
1201  }
1202 
1203  if (FL_TEST(klass, RMODULE_IS_REFINEMENT)) {
1204  VALUE refined_class =
1205  rb_refinement_module_get_refined_class(klass);
1206 
1207  rb_id_table_foreach(RCLASS_M_TBL(module), add_refined_method_entry_i, (void *)refined_class);
1208  FL_SET(c, RMODULE_INCLUDED_INTO_REFINEMENT);
1209  }
1210 
1211  tbl = RCLASS_CONST_TBL(module);
1212  if (tbl && rb_id_table_size(tbl)) constant_changed = 1;
1213  skip:
1214  module = RCLASS_SUPER(module);
1215  }
1216 
1217  if (constant_changed) rb_clear_constant_cache();
1218 
1219  return method_changed;
1220 }
1221 
1222 static int
1223 include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super)
1224 {
1225  return do_include_modules_at(klass, c, module, search_super, true);
1226 }
1227 
1228 static enum rb_id_table_iterator_result
1229 move_refined_method(ID key, VALUE value, void *data)
1230 {
1231  rb_method_entry_t *me = (rb_method_entry_t *)value;
1232 
1233  if (me->def->type == VM_METHOD_TYPE_REFINED) {
1234  VALUE klass = (VALUE)data;
1235  struct rb_id_table *tbl = RCLASS_M_TBL(klass);
1236 
1237  if (me->def->body.refined.orig_me) {
1238  const rb_method_entry_t *orig_me = me->def->body.refined.orig_me, *new_me;
1239  RB_OBJ_WRITE(me, &me->def->body.refined.orig_me, NULL);
1240  new_me = rb_method_entry_clone(me);
1241  rb_method_table_insert(klass, tbl, key, new_me);
1242  rb_method_entry_copy(me, orig_me);
1243  return ID_TABLE_CONTINUE;
1244  }
1245  else {
1246  rb_method_table_insert(klass, tbl, key, me);
1247  return ID_TABLE_DELETE;
1248  }
1249  }
1250  else {
1251  return ID_TABLE_CONTINUE;
1252  }
1253 }
1254 
1255 static enum rb_id_table_iterator_result
1256 cache_clear_refined_method(ID key, VALUE value, void *data)
1257 {
1258  rb_method_entry_t *me = (rb_method_entry_t *) value;
1259 
1260  if (me->def->type == VM_METHOD_TYPE_REFINED && me->def->body.refined.orig_me) {
1261  VALUE klass = (VALUE)data;
1262  rb_clear_method_cache(klass, me->called_id);
1263  }
1264  // Refined method entries without an orig_me is going to stay in the method
1265  // table of klass, like before the move, so no need to clear the cache.
1266 
1267  return ID_TABLE_CONTINUE;
1268 }
1269 
1270 static bool
1271 ensure_origin(VALUE klass)
1272 {
1273  VALUE origin = RCLASS_ORIGIN(klass);
1274  if (origin == klass) {
1275  origin = class_alloc(T_ICLASS, klass);
1276  RCLASS_SET_SUPER(origin, RCLASS_SUPER(klass));
1277  RCLASS_SET_SUPER(klass, origin);
1278  RCLASS_SET_ORIGIN(klass, origin);
1279  RCLASS_M_TBL(origin) = RCLASS_M_TBL(klass);
1280  RCLASS_M_TBL_INIT(klass);
1281  rb_id_table_foreach(RCLASS_M_TBL(origin), cache_clear_refined_method, (void *)klass);
1282  rb_id_table_foreach(RCLASS_M_TBL(origin), move_refined_method, (void *)klass);
1283  return true;
1284  }
1285  return false;
1286 }
1287 
1288 void
1290 {
1291  int changed;
1292  bool klass_had_no_origin;
1293 
1294  ensure_includable(klass, module);
1295  if (module_in_super_chain(klass, module))
1296  rb_raise(rb_eArgError, "cyclic prepend detected");
1297 
1298  klass_had_no_origin = ensure_origin(klass);
1299  changed = do_include_modules_at(klass, klass, module, FALSE, false);
1300  RUBY_ASSERT(changed >= 0); // already checked for cyclic prepend above
1301  if (changed) {
1302  rb_vm_check_redefinition_by_prepend(klass);
1303  }
1304  if (RB_TYPE_P(klass, T_MODULE)) {
1305  rb_subclass_entry_t *iclass = RCLASS_SUBCLASSES(klass);
1306  // skip the placeholder subclass entry at the head of the list if it exists
1307  if (iclass && iclass->next) {
1308  RUBY_ASSERT(!iclass->klass);
1309  iclass = iclass->next;
1310  }
1311 
1312  VALUE klass_origin = RCLASS_ORIGIN(klass);
1313  struct rb_id_table *klass_m_tbl = RCLASS_M_TBL(klass);
1314  struct rb_id_table *klass_origin_m_tbl = RCLASS_M_TBL(klass_origin);
1315  while (iclass) {
1316  /* During lazy sweeping, iclass->klass could be a dead object that
1317  * has not yet been swept. */
1318  if (!rb_objspace_garbage_object_p(iclass->klass)) {
1319  if (klass_had_no_origin && klass_origin_m_tbl == RCLASS_M_TBL(iclass->klass)) {
1320  // backfill an origin iclass to handle refinements and future prepends
1321  rb_id_table_foreach(RCLASS_M_TBL(iclass->klass), clear_module_cache_i, (void *)iclass->klass);
1322  RCLASS_M_TBL(iclass->klass) = klass_m_tbl;
1323  VALUE origin = rb_include_class_new(klass_origin, RCLASS_SUPER(iclass->klass));
1324  RCLASS_SET_SUPER(iclass->klass, origin);
1325  RCLASS_SET_INCLUDER(origin, RCLASS_INCLUDER(iclass->klass));
1326  RCLASS_SET_ORIGIN(iclass->klass, origin);
1327  RICLASS_SET_ORIGIN_SHARED_MTBL(origin);
1328  }
1329  include_modules_at(iclass->klass, iclass->klass, module, FALSE);
1330  }
1331 
1332  iclass = iclass->next;
1333  }
1334  }
1335 }
1336 
1337 /*
1338  * call-seq:
1339  * mod.included_modules -> array
1340  *
1341  * Returns the list of modules included or prepended in <i>mod</i>
1342  * or one of <i>mod</i>'s ancestors.
1343  *
1344  * module Sub
1345  * end
1346  *
1347  * module Mixin
1348  * prepend Sub
1349  * end
1350  *
1351  * module Outer
1352  * include Mixin
1353  * end
1354  *
1355  * Mixin.included_modules #=> [Sub]
1356  * Outer.included_modules #=> [Sub, Mixin]
1357  */
1358 
1359 VALUE
1361 {
1362  VALUE ary = rb_ary_new();
1363  VALUE p;
1364  VALUE origin = RCLASS_ORIGIN(mod);
1365 
1366  for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
1367  if (p != origin && RCLASS_ORIGIN(p) == p && BUILTIN_TYPE(p) == T_ICLASS) {
1368  VALUE m = RBASIC(p)->klass;
1369  if (RB_TYPE_P(m, T_MODULE))
1370  rb_ary_push(ary, m);
1371  }
1372  }
1373  return ary;
1374 }
1375 
1376 /*
1377  * call-seq:
1378  * mod.include?(module) -> true or false
1379  *
1380  * Returns <code>true</code> if <i>module</i> is included
1381  * or prepended in <i>mod</i> or one of <i>mod</i>'s ancestors.
1382  *
1383  * module A
1384  * end
1385  * class B
1386  * include A
1387  * end
1388  * class C < B
1389  * end
1390  * B.include?(A) #=> true
1391  * C.include?(A) #=> true
1392  * A.include?(A) #=> false
1393  */
1394 
1395 VALUE
1397 {
1398  VALUE p;
1399 
1400  Check_Type(mod2, T_MODULE);
1401  for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
1402  if (BUILTIN_TYPE(p) == T_ICLASS && !FL_TEST(p, RICLASS_IS_ORIGIN)) {
1403  if (RBASIC(p)->klass == mod2) return Qtrue;
1404  }
1405  }
1406  return Qfalse;
1407 }
1408 
1409 /*
1410  * call-seq:
1411  * mod.ancestors -> array
1412  *
1413  * Returns a list of modules included/prepended in <i>mod</i>
1414  * (including <i>mod</i> itself).
1415  *
1416  * module Mod
1417  * include Math
1418  * include Comparable
1419  * prepend Enumerable
1420  * end
1421  *
1422  * Mod.ancestors #=> [Enumerable, Mod, Comparable, Math]
1423  * Math.ancestors #=> [Math]
1424  * Enumerable.ancestors #=> [Enumerable]
1425  */
1426 
1427 VALUE
1429 {
1430  VALUE p, ary = rb_ary_new();
1431  VALUE refined_class = Qnil;
1432  if (FL_TEST(mod, RMODULE_IS_REFINEMENT)) {
1433  refined_class = rb_refinement_module_get_refined_class(mod);
1434  }
1435 
1436  for (p = mod; p; p = RCLASS_SUPER(p)) {
1437  if (p == refined_class) break;
1438  if (p != RCLASS_ORIGIN(p)) continue;
1439  if (BUILTIN_TYPE(p) == T_ICLASS) {
1440  rb_ary_push(ary, RBASIC(p)->klass);
1441  }
1442  else {
1443  rb_ary_push(ary, p);
1444  }
1445  }
1446  return ary;
1447 }
1448 
1450 {
1451  VALUE buffer;
1452  long count;
1453  long maxcount;
1454  bool immediate_only;
1455 };
1456 
1457 static void
1458 class_descendants_recursive(VALUE klass, VALUE v)
1459 {
1460  struct subclass_traverse_data *data = (struct subclass_traverse_data *) v;
1461 
1462  if (BUILTIN_TYPE(klass) == T_CLASS && !FL_TEST(klass, FL_SINGLETON)) {
1463  if (data->buffer && data->count < data->maxcount && !rb_objspace_garbage_object_p(klass)) {
1464  // assumes that this does not cause GC as long as the length does not exceed the capacity
1465  rb_ary_push(data->buffer, klass);
1466  }
1467  data->count++;
1468  if (!data->immediate_only) {
1469  rb_class_foreach_subclass(klass, class_descendants_recursive, v);
1470  }
1471  }
1472  else {
1473  rb_class_foreach_subclass(klass, class_descendants_recursive, v);
1474  }
1475 }
1476 
1477 static VALUE
1478 class_descendants(VALUE klass, bool immediate_only)
1479 {
1480  struct subclass_traverse_data data = { Qfalse, 0, -1, immediate_only };
1481 
1482  // estimate the count of subclasses
1483  rb_class_foreach_subclass(klass, class_descendants_recursive, (VALUE) &data);
1484 
1485  // the following allocation may cause GC which may change the number of subclasses
1486  data.buffer = rb_ary_new_capa(data.count);
1487  data.maxcount = data.count;
1488  data.count = 0;
1489 
1490  size_t gc_count = rb_gc_count();
1491 
1492  // enumerate subclasses
1493  rb_class_foreach_subclass(klass, class_descendants_recursive, (VALUE) &data);
1494 
1495  if (gc_count != rb_gc_count()) {
1496  rb_bug("GC must not occur during the subclass iteration of Class#descendants");
1497  }
1498 
1499  return data.buffer;
1500 }
1501 
1502 /*
1503  * call-seq:
1504  * subclasses -> array
1505  *
1506  * Returns an array of classes where the receiver is the
1507  * direct superclass of the class, excluding singleton classes.
1508  * The order of the returned array is not defined.
1509  *
1510  * class A; end
1511  * class B < A; end
1512  * class C < B; end
1513  * class D < A; end
1514  *
1515  * A.subclasses #=> [D, B]
1516  * B.subclasses #=> [C]
1517  * C.subclasses #=> []
1518  */
1519 
1520 VALUE
1522 {
1523  return class_descendants(klass, true);
1524 }
1525 
1526 static void
1527 ins_methods_push(st_data_t name, st_data_t ary)
1528 {
1529  rb_ary_push((VALUE)ary, ID2SYM((ID)name));
1530 }
1531 
1532 static int
1533 ins_methods_i(st_data_t name, st_data_t type, st_data_t ary)
1534 {
1535  switch ((rb_method_visibility_t)type) {
1536  case METHOD_VISI_UNDEF:
1537  case METHOD_VISI_PRIVATE:
1538  break;
1539  default: /* everything but private */
1540  ins_methods_push(name, ary);
1541  break;
1542  }
1543  return ST_CONTINUE;
1544 }
1545 
1546 static int
1547 ins_methods_type_i(st_data_t name, st_data_t type, st_data_t ary, rb_method_visibility_t visi)
1548 {
1549  if ((rb_method_visibility_t)type == visi) {
1550  ins_methods_push(name, ary);
1551  }
1552  return ST_CONTINUE;
1553 }
1554 
1555 static int
1556 ins_methods_prot_i(st_data_t name, st_data_t type, st_data_t ary)
1557 {
1558  return ins_methods_type_i(name, type, ary, METHOD_VISI_PROTECTED);
1559 }
1560 
1561 static int
1562 ins_methods_priv_i(st_data_t name, st_data_t type, st_data_t ary)
1563 {
1564  return ins_methods_type_i(name, type, ary, METHOD_VISI_PRIVATE);
1565 }
1566 
1567 static int
1568 ins_methods_pub_i(st_data_t name, st_data_t type, st_data_t ary)
1569 {
1570  return ins_methods_type_i(name, type, ary, METHOD_VISI_PUBLIC);
1571 }
1572 
1574  st_table *list;
1575  int recur;
1576 };
1577 
1578 static enum rb_id_table_iterator_result
1579 method_entry_i(ID key, VALUE value, void *data)
1580 {
1581  const rb_method_entry_t *me = (const rb_method_entry_t *)value;
1582  struct method_entry_arg *arg = (struct method_entry_arg *)data;
1583  rb_method_visibility_t type;
1584 
1585  if (me->def->type == VM_METHOD_TYPE_REFINED) {
1586  VALUE owner = me->owner;
1587  me = rb_resolve_refined_method(Qnil, me);
1588  if (!me) return ID_TABLE_CONTINUE;
1589  if (!arg->recur && me->owner != owner) return ID_TABLE_CONTINUE;
1590  }
1591  if (!st_is_member(arg->list, key)) {
1592  if (UNDEFINED_METHOD_ENTRY_P(me)) {
1593  type = METHOD_VISI_UNDEF; /* none */
1594  }
1595  else {
1596  type = METHOD_ENTRY_VISI(me);
1597  RUBY_ASSERT(type != METHOD_VISI_UNDEF);
1598  }
1599  st_add_direct(arg->list, key, (st_data_t)type);
1600  }
1601  return ID_TABLE_CONTINUE;
1602 }
1603 
1604 static void
1605 add_instance_method_list(VALUE mod, struct method_entry_arg *me_arg)
1606 {
1607  struct rb_id_table *m_tbl = RCLASS_M_TBL(mod);
1608  if (!m_tbl) return;
1609  rb_id_table_foreach(m_tbl, method_entry_i, me_arg);
1610 }
1611 
1612 static bool
1613 particular_class_p(VALUE mod)
1614 {
1615  if (!mod) return false;
1616  if (FL_TEST(mod, FL_SINGLETON)) return true;
1617  if (BUILTIN_TYPE(mod) == T_ICLASS) return true;
1618  return false;
1619 }
1620 
1621 static VALUE
1622 class_instance_method_list(int argc, const VALUE *argv, VALUE mod, int obj, int (*func) (st_data_t, st_data_t, st_data_t))
1623 {
1624  VALUE ary;
1625  int recur = TRUE, prepended = 0;
1626  struct method_entry_arg me_arg;
1627 
1628  if (rb_check_arity(argc, 0, 1)) recur = RTEST(argv[0]);
1629 
1630  me_arg.list = st_init_numtable();
1631  me_arg.recur = recur;
1632 
1633  if (obj) {
1634  for (; particular_class_p(mod); mod = RCLASS_SUPER(mod)) {
1635  add_instance_method_list(mod, &me_arg);
1636  }
1637  }
1638 
1639  if (!recur && RCLASS_ORIGIN(mod) != mod) {
1640  mod = RCLASS_ORIGIN(mod);
1641  prepended = 1;
1642  }
1643 
1644  for (; mod; mod = RCLASS_SUPER(mod)) {
1645  add_instance_method_list(mod, &me_arg);
1646  if (BUILTIN_TYPE(mod) == T_ICLASS && !prepended) continue;
1647  if (!recur) break;
1648  }
1649  ary = rb_ary_new2(me_arg.list->num_entries);
1650  st_foreach(me_arg.list, func, ary);
1651  st_free_table(me_arg.list);
1652 
1653  return ary;
1654 }
1655 
1656 /*
1657  * call-seq:
1658  * mod.instance_methods(include_super=true) -> array
1659  *
1660  * Returns an array containing the names of the public and protected instance
1661  * methods in the receiver. For a module, these are the public and protected methods;
1662  * for a class, they are the instance (not singleton) methods. If the optional
1663  * parameter is <code>false</code>, the methods of any ancestors are not included.
1664  *
1665  * module A
1666  * def method1() end
1667  * end
1668  * class B
1669  * include A
1670  * def method2() end
1671  * end
1672  * class C < B
1673  * def method3() end
1674  * end
1675  *
1676  * A.instance_methods(false) #=> [:method1]
1677  * B.instance_methods(false) #=> [:method2]
1678  * B.instance_methods(true).include?(:method1) #=> true
1679  * C.instance_methods(false) #=> [:method3]
1680  * C.instance_methods.include?(:method2) #=> true
1681  */
1682 
1683 VALUE
1684 rb_class_instance_methods(int argc, const VALUE *argv, VALUE mod)
1685 {
1686  return class_instance_method_list(argc, argv, mod, 0, ins_methods_i);
1687 }
1688 
1689 /*
1690  * call-seq:
1691  * mod.protected_instance_methods(include_super=true) -> array
1692  *
1693  * Returns a list of the protected instance methods defined in
1694  * <i>mod</i>. If the optional parameter is <code>false</code>, the
1695  * methods of any ancestors are not included.
1696  */
1697 
1698 VALUE
1700 {
1701  return class_instance_method_list(argc, argv, mod, 0, ins_methods_prot_i);
1702 }
1703 
1704 /*
1705  * call-seq:
1706  * mod.private_instance_methods(include_super=true) -> array
1707  *
1708  * Returns a list of the private instance methods defined in
1709  * <i>mod</i>. If the optional parameter is <code>false</code>, the
1710  * methods of any ancestors are not included.
1711  *
1712  * module Mod
1713  * def method1() end
1714  * private :method1
1715  * def method2() end
1716  * end
1717  * Mod.instance_methods #=> [:method2]
1718  * Mod.private_instance_methods #=> [:method1]
1719  */
1720 
1721 VALUE
1722 rb_class_private_instance_methods(int argc, const VALUE *argv, VALUE mod)
1723 {
1724  return class_instance_method_list(argc, argv, mod, 0, ins_methods_priv_i);
1725 }
1726 
1727 /*
1728  * call-seq:
1729  * mod.public_instance_methods(include_super=true) -> array
1730  *
1731  * Returns a list of the public instance methods defined in <i>mod</i>.
1732  * If the optional parameter is <code>false</code>, the methods of
1733  * any ancestors are not included.
1734  */
1735 
1736 VALUE
1737 rb_class_public_instance_methods(int argc, const VALUE *argv, VALUE mod)
1738 {
1739  return class_instance_method_list(argc, argv, mod, 0, ins_methods_pub_i);
1740 }
1741 
1742 /*
1743  * call-seq:
1744  * obj.methods(regular=true) -> array
1745  *
1746  * Returns a list of the names of public and protected methods of
1747  * <i>obj</i>. This will include all the methods accessible in
1748  * <i>obj</i>'s ancestors.
1749  * If the optional parameter is <code>false</code>, it
1750  * returns an array of <i>obj</i>'s public and protected singleton methods,
1751  * the array will not include methods in modules included in <i>obj</i>.
1752  *
1753  * class Klass
1754  * def klass_method()
1755  * end
1756  * end
1757  * k = Klass.new
1758  * k.methods[0..9] #=> [:klass_method, :nil?, :===,
1759  * # :==~, :!, :eql?
1760  * # :hash, :<=>, :class, :singleton_class]
1761  * k.methods.length #=> 56
1762  *
1763  * k.methods(false) #=> []
1764  * def k.singleton_method; end
1765  * k.methods(false) #=> [:singleton_method]
1766  *
1767  * module M123; def m123; end end
1768  * k.extend M123
1769  * k.methods(false) #=> [:singleton_method]
1770  */
1771 
1772 VALUE
1773 rb_obj_methods(int argc, const VALUE *argv, VALUE obj)
1774 {
1775  rb_check_arity(argc, 0, 1);
1776  if (argc > 0 && !RTEST(argv[0])) {
1777  return rb_obj_singleton_methods(argc, argv, obj);
1778  }
1779  return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_i);
1780 }
1781 
1782 /*
1783  * call-seq:
1784  * obj.protected_methods(all=true) -> array
1785  *
1786  * Returns the list of protected methods accessible to <i>obj</i>. If
1787  * the <i>all</i> parameter is set to <code>false</code>, only those methods
1788  * in the receiver will be listed.
1789  */
1790 
1791 VALUE
1792 rb_obj_protected_methods(int argc, const VALUE *argv, VALUE obj)
1793 {
1794  return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_prot_i);
1795 }
1796 
1797 /*
1798  * call-seq:
1799  * obj.private_methods(all=true) -> array
1800  *
1801  * Returns the list of private methods accessible to <i>obj</i>. If
1802  * the <i>all</i> parameter is set to <code>false</code>, only those methods
1803  * in the receiver will be listed.
1804  */
1805 
1806 VALUE
1807 rb_obj_private_methods(int argc, const VALUE *argv, VALUE obj)
1808 {
1809  return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_priv_i);
1810 }
1811 
1812 /*
1813  * call-seq:
1814  * obj.public_methods(all=true) -> array
1815  *
1816  * Returns the list of public methods accessible to <i>obj</i>. If
1817  * the <i>all</i> parameter is set to <code>false</code>, only those methods
1818  * in the receiver will be listed.
1819  */
1820 
1821 VALUE
1822 rb_obj_public_methods(int argc, const VALUE *argv, VALUE obj)
1823 {
1824  return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_pub_i);
1825 }
1826 
1827 /*
1828  * call-seq:
1829  * obj.singleton_methods(all=true) -> array
1830  *
1831  * Returns an array of the names of singleton methods for <i>obj</i>.
1832  * If the optional <i>all</i> parameter is true, the list will include
1833  * methods in modules included in <i>obj</i>.
1834  * Only public and protected singleton methods are returned.
1835  *
1836  * module Other
1837  * def three() end
1838  * end
1839  *
1840  * class Single
1841  * def Single.four() end
1842  * end
1843  *
1844  * a = Single.new
1845  *
1846  * def a.one()
1847  * end
1848  *
1849  * class << a
1850  * include Other
1851  * def two()
1852  * end
1853  * end
1854  *
1855  * Single.singleton_methods #=> [:four]
1856  * a.singleton_methods(false) #=> [:two, :one]
1857  * a.singleton_methods #=> [:two, :one, :three]
1858  */
1859 
1860 VALUE
1861 rb_obj_singleton_methods(int argc, const VALUE *argv, VALUE obj)
1862 {
1863  VALUE ary, klass, origin;
1864  struct method_entry_arg me_arg;
1865  struct rb_id_table *mtbl;
1866  int recur = TRUE;
1867 
1868  if (rb_check_arity(argc, 0, 1)) recur = RTEST(argv[0]);
1869  if (RB_TYPE_P(obj, T_CLASS) && FL_TEST(obj, FL_SINGLETON)) {
1870  rb_singleton_class(obj);
1871  }
1872  klass = CLASS_OF(obj);
1873  origin = RCLASS_ORIGIN(klass);
1874  me_arg.list = st_init_numtable();
1875  me_arg.recur = recur;
1876  if (klass && FL_TEST(klass, FL_SINGLETON)) {
1877  if ((mtbl = RCLASS_M_TBL(origin)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
1878  klass = RCLASS_SUPER(klass);
1879  }
1880  if (recur) {
1881  while (klass && (FL_TEST(klass, FL_SINGLETON) || RB_TYPE_P(klass, T_ICLASS))) {
1882  if (klass != origin && (mtbl = RCLASS_M_TBL(klass)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
1883  klass = RCLASS_SUPER(klass);
1884  }
1885  }
1886  ary = rb_ary_new2(me_arg.list->num_entries);
1887  st_foreach(me_arg.list, ins_methods_i, ary);
1888  st_free_table(me_arg.list);
1889 
1890  return ary;
1891 }
1892 
1901 #ifdef rb_define_method_id
1902 #undef rb_define_method_id
1903 #endif
1904 void
1905 rb_define_method_id(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc)
1906 {
1907  rb_add_method_cfunc(klass, mid, func, argc, METHOD_VISI_PUBLIC);
1908 }
1909 
1910 #ifdef rb_define_method
1911 #undef rb_define_method
1912 #endif
1913 void
1914 rb_define_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
1915 {
1916  rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PUBLIC);
1917 }
1918 
1919 #ifdef rb_define_protected_method
1920 #undef rb_define_protected_method
1921 #endif
1922 void
1923 rb_define_protected_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
1924 {
1925  rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PROTECTED);
1926 }
1927 
1928 #ifdef rb_define_private_method
1929 #undef rb_define_private_method
1930 #endif
1931 void
1932 rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
1933 {
1934  rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PRIVATE);
1935 }
1936 
1937 void
1938 rb_undef_method(VALUE klass, const char *name)
1939 {
1940  rb_add_method(klass, rb_intern(name), VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
1941 }
1942 
1943 static enum rb_id_table_iterator_result
1944 undef_method_i(ID name, VALUE value, void *data)
1945 {
1946  VALUE klass = (VALUE)data;
1947  rb_add_method(klass, name, VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
1948  return ID_TABLE_CONTINUE;
1949 }
1950 
1951 void
1952 rb_undef_methods_from(VALUE klass, VALUE super)
1953 {
1954  struct rb_id_table *mtbl = RCLASS_M_TBL(super);
1955  if (mtbl) {
1956  rb_id_table_foreach(mtbl, undef_method_i, (void *)klass);
1957  }
1958 }
1959 
1968 static inline VALUE
1969 special_singleton_class_of(VALUE obj)
1970 {
1971  switch (obj) {
1972  case Qnil: return rb_cNilClass;
1973  case Qfalse: return rb_cFalseClass;
1974  case Qtrue: return rb_cTrueClass;
1975  default: return Qnil;
1976  }
1977 }
1978 
1979 VALUE
1980 rb_special_singleton_class(VALUE obj)
1981 {
1982  return special_singleton_class_of(obj);
1983 }
1984 
1994 static VALUE
1995 singleton_class_of(VALUE obj)
1996 {
1997  VALUE klass;
1998 
1999  switch (TYPE(obj)) {
2000  case T_FIXNUM:
2001  case T_BIGNUM:
2002  case T_FLOAT:
2003  case T_SYMBOL:
2004  rb_raise(rb_eTypeError, "can't define singleton");
2005 
2006  case T_FALSE:
2007  case T_TRUE:
2008  case T_NIL:
2009  klass = special_singleton_class_of(obj);
2010  if (NIL_P(klass))
2011  rb_bug("unknown immediate %p", (void *)obj);
2012  return klass;
2013 
2014  case T_STRING:
2015  if (FL_TEST_RAW(obj, RSTRING_FSTR)) {
2016  rb_raise(rb_eTypeError, "can't define singleton");
2017  }
2018  }
2019 
2020  klass = RBASIC(obj)->klass;
2021  if (!(FL_TEST(klass, FL_SINGLETON) &&
2022  rb_attr_get(klass, id_attached) == obj)) {
2023  rb_serial_t serial = RCLASS_SERIAL(klass);
2024  klass = rb_make_metaclass(obj, klass);
2025  RCLASS_SERIAL(klass) = serial;
2026  }
2027 
2028  RB_FL_SET_RAW(klass, RB_OBJ_FROZEN_RAW(obj));
2029 
2030  return klass;
2031 }
2032 
2033 void
2035 {
2036  /* should not propagate to meta-meta-class, and so on */
2037  if (!(RBASIC(x)->flags & FL_SINGLETON)) {
2038  VALUE klass = RBASIC_CLASS(x);
2039  if (klass && (klass = RCLASS_ORIGIN(klass)) != 0 &&
2040  FL_TEST(klass, (FL_SINGLETON|FL_FREEZE)) == FL_SINGLETON) {
2041  OBJ_FREEZE_RAW(klass);
2042  }
2043  }
2044 }
2045 
2053 VALUE
2055 {
2056  VALUE klass;
2057 
2058  if (SPECIAL_CONST_P(obj)) {
2059  return rb_special_singleton_class(obj);
2060  }
2061  klass = RBASIC(obj)->klass;
2062  if (!FL_TEST(klass, FL_SINGLETON)) return Qnil;
2063  if (rb_attr_get(klass, id_attached) != obj) return Qnil;
2064  return klass;
2065 }
2066 
2067 VALUE
2069 {
2070  VALUE klass = singleton_class_of(obj);
2071 
2072  /* ensures an exposed class belongs to its own eigenclass */
2073  if (RB_TYPE_P(obj, T_CLASS)) (void)ENSURE_EIGENCLASS(klass);
2074 
2075  return klass;
2076 }
2077 
2087 #ifdef rb_define_singleton_method
2088 #undef rb_define_singleton_method
2089 #endif
2090 void
2091 rb_define_singleton_method(VALUE obj, const char *name, VALUE (*func)(ANYARGS), int argc)
2092 {
2093  rb_define_method(singleton_class_of(obj), name, func, argc);
2094 }
2095 
2096 #ifdef rb_define_module_function
2097 #undef rb_define_module_function
2098 #endif
2099 void
2100 rb_define_module_function(VALUE module, const char *name, VALUE (*func)(ANYARGS), int argc)
2101 {
2102  rb_define_private_method(module, name, func, argc);
2103  rb_define_singleton_method(module, name, func, argc);
2104 }
2105 
2106 #ifdef rb_define_global_function
2107 #undef rb_define_global_function
2108 #endif
2109 void
2110 rb_define_global_function(const char *name, VALUE (*func)(ANYARGS), int argc)
2111 {
2112  rb_define_module_function(rb_mKernel, name, func, argc);
2113 }
2114 
2115 void
2116 rb_define_alias(VALUE klass, const char *name1, const char *name2)
2117 {
2118  rb_alias(klass, rb_intern(name1), rb_intern(name2));
2119 }
2120 
2121 void
2122 rb_define_attr(VALUE klass, const char *name, int read, int write)
2123 {
2124  rb_attr(klass, rb_intern(name), read, write, FALSE);
2125 }
2126 
2127 MJIT_FUNC_EXPORTED VALUE
2128 rb_keyword_error_new(const char *error, VALUE keys)
2129 {
2130  long i = 0, len = RARRAY_LEN(keys);
2131  VALUE error_message = rb_sprintf("%s keyword%.*s", error, len > 1, "s");
2132 
2133  if (len > 0) {
2134  rb_str_cat_cstr(error_message, ": ");
2135  while (1) {
2136  const VALUE k = RARRAY_AREF(keys, i);
2137  rb_str_append(error_message, rb_inspect(k));
2138  if (++i >= len) break;
2139  rb_str_cat_cstr(error_message, ", ");
2140  }
2141  }
2142 
2143  return rb_exc_new_str(rb_eArgError, error_message);
2144 }
2145 
2146 NORETURN(static void rb_keyword_error(const char *error, VALUE keys));
2147 static void
2148 rb_keyword_error(const char *error, VALUE keys)
2149 {
2150  rb_exc_raise(rb_keyword_error_new(error, keys));
2151 }
2152 
2153 NORETURN(static void unknown_keyword_error(VALUE hash, const ID *table, int keywords));
2154 static void
2155 unknown_keyword_error(VALUE hash, const ID *table, int keywords)
2156 {
2157  int i;
2158  for (i = 0; i < keywords; i++) {
2159  st_data_t key = ID2SYM(table[i]);
2160  rb_hash_stlike_delete(hash, &key, NULL);
2161  }
2162  rb_keyword_error("unknown", rb_hash_keys(hash));
2163 }
2164 
2165 
2166 static int
2167 separate_symbol(st_data_t key, st_data_t value, st_data_t arg)
2168 {
2169  VALUE *kwdhash = (VALUE *)arg;
2170  if (!SYMBOL_P(key)) kwdhash++;
2171  if (!*kwdhash) *kwdhash = rb_hash_new();
2172  rb_hash_aset(*kwdhash, (VALUE)key, (VALUE)value);
2173  return ST_CONTINUE;
2174 }
2175 
2176 VALUE
2178 {
2179  VALUE parthash[2] = {0, 0};
2180  VALUE hash = *orighash;
2181 
2182  if (RHASH_EMPTY_P(hash)) {
2183  *orighash = 0;
2184  return hash;
2185  }
2186  rb_hash_foreach(hash, separate_symbol, (st_data_t)&parthash);
2187  *orighash = parthash[1];
2188  if (parthash[1] && RBASIC_CLASS(hash) != rb_cHash) {
2189  RBASIC_SET_CLASS(parthash[1], RBASIC_CLASS(hash));
2190  }
2191  return parthash[0];
2192 }
2193 
2194 int
2195 rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
2196 {
2197  int i = 0, j;
2198  int rest = 0;
2199  VALUE missing = Qnil;
2200  st_data_t key;
2201 
2202 #define extract_kwarg(keyword, val) \
2203  (key = (st_data_t)(keyword), values ? \
2204  (rb_hash_stlike_delete(keyword_hash, &key, &(val)) || ((val) = Qundef, 0)) : \
2205  rb_hash_stlike_lookup(keyword_hash, key, NULL))
2206 
2207  if (NIL_P(keyword_hash)) keyword_hash = 0;
2208 
2209  if (optional < 0) {
2210  rest = 1;
2211  optional = -1-optional;
2212  }
2213  if (required) {
2214  for (; i < required; i++) {
2215  VALUE keyword = ID2SYM(table[i]);
2216  if (keyword_hash) {
2217  if (extract_kwarg(keyword, values[i])) {
2218  continue;
2219  }
2220  }
2221  if (NIL_P(missing)) missing = rb_ary_tmp_new(1);
2222  rb_ary_push(missing, keyword);
2223  }
2224  if (!NIL_P(missing)) {
2225  rb_keyword_error("missing", missing);
2226  }
2227  }
2228  j = i;
2229  if (optional && keyword_hash) {
2230  for (i = 0; i < optional; i++) {
2231  if (extract_kwarg(ID2SYM(table[required+i]), values[required+i])) {
2232  j++;
2233  }
2234  }
2235  }
2236  if (!rest && keyword_hash) {
2237  if (RHASH_SIZE(keyword_hash) > (unsigned int)(values ? 0 : j)) {
2238  unknown_keyword_error(keyword_hash, table, required+optional);
2239  }
2240  }
2241  if (values && !keyword_hash) {
2242  for (i = 0; i < required + optional; i++) {
2243  values[i] = Qundef;
2244  }
2245  }
2246  return j;
2247 #undef extract_kwarg
2248 }
2249 
2251  int kw_flag;
2252  int n_lead;
2253  int n_opt;
2254  int n_trail;
2255  bool f_var;
2256  bool f_hash;
2257  bool f_block;
2258 };
2259 
2260 static void
2261 rb_scan_args_parse(int kw_flag, const char *fmt, struct rb_scan_args_t *arg)
2262 {
2263  const char *p = fmt;
2264 
2265  memset(arg, 0, sizeof(*arg));
2266  arg->kw_flag = kw_flag;
2267 
2268  if (ISDIGIT(*p)) {
2269  arg->n_lead = *p - '0';
2270  p++;
2271  if (ISDIGIT(*p)) {
2272  arg->n_opt = *p - '0';
2273  p++;
2274  }
2275  }
2276  if (*p == '*') {
2277  arg->f_var = 1;
2278  p++;
2279  }
2280  if (ISDIGIT(*p)) {
2281  arg->n_trail = *p - '0';
2282  p++;
2283  }
2284  if (*p == ':') {
2285  arg->f_hash = 1;
2286  p++;
2287  }
2288  if (*p == '&') {
2289  arg->f_block = 1;
2290  p++;
2291  }
2292  if (*p != '\0') {
2293  rb_fatal("bad scan arg format: %s", fmt);
2294  }
2295 }
2296 
2297 static int
2298 rb_scan_args_assign(const struct rb_scan_args_t *arg, int argc, const VALUE *const argv, va_list vargs)
2299 {
2300  int i, argi = 0;
2301  VALUE *var, hash = Qnil;
2302 #define rb_scan_args_next_param() va_arg(vargs, VALUE *)
2303  const int kw_flag = arg->kw_flag;
2304  const int n_lead = arg->n_lead;
2305  const int n_opt = arg->n_opt;
2306  const int n_trail = arg->n_trail;
2307  const int n_mand = n_lead + n_trail;
2308  const bool f_var = arg->f_var;
2309  const bool f_hash = arg->f_hash;
2310  const bool f_block = arg->f_block;
2311 
2312  /* capture an option hash - phase 1: pop from the argv */
2313  if (f_hash && argc > 0) {
2314  VALUE last = argv[argc - 1];
2315  if (rb_scan_args_keyword_p(kw_flag, last)) {
2316  hash = rb_hash_dup(last);
2317  argc--;
2318  }
2319  }
2320 
2321  if (argc < n_mand) {
2322  goto argc_error;
2323  }
2324 
2325  /* capture leading mandatory arguments */
2326  for (i = 0; i < n_lead; i++) {
2327  var = rb_scan_args_next_param();
2328  if (var) *var = argv[argi];
2329  argi++;
2330  }
2331  /* capture optional arguments */
2332  for (i = 0; i < n_opt; i++) {
2333  var = rb_scan_args_next_param();
2334  if (argi < argc - n_trail) {
2335  if (var) *var = argv[argi];
2336  argi++;
2337  }
2338  else {
2339  if (var) *var = Qnil;
2340  }
2341  }
2342  /* capture variable length arguments */
2343  if (f_var) {
2344  int n_var = argc - argi - n_trail;
2345 
2346  var = rb_scan_args_next_param();
2347  if (0 < n_var) {
2348  if (var) *var = rb_ary_new_from_values(n_var, &argv[argi]);
2349  argi += n_var;
2350  }
2351  else {
2352  if (var) *var = rb_ary_new();
2353  }
2354  }
2355  /* capture trailing mandatory arguments */
2356  for (i = 0; i < n_trail; i++) {
2357  var = rb_scan_args_next_param();
2358  if (var) *var = argv[argi];
2359  argi++;
2360  }
2361  /* capture an option hash - phase 2: assignment */
2362  if (f_hash) {
2363  var = rb_scan_args_next_param();
2364  if (var) *var = hash;
2365  }
2366  /* capture iterator block */
2367  if (f_block) {
2368  var = rb_scan_args_next_param();
2369  if (rb_block_given_p()) {
2370  *var = rb_block_proc();
2371  }
2372  else {
2373  *var = Qnil;
2374  }
2375  }
2376 
2377  if (argi == argc) {
2378  return argc;
2379  }
2380 
2381  argc_error:
2382  return -(argc + 1);
2383 #undef rb_scan_args_next_param
2384 }
2385 
2386 static int
2387 rb_scan_args_result(const struct rb_scan_args_t *const arg, int argc)
2388 {
2389  const int n_lead = arg->n_lead;
2390  const int n_opt = arg->n_opt;
2391  const int n_trail = arg->n_trail;
2392  const int n_mand = n_lead + n_trail;
2393  const bool f_var = arg->f_var;
2394 
2395  if (argc >= 0) {
2396  return argc;
2397  }
2398 
2399  argc = -argc - 1;
2400  rb_error_arity(argc, n_mand, f_var ? UNLIMITED_ARGUMENTS : n_mand + n_opt);
2401  UNREACHABLE_RETURN(-1);
2402 }
2403 
2404 #undef rb_scan_args
2405 int
2406 rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...)
2407 {
2408  va_list vargs;
2409  struct rb_scan_args_t arg;
2410  rb_scan_args_parse(RB_SCAN_ARGS_PASS_CALLED_KEYWORDS, fmt, &arg);
2411  va_start(vargs,fmt);
2412  argc = rb_scan_args_assign(&arg, argc, argv, vargs);
2413  va_end(vargs);
2414  return rb_scan_args_result(&arg, argc);
2415 }
2416 
2417 #undef rb_scan_args_kw
2418 int
2419 rb_scan_args_kw(int kw_flag, int argc, const VALUE *argv, const char *fmt, ...)
2420 {
2421  va_list vargs;
2422  struct rb_scan_args_t arg;
2423  rb_scan_args_parse(kw_flag, fmt, &arg);
2424  va_start(vargs,fmt);
2425  argc = rb_scan_args_assign(&arg, argc, argv, vargs);
2426  va_end(vargs);
2427  return rb_scan_args_result(&arg, argc);
2428 }
2429 
#define RUBY_ASSERT(expr)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition: assert.h:177
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition: dllexport.h:47
static VALUE RB_OBJ_FROZEN_RAW(VALUE obj)
This is an implenentation detail of RB_OBJ_FROZEN().
Definition: fl_type.h:912
static void RB_FL_SET_RAW(VALUE obj, VALUE flags)
This is an implenentation detail of RB_FL_SET().
Definition: fl_type.h:644
@ RUBY_FL_USER5
User-defined flag.
Definition: fl_type.h:365
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:1699
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
Definition: class.c:1043
VALUE rb_refinement_new(void)
Creates a new, anonymous refinement.
Definition: class.c:935
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:837
VALUE rb_class_new(VALUE super)
Creates a new, anonymous class.
Definition: class.c:275
static VALUE make_singleton_class(VALUE obj)
Creates a singleton class for obj.
Definition: class.c:678
VALUE rb_singleton_class_clone(VALUE obj)
Clones a singleton class.
Definition: class.c:522
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:1289
VALUE rb_class_subclasses(VALUE klass)
Queries the class's direct descendants.
Definition: class.c:1521
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
Definition: class.c:2068
void Init_class_hierarchy(void)
Internal header aggregating init functions.
Definition: class.c:756
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:869
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:1861
VALUE rb_module_new(void)
Creates a new, anonymous module.
Definition: class.c:929
#define META_CLASS_OF_CLASS_CLASS_P(k)
whether k is a meta^(n)-class of Class class
Definition: class.c:601
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:1684
void rb_check_inheritable(VALUE super)
Asserts that the given class can derive a child class.
Definition: class.c:260
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:1737
VALUE rb_class_boot(VALUE super)
A utility function that wraps class_alloc.
Definition: class.c:249
VALUE rb_define_module(const char *name)
Defines a top-level module.
Definition: class.c:948
void rb_class_modify_check(VALUE klass)
Asserts that klass is not a frozen class.
Definition: eval.c:422
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:978
void rb_singleton_class_attached(VALUE klass, VALUE obj)
Attaches a singleton class to its corresponding object.
Definition: class.c:589
void rb_freeze_singleton_class(VALUE x)
This is an implementation detail of RB_OBJ_FREEZE().
Definition: class.c:2034
VALUE rb_mod_included_modules(VALUE mod)
Queries the list of included modules.
Definition: class.c:1360
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:875
VALUE rb_mod_ancestors(VALUE mod)
Queries the module's ancestors.
Definition: class.c:1428
static VALUE make_metaclass(VALUE klass)
Creates a metaclass of klass.
Definition: class.c:646
static VALUE class_alloc(VALUE flags, VALUE klass)
Allocates a struct RClass for a new class.
Definition: class.c:196
VALUE rb_class_inherited(VALUE super, VALUE klass)
Calls Class::inherited.
Definition: class.c:828
VALUE rb_mod_include_p(VALUE mod, VALUE mod2)
Queries if the passed module is included by the module.
Definition: class.c:1396
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:1722
#define ENSURE_EIGENCLASS(klass)
ensures klass belongs to its own eigenclass.
Definition: class.c:632
VALUE rb_mod_init_copy(VALUE clone, VALUE orig)
The comment that comes with this function says :nodoc:.
Definition: class.c:414
VALUE rb_define_module_under(VALUE outer, const char *name)
Defines a module under the namespace of outer.
Definition: class.c:972
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:2054
VALUE rb_define_module_id(ID id)
This is a very badly designed API that creates an anonymous module.
Definition: class.c:942
VALUE rb_define_class_id(ID id, VALUE super)
This is a very badly designed API that creates an anonymous class.
Definition: class.c:807
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition: class.c:2116
void rb_define_module_function(VALUE module, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a module function for a module.
Definition: class.c:2100
void rb_define_protected_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Identical to rb_define_method(), except it defines a protected method.
Definition: class.c:1923
VALUE rb_extract_keywords(VALUE *orighash)
Splits a hash into two.
Definition: class.c:2177
void rb_define_attr(VALUE klass, const char *name, int read, int write)
Defines public accessor method(s) for an attribute.
Definition: class.c:2122
void rb_define_private_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Identical to rb_define_method(), except it defines a private method.
Definition: class.c:1932
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
Definition: class.c:1938
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:2419
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:2406
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a method.
Definition: class.c:1914
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Identical to rb_define_method(), except it defines a singleton method.
Definition: class.c:2091
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition: eval.c:854
void rb_define_method_id(VALUE klass, ID mid, VALUE(*func)(ANYARGS), int argc)
Identical to rb_define_method(), except it takes the name of the method in ID instead of C's string.
Definition: class.c:1905
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
Definition: class.c:2195
void rb_define_global_function(const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a global function.
Definition: class.c:2110
#define TYPE(_)
Old name of rb_type.
Definition: value_type.h:107
#define FL_SINGLETON
Old name of RUBY_FL_SINGLETON.
Definition: fl_type.h:58
#define FL_UNSET_RAW
Old name of RB_FL_UNSET_RAW.
Definition: fl_type.h:142
#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:394
#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 T_MASK
Old name of RUBY_T_MASK.
Definition: value_type.h:68
#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_RAW
Old name of RB_OBJ_FREEZE_RAW.
Definition: fl_type.h:144
#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:31
#define ZALLOC
Old name of RB_ZALLOC.
Definition: memory.h:396
#define CLASS_OF
Old name of rb_class_of.
Definition: globals.h:203
#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:140
#define FL_SET
Old name of RB_FL_SET.
Definition: fl_type.h:137
#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:139
#define FL_PROMOTED1
Old name of RUBY_FL_PROMOTED1.
Definition: fl_type.h:61
#define FL_FREEZE
Old name of RUBY_FL_FREEZE.
Definition: fl_type.h:68
#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:651
#define SYMBOL_P
Old name of RB_SYMBOL_P.
Definition: value_type.h:88
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
Definition: error.c:3025
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition: eval.c:675
void rb_bug(const char *fmt,...)
Interpreter panic switch.
Definition: error.c:802
VALUE rb_eTypeError
TypeError exception.
Definition: error.c:1099
void rb_fatal(const char *fmt,...)
Raises the unsung "fatal" exception.
Definition: error.c:3076
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:1150
VALUE rb_eArgError
ArgumentError exception.
Definition: error.c:1100
VALUE rb_cClass
Class class.
Definition: object.c:52
VALUE rb_mKernel
Kernel module.
Definition: object.c:49
VALUE rb_cRefinement
Refinement class.
Definition: object.c:53
VALUE rb_cNilClass
NilClass class.
Definition: object.c:55
VALUE rb_cHash
Hash class.
Definition: hash.c:92
VALUE rb_cFalseClass
FalseClass class.
Definition: object.c:57
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition: object.c:188
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
Definition: object.c:564
VALUE rb_cBasicObject
BasicObject class.
Definition: object.c:48
VALUE rb_cModule
Module class.
Definition: object.c:51
VALUE rb_class_real(VALUE klass)
Finds a "real" class.
Definition: object.c:178
VALUE rb_cTrueClass
TrueClass class.
Definition: object.c:56
#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: rgengc.h:232
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition: rgengc.h:220
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition: vm_eval.c:1102
void rb_gc_register_mark_object(VALUE object)
Inform the garbage collector that object is a live Ruby object that should not be moved.
Definition: gc.c:8687
VALUE rb_ary_new_from_values(long n, const VALUE *elts)
Identical to rb_ary_new_from_args(), except how objects are passed.
Definition: array.c:789
VALUE rb_ary_cat(VALUE ary, const VALUE *train, long len)
Destructively appends multiple elements at the end of the array.
Definition: array.c:1321
VALUE rb_ary_new(void)
Allocates a new, empty array.
Definition: array.c:750
VALUE rb_ary_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
Definition: array.c:744
VALUE rb_ary_resize(VALUE ary, long len)
Expands or shrinks the passed array to the passed length.
Definition: array.c:2234
VALUE rb_ary_tmp_new(long capa)
Allocates a "temporary" array.
Definition: array.c:847
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
Definition: array.c:1308
#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:294
size_t rb_gc_count(void)
Identical to rb_gc_stat(), with "count" parameter.
Definition: gc.c:10325
void rb_hash_foreach(VALUE hash, int(*func)(VALUE key, VALUE val, VALUE arg), VALUE arg)
Iterates over a hash.
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Inserts or replaces ("upsert"s) the objects into the given hash table.
Definition: hash.c:2903
VALUE rb_hash_dup(VALUE hash)
Duplicates a hash.
Definition: hash.c:1585
VALUE rb_hash_new(void)
Creates a new, empty hash object.
Definition: hash.c:1529
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
Definition: proc.c:848
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
Definition: string.c:3317
VALUE rb_str_cat_cstr(VALUE dst, const char *src)
Identical to rb_str_cat(), except it assumes the passed pointer is a pointer to a C string.
Definition: string.c:3171
VALUE rb_const_get(VALUE space, ID name)
Identical to rb_const_defined(), except it returns the actual defined value.
Definition: variable.c:2733
VALUE rb_attr_get(VALUE obj, ID name)
Identical to rb_ivar_get()
Definition: variable.c:1293
void rb_const_set(VALUE space, ID name, VALUE val)
Names a constant.
Definition: variable.c:3106
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:2739
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:215
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:3043
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
Definition: variable.c:172
int rb_const_defined(VALUE space, ID name)
Queries if the constant is defined at the namespace.
Definition: variable.c:3037
void rb_alias(VALUE klass, ID dst, ID src)
Resembles alias.
Definition: vm_method.c:2100
void rb_clear_constant_cache(void)
Clears the constant cache.
Definition: vm_method.c:130
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:1680
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
Definition: symbol.h:276
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
Definition: symbol.c:782
VALUE rb_id2str(ID id)
Identical to rb_id2name(), except it returns a Ruby's String instead of C's.
Definition: symbol.c:935
VALUE rb_sprintf(const char *fmt,...)
Ruby's extended sprintf(3).
Definition: sprintf.c:1201
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
Definition: memory.h:366
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition: cxxanyargs.hpp:56
int st_foreach(st_table *q, int_type *w, st_data_t e)
Iteration over the given table.
Definition: cxxanyargs.hpp:432
#define RARRAY_LEN
Just another name of rb_array_len.
Definition: rarray.h:68
#define RARRAY_AREF(a, i)
Definition: rarray.h:588
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
Definition: rbasic.h:152
#define RBASIC(obj)
Convenient casting macro.
Definition: rbasic.h:40
#define RCLASS_SUPER
Just another name of rb_class_get_superclass.
Definition: rclass.h:46
#define RCLASS(obj)
Convenient casting macro.
Definition: rclass.h:40
#define RGENGC_WB_PROTECTED_CLASS
This is a compile-time flag to enable/disable write barrier for struct RClass.
Definition: rgengc.h:140
#define RHASH_SIZE(h)
Queries the size of the hash.
Definition: rhash.h:82
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
Definition: rhash.h:92
#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.h:67
Definition: class.c:1573
Definition: constant.h:33
CREF (Class REFerence)
Definition: method.h:44
Definition: method.h:54
rb_cref_t * cref
class reference, should be marked
Definition: method.h:136
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
Definition: method.h:135
Internal header for Class.
Definition: class.h:22
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:432
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:375