17#include "ruby/internal/config.h"
21#include "debug_counter.h"
24#include "internal/box.h"
25#include "internal/class.h"
26#include "internal/error.h"
27#include "internal/eval.h"
28#include "internal/gc.h"
29#include "internal/hash.h"
30#include "internal/object.h"
31#include "internal/string.h"
32#include "internal/variable.h"
33#include "internal/vm.h"
37#include "ractor_core.h"
84#define METACLASS_OF(k) RBASIC(k)->klass
85#define SET_METACLASS_OF(k, cls) RBASIC_SET_CLASS(k, cls)
91 st_data_t key = (st_data_t)box->box_object;
92 st_delete(box->classext_cow_classes, &klass, 0);
93 st_delete(RCLASS_CLASSEXT_TBL(klass), &key, &ext);
102 rb_id_table_free(RCLASSEXT_M_TBL(ext));
104 if (!RCLASSEXT_SHARED_CONST_TBL(ext) && (tbl = RCLASSEXT_CONST_TBL(ext)) != NULL) {
105 rb_free_const_table(tbl);
108 if (RCLASSEXT_SUPERCLASSES_WITH_SELF(ext)) {
110 size_t depth = RCLASSEXT_SUPERCLASS_DEPTH(ext);
111 if (depth != RCLASS_MAX_SUPERCLASS_DEPTH) {
114 SIZED_FREE_N(RCLASSEXT_SUPERCLASSES(ext), depth);
125 if (RCLASSEXT_ICLASS_IS_ORIGIN(ext) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext)) {
127 rb_id_table_free(RCLASSEXT_M_TBL(ext));
130 if (RCLASSEXT_CALLABLE_M_TBL(ext) != NULL) {
131 rb_id_table_free(RCLASSEXT_CALLABLE_M_TBL(ext));
142 if (RCLASSEXT_ICLASS_IS_ORIGIN(ext) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext)) {
144 rb_id_table_free(RCLASSEXT_M_TBL(ext));
147 if (RCLASSEXT_CALLABLE_M_TBL(ext) != NULL) {
148 rb_id_table_free(RCLASSEXT_CALLABLE_M_TBL(ext));
160set_box_classext_update(st_data_t *key_ptr, st_data_t *val_ptr, st_data_t a,
int existing)
166 iclass_free_orphan_classext(args->obj, (
rb_classext_t *)*val_ptr);
169 rb_bug(
"Updating existing classext for non-iclass never happen");
173 *val_ptr = (st_data_t)args->ext;
186 VM_ASSERT(BOX_MUTABLE_P(box));
188 st_update(RCLASS_CLASSEXT_TBL(obj), (st_data_t)box->box_object, set_box_classext_update, (st_data_t)&args);
193 rb_gc_writebarrier_remember(obj);
195 st_insert(box->classext_cow_classes, (st_data_t)obj, 0);
205static enum rb_id_table_iterator_result
206duplicate_classext_m_tbl_i(
ID key,
VALUE value,
void *data)
210 rb_method_table_insert0(arg->klass, arg->tbl, key, me,
false);
211 return ID_TABLE_CONTINUE;
215duplicate_classext_m_tbl(
struct rb_id_table *orig,
VALUE klass,
bool init_missing)
220 return rb_id_table_create(0);
224 tbl = rb_id_table_create(rb_id_table_size(orig));
229 rb_id_table_foreach(orig, duplicate_classext_m_tbl_i, &data);
239 dst->flag = src->flag;
240 dst->line = src->line;
247static enum rb_id_table_iterator_result
248duplicate_classext_const_tbl_i(
ID key,
VALUE value,
void *data)
253 rb_id_table_insert(arg->tbl, key, (
VALUE)entry);
255 return ID_TABLE_CONTINUE;
266 dst = rb_id_table_create(rb_id_table_size(src));
272 rb_id_table_foreach(src, duplicate_classext_const_tbl_i, (
void *)&data);
283 rb_classext_t *ext = RCLASS_EXT_TABLE_LOOKUP_INTERNAL(iclass, box);
288 rb_invalidate_method_caches(RCLASSEXT_CALLABLE_M_TBL(ext), RCLASSEXT_CC_TBL(ext));
293 RCLASSEXT_BOX(ext) = box;
296 first_set = RCLASS_SET_BOX_CLASSEXT(iclass, box, ext);
298 RCLASSEXT_SUPER(ext) = RCLASSEXT_SUPER(src);
301 if (RCLASSEXT_ICLASS_IS_ORIGIN(src) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(src)) {
302 RCLASSEXT_M_TBL(ext) = duplicate_classext_m_tbl(RCLASSEXT_M_TBL(src), iclass,
true);
305 RCLASSEXT_M_TBL(ext) = RCLASSEXT_M_TBL(mod_ext);
308 RCLASSEXT_CONST_TBL(ext) = RCLASSEXT_CONST_TBL(mod_ext);
309 RCLASSEXT_CVC_TBL(ext) = RCLASSEXT_CVC_TBL(mod_ext);
317 RCLASSEXT_SET_ORIGIN(ext, iclass, RCLASSEXT_ORIGIN(src));
318 RCLASSEXT_ICLASS_IS_ORIGIN(ext) = RCLASSEXT_ICLASS_IS_ORIGIN(src);
319 RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext) = RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(src);
321 RCLASSEXT_SET_INCLUDER(ext, iclass, RCLASSEXT_INCLUDER(src));
324 RCLASS_SET_PRIME_CLASSEXT_WRITABLE(iclass,
false);
336 RCLASSEXT_BOX(ext) = box;
340 *first_set = RCLASS_SET_BOX_CLASSEXT(klass, box, ext);
342 RCLASSEXT_SUPER(ext) = RCLASSEXT_SUPER(orig);
344 RCLASSEXT_M_TBL(ext) = duplicate_classext_m_tbl(RCLASSEXT_M_TBL(orig), klass, dup_iclass);
345 RCLASSEXT_ICLASS_IS_ORIGIN(ext) =
true;
346 RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext) =
false;
348 if (orig->fields_obj) {
349 RB_OBJ_WRITE(klass, &ext->fields_obj, rb_imemo_fields_clone(orig->fields_obj));
352 if (RCLASSEXT_SHARED_CONST_TBL(orig)) {
353 RCLASSEXT_CONST_TBL(ext) = RCLASSEXT_CONST_TBL(orig);
354 RCLASSEXT_SHARED_CONST_TBL(ext) =
true;
357 RCLASSEXT_CONST_TBL(ext) = duplicate_classext_const_tbl(RCLASSEXT_CONST_TBL(orig), klass);
358 RCLASSEXT_SHARED_CONST_TBL(ext) =
false;
370 VALUE cvc_table = RCLASSEXT_CVC_TBL(orig);
372 cvc_table = rb_marked_id_table_dup(cvc_table);
374 else if (dup_iclass) {
375 cvc_table = rb_marked_id_table_new(2);
377 RB_OBJ_WRITE(klass, &RCLASSEXT_CVC_TBL(ext), cvc_table);
381 RCLASSEXT_SET_ORIGIN(ext, klass, RCLASSEXT_ORIGIN(orig));
390 RCLASSEXT_PERMANENT_CLASSPATH(ext) = RCLASSEXT_PERMANENT_CLASSPATH(orig);
391 RCLASSEXT_CLASSPATH(ext) = RCLASSEXT_CLASSPATH(orig);
403 VALUE subs_v = RCLASSEXT_SUBCLASSES(orig);
406 VALUE *entries = rb_imemo_subclasses_entries(subs_v);
407 for (uint32_t i = 0; i < subs->count; i++) {
408 VALUE iclass = entries[i];
409 if (!iclass)
continue;
419 class_duplicate_iclass_classext(iclass, ext, box);
429rb_class_ensure_writable(
VALUE klass)
432 RCLASS_EXT_WRITABLE(klass);
436 rb_class_classext_foreach_callback_func *func;
441class_classext_foreach_i(st_data_t key, st_data_t value, st_data_t arg)
444 rb_class_classext_foreach_callback_func *func =
foreach_arg->func;
450rb_class_classext_foreach(
VALUE klass, rb_class_classext_foreach_callback_func *func,
void *arg)
452 st_table *tbl = RCLASS_CLASSEXT_TBL(klass);
457 rb_st_foreach(tbl, class_classext_foreach_i, (st_data_t)&
foreach_arg);
459 func(RCLASS_EXT_PRIME(klass),
true, (
VALUE)NULL, arg);
469rb_class_singleton_p(
VALUE klass)
471 return RCLASS_SINGLETON_P(klass);
475rb_class_variation_count(
VALUE klass)
477 return RCLASS_VARIATION_COUNT(klass);
481push_subclass_entry_to_list(
VALUE super,
VALUE klass)
490 VALUE subs_v = RCLASS_SUBCLASSES(super);
493 if (!subs || subs->count == subs->capacity) {
494 VALUE *old_entries = subs ? rb_imemo_subclasses_entries(subs_v) : NULL;
496 for (uint32_t i = 0; subs && i < subs->count; i++) {
497 if (old_entries[i]) live++;
500 uint32_t cap = subs ? subs->capacity : 2;
501 if (live * 2 >= cap) cap *= 2;
503 VALUE new_v = rb_imemo_subclasses_new(cap);
505 VALUE *new_entries = rb_imemo_subclasses_entries(new_v);
506 for (uint32_t i = 0; subs && i < subs->count; i++) {
507 VALUE entry = old_entries[i];
509 new_entries[new_subs->count++] = entry;
513 RCLASS_SET_SUBCLASSES(super, new_v);
518 rb_imemo_subclasses_entries(subs_v)[subs->count++] = klass;
524rb_class_subclass_add(
VALUE super,
VALUE klass)
526 if (super && !UNDEF_P(super)) {
529 push_subclass_entry_to_list(super, klass);
534rb_module_add_to_subclasses_list(
VALUE module,
VALUE iclass)
536 if (module && !UNDEF_P(module)) {
539 push_subclass_entry_to_list(module, iclass);
546 VALUE subs_v = RCLASS_SUBCLASSES(klass);
550 VALUE *entries = rb_imemo_subclasses_entries(subs_v);
551 for (uint32_t i = 0; i < subs->count; i++) {
552 VALUE curklass = entries[i];
560class_switch_superclass(
VALUE super,
VALUE klass)
564 rb_class_subclass_add(super, klass);
580 const rb_box_t *box = rb_current_box();
582 if (!ruby_box_init_done) {
594 if (boxable) flags |= RCLASS_BOXABLE;
596 shape_id_t shape_id = ROOT_SHAPE_ID;
598 shape_id |= SHAPE_ID_LAYOUT_OTHER;
601 shape_id |= SHAPE_ID_LAYOUT_RCLASS;
604 struct RClass *obj = (
struct RClass *)rb_newobj(GET_EC(), klass, flags, shape_id,
true, alloc_size);
612 if (
type !=
T_ICLASS && UNLIKELY(!rb_ractor_main_p())) {
613 RCLASS_SET_OWNER_RACTOR_ID((
VALUE)obj, rb_ractor_id(GET_RACTOR()));
627 RCLASS_PRIME_BOX((
VALUE)obj) = box;
630 RCLASS_SET_PRIME_CLASSEXT_WRITABLE((
VALUE)obj, !boxable || BOX_USER_P(box));
633 RCLASS_SET_REFINED_CLASS((
VALUE)obj,
Qnil);
641 bool boxable = rb_box_available() && BOX_MASTER_P(rb_current_box());
646rb_class_owned_by_ractor_p(rb_serial_t owner_id)
648 return owner_id == rb_ractor_id(GET_RACTOR());
655class_owner_name(
VALUE klass)
659 VALUE obj = RCLASS_ATTACHED_OBJECT(klass);
665class_own_metaclass(
VALUE klass)
667 VALUE meta = METACLASS_OF(klass);
668 return (RCLASS_SINGLETON_P(meta) && RCLASS_ATTACHED_OBJECT(meta) == klass) ? meta : 0;
674rb_class_take_ownership(
VALUE klass)
677 rb_serial_t
id = rb_ractor_main_p() ? 0 : rb_ractor_id(GET_RACTOR());
681 RCLASS_SET_OWNER_RACTOR_ID(klass,
id);
682 }
while ((klass = class_own_metaclass(klass)) != 0);
686rb_class_owner_check(
VALUE klass)
688 if (UNLIKELY(!rb_class_owned_p(klass))) {
689 rb_raise(rb_eRactorIsolationError,
690 "can not modify %"PRIsVALUE
" because it is created by another Ractor",
691 class_owner_name(klass));
696class_associate_super(
VALUE klass,
VALUE super,
bool init)
698 if (super && !UNDEF_P(super)) {
703 if (RCLASS_SINGLETON_P(klass)) {
710 class_switch_superclass(super, klass);
715 RCLASS_SET_SUPER(klass, super);
718 RCLASS_WRITE_SUPER(klass, super);
720 rb_class_update_superclasses(klass);
727 return class_associate_super(klass, super,
false);
731class_initialize_method_table(
VALUE c)
734 RCLASS_SET_M_TBL(c, rb_id_table_create(0));
738class_clear_method_table(
VALUE c)
740 RCLASS_WRITE_M_TBL(c, rb_id_table_create(0));
744class_boot_boxable(
VALUE super,
bool boxable)
750 class_initialize_method_table(klass);
752 class_associate_super(klass, super,
true);
753 if (super && !UNDEF_P(super)) {
754 RCLASS_SET_ALLOCATOR(klass, RCLASS_ALLOCATOR(super));
755 rb_class_set_initialized(klass);
773 return class_boot_boxable(super,
false);
777class_superclasses_including_self(
VALUE klass)
779 if (RCLASS_SUPERCLASSES_WITH_SELF_P(klass))
780 return RCLASS_SUPERCLASSES(klass);
782 size_t depth = RCLASS_SUPERCLASS_DEPTH(klass);
785 memcpy(superclasses, RCLASS_SUPERCLASSES(klass),
sizeof(
VALUE) * depth);
786 superclasses[depth] = klass;
792rb_class_update_superclasses(
VALUE klass)
799 if (UNDEF_P(super))
return;
802 if (RCLASS_SUPERCLASSES(klass))
817 rb_class_update_superclasses(super);
820 if (!RCLASS_SUPERCLASS_DEPTH(super))
824 super_depth = RCLASS_SUPERCLASS_DEPTH(super);
825 if (RCLASS_SUPERCLASSES_WITH_SELF_P(super)) {
826 superclasses = RCLASS_SUPERCLASSES(super);
829 superclasses = class_superclasses_including_self(super);
830 RCLASS_WRITE_SUPERCLASSES(super, super_depth, superclasses,
true);
833 size_t depth = super_depth == RCLASS_MAX_SUPERCLASS_DEPTH ? super_depth : super_depth + 1;
834 RCLASS_WRITE_SUPERCLASSES(klass, depth, superclasses,
false);
841 rb_raise(
rb_eTypeError,
"superclass must be an instance of Class (given an instance of %"PRIsVALUE
")",
844 if (RCLASS_SINGLETON_P(super)) {
845 rb_raise(
rb_eTypeError,
"can't make subclass of singleton class");
859 RCLASS_SET_MAX_IV_COUNT(klass, RCLASS_MAX_IV_COUNT(super));
860 RUBY_ASSERT(getenv(
"RUBY_BOX") || RCLASS_PRIME_CLASSEXT_WRITABLE_P(klass));
866rb_class_s_alloc(
VALUE klass)
874 rb_method_entry_set(new_klass, mid, me, METHOD_ENTRY_VISI(me));
881static enum rb_id_table_iterator_result
882clone_method_i(
ID key,
VALUE value,
void *data)
886 return ID_TABLE_CONTINUE;
902 rb_id_table_insert(arg->tbl, key, (
VALUE)nce);
903 return ID_TABLE_CONTINUE;
906static enum rb_id_table_iterator_result
907clone_const_i(
ID key,
VALUE value,
void *data)
918 if (RCLASS_INITIALIZED_P(clone)) {
921 if (RCLASS_SINGLETON_P(orig)) {
932cvc_table_entry_alloc(
void)
937static enum rb_id_table_iterator_result
938cvc_table_copy(
ID id,
VALUE val,
void *data)
946 ent = cvc_table_entry_alloc();
949 ent->global_cvar_state = orig_entry->global_cvar_state;
950 rb_marked_id_table_insert(ctx->new_table,
id, (
VALUE)ent);
952 return ID_TABLE_CONTINUE;
958 if (RCLASS_CONST_TBL(clone)) {
959 rb_free_const_table(RCLASS_CONST_TBL(clone));
960 RCLASS_WRITE_CONST_TBL(clone, 0,
false);
962 if (RCLASS_CVC_TBL(orig)) {
963 VALUE rb_cvc_tbl = RCLASS_CVC_TBL(orig);
964 VALUE rb_cvc_tbl_dup = rb_marked_id_table_new(rb_marked_id_table_size(rb_cvc_tbl));
968 ctx.new_table = rb_cvc_tbl_dup;
969 rb_marked_id_table_foreach(rb_cvc_tbl, cvc_table_copy, &ctx);
970 RCLASS_WRITE_CVC_TBL(clone, rb_cvc_tbl_dup);
972 rb_id_table_free(RCLASS_M_TBL(clone));
973 RCLASS_WRITE_M_TBL(clone, 0);
975 rb_fields_tbl_copy(clone, orig);
977 if (RCLASS_CONST_TBL(orig)) {
980 struct rb_id_table *orig_tbl = RCLASS_CONST_TBL(orig);
981 arg.tbl = const_tbl = rb_id_table_create(rb_id_table_size(orig_tbl));
983 rb_id_table_foreach(orig_tbl, clone_const_i, &arg);
984 RCLASS_WRITE_CONST_TBL(clone, const_tbl,
false);
985 rb_gc_writebarrier_remember(clone);
989static bool ensure_origin(
VALUE klass);
992rb_class_set_initialized(
VALUE klass)
1000rb_module_check_initializable(
VALUE mod)
1002 if (RCLASS_INITIALIZED_P(mod)) {
1007static enum rb_id_table_iterator_result
1008init_copy_check_const_i(
ID id,
VALUE v,
void *data)
1012 rb_raise(rb_eRactorIsolationError,
1013 "can not copy a class/module created by another Ractor because "
1014 "constant %"PRIsVALUE
" refers to an unshareable object", rb_id2str(
id));
1016 return ID_TABLE_CONTINUE;
1020init_copy_check_field_i(
ID id,
VALUE val, st_data_t arg)
1023 rb_raise(rb_eRactorIsolationError,
1024 "can not copy a class/module created by another Ractor because "
1025 "variable %"PRIsVALUE
" refers to an unshareable object", rb_id2str(
id));
1032static enum rb_id_table_iterator_result
1033move_check_const_i(
ID id,
VALUE v,
void *data)
1037 rb_raise(rb_eRactorIsolationError,
1038 "can not move an object whose singleton class has constant %"PRIsVALUE
1039 " referring to an unshareable object", rb_id2str(
id));
1041 return ID_TABLE_CONTINUE;
1045move_check_field_i(
ID id,
VALUE val, st_data_t arg)
1048 rb_raise(rb_eRactorIsolationError,
1049 "can not move an object whose singleton class has variable %"PRIsVALUE
1050 " referring to an unshareable object", rb_id2str(
id));
1058rb_class_check_singleton_movable(
VALUE klass)
1061 if (RCLASS_CONST_TBL(klass)) {
1062 rb_id_table_foreach(RCLASS_CONST_TBL(klass), move_check_const_i, NULL);
1064 rb_ivar_foreach_buffered(klass, move_check_field_i, 0);
1065 }
while ((klass = class_own_metaclass(klass)) != 0);
1069init_copy_check_tables(
VALUE klass)
1071 if (RCLASS_CONST_TBL(klass)) {
1072 rb_id_table_foreach(RCLASS_CONST_TBL(klass), init_copy_check_const_i, NULL);
1074 rb_ivar_foreach_buffered(klass, init_copy_check_field_i, 0);
1078init_copy_owner_check(
VALUE orig)
1080 if (!rb_class_owned_p(orig)) {
1081 init_copy_check_tables(orig);
1084 VALUE meta = METACLASS_OF(orig);
1085 if (RCLASS_SINGLETON_P(meta)) {
1086 init_copy_check_tables(meta);
1100 class_init_copy_check(clone, orig);
1103 rb_module_check_initializable(clone);
1113 init_copy_owner_check(orig);
1115 rb_class_set_initialized(clone);
1117 if (!RCLASS_SINGLETON_P(
CLASS_OF(clone))) {
1122 RCLASS_SET_ALLOCATOR(clone, RCLASS_ALLOCATOR(orig));
1124 copy_tables(clone, orig);
1125 if (RCLASS_M_TBL(orig)) {
1127 arg.new_klass = clone;
1128 class_initialize_method_table(clone);
1129 rb_id_table_foreach(RCLASS_M_TBL(orig), clone_method_i, &arg);
1132 if (RCLASS_ORIGIN(orig) == orig) {
1137 VALUE orig_origin = RCLASS_ORIGIN(orig);
1138 VALUE prev_clone_p = clone;
1146 ensure_origin(clone);
1147 clone_origin = RCLASS_ORIGIN(clone);
1149 while (p && p != orig_origin) {
1151 rb_bug(
"non iclass between module/class and origin");
1153 clone_p = class_alloc(
T_ICLASS, METACLASS_OF(p));
1154 RCLASS_SET_M_TBL(clone_p, RCLASS_M_TBL(p));
1155 rb_class_set_super(prev_clone_p, clone_p);
1156 prev_clone_p = clone_p;
1157 RCLASS_SET_CONST_TBL(clone_p, RCLASS_CONST_TBL(p),
false);
1158 RCLASS_SET_INCLUDER(clone_p, clone);
1159 add_subclass = TRUE;
1160 if (p != RCLASS_ORIGIN(p)) {
1161 origin[0] = clone_p;
1162 origin[1] = RCLASS_ORIGIN(p);
1165 else if ((origin_len =
RARRAY_LEN(origin_stack)) > 1 &&
1167 RCLASS_WRITE_ORIGIN(
RARRAY_AREF(origin_stack, (origin_len -= 2)), clone_p);
1168 RICLASS_WRITE_ORIGIN_SHARED_MTBL(clone_p);
1170 add_subclass = FALSE;
1173 rb_module_add_to_subclasses_list(METACLASS_OF(p), clone_p);
1178 if (p == orig_origin) {
1180 rb_class_set_super(clone_p, clone_origin);
1181 rb_class_set_super(clone_origin,
RCLASS_SUPER(orig_origin));
1183 copy_tables(clone_origin, orig_origin);
1184 if (RCLASS_M_TBL(orig_origin)) {
1186 arg.new_klass = clone;
1187 class_initialize_method_table(clone_origin);
1188 rb_id_table_foreach(RCLASS_M_TBL(orig_origin), clone_method_i, &arg);
1192 rb_bug(
"no origin for class that has origin");
1195 rb_class_update_superclasses(clone);
1211 return rb_singleton_class_clone_and_attach(obj,
Qundef);
1216rb_singleton_class_clone_and_attach(
VALUE obj,
VALUE attach)
1218 const VALUE klass = METACLASS_OF(obj);
1224 if (!(RCLASS_SINGLETON_P(klass) && RCLASS_ATTACHED_OBJECT(klass) == obj)) {
1230 bool klass_of_clone_is_new;
1235 klass_of_clone_is_new =
true;
1236 RBASIC_SET_CLASS(clone, clone);
1242 klass_of_clone_is_new = (METACLASS_OF(klass) != klass_metaclass_clone);
1243 RBASIC_SET_CLASS(clone, klass_metaclass_clone);
1247 class_initialize_method_table(clone);
1250 rb_fields_tbl_copy(clone, klass);
1251 if (RCLASS_CONST_TBL(klass)) {
1254 arg.tbl = table = rb_id_table_create(rb_id_table_size(RCLASS_CONST_TBL(klass)));
1256 rb_id_table_foreach(RCLASS_CONST_TBL(klass), clone_const_i, &arg);
1257 RCLASS_SET_CONST_TBL(clone, table,
false);
1259 if (!UNDEF_P(attach)) {
1264 arg.new_klass = clone;
1265 rb_id_table_foreach(RCLASS_M_TBL(klass), clone_method_i, &arg);
1267 if (klass_of_clone_is_new) {
1279 if (RCLASS_SINGLETON_P(klass)) {
1280 RCLASS_SET_ATTACHED_OBJECT(klass, obj);
1289#define META_CLASS_OF_CLASS_CLASS_P(k) (METACLASS_OF(k) == (k))
1292rb_singleton_class_has_metaclass_p(
VALUE sklass)
1294 return RCLASS_ATTACHED_OBJECT(METACLASS_OF(sklass)) == sklass;
1298rb_singleton_class_internal_p(
VALUE sklass)
1301 !rb_singleton_class_has_metaclass_p(sklass));
1309#define HAVE_METACLASS_P(k) \
1310 (FL_TEST(METACLASS_OF(k), FL_SINGLETON) && \
1311 rb_singleton_class_has_metaclass_p(k))
1320#define ENSURE_EIGENCLASS(klass) \
1321 (HAVE_METACLASS_P(klass) ? METACLASS_OF(klass) : make_metaclass(klass))
1341 RCLASS_SET_OWNER_RACTOR_ID(metaclass, RCLASS_OWNER_RACTOR_ID(klass));
1345 SET_METACLASS_OF(klass, metaclass);
1346 SET_METACLASS_OF(metaclass, metaclass);
1349 VALUE tmp = METACLASS_OF(klass);
1350 SET_METACLASS_OF(klass, metaclass);
1357 rb_class_set_initialized(klass);
1360 rb_class_update_superclasses(METACLASS_OF(metaclass));
1374 VALUE orig_class = METACLASS_OF(obj);
1379 RCLASS_SET_OWNER_RACTOR_ID(klass, RCLASS_OWNER_RACTOR_ID(obj));
1384 RCLASS_SET_OWNER_RACTOR_ID(klass, 0);
1386 class_initialize_method_table(klass);
1387 class_associate_super(klass, orig_class,
true);
1388 if (orig_class && !UNDEF_P(orig_class)) {
1389 rb_class_set_initialized(klass);
1392 RBASIC_SET_CLASS(obj, klass);
1394 rb_yjit_invalidate_no_singleton_class(orig_class);
1395 rb_zjit_invalidate_no_singleton_class(orig_class);
1397 SET_METACLASS_OF(klass, METACLASS_OF(
rb_class_real(orig_class)));
1403boot_defclass(
const char *name,
VALUE super)
1406 ID id = rb_intern(name);
1409 rb_vm_register_global_object(obj);
1460refinement_import_methods(
int argc,
VALUE *argv,
VALUE refinement)
1485Init_class_hierarchy(
void)
1546 rb_make_metaclass(klass, METACLASS_OF(super));
1566 return rb_funcallv_uncached(super, inherited, 1, &klass);
1569#ifdef rb_define_class
1570#undef rb_define_class
1573rb_define_class(
const char *name,
VALUE super)
1575 return rb_define_class_under(
rb_cObject, name, super);
1578#ifdef rb_define_class_under
1579#undef rb_define_class_under
1588rb_define_class_id_under_no_pin(
VALUE outer,
ID id,
VALUE super)
1600 rb_raise(
rb_eTypeError,
"%"PRIsVALUE
"::%"PRIsVALUE
" is not a class"
1607 rb_raise(
rb_eTypeError,
"superclass mismatch for class %s", rb_id2name(
id));
1611 "%"PRIsVALUE
"::%"PRIsVALUE
""
1612 " (%"PRIsVALUE
" is given but was %"PRIsVALUE
")",
1621 rb_raise(rb_eArgError,
"no super class for '%"PRIsVALUE
"'", rb_id2str(
id));
1624 rb_raise(rb_eArgError,
"no super class for '%"PRIsVALUE
"::%"PRIsVALUE
"'",
1639 VALUE klass = rb_define_class_id_under_no_pin(outer,
id, super);
1640 rb_vm_register_global_object(klass);
1645rb_module_s_alloc(
VALUE klass)
1648 class_initialize_method_table(mod);
1653module_new(
VALUE klass)
1656 class_initialize_method_table(mdl);
1679#ifdef rb_define_module
1680#undef rb_define_module
1683rb_define_module(
const char *name)
1688#ifdef rb_define_module_under
1689#undef rb_define_module_under
1692rb_define_module_under(
VALUE outer,
const char *name)
1703 module = rb_const_get_at(outer, id);
1706 rb_raise(
rb_eTypeError,
"%s is not a module (%"PRIsVALUE
")",
1710 rb_raise(
rb_eTypeError,
"%"PRIsVALUE
"::%"PRIsVALUE
" is not a module"
1716 rb_vm_register_global_object(module);
1719 module = rb_module_new();
1722 rb_vm_register_global_object(module);
1728rb_include_class_new(
VALUE module,
VALUE super)
1732 RCLASS_SET_M_TBL(klass, RCLASS_WRITABLE_M_TBL(module));
1734 RCLASS_SET_ORIGIN(klass, klass);
1736 module = METACLASS_OF(module);
1739 if (RCLASS_WRITABLE_CONST_TBL(module)) {
1740 RCLASS_SET_CONST_TBL(klass, RCLASS_WRITABLE_CONST_TBL(module),
true);
1743 RCLASS_WRITE_CONST_TBL(module, rb_id_table_create(0),
false);
1744 RCLASS_SET_CONST_TBL(klass, RCLASS_WRITABLE_CONST_TBL(module),
true);
1747 RCLASS_SET_CVC_TBL(klass, RCLASS_WRITABLE_CVC_TBL(module));
1749 class_associate_super(klass, super,
true);
1750 RBASIC_SET_CLASS(klass, module);
1752 return (
VALUE)klass;
1755static int include_modules_at(
const VALUE klass,
VALUE c,
VALUE module,
int search_super);
1762 rb_class_set_initialized(module);
1763 if (!
NIL_P(rb_refinement_module_get_refined_class(module))) {
1764 rb_raise(rb_eArgError,
"refinement module is not allowed");
1773 ensure_includable(klass, module);
1775 changed = include_modules_at(klass, RCLASS_ORIGIN(klass), module, TRUE);
1777 rb_raise(rb_eArgError,
"cyclic include detected");
1780 VALUE subs_v = RCLASS_SUBCLASSES(klass);
1783 VALUE *entries = rb_imemo_subclasses_entries(subs_v);
1784 for (uint32_t i = 0; i < subs->count; i++) {
1785 VALUE check_class = entries[i];
1786 if (!check_class)
continue;
1791 if (!rb_objspace_garbage_object_p(check_class)) {
1792 VALUE walk = check_class;
1797 (METACLASS_OF(walk) == module)) {
1804 include_modules_at(check_class, RCLASS_ORIGIN(check_class), module, TRUE);
1812static enum rb_id_table_iterator_result
1813add_refined_method_entry_i(
ID key,
VALUE value,
void *data)
1815 rb_add_refined_method_entry((
VALUE)data, key);
1816 return ID_TABLE_CONTINUE;
1819static enum rb_id_table_iterator_result
1820clear_module_cache_i(
ID id,
VALUE val,
void *data)
1823 rb_clear_method_cache(klass,
id);
1824 return ID_TABLE_CONTINUE;
1828module_in_super_chain(
const VALUE klass,
VALUE module)
1830 struct rb_id_table *
const klass_m_tbl = RCLASS_M_TBL(RCLASS_ORIGIN(klass));
1833 if (klass_m_tbl == RCLASS_M_TBL(module))
1835 module = RCLASS_SUPER(module);
1843static enum rb_id_table_iterator_result
1844clear_constant_cache_i(
ID id,
VALUE value,
void *data)
1847 return ID_TABLE_CONTINUE;
1851do_include_modules_at(
const VALUE klass,
VALUE c,
VALUE module,
int search_super,
bool check_cyclic)
1853 VALUE p, iclass, origin_stack = 0;
1854 int method_changed = 0;
1856 VALUE klass_origin = RCLASS_ORIGIN(klass);
1857 VALUE original_klass = klass;
1859 if (check_cyclic && module_in_super_chain(klass, module))
1864 int superclass_seen = FALSE;
1870 if (klass_origin != c || search_super) {
1876 if (klass_origin == p && !search_super)
1881 if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) {
1882 if (!superclass_seen && c_seen) {
1889 superclass_seen = TRUE;
1897 RB_DEBUG_COUNTER_INC(cvar_include_invalidate);
1898 ruby_vm_global_cvar_state++;
1899 tbl = RCLASS_M_TBL(module);
1900 if (tbl && rb_id_table_size(tbl)) {
1903 rb_id_table_foreach(tbl, clear_module_cache_i, (
void *)super_class);
1908 rb_id_table_foreach(tbl, clear_module_cache_i, (
void *)original_klass);
1915 iclass = rb_include_class_new(module, super_class);
1916 c = rb_class_set_super(c, iclass);
1917 RCLASS_SET_INCLUDER(iclass, klass);
1918 if (module != RCLASS_ORIGIN(module)) {
1920 VALUE origin[2] = {iclass, RCLASS_ORIGIN(module)};
1923 else if (origin_stack && (origin_len =
RARRAY_LEN(origin_stack)) > 1 &&
1924 RARRAY_AREF(origin_stack, origin_len - 1) == module) {
1925 RCLASS_WRITE_ORIGIN(
RARRAY_AREF(origin_stack, (origin_len -= 2)), iclass);
1926 RICLASS_WRITE_ORIGIN_SHARED_MTBL(iclass);
1932 rb_module_add_to_subclasses_list(m, iclass);
1935 VALUE refined_class =
1936 rb_refinement_module_get_refined_class(klass);
1938 rb_id_table_foreach(RCLASS_M_TBL(module), add_refined_method_entry_i, (
void *)refined_class);
1942 tbl = RCLASS_CONST_TBL(module);
1943 if (tbl && rb_id_table_size(tbl))
1944 rb_id_table_foreach(tbl, clear_constant_cache_i, NULL);
1946 module = RCLASS_SUPER(module);
1949 return method_changed;
1953include_modules_at(
const VALUE klass,
VALUE c,
VALUE module,
int search_super)
1955 return do_include_modules_at(klass, c, module, search_super,
true);
1958static enum rb_id_table_iterator_result
1959move_refined_method(
ID key,
VALUE value,
void *data)
1963 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1965 struct rb_id_table *tbl = RCLASS_WRITABLE_M_TBL(klass);
1967 if (me->def->body.refined.orig_me) {
1969 RB_OBJ_WRITE(me, &me->def->body.refined.orig_me, NULL);
1970 new_me = rb_method_entry_clone(me);
1971 rb_method_table_insert(klass, tbl, key, new_me);
1972 rb_method_entry_copy(me, orig_me);
1973 return ID_TABLE_CONTINUE;
1976 rb_method_table_insert(klass, tbl, key, me);
1977 return ID_TABLE_DELETE;
1981 return ID_TABLE_CONTINUE;
1985static enum rb_id_table_iterator_result
1986cache_clear_refined_method(
ID key,
VALUE value,
void *data)
1990 if (me->def->type == VM_METHOD_TYPE_REFINED && me->def->body.refined.orig_me) {
1992 rb_clear_method_cache(klass, me->called_id);
1997 return ID_TABLE_CONTINUE;
2001ensure_origin(
VALUE klass)
2003 VALUE origin = RCLASS_ORIGIN(klass);
2004 if (origin == klass) {
2008 rb_class_ensure_writable(klass);
2009 origin = class_alloc(
T_ICLASS, klass);
2010 RCLASS_SET_M_TBL(origin, RCLASS_M_TBL(klass));
2012 rb_class_set_super(klass, origin);
2013 RCLASS_WRITE_ORIGIN(klass, origin);
2017 rb_gc_writebarrier_remember(origin);
2019 class_clear_method_table(klass);
2020 rb_id_table_foreach(RCLASS_M_TBL(origin), cache_clear_refined_method, (
void *)klass);
2021 rb_id_table_foreach(RCLASS_M_TBL(origin), move_refined_method, (
void *)klass);
2031 bool klass_had_no_origin;
2033 ensure_includable(klass, module);
2034 if (module_in_super_chain(klass, module))
2035 rb_raise(rb_eArgError,
"cyclic prepend detected");
2037 klass_had_no_origin = ensure_origin(klass);
2038 changed = do_include_modules_at(klass, klass, module, FALSE,
false);
2041 rb_vm_check_redefinition_by_prepend(klass);
2044 VALUE subs_v = RCLASS_SUBCLASSES(klass);
2045 VALUE klass_origin = RCLASS_ORIGIN(klass);
2046 struct rb_id_table *klass_m_tbl = RCLASS_M_TBL(klass);
2047 struct rb_id_table *klass_origin_m_tbl = RCLASS_M_TBL(klass_origin);
2050 VALUE *entries = rb_imemo_subclasses_entries(subs_v);
2051 VALUE new_origins = 0;
2052 for (uint32_t i = 0; i < subs->count; i++) {
2053 const VALUE subclass = entries[i];
2054 if (!subclass)
continue;
2057 if (!rb_objspace_garbage_object_p(subclass)) {
2058 if (klass_had_no_origin && klass_origin_m_tbl == RCLASS_M_TBL(subclass)) {
2060 rb_id_table_foreach(RCLASS_M_TBL(subclass), clear_module_cache_i, (
void *)subclass);
2061 RCLASS_WRITE_M_TBL(subclass, klass_m_tbl);
2063 rb_class_set_super(subclass, origin);
2064 RCLASS_SET_INCLUDER(origin, RCLASS_INCLUDER(subclass));
2065 RCLASS_WRITE_ORIGIN(subclass, origin);
2066 RICLASS_SET_ORIGIN_SHARED_MTBL(origin);
2070 include_modules_at(subclass, subclass, module, FALSE);
2076 for (
long i = 0; i <
RARRAY_LEN(new_origins); i++) {
2077 rb_module_add_to_subclasses_list(klass,
RARRAY_AREF(new_origins, i));
2112 VALUE origin = RCLASS_ORIGIN(mod);
2116 VALUE m = METACLASS_OF(p);
2151 if (METACLASS_OF(p) == mod2)
return Qtrue;
2181 refined_class = rb_refinement_module_get_refined_class(mod);
2185 if (p == refined_class)
break;
2186 if (p != RCLASS_ORIGIN(p))
continue;
2202 bool immediate_only;
2206class_descendants_recursive(
VALUE klass,
VALUE v)
2212 if (!RCLASS_SINGLETON_P(klass)) {
2213 if (data->buffer && data->count < data->maxcount && !rb_objspace_garbage_object_p(klass)) {
2218 if (data->immediate_only)
return;
2220 rb_class_foreach_subclass(klass, class_descendants_recursive, v);
2224class_descendants(
VALUE klass,
bool immediate_only)
2229 rb_class_foreach_subclass(klass, class_descendants_recursive, (
VALUE) &data);
2233 data.maxcount = data.count;
2236 size_t gc_count = rb_gc_count();
2239 rb_class_foreach_subclass(klass, class_descendants_recursive, (
VALUE) &data);
2241 if (gc_count != rb_gc_count()) {
2242 rb_bug(
"GC must not occur during the subclass iteration of Class#descendants");
2290 return class_descendants(klass,
true);
2301static void module_descendants_recursive(
VALUE entry,
VALUE v);
2307 if (data->buffer && data->count >= data->maxcount)
return;
2309 if (st_insert(data->visited, (st_data_t)klass, 1))
return;
2316 rb_class_foreach_subclass(klass, module_descendants_recursive, (
VALUE)data);
2322iclass_in_ancestors_p(
VALUE iclass,
VALUE includer)
2325 if (p == iclass)
return true;
2331module_descendants_recursive(
VALUE entry,
VALUE v)
2335 if (rb_objspace_garbage_object_p(entry))
return;
2340 VALUE includer = RCLASS_INCLUDER(entry);
2342 includer = RCLASS_INCLUDER(includer);
2344 if (!includer || UNDEF_P(includer))
return;
2345 if (rb_objspace_garbage_object_p(includer))
return;
2346 if (RCLASS_SINGLETON_P(includer))
return;
2347 if (!iclass_in_ancestors_p(entry, includer))
return;
2348 module_descendants_add(includer, data);
2351 if (RCLASS_SINGLETON_P(entry))
return;
2352 module_descendants_add(entry, data);
2389 data.visited = st_init_numtable();
2390 st_insert(data.visited, (st_data_t)mod, 1);
2391 rb_class_foreach_subclass(mod, module_descendants_recursive, (
VALUE)&data);
2392 st_free_table(data.visited);
2396 data.maxcount = data.count;
2399 data.visited = st_init_numtable_with_size(data.maxcount + 1);
2400 st_insert(data.visited, (st_data_t)mod, 1);
2402 size_t gc_count = rb_gc_count();
2404 rb_class_foreach_subclass(mod, module_descendants_recursive, (
VALUE)&data);
2406 if (gc_count != rb_gc_count()) {
2407 rb_bug(
"GC must not occur during the subclass iteration of Module#descendants");
2409 st_free_table(data.visited);
2437 if (!RCLASS_SINGLETON_P(klass)) {
2438 rb_raise(
rb_eTypeError,
"'%"PRIsVALUE
"' is not a singleton class", klass);
2441 const VALUE obj = RCLASS_ATTACHED_OBJECT(klass);
2448 rb_raise(rb_eRactorIsolationError,
2449 "can not get an unshareable attached object from another Ractor");
2456ins_methods_push(st_data_t name, st_data_t ary)
2462ins_methods_i(st_data_t name, st_data_t
type, st_data_t ary)
2464 switch ((rb_method_visibility_t)
type) {
2465 case METHOD_VISI_UNDEF:
2466 case METHOD_VISI_PRIVATE:
2469 ins_methods_push(name, ary);
2476ins_methods_type_i(st_data_t name, st_data_t
type, st_data_t ary, rb_method_visibility_t visi)
2478 if ((rb_method_visibility_t)
type == visi) {
2479 ins_methods_push(name, ary);
2485ins_methods_prot_i(st_data_t name, st_data_t
type, st_data_t ary)
2487 return ins_methods_type_i(name,
type, ary, METHOD_VISI_PROTECTED);
2491ins_methods_priv_i(st_data_t name, st_data_t
type, st_data_t ary)
2493 return ins_methods_type_i(name,
type, ary, METHOD_VISI_PRIVATE);
2497ins_methods_pub_i(st_data_t name, st_data_t
type, st_data_t ary)
2499 return ins_methods_type_i(name,
type, ary, METHOD_VISI_PUBLIC);
2503ins_methods_undef_i(st_data_t name, st_data_t
type, st_data_t ary)
2505 return ins_methods_type_i(name,
type, ary, METHOD_VISI_UNDEF);
2513static enum rb_id_table_iterator_result
2514method_entry_i(
ID key,
VALUE value,
void *data)
2518 rb_method_visibility_t
type;
2520 if (me->def->type == VM_METHOD_TYPE_REFINED) {
2521 VALUE owner = me->owner;
2522 me = rb_resolve_refined_method(
Qnil, me);
2523 if (!me)
return ID_TABLE_CONTINUE;
2524 if (!arg->recur && me->owner != owner)
return ID_TABLE_CONTINUE;
2526 if (!st_is_member(arg->list, key)) {
2527 if (UNDEFINED_METHOD_ENTRY_P(me)) {
2528 type = METHOD_VISI_UNDEF;
2531 type = METHOD_ENTRY_VISI(me);
2534 st_add_direct(arg->list, key, (st_data_t)
type);
2536 return ID_TABLE_CONTINUE;
2544 rb_id_table_foreach(m_tbl, method_entry_i, me_arg);
2548particular_class_p(
VALUE mod)
2550 if (!mod)
return false;
2551 if (RCLASS_SINGLETON_P(mod))
return true;
2557class_instance_method_list(
int argc,
const VALUE *argv,
VALUE mod,
int obj,
int (*func) (st_data_t, st_data_t, st_data_t))
2560 int recur = TRUE, prepended = 0;
2565 me_arg.list = st_init_numtable();
2566 me_arg.recur = recur;
2569 for (; particular_class_p(mod); mod =
RCLASS_SUPER(mod)) {
2570 add_instance_method_list(mod, &me_arg);
2574 if (!recur && RCLASS_ORIGIN(mod) != mod) {
2575 mod = RCLASS_ORIGIN(mod);
2580 add_instance_method_list(mod, &me_arg);
2585 st_foreach(me_arg.list, func, ary);
2586 st_free_table(me_arg.list);
2630 return class_instance_method_list(argc, argv, mod, 0, ins_methods_i);
2645 return class_instance_method_list(argc, argv, mod, 0, ins_methods_prot_i);
2668 return class_instance_method_list(argc, argv, mod, 0, ins_methods_priv_i);
2683 return class_instance_method_list(argc, argv, mod, 0, ins_methods_pub_i);
2695rb_class_undefined_instance_methods(
VALUE mod)
2698 return class_instance_method_list(1, &include_super, mod, 0, ins_methods_undef_i);
2732rb_obj_methods(
int argc,
const VALUE *argv,
VALUE obj)
2735 if (argc > 0 && !
RTEST(argv[0])) {
2738 return class_instance_method_list(argc, argv,
CLASS_OF(obj), 1, ins_methods_i);
2751rb_obj_protected_methods(
int argc,
const VALUE *argv,
VALUE obj)
2753 return class_instance_method_list(argc, argv,
CLASS_OF(obj), 1, ins_methods_prot_i);
2766rb_obj_private_methods(
int argc,
const VALUE *argv,
VALUE obj)
2768 return class_instance_method_list(argc, argv,
CLASS_OF(obj), 1, ins_methods_priv_i);
2781rb_obj_public_methods(
int argc,
const VALUE *argv,
VALUE obj)
2783 return class_instance_method_list(argc, argv,
CLASS_OF(obj), 1, ins_methods_pub_i);
2822 VALUE ary, klass, origin;
2832 origin = RCLASS_ORIGIN(klass);
2833 me_arg.list = st_init_numtable();
2834 me_arg.recur = recur;
2835 if (klass && RCLASS_SINGLETON_P(klass)) {
2836 if ((mtbl = RCLASS_M_TBL(origin)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2841 if (klass != origin && (mtbl = RCLASS_M_TBL(klass)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2846 st_foreach(me_arg.list, ins_methods_i, ary);
2847 st_free_table(me_arg.list);
2860#ifdef rb_define_method_id
2861#undef rb_define_method_id
2866 rb_add_method_cfunc(klass, mid, func, argc, METHOD_VISI_PUBLIC);
2869#ifdef rb_define_method
2870#undef rb_define_method
2875 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PUBLIC);
2878#ifdef rb_define_protected_method
2879#undef rb_define_protected_method
2884 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PROTECTED);
2887#ifdef rb_define_private_method
2888#undef rb_define_private_method
2893 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PRIVATE);
2899 rb_add_method(klass, rb_intern(name), VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2902static enum rb_id_table_iterator_result
2903undef_method_i(
ID name,
VALUE value,
void *data)
2906 rb_add_method(klass, name, VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2907 return ID_TABLE_CONTINUE;
2911rb_undef_methods_from(
VALUE klass,
VALUE super)
2915 rb_id_table_foreach(mtbl, undef_method_i, (
void *)klass);
2928special_singleton_class_of(
VALUE obj)
2934 default:
return Qnil;
2939rb_special_singleton_class(
VALUE obj)
2941 return special_singleton_class_of(obj);
2954singleton_class_of(
VALUE obj,
bool ensure_eigenclass)
2958 switch (
TYPE(obj)) {
2968 klass = special_singleton_class_of(obj);
2970 rb_bug(
"unknown immediate %p", (
void *)obj);
2974 if (CHILLED_STRING_P(obj)) {
2975 CHILLED_STRING_MUTATED(obj);
2985 RB_VM_LOCK_ENTER_LEV(&lev);
2988 klass = METACLASS_OF(obj);
2989 if (!(RCLASS_SINGLETON_P(klass) &&
2990 RCLASS_ATTACHED_OBJECT(klass) == obj)) {
2991 klass = rb_make_metaclass(obj, klass);
3000 RB_VM_LOCK_LEAVE_LEV(&lev);
3006#if RUBY_VERSION_SINCE(4, 2)
3007RBIMPL_TODO(
"make rb_freeze_singleton_class internal; remove from fl_type.h")
3019 (RCLASS_ATTACHED_OBJECT(klass) == attached_object)) {
3020 attached_object = klass;
3038 return rb_special_singleton_class(obj);
3040 klass = METACLASS_OF(obj);
3041 if (!RCLASS_SINGLETON_P(klass))
return Qnil;
3042 if (RCLASS_ATTACHED_OBJECT(klass) != obj)
return Qnil;
3049 return singleton_class_of(obj,
true);
3061#ifdef rb_define_singleton_method
3062#undef rb_define_singleton_method
3070#ifdef rb_define_module_function
3071#undef rb_define_module_function
3080#ifdef rb_define_global_function
3081#undef rb_define_global_function
3092 rb_alias(klass, rb_intern(name1), rb_intern(name2));
3098 rb_attr(klass, rb_intern(name), read, write, FALSE);
3102rb_keyword_error_new(
const char *error,
VALUE keys)
3105 VALUE error_message = rb_sprintf(
"%s keyword%.*s", error,
len > 1,
"s");
3112 if (++i >=
len)
break;
3120NORETURN(
static void rb_keyword_error(
const char *error,
VALUE keys));
3122rb_keyword_error(
const char *error,
VALUE keys)
3127NORETURN(
static void unknown_keyword_error(
VALUE hash,
const ID *table,
int keywords));
3129unknown_keyword_error(
VALUE hash,
const ID *table,
int keywords)
3132 for (i = 0; i < keywords; i++) {
3133 st_data_t key =
ID2SYM(table[i]);
3134 rb_hash_stlike_delete(hash, &key, NULL);
3136 rb_keyword_error(
"unknown", rb_hash_keys(hash));
3141separate_symbol(st_data_t key, st_data_t value, st_data_t arg)
3145 if (!*kwdhash) *kwdhash = rb_hash_new();
3146 rb_hash_aset(*kwdhash, (
VALUE)key, (
VALUE)value);
3153 VALUE parthash[2] = {0, 0};
3154 VALUE hash = *orighash;
3161 *orighash = parthash[1];
3176#define extract_kwarg(keyword, val) \
3177 (key = (st_data_t)(keyword), values ? \
3178 (rb_hash_stlike_delete(keyword_hash, &key, &(val)) || ((val) = Qundef, 0)) : \
3179 rb_hash_stlike_lookup(keyword_hash, key, NULL))
3181 if (
NIL_P(keyword_hash)) keyword_hash = 0;
3185 optional = -1-optional;
3188 for (; i < required; i++) {
3191 if (extract_kwarg(keyword, values[i])) {
3198 if (!
NIL_P(missing)) {
3199 rb_keyword_error(
"missing", missing);
3203 if (optional && keyword_hash) {
3204 for (i = 0; i < optional; i++) {
3205 if (extract_kwarg(
ID2SYM(table[required+i]), values[required+i])) {
3210 if (!rest && keyword_hash) {
3211 if (
RHASH_SIZE(keyword_hash) > (
unsigned int)(values ? 0 : j)) {
3212 unknown_keyword_error(keyword_hash, table, required+optional);
3215 if (values && !keyword_hash) {
3216 for (i = 0; i < required + optional; i++) {
3235rb_scan_args_parse(
int kw_flag,
const char *fmt,
struct rb_scan_args_t *arg)
3237 const char *p = fmt;
3239 memset(arg, 0,
sizeof(*arg));
3240 arg->kw_flag = kw_flag;
3243 arg->n_lead = *p -
'0';
3246 arg->n_opt = *p -
'0';
3255 arg->n_trail = *p -
'0';
3267 rb_fatal(
"bad scan arg format: %s", fmt);
3272rb_scan_args_assign(
const struct rb_scan_args_t *arg,
int argc,
const VALUE *
const argv, va_list vargs)
3276#define rb_scan_args_next_param() va_arg(vargs, VALUE *)
3277 const int kw_flag = arg->kw_flag;
3278 const int n_lead = arg->n_lead;
3279 const int n_opt = arg->n_opt;
3280 const int n_trail = arg->n_trail;
3281 const int n_mand = n_lead + n_trail;
3282 const bool f_var = arg->f_var;
3283 const bool f_hash = arg->f_hash;
3284 const bool f_block = arg->f_block;
3287 if (f_hash && argc > 0) {
3288 VALUE last = argv[argc - 1];
3289 if (rb_scan_args_keyword_p(kw_flag, last)) {
3290 hash = rb_hash_dup(last);
3295 if (argc < n_mand) {
3300 for (i = 0; i < n_lead; i++) {
3301 var = rb_scan_args_next_param();
3302 if (var) *var = argv[argi];
3306 for (i = 0; i < n_opt; i++) {
3307 var = rb_scan_args_next_param();
3308 if (argi < argc - n_trail) {
3309 if (var) *var = argv[argi];
3313 if (var) *var =
Qnil;
3318 int n_var = argc - argi - n_trail;
3320 var = rb_scan_args_next_param();
3330 for (i = 0; i < n_trail; i++) {
3331 var = rb_scan_args_next_param();
3332 if (var) *var = argv[argi];
3337 var = rb_scan_args_next_param();
3338 if (var) *var = hash;
3342 var = rb_scan_args_next_param();
3357#undef rb_scan_args_next_param
3361rb_scan_args_result(
const struct rb_scan_args_t *
const arg,
int argc)
3363 const int n_lead = arg->n_lead;
3364 const int n_opt = arg->n_opt;
3365 const int n_trail = arg->n_trail;
3366 const int n_mand = n_lead + n_trail;
3367 const bool f_var = arg->f_var;
3385 va_start(vargs,fmt);
3386 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
3388 return rb_scan_args_result(&arg, argc);
3391#undef rb_scan_args_kw
3397 rb_scan_args_parse(kw_flag, fmt, &arg);
3398 va_start(vargs,fmt);
3399 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
3401 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_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_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_super_of(VALUE klass)
Internal header for Objspace.
VALUE rb_class_boot(VALUE super)
A utility function that wraps class_alloc.
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.
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.
void rb_freeze_singleton_class(VALUE attached_object)
This is an implementation detail of RB_OBJ_FREEZE().
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_mod_descendants(VALUE mod)
Queries the module's descendants.
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 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 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_class_superclass(VALUE klass)
Queries the parent of the given class.
VALUE rb_mKernel
Kernel module.
VALUE rb_cObject
Object class.
VALUE rb_any_to_s(VALUE obj)
Generates a textual representation of the given object.
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_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.
int rb_is_instance_id(ID id)
Classifies the given ID, then sees if it is an instance variable.
int rb_is_class_id(ID id)
Classifies the given ID, then sees if it is a class variable.
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.
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.
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 RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
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.
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.