17#include "ruby/internal/config.h"
21#include "debug_counter.h"
24#include "internal/box.h"
25#include "internal/class.h"
26#include "internal/eval.h"
27#include "internal/hash.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)
83static enum rb_id_table_iterator_result
84cvar_table_free_i(
VALUE value,
void *ctx)
87 return ID_TABLE_CONTINUE;
94 st_data_t key = (st_data_t)box->box_object;
95 VALUE obj_id = rb_obj_id(klass);
96 st_delete(box->classext_cow_classes, &obj_id, 0);
97 st_delete(RCLASS_CLASSEXT_TBL(klass), &key, &ext);
106 rb_id_table_free(RCLASSEXT_M_TBL(ext));
108 if (!RCLASSEXT_SHARED_CONST_TBL(ext) && (tbl = RCLASSEXT_CONST_TBL(ext)) != NULL) {
109 rb_free_const_table(tbl);
112 if ((tbl = RCLASSEXT_CVC_TBL(ext)) != NULL) {
113 rb_id_table_foreach_values(tbl, cvar_table_free_i, NULL);
114 rb_id_table_free(tbl);
117 rb_class_classext_free_subclasses(ext, klass,
false);
119 if (RCLASSEXT_SUPERCLASSES_WITH_SELF(ext)) {
121 xfree(RCLASSEXT_SUPERCLASSES(ext));
132 if (RCLASSEXT_ICLASS_IS_ORIGIN(ext) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext)) {
134 rb_id_table_free(RCLASSEXT_M_TBL(ext));
137 if (RCLASSEXT_CALLABLE_M_TBL(ext) != NULL) {
138 rb_id_table_free(RCLASSEXT_CALLABLE_M_TBL(ext));
141 rb_class_classext_free_subclasses(ext, klass,
false);
151 if (RCLASSEXT_ICLASS_IS_ORIGIN(ext) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext)) {
153 rb_id_table_free(RCLASSEXT_M_TBL(ext));
156 if (RCLASSEXT_CALLABLE_M_TBL(ext) != NULL) {
157 rb_id_table_free(RCLASSEXT_CALLABLE_M_TBL(ext));
160 rb_class_classext_free_subclasses(ext, klass,
true);
171set_box_classext_update(st_data_t *key_ptr, st_data_t *val_ptr, st_data_t a,
int existing)
177 iclass_free_orphan_classext(args->obj, (
rb_classext_t *)*val_ptr);
180 rb_bug(
"Updating existing classext for non-iclass never happen");
184 *val_ptr = (st_data_t)args->ext;
197 VM_ASSERT(BOX_USER_P(box));
199 st_update(RCLASS_CLASSEXT_TBL(obj), (st_data_t)box->box_object, set_box_classext_update, (st_data_t)&args);
200 st_insert(box->classext_cow_classes, (st_data_t)rb_obj_id(obj), obj);
206 rb_gc_writebarrier_remember(obj);
216static enum rb_id_table_iterator_result
217duplicate_classext_id_table_i(
ID key,
VALUE value,
void *data)
220 rb_id_table_insert(tbl, key, value);
221 return ID_TABLE_CONTINUE;
224static enum rb_id_table_iterator_result
225duplicate_classext_m_tbl_i(
ID key,
VALUE value,
void *data)
229 rb_method_table_insert0(arg->klass, arg->tbl, key, me,
false);
230 return ID_TABLE_CONTINUE;
234duplicate_classext_m_tbl(
struct rb_id_table *orig,
VALUE klass,
bool init_missing)
239 return rb_id_table_create(0);
243 tbl = rb_id_table_create(rb_id_table_size(orig));
248 rb_id_table_foreach(orig, duplicate_classext_m_tbl_i, &data);
253duplicate_classext_id_table(
struct rb_id_table *orig,
bool init_missing)
259 return rb_id_table_create(0);
263 tbl = rb_id_table_create(rb_id_table_size(orig));
264 rb_id_table_foreach(orig, duplicate_classext_id_table_i, tbl);
274 dst->flag = src->flag;
275 dst->line = src->line;
282static enum rb_id_table_iterator_result
283duplicate_classext_const_tbl_i(
ID key,
VALUE value,
void *data)
288 rb_id_table_insert(arg->tbl, key, (
VALUE)entry);
290 return ID_TABLE_CONTINUE;
301 dst = rb_id_table_create(rb_id_table_size(src));
307 rb_id_table_foreach(src, duplicate_classext_const_tbl_i, (
void *)&data);
313box_subclasses_tbl_key(
const rb_box_t *box)
318 return (
VALUE)box->box_id;
329 if (RCLASSEXT_SUBCLASSES(orig)) {
330 orig_anchor = RCLASSEXT_SUBCLASSES(orig);
331 box_subclasses = orig_anchor->box_subclasses;
335 anchor->box_subclasses = rb_box_subclasses_ref_inc(box_subclasses);
340 RCLASSEXT_SUBCLASSES(copy) = anchor;
343 entry = orig_anchor->head;
348 if (rb_objspace_garbage_object_p(entry->klass)) {
353 cdr->klass = entry->klass;
357 VALUE box_id = box_subclasses_tbl_key(RCLASSEXT_BOX(copy));
359 st_insert(tbl, box_id, (st_data_t)first);
366 if (RCLASSEXT_BOX_SUPER_SUBCLASSES(orig))
367 RCLASSEXT_BOX_SUPER_SUBCLASSES(copy) = rb_box_subclasses_ref_inc(RCLASSEXT_BOX_SUPER_SUBCLASSES(orig));
368 if (RCLASSEXT_BOX_MODULE_SUBCLASSES(orig))
369 RCLASSEXT_BOX_MODULE_SUBCLASSES(copy) = rb_box_subclasses_ref_inc(RCLASSEXT_BOX_MODULE_SUBCLASSES(orig));
378 rb_classext_t *ext = RCLASS_EXT_TABLE_LOOKUP_INTERNAL(iclass, box);
383 rb_invalidate_method_caches(RCLASSEXT_CALLABLE_M_TBL(ext), RCLASSEXT_CC_TBL(ext));
388 RCLASSEXT_BOX(ext) = box;
390 RCLASSEXT_SUPER(ext) = RCLASSEXT_SUPER(src);
393 if (RCLASSEXT_ICLASS_IS_ORIGIN(src) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(src)) {
394 RCLASSEXT_M_TBL(ext) = duplicate_classext_m_tbl(RCLASSEXT_M_TBL(src), iclass,
true);
397 RCLASSEXT_M_TBL(ext) = RCLASSEXT_M_TBL(mod_ext);
400 RCLASSEXT_CONST_TBL(ext) = RCLASSEXT_CONST_TBL(mod_ext);
401 RCLASSEXT_CVC_TBL(ext) = RCLASSEXT_CVC_TBL(mod_ext);
408 duplicate_classext_subclasses(src, ext);
410 RCLASSEXT_SET_ORIGIN(ext, iclass, RCLASSEXT_ORIGIN(src));
411 RCLASSEXT_ICLASS_IS_ORIGIN(ext) = RCLASSEXT_ICLASS_IS_ORIGIN(src);
412 RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext) = RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(src);
414 RCLASSEXT_SET_INCLUDER(ext, iclass, RCLASSEXT_INCLUDER(src));
418 first_set = RCLASS_SET_BOX_CLASSEXT(iclass, box, ext);
420 RCLASS_SET_PRIME_CLASSEXT_WRITABLE(iclass,
false);
432 RCLASSEXT_BOX(ext) = box;
434 RCLASSEXT_SUPER(ext) = RCLASSEXT_SUPER(orig);
436 RCLASSEXT_M_TBL(ext) = duplicate_classext_m_tbl(RCLASSEXT_M_TBL(orig), klass, dup_iclass);
437 RCLASSEXT_ICLASS_IS_ORIGIN(ext) =
true;
438 RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext) =
false;
440 if (orig->fields_obj) {
441 RB_OBJ_WRITE(klass, &ext->fields_obj, rb_imemo_fields_clone(orig->fields_obj));
444 if (RCLASSEXT_SHARED_CONST_TBL(orig)) {
445 RCLASSEXT_CONST_TBL(ext) = RCLASSEXT_CONST_TBL(orig);
446 RCLASSEXT_SHARED_CONST_TBL(ext) =
true;
449 RCLASSEXT_CONST_TBL(ext) = duplicate_classext_const_tbl(RCLASSEXT_CONST_TBL(orig), klass);
450 RCLASSEXT_SHARED_CONST_TBL(ext) =
false;
462 RCLASSEXT_CVC_TBL(ext) = duplicate_classext_id_table(RCLASSEXT_CVC_TBL(orig), dup_iclass);
465 duplicate_classext_subclasses(orig, ext);
467 RCLASSEXT_SET_ORIGIN(ext, klass, RCLASSEXT_ORIGIN(orig));
476 RCLASSEXT_PERMANENT_CLASSPATH(ext) = RCLASSEXT_PERMANENT_CLASSPATH(orig);
477 RCLASSEXT_CLONED(ext) = RCLASSEXT_CLONED(orig);
478 RCLASSEXT_CLASSPATH(ext) = RCLASSEXT_CLASSPATH(orig);
491 while (subclass_entry) {
493 iclass = subclass_entry->klass;
499 class_duplicate_iclass_classext(iclass, ext, box);
502 subclass_entry = subclass_entry->next;
510rb_class_ensure_writable(
VALUE klass)
513 RCLASS_EXT_WRITABLE(klass);
517 rb_class_classext_foreach_callback_func *func;
522class_classext_foreach_i(st_data_t key, st_data_t value, st_data_t arg)
525 rb_class_classext_foreach_callback_func *func =
foreach_arg->func;
531rb_class_classext_foreach(
VALUE klass, rb_class_classext_foreach_callback_func *func,
void *arg)
533 st_table *tbl = RCLASS_CLASSEXT_TBL(klass);
538 rb_st_foreach(tbl, class_classext_foreach_i, (st_data_t)&
foreach_arg);
540 func(RCLASS_EXT_PRIME(klass),
true, (
VALUE)NULL, arg);
544rb_class_super_of(
VALUE klass)
550rb_class_singleton_p(
VALUE klass)
552 return RCLASS_SINGLETON_P(klass);
556rb_class_variation_count(
VALUE klass)
558 return RCLASS_VARIATION_COUNT(klass);
562push_subclass_entry_to_list(
VALUE super,
VALUE klass,
bool is_module)
568 const rb_box_t *box = rb_current_box();
571 entry->klass = klass;
574 anchor = RCLASS_WRITABLE_SUBCLASSES(super);
577 VM_ASSERT(box_subclasses);
578 tbl = box_subclasses->tbl;
583 head->next->prev = entry;
584 entry->next = head->next;
588 st_insert(tbl, box_subclasses_tbl_key(box), (st_data_t)entry);
592 RCLASS_WRITE_BOX_MODULE_SUBCLASSES(klass, anchor->box_subclasses);
595 RCLASS_WRITE_BOX_SUPER_SUBCLASSES(klass, anchor->box_subclasses);
600rb_class_subclass_add(
VALUE super,
VALUE klass)
602 if (super && !UNDEF_P(super)) {
603 push_subclass_entry_to_list(super, klass,
false);
608rb_module_add_to_subclasses_list(
VALUE module,
VALUE iclass)
610 if (module && !UNDEF_P(module)) {
611 push_subclass_entry_to_list(module, iclass,
true);
616class_get_subclasses_for_ns(
struct st_table *tbl,
VALUE box_id)
619 if (st_lookup(tbl, (st_data_t)box_id, &value)) {
626remove_class_from_subclasses_replace_first_entry(st_data_t *key, st_data_t *value, st_data_t arg,
int existing)
636 bool first_entry =
true;
638 if (entry->klass == klass) {
650 st_update(tbl, box_id, remove_class_from_subclasses_replace_first_entry, (st_data_t)next);
654 st_delete(tbl, &box_id, NULL);
662 else if (first_entry) {
670rb_class_remove_from_super_subclasses(
VALUE klass)
675 if (!box_subclasses)
return;
676 remove_class_from_subclasses(box_subclasses->tbl, box_subclasses_tbl_key(RCLASSEXT_BOX(ext)), klass);
677 rb_box_subclasses_ref_dec(box_subclasses);
678 RCLASSEXT_BOX_SUPER_SUBCLASSES(ext) = 0;
685 struct st_table *tbl = anchor->box_subclasses->tbl;
686 VALUE box_id = box_subclasses_tbl_key(RCLASSEXT_BOX(ext));
695 rb_box_subclasses_ref_count(anchor->box_subclasses) > 0,
696 "box_subclasses refcount (%p) %ld", anchor->box_subclasses, rb_box_subclasses_ref_count(anchor->box_subclasses));
697 st_delete(tbl, &box_id, NULL);
698 rb_box_subclasses_ref_dec(anchor->box_subclasses);
701 if (RCLASSEXT_BOX_SUPER_SUBCLASSES(ext)) {
703 if (!replacing) remove_class_from_subclasses(box_sub->tbl, box_id, klass);
704 rb_box_subclasses_ref_dec(box_sub);
706 if (RCLASSEXT_BOX_MODULE_SUBCLASSES(ext)) {
708 if (!replacing) remove_class_from_subclasses(box_sub->tbl, box_id, klass);
709 rb_box_subclasses_ref_dec(box_sub);
721 VALUE curklass = cur->klass;
731class_detach_subclasses(
VALUE klass,
VALUE arg)
733 rb_class_remove_from_super_subclasses(klass);
737class_switch_superclass(
VALUE super,
VALUE klass)
740 class_detach_subclasses(klass,
Qnil);
741 rb_class_subclass_add(super, klass);
760 const rb_box_t *box = rb_current_box();
762 if (!ruby_box_init_done) {
778 box_subclasses->refcount = 1;
779 box_subclasses->tbl = st_init_numtable();
781 anchor->box_subclasses = box_subclasses;
788 if (boxable) flags |= RCLASS_BOXABLE;
790 NEWOBJ_OF(obj,
struct RClass, klass, flags, alloc_size, 0);
807 RCLASS_PRIME_BOX((
VALUE)obj) = box;
810 RCLASS_SET_PRIME_CLASSEXT_WRITABLE((
VALUE)obj, !boxable || BOX_USER_P(box));
813 RCLASS_SET_REFINED_CLASS((
VALUE)obj,
Qnil);
815 RCLASS_SET_SUBCLASSES((
VALUE)obj, anchor);
823 bool boxable = rb_box_available() && BOX_ROOT_P(rb_current_box());
828class_associate_super(
VALUE klass,
VALUE super,
bool init)
830 if (super && !UNDEF_P(super)) {
831 class_switch_superclass(super, klass);
834 RCLASS_SET_SUPER(klass, super);
837 RCLASS_WRITE_SUPER(klass, super);
839 rb_class_update_superclasses(klass);
846 return class_associate_super(klass, super,
false);
850class_initialize_method_table(
VALUE c)
853 RCLASS_SET_M_TBL(c, rb_id_table_create(0));
857class_clear_method_table(
VALUE c)
859 RCLASS_WRITE_M_TBL(c, rb_id_table_create(0));
863class_boot_boxable(
VALUE super,
bool boxable)
869 class_initialize_method_table(klass);
871 class_associate_super(klass, super,
true);
872 if (super && !UNDEF_P(super)) {
873 rb_class_set_initialized(klass);
891 return class_boot_boxable(super,
false);
895class_superclasses_including_self(
VALUE klass)
897 if (RCLASS_SUPERCLASSES_WITH_SELF_P(klass))
898 return RCLASS_SUPERCLASSES(klass);
900 size_t depth = RCLASS_SUPERCLASS_DEPTH(klass);
903 memcpy(superclasses, RCLASS_SUPERCLASSES(klass),
sizeof(
VALUE) * depth);
904 superclasses[depth] = klass;
910rb_class_update_superclasses(
VALUE klass)
917 if (UNDEF_P(super))
return;
920 if (RCLASS_SUPERCLASSES(klass))
935 rb_class_update_superclasses(super);
938 if (!RCLASS_SUPERCLASS_DEPTH(super))
942 super_depth = RCLASS_SUPERCLASS_DEPTH(super);
943 if (RCLASS_SUPERCLASSES_WITH_SELF_P(super)) {
944 superclasses = RCLASS_SUPERCLASSES(super);
947 superclasses = class_superclasses_including_self(super);
948 RCLASS_WRITE_SUPERCLASSES(super, super_depth, superclasses,
true);
951 size_t depth = super_depth == RCLASS_MAX_SUPERCLASS_DEPTH ? super_depth : super_depth + 1;
952 RCLASS_WRITE_SUPERCLASSES(klass, depth, superclasses,
false);
959 rb_raise(
rb_eTypeError,
"superclass must be an instance of Class (given an instance of %"PRIsVALUE
")",
962 if (RCLASS_SINGLETON_P(super)) {
963 rb_raise(
rb_eTypeError,
"can't make subclass of singleton class");
978 RCLASS_SET_MAX_IV_COUNT(klass, RCLASS_MAX_IV_COUNT(super));
981 RUBY_ASSERT(getenv(
"RUBY_BOX") || RCLASS_PRIME_CLASSEXT_WRITABLE_P(klass));
987rb_class_s_alloc(
VALUE klass)
995 if (me->def->type == VM_METHOD_TYPE_ISEQ) {
996 rb_cref_t *new_cref = rb_vm_rewrite_cref(me->def->body.iseq.
cref, old_klass, new_klass);
997 rb_add_method_iseq(new_klass, mid, me->def->body.iseq.
iseqptr, new_cref, METHOD_ENTRY_VISI(me));
1000 rb_method_entry_set(new_klass, mid, me, METHOD_ENTRY_VISI(me));
1009static enum rb_id_table_iterator_result
1010clone_method_i(
ID key,
VALUE value,
void *data)
1013 clone_method(arg->old_klass, arg->new_klass, key, (
const rb_method_entry_t *)value);
1014 return ID_TABLE_CONTINUE;
1030 rb_id_table_insert(arg->tbl, key, (
VALUE)nce);
1031 return ID_TABLE_CONTINUE;
1034static enum rb_id_table_iterator_result
1035clone_const_i(
ID key,
VALUE value,
void *data)
1041class_init_copy_check(
VALUE clone,
VALUE orig)
1046 if (RCLASS_INITIALIZED_P(clone)) {
1049 if (RCLASS_SINGLETON_P(orig)) {
1059static enum rb_id_table_iterator_result
1060cvc_table_copy(
ID id,
VALUE val,
void *data)
1069 ent->class_value = ctx->clone;
1070 ent->cref = orig_entry->cref;
1071 ent->global_cvar_state = orig_entry->global_cvar_state;
1072 rb_id_table_insert(ctx->new_table,
id, (
VALUE)ent);
1076 return ID_TABLE_CONTINUE;
1082 if (RCLASS_CONST_TBL(clone)) {
1083 rb_free_const_table(RCLASS_CONST_TBL(clone));
1084 RCLASS_WRITE_CONST_TBL(clone, 0,
false);
1086 if (RCLASS_CVC_TBL(orig)) {
1087 struct rb_id_table *rb_cvc_tbl = RCLASS_CVC_TBL(orig);
1088 struct rb_id_table *rb_cvc_tbl_dup = rb_id_table_create(rb_id_table_size(rb_cvc_tbl));
1092 ctx.new_table = rb_cvc_tbl_dup;
1093 rb_id_table_foreach(rb_cvc_tbl, cvc_table_copy, &ctx);
1094 RCLASS_WRITE_CVC_TBL(clone, rb_cvc_tbl_dup);
1096 rb_id_table_free(RCLASS_M_TBL(clone));
1097 RCLASS_WRITE_M_TBL(clone, 0);
1099 rb_fields_tbl_copy(clone, orig);
1101 if (RCLASS_CONST_TBL(orig)) {
1104 struct rb_id_table *orig_tbl = RCLASS_CONST_TBL(orig);
1105 arg.tbl = const_tbl = rb_id_table_create(rb_id_table_size(orig_tbl));
1107 rb_id_table_foreach(orig_tbl, clone_const_i, &arg);
1108 RCLASS_WRITE_CONST_TBL(clone, const_tbl,
false);
1112static bool ensure_origin(
VALUE klass);
1115rb_class_set_initialized(
VALUE klass)
1123rb_module_check_initializable(
VALUE mod)
1125 if (RCLASS_INITIALIZED_P(mod)) {
1139 class_init_copy_check(clone, orig);
1142 rb_module_check_initializable(clone);
1152 rb_class_set_initialized(clone);
1157 RCLASS_SET_CLONED(clone,
true);
1158 RCLASS_SET_CLONED(orig,
true);
1160 if (!RCLASS_SINGLETON_P(
CLASS_OF(clone))) {
1165 RCLASS_SET_ALLOCATOR(clone, RCLASS_ALLOCATOR(orig));
1167 copy_tables(clone, orig);
1168 if (RCLASS_M_TBL(orig)) {
1170 arg.old_klass = orig;
1171 arg.new_klass = clone;
1172 class_initialize_method_table(clone);
1173 rb_id_table_foreach(RCLASS_M_TBL(orig), clone_method_i, &arg);
1176 if (RCLASS_ORIGIN(orig) == orig) {
1181 VALUE orig_origin = RCLASS_ORIGIN(orig);
1182 VALUE prev_clone_p = clone;
1190 ensure_origin(clone);
1191 clone_origin = RCLASS_ORIGIN(clone);
1193 while (p && p != orig_origin) {
1195 rb_bug(
"non iclass between module/class and origin");
1197 clone_p = class_alloc(
T_ICLASS, METACLASS_OF(p));
1198 RCLASS_SET_M_TBL(clone_p, RCLASS_M_TBL(p));
1199 rb_class_set_super(prev_clone_p, clone_p);
1200 prev_clone_p = clone_p;
1201 RCLASS_SET_CONST_TBL(clone_p, RCLASS_CONST_TBL(p),
false);
1203 RCLASS_SET_INCLUDER(clone_p, clone);
1205 add_subclass = TRUE;
1206 if (p != RCLASS_ORIGIN(p)) {
1207 origin[0] = clone_p;
1208 origin[1] = RCLASS_ORIGIN(p);
1211 else if ((origin_len =
RARRAY_LEN(origin_stack)) > 1 &&
1213 RCLASS_WRITE_ORIGIN(
RARRAY_AREF(origin_stack, (origin_len -= 2)), clone_p);
1214 RICLASS_WRITE_ORIGIN_SHARED_MTBL(clone_p);
1216 add_subclass = FALSE;
1219 rb_module_add_to_subclasses_list(METACLASS_OF(p), clone_p);
1224 if (p == orig_origin) {
1226 rb_class_set_super(clone_p, clone_origin);
1227 rb_class_set_super(clone_origin,
RCLASS_SUPER(orig_origin));
1229 copy_tables(clone_origin, orig_origin);
1230 if (RCLASS_M_TBL(orig_origin)) {
1232 arg.old_klass = orig;
1233 arg.new_klass = clone;
1234 class_initialize_method_table(clone_origin);
1235 rb_id_table_foreach(RCLASS_M_TBL(orig_origin), clone_method_i, &arg);
1239 rb_bug(
"no origin for class that has origin");
1242 rb_class_update_superclasses(clone);
1251 return rb_singleton_class_clone_and_attach(obj,
Qundef);
1256rb_singleton_class_clone_and_attach(
VALUE obj,
VALUE attach)
1258 const VALUE klass = METACLASS_OF(obj);
1264 if (!(RCLASS_SINGLETON_P(klass) && RCLASS_ATTACHED_OBJECT(klass) == obj)) {
1270 bool klass_of_clone_is_new;
1275 klass_of_clone_is_new =
true;
1276 RBASIC_SET_CLASS(clone, clone);
1282 klass_of_clone_is_new = (METACLASS_OF(klass) != klass_metaclass_clone);
1283 RBASIC_SET_CLASS(clone, klass_metaclass_clone);
1287 class_initialize_method_table(clone);
1290 rb_fields_tbl_copy(clone, klass);
1291 if (RCLASS_CONST_TBL(klass)) {
1294 arg.tbl = table = rb_id_table_create(rb_id_table_size(RCLASS_CONST_TBL(klass)));
1296 rb_id_table_foreach(RCLASS_CONST_TBL(klass), clone_const_i, &arg);
1297 RCLASS_SET_CONST_TBL(clone, table,
false);
1299 if (!UNDEF_P(attach)) {
1304 arg.old_klass = klass;
1305 arg.new_klass = clone;
1306 rb_id_table_foreach(RCLASS_M_TBL(klass), clone_method_i, &arg);
1308 if (klass_of_clone_is_new) {
1320 if (RCLASS_SINGLETON_P(klass)) {
1321 RCLASS_SET_ATTACHED_OBJECT(klass, obj);
1330#define META_CLASS_OF_CLASS_CLASS_P(k) (METACLASS_OF(k) == (k))
1333rb_singleton_class_has_metaclass_p(
VALUE sklass)
1335 return RCLASS_ATTACHED_OBJECT(METACLASS_OF(sklass)) == sklass;
1339rb_singleton_class_internal_p(
VALUE sklass)
1342 !rb_singleton_class_has_metaclass_p(sklass));
1350#define HAVE_METACLASS_P(k) \
1351 (FL_TEST(METACLASS_OF(k), FL_SINGLETON) && \
1352 rb_singleton_class_has_metaclass_p(k))
1361#define ENSURE_EIGENCLASS(klass) \
1362 (HAVE_METACLASS_P(klass) ? METACLASS_OF(klass) : make_metaclass(klass))
1384 SET_METACLASS_OF(klass, metaclass);
1385 SET_METACLASS_OF(metaclass, metaclass);
1388 VALUE tmp = METACLASS_OF(klass);
1389 SET_METACLASS_OF(klass, metaclass);
1396 rb_class_set_initialized(klass);
1399 rb_class_update_superclasses(METACLASS_OF(metaclass));
1413 VALUE orig_class = METACLASS_OF(obj);
1414 VALUE klass = class_boot_boxable(orig_class,
FL_TEST_RAW(orig_class, RCLASS_BOXABLE));
1417 RBASIC_SET_CLASS(obj, klass);
1419 rb_yjit_invalidate_no_singleton_class(orig_class);
1420 rb_zjit_invalidate_no_singleton_class(orig_class);
1422 SET_METACLASS_OF(klass, METACLASS_OF(
rb_class_real(orig_class)));
1428boot_defclass(
const char *name,
VALUE super)
1431 ID id = rb_intern(name);
1433 rb_const_set((rb_cObject ? rb_cObject : obj), id, obj);
1434 rb_vm_register_global_object(obj);
1485refinement_import_methods(
int argc,
VALUE *argv,
VALUE refinement)
1510Init_class_hierarchy(
void)
1514 rb_vm_register_global_object(rb_cObject);
1519 rb_cModule = boot_defclass(
"Module", rb_cObject);
1531 RBASIC_SET_CLASS(rb_cObject,
rb_cClass);
1565 if (!super) super = rb_cObject;
1567 rb_make_metaclass(klass, METACLASS_OF(super));
1585 if (!super) super = rb_cObject;
1587 return rb_funcall(super, inherited, 1, klass);
1594 ID id = rb_intern(name);
1603 rb_raise(
rb_eTypeError,
"superclass mismatch for class %s", name);
1607 rb_vm_register_global_object(klass);
1611 rb_raise(rb_eArgError,
"no super class for '%s'", name);
1614 rb_vm_register_global_object(klass);
1628rb_define_class_id_under_no_pin(
VALUE outer,
ID id,
VALUE super)
1635 rb_raise(
rb_eTypeError,
"%"PRIsVALUE
"::%"PRIsVALUE
" is not a class"
1641 "%"PRIsVALUE
"::%"PRIsVALUE
""
1642 " (%"PRIsVALUE
" is given but was %"PRIsVALUE
")",
1649 rb_raise(rb_eArgError,
"no super class for '%"PRIsVALUE
"::%"PRIsVALUE
"'",
1663 VALUE klass = rb_define_class_id_under_no_pin(outer,
id, super);
1664 rb_vm_register_global_object(klass);
1669rb_module_s_alloc(
VALUE klass)
1672 class_initialize_method_table(mod);
1677module_new(
VALUE klass)
1680 class_initialize_method_table(mdl);
1707 ID id = rb_intern(name);
1710 module = rb_const_get(rb_cObject, id);
1712 rb_raise(
rb_eTypeError,
"%s is not a module (%"PRIsVALUE
")",
1716 rb_vm_register_global_object(module);
1719 module = rb_module_new();
1720 rb_vm_register_global_object(module);
1738 module = rb_const_get_at(outer, id);
1740 rb_raise(
rb_eTypeError,
"%"PRIsVALUE
"::%"PRIsVALUE
" is not a module"
1745 rb_vm_register_global_object(module);
1748 module = rb_module_new();
1751 rb_vm_register_global_object(module);
1757rb_include_class_new(
VALUE module,
VALUE super)
1761 RCLASS_SET_M_TBL(klass, RCLASS_WRITABLE_M_TBL(module));
1763 RCLASS_SET_ORIGIN(klass, klass);
1765 module = METACLASS_OF(module);
1768 if (RCLASS_WRITABLE_CONST_TBL(module)) {
1769 RCLASS_SET_CONST_TBL(klass, RCLASS_WRITABLE_CONST_TBL(module),
true);
1772 RCLASS_WRITE_CONST_TBL(module, rb_id_table_create(0),
false);
1773 RCLASS_SET_CONST_TBL(klass, RCLASS_WRITABLE_CONST_TBL(module),
true);
1776 RCLASS_SET_CVC_TBL(klass, RCLASS_WRITABLE_CVC_TBL(module));
1778 class_associate_super(klass, super,
true);
1779 RBASIC_SET_CLASS(klass, module);
1781 return (
VALUE)klass;
1784static int include_modules_at(
const VALUE klass,
VALUE c,
VALUE module,
int search_super);
1791 rb_class_set_initialized(module);
1792 if (!
NIL_P(rb_refinement_module_get_refined_class(module))) {
1793 rb_raise(rb_eArgError,
"refinement module is not allowed");
1802 ensure_includable(klass, module);
1804 changed = include_modules_at(klass, RCLASS_ORIGIN(klass), module, TRUE);
1806 rb_raise(rb_eArgError,
"cyclic include detected");
1812 VALUE check_class = iclass->klass;
1815 if (!rb_objspace_garbage_object_p(check_class)) {
1816 while (check_class) {
1817 RUBY_ASSERT(!rb_objspace_garbage_object_p(check_class));
1820 (METACLASS_OF(check_class) == module)) {
1827 include_modules_at(iclass->klass, RCLASS_ORIGIN(iclass->klass), module, TRUE);
1831 iclass = iclass->next;
1836static enum rb_id_table_iterator_result
1837add_refined_method_entry_i(
ID key,
VALUE value,
void *data)
1839 rb_add_refined_method_entry((
VALUE)data, key);
1840 return ID_TABLE_CONTINUE;
1843static enum rb_id_table_iterator_result
1844clear_module_cache_i(
ID id,
VALUE val,
void *data)
1847 rb_clear_method_cache(klass,
id);
1848 return ID_TABLE_CONTINUE;
1852module_in_super_chain(
const VALUE klass,
VALUE module)
1854 struct rb_id_table *
const klass_m_tbl = RCLASS_M_TBL(RCLASS_ORIGIN(klass));
1857 if (klass_m_tbl == RCLASS_M_TBL(module))
1859 module = RCLASS_SUPER(module);
1867static enum rb_id_table_iterator_result
1868clear_constant_cache_i(
ID id,
VALUE value,
void *data)
1871 return ID_TABLE_CONTINUE;
1875do_include_modules_at(
const VALUE klass,
VALUE c,
VALUE module,
int search_super,
bool check_cyclic)
1877 VALUE p, iclass, origin_stack = 0;
1878 int method_changed = 0;
1880 VALUE klass_origin = RCLASS_ORIGIN(klass);
1881 VALUE original_klass = klass;
1883 if (check_cyclic && module_in_super_chain(klass, module))
1888 int superclass_seen = FALSE;
1894 if (klass_origin != c || search_super) {
1900 if (klass_origin == p && !search_super)
1905 if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) {
1906 if (!superclass_seen && c_seen) {
1913 superclass_seen = TRUE;
1921 RB_DEBUG_COUNTER_INC(cvar_include_invalidate);
1922 ruby_vm_global_cvar_state++;
1923 tbl = RCLASS_M_TBL(module);
1924 if (tbl && rb_id_table_size(tbl)) {
1927 rb_id_table_foreach(tbl, clear_module_cache_i, (
void *)super_class);
1932 rb_id_table_foreach(tbl, clear_module_cache_i, (
void *)original_klass);
1939 iclass = rb_include_class_new(module, super_class);
1940 c = rb_class_set_super(c, iclass);
1941 RCLASS_SET_INCLUDER(iclass, klass);
1942 if (module != RCLASS_ORIGIN(module)) {
1944 VALUE origin[2] = {iclass, RCLASS_ORIGIN(module)};
1947 else if (origin_stack && (origin_len =
RARRAY_LEN(origin_stack)) > 1 &&
1948 RARRAY_AREF(origin_stack, origin_len - 1) == module) {
1949 RCLASS_WRITE_ORIGIN(
RARRAY_AREF(origin_stack, (origin_len -= 2)), iclass);
1950 RICLASS_WRITE_ORIGIN_SHARED_MTBL(iclass);
1956 rb_module_add_to_subclasses_list(m, iclass);
1959 VALUE refined_class =
1960 rb_refinement_module_get_refined_class(klass);
1962 rb_id_table_foreach(RCLASS_M_TBL(module), add_refined_method_entry_i, (
void *)refined_class);
1966 tbl = RCLASS_CONST_TBL(module);
1967 if (tbl && rb_id_table_size(tbl))
1968 rb_id_table_foreach(tbl, clear_constant_cache_i, NULL);
1970 module = RCLASS_SUPER(module);
1973 return method_changed;
1977include_modules_at(
const VALUE klass,
VALUE c,
VALUE module,
int search_super)
1979 return do_include_modules_at(klass, c, module, search_super,
true);
1982static enum rb_id_table_iterator_result
1983move_refined_method(
ID key,
VALUE value,
void *data)
1987 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1989 struct rb_id_table *tbl = RCLASS_WRITABLE_M_TBL(klass);
1991 if (me->def->body.refined.orig_me) {
1993 RB_OBJ_WRITE(me, &me->def->body.refined.orig_me, NULL);
1994 new_me = rb_method_entry_clone(me);
1995 rb_method_table_insert(klass, tbl, key, new_me);
1996 rb_method_entry_copy(me, orig_me);
1997 return ID_TABLE_CONTINUE;
2000 rb_method_table_insert(klass, tbl, key, me);
2001 return ID_TABLE_DELETE;
2005 return ID_TABLE_CONTINUE;
2009static enum rb_id_table_iterator_result
2010cache_clear_refined_method(
ID key,
VALUE value,
void *data)
2014 if (me->def->type == VM_METHOD_TYPE_REFINED && me->def->body.refined.orig_me) {
2016 rb_clear_method_cache(klass, me->called_id);
2021 return ID_TABLE_CONTINUE;
2025ensure_origin(
VALUE klass)
2027 VALUE origin = RCLASS_ORIGIN(klass);
2028 if (origin == klass) {
2029 origin = class_alloc(
T_ICLASS, klass);
2030 RCLASS_SET_M_TBL(origin, RCLASS_M_TBL(klass));
2032 rb_class_set_super(klass, origin);
2033 RCLASS_WRITE_ORIGIN(klass, origin);
2037 rb_gc_writebarrier_remember(origin);
2039 class_clear_method_table(klass);
2040 rb_id_table_foreach(RCLASS_M_TBL(origin), cache_clear_refined_method, (
void *)klass);
2041 rb_id_table_foreach(RCLASS_M_TBL(origin), move_refined_method, (
void *)klass);
2051 bool klass_had_no_origin;
2053 ensure_includable(klass, module);
2054 if (module_in_super_chain(klass, module))
2055 rb_raise(rb_eArgError,
"cyclic prepend detected");
2057 klass_had_no_origin = ensure_origin(klass);
2058 changed = do_include_modules_at(klass, klass, module, FALSE,
false);
2061 rb_vm_check_redefinition_by_prepend(klass);
2065 VALUE klass_origin = RCLASS_ORIGIN(klass);
2066 struct rb_id_table *klass_m_tbl = RCLASS_M_TBL(klass);
2067 struct rb_id_table *klass_origin_m_tbl = RCLASS_M_TBL(klass_origin);
2071 if (!rb_objspace_garbage_object_p(iclass->klass)) {
2072 const VALUE subclass = iclass->klass;
2073 if (klass_had_no_origin && klass_origin_m_tbl == RCLASS_M_TBL(subclass)) {
2075 rb_id_table_foreach(RCLASS_M_TBL(subclass), clear_module_cache_i, (
void *)subclass);
2076 RCLASS_WRITE_M_TBL(subclass, klass_m_tbl);
2078 rb_class_set_super(subclass, origin);
2079 RCLASS_SET_INCLUDER(origin, RCLASS_INCLUDER(subclass));
2080 RCLASS_WRITE_ORIGIN(subclass, origin);
2081 RICLASS_SET_ORIGIN_SHARED_MTBL(origin);
2083 include_modules_at(subclass, subclass, module, FALSE);
2086 iclass = iclass->next;
2118 VALUE origin = RCLASS_ORIGIN(mod);
2122 VALUE m = METACLASS_OF(p);
2157 if (METACLASS_OF(p) == mod2)
return Qtrue;
2187 refined_class = rb_refinement_module_get_refined_class(mod);
2191 if (p == refined_class)
break;
2192 if (p != RCLASS_ORIGIN(p))
continue;
2208 bool immediate_only;
2212class_descendants_recursive(
VALUE klass,
VALUE v)
2217 if (data->buffer && data->count < data->maxcount && !rb_objspace_garbage_object_p(klass)) {
2222 if (!data->immediate_only) {
2223 rb_class_foreach_subclass(klass, class_descendants_recursive, v);
2227 rb_class_foreach_subclass(klass, class_descendants_recursive, v);
2232class_descendants(
VALUE klass,
bool immediate_only)
2237 rb_class_foreach_subclass(klass, class_descendants_recursive, (
VALUE) &data);
2241 data.maxcount = data.count;
2244 size_t gc_count = rb_gc_count();
2247 rb_class_foreach_subclass(klass, class_descendants_recursive, (
VALUE) &data);
2249 if (gc_count != rb_gc_count()) {
2250 rb_bug(
"GC must not occur during the subclass iteration of Class#descendants");
2298 return class_descendants(klass,
true);
2321 if (!RCLASS_SINGLETON_P(klass)) {
2322 rb_raise(
rb_eTypeError,
"'%"PRIsVALUE
"' is not a singleton class", klass);
2325 return RCLASS_ATTACHED_OBJECT(klass);
2329ins_methods_push(st_data_t name, st_data_t ary)
2335ins_methods_i(st_data_t name, st_data_t
type, st_data_t ary)
2337 switch ((rb_method_visibility_t)
type) {
2338 case METHOD_VISI_UNDEF:
2339 case METHOD_VISI_PRIVATE:
2342 ins_methods_push(name, ary);
2349ins_methods_type_i(st_data_t name, st_data_t
type, st_data_t ary, rb_method_visibility_t visi)
2351 if ((rb_method_visibility_t)
type == visi) {
2352 ins_methods_push(name, ary);
2358ins_methods_prot_i(st_data_t name, st_data_t
type, st_data_t ary)
2360 return ins_methods_type_i(name,
type, ary, METHOD_VISI_PROTECTED);
2364ins_methods_priv_i(st_data_t name, st_data_t
type, st_data_t ary)
2366 return ins_methods_type_i(name,
type, ary, METHOD_VISI_PRIVATE);
2370ins_methods_pub_i(st_data_t name, st_data_t
type, st_data_t ary)
2372 return ins_methods_type_i(name,
type, ary, METHOD_VISI_PUBLIC);
2376ins_methods_undef_i(st_data_t name, st_data_t
type, st_data_t ary)
2378 return ins_methods_type_i(name,
type, ary, METHOD_VISI_UNDEF);
2386static enum rb_id_table_iterator_result
2387method_entry_i(
ID key,
VALUE value,
void *data)
2391 rb_method_visibility_t
type;
2393 if (me->def->type == VM_METHOD_TYPE_REFINED) {
2394 VALUE owner = me->owner;
2395 me = rb_resolve_refined_method(
Qnil, me);
2396 if (!me)
return ID_TABLE_CONTINUE;
2397 if (!arg->recur && me->owner != owner)
return ID_TABLE_CONTINUE;
2399 if (!st_is_member(arg->list, key)) {
2400 if (UNDEFINED_METHOD_ENTRY_P(me)) {
2401 type = METHOD_VISI_UNDEF;
2404 type = METHOD_ENTRY_VISI(me);
2407 st_add_direct(arg->list, key, (st_data_t)
type);
2409 return ID_TABLE_CONTINUE;
2417 rb_id_table_foreach(m_tbl, method_entry_i, me_arg);
2421particular_class_p(
VALUE mod)
2423 if (!mod)
return false;
2424 if (RCLASS_SINGLETON_P(mod))
return true;
2430class_instance_method_list(
int argc,
const VALUE *argv,
VALUE mod,
int obj,
int (*func) (st_data_t, st_data_t, st_data_t))
2433 int recur = TRUE, prepended = 0;
2438 me_arg.list = st_init_numtable();
2439 me_arg.recur = recur;
2442 for (; particular_class_p(mod); mod =
RCLASS_SUPER(mod)) {
2443 add_instance_method_list(mod, &me_arg);
2447 if (!recur && RCLASS_ORIGIN(mod) != mod) {
2448 mod = RCLASS_ORIGIN(mod);
2453 add_instance_method_list(mod, &me_arg);
2458 st_foreach(me_arg.list, func, ary);
2459 st_free_table(me_arg.list);
2503 return class_instance_method_list(argc, argv, mod, 0, ins_methods_i);
2518 return class_instance_method_list(argc, argv, mod, 0, ins_methods_prot_i);
2541 return class_instance_method_list(argc, argv, mod, 0, ins_methods_priv_i);
2556 return class_instance_method_list(argc, argv, mod, 0, ins_methods_pub_i);
2568rb_class_undefined_instance_methods(
VALUE mod)
2571 return class_instance_method_list(1, &include_super, mod, 0, ins_methods_undef_i);
2605rb_obj_methods(
int argc,
const VALUE *argv,
VALUE obj)
2608 if (argc > 0 && !
RTEST(argv[0])) {
2611 return class_instance_method_list(argc, argv,
CLASS_OF(obj), 1, ins_methods_i);
2624rb_obj_protected_methods(
int argc,
const VALUE *argv,
VALUE obj)
2626 return class_instance_method_list(argc, argv,
CLASS_OF(obj), 1, ins_methods_prot_i);
2639rb_obj_private_methods(
int argc,
const VALUE *argv,
VALUE obj)
2641 return class_instance_method_list(argc, argv,
CLASS_OF(obj), 1, ins_methods_priv_i);
2654rb_obj_public_methods(
int argc,
const VALUE *argv,
VALUE obj)
2656 return class_instance_method_list(argc, argv,
CLASS_OF(obj), 1, ins_methods_pub_i);
2695 VALUE ary, klass, origin;
2701 if (RCLASS_SINGLETON_P(obj)) {
2705 origin = RCLASS_ORIGIN(klass);
2706 me_arg.list = st_init_numtable();
2707 me_arg.recur = recur;
2708 if (klass && RCLASS_SINGLETON_P(klass)) {
2709 if ((mtbl = RCLASS_M_TBL(origin)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2714 if (klass != origin && (mtbl = RCLASS_M_TBL(klass)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2719 st_foreach(me_arg.list, ins_methods_i, ary);
2720 st_free_table(me_arg.list);
2733#ifdef rb_define_method_id
2734#undef rb_define_method_id
2739 rb_add_method_cfunc(klass, mid, func, argc, METHOD_VISI_PUBLIC);
2742#ifdef rb_define_method
2743#undef rb_define_method
2748 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PUBLIC);
2751#ifdef rb_define_protected_method
2752#undef rb_define_protected_method
2757 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PROTECTED);
2760#ifdef rb_define_private_method
2761#undef rb_define_private_method
2766 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PRIVATE);
2772 rb_add_method(klass, rb_intern(name), VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2775static enum rb_id_table_iterator_result
2776undef_method_i(
ID name,
VALUE value,
void *data)
2779 rb_add_method(klass, name, VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2780 return ID_TABLE_CONTINUE;
2784rb_undef_methods_from(
VALUE klass,
VALUE super)
2788 rb_id_table_foreach(mtbl, undef_method_i, (
void *)klass);
2801special_singleton_class_of(
VALUE obj)
2807 default:
return Qnil;
2812rb_special_singleton_class(
VALUE obj)
2814 return special_singleton_class_of(obj);
2827singleton_class_of(
VALUE obj,
bool ensure_eigenclass)
2831 switch (
TYPE(obj)) {
2841 klass = special_singleton_class_of(obj);
2843 rb_bug(
"unknown immediate %p", (
void *)obj);
2847 if (CHILLED_STRING_P(obj)) {
2848 CHILLED_STRING_MUTATED(obj);
2858 RB_VM_LOCK_ENTER_LEV(&lev);
2861 klass = METACLASS_OF(obj);
2862 if (!(RCLASS_SINGLETON_P(klass) &&
2863 RCLASS_ATTACHED_OBJECT(klass) == obj)) {
2864 klass = rb_make_metaclass(obj, klass);
2873 RB_VM_LOCK_LEAVE_LEV(&lev);
2883 if (!RCLASS_SINGLETON_P(x)) {
2906 return rb_special_singleton_class(obj);
2908 klass = METACLASS_OF(obj);
2909 if (!RCLASS_SINGLETON_P(klass))
return Qnil;
2910 if (RCLASS_ATTACHED_OBJECT(klass) != obj)
return Qnil;
2917 return singleton_class_of(obj,
true);
2929#ifdef rb_define_singleton_method
2930#undef rb_define_singleton_method
2938#ifdef rb_define_module_function
2939#undef rb_define_module_function
2948#ifdef rb_define_global_function
2949#undef rb_define_global_function
2960 rb_alias(klass, rb_intern(name1), rb_intern(name2));
2966 rb_attr(klass, rb_intern(name), read, write, FALSE);
2970rb_keyword_error_new(
const char *error,
VALUE keys)
2973 VALUE error_message = rb_sprintf(
"%s keyword%.*s", error,
len > 1,
"s");
2980 if (++i >=
len)
break;
2988NORETURN(
static void rb_keyword_error(
const char *error,
VALUE keys));
2990rb_keyword_error(
const char *error,
VALUE keys)
2995NORETURN(
static void unknown_keyword_error(
VALUE hash,
const ID *table,
int keywords));
2997unknown_keyword_error(
VALUE hash,
const ID *table,
int keywords)
3000 for (i = 0; i < keywords; i++) {
3001 st_data_t key =
ID2SYM(table[i]);
3002 rb_hash_stlike_delete(hash, &key, NULL);
3004 rb_keyword_error(
"unknown", rb_hash_keys(hash));
3009separate_symbol(st_data_t key, st_data_t value, st_data_t arg)
3013 if (!*kwdhash) *kwdhash = rb_hash_new();
3014 rb_hash_aset(*kwdhash, (
VALUE)key, (
VALUE)value);
3021 VALUE parthash[2] = {0, 0};
3022 VALUE hash = *orighash;
3029 *orighash = parthash[1];
3044#define extract_kwarg(keyword, val) \
3045 (key = (st_data_t)(keyword), values ? \
3046 (rb_hash_stlike_delete(keyword_hash, &key, &(val)) || ((val) = Qundef, 0)) : \
3047 rb_hash_stlike_lookup(keyword_hash, key, NULL))
3049 if (
NIL_P(keyword_hash)) keyword_hash = 0;
3053 optional = -1-optional;
3056 for (; i < required; i++) {
3059 if (extract_kwarg(keyword, values[i])) {
3066 if (!
NIL_P(missing)) {
3067 rb_keyword_error(
"missing", missing);
3071 if (optional && keyword_hash) {
3072 for (i = 0; i < optional; i++) {
3073 if (extract_kwarg(
ID2SYM(table[required+i]), values[required+i])) {
3078 if (!rest && keyword_hash) {
3079 if (
RHASH_SIZE(keyword_hash) > (
unsigned int)(values ? 0 : j)) {
3080 unknown_keyword_error(keyword_hash, table, required+optional);
3083 if (values && !keyword_hash) {
3084 for (i = 0; i < required + optional; i++) {
3103rb_scan_args_parse(
int kw_flag,
const char *fmt,
struct rb_scan_args_t *arg)
3105 const char *p = fmt;
3107 memset(arg, 0,
sizeof(*arg));
3108 arg->kw_flag = kw_flag;
3111 arg->n_lead = *p -
'0';
3114 arg->n_opt = *p -
'0';
3123 arg->n_trail = *p -
'0';
3135 rb_fatal(
"bad scan arg format: %s", fmt);
3140rb_scan_args_assign(
const struct rb_scan_args_t *arg,
int argc,
const VALUE *
const argv, va_list vargs)
3144#define rb_scan_args_next_param() va_arg(vargs, VALUE *)
3145 const int kw_flag = arg->kw_flag;
3146 const int n_lead = arg->n_lead;
3147 const int n_opt = arg->n_opt;
3148 const int n_trail = arg->n_trail;
3149 const int n_mand = n_lead + n_trail;
3150 const bool f_var = arg->f_var;
3151 const bool f_hash = arg->f_hash;
3152 const bool f_block = arg->f_block;
3155 if (f_hash && argc > 0) {
3156 VALUE last = argv[argc - 1];
3157 if (rb_scan_args_keyword_p(kw_flag, last)) {
3158 hash = rb_hash_dup(last);
3163 if (argc < n_mand) {
3168 for (i = 0; i < n_lead; i++) {
3169 var = rb_scan_args_next_param();
3170 if (var) *var = argv[argi];
3174 for (i = 0; i < n_opt; i++) {
3175 var = rb_scan_args_next_param();
3176 if (argi < argc - n_trail) {
3177 if (var) *var = argv[argi];
3181 if (var) *var =
Qnil;
3186 int n_var = argc - argi - n_trail;
3188 var = rb_scan_args_next_param();
3198 for (i = 0; i < n_trail; i++) {
3199 var = rb_scan_args_next_param();
3200 if (var) *var = argv[argi];
3205 var = rb_scan_args_next_param();
3206 if (var) *var = hash;
3210 var = rb_scan_args_next_param();
3225#undef rb_scan_args_next_param
3229rb_scan_args_result(
const struct rb_scan_args_t *
const arg,
int argc)
3231 const int n_lead = arg->n_lead;
3232 const int n_opt = arg->n_opt;
3233 const int n_trail = arg->n_trail;
3234 const int n_mand = n_lead + n_trail;
3235 const bool f_var = arg->f_var;
3253 va_start(vargs,fmt);
3254 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
3256 return rb_scan_args_result(&arg, argc);
3259#undef rb_scan_args_kw
3265 rb_scan_args_parse(kw_flag, fmt, &arg);
3266 va_start(vargs,fmt);
3267 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
3269 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.
static VALUE class_alloc0(enum ruby_value_type type, VALUE klass, bool boxable)
Allocates a struct RClass for a new class, iclass, or module.
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.
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 FL_SHAREABLE
Old name of RUBY_FL_SHAREABLE.
#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 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.
#define OBJ_FROZEN_RAW
Old name of RB_OBJ_FROZEN_RAW.
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.
static bool rb_ractor_shareable_p(VALUE obj)
Queries if multiple Ractors can share the passed object or not.
#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 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.
Internal header for Ruby Box.
Internal header for Class.
rb_cref_t * cref
class reference, should be marked
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
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.