17#include "ruby/internal/config.h"
21#include "debug_counter.h"
24#include "internal/box.h"
25#include "internal/class.h"
26#include "internal/eval.h"
27#include "internal/hash.h"
28#include "internal/object.h"
29#include "internal/string.h"
30#include "internal/variable.h"
80#define METACLASS_OF(k) RBASIC(k)->klass
81#define SET_METACLASS_OF(k, cls) RBASIC_SET_CLASS(k, cls)
83static void rb_class_remove_from_super_subclasses(
VALUE klass);
84static void rb_class_remove_from_module_subclasses(
VALUE klass);
85static void rb_class_classext_free_subclasses(
rb_classext_t *ext);
91 st_data_t key = (st_data_t)box->box_object;
92 VALUE obj_id = rb_obj_id(klass);
93 st_delete(box->classext_cow_classes, &obj_id, 0);
94 st_delete(RCLASS_CLASSEXT_TBL(klass), &key, &ext);
103 rb_id_table_free(RCLASSEXT_M_TBL(ext));
105 if (!RCLASSEXT_SHARED_CONST_TBL(ext) && (tbl = RCLASSEXT_CONST_TBL(ext)) != NULL) {
106 rb_free_const_table(tbl);
110 rb_class_remove_from_super_subclasses(klass);
111 rb_class_classext_free_subclasses(ext);
114 if (RCLASSEXT_SUPERCLASSES_WITH_SELF(ext)) {
116 size_t depth = RCLASSEXT_SUPERCLASS_DEPTH(ext);
117 if (depth != RCLASS_MAX_SUPERCLASS_DEPTH) {
120 SIZED_FREE_N(RCLASSEXT_SUPERCLASSES(ext), depth);
131 if (RCLASSEXT_ICLASS_IS_ORIGIN(ext) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext)) {
133 rb_id_table_free(RCLASSEXT_M_TBL(ext));
136 if (RCLASSEXT_CALLABLE_M_TBL(ext) != NULL) {
137 rb_id_table_free(RCLASSEXT_CALLABLE_M_TBL(ext));
141 rb_class_remove_from_super_subclasses(klass);
142 rb_class_remove_from_module_subclasses(klass);
153 if (RCLASSEXT_ICLASS_IS_ORIGIN(ext) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext)) {
155 rb_id_table_free(RCLASSEXT_M_TBL(ext));
158 if (RCLASSEXT_CALLABLE_M_TBL(ext) != NULL) {
159 rb_id_table_free(RCLASSEXT_CALLABLE_M_TBL(ext));
171set_box_classext_update(st_data_t *key_ptr, st_data_t *val_ptr, st_data_t a,
int existing)
177 iclass_free_orphan_classext(args->obj, (
rb_classext_t *)*val_ptr);
180 rb_bug(
"Updating existing classext for non-iclass never happen");
184 *val_ptr = (st_data_t)args->ext;
197 VM_ASSERT(BOX_USER_P(box));
199 st_update(RCLASS_CLASSEXT_TBL(obj), (st_data_t)box->box_object, set_box_classext_update, (st_data_t)&args);
204 rb_gc_writebarrier_remember(obj);
206 st_insert(box->classext_cow_classes, (st_data_t)rb_obj_id(obj), obj);
216static enum rb_id_table_iterator_result
217duplicate_classext_m_tbl_i(
ID key,
VALUE value,
void *data)
221 rb_method_table_insert0(arg->klass, arg->tbl, key, me,
false);
222 return ID_TABLE_CONTINUE;
226duplicate_classext_m_tbl(
struct rb_id_table *orig,
VALUE klass,
bool init_missing)
231 return rb_id_table_create(0);
235 tbl = rb_id_table_create(rb_id_table_size(orig));
240 rb_id_table_foreach(orig, duplicate_classext_m_tbl_i, &data);
250 dst->flag = src->flag;
251 dst->line = src->line;
258static enum rb_id_table_iterator_result
259duplicate_classext_const_tbl_i(
ID key,
VALUE value,
void *data)
264 rb_id_table_insert(arg->tbl, key, (
VALUE)entry);
266 return ID_TABLE_CONTINUE;
277 dst = rb_id_table_create(rb_id_table_size(src));
283 rb_id_table_foreach(src, duplicate_classext_const_tbl_i, (
void *)&data);
294 rb_classext_t *ext = RCLASS_EXT_TABLE_LOOKUP_INTERNAL(iclass, box);
299 rb_invalidate_method_caches(RCLASSEXT_CALLABLE_M_TBL(ext), RCLASSEXT_CC_TBL(ext));
304 RCLASSEXT_BOX(ext) = box;
306 RCLASSEXT_SUPER(ext) = RCLASSEXT_SUPER(src);
309 if (RCLASSEXT_ICLASS_IS_ORIGIN(src) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(src)) {
310 RCLASSEXT_M_TBL(ext) = duplicate_classext_m_tbl(RCLASSEXT_M_TBL(src), iclass,
true);
313 RCLASSEXT_M_TBL(ext) = RCLASSEXT_M_TBL(mod_ext);
316 RCLASSEXT_CONST_TBL(ext) = RCLASSEXT_CONST_TBL(mod_ext);
317 RCLASSEXT_CVC_TBL(ext) = RCLASSEXT_CVC_TBL(mod_ext);
325 RCLASSEXT_SET_ORIGIN(ext, iclass, RCLASSEXT_ORIGIN(src));
326 RCLASSEXT_ICLASS_IS_ORIGIN(ext) = RCLASSEXT_ICLASS_IS_ORIGIN(src);
327 RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext) = RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(src);
329 RCLASSEXT_SET_INCLUDER(ext, iclass, RCLASSEXT_INCLUDER(src));
333 first_set = RCLASS_SET_BOX_CLASSEXT(iclass, box, ext);
335 RCLASS_SET_PRIME_CLASSEXT_WRITABLE(iclass,
false);
347 RCLASSEXT_BOX(ext) = box;
349 RCLASSEXT_SUPER(ext) = RCLASSEXT_SUPER(orig);
351 RCLASSEXT_M_TBL(ext) = duplicate_classext_m_tbl(RCLASSEXT_M_TBL(orig), klass, dup_iclass);
352 RCLASSEXT_ICLASS_IS_ORIGIN(ext) =
true;
353 RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext) =
false;
355 if (orig->fields_obj) {
356 RB_OBJ_WRITE(klass, &ext->fields_obj, rb_imemo_fields_clone(orig->fields_obj));
359 if (RCLASSEXT_SHARED_CONST_TBL(orig)) {
360 RCLASSEXT_CONST_TBL(ext) = RCLASSEXT_CONST_TBL(orig);
361 RCLASSEXT_SHARED_CONST_TBL(ext) =
true;
364 RCLASSEXT_CONST_TBL(ext) = duplicate_classext_const_tbl(RCLASSEXT_CONST_TBL(orig), klass);
365 RCLASSEXT_SHARED_CONST_TBL(ext) =
false;
377 VALUE cvc_table = RCLASSEXT_CVC_TBL(orig);
379 cvc_table = rb_marked_id_table_dup(cvc_table);
381 else if (dup_iclass) {
382 cvc_table = rb_marked_id_table_new(2);
384 RB_OBJ_WRITE(klass, &RCLASSEXT_CVC_TBL(ext), cvc_table);
388 RCLASSEXT_SET_ORIGIN(ext, klass, RCLASSEXT_ORIGIN(orig));
397 RCLASSEXT_PERMANENT_CLASSPATH(ext) = RCLASSEXT_PERMANENT_CLASSPATH(orig);
398 RCLASSEXT_CLONED(ext) = RCLASSEXT_CLONED(orig);
399 RCLASSEXT_CLASSPATH(ext) = RCLASSEXT_CLASSPATH(orig);
412 if (subclass_entry) subclass_entry = subclass_entry->next;
413 while (subclass_entry) {
414 VALUE iclass = subclass_entry->klass;
425 class_duplicate_iclass_classext(iclass, ext, box);
427 subclass_entry = subclass_entry->next;
435rb_class_ensure_writable(
VALUE klass)
438 RCLASS_EXT_WRITABLE(klass);
442 rb_class_classext_foreach_callback_func *func;
447class_classext_foreach_i(st_data_t key, st_data_t value, st_data_t arg)
450 rb_class_classext_foreach_callback_func *func =
foreach_arg->func;
456rb_class_classext_foreach(
VALUE klass, rb_class_classext_foreach_callback_func *func,
void *arg)
458 st_table *tbl = RCLASS_CLASSEXT_TBL(klass);
463 rb_st_foreach(tbl, class_classext_foreach_i, (st_data_t)&
foreach_arg);
465 func(RCLASS_EXT_PRIME(klass),
true, (
VALUE)NULL, arg);
469rb_class_super_of(
VALUE klass)
475rb_class_singleton_p(
VALUE klass)
477 return RCLASS_SINGLETON_P(klass);
481rb_class_variation_count(
VALUE klass)
483 return RCLASS_VARIATION_COUNT(klass);
487push_subclass_entry_to_list(
VALUE super,
VALUE klass)
498 entry->klass = klass;
501 head = RCLASS_WRITABLE_SUBCLASSES(super);
504 RCLASS_SET_SUBCLASSES(super, head);
506 entry->next = head->next;
510 head->next->prev = entry;
519rb_class_subclass_add(
VALUE super,
VALUE klass)
521 if (super && !UNDEF_P(super)) {
525 RCLASS_EXT_PRIME(klass)->subclass_entry = entry;
530rb_module_add_to_subclasses_list(
VALUE module,
VALUE iclass)
532 if (module && !UNDEF_P(module)) {
536 RCLASS_EXT_PRIME(iclass)->module_subclass_entry = entry;
558rb_class_remove_from_super_subclasses(
VALUE klass)
564 rb_subclass_entry_remove(entry);
565 RCLASSEXT_SUBCLASS_ENTRY(ext) = NULL;
569rb_class_remove_from_module_subclasses(
VALUE klass)
575 rb_subclass_entry_remove(entry);
576 RCLASSEXT_MODULE_SUBCLASS_ENTRY(ext) = NULL;
591 if (RCLASSEXT_SUBCLASS_ENTRY(child_ext) == entry) {
592 RCLASSEXT_SUBCLASS_ENTRY(child_ext) = NULL;
594 if (RCLASSEXT_MODULE_SUBCLASS_ENTRY(child_ext) == entry) {
595 RCLASSEXT_MODULE_SUBCLASS_ENTRY(child_ext) = NULL;
607 RCLASSEXT_SUBCLASSES(ext) = NULL;
619 VALUE curklass = cur->klass;
629class_detach_subclasses(
VALUE klass,
VALUE arg)
631 rb_class_remove_from_super_subclasses(klass);
635class_switch_superclass(
VALUE super,
VALUE klass)
638 class_detach_subclasses(klass,
Qnil);
639 rb_class_subclass_add(super, klass);
656 const rb_box_t *box = rb_current_box();
658 if (!ruby_box_init_done) {
670 if (boxable) flags |= RCLASS_BOXABLE;
672 NEWOBJ_OF(obj,
struct RClass, klass, flags, alloc_size);
689 RCLASS_PRIME_BOX((
VALUE)obj) = box;
692 RCLASS_SET_PRIME_CLASSEXT_WRITABLE((
VALUE)obj, !boxable || BOX_USER_P(box));
695 RCLASS_SET_REFINED_CLASS((
VALUE)obj,
Qnil);
703 bool boxable = rb_box_available() && BOX_ROOT_P(rb_current_box());
708class_associate_super(
VALUE klass,
VALUE super,
bool init)
710 if (super && !UNDEF_P(super)) {
715 if (RCLASS_SINGLETON_P(klass)) {
722 class_switch_superclass(super, klass);
727 RCLASS_SET_SUPER(klass, super);
730 RCLASS_WRITE_SUPER(klass, super);
732 rb_class_update_superclasses(klass);
739 return class_associate_super(klass, super,
false);
743class_initialize_method_table(
VALUE c)
746 RCLASS_SET_M_TBL(c, rb_id_table_create(0));
750class_clear_method_table(
VALUE c)
752 RCLASS_WRITE_M_TBL(c, rb_id_table_create(0));
756class_boot_boxable(
VALUE super,
bool boxable)
762 class_initialize_method_table(klass);
764 class_associate_super(klass, super,
true);
765 if (super && !UNDEF_P(super)) {
766 RCLASS_SET_ALLOCATOR(klass, RCLASS_ALLOCATOR(super));
767 rb_class_set_initialized(klass);
785 return class_boot_boxable(super,
false);
789class_superclasses_including_self(
VALUE klass)
791 if (RCLASS_SUPERCLASSES_WITH_SELF_P(klass))
792 return RCLASS_SUPERCLASSES(klass);
794 size_t depth = RCLASS_SUPERCLASS_DEPTH(klass);
797 memcpy(superclasses, RCLASS_SUPERCLASSES(klass),
sizeof(
VALUE) * depth);
798 superclasses[depth] = klass;
804rb_class_update_superclasses(
VALUE klass)
811 if (UNDEF_P(super))
return;
814 if (RCLASS_SUPERCLASSES(klass))
829 rb_class_update_superclasses(super);
832 if (!RCLASS_SUPERCLASS_DEPTH(super))
836 super_depth = RCLASS_SUPERCLASS_DEPTH(super);
837 if (RCLASS_SUPERCLASSES_WITH_SELF_P(super)) {
838 superclasses = RCLASS_SUPERCLASSES(super);
841 superclasses = class_superclasses_including_self(super);
842 RCLASS_WRITE_SUPERCLASSES(super, super_depth, superclasses,
true);
845 size_t depth = super_depth == RCLASS_MAX_SUPERCLASS_DEPTH ? super_depth : super_depth + 1;
846 RCLASS_WRITE_SUPERCLASSES(klass, depth, superclasses,
false);
853 rb_raise(
rb_eTypeError,
"superclass must be an instance of Class (given an instance of %"PRIsVALUE
")",
856 if (RCLASS_SINGLETON_P(super)) {
857 rb_raise(
rb_eTypeError,
"can't make subclass of singleton class");
871 RCLASS_SET_MAX_IV_COUNT(klass, RCLASS_MAX_IV_COUNT(super));
872 RUBY_ASSERT(getenv(
"RUBY_BOX") || RCLASS_PRIME_CLASSEXT_WRITABLE_P(klass));
878rb_class_s_alloc(
VALUE klass)
886 if (me->def->type == VM_METHOD_TYPE_ISEQ) {
887 rb_cref_t *new_cref = rb_vm_rewrite_cref(me->def->body.iseq.
cref, old_klass, new_klass);
888 rb_add_method_iseq(new_klass, mid, me->def->body.iseq.
iseqptr, new_cref, METHOD_ENTRY_VISI(me));
891 rb_method_entry_set(new_klass, mid, me, METHOD_ENTRY_VISI(me));
900static enum rb_id_table_iterator_result
901clone_method_i(
ID key,
VALUE value,
void *data)
904 clone_method(arg->old_klass, arg->new_klass, key, (
const rb_method_entry_t *)value);
905 return ID_TABLE_CONTINUE;
921 rb_id_table_insert(arg->tbl, key, (
VALUE)nce);
922 return ID_TABLE_CONTINUE;
925static enum rb_id_table_iterator_result
926clone_const_i(
ID key,
VALUE value,
void *data)
937 if (RCLASS_INITIALIZED_P(clone)) {
940 if (RCLASS_SINGLETON_P(orig)) {
951cvc_table_entry_alloc(
void)
956static enum rb_id_table_iterator_result
957cvc_table_copy(
ID id,
VALUE val,
void *data)
965 ent = cvc_table_entry_alloc();
968 ent->global_cvar_state = orig_entry->global_cvar_state;
969 rb_marked_id_table_insert(ctx->new_table,
id, (
VALUE)ent);
971 return ID_TABLE_CONTINUE;
977 if (RCLASS_CONST_TBL(clone)) {
978 rb_free_const_table(RCLASS_CONST_TBL(clone));
979 RCLASS_WRITE_CONST_TBL(clone, 0,
false);
981 if (RCLASS_CVC_TBL(orig)) {
982 VALUE rb_cvc_tbl = RCLASS_CVC_TBL(orig);
983 VALUE rb_cvc_tbl_dup = rb_marked_id_table_new(rb_marked_id_table_size(rb_cvc_tbl));
987 ctx.new_table = rb_cvc_tbl_dup;
988 rb_marked_id_table_foreach(rb_cvc_tbl, cvc_table_copy, &ctx);
989 RCLASS_WRITE_CVC_TBL(clone, rb_cvc_tbl_dup);
991 rb_id_table_free(RCLASS_M_TBL(clone));
992 RCLASS_WRITE_M_TBL(clone, 0);
994 rb_fields_tbl_copy(clone, orig);
996 if (RCLASS_CONST_TBL(orig)) {
999 struct rb_id_table *orig_tbl = RCLASS_CONST_TBL(orig);
1000 arg.tbl = const_tbl = rb_id_table_create(rb_id_table_size(orig_tbl));
1002 rb_id_table_foreach(orig_tbl, clone_const_i, &arg);
1003 RCLASS_WRITE_CONST_TBL(clone, const_tbl,
false);
1004 rb_gc_writebarrier_remember(clone);
1008static bool ensure_origin(
VALUE klass);
1011rb_class_set_initialized(
VALUE klass)
1019rb_module_check_initializable(
VALUE mod)
1021 if (RCLASS_INITIALIZED_P(mod)) {
1035 class_init_copy_check(clone, orig);
1038 rb_module_check_initializable(clone);
1048 rb_class_set_initialized(clone);
1053 RCLASS_SET_CLONED(clone,
true);
1054 RCLASS_SET_CLONED(orig,
true);
1056 if (!RCLASS_SINGLETON_P(
CLASS_OF(clone))) {
1061 RCLASS_SET_ALLOCATOR(clone, RCLASS_ALLOCATOR(orig));
1063 copy_tables(clone, orig);
1064 if (RCLASS_M_TBL(orig)) {
1066 arg.old_klass = orig;
1067 arg.new_klass = clone;
1068 class_initialize_method_table(clone);
1069 rb_id_table_foreach(RCLASS_M_TBL(orig), clone_method_i, &arg);
1072 if (RCLASS_ORIGIN(orig) == orig) {
1077 VALUE orig_origin = RCLASS_ORIGIN(orig);
1078 VALUE prev_clone_p = clone;
1086 ensure_origin(clone);
1087 clone_origin = RCLASS_ORIGIN(clone);
1089 while (p && p != orig_origin) {
1091 rb_bug(
"non iclass between module/class and origin");
1093 clone_p = class_alloc(
T_ICLASS, METACLASS_OF(p));
1094 RCLASS_SET_M_TBL(clone_p, RCLASS_M_TBL(p));
1095 rb_class_set_super(prev_clone_p, clone_p);
1096 prev_clone_p = clone_p;
1097 RCLASS_SET_CONST_TBL(clone_p, RCLASS_CONST_TBL(p),
false);
1099 RCLASS_SET_INCLUDER(clone_p, clone);
1101 add_subclass = TRUE;
1102 if (p != RCLASS_ORIGIN(p)) {
1103 origin[0] = clone_p;
1104 origin[1] = RCLASS_ORIGIN(p);
1107 else if ((origin_len =
RARRAY_LEN(origin_stack)) > 1 &&
1109 RCLASS_WRITE_ORIGIN(
RARRAY_AREF(origin_stack, (origin_len -= 2)), clone_p);
1110 RICLASS_WRITE_ORIGIN_SHARED_MTBL(clone_p);
1112 add_subclass = FALSE;
1115 rb_module_add_to_subclasses_list(METACLASS_OF(p), clone_p);
1120 if (p == orig_origin) {
1122 rb_class_set_super(clone_p, clone_origin);
1123 rb_class_set_super(clone_origin,
RCLASS_SUPER(orig_origin));
1125 copy_tables(clone_origin, orig_origin);
1126 if (RCLASS_M_TBL(orig_origin)) {
1128 arg.old_klass = orig;
1129 arg.new_klass = clone;
1130 class_initialize_method_table(clone_origin);
1131 rb_id_table_foreach(RCLASS_M_TBL(orig_origin), clone_method_i, &arg);
1135 rb_bug(
"no origin for class that has origin");
1138 rb_class_update_superclasses(clone);
1147 return rb_singleton_class_clone_and_attach(obj,
Qundef);
1152rb_singleton_class_clone_and_attach(
VALUE obj,
VALUE attach)
1154 const VALUE klass = METACLASS_OF(obj);
1160 if (!(RCLASS_SINGLETON_P(klass) && RCLASS_ATTACHED_OBJECT(klass) == obj)) {
1166 bool klass_of_clone_is_new;
1171 klass_of_clone_is_new =
true;
1172 RBASIC_SET_CLASS(clone, clone);
1178 klass_of_clone_is_new = (METACLASS_OF(klass) != klass_metaclass_clone);
1179 RBASIC_SET_CLASS(clone, klass_metaclass_clone);
1183 class_initialize_method_table(clone);
1186 rb_fields_tbl_copy(clone, klass);
1187 if (RCLASS_CONST_TBL(klass)) {
1190 arg.tbl = table = rb_id_table_create(rb_id_table_size(RCLASS_CONST_TBL(klass)));
1192 rb_id_table_foreach(RCLASS_CONST_TBL(klass), clone_const_i, &arg);
1193 RCLASS_SET_CONST_TBL(clone, table,
false);
1195 if (!UNDEF_P(attach)) {
1200 arg.old_klass = klass;
1201 arg.new_klass = clone;
1202 rb_id_table_foreach(RCLASS_M_TBL(klass), clone_method_i, &arg);
1204 if (klass_of_clone_is_new) {
1216 if (RCLASS_SINGLETON_P(klass)) {
1217 RCLASS_SET_ATTACHED_OBJECT(klass, obj);
1226#define META_CLASS_OF_CLASS_CLASS_P(k) (METACLASS_OF(k) == (k))
1229rb_singleton_class_has_metaclass_p(
VALUE sklass)
1231 return RCLASS_ATTACHED_OBJECT(METACLASS_OF(sklass)) == sklass;
1235rb_singleton_class_internal_p(
VALUE sklass)
1238 !rb_singleton_class_has_metaclass_p(sklass));
1246#define HAVE_METACLASS_P(k) \
1247 (FL_TEST(METACLASS_OF(k), FL_SINGLETON) && \
1248 rb_singleton_class_has_metaclass_p(k))
1257#define ENSURE_EIGENCLASS(klass) \
1258 (HAVE_METACLASS_P(klass) ? METACLASS_OF(klass) : make_metaclass(klass))
1280 SET_METACLASS_OF(klass, metaclass);
1281 SET_METACLASS_OF(metaclass, metaclass);
1284 VALUE tmp = METACLASS_OF(klass);
1285 SET_METACLASS_OF(klass, metaclass);
1292 rb_class_set_initialized(klass);
1295 rb_class_update_superclasses(METACLASS_OF(metaclass));
1309 VALUE orig_class = METACLASS_OF(obj);
1312 class_initialize_method_table(klass);
1313 class_associate_super(klass, orig_class,
true);
1314 if (orig_class && !UNDEF_P(orig_class)) {
1315 rb_class_set_initialized(klass);
1318 RBASIC_SET_CLASS(obj, klass);
1320 rb_yjit_invalidate_no_singleton_class(orig_class);
1321 rb_zjit_invalidate_no_singleton_class(orig_class);
1323 SET_METACLASS_OF(klass, METACLASS_OF(
rb_class_real(orig_class)));
1329boot_defclass(
const char *name,
VALUE super)
1332 ID id = rb_intern(name);
1335 rb_vm_register_global_object(obj);
1386refinement_import_methods(
int argc,
VALUE *argv,
VALUE refinement)
1411Init_class_hierarchy(
void)
1472 rb_make_metaclass(klass, METACLASS_OF(super));
1492 return rb_funcall(super, inherited, 1, klass);
1499 ID id = rb_intern(name);
1508 rb_raise(
rb_eTypeError,
"superclass mismatch for class %s", name);
1512 rb_vm_register_global_object(klass);
1516 rb_raise(rb_eArgError,
"no super class for '%s'", name);
1519 rb_vm_register_global_object(klass);
1533rb_define_class_id_under_no_pin(
VALUE outer,
ID id,
VALUE super)
1540 rb_raise(
rb_eTypeError,
"%"PRIsVALUE
"::%"PRIsVALUE
" is not a class"
1546 "%"PRIsVALUE
"::%"PRIsVALUE
""
1547 " (%"PRIsVALUE
" is given but was %"PRIsVALUE
")",
1554 rb_raise(rb_eArgError,
"no super class for '%"PRIsVALUE
"::%"PRIsVALUE
"'",
1568 VALUE klass = rb_define_class_id_under_no_pin(outer,
id, super);
1569 rb_vm_register_global_object(klass);
1574rb_module_s_alloc(
VALUE klass)
1577 class_initialize_method_table(mod);
1582module_new(
VALUE klass)
1585 class_initialize_method_table(mdl);
1612 ID id = rb_intern(name);
1615 module = rb_const_get(rb_cObject, id);
1617 rb_raise(
rb_eTypeError,
"%s is not a module (%"PRIsVALUE
")",
1621 rb_vm_register_global_object(module);
1624 module = rb_module_new();
1625 rb_vm_register_global_object(module);
1643 module = rb_const_get_at(outer, id);
1645 rb_raise(
rb_eTypeError,
"%"PRIsVALUE
"::%"PRIsVALUE
" is not a module"
1650 rb_vm_register_global_object(module);
1653 module = rb_module_new();
1656 rb_vm_register_global_object(module);
1662rb_include_class_new(
VALUE module,
VALUE super)
1666 RCLASS_SET_M_TBL(klass, RCLASS_WRITABLE_M_TBL(module));
1668 RCLASS_SET_ORIGIN(klass, klass);
1670 module = METACLASS_OF(module);
1673 if (RCLASS_WRITABLE_CONST_TBL(module)) {
1674 RCLASS_SET_CONST_TBL(klass, RCLASS_WRITABLE_CONST_TBL(module),
true);
1677 RCLASS_WRITE_CONST_TBL(module, rb_id_table_create(0),
false);
1678 RCLASS_SET_CONST_TBL(klass, RCLASS_WRITABLE_CONST_TBL(module),
true);
1681 RCLASS_SET_CVC_TBL(klass, RCLASS_WRITABLE_CVC_TBL(module));
1683 class_associate_super(klass, super,
true);
1684 RBASIC_SET_CLASS(klass, module);
1686 return (
VALUE)klass;
1689static int include_modules_at(
const VALUE klass,
VALUE c,
VALUE module,
int search_super);
1696 rb_class_set_initialized(module);
1697 if (!
NIL_P(rb_refinement_module_get_refined_class(module))) {
1698 rb_raise(rb_eArgError,
"refinement module is not allowed");
1707 ensure_includable(klass, module);
1709 changed = include_modules_at(klass, RCLASS_ORIGIN(klass), module, TRUE);
1711 rb_raise(rb_eArgError,
"cyclic include detected");
1717 VALUE check_class = iclass->klass;
1720 if (!rb_objspace_garbage_object_p(check_class)) {
1721 while (check_class) {
1722 RUBY_ASSERT(!rb_objspace_garbage_object_p(check_class));
1725 (METACLASS_OF(check_class) == module)) {
1732 include_modules_at(iclass->klass, RCLASS_ORIGIN(iclass->klass), module, TRUE);
1736 iclass = iclass->next;
1741static enum rb_id_table_iterator_result
1742add_refined_method_entry_i(
ID key,
VALUE value,
void *data)
1744 rb_add_refined_method_entry((
VALUE)data, key);
1745 return ID_TABLE_CONTINUE;
1748static enum rb_id_table_iterator_result
1749clear_module_cache_i(
ID id,
VALUE val,
void *data)
1752 rb_clear_method_cache(klass,
id);
1753 return ID_TABLE_CONTINUE;
1757module_in_super_chain(
const VALUE klass,
VALUE module)
1759 struct rb_id_table *
const klass_m_tbl = RCLASS_M_TBL(RCLASS_ORIGIN(klass));
1762 if (klass_m_tbl == RCLASS_M_TBL(module))
1764 module = RCLASS_SUPER(module);
1772static enum rb_id_table_iterator_result
1773clear_constant_cache_i(
ID id,
VALUE value,
void *data)
1776 return ID_TABLE_CONTINUE;
1780do_include_modules_at(
const VALUE klass,
VALUE c,
VALUE module,
int search_super,
bool check_cyclic)
1782 VALUE p, iclass, origin_stack = 0;
1783 int method_changed = 0;
1785 VALUE klass_origin = RCLASS_ORIGIN(klass);
1786 VALUE original_klass = klass;
1788 if (check_cyclic && module_in_super_chain(klass, module))
1793 int superclass_seen = FALSE;
1799 if (klass_origin != c || search_super) {
1805 if (klass_origin == p && !search_super)
1810 if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) {
1811 if (!superclass_seen && c_seen) {
1818 superclass_seen = TRUE;
1826 RB_DEBUG_COUNTER_INC(cvar_include_invalidate);
1827 ruby_vm_global_cvar_state++;
1828 tbl = RCLASS_M_TBL(module);
1829 if (tbl && rb_id_table_size(tbl)) {
1832 rb_id_table_foreach(tbl, clear_module_cache_i, (
void *)super_class);
1837 rb_id_table_foreach(tbl, clear_module_cache_i, (
void *)original_klass);
1844 iclass = rb_include_class_new(module, super_class);
1845 c = rb_class_set_super(c, iclass);
1846 RCLASS_SET_INCLUDER(iclass, klass);
1847 if (module != RCLASS_ORIGIN(module)) {
1849 VALUE origin[2] = {iclass, RCLASS_ORIGIN(module)};
1852 else if (origin_stack && (origin_len =
RARRAY_LEN(origin_stack)) > 1 &&
1853 RARRAY_AREF(origin_stack, origin_len - 1) == module) {
1854 RCLASS_WRITE_ORIGIN(
RARRAY_AREF(origin_stack, (origin_len -= 2)), iclass);
1855 RICLASS_WRITE_ORIGIN_SHARED_MTBL(iclass);
1861 rb_module_add_to_subclasses_list(m, iclass);
1864 VALUE refined_class =
1865 rb_refinement_module_get_refined_class(klass);
1867 rb_id_table_foreach(RCLASS_M_TBL(module), add_refined_method_entry_i, (
void *)refined_class);
1871 tbl = RCLASS_CONST_TBL(module);
1872 if (tbl && rb_id_table_size(tbl))
1873 rb_id_table_foreach(tbl, clear_constant_cache_i, NULL);
1875 module = RCLASS_SUPER(module);
1878 return method_changed;
1882include_modules_at(
const VALUE klass,
VALUE c,
VALUE module,
int search_super)
1884 return do_include_modules_at(klass, c, module, search_super,
true);
1887static enum rb_id_table_iterator_result
1888move_refined_method(
ID key,
VALUE value,
void *data)
1892 if (me->def->type == VM_METHOD_TYPE_REFINED) {
1894 struct rb_id_table *tbl = RCLASS_WRITABLE_M_TBL(klass);
1896 if (me->def->body.refined.orig_me) {
1898 RB_OBJ_WRITE(me, &me->def->body.refined.orig_me, NULL);
1899 new_me = rb_method_entry_clone(me);
1900 rb_method_table_insert(klass, tbl, key, new_me);
1901 rb_method_entry_copy(me, orig_me);
1902 return ID_TABLE_CONTINUE;
1905 rb_method_table_insert(klass, tbl, key, me);
1906 return ID_TABLE_DELETE;
1910 return ID_TABLE_CONTINUE;
1914static enum rb_id_table_iterator_result
1915cache_clear_refined_method(
ID key,
VALUE value,
void *data)
1919 if (me->def->type == VM_METHOD_TYPE_REFINED && me->def->body.refined.orig_me) {
1921 rb_clear_method_cache(klass, me->called_id);
1926 return ID_TABLE_CONTINUE;
1930ensure_origin(
VALUE klass)
1932 VALUE origin = RCLASS_ORIGIN(klass);
1933 if (origin == klass) {
1934 origin = class_alloc(
T_ICLASS, klass);
1935 RCLASS_SET_M_TBL(origin, RCLASS_M_TBL(klass));
1937 rb_class_set_super(klass, origin);
1938 RCLASS_WRITE_ORIGIN(klass, origin);
1942 rb_gc_writebarrier_remember(origin);
1944 class_clear_method_table(klass);
1945 rb_id_table_foreach(RCLASS_M_TBL(origin), cache_clear_refined_method, (
void *)klass);
1946 rb_id_table_foreach(RCLASS_M_TBL(origin), move_refined_method, (
void *)klass);
1956 bool klass_had_no_origin;
1958 ensure_includable(klass, module);
1959 if (module_in_super_chain(klass, module))
1960 rb_raise(rb_eArgError,
"cyclic prepend detected");
1962 klass_had_no_origin = ensure_origin(klass);
1963 changed = do_include_modules_at(klass, klass, module, FALSE,
false);
1966 rb_vm_check_redefinition_by_prepend(klass);
1970 VALUE klass_origin = RCLASS_ORIGIN(klass);
1971 struct rb_id_table *klass_m_tbl = RCLASS_M_TBL(klass);
1972 struct rb_id_table *klass_origin_m_tbl = RCLASS_M_TBL(klass_origin);
1976 if (!rb_objspace_garbage_object_p(iclass->klass)) {
1977 const VALUE subclass = iclass->klass;
1978 if (klass_had_no_origin && klass_origin_m_tbl == RCLASS_M_TBL(subclass)) {
1980 rb_id_table_foreach(RCLASS_M_TBL(subclass), clear_module_cache_i, (
void *)subclass);
1981 RCLASS_WRITE_M_TBL(subclass, klass_m_tbl);
1983 rb_class_set_super(subclass, origin);
1984 RCLASS_SET_INCLUDER(origin, RCLASS_INCLUDER(subclass));
1985 RCLASS_WRITE_ORIGIN(subclass, origin);
1986 RICLASS_SET_ORIGIN_SHARED_MTBL(origin);
1988 include_modules_at(subclass, subclass, module, FALSE);
1991 iclass = iclass->next;
2023 VALUE origin = RCLASS_ORIGIN(mod);
2027 VALUE m = METACLASS_OF(p);
2062 if (METACLASS_OF(p) == mod2)
return Qtrue;
2092 refined_class = rb_refinement_module_get_refined_class(mod);
2096 if (p == refined_class)
break;
2097 if (p != RCLASS_ORIGIN(p))
continue;
2113 bool immediate_only;
2117class_descendants_recursive(
VALUE klass,
VALUE v)
2123 if (!RCLASS_SINGLETON_P(klass)) {
2124 if (data->buffer && data->count < data->maxcount && !rb_objspace_garbage_object_p(klass)) {
2129 if (data->immediate_only)
return;
2131 rb_class_foreach_subclass(klass, class_descendants_recursive, v);
2135class_descendants(
VALUE klass,
bool immediate_only)
2140 rb_class_foreach_subclass(klass, class_descendants_recursive, (
VALUE) &data);
2144 data.maxcount = data.count;
2147 size_t gc_count = rb_gc_count();
2150 rb_class_foreach_subclass(klass, class_descendants_recursive, (
VALUE) &data);
2152 if (gc_count != rb_gc_count()) {
2153 rb_bug(
"GC must not occur during the subclass iteration of Class#descendants");
2201 return class_descendants(klass,
true);
2224 if (!RCLASS_SINGLETON_P(klass)) {
2225 rb_raise(
rb_eTypeError,
"'%"PRIsVALUE
"' is not a singleton class", klass);
2228 return RCLASS_ATTACHED_OBJECT(klass);
2232ins_methods_push(st_data_t name, st_data_t ary)
2238ins_methods_i(st_data_t name, st_data_t
type, st_data_t ary)
2240 switch ((rb_method_visibility_t)
type) {
2241 case METHOD_VISI_UNDEF:
2242 case METHOD_VISI_PRIVATE:
2245 ins_methods_push(name, ary);
2252ins_methods_type_i(st_data_t name, st_data_t
type, st_data_t ary, rb_method_visibility_t visi)
2254 if ((rb_method_visibility_t)
type == visi) {
2255 ins_methods_push(name, ary);
2261ins_methods_prot_i(st_data_t name, st_data_t
type, st_data_t ary)
2263 return ins_methods_type_i(name,
type, ary, METHOD_VISI_PROTECTED);
2267ins_methods_priv_i(st_data_t name, st_data_t
type, st_data_t ary)
2269 return ins_methods_type_i(name,
type, ary, METHOD_VISI_PRIVATE);
2273ins_methods_pub_i(st_data_t name, st_data_t
type, st_data_t ary)
2275 return ins_methods_type_i(name,
type, ary, METHOD_VISI_PUBLIC);
2279ins_methods_undef_i(st_data_t name, st_data_t
type, st_data_t ary)
2281 return ins_methods_type_i(name,
type, ary, METHOD_VISI_UNDEF);
2289static enum rb_id_table_iterator_result
2290method_entry_i(
ID key,
VALUE value,
void *data)
2294 rb_method_visibility_t
type;
2296 if (me->def->type == VM_METHOD_TYPE_REFINED) {
2297 VALUE owner = me->owner;
2298 me = rb_resolve_refined_method(
Qnil, me);
2299 if (!me)
return ID_TABLE_CONTINUE;
2300 if (!arg->recur && me->owner != owner)
return ID_TABLE_CONTINUE;
2302 if (!st_is_member(arg->list, key)) {
2303 if (UNDEFINED_METHOD_ENTRY_P(me)) {
2304 type = METHOD_VISI_UNDEF;
2307 type = METHOD_ENTRY_VISI(me);
2310 st_add_direct(arg->list, key, (st_data_t)
type);
2312 return ID_TABLE_CONTINUE;
2320 rb_id_table_foreach(m_tbl, method_entry_i, me_arg);
2324particular_class_p(
VALUE mod)
2326 if (!mod)
return false;
2327 if (RCLASS_SINGLETON_P(mod))
return true;
2333class_instance_method_list(
int argc,
const VALUE *argv,
VALUE mod,
int obj,
int (*func) (st_data_t, st_data_t, st_data_t))
2336 int recur = TRUE, prepended = 0;
2341 me_arg.list = st_init_numtable();
2342 me_arg.recur = recur;
2345 for (; particular_class_p(mod); mod =
RCLASS_SUPER(mod)) {
2346 add_instance_method_list(mod, &me_arg);
2350 if (!recur && RCLASS_ORIGIN(mod) != mod) {
2351 mod = RCLASS_ORIGIN(mod);
2356 add_instance_method_list(mod, &me_arg);
2361 st_foreach(me_arg.list, func, ary);
2362 st_free_table(me_arg.list);
2406 return class_instance_method_list(argc, argv, mod, 0, ins_methods_i);
2421 return class_instance_method_list(argc, argv, mod, 0, ins_methods_prot_i);
2444 return class_instance_method_list(argc, argv, mod, 0, ins_methods_priv_i);
2459 return class_instance_method_list(argc, argv, mod, 0, ins_methods_pub_i);
2471rb_class_undefined_instance_methods(
VALUE mod)
2474 return class_instance_method_list(1, &include_super, mod, 0, ins_methods_undef_i);
2508rb_obj_methods(
int argc,
const VALUE *argv,
VALUE obj)
2511 if (argc > 0 && !
RTEST(argv[0])) {
2514 return class_instance_method_list(argc, argv,
CLASS_OF(obj), 1, ins_methods_i);
2527rb_obj_protected_methods(
int argc,
const VALUE *argv,
VALUE obj)
2529 return class_instance_method_list(argc, argv,
CLASS_OF(obj), 1, ins_methods_prot_i);
2542rb_obj_private_methods(
int argc,
const VALUE *argv,
VALUE obj)
2544 return class_instance_method_list(argc, argv,
CLASS_OF(obj), 1, ins_methods_priv_i);
2557rb_obj_public_methods(
int argc,
const VALUE *argv,
VALUE obj)
2559 return class_instance_method_list(argc, argv,
CLASS_OF(obj), 1, ins_methods_pub_i);
2598 VALUE ary, klass, origin;
2608 origin = RCLASS_ORIGIN(klass);
2609 me_arg.list = st_init_numtable();
2610 me_arg.recur = recur;
2611 if (klass && RCLASS_SINGLETON_P(klass)) {
2612 if ((mtbl = RCLASS_M_TBL(origin)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2617 if (klass != origin && (mtbl = RCLASS_M_TBL(klass)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
2622 st_foreach(me_arg.list, ins_methods_i, ary);
2623 st_free_table(me_arg.list);
2636#ifdef rb_define_method_id
2637#undef rb_define_method_id
2642 rb_add_method_cfunc(klass, mid, func, argc, METHOD_VISI_PUBLIC);
2645#ifdef rb_define_method
2646#undef rb_define_method
2651 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PUBLIC);
2654#ifdef rb_define_protected_method
2655#undef rb_define_protected_method
2660 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PROTECTED);
2663#ifdef rb_define_private_method
2664#undef rb_define_private_method
2669 rb_add_method_cfunc(klass, rb_intern(name), func, argc, METHOD_VISI_PRIVATE);
2675 rb_add_method(klass, rb_intern(name), VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2678static enum rb_id_table_iterator_result
2679undef_method_i(
ID name,
VALUE value,
void *data)
2682 rb_add_method(klass, name, VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_UNDEF);
2683 return ID_TABLE_CONTINUE;
2687rb_undef_methods_from(
VALUE klass,
VALUE super)
2691 rb_id_table_foreach(mtbl, undef_method_i, (
void *)klass);
2704special_singleton_class_of(
VALUE obj)
2710 default:
return Qnil;
2715rb_special_singleton_class(
VALUE obj)
2717 return special_singleton_class_of(obj);
2730singleton_class_of(
VALUE obj,
bool ensure_eigenclass)
2734 switch (
TYPE(obj)) {
2744 klass = special_singleton_class_of(obj);
2746 rb_bug(
"unknown immediate %p", (
void *)obj);
2750 if (CHILLED_STRING_P(obj)) {
2751 CHILLED_STRING_MUTATED(obj);
2761 RB_VM_LOCK_ENTER_LEV(&lev);
2764 klass = METACLASS_OF(obj);
2765 if (!(RCLASS_SINGLETON_P(klass) &&
2766 RCLASS_ATTACHED_OBJECT(klass) == obj)) {
2767 klass = rb_make_metaclass(obj, klass);
2776 RB_VM_LOCK_LEAVE_LEV(&lev);
2792 (RCLASS_ATTACHED_OBJECT(klass) == attached_object)) {
2793 attached_object = klass;
2811 return rb_special_singleton_class(obj);
2813 klass = METACLASS_OF(obj);
2814 if (!RCLASS_SINGLETON_P(klass))
return Qnil;
2815 if (RCLASS_ATTACHED_OBJECT(klass) != obj)
return Qnil;
2822 return singleton_class_of(obj,
true);
2834#ifdef rb_define_singleton_method
2835#undef rb_define_singleton_method
2843#ifdef rb_define_module_function
2844#undef rb_define_module_function
2853#ifdef rb_define_global_function
2854#undef rb_define_global_function
2865 rb_alias(klass, rb_intern(name1), rb_intern(name2));
2871 rb_attr(klass, rb_intern(name), read, write, FALSE);
2875rb_keyword_error_new(
const char *error,
VALUE keys)
2878 VALUE error_message = rb_sprintf(
"%s keyword%.*s", error,
len > 1,
"s");
2885 if (++i >=
len)
break;
2893NORETURN(
static void rb_keyword_error(
const char *error,
VALUE keys));
2895rb_keyword_error(
const char *error,
VALUE keys)
2900NORETURN(
static void unknown_keyword_error(
VALUE hash,
const ID *table,
int keywords));
2902unknown_keyword_error(
VALUE hash,
const ID *table,
int keywords)
2905 for (i = 0; i < keywords; i++) {
2906 st_data_t key =
ID2SYM(table[i]);
2907 rb_hash_stlike_delete(hash, &key, NULL);
2909 rb_keyword_error(
"unknown", rb_hash_keys(hash));
2914separate_symbol(st_data_t key, st_data_t value, st_data_t arg)
2918 if (!*kwdhash) *kwdhash = rb_hash_new();
2919 rb_hash_aset(*kwdhash, (
VALUE)key, (
VALUE)value);
2926 VALUE parthash[2] = {0, 0};
2927 VALUE hash = *orighash;
2934 *orighash = parthash[1];
2949#define extract_kwarg(keyword, val) \
2950 (key = (st_data_t)(keyword), values ? \
2951 (rb_hash_stlike_delete(keyword_hash, &key, &(val)) || ((val) = Qundef, 0)) : \
2952 rb_hash_stlike_lookup(keyword_hash, key, NULL))
2954 if (
NIL_P(keyword_hash)) keyword_hash = 0;
2958 optional = -1-optional;
2961 for (; i < required; i++) {
2964 if (extract_kwarg(keyword, values[i])) {
2971 if (!
NIL_P(missing)) {
2972 rb_keyword_error(
"missing", missing);
2976 if (optional && keyword_hash) {
2977 for (i = 0; i < optional; i++) {
2978 if (extract_kwarg(
ID2SYM(table[required+i]), values[required+i])) {
2983 if (!rest && keyword_hash) {
2984 if (
RHASH_SIZE(keyword_hash) > (
unsigned int)(values ? 0 : j)) {
2985 unknown_keyword_error(keyword_hash, table, required+optional);
2988 if (values && !keyword_hash) {
2989 for (i = 0; i < required + optional; i++) {
3008rb_scan_args_parse(
int kw_flag,
const char *fmt,
struct rb_scan_args_t *arg)
3010 const char *p = fmt;
3012 memset(arg, 0,
sizeof(*arg));
3013 arg->kw_flag = kw_flag;
3016 arg->n_lead = *p -
'0';
3019 arg->n_opt = *p -
'0';
3028 arg->n_trail = *p -
'0';
3040 rb_fatal(
"bad scan arg format: %s", fmt);
3045rb_scan_args_assign(
const struct rb_scan_args_t *arg,
int argc,
const VALUE *
const argv, va_list vargs)
3049#define rb_scan_args_next_param() va_arg(vargs, VALUE *)
3050 const int kw_flag = arg->kw_flag;
3051 const int n_lead = arg->n_lead;
3052 const int n_opt = arg->n_opt;
3053 const int n_trail = arg->n_trail;
3054 const int n_mand = n_lead + n_trail;
3055 const bool f_var = arg->f_var;
3056 const bool f_hash = arg->f_hash;
3057 const bool f_block = arg->f_block;
3060 if (f_hash && argc > 0) {
3061 VALUE last = argv[argc - 1];
3062 if (rb_scan_args_keyword_p(kw_flag, last)) {
3063 hash = rb_hash_dup(last);
3068 if (argc < n_mand) {
3073 for (i = 0; i < n_lead; i++) {
3074 var = rb_scan_args_next_param();
3075 if (var) *var = argv[argi];
3079 for (i = 0; i < n_opt; i++) {
3080 var = rb_scan_args_next_param();
3081 if (argi < argc - n_trail) {
3082 if (var) *var = argv[argi];
3086 if (var) *var =
Qnil;
3091 int n_var = argc - argi - n_trail;
3093 var = rb_scan_args_next_param();
3103 for (i = 0; i < n_trail; i++) {
3104 var = rb_scan_args_next_param();
3105 if (var) *var = argv[argi];
3110 var = rb_scan_args_next_param();
3111 if (var) *var = hash;
3115 var = rb_scan_args_next_param();
3130#undef rb_scan_args_next_param
3134rb_scan_args_result(
const struct rb_scan_args_t *
const arg,
int argc)
3136 const int n_lead = arg->n_lead;
3137 const int n_opt = arg->n_opt;
3138 const int n_trail = arg->n_trail;
3139 const int n_mand = n_lead + n_trail;
3140 const bool f_var = arg->f_var;
3158 va_start(vargs,fmt);
3159 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
3161 return rb_scan_args_result(&arg, argc);
3164#undef rb_scan_args_kw
3170 rb_scan_args_parse(kw_flag, fmt, &arg);
3171 va_start(vargs,fmt);
3172 argc = rb_scan_args_assign(&arg, argc, argv, vargs);
3174 return rb_scan_args_result(&arg, argc);
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_method_id(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
#define rb_define_protected_method(klass, mid, func, arity)
Defines klass#mid and makes it protected.
#define rb_define_module_function(klass, mid, func, arity)
Defines klass#mid and makes it a module function.
#define rb_define_private_method(klass, mid, func, arity)
Defines klass#mid and makes it private.
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
#define RUBY_EXTERN
Declaration of externally visible global variables.
static VALUE RB_OBJ_FROZEN_RAW(VALUE obj)
This is an implementation detail of RB_OBJ_FROZEN().
static void RB_FL_SET_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_SET().
VALUE rb_class_protected_instance_methods(int argc, const VALUE *argv, VALUE mod)
Identical to rb_class_instance_methods(), except it returns names of methods that are protected only.
static VALUE class_alloc0(enum ruby_value_type type, VALUE klass, bool boxable)
Allocates a struct RClass for a new class, iclass, or module.
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
VALUE rb_refinement_new(void)
Creates a new, anonymous refinement.
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
VALUE rb_class_new(VALUE super)
Creates a new, anonymous class.
static VALUE make_singleton_class(VALUE obj)
Creates a singleton class for obj.
VALUE rb_singleton_class_clone(VALUE obj)
Clones a singleton class.
void rb_prepend_module(VALUE klass, VALUE module)
Identical to rb_include_module(), except it "prepends" the passed module to the klass,...
VALUE rb_class_subclasses(VALUE klass)
Queries the class's direct descendants.
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
VALUE rb_class_attached_object(VALUE klass)
Returns the attached object for a singleton class.
VALUE rb_obj_singleton_methods(int argc, const VALUE *argv, VALUE obj)
Identical to rb_class_instance_methods(), except it returns names of singleton methods instead of ins...
VALUE rb_module_new(void)
Creates a new, anonymous module.
#define META_CLASS_OF_CLASS_CLASS_P(k)
whether k is a meta^(n)-class of Class class
VALUE rb_class_instance_methods(int argc, const VALUE *argv, VALUE mod)
Generates an array of symbols, which are the list of method names defined in the passed class.
void rb_check_inheritable(VALUE super)
Asserts that the given class can derive a child class.
VALUE rb_class_public_instance_methods(int argc, const VALUE *argv, VALUE mod)
Identical to rb_class_instance_methods(), except it returns names of methods that are public only.
VALUE rb_class_boot(VALUE super)
A utility function that wraps class_alloc.
VALUE rb_define_module(const char *name)
Defines a top-level module.
void rb_class_modify_check(VALUE klass)
Asserts that klass is not a frozen class.
VALUE rb_define_module_id_under(VALUE outer, ID id)
Identical to rb_define_module_under(), except it takes the name in ID instead of C's string.
void rb_singleton_class_attached(VALUE klass, VALUE obj)
Attaches a singleton class to its corresponding object.
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_singleton_class_get(VALUE obj)
Returns the singleton class of obj, or nil if obj is not a singleton object.
VALUE rb_define_module_id(ID id)
This is a very badly designed API that creates an anonymous module.
VALUE rb_define_class_id(ID id, VALUE super)
This is a very badly designed API that creates an anonymous class.
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
VALUE rb_extract_keywords(VALUE *orighash)
Splits a hash into two.
void rb_define_attr(VALUE klass, const char *name, int read, int write)
Defines public accessor method(s) for an attribute.
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
int rb_scan_args_kw(int kw_flag, int argc, const VALUE *argv, const char *fmt,...)
Identical to rb_scan_args(), except it also accepts kw_splat.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
int rb_block_given_p(void)
Determines if the current method is given a block.
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
#define TYPE(_)
Old name of rb_type.
#define FL_SINGLETON
Old name of RUBY_FL_SINGLETON.
#define OBJ_INIT_COPY(obj, orig)
Old name of RB_OBJ_INIT_COPY.
#define ALLOC
Old name of RB_ALLOC.
#define T_STRING
Old name of RUBY_T_STRING.
#define xfree
Old name of ruby_xfree.
#define Qundef
Old name of RUBY_Qundef.
#define T_NIL
Old name of RUBY_T_NIL.
#define T_FLOAT
Old name of RUBY_T_FLOAT.
#define ID2SYM
Old name of RB_ID2SYM.
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define ZALLOC
Old name of RB_ZALLOC.
#define FL_SHAREABLE
Old name of RUBY_FL_SHAREABLE.
#define CLASS_OF
Old name of rb_class_of.
#define xmalloc
Old name of ruby_xmalloc.
#define T_MODULE
Old name of RUBY_T_MODULE.
#define ISDIGIT
Old name of rb_isdigit.
#define T_TRUE
Old name of RUBY_T_TRUE.
#define T_ICLASS
Old name of RUBY_T_ICLASS.
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
#define FL_SET
Old name of RB_FL_SET.
#define T_FALSE
Old name of RUBY_T_FALSE.
#define Qtrue
Old name of RUBY_Qtrue.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define NIL_P
Old name of RB_NIL_P.
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
#define T_CLASS
Old name of RUBY_T_CLASS.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
#define FL_TEST
Old name of RB_FL_TEST.
#define CONST_ID
Old name of RUBY_CONST_ID.
#define rb_ary_new2
Old name of rb_ary_new_capa.
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
#define OBJ_FROZEN_RAW
Old name of RB_OBJ_FROZEN_RAW.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Identical to rb_exc_new_cstr(), except it takes a Ruby's string instead of C's.
VALUE rb_cClass
Class class.
VALUE rb_mKernel
Kernel module.
VALUE rb_cObject
Object class.
VALUE rb_cRefinement
Refinement class.
VALUE rb_cNilClass
NilClass class.
VALUE rb_cHash
Hash class.
VALUE rb_cFalseClass
FalseClass class.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
VALUE rb_cBasicObject
BasicObject class.
VALUE rb_cModule
Module class.
VALUE rb_class_real(VALUE klass)
Finds a "real" class.
VALUE rb_cTrueClass
TrueClass class.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_ary_new_from_values(long n, const VALUE *elts)
Identical to rb_ary_new_from_args(), except how objects are passed.
VALUE rb_ary_cat(VALUE ary, const VALUE *train, long len)
Destructively appends multiple elements at the end of the array.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_resize(VALUE ary, long len)
Expands or shrinks the passed array to the passed length.
VALUE rb_ary_hidden_new(long capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
#define UNLIMITED_ARGUMENTS
This macro is used in conjunction with rb_check_arity().
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
#define rb_str_cat_cstr(buf, str)
Identical to rb_str_cat(), except it assumes the passed pointer is a pointer to a C string.
VALUE rb_const_get(VALUE space, ID name)
Identical to rb_const_defined(), except it returns the actual defined value.
void rb_const_set(VALUE space, ID name, VALUE val)
Names a constant.
VALUE rb_const_get_at(VALUE space, ID name)
Identical to rb_const_defined_at(), except it returns the actual defined value.
void rb_set_class_path_string(VALUE klass, VALUE space, VALUE name)
Identical to rb_set_class_path(), except it accepts the name as Ruby's string instead of C's.
int rb_const_defined_at(VALUE space, ID name)
Identical to rb_const_defined(), except it doesn't look for parent classes.
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
int rb_const_defined(VALUE space, ID name)
Queries if the constant is defined at the namespace.
void rb_alias(VALUE klass, ID dst, ID src)
Resembles alias.
void rb_attr(VALUE klass, ID name, int need_reader, int need_writer, int honour_visibility)
This function resembles now-deprecated Module#attr.
void rb_clear_constant_cache_for_id(ID id)
Clears the inline constant caches associated with a particular ID.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
int len
Length of the buffer.
static bool rb_ractor_shareable_p(VALUE obj)
Queries if multiple Ractors can share the passed object or not.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
VALUE type(ANYARGS)
ANYARGS-ed function type.
void rb_hash_foreach(VALUE q, int_type *w, VALUE e)
Iteration over the given hash.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
#define RCLASS_SUPER
Just another name of rb_class_get_superclass.
#define RHASH_SIZE(h)
Queries the size of the hash.
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
#define RB_SCAN_ARGS_PASS_CALLED_KEYWORDS
Same behaviour as rb_scan_args().
#define RTEST
This is an old name of RB_TEST.
#define ANYARGS
Functions declared using this macro take arbitrary arguments, including void.
Internal header for Ruby Box.
rb_cref_t * cref
class reference, should be marked
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
Internal header for Class.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
ruby_value_type
C-level type of an object.