17#include "ruby/internal/config.h"
21#include "debug_counter.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"
70#define METACLASS_OF(k) RBASIC(k)->klass
71#define SET_METACLASS_OF(k, cls) RBASIC_SET_CLASS(k, cls)
80static enum rb_id_table_iterator_result
81duplicate_classext_id_table_i(
ID key,
VALUE value,
void *data)
84 rb_id_table_insert(tbl, key, value);
85 return ID_TABLE_CONTINUE;
88static enum rb_id_table_iterator_result
89duplicate_classext_m_tbl_i(
ID key,
VALUE value,
void *data)
93 rb_method_table_insert0(arg->klass, arg->tbl, key, me,
false);
94 return ID_TABLE_CONTINUE;
98duplicate_classext_m_tbl(
struct rb_id_table *orig,
VALUE klass,
bool init_missing)
103 return rb_id_table_create(0);
107 tbl = rb_id_table_create(rb_id_table_size(orig));
112 rb_id_table_foreach(orig, duplicate_classext_m_tbl_i, &data);
117duplicate_classext_id_table(
struct rb_id_table *orig,
bool init_missing)
123 return rb_id_table_create(0);
127 tbl = rb_id_table_create(rb_id_table_size(orig));
128 rb_id_table_foreach(orig, duplicate_classext_id_table_i, tbl);
138 dst->flag = src->flag;
139 dst->line = src->line;
146static enum rb_id_table_iterator_result
147duplicate_classext_const_tbl_i(
ID key,
VALUE value,
void *data)
152 rb_id_table_insert(arg->tbl, key, (
VALUE)entry);
154 return ID_TABLE_CONTINUE;
165 dst = rb_id_table_create(rb_id_table_size(src));
171 rb_id_table_foreach(src, duplicate_classext_const_tbl_i, (
void *)&data);
182 return (
VALUE)ns->ns_id;
193 if (RCLASSEXT_SUBCLASSES(orig)) {
194 orig_anchor = RCLASSEXT_SUBCLASSES(orig);
195 ns_subclasses = orig_anchor->ns_subclasses;
199 anchor->ns_subclasses = rb_ns_subclasses_ref_inc(ns_subclasses);
204 RCLASSEXT_SUBCLASSES(copy) = anchor;
207 entry = orig_anchor->head;
212 if (rb_objspace_garbage_object_p(entry->klass)) {
217 cdr->klass = entry->klass;
221 VALUE ns_id = namespace_subclasses_tbl_key(RCLASSEXT_NS(copy));
223 st_insert(tbl, ns_id, (st_data_t)first);
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));
242 rb_classext_t *ext = RCLASS_EXT_TABLE_LOOKUP_INTERNAL(iclass, ns);
247 rb_invalidate_method_caches(RCLASSEXT_CALLABLE_M_TBL(ext), RCLASSEXT_CC_TBL(ext));
252 RCLASSEXT_NS(ext) = ns;
254 RCLASSEXT_SUPER(ext) = RCLASSEXT_SUPER(src);
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);
261 RCLASSEXT_M_TBL(ext) = RCLASSEXT_M_TBL(mod_ext);
264 RCLASSEXT_CONST_TBL(ext) = RCLASSEXT_CONST_TBL(mod_ext);
265 RCLASSEXT_CVC_TBL(ext) = RCLASSEXT_CVC_TBL(mod_ext);
272 duplicate_classext_subclasses(src, ext);
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);
278 RCLASSEXT_SET_INCLUDER(ext, iclass, RCLASSEXT_INCLUDER(src));
280 first_set = RCLASS_SET_NAMESPACE_CLASSEXT(iclass, ns, ext);
282 RCLASS_SET_PRIME_CLASSEXT_WRITABLE(iclass,
false);
294 RCLASSEXT_NS(ext) = ns;
296 RCLASSEXT_SUPER(ext) = RCLASSEXT_SUPER(orig);
298 RCLASSEXT_M_TBL(ext) = duplicate_classext_m_tbl(RCLASSEXT_M_TBL(orig), klass, dup_iclass);
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);
308 RCLASSEXT_FIELDS(ext) = (
VALUE *)st_init_numtable();
312 if (RCLASSEXT_SHARED_CONST_TBL(orig)) {
313 RCLASSEXT_CONST_TBL(ext) = RCLASSEXT_CONST_TBL(orig);
314 RCLASSEXT_SHARED_CONST_TBL(ext) =
true;
317 RCLASSEXT_CONST_TBL(ext) = duplicate_classext_const_tbl(RCLASSEXT_CONST_TBL(orig), klass);
318 RCLASSEXT_SHARED_CONST_TBL(ext) =
false;
330 RCLASSEXT_CVC_TBL(ext) = duplicate_classext_id_table(RCLASSEXT_CVC_TBL(orig), dup_iclass);
333 duplicate_classext_subclasses(orig, ext);
335 RCLASSEXT_SET_ORIGIN(ext, klass, RCLASSEXT_ORIGIN(orig));
344 RCLASSEXT_PERMANENT_CLASSPATH(ext) = RCLASSEXT_PERMANENT_CLASSPATH(orig);
345 RCLASSEXT_CLONED(ext) = RCLASSEXT_CLONED(orig);
346 RCLASSEXT_CLASSPATH(ext) = RCLASSEXT_CLASSPATH(orig);
359 while (subclass_entry) {
361 iclass = subclass_entry->klass;
365 class_duplicate_iclass_classext(iclass, ext, ns);
368 subclass_entry = subclass_entry->next;
376rb_class_ensure_writable(
VALUE klass)
379 RCLASS_EXT_WRITABLE(klass);
383 rb_class_classext_foreach_callback_func *func;
388class_classext_foreach_i(st_data_t key, st_data_t value, st_data_t arg)
391 rb_class_classext_foreach_callback_func *func =
foreach_arg->func;
397rb_class_classext_foreach(
VALUE klass, rb_class_classext_foreach_callback_func *func,
void *arg)
404 rb_st_foreach(tbl, class_classext_foreach_i, (st_data_t)&
foreach_arg);
406 func(RCLASS_EXT_PRIME(klass),
true, (
VALUE)NULL, arg);
410rb_class_super_of(
VALUE klass)
416rb_class_singleton_p(
VALUE klass)
418 return RCLASS_SINGLETON_P(klass);
422rb_class_variation_count(
VALUE klass)
424 return RCLASS_VARIATION_COUNT(klass);
428push_subclass_entry_to_list(
VALUE super,
VALUE klass,
bool is_module)
437 entry->klass = klass;
440 anchor = RCLASS_WRITABLE_SUBCLASSES(super);
443 VM_ASSERT(ns_subclasses);
444 tbl = ns_subclasses->tbl;
449 head->next->prev = entry;
450 entry->next = head->next;
454 st_insert(tbl, namespace_subclasses_tbl_key(ns), (st_data_t)entry);
458 RCLASS_WRITE_NS_MODULE_SUBCLASSES(klass, anchor->ns_subclasses);
461 RCLASS_WRITE_NS_SUPER_SUBCLASSES(klass, anchor->ns_subclasses);
466rb_class_subclass_add(
VALUE super,
VALUE klass)
468 if (super && !UNDEF_P(super)) {
469 push_subclass_entry_to_list(super, klass,
false);
474rb_module_add_to_subclasses_list(
VALUE module,
VALUE iclass)
476 if (module && !UNDEF_P(module)) {
477 push_subclass_entry_to_list(module, iclass,
true);
482rb_class_remove_subclass_head(
VALUE klass)
485 rb_class_classext_free_subclasses(ext, klass);
489class_get_subclasses_for_ns(
struct st_table *tbl,
VALUE ns_id)
492 if (st_lookup(tbl, (st_data_t)ns_id, &value)) {
502 bool first_entry =
true;
504 if (entry->klass == klass) {
518 st_insert(tbl, ns_id, (st_data_t)next);
522 st_delete(tbl, &ns_id, NULL);
527 else if (first_entry) {
535rb_class_remove_from_super_subclasses(
VALUE klass)
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;
547rb_class_remove_from_module_subclasses(
VALUE klass)
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;
562 struct st_table *tbl = anchor->ns_subclasses->tbl;
563 VALUE ns_id = namespace_subclasses_tbl_key(RCLASSEXT_NS(ext));
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);
578 if (RCLASSEXT_NS_SUPER_SUBCLASSES(ext)) {
580 remove_class_from_subclasses(ns_sub->tbl, ns_id, klass);
581 rb_ns_subclasses_ref_dec(ns_sub);
583 if (RCLASSEXT_NS_MODULE_SUBCLASSES(ext)) {
585 remove_class_from_subclasses(ns_sub->tbl, ns_id, klass);
586 rb_ns_subclasses_ref_dec(ns_sub);
598 VALUE curklass = cur->klass;
608class_detach_subclasses(
VALUE klass,
VALUE arg)
610 rb_class_remove_from_super_subclasses(klass);
614rb_class_detach_subclasses(
VALUE klass)
616 rb_class_foreach_subclass(klass, class_detach_subclasses,
Qnil);
620class_detach_module_subclasses(
VALUE klass,
VALUE arg)
622 rb_class_remove_from_module_subclasses(klass);
626rb_class_detach_module_subclasses(
VALUE klass)
628 rb_class_foreach_subclass(klass, class_detach_module_subclasses,
Qnil);
632class_switch_superclass(
VALUE super,
VALUE klass)
634 class_detach_subclasses(klass,
Qnil);
635 rb_class_subclass_add(super, klass);
663 ns_subclasses->refcount = 1;
664 ns_subclasses->tbl = st_init_numtable();
666 anchor->ns_subclasses = ns_subclasses;
673 NEWOBJ_OF(obj,
struct RClass, klass, flags, alloc_size, 0);
684 RCLASS_PRIME_NS((
VALUE)obj) = ns;
687 RCLASS_SET_PRIME_CLASSEXT_WRITABLE((
VALUE)obj, !rb_namespace_available() || NAMESPACE_USER_P(ns) ?
true :
false);
690 RCLASS_SET_REFINED_CLASS((
VALUE)obj,
Qnil);
692 RCLASS_SET_SUBCLASSES((
VALUE)obj, anchor);
698class_associate_super(
VALUE klass,
VALUE super,
bool init)
700 if (super && !UNDEF_P(super)) {
701 class_switch_superclass(super, klass);
704 RCLASS_SET_SUPER(klass, super);
707 RCLASS_WRITE_SUPER(klass, super);
709 rb_class_update_superclasses(klass);
716 return class_associate_super(klass, super,
false);
720class_initialize_method_table(
VALUE c)
723 RCLASS_SET_M_TBL_EVEN_WHEN_PROMOTED(c, rb_id_table_create(0));
727class_clear_method_table(
VALUE c)
729 RCLASS_WRITE_M_TBL_EVEN_WHEN_PROMOTED(c, rb_id_table_create(0));
748 class_initialize_method_table(klass);
750 class_associate_super(klass, super,
true);
751 if (super && !UNDEF_P(super)) {
752 rb_class_set_initialized(klass);
759class_superclasses_including_self(
VALUE klass)
761 if (RCLASS_SUPERCLASSES_WITH_SELF_P(klass))
762 return RCLASS_SUPERCLASSES(klass);
764 size_t depth = RCLASS_SUPERCLASS_DEPTH(klass);
767 memcpy(superclasses, RCLASS_SUPERCLASSES(klass),
sizeof(
VALUE) * depth);
768 superclasses[depth] = klass;
774rb_class_update_superclasses(
VALUE klass)
781 if (UNDEF_P(super))
return;
784 if (RCLASS_SUPERCLASSES(klass))
799 rb_class_update_superclasses(super);
802 if (!RCLASS_SUPERCLASS_DEPTH(super))
806 super_depth = RCLASS_SUPERCLASS_DEPTH(super);
807 if (RCLASS_SUPERCLASSES_WITH_SELF_P(super)) {
808 superclasses = RCLASS_SUPERCLASSES(super);
811 superclasses = class_superclasses_including_self(super);
812 RCLASS_WRITE_SUPERCLASSES(super, super_depth, superclasses,
true);
815 size_t depth = super_depth == RCLASS_MAX_SUPERCLASS_DEPTH ? super_depth : super_depth + 1;
816 RCLASS_WRITE_SUPERCLASSES(klass, depth, superclasses,
false);
823 rb_raise(
rb_eTypeError,
"superclass must be an instance of Class (given an instance of %"PRIsVALUE
")",
826 if (RCLASS_SINGLETON_P(super)) {
827 rb_raise(
rb_eTypeError,
"can't make subclass of singleton class");
842 RCLASS_SET_MAX_IV_COUNT(klass, RCLASS_MAX_IV_COUNT(super));
845 RUBY_ASSERT(getenv(
"RUBY_NAMESPACE") || RCLASS_PRIME_CLASSEXT_WRITABLE_P(klass));
851rb_class_s_alloc(
VALUE klass)
859 if (me->def->type == VM_METHOD_TYPE_ISEQ) {
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));
865 rb_method_entry_set(new_klass, mid, me, METHOD_ENTRY_VISI(me));
874static enum rb_id_table_iterator_result
875clone_method_i(
ID key,
VALUE value,
void *data)
878 clone_method(arg->old_klass, arg->new_klass, key, (
const rb_method_entry_t *)value);
879 return ID_TABLE_CONTINUE;
895 rb_id_table_insert(arg->tbl, key, (
VALUE)nce);
896 return ID_TABLE_CONTINUE;
899static enum rb_id_table_iterator_result
900clone_const_i(
ID key,
VALUE value,
void *data)
911 if (RCLASS_INITIALIZED_P(clone)) {
914 if (RCLASS_SINGLETON_P(orig)) {
924static enum rb_id_table_iterator_result
925cvc_table_copy(
ID id,
VALUE val,
void *data)
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);
941 return ID_TABLE_CONTINUE;
947 if (RCLASS_CONST_TBL(clone)) {
948 rb_free_const_table(RCLASS_CONST_TBL(clone));
949 RCLASS_WRITE_CONST_TBL(clone, 0,
false);
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));
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);
961 rb_id_table_free(RCLASS_M_TBL(clone));
962 RCLASS_WRITE_M_TBL_EVEN_WHEN_PROMOTED(clone, 0);
966 rb_fields_tbl_copy(clone, orig);
968 rb_attr_delete(clone,
id);
970 rb_attr_delete(clone,
id);
972 if (RCLASS_CONST_TBL(orig)) {
975 arg.tbl = const_tbl = rb_id_table_create(0);
977 rb_id_table_foreach(RCLASS_CONST_TBL(orig), clone_const_i, &arg);
978 RCLASS_WRITE_CONST_TBL(clone, const_tbl,
false);
982static bool ensure_origin(
VALUE klass);
985rb_class_set_initialized(
VALUE klass)
993rb_module_check_initializable(
VALUE mod)
995 if (RCLASS_INITIALIZED_P(mod)) {
1009 class_init_copy_check(clone, orig);
1012 rb_module_check_initializable(clone);
1022 rb_class_set_initialized(clone);
1027 RCLASS_SET_CLONED(clone,
true);
1028 RCLASS_SET_CLONED(orig,
true);
1030 if (!RCLASS_SINGLETON_P(
CLASS_OF(clone))) {
1035 RCLASS_SET_ALLOCATOR(clone, RCLASS_ALLOCATOR(orig));
1037 copy_tables(clone, orig);
1038 if (RCLASS_M_TBL(orig)) {
1040 arg.old_klass = orig;
1041 arg.new_klass = clone;
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);
1048 if (RCLASS_ORIGIN(orig) == orig) {
1053 VALUE orig_origin = RCLASS_ORIGIN(orig);
1054 VALUE prev_clone_p = clone;
1062 ensure_origin(clone);
1063 clone_origin = RCLASS_ORIGIN(clone);
1065 while (p && p != orig_origin) {
1067 rb_bug(
"non iclass between module/class and origin");
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);
1078 RCLASS_SET_INCLUDER(clone_p, clone);
1080 add_subclass = TRUE;
1081 if (p != RCLASS_ORIGIN(p)) {
1082 origin[0] = clone_p;
1083 origin[1] = RCLASS_ORIGIN(p);
1086 else if ((origin_len =
RARRAY_LEN(origin_stack)) > 1 &&
1088 RCLASS_WRITE_ORIGIN(
RARRAY_AREF(origin_stack, (origin_len -= 2)), clone_p);
1089 RICLASS_WRITE_ORIGIN_SHARED_MTBL(clone_p);
1091 add_subclass = FALSE;
1094 rb_module_add_to_subclasses_list(METACLASS_OF(p), clone_p);
1099 if (p == orig_origin) {
1101 rb_class_set_super(clone_p, clone_origin);
1102 rb_class_set_super(clone_origin,
RCLASS_SUPER(orig_origin));
1104 copy_tables(clone_origin, orig_origin);
1105 if (RCLASS_M_TBL(orig_origin)) {
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);
1114 rb_bug(
"no origin for class that has origin");
1117 rb_class_update_superclasses(clone);
1126 return rb_singleton_class_clone_and_attach(obj,
Qundef);
1131rb_singleton_class_clone_and_attach(
VALUE obj,
VALUE attach)
1133 const VALUE klass = METACLASS_OF(obj);
1139 if (!(RCLASS_SINGLETON_P(klass) && RCLASS_ATTACHED_OBJECT(klass) == obj)) {
1145 bool klass_of_clone_is_new;
1150 klass_of_clone_is_new =
true;
1151 RBASIC_SET_CLASS(clone, clone);
1157 klass_of_clone_is_new = (METACLASS_OF(klass) != klass_metaclass_clone);
1158 RBASIC_SET_CLASS(clone, klass_metaclass_clone);
1162 class_initialize_method_table(clone);
1165 rb_fields_tbl_copy(clone, klass);
1166 if (RCLASS_CONST_TBL(klass)) {
1169 arg.tbl = table = rb_id_table_create(0);
1171 rb_id_table_foreach(RCLASS_CONST_TBL(klass), clone_const_i, &arg);
1172 RCLASS_SET_CONST_TBL(clone, table,
false);
1174 if (!UNDEF_P(attach)) {
1179 arg.old_klass = klass;
1180 arg.new_klass = clone;
1181 rb_id_table_foreach(RCLASS_M_TBL(klass), clone_method_i, &arg);
1183 if (klass_of_clone_is_new) {
1195 if (RCLASS_SINGLETON_P(klass)) {
1196 RCLASS_SET_ATTACHED_OBJECT(klass, obj);
1205#define META_CLASS_OF_CLASS_CLASS_P(k) (METACLASS_OF(k) == (k))
1208rb_singleton_class_has_metaclass_p(
VALUE sklass)
1210 return RCLASS_ATTACHED_OBJECT(METACLASS_OF(sklass)) == sklass;
1214rb_singleton_class_internal_p(
VALUE sklass)
1217 !rb_singleton_class_has_metaclass_p(sklass));
1225#define HAVE_METACLASS_P(k) \
1226 (FL_TEST(METACLASS_OF(k), FL_SINGLETON) && \
1227 rb_singleton_class_has_metaclass_p(k))
1236#define ENSURE_EIGENCLASS(klass) \
1237 (HAVE_METACLASS_P(klass) ? METACLASS_OF(klass) : make_metaclass(klass))
1259 SET_METACLASS_OF(klass, metaclass);
1260 SET_METACLASS_OF(metaclass, metaclass);
1263 VALUE tmp = METACLASS_OF(klass);
1264 SET_METACLASS_OF(klass, metaclass);
1271 rb_class_set_initialized(klass);
1274 rb_class_update_superclasses(METACLASS_OF(metaclass));
1288 VALUE orig_class = METACLASS_OF(obj);
1292 RBASIC_SET_CLASS(obj, klass);
1294 rb_yjit_invalidate_no_singleton_class(orig_class);
1296 SET_METACLASS_OF(klass, METACLASS_OF(
rb_class_real(orig_class)));
1302boot_defclass(
const char *name,
VALUE super)
1305 ID id = rb_intern(name);
1307 rb_const_set((rb_cObject ? rb_cObject : obj), id, obj);
1308 rb_vm_register_global_object(obj);
1359refinement_import_methods(
int argc,
VALUE *argv,
VALUE refinement)
1384Init_class_hierarchy(
void)
1388 rb_vm_register_global_object(rb_cObject);
1393 rb_cModule = boot_defclass(
"Module", rb_cObject);
1405 RBASIC_SET_CLASS(rb_cObject,
rb_cClass);
1439 if (!super) super = rb_cObject;
1441 rb_make_metaclass(klass, METACLASS_OF(super));
1459 if (!super) super = rb_cObject;
1461 return rb_funcall(super, inherited, 1, klass);
1471 id = rb_intern(name);
1472 if (NAMESPACE_OPTIONAL_P(ns)) {
1482 rb_raise(
rb_eTypeError,
"superclass mismatch for class %s", name);
1486 rb_vm_register_global_object(klass);
1490 rb_raise(rb_eArgError,
"no super class for '%s'", name);
1493 rb_vm_register_global_object(klass);
1507rb_define_class_id_under_no_pin(
VALUE outer,
ID id,
VALUE super)
1514 rb_raise(
rb_eTypeError,
"%"PRIsVALUE
"::%"PRIsVALUE
" is not a class"
1520 "%"PRIsVALUE
"::%"PRIsVALUE
""
1521 " (%"PRIsVALUE
" is given but was %"PRIsVALUE
")",
1528 rb_raise(rb_eArgError,
"no super class for '%"PRIsVALUE
"::%"PRIsVALUE
"'",
1542 VALUE klass = rb_define_class_id_under_no_pin(outer,
id, super);
1543 rb_vm_register_global_object(klass);
1548rb_module_s_alloc(
VALUE klass)
1551 class_initialize_method_table(mod);
1556module_new(
VALUE klass)
1559 class_initialize_method_table(mdl);
1589 id = rb_intern(name);
1590 if (NAMESPACE_OPTIONAL_P(ns)) {
1594 module = rb_const_get(rb_cObject, id);
1596 rb_raise(
rb_eTypeError,
"%s is not a module (%"PRIsVALUE
")",
1600 rb_vm_register_global_object(module);
1603 module = rb_module_new();
1604 rb_vm_register_global_object(module);
1622 module = rb_const_get_at(outer, id);
1624 rb_raise(
rb_eTypeError,
"%"PRIsVALUE
"::%"PRIsVALUE
" is not a module"
1629 rb_vm_register_global_object(module);
1632 module = rb_module_new();
1635 rb_vm_register_global_object(module);
1641rb_include_class_new(
VALUE module,
VALUE super)
1645 RCLASS_SET_M_TBL(klass, RCLASS_WRITABLE_M_TBL(module));
1647 RCLASS_SET_ORIGIN(klass, klass);
1649 module = METACLASS_OF(module);
1652 if (RCLASS_WRITABLE_CONST_TBL(module)) {
1653 RCLASS_SET_CONST_TBL(klass, RCLASS_WRITABLE_CONST_TBL(module),
true);
1656 RCLASS_WRITE_CONST_TBL(module, rb_id_table_create(0),
false);
1657 RCLASS_SET_CONST_TBL(klass, RCLASS_WRITABLE_CONST_TBL(module),
true);
1660 RCLASS_SET_CVC_TBL(klass, RCLASS_WRITABLE_CVC_TBL(module));
1662 class_associate_super(klass, super,
true);
1663 RBASIC_SET_CLASS(klass, module);
1665 return (
VALUE)klass;
1668static int include_modules_at(
const VALUE klass,
VALUE c,
VALUE module,
int search_super);
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");
1686 ensure_includable(klass, module);
1688 changed = include_modules_at(klass, RCLASS_ORIGIN(klass), module, TRUE);
1690 rb_raise(rb_eArgError,
"cyclic include detected");
1696 VALUE check_class = iclass->klass;
1699 if (!rb_objspace_garbage_object_p(check_class)) {
1700 while (check_class) {
1701 RUBY_ASSERT(!rb_objspace_garbage_object_p(check_class));
1704 (METACLASS_OF(check_class) == module)) {
1711 include_modules_at(iclass->klass, RCLASS_ORIGIN(iclass->klass), module, TRUE);
1715 iclass = iclass->next;
1720static enum rb_id_table_iterator_result
1721add_refined_method_entry_i(
ID key,
VALUE value,
void *data)
1723 rb_add_refined_method_entry((
VALUE)data, key);
1724 return ID_TABLE_CONTINUE;
1727static enum rb_id_table_iterator_result
1728clear_module_cache_i(
ID id,
VALUE val,
void *data)
1731 rb_clear_method_cache(klass,
id);
1732 return ID_TABLE_CONTINUE;
1736module_in_super_chain(
const VALUE klass,
VALUE module)
1738 struct rb_id_table *
const klass_m_tbl = RCLASS_M_TBL(RCLASS_ORIGIN(klass));
1741 if (klass_m_tbl == RCLASS_M_TBL(module))
1743 module = RCLASS_SUPER(module);
1751static enum rb_id_table_iterator_result
1752clear_constant_cache_i(
ID id,
VALUE value,
void *data)
1755 return ID_TABLE_CONTINUE;
1759do_include_modules_at(
const VALUE klass,
VALUE c,
VALUE module,
int search_super,
bool check_cyclic)
1761 VALUE p, iclass, origin_stack = 0;
1762 int method_changed = 0;
1764 VALUE klass_origin = RCLASS_ORIGIN(klass);
1765 VALUE original_klass = klass;
1767 if (check_cyclic && module_in_super_chain(klass, module))
1772 int superclass_seen = FALSE;
1778 if (klass_origin != c || search_super) {
1784 if (klass_origin == p && !search_super)
1789 if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) {
1790 if (!superclass_seen && c_seen) {
1797 superclass_seen = TRUE;
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)) {
1811 rb_id_table_foreach(tbl, clear_module_cache_i, (
void *)super_class);
1816 rb_id_table_foreach(tbl, clear_module_cache_i, (
void *)original_klass);
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)) {
1828 VALUE origin[2] = {iclass, RCLASS_ORIGIN(module)};
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);
1840 rb_module_add_to_subclasses_list(m, iclass);
1843 VALUE refined_class =
1844 rb_refinement_module_get_refined_class(klass);
1846 rb_id_table_foreach(RCLASS_M_TBL(module), add_refined_method_entry_i, (
void *)refined_class);
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);
1854 module = RCLASS_SUPER(module);
1857 return method_changed;
1861include_modules_at(
const VALUE klass,
VALUE c,
VALUE module,
int search_super)
1863 return do_include_modules_at(klass, c, module, search_super,
true);
1866static enum rb_id_table_iterator_result
1867move_refined_method(
ID key,
VALUE value,
void *data)
1871 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1873 struct rb_id_table *tbl = RCLASS_WRITABLE_M_TBL(klass);
1875 if (me->def->body.refined.orig_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;
1884 rb_method_table_insert(klass, tbl, key, me);
1885 return ID_TABLE_DELETE;
1889 return ID_TABLE_CONTINUE;
1893static enum rb_id_table_iterator_result
1894cache_clear_refined_method(
ID key,
VALUE value,
void *data)
1898 if (me->def->type == VM_METHOD_TYPE_REFINED && me->def->body.refined.orig_me) {
1900 rb_clear_method_cache(klass, me->called_id);
1905 return ID_TABLE_CONTINUE;
1909ensure_origin(
VALUE klass)
1911 VALUE origin = RCLASS_ORIGIN(klass);
1912 if (origin == klass) {
1914 RCLASS_SET_M_TBL(origin, RCLASS_M_TBL(klass));
1916 rb_class_set_super(klass, origin);
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);
1930 bool klass_had_no_origin;
1932 ensure_includable(klass, module);
1933 if (module_in_super_chain(klass, module))
1934 rb_raise(rb_eArgError,
"cyclic prepend detected");
1936 klass_had_no_origin = ensure_origin(klass);
1937 changed = do_include_modules_at(klass, klass, module, FALSE,
false);
1940 rb_vm_check_redefinition_by_prepend(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);
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)) {
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);
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);
1962 include_modules_at(subclass, subclass, module, FALSE);
1965 iclass = iclass->next;
1997 VALUE origin = RCLASS_ORIGIN(mod);
2001 VALUE m = METACLASS_OF(p);
2036 if (METACLASS_OF(p) == mod2)
return Qtrue;
2066 refined_class = rb_refinement_module_get_refined_class(mod);
2070 if (p == refined_class)
break;
2071 if (p != RCLASS_ORIGIN(p))
continue;
2087 bool immediate_only;
2091class_descendants_recursive(
VALUE klass,
VALUE v)
2096 if (data->buffer && data->count < data->maxcount && !rb_objspace_garbage_object_p(klass)) {
2101 if (!data->immediate_only) {
2102 rb_class_foreach_subclass(klass, class_descendants_recursive, v);
2106 rb_class_foreach_subclass(klass, class_descendants_recursive, v);
2111class_descendants(
VALUE klass,
bool immediate_only)
2116 rb_class_foreach_subclass(klass, class_descendants_recursive, (
VALUE) &data);
2120 data.maxcount = data.count;
2123 size_t gc_count = rb_gc_count();
2126 rb_class_foreach_subclass(klass, class_descendants_recursive, (
VALUE) &data);
2128 if (gc_count != rb_gc_count()) {
2129 rb_bug(
"GC must not occur during the subclass iteration of Class#descendants");
2177 return class_descendants(klass,
true);
2200 if (!RCLASS_SINGLETON_P(klass)) {
2201 rb_raise(
rb_eTypeError,
"'%"PRIsVALUE
"' is not a singleton class", klass);
2204 return RCLASS_ATTACHED_OBJECT(klass);
2208ins_methods_push(st_data_t name, st_data_t ary)
2214ins_methods_i(st_data_t name, st_data_t
type, st_data_t ary)
2216 switch ((rb_method_visibility_t)
type) {
2217 case METHOD_VISI_UNDEF:
2218 case METHOD_VISI_PRIVATE:
2221 ins_methods_push(name, ary);
2228ins_methods_type_i(st_data_t name, st_data_t
type, st_data_t ary, rb_method_visibility_t visi)
2230 if ((rb_method_visibility_t)
type == visi) {
2231 ins_methods_push(name, ary);
2237ins_methods_prot_i(st_data_t name, st_data_t
type, st_data_t ary)
2239 return ins_methods_type_i(name,
type, ary, METHOD_VISI_PROTECTED);
2243ins_methods_priv_i(st_data_t name, st_data_t
type, st_data_t ary)
2245 return ins_methods_type_i(name,
type, ary, METHOD_VISI_PRIVATE);
2249ins_methods_pub_i(st_data_t name, st_data_t
type, st_data_t ary)
2251 return ins_methods_type_i(name,
type, ary, METHOD_VISI_PUBLIC);
2255ins_methods_undef_i(st_data_t name, st_data_t
type, st_data_t ary)
2257 return ins_methods_type_i(name,
type, ary, METHOD_VISI_UNDEF);
2265static enum rb_id_table_iterator_result
2266method_entry_i(
ID key,
VALUE value,
void *data)
2270 rb_method_visibility_t
type;
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;
2278 if (!st_is_member(arg->list, key)) {
2279 if (UNDEFINED_METHOD_ENTRY_P(me)) {
2280 type = METHOD_VISI_UNDEF;
2283 type = METHOD_ENTRY_VISI(me);
2286 st_add_direct(arg->list, key, (st_data_t)
type);
2288 return ID_TABLE_CONTINUE;
2296 rb_id_table_foreach(m_tbl, method_entry_i, me_arg);
2300particular_class_p(
VALUE mod)
2302 if (!mod)
return false;
2303 if (RCLASS_SINGLETON_P(mod))
return true;
2309class_instance_method_list(
int argc,
const VALUE *argv,
VALUE mod,
int obj,
int (*func) (st_data_t, st_data_t, st_data_t))
2312 int recur = TRUE, prepended = 0;
2317 me_arg.list = st_init_numtable();
2318 me_arg.recur = recur;
2321 for (; particular_class_p(mod); mod =
RCLASS_SUPER(mod)) {
2322 add_instance_method_list(mod, &me_arg);
2326 if (!recur && RCLASS_ORIGIN(mod) != mod) {
2327 mod = RCLASS_ORIGIN(mod);
2332 add_instance_method_list(mod, &me_arg);
2337 st_foreach(me_arg.list, func, ary);
2338 st_free_table(me_arg.list);
2382 return class_instance_method_list(argc, argv, mod, 0, ins_methods_i);
2397 return class_instance_method_list(argc, argv, mod, 0, ins_methods_prot_i);
2420 return class_instance_method_list(argc, argv, mod, 0, ins_methods_priv_i);
2435 return class_instance_method_list(argc, argv, mod, 0, ins_methods_pub_i);
2447rb_class_undefined_instance_methods(
VALUE mod)
2450 return class_instance_method_list(1, &include_super, mod, 0, ins_methods_undef_i);
2484rb_obj_methods(
int argc,
const VALUE *argv,
VALUE obj)
2487 if (argc > 0 && !
RTEST(argv[0])) {
2490 return class_instance_method_list(argc, argv,
CLASS_OF(obj), 1, ins_methods_i);
2503rb_obj_protected_methods(
int argc,
const VALUE *argv,
VALUE obj)
2505 return class_instance_method_list(argc, argv,
CLASS_OF(obj), 1, ins_methods_prot_i);
2518rb_obj_private_methods(
int argc,
const VALUE *argv,
VALUE obj)
2520 return class_instance_method_list(argc, argv,
CLASS_OF(obj), 1, ins_methods_priv_i);
2533rb_obj_public_methods(
int argc,
const VALUE *argv,
VALUE obj)
2535 return class_instance_method_list(argc, argv,
CLASS_OF(obj), 1, ins_methods_pub_i);
2574 VALUE ary, klass, origin;
2580 if (RCLASS_SINGLETON_P(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);
2593 if (klass != origin && (mtbl = RCLASS_M_TBL(klass)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2598 st_foreach(me_arg.list, ins_methods_i, ary);
2599 st_free_table(me_arg.list);
2612#ifdef rb_define_method_id
2613#undef rb_define_method_id
2618 rb_add_method_cfunc(klass, mid, func, argc, METHOD_VISI_PUBLIC);
2621#ifdef rb_define_method
2622#undef rb_define_method
2627 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PUBLIC);
2630#ifdef rb_define_protected_method
2631#undef rb_define_protected_method
2636 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PROTECTED);
2639#ifdef rb_define_private_method
2640#undef rb_define_private_method
2645 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PRIVATE);
2651 rb_add_method(klass, rb_intern(name), VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2654static enum rb_id_table_iterator_result
2655undef_method_i(
ID name,
VALUE value,
void *data)
2658 rb_add_method(klass, name, VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2659 return ID_TABLE_CONTINUE;
2663rb_undef_methods_from(
VALUE klass,
VALUE super)
2667 rb_id_table_foreach(mtbl, undef_method_i, (
void *)klass);
2680special_singleton_class_of(
VALUE obj)
2686 default:
return Qnil;
2691rb_special_singleton_class(
VALUE obj)
2693 return special_singleton_class_of(obj);
2706singleton_class_of(
VALUE obj)
2710 switch (
TYPE(obj)) {
2720 klass = special_singleton_class_of(obj);
2722 rb_bug(
"unknown immediate %p", (
void *)obj);
2726 if (CHILLED_STRING_P(obj)) {
2727 CHILLED_STRING_MUTATED(obj);
2734 klass = METACLASS_OF(obj);
2735 if (!(RCLASS_SINGLETON_P(klass) &&
2736 RCLASS_ATTACHED_OBJECT(klass) == obj)) {
2737 klass = rb_make_metaclass(obj, klass);
2749 if (!RCLASS_SINGLETON_P(x)) {
2771 return rb_special_singleton_class(obj);
2773 klass = METACLASS_OF(obj);
2774 if (!RCLASS_SINGLETON_P(klass))
return Qnil;
2775 if (RCLASS_ATTACHED_OBJECT(klass) != obj)
return Qnil;
2782 VALUE klass = singleton_class_of(obj);
2799#ifdef rb_define_singleton_method
2800#undef rb_define_singleton_method
2808#ifdef rb_define_module_function
2809#undef rb_define_module_function
2818#ifdef rb_define_global_function
2819#undef rb_define_global_function
2830 rb_alias(klass, rb_intern(name1), rb_intern(name2));
2836 rb_attr(klass, rb_intern(name), read, write, FALSE);
2840rb_keyword_error_new(
const char *error,
VALUE keys)
2843 VALUE error_message = rb_sprintf(
"%s keyword%.*s", error,
len > 1,
"s");
2850 if (++i >=
len)
break;
2858NORETURN(
static void rb_keyword_error(
const char *error,
VALUE keys));
2860rb_keyword_error(
const char *error,
VALUE keys)
2865NORETURN(
static void unknown_keyword_error(
VALUE hash,
const ID *table,
int keywords));
2867unknown_keyword_error(
VALUE hash,
const ID *table,
int keywords)
2870 for (i = 0; i < keywords; i++) {
2871 st_data_t key =
ID2SYM(table[i]);
2872 rb_hash_stlike_delete(hash, &key, NULL);
2874 rb_keyword_error(
"unknown", rb_hash_keys(hash));
2879separate_symbol(st_data_t key, st_data_t value, st_data_t arg)
2883 if (!*kwdhash) *kwdhash = rb_hash_new();
2884 rb_hash_aset(*kwdhash, (
VALUE)key, (
VALUE)value);
2891 VALUE parthash[2] = {0, 0};
2892 VALUE hash = *orighash;
2899 *orighash = parthash[1];
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))
2919 if (
NIL_P(keyword_hash)) keyword_hash = 0;
2923 optional = -1-optional;
2926 for (; i < required; i++) {
2929 if (extract_kwarg(keyword, values[i])) {
2936 if (!
NIL_P(missing)) {
2937 rb_keyword_error(
"missing", missing);
2941 if (optional && keyword_hash) {
2942 for (i = 0; i < optional; i++) {
2943 if (extract_kwarg(
ID2SYM(table[required+i]), values[required+i])) {
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);
2953 if (values && !keyword_hash) {
2954 for (i = 0; i < required + optional; i++) {
2973rb_scan_args_parse(
int kw_flag,
const char *fmt,
struct rb_scan_args_t *arg)
2975 const char *p = fmt;
2977 memset(arg, 0,
sizeof(*arg));
2978 arg->kw_flag = kw_flag;
2981 arg->n_lead = *p -
'0';
2984 arg->n_opt = *p -
'0';
2993 arg->n_trail = *p -
'0';
3005 rb_fatal(
"bad scan arg format: %s", fmt);
3010rb_scan_args_assign(
const struct rb_scan_args_t *arg,
int argc,
const VALUE *
const argv, va_list vargs)
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;
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);
3033 if (argc < n_mand) {
3038 for (i = 0; i < n_lead; i++) {
3039 var = rb_scan_args_next_param();
3040 if (var) *var = argv[argi];
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];
3051 if (var) *var =
Qnil;
3056 int n_var = argc - argi - n_trail;
3058 var = rb_scan_args_next_param();
3068 for (i = 0; i < n_trail; i++) {
3069 var = rb_scan_args_next_param();
3070 if (var) *var = argv[argi];
3075 var = rb_scan_args_next_param();
3076 if (var) *var = hash;
3080 var = rb_scan_args_next_param();
3095#undef rb_scan_args_next_param
3099rb_scan_args_result(
const struct rb_scan_args_t *
const arg,
int argc)
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;
3123 va_start(vargs,fmt);
3124 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
3126 return rb_scan_args_result(&arg, argc);
3129#undef rb_scan_args_kw
3135 rb_scan_args_parse(kw_flag, fmt, &arg);
3136 va_start(vargs,fmt);
3137 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
3139 return rb_scan_args_result(&arg, argc);
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#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.
static VALUE RB_OBJ_FROZEN_RAW(VALUE obj)
This is an implementation detail of RB_OBJ_FROZEN().
static void RB_FL_SET_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_SET().
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.
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
VALUE rb_refinement_new(void)
Creates a new, anonymous refinement.
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
VALUE rb_class_new(VALUE super)
Creates a new, anonymous class.
static VALUE make_singleton_class(VALUE obj)
Creates a singleton class for obj.
VALUE rb_singleton_class_clone(VALUE obj)
Clones a singleton class.
void rb_prepend_module(VALUE klass, VALUE module)
Identical to rb_include_module(), except it "prepends" the passed module to the klass,...
VALUE rb_class_subclasses(VALUE klass)
Queries the class's direct descendants.
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
VALUE rb_class_attached_object(VALUE klass)
Returns the attached object for a singleton class.
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...
VALUE rb_module_new(void)
Creates a new, anonymous module.
#define META_CLASS_OF_CLASS_CLASS_P(k)
whether k is a meta^(n)-class of Class class
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.
void rb_check_inheritable(VALUE super)
Asserts that the given class can derive a child class.
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.
VALUE rb_class_boot(VALUE super)
A utility function that wraps class_alloc.
VALUE rb_define_module(const char *name)
Defines a top-level module.
void rb_class_modify_check(VALUE klass)
Asserts that klass is not a frozen class.
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.
void rb_singleton_class_attached(VALUE klass, VALUE obj)
Attaches a singleton class to its corresponding object.
void rb_freeze_singleton_class(VALUE x)
This is an implementation detail of RB_OBJ_FREEZE().
VALUE rb_mod_included_modules(VALUE mod)
Queries the list of included modules.
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.
VALUE rb_mod_ancestors(VALUE mod)
Queries the module's ancestors.
static VALUE make_metaclass(VALUE klass)
Creates a metaclass of klass
VALUE rb_class_inherited(VALUE super, VALUE klass)
Calls Class::inherited.
VALUE rb_mod_include_p(VALUE mod, VALUE mod2)
Queries if the passed module is included by the module.
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.
#define ENSURE_EIGENCLASS(klass)
ensures klass belongs to its own eigenclass.
VALUE rb_mod_init_copy(VALUE clone, VALUE orig)
The comment that comes with this function says :nodoc:.
VALUE rb_define_module_under(VALUE outer, const char *name)
Defines a module under the namespace of outer.
VALUE rb_singleton_class_get(VALUE obj)
Returns the singleton class of obj, or nil if obj is not a singleton object.
VALUE rb_define_module_id(ID id)
This is a very badly designed API that creates an anonymous module.
VALUE rb_define_class_id(ID id, VALUE super)
This is a very badly designed API that creates an anonymous class.
static VALUE class_alloc(enum ruby_value_type type, VALUE klass)
Allocates a struct RClass for a new class, iclass, or module.
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
VALUE rb_extract_keywords(VALUE *orighash)
Splits a hash into two.
void rb_define_attr(VALUE klass, const char *name, int read, int write)
Defines public accessor method(s) for an attribute.
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
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.
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.
int rb_block_given_p(void)
Determines if the current method is given a block.
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
#define TYPE(_)
Old name of rb_type.
#define FL_SINGLETON
Old name of RUBY_FL_SINGLETON.
#define OBJ_INIT_COPY(obj, orig)
Old name of RB_OBJ_INIT_COPY.
#define ALLOC
Old name of RB_ALLOC.
#define T_STRING
Old name of RUBY_T_STRING.
#define xfree
Old name of ruby_xfree.
#define Qundef
Old name of RUBY_Qundef.
#define T_NIL
Old name of RUBY_T_NIL.
#define T_FLOAT
Old name of RUBY_T_FLOAT.
#define ID2SYM
Old name of RB_ID2SYM.
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define ZALLOC
Old name of RB_ZALLOC.
#define CLASS_OF
Old name of rb_class_of.
#define xmalloc
Old name of ruby_xmalloc.
#define T_MODULE
Old name of RUBY_T_MODULE.
#define ISDIGIT
Old name of rb_isdigit.
#define T_TRUE
Old name of RUBY_T_TRUE.
#define T_ICLASS
Old name of RUBY_T_ICLASS.
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
#define FL_SET
Old name of RB_FL_SET.
#define T_FALSE
Old name of RUBY_T_FALSE.
#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.
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
#define T_CLASS
Old name of RUBY_T_CLASS.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
#define FL_TEST
Old name of RB_FL_TEST.
#define FL_FREEZE
Old name of RUBY_FL_FREEZE.
#define CONST_ID
Old name of RUBY_CONST_ID.
#define rb_ary_new2
Old name of rb_ary_new_capa.
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
VALUE rb_eTypeError
TypeError exception.
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.
VALUE rb_cClass
Class class.
VALUE rb_mKernel
Kernel module.
VALUE rb_cRefinement
Refinement class.
VALUE rb_cNilClass
NilClass class.
VALUE rb_cHash
Hash class.
VALUE rb_cFalseClass
FalseClass class.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
VALUE rb_cBasicObject
BasicObject class.
VALUE rb_cModule
Module class.
VALUE rb_class_real(VALUE klass)
Finds a "real" class.
VALUE rb_cTrueClass
TrueClass class.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
#define RGENGC_WB_PROTECTED_CLASS
This is a compile-time flag to enable/disable write barrier for struct RClass.
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().
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
#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.
VALUE rb_const_get(VALUE space, ID name)
Identical to rb_const_defined(), except it returns the actual defined value.
void rb_const_set(VALUE space, ID name, VALUE val)
Names a constant.
VALUE rb_const_get_at(VALUE space, ID name)
Identical to rb_const_defined_at(), except it returns the actual defined value.
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.
int rb_const_defined_at(VALUE space, ID name)
Identical to rb_const_defined(), except it doesn't look for parent classes.
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
int rb_const_defined(VALUE space, ID name)
Queries if the constant is defined at the namespace.
void rb_alias(VALUE klass, ID dst, ID src)
Resembles alias.
void rb_attr(VALUE klass, ID name, int need_reader, int need_writer, int honour_visibility)
This function resembles now-deprecated Module#attr.
void rb_clear_constant_cache_for_id(ID id)
Clears the inline constant caches associated with a particular ID.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
int len
Length of the buffer.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
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.
#define RARRAY_AREF(a, i)
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
#define RCLASS_SUPER
Just another name of rb_class_get_superclass.
#define RCLASS(obj)
Convenient casting macro.
#define RHASH_SIZE(h)
Queries the size of the hash.
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
#define RB_SCAN_ARGS_PASS_CALLED_KEYWORDS
Same behaviour as rb_scan_args().
#define RTEST
This is an old name of RB_TEST.
#define ANYARGS
Functions declared using this macro take arbitrary arguments, including void.
rb_cref_t * cref
class reference, should be marked
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
Internal header for Namespace.
Internal header for Class.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
ruby_value_type
C-level type of an object.