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;
221 RCLASSEXT_M_TBL(ext) = init_missing ? rb_id_table_create(0) : NULL;
224 struct rb_id_table *tbl = rb_id_table_create(rb_id_table_size(orig));
225 RCLASSEXT_M_TBL(ext) = tbl;
230 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);
300 RCLASSEXT_ICLASS_IS_ORIGIN(ext) = RCLASSEXT_ICLASS_IS_ORIGIN(src);
301 RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext) = RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(src);
304 if (RCLASSEXT_ICLASS_IS_ORIGIN(src) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(src)) {
305 duplicate_classext_m_tbl(ext, RCLASSEXT_M_TBL(src), iclass,
true);
308 RCLASSEXT_M_TBL(ext) = RCLASSEXT_M_TBL(mod_ext);
311 RCLASSEXT_CONST_TBL(ext) = RCLASSEXT_CONST_TBL(mod_ext);
312 RCLASSEXT_CVC_TBL(ext) = RCLASSEXT_CVC_TBL(mod_ext);
320 RCLASSEXT_SET_ORIGIN(ext, iclass, RCLASSEXT_ORIGIN(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_ICLASS_IS_ORIGIN(ext) =
true;
345 duplicate_classext_m_tbl(ext, RCLASSEXT_M_TBL(orig), klass, dup_iclass);
346 RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext) =
false;
348 VALUE orig_fields_obj = RCLASSEXT_FIELDS_OBJ(orig);
349 if (orig_fields_obj) {
350 RCLASSEXT_SET_FIELDS_OBJ(klass, ext, rb_imemo_fields_clone(orig_fields_obj));
353 if (RCLASSEXT_SHARED_CONST_TBL(orig)) {
354 RCLASSEXT_CONST_TBL(ext) = RCLASSEXT_CONST_TBL(orig);
355 RCLASSEXT_SHARED_CONST_TBL(ext) =
true;
358 RCLASSEXT_CONST_TBL(ext) = duplicate_classext_const_tbl(RCLASSEXT_CONST_TBL(orig), klass);
359 RCLASSEXT_SHARED_CONST_TBL(ext) =
false;
371 VALUE cvc_table = RCLASSEXT_CVC_TBL(orig);
373 cvc_table = rb_marked_id_table_dup(cvc_table);
375 else if (dup_iclass) {
376 cvc_table = rb_marked_id_table_new(2);
378 RB_OBJ_WRITE(klass, &RCLASSEXT_CVC_TBL(ext), cvc_table);
382 RCLASSEXT_SET_ORIGIN(ext, klass, RCLASSEXT_ORIGIN(orig));
391 RCLASSEXT_PERMANENT_CLASSPATH(ext) = RCLASSEXT_PERMANENT_CLASSPATH(orig);
392 RCLASSEXT_CLASSPATH(ext) = RCLASSEXT_CLASSPATH(orig);
404 VALUE subs_v = RCLASSEXT_SUBCLASSES(orig);
407 VALUE *entries = rb_imemo_subclasses_entries(subs_v);
408 for (uint32_t i = 0; i < subs->count; i++) {
409 VALUE iclass = entries[i];
410 if (!iclass)
continue;
420 class_duplicate_iclass_classext(iclass, ext, box);
430rb_class_ensure_writable(
VALUE klass)
433 RCLASS_EXT_WRITABLE(klass);
437 rb_class_classext_foreach_callback_func *func;
442class_classext_foreach_i(st_data_t key, st_data_t value, st_data_t arg)
445 rb_class_classext_foreach_callback_func *func =
foreach_arg->func;
451rb_class_classext_foreach(
VALUE klass, rb_class_classext_foreach_callback_func *func,
void *arg)
453 st_table *tbl = RCLASS_CLASSEXT_TBL(klass);
458 rb_st_foreach(tbl, class_classext_foreach_i, (st_data_t)&
foreach_arg);
460 func(RCLASS_EXT_PRIME(klass),
true, (
VALUE)NULL, arg);
470rb_class_singleton_p(
VALUE klass)
472 return RCLASS_SINGLETON_P(klass);
476rb_class_variation_count(
VALUE klass)
478 return RCLASS_VARIATION_COUNT(klass);
482push_subclass_entry_to_list(
VALUE super,
VALUE klass)
491 VALUE subs_v = RCLASS_SUBCLASSES(super);
494 if (!subs || subs->count == subs->capacity) {
495 VALUE *old_entries = subs ? rb_imemo_subclasses_entries(subs_v) : NULL;
497 for (uint32_t i = 0; subs && i < subs->count; i++) {
498 if (old_entries[i]) live++;
501 uint32_t cap = subs ? subs->capacity : 2;
502 if (live * 2 >= cap) cap *= 2;
504 VALUE new_v = rb_imemo_subclasses_new(cap);
506 VALUE *new_entries = rb_imemo_subclasses_entries(new_v);
507 for (uint32_t i = 0; subs && i < subs->count; i++) {
508 VALUE entry = old_entries[i];
510 new_entries[new_subs->count++] = entry;
514 RCLASS_SET_SUBCLASSES(super, new_v);
519 rb_imemo_subclasses_entries(subs_v)[subs->count++] = klass;
525rb_class_subclass_add(
VALUE super,
VALUE klass)
527 if (super && !UNDEF_P(super)) {
530 push_subclass_entry_to_list(super, klass);
535rb_module_add_to_subclasses_list(
VALUE module,
VALUE iclass)
537 if (module && !UNDEF_P(module)) {
540 push_subclass_entry_to_list(module, iclass);
547 VALUE subs_v = RCLASS_SUBCLASSES(klass);
551 VALUE *entries = rb_imemo_subclasses_entries(subs_v);
552 for (uint32_t i = 0; i < subs->count; i++) {
553 VALUE curklass = entries[i];
561class_switch_superclass(
VALUE super,
VALUE klass)
565 rb_class_subclass_add(super, klass);
581 const rb_box_t *box = rb_current_box();
583 if (!ruby_box_init_done) {
595 if (boxable) flags |= RCLASS_BOXABLE;
597 shape_id_t shape_id = ROOT_SHAPE_ID;
599 shape_id |= SHAPE_ID_LAYOUT_OTHER;
602 shape_id |= SHAPE_ID_LAYOUT_RCLASS;
605 struct RClass *obj = (
struct RClass *)rb_newobj(GET_EC(), klass, flags, shape_id,
true, alloc_size);
613 if (
type !=
T_ICLASS && UNLIKELY(!rb_ractor_main_p())) {
614 RCLASS_SET_OWNER_RACTOR_ID((
VALUE)obj, rb_ractor_id(GET_RACTOR()));
628 RCLASS_PRIME_BOX((
VALUE)obj) = box;
631 RCLASS_SET_PRIME_CLASSEXT_WRITABLE((
VALUE)obj, !boxable || BOX_USER_P(box));
634 RCLASS_SET_REFINED_CLASS((
VALUE)obj,
Qnil);
642 bool boxable = rb_box_available() && BOX_MASTER_P(rb_current_box());
647rb_class_owned_by_ractor_p(rb_serial_t owner_id)
649 return owner_id == rb_ractor_id(GET_RACTOR());
656class_owner_name(
VALUE klass)
660 VALUE obj = RCLASS_ATTACHED_OBJECT(klass);
666class_own_metaclass(
VALUE klass)
668 VALUE meta = METACLASS_OF(klass);
669 return (RCLASS_SINGLETON_P(meta) && RCLASS_ATTACHED_OBJECT(meta) == klass) ? meta : 0;
675rb_class_take_ownership(
VALUE klass)
678 rb_serial_t
id = rb_ractor_main_p() ? 0 : rb_ractor_id(GET_RACTOR());
682 RCLASS_SET_OWNER_RACTOR_ID(klass,
id);
683 }
while ((klass = class_own_metaclass(klass)) != 0);
687rb_class_owner_check(
VALUE klass)
689 if (UNLIKELY(!rb_class_owned_p(klass))) {
690 rb_raise(rb_eRactorIsolationError,
691 "can not modify %"PRIsVALUE
" because it is created by another Ractor",
692 class_owner_name(klass));
697class_associate_super(
VALUE klass,
VALUE super,
bool init)
699 if (super && !UNDEF_P(super)) {
704 if (RCLASS_SINGLETON_P(klass)) {
711 class_switch_superclass(super, klass);
716 RCLASS_SET_SUPER(klass, super);
719 RCLASS_WRITE_SUPER(klass, super);
721 rb_class_update_superclasses(klass);
728 return class_associate_super(klass, super,
false);
732class_initialize_method_table(
VALUE c)
735 RCLASS_SET_M_TBL(c, rb_id_table_create(0));
739class_clear_method_table(
VALUE c)
741 RCLASS_WRITE_M_TBL(c, rb_id_table_create(0));
745class_boot_boxable(
VALUE super,
bool boxable)
751 class_initialize_method_table(klass);
753 class_associate_super(klass, super,
true);
754 if (super && !UNDEF_P(super)) {
755 RCLASS_SET_ALLOCATOR(klass, RCLASS_ALLOCATOR(super));
756 rb_class_set_initialized(klass);
774 return class_boot_boxable(super,
false);
778class_superclasses_including_self(
VALUE klass)
780 if (RCLASS_SUPERCLASSES_WITH_SELF_P(klass))
781 return RCLASS_SUPERCLASSES(klass);
783 size_t depth = RCLASS_SUPERCLASS_DEPTH(klass);
786 memcpy(superclasses, RCLASS_SUPERCLASSES(klass),
sizeof(
VALUE) * depth);
787 superclasses[depth] = klass;
793rb_class_update_superclasses(
VALUE klass)
800 if (UNDEF_P(super))
return;
803 if (RCLASS_SUPERCLASSES(klass))
818 rb_class_update_superclasses(super);
821 if (!RCLASS_SUPERCLASS_DEPTH(super))
825 super_depth = RCLASS_SUPERCLASS_DEPTH(super);
826 if (RCLASS_SUPERCLASSES_WITH_SELF_P(super)) {
827 superclasses = RCLASS_SUPERCLASSES(super);
830 superclasses = class_superclasses_including_self(super);
831 RCLASS_WRITE_SUPERCLASSES(super, super_depth, superclasses,
true);
834 size_t depth = super_depth == RCLASS_MAX_SUPERCLASS_DEPTH ? super_depth : super_depth + 1;
835 RCLASS_WRITE_SUPERCLASSES(klass, depth, superclasses,
false);
842 rb_raise(
rb_eTypeError,
"superclass must be an instance of Class (given an instance of %"PRIsVALUE
")",
845 if (RCLASS_SINGLETON_P(super)) {
846 rb_raise(
rb_eTypeError,
"can't make subclass of singleton class");
860 RCLASS_SET_MAX_IV_COUNT(klass, RCLASS_MAX_IV_COUNT(super));
861 RUBY_ASSERT(getenv(
"RUBY_BOX") || RCLASS_PRIME_CLASSEXT_WRITABLE_P(klass));
867rb_class_s_alloc(
VALUE klass)
875 rb_method_entry_set(new_klass, mid, me, METHOD_ENTRY_VISI(me));
882static enum rb_id_table_iterator_result
883clone_method_i(
ID key,
VALUE value,
void *data)
887 return ID_TABLE_CONTINUE;
903 rb_id_table_insert(arg->tbl, key, (
VALUE)nce);
904 return ID_TABLE_CONTINUE;
907static enum rb_id_table_iterator_result
908clone_const_i(
ID key,
VALUE value,
void *data)
920 if (RCLASS_SINGLETON_P(orig)) {
931cvc_table_entry_alloc(
void)
936static enum rb_id_table_iterator_result
937cvc_table_copy(
ID id,
VALUE val,
void *data)
945 ent = cvc_table_entry_alloc();
948 ent->global_cvar_state = orig_entry->global_cvar_state;
949 rb_marked_id_table_insert(ctx->new_table,
id, (
VALUE)ent);
951 return ID_TABLE_CONTINUE;
957 if (RCLASS_CONST_TBL(clone)) {
958 rb_free_const_table(RCLASS_CONST_TBL(clone));
959 RCLASS_WRITE_CONST_TBL(clone, 0,
false);
961 if (RCLASS_CVC_TBL(orig)) {
962 VALUE rb_cvc_tbl = RCLASS_CVC_TBL(orig);
963 VALUE rb_cvc_tbl_dup = rb_marked_id_table_new(rb_marked_id_table_size(rb_cvc_tbl));
967 ctx.new_table = rb_cvc_tbl_dup;
968 rb_marked_id_table_foreach(rb_cvc_tbl, cvc_table_copy, &ctx);
969 RCLASS_WRITE_CVC_TBL(clone, rb_cvc_tbl_dup);
971 rb_id_table_free(RCLASS_M_TBL(clone));
972 RCLASS_WRITE_M_TBL(clone, 0);
974 rb_fields_tbl_copy(clone, orig);
976 if (RCLASS_CONST_TBL(orig)) {
979 struct rb_id_table *orig_tbl = RCLASS_CONST_TBL(orig);
982 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);
985 RCLASS_WRITE_CONST_TBL(clone, const_tbl,
false);
986 rb_gc_writebarrier_remember(clone);
990static bool ensure_origin(
VALUE klass);
993rb_class_set_initialized(
VALUE klass)
1001rb_module_check_initializable(
VALUE mod)
1003 if (RCLASS_INITIALIZED_P(mod)) {
1008static enum rb_id_table_iterator_result
1009move_check_const_i(
ID id,
VALUE v,
void *data)
1013 rb_raise(rb_eRactorIsolationError,
1014 "can not move an object whose singleton class has constant %"PRIsVALUE
1015 " referring to an unshareable object", rb_id2str(
id));
1017 return ID_TABLE_CONTINUE;
1021move_check_field_i(
ID id,
VALUE val, st_data_t arg)
1024 rb_raise(rb_eRactorIsolationError,
1025 "can not move an object whose singleton class has variable %"PRIsVALUE
1026 " referring to an unshareable object", rb_id2str(
id));
1034rb_class_check_singleton_movable(
VALUE klass)
1037 if (RCLASS_CONST_TBL(klass)) {
1038 rb_id_table_foreach(RCLASS_CONST_TBL(klass), move_check_const_i, NULL);
1040 rb_ivar_foreach_buffered(klass, move_check_field_i, 0);
1041 }
while ((klass = class_own_metaclass(klass)) != 0);
1046init_copy_owner_check(
VALUE orig)
1048 VALUE meta = METACLASS_OF(orig);
1049 if (!rb_class_owned_p(orig) || (RCLASS_SINGLETON_P(meta) && !rb_class_owned_p(meta))) {
1050 rb_raise(rb_eRactorIsolationError,
"can not copy a class/module created by another Ractor");
1063 class_init_copy_check(clone, orig);
1066 rb_module_check_initializable(clone);
1076 init_copy_owner_check(orig);
1078 if (RCLASS_INITIALIZED_P(orig)) {
1079 rb_class_set_initialized(clone);
1082 if (!RCLASS_SINGLETON_P(
CLASS_OF(clone))) {
1087 RCLASS_SET_ALLOCATOR(clone, RCLASS_ALLOCATOR(orig));
1089 copy_tables(clone, orig);
1090 if (RCLASS_M_TBL(orig)) {
1092 arg.new_klass = clone;
1093 class_initialize_method_table(clone);
1094 rb_id_table_foreach(RCLASS_M_TBL(orig), clone_method_i, &arg);
1097 if (RCLASS_ORIGIN(orig) == orig) {
1102 VALUE orig_origin = RCLASS_ORIGIN(orig);
1103 VALUE prev_clone_p = clone;
1111 ensure_origin(clone);
1112 clone_origin = RCLASS_ORIGIN(clone);
1114 while (p && p != orig_origin) {
1116 rb_bug(
"non iclass between module/class and origin");
1118 clone_p = class_alloc(
T_ICLASS, METACLASS_OF(p));
1119 RCLASS_SET_M_TBL(clone_p, RCLASS_M_TBL(p));
1120 rb_class_set_super(prev_clone_p, clone_p);
1121 prev_clone_p = clone_p;
1122 RCLASS_SET_CONST_TBL(clone_p, RCLASS_CONST_TBL(p),
false);
1123 RCLASS_SET_INCLUDER(clone_p, clone);
1124 add_subclass = TRUE;
1125 if (p != RCLASS_ORIGIN(p)) {
1126 origin[0] = clone_p;
1127 origin[1] = RCLASS_ORIGIN(p);
1130 else if ((origin_len =
RARRAY_LEN(origin_stack)) > 1 &&
1132 RCLASS_WRITE_ORIGIN(
RARRAY_AREF(origin_stack, (origin_len -= 2)), clone_p);
1133 RICLASS_WRITE_ORIGIN_SHARED_MTBL(clone_p);
1135 add_subclass = FALSE;
1138 rb_module_add_to_subclasses_list(METACLASS_OF(p), clone_p);
1143 if (p == orig_origin) {
1145 rb_class_set_super(clone_p, clone_origin);
1146 rb_class_set_super(clone_origin,
RCLASS_SUPER(orig_origin));
1148 copy_tables(clone_origin, orig_origin);
1149 if (RCLASS_M_TBL(orig_origin)) {
1151 arg.new_klass = clone;
1152 class_initialize_method_table(clone_origin);
1153 rb_id_table_foreach(RCLASS_M_TBL(orig_origin), clone_method_i, &arg);
1157 rb_bug(
"no origin for class that has origin");
1160 rb_class_update_superclasses(clone);
1176 return rb_singleton_class_clone_and_attach(obj,
Qundef);
1181rb_singleton_class_clone_and_attach(
VALUE obj,
VALUE attach)
1183 const VALUE klass = METACLASS_OF(obj);
1189 if (!(RCLASS_SINGLETON_P(klass) && RCLASS_ATTACHED_OBJECT(klass) == obj)) {
1195 bool klass_of_clone_is_new;
1200 klass_of_clone_is_new =
true;
1201 RBASIC_SET_CLASS(clone, clone);
1207 klass_of_clone_is_new = (METACLASS_OF(klass) != klass_metaclass_clone);
1208 RBASIC_SET_CLASS(clone, klass_metaclass_clone);
1212 class_initialize_method_table(clone);
1215 rb_fields_tbl_copy(clone, klass);
1216 if (RCLASS_CONST_TBL(klass)) {
1221 arg.tbl = table = rb_id_table_create(rb_id_table_size(RCLASS_CONST_TBL(klass)));
1222 rb_id_table_foreach(RCLASS_CONST_TBL(klass), clone_const_i, &arg);
1224 RCLASS_SET_CONST_TBL(clone, table,
false);
1226 if (!UNDEF_P(attach)) {
1231 arg.new_klass = clone;
1232 rb_id_table_foreach(RCLASS_M_TBL(klass), clone_method_i, &arg);
1234 if (klass_of_clone_is_new) {
1246 if (RCLASS_SINGLETON_P(klass)) {
1247 RCLASS_SET_ATTACHED_OBJECT(klass, obj);
1256#define META_CLASS_OF_CLASS_CLASS_P(k) (METACLASS_OF(k) == (k))
1259rb_singleton_class_has_metaclass_p(
VALUE sklass)
1261 return RCLASS_ATTACHED_OBJECT(METACLASS_OF(sklass)) == sklass;
1265rb_singleton_class_internal_p(
VALUE sklass)
1268 !rb_singleton_class_has_metaclass_p(sklass));
1276#define HAVE_METACLASS_P(k) \
1277 (FL_TEST(METACLASS_OF(k), FL_SINGLETON) && \
1278 rb_singleton_class_has_metaclass_p(k))
1287#define ENSURE_EIGENCLASS(klass) \
1288 (HAVE_METACLASS_P(klass) ? METACLASS_OF(klass) : make_metaclass(klass))
1308 RCLASS_SET_OWNER_RACTOR_ID(metaclass, RCLASS_OWNER_RACTOR_ID(klass));
1312 SET_METACLASS_OF(klass, metaclass);
1313 SET_METACLASS_OF(metaclass, metaclass);
1316 VALUE tmp = METACLASS_OF(klass);
1317 SET_METACLASS_OF(klass, metaclass);
1324 rb_class_set_initialized(metaclass);
1327 rb_class_update_superclasses(METACLASS_OF(metaclass));
1341 VALUE orig_class = METACLASS_OF(obj);
1346 RCLASS_SET_OWNER_RACTOR_ID(klass, RCLASS_OWNER_RACTOR_ID(obj));
1351 RCLASS_SET_OWNER_RACTOR_ID(klass, 0);
1353 class_initialize_method_table(klass);
1354 class_associate_super(klass, orig_class,
true);
1355 if (orig_class && !UNDEF_P(orig_class)) {
1356 rb_class_set_initialized(klass);
1359 RBASIC_SET_CLASS(obj, klass);
1361 rb_yjit_invalidate_no_singleton_class(orig_class);
1362 rb_zjit_invalidate_no_singleton_class(orig_class);
1364 SET_METACLASS_OF(klass, METACLASS_OF(
rb_class_real(orig_class)));
1370boot_defclass(
const char *name,
VALUE super)
1373 ID id = rb_intern(name);
1376 rb_vm_register_global_object(obj);
1427refinement_import_methods(
int argc,
VALUE *argv,
VALUE refinement)
1452Init_class_hierarchy(
void)
1514 rb_make_metaclass(klass, METACLASS_OF(super));
1534 return rb_funcallv_uncached(super, inherited, 1, &klass);
1537#ifdef rb_define_class
1538#undef rb_define_class
1541rb_define_class(
const char *name,
VALUE super)
1543 return rb_define_class_under(
rb_cObject, name, super);
1546#ifdef rb_define_class_under
1547#undef rb_define_class_under
1556rb_define_class_id_under_no_pin(
VALUE outer,
ID id,
VALUE super)
1568 rb_raise(
rb_eTypeError,
"%"PRIsVALUE
"::%"PRIsVALUE
" is not a class"
1575 rb_raise(
rb_eTypeError,
"superclass mismatch for class %s", rb_id2name(
id));
1579 "%"PRIsVALUE
"::%"PRIsVALUE
""
1580 " (%"PRIsVALUE
" is given but was %"PRIsVALUE
")",
1589 rb_raise(rb_eArgError,
"no super class for '%"PRIsVALUE
"'", rb_id2str(
id));
1592 rb_raise(rb_eArgError,
"no super class for '%"PRIsVALUE
"::%"PRIsVALUE
"'",
1607 VALUE klass = rb_define_class_id_under_no_pin(outer,
id, super);
1608 rb_vm_register_global_object(klass);
1613rb_module_s_alloc(
VALUE klass)
1616 class_initialize_method_table(mod);
1621module_new(
VALUE klass)
1624 class_initialize_method_table(mdl);
1647#ifdef rb_define_module
1648#undef rb_define_module
1651rb_define_module(
const char *name)
1656#ifdef rb_define_module_under
1657#undef rb_define_module_under
1660rb_define_module_under(
VALUE outer,
const char *name)
1671 module = rb_const_get_at(outer, id);
1674 rb_raise(
rb_eTypeError,
"%s is not a module (%"PRIsVALUE
")",
1678 rb_raise(
rb_eTypeError,
"%"PRIsVALUE
"::%"PRIsVALUE
" is not a module"
1684 rb_vm_register_global_object(module);
1687 module = rb_module_new();
1690 rb_vm_register_global_object(module);
1696rb_include_class_new(
VALUE module,
VALUE super)
1700 RCLASS_SET_M_TBL(klass, RCLASS_WRITABLE_M_TBL(module));
1702 RCLASS_SET_ORIGIN(klass, klass);
1704 module = METACLASS_OF(module);
1707 if (RCLASS_WRITABLE_CONST_TBL(module)) {
1708 RCLASS_SET_CONST_TBL(klass, RCLASS_WRITABLE_CONST_TBL(module),
true);
1711 RCLASS_WRITE_CONST_TBL(module, rb_id_table_create(0),
false);
1712 RCLASS_SET_CONST_TBL(klass, RCLASS_WRITABLE_CONST_TBL(module),
true);
1715 RCLASS_SET_CVC_TBL(klass, RCLASS_WRITABLE_CVC_TBL(module));
1717 class_associate_super(klass, super,
true);
1718 RBASIC_SET_CLASS(klass, module);
1720 return (
VALUE)klass;
1723static int include_modules_at(
const VALUE klass,
VALUE c,
VALUE module,
int search_super);
1730 rb_class_set_initialized(module);
1731 if (!
NIL_P(rb_refinement_module_get_refined_class(module))) {
1732 rb_raise(rb_eArgError,
"refinement module is not allowed");
1741 ensure_includable(klass, module);
1743 changed = include_modules_at(klass, RCLASS_ORIGIN(klass), module, TRUE);
1745 rb_raise(rb_eArgError,
"cyclic include detected");
1748 VALUE subs_v = RCLASS_SUBCLASSES(klass);
1751 VALUE *entries = rb_imemo_subclasses_entries(subs_v);
1752 for (uint32_t i = 0; i < subs->count; i++) {
1753 VALUE check_class = entries[i];
1754 if (!check_class)
continue;
1759 if (!rb_objspace_garbage_object_p(check_class)) {
1760 VALUE walk = check_class;
1765 (METACLASS_OF(walk) == module)) {
1772 include_modules_at(check_class, RCLASS_ORIGIN(check_class), module, TRUE);
1780static enum rb_id_table_iterator_result
1781add_refined_method_entry_i(
ID key,
VALUE value,
void *data)
1783 rb_add_refined_method_entry((
VALUE)data, key);
1784 return ID_TABLE_CONTINUE;
1787static enum rb_id_table_iterator_result
1788clear_module_cache_i(
ID id,
VALUE val,
void *data)
1791 rb_clear_method_cache(klass,
id);
1792 return ID_TABLE_CONTINUE;
1796module_in_super_chain(
const VALUE klass,
VALUE module)
1798 struct rb_id_table *
const klass_m_tbl = RCLASS_M_TBL(RCLASS_ORIGIN(klass));
1801 if (klass_m_tbl == RCLASS_M_TBL(module))
1803 module = RCLASS_SUPER(module);
1811static enum rb_id_table_iterator_result
1812clear_constant_cache_i(
ID id,
VALUE value,
void *data)
1815 return ID_TABLE_CONTINUE;
1819do_include_modules_at(
const VALUE klass,
VALUE c,
VALUE module,
int search_super,
bool check_cyclic)
1821 VALUE p, iclass, origin_stack = 0;
1822 int method_changed = 0;
1824 VALUE klass_origin = RCLASS_ORIGIN(klass);
1825 VALUE original_klass = klass;
1827 if (check_cyclic && module_in_super_chain(klass, module))
1832 int superclass_seen = FALSE;
1838 if (klass_origin != c || search_super) {
1844 if (klass_origin == p && !search_super)
1849 if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) {
1850 if (!superclass_seen && c_seen) {
1857 superclass_seen = TRUE;
1865 RB_DEBUG_COUNTER_INC(cvar_include_invalidate);
1866 ruby_vm_global_cvar_state++;
1867 tbl = RCLASS_M_TBL(module);
1868 if (tbl && rb_id_table_size(tbl)) {
1871 rb_id_table_foreach(tbl, clear_module_cache_i, (
void *)super_class);
1876 rb_id_table_foreach(tbl, clear_module_cache_i, (
void *)original_klass);
1883 iclass = rb_include_class_new(module, super_class);
1884 c = rb_class_set_super(c, iclass);
1885 RCLASS_SET_INCLUDER(iclass, klass);
1886 if (module != RCLASS_ORIGIN(module)) {
1888 VALUE origin[2] = {iclass, RCLASS_ORIGIN(module)};
1891 else if (origin_stack && (origin_len =
RARRAY_LEN(origin_stack)) > 1 &&
1892 RARRAY_AREF(origin_stack, origin_len - 1) == module) {
1893 RCLASS_WRITE_ORIGIN(
RARRAY_AREF(origin_stack, (origin_len -= 2)), iclass);
1894 RICLASS_WRITE_ORIGIN_SHARED_MTBL(iclass);
1900 rb_module_add_to_subclasses_list(m, iclass);
1903 VALUE refined_class =
1904 rb_refinement_module_get_refined_class(klass);
1906 rb_id_table_foreach(RCLASS_M_TBL(module), add_refined_method_entry_i, (
void *)refined_class);
1910 tbl = RCLASS_CONST_TBL(module);
1911 if (tbl && rb_id_table_size(tbl))
1912 rb_id_table_foreach(tbl, clear_constant_cache_i, NULL);
1914 module = RCLASS_SUPER(module);
1917 return method_changed;
1921include_modules_at(
const VALUE klass,
VALUE c,
VALUE module,
int search_super)
1923 return do_include_modules_at(klass, c, module, search_super,
true);
1926static enum rb_id_table_iterator_result
1927move_refined_method(
ID key,
VALUE value,
void *data)
1931 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1933 struct rb_id_table *tbl = RCLASS_WRITABLE_M_TBL(klass);
1935 if (me->def->body.refined.orig_me) {
1937 RB_OBJ_WRITE(me, &me->def->body.refined.orig_me, NULL);
1938 new_me = rb_method_entry_clone(me);
1939 rb_method_table_insert(klass, tbl, key, new_me);
1940 rb_method_entry_copy(me, orig_me);
1941 return ID_TABLE_CONTINUE;
1944 rb_method_table_insert(klass, tbl, key, me);
1945 return ID_TABLE_DELETE;
1949 return ID_TABLE_CONTINUE;
1953static enum rb_id_table_iterator_result
1954cache_clear_refined_method(
ID key,
VALUE value,
void *data)
1958 if (me->def->type == VM_METHOD_TYPE_REFINED && me->def->body.refined.orig_me) {
1960 rb_clear_method_cache(klass, me->called_id);
1965 return ID_TABLE_CONTINUE;
1969ensure_origin(
VALUE klass)
1971 VALUE origin = RCLASS_ORIGIN(klass);
1972 if (origin == klass) {
1976 rb_class_ensure_writable(klass);
1977 origin = class_alloc(
T_ICLASS, klass);
1978 RCLASS_SET_M_TBL(origin, RCLASS_M_TBL(klass));
1980 rb_class_set_super(klass, origin);
1981 RCLASS_WRITE_ORIGIN(klass, origin);
1985 rb_gc_writebarrier_remember(origin);
1987 class_clear_method_table(klass);
1988 rb_id_table_foreach(RCLASS_M_TBL(origin), cache_clear_refined_method, (
void *)klass);
1989 rb_id_table_foreach(RCLASS_M_TBL(origin), move_refined_method, (
void *)klass);
1999 bool klass_had_no_origin;
2001 ensure_includable(klass, module);
2002 if (module_in_super_chain(klass, module))
2003 rb_raise(rb_eArgError,
"cyclic prepend detected");
2005 klass_had_no_origin = ensure_origin(klass);
2006 changed = do_include_modules_at(klass, klass, module, FALSE,
false);
2009 rb_vm_check_redefinition_by_prepend(klass);
2012 VALUE subs_v = RCLASS_SUBCLASSES(klass);
2013 VALUE klass_origin = RCLASS_ORIGIN(klass);
2014 struct rb_id_table *klass_m_tbl = RCLASS_M_TBL(klass);
2015 struct rb_id_table *klass_origin_m_tbl = RCLASS_M_TBL(klass_origin);
2018 VALUE *entries = rb_imemo_subclasses_entries(subs_v);
2019 VALUE new_origins = 0;
2020 for (uint32_t i = 0; i < subs->count; i++) {
2021 const VALUE subclass = entries[i];
2022 if (!subclass)
continue;
2025 if (!rb_objspace_garbage_object_p(subclass)) {
2032 if (RICLASS_IS_ORIGIN_P(subclass))
continue;
2033 if (klass_had_no_origin && klass_origin_m_tbl == RCLASS_M_TBL(subclass)) {
2035 rb_id_table_foreach(RCLASS_M_TBL(subclass), clear_module_cache_i, (
void *)subclass);
2036 RCLASS_WRITE_M_TBL(subclass, klass_m_tbl);
2038 rb_class_set_super(subclass, origin);
2039 RCLASS_SET_INCLUDER(origin, RCLASS_INCLUDER(subclass));
2040 RCLASS_WRITE_ORIGIN(subclass, origin);
2041 RICLASS_SET_ORIGIN_SHARED_MTBL(origin);
2045 include_modules_at(subclass, subclass, module, FALSE);
2052 rb_module_add_to_subclasses_list(klass,
RARRAY_AREF(new_origins, i));
2087 VALUE origin = RCLASS_ORIGIN(mod);
2091 VALUE m = METACLASS_OF(p);
2126 if (METACLASS_OF(p) == mod2)
return Qtrue;
2160 refined_class = rb_refinement_module_get_refined_class(mod);
2164 if (p == refined_class)
break;
2165 if (p != RCLASS_ORIGIN(p))
continue;
2181 bool immediate_only;
2185class_descendants_recursive(
VALUE klass,
VALUE v)
2191 if (!RCLASS_SINGLETON_P(klass)) {
2192 if (data->buffer && data->count < data->maxcount && !rb_objspace_garbage_object_p(klass)) {
2197 if (data->immediate_only)
return;
2199 rb_class_foreach_subclass(klass, class_descendants_recursive, v);
2203class_descendants(
VALUE klass,
bool immediate_only)
2208 rb_class_foreach_subclass(klass, class_descendants_recursive, (
VALUE) &data);
2212 data.maxcount = data.count;
2215 size_t gc_count = rb_gc_count();
2218 rb_class_foreach_subclass(klass, class_descendants_recursive, (
VALUE) &data);
2220 if (gc_count != rb_gc_count()) {
2221 rb_bug(
"GC must not occur during the subclass iteration of Class#descendants");
2269 return class_descendants(klass,
true);
2280static void module_descendants_recursive(
VALUE entry,
VALUE v);
2286 if (data->buffer && data->count >= data->maxcount)
return;
2288 if (st_insert(data->visited, (st_data_t)klass, 1))
return;
2295 rb_class_foreach_subclass(klass, module_descendants_recursive, (
VALUE)data);
2301iclass_in_ancestors_p(
VALUE iclass,
VALUE includer)
2304 if (p == iclass)
return true;
2310module_descendants_recursive(
VALUE entry,
VALUE v)
2314 if (rb_objspace_garbage_object_p(entry))
return;
2319 VALUE includer = RCLASS_INCLUDER(entry);
2321 includer = RCLASS_INCLUDER(includer);
2323 if (!includer || UNDEF_P(includer))
return;
2324 if (rb_objspace_garbage_object_p(includer))
return;
2325 if (RCLASS_SINGLETON_P(includer))
return;
2326 if (!iclass_in_ancestors_p(entry, includer))
return;
2327 module_descendants_add(includer, data);
2330 if (RCLASS_SINGLETON_P(entry))
return;
2331 module_descendants_add(entry, data);
2363 data.visited = st_init_numtable();
2364 st_insert(data.visited, (st_data_t)mod, 1);
2365 rb_class_foreach_subclass(mod, module_descendants_recursive, (
VALUE)&data);
2366 st_free_table(data.visited);
2370 data.maxcount = data.count;
2373 data.visited = st_init_numtable_with_size(data.maxcount + 1);
2374 st_insert(data.visited, (st_data_t)mod, 1);
2376 size_t gc_count = rb_gc_count();
2378 rb_class_foreach_subclass(mod, module_descendants_recursive, (
VALUE)&data);
2380 if (gc_count != rb_gc_count()) {
2381 rb_bug(
"GC must not occur during the subclass iteration of Module#descendants");
2383 st_free_table(data.visited);
2411 if (!RCLASS_SINGLETON_P(klass)) {
2412 rb_raise(
rb_eTypeError,
"'%"PRIsVALUE
"' is not a singleton class", klass);
2415 const VALUE obj = RCLASS_ATTACHED_OBJECT(klass);
2422 rb_raise(rb_eRactorIsolationError,
2423 "can not get an unshareable attached object from another Ractor");
2430ins_methods_push(st_data_t name, st_data_t ary)
2436ins_methods_i(st_data_t name, st_data_t
type, st_data_t ary)
2438 switch ((rb_method_visibility_t)
type) {
2439 case METHOD_VISI_UNDEF:
2440 case METHOD_VISI_PRIVATE:
2443 ins_methods_push(name, ary);
2450ins_methods_type_i(st_data_t name, st_data_t
type, st_data_t ary, rb_method_visibility_t visi)
2452 if ((rb_method_visibility_t)
type == visi) {
2453 ins_methods_push(name, ary);
2459ins_methods_prot_i(st_data_t name, st_data_t
type, st_data_t ary)
2461 return ins_methods_type_i(name,
type, ary, METHOD_VISI_PROTECTED);
2465ins_methods_priv_i(st_data_t name, st_data_t
type, st_data_t ary)
2467 return ins_methods_type_i(name,
type, ary, METHOD_VISI_PRIVATE);
2471ins_methods_pub_i(st_data_t name, st_data_t
type, st_data_t ary)
2473 return ins_methods_type_i(name,
type, ary, METHOD_VISI_PUBLIC);
2477ins_methods_undef_i(st_data_t name, st_data_t
type, st_data_t ary)
2479 return ins_methods_type_i(name,
type, ary, METHOD_VISI_UNDEF);
2487static enum rb_id_table_iterator_result
2488method_entry_i(
ID key,
VALUE value,
void *data)
2492 rb_method_visibility_t
type;
2494 if (me->def->type == VM_METHOD_TYPE_REFINED) {
2495 VALUE owner = me->owner;
2496 me = rb_resolve_refined_method(
Qnil, me);
2497 if (!me)
return ID_TABLE_CONTINUE;
2498 if (!arg->recur && me->owner != owner)
return ID_TABLE_CONTINUE;
2500 if (!st_is_member(arg->list, key)) {
2501 if (UNDEFINED_METHOD_ENTRY_P(me)) {
2502 type = METHOD_VISI_UNDEF;
2505 type = METHOD_ENTRY_VISI(me);
2508 st_add_direct(arg->list, key, (st_data_t)
type);
2510 return ID_TABLE_CONTINUE;
2518 rb_id_table_foreach(m_tbl, method_entry_i, me_arg);
2522particular_class_p(
VALUE mod)
2524 if (!mod)
return false;
2525 if (RCLASS_SINGLETON_P(mod))
return true;
2531class_instance_method_list(
int argc,
const VALUE *argv,
VALUE mod,
int obj,
int (*func) (st_data_t, st_data_t, st_data_t))
2534 int recur = TRUE, prepended = 0;
2539 me_arg.list = st_init_numtable();
2540 me_arg.recur = recur;
2543 for (; particular_class_p(mod); mod =
RCLASS_SUPER(mod)) {
2544 add_instance_method_list(mod, &me_arg);
2548 if (!recur && RCLASS_ORIGIN(mod) != mod) {
2549 mod = RCLASS_ORIGIN(mod);
2554 add_instance_method_list(mod, &me_arg);
2559 st_foreach(me_arg.list, func, ary);
2560 st_free_table(me_arg.list);
2604 return class_instance_method_list(argc, argv, mod, 0, ins_methods_i);
2619 return class_instance_method_list(argc, argv, mod, 0, ins_methods_prot_i);
2642 return class_instance_method_list(argc, argv, mod, 0, ins_methods_priv_i);
2657 return class_instance_method_list(argc, argv, mod, 0, ins_methods_pub_i);
2669rb_class_undefined_instance_methods(
VALUE mod)
2672 return class_instance_method_list(1, &include_super, mod, 0, ins_methods_undef_i);
2706rb_obj_methods(
int argc,
const VALUE *argv,
VALUE obj)
2709 if (argc > 0 && !
RTEST(argv[0])) {
2712 return class_instance_method_list(argc, argv,
CLASS_OF(obj), 1, ins_methods_i);
2725rb_obj_protected_methods(
int argc,
const VALUE *argv,
VALUE obj)
2727 return class_instance_method_list(argc, argv,
CLASS_OF(obj), 1, ins_methods_prot_i);
2740rb_obj_private_methods(
int argc,
const VALUE *argv,
VALUE obj)
2742 return class_instance_method_list(argc, argv,
CLASS_OF(obj), 1, ins_methods_priv_i);
2755rb_obj_public_methods(
int argc,
const VALUE *argv,
VALUE obj)
2757 return class_instance_method_list(argc, argv,
CLASS_OF(obj), 1, ins_methods_pub_i);
2796 VALUE ary, klass, origin;
2806 origin = RCLASS_ORIGIN(klass);
2807 me_arg.list = st_init_numtable();
2808 me_arg.recur = recur;
2809 if (klass && RCLASS_SINGLETON_P(klass)) {
2810 if ((mtbl = RCLASS_M_TBL(origin)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2815 if (klass != origin && (mtbl = RCLASS_M_TBL(klass)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2820 st_foreach(me_arg.list, ins_methods_i, ary);
2821 st_free_table(me_arg.list);
2834#ifdef rb_define_method_id
2835#undef rb_define_method_id
2840 rb_add_method_cfunc(klass, mid, func, argc, METHOD_VISI_PUBLIC);
2843#ifdef rb_define_method
2844#undef rb_define_method
2849 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PUBLIC);
2852#ifdef rb_define_protected_method
2853#undef rb_define_protected_method
2858 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PROTECTED);
2861#ifdef rb_define_private_method
2862#undef rb_define_private_method
2867 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PRIVATE);
2873 rb_add_method(klass, rb_intern(name), VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2876static enum rb_id_table_iterator_result
2877undef_method_i(
ID name,
VALUE value,
void *data)
2880 rb_add_method(klass, name, VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2881 return ID_TABLE_CONTINUE;
2885rb_undef_methods_from(
VALUE klass,
VALUE super)
2889 rb_id_table_foreach(mtbl, undef_method_i, (
void *)klass);
2902special_singleton_class_of(
VALUE obj)
2908 default:
return Qnil;
2913rb_special_singleton_class(
VALUE obj)
2915 return special_singleton_class_of(obj);
2928singleton_class_of(
VALUE obj,
bool ensure_eigenclass)
2932 switch (
TYPE(obj)) {
2942 klass = special_singleton_class_of(obj);
2944 rb_bug(
"unknown immediate %p", (
void *)obj);
2948 if (CHILLED_STRING_P(obj)) {
2949 CHILLED_STRING_MUTATED(obj);
2959 RB_VM_LOCK_ENTER_LEV(&lev);
2962 klass = METACLASS_OF(obj);
2963 if (!(RCLASS_SINGLETON_P(klass) &&
2964 RCLASS_ATTACHED_OBJECT(klass) == obj)) {
2965 klass = rb_make_metaclass(obj, klass);
2978 RB_VM_LOCK_LEAVE_LEV(&lev);
2984#if RUBY_VERSION_SINCE(4, 2)
2985RBIMPL_TODO(
"make rb_freeze_singleton_class internal; remove from fl_type.h")
2997 (RCLASS_ATTACHED_OBJECT(klass) == attached_object)) {
2998 attached_object = klass;
3016 return rb_special_singleton_class(obj);
3018 klass = METACLASS_OF(obj);
3019 if (!RCLASS_SINGLETON_P(klass))
return Qnil;
3020 if (RCLASS_ATTACHED_OBJECT(klass) != obj)
return Qnil;
3027 return singleton_class_of(obj,
true);
3039#ifdef rb_define_singleton_method
3040#undef rb_define_singleton_method
3048#ifdef rb_define_module_function
3049#undef rb_define_module_function
3058#ifdef rb_define_global_function
3059#undef rb_define_global_function
3070 rb_alias(klass, rb_intern(name1), rb_intern(name2));
3076 rb_attr(klass, rb_intern(name), read, write, FALSE);
3080rb_keyword_error_new(
const char *error,
VALUE keys)
3083 VALUE error_message = rb_sprintf(
"%s keyword%.*s", error,
len > 1,
"s");
3090 if (++i >=
len)
break;
3098NORETURN(
static void rb_keyword_error(
const char *error,
VALUE keys));
3100rb_keyword_error(
const char *error,
VALUE keys)
3105NORETURN(
static void unknown_keyword_error(
VALUE hash,
const ID *table,
int keywords));
3107unknown_keyword_error(
VALUE hash,
const ID *table,
int keywords)
3110 for (i = 0; i < keywords; i++) {
3111 st_data_t key =
ID2SYM(table[i]);
3112 rb_hash_stlike_delete(hash, &key, NULL);
3114 rb_keyword_error(
"unknown", rb_hash_keys(hash));
3119separate_symbol(st_data_t key, st_data_t value, st_data_t arg)
3123 if (!*kwdhash) *kwdhash = rb_hash_new();
3124 rb_hash_aset(*kwdhash, (
VALUE)key, (
VALUE)value);
3131 VALUE parthash[2] = {0, 0};
3132 VALUE hash = *orighash;
3139 *orighash = parthash[1];
3154#define extract_kwarg(keyword, val) \
3155 (key = (st_data_t)(keyword), values ? \
3156 (rb_hash_stlike_delete(keyword_hash, &key, &(val)) || ((val) = Qundef, 0)) : \
3157 rb_hash_stlike_lookup(keyword_hash, key, NULL))
3159 if (
NIL_P(keyword_hash)) keyword_hash = 0;
3163 optional = -1-optional;
3166 for (; i < required; i++) {
3169 if (extract_kwarg(keyword, values[i])) {
3176 if (!
NIL_P(missing)) {
3177 rb_keyword_error(
"missing", missing);
3181 if (optional && keyword_hash) {
3182 for (i = 0; i < optional; i++) {
3183 if (extract_kwarg(
ID2SYM(table[required+i]), values[required+i])) {
3188 if (!rest && keyword_hash) {
3189 if (
RHASH_SIZE(keyword_hash) > (
unsigned int)(values ? 0 : j)) {
3190 unknown_keyword_error(keyword_hash, table, required+optional);
3193 if (values && !keyword_hash) {
3194 for (i = 0; i < required + optional; i++) {
3213rb_scan_args_parse(
int kw_flag,
const char *fmt,
struct rb_scan_args_t *arg)
3215 const char *p = fmt;
3217 memset(arg, 0,
sizeof(*arg));
3218 arg->kw_flag = kw_flag;
3221 arg->n_lead = *p -
'0';
3224 arg->n_opt = *p -
'0';
3233 arg->n_trail = *p -
'0';
3245 rb_fatal(
"bad scan arg format: %s", fmt);
3250rb_scan_args_assign(
const struct rb_scan_args_t *arg,
int argc,
const VALUE *
const argv, va_list vargs)
3254#define rb_scan_args_next_param() va_arg(vargs, VALUE *)
3255 const int kw_flag = arg->kw_flag;
3256 const int n_lead = arg->n_lead;
3257 const int n_opt = arg->n_opt;
3258 const int n_trail = arg->n_trail;
3259 const int n_mand = n_lead + n_trail;
3260 const bool f_var = arg->f_var;
3261 const bool f_hash = arg->f_hash;
3262 const bool f_block = arg->f_block;
3265 if (f_hash && argc > 0) {
3266 VALUE last = argv[argc - 1];
3267 if (rb_scan_args_keyword_p(kw_flag, last)) {
3268 hash = rb_hash_dup(last);
3273 if (argc < n_mand) {
3278 for (i = 0; i < n_lead; i++) {
3279 var = rb_scan_args_next_param();
3280 if (var) *var = argv[argi];
3284 for (i = 0; i < n_opt; i++) {
3285 var = rb_scan_args_next_param();
3286 if (argi < argc - n_trail) {
3287 if (var) *var = argv[argi];
3291 if (var) *var =
Qnil;
3296 int n_var = argc - argi - n_trail;
3298 var = rb_scan_args_next_param();
3308 for (i = 0; i < n_trail; i++) {
3309 var = rb_scan_args_next_param();
3310 if (var) *var = argv[argi];
3315 var = rb_scan_args_next_param();
3316 if (var) *var = hash;
3320 var = rb_scan_args_next_param();
3335#undef rb_scan_args_next_param
3339rb_scan_args_result(
const struct rb_scan_args_t *
const arg,
int argc)
3341 const int n_lead = arg->n_lead;
3342 const int n_opt = arg->n_opt;
3343 const int n_trail = arg->n_trail;
3344 const int n_mand = n_lead + n_trail;
3345 const bool f_var = arg->f_var;
3363 va_start(vargs,fmt);
3364 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
3366 return rb_scan_args_result(&arg, argc);
3369#undef rb_scan_args_kw
3375 rb_scan_args_parse(kw_flag, fmt, &arg);
3376 va_start(vargs,fmt);
3377 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
3379 return rb_scan_args_result(&arg, argc);
#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().
void rb_obj_freeze_inline(VALUE obj)
Prevents further modifications to the given object.
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.
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
VALUE rb_ary_new_capa(rb_len_t capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_resize(VALUE ary, rb_len_t len)
Expands or shrinks the passed array to the passed length.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_hidden_new(rb_len_t 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.
VALUE rb_ary_cat(VALUE ary, const VALUE *train, rb_len_t len)
Destructively appends multiple elements at the end of the array.
VALUE rb_ary_new_from_values(rb_len_t n, const VALUE *elts)
Identical to rb_ary_new_from_args(), except how objects are passed.
#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)
References an object in an array.
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.
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
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.