14#include "ruby/internal/config.h"
17#include "ccan/list/list.h"
19#include "debug_counter.h"
23#include "internal/class.h"
24#include "internal/compilers.h"
25#include "internal/error.h"
26#include "internal/eval.h"
27#include "internal/hash.h"
28#include "internal/namespace.h"
29#include "internal/object.h"
30#include "internal/gc.h"
31#include "internal/re.h"
32#include "internal/symbol.h"
33#include "internal/thread.h"
34#include "internal/variable.h"
42#include "ractor_core.h"
46#define GET_GLOBAL_CVAR_STATE() (ruby_vm_global_cvar_state)
48typedef void rb_gvar_compact_t(
void *var);
56static VALUE autoload_features;
61static VALUE autoload_mutex;
63static void check_before_mod_set(
VALUE,
ID,
VALUE,
const char *);
65static VALUE rb_const_search(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility);
68typedef int rb_ivar_foreach_callback_func(
ID key,
VALUE val, st_data_t arg);
69static void rb_field_foreach(
VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg,
bool ivar_only);
74 rb_global_tbl = rb_id_table_create(0);
75 generic_fields_tbl_ = st_init_numtable();
80 rb_vm_register_global_object(autoload_mutex);
82 autoload_features = rb_ident_hash_new();
84 rb_vm_register_global_object(autoload_features);
88rb_namespace_p(
VALUE obj)
109classname(
VALUE klass,
bool *permanent)
113 VALUE classpath = RCLASS_CLASSPATH(klass);
114 if (classpath == 0)
return Qnil;
116 *permanent = RCLASS_PERMANENT_CLASSPATH_P(klass);
122rb_mod_name0(
VALUE klass,
bool *permanent)
124 return classname(klass, permanent);
139 return classname(mod, &permanent);
144is_constant_path(
VALUE name)
146 const char *path = RSTRING_PTR(name);
150 const char *p = path;
152 if (p >= pend || !*p) {
157 if (p + 2 <= pend && p[0] ==
':' && p[1] ==
':') {
161 const char *pbeg = p;
162 while (p < pend && *p !=
':') p++;
164 if (pbeg == p)
return false;
166 if (rb_enc_symname_type(pbeg, p - pbeg, enc, 0) != ID_CONST) {
183set_sub_temporary_name_recursive(
VALUE mod,
VALUE data,
int recursive)
185 if (recursive)
return Qfalse;
190 name = build_const_path(rb_ary_last(0, 0, args->names), args->last);
192 set_sub_temporary_name_foreach(mod, args, name);
197set_sub_temporary_name_topmost(
VALUE mod,
VALUE data,
int recursive)
199 if (recursive)
return Qfalse;
202 VALUE name = args->names;
206 set_sub_temporary_name_foreach(mod, args, name);
210static enum rb_id_table_iterator_result
211set_sub_temporary_name_i(
ID id,
VALUE val,
void *data)
214 if (rb_namespace_p(val) && !RCLASS_PERMANENT_CLASSPATH_P(val)) {
220 return ID_TABLE_CONTINUE;
226 RCLASS_WRITE_CLASSPATH(mod, name, FALSE);
230 rb_id_table_foreach(tbl, set_sub_temporary_name_i, args);
235 rb_id_table_foreach(tbl, set_sub_temporary_name_i, args);
236 rb_ary_set_len(args->names, names_len);
297rb_mod_set_temporary_name(
VALUE mod,
VALUE name)
300 if (RCLASS_PERMANENT_CLASSPATH_P(mod)) {
306 RB_VM_LOCKING() { set_sub_temporary_name(mod, 0);}
312 if (RSTRING_LEN(name) == 0) {
313 rb_raise(rb_eArgError,
"empty class/module name");
316 if (is_constant_path(name)) {
317 rb_raise(rb_eArgError,
"the temporary name must not be a constant path to avoid confusion");
323 RB_VM_LOCKING() { set_sub_temporary_name(mod, name);}
335 path = rb_sprintf(
"#<Class:%p>", (
void*)obj);
338 path = rb_sprintf(
"#<Module:%p>", (
void*)obj);
341 path = rb_sprintf(
"#<%"PRIsVALUE
":%p>", klass, (
void*)obj);
351rb_tmp_class_path(
VALUE klass,
bool *permanent, fallback_func fallback)
353 VALUE path = classname(klass, permanent);
365 path = rb_tmp_class_path(
RBASIC(klass)->klass, &perm, fallback);
370 return fallback(klass, path);
377 VALUE path = rb_tmp_class_path(klass, &permanent, make_temporary_path);
395rb_search_class_path(
VALUE klass)
398 return rb_tmp_class_path(klass, &permanent, no_fallback);
407 return rb_fstring(path);
411build_const_path(
VALUE head,
ID tail)
413 return build_const_pathname(head, rb_id2str(tail));
419 bool permanent =
true;
422 if (under == rb_cObject) {
426 str = rb_tmp_class_path(under, &permanent, make_temporary_path);
427 str = build_const_pathname(str, name);
430 RCLASS_SET_CLASSPATH(klass, str, permanent);
445 const char *pbeg, *pend, *p, *path = RSTRING_PTR(pathname);
447 VALUE c = rb_cObject;
449 if (!rb_enc_asciicompat(enc)) {
450 rb_raise(rb_eArgError,
"invalid class path encoding (non ASCII)");
453 pend = path + RSTRING_LEN(pathname);
454 if (path == pend || path[0] ==
'#') {
455 rb_raise(rb_eArgError,
"can't retrieve anonymous class %"PRIsVALUE,
459 while (p < pend && *p !=
':') p++;
461 if (p < pend && p[0] ==
':') {
462 if ((
size_t)(pend - p) < 2 || p[1] !=
':')
goto undefined_class;
467 goto undefined_class;
469 c = rb_const_search(c,
id, TRUE, FALSE, FALSE);
470 if (UNDEF_P(c))
goto undefined_class;
471 if (!rb_namespace_p(c)) {
472 rb_raise(
rb_eTypeError,
"%"PRIsVALUE
" does not refer to class/module",
481 rb_raise(rb_eArgError,
"undefined class/module % "PRIsVALUE,
502 VALUE path = rb_tmp_class_path(
rb_class_real(klass), &permanent, make_temporary_path);
503 if (
NIL_P(path))
return NULL;
504 return RSTRING_PTR(path);
527 rb_gvar_compact_t *compactor;
529 bool namespace_ready;
552static enum rb_id_table_iterator_result
553free_global_entry_i(
VALUE val,
void *arg)
556 entry->var->counter--;
557 if (entry->var->counter == 0) {
558 free_global_variable(entry->var);
561 return ID_TABLE_DELETE;
565rb_free_rb_global_tbl(
void)
567 rb_id_table_foreach_values(rb_global_tbl, free_global_entry_i, 0);
568 rb_id_table_free(rb_global_tbl);
572rb_free_generic_fields_tbl_(
void)
574 st_free_table(generic_fields_tbl_);
578rb_find_global_entry(
ID id)
583 if (!rb_id_table_lookup(rb_global_tbl,
id, &data)) {
591 if (UNLIKELY(!rb_ractor_main_p()) && (!entry || !entry->ractor_local)) {
592 rb_raise(rb_eRactorIsolationError,
"can not access global variables %s from non-main Ractors", rb_id2name(
id));
599rb_gvar_ractor_local(
const char *name)
602 entry->ractor_local =
true;
606rb_gvar_namespace_ready(
const char *name)
609 entry->var->namespace_ready =
true;
613rb_gvar_undef_compactor(
void *var)
627 entry->ractor_local =
false;
633 var->compactor = rb_gvar_undef_compactor;
635 var->block_trace = 0;
637 var->namespace_ready =
false;
638 rb_id_table_insert(rb_global_tbl,
id, (
VALUE)entry);
646 rb_warning(
"global variable '%"PRIsVALUE
"' not initialized", QUOTE_ID(
id));
652rb_gvar_val_compactor(
void *_var)
659 VALUE new = rb_gc_location(obj);
661 var->data = (
void*)
new;
673 var->compactor = rb_gvar_val_compactor;
675 var->data = (
void*)val;
693 var->data = (
void*)val;
700 if (data) rb_gc_mark_movable(data);
706 if (!var)
return Qnil;
719 if (var) rb_gc_mark_maybe(*var);
725 rb_name_error(
id,
"%"PRIsVALUE
" is a read-only variable", QUOTE_ID(
id));
728static enum rb_id_table_iterator_result
729mark_global_entry(
VALUE v,
void *ignored)
735 (*var->marker)(var->data);
738 if (trace->data) rb_gc_mark_maybe(trace->data);
741 return ID_TABLE_CONTINUE;
744#define gc_mark_table(task) \
745 if (rb_global_tbl) { rb_id_table_foreach_values(rb_global_tbl, task##_global_entry, 0); }
748rb_gc_mark_global_tbl(
void)
753static enum rb_id_table_iterator_result
754update_global_entry(
VALUE v,
void *ignored)
759 (*var->compactor)(var);
760 return ID_TABLE_CONTINUE;
764rb_gc_update_global_tbl(
void)
766 gc_mark_table(update);
770global_id(
const char *name)
774 if (name[0] ==
'$')
id = rb_intern(name);
776 size_t len = strlen(name);
780 memcpy(buf+1, name,
len);
781 id = rb_intern2(buf,
len+1);
788find_global_id(
const char *name)
791 size_t len = strlen(name);
793 if (name[0] ==
'$') {
800 memcpy(buf+1, name,
len);
816 ID id = global_id(name);
819 gvar->data = (
void*)var;
871 trace->next = entry->var->trace;
872 trace->func = rb_trace_eval;
875 entry->var->trace = trace;
889 while (trace->next) {
892 trace->next = next->next;
915 if ((entry = rb_find_global_entry(
id)) == NULL) {
916 rb_name_error(
id,
"undefined global variable %"PRIsVALUE
"", QUOTE_ID(
id));
919 trace = entry->var->trace;
930 if (!entry->var->block_trace) remove_trace(entry->var);
935 if (trace->data == cmd) {
937 if (!entry->var->block_trace) remove_trace(entry->var);
958 (*trace->func)(trace->data, data->val);
969 var->block_trace = 0;
980 (*var->setter)(val, entry->id, var->data);
982 if (var->trace && !var->block_trace) {
983 var->block_trace = 1;
984 trace.trace = var->trace;
991#define USE_NAMESPACE_GVAR_TBL(ns,entry) \
992 (NAMESPACE_OPTIONAL_P(ns) && \
993 (!entry || !entry->var->namespace_ready || entry->var->setter != rb_gvar_readonly_setter))
1004 if (USE_NAMESPACE_GVAR_TBL(ns, entry)) {
1005 rb_hash_aset(ns->gvar_tbl,
rb_id2sym(entry->id), val);
1010 retval = rb_gvar_set_entry(entry, val);
1018 return rb_gvar_set(global_id(name), val);
1024 VALUE retval, gvars, key;
1029 if (USE_NAMESPACE_GVAR_TBL(ns, entry)) {
1030 gvars = ns->gvar_tbl;
1032 if (
RTEST(rb_hash_has_key(gvars, key))) {
1033 retval = rb_hash_aref(gvars, key);
1036 retval = (*var->getter)(entry->id, var->data);
1038 retval =
rb_funcall(retval, rb_intern(
"clone"), 0);
1040 rb_hash_aset(gvars, key, retval);
1044 retval = (*var->getter)(entry->id, var->data);
1052 ID id = find_global_id(name);
1055 rb_warning(
"global variable '%s' not initialized", name);
1059 return rb_gvar_get(
id);
1063rb_gvar_defined(
ID id)
1070rb_gvar_getter_function_of(
ID id)
1073 return entry->var->getter;
1077rb_gvar_setter_function_of(
ID id)
1080 return entry->var->setter;
1083static enum rb_id_table_iterator_result
1084gvar_i(
ID key,
VALUE val,
void *a)
1088 return ID_TABLE_CONTINUE;
1097 if (!rb_ractor_main_p()) {
1098 rb_raise(rb_eRactorIsolationError,
"can not access global variables from non-main Ractors");
1102 rb_id_table_foreach(rb_global_tbl, gvar_i, (
void *)ary);
1103 if (!
NIL_P(backref)) {
1105 int i, nmatch = rb_match_count(backref);
1107 for (i = 1; i <= nmatch; ++i) {
1111 buf[1] = (char)(i +
'0');
1112 sym =
ID2SYM(rb_intern2(buf, 2));
1131 if (!rb_ractor_main_p()) {
1132 rb_raise(rb_eRactorIsolationError,
"can not access global variables from non-main Ractors");
1136 if (!rb_id_table_lookup(gtbl, name1, &data1)) {
1139 rb_id_table_insert(gtbl, name1, (
VALUE)entry1);
1141 else if ((entry1 = (
struct rb_global_entry *)data1)->var != entry2->var) {
1143 if (var->block_trace) {
1147 if (var->counter == 0) {
1148 free_global_variable(var);
1154 entry2->var->counter++;
1155 entry1->var = entry2->var;
1159IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(
ID id)
1161 if (UNLIKELY(!rb_ractor_main_p())) {
1163 rb_raise(rb_eRactorIsolationError,
"can not set instance variables of classes/modules by non-main Ractors");
1168#define CVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR() \
1169 if (UNLIKELY(!rb_ractor_main_p())) { \
1170 rb_raise(rb_eRactorIsolationError, "can not access class variables from non-main Ractors"); \
1174generic_fields_tbl(
VALUE obj,
ID id,
bool force_check_ractor)
1176 ASSERT_vm_locking();
1180 UNLIKELY(!rb_ractor_main_p()) &&
1183 rb_raise(rb_eRactorIsolationError,
"can not access instance variables of shareable objects from non-main Ractors");
1185 return generic_fields_tbl_;
1189generic_fields_tbl_no_ractor_check(
VALUE obj)
1191 return generic_fields_tbl(obj, 0,
false);
1195rb_generic_fields_tbl_get(
void)
1197 return generic_fields_tbl_;
1201generic_fields_lookup(
VALUE obj,
ID id,
bool force_check_ractor)
1205 st_table *generic_tbl = generic_fields_tbl(obj,
id,
false);
1206 st_lookup(generic_tbl, obj, (st_data_t *)&fields_obj);
1212generic_fields_insert(
VALUE obj,
VALUE fields_obj)
1214 RUBY_ASSERT(IMEMO_TYPE_P(fields_obj, imemo_fields));
1217 st_table *generic_tbl = generic_fields_tbl_no_ractor_check(obj);
1218 st_insert(generic_tbl, obj, fields_obj);
1224rb_gen_fields_tbl_get(
VALUE obj,
ID id,
VALUE *fields_obj)
1232 if (st_lookup(generic_fields_tbl(obj,
id,
false), (st_data_t)obj, &data)) {
1233 *fields_obj = (
VALUE)data;
1242rb_ivar_generic_fields_tbl_lookup(
VALUE obj,
VALUE *fields_obj)
1244 return rb_gen_fields_tbl_get(obj, 0, fields_obj);
1248rb_mark_generic_ivar(
VALUE obj)
1251 if (st_lookup(generic_fields_tbl_no_ractor_check(obj), (st_data_t)obj, (st_data_t *)&data)) {
1252 rb_gc_mark_movable(data);
1259 if (rb_obj_exivar_p(obj)) {
1260 st_data_t key = (st_data_t)obj, value;
1263 st_delete(generic_fields_tbl_no_ractor_check(obj), &key, &value);
1269rb_obj_field_get(
VALUE obj, shape_id_t target_shape_id)
1272 RUBY_ASSERT(RSHAPE_TYPE_P(target_shape_id, SHAPE_IVAR) || RSHAPE_TYPE_P(target_shape_id, SHAPE_OBJ_ID));
1275 ASSERT_vm_locking();
1276 VALUE field_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
1278 return rb_obj_field_get(field_obj, target_shape_id);
1283 if (rb_shape_too_complex_p(target_shape_id)) {
1288 rb_bug(
"Unreachable");
1291 fields_hash = ROBJECT_FIELDS_HASH(obj);
1295 fields_hash = rb_imemo_fields_complex_tbl(obj);
1299 VALUE fields_obj = 0;
1300 rb_ivar_generic_fields_tbl_lookup(obj, &fields_obj);
1302 fields_hash = rb_imemo_fields_complex_tbl(fields_obj);
1306 st_lookup(fields_hash, RSHAPE_EDGE_NAME(target_shape_id), &value);
1309 if (UNDEF_P(value)) {
1310 rb_bug(
"Object's shape includes object_id, but it's missing %s", rb_obj_info(obj));
1318 attr_index_t attr_index = RSHAPE_INDEX(target_shape_id);
1323 rb_bug(
"Unreachable");
1330 fields = rb_imemo_fields_ptr(obj);
1334 VALUE fields_obj = 0;
1335 rb_ivar_generic_fields_tbl_lookup(obj, &fields_obj);
1337 fields = rb_imemo_fields_ptr(fields_obj);
1340 return fields[attr_index];
1348 shape_id_t shape_id;
1356 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
1358 val = rb_ivar_lookup(fields_obj,
id, undef);
1363 UNLIKELY(!rb_ractor_main_p()) &&
1365 rb_raise(rb_eRactorIsolationError,
1366 "can not get unshareable values from instance variables of classes/modules from non-main Ractors");
1374 shape_id = RBASIC_SHAPE_ID(obj);
1376 if (rb_shape_too_complex_p(shape_id)) {
1377 st_table *iv_table = rb_imemo_fields_complex_tbl(obj);
1379 if (rb_st_lookup(iv_table, (st_data_t)
id, (st_data_t *)&val)) {
1388 ivar_list = rb_imemo_fields_ptr(obj);
1393 shape_id = RBASIC_SHAPE_ID(obj);
1394 if (rb_shape_too_complex_p(shape_id)) {
1395 st_table *iv_table = ROBJECT_FIELDS_HASH(obj);
1397 if (rb_st_lookup(iv_table, (st_data_t)
id, (st_data_t *)&val)) {
1410 shape_id = RBASIC_SHAPE_ID(obj);
1411 if (rb_obj_exivar_p(obj)) {
1412 VALUE fields_obj = 0;
1413 rb_gen_fields_tbl_get(obj,
id, &fields_obj);
1417 if (rb_shape_obj_too_complex_p(fields_obj)) {
1419 if (rb_st_lookup(rb_imemo_fields_complex_tbl(fields_obj), (st_data_t)
id, (st_data_t *)&val)) {
1426 ivar_list = rb_imemo_fields_ptr(fields_obj);
1434 attr_index_t index = 0;
1435 if (rb_shape_get_iv_index(shape_id,
id, &index)) {
1436 return ivar_list[index];
1445 VALUE iv = rb_ivar_lookup(obj,
id,
Qnil);
1446 RB_DEBUG_COUNTER_INC(ivar_get_base);
1453 return rb_ivar_lookup(obj,
id,
Qnil);
1459 rb_check_frozen(obj);
1463 IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(
id);
1465 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
1467 if (rb_multi_ractor_p()) {
1468 fields_obj = rb_imemo_fields_clone(fields_obj);
1469 val = rb_ivar_delete(fields_obj,
id, undef);
1470 RCLASS_WRITABLE_SET_FIELDS_OBJ(obj, fields_obj);
1473 val = rb_ivar_delete(fields_obj,
id, undef);
1479 shape_id_t old_shape_id = rb_obj_shape_id(obj);
1480 if (rb_shape_too_complex_p(old_shape_id)) {
1484 shape_id_t removed_shape_id = 0;
1485 shape_id_t next_shape_id = rb_shape_transition_remove_ivar(obj,
id, &removed_shape_id);
1487 if (next_shape_id == old_shape_id) {
1491 if (UNLIKELY(rb_shape_too_complex_p(next_shape_id))) {
1492 rb_evict_fields_to_hash(obj);
1496 RUBY_ASSERT(RSHAPE_LEN(next_shape_id) == RSHAPE_LEN(old_shape_id) - 1);
1502 rb_bug(
"Unreachable");
1506 fields = rb_imemo_fields_ptr(obj);
1513 rb_gen_fields_tbl_get(obj,
id, &fields_obj);
1514 fields = rb_imemo_fields_ptr(fields_obj);
1519 RUBY_ASSERT(removed_shape_id != INVALID_SHAPE_ID);
1521 attr_index_t removed_index = RSHAPE_INDEX(removed_shape_id);
1522 val = fields[removed_index];
1524 attr_index_t new_fields_count = RSHAPE_LEN(next_shape_id);
1525 if (new_fields_count) {
1526 size_t trailing_fields = new_fields_count - removed_index;
1528 MEMMOVE(&fields[removed_index], &fields[removed_index + 1],
VALUE, trailing_fields);
1544 rb_obj_set_shape_id(obj, next_shape_id);
1554 rb_bug(
"Unreachable");
1559 table = rb_imemo_fields_complex_tbl(obj);
1563 table = ROBJECT_FIELDS_HASH(obj);
1568 if (rb_gen_fields_tbl_get(obj, 0, &fields_obj)) {
1569 table = rb_imemo_fields_complex_tbl(fields_obj);
1576 if (!st_delete(table, (st_data_t *)&
id, (st_data_t *)&val)) {
1586rb_attr_delete(
VALUE obj,
ID id)
1588 return rb_ivar_delete(obj,
id,
Qnil);
1591static inline void generic_update_fields_obj(
VALUE obj,
VALUE fields_obj,
const VALUE original_fields_obj);
1597 return obj_transition_too_complex(RCLASS_WRITABLE_ENSURE_FIELDS_OBJ(obj), table);
1601 shape_id_t shape_id = rb_shape_transition_complex(obj);
1606 VALUE *old_fields = NULL;
1607 if (!(
RBASIC(obj)->flags & ROBJECT_EMBED)) {
1610 RBASIC_SET_SHAPE_ID(obj, shape_id);
1611 ROBJECT_SET_FIELDS_HASH(obj, table);
1619 rb_bug(
"Unreachable");
1624 RBASIC_SET_SHAPE_ID(fields_obj, shape_id);
1627 const VALUE original_fields_obj = generic_fields_lookup(obj, 0,
false);
1628 generic_update_fields_obj(obj, fields_obj, original_fields_obj);
1630 RBASIC_SET_SHAPE_ID(obj, shape_id);
1642 RUBY_ASSERT(rb_shape_canonical_p(RBASIC_SHAPE_ID(obj)));
1643 RUBY_ASSERT(RSHAPE_LEN(RBASIC_SHAPE_ID(obj)) == 0);
1645 obj_transition_too_complex(obj, table);
1649imemo_fields_complex_from_obj_i(
ID key,
VALUE val, st_data_t arg)
1652 st_table *table = rb_imemo_fields_complex_tbl(fields);
1654 RUBY_ASSERT(!st_lookup(table, (st_data_t)key, NULL));
1655 st_add_direct(table, (st_data_t)key, (st_data_t)val);
1662imemo_fields_complex_from_obj(
VALUE klass,
VALUE source_fields_obj, shape_id_t shape_id)
1664 attr_index_t
len = source_fields_obj ? RSHAPE_LEN(RBASIC_SHAPE_ID(source_fields_obj)) : 0;
1665 VALUE fields_obj = rb_imemo_fields_new_complex(klass,
len + 1);
1667 rb_field_foreach(source_fields_obj, imemo_fields_complex_from_obj_i, (st_data_t)fields_obj,
false);
1668 RBASIC_SET_SHAPE_ID(fields_obj, shape_id);
1673void rb_obj_copy_fields_to_hash_table(
VALUE obj,
st_table *table);
1677rb_evict_fields_to_hash(
VALUE obj)
1681 st_table *table = st_init_numtable_with_size(RSHAPE_LEN(RBASIC_SHAPE_ID(obj)));
1682 rb_obj_copy_fields_to_hash_table(obj, table);
1683 shape_id_t new_shape_id = obj_transition_too_complex(obj, table);
1686 return new_shape_id;
1690rb_evict_ivars_to_hash(
VALUE obj)
1697 rb_obj_copy_ivs_to_hash_table(obj, table);
1698 obj_transition_too_complex(obj, table);
1709general_ivar_set(
VALUE obj,
ID id,
VALUE val, void *data,
1711 void (*shape_resize_fields_func)(
VALUE, attr_index_t, attr_index_t,
void *),
1712 void (*set_shape_id_func)(
VALUE, shape_id_t,
void *),
1713 shape_id_t (*transition_too_complex_func)(
VALUE,
void *),
1721 shape_id_t current_shape_id = RBASIC_SHAPE_ID(obj);
1723 if (UNLIKELY(rb_shape_too_complex_p(current_shape_id))) {
1728 if (!rb_shape_get_iv_index(current_shape_id,
id, &index)) {
1729 result.existing =
false;
1731 index = RSHAPE_LEN(current_shape_id);
1732 if (index >= SHAPE_MAX_FIELDS) {
1733 rb_raise(rb_eArgError,
"too many instance variables");
1736 shape_id_t next_shape_id = rb_shape_transition_add_ivar(obj,
id);
1737 if (UNLIKELY(rb_shape_too_complex_p(next_shape_id))) {
1738 current_shape_id = transition_too_complex_func(obj, data);
1741 else if (UNLIKELY(RSHAPE_CAPACITY(next_shape_id) != RSHAPE_CAPACITY(current_shape_id))) {
1742 RUBY_ASSERT(RSHAPE_CAPACITY(next_shape_id) > RSHAPE_CAPACITY(current_shape_id));
1743 shape_resize_fields_func(obj, RSHAPE_CAPACITY(current_shape_id), RSHAPE_CAPACITY(next_shape_id), data);
1746 RUBY_ASSERT(RSHAPE_TYPE_P(next_shape_id, SHAPE_IVAR));
1747 RUBY_ASSERT(index == (RSHAPE_INDEX(next_shape_id)));
1748 set_shape_id_func(obj, next_shape_id, data);
1751 VALUE *table = shape_fields_func(obj, data);
1754 result.index = index;
1761 st_table *table = too_complex_table_func(obj, data);
1762 result.existing = st_insert(table, (st_data_t)
id, (st_data_t)val);
1770general_field_set(
VALUE obj, shape_id_t target_shape_id,
VALUE val,
void *data,
1772 void (*shape_resize_fields_func)(
VALUE, attr_index_t, attr_index_t,
void *),
1773 void (*set_shape_id_func)(
VALUE, shape_id_t,
void *),
1774 shape_id_t (*transition_too_complex_func)(
VALUE,
void *),
1777 shape_id_t current_shape_id = RBASIC_SHAPE_ID(obj);
1779 if (UNLIKELY(rb_shape_too_complex_p(target_shape_id))) {
1780 if (UNLIKELY(!rb_shape_too_complex_p(current_shape_id))) {
1781 current_shape_id = transition_too_complex_func(obj, data);
1784 st_table *table = too_complex_table_func(obj, data);
1786 if (RSHAPE_LEN(target_shape_id) > RSHAPE_LEN(current_shape_id)) {
1787 RBASIC_SET_SHAPE_ID(obj, target_shape_id);
1791 st_insert(table, (st_data_t)RSHAPE_EDGE_NAME(target_shape_id), (st_data_t)val);
1795 attr_index_t index = RSHAPE_INDEX(target_shape_id);
1796 if (index >= RSHAPE_CAPACITY(current_shape_id)) {
1797 shape_resize_fields_func(obj, RSHAPE_CAPACITY(current_shape_id), RSHAPE_CAPACITY(target_shape_id), data);
1800 if (RSHAPE_LEN(target_shape_id) > RSHAPE_LEN(current_shape_id)) {
1801 set_shape_id_func(obj, target_shape_id, data);
1804 VALUE *table = shape_fields_func(obj, data);
1810generic_update_fields_obj(
VALUE obj,
VALUE fields_obj,
const VALUE original_fields_obj)
1812 if (fields_obj != original_fields_obj) {
1813 if (original_fields_obj) {
1815 rb_imemo_fields_clear(original_fields_obj);
1818 generic_fields_insert(obj, fields_obj);
1825 bool existing =
true;
1827 VALUE fields_obj = generic_fields_lookup(obj,
id,
false);
1829 const VALUE original_fields_obj = fields_obj;
1831 fields_obj = rb_imemo_fields_new(
rb_obj_class(obj), 1);
1833 RUBY_ASSERT(RBASIC_SHAPE_ID(obj) == RBASIC_SHAPE_ID(fields_obj));
1835 shape_id_t current_shape_id = RBASIC_SHAPE_ID(fields_obj);
1836 shape_id_t next_shape_id = current_shape_id;
1838 if (UNLIKELY(rb_shape_too_complex_p(current_shape_id))) {
1843 if (!rb_shape_get_iv_index(current_shape_id,
id, &index)) {
1846 index = RSHAPE_LEN(current_shape_id);
1847 if (index >= SHAPE_MAX_FIELDS) {
1848 rb_raise(rb_eArgError,
"too many instance variables");
1851 next_shape_id = rb_shape_transition_add_ivar(fields_obj,
id);
1852 if (UNLIKELY(rb_shape_too_complex_p(next_shape_id))) {
1853 fields_obj = imemo_fields_complex_from_obj(
rb_obj_class(obj), original_fields_obj, next_shape_id);
1857 attr_index_t next_capacity = RSHAPE_CAPACITY(next_shape_id);
1858 attr_index_t current_capacity = RSHAPE_CAPACITY(current_shape_id);
1860 if (next_capacity != current_capacity) {
1863 fields_obj = rb_imemo_fields_new(
rb_obj_class(obj), next_capacity);
1864 if (original_fields_obj) {
1865 attr_index_t fields_count = RSHAPE_LEN(current_shape_id);
1866 VALUE *fields = rb_imemo_fields_ptr(fields_obj);
1867 MEMCPY(fields, rb_imemo_fields_ptr(original_fields_obj),
VALUE, fields_count);
1868 for (attr_index_t i = 0; i < fields_count; i++) {
1875 RUBY_ASSERT(index == (RSHAPE_LEN(next_shape_id) - 1));
1878 VALUE *fields = rb_imemo_fields_ptr(fields_obj);
1882 RBASIC_SET_SHAPE_ID(fields_obj, next_shape_id);
1885 generic_update_fields_obj(obj, fields_obj, original_fields_obj);
1888 RBASIC_SET_SHAPE_ID(obj, next_shape_id);
1891 RUBY_ASSERT(RBASIC_SHAPE_ID(obj) == RBASIC_SHAPE_ID(fields_obj));
1897 st_table *table = rb_imemo_fields_complex_tbl(fields_obj);
1898 existing = st_insert(table, (st_data_t)
id, (st_data_t)val);
1901 generic_update_fields_obj(obj, fields_obj, original_fields_obj);
1904 RBASIC_SET_SHAPE_ID(obj, next_shape_id);
1908 RUBY_ASSERT(RBASIC_SHAPE_ID(obj) == RBASIC_SHAPE_ID(fields_obj));
1914generic_field_set(
VALUE obj, shape_id_t target_shape_id,
VALUE val)
1916 bool existing =
true;
1918 VALUE fields_obj = generic_fields_lookup(obj, RSHAPE_EDGE_NAME(target_shape_id),
false);
1919 const VALUE original_fields_obj = fields_obj;
1921 shape_id_t current_shape_id = fields_obj ? RBASIC_SHAPE_ID(fields_obj) : ROOT_SHAPE_ID;
1923 if (UNLIKELY(rb_shape_too_complex_p(target_shape_id))) {
1924 if (UNLIKELY(!rb_shape_too_complex_p(current_shape_id))) {
1925 fields_obj = imemo_fields_complex_from_obj(
rb_obj_class(obj), original_fields_obj, target_shape_id);
1926 current_shape_id = target_shape_id;
1930 st_table *table = rb_imemo_fields_complex_tbl(fields_obj);
1933 st_insert(table, (st_data_t)RSHAPE_EDGE_NAME(target_shape_id), (st_data_t)val);
1935 RBASIC_SET_SHAPE_ID(fields_obj, target_shape_id);
1938 attr_index_t index = RSHAPE_INDEX(target_shape_id);
1939 if (index >= RSHAPE_CAPACITY(current_shape_id)) {
1940 fields_obj = rb_imemo_fields_new(
rb_obj_class(obj), RSHAPE_CAPACITY(target_shape_id));
1941 if (original_fields_obj) {
1942 attr_index_t fields_count = RSHAPE_LEN(current_shape_id);
1943 VALUE *fields = rb_imemo_fields_ptr(fields_obj);
1944 MEMCPY(fields, rb_imemo_fields_ptr(original_fields_obj),
VALUE, fields_count);
1945 for (attr_index_t i = 0; i < fields_count; i++) {
1951 VALUE *table = rb_imemo_fields_ptr(fields_obj);
1954 if (RSHAPE_LEN(target_shape_id) > RSHAPE_LEN(current_shape_id)) {
1956 RBASIC_SET_SHAPE_ID(fields_obj, target_shape_id);
1960 generic_update_fields_obj(obj, fields_obj, original_fields_obj);
1963 RBASIC_SET_SHAPE_ID(obj, target_shape_id);
1966 RUBY_ASSERT(RBASIC_SHAPE_ID(obj) == RBASIC_SHAPE_ID(fields_obj));
1970rb_ensure_iv_list_size(
VALUE obj, uint32_t current_len, uint32_t new_capacity)
1974 if (
RBASIC(obj)->flags & ROBJECT_EMBED) {
1979 ROBJECT(obj)->as.heap.fields = newptr;
1987rb_obj_copy_ivs_to_hash_table_i(
ID key,
VALUE val, st_data_t arg)
1991 st_add_direct((
st_table *)arg, (st_data_t)key, (st_data_t)val);
1998 rb_ivar_foreach(obj, rb_obj_copy_ivs_to_hash_table_i, (st_data_t)table);
2004 rb_field_foreach(obj, rb_obj_copy_ivs_to_hash_table_i, (st_data_t)table,
false);
2008obj_ivar_set_shape_fields(
VALUE obj,
void *_data)
2016obj_ivar_set_shape_resize_fields(
VALUE obj, attr_index_t old_capa, attr_index_t new_capa,
void *_data)
2018 rb_ensure_iv_list_size(obj, old_capa, new_capa);
2022obj_ivar_set_set_shape_id(
VALUE obj, shape_id_t shape_id,
void *_data)
2024 rb_obj_set_shape_id(obj, shape_id);
2028obj_ivar_set_transition_too_complex(
VALUE obj,
void *_data)
2030 return rb_evict_fields_to_hash(obj);
2034obj_ivar_set_too_complex_table(
VALUE obj,
void *_data)
2038 return ROBJECT_FIELDS_HASH(obj);
2044 return general_ivar_set(obj,
id, val, NULL,
2045 obj_ivar_set_shape_fields,
2046 obj_ivar_set_shape_resize_fields,
2047 obj_ivar_set_set_shape_id,
2048 obj_ivar_set_transition_too_complex,
2049 obj_ivar_set_too_complex_table).index;
2053obj_field_set(
VALUE obj, shape_id_t target_shape_id,
VALUE val)
2055 general_field_set(obj, target_shape_id, val, NULL,
2056 obj_ivar_set_shape_fields,
2057 obj_ivar_set_shape_resize_fields,
2058 obj_ivar_set_set_shape_id,
2059 obj_ivar_set_transition_too_complex,
2060 obj_ivar_set_too_complex_table);
2070 rb_check_frozen(obj);
2071 rb_obj_ivar_set(obj,
id, val);
2076rb_obj_set_shape_id(
VALUE obj, shape_id_t shape_id)
2078 shape_id_t old_shape_id = rb_obj_shape_id(obj);
2079 if (old_shape_id == shape_id) {
2083 RB_SET_SHAPE_ID(obj, shape_id);
2095 RB_SET_SHAPE_ID(x, rb_shape_transition_frozen(x));
2106 RB_DEBUG_COUNTER_INC(ivar_set_base);
2111 rb_obj_ivar_set(obj,
id, val);
2116 IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(
id);
2117 rb_class_ivar_set(obj,
id, val);
2121 generic_ivar_set(obj,
id, val);
2129 rb_check_frozen(obj);
2130 ivar_set(obj,
id, val);
2140 ivar_set(obj,
id, val);
2144rb_obj_field_set(
VALUE obj, shape_id_t target_shape_id,
VALUE val)
2148 obj_field_set(obj, target_shape_id, val);
2153 rb_bug(
"Unreachable");
2156 generic_field_set(obj, target_shape_id, val);
2162ivar_defined0(
VALUE obj,
ID id)
2166 if (rb_shape_obj_too_complex_p(obj)) {
2172 rb_bug(
"Unreachable");
2177 table = rb_imemo_fields_complex_tbl(obj);
2181 table = ROBJECT_FIELDS_HASH(obj);
2186 if (rb_gen_fields_tbl_get(obj, 0, &fields_obj)) {
2187 table = rb_imemo_fields_complex_tbl(fields_obj);
2192 if (!table || !rb_st_lookup(table,
id, &idx)) {
2199 return RBOOL(rb_shape_get_iv_index(RBASIC_SHAPE_ID(obj),
id, &index));
2213 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
2215 defined = ivar_defined0(fields_obj,
id);
2220 defined = ivar_defined0(obj,
id);
2228 struct gen_fields_tbl *fields_tbl;
2230 rb_ivar_foreach_callback_func *func;
2236iterate_over_shapes_callback(shape_id_t shape_id,
void *data)
2240 if (itr_data->ivar_only && !RSHAPE_TYPE_P(shape_id, SHAPE_IVAR)) {
2247 RUBY_ASSERT(!rb_shape_obj_too_complex_p(itr_data->obj));
2251 RUBY_ASSERT(IMEMO_TYPE_P(itr_data->obj, imemo_fields));
2252 RUBY_ASSERT(!rb_shape_obj_too_complex_p(itr_data->obj));
2254 fields = rb_imemo_fields_ptr(itr_data->obj);
2257 rb_bug(
"Unreachable");
2260 VALUE val = fields[RSHAPE_INDEX(shape_id)];
2261 return itr_data->func(RSHAPE_EDGE_NAME(shape_id), val, itr_data->arg);
2268iterate_over_shapes(shape_id_t shape_id, rb_ivar_foreach_callback_func *callback,
struct iv_itr_data *itr_data)
2270 rb_shape_foreach_field(shape_id, iterate_over_shapes_callback, itr_data);
2274each_hash_iv(st_data_t
id, st_data_t val, st_data_t data)
2277 rb_ivar_foreach_callback_func *callback = itr_data->func;
2278 return callback((
ID)
id, (
VALUE)val, itr_data->arg);
2282obj_fields_each(
VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg,
bool ivar_only)
2288 .ivar_only = ivar_only,
2291 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
2292 if (rb_shape_too_complex_p(shape_id)) {
2293 rb_st_foreach(ROBJECT_FIELDS_HASH(obj), each_hash_iv, (st_data_t)&itr_data);
2297 iterate_over_shapes(shape_id, func, &itr_data);
2302imemo_fields_each(
VALUE fields_obj, rb_ivar_foreach_callback_func *func, st_data_t arg,
bool ivar_only)
2304 IMEMO_TYPE_P(fields_obj, imemo_fields);
2310 .ivar_only = ivar_only,
2313 shape_id_t shape_id = RBASIC_SHAPE_ID(fields_obj);
2314 if (rb_shape_too_complex_p(shape_id)) {
2315 rb_st_foreach(rb_imemo_fields_complex_tbl(fields_obj), each_hash_iv, (st_data_t)&itr_data);
2318 itr_data.fields = rb_imemo_fields_ptr(fields_obj);
2319 iterate_over_shapes(shape_id, func, &itr_data);
2327 VALUE new_fields_obj;
2329 rb_check_frozen(dest);
2331 if (!rb_obj_exivar_p(obj)) {
2335 shape_id_t src_shape_id = rb_obj_shape_id(obj);
2337 if (rb_gen_fields_tbl_get(obj, 0, &fields_obj)) {
2343 if (rb_shape_too_complex_p(src_shape_id)) {
2344 rb_shape_copy_complex_ivars(dest, obj, src_shape_id, rb_imemo_fields_complex_tbl(fields_obj));
2348 shape_id_t dest_shape_id = src_shape_id;
2349 shape_id_t initial_shape_id = rb_obj_shape_id(dest);
2351 if (!rb_shape_canonical_p(src_shape_id)) {
2352 RUBY_ASSERT(RSHAPE_TYPE_P(initial_shape_id, SHAPE_ROOT));
2354 dest_shape_id = rb_shape_rebuild(initial_shape_id, src_shape_id);
2355 if (UNLIKELY(rb_shape_too_complex_p(dest_shape_id))) {
2356 st_table *table = rb_st_init_numtable_with_size(src_num_ivs);
2357 rb_obj_copy_ivs_to_hash_table(obj, table);
2358 rb_obj_init_too_complex(dest, table);
2363 if (!RSHAPE_LEN(dest_shape_id)) {
2364 rb_obj_set_shape_id(dest, dest_shape_id);
2368 new_fields_obj = rb_imemo_fields_new(
rb_obj_class(dest), RSHAPE_CAPACITY(dest_shape_id));
2369 VALUE *src_buf = rb_imemo_fields_ptr(fields_obj);
2370 VALUE *dest_buf = rb_imemo_fields_ptr(new_fields_obj);
2371 rb_shape_copy_fields(dest, dest_buf, dest_shape_id, obj, src_buf, src_shape_id);
2372 RBASIC_SET_SHAPE_ID(new_fields_obj, dest_shape_id);
2375 generic_fields_tbl_no_ractor_check(dest);
2376 st_insert(generic_fields_tbl_no_ractor_check(obj), (st_data_t)dest, (st_data_t)new_fields_obj);
2380 RBASIC_SET_SHAPE_ID(dest, dest_shape_id);
2389rb_replace_generic_ivar(
VALUE clone,
VALUE obj)
2392 st_data_t fields_tbl, obj_data = (st_data_t)obj;
2393 if (st_delete(generic_fields_tbl_, &obj_data, &fields_tbl)) {
2394 st_insert(generic_fields_tbl_, (st_data_t)clone, fields_tbl);
2397 rb_bug(
"unreachable");
2403rb_field_foreach(
VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg,
bool ivar_only)
2408 if (IMEMO_TYPE_P(obj, imemo_fields)) {
2409 imemo_fields_each(obj, func, arg, ivar_only);
2413 obj_fields_each(obj, func, arg, ivar_only);
2418 IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(0);
2419 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
2421 imemo_fields_each(fields_obj, func, arg, ivar_only);
2426 if (rb_obj_exivar_p(obj)) {
2427 VALUE fields_obj = 0;
2428 if (!rb_gen_fields_tbl_get(obj, 0, &fields_obj))
return;
2430 imemo_fields_each(fields_obj, func, arg, ivar_only);
2439 rb_field_foreach(obj, func, arg,
true);
2447 st_index_t iv_count = 0;
2450 iv_count = ROBJECT_FIELDS_COUNT(obj);
2456 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
2460 if (rb_shape_obj_too_complex_p(fields_obj)) {
2461 iv_count = rb_st_table_size(rb_imemo_fields_complex_tbl(fields_obj));
2464 iv_count = RBASIC_FIELDS_COUNT(fields_obj);
2472 if (rb_shape_obj_too_complex_p(obj)) {
2473 iv_count = rb_st_table_size(rb_imemo_fields_complex_tbl(obj));
2476 iv_count = RBASIC_FIELDS_COUNT(obj);
2481 if (rb_obj_exivar_p(obj)) {
2483 if (rb_shape_obj_too_complex_p(obj)) {
2486 if (rb_gen_fields_tbl_get(obj, 0, &fields_obj)) {
2487 iv_count = rb_st_table_size(rb_imemo_fields_complex_tbl(fields_obj));
2491 iv_count = RBASIC_FIELDS_COUNT(obj);
2497 if (rb_shape_obj_has_id(obj)) {
2505ivar_i(
ID key,
VALUE v, st_data_t a)
2542#define rb_is_constant_id rb_is_const_id
2543#define rb_is_constant_name rb_is_const_name
2544#define id_for_var(obj, name, part, type) \
2545 id_for_var_message(obj, name, type, "'%1$s' is not allowed as "#part" "#type" variable name")
2546#define id_for_var_message(obj, name, type, message) \
2547 check_id_type(obj, &(name), rb_is_##type##_id, rb_is_##type##_name, message, strlen(message))
2550 int (*valid_id_p)(
ID),
int (*valid_name_p)(
VALUE),
2551 const char *message,
size_t message_len)
2554 VALUE name = *pname;
2556 if (
id ? !valid_id_p(
id) : !valid_name_p(name)) {
2557 rb_name_err_raise_str(rb_fstring_new(message, message_len),
2589 const ID id = id_for_var(obj, name, an, instance);
2593 rb_check_frozen(obj);
2598 if (!UNDEF_P(val))
return val;
2601 rb_name_err_raise(
"instance variable %1$s not defined",
2606NORETURN(
static void uninitialized_constant(
VALUE,
VALUE));
2608uninitialized_constant(
VALUE klass,
VALUE name)
2611 rb_name_err_raise(
"uninitialized constant %2$s::%1$s",
2614 rb_name_err_raise(
"uninitialized constant %1$s",
2621 VALUE value = rb_funcallv(klass, idConst_missing, 1, &name);
2622 rb_vm_inc_const_missing_count();
2671 VALUE ref = ec->private_const_reference;
2672 rb_vm_pop_cfunc_frame();
2674 ec->private_const_reference = 0;
2675 rb_name_err_raise(
"private constant %2$s::%1$s referenced", ref, name);
2677 uninitialized_constant(klass, name);
2683autoload_table_mark(
void *ptr)
2685 rb_mark_tbl_no_pin((
st_table *)ptr);
2689autoload_table_free(
void *ptr)
2695autoload_table_memsize(
const void *ptr)
2698 return st_memsize(tbl);
2702autoload_table_compact(
void *ptr)
2704 rb_gc_ref_update_table_values_only((
st_table *)ptr);
2709 {autoload_table_mark, autoload_table_free, autoload_table_memsize, autoload_table_compact,},
2710 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
2713#define check_autoload_table(av) \
2714 (struct st_table *)rb_check_typeddata((av), &autoload_table_type)
2725 if (RICLASS_IS_ORIGIN_P(mod)) {
2729 mod =
RBASIC(mod)->klass;
2737 VALUE tbl_value = rb_ivar_lookup(mod, autoload,
Qfalse);
2738 if (!
RTEST(tbl_value) || !(tbl = check_autoload_table(tbl_value)) || !st_lookup(tbl, (st_data_t)
id, &val)) {
2748 struct ccan_list_node cnode;
2751 VALUE autoload_data_value;
2767 rb_const_flag_t flag;
2783 rb_serial_t fork_gen;
2786 struct ccan_list_head constants;
2790autoload_data_compact(
void *ptr)
2794 p->feature = rb_gc_location(p->feature);
2795 p->mutex = rb_gc_location(p->mutex);
2799autoload_data_mark(
void *ptr)
2803 rb_gc_mark_movable(p->feature);
2804 rb_gc_mark_movable(p->mutex);
2808autoload_data_free(
void *ptr)
2813 ccan_list_for_each_safe(&p->constants,
autoload_const, next, cnode) {
2821autoload_data_memsize(
const void *ptr)
2828 {autoload_data_mark, autoload_data_free, autoload_data_memsize, autoload_data_compact},
2829 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
2833autoload_const_compact(
void *ptr)
2837 ac->module = rb_gc_location(ac->module);
2838 ac->autoload_data_value = rb_gc_location(ac->autoload_data_value);
2839 ac->value = rb_gc_location(ac->value);
2840 ac->file = rb_gc_location(ac->file);
2841 ac->namespace = rb_gc_location(ac->namespace);
2845autoload_const_mark(
void *ptr)
2849 rb_gc_mark_movable(ac->module);
2850 rb_gc_mark_movable(ac->autoload_data_value);
2851 rb_gc_mark_movable(ac->value);
2852 rb_gc_mark_movable(ac->file);
2853 rb_gc_mark_movable(ac->namespace);
2857autoload_const_memsize(
const void *ptr)
2863autoload_const_free(
void *ptr)
2873 {autoload_const_mark, autoload_const_free, autoload_const_memsize, autoload_const_compact,},
2874 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
2878get_autoload_data(
VALUE autoload_const_value,
struct autoload_const **autoload_const_pointer)
2891 if (autoload_const_pointer) *autoload_const_pointer =
autoload_const;
2897 VALUE dst_tbl_value;
2903autoload_copy_table_for_namespace_i(st_data_t key, st_data_t value, st_data_t arg)
2907 struct st_table *tbl = data->dst_tbl;
2908 VALUE tbl_value = data->dst_tbl_value;
2914 VALUE autoload_data_value = src_const->autoload_data_value;
2926 st_insert(tbl, (st_data_t)
autoload_const->name, (st_data_t)new_value);
2935 struct st_table *src_tbl, *dst_tbl;
2936 VALUE src_tbl_value, dst_tbl_value;
2937 if (!rb_st_lookup(iv_ptr, (st_data_t)autoload, (st_data_t *)&src_tbl_value)) {
2941 if (!
RTEST(src_tbl_value) || !(src_tbl = check_autoload_table(src_tbl_value))) {
2945 src_tbl = check_autoload_table(src_tbl_value);
2951 .dst_tbl_value = dst_tbl_value,
2956 st_foreach(src_tbl, autoload_copy_table_for_namespace_i, (st_data_t)&data);
2957 st_insert(iv_ptr, (st_data_t)autoload, (st_data_t)dst_tbl_value);
2961rb_autoload(
VALUE module,
ID name,
const char *feature)
2963 if (!feature || !*feature) {
2964 rb_raise(rb_eArgError,
"empty feature name");
2967 rb_autoload_str(module, name, rb_fstring_cstr(feature));
2971static void const_added(
VALUE klass,
ID const_name);
2981autoload_feature_lookup_or_create(
VALUE feature,
struct autoload_data **autoload_data_pointer)
2983 RUBY_ASSERT_MUTEX_OWNED(autoload_mutex);
2984 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
2986 VALUE autoload_data_value = rb_hash_aref(autoload_features, feature);
2989 if (
NIL_P(autoload_data_value)) {
2995 if (autoload_data_pointer) *autoload_data_pointer =
autoload_data;
2997 rb_hash_aset(autoload_features, feature, autoload_data_value);
2999 else if (autoload_data_pointer) {
3000 *autoload_data_pointer =
rb_check_typeddata(autoload_data_value, &autoload_data_type);
3003 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
3004 return autoload_data_value;
3008autoload_table_lookup_or_create(
VALUE module)
3010 VALUE autoload_table_value = rb_ivar_lookup(module, autoload,
Qfalse);
3011 if (
RTEST(autoload_table_value)) {
3012 return autoload_table_value;
3016 rb_class_ivar_set(module, autoload, autoload_table_value);
3018 return autoload_table_value;
3023autoload_synchronized(
VALUE _arguments)
3027 rb_const_entry_t *constant_entry = rb_const_lookup(arguments->module, arguments->name);
3028 if (constant_entry && !UNDEF_P(constant_entry->value)) {
3033 const_set(arguments->module, arguments->name,
Qundef);
3035 VALUE autoload_table_value = autoload_table_lookup_or_create(arguments->module);
3036 struct st_table *autoload_table = check_autoload_table(autoload_table_value);
3039 VALUE feature = rb_fstring(arguments->feature);
3042 VALUE autoload_data_value = autoload_feature_lookup_or_create(feature, &
autoload_data);
3054 st_insert(autoload_table, (st_data_t)arguments->name, (st_data_t)autoload_const_value);
3068 rb_raise(
rb_eNameError,
"autoload must be constant name: %"PRIsVALUE
"", QUOTE_ID(name));
3072 if (!RSTRING_LEN(feature)) {
3073 rb_raise(rb_eArgError,
"empty feature name");
3080 .namespace = current_namespace,
3085 if (result ==
Qtrue) {
3086 const_added(module, name);
3091autoload_delete(
VALUE module,
ID name)
3093 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
3095 st_data_t load = 0, key = name;
3099 VALUE table_value = rb_ivar_lookup(module, autoload,
Qfalse);
3100 if (
RTEST(table_value)) {
3101 struct st_table *table = check_autoload_table(table_value);
3103 st_delete(table, &key, &load);
3126 if (table->num_entries == 0) {
3127 rb_attr_delete(module, autoload);
3132 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
3138 return ele->mutex !=
Qnil && !rb_mutex_owned_p(ele->mutex);
3142check_autoload_required(
VALUE mod,
ID id,
const char **loadingpath)
3146 const char *loading;
3148 if (!autoload_const_value || !(
autoload_data = get_autoload_data(autoload_const_value, 0))) {
3161 return autoload_const_value;
3164 loading = RSTRING_PTR(feature);
3167 return autoload_const_value;
3170 if (loadingpath && loading) {
3171 *loadingpath = loading;
3172 return autoload_const_value;
3181rb_autoloading_value(
VALUE mod,
ID id,
VALUE* value, rb_const_flag_t *flag)
3184 if (!ac)
return FALSE;
3200 return ele->mutex !=
Qnil && rb_mutex_owned_p(ele->mutex);
3207autoloading_const_entry(
VALUE mod,
ID id)
3214 if (!load || !(ele = get_autoload_data(load, &ac))) {
3220 if (autoload_by_current(ele)) {
3221 if (!UNDEF_P(ac->value)) {
3230autoload_defined_p(
VALUE mod,
ID id)
3235 if (!ce || !UNDEF_P(ce->value)) {
3241 return !rb_autoloading_value(mod,
id, NULL, NULL);
3263 check_before_mod_set(ac->module, ac->name, ac->value,
"constant");
3266 const_tbl_update(ac,
true);
3273autoload_load_needed(
VALUE _arguments)
3277 const char *loading = 0, *src;
3279 if (!autoload_defined_p(arguments->module, arguments->name)) {
3283 VALUE autoload_const_value = check_autoload_required(arguments->module, arguments->name, &loading);
3284 if (!autoload_const_value) {
3289 if (src && loading && strcmp(src, loading) == 0) {
3310 return autoload_const_value;
3314autoload_apply_constants(
VALUE _arguments)
3316 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
3328 ccan_list_for_each_safe(&arguments->autoload_data->constants,
autoload_const, next, cnode) {
3334 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
3346autoload_feature_require_in_builtin(
VALUE arg)
3350 VALUE result =
rb_funcall(data->receiver, rb_intern(
"require"), 1, data->feature);
3351 if (
RTEST(result)) {
3358autoload_feature_require_ensure_in_builtin(
VALUE _arg)
3364 rb_gccct_clear_table(
Qnil);
3365 rb_namespace_disable_builtin();
3370autoload_feature_require_in_builtin_wrap(
VALUE arg)
3372 return rb_ensure(autoload_feature_require_in_builtin, arg,
3373 autoload_feature_require_ensure_in_builtin,
Qnil);
3377autoload_feature_require(
VALUE _arguments)
3379 VALUE receiver = rb_vm_top_self();
3389 if (
NIL_P(autoload_namespace)) {
3390 rb_namespace_enable_builtin();
3397 rb_gccct_clear_table(
Qnil);
3399 .arguments = arguments,
3400 .receiver = receiver,
3401 .feature = arguments->autoload_data->feature,
3403 return rb_namespace_exec(rb_builtin_namespace(), autoload_feature_require_in_builtin_wrap, (
VALUE)&data);
3406 if (
RTEST(autoload_namespace) && NAMESPACE_OPTIONAL_P(rb_get_namespace_t(autoload_namespace))) {
3407 receiver = autoload_namespace;
3410 VALUE result =
rb_funcall(receiver, rb_intern(
"require"), 1, arguments->autoload_data->feature);
3412 if (
RTEST(result)) {
3419autoload_try_load(
VALUE _arguments)
3423 VALUE result = autoload_feature_require(_arguments);
3426 rb_const_entry_t *ce = rb_const_lookup(arguments->module, arguments->name);
3428 if (!ce || UNDEF_P(ce->value)) {
3433 if (arguments->module == rb_cObject) {
3435 "Expected %"PRIsVALUE
" to define %"PRIsVALUE
" but it didn't",
3436 arguments->autoload_data->feature,
3442 "Expected %"PRIsVALUE
" to define %"PRIsVALUE
"::%"PRIsVALUE
" but it didn't",
3443 arguments->autoload_data->feature,
3451 ce->flag |= arguments->flag;
3463 if (!ce || !UNDEF_P(ce->value)) {
3468 if (UNLIKELY(!rb_ractor_main_p())) {
3469 return rb_ractor_autoload_load(module, name);
3479 if (autoload_const_value ==
Qfalse)
return autoload_const_value;
3481 arguments.flag = ce->flag & (CONST_DEPRECATED | CONST_VISIBILITY_MASK);
3498 return rb_autoload_at_p(mod,
id, TRUE);
3502rb_autoload_at_p(
VALUE mod,
ID id,
int recur)
3507 while (!autoload_defined_p(mod,
id)) {
3508 if (!recur)
return Qnil;
3510 if (!mod)
return Qnil;
3512 load = check_autoload_required(mod,
id, 0);
3513 if (!load)
return Qnil;
3514 return (ele = get_autoload_data(load, 0)) ? ele->feature :
Qnil;
3520 if (RB_CONST_DEPRECATED_P(ce) &&
3522 if (klass == rb_cObject) {
3533rb_const_get_0(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility)
3535 VALUE c = rb_const_search(klass,
id, exclude, recurse, visibility);
3537 if (UNLIKELY(!rb_ractor_main_p())) {
3539 rb_raise(rb_eRactorIsolationError,
"can not access non-shareable objects in constant %"PRIsVALUE
"::%s by non-main Ractor.",
rb_class_path(klass), rb_id2name(
id));
3544 return rb_const_missing(klass,
ID2SYM(
id));
3548rb_const_search_from(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility)
3550 VALUE value, current;
3551 bool first_iteration =
true;
3553 for (current = klass;
3555 current =
RCLASS_SUPER(current), first_iteration =
false) {
3560 if (!first_iteration && RCLASS_ORIGIN(current) != current) {
3573 while ((ce = rb_const_lookup(tmp,
id))) {
3574 if (visibility && RB_CONST_PRIVATE_P(ce)) {
3575 GET_EC()->private_const_reference = tmp;
3578 rb_const_warn_if_deprecated(ce, tmp,
id);
3580 if (UNDEF_P(value)) {
3582 if (am == tmp)
break;
3584 ac = autoloading_const_entry(tmp,
id);
3585 if (ac)
return ac->value;
3589 if (exclude && tmp == rb_cObject) {
3594 if (!recurse)
break;
3598 GET_EC()->private_const_reference = 0;
3603rb_const_search(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility)
3607 if (klass == rb_cObject) exclude = FALSE;
3608 value = rb_const_search_from(klass,
id, exclude, recurse, visibility);
3609 if (!UNDEF_P(value))
return value;
3610 if (exclude)
return value;
3613 return rb_const_search_from(rb_cObject,
id, FALSE, recurse, visibility);
3619 return rb_const_get_0(klass,
id, TRUE, TRUE, FALSE);
3625 return rb_const_get_0(klass,
id, FALSE, TRUE, FALSE);
3631 return rb_const_get_0(klass,
id, TRUE, FALSE, FALSE);
3635rb_public_const_get_from(
VALUE klass,
ID id)
3637 return rb_const_get_0(klass,
id, TRUE, TRUE, TRUE);
3641rb_public_const_get_at(
VALUE klass,
ID id)
3643 return rb_const_get_0(klass,
id, TRUE, FALSE, TRUE);
3646NORETURN(
static void undefined_constant(
VALUE mod,
VALUE name));
3650 rb_name_err_raise(
"constant %2$s::%1$s not defined",
3655rb_const_location_from(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility)
3657 while (
RTEST(klass)) {
3660 while ((ce = rb_const_lookup(klass,
id))) {
3661 if (visibility && RB_CONST_PRIVATE_P(ce)) {
3664 if (exclude && klass == rb_cObject) {
3668 if (UNDEF_P(ce->value)) {
3670 if (
RTEST(autoload_const_value)) {
3683 if (!recurse)
break;
3692rb_const_location(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility)
3696 if (klass == rb_cObject) exclude = FALSE;
3697 loc = rb_const_location_from(klass,
id, exclude, recurse, visibility);
3698 if (!
NIL_P(loc))
return loc;
3699 if (exclude)
return loc;
3702 return rb_const_location_from(rb_cObject,
id, FALSE, recurse, visibility);
3706rb_const_source_location(
VALUE klass,
ID id)
3708 return rb_const_location(klass,
id, FALSE, TRUE, FALSE);
3712rb_const_source_location_at(
VALUE klass,
ID id)
3714 return rb_const_location(klass,
id, TRUE, FALSE, FALSE);
3730 const ID id = id_for_var(mod, name, a, constant);
3733 undefined_constant(mod, name);
3746 rb_check_frozen(mod);
3748 ce = rb_const_lookup(mod,
id);
3749 if (!ce || !rb_id_table_delete(RCLASS_WRITABLE_CONST_TBL(mod),
id)) {
3751 rb_name_err_raise(
"cannot remove %2$s::%1$s", mod,
ID2SYM(
id));
3754 undefined_constant(mod,
ID2SYM(
id));
3757 rb_const_warn_if_deprecated(ce, mod,
id);
3763 autoload_delete(mod,
id);
3767 if (ce != const_lookup(RCLASS_PRIME_CONST_TBL(mod),
id)) {
3776cv_i_update(st_data_t *k, st_data_t *v, st_data_t a,
int existing)
3778 if (existing)
return ST_STOP;
3783static enum rb_id_table_iterator_result
3790 st_update(tbl, (st_data_t)key, cv_i_update, (st_data_t)ce);
3792 return ID_TABLE_CONTINUE;
3795static enum rb_id_table_iterator_result
3796rb_local_constants_i(
ID const_name,
VALUE const_value,
void *ary)
3801 return ID_TABLE_CONTINUE;
3805rb_local_constants(
VALUE mod)
3814 rb_id_table_foreach(tbl, rb_local_constants_i, (
void *)ary);
3825 tbl = st_init_numtable();
3827 if (RCLASS_CONST_TBL(mod)) {
3829 rb_id_table_foreach(RCLASS_CONST_TBL(mod), sv_i, tbl);
3843 if (tmp == rb_cObject && mod != rb_cObject)
break;
3849list_i(st_data_t key, st_data_t value,
VALUE ary)
3892 bool inherit =
true;
3900 return rb_local_constants(mod);
3905rb_const_defined_0(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility)
3914 if ((ce = rb_const_lookup(tmp,
id))) {
3915 if (visibility && RB_CONST_PRIVATE_P(ce)) {
3918 if (UNDEF_P(ce->value) && !check_autoload_required(tmp,
id, 0) &&
3919 !rb_autoloading_value(tmp,
id, NULL, NULL))
3922 if (exclude && tmp == rb_cObject && klass != rb_cObject) {
3928 if (!recurse)
break;
3942 return rb_const_defined_0(klass,
id, TRUE, TRUE, FALSE);
3948 return rb_const_defined_0(klass,
id, FALSE, TRUE, FALSE);
3954 return rb_const_defined_0(klass,
id, TRUE, FALSE, FALSE);
3958rb_public_const_defined_from(
VALUE klass,
ID id)
3960 return rb_const_defined_0(klass,
id, TRUE, TRUE, TRUE);
3964check_before_mod_set(
VALUE klass,
ID id,
VALUE val,
const char *dest)
3966 rb_check_frozen(klass);
3969static void set_namespace_path(
VALUE named_namespace,
VALUE name);
3971static enum rb_id_table_iterator_result
3972set_namespace_path_i(
ID id,
VALUE v,
void *payload)
3975 VALUE value = ce->value;
3978 return ID_TABLE_CONTINUE;
3981 bool has_permanent_classpath;
3982 classname(value, &has_permanent_classpath);
3983 if (has_permanent_classpath) {
3984 return ID_TABLE_CONTINUE;
3986 set_namespace_path(value, build_const_path(parental_path,
id));
3988 if (!RCLASS_PERMANENT_CLASSPATH_P(value)) {
3989 RCLASS_WRITE_CLASSPATH(value, 0,
false);
3992 return ID_TABLE_CONTINUE;
4001set_namespace_path(
VALUE named_namespace,
VALUE namespace_path)
4003 struct rb_id_table *const_table = RCLASS_CONST_TBL(named_namespace);
4006 RCLASS_WRITE_CLASSPATH(named_namespace, namespace_path,
true);
4009 rb_id_table_foreach(const_table, set_namespace_path_i, &namespace_path);
4015const_added(
VALUE klass,
ID const_name)
4017 if (GET_VM()->running) {
4019 rb_funcallv(klass, idConst_added, 1, &name);
4029 rb_raise(
rb_eTypeError,
"no class/module to define constant %"PRIsVALUE
"",
4034 rb_raise(rb_eRactorIsolationError,
"can not set constants with non-shareable objects by non-main Ractors");
4037 check_before_mod_set(klass,
id, val,
"constant");
4040 struct rb_id_table *tbl = RCLASS_WRITABLE_CONST_TBL(klass);
4042 tbl = rb_id_table_create(0);
4043 RCLASS_WRITE_CONST_TBL(klass, tbl,
false);
4046 rb_id_table_insert(tbl,
id, (
VALUE)ce);
4047 setup_const_entry(ce, klass, val, CONST_PUBLIC);
4051 .module = klass, .name = id,
4052 .value = val, .flag = CONST_PUBLIC,
4055 ac.file = rb_source_location(&ac.line);
4056 const_tbl_update(&ac,
false);
4064 if (rb_cObject && rb_namespace_p(val)) {
4065 bool val_path_permanent;
4066 VALUE val_path = classname(val, &val_path_permanent);
4067 if (
NIL_P(val_path) || !val_path_permanent) {
4068 if (klass == rb_cObject) {
4069 set_namespace_path(val, rb_id2str(
id));
4072 bool parental_path_permanent;
4073 VALUE parental_path = classname(klass, &parental_path_permanent);
4074 if (
NIL_P(parental_path)) {
4076 parental_path = rb_tmp_class_path(klass, &throwaway, make_temporary_path);
4078 if (parental_path_permanent && !val_path_permanent) {
4079 set_namespace_path(val, build_const_path(parental_path,
id));
4081 else if (!parental_path_permanent &&
NIL_P(val_path)) {
4082 RCLASS_SET_CLASSPATH(val, build_const_path(parental_path,
id),
false);
4092 const_set(klass,
id, val);
4093 const_added(klass,
id);
4097autoload_data_for_named_constant(
VALUE module,
ID name,
struct autoload_const **autoload_const_pointer)
4100 if (!autoload_data_value)
return 0;
4117 VALUE klass = ac->module;
4118 VALUE val = ac->value;
4120 struct rb_id_table *tbl = RCLASS_CONST_TBL(klass);
4121 rb_const_flag_t visibility = ac->flag;
4124 if (rb_id_table_lookup(tbl,
id, &value)) {
4126 if (UNDEF_P(ce->value)) {
4127 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
4128 VALUE file = ac->file;
4129 int line = ac->line;
4130 struct autoload_data *ele = autoload_data_for_named_constant(klass,
id, &ac);
4132 if (!autoload_force && ele) {
4136 ac->file = rb_source_location(&ac->line);
4140 autoload_delete(klass,
id);
4141 ce->flag = visibility;
4146 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
4150 VALUE name = QUOTE_ID(
id);
4151 visibility = ce->flag;
4152 if (klass == rb_cObject)
4153 rb_warn(
"already initialized constant %"PRIsVALUE
"", name);
4155 rb_warn(
"already initialized constant %"PRIsVALUE
"::%"PRIsVALUE
"",
4157 if (!
NIL_P(ce->file) && ce->line) {
4158 rb_compile_warn(RSTRING_PTR(ce->file), ce->line,
4159 "previous definition of %"PRIsVALUE
" was here", name);
4163 setup_const_entry(ce, klass, val, visibility);
4166 tbl = RCLASS_WRITABLE_CONST_TBL(klass);
4170 rb_id_table_insert(tbl,
id, (
VALUE)ce);
4171 setup_const_entry(ce, klass, val, visibility);
4177 rb_const_flag_t visibility)
4179 ce->flag = visibility;
4181 RB_OBJ_WRITE(klass, &ce->file, rb_source_location(&ce->line));
4187 ID id = rb_intern(name);
4190 rb_warn(
"rb_define_const: invalid name '%s' for constant", name);
4193 rb_vm_register_global_object(val);
4201 rb_define_const(rb_cObject, name, val);
4205set_const_visibility(
VALUE mod,
int argc,
const VALUE *argv,
4206 rb_const_flag_t flag, rb_const_flag_t mask)
4214 rb_warning(
"%"PRIsVALUE
" with no argument is just ignored",
4215 QUOTE_ID(rb_frame_callee()));
4219 for (i = 0; i < argc; i++) {
4221 VALUE val = argv[i];
4224 undefined_constant(mod, val);
4226 if ((ce = rb_const_lookup(mod,
id))) {
4229 if (UNDEF_P(ce->value)) {
4232 ele = autoload_data_for_named_constant(mod,
id, &ac);
4241 undefined_constant(mod,
ID2SYM(
id));
4251 long len = strlen(name);
4255 undefined_constant(mod, rb_fstring_new(name,
len));
4257 if (!(ce = rb_const_lookup(mod,
id))) {
4258 undefined_constant(mod,
ID2SYM(
id));
4260 ce->flag |= CONST_DEPRECATED;
4271rb_mod_private_constant(
int argc,
const VALUE *argv,
VALUE obj)
4273 set_const_visibility(obj, argc, argv, CONST_PRIVATE, CONST_VISIBILITY_MASK);
4285rb_mod_public_constant(
int argc,
const VALUE *argv,
VALUE obj)
4287 set_const_visibility(obj, argc, argv, CONST_PUBLIC, CONST_VISIBILITY_MASK);
4311rb_mod_deprecate_constant(
int argc,
const VALUE *argv,
VALUE obj)
4313 set_const_visibility(obj, argc, argv, CONST_DEPRECATED, CONST_DEPRECATED);
4318original_module(
VALUE c)
4326cvar_lookup_at(
VALUE klass,
ID id, st_data_t *v)
4329 if (RICLASS_IS_ORIGIN_P(klass)) {
4334 klass =
RBASIC(klass)->klass;
4339 if (UNDEF_P(n))
return 0;
4346cvar_front_klass(
VALUE klass)
4348 if (RCLASS_SINGLETON_P(klass)) {
4349 VALUE obj = RCLASS_ATTACHED_OBJECT(klass);
4350 if (rb_namespace_p(obj)) {
4360 if (front && target != front) {
4361 if (original_module(front) != original_module(target)) {
4363 "class variable % "PRIsVALUE
" of %"PRIsVALUE
" is overtaken by %"PRIsVALUE
"",
4368 rb_ivar_delete(front,
id,
Qundef);
4373#define CVAR_FOREACH_ANCESTORS(klass, v, r) \
4374 for (klass = cvar_front_klass(klass); klass; klass = RCLASS_SUPER(klass)) { \
4375 if (cvar_lookup_at(klass, id, (v))) { \
4380#define CVAR_LOOKUP(v,r) do {\
4381 CVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(); \
4382 if (cvar_lookup_at(klass, id, (v))) {r;}\
4383 CVAR_FOREACH_ANCESTORS(klass, v, r);\
4401check_for_cvar_table(
VALUE subclass,
VALUE key)
4405 RB_DEBUG_COUNTER_INC(cvar_class_invalidate);
4406 ruby_vm_global_cvar_state++;
4410 rb_class_foreach_subclass(subclass, check_for_cvar_table, key);
4416 VALUE tmp, front = 0, target = 0;
4419 CVAR_LOOKUP(0, {
if (!front) front = klass; target = klass;});
4421 cvar_overtaken(front, target,
id);
4428 target =
RBASIC(target)->klass;
4430 check_before_mod_set(target,
id, val,
"class variable");
4432 int result = rb_class_ivar_set(target,
id, val);
4434 struct rb_id_table *rb_cvc_tbl = RCLASS_WRITABLE_CVC_TBL(target);
4437 rb_cvc_tbl = rb_id_table_create(2);
4438 RCLASS_WRITE_CVC_TBL(target, rb_cvc_tbl);
4444 if (!rb_id_table_lookup(rb_cvc_tbl,
id, &ent_data)) {
4446 ent->class_value = target;
4447 ent->global_cvar_state = GET_GLOBAL_CVAR_STATE();
4449 rb_id_table_insert(rb_cvc_tbl,
id, (
VALUE)ent);
4450 RB_DEBUG_COUNTER_INC(cvar_inline_miss);
4453 ent = (
void *)ent_data;
4454 ent->global_cvar_state = GET_GLOBAL_CVAR_STATE();
4462 if (RCLASS_SUBCLASSES_FIRST(target)) {
4463 rb_class_foreach_subclass(target, check_for_cvar_table,
id);
4475 value = find_cvar(klass, front, &target,
id);
4477 rb_name_err_raise(
"uninitialized class variable %1$s in %2$s",
4480 cvar_overtaken(*front, target,
id);
4481 return (
VALUE)value;
4494 if (!klass)
return Qfalse;
4495 CVAR_LOOKUP(0,
return Qtrue);
4500cv_intern(
VALUE klass,
const char *name)
4502 ID id = rb_intern(name);
4504 rb_name_err_raise(
"wrong class variable name %1$s",
4513 ID id = cv_intern(klass, name);
4520 ID id = cv_intern(klass, name);
4531cv_i(
ID key,
VALUE v, st_data_t a)
4536 st_update(tbl, (st_data_t)key, cv_i_update, 0);
4542mod_cvar_at(
VALUE mod,
void *data)
4546 tbl = st_init_numtable();
4548 mod = original_module(mod);
4555mod_cvar_of(
VALUE mod,
void *data)
4558 if (RCLASS_SINGLETON_P(mod)) {
4559 if (rb_namespace_p(RCLASS_ATTACHED_OBJECT(mod))) {
4560 data = mod_cvar_at(tmp, data);
4561 tmp = cvar_front_klass(tmp);
4565 data = mod_cvar_at(tmp, data);
4573cv_list_i(st_data_t key, st_data_t value,
VALUE ary)
4581cvar_list(
void *data)
4617 bool inherit =
true;
4622 tbl = mod_cvar_of(mod, 0);
4625 tbl = mod_cvar_at(mod, 0);
4627 return cvar_list(tbl);
4652 const ID id = id_for_var_message(mod, name,
class,
"wrong class variable name %1$s");
4658 rb_check_frozen(mod);
4659 val = rb_ivar_delete(mod,
id,
Qundef);
4660 if (!UNDEF_P(val)) {
4664 rb_name_err_raise(
"cannot remove %1$s for %2$s", mod,
ID2SYM(
id));
4667 rb_name_err_raise(
"class variable %1$s not defined for %2$s",
4686 ID id = rb_intern(name);
4692class_fields_ivar_set(
VALUE klass,
VALUE fields_obj,
ID id,
VALUE val,
bool concurrent,
VALUE *new_fields_obj)
4694 bool existing =
true;
4695 const VALUE original_fields_obj = fields_obj;
4696 fields_obj = original_fields_obj ? original_fields_obj : rb_imemo_fields_new(
rb_singleton_class(klass), 1);
4698 shape_id_t current_shape_id = RBASIC_SHAPE_ID(fields_obj);
4699 shape_id_t next_shape_id = current_shape_id;
4701 if (UNLIKELY(rb_shape_too_complex_p(current_shape_id))) {
4706 if (!rb_shape_get_iv_index(current_shape_id,
id, &index)) {
4709 index = RSHAPE_LEN(current_shape_id);
4710 if (index >= SHAPE_MAX_FIELDS) {
4711 rb_raise(rb_eArgError,
"too many instance variables");
4714 next_shape_id = rb_shape_transition_add_ivar(fields_obj,
id);
4715 if (UNLIKELY(rb_shape_too_complex_p(next_shape_id))) {
4716 fields_obj = imemo_fields_complex_from_obj(
rb_singleton_class(klass), original_fields_obj, next_shape_id);
4720 attr_index_t next_capacity = RSHAPE_CAPACITY(next_shape_id);
4721 attr_index_t current_capacity = RSHAPE_CAPACITY(current_shape_id);
4723 if (concurrent || next_capacity != current_capacity) {
4724 RUBY_ASSERT(concurrent || next_capacity > current_capacity);
4729 if (original_fields_obj) {
4730 VALUE *fields = rb_imemo_fields_ptr(fields_obj);
4731 attr_index_t fields_count = RSHAPE_LEN(current_shape_id);
4732 MEMCPY(fields, rb_imemo_fields_ptr(original_fields_obj),
VALUE, fields_count);
4733 for (attr_index_t i = 0; i < fields_count; i++) {
4740 RUBY_ASSERT(index == (RSHAPE_LEN(next_shape_id) - 1));
4743 VALUE *fields = rb_imemo_fields_ptr(fields_obj);
4747 RBASIC_SET_SHAPE_ID(fields_obj, next_shape_id);
4750 *new_fields_obj = fields_obj;
4755 st_table *table = rb_imemo_fields_complex_tbl(fields_obj);
4756 existing = st_insert(table, (st_data_t)
id, (st_data_t)val);
4759 if (fields_obj != original_fields_obj) {
4760 RBASIC_SET_SHAPE_ID(fields_obj, next_shape_id);
4764 *new_fields_obj = fields_obj;
4772 rb_check_frozen(obj);
4774 rb_class_ensure_writable(obj);
4776 const VALUE original_fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
4777 VALUE new_fields_obj = 0;
4779 bool existing = class_fields_ivar_set(obj, original_fields_obj,
id, val, rb_multi_ractor_p(), &new_fields_obj);
4781 if (new_fields_obj != original_fields_obj) {
4782 RCLASS_WRITABLE_SET_FIELDS_OBJ(obj, new_fields_obj);
4789 RBASIC_SET_SHAPE_ID(obj, RBASIC_SHAPE_ID(new_fields_obj));
4795tbl_copy_i(
ID key,
VALUE val, st_data_t dest)
4797 rb_class_ivar_set((
VALUE)dest, key, val);
4807 RUBY_ASSERT(RSHAPE_TYPE_P(RBASIC_SHAPE_ID(dst), SHAPE_ROOT));
4819 r = rb_id_table_lookup(tbl,
id, &val);
4828rb_const_lookup(
VALUE klass,
ID id)
4830 return const_lookup(RCLASS_CONST_TBL(klass),
id);
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#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 VALUE RB_FL_TEST_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_TEST().
static bool RB_FL_ABLE(VALUE obj)
Checks if the object is flaggable.
static void RB_FL_SET_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_SET().
void rb_obj_freeze_inline(VALUE obj)
Prevents further modifications to the given object.
static void RB_FL_UNSET_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_UNSET().
@ RUBY_FL_FREEZE
This flag has something to do with data immutability.
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
void rb_class_modify_check(VALUE klass)
Asserts that klass is not a frozen class.
void rb_freeze_singleton_class(VALUE x)
This is an implementation detail of RB_OBJ_FREEZE().
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.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define TYPE(_)
Old name of rb_type.
#define FL_USER3
Old name of RUBY_FL_USER3.
#define REALLOC_N
Old name of RB_REALLOC_N.
#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 rb_str_cat2
Old name of rb_str_cat_cstr.
#define T_IMEMO
Old name of RUBY_T_IMEMO.
#define ID2SYM
Old name of RB_ID2SYM.
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define ZALLOC
Old name of RB_ZALLOC.
#define CLASS_OF
Old name of rb_class_of.
#define T_MODULE
Old name of RUBY_T_MODULE.
#define T_ICLASS
Old name of RUBY_T_ICLASS.
#define ALLOC_N
Old name of RB_ALLOC_N.
#define rb_ary_new3
Old name of rb_ary_new_from_args.
#define FL_USER2
Old name of RUBY_FL_USER2.
#define Qtrue
Old name of RUBY_Qtrue.
#define INT2NUM
Old name of RB_INT2NUM.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_OBJECT
Old name of RUBY_T_OBJECT.
#define NIL_P
Old name of RB_NIL_P.
#define ALLOCV_N
Old name of RB_ALLOCV_N.
#define T_CLASS
Old name of RUBY_T_CLASS.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
#define rb_ary_new2
Old name of rb_ary_new_capa.
#define ALLOCV_END
Old name of RB_ALLOCV_END.
void rb_category_warn(rb_warning_category_t category, const char *fmt,...)
Identical to rb_category_warning(), except it reports unless $VERBOSE is nil.
void rb_name_error(ID id, const char *fmt,...)
Raises an instance of rb_eNameError.
VALUE rb_eTypeError
TypeError exception.
void rb_name_error_str(VALUE str, const char *fmt,...)
Identical to rb_name_error(), except it takes a VALUE instead of ID.
VALUE rb_eNameError
NameError exception.
VALUE rb_eRuntimeError
RuntimeError exception.
void * rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type)
Identical to rb_typeddata_is_kind_of(), except it raises exceptions instead of returning false.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
void rb_warning(const char *fmt,...)
Issues a warning.
@ RB_WARN_CATEGORY_DEPRECATED
Warning is for deprecated features.
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_cModule
Module class.
VALUE rb_class_real(VALUE klass)
Finds a "real" class.
size_t rb_obj_embedded_size(uint32_t fields_count)
Internal header for Object.
#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.
ID rb_check_id_cstr(const char *ptr, long len, rb_encoding *enc)
Identical to rb_check_id(), except it takes a pointer to a memory region instead of Ruby's string.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_ary_new(void)
Allocates a new, empty array.
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.
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Identical to rb_ary_new_from_values(), except it expects exactly two parameters.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
#define st_foreach_safe
Just another name of rb_st_foreach_safe.
int rb_feature_provided(const char *feature, const char **loading)
Identical to rb_provided(), except it additionally returns the "canonical" name of the loaded feature...
VALUE rb_backref_get(void)
Queries the last match, or Regexp.last_match, or the $~.
int rb_is_instance_id(ID id)
Classifies the given ID, then sees if it is an instance variable.
int rb_is_const_id(ID id)
Classifies the given ID, then sees if it is a constant.
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_reg_nth_defined(int n, VALUE md)
Identical to rb_reg_nth_match(), except it just returns Boolean.
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
VALUE rb_str_subseq(VALUE str, long beg, long len)
Identical to rb_str_substr(), except the numbers are interpreted as byte offsets instead of character...
VALUE rb_str_new_frozen(VALUE str)
Creates a frozen copy of the string, if necessary.
VALUE rb_str_dup(VALUE str)
Duplicates a string.
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
VALUE rb_str_intern(VALUE str)
Identical to rb_to_symbol(), except it assumes the receiver being an instance of RString.
VALUE rb_mutex_new(void)
Creates a mutex.
VALUE rb_mutex_synchronize(VALUE mutex, VALUE(*func)(VALUE arg), VALUE arg)
Obtains the lock, runs the passed function, and releases the lock when it completes.
VALUE rb_exec_recursive_paired(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE p, VALUE h)
Identical to rb_exec_recursive(), except it checks for the recursion on the ordered pair of { g,...
VALUE rb_mod_remove_cvar(VALUE mod, VALUE name)
Resembles Module#remove_class_variable.
VALUE rb_obj_instance_variables(VALUE obj)
Resembles Object#instance_variables.
VALUE rb_f_untrace_var(int argc, const VALUE *argv)
Deletes the passed tracer from the passed global variable, or if omitted, deletes everything.
VALUE rb_const_get(VALUE space, ID name)
Identical to rb_const_defined(), except it returns the actual defined value.
VALUE rb_const_list(void *)
This is another mysterious API that comes with no documents at all.
VALUE rb_path2class(const char *path)
Resolves a Q::W::E::R-style path string to the actual class it points.
VALUE rb_autoload_p(VALUE space, ID name)
Queries if an autoload is defined at a point.
void rb_set_class_path(VALUE klass, VALUE space, const char *name)
Names a class.
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
VALUE rb_mod_remove_const(VALUE space, VALUE name)
Resembles Module#remove_const.
VALUE rb_class_path_cached(VALUE mod)
Just another name of rb_mod_name.
VALUE rb_f_trace_var(int argc, const VALUE *argv)
Traces a global variable.
void rb_cvar_set(VALUE klass, ID name, VALUE val)
Assigns a value to a class variable.
VALUE rb_cvar_get(VALUE klass, ID name)
Obtains a value from a class variable.
VALUE rb_mod_constants(int argc, const VALUE *argv, VALUE recv)
Resembles Module#constants.
VALUE rb_cvar_find(VALUE klass, ID name, VALUE *front)
Identical to rb_cvar_get(), except it takes additional "front" pointer.
VALUE rb_path_to_class(VALUE path)
Identical to rb_path2class(), except it accepts the path as Ruby's string instead of C's.
VALUE rb_ivar_get(VALUE obj, ID name)
Identical to rb_iv_get(), except it accepts the name as an ID instead of a C string.
void rb_const_set(VALUE space, ID name, VALUE val)
Names a constant.
VALUE rb_autoload_load(VALUE space, ID name)
Kicks the autoload procedure as if it was "touched".
VALUE rb_mod_name(VALUE mod)
Queries the name of a module.
VALUE rb_class_name(VALUE obj)
Queries the name of the given object's class.
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.
void rb_alias_variable(ID dst, ID src)
Aliases a global variable.
void rb_define_class_variable(VALUE, const char *, VALUE)
Just another name of rb_cv_set.
VALUE rb_obj_remove_instance_variable(VALUE obj, VALUE name)
Resembles Object#remove_instance_variable.
void * rb_mod_const_of(VALUE, void *)
This is a variant of rb_mod_const_at().
st_index_t rb_ivar_count(VALUE obj)
Number of instance variables defined on an object.
void * rb_mod_const_at(VALUE, void *)
This API is mysterious.
VALUE rb_const_remove(VALUE space, ID name)
Identical to rb_mod_remove_const(), except it takes the name as ID instead of VALUE.
VALUE rb_const_get_from(VALUE space, ID name)
Identical to rb_const_defined_at(), except it returns the actual defined value.
VALUE rb_ivar_defined(VALUE obj, ID name)
Queries if the instance variable is defined at the object.
VALUE rb_cv_get(VALUE klass, const char *name)
Identical to rb_cvar_get(), except it accepts C's string instead of ID.
int rb_const_defined_at(VALUE space, ID name)
Identical to rb_const_defined(), except it doesn't look for parent classes.
void rb_cv_set(VALUE klass, const char *name, VALUE val)
Identical to rb_cvar_set(), except it accepts C's string instead of ID.
VALUE rb_mod_class_variables(int argc, const VALUE *argv, VALUE recv)
Resembles Module#class_variables.
VALUE rb_f_global_variables(void)
Queries the list of global variables.
VALUE rb_cvar_defined(VALUE klass, ID name)
Queries if the given class has the given class variable.
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
int rb_const_defined_from(VALUE space, ID name)
Identical to rb_const_defined(), except it returns false for private constants.
int rb_const_defined(VALUE space, ID name)
Queries if the constant is defined at the namespace.
void rb_free_generic_ivar(VALUE obj)
Frees the list of instance variables.
const char * rb_sourcefile(void)
Resembles __FILE__.
void rb_clear_constant_cache_for_id(ID id)
Clears the inline constant caches associated with a particular ID.
VALUE rb_eval_cmd_kw(VALUE cmd, VALUE arg, int kw_splat)
This API is practically a variant of rb_proc_call_kw() now.
int rb_obj_respond_to(VALUE obj, ID mid, int private_p)
Identical to rb_respond_to(), except it additionally takes the visibility parameter.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
VALUE rb_id2sym(ID id)
Allocates an instance of rb_cSymbol that has the given id.
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
rb_gvar_setter_t rb_gvar_var_setter
rb_gvar_marker_t rb_gvar_var_marker
void rb_define_global_const(const char *name, VALUE val)
Identical to rb_define_const(), except it defines that of "global", i.e.
VALUE rb_gv_get(const char *name)
Obtains a global variable.
void rb_define_variable(const char *name, VALUE *var)
"Shares" a global variable between Ruby and C.
void rb_gvar_marker_t(VALUE *var)
Type that represents a global variable marker function.
void rb_deprecate_constant(VALUE mod, const char *name)
Asserts that the given constant is deprecated.
void rb_gvar_setter_t(VALUE val, ID id, VALUE *data)
Type that represents a global variable setter function.
rb_gvar_setter_t rb_gvar_val_setter
This is the setter function that backs global variables defined from a ruby script.
rb_gvar_marker_t rb_gvar_undef_marker
void rb_define_readonly_variable(const char *name, const VALUE *var)
Identical to rb_define_variable(), except it does not allow Ruby programs to assign values to such gl...
rb_gvar_setter_t rb_gvar_readonly_setter
This function just raises rb_eNameError.
rb_gvar_getter_t rb_gvar_undef_getter
VALUE rb_gv_set(const char *name, VALUE val)
Assigns to a global variable.
rb_gvar_marker_t rb_gvar_val_marker
This is the setter function that backs global variables defined from a ruby script.
VALUE rb_gvar_getter_t(ID id, VALUE *data)
Type that represents a global variable getter function.
VALUE rb_iv_get(VALUE obj, const char *name)
Obtains an instance variable.
rb_gvar_setter_t rb_gvar_undef_setter
rb_gvar_getter_t rb_gvar_val_getter
This is the getter function that backs global variables defined from a ruby script.
VALUE rb_iv_set(VALUE obj, const char *name, VALUE val)
Assigns to an instance variable.
rb_gvar_getter_t rb_gvar_var_getter
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.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
#define MEMMOVE(p1, p2, type, n)
Handy macro to call memmove.
void rb_define_hooked_variable(const char *q, VALUE *w, type *e, void_type *r)
Define a function-backended global variable.
VALUE type(ANYARGS)
ANYARGS-ed function type.
void rb_define_virtual_variable(const char *q, type *w, void_type *e)
Define a function-backended global variable.
void rb_ivar_foreach(VALUE q, int_type *w, VALUE e)
Iteration over each instance variable of the object.
VALUE rb_ensure(type *q, VALUE w, type *e, VALUE r)
An equivalent of ensure clause.
void rb_copy_generic_ivar(VALUE clone, VALUE obj)
Copies the list of instance variables.
#define RARRAY_LEN
Just another name of rb_array_len.
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
#define RBASIC(obj)
Convenient casting macro.
#define RCLASS_SUPER
Just another name of rb_class_get_superclass.
#define ROBJECT(obj)
Convenient casting macro.
static VALUE * ROBJECT_FIELDS(VALUE obj)
Queries the instance variables.
#define StringValue(v)
Ensures that the parameter object is a String.
static char * RSTRING_END(VALUE str)
Queries the end of the contents pointer of the string.
#define RTYPEDDATA_DATA(v)
Convenient getter macro.
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
const char * rb_class2name(VALUE klass)
Queries the name of the passed class.
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
#define RB_NO_KEYWORDS
Do not pass keywords.
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
This is the struct that holds necessary info for a struct.
Internal header for Namespace.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static enum ruby_value_type rb_type(VALUE obj)
Identical to RB_BUILTIN_TYPE(), except it can also accept special constants.
static enum ruby_value_type RB_BUILTIN_TYPE(VALUE obj)
Queries the type of the 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.