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"
80#define METACLASS_OF(k) RBASIC(k)->klass
81#define SET_METACLASS_OF(k, cls) RBASIC_SET_CLASS(k, cls)
90static enum rb_id_table_iterator_result
91duplicate_classext_id_table_i(
ID key,
VALUE value,
void *data)
94 rb_id_table_insert(tbl, key, value);
95 return ID_TABLE_CONTINUE;
98static enum rb_id_table_iterator_result
99duplicate_classext_m_tbl_i(
ID key,
VALUE value,
void *data)
103 rb_method_table_insert0(arg->klass, arg->tbl, key, me,
false);
104 return ID_TABLE_CONTINUE;
108duplicate_classext_m_tbl(
struct rb_id_table *orig,
VALUE klass,
bool init_missing)
113 return rb_id_table_create(0);
117 tbl = rb_id_table_create(rb_id_table_size(orig));
122 rb_id_table_foreach(orig, duplicate_classext_m_tbl_i, &data);
127duplicate_classext_id_table(
struct rb_id_table *orig,
bool init_missing)
133 return rb_id_table_create(0);
137 tbl = rb_id_table_create(rb_id_table_size(orig));
138 rb_id_table_foreach(orig, duplicate_classext_id_table_i, tbl);
148 dst->flag = src->flag;
149 dst->line = src->line;
156static enum rb_id_table_iterator_result
157duplicate_classext_const_tbl_i(
ID key,
VALUE value,
void *data)
162 rb_id_table_insert(arg->tbl, key, (
VALUE)entry);
164 return ID_TABLE_CONTINUE;
175 dst = rb_id_table_create(rb_id_table_size(src));
181 rb_id_table_foreach(src, duplicate_classext_const_tbl_i, (
void *)&data);
189 RCLASSEXT_SUPERCLASSES(copy) = RCLASSEXT_SUPERCLASSES(orig);
190 RCLASSEXT_SUPERCLASS_DEPTH(copy) = RCLASSEXT_SUPERCLASS_DEPTH(orig);
192 RCLASSEXT_SUPERCLASSES_OWNER(copy) =
false;
193 RCLASSEXT_SUPERCLASSES_WITH_SELF(copy) = RCLASSEXT_SUPERCLASSES_WITH_SELF(orig);
202 return (
VALUE)ns->ns_id;
213 if (RCLASSEXT_SUBCLASSES(orig)) {
214 orig_anchor = RCLASSEXT_SUBCLASSES(orig);
215 ns_subclasses = orig_anchor->ns_subclasses;
219 anchor->ns_subclasses = rb_ns_subclasses_ref_inc(ns_subclasses);
224 RCLASSEXT_SUBCLASSES(copy) = anchor;
227 entry = orig_anchor->head;
232 if (rb_objspace_garbage_object_p(entry->klass)) {
237 cdr->klass = entry->klass;
241 VALUE ns_id = namespace_subclasses_tbl_key(RCLASSEXT_NS(copy));
243 st_insert(tbl, ns_id, (st_data_t)first);
250 if (RCLASSEXT_NS_SUPER_SUBCLASSES(orig))
251 RCLASSEXT_NS_SUPER_SUBCLASSES(copy) = rb_ns_subclasses_ref_inc(RCLASSEXT_NS_SUPER_SUBCLASSES(orig));
252 if (RCLASSEXT_NS_MODULE_SUBCLASSES(orig))
253 RCLASSEXT_NS_MODULE_SUBCLASSES(copy) = rb_ns_subclasses_ref_inc(RCLASSEXT_NS_MODULE_SUBCLASSES(orig));
260 rb_classext_t *ext = RCLASS_EXT_TABLE_LOOKUP_INTERNAL(iclass, ns);
265 rb_invalidate_method_caches(RCLASSEXT_CALLABLE_M_TBL(ext), RCLASSEXT_CC_TBL(ext));
270 RCLASSEXT_NS(ext) = ns;
272 RCLASSEXT_SUPER(ext) = RCLASSEXT_SUPER(src);
275 if (RCLASSEXT_ICLASS_IS_ORIGIN(src) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(src)) {
276 RCLASSEXT_M_TBL(ext) = duplicate_classext_m_tbl(RCLASSEXT_M_TBL(src), iclass,
true);
279 RCLASSEXT_M_TBL(ext) = RCLASSEXT_M_TBL(mod_ext);
281 RCLASSEXT_FIELDS(ext) = (
VALUE *)st_init_numtable();
282 RCLASSEXT_CONST_TBL(ext) = RCLASSEXT_CONST_TBL(mod_ext);
283 RCLASSEXT_CVC_TBL(ext) = RCLASSEXT_CVC_TBL(mod_ext);
290 duplicate_classext_subclasses(src, ext);
292 RCLASSEXT_SET_ORIGIN(ext, iclass, RCLASSEXT_ORIGIN(src));
293 RCLASSEXT_ICLASS_IS_ORIGIN(ext) = RCLASSEXT_ICLASS_IS_ORIGIN(src);
294 RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext) = RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(src);
296 RCLASSEXT_SET_INCLUDER(ext, iclass, RCLASSEXT_INCLUDER(src));
298 first_set = RCLASS_SET_NAMESPACE_CLASSEXT(iclass, ns, ext);
300 RCLASS_SET_PRIME_CLASSEXT_WRITABLE(iclass,
false);
312 RCLASSEXT_NS(ext) = ns;
314 RCLASSEXT_SUPER(ext) = RCLASSEXT_SUPER(orig);
316 RCLASSEXT_M_TBL(ext) = duplicate_classext_m_tbl(RCLASSEXT_M_TBL(orig), klass, dup_iclass);
319 if (RCLASSEXT_FIELDS(orig)) {
320 RCLASSEXT_FIELDS(ext) = (
VALUE *)st_copy((
st_table *)RCLASSEXT_FIELDS(orig));
321 rb_autoload_copy_table_for_namespace((
st_table *)RCLASSEXT_FIELDS(ext), ns);
324 RCLASSEXT_FIELDS(ext) = (
VALUE *)st_init_numtable();
327 if (RCLASSEXT_SHARED_CONST_TBL(orig)) {
328 RCLASSEXT_CONST_TBL(ext) = RCLASSEXT_CONST_TBL(orig);
329 RCLASSEXT_SHARED_CONST_TBL(ext) =
true;
332 RCLASSEXT_CONST_TBL(ext) = duplicate_classext_const_tbl(RCLASSEXT_CONST_TBL(orig), klass);
333 RCLASSEXT_SHARED_CONST_TBL(ext) =
false;
345 RCLASSEXT_CVC_TBL(ext) = duplicate_classext_id_table(RCLASSEXT_CVC_TBL(orig), dup_iclass);
348 duplicate_classext_superclasses(orig, ext);
351 duplicate_classext_subclasses(orig, ext);
353 RCLASSEXT_SET_ORIGIN(ext, klass, RCLASSEXT_ORIGIN(orig));
360 RCLASSEXT_MAX_IV_COUNT(ext) = RCLASSEXT_MAX_IV_COUNT(orig);
361 RCLASSEXT_VARIATION_COUNT(ext) = RCLASSEXT_VARIATION_COUNT(orig);
362 RCLASSEXT_PERMANENT_CLASSPATH(ext) = RCLASSEXT_PERMANENT_CLASSPATH(orig);
363 RCLASSEXT_CLONED(ext) = RCLASSEXT_CLONED(orig);
364 RCLASSEXT_CLASSPATH(ext) = RCLASSEXT_CLASSPATH(orig);
377 while (subclass_entry) {
379 iclass = subclass_entry->klass;
381 class_duplicate_iclass_classext(iclass, ext, ns);
384 subclass_entry = subclass_entry->next;
392rb_class_ensure_writable(
VALUE klass)
395 RCLASS_EXT_WRITABLE(klass);
399 rb_class_classext_foreach_callback_func *func;
404class_classext_foreach_i(st_data_t key, st_data_t value, st_data_t arg)
407 rb_class_classext_foreach_callback_func *func =
foreach_arg->func;
413rb_class_classext_foreach(
VALUE klass, rb_class_classext_foreach_callback_func *func,
void *arg)
420 rb_st_foreach(tbl, class_classext_foreach_i, (st_data_t)&
foreach_arg);
422 func(RCLASS_EXT_PRIME(klass),
true, (
VALUE)NULL, arg);
426rb_class_super_of(
VALUE klass)
432rb_class_singleton_p(
VALUE klass)
434 return RCLASS_SINGLETON_P(klass);
438rb_class_variation_count(
VALUE klass)
440 return RCLASS_VARIATION_COUNT(klass);
444push_subclass_entry_to_list(
VALUE super,
VALUE klass,
bool is_module)
453 entry->klass = klass;
457 anchor = RCLASS_WRITABLE_SUBCLASSES(super);
460 VM_ASSERT(ns_subclasses);
461 tbl = ns_subclasses->tbl;
466 head->next->prev = entry;
467 entry->next = head->next;
471 st_insert(tbl, namespace_subclasses_tbl_key(ns), (st_data_t)entry);
476 RCLASS_WRITE_NS_MODULE_SUBCLASSES(klass, anchor->ns_subclasses);
479 RCLASS_WRITE_NS_SUPER_SUBCLASSES(klass, anchor->ns_subclasses);
484rb_class_subclass_add(
VALUE super,
VALUE klass)
486 if (super && !UNDEF_P(super)) {
487 push_subclass_entry_to_list(super, klass,
false);
492rb_module_add_to_subclasses_list(
VALUE module,
VALUE iclass)
494 if (module && !UNDEF_P(module)) {
495 push_subclass_entry_to_list(module, iclass,
true);
500rb_class_remove_subclass_head(
VALUE klass)
503 rb_class_classext_free_subclasses(ext, klass);
507class_get_subclasses_for_ns(
struct st_table *tbl,
VALUE ns_id)
510 if (st_lookup(tbl, (st_data_t)ns_id, &value)) {
520 bool first_entry =
true;
522 if (entry->klass == klass) {
536 st_insert(tbl, ns_id, (st_data_t)next);
540 st_delete(tbl, &ns_id, NULL);
545 else if (first_entry) {
553rb_class_remove_from_super_subclasses(
VALUE klass)
558 if (!ns_subclasses)
return;
559 remove_class_from_subclasses(ns_subclasses->tbl, namespace_subclasses_tbl_key(RCLASSEXT_NS(ext)), klass);
560 rb_ns_subclasses_ref_dec(ns_subclasses);
561 RCLASSEXT_NS_SUPER_SUBCLASSES(ext) = 0;
565rb_class_remove_from_module_subclasses(
VALUE klass)
570 if (!ns_subclasses)
return;
571 remove_class_from_subclasses(ns_subclasses->tbl, namespace_subclasses_tbl_key(RCLASSEXT_NS(ext)), klass);
572 rb_ns_subclasses_ref_dec(ns_subclasses);
573 RCLASSEXT_NS_MODULE_SUBCLASSES(ext) = 0;
580 struct st_table *tbl = anchor->ns_subclasses->tbl;
581 VALUE ns_id = namespace_subclasses_tbl_key(RCLASSEXT_NS(ext));
590 rb_ns_subclasses_ref_count(anchor->ns_subclasses) > 0,
591 "ns_subclasses refcount (%p) %ld", anchor->ns_subclasses, rb_ns_subclasses_ref_count(anchor->ns_subclasses));
592 st_delete(tbl, &ns_id, NULL);
593 rb_ns_subclasses_ref_dec(anchor->ns_subclasses);
596 if (RCLASSEXT_NS_SUPER_SUBCLASSES(ext)) {
598 remove_class_from_subclasses(ns_sub->tbl, ns_id, klass);
599 rb_ns_subclasses_ref_dec(ns_sub);
601 if (RCLASSEXT_NS_MODULE_SUBCLASSES(ext)) {
603 remove_class_from_subclasses(ns_sub->tbl, ns_id, klass);
604 rb_ns_subclasses_ref_dec(ns_sub);
616 VALUE curklass = cur->klass;
626class_detach_subclasses(
VALUE klass,
VALUE arg)
628 rb_class_remove_from_super_subclasses(klass);
632rb_class_detach_subclasses(
VALUE klass)
634 rb_class_foreach_subclass(klass, class_detach_subclasses,
Qnil);
638class_detach_module_subclasses(
VALUE klass,
VALUE arg)
640 rb_class_remove_from_module_subclasses(klass);
644rb_class_detach_module_subclasses(
VALUE klass)
646 rb_class_foreach_subclass(klass, class_detach_module_subclasses,
Qnil);
650class_switch_superclass(
VALUE super,
VALUE klass)
652 class_detach_subclasses(klass,
Qnil);
653 rb_class_subclass_add(super, klass);
683 ns_subclasses->refcount = 1;
684 ns_subclasses->tbl = st_init_numtable();
686 anchor->ns_subclasses = ns_subclasses;
691 NEWOBJ_OF(obj,
struct RClass, klass, flags, alloc_size, 0);
702 RCLASS_PRIME_NS((
VALUE)obj) = ns;
705 RCLASS_SET_PRIME_CLASSEXT_WRITABLE((
VALUE)obj, NAMESPACE_USER_P(ns) ?
true :
false);
708 RCLASS_SET_REFINED_CLASS((
VALUE)obj,
Qnil);
709 RCLASS_SET_ALLOCATOR((
VALUE)obj, 0);
711 RCLASS_SET_SUBCLASSES((
VALUE)obj, anchor);
717class_associate_super(
VALUE klass,
VALUE super,
bool init)
719 if (super && !UNDEF_P(super)) {
720 class_switch_superclass(super, klass);
723 RCLASS_SET_SUPER(klass, super);
726 RCLASS_WRITE_SUPER(klass, super);
728 rb_class_update_superclasses(klass);
735 return class_associate_super(klass, super,
false);
739class_initialize_method_table(
VALUE c)
742 RCLASS_SET_M_TBL_EVEN_WHEN_PROMOTED(c, rb_id_table_create(0));
746class_clear_method_table(
VALUE c)
748 RCLASS_WRITE_M_TBL_EVEN_WHEN_PROMOTED(c, rb_id_table_create(0));
767 class_initialize_method_table(klass);
769 class_associate_super(klass, super,
true);
775class_superclasses_including_self(
VALUE klass)
777 if (RCLASS_SUPERCLASSES_WITH_SELF_P(klass))
778 return RCLASS_SUPERCLASSES(klass);
780 size_t depth = RCLASS_SUPERCLASS_DEPTH(klass);
783 memcpy(superclasses, RCLASS_SUPERCLASSES(klass),
sizeof(
VALUE) * depth);
784 superclasses[depth] = klass;
790rb_class_update_superclasses(
VALUE klass)
797 if (UNDEF_P(super))
return;
800 if (RCLASS_SUPERCLASSES(klass))
815 rb_class_update_superclasses(super);
818 if (!RCLASS_SUPERCLASS_DEPTH(super))
822 super_depth = RCLASS_SUPERCLASS_DEPTH(super);
823 if (RCLASS_SUPERCLASSES_WITH_SELF_P(super)) {
824 superclasses = RCLASS_SUPERCLASSES(super);
827 superclasses = class_superclasses_including_self(super);
828 RCLASS_WRITE_SUPERCLASSES(super, super_depth, superclasses,
true,
true);
831 size_t depth = super_depth == RCLASS_MAX_SUPERCLASS_DEPTH ? super_depth : super_depth + 1;
832 RCLASS_WRITE_SUPERCLASSES(klass, depth, superclasses,
false,
false);
839 rb_raise(
rb_eTypeError,
"superclass must be an instance of Class (given an instance of %"PRIsVALUE
")",
842 if (RCLASS_SINGLETON_P(super)) {
843 rb_raise(
rb_eTypeError,
"can't make subclass of singleton class");
858 RCLASS_SET_MAX_IV_COUNT(klass, RCLASS_MAX_IV_COUNT(super));
865rb_class_s_alloc(
VALUE klass)
873 if (me->def->type == VM_METHOD_TYPE_ISEQ) {
875 rb_vm_rewrite_cref(me->def->body.iseq.
cref, old_klass, new_klass, &new_cref);
876 rb_add_method_iseq(new_klass, mid, me->def->body.iseq.
iseqptr, new_cref, METHOD_ENTRY_VISI(me));
879 rb_method_entry_set(new_klass, mid, me, METHOD_ENTRY_VISI(me));
888static enum rb_id_table_iterator_result
889clone_method_i(
ID key,
VALUE value,
void *data)
892 clone_method(arg->old_klass, arg->new_klass, key, (
const rb_method_entry_t *)value);
893 return ID_TABLE_CONTINUE;
909 rb_id_table_insert(arg->tbl, key, (
VALUE)nce);
910 return ID_TABLE_CONTINUE;
913static enum rb_id_table_iterator_result
914clone_const_i(
ID key,
VALUE value,
void *data)
928 if (RCLASS_SINGLETON_P(orig)) {
938static enum rb_id_table_iterator_result
939cvc_table_copy(
ID id,
VALUE val,
void *data)
948 ent->class_value = ctx->clone;
949 ent->cref = orig_entry->cref;
950 ent->global_cvar_state = orig_entry->global_cvar_state;
951 rb_id_table_insert(ctx->new_table,
id, (
VALUE)ent);
955 return ID_TABLE_CONTINUE;
961 if (RCLASS_CONST_TBL(clone)) {
962 rb_free_const_table(RCLASS_CONST_TBL(clone));
963 RCLASS_WRITE_CONST_TBL(clone, 0,
false);
965 if (RCLASS_CVC_TBL(orig)) {
966 struct rb_id_table *rb_cvc_tbl = RCLASS_CVC_TBL(orig);
967 struct rb_id_table *rb_cvc_tbl_dup = rb_id_table_create(rb_id_table_size(rb_cvc_tbl));
971 ctx.new_table = rb_cvc_tbl_dup;
972 rb_id_table_foreach(rb_cvc_tbl, cvc_table_copy, &ctx);
973 RCLASS_WRITE_CVC_TBL(clone, rb_cvc_tbl_dup);
975 rb_id_table_free(RCLASS_M_TBL(clone));
976 RCLASS_WRITE_M_TBL_EVEN_WHEN_PROMOTED(clone, 0);
980 rb_fields_tbl_copy(clone, orig);
982 rb_attr_delete(clone,
id);
984 rb_attr_delete(clone,
id);
986 if (RCLASS_CONST_TBL(orig)) {
989 arg.tbl = const_tbl = rb_id_table_create(0);
991 rb_id_table_foreach(RCLASS_CONST_TBL(orig), clone_const_i, &arg);
992 RCLASS_WRITE_CONST_TBL(clone, const_tbl,
false);
996static bool ensure_origin(
VALUE klass);
1001enum {RMODULE_ALLOCATED_BUT_NOT_INITIALIZED =
RUBY_FL_USER1};
1004RMODULE_UNINITIALIZED(
VALUE module)
1006 return FL_TEST_RAW(module, RMODULE_ALLOCATED_BUT_NOT_INITIALIZED);
1010rb_module_set_initialized(
VALUE mod)
1012 FL_UNSET_RAW(mod, RMODULE_ALLOCATED_BUT_NOT_INITIALIZED);
1017rb_module_check_initializable(
VALUE mod)
1019 if (!RMODULE_UNINITIALIZED(mod)) {
1031 class_init_copy_check(clone, orig);
1034 rb_module_check_initializable(clone);
1044 RCLASS_SET_CLONED(clone,
true);
1045 RCLASS_SET_CLONED(orig,
true);
1047 if (!RCLASS_SINGLETON_P(
CLASS_OF(clone))) {
1051 RCLASS_SET_ALLOCATOR(clone, RCLASS_ALLOCATOR(orig));
1052 copy_tables(clone, orig);
1053 if (RCLASS_M_TBL(orig)) {
1055 arg.old_klass = orig;
1056 arg.new_klass = clone;
1059 RCLASS_SET_M_TBL_EVEN_WHEN_PROMOTED(clone, rb_id_table_create(0));
1060 rb_id_table_foreach(RCLASS_M_TBL(orig), clone_method_i, &arg);
1063 if (RCLASS_ORIGIN(orig) == orig) {
1068 VALUE orig_origin = RCLASS_ORIGIN(orig);
1069 VALUE prev_clone_p = clone;
1077 ensure_origin(clone);
1078 clone_origin = RCLASS_ORIGIN(clone);
1080 while (p && p != orig_origin) {
1082 rb_bug(
"non iclass between module/class and origin");
1088 RCLASS_SET_M_TBL(clone_p, RCLASS_M_TBL(p));
1089 rb_class_set_super(prev_clone_p, clone_p);
1090 prev_clone_p = clone_p;
1091 RCLASS_SET_CONST_TBL(clone_p, RCLASS_CONST_TBL(p),
false);
1092 RCLASS_SET_ALLOCATOR(clone_p, RCLASS_ALLOCATOR(p));
1094 RCLASS_SET_INCLUDER(clone_p, clone);
1096 add_subclass = TRUE;
1097 if (p != RCLASS_ORIGIN(p)) {
1098 origin[0] = clone_p;
1099 origin[1] = RCLASS_ORIGIN(p);
1102 else if ((origin_len =
RARRAY_LEN(origin_stack)) > 1 &&
1104 RCLASS_WRITE_ORIGIN(
RARRAY_AREF(origin_stack, (origin_len -= 2)), clone_p);
1105 RICLASS_WRITE_ORIGIN_SHARED_MTBL(clone_p);
1107 add_subclass = FALSE;
1110 rb_module_add_to_subclasses_list(METACLASS_OF(p), clone_p);
1115 if (p == orig_origin) {
1117 rb_class_set_super(clone_p, clone_origin);
1118 rb_class_set_super(clone_origin,
RCLASS_SUPER(orig_origin));
1120 copy_tables(clone_origin, orig_origin);
1121 if (RCLASS_M_TBL(orig_origin)) {
1123 arg.old_klass = orig;
1124 arg.new_klass = clone;
1125 class_initialize_method_table(clone_origin);
1126 rb_id_table_foreach(RCLASS_M_TBL(orig_origin), clone_method_i, &arg);
1130 rb_bug(
"no origin for class that has origin");
1133 rb_class_update_superclasses(clone);
1142 return rb_singleton_class_clone_and_attach(obj,
Qundef);
1147rb_singleton_class_clone_and_attach(
VALUE obj,
VALUE attach)
1149 const VALUE klass = METACLASS_OF(obj);
1155 if (!(RCLASS_SINGLETON_P(klass) && RCLASS_ATTACHED_OBJECT(klass) == obj)) {
1161 bool klass_of_clone_is_new;
1165 klass_of_clone_is_new =
true;
1166 RBASIC_SET_CLASS(clone, clone);
1172 klass_of_clone_is_new = (METACLASS_OF(klass) != klass_metaclass_clone);
1173 RBASIC_SET_CLASS(clone, klass_metaclass_clone);
1177 class_initialize_method_table(clone);
1180 rb_fields_tbl_copy(clone, klass);
1181 if (RCLASS_CONST_TBL(klass)) {
1184 arg.tbl = table = rb_id_table_create(0);
1186 rb_id_table_foreach(RCLASS_CONST_TBL(klass), clone_const_i, &arg);
1187 RCLASS_SET_CONST_TBL(clone, table,
false);
1189 if (!UNDEF_P(attach)) {
1194 arg.old_klass = klass;
1195 arg.new_klass = clone;
1196 rb_id_table_foreach(RCLASS_M_TBL(klass), clone_method_i, &arg);
1198 if (klass_of_clone_is_new) {
1210 if (RCLASS_SINGLETON_P(klass)) {
1211 RCLASS_SET_ATTACHED_OBJECT(klass, obj);
1220#define META_CLASS_OF_CLASS_CLASS_P(k) (METACLASS_OF(k) == (k))
1223rb_singleton_class_has_metaclass_p(
VALUE sklass)
1225 return RCLASS_ATTACHED_OBJECT(METACLASS_OF(sklass)) == sklass;
1229rb_singleton_class_internal_p(
VALUE sklass)
1232 !rb_singleton_class_has_metaclass_p(sklass));
1240#define HAVE_METACLASS_P(k) \
1241 (FL_TEST(METACLASS_OF(k), FL_SINGLETON) && \
1242 rb_singleton_class_has_metaclass_p(k))
1251#define ENSURE_EIGENCLASS(klass) \
1252 (HAVE_METACLASS_P(klass) ? METACLASS_OF(klass) : make_metaclass(klass))
1274 SET_METACLASS_OF(klass, metaclass);
1275 SET_METACLASS_OF(metaclass, metaclass);
1278 VALUE tmp = METACLASS_OF(klass);
1279 SET_METACLASS_OF(klass, metaclass);
1288 rb_class_update_superclasses(METACLASS_OF(metaclass));
1302 VALUE orig_class = METACLASS_OF(obj);
1306 RBASIC_SET_CLASS(obj, klass);
1308 rb_yjit_invalidate_no_singleton_class(orig_class);
1310 SET_METACLASS_OF(klass, METACLASS_OF(
rb_class_real(orig_class)));
1316boot_defclass(
const char *name,
VALUE super)
1319 ID id = rb_intern(name);
1321 rb_const_set((rb_cObject ? rb_cObject : obj), id, obj);
1322 rb_vm_register_global_object(obj);
1373refinement_import_methods(
int argc,
VALUE *argv,
VALUE refinement)
1398Init_class_hierarchy(
void)
1402 rb_vm_register_global_object(rb_cObject);
1407 rb_cModule = boot_defclass(
"Module", rb_cObject);
1419 RBASIC_SET_CLASS(rb_cObject,
rb_cClass);
1453 if (!super) super = rb_cObject;
1455 rb_make_metaclass(klass, METACLASS_OF(super));
1473 if (!super) super = rb_cObject;
1475 return rb_funcall(super, inherited, 1, klass);
1485 id = rb_intern(name);
1486 if (NAMESPACE_OPTIONAL_P(ns)) {
1496 rb_raise(
rb_eTypeError,
"superclass mismatch for class %s", name);
1500 rb_vm_register_global_object(klass);
1504 rb_raise(rb_eArgError,
"no super class for '%s'", name);
1507 rb_vm_register_global_object(klass);
1521rb_define_class_id_under_no_pin(
VALUE outer,
ID id,
VALUE super)
1528 rb_raise(
rb_eTypeError,
"%"PRIsVALUE
"::%"PRIsVALUE
" is not a class"
1534 "%"PRIsVALUE
"::%"PRIsVALUE
""
1535 " (%"PRIsVALUE
" is given but was %"PRIsVALUE
")",
1542 rb_raise(rb_eArgError,
"no super class for '%"PRIsVALUE
"::%"PRIsVALUE
"'",
1556 VALUE klass = rb_define_class_id_under_no_pin(outer,
id, super);
1557 rb_vm_register_global_object(klass);
1562rb_module_s_alloc(
VALUE klass)
1565 class_initialize_method_table(mod);
1566 FL_SET(mod, RMODULE_ALLOCATED_BUT_NOT_INITIALIZED);
1571module_new(
VALUE klass)
1574 class_initialize_method_table(mdl);
1604 id = rb_intern(name);
1605 if (NAMESPACE_OPTIONAL_P(ns)) {
1609 module = rb_const_get(rb_cObject, id);
1611 rb_raise(
rb_eTypeError,
"%s is not a module (%"PRIsVALUE
")",
1615 rb_vm_register_global_object(module);
1618 module = rb_module_new();
1619 rb_vm_register_global_object(module);
1637 module = rb_const_get_at(outer, id);
1639 rb_raise(
rb_eTypeError,
"%"PRIsVALUE
"::%"PRIsVALUE
" is not a module"
1644 rb_vm_register_global_object(module);
1647 module = rb_module_new();
1650 rb_vm_register_global_object(module);
1656rb_include_class_new(
VALUE module,
VALUE super)
1660 RCLASS_SET_M_TBL(klass, RCLASS_WRITABLE_M_TBL(module));
1662 RCLASS_SET_ORIGIN(klass, klass);
1664 module = METACLASS_OF(module);
1667 if (RCLASS_WRITABLE_CONST_TBL(module)) {
1668 RCLASS_SET_CONST_TBL(klass, RCLASS_WRITABLE_CONST_TBL(module),
true);
1671 RCLASS_WRITE_CONST_TBL(module, rb_id_table_create(0),
false);
1672 RCLASS_SET_CONST_TBL(klass, RCLASS_WRITABLE_CONST_TBL(module),
true);
1675 RCLASS_SET_CVC_TBL(klass, RCLASS_WRITABLE_CVC_TBL(module));
1677 class_associate_super(klass, super,
true);
1678 RBASIC_SET_CLASS(klass, module);
1680 return (
VALUE)klass;
1683static int include_modules_at(
const VALUE klass,
VALUE c,
VALUE module,
int search_super);
1690 rb_module_set_initialized(module);
1691 if (!
NIL_P(rb_refinement_module_get_refined_class(module))) {
1692 rb_raise(rb_eArgError,
"refinement module is not allowed");
1701 ensure_includable(klass, module);
1703 changed = include_modules_at(klass, RCLASS_ORIGIN(klass), module, TRUE);
1705 rb_raise(rb_eArgError,
"cyclic include detected");
1711 VALUE check_class = iclass->klass;
1714 if (!rb_objspace_garbage_object_p(check_class)) {
1715 while (check_class) {
1716 RUBY_ASSERT(!rb_objspace_garbage_object_p(check_class));
1719 (METACLASS_OF(check_class) == module)) {
1726 include_modules_at(iclass->klass, RCLASS_ORIGIN(iclass->klass), module, TRUE);
1730 iclass = iclass->next;
1735static enum rb_id_table_iterator_result
1736add_refined_method_entry_i(
ID key,
VALUE value,
void *data)
1738 rb_add_refined_method_entry((
VALUE)data, key);
1739 return ID_TABLE_CONTINUE;
1742static enum rb_id_table_iterator_result
1743clear_module_cache_i(
ID id,
VALUE val,
void *data)
1746 rb_clear_method_cache(klass,
id);
1747 return ID_TABLE_CONTINUE;
1751module_in_super_chain(
const VALUE klass,
VALUE module)
1753 struct rb_id_table *
const klass_m_tbl = RCLASS_M_TBL(RCLASS_ORIGIN(klass));
1756 if (klass_m_tbl == RCLASS_M_TBL(module))
1758 module = RCLASS_SUPER(module);
1766static enum rb_id_table_iterator_result
1767clear_constant_cache_i(
ID id,
VALUE value,
void *data)
1770 return ID_TABLE_CONTINUE;
1774do_include_modules_at(
const VALUE klass,
VALUE c,
VALUE module,
int search_super,
bool check_cyclic)
1776 VALUE p, iclass, origin_stack = 0;
1777 int method_changed = 0;
1779 VALUE klass_origin = RCLASS_ORIGIN(klass);
1780 VALUE original_klass = klass;
1782 if (check_cyclic && module_in_super_chain(klass, module))
1787 int superclass_seen = FALSE;
1793 if (klass_origin != c || search_super) {
1799 if (klass_origin == p && !search_super)
1804 if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) {
1805 if (!superclass_seen && c_seen) {
1812 superclass_seen = TRUE;
1820 RB_DEBUG_COUNTER_INC(cvar_include_invalidate);
1821 ruby_vm_global_cvar_state++;
1822 tbl = RCLASS_M_TBL(module);
1823 if (tbl && rb_id_table_size(tbl)) {
1826 rb_id_table_foreach(tbl, clear_module_cache_i, (
void *)super_class);
1831 rb_id_table_foreach(tbl, clear_module_cache_i, (
void *)original_klass);
1838 iclass = rb_include_class_new(module, super_class);
1839 c = rb_class_set_super(c, iclass);
1840 RCLASS_SET_INCLUDER(iclass, klass);
1841 if (module != RCLASS_ORIGIN(module)) {
1843 VALUE origin[2] = {iclass, RCLASS_ORIGIN(module)};
1846 else if (origin_stack && (origin_len =
RARRAY_LEN(origin_stack)) > 1 &&
1847 RARRAY_AREF(origin_stack, origin_len - 1) == module) {
1848 RCLASS_WRITE_ORIGIN(
RARRAY_AREF(origin_stack, (origin_len -= 2)), iclass);
1849 RICLASS_WRITE_ORIGIN_SHARED_MTBL(iclass);
1855 rb_module_add_to_subclasses_list(m, iclass);
1858 VALUE refined_class =
1859 rb_refinement_module_get_refined_class(klass);
1861 rb_id_table_foreach(RCLASS_M_TBL(module), add_refined_method_entry_i, (
void *)refined_class);
1865 tbl = RCLASS_CONST_TBL(module);
1866 if (tbl && rb_id_table_size(tbl))
1867 rb_id_table_foreach(tbl, clear_constant_cache_i, NULL);
1869 module = RCLASS_SUPER(module);
1872 return method_changed;
1876include_modules_at(
const VALUE klass,
VALUE c,
VALUE module,
int search_super)
1878 return do_include_modules_at(klass, c, module, search_super,
true);
1881static enum rb_id_table_iterator_result
1882move_refined_method(
ID key,
VALUE value,
void *data)
1886 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1888 struct rb_id_table *tbl = RCLASS_WRITABLE_M_TBL(klass);
1890 if (me->def->body.refined.orig_me) {
1892 RB_OBJ_WRITE(me, &me->def->body.refined.orig_me, NULL);
1893 new_me = rb_method_entry_clone(me);
1894 rb_method_table_insert(klass, tbl, key, new_me);
1895 rb_method_entry_copy(me, orig_me);
1896 return ID_TABLE_CONTINUE;
1899 rb_method_table_insert(klass, tbl, key, me);
1900 return ID_TABLE_DELETE;
1904 return ID_TABLE_CONTINUE;
1908static enum rb_id_table_iterator_result
1909cache_clear_refined_method(
ID key,
VALUE value,
void *data)
1913 if (me->def->type == VM_METHOD_TYPE_REFINED && me->def->body.refined.orig_me) {
1915 rb_clear_method_cache(klass, me->called_id);
1920 return ID_TABLE_CONTINUE;
1924ensure_origin(
VALUE klass)
1926 VALUE origin = RCLASS_ORIGIN(klass);
1927 if (origin == klass) {
1929 RCLASS_SET_M_TBL(origin, RCLASS_M_TBL(klass));
1931 rb_class_set_super(klass, origin);
1932 RCLASS_WRITE_ORIGIN(klass, origin);
1933 class_clear_method_table(klass);
1934 rb_id_table_foreach(RCLASS_M_TBL(origin), cache_clear_refined_method, (
void *)klass);
1935 rb_id_table_foreach(RCLASS_M_TBL(origin), move_refined_method, (
void *)klass);
1945 bool klass_had_no_origin;
1947 ensure_includable(klass, module);
1948 if (module_in_super_chain(klass, module))
1949 rb_raise(rb_eArgError,
"cyclic prepend detected");
1951 klass_had_no_origin = ensure_origin(klass);
1952 changed = do_include_modules_at(klass, klass, module, FALSE,
false);
1955 rb_vm_check_redefinition_by_prepend(klass);
1959 VALUE klass_origin = RCLASS_ORIGIN(klass);
1960 struct rb_id_table *klass_m_tbl = RCLASS_M_TBL(klass);
1961 struct rb_id_table *klass_origin_m_tbl = RCLASS_M_TBL(klass_origin);
1965 if (!rb_objspace_garbage_object_p(iclass->klass)) {
1966 const VALUE subclass = iclass->klass;
1967 if (klass_had_no_origin && klass_origin_m_tbl == RCLASS_M_TBL(subclass)) {
1969 rb_id_table_foreach(RCLASS_M_TBL(subclass), clear_module_cache_i, (
void *)subclass);
1970 RCLASS_WRITE_M_TBL_EVEN_WHEN_PROMOTED(subclass, klass_m_tbl);
1972 rb_class_set_super(subclass, origin);
1973 RCLASS_SET_INCLUDER(origin, RCLASS_INCLUDER(subclass));
1974 RCLASS_WRITE_ORIGIN(subclass, origin);
1975 RICLASS_SET_ORIGIN_SHARED_MTBL(origin);
1977 include_modules_at(subclass, subclass, module, FALSE);
1980 iclass = iclass->next;
2012 VALUE origin = RCLASS_ORIGIN(mod);
2016 VALUE m = METACLASS_OF(p);
2051 if (METACLASS_OF(p) == mod2)
return Qtrue;
2081 refined_class = rb_refinement_module_get_refined_class(mod);
2085 if (p == refined_class)
break;
2086 if (p != RCLASS_ORIGIN(p))
continue;
2102 bool immediate_only;
2106class_descendants_recursive(
VALUE klass,
VALUE v)
2111 if (data->buffer && data->count < data->maxcount && !rb_objspace_garbage_object_p(klass)) {
2116 if (!data->immediate_only) {
2117 rb_class_foreach_subclass(klass, class_descendants_recursive, v);
2121 rb_class_foreach_subclass(klass, class_descendants_recursive, v);
2126class_descendants(
VALUE klass,
bool immediate_only)
2131 rb_class_foreach_subclass(klass, class_descendants_recursive, (
VALUE) &data);
2135 data.maxcount = data.count;
2138 size_t gc_count = rb_gc_count();
2141 rb_class_foreach_subclass(klass, class_descendants_recursive, (
VALUE) &data);
2143 if (gc_count != rb_gc_count()) {
2144 rb_bug(
"GC must not occur during the subclass iteration of Class#descendants");
2192 return class_descendants(klass,
true);
2215 if (!RCLASS_SINGLETON_P(klass)) {
2216 rb_raise(
rb_eTypeError,
"'%"PRIsVALUE
"' is not a singleton class", klass);
2219 return RCLASS_ATTACHED_OBJECT(klass);
2223ins_methods_push(st_data_t name, st_data_t ary)
2229ins_methods_i(st_data_t name, st_data_t
type, st_data_t ary)
2231 switch ((rb_method_visibility_t)
type) {
2232 case METHOD_VISI_UNDEF:
2233 case METHOD_VISI_PRIVATE:
2236 ins_methods_push(name, ary);
2243ins_methods_type_i(st_data_t name, st_data_t
type, st_data_t ary, rb_method_visibility_t visi)
2245 if ((rb_method_visibility_t)
type == visi) {
2246 ins_methods_push(name, ary);
2252ins_methods_prot_i(st_data_t name, st_data_t
type, st_data_t ary)
2254 return ins_methods_type_i(name,
type, ary, METHOD_VISI_PROTECTED);
2258ins_methods_priv_i(st_data_t name, st_data_t
type, st_data_t ary)
2260 return ins_methods_type_i(name,
type, ary, METHOD_VISI_PRIVATE);
2264ins_methods_pub_i(st_data_t name, st_data_t
type, st_data_t ary)
2266 return ins_methods_type_i(name,
type, ary, METHOD_VISI_PUBLIC);
2270ins_methods_undef_i(st_data_t name, st_data_t
type, st_data_t ary)
2272 return ins_methods_type_i(name,
type, ary, METHOD_VISI_UNDEF);
2280static enum rb_id_table_iterator_result
2281method_entry_i(
ID key,
VALUE value,
void *data)
2285 rb_method_visibility_t
type;
2287 if (me->def->type == VM_METHOD_TYPE_REFINED) {
2288 VALUE owner = me->owner;
2289 me = rb_resolve_refined_method(
Qnil, me);
2290 if (!me)
return ID_TABLE_CONTINUE;
2291 if (!arg->recur && me->owner != owner)
return ID_TABLE_CONTINUE;
2293 if (!st_is_member(arg->list, key)) {
2294 if (UNDEFINED_METHOD_ENTRY_P(me)) {
2295 type = METHOD_VISI_UNDEF;
2298 type = METHOD_ENTRY_VISI(me);
2301 st_add_direct(arg->list, key, (st_data_t)
type);
2303 return ID_TABLE_CONTINUE;
2311 rb_id_table_foreach(m_tbl, method_entry_i, me_arg);
2315particular_class_p(
VALUE mod)
2317 if (!mod)
return false;
2318 if (RCLASS_SINGLETON_P(mod))
return true;
2324class_instance_method_list(
int argc,
const VALUE *argv,
VALUE mod,
int obj,
int (*func) (st_data_t, st_data_t, st_data_t))
2327 int recur = TRUE, prepended = 0;
2332 me_arg.list = st_init_numtable();
2333 me_arg.recur = recur;
2336 for (; particular_class_p(mod); mod =
RCLASS_SUPER(mod)) {
2337 add_instance_method_list(mod, &me_arg);
2341 if (!recur && RCLASS_ORIGIN(mod) != mod) {
2342 mod = RCLASS_ORIGIN(mod);
2347 add_instance_method_list(mod, &me_arg);
2352 st_foreach(me_arg.list, func, ary);
2353 st_free_table(me_arg.list);
2397 return class_instance_method_list(argc, argv, mod, 0, ins_methods_i);
2412 return class_instance_method_list(argc, argv, mod, 0, ins_methods_prot_i);
2435 return class_instance_method_list(argc, argv, mod, 0, ins_methods_priv_i);
2450 return class_instance_method_list(argc, argv, mod, 0, ins_methods_pub_i);
2462rb_class_undefined_instance_methods(
VALUE mod)
2465 return class_instance_method_list(1, &include_super, mod, 0, ins_methods_undef_i);
2499rb_obj_methods(
int argc,
const VALUE *argv,
VALUE obj)
2502 if (argc > 0 && !
RTEST(argv[0])) {
2505 return class_instance_method_list(argc, argv,
CLASS_OF(obj), 1, ins_methods_i);
2518rb_obj_protected_methods(
int argc,
const VALUE *argv,
VALUE obj)
2520 return class_instance_method_list(argc, argv,
CLASS_OF(obj), 1, ins_methods_prot_i);
2533rb_obj_private_methods(
int argc,
const VALUE *argv,
VALUE obj)
2535 return class_instance_method_list(argc, argv,
CLASS_OF(obj), 1, ins_methods_priv_i);
2548rb_obj_public_methods(
int argc,
const VALUE *argv,
VALUE obj)
2550 return class_instance_method_list(argc, argv,
CLASS_OF(obj), 1, ins_methods_pub_i);
2589 VALUE ary, klass, origin;
2595 if (RCLASS_SINGLETON_P(obj)) {
2599 origin = RCLASS_ORIGIN(klass);
2600 me_arg.list = st_init_numtable();
2601 me_arg.recur = recur;
2602 if (klass && RCLASS_SINGLETON_P(klass)) {
2603 if ((mtbl = RCLASS_M_TBL(origin)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2608 if (klass != origin && (mtbl = RCLASS_M_TBL(klass)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2613 st_foreach(me_arg.list, ins_methods_i, ary);
2614 st_free_table(me_arg.list);
2627#ifdef rb_define_method_id
2628#undef rb_define_method_id
2633 rb_add_method_cfunc(klass, mid, func, argc, METHOD_VISI_PUBLIC);
2636#ifdef rb_define_method
2637#undef rb_define_method
2642 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PUBLIC);
2645#ifdef rb_define_protected_method
2646#undef rb_define_protected_method
2651 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PROTECTED);
2654#ifdef rb_define_private_method
2655#undef rb_define_private_method
2660 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PRIVATE);
2666 rb_add_method(klass, rb_intern(name), VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2669static enum rb_id_table_iterator_result
2670undef_method_i(
ID name,
VALUE value,
void *data)
2673 rb_add_method(klass, name, VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2674 return ID_TABLE_CONTINUE;
2678rb_undef_methods_from(
VALUE klass,
VALUE super)
2682 rb_id_table_foreach(mtbl, undef_method_i, (
void *)klass);
2695special_singleton_class_of(
VALUE obj)
2701 default:
return Qnil;
2706rb_special_singleton_class(
VALUE obj)
2708 return special_singleton_class_of(obj);
2721singleton_class_of(
VALUE obj)
2725 switch (
TYPE(obj)) {
2735 klass = special_singleton_class_of(obj);
2737 rb_bug(
"unknown immediate %p", (
void *)obj);
2741 if (CHILLED_STRING_P(obj)) {
2742 CHILLED_STRING_MUTATED(obj);
2749 klass = METACLASS_OF(obj);
2750 if (!(RCLASS_SINGLETON_P(klass) &&
2751 RCLASS_ATTACHED_OBJECT(klass) == obj)) {
2752 klass = rb_make_metaclass(obj, klass);
2764 if (!RCLASS_SINGLETON_P(x)) {
2786 return rb_special_singleton_class(obj);
2788 klass = METACLASS_OF(obj);
2789 if (!RCLASS_SINGLETON_P(klass))
return Qnil;
2790 if (RCLASS_ATTACHED_OBJECT(klass) != obj)
return Qnil;
2797 VALUE klass = singleton_class_of(obj);
2814#ifdef rb_define_singleton_method
2815#undef rb_define_singleton_method
2823#ifdef rb_define_module_function
2824#undef rb_define_module_function
2833#ifdef rb_define_global_function
2834#undef rb_define_global_function
2845 rb_alias(klass, rb_intern(name1), rb_intern(name2));
2851 rb_attr(klass, rb_intern(name), read, write, FALSE);
2855rb_keyword_error_new(
const char *error,
VALUE keys)
2858 VALUE error_message = rb_sprintf(
"%s keyword%.*s", error,
len > 1,
"s");
2865 if (++i >=
len)
break;
2873NORETURN(
static void rb_keyword_error(
const char *error,
VALUE keys));
2875rb_keyword_error(
const char *error,
VALUE keys)
2880NORETURN(
static void unknown_keyword_error(
VALUE hash,
const ID *table,
int keywords));
2882unknown_keyword_error(
VALUE hash,
const ID *table,
int keywords)
2885 for (i = 0; i < keywords; i++) {
2886 st_data_t key =
ID2SYM(table[i]);
2887 rb_hash_stlike_delete(hash, &key, NULL);
2889 rb_keyword_error(
"unknown", rb_hash_keys(hash));
2894separate_symbol(st_data_t key, st_data_t value, st_data_t arg)
2898 if (!*kwdhash) *kwdhash = rb_hash_new();
2899 rb_hash_aset(*kwdhash, (
VALUE)key, (
VALUE)value);
2906 VALUE parthash[2] = {0, 0};
2907 VALUE hash = *orighash;
2914 *orighash = parthash[1];
2929#define extract_kwarg(keyword, val) \
2930 (key = (st_data_t)(keyword), values ? \
2931 (rb_hash_stlike_delete(keyword_hash, &key, &(val)) || ((val) = Qundef, 0)) : \
2932 rb_hash_stlike_lookup(keyword_hash, key, NULL))
2934 if (
NIL_P(keyword_hash)) keyword_hash = 0;
2938 optional = -1-optional;
2941 for (; i < required; i++) {
2944 if (extract_kwarg(keyword, values[i])) {
2951 if (!
NIL_P(missing)) {
2952 rb_keyword_error(
"missing", missing);
2956 if (optional && keyword_hash) {
2957 for (i = 0; i < optional; i++) {
2958 if (extract_kwarg(
ID2SYM(table[required+i]), values[required+i])) {
2963 if (!rest && keyword_hash) {
2964 if (
RHASH_SIZE(keyword_hash) > (
unsigned int)(values ? 0 : j)) {
2965 unknown_keyword_error(keyword_hash, table, required+optional);
2968 if (values && !keyword_hash) {
2969 for (i = 0; i < required + optional; i++) {
2988rb_scan_args_parse(
int kw_flag,
const char *fmt,
struct rb_scan_args_t *arg)
2990 const char *p = fmt;
2992 memset(arg, 0,
sizeof(*arg));
2993 arg->kw_flag = kw_flag;
2996 arg->n_lead = *p -
'0';
2999 arg->n_opt = *p -
'0';
3008 arg->n_trail = *p -
'0';
3020 rb_fatal(
"bad scan arg format: %s", fmt);
3025rb_scan_args_assign(
const struct rb_scan_args_t *arg,
int argc,
const VALUE *
const argv, va_list vargs)
3029#define rb_scan_args_next_param() va_arg(vargs, VALUE *)
3030 const int kw_flag = arg->kw_flag;
3031 const int n_lead = arg->n_lead;
3032 const int n_opt = arg->n_opt;
3033 const int n_trail = arg->n_trail;
3034 const int n_mand = n_lead + n_trail;
3035 const bool f_var = arg->f_var;
3036 const bool f_hash = arg->f_hash;
3037 const bool f_block = arg->f_block;
3040 if (f_hash && argc > 0) {
3041 VALUE last = argv[argc - 1];
3042 if (rb_scan_args_keyword_p(kw_flag, last)) {
3043 hash = rb_hash_dup(last);
3048 if (argc < n_mand) {
3053 for (i = 0; i < n_lead; i++) {
3054 var = rb_scan_args_next_param();
3055 if (var) *var = argv[argi];
3059 for (i = 0; i < n_opt; i++) {
3060 var = rb_scan_args_next_param();
3061 if (argi < argc - n_trail) {
3062 if (var) *var = argv[argi];
3066 if (var) *var =
Qnil;
3071 int n_var = argc - argi - n_trail;
3073 var = rb_scan_args_next_param();
3083 for (i = 0; i < n_trail; i++) {
3084 var = rb_scan_args_next_param();
3085 if (var) *var = argv[argi];
3090 var = rb_scan_args_next_param();
3091 if (var) *var = hash;
3095 var = rb_scan_args_next_param();
3110#undef rb_scan_args_next_param
3114rb_scan_args_result(
const struct rb_scan_args_t *
const arg,
int argc)
3116 const int n_lead = arg->n_lead;
3117 const int n_opt = arg->n_opt;
3118 const int n_trail = arg->n_trail;
3119 const int n_mand = n_lead + n_trail;
3120 const bool f_var = arg->f_var;
3138 va_start(vargs,fmt);
3139 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
3141 return rb_scan_args_result(&arg, argc);
3144#undef rb_scan_args_kw
3150 rb_scan_args_parse(kw_flag, fmt, &arg);
3151 va_start(vargs,fmt);
3152 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
3154 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().
@ RUBY_FL_USER1
User-defined flag.
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
static VALUE class_alloc(VALUE flags, VALUE klass)
Allocates a struct RClass for a new class.
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.
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 FL_UNSET_RAW
Old name of RB_FL_UNSET_RAW.
#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 T_MASK
Old name of RUBY_T_MASK.
#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 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 RBASIC(obj)
Convenient casting macro.
#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.