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)
88rb_class_unlink_classext(
VALUE klass,
const rb_box_t *box)
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);
94 return (rb_classext_t *)ext;
98rb_class_classext_free(
VALUE klass, rb_classext_t *ext,
bool is_prime)
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);
123rb_iclass_classext_free(
VALUE klass, rb_classext_t *ext,
bool is_prime)
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));
140iclass_free_orphan_classext(
VALUE klass, rb_classext_t *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;
179rb_class_set_box_classext(
VALUE obj,
const rb_box_t *box, rb_classext_t *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)
209 rb_method_entry_t *me = (rb_method_entry_t *)value;
210 rb_method_table_insert0(arg->klass, arg->tbl, key, me,
false);
211 return ID_TABLE_CONTINUE;
218duplicate_classext_m_tbl(rb_classext_t *ext,
struct rb_id_table *orig,
VALUE klass,
bool init_missing)
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);
233static rb_const_entry_t *
234duplicate_classext_const_entry(rb_const_entry_t *src,
VALUE klass)
237 rb_const_entry_t *dst =
ZALLOC(rb_const_entry_t);
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)
251 rb_const_entry_t *entry = duplicate_classext_const_entry((rb_const_entry_t *)value, arg->klass);
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);
278class_duplicate_iclass_classext(
VALUE iclass, rb_classext_t *mod_ext,
const rb_box_t *box)
282 rb_classext_t *src = RCLASS_EXT_PRIME(iclass);
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));
291 ext =
ZALLOC(rb_classext_t);
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);
329rb_class_duplicate_classext(rb_classext_t *orig,
VALUE klass,
const rb_box_t *box,
int *first_set)
333 rb_classext_t *ext =
ZALLOC(rb_classext_t);
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) {
591 if (struct_inline_field) alloc_size +=
sizeof(
VALUE);
596 if (boxable) flags |= RCLASS_BOXABLE;
598 shape_id_t shape_id = ROOT_SHAPE_ID;
600 shape_id |= SHAPE_ID_LAYOUT_OTHER;
603 shape_id |= SHAPE_ID_LAYOUT_RCLASS;
606 struct RClass *obj = (
struct RClass *)rb_newobj(GET_EC(), klass, flags, shape_id,
true, alloc_size);
610 memset(RCLASS_EXT_PRIME(obj), 0,
sizeof(rb_classext_t));
614 if (
type !=
T_ICLASS && UNLIKELY(!rb_ractor_main_p())) {
615 RCLASS_SET_OWNER_RACTOR_ID((
VALUE)obj, rb_ractor_id(GET_RACTOR()));
629 RCLASS_EXT_PRIME(obj)->has_struct_inline_field = struct_inline_field;
630 if (struct_inline_field) *RCLASS_STRUCT_INLINE_FIELD_PTR((
VALUE)obj) =
Qnil;
632 RCLASS_PRIME_BOX((
VALUE)obj) = box;
635 RCLASS_SET_PRIME_CLASSEXT_WRITABLE((
VALUE)obj, !boxable || BOX_USER_P(box));
638 RCLASS_SET_REFINED_CLASS((
VALUE)obj,
Qnil);
646 bool boxable = rb_box_available() && BOX_MASTER_P(rb_current_box());
651rb_class_owned_by_ractor_p(rb_serial_t owner_id)
653 return owner_id == rb_ractor_id(GET_RACTOR());
660class_owner_name(
VALUE klass)
664 VALUE obj = RCLASS_ATTACHED_OBJECT(klass);
670class_own_metaclass(
VALUE klass)
672 VALUE meta = METACLASS_OF(klass);
673 return (RCLASS_SINGLETON_P(meta) && RCLASS_ATTACHED_OBJECT(meta) == klass) ? meta : 0;
679rb_class_take_ownership(
VALUE klass)
682 rb_serial_t
id = rb_ractor_main_p() ? 0 : rb_ractor_id(GET_RACTOR());
686 RCLASS_SET_OWNER_RACTOR_ID(klass,
id);
687 }
while ((klass = class_own_metaclass(klass)) != 0);
691rb_class_owner_check(
VALUE klass)
693 if (UNLIKELY(!rb_class_owned_p(klass))) {
694 rb_raise(rb_eRactorIsolationError,
695 "can not modify %"PRIsVALUE
" because it is created by another Ractor",
696 class_owner_name(klass));
701class_associate_super(
VALUE klass,
VALUE super,
bool init)
703 if (super && !UNDEF_P(super)) {
708 if (RCLASS_SINGLETON_P(klass)) {
715 class_switch_superclass(super, klass);
720 RCLASS_SET_SUPER(klass, super);
723 RCLASS_WRITE_SUPER(klass, super);
725 rb_class_update_superclasses(klass);
732 return class_associate_super(klass, super,
false);
736class_initialize_method_table(
VALUE c)
739 RCLASS_SET_M_TBL(c, rb_id_table_create(0));
743class_clear_method_table(
VALUE c)
745 RCLASS_WRITE_M_TBL(c, rb_id_table_create(0));
749class_boot_boxable(
VALUE super,
bool boxable,
bool struct_inline_field)
755 class_initialize_method_table(klass);
757 class_associate_super(klass, super,
true);
758 if (super && !UNDEF_P(super)) {
759 RCLASS_SET_ALLOCATOR(klass, RCLASS_ALLOCATOR(super));
760 rb_class_set_initialized(klass);
778 return class_boot_boxable(super,
false,
false);
782class_superclasses_including_self(
VALUE klass)
784 if (RCLASS_SUPERCLASSES_WITH_SELF_P(klass))
785 return RCLASS_SUPERCLASSES(klass);
787 size_t depth = RCLASS_SUPERCLASS_DEPTH(klass);
790 memcpy(superclasses, RCLASS_SUPERCLASSES(klass),
sizeof(
VALUE) * depth);
791 superclasses[depth] = klass;
797rb_class_update_superclasses(
VALUE klass)
804 if (UNDEF_P(super))
return;
807 if (RCLASS_SUPERCLASSES(klass))
822 rb_class_update_superclasses(super);
825 if (!RCLASS_SUPERCLASS_DEPTH(super))
829 super_depth = RCLASS_SUPERCLASS_DEPTH(super);
830 if (RCLASS_SUPERCLASSES_WITH_SELF_P(super)) {
831 superclasses = RCLASS_SUPERCLASSES(super);
834 superclasses = class_superclasses_including_self(super);
835 RCLASS_WRITE_SUPERCLASSES(super, super_depth, superclasses,
true);
838 size_t depth = super_depth == RCLASS_MAX_SUPERCLASS_DEPTH ? super_depth : super_depth + 1;
839 RCLASS_WRITE_SUPERCLASSES(klass, depth, superclasses,
false);
846 rb_raise(
rb_eTypeError,
"superclass must be an instance of Class (given an instance of %"PRIsVALUE
")",
849 if (RCLASS_SINGLETON_P(super)) {
850 rb_raise(
rb_eTypeError,
"can't make subclass of singleton class");
858class_copy_struct_inline_field(
VALUE klass,
VALUE from)
860 if (RCLASS_HAS_STRUCT_INLINE_FIELD(from)) {
861 RCLASS_SET_STRUCT_INLINE_FIELD(klass, RCLASS_STRUCT_INLINE_FIELD(from));
868class_new(
VALUE super,
bool struct_inline_field)
872 VALUE klass = class_boot_boxable(super,
false, struct_inline_field || RCLASS_HAS_STRUCT_INLINE_FIELD(super));
873 class_copy_struct_inline_field(klass, super);
875 RCLASS_SET_MAX_IV_COUNT(klass, RCLASS_MAX_IV_COUNT(super));
876 RUBY_ASSERT(getenv(
"RUBY_BOX") || RCLASS_PRIME_CLASSEXT_WRITABLE_P(klass));
884 return class_new(super,
false);
888rb_class_new_with_struct_inline_field(
VALUE super)
890 return class_new(super,
true);
896rb_class_alloc_with_struct_inline_field_of(
VALUE from)
898 VALUE klass = class_boot_boxable(0,
false, RCLASS_HAS_STRUCT_INLINE_FIELD(from));
899 class_copy_struct_inline_field(klass, from);
904clone_method(
VALUE new_klass,
ID mid,
const rb_method_entry_t *me)
906 rb_method_entry_set(new_klass, mid, me, METHOD_ENTRY_VISI(me));
913static enum rb_id_table_iterator_result
914clone_method_i(
ID key,
VALUE value,
void *data)
917 clone_method(arg->new_klass, key, (
const rb_method_entry_t *)value);
918 return ID_TABLE_CONTINUE;
929 rb_const_entry_t *nce =
ALLOC(rb_const_entry_t);
930 MEMCPY(nce, ce, rb_const_entry_t, 1);
934 rb_id_table_insert(arg->tbl, key, (
VALUE)nce);
935 return ID_TABLE_CONTINUE;
938static enum rb_id_table_iterator_result
939clone_const_i(
ID key,
VALUE value,
void *data)
941 return clone_const(key, (
const rb_const_entry_t *)value, data);
951 if (RCLASS_SINGLETON_P(orig)) {
962cvc_table_entry_alloc(
void)
967static enum rb_id_table_iterator_result
968cvc_table_copy(
ID id,
VALUE val,
void *data)
976 ent = cvc_table_entry_alloc();
979 ent->global_cvar_state = orig_entry->global_cvar_state;
980 rb_marked_id_table_insert(ctx->new_table,
id, (
VALUE)ent);
982 return ID_TABLE_CONTINUE;
988 if (RCLASS_CONST_TBL(clone)) {
989 rb_free_const_table(RCLASS_CONST_TBL(clone));
990 RCLASS_WRITE_CONST_TBL(clone, 0,
false);
992 if (RCLASS_CVC_TBL(orig)) {
993 VALUE rb_cvc_tbl = RCLASS_CVC_TBL(orig);
994 VALUE rb_cvc_tbl_dup = rb_marked_id_table_new(rb_marked_id_table_size(rb_cvc_tbl));
998 ctx.new_table = rb_cvc_tbl_dup;
999 rb_marked_id_table_foreach(rb_cvc_tbl, cvc_table_copy, &ctx);
1000 RCLASS_WRITE_CVC_TBL(clone, rb_cvc_tbl_dup);
1002 rb_id_table_free(RCLASS_M_TBL(clone));
1003 RCLASS_WRITE_M_TBL(clone, 0);
1005 rb_fields_tbl_copy(clone, orig);
1007 if (RCLASS_CONST_TBL(orig)) {
1010 struct rb_id_table *orig_tbl = RCLASS_CONST_TBL(orig);
1013 arg.tbl = const_tbl = rb_id_table_create(rb_id_table_size(orig_tbl));
1014 rb_id_table_foreach(orig_tbl, clone_const_i, &arg);
1016 RCLASS_WRITE_CONST_TBL(clone, const_tbl,
false);
1017 rb_gc_writebarrier_remember(clone);
1021static bool ensure_origin(
VALUE klass);
1024rb_class_set_initialized(
VALUE klass)
1032rb_module_check_initializable(
VALUE mod)
1034 if (RCLASS_INITIALIZED_P(mod)) {
1039static enum rb_id_table_iterator_result
1040move_check_const_i(
ID id,
VALUE v,
void *data)
1042 const rb_const_entry_t *ce = (
const rb_const_entry_t *)v;
1044 rb_raise(rb_eRactorIsolationError,
1045 "can not move an object whose singleton class has constant %"PRIsVALUE
1046 " referring to an unshareable object", rb_id2str(
id));
1048 return ID_TABLE_CONTINUE;
1052move_check_field_i(
ID id,
VALUE val, st_data_t arg)
1055 rb_raise(rb_eRactorIsolationError,
1056 "can not move an object whose singleton class has variable %"PRIsVALUE
1057 " referring to an unshareable object", rb_id2str(
id));
1065rb_class_check_singleton_movable(
VALUE klass)
1068 if (RCLASS_CONST_TBL(klass)) {
1069 rb_id_table_foreach(RCLASS_CONST_TBL(klass), move_check_const_i, NULL);
1071 rb_ivar_foreach_buffered(klass, move_check_field_i, 0);
1072 }
while ((klass = class_own_metaclass(klass)) != 0);
1077init_copy_owner_check(
VALUE orig)
1079 VALUE meta = METACLASS_OF(orig);
1080 if (!rb_class_owned_p(orig) || (RCLASS_SINGLETON_P(meta) && !rb_class_owned_p(meta))) {
1081 rb_raise(rb_eRactorIsolationError,
"can not copy a class/module created by another Ractor");
1094 class_init_copy_check(clone, orig);
1097 rb_module_check_initializable(clone);
1107 init_copy_owner_check(orig);
1109 if (RCLASS_INITIALIZED_P(orig)) {
1110 rb_class_set_initialized(clone);
1113 if (!RCLASS_SINGLETON_P(
CLASS_OF(clone))) {
1118 RCLASS_SET_ALLOCATOR(clone, RCLASS_ALLOCATOR(orig));
1119 if (RCLASS_HAS_STRUCT_INLINE_FIELD(orig) && !RCLASS_HAS_STRUCT_INLINE_FIELD(clone)) {
1121 rb_raise(
rb_eTypeError,
"can't copy %"PRIsVALUE
" into an allocated class", orig);
1123 class_copy_struct_inline_field(clone, orig);
1125 copy_tables(clone, orig);
1126 if (RCLASS_M_TBL(orig)) {
1128 arg.new_klass = clone;
1129 class_initialize_method_table(clone);
1130 rb_id_table_foreach(RCLASS_M_TBL(orig), clone_method_i, &arg);
1133 if (RCLASS_ORIGIN(orig) == orig) {
1138 VALUE orig_origin = RCLASS_ORIGIN(orig);
1139 VALUE prev_clone_p = clone;
1147 ensure_origin(clone);
1148 clone_origin = RCLASS_ORIGIN(clone);
1150 while (p && p != orig_origin) {
1152 rb_bug(
"non iclass between module/class and origin");
1154 clone_p = class_alloc(
T_ICLASS, METACLASS_OF(p));
1155 RCLASS_SET_M_TBL(clone_p, RCLASS_M_TBL(p));
1156 rb_class_set_super(prev_clone_p, clone_p);
1157 prev_clone_p = clone_p;
1158 RCLASS_SET_CONST_TBL(clone_p, RCLASS_CONST_TBL(p),
false);
1159 RCLASS_SET_INCLUDER(clone_p, clone);
1160 add_subclass = TRUE;
1161 if (p != RCLASS_ORIGIN(p)) {
1162 origin[0] = clone_p;
1163 origin[1] = RCLASS_ORIGIN(p);
1166 else if ((origin_len =
RARRAY_LEN(origin_stack)) > 1 &&
1168 RCLASS_WRITE_ORIGIN(
RARRAY_AREF(origin_stack, (origin_len -= 2)), clone_p);
1169 RICLASS_WRITE_ORIGIN_SHARED_MTBL(clone_p);
1171 add_subclass = FALSE;
1174 rb_module_add_to_subclasses_list(METACLASS_OF(p), clone_p);
1179 if (p == orig_origin) {
1181 rb_class_set_super(clone_p, clone_origin);
1182 rb_class_set_super(clone_origin,
RCLASS_SUPER(orig_origin));
1184 copy_tables(clone_origin, orig_origin);
1185 if (RCLASS_M_TBL(orig_origin)) {
1187 arg.new_klass = clone;
1188 class_initialize_method_table(clone_origin);
1189 rb_id_table_foreach(RCLASS_M_TBL(orig_origin), clone_method_i, &arg);
1193 rb_bug(
"no origin for class that has origin");
1196 rb_class_update_superclasses(clone);
1212 return rb_singleton_class_clone_and_attach(obj,
Qundef);
1217rb_singleton_class_clone_and_attach(
VALUE obj,
VALUE attach)
1219 const VALUE klass = METACLASS_OF(obj);
1225 if (!(RCLASS_SINGLETON_P(klass) && RCLASS_ATTACHED_OBJECT(klass) == obj)) {
1231 bool klass_of_clone_is_new;
1236 klass_of_clone_is_new =
true;
1237 RBASIC_SET_CLASS(clone, clone);
1243 klass_of_clone_is_new = (METACLASS_OF(klass) != klass_metaclass_clone);
1244 RBASIC_SET_CLASS(clone, klass_metaclass_clone);
1248 class_initialize_method_table(clone);
1251 rb_fields_tbl_copy(clone, klass);
1252 if (RCLASS_CONST_TBL(klass)) {
1257 arg.tbl = table = rb_id_table_create(rb_id_table_size(RCLASS_CONST_TBL(klass)));
1258 rb_id_table_foreach(RCLASS_CONST_TBL(klass), clone_const_i, &arg);
1260 RCLASS_SET_CONST_TBL(clone, table,
false);
1262 if (!UNDEF_P(attach)) {
1267 arg.new_klass = clone;
1268 rb_id_table_foreach(RCLASS_M_TBL(klass), clone_method_i, &arg);
1270 if (klass_of_clone_is_new) {
1282 if (RCLASS_SINGLETON_P(klass)) {
1283 RCLASS_SET_ATTACHED_OBJECT(klass, obj);
1292#define META_CLASS_OF_CLASS_CLASS_P(k) (METACLASS_OF(k) == (k))
1295rb_singleton_class_has_metaclass_p(
VALUE sklass)
1297 return RCLASS_ATTACHED_OBJECT(METACLASS_OF(sklass)) == sklass;
1301rb_singleton_class_internal_p(
VALUE sklass)
1304 !rb_singleton_class_has_metaclass_p(sklass));
1312#define HAVE_METACLASS_P(k) \
1313 (FL_TEST(METACLASS_OF(k), FL_SINGLETON) && \
1314 rb_singleton_class_has_metaclass_p(k))
1323#define ENSURE_EIGENCLASS(klass) \
1324 (HAVE_METACLASS_P(klass) ? METACLASS_OF(klass) : make_metaclass(klass))
1344 RCLASS_SET_OWNER_RACTOR_ID(metaclass, RCLASS_OWNER_RACTOR_ID(klass));
1348 SET_METACLASS_OF(klass, metaclass);
1349 SET_METACLASS_OF(metaclass, metaclass);
1352 VALUE tmp = METACLASS_OF(klass);
1353 SET_METACLASS_OF(klass, metaclass);
1360 rb_class_set_initialized(metaclass);
1363 rb_class_update_superclasses(METACLASS_OF(metaclass));
1377 VALUE orig_class = METACLASS_OF(obj);
1382 RCLASS_SET_OWNER_RACTOR_ID(klass, RCLASS_OWNER_RACTOR_ID(obj));
1387 RCLASS_SET_OWNER_RACTOR_ID(klass, 0);
1389 class_initialize_method_table(klass);
1390 class_associate_super(klass, orig_class,
true);
1391 if (orig_class && !UNDEF_P(orig_class)) {
1392 rb_class_set_initialized(klass);
1395 RBASIC_SET_CLASS(obj, klass);
1397 rb_yjit_invalidate_no_singleton_class(orig_class);
1398 rb_zjit_invalidate_no_singleton_class(orig_class);
1400 SET_METACLASS_OF(klass, METACLASS_OF(
rb_class_real(orig_class)));
1406boot_defclass(
const char *name,
VALUE super)
1409 ID id = rb_intern(name);
1412 rb_vm_register_global_object(obj);
1463refinement_import_methods(
int argc,
VALUE *argv,
VALUE refinement)
1550 rb_make_metaclass(klass, METACLASS_OF(super));
1570 return rb_funcallv_uncached(super, inherited, 1, &klass);
1573#ifdef rb_define_class
1574#undef rb_define_class
1582#ifdef rb_define_class_under
1583#undef rb_define_class_under
1592rb_define_class_id_under_no_pin(
VALUE outer,
ID id,
VALUE super)
1604 rb_raise(
rb_eTypeError,
"%"PRIsVALUE
"::%"PRIsVALUE
" is not a class"
1611 rb_raise(
rb_eTypeError,
"superclass mismatch for class %s", rb_id2name(
id));
1615 "%"PRIsVALUE
"::%"PRIsVALUE
""
1616 " (%"PRIsVALUE
" is given but was %"PRIsVALUE
")",
1625 rb_raise(rb_eArgError,
"no super class for '%"PRIsVALUE
"'", rb_id2str(
id));
1628 rb_raise(rb_eArgError,
"no super class for '%"PRIsVALUE
"::%"PRIsVALUE
"'",
1643 VALUE klass = rb_define_class_id_under_no_pin(outer,
id, super);
1644 rb_vm_register_global_object(klass);
1649rb_module_s_alloc(
VALUE klass)
1652 class_initialize_method_table(mod);
1657module_new(
VALUE klass)
1660 class_initialize_method_table(mdl);
1683#ifdef rb_define_module
1684#undef rb_define_module
1692#ifdef rb_define_module_under
1693#undef rb_define_module_under
1707 module = rb_const_get_at(outer, id);
1710 rb_raise(
rb_eTypeError,
"%s is not a module (%"PRIsVALUE
")",
1714 rb_raise(
rb_eTypeError,
"%"PRIsVALUE
"::%"PRIsVALUE
" is not a module"
1720 rb_vm_register_global_object(module);
1723 module = rb_module_new();
1726 rb_vm_register_global_object(module);
1732rb_include_class_new(
VALUE module,
VALUE super)
1736 RCLASS_SET_M_TBL(klass, RCLASS_WRITABLE_M_TBL(module));
1738 RCLASS_SET_ORIGIN(klass, klass);
1740 module = METACLASS_OF(module);
1743 if (RCLASS_WRITABLE_CONST_TBL(module)) {
1744 RCLASS_SET_CONST_TBL(klass, RCLASS_WRITABLE_CONST_TBL(module),
true);
1747 RCLASS_WRITE_CONST_TBL(module, rb_id_table_create(0),
false);
1748 RCLASS_SET_CONST_TBL(klass, RCLASS_WRITABLE_CONST_TBL(module),
true);
1751 RCLASS_SET_CVC_TBL(klass, RCLASS_WRITABLE_CVC_TBL(module));
1753 class_associate_super(klass, super,
true);
1754 RBASIC_SET_CLASS(klass, module);
1756 return (
VALUE)klass;
1759static int include_modules_at(
const VALUE klass,
VALUE c,
VALUE module,
int search_super);
1766 rb_class_set_initialized(module);
1767 if (!
NIL_P(rb_refinement_module_get_refined_class(module))) {
1768 rb_raise(rb_eArgError,
"refinement module is not allowed");
1777 ensure_includable(klass, module);
1779 changed = include_modules_at(klass, RCLASS_ORIGIN(klass), module, TRUE);
1781 rb_raise(rb_eArgError,
"cyclic include detected");
1784 VALUE subs_v = RCLASS_SUBCLASSES(klass);
1787 VALUE *entries = rb_imemo_subclasses_entries(subs_v);
1788 for (uint32_t i = 0; i < subs->count; i++) {
1789 VALUE check_class = entries[i];
1790 if (!check_class)
continue;
1795 if (!rb_objspace_garbage_object_p(check_class)) {
1796 VALUE walk = check_class;
1801 (METACLASS_OF(walk) == module)) {
1808 include_modules_at(check_class, RCLASS_ORIGIN(check_class), module, TRUE);
1816static enum rb_id_table_iterator_result
1817add_refined_method_entry_i(
ID key,
VALUE value,
void *data)
1819 rb_add_refined_method_entry((
VALUE)data, key);
1820 return ID_TABLE_CONTINUE;
1823static enum rb_id_table_iterator_result
1824clear_module_cache_i(
ID id,
VALUE val,
void *data)
1827 rb_clear_method_cache(klass,
id);
1828 return ID_TABLE_CONTINUE;
1832module_in_super_chain(
const VALUE klass,
VALUE module)
1834 struct rb_id_table *
const klass_m_tbl = RCLASS_M_TBL(RCLASS_ORIGIN(klass));
1837 if (klass_m_tbl == RCLASS_M_TBL(module))
1839 module = RCLASS_SUPER(module);
1847static enum rb_id_table_iterator_result
1848clear_constant_cache_i(
ID id,
VALUE value,
void *data)
1851 return ID_TABLE_CONTINUE;
1855do_include_modules_at(
const VALUE klass,
VALUE c,
VALUE module,
int search_super,
bool check_cyclic)
1857 VALUE p, iclass, origin_stack = 0;
1858 int method_changed = 0;
1860 VALUE klass_origin = RCLASS_ORIGIN(klass);
1861 VALUE original_klass = klass;
1863 if (check_cyclic && module_in_super_chain(klass, module))
1868 int superclass_seen = FALSE;
1874 if (klass_origin != c || search_super) {
1880 if (klass_origin == p && !search_super)
1885 if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) {
1886 if (!superclass_seen && c_seen) {
1893 superclass_seen = TRUE;
1901 RB_DEBUG_COUNTER_INC(cvar_include_invalidate);
1902 ruby_vm_global_cvar_state++;
1903 tbl = RCLASS_M_TBL(module);
1904 if (tbl && rb_id_table_size(tbl)) {
1907 rb_id_table_foreach(tbl, clear_module_cache_i, (
void *)super_class);
1912 rb_id_table_foreach(tbl, clear_module_cache_i, (
void *)original_klass);
1919 iclass = rb_include_class_new(module, super_class);
1920 c = rb_class_set_super(c, iclass);
1921 RCLASS_SET_INCLUDER(iclass, klass);
1922 if (module != RCLASS_ORIGIN(module)) {
1924 VALUE origin[2] = {iclass, RCLASS_ORIGIN(module)};
1927 else if (origin_stack && (origin_len =
RARRAY_LEN(origin_stack)) > 1 &&
1928 RARRAY_AREF(origin_stack, origin_len - 1) == module) {
1929 RCLASS_WRITE_ORIGIN(
RARRAY_AREF(origin_stack, (origin_len -= 2)), iclass);
1930 RICLASS_WRITE_ORIGIN_SHARED_MTBL(iclass);
1936 rb_module_add_to_subclasses_list(m, iclass);
1939 VALUE refined_class =
1940 rb_refinement_module_get_refined_class(klass);
1942 rb_id_table_foreach(RCLASS_M_TBL(module), add_refined_method_entry_i, (
void *)refined_class);
1946 tbl = RCLASS_CONST_TBL(module);
1947 if (tbl && rb_id_table_size(tbl))
1948 rb_id_table_foreach(tbl, clear_constant_cache_i, NULL);
1950 module = RCLASS_SUPER(module);
1953 return method_changed;
1957include_modules_at(
const VALUE klass,
VALUE c,
VALUE module,
int search_super)
1959 return do_include_modules_at(klass, c, module, search_super,
true);
1962static enum rb_id_table_iterator_result
1963move_refined_method(
ID key,
VALUE value,
void *data)
1965 rb_method_entry_t *me = (rb_method_entry_t *)value;
1967 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1969 struct rb_id_table *tbl = RCLASS_WRITABLE_M_TBL(klass);
1971 if (me->def->body.refined.orig_me) {
1972 const rb_method_entry_t *orig_me = me->def->body.refined.orig_me, *new_me;
1973 RB_OBJ_WRITE(me, &me->def->body.refined.orig_me, NULL);
1974 new_me = rb_method_entry_clone(me);
1975 rb_method_table_insert(klass, tbl, key, new_me);
1976 rb_method_entry_copy(me, orig_me);
1977 return ID_TABLE_CONTINUE;
1980 rb_method_table_insert(klass, tbl, key, me);
1981 return ID_TABLE_DELETE;
1985 return ID_TABLE_CONTINUE;
1989static enum rb_id_table_iterator_result
1990cache_clear_refined_method(
ID key,
VALUE value,
void *data)
1992 rb_method_entry_t *me = (rb_method_entry_t *) value;
1994 if (me->def->type == VM_METHOD_TYPE_REFINED && me->def->body.refined.orig_me) {
1996 rb_clear_method_cache(klass, me->called_id);
2001 return ID_TABLE_CONTINUE;
2005ensure_origin(
VALUE klass)
2007 VALUE origin = RCLASS_ORIGIN(klass);
2008 if (origin == klass) {
2012 rb_class_ensure_writable(klass);
2013 origin = class_alloc(
T_ICLASS, klass);
2014 RCLASS_SET_M_TBL(origin, RCLASS_M_TBL(klass));
2016 rb_class_set_super(klass, origin);
2017 RCLASS_WRITE_ORIGIN(klass, origin);
2021 rb_gc_writebarrier_remember(origin);
2023 class_clear_method_table(klass);
2024 rb_id_table_foreach(RCLASS_M_TBL(origin), cache_clear_refined_method, (
void *)klass);
2025 rb_id_table_foreach(RCLASS_M_TBL(origin), move_refined_method, (
void *)klass);
2035 bool klass_had_no_origin;
2037 ensure_includable(klass, module);
2038 if (module_in_super_chain(klass, module))
2039 rb_raise(rb_eArgError,
"cyclic prepend detected");
2041 klass_had_no_origin = ensure_origin(klass);
2042 changed = do_include_modules_at(klass, klass, module, FALSE,
false);
2045 rb_vm_check_redefinition_by_prepend(klass);
2048 VALUE subs_v = RCLASS_SUBCLASSES(klass);
2049 VALUE klass_origin = RCLASS_ORIGIN(klass);
2050 struct rb_id_table *klass_m_tbl = RCLASS_M_TBL(klass);
2051 struct rb_id_table *klass_origin_m_tbl = RCLASS_M_TBL(klass_origin);
2054 VALUE *entries = rb_imemo_subclasses_entries(subs_v);
2055 VALUE new_origins = 0;
2056 for (uint32_t i = 0; i < subs->count; i++) {
2057 const VALUE subclass = entries[i];
2058 if (!subclass)
continue;
2061 if (!rb_objspace_garbage_object_p(subclass)) {
2068 if (RICLASS_IS_ORIGIN_P(subclass))
continue;
2069 if (klass_had_no_origin && klass_origin_m_tbl == RCLASS_M_TBL(subclass)) {
2071 rb_id_table_foreach(RCLASS_M_TBL(subclass), clear_module_cache_i, (
void *)subclass);
2072 RCLASS_WRITE_M_TBL(subclass, klass_m_tbl);
2074 rb_class_set_super(subclass, origin);
2075 RCLASS_SET_INCLUDER(origin, RCLASS_INCLUDER(subclass));
2076 RCLASS_WRITE_ORIGIN(subclass, origin);
2077 RICLASS_SET_ORIGIN_SHARED_MTBL(origin);
2081 include_modules_at(subclass, subclass, module, FALSE);
2088 rb_module_add_to_subclasses_list(klass,
RARRAY_AREF(new_origins, i));
2123 VALUE origin = RCLASS_ORIGIN(mod);
2127 VALUE m = METACLASS_OF(p);
2162 if (METACLASS_OF(p) == mod2)
return Qtrue;
2196 refined_class = rb_refinement_module_get_refined_class(mod);
2200 if (p == refined_class)
break;
2201 if (p != RCLASS_ORIGIN(p))
continue;
2217 bool immediate_only;
2221class_descendants_recursive(
VALUE klass,
VALUE v)
2227 if (!RCLASS_SINGLETON_P(klass)) {
2228 if (data->buffer && data->count < data->maxcount && !rb_objspace_garbage_object_p(klass)) {
2233 if (data->immediate_only)
return;
2235 rb_class_foreach_subclass(klass, class_descendants_recursive, v);
2239class_descendants(
VALUE klass,
bool immediate_only)
2244 rb_class_foreach_subclass(klass, class_descendants_recursive, (
VALUE) &data);
2248 data.maxcount = data.count;
2254 rb_class_foreach_subclass(klass, class_descendants_recursive, (
VALUE) &data);
2257 rb_bug(
"GC must not occur during the subclass iteration of Class#descendants");
2305 return class_descendants(klass,
true);
2316static void module_descendants_recursive(
VALUE entry,
VALUE v);
2322 if (data->buffer && data->count >= data->maxcount)
return;
2324 if (st_insert(data->visited, (st_data_t)klass, 1))
return;
2331 rb_class_foreach_subclass(klass, module_descendants_recursive, (
VALUE)data);
2337iclass_in_ancestors_p(
VALUE iclass,
VALUE includer)
2340 if (p == iclass)
return true;
2346module_descendants_recursive(
VALUE entry,
VALUE v)
2350 if (rb_objspace_garbage_object_p(entry))
return;
2355 VALUE includer = RCLASS_INCLUDER(entry);
2357 includer = RCLASS_INCLUDER(includer);
2359 if (!includer || UNDEF_P(includer))
return;
2360 if (rb_objspace_garbage_object_p(includer))
return;
2361 if (RCLASS_SINGLETON_P(includer))
return;
2362 if (!iclass_in_ancestors_p(entry, includer))
return;
2363 module_descendants_add(includer, data);
2366 if (RCLASS_SINGLETON_P(entry))
return;
2367 module_descendants_add(entry, data);
2399 data.visited = st_init_numtable();
2400 st_insert(data.visited, (st_data_t)mod, 1);
2401 rb_class_foreach_subclass(mod, module_descendants_recursive, (
VALUE)&data);
2402 st_free_table(data.visited);
2406 data.maxcount = data.count;
2409 data.visited = st_init_numtable_with_size(data.maxcount + 1);
2410 st_insert(data.visited, (st_data_t)mod, 1);
2414 rb_class_foreach_subclass(mod, module_descendants_recursive, (
VALUE)&data);
2417 rb_bug(
"GC must not occur during the subclass iteration of Module#descendants");
2419 st_free_table(data.visited);
2447 if (!RCLASS_SINGLETON_P(klass)) {
2448 rb_raise(
rb_eTypeError,
"'%"PRIsVALUE
"' is not a singleton class", klass);
2451 const VALUE obj = RCLASS_ATTACHED_OBJECT(klass);
2458 rb_raise(rb_eRactorIsolationError,
2459 "can not get an unshareable attached object from another Ractor");
2466ins_methods_push(st_data_t name, st_data_t ary)
2472ins_methods_i(st_data_t name, st_data_t
type, st_data_t ary)
2474 switch ((rb_method_visibility_t)
type) {
2475 case METHOD_VISI_UNDEF:
2476 case METHOD_VISI_PRIVATE:
2479 ins_methods_push(name, ary);
2486ins_methods_type_i(st_data_t name, st_data_t
type, st_data_t ary, rb_method_visibility_t visi)
2488 if ((rb_method_visibility_t)
type == visi) {
2489 ins_methods_push(name, ary);
2495ins_methods_prot_i(st_data_t name, st_data_t
type, st_data_t ary)
2497 return ins_methods_type_i(name,
type, ary, METHOD_VISI_PROTECTED);
2501ins_methods_priv_i(st_data_t name, st_data_t
type, st_data_t ary)
2503 return ins_methods_type_i(name,
type, ary, METHOD_VISI_PRIVATE);
2507ins_methods_pub_i(st_data_t name, st_data_t
type, st_data_t ary)
2509 return ins_methods_type_i(name,
type, ary, METHOD_VISI_PUBLIC);
2513ins_methods_undef_i(st_data_t name, st_data_t
type, st_data_t ary)
2515 return ins_methods_type_i(name,
type, ary, METHOD_VISI_UNDEF);
2523static enum rb_id_table_iterator_result
2524method_entry_i(
ID key,
VALUE value,
void *data)
2526 const rb_method_entry_t *me = (
const rb_method_entry_t *)value;
2528 rb_method_visibility_t
type;
2530 if (me->def->type == VM_METHOD_TYPE_REFINED) {
2531 VALUE owner = me->owner;
2532 me = rb_resolve_refined_method(
Qnil, me);
2533 if (!me)
return ID_TABLE_CONTINUE;
2534 if (!arg->recur && me->owner != owner)
return ID_TABLE_CONTINUE;
2536 if (!st_is_member(arg->list, key)) {
2537 if (UNDEFINED_METHOD_ENTRY_P(me)) {
2538 type = METHOD_VISI_UNDEF;
2541 type = METHOD_ENTRY_VISI(me);
2544 st_add_direct(arg->list, key, (st_data_t)
type);
2546 return ID_TABLE_CONTINUE;
2554 rb_id_table_foreach(m_tbl, method_entry_i, me_arg);
2558particular_class_p(
VALUE mod)
2560 if (!mod)
return false;
2561 if (RCLASS_SINGLETON_P(mod))
return true;
2567class_instance_method_list(
int argc,
const VALUE *argv,
VALUE mod,
int obj,
int (*func) (st_data_t, st_data_t, st_data_t))
2570 int recur = TRUE, prepended = 0;
2575 me_arg.list = st_init_numtable();
2576 me_arg.recur = recur;
2579 for (; particular_class_p(mod); mod =
RCLASS_SUPER(mod)) {
2580 add_instance_method_list(mod, &me_arg);
2584 if (!recur && RCLASS_ORIGIN(mod) != mod) {
2585 mod = RCLASS_ORIGIN(mod);
2590 add_instance_method_list(mod, &me_arg);
2596 st_free_table(me_arg.list);
2640 return class_instance_method_list(argc, argv, mod, 0, ins_methods_i);
2655 return class_instance_method_list(argc, argv, mod, 0, ins_methods_prot_i);
2678 return class_instance_method_list(argc, argv, mod, 0, ins_methods_priv_i);
2693 return class_instance_method_list(argc, argv, mod, 0, ins_methods_pub_i);
2705rb_class_undefined_instance_methods(
VALUE mod)
2708 return class_instance_method_list(1, &include_super, mod, 0, ins_methods_undef_i);
2742rb_obj_methods(
int argc,
const VALUE *argv,
VALUE obj)
2745 if (argc > 0 && !
RTEST(argv[0])) {
2748 return class_instance_method_list(argc, argv,
CLASS_OF(obj), 1, ins_methods_i);
2761rb_obj_protected_methods(
int argc,
const VALUE *argv,
VALUE obj)
2763 return class_instance_method_list(argc, argv,
CLASS_OF(obj), 1, ins_methods_prot_i);
2776rb_obj_private_methods(
int argc,
const VALUE *argv,
VALUE obj)
2778 return class_instance_method_list(argc, argv,
CLASS_OF(obj), 1, ins_methods_priv_i);
2791rb_obj_public_methods(
int argc,
const VALUE *argv,
VALUE obj)
2793 return class_instance_method_list(argc, argv,
CLASS_OF(obj), 1, ins_methods_pub_i);
2832 VALUE ary, klass, origin;
2842 origin = RCLASS_ORIGIN(klass);
2843 me_arg.list = st_init_numtable();
2844 me_arg.recur = recur;
2845 if (klass && RCLASS_SINGLETON_P(klass)) {
2846 if ((mtbl = RCLASS_M_TBL(origin)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2851 if (klass != origin && (mtbl = RCLASS_M_TBL(klass)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2857 st_free_table(me_arg.list);
2870#ifdef rb_define_method_id
2871#undef rb_define_method_id
2876 rb_add_method_cfunc(klass, mid, func, argc, METHOD_VISI_PUBLIC);
2879#ifdef rb_define_method
2880#undef rb_define_method
2885 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PUBLIC);
2888#ifdef rb_define_protected_method
2889#undef rb_define_protected_method
2894 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PROTECTED);
2897#ifdef rb_define_private_method
2898#undef rb_define_private_method
2903 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PRIVATE);
2909 rb_add_method(klass, rb_intern(name), VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2912static enum rb_id_table_iterator_result
2913undef_method_i(
ID name,
VALUE value,
void *data)
2916 rb_add_method(klass, name, VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2917 return ID_TABLE_CONTINUE;
2921rb_undef_methods_from(
VALUE klass,
VALUE super)
2925 rb_id_table_foreach(mtbl, undef_method_i, (
void *)klass);
2938special_singleton_class_of(
VALUE obj)
2944 default:
return Qnil;
2949rb_special_singleton_class(
VALUE obj)
2951 return special_singleton_class_of(obj);
2964singleton_class_of(
VALUE obj,
bool ensure_eigenclass)
2968 switch (
TYPE(obj)) {
2978 klass = special_singleton_class_of(obj);
2980 rb_bug(
"unknown immediate %p", (
void *)obj);
2984 if (CHILLED_STRING_P(obj)) {
2985 CHILLED_STRING_MUTATED(obj);
2995 RB_VM_LOCK_ENTER_LEV(&lev);
2998 klass = METACLASS_OF(obj);
2999 if (!(RCLASS_SINGLETON_P(klass) &&
3000 RCLASS_ATTACHED_OBJECT(klass) == obj)) {
3001 klass = rb_make_metaclass(obj, klass);
3014 RB_VM_LOCK_LEAVE_LEV(&lev);
3020#if RUBY_VERSION_SINCE(4, 2)
3021RBIMPL_TODO(
"make rb_freeze_singleton_class internal; remove from fl_type.h")
3033 (RCLASS_ATTACHED_OBJECT(klass) == attached_object)) {
3034 attached_object = klass;
3052 return rb_special_singleton_class(obj);
3054 klass = METACLASS_OF(obj);
3055 if (!RCLASS_SINGLETON_P(klass))
return Qnil;
3056 if (RCLASS_ATTACHED_OBJECT(klass) != obj)
return Qnil;
3063 return singleton_class_of(obj,
true);
3075#ifdef rb_define_singleton_method
3076#undef rb_define_singleton_method
3084#ifdef rb_define_module_function
3085#undef rb_define_module_function
3094#ifdef rb_define_global_function
3095#undef rb_define_global_function
3106 rb_alias(klass, rb_intern(name1), rb_intern(name2));
3112 rb_attr(klass, rb_intern(name), read, write, FALSE);
3116rb_keyword_error_new(
const char *error,
VALUE keys)
3119 VALUE error_message = rb_sprintf(
"%s keyword%.*s", error,
len > 1,
"s");
3126 if (++i >=
len)
break;
3134NORETURN(
static void rb_keyword_error(
const char *error,
VALUE keys));
3136rb_keyword_error(
const char *error,
VALUE keys)
3141NORETURN(
static void unknown_keyword_error(
VALUE hash,
const ID *table,
int keywords));
3143unknown_keyword_error(
VALUE hash,
const ID *table,
int keywords)
3146 for (i = 0; i < keywords; i++) {
3147 st_data_t key =
ID2SYM(table[i]);
3148 rb_hash_stlike_delete(hash, &key, NULL);
3150 rb_keyword_error(
"unknown", rb_hash_keys(hash));
3155separate_symbol(st_data_t key, st_data_t value, st_data_t arg)
3160 rb_hash_aset(*kwdhash, (
VALUE)key, (
VALUE)value);
3167 VALUE parthash[2] = {0, 0};
3168 VALUE hash = *orighash;
3175 *orighash = parthash[1];
3190#define extract_kwarg(keyword, val) \
3191 (key = (st_data_t)(keyword), values ? \
3192 (rb_hash_stlike_delete(keyword_hash, &key, &(val)) || ((val) = Qundef, 0)) : \
3193 rb_hash_stlike_lookup(keyword_hash, key, NULL))
3195 if (
NIL_P(keyword_hash)) keyword_hash = 0;
3199 optional = -1-optional;
3202 for (; i < required; i++) {
3205 if (extract_kwarg(keyword, values[i])) {
3212 if (!
NIL_P(missing)) {
3213 rb_keyword_error(
"missing", missing);
3217 if (optional && keyword_hash) {
3218 for (i = 0; i < optional; i++) {
3219 if (extract_kwarg(
ID2SYM(table[required+i]), values[required+i])) {
3224 if (!rest && keyword_hash) {
3225 if (
RHASH_SIZE(keyword_hash) > (
unsigned int)(values ? 0 : j)) {
3226 unknown_keyword_error(keyword_hash, table, required+optional);
3229 if (values && !keyword_hash) {
3230 for (i = 0; i < required + optional; i++) {
3249rb_scan_args_parse(
int kw_flag,
const char *fmt,
struct rb_scan_args_t *arg)
3251 const char *p = fmt;
3253 memset(arg, 0,
sizeof(*arg));
3254 arg->kw_flag = kw_flag;
3257 arg->n_lead = *p -
'0';
3260 arg->n_opt = *p -
'0';
3269 arg->n_trail = *p -
'0';
3281 rb_fatal(
"bad scan arg format: %s", fmt);
3286rb_scan_args_assign(
const struct rb_scan_args_t *arg,
int argc,
const VALUE *
const argv, va_list vargs)
3290#define rb_scan_args_next_param() va_arg(vargs, VALUE *)
3291 const int kw_flag = arg->kw_flag;
3292 const int n_lead = arg->n_lead;
3293 const int n_opt = arg->n_opt;
3294 const int n_trail = arg->n_trail;
3295 const int n_mand = n_lead + n_trail;
3296 const bool f_var = arg->f_var;
3297 const bool f_hash = arg->f_hash;
3298 const bool f_block = arg->f_block;
3301 if (f_hash && argc > 0) {
3302 VALUE last = argv[argc - 1];
3303 if (rb_scan_args_keyword_p(kw_flag, last)) {
3304 hash = rb_hash_dup(last);
3309 if (argc < n_mand) {
3314 for (i = 0; i < n_lead; i++) {
3315 var = rb_scan_args_next_param();
3316 if (var) *var = argv[argi];
3320 for (i = 0; i < n_opt; i++) {
3321 var = rb_scan_args_next_param();
3322 if (argi < argc - n_trail) {
3323 if (var) *var = argv[argi];
3327 if (var) *var =
Qnil;
3332 int n_var = argc - argi - n_trail;
3334 var = rb_scan_args_next_param();
3344 for (i = 0; i < n_trail; i++) {
3345 var = rb_scan_args_next_param();
3346 if (var) *var = argv[argi];
3351 var = rb_scan_args_next_param();
3352 if (var) *var = hash;
3356 var = rb_scan_args_next_param();
3371#undef rb_scan_args_next_param
3375rb_scan_args_result(
const struct rb_scan_args_t *
const arg,
int argc)
3377 const int n_lead = arg->n_lead;
3378 const int n_opt = arg->n_opt;
3379 const int n_trail = arg->n_trail;
3380 const int n_mand = n_lead + n_trail;
3381 const bool f_var = arg->f_var;
3399 va_start(vargs,fmt);
3400 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
3402 return rb_scan_args_result(&arg, argc);
3405#undef rb_scan_args_kw
3411 rb_scan_args_parse(kw_flag, fmt, &arg);
3412 va_start(vargs,fmt);
3413 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
3415 return rb_scan_args_result(&arg, argc);
#define ISDIGIT
@old{rb_isdigit}
#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.
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
VALUE rb_refinement_new(void)
Creates a new, anonymous refinement.
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
VALUE rb_class_new(VALUE super)
Creates a new, anonymous class.
static VALUE make_singleton_class(VALUE obj)
Creates a singleton class for obj.
VALUE rb_singleton_class_clone(VALUE obj)
Clones a singleton class.
void rb_prepend_module(VALUE klass, VALUE module)
Identical to rb_include_module(), except it "prepends" the passed module to the klass,...
VALUE rb_class_subclasses(VALUE klass)
Queries the class's direct descendants.
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
static VALUE class_alloc0(enum ruby_value_type type, VALUE klass, bool boxable, bool struct_inline_field)
Allocates a struct RClass for a new class, iclass, or module.
void Init_class_hierarchy(void)
Initializes the world of objects and classes.
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
VALUE rb_class_attached_object(VALUE klass)
Returns the attached object for a singleton class.
VALUE rb_obj_singleton_methods(int argc, const VALUE *argv, VALUE obj)
Identical to rb_class_instance_methods(), except it returns names of singleton methods instead of ins...
VALUE rb_module_new(void)
Creates a new, anonymous module.
#define META_CLASS_OF_CLASS_CLASS_P(k)
whether k is a meta^(n)-class of Class class
VALUE rb_class_instance_methods(int argc, const VALUE *argv, VALUE mod)
Generates an array of symbols, which are the list of method names defined in the passed class.
void rb_check_inheritable(VALUE super)
Asserts that the given class can derive a child class.
VALUE rb_class_public_instance_methods(int argc, const VALUE *argv, VALUE mod)
Identical to rb_class_instance_methods(), except it returns names of methods that are public only.
VALUE rb_class_super_of(VALUE klass)
Internal header for Objspace.
VALUE rb_class_boot(VALUE super)
A utility function that wraps class_alloc.
VALUE rb_define_module(const char *name)
Defines a top-level module.
void rb_class_modify_check(VALUE klass)
Asserts that klass is not a frozen class.
VALUE rb_define_module_id_under(VALUE outer, ID id)
Identical to rb_define_module_under(), except it takes the name in ID instead of C's string.
void rb_singleton_class_attached(VALUE klass, VALUE obj)
Attaches a singleton class to its corresponding object.
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_define_module_under(VALUE outer, const char *name)
Defines a module under the namespace of outer.
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 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.
size_t rb_gc_count(void)
Identical to rb_gc_stat(), with "count" parameter.
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.
VALUE rb_hash_new(void)
Creates a new, empty hash object.
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.
int st_foreach(st_table *q, int_type *w, st_data_t e)
Iteration over the given table.
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 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.