14#include "ruby/internal/config.h"
17#include "ccan/list/list.h"
19#include "debug_counter.h"
23#include "internal/box.h"
24#include "internal/class.h"
25#include "internal/compilers.h"
26#include "internal/error.h"
27#include "internal/eval.h"
28#include "eval_intern.h"
29#include "internal/hash.h"
30#include "internal/object.h"
31#include "internal/gc.h"
32#include "internal/re.h"
33#include "internal/string.h"
34#include "internal/struct.h"
35#include "internal/symbol.h"
36#include "internal/thread.h"
37#include "internal/variable.h"
38#include "internal/vm.h"
46#include "ractor_core.h"
50#define GET_GLOBAL_CVAR_STATE() (ruby_vm_global_cvar_state)
52typedef void rb_gvar_compact_t(
void *var);
59static VALUE autoload_features;
64static VALUE autoload_mutex;
66static void check_before_mod_set(
VALUE,
ID,
VALUE,
const char *);
68static VALUE rb_const_search(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility,
VALUE *found_in);
77typedef int rb_ivar_foreach_callback_func(
ID key,
VALUE val, st_data_t arg);
78static void rb_field_foreach(
VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg,
bool ivar_only);
81rb_generic_fields_lock_atfork(
void)
90 rb_global_tbl = rb_id_table_create(0);
91 generic_fields_tbl_ = st_init_numtable();
95 rb_vm_register_global_object(autoload_mutex);
97 autoload_features = rb_ident_hash_new();
99 rb_vm_register_global_object(autoload_features);
103rb_namespace_p(
VALUE obj)
124classname(
VALUE klass,
bool *permanent)
128 VALUE classpath = RCLASS_CLASSPATH(klass);
129 if (classpath == 0)
return Qnil;
131 *permanent = RCLASS_PERMANENT_CLASSPATH_P(klass);
137rb_mod_name0(
VALUE klass,
bool *permanent)
139 return classname(klass, permanent);
154 return classname(mod, &permanent);
159is_constant_path(
VALUE name)
161 const char *path = RSTRING_PTR(name);
165 const char *p = path;
167 if (p >= pend || !*p) {
172 if (p + 2 <= pend && p[0] ==
':' && p[1] ==
':') {
176 const char *pbeg = p;
177 while (p < pend && *p !=
':') p++;
179 if (pbeg == p)
return false;
181 if (rb_enc_symname_type(pbeg, p - pbeg, enc, 0) != ID_CONST) {
198set_sub_temporary_name_recursive(
VALUE mod,
VALUE data,
int recursive)
200 if (recursive)
return Qfalse;
205 name = build_const_path(rb_ary_last(0, 0, args->names), args->last);
207 set_sub_temporary_name_foreach(mod, args, name);
212set_sub_temporary_name_topmost(
VALUE mod,
VALUE data,
int recursive)
214 if (recursive)
return Qfalse;
217 VALUE name = args->names;
221 set_sub_temporary_name_foreach(mod, args, name);
225static enum rb_id_table_iterator_result
226set_sub_temporary_name_i(
ID id,
VALUE val,
void *data)
229 if (rb_namespace_p(val) && !RCLASS_PERMANENT_CLASSPATH_P(val)) {
235 return ID_TABLE_CONTINUE;
241 RCLASS_WRITE_CLASSPATH(mod, name, FALSE);
245 rb_id_table_foreach(tbl, set_sub_temporary_name_i, args);
250 rb_id_table_foreach(tbl, set_sub_temporary_name_i, args);
251 rb_ary_set_len(args->names, names_len);
312rb_mod_set_temporary_name(
VALUE mod,
VALUE name)
314 rb_class_owner_check(mod);
317 if (RCLASS_PERMANENT_CLASSPATH_P(mod)) {
324 set_sub_temporary_name(mod, 0);
331 if (RSTRING_LEN(name) == 0) {
332 rb_raise(rb_eArgError,
"empty class/module name");
335 if (is_constant_path(name)) {
336 rb_raise(rb_eArgError,
"the temporary name must not be a constant path to avoid confusion");
344 set_sub_temporary_name(mod, name);
357 path = rb_sprintf(
"#<Class:%p>", (
void*)obj);
360 path = rb_sprintf(
"#<Module:%p>", (
void*)obj);
363 path = rb_sprintf(
"#<%"PRIsVALUE
":%p>", klass, (
void*)obj);
373rb_tmp_class_path(
VALUE klass,
bool *permanent, fallback_func fallback)
375 VALUE path = classname(klass, permanent);
387 path = rb_tmp_class_path(
RBASIC(klass)->klass, &perm, fallback);
392 return fallback(klass, path);
399 VALUE path = rb_tmp_class_path(klass, &permanent, make_temporary_path);
417rb_search_class_path(
VALUE klass)
420 return rb_tmp_class_path(klass, &permanent, no_fallback);
429 return rb_fstring(path);
433build_const_path(
VALUE head,
ID tail)
435 return build_const_pathname(head, rb_id2str(tail));
441 bool permanent =
true;
448 str = rb_tmp_class_path(under, &permanent, make_temporary_path);
449 str = build_const_pathname(str, name);
453 RCLASS_SET_CLASSPATH(klass, str, permanent);
468 const char *pbeg, *pend, *p, *path = RSTRING_PTR(pathname);
472 if (!rb_enc_asciicompat(enc)) {
473 rb_raise(rb_eArgError,
"invalid class path encoding (non ASCII)");
476 pend = path + RSTRING_LEN(pathname);
477 if (path == pend || path[0] ==
'#') {
478 rb_raise(rb_eArgError,
"can't retrieve anonymous class %"PRIsVALUE,
482 while (p < pend && *p !=
':') p++;
484 if (p < pend && p[0] ==
':') {
485 if ((
size_t)(pend - p) < 2 || p[1] !=
':')
goto undefined_class;
490 goto undefined_class;
492 c = rb_const_search(c,
id, TRUE, FALSE, FALSE, NULL);
493 if (UNDEF_P(c))
goto undefined_class;
494 if (!rb_namespace_p(c)) {
495 rb_raise(
rb_eTypeError,
"%"PRIsVALUE
" does not refer to class/module",
504 rb_raise(rb_eArgError,
"undefined class/module % "PRIsVALUE,
525 VALUE path = rb_tmp_class_path(
rb_class_real(klass), &permanent, make_temporary_path);
526 if (
NIL_P(path))
return NULL;
527 return RSTRING_PTR(path);
550 rb_gvar_compact_t *compactor;
577static enum rb_id_table_iterator_result
578free_global_entry_i(
VALUE val,
void *arg)
581 entry->var->counter--;
582 if (entry->var->counter == 0) {
583 free_global_variable(entry->var);
586 return ID_TABLE_DELETE;
590rb_free_rb_global_tbl(
void)
592 rb_id_table_foreach_values(rb_global_tbl, free_global_entry_i, 0);
593 rb_id_table_free(rb_global_tbl);
597rb_free_generic_fields_tbl_(
void)
599 st_free_table(generic_fields_tbl_);
603rb_gvar_undef_compactor(
void *var)
607NORETURN(
static void global_entry_isolation_error(
ID id));
610global_entry_isolation_error(
ID id)
612 rb_raise(rb_eRactorIsolationError,
"can not access global variable %s from non-main Ractor", rb_id2name(
id));
618global_entry_lookup(
ID id,
bool create_entry,
bool *isolation_error)
624 if (rb_id_table_lookup(rb_global_tbl,
id, &data)) {
632 *isolation_error = UNLIKELY(!rb_ractor_main_p()) && (!entry || !entry->ractor_local);
634 if (!entry && create_entry && !*isolation_error) {
639 entry->ractor_local =
false;
646 var->compactor = rb_gvar_undef_compactor;
648 var->block_trace = 0;
650 var->box_ready =
false;
651 var->box_dynamic =
false;
652 rb_id_table_insert(rb_global_tbl,
id, (
VALUE)entry);
660rb_find_global_entry(
ID id)
662 bool isolation_error;
663 struct rb_global_entry *entry = global_entry_lookup(
id,
false, &isolation_error);
665 if (isolation_error) global_entry_isolation_error(
id);
671rb_gvar_ractor_local(
const char *name)
674 entry->ractor_local =
true;
678rb_gvar_box_ready(
const char *name)
681 entry->var->box_ready =
true;
685rb_gvar_box_dynamic(
const char *name)
688 entry->var->box_dynamic =
true;
694 bool isolation_error;
695 struct rb_global_entry *entry = global_entry_lookup(
id,
true, &isolation_error);
697 if (isolation_error) global_entry_isolation_error(
id);
705 rb_warning(
"global variable '%"PRIsVALUE
"' not initialized", QUOTE_ID(
id));
711rb_gvar_val_compactor(
void *_var)
716 rb_gc_update_moved_ptr(&var->data);
727 var->compactor = rb_gvar_val_compactor;
729 var->data = (
void*)val;
747 var->data = (
void*)val;
754 if (data) rb_gc_mark_movable(data);
760 if (!var)
return Qnil;
773 if (var) rb_gc_mark_maybe(*var);
779 rb_name_error(
id,
"%"PRIsVALUE
" is a read-only variable", QUOTE_ID(
id));
782static enum rb_id_table_iterator_result
783mark_global_entry(
VALUE v,
void *ignored)
789 (*var->marker)(var->data);
792 if (trace->data) rb_gc_mark_maybe(trace->data);
795 return ID_TABLE_CONTINUE;
798#define gc_mark_table(task) \
799 if (rb_global_tbl) { rb_id_table_foreach_values(rb_global_tbl, task##_global_entry, 0); }
802rb_gc_mark_global_tbl(
void)
807static enum rb_id_table_iterator_result
808update_global_entry(
VALUE v,
void *ignored)
813 (*var->compactor)(var);
814 return ID_TABLE_CONTINUE;
818rb_gc_update_global_tbl(
void)
820 gc_mark_table(update);
824global_id(
const char *name)
828 if (name[0] ==
'$')
id = rb_intern(name);
830 size_t len = strlen(name);
834 memcpy(buf+1, name,
len);
835 id = rb_intern2(buf,
len+1);
842find_global_id(
const char *name)
845 size_t len = strlen(name);
847 if (name[0] ==
'$') {
854 memcpy(buf+1, name,
len);
870 ID id = global_id(name);
873 gvar->data = (
void*)var;
925 trace->next = entry->var->trace;
926 trace->func = rb_trace_eval;
929 entry->var->trace = trace;
943 while (trace->next) {
946 trace->next = next->next;
969 if ((entry = rb_find_global_entry(
id)) == NULL) {
970 rb_name_error(
id,
"undefined global variable %"PRIsVALUE
"", QUOTE_ID(
id));
973 trace = entry->var->trace;
984 if (!entry->var->block_trace) remove_trace(entry->var);
989 if (trace->data == cmd) {
991 if (!entry->var->block_trace) remove_trace(entry->var);
1012 (*trace->func)(trace->data, data->val);
1013 trace = trace->next;
1023 var->block_trace = 0;
1033 if (var->trace && !var->block_trace) {
1034 var->block_trace = 1;
1035 trace.trace = var->trace;
1046 (*var->setter)(val, entry->id, var->data);
1047 gvar_trace(var, val);
1054 return BOX_USER_P(box) &&
1055 !entry->var->box_dynamic &&
1064 const rb_box_t *box = rb_current_box();
1065 bool use_box_tbl =
false;
1066 bool isolation_error =
false;
1069 entry = global_entry_lookup(
id,
true, &isolation_error);
1071 if (!isolation_error && gvar_use_box_tbl(box, entry)) {
1073 rb_hash_aset(box->gvar_tbl,
rb_id2sym(entry->var->id), val);
1078 if (isolation_error) global_entry_isolation_error(
id);
1081 gvar_trace(entry->var, val);
1084 retval = rb_gvar_set_entry(entry, val);
1092 return rb_gvar_set(global_id(name), val);
1098 VALUE retval, gvars, key;
1099 const rb_box_t *box = rb_current_box();
1100 bool use_box_tbl =
false;
1101 bool isolation_error =
false;
1107 entry = global_entry_lookup(
id,
true, &isolation_error);
1109 if (!isolation_error) {
1112 if (gvar_use_box_tbl(box, entry)) {
1114 gvars = box->gvar_tbl;
1116 if (
RTEST(rb_hash_has_key(gvars, key))) {
1117 retval = rb_hash_aref(gvars, key);
1125 retval = (*var->getter)(entry->id, var->data);
1127 retval =
rb_funcall(retval, rb_intern(
"clone"), 0);
1131 if (cache) rb_hash_aset(gvars, key, retval);
1137 if (isolation_error) global_entry_isolation_error(
id);
1140 retval = (*var->getter)(entry->id, var->data);
1148 ID id = find_global_id(name);
1151 rb_warning(
"global variable '%s' not initialized", name);
1155 return rb_gvar_get(
id);
1159rb_gvar_defined(
ID id)
1161 const rb_box_t *box = rb_current_box();
1168 (gvar_use_box_tbl(box, entry) &&
1169 RTEST(rb_hash_has_key(box->gvar_tbl,
rb_id2sym(entry->var->id))));
1171 return RBOOL(defined);
1175rb_gvar_getter_function_of(
ID id)
1178 return entry->var->getter;
1182rb_gvar_setter_function_of(
ID id)
1185 return entry->var->setter;
1188static enum rb_id_table_iterator_result
1189gvar_i(
ID key,
VALUE val,
void *a)
1193 return ID_TABLE_CONTINUE;
1202 if (!rb_ractor_main_p()) {
1203 rb_raise(rb_eRactorIsolationError,
"can not access global variables from non-main Ractors");
1207 rb_id_table_foreach(rb_global_tbl, gvar_i, (
void *)ary);
1208 if (!
NIL_P(backref)) {
1210 int i, nmatch = rb_match_count(backref);
1212 for (i = 1; i <= nmatch; ++i) {
1216 buf[1] = (char)(i +
'0');
1217 sym =
ID2SYM(rb_intern2(buf, 2));
1235 bool tracer_error =
false;
1237 if (!rb_ractor_main_p()) {
1238 rb_raise(rb_eRactorIsolationError,
"can not access global variables from non-main Ractors");
1242 bool isolation_error;
1243 entry2 = global_entry_lookup(name2,
true, &isolation_error);
1244 VM_ASSERT(!isolation_error);
1246 if (!rb_id_table_lookup(gtbl, name1, &data1)) {
1249 rb_id_table_insert(gtbl, name1, (
VALUE)entry1);
1251 else if ((entry1 = (
struct rb_global_entry *)data1)->var != entry2->var) {
1253 if (var->block_trace) {
1254 tracer_error =
true;
1258 if (var->counter == 0) {
1259 free_global_variable(var);
1263 if (!tracer_error && entry1->var != entry2->var) {
1264 entry2->var->counter++;
1265 entry1->var = entry2->var;
1273class_ivar_set_ractor_check(
VALUE klass,
ID id)
1276 UNLIKELY(!rb_class_owned_p(klass))) {
1278 rb_eRactorIsolationError,
1279 "can not set instance variables of classes/modules "
1280 "created by another Ractor (%"PRIsVALUE
" from %"PRIsVALUE
")",
1290cvar_set_ractor_check(
VALUE klass,
ID id)
1292 if (UNLIKELY(!rb_class_owned_p(klass))) {
1293 rb_raise(rb_eRactorIsolationError,
1294 "can not set class variable %"PRIsVALUE
" of %"PRIsVALUE
", which was created by another Ractor",
1295 rb_id2str(
id), klass);
1303 rb_raise(rb_eRactorIsolationError,
1304 "can not read non-shareable class variable %"PRIsVALUE
" of %"PRIsVALUE
", which was created by another Ractor",
1305 rb_id2str(
id), klass);
1310ivar_ractor_assert(
VALUE obj,
ID id)
1318 rb_shape_frozen_p(RBASIC_SHAPE_ID(obj)),
1319 "shareable object must not have writable instance variables");
1323rb_generic_fields_tbl_get(
void)
1325 return generic_fields_tbl_;
1334 if (rb_multi_ractor_p()) {
1342 if (rb_multi_ractor_p()) {
1348rb_mark_generic_ivar(
VALUE obj)
1353 if (rb_gc_during_global_gc_p() && rb_gc_multi_objspace_p()) {
1361 st_lookup(generic_fields_tbl_, (st_data_t)obj, (st_data_t *)&data);
1364 rb_gc_mark_movable(data);
1372rb_obj_fields_generic_uncached(
VALUE obj)
1374 VALUE fields_obj = 0;
1378 found = st_lookup(generic_fields_tbl_, (st_data_t)obj, (st_data_t *)&fields_obj);
1382 rb_bug(
"Object is missing entry in generic_fields_tbl");
1388obj_use_generic_fields_tbl_p(
VALUE obj)
1403rb_obj_fields(
VALUE obj,
ID field_name)
1405 ivar_ractor_assert(obj, field_name);
1413 return ROBJECT_FIELDS_OBJ(obj);
1417 return RCLASS_WRITABLE_FIELDS_OBJ(obj);
1420 return rbimpl_atomic_value_load(&
RTYPEDDATA(obj)->fields_obj, RBIMPL_ATOMIC_RELAXED);
1423 return RSTRUCT_FIELDS_OBJ(obj);
1427 VALUE fields_obj = 0;
1429 if (rb_obj_shape_has_fields(obj)) {
1431 if (ec->gen_fields_cache.obj == obj && !UNDEF_P(ec->gen_fields_cache.fields_obj) && rb_imemo_fields_owner(ec->gen_fields_cache.fields_obj) == obj) {
1432 fields_obj = ec->gen_fields_cache.fields_obj;
1433 RUBY_ASSERT(fields_obj == rb_obj_fields_generic_uncached(obj));
1436 fields_obj = rb_obj_fields_generic_uncached(obj);
1437 ec->gen_fields_cache.fields_obj = fields_obj;
1438 ec->gen_fields_cache.obj = obj;
1450 if (rb_obj_gen_fields_p(obj)) {
1451 st_data_t key = (st_data_t)obj, value;
1457 RSTRUCT_SET_FIELDS_OBJ(obj, 0);
1465 if (ec->gen_fields_cache.obj == obj) {
1466 ec->gen_fields_cache.obj =
Qundef;
1467 ec->gen_fields_cache.fields_obj =
Qundef;
1472 if (rb_gc_during_global_gc_p() || ruby_vm_during_cleanup) {
1480 deleted = st_delete(generic_fields_tbl_, &key, &value);
1483 rb_bug(
"Object is missing entry in generic_fields_tbl");
1487 RBASIC_SET_SHAPE_ID(obj, ROOT_SHAPE_ID);
1492rb_obj_set_fields(
VALUE obj,
VALUE fields_obj,
ID field_name,
VALUE original_fields_obj)
1494 ivar_ractor_assert(obj, field_name);
1499 rb_imemo_fields_clear(original_fields_obj);
1503 RUBY_ASSERT(IMEMO_TYPE_P(fields_obj, imemo_fields));
1507 if (fields_obj != original_fields_obj) {
1511 ROBJECT_SET_EXTENDED(obj, fields_obj);
1514 RB_OBJ_ATOMIC_WRITE(obj, &
RTYPEDDATA(obj)->fields_obj, fields_obj);
1517 RSTRUCT_SET_FIELDS_OBJ(obj, fields_obj);
1526 bool gc_disabled =
RTEST(rb_gc_local_disable_no_rest());
1528 enum ruby_tag_type state;
1530 EC_PUSH_TAG(insert_ec);
1531 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1532 st_insert(generic_fields_tbl_, (st_data_t)obj, (st_data_t)fields_obj);
1536 if (!gc_disabled) rb_gc_local_enable();
1537 if (state != TAG_NONE) EC_JUMP_TAG(insert_ec, state);
1541 if (ec->gen_fields_cache.fields_obj != fields_obj) {
1542 ec->gen_fields_cache.obj = obj;
1543 ec->gen_fields_cache.fields_obj = fields_obj;
1548 if (original_fields_obj && original_fields_obj != obj) {
1550 rb_imemo_fields_clear(original_fields_obj);
1555 RBASIC_SET_SHAPE_ID_WITH_LAYOUT(obj, RBASIC_SHAPE_ID(fields_obj), SHAPE_ID_LAYOUT_EXTENDED);
1558 RBASIC_SET_SHAPE_ID(obj, RBASIC_SHAPE_ID(fields_obj));
1563rb_obj_replace_fields(
VALUE obj,
VALUE fields_obj)
1565 if (obj_use_generic_fields_tbl_p(obj)) {
1569 VALUE original_fields_obj = rb_obj_fields_no_ractor_check(obj);
1570 rb_obj_set_fields(obj, fields_obj, 0, original_fields_obj);
1574 VALUE original_fields_obj = rb_obj_fields_no_ractor_check(obj);
1575 rb_obj_set_fields(obj, fields_obj, 0, original_fields_obj);
1580rb_imemo_fields_field_get(
VALUE fields_obj, shape_id_t target_shape_id)
1582 RUBY_ASSERT(RSHAPE_TYPE_P(target_shape_id, SHAPE_IVAR) || RSHAPE_TYPE_P(target_shape_id, SHAPE_OBJ_ID));
1584 if (UNLIKELY(rb_shape_complex_p(target_shape_id))) {
1585 st_table *fields_hash = rb_imemo_fields_complex_tbl(fields_obj);
1587 st_lookup(fields_hash, RSHAPE_EDGE_NAME(target_shape_id), &value);
1592 attr_index_t index = RSHAPE_INDEX(target_shape_id);
1593 return rb_imemo_fields_ptr(fields_obj)[index];
1597rb_obj_field_get(
VALUE obj, shape_id_t target_shape_id)
1601 return rb_imemo_fields_field_get(rb_obj_fields(obj, RSHAPE_EDGE_NAME(target_shape_id)), target_shape_id);
1611 VALUE fields_obj = rb_obj_fields(obj, is_class ? 0 : id);
1617 shape_id_t shape_id = RBASIC_SHAPE_ID(fields_obj);
1620 if (UNLIKELY(rb_shape_complex_p(shape_id))) {
1621 st_table *iv_table = rb_imemo_fields_complex_tbl(fields_obj);
1622 if (!rb_st_lookup(iv_table, (st_data_t)
id, (st_data_t *)&val)) {
1627 attr_index_t index = 0;
1628 if (!rb_shape_get_iv_index(shape_id,
id, &index)) {
1631 val = rb_imemo_fields_ptr(fields_obj)[index];
1637 rb_eRactorIsolationError,
1638 "can not get unshareable values from instance variables of classes/modules "
1639 "created by another Ractor (%"PRIsVALUE
" from %"PRIsVALUE
")",
1652 VALUE iv = rb_ivar_lookup(obj,
id,
Qnil);
1653 RB_DEBUG_COUNTER_INC(ivar_get_base);
1658rb_ivar_get_at(
VALUE obj, attr_index_t index,
ID id)
1669 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
1670 VALUE val = rb_imemo_fields_ptr(fields_obj)[index];
1674 rb_eRactorIsolationError,
1675 "can not get unshareable values from instance variables of classes/modules "
1676 "created by another Ractor (%"PRIsVALUE
" from %"PRIsVALUE
")",
1686 VALUE fields_obj = rb_obj_fields(obj,
id);
1687 return rb_imemo_fields_ptr(fields_obj)[index];
1693rb_ivar_get_at_no_ractor_check(
VALUE obj, attr_index_t index)
1697 VALUE fields_obj = rb_obj_fields_no_ractor_check(obj);
1698 return rb_imemo_fields_ptr(fields_obj)[index];
1704 return rb_ivar_lookup(obj,
id,
Qnil);
1707static VALUE imemo_fields_evacutate_to_complex(
VALUE owner,
VALUE source_fields_obj, shape_id_t shape_id,
int extra_capa);
1710rb_obj_convert_too_complex(
VALUE obj,
VALUE fields_obj, shape_id_t shape_id)
1715 shape_id = rb_shape_transition_complex(shape_id);
1716 VALUE new_fields_obj = imemo_fields_evacutate_to_complex(obj, fields_obj, shape_id, 1);
1717 ROBJECT_SET_EXTENDED(obj, new_fields_obj);
1718 RBASIC_SET_SHAPE_ID_WITH_LAYOUT(obj, shape_id, SHAPE_ID_LAYOUT_EXTENDED);
1725 rb_check_frozen(obj);
1728 bool concurrent =
false;
1732 class_ivar_set_ractor_check(obj,
id);
1734 if (rb_multi_ractor_p()) {
1739 VALUE fields_obj = rb_obj_fields(obj,
id);
1744 const VALUE original_fields_obj = fields_obj;
1746 fields_obj = rb_imemo_fields_clone(fields_obj);
1749 shape_id_t old_shape_id = RBASIC_SHAPE_ID(fields_obj);
1750 shape_id_t removed_shape_id;
1751 shape_id_t next_shape_id = rb_obj_shape_transition_remove_ivar(fields_obj,
id, &removed_shape_id);
1753 if (UNLIKELY(rb_shape_complex_p(next_shape_id))) {
1754 if (UNLIKELY(!rb_shape_complex_p(old_shape_id))) {
1755 fields_obj = imemo_fields_evacutate_to_complex(obj, fields_obj, next_shape_id, -1);
1758 if (!st_delete(rb_imemo_fields_complex_tbl(fields_obj), &key, (st_data_t *)&val)) {
1763 if (next_shape_id == old_shape_id) {
1767 RUBY_ASSERT(removed_shape_id != INVALID_SHAPE_ID);
1768 RUBY_ASSERT(RSHAPE_LEN(next_shape_id) == RSHAPE_LEN(old_shape_id) - 1);
1770 VALUE *fields = rb_imemo_fields_ptr(fields_obj);
1771 attr_index_t removed_index = RSHAPE_INDEX(removed_shape_id);
1772 val = fields[removed_index];
1774 attr_index_t new_fields_count = RSHAPE_LEN(next_shape_id);
1775 if (new_fields_count) {
1776 size_t trailing_fields = new_fields_count - removed_index;
1778 MEMMOVE(&fields[removed_index], &fields[removed_index + 1],
VALUE, trailing_fields);
1779 RBASIC_SET_SHAPE_ID(fields_obj, next_shape_id);
1781 if (
type ==
T_OBJECT && obj != fields_obj && new_fields_count == rb_shape_embedded_capacity(RBASIC_SHAPE_ID(obj))) {
1786 VALUE *embedded_fields = ROBJECT_EMBEDDED_FIELDS(obj);
1787 MEMCPY(embedded_fields, fields,
VALUE, new_fields_count);
1788 for (attr_index_t i = 0; i < new_fields_count; i++) {
1800 if (fields_obj != original_fields_obj) {
1803 if (fields_obj && fields_obj != obj) {
1804 ROBJECT_SET_EXTENDED(obj, fields_obj);
1809 RCLASS_WRITABLE_SET_FIELDS_OBJ(obj, fields_obj);
1812 rb_obj_set_fields(obj, fields_obj,
id, original_fields_obj);
1818 if (!fields_obj || fields_obj == obj) {
1819 RBASIC_SET_SHAPE_ID_WITH_LAYOUT(obj, next_shape_id, SHAPE_ID_LAYOUT_ROBJECT);
1822 RBASIC_SET_SHAPE_ID_WITH_LAYOUT(obj, next_shape_id, SHAPE_ID_LAYOUT_EXTENDED);
1826 RBASIC_SET_SHAPE_ID(obj, next_shape_id);
1833rb_attr_delete(
VALUE obj,
ID id)
1835 return rb_ivar_delete(obj,
id,
Qnil);
1839imemo_fields_complex_from_obj_i(
ID key,
VALUE val, st_data_t arg)
1842 st_table *table = rb_imemo_fields_complex_tbl(fields);
1844 RUBY_ASSERT(!st_lookup(table, (st_data_t)key, NULL));
1845 st_add_direct(table, (st_data_t)key, (st_data_t)val);
1852imemo_fields_shref_i(
ID key,
VALUE val, st_data_t arg)
1859 rb_gc_writebarrier(fields_obj, val);
1867rb_imemo_fields_record_shrefs(
VALUE fields_obj)
1869 rb_field_foreach(fields_obj, imemo_fields_shref_i, (st_data_t)fields_obj,
false);
1873imemo_fields_complex_from_obj(
VALUE owner,
VALUE source, shape_id_t shape_id,
bool ivar_only,
int extra_capa)
1875 attr_index_t
len = source ? RSHAPE_LEN(RBASIC_SHAPE_ID(source)) : 0;
1876 int capa = (
len + extra_capa);
1881 rb_field_foreach(source, imemo_fields_complex_from_obj_i, (st_data_t)fields_obj, ivar_only);
1887imemo_fields_evacutate_to_complex(
VALUE owner,
VALUE source, shape_id_t shape_id,
int extra_capa)
1889 return imemo_fields_complex_from_obj(owner, source, shape_id,
false, extra_capa);
1893rb_obj_complex_fields_build(
VALUE obj)
1895 return imemo_fields_complex_from_obj(obj, obj, ROOT_COMPLEX_SHAPE_ID,
true, 0);
1899imemo_fields_copy_append(
VALUE owner,
VALUE source_fields_obj, shape_id_t current_shape_id, shape_id_t target_shape_id,
VALUE val)
1901 attr_index_t fields_count = RSHAPE_LEN(current_shape_id);
1905 VALUE *fields = rb_imemo_fields_ptr(fields_obj);
1907 if (source_fields_obj) {
1908 MEMCPY(fields, rb_imemo_fields_ptr(source_fields_obj),
VALUE, fields_count);
1909 for (attr_index_t i = 0; i < fields_count; i++) {
1920imemo_fields_set(
VALUE owner,
VALUE fields_obj, shape_id_t target_shape_id,
ID field_name,
VALUE val,
bool concurrent)
1922 const VALUE original_fields_obj = fields_obj;
1923 shape_id_t current_shape_id = fields_obj ? RBASIC_SHAPE_ID(fields_obj) : ROOT_SHAPE_ID;
1925 if (UNLIKELY(rb_shape_complex_p(target_shape_id))) {
1926 if (rb_shape_complex_p(current_shape_id)) {
1931 fields_obj = rb_imemo_fields_clone(fields_obj);
1935 fields_obj = imemo_fields_evacutate_to_complex(owner, original_fields_obj, target_shape_id, 1);
1936 current_shape_id = target_shape_id;
1939 st_table *table = rb_imemo_fields_complex_tbl(fields_obj);
1942 st_insert(table, (st_data_t)field_name, (st_data_t)val);
1944 RBASIC_SET_SHAPE_ID(fields_obj, target_shape_id);
1947 attr_index_t index = RSHAPE_INDEX(target_shape_id);
1948 if (concurrent || index >= rb_shape_embedded_capacity(current_shape_id)) {
1949 return imemo_fields_copy_append(owner, original_fields_obj, current_shape_id, target_shape_id, val);
1952 VALUE *table = rb_imemo_fields_ptr(fields_obj);
1955 if (index >= RSHAPE_LEN(current_shape_id)) {
1956 RBASIC_SET_SHAPE_ID(fields_obj, target_shape_id);
1964generic_field_set(
VALUE obj, shape_id_t target_shape_id,
ID field_name,
VALUE val)
1967 field_name = RSHAPE_EDGE_NAME(target_shape_id);
1971 const VALUE original_fields_obj = rb_obj_fields(obj, field_name);
1974 VALUE fields_obj = imemo_fields_set(obj, original_fields_obj, target_shape_id, field_name, val, concurrent);
1976 rb_obj_set_fields(obj, fields_obj, field_name, original_fields_obj);
1977 return rb_shape_complex_p(target_shape_id) ? ATTR_INDEX_NOT_SET : RSHAPE_INDEX(target_shape_id);
1981generic_shape_ivar(
VALUE obj,
ID id,
bool *new_ivar_out)
1983 bool new_ivar =
false;
1984 shape_id_t current_shape_id = RBASIC_SHAPE_ID(obj);
1985 shape_id_t target_shape_id = current_shape_id;
1987 if (!rb_shape_complex_p(current_shape_id)) {
1988 if (!rb_shape_find_ivar(current_shape_id,
id, &target_shape_id)) {
1990 target_shape_id = rb_obj_shape_transition_add_ivar(obj,
id);
1994 *new_ivar_out = new_ivar;
1995 return target_shape_id;
2002 shape_id_t target_shape_id = generic_shape_ivar(obj,
id, &dontcare);
2003 return generic_field_set(obj, target_shape_id,
id, val);
2007obj_field_set(
VALUE obj, shape_id_t target_shape_id,
ID field_name,
VALUE val)
2010 VALUE fields_obj = ROBJECT_FIELDS_OBJ(obj);
2011 shape_id_t current_shape_id = RBASIC_SHAPE_ID(obj);
2013 if (UNLIKELY(rb_shape_complex_p(target_shape_id))) {
2014 if (UNLIKELY(!rb_shape_complex_p(current_shape_id))) {
2015 current_shape_id = rb_obj_convert_too_complex(obj, fields_obj, current_shape_id);
2016 fields_obj = ROBJECT_FIELDS_OBJ(obj);
2023 field_name = RSHAPE_EDGE_NAME(target_shape_id);
2027 st_insert(rb_imemo_fields_complex_tbl(fields_obj), (st_data_t)field_name, (st_data_t)val);
2030 RBASIC_SET_SHAPE_ID(obj, target_shape_id);
2031 if (obj != fields_obj) {
2032 RBASIC_SET_SHAPE_ID(fields_obj, target_shape_id);
2035 return ATTR_INDEX_NOT_SET;
2038 attr_index_t index = RSHAPE_INDEX(target_shape_id);
2040 if (index < RSHAPE_LEN(current_shape_id)) {
2042 RB_OBJ_WRITE(fields_obj, &rb_imemo_fields_ptr(fields_obj)[index], val);
2046 RUBY_ASSERT(index == RSHAPE_LEN(current_shape_id));
2048 if (UNLIKELY(index >= RSHAPE_CAPACITY(current_shape_id))) {
2049 fields_obj = imemo_fields_copy_append(obj, fields_obj, current_shape_id, target_shape_id, val);
2050 ROBJECT_SET_EXTENDED(obj, fields_obj);
2051 RBASIC_SET_FULL_SHAPE_ID(obj, rb_shape_transition_layout(target_shape_id, SHAPE_ID_LAYOUT_EXTENDED));
2054 RB_OBJ_WRITE(fields_obj, &rb_imemo_fields_ptr(fields_obj)[index], val);
2055 RBASIC_SET_SHAPE_ID(obj, target_shape_id);
2058 if (obj != fields_obj) {
2059 RBASIC_SET_SHAPE_ID(fields_obj, target_shape_id);
2070 shape_id_t target_shape_id = generic_shape_ivar(obj,
id, &dontcare);
2071 return obj_field_set(obj, target_shape_id,
id, val);
2075rb_check_ivar_modifiable(
VALUE obj)
2077 if (UNLIKELY(!
RB_FL_ABLE(obj) || rb_shape_frozen_p(RBASIC_SHAPE_ID(obj)))) {
2078 rb_check_frozen(obj);
2082 rb_raise(rb_eRactorIsolationError,
2083 "can't modify instance variables of a shareable %"PRIsVALUE,
2086 else if (UNLIKELY(CHILLED_STRING_P(obj))) {
2087 CHILLED_STRING_MUTATED(obj);
2100 rb_check_ivar_modifiable(obj);
2101 obj_ivar_set(obj,
id, val);
2115 shape_id_t shape_id = rb_obj_shape_transition_frozen(x);
2121 RBASIC_SET_SHAPE_ID(x, shape_id);
2124 RBASIC_SET_SHAPE_ID(x, shape_id);
2134static attr_index_t class_ivar_set(
VALUE obj,
ID id,
VALUE val,
bool *new_ivar);
2139 RB_DEBUG_COUNTER_INC(ivar_set_base);
2143 return obj_ivar_set(obj,
id, val);
2147 class_ivar_set_ractor_check(obj,
id);
2149 return class_ivar_set(obj,
id, val, &dontcare);
2152 return generic_ivar_set(obj,
id, val);
2159 rb_check_ivar_modifiable(obj);
2160 ivar_set(obj,
id, val);
2167 return ivar_set(obj,
id, val);
2176 ivar_set(obj,
id, val);
2180rb_obj_field_set(
VALUE obj, shape_id_t target_shape_id,
ID field_name,
VALUE val)
2184 return obj_field_set(obj, target_shape_id, field_name, val);
2188 rb_bug(
"Unreachable");
2191 return generic_field_set(obj, target_shape_id, field_name, val);
2196ivar_defined0(
VALUE obj,
ID id)
2198 if (rb_obj_shape_complex_p(obj)) {
2200 VALUE fields_obj = rb_obj_fields_no_ractor_check(obj);
2201 st_table *table = rb_imemo_fields_complex_tbl(fields_obj);
2204 if (!table || !rb_st_lookup(table,
id, &idx)) {
2212 return RBOOL(rb_shape_get_iv_index(RBASIC_SHAPE_ID(obj),
id, &index));
2226 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
2228 defined = ivar_defined0(fields_obj,
id);
2233 defined = ivar_defined0(obj,
id);
2241 struct gen_fields_tbl *fields_tbl;
2243 rb_ivar_foreach_callback_func *func;
2245 shape_id_t shape_id;
2250iterate_over_shapes_callback(shape_id_t shape_id,
void *data)
2254 if (itr_data->ivar_only && !RSHAPE_TYPE_P(shape_id, SHAPE_IVAR)) {
2261 RUBY_ASSERT(!rb_obj_shape_complex_p(itr_data->obj));
2262 fields = ROBJECT_FIELDS(itr_data->obj);
2265 RUBY_ASSERT(IMEMO_TYPE_P(itr_data->obj, imemo_fields));
2266 RUBY_ASSERT(!rb_obj_shape_complex_p(itr_data->obj));
2268 fields = rb_imemo_fields_ptr(itr_data->obj);
2271 rb_bug(
"Unreachable");
2274 RUBY_ASSERT(itr_data->shape_id == RBASIC_SHAPE_ID(itr_data->obj));
2276 VALUE val = fields[RSHAPE_INDEX(shape_id)];
2277 int ret = itr_data->func(RSHAPE_EDGE_NAME(shape_id), val, itr_data->arg);
2279 RUBY_ASSERT(itr_data->shape_id == RBASIC_SHAPE_ID(itr_data->obj));
2288iterate_over_shapes(shape_id_t shape_id, rb_ivar_foreach_callback_func *callback,
struct iv_itr_data *itr_data)
2290 rb_shape_foreach_field(shape_id, iterate_over_shapes_callback, itr_data);
2294each_hash_iv(st_data_t
id, st_data_t val, st_data_t data)
2297 rb_ivar_foreach_callback_func *callback = itr_data->func;
2298 if ((
ID)
id == rb_shape_tree.id_object_id) {
2301 return callback((
ID)
id, (
VALUE)val, itr_data->arg);
2305obj_fields_each(
VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg,
bool ivar_only)
2311 .ivar_only = ivar_only,
2314 VALUE fields_obj = ROBJECT_FIELDS_OBJ(obj);
2315 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
2317 if (rb_shape_complex_p(shape_id)) {
2318 st_foreach_safe(rb_imemo_fields_complex_tbl(fields_obj), each_hash_iv, (st_data_t)&itr_data);
2321 itr_data.fields = rb_imemo_fields_ptr(fields_obj);
2322 itr_data.shape_id = shape_id;
2323 iterate_over_shapes(shape_id, func, &itr_data);
2328imemo_fields_each(
VALUE fields_obj, rb_ivar_foreach_callback_func *func, st_data_t arg,
bool ivar_only)
2330 IMEMO_TYPE_P(fields_obj, imemo_fields);
2336 .ivar_only = ivar_only,
2339 shape_id_t shape_id = RBASIC_SHAPE_ID(fields_obj);
2340 if (rb_shape_complex_p(shape_id)) {
2341 rb_st_foreach(rb_imemo_fields_complex_tbl(fields_obj), each_hash_iv, (st_data_t)&itr_data);
2344 itr_data.fields = rb_imemo_fields_ptr(fields_obj);
2345 itr_data.shape_id = shape_id;
2346 iterate_over_shapes(shape_id, func, &itr_data);
2353 VALUE new_fields_obj;
2355 rb_check_frozen(dest);
2357 if (!rb_obj_gen_fields_p(obj)) {
2361 shape_id_t src_shape_id = rb_obj_shape_id(obj);
2363 VALUE fields_obj = rb_obj_fields_no_ractor_check(obj);
2371 shape_id_t initial_shape_id = rb_obj_shape_id(dest);
2372 shape_id_t dest_shape_id = rb_shape_rebuild(initial_shape_id, src_shape_id);
2374 if (UNLIKELY(rb_shape_complex_p(dest_shape_id))) {
2375 rb_obj_replace_fields(dest, rb_obj_complex_fields_build(obj));
2379 if (!RSHAPE_LEN(dest_shape_id)) {
2380 RBASIC_SET_SHAPE_ID(dest, dest_shape_id);
2385 VALUE *src_buf = rb_imemo_fields_ptr(fields_obj);
2386 VALUE *dest_buf = rb_imemo_fields_ptr(new_fields_obj);
2387 rb_shape_copy_fields(new_fields_obj, dest_buf, dest_shape_id, src_buf, src_shape_id);
2389 rb_obj_replace_fields(dest, new_fields_obj);
2397rb_generic_fields_shared_table_foreach(
void (*cb)(
struct st_table *tbl,
void *arg),
void *arg)
2400 if (generic_fields_tbl_ != NULL) {
2401 cb(generic_fields_tbl_, arg);
2410rb_generic_fields_tables_foreach(
void (*cb)(
struct st_table *tbl,
void *arg),
void *arg)
2412 if (generic_fields_tbl_ != NULL) {
2413 cb(generic_fields_tbl_, arg);
2418rb_field_foreach(
VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg,
bool ivar_only)
2423 if (IMEMO_TYPE_P(obj, imemo_fields)) {
2424 imemo_fields_each(obj, func, arg, ivar_only);
2428 obj_fields_each(obj, func, arg, ivar_only);
2435 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
2437 imemo_fields_each(fields_obj, func, arg, ivar_only);
2443 VALUE fields_obj = rb_obj_fields_no_ractor_check(obj);
2445 imemo_fields_each(fields_obj, func, arg, ivar_only);
2458collect_ivar_i(
ID id,
VALUE val, st_data_t arg)
2470 rb_field_foreach(obj, func, arg,
true);
2474rb_ivar_foreach_buffered(
VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg)
2477 if (count == 0)
return;
2483 rb_field_foreach(obj, collect_ivar_i, (st_data_t)&pos,
true);
2486 for (st_index_t i = 0; i < count; i++) {
2487 if (func(buf[i].name, buf[i].val, arg) == ST_STOP)
break;
2498 st_index_t iv_count = 0;
2499 VALUE fields_obj = rb_obj_fields_no_ractor_check(obj);
2502 if (rb_obj_shape_complex_p(fields_obj)) {
2503 iv_count = rb_st_table_size(rb_imemo_fields_complex_tbl(fields_obj));
2506 iv_count = RBASIC_FIELDS_COUNT(obj);
2510 if (rb_obj_shape_has_id(obj)) {
2518ivar_i(
ID key,
VALUE v, st_data_t a)
2553#define rb_is_constant_id rb_is_const_id
2554#define rb_is_constant_name rb_is_const_name
2555#define id_for_var(obj, name, part, type) \
2556 id_for_var_message(obj, name, type, "'%1$s' is not allowed as "#part" "#type" variable name")
2557#define id_for_var_message(obj, name, type, message) \
2558 check_id_type(obj, &(name), rb_is_##type##_id, rb_is_##type##_name, message, strlen(message))
2561 int (*valid_id_p)(
ID),
int (*valid_name_p)(
VALUE),
2562 const char *message,
size_t message_len)
2565 VALUE name = *pname;
2567 if (
id ? !valid_id_p(
id) : !valid_name_p(name)) {
2568 rb_name_err_raise_str(rb_fstring_new(message, message_len),
2600 const ID id = id_for_var(obj, name, an, instance);
2604 rb_check_frozen(obj);
2609 if (!UNDEF_P(val))
return val;
2612 rb_name_err_raise(
"instance variable %1$s not defined",
2617NORETURN(
static void uninitialized_constant(
VALUE,
VALUE));
2619uninitialized_constant(
VALUE klass,
VALUE name)
2622 rb_name_err_raise(
"uninitialized constant %2$s::%1$s",
2625 rb_name_err_raise(
"uninitialized constant %1$s",
2633 rb_vm_inc_const_missing_count();
2682 VALUE ref = ec->private_const_reference;
2683 rb_vm_pop_cfunc_frame();
2685 ec->private_const_reference = 0;
2686 rb_name_err_raise(
"private constant %2$s::%1$s referenced", ref, name);
2688 uninitialized_constant(klass, name);
2696 struct ccan_list_node cnode;
2699 VALUE autoload_data_value;
2709 VALUE autoload_call_file;
2718 rb_const_flag_t flag;
2734 rb_serial_t fork_gen;
2737 struct ccan_list_head constants;
2741autoload_data_mark_and_move(
void *ptr)
2745 rb_gc_mark_and_move(&p->feature);
2746 rb_gc_mark_and_move(&p->mutex);
2750autoload_data_free(
void *ptr)
2755 ccan_list_for_each_safe(&p->constants,
autoload_const, next, cnode) {
2763autoload_data_memsize(
const void *ptr)
2770 {autoload_data_mark_and_move, autoload_data_free, autoload_data_memsize, autoload_data_mark_and_move},
2775autoload_const_mark_and_move(
void *ptr)
2779 rb_gc_mark_and_move(&ac->module);
2780 rb_gc_mark_and_move(&ac->autoload_call_file);
2781 rb_gc_mark_and_move(&ac->autoload_data_value);
2782 rb_gc_mark_and_move(&ac->value);
2783 rb_gc_mark_and_move(&ac->file);
2784 rb_gc_mark_and_move(&ac->box_value);
2788autoload_const_memsize(
const void *ptr)
2794autoload_const_free(
void *ptr)
2804 {autoload_const_mark_and_move, autoload_const_free, autoload_const_memsize, autoload_const_mark_and_move,},
2809get_autoload_data(
VALUE autoload_const_value,
struct autoload_const **autoload_const_pointer)
2822 if (autoload_const_pointer) *autoload_const_pointer =
autoload_const;
2829autoload_entry(
VALUE mod,
ID id)
2832 if (!ce || !UNDEF_P(ce->value))
return 0;
2833 VM_ASSERT(rb_typeddata_is_kind_of(ce->u.autoload_const, &autoload_const_type));
2834 return ce->u.autoload_const;
2839static void const_added(
VALUE klass,
ID const_name);
2849autoload_feature_lookup_or_create(
VALUE feature,
struct autoload_data **autoload_data_pointer)
2851 RUBY_ASSERT_MUTEX_OWNED(autoload_mutex);
2852 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
2854 VALUE autoload_data_value = rb_hash_aref(autoload_features, feature);
2857 if (
NIL_P(autoload_data_value)) {
2863 if (autoload_data_pointer) *autoload_data_pointer =
autoload_data;
2865 rb_hash_aset(autoload_features, feature, autoload_data_value);
2867 else if (autoload_data_pointer) {
2868 *autoload_data_pointer =
rb_check_typeddata(autoload_data_value, &autoload_data_type);
2871 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
2872 return autoload_data_value;
2876autoload_synchronized(
VALUE _arguments)
2880 rb_const_entry_t *constant_entry = rb_const_lookup(arguments->module, arguments->name);
2881 if (constant_entry && !UNDEF_P(constant_entry->value)) {
2886 const_set(arguments->module, arguments->name,
Qundef);
2887 constant_entry = rb_const_lookup(arguments->module, arguments->name);
2890 VALUE feature = rb_fstring(arguments->feature);
2893 VALUE autoload_data_value = autoload_feature_lookup_or_create(feature, &
autoload_data);
2906 RB_OBJ_WRITE(arguments->module, &constant_entry->u.autoload_const, autoload_const_value);
2915 const rb_box_t *box = rb_current_box();
2916 VALUE current_box_value = rb_get_box_object((
rb_box_t *)box);
2919 rb_raise(
rb_eNameError,
"autoload must be constant name: %"PRIsVALUE
"", QUOTE_ID(name));
2922 rb_class_owner_check(module);
2925 if (!RSTRING_LEN(feature)) {
2926 rb_raise(rb_eArgError,
"empty feature name");
2933 .box_value = current_box_value,
2938 if (result ==
Qtrue) {
2939 const_added(module, name);
2944autoload_delete(
VALUE autoload_const_value)
2946 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
2948 VM_ASSERT(rb_typeddata_is_kind_of(autoload_const_value, &autoload_const_type));
2966 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
2976check_autoload_required(
VALUE mod,
ID id,
const char **loadingpath)
2978 VALUE autoload_const_value = autoload_entry(mod,
id);
2980 const char *loading;
2982 if (!autoload_const_value || !(
autoload_data = get_autoload_data(autoload_const_value, 0))) {
2995 return autoload_const_value;
2998 loading = RSTRING_PTR(feature);
3001 return autoload_const_value;
3004 if (loadingpath && loading) {
3005 *loadingpath = loading;
3006 return autoload_const_value;
3015rb_autoloading_value(
VALUE mod,
ID id,
VALUE* value, rb_const_flag_t *flag)
3018 if (!ac)
return FALSE;
3034 return ele->mutex !=
Qnil && rb_mutex_owned_p(ele->mutex);
3041autoloading_const_entry(
VALUE mod,
ID id)
3043 VALUE load = autoload_entry(mod,
id);
3048 if (!load || !(ele = get_autoload_data(load, &ac))) {
3054 if (autoload_by_current(ele)) {
3055 if (!UNDEF_P(ac->value)) {
3064autoload_defined_p(
VALUE mod,
ID id)
3069 if (!ce || !UNDEF_P(ce->value)) {
3075 return !rb_autoloading_value(mod,
id, NULL, NULL);
3094 if (!warning->id)
return;
3096 VALUE name = QUOTE_ID(warning->id);
3099 if (!
NIL_P(warning->file) && warning->line) {
3100 previous = rb_sprintf(
"\n%"PRIsVALUE
":%d: warning: previous definition of %"PRIsVALUE
" was here",
3101 warning->file, warning->line, name);
3105 rb_warn(
"already initialized constant %"PRIsVALUE
"%"PRIsVALUE
"", name, previous);
3108 rb_warn(
"already initialized constant %"PRIsVALUE
"::%"PRIsVALUE
"%"PRIsVALUE
"",
3130 check_before_mod_set(ac->module, ac->name, ac->value,
"constant");
3134 const_tbl_update(ac,
true, &warning);
3136 const_warning_emit(&warning);
3142autoload_load_needed(
VALUE _arguments)
3146 const char *loading = 0, *src;
3148 if (!autoload_defined_p(arguments->module, arguments->name)) {
3152 VALUE autoload_const_value = check_autoload_required(arguments->module, arguments->name, &loading);
3153 if (!autoload_const_value) {
3158 if (src && loading && strcmp(src, loading) == 0) {
3179 return autoload_const_value;
3183autoload_apply_constants(
VALUE _arguments)
3185 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
3197 ccan_list_for_each_safe(&arguments->autoload_data->constants,
autoload_const, next, cnode) {
3203 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
3210autoload_require_and_apply(
VALUE receiver,
VALUE _arguments)
3214 VALUE result =
rb_funcall(receiver, rb_intern(
"require"), 1, arguments->autoload_data->feature);
3215 if (
RTEST(result)) {
3222autoload_feature_require_in_box(
VALUE receiver,
VALUE _arguments)
3224 rb_vm_frame_flag_set_box_require(GET_EC());
3226 return autoload_require_and_apply(receiver, _arguments);
3230autoload_feature_require(
VALUE _arguments)
3246 rb_gccct_clear_table();
3248 VALUE feature = arguments->autoload_data->feature;
3250 if (rb_box_available() && BOX_OBJ_P(autoload_box_value)) {
3251 box = rb_get_box_t(autoload_box_value);
3254 if (box && box->top_self) {
3260 return rb_vm_call_cfunc_in_box(box->top_self, autoload_feature_require_in_box,
3261 box->top_self, _arguments, feature, box);
3263 return autoload_require_and_apply(rb_vm_top_self(), _arguments);
3272 if (rb_box_available() && BOX_OBJ_P(ac->box_value)) {
3273 const rb_box_t *box = rb_get_box_t(ac->box_value);
3274 if (box->top_self)
return box;
3276 return rb_current_box();
3283 return const_lookup(RCLASSEXT_CONST_TBL(RCLASS_EXT_READABLE_IN_BOX(mod, box)),
id);
3287autoload_try_load(
VALUE _arguments)
3292 VALUE result = autoload_feature_require(_arguments);
3295 rb_const_entry_t *ce = rb_const_lookup(arguments->module, arguments->name);
3300 if (ce && UNDEF_P(ce->value)) {
3301 rb_const_entry_t *loaded = const_lookup_in_box(arguments->module, arguments->name,
3302 autoload_box(arguments->autoload_const));
3303 if (loaded && !UNDEF_P(loaded->value)) {
3305 ce->flag = loaded->flag;
3306 RB_OBJ_WRITE(arguments->module, &ce->value, loaded->value);
3307 RB_OBJ_WRITE(arguments->module, &ce->u.file, loaded->u.file);
3308 ce->line = loaded->line;
3315 if (!ce || UNDEF_P(ce->value)) {
3322 "Expected %"PRIsVALUE
" to define %"PRIsVALUE
" but it didn't",
3323 arguments->autoload_data->feature,
3329 "Expected %"PRIsVALUE
" to define %"PRIsVALUE
"::%"PRIsVALUE
" but it didn't",
3330 arguments->autoload_data->feature,
3338 ce->flag |= arguments->flag;
3350 if (!ce || !UNDEF_P(ce->value)) {
3355 if (UNLIKELY(!rb_ractor_main_p())) {
3356 return rb_ractor_autoload_load(module, name);
3366 if (autoload_const_value ==
Qfalse)
return autoload_const_value;
3368 arguments.flag = ce->flag & (CONST_DEPRECATED | CONST_VISIBILITY_MASK);
3385 return rb_autoload_at_p(mod,
id, TRUE);
3389rb_autoload_at_p(
VALUE mod,
ID id,
int recur)
3394 while (!autoload_defined_p(mod,
id)) {
3395 if (!recur)
return Qnil;
3397 if (!mod)
return Qnil;
3399 load = check_autoload_required(mod,
id, 0);
3400 if (!load)
return Qnil;
3401 return (ele = get_autoload_data(load, 0)) ? ele->feature :
Qnil;
3407 if (RB_CONST_DEPRECATED_P(ce) &&
3420rb_const_get_0(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility)
3423 VALUE c = rb_const_search(klass,
id, exclude, recurse, visibility, &found_in);
3425 if (UNLIKELY(!rb_class_owned_p(found_in))) {
3427 rb_raise(rb_eRactorIsolationError,
"can not access non-shareable objects in constant %"PRIsVALUE
"::%"PRIsVALUE
" of a class/module created by another Ractor.",
rb_class_path(found_in), rb_id2str(
id));
3432 return rb_const_missing(klass,
ID2SYM(
id));
3436rb_const_search_from(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility,
VALUE *found_in)
3438 VALUE value, current;
3439 bool first_iteration =
true;
3441 for (current = klass;
3443 current =
RCLASS_SUPER(current), first_iteration =
false) {
3448 if (!first_iteration && RCLASS_ORIGIN(current) != current) {
3461 while (rb_const_lookup_copy(tmp,
id, ce)) {
3462 if (visibility && RB_CONST_PRIVATE_P(ce)) {
3463 GET_EC()->private_const_reference = tmp;
3466 rb_const_warn_if_deprecated(ce, tmp,
id);
3468 if (UNDEF_P(value)) {
3470 if (am == tmp)
break;
3472 ac = autoloading_const_entry(tmp,
id);
3474 if (found_in) { *found_in = tmp; }
3483 if (found_in) { *found_in = tmp; }
3486 if (!recurse)
break;
3490 GET_EC()->private_const_reference = 0;
3495rb_const_search(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility,
VALUE *found_in)
3500 value = rb_const_search_from(klass,
id, exclude, recurse, visibility, found_in);
3501 if (!UNDEF_P(value))
return value;
3502 if (exclude)
return value;
3505 return rb_const_search_from(
rb_cObject,
id, FALSE, recurse, visibility, found_in);
3511 return rb_const_get_0(klass,
id, TRUE, TRUE, FALSE);
3517 return rb_const_get_0(klass,
id, FALSE, TRUE, FALSE);
3523 return rb_const_get_0(klass,
id, TRUE, FALSE, FALSE);
3527rb_public_const_get_from(
VALUE klass,
ID id)
3529 return rb_const_get_0(klass,
id, TRUE, TRUE, TRUE);
3533rb_public_const_get_at(
VALUE klass,
ID id)
3535 return rb_const_get_0(klass,
id, TRUE, FALSE, TRUE);
3538NORETURN(
static void undefined_constant(
VALUE mod,
VALUE name));
3542 rb_name_err_raise(
"constant %2$s::%1$s not defined",
3547rb_const_location_from(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility)
3549 while (
RTEST(klass)) {
3552 while (rb_const_lookup_copy(klass,
id, ce)) {
3553 if (visibility && RB_CONST_PRIVATE_P(ce)) {
3560 VALUE file = ce->u.file;
3561 VALUE autoload_const_value = autoload_entry(klass,
id);
3562 if (autoload_const_value) {
3575 if (!recurse)
break;
3584rb_const_location(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility)
3589 loc = rb_const_location_from(klass,
id, exclude, recurse, visibility);
3590 if (!
NIL_P(loc))
return loc;
3591 if (exclude)
return loc;
3594 return rb_const_location_from(
rb_cObject,
id, FALSE, recurse, visibility);
3598rb_const_source_location(
VALUE klass,
ID id)
3600 return rb_const_location(klass,
id, FALSE, TRUE, FALSE);
3604rb_const_source_location_at(
VALUE klass,
ID id)
3606 return rb_const_location(klass,
id, TRUE, FALSE, FALSE);
3622 const ID id = id_for_var(mod, name, a, constant);
3625 undefined_constant(mod, name);
3638 rb_check_frozen(mod);
3639 rb_class_owner_check(mod);
3641 ce = rb_const_lookup(mod,
id);
3645 rb_name_err_raise(
"cannot remove %2$s::%1$s", mod,
ID2SYM(
id));
3648 undefined_constant(mod,
ID2SYM(
id));
3651 VALUE writable_ce = 0;
3654 found = rb_id_table_lookup(RCLASS_WRITABLE_CONST_TBL(mod),
id, &writable_ce);
3655 if (found) rb_id_table_delete(RCLASS_WRITABLE_CONST_TBL(mod),
id);
3661 rb_const_warn_if_deprecated(ce, mod,
id);
3667 autoload_delete(ce->u.autoload_const);
3671 if (ce != const_lookup(RCLASS_PRIME_CONST_TBL(mod),
id)) {
3680cv_i_update(st_data_t *k, st_data_t *v, st_data_t a,
int existing)
3682 if (existing)
return ST_STOP;
3687static enum rb_id_table_iterator_result
3694 st_update(tbl, (st_data_t)key, cv_i_update, (st_data_t)ce);
3696 return ID_TABLE_CONTINUE;
3699static enum rb_id_table_iterator_result
3700rb_local_constants_i(
ID const_name,
VALUE const_value,
void *ary)
3705 return ID_TABLE_CONTINUE;
3709rb_local_constants(
VALUE mod)
3718 rb_id_table_foreach(tbl, rb_local_constants_i, (
void *)ary);
3729 tbl = st_init_numtable();
3731 if (RCLASS_CONST_TBL(mod)) {
3733 rb_id_table_foreach(RCLASS_CONST_TBL(mod), sv_i, tbl);
3753list_i(st_data_t key, st_data_t value,
VALUE ary)
3796 bool inherit =
true;
3804 return rb_local_constants(mod);
3809rb_const_defined_0(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility)
3818 if (rb_const_lookup_copy(tmp,
id, ce)) {
3819 if (visibility && RB_CONST_PRIVATE_P(ce)) {
3822 if (UNDEF_P(ce->value) && !check_autoload_required(tmp,
id, 0) &&
3823 !rb_autoloading_value(tmp,
id, NULL, NULL))
3832 if (!recurse)
break;
3846 return rb_const_defined_0(klass,
id, TRUE, TRUE, FALSE);
3852 return rb_const_defined_0(klass,
id, FALSE, TRUE, FALSE);
3858 return rb_const_defined_0(klass,
id, TRUE, FALSE, FALSE);
3862rb_public_const_defined_from(
VALUE klass,
ID id)
3864 return rb_const_defined_0(klass,
id, TRUE, TRUE, TRUE);
3868check_before_mod_set(
VALUE klass,
ID id,
VALUE val,
const char *dest)
3870 rb_check_frozen(klass);
3873static void set_namespace_path(
VALUE named_namespace,
VALUE name);
3875static enum rb_id_table_iterator_result
3876set_namespace_path_i(
ID id,
VALUE v,
void *payload)
3879 VALUE value = ce->value;
3882 return ID_TABLE_CONTINUE;
3885 bool has_permanent_classpath;
3886 classname(value, &has_permanent_classpath);
3887 if (has_permanent_classpath) {
3888 return ID_TABLE_CONTINUE;
3890 set_namespace_path(value, build_const_path(parental_path,
id));
3892 if (!RCLASS_PERMANENT_CLASSPATH_P(value)) {
3893 RCLASS_WRITE_CLASSPATH(value, 0,
false);
3896 return ID_TABLE_CONTINUE;
3905set_namespace_path(
VALUE named_namespace,
VALUE namespace_path)
3907 struct rb_id_table *const_table = RCLASS_CONST_TBL(named_namespace);
3911 RCLASS_WRITE_CLASSPATH(named_namespace, namespace_path,
true);
3914 rb_id_table_foreach(const_table, set_namespace_path_i, &namespace_path);
3920const_added(
VALUE klass,
ID const_name)
3922 if (GET_VM()->running) {
3924 rb_funcallv_uncached(klass, idConst_added, 1, &arg);
3934 rb_raise(
rb_eTypeError,
"no class/module to define constant %"PRIsVALUE
"",
3938 if (UNLIKELY(!rb_class_owned_p(klass))) {
3939 rb_raise(rb_eRactorIsolationError,
"can not set constants of classes/modules created by another Ractor");
3942 check_before_mod_set(klass,
id, val,
"constant");
3946 struct rb_id_table *tbl = RCLASS_WRITABLE_CONST_TBL(klass);
3948 tbl = rb_id_table_create(0);
3949 RCLASS_WRITE_CONST_TBL(klass, tbl,
false);
3952 rb_id_table_insert(tbl,
id, (
VALUE)ce);
3953 setup_const_entry(ce, klass, val, CONST_PUBLIC);
3957 .module = klass, .name = id,
3958 .value = val, .flag = CONST_PUBLIC,
3961 ac.file = rb_source_location(&ac.line);
3962 const_tbl_update(&ac,
false, &warning);
3965 const_warning_emit(&warning);
3972 bool val_path_permanent;
3973 VALUE val_path = classname(val, &val_path_permanent);
3974 if (
NIL_P(val_path) || !val_path_permanent) {
3976 set_namespace_path(val, rb_id2str(
id));
3979 bool parental_path_permanent;
3980 VALUE parental_path = classname(klass, &parental_path_permanent);
3981 if (
NIL_P(parental_path)) {
3983 parental_path = rb_tmp_class_path(klass, &throwaway, make_temporary_path);
3985 if (parental_path_permanent && !val_path_permanent) {
3986 set_namespace_path(val, build_const_path(parental_path,
id));
3988 else if (!parental_path_permanent &&
NIL_P(val_path)) {
3989 VALUE path = build_const_path(parental_path,
id);
3990 RCLASS_SET_CLASSPATH(val, path,
false);
4000 const_set(klass,
id, val);
4001 const_added(klass,
id);
4005autoload_const_value_for_named_constant(
VALUE module,
ID name,
struct autoload_const **autoload_const_pointer)
4007 VALUE autoload_const_value = autoload_entry(module, name);
4008 if (!autoload_const_value)
return Qfalse;
4015 return autoload_const_value;
4025 ASSERT_vm_locking();
4027 VALUE klass = ac->module;
4028 VALUE val = ac->value;
4030 struct rb_id_table *tbl = RCLASS_CONST_TBL(klass);
4031 rb_const_flag_t visibility = ac->flag;
4034 if (rb_id_table_lookup(tbl,
id, &value)) {
4036 if (UNDEF_P(ce->value)) {
4037 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
4038 VALUE file = ac->file;
4039 int line = ac->line;
4040 VALUE autoload_const_value = autoload_const_value_for_named_constant(klass,
id, &ac);
4042 if (!autoload_force && autoload_const_value) {
4046 RB_OBJ_WRITE(autoload_const_value, &ac->file, rb_source_location(&ac->line));
4050 autoload_delete(ce->u.autoload_const);
4051 ce->flag = visibility;
4056 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
4060 visibility = ce->flag;
4063 warning->module = klass;
4064 warning->file = ce->u.file;
4065 warning->line = ce->line;
4068 setup_const_entry(ce, klass, val, visibility);
4071 tbl = RCLASS_WRITABLE_CONST_TBL(klass);
4075 rb_id_table_insert(tbl,
id, (
VALUE)ce);
4076 setup_const_entry(ce, klass, val, visibility);
4082 rb_const_flag_t visibility)
4084 ce->flag = visibility;
4086 RB_OBJ_WRITE(klass, &ce->u.file, rb_source_location(&ce->line));
4092 ID id = rb_intern(name);
4095 rb_warn(
"rb_define_const: invalid name '%s' for constant", name);
4098 rb_vm_register_global_object(val);
4110set_const_visibility(
VALUE mod,
int argc,
const VALUE *argv,
4111 rb_const_flag_t flag, rb_const_flag_t mask)
4119 rb_warning(
"%"PRIsVALUE
" with no argument is just ignored",
4120 QUOTE_ID(rb_frame_callee()));
4124 for (i = 0; i < argc; i++) {
4126 VALUE val = argv[i];
4129 undefined_constant(mod, val);
4131 if ((ce = rb_const_lookup(mod,
id))) {
4134 if (UNDEF_P(ce->value)) {
4135 if (autoload_const_value_for_named_constant(mod,
id, &ac)) {
4143 undefined_constant(mod,
ID2SYM(
id));
4153 long len = strlen(name);
4157 undefined_constant(mod, rb_fstring_new(name,
len));
4159 if (!(ce = rb_const_lookup(mod,
id))) {
4160 undefined_constant(mod,
ID2SYM(
id));
4162 ce->flag |= CONST_DEPRECATED;
4173rb_mod_private_constant(
int argc,
const VALUE *argv,
VALUE obj)
4175 set_const_visibility(obj, argc, argv, CONST_PRIVATE, CONST_VISIBILITY_MASK);
4187rb_mod_public_constant(
int argc,
const VALUE *argv,
VALUE obj)
4189 set_const_visibility(obj, argc, argv, CONST_PUBLIC, CONST_VISIBILITY_MASK);
4213rb_mod_deprecate_constant(
int argc,
const VALUE *argv,
VALUE obj)
4215 set_const_visibility(obj, argc, argv, CONST_DEPRECATED, CONST_DEPRECATED);
4220original_module(
VALUE c)
4228cvar_lookup_at(
VALUE klass,
ID id, st_data_t *v)
4231 if (RICLASS_IS_ORIGIN_P(klass)) {
4236 klass =
RBASIC(klass)->klass;
4241 if (UNDEF_P(n))
return 0;
4248cvar_front_klass(
VALUE klass)
4250 if (RCLASS_SINGLETON_P(klass)) {
4251 VALUE obj = RCLASS_ATTACHED_OBJECT(klass);
4252 if (rb_namespace_p(obj)) {
4262 if (front && target != front) {
4263 if (original_module(front) != original_module(target)) {
4265 "class variable % "PRIsVALUE
" of %"PRIsVALUE
" is overtaken by %"PRIsVALUE
"",
4271 rb_ivar_delete(front,
id,
Qundef);
4276#define CVAR_FOREACH_ANCESTORS(klass, v, r) \
4277 for (klass = cvar_front_klass(klass); klass; klass = RCLASS_SUPER(klass)) { \
4278 if (cvar_lookup_at(klass, id, (v))) { \
4283#define CVAR_LOOKUP(v,r) do {\
4284 if (cvar_lookup_at(klass, id, (v))) {r;}\
4285 CVAR_FOREACH_ANCESTORS(klass, v, r);\
4305 VALUE tmp, front = 0, target = 0;
4308 CVAR_LOOKUP(0, {
if (!front) front = klass; target = klass;});
4310 cvar_overtaken(front, target,
id);
4317 target =
RBASIC(target)->klass;
4319 cvar_set_ractor_check(target,
id);
4320 check_before_mod_set(target,
id, val,
"class variable");
4322 bool new_cvar = rb_class_ivar_set(target,
id, val);
4324 VALUE cvc_tbl = RCLASS_WRITABLE_CVC_TBL(target);
4329 if (!cvc_tbl || !rb_marked_id_table_lookup(cvc_tbl,
id, &ent_data)) {
4333 ent->global_cvar_state = GET_GLOBAL_CVAR_STATE();
4335 VALUE new_cvc_tbl = cvc_tbl;
4337 new_cvc_tbl = rb_marked_id_table_new(2);
4339 else if (rb_multi_ractor_p()) {
4340 new_cvc_tbl = rb_marked_id_table_dup(cvc_tbl);
4343 rb_marked_id_table_insert(new_cvc_tbl,
id, (
VALUE)ent);
4344 if (new_cvc_tbl != cvc_tbl) {
4345 RCLASS_WRITE_CVC_TBL(target, new_cvc_tbl);
4347 RB_DEBUG_COUNTER_INC(cvar_inline_miss);
4350 ent = (
void *)ent_data;
4351 ent->global_cvar_state = GET_GLOBAL_CVAR_STATE();
4358 ruby_vm_global_cvar_state++;
4368 value = find_cvar(klass, front, &target,
id);
4370 rb_name_err_raise(
"uninitialized class variable %1$s in %2$s",
4373 cvar_overtaken(*front, target,
id);
4375 target =
RBASIC(target)->klass;
4377 cvar_read_ractor_check(target,
id, value);
4378 return (
VALUE)value;
4391 if (!klass)
return Qfalse;
4392 CVAR_LOOKUP(0,
return Qtrue);
4397cv_intern(
VALUE klass,
const char *name)
4399 ID id = rb_intern(name);
4401 rb_name_err_raise(
"wrong class variable name %1$s",
4410 ID id = cv_intern(klass, name);
4417 ID id = cv_intern(klass, name);
4428cv_i(
ID key,
VALUE v, st_data_t a)
4433 st_update(tbl, (st_data_t)key, cv_i_update, 0);
4439mod_cvar_at(
VALUE mod,
void *data)
4443 tbl = st_init_numtable();
4445 mod = original_module(mod);
4452mod_cvar_of(
VALUE mod,
void *data)
4455 if (RCLASS_SINGLETON_P(mod)) {
4456 if (rb_namespace_p(RCLASS_ATTACHED_OBJECT(mod))) {
4457 data = mod_cvar_at(tmp, data);
4458 tmp = cvar_front_klass(tmp);
4462 data = mod_cvar_at(tmp, data);
4470cv_list_i(st_data_t key, st_data_t value,
VALUE ary)
4478cvar_list(
void *data)
4514 bool inherit =
true;
4519 tbl = mod_cvar_of(mod, 0);
4522 tbl = mod_cvar_at(mod, 0);
4524 return cvar_list(tbl);
4549 const ID id = id_for_var_message(mod, name,
class,
"wrong class variable name %1$s");
4555 rb_check_frozen(mod);
4556 cvar_set_ractor_check(mod,
id);
4557 val = rb_ivar_delete(mod,
id,
Qundef);
4558 if (!UNDEF_P(val)) {
4562 rb_name_err_raise(
"cannot remove %1$s for %2$s", mod,
ID2SYM(
id));
4565 rb_name_err_raise(
"class variable %1$s not defined for %2$s",
4584 ID id = rb_intern(name);
4590class_fields_ivar_set(
VALUE klass,
VALUE fields_obj,
ID id,
VALUE val,
bool concurrent,
VALUE *new_fields_obj,
bool *new_ivar_out)
4592 const VALUE original_fields_obj = fields_obj;
4593 fields_obj = original_fields_obj ? original_fields_obj : rb_imemo_fields_new(klass, ROOT_SHAPE_ID,
true);
4595 shape_id_t current_shape_id = RBASIC_SHAPE_ID(fields_obj);
4596 shape_id_t next_shape_id = current_shape_id;
4597 if (UNLIKELY(rb_shape_complex_p(current_shape_id))) {
4602 next_shape_id = generic_shape_ivar(fields_obj,
id, &new_ivar);
4604 if (UNLIKELY(rb_shape_complex_p(next_shape_id))) {
4605 fields_obj = imemo_fields_evacutate_to_complex(klass, fields_obj, next_shape_id, 1);
4609 attr_index_t index = RSHAPE_INDEX(next_shape_id);
4610 if (new_ivar && index >= RSHAPE_CAPACITY(current_shape_id)) {
4613 fields_obj = imemo_fields_copy_append(klass, fields_obj, current_shape_id, next_shape_id, val);
4616 VALUE *fields = rb_imemo_fields_ptr(fields_obj);
4618 if (concurrent && original_fields_obj == fields_obj) {
4624 RB_OBJ_ATOMIC_WRITE(fields_obj, &fields[index], val);
4631 RUBY_ASSERT(rb_shape_layout(next_shape_id) == SHAPE_ID_LAYOUT_ROBJECT);
4632 RBASIC_SET_SHAPE_ID(fields_obj, next_shape_id);
4636 *new_fields_obj = fields_obj;
4637 *new_ivar_out = new_ivar;
4642 if (concurrent && fields_obj == original_fields_obj) {
4646 fields_obj = rb_imemo_fields_clone(fields_obj);
4649 st_table *table = rb_imemo_fields_complex_tbl(fields_obj);
4650 new_ivar = !st_insert(table, (st_data_t)
id, (st_data_t)val);
4653 if (fields_obj != original_fields_obj) {
4654 RUBY_ASSERT(rb_shape_layout(next_shape_id) == SHAPE_ID_LAYOUT_ROBJECT);
4655 RBASIC_SET_SHAPE_ID(fields_obj, next_shape_id);
4659 *new_fields_obj = fields_obj;
4660 *new_ivar_out = new_ivar;
4661 return ATTR_INDEX_NOT_SET;
4665class_ivar_set(
VALUE obj,
ID id,
VALUE val,
bool *new_ivar)
4667 rb_class_ensure_writable(obj);
4669 const VALUE original_fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
4670 VALUE new_fields_obj = 0;
4672 attr_index_t index = class_fields_ivar_set(obj, original_fields_obj,
id, val, rb_multi_ractor_p(), &new_fields_obj, new_ivar);
4674 if (new_fields_obj != original_fields_obj) {
4675 RCLASS_WRITABLE_SET_FIELDS_OBJ(obj, new_fields_obj);
4681 RBASIC_SET_SHAPE_ID(obj, RBASIC_SHAPE_ID(new_fields_obj));
4689 rb_check_frozen(obj);
4692 class_ivar_set(obj,
id, val, &new_ivar);
4701 RUBY_ASSERT(RSHAPE_TYPE_P(RBASIC_SHAPE_ID(dst), SHAPE_ROOT));
4703 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(src);
4705 VALUE dst_fields_obj = rb_imemo_fields_clone(fields_obj);
4708 shape_id_t shape_id = RBASIC_SHAPE_ID(dst_fields_obj) & ~SHAPE_ID_FL_FROZEN;
4709 RBASIC_SET_SHAPE_ID(dst_fields_obj, shape_id);
4710 RCLASS_WRITABLE_SET_FIELDS_OBJ(dst, dst_fields_obj);
4711 RBASIC_SET_SHAPE_ID(dst, shape_id);
4722 r = rb_id_table_lookup(tbl,
id, &val);
4731rb_const_lookup(
VALUE klass,
ID id)
4733 return const_lookup(RCLASS_CONST_TBL(klass),
id);
4743 struct rb_id_table *tbl = RCLASS_CONST_TBL(klass);
4745 if (tbl && rb_id_table_lookup(tbl,
id, &entry)) {
#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 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.
void rb_class_modify_check(VALUE klass)
Asserts that klass is not a frozen class.
void rb_freeze_singleton_class(VALUE attached_object)
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 ALLOC
Old name of RB_ALLOC.
#define T_STRING
Old name of RUBY_T_STRING.
#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 T_STRUCT
Old name of RUBY_T_STRUCT.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define T_DATA
Old name of RUBY_T_DATA.
#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 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_cObject
Object class.
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.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
ID rb_check_id_cstr(const char *ptr, rb_len_t 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_funcallv(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcall(), except it takes the method arguments as a C array.
VALUE rb_ary_new_capa(rb_len_t capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_hidden_new(rb_len_t capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_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_subseq(VALUE str, rb_len_t beg, rb_len_t len)
Identical to rb_str_substr(), except the numbers are interpreted as byte offsets instead of character...
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_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_locked_p(VALUE mutex)
Queries if there are any threads that holds the lock.
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.
int rb_obj_respond_to(VALUE obj, ID mid, int private_p)
Identical to rb_respond_to(), except it additionally takes the visibility parameter.
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.
ID rb_to_id(VALUE str)
Identical to rb_intern_str(), except it tries to convert the parameter object to an instance of rb_cS...
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 capa
Designed capacity of the buffer.
int len
Length of the buffer.
#define RB_OBJ_SET_SHAREABLE(obj)
Wrapper of rb_obj_set_shareable().
static bool rb_ractor_shareable_p(VALUE obj)
Queries if multiple Ractors can share the passed object or not.
#define RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
#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 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 RUBY_TYPED_FREE_IMMEDIATELY
Macros to see if each corresponding flag is defined.
#define RTYPEDDATA(obj)
Convenient casting macro.
#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.
Internal header for Ruby Box.
Internal header for Class.
This is the struct that holds necessary info for a struct.
void rb_native_mutex_lock(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_lock.
void rb_native_mutex_initialize(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_initialize.
void rb_native_mutex_unlock(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_unlock.
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static 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.