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);
60static VALUE autoload_features;
65static VALUE autoload_mutex;
67static void check_before_mod_set(
VALUE,
ID,
VALUE,
const char *);
69static VALUE rb_const_search(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility,
VALUE *found_in);
78typedef int rb_ivar_foreach_callback_func(
ID key,
VALUE val, st_data_t arg);
79static void rb_field_foreach(
VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg,
bool ivar_only);
82rb_generic_fields_lock_atfork(
void)
91 rb_global_tbl = rb_id_table_create(0);
92 generic_fields_tbl_ = st_init_numtable();
97 rb_vm_register_global_object(autoload_mutex);
99 autoload_features = rb_ident_hash_new();
101 rb_vm_register_global_object(autoload_features);
105rb_namespace_p(
VALUE obj)
126classname(
VALUE klass,
bool *permanent)
130 VALUE classpath = RCLASS_CLASSPATH(klass);
131 if (classpath == 0)
return Qnil;
133 *permanent = RCLASS_PERMANENT_CLASSPATH_P(klass);
139rb_mod_name0(
VALUE klass,
bool *permanent)
141 return classname(klass, permanent);
156 return classname(mod, &permanent);
161is_constant_path(
VALUE name)
163 const char *path = RSTRING_PTR(name);
167 const char *p = path;
169 if (p >= pend || !*p) {
174 if (p + 2 <= pend && p[0] ==
':' && p[1] ==
':') {
178 const char *pbeg = p;
179 while (p < pend && *p !=
':') p++;
181 if (pbeg == p)
return false;
183 if (rb_enc_symname_type(pbeg, p - pbeg, enc, 0) != ID_CONST) {
200set_sub_temporary_name_recursive(
VALUE mod,
VALUE data,
int recursive)
202 if (recursive)
return Qfalse;
207 name = build_const_path(rb_ary_last(0, 0, args->names), args->last);
209 set_sub_temporary_name_foreach(mod, args, name);
214set_sub_temporary_name_topmost(
VALUE mod,
VALUE data,
int recursive)
216 if (recursive)
return Qfalse;
219 VALUE name = args->names;
223 set_sub_temporary_name_foreach(mod, args, name);
227static enum rb_id_table_iterator_result
228set_sub_temporary_name_i(
ID id,
VALUE val,
void *data)
231 if (rb_namespace_p(val) && !RCLASS_PERMANENT_CLASSPATH_P(val)) {
237 return ID_TABLE_CONTINUE;
243 RCLASS_WRITE_CLASSPATH(mod, name, FALSE);
247 rb_id_table_foreach(tbl, set_sub_temporary_name_i, args);
252 rb_id_table_foreach(tbl, set_sub_temporary_name_i, args);
253 rb_ary_set_len(args->names, names_len);
314rb_mod_set_temporary_name(
VALUE mod,
VALUE name)
316 rb_class_owner_check(mod);
319 if (RCLASS_PERMANENT_CLASSPATH_P(mod)) {
326 set_sub_temporary_name(mod, 0);
333 if (RSTRING_LEN(name) == 0) {
334 rb_raise(rb_eArgError,
"empty class/module name");
337 if (is_constant_path(name)) {
338 rb_raise(rb_eArgError,
"the temporary name must not be a constant path to avoid confusion");
346 set_sub_temporary_name(mod, name);
359 path = rb_sprintf(
"#<Class:%p>", (
void*)obj);
362 path = rb_sprintf(
"#<Module:%p>", (
void*)obj);
365 path = rb_sprintf(
"#<%"PRIsVALUE
":%p>", klass, (
void*)obj);
375rb_tmp_class_path(
VALUE klass,
bool *permanent, fallback_func fallback)
377 VALUE path = classname(klass, permanent);
389 path = rb_tmp_class_path(
RBASIC(klass)->klass, &perm, fallback);
394 return fallback(klass, path);
401 VALUE path = rb_tmp_class_path(klass, &permanent, make_temporary_path);
419rb_search_class_path(
VALUE klass)
422 return rb_tmp_class_path(klass, &permanent, no_fallback);
431 return rb_fstring(path);
435build_const_path(
VALUE head,
ID tail)
437 return build_const_pathname(head, rb_id2str(tail));
443 bool permanent =
true;
450 str = rb_tmp_class_path(under, &permanent, make_temporary_path);
451 str = build_const_pathname(str, name);
455 RCLASS_SET_CLASSPATH(klass, str, permanent);
470 const char *pbeg, *pend, *p, *path = RSTRING_PTR(pathname);
474 if (!rb_enc_asciicompat(enc)) {
475 rb_raise(rb_eArgError,
"invalid class path encoding (non ASCII)");
478 pend = path + RSTRING_LEN(pathname);
479 if (path == pend || path[0] ==
'#') {
480 rb_raise(rb_eArgError,
"can't retrieve anonymous class %"PRIsVALUE,
484 while (p < pend && *p !=
':') p++;
486 if (p < pend && p[0] ==
':') {
487 if ((
size_t)(pend - p) < 2 || p[1] !=
':')
goto undefined_class;
492 goto undefined_class;
494 c = rb_const_search(c,
id, TRUE, FALSE, FALSE, NULL);
495 if (UNDEF_P(c))
goto undefined_class;
496 if (!rb_namespace_p(c)) {
497 rb_raise(
rb_eTypeError,
"%"PRIsVALUE
" does not refer to class/module",
506 rb_raise(rb_eArgError,
"undefined class/module % "PRIsVALUE,
527 VALUE path = rb_tmp_class_path(
rb_class_real(klass), &permanent, make_temporary_path);
528 if (
NIL_P(path))
return NULL;
529 return RSTRING_PTR(path);
552 rb_gvar_compact_t *compactor;
579static enum rb_id_table_iterator_result
580free_global_entry_i(
VALUE val,
void *arg)
583 entry->var->counter--;
584 if (entry->var->counter == 0) {
585 free_global_variable(entry->var);
588 return ID_TABLE_DELETE;
592rb_free_rb_global_tbl(
void)
594 rb_id_table_foreach_values(rb_global_tbl, free_global_entry_i, 0);
595 rb_id_table_free(rb_global_tbl);
599rb_free_generic_fields_tbl_(
void)
601 st_free_table(generic_fields_tbl_);
605rb_gvar_undef_compactor(
void *var)
609NORETURN(
static void global_entry_isolation_error(
ID id));
612global_entry_isolation_error(
ID id)
614 rb_raise(rb_eRactorIsolationError,
"can not access global variable %s from non-main Ractor", rb_id2name(
id));
620global_entry_lookup(
ID id,
bool create_entry,
bool *isolation_error)
626 if (rb_id_table_lookup(rb_global_tbl,
id, &data)) {
634 *isolation_error = UNLIKELY(!rb_ractor_main_p()) && (!entry || !entry->ractor_local);
636 if (!entry && create_entry && !*isolation_error) {
641 entry->ractor_local =
false;
648 var->compactor = rb_gvar_undef_compactor;
650 var->block_trace = 0;
652 var->box_ready =
false;
653 var->box_dynamic =
false;
654 rb_id_table_insert(rb_global_tbl,
id, (
VALUE)entry);
662rb_find_global_entry(
ID id)
664 bool isolation_error;
665 struct rb_global_entry *entry = global_entry_lookup(
id,
false, &isolation_error);
667 if (isolation_error) global_entry_isolation_error(
id);
673rb_gvar_ractor_local(
const char *name)
676 entry->ractor_local =
true;
680rb_gvar_box_ready(
const char *name)
683 entry->var->box_ready =
true;
687rb_gvar_box_dynamic(
const char *name)
690 entry->var->box_dynamic =
true;
696 bool isolation_error;
697 struct rb_global_entry *entry = global_entry_lookup(
id,
true, &isolation_error);
699 if (isolation_error) global_entry_isolation_error(
id);
707 rb_warning(
"global variable '%"PRIsVALUE
"' not initialized", QUOTE_ID(
id));
713rb_gvar_val_compactor(
void *_var)
718 rb_gc_update_moved_ptr(&var->data);
729 var->compactor = rb_gvar_val_compactor;
731 var->data = (
void*)val;
749 var->data = (
void*)val;
756 if (data) rb_gc_mark_movable(data);
762 if (!var)
return Qnil;
775 if (var) rb_gc_mark_maybe(*var);
781 rb_name_error(
id,
"%"PRIsVALUE
" is a read-only variable", QUOTE_ID(
id));
784static enum rb_id_table_iterator_result
785mark_global_entry(
VALUE v,
void *ignored)
791 (*var->marker)(var->data);
794 if (trace->data) rb_gc_mark_maybe(trace->data);
797 return ID_TABLE_CONTINUE;
800#define gc_mark_table(task) \
801 if (rb_global_tbl) { rb_id_table_foreach_values(rb_global_tbl, task##_global_entry, 0); }
804rb_gc_mark_global_tbl(
void)
809static enum rb_id_table_iterator_result
810update_global_entry(
VALUE v,
void *ignored)
815 (*var->compactor)(var);
816 return ID_TABLE_CONTINUE;
820rb_gc_update_global_tbl(
void)
822 gc_mark_table(update);
826global_id(
const char *name)
830 if (name[0] ==
'$')
id = rb_intern(name);
832 size_t len = strlen(name);
836 memcpy(buf+1, name,
len);
837 id = rb_intern2(buf,
len+1);
844find_global_id(
const char *name)
847 size_t len = strlen(name);
849 if (name[0] ==
'$') {
856 memcpy(buf+1, name,
len);
872 ID id = global_id(name);
875 gvar->data = (
void*)var;
927 trace->next = entry->var->trace;
928 trace->func = rb_trace_eval;
931 entry->var->trace = trace;
945 while (trace->next) {
948 trace->next = next->next;
971 if ((entry = rb_find_global_entry(
id)) == NULL) {
972 rb_name_error(
id,
"undefined global variable %"PRIsVALUE
"", QUOTE_ID(
id));
975 trace = entry->var->trace;
986 if (!entry->var->block_trace) remove_trace(entry->var);
991 if (trace->data == cmd) {
993 if (!entry->var->block_trace) remove_trace(entry->var);
1014 (*trace->func)(trace->data, data->val);
1015 trace = trace->next;
1025 var->block_trace = 0;
1035 if (var->trace && !var->block_trace) {
1036 var->block_trace = 1;
1037 trace.trace = var->trace;
1048 (*var->setter)(val, entry->id, var->data);
1049 gvar_trace(var, val);
1056 return BOX_USER_P(box) &&
1057 !entry->var->box_dynamic &&
1066 const rb_box_t *box = rb_current_box();
1067 bool use_box_tbl =
false;
1068 bool isolation_error =
false;
1071 entry = global_entry_lookup(
id,
true, &isolation_error);
1073 if (!isolation_error && gvar_use_box_tbl(box, entry)) {
1075 rb_hash_aset(box->gvar_tbl,
rb_id2sym(entry->var->id), val);
1080 if (isolation_error) global_entry_isolation_error(
id);
1083 gvar_trace(entry->var, val);
1086 retval = rb_gvar_set_entry(entry, val);
1094 return rb_gvar_set(global_id(name), val);
1100 VALUE retval, gvars, key;
1101 const rb_box_t *box = rb_current_box();
1102 bool use_box_tbl =
false;
1103 bool isolation_error =
false;
1109 entry = global_entry_lookup(
id,
true, &isolation_error);
1111 if (!isolation_error) {
1114 if (gvar_use_box_tbl(box, entry)) {
1116 gvars = box->gvar_tbl;
1118 if (
RTEST(rb_hash_has_key(gvars, key))) {
1119 retval = rb_hash_aref(gvars, key);
1127 retval = (*var->getter)(entry->id, var->data);
1129 retval =
rb_funcall(retval, rb_intern(
"clone"), 0);
1133 if (cache) rb_hash_aset(gvars, key, retval);
1139 if (isolation_error) global_entry_isolation_error(
id);
1142 retval = (*var->getter)(entry->id, var->data);
1150 ID id = find_global_id(name);
1153 rb_warning(
"global variable '%s' not initialized", name);
1157 return rb_gvar_get(
id);
1161rb_gvar_defined(
ID id)
1163 const rb_box_t *box = rb_current_box();
1170 (gvar_use_box_tbl(box, entry) &&
1171 RTEST(rb_hash_has_key(box->gvar_tbl,
rb_id2sym(entry->var->id))));
1173 return RBOOL(defined);
1177rb_gvar_getter_function_of(
ID id)
1180 return entry->var->getter;
1184rb_gvar_setter_function_of(
ID id)
1187 return entry->var->setter;
1190static enum rb_id_table_iterator_result
1191gvar_i(
ID key,
VALUE val,
void *a)
1195 return ID_TABLE_CONTINUE;
1204 if (!rb_ractor_main_p()) {
1205 rb_raise(rb_eRactorIsolationError,
"can not access global variables from non-main Ractors");
1209 rb_id_table_foreach(rb_global_tbl, gvar_i, (
void *)ary);
1210 if (!
NIL_P(backref)) {
1212 int i, nmatch = rb_match_count(backref);
1214 for (i = 1; i <= nmatch; ++i) {
1218 buf[1] = (char)(i +
'0');
1219 sym =
ID2SYM(rb_intern2(buf, 2));
1237 bool tracer_error =
false;
1239 if (!rb_ractor_main_p()) {
1240 rb_raise(rb_eRactorIsolationError,
"can not access global variables from non-main Ractors");
1244 bool isolation_error;
1245 entry2 = global_entry_lookup(name2,
true, &isolation_error);
1246 VM_ASSERT(!isolation_error);
1248 if (!rb_id_table_lookup(gtbl, name1, &data1)) {
1251 rb_id_table_insert(gtbl, name1, (
VALUE)entry1);
1253 else if ((entry1 = (
struct rb_global_entry *)data1)->var != entry2->var) {
1255 if (var->block_trace) {
1256 tracer_error =
true;
1260 if (var->counter == 0) {
1261 free_global_variable(var);
1265 if (!tracer_error && entry1->var != entry2->var) {
1266 entry2->var->counter++;
1267 entry1->var = entry2->var;
1275class_ivar_set_ractor_check(
VALUE klass,
ID id)
1278 UNLIKELY(!rb_class_owned_p(klass))) {
1279 rb_raise(rb_eRactorIsolationError,
"can not set instance variables of classes/modules created by another Ractor");
1286cvar_set_ractor_check(
VALUE klass,
ID id)
1288 if (UNLIKELY(!rb_class_owned_p(klass))) {
1289 rb_raise(rb_eRactorIsolationError,
1290 "can not set class variable %"PRIsVALUE
" of %"PRIsVALUE
", which was created by another Ractor",
1291 rb_id2str(
id), klass);
1299 rb_raise(rb_eRactorIsolationError,
1300 "can not read non-shareable class variable %"PRIsVALUE
" of %"PRIsVALUE
", which was created by another Ractor",
1301 rb_id2str(
id), klass);
1306ivar_ractor_assert(
VALUE obj,
ID id)
1314 rb_shape_frozen_p(RBASIC_SHAPE_ID(obj)),
1315 "shareable object must not have writable instance variables");
1319rb_generic_fields_tbl_get(
void)
1321 return generic_fields_tbl_;
1330 if (rb_multi_ractor_p()) {
1338 if (rb_multi_ractor_p()) {
1344rb_mark_generic_ivar(
VALUE obj)
1349 if (rb_gc_during_global_gc_p() && rb_gc_multi_objspace_p()) {
1357 st_lookup(generic_fields_tbl_, (st_data_t)obj, (st_data_t *)&data);
1360 rb_gc_mark_movable(data);
1368rb_obj_fields_generic_uncached(
VALUE obj)
1370 VALUE fields_obj = 0;
1374 found = st_lookup(generic_fields_tbl_, (st_data_t)obj, (st_data_t *)&fields_obj);
1378 rb_bug(
"Object is missing entry in generic_fields_tbl");
1384obj_use_generic_fields_tbl_p(
VALUE obj)
1399rb_obj_fields(
VALUE obj,
ID field_name)
1401 ivar_ractor_assert(obj, field_name);
1409 return ROBJECT_FIELDS_OBJ(obj);
1413 return RCLASS_WRITABLE_FIELDS_OBJ(obj);
1419 return RSTRUCT_FIELDS_OBJ(obj);
1423 VALUE fields_obj = 0;
1425 if (rb_obj_shape_has_fields(obj)) {
1427 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) {
1428 fields_obj = ec->gen_fields_cache.fields_obj;
1429 RUBY_ASSERT(fields_obj == rb_obj_fields_generic_uncached(obj));
1432 fields_obj = rb_obj_fields_generic_uncached(obj);
1433 ec->gen_fields_cache.fields_obj = fields_obj;
1434 ec->gen_fields_cache.obj = obj;
1446 if (rb_obj_gen_fields_p(obj)) {
1447 st_data_t key = (st_data_t)obj, value;
1453 RSTRUCT_SET_FIELDS_OBJ(obj, 0);
1461 if (ec->gen_fields_cache.obj == obj) {
1462 ec->gen_fields_cache.obj =
Qundef;
1463 ec->gen_fields_cache.fields_obj =
Qundef;
1468 if (rb_gc_during_global_gc_p() || ruby_vm_during_cleanup) {
1476 deleted = st_delete(generic_fields_tbl_, &key, &value);
1479 rb_bug(
"Object is missing entry in generic_fields_tbl");
1483 RBASIC_SET_SHAPE_ID(obj, ROOT_SHAPE_ID);
1488rb_obj_set_fields(
VALUE obj,
VALUE fields_obj,
ID field_name,
VALUE original_fields_obj)
1490 ivar_ractor_assert(obj, field_name);
1495 rb_imemo_fields_clear(original_fields_obj);
1499 RUBY_ASSERT(IMEMO_TYPE_P(fields_obj, imemo_fields));
1503 if (fields_obj != original_fields_obj) {
1507 ROBJECT_SET_EXTENDED(obj, fields_obj);
1513 RSTRUCT_SET_FIELDS_OBJ(obj, fields_obj);
1522 bool gc_disabled =
RTEST(rb_gc_local_disable_no_rest());
1524 enum ruby_tag_type state;
1526 EC_PUSH_TAG(insert_ec);
1527 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1528 st_insert(generic_fields_tbl_, (st_data_t)obj, (st_data_t)fields_obj);
1532 if (!gc_disabled) rb_gc_local_enable();
1533 if (state != TAG_NONE) EC_JUMP_TAG(insert_ec, state);
1537 if (ec->gen_fields_cache.fields_obj != fields_obj) {
1538 ec->gen_fields_cache.obj = obj;
1539 ec->gen_fields_cache.fields_obj = fields_obj;
1544 if (original_fields_obj && original_fields_obj != obj) {
1546 rb_imemo_fields_clear(original_fields_obj);
1551 RBASIC_SET_SHAPE_ID_WITH_LAYOUT(obj, RBASIC_SHAPE_ID(fields_obj), SHAPE_ID_LAYOUT_EXTENDED);
1554 RBASIC_SET_SHAPE_ID(obj, RBASIC_SHAPE_ID(fields_obj));
1559rb_obj_replace_fields(
VALUE obj,
VALUE fields_obj)
1561 if (obj_use_generic_fields_tbl_p(obj)) {
1565 VALUE original_fields_obj = rb_obj_fields_no_ractor_check(obj);
1566 rb_obj_set_fields(obj, fields_obj, 0, original_fields_obj);
1570 VALUE original_fields_obj = rb_obj_fields_no_ractor_check(obj);
1571 rb_obj_set_fields(obj, fields_obj, 0, original_fields_obj);
1576rb_obj_field_get(
VALUE obj, shape_id_t target_shape_id)
1579 RUBY_ASSERT(RSHAPE_TYPE_P(target_shape_id, SHAPE_IVAR) || RSHAPE_TYPE_P(target_shape_id, SHAPE_OBJ_ID));
1581 VALUE fields_obj = rb_obj_fields(obj, RSHAPE_EDGE_NAME(target_shape_id));
1583 if (UNLIKELY(rb_shape_complex_p(target_shape_id))) {
1584 st_table *fields_hash = rb_imemo_fields_complex_tbl(fields_obj);
1586 st_lookup(fields_hash, RSHAPE_EDGE_NAME(target_shape_id), &value);
1591 attr_index_t index = RSHAPE_INDEX(target_shape_id);
1592 return rb_imemo_fields_ptr(fields_obj)[index];
1602 VALUE fields_obj = rb_obj_fields(obj, is_class ? 0 : id);
1608 shape_id_t shape_id = RBASIC_SHAPE_ID(fields_obj);
1611 if (UNLIKELY(rb_shape_complex_p(shape_id))) {
1612 st_table *iv_table = rb_imemo_fields_complex_tbl(fields_obj);
1613 if (!rb_st_lookup(iv_table, (st_data_t)
id, (st_data_t *)&val)) {
1618 attr_index_t index = 0;
1619 if (!rb_shape_get_iv_index(shape_id,
id, &index)) {
1622 val = rb_imemo_fields_ptr(fields_obj)[index];
1628 rb_eRactorIsolationError,
1629 "can not get unshareable values from instance variables of classes/modules "
1630 "created by another Ractor (%"PRIsVALUE
" from %"PRIsVALUE
")",
1643 VALUE iv = rb_ivar_lookup(obj,
id,
Qnil);
1644 RB_DEBUG_COUNTER_INC(ivar_get_base);
1649rb_ivar_get_at(
VALUE obj, attr_index_t index,
ID id)
1660 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
1661 VALUE val = rb_imemo_fields_ptr(fields_obj)[index];
1664 rb_raise(rb_eRactorIsolationError,
1665 "can not get unshareable values from instance variables of classes/modules created by another Ractor");
1672 VALUE fields_obj = rb_obj_fields(obj,
id);
1673 return rb_imemo_fields_ptr(fields_obj)[index];
1679rb_ivar_get_at_no_ractor_check(
VALUE obj, attr_index_t index)
1683 VALUE fields_obj = rb_obj_fields_no_ractor_check(obj);
1684 return rb_imemo_fields_ptr(fields_obj)[index];
1690 return rb_ivar_lookup(obj,
id,
Qnil);
1693static VALUE imemo_fields_evacutate_to_complex(
VALUE owner,
VALUE source_fields_obj, shape_id_t shape_id,
int extra_capa);
1696rb_obj_convert_too_complex(
VALUE obj,
VALUE fields_obj, shape_id_t shape_id)
1701 shape_id = rb_shape_transition_complex(shape_id);
1702 VALUE new_fields_obj = imemo_fields_evacutate_to_complex(obj, fields_obj, shape_id, 1);
1703 ROBJECT_SET_EXTENDED(obj, new_fields_obj);
1704 RBASIC_SET_SHAPE_ID_WITH_LAYOUT(obj, shape_id, SHAPE_ID_LAYOUT_EXTENDED);
1711 rb_check_frozen(obj);
1714 bool concurrent =
false;
1718 class_ivar_set_ractor_check(obj,
id);
1720 if (rb_multi_ractor_p()) {
1725 VALUE fields_obj = rb_obj_fields(obj,
id);
1730 const VALUE original_fields_obj = fields_obj;
1732 fields_obj = rb_imemo_fields_clone(fields_obj);
1735 shape_id_t old_shape_id = RBASIC_SHAPE_ID(fields_obj);
1736 shape_id_t removed_shape_id;
1737 shape_id_t next_shape_id = rb_obj_shape_transition_remove_ivar(fields_obj,
id, &removed_shape_id);
1739 if (UNLIKELY(rb_shape_complex_p(next_shape_id))) {
1740 if (UNLIKELY(!rb_shape_complex_p(old_shape_id))) {
1741 fields_obj = imemo_fields_evacutate_to_complex(obj, fields_obj, next_shape_id, -1);
1744 if (!st_delete(rb_imemo_fields_complex_tbl(fields_obj), &key, (st_data_t *)&val)) {
1749 if (next_shape_id == old_shape_id) {
1753 RUBY_ASSERT(removed_shape_id != INVALID_SHAPE_ID);
1754 RUBY_ASSERT(RSHAPE_LEN(next_shape_id) == RSHAPE_LEN(old_shape_id) - 1);
1756 VALUE *fields = rb_imemo_fields_ptr(fields_obj);
1757 attr_index_t removed_index = RSHAPE_INDEX(removed_shape_id);
1758 val = fields[removed_index];
1760 attr_index_t new_fields_count = RSHAPE_LEN(next_shape_id);
1761 if (new_fields_count) {
1762 size_t trailing_fields = new_fields_count - removed_index;
1764 MEMMOVE(&fields[removed_index], &fields[removed_index + 1],
VALUE, trailing_fields);
1765 RBASIC_SET_SHAPE_ID(fields_obj, next_shape_id);
1767 if (
type ==
T_OBJECT && obj != fields_obj && new_fields_count == rb_shape_embedded_capacity(RBASIC_SHAPE_ID(obj))) {
1772 VALUE *embedded_fields = ROBJECT_EMBEDDED_FIELDS(obj);
1773 MEMCPY(embedded_fields, fields,
VALUE, new_fields_count);
1774 for (attr_index_t i = 0; i < new_fields_count; i++) {
1786 if (fields_obj != original_fields_obj) {
1789 if (fields_obj && fields_obj != obj) {
1790 ROBJECT_SET_EXTENDED(obj, fields_obj);
1795 RCLASS_WRITABLE_SET_FIELDS_OBJ(obj, fields_obj);
1798 rb_obj_set_fields(obj, fields_obj,
id, original_fields_obj);
1804 if (!fields_obj || fields_obj == obj) {
1805 RBASIC_SET_SHAPE_ID_WITH_LAYOUT(obj, next_shape_id, SHAPE_ID_LAYOUT_ROBJECT);
1808 RBASIC_SET_SHAPE_ID_WITH_LAYOUT(obj, next_shape_id, SHAPE_ID_LAYOUT_EXTENDED);
1812 RBASIC_SET_SHAPE_ID(obj, next_shape_id);
1819rb_attr_delete(
VALUE obj,
ID id)
1821 return rb_ivar_delete(obj,
id,
Qnil);
1825imemo_fields_complex_from_obj_i(
ID key,
VALUE val, st_data_t arg)
1828 st_table *table = rb_imemo_fields_complex_tbl(fields);
1830 RUBY_ASSERT(!st_lookup(table, (st_data_t)key, NULL));
1831 st_add_direct(table, (st_data_t)key, (st_data_t)val);
1838imemo_fields_shref_i(
ID key,
VALUE val, st_data_t arg)
1845 rb_gc_writebarrier(fields_obj, val);
1853rb_imemo_fields_record_shrefs(
VALUE fields_obj)
1855 rb_field_foreach(fields_obj, imemo_fields_shref_i, (st_data_t)fields_obj,
false);
1859imemo_fields_complex_from_obj(
VALUE owner,
VALUE source, shape_id_t shape_id,
bool ivar_only,
int extra_capa)
1861 attr_index_t
len = source ? RSHAPE_LEN(RBASIC_SHAPE_ID(source)) : 0;
1862 int capa = (
len + extra_capa);
1867 rb_field_foreach(source, imemo_fields_complex_from_obj_i, (st_data_t)fields_obj, ivar_only);
1873imemo_fields_evacutate_to_complex(
VALUE owner,
VALUE source, shape_id_t shape_id,
int extra_capa)
1875 return imemo_fields_complex_from_obj(owner, source, shape_id,
false, extra_capa);
1879rb_obj_complex_fields_build(
VALUE obj)
1881 return imemo_fields_complex_from_obj(obj, obj, ROOT_COMPLEX_SHAPE_ID,
true, 0);
1885imemo_fields_copy_append(
VALUE owner,
VALUE source_fields_obj, shape_id_t current_shape_id, shape_id_t target_shape_id,
VALUE val)
1887 attr_index_t fields_count = RSHAPE_LEN(current_shape_id);
1891 VALUE *fields = rb_imemo_fields_ptr(fields_obj);
1893 if (source_fields_obj) {
1894 MEMCPY(fields, rb_imemo_fields_ptr(source_fields_obj),
VALUE, fields_count);
1895 for (attr_index_t i = 0; i < fields_count; i++) {
1906imemo_fields_set(
VALUE owner,
VALUE fields_obj, shape_id_t target_shape_id,
ID field_name,
VALUE val,
bool concurrent)
1908 const VALUE original_fields_obj = fields_obj;
1909 shape_id_t current_shape_id = fields_obj ? RBASIC_SHAPE_ID(fields_obj) : ROOT_SHAPE_ID;
1911 if (UNLIKELY(rb_shape_complex_p(target_shape_id))) {
1912 if (rb_shape_complex_p(current_shape_id)) {
1917 fields_obj = rb_imemo_fields_clone(fields_obj);
1921 fields_obj = imemo_fields_evacutate_to_complex(owner, original_fields_obj, target_shape_id, 1);
1922 current_shape_id = target_shape_id;
1925 st_table *table = rb_imemo_fields_complex_tbl(fields_obj);
1928 st_insert(table, (st_data_t)field_name, (st_data_t)val);
1930 RBASIC_SET_SHAPE_ID(fields_obj, target_shape_id);
1933 attr_index_t index = RSHAPE_INDEX(target_shape_id);
1934 if (concurrent || index >= rb_shape_embedded_capacity(current_shape_id)) {
1935 return imemo_fields_copy_append(owner, original_fields_obj, current_shape_id, target_shape_id, val);
1938 VALUE *table = rb_imemo_fields_ptr(fields_obj);
1941 if (index >= RSHAPE_LEN(current_shape_id)) {
1942 RBASIC_SET_SHAPE_ID(fields_obj, target_shape_id);
1950generic_field_set(
VALUE obj, shape_id_t target_shape_id,
ID field_name,
VALUE val)
1953 field_name = RSHAPE_EDGE_NAME(target_shape_id);
1957 const VALUE original_fields_obj = rb_obj_fields(obj, field_name);
1958 VALUE fields_obj = imemo_fields_set(obj, original_fields_obj, target_shape_id, field_name, val,
false);
1960 rb_obj_set_fields(obj, fields_obj, field_name, original_fields_obj);
1961 return rb_shape_complex_p(target_shape_id) ? ATTR_INDEX_NOT_SET : RSHAPE_INDEX(target_shape_id);
1965generic_shape_ivar(
VALUE obj,
ID id,
bool *new_ivar_out)
1967 bool new_ivar =
false;
1968 shape_id_t current_shape_id = RBASIC_SHAPE_ID(obj);
1969 shape_id_t target_shape_id = current_shape_id;
1971 if (!rb_shape_complex_p(current_shape_id)) {
1972 if (!rb_shape_find_ivar(current_shape_id,
id, &target_shape_id)) {
1974 target_shape_id = rb_obj_shape_transition_add_ivar(obj,
id);
1978 *new_ivar_out = new_ivar;
1979 return target_shape_id;
1986 shape_id_t target_shape_id = generic_shape_ivar(obj,
id, &dontcare);
1987 return generic_field_set(obj, target_shape_id,
id, val);
1991obj_field_set(
VALUE obj, shape_id_t target_shape_id,
ID field_name,
VALUE val)
1994 VALUE fields_obj = ROBJECT_FIELDS_OBJ(obj);
1995 shape_id_t current_shape_id = RBASIC_SHAPE_ID(obj);
1997 if (UNLIKELY(rb_shape_complex_p(target_shape_id))) {
1998 if (UNLIKELY(!rb_shape_complex_p(current_shape_id))) {
1999 current_shape_id = rb_obj_convert_too_complex(obj, fields_obj, current_shape_id);
2000 fields_obj = ROBJECT_FIELDS_OBJ(obj);
2007 field_name = RSHAPE_EDGE_NAME(target_shape_id);
2011 st_insert(rb_imemo_fields_complex_tbl(fields_obj), (st_data_t)field_name, (st_data_t)val);
2014 RBASIC_SET_SHAPE_ID(obj, target_shape_id);
2015 if (obj != fields_obj) {
2016 RBASIC_SET_SHAPE_ID(fields_obj, target_shape_id);
2019 return ATTR_INDEX_NOT_SET;
2022 attr_index_t index = RSHAPE_INDEX(target_shape_id);
2024 if (index < RSHAPE_LEN(current_shape_id)) {
2026 RB_OBJ_WRITE(fields_obj, &rb_imemo_fields_ptr(fields_obj)[index], val);
2030 RUBY_ASSERT(index == RSHAPE_LEN(current_shape_id));
2032 if (UNLIKELY(index >= RSHAPE_CAPACITY(current_shape_id))) {
2033 fields_obj = imemo_fields_copy_append(obj, fields_obj, current_shape_id, target_shape_id, val);
2034 ROBJECT_SET_EXTENDED(obj, fields_obj);
2035 RBASIC_SET_FULL_SHAPE_ID(obj, rb_shape_transition_layout(target_shape_id, SHAPE_ID_LAYOUT_EXTENDED));
2038 RB_OBJ_WRITE(fields_obj, &rb_imemo_fields_ptr(fields_obj)[index], val);
2039 RBASIC_SET_SHAPE_ID(obj, target_shape_id);
2042 if (obj != fields_obj) {
2043 RBASIC_SET_SHAPE_ID(fields_obj, target_shape_id);
2054 shape_id_t target_shape_id = generic_shape_ivar(obj,
id, &dontcare);
2055 return obj_field_set(obj, target_shape_id,
id, val);
2059rb_check_ivar_modifiable(
VALUE obj)
2061 if (UNLIKELY(!
RB_FL_ABLE(obj) || rb_shape_frozen_p(RBASIC_SHAPE_ID(obj)))) {
2062 rb_check_frozen(obj);
2066 rb_raise(rb_eRactorIsolationError,
2067 "can't modify instance variables of a shareable %"PRIsVALUE,
2070 else if (UNLIKELY(CHILLED_STRING_P(obj))) {
2071 CHILLED_STRING_MUTATED(obj);
2084 rb_check_ivar_modifiable(obj);
2085 obj_ivar_set(obj,
id, val);
2099 shape_id_t shape_id = rb_obj_shape_transition_frozen(x);
2105 RBASIC_SET_SHAPE_ID(x, shape_id);
2108 RBASIC_SET_SHAPE_ID(x, shape_id);
2118static attr_index_t class_ivar_set(
VALUE obj,
ID id,
VALUE val,
bool *new_ivar);
2123 RB_DEBUG_COUNTER_INC(ivar_set_base);
2127 return obj_ivar_set(obj,
id, val);
2131 class_ivar_set_ractor_check(obj,
id);
2133 return class_ivar_set(obj,
id, val, &dontcare);
2136 return generic_ivar_set(obj,
id, val);
2143 rb_check_ivar_modifiable(obj);
2144 ivar_set(obj,
id, val);
2151 return ivar_set(obj,
id, val);
2160 ivar_set(obj,
id, val);
2164rb_obj_field_set(
VALUE obj, shape_id_t target_shape_id,
ID field_name,
VALUE val)
2168 return obj_field_set(obj, target_shape_id, field_name, val);
2172 rb_bug(
"Unreachable");
2175 return generic_field_set(obj, target_shape_id, field_name, val);
2180ivar_defined0(
VALUE obj,
ID id)
2182 if (rb_obj_shape_complex_p(obj)) {
2184 VALUE fields_obj = rb_obj_fields_no_ractor_check(obj);
2185 st_table *table = rb_imemo_fields_complex_tbl(fields_obj);
2188 if (!table || !rb_st_lookup(table,
id, &idx)) {
2196 return RBOOL(rb_shape_get_iv_index(RBASIC_SHAPE_ID(obj),
id, &index));
2210 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
2212 defined = ivar_defined0(fields_obj,
id);
2217 defined = ivar_defined0(obj,
id);
2225 struct gen_fields_tbl *fields_tbl;
2227 rb_ivar_foreach_callback_func *func;
2229 shape_id_t shape_id;
2234iterate_over_shapes_callback(shape_id_t shape_id,
void *data)
2238 if (itr_data->ivar_only && !RSHAPE_TYPE_P(shape_id, SHAPE_IVAR)) {
2245 RUBY_ASSERT(!rb_obj_shape_complex_p(itr_data->obj));
2246 fields = ROBJECT_FIELDS(itr_data->obj);
2249 RUBY_ASSERT(IMEMO_TYPE_P(itr_data->obj, imemo_fields));
2250 RUBY_ASSERT(!rb_obj_shape_complex_p(itr_data->obj));
2252 fields = rb_imemo_fields_ptr(itr_data->obj);
2255 rb_bug(
"Unreachable");
2258 RUBY_ASSERT(itr_data->shape_id == RBASIC_SHAPE_ID(itr_data->obj));
2260 VALUE val = fields[RSHAPE_INDEX(shape_id)];
2261 int ret = itr_data->func(RSHAPE_EDGE_NAME(shape_id), val, itr_data->arg);
2263 RUBY_ASSERT(itr_data->shape_id == RBASIC_SHAPE_ID(itr_data->obj));
2272iterate_over_shapes(shape_id_t shape_id, rb_ivar_foreach_callback_func *callback,
struct iv_itr_data *itr_data)
2274 rb_shape_foreach_field(shape_id, iterate_over_shapes_callback, itr_data);
2278each_hash_iv(st_data_t
id, st_data_t val, st_data_t data)
2281 rb_ivar_foreach_callback_func *callback = itr_data->func;
2282 if ((
ID)
id == rb_shape_tree.id_object_id) {
2285 return callback((
ID)
id, (
VALUE)val, itr_data->arg);
2289obj_fields_each(
VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg,
bool ivar_only)
2295 .ivar_only = ivar_only,
2298 VALUE fields_obj = ROBJECT_FIELDS_OBJ(obj);
2299 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
2301 if (rb_shape_complex_p(shape_id)) {
2302 st_foreach_safe(rb_imemo_fields_complex_tbl(fields_obj), each_hash_iv, (st_data_t)&itr_data);
2305 itr_data.fields = rb_imemo_fields_ptr(fields_obj);
2306 itr_data.shape_id = shape_id;
2307 iterate_over_shapes(shape_id, func, &itr_data);
2312imemo_fields_each(
VALUE fields_obj, rb_ivar_foreach_callback_func *func, st_data_t arg,
bool ivar_only)
2314 IMEMO_TYPE_P(fields_obj, imemo_fields);
2320 .ivar_only = ivar_only,
2323 shape_id_t shape_id = RBASIC_SHAPE_ID(fields_obj);
2324 if (rb_shape_complex_p(shape_id)) {
2325 rb_st_foreach(rb_imemo_fields_complex_tbl(fields_obj), each_hash_iv, (st_data_t)&itr_data);
2328 itr_data.fields = rb_imemo_fields_ptr(fields_obj);
2329 itr_data.shape_id = shape_id;
2330 iterate_over_shapes(shape_id, func, &itr_data);
2337 VALUE new_fields_obj;
2339 rb_check_frozen(dest);
2341 if (!rb_obj_gen_fields_p(obj)) {
2345 shape_id_t src_shape_id = rb_obj_shape_id(obj);
2347 VALUE fields_obj = rb_obj_fields_no_ractor_check(obj);
2355 shape_id_t initial_shape_id = rb_obj_shape_id(dest);
2356 shape_id_t dest_shape_id = rb_shape_rebuild(initial_shape_id, src_shape_id);
2358 if (UNLIKELY(rb_shape_complex_p(dest_shape_id))) {
2359 rb_obj_replace_fields(dest, rb_obj_complex_fields_build(obj));
2363 if (!RSHAPE_LEN(dest_shape_id)) {
2364 RBASIC_SET_SHAPE_ID(dest, 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(new_fields_obj, dest_buf, dest_shape_id, src_buf, src_shape_id);
2373 rb_obj_replace_fields(dest, new_fields_obj);
2381rb_generic_fields_shared_table_foreach(
void (*cb)(
struct st_table *tbl,
void *arg),
void *arg)
2384 if (generic_fields_tbl_ != NULL) {
2385 cb(generic_fields_tbl_, arg);
2394rb_generic_fields_tables_foreach(
void (*cb)(
struct st_table *tbl,
void *arg),
void *arg)
2396 if (generic_fields_tbl_ != NULL) {
2397 cb(generic_fields_tbl_, arg);
2402rb_field_foreach(
VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg,
bool ivar_only)
2407 if (IMEMO_TYPE_P(obj, imemo_fields)) {
2408 imemo_fields_each(obj, func, arg, ivar_only);
2412 obj_fields_each(obj, func, arg, ivar_only);
2419 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
2421 imemo_fields_each(fields_obj, func, arg, ivar_only);
2427 VALUE fields_obj = rb_obj_fields_no_ractor_check(obj);
2429 imemo_fields_each(fields_obj, func, arg, ivar_only);
2442collect_ivar_i(
ID id,
VALUE val, st_data_t arg)
2454 rb_field_foreach(obj, func, arg,
true);
2458rb_ivar_foreach_buffered(
VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg)
2461 if (count == 0)
return;
2467 rb_field_foreach(obj, collect_ivar_i, (st_data_t)&pos,
true);
2470 for (st_index_t i = 0; i < count; i++) {
2471 if (func(buf[i].name, buf[i].val, arg) == ST_STOP)
break;
2482 st_index_t iv_count = 0;
2483 VALUE fields_obj = rb_obj_fields_no_ractor_check(obj);
2486 if (rb_obj_shape_complex_p(fields_obj)) {
2487 iv_count = rb_st_table_size(rb_imemo_fields_complex_tbl(fields_obj));
2490 iv_count = RBASIC_FIELDS_COUNT(obj);
2494 if (rb_obj_shape_has_id(obj)) {
2502ivar_i(
ID key,
VALUE v, st_data_t a)
2537#define rb_is_constant_id rb_is_const_id
2538#define rb_is_constant_name rb_is_const_name
2539#define id_for_var(obj, name, part, type) \
2540 id_for_var_message(obj, name, type, "'%1$s' is not allowed as "#part" "#type" variable name")
2541#define id_for_var_message(obj, name, type, message) \
2542 check_id_type(obj, &(name), rb_is_##type##_id, rb_is_##type##_name, message, strlen(message))
2545 int (*valid_id_p)(
ID),
int (*valid_name_p)(
VALUE),
2546 const char *message,
size_t message_len)
2549 VALUE name = *pname;
2551 if (
id ? !valid_id_p(
id) : !valid_name_p(name)) {
2552 rb_name_err_raise_str(rb_fstring_new(message, message_len),
2584 const ID id = id_for_var(obj, name, an, instance);
2588 rb_check_frozen(obj);
2593 if (!UNDEF_P(val))
return val;
2596 rb_name_err_raise(
"instance variable %1$s not defined",
2601NORETURN(
static void uninitialized_constant(
VALUE,
VALUE));
2603uninitialized_constant(
VALUE klass,
VALUE name)
2606 rb_name_err_raise(
"uninitialized constant %2$s::%1$s",
2609 rb_name_err_raise(
"uninitialized constant %1$s",
2617 rb_vm_inc_const_missing_count();
2666 VALUE ref = ec->private_const_reference;
2667 rb_vm_pop_cfunc_frame();
2669 ec->private_const_reference = 0;
2670 rb_name_err_raise(
"private constant %2$s::%1$s referenced", ref, name);
2672 uninitialized_constant(klass, name);
2678autoload_table_mark(
void *ptr)
2680 rb_mark_tbl_no_pin((
st_table *)ptr);
2684autoload_table_free(
void *ptr)
2690autoload_table_memsize(
const void *ptr)
2693 return st_memsize(tbl);
2697autoload_table_compact(
void *ptr)
2699 rb_gc_ref_update_table_values_only((
st_table *)ptr);
2704 {autoload_table_mark, autoload_table_free, autoload_table_memsize, autoload_table_compact,},
2705 0, 0, RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED
2708#define check_autoload_table(av) \
2709 (struct st_table *)rb_check_typeddata((av), &autoload_table_type)
2720 if (RICLASS_IS_ORIGIN_P(mod)) {
2724 mod =
RBASIC(mod)->klass;
2732 VALUE tbl_value = rb_ivar_lookup(mod, autoload,
Qfalse);
2733 if (!
RTEST(tbl_value) || !(tbl = check_autoload_table(tbl_value)) || !st_lookup(tbl, (st_data_t)
id, &val)) {
2743 struct ccan_list_node cnode;
2746 VALUE autoload_data_value;
2762 rb_const_flag_t flag;
2778 rb_serial_t fork_gen;
2781 struct ccan_list_head constants;
2785autoload_data_mark_and_move(
void *ptr)
2789 rb_gc_mark_and_move(&p->feature);
2790 rb_gc_mark_and_move(&p->mutex);
2794autoload_data_free(
void *ptr)
2799 ccan_list_for_each_safe(&p->constants,
autoload_const, next, cnode) {
2807autoload_data_memsize(
const void *ptr)
2814 {autoload_data_mark_and_move, autoload_data_free, autoload_data_memsize, autoload_data_mark_and_move},
2819autoload_const_mark_and_move(
void *ptr)
2823 rb_gc_mark_and_move(&ac->module);
2824 rb_gc_mark_and_move(&ac->autoload_data_value);
2825 rb_gc_mark_and_move(&ac->value);
2826 rb_gc_mark_and_move(&ac->file);
2827 rb_gc_mark_and_move(&ac->box_value);
2831autoload_const_memsize(
const void *ptr)
2837autoload_const_free(
void *ptr)
2847 {autoload_const_mark_and_move, autoload_const_free, autoload_const_memsize, autoload_const_mark_and_move,},
2852get_autoload_data(
VALUE autoload_const_value,
struct autoload_const **autoload_const_pointer)
2865 if (autoload_const_pointer) *autoload_const_pointer =
autoload_const;
2872static void const_added(
VALUE klass,
ID const_name);
2882autoload_feature_lookup_or_create(
VALUE feature,
struct autoload_data **autoload_data_pointer)
2884 RUBY_ASSERT_MUTEX_OWNED(autoload_mutex);
2885 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
2887 VALUE autoload_data_value = rb_hash_aref(autoload_features, feature);
2890 if (
NIL_P(autoload_data_value)) {
2896 if (autoload_data_pointer) *autoload_data_pointer =
autoload_data;
2898 rb_hash_aset(autoload_features, feature, autoload_data_value);
2900 else if (autoload_data_pointer) {
2901 *autoload_data_pointer =
rb_check_typeddata(autoload_data_value, &autoload_data_type);
2904 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
2905 return autoload_data_value;
2909autoload_table_lookup_or_create(
VALUE module)
2911 VALUE autoload_table_value = rb_ivar_lookup(module, autoload,
Qfalse);
2912 if (
RTEST(autoload_table_value)) {
2913 return autoload_table_value;
2917 rb_class_ivar_set(module, autoload, autoload_table_value);
2919 return autoload_table_value;
2924autoload_synchronized(
VALUE _arguments)
2928 rb_const_entry_t *constant_entry = rb_const_lookup(arguments->module, arguments->name);
2929 if (constant_entry && !UNDEF_P(constant_entry->value)) {
2934 const_set(arguments->module, arguments->name,
Qundef);
2936 VALUE autoload_table_value = autoload_table_lookup_or_create(arguments->module);
2937 struct st_table *autoload_table = check_autoload_table(autoload_table_value);
2940 VALUE feature = rb_fstring(arguments->feature);
2943 VALUE autoload_data_value = autoload_feature_lookup_or_create(feature, &
autoload_data);
2955 st_insert(autoload_table, (st_data_t)arguments->name, (st_data_t)autoload_const_value);
2965 const rb_box_t *box = rb_current_box();
2966 VALUE current_box_value = rb_get_box_object((
rb_box_t *)box);
2969 rb_raise(
rb_eNameError,
"autoload must be constant name: %"PRIsVALUE
"", QUOTE_ID(name));
2972 rb_class_owner_check(module);
2975 if (!RSTRING_LEN(feature)) {
2976 rb_raise(rb_eArgError,
"empty feature name");
2983 .box_value = current_box_value,
2988 if (result ==
Qtrue) {
2989 const_added(module, name);
2994autoload_delete(
VALUE module,
ID name)
2996 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
2998 st_data_t load = 0, key = name;
3002 VALUE table_value = rb_ivar_lookup(module, autoload,
Qfalse);
3003 if (
RTEST(table_value)) {
3004 struct st_table *table = check_autoload_table(table_value);
3006 st_delete(table, &key, &load);
3029 if (table->num_entries == 0) {
3030 rb_attr_delete(module, autoload);
3035 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
3041 return ele->mutex !=
Qnil && !rb_mutex_owned_p(ele->mutex);
3045check_autoload_required(
VALUE mod,
ID id,
const char **loadingpath)
3049 const char *loading;
3051 if (!autoload_const_value || !(
autoload_data = get_autoload_data(autoload_const_value, 0))) {
3064 return autoload_const_value;
3067 loading = RSTRING_PTR(feature);
3070 return autoload_const_value;
3073 if (loadingpath && loading) {
3074 *loadingpath = loading;
3075 return autoload_const_value;
3084rb_autoloading_value(
VALUE mod,
ID id,
VALUE* value, rb_const_flag_t *flag)
3087 if (!ac)
return FALSE;
3103 return ele->mutex !=
Qnil && rb_mutex_owned_p(ele->mutex);
3110autoloading_const_entry(
VALUE mod,
ID id)
3117 if (!load || !(ele = get_autoload_data(load, &ac))) {
3123 if (autoload_by_current(ele)) {
3124 if (!UNDEF_P(ac->value)) {
3133autoload_defined_p(
VALUE mod,
ID id)
3138 if (!ce || !UNDEF_P(ce->value)) {
3144 return !rb_autoloading_value(mod,
id, NULL, NULL);
3166 check_before_mod_set(ac->module, ac->name, ac->value,
"constant");
3169 const_tbl_update(ac,
true);
3176autoload_load_needed(
VALUE _arguments)
3180 const char *loading = 0, *src;
3182 if (!autoload_defined_p(arguments->module, arguments->name)) {
3186 VALUE autoload_const_value = check_autoload_required(arguments->module, arguments->name, &loading);
3187 if (!autoload_const_value) {
3192 if (src && loading && strcmp(src, loading) == 0) {
3213 return autoload_const_value;
3217autoload_apply_constants(
VALUE _arguments)
3219 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
3231 ccan_list_for_each_safe(&arguments->autoload_data->constants,
autoload_const, next, cnode) {
3237 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
3243autoload_feature_require_in_box(
VALUE receiver,
VALUE feature)
3245 rb_vm_frame_flag_set_box_require(GET_EC());
3247 return rb_funcall(receiver, rb_intern(
"require"), 1, feature);
3251autoload_feature_require(
VALUE _arguments)
3267 rb_gccct_clear_table();
3269 VALUE feature = arguments->autoload_data->feature;
3271 if (rb_box_available() && BOX_OBJ_P(autoload_box_value)) {
3272 box = rb_get_box_t(autoload_box_value);
3276 if (box && box->top_self) {
3282 result = rb_vm_call_cfunc_in_box(box->top_self, autoload_feature_require_in_box,
3283 box->top_self, feature, feature, box);
3286 result =
rb_funcall(rb_vm_top_self(), rb_intern(
"require"), 1, feature);
3289 if (
RTEST(result)) {
3296autoload_try_load(
VALUE _arguments)
3300 VALUE result = autoload_feature_require(_arguments);
3303 rb_const_entry_t *ce = rb_const_lookup(arguments->module, arguments->name);
3305 if (!ce || UNDEF_P(ce->value)) {
3312 "Expected %"PRIsVALUE
" to define %"PRIsVALUE
" but it didn't",
3313 arguments->autoload_data->feature,
3319 "Expected %"PRIsVALUE
" to define %"PRIsVALUE
"::%"PRIsVALUE
" but it didn't",
3320 arguments->autoload_data->feature,
3328 ce->flag |= arguments->flag;
3340 if (!ce || !UNDEF_P(ce->value)) {
3345 if (UNLIKELY(!rb_ractor_main_p())) {
3346 return rb_ractor_autoload_load(module, name);
3356 if (autoload_const_value ==
Qfalse)
return autoload_const_value;
3358 arguments.flag = ce->flag & (CONST_DEPRECATED | CONST_VISIBILITY_MASK);
3375 return rb_autoload_at_p(mod,
id, TRUE);
3379rb_autoload_at_p(
VALUE mod,
ID id,
int recur)
3384 while (!autoload_defined_p(mod,
id)) {
3385 if (!recur)
return Qnil;
3387 if (!mod)
return Qnil;
3389 load = check_autoload_required(mod,
id, 0);
3390 if (!load)
return Qnil;
3391 return (ele = get_autoload_data(load, 0)) ? ele->feature :
Qnil;
3397 if (RB_CONST_DEPRECATED_P(ce) &&
3410rb_const_get_0(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility)
3413 VALUE c = rb_const_search(klass,
id, exclude, recurse, visibility, &found_in);
3415 if (UNLIKELY(!rb_class_owned_p(found_in))) {
3417 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));
3422 return rb_const_missing(klass,
ID2SYM(
id));
3426rb_const_search_from(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility,
VALUE *found_in)
3428 VALUE value, current;
3429 bool first_iteration =
true;
3431 for (current = klass;
3433 current =
RCLASS_SUPER(current), first_iteration =
false) {
3438 if (!first_iteration && RCLASS_ORIGIN(current) != current) {
3451 while ((ce = rb_const_lookup(tmp,
id))) {
3452 if (visibility && RB_CONST_PRIVATE_P(ce)) {
3453 GET_EC()->private_const_reference = tmp;
3456 rb_const_warn_if_deprecated(ce, tmp,
id);
3458 if (UNDEF_P(value)) {
3460 if (am == tmp)
break;
3462 ac = autoloading_const_entry(tmp,
id);
3464 if (found_in) { *found_in = tmp; }
3473 if (found_in) { *found_in = tmp; }
3476 if (!recurse)
break;
3480 GET_EC()->private_const_reference = 0;
3485rb_const_search(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility,
VALUE *found_in)
3490 value = rb_const_search_from(klass,
id, exclude, recurse, visibility, found_in);
3491 if (!UNDEF_P(value))
return value;
3492 if (exclude)
return value;
3495 return rb_const_search_from(
rb_cObject,
id, FALSE, recurse, visibility, found_in);
3501 return rb_const_get_0(klass,
id, TRUE, TRUE, FALSE);
3507 return rb_const_get_0(klass,
id, FALSE, TRUE, FALSE);
3513 return rb_const_get_0(klass,
id, TRUE, FALSE, FALSE);
3517rb_public_const_get_from(
VALUE klass,
ID id)
3519 return rb_const_get_0(klass,
id, TRUE, TRUE, TRUE);
3523rb_public_const_get_at(
VALUE klass,
ID id)
3525 return rb_const_get_0(klass,
id, TRUE, FALSE, TRUE);
3528NORETURN(
static void undefined_constant(
VALUE mod,
VALUE name));
3532 rb_name_err_raise(
"constant %2$s::%1$s not defined",
3537rb_const_location_from(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility)
3539 while (
RTEST(klass)) {
3542 while ((ce = rb_const_lookup(klass,
id))) {
3543 if (visibility && RB_CONST_PRIVATE_P(ce)) {
3550 if (UNDEF_P(ce->value)) {
3552 if (
RTEST(autoload_const_value)) {
3565 if (!recurse)
break;
3574rb_const_location(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility)
3579 loc = rb_const_location_from(klass,
id, exclude, recurse, visibility);
3580 if (!
NIL_P(loc))
return loc;
3581 if (exclude)
return loc;
3584 return rb_const_location_from(
rb_cObject,
id, FALSE, recurse, visibility);
3588rb_const_source_location(
VALUE klass,
ID id)
3590 return rb_const_location(klass,
id, FALSE, TRUE, FALSE);
3594rb_const_source_location_at(
VALUE klass,
ID id)
3596 return rb_const_location(klass,
id, TRUE, FALSE, FALSE);
3612 const ID id = id_for_var(mod, name, a, constant);
3615 undefined_constant(mod, name);
3628 rb_check_frozen(mod);
3629 rb_class_owner_check(mod);
3631 ce = rb_const_lookup(mod,
id);
3635 rb_name_err_raise(
"cannot remove %2$s::%1$s", mod,
ID2SYM(
id));
3638 undefined_constant(mod,
ID2SYM(
id));
3641 VALUE writable_ce = 0;
3642 if (rb_id_table_lookup(RCLASS_WRITABLE_CONST_TBL(mod),
id, &writable_ce)) {
3643 rb_id_table_delete(RCLASS_WRITABLE_CONST_TBL(mod),
id);
3649 rb_const_warn_if_deprecated(ce, mod,
id);
3655 autoload_delete(mod,
id);
3659 if (ce != const_lookup(RCLASS_PRIME_CONST_TBL(mod),
id)) {
3668cv_i_update(st_data_t *k, st_data_t *v, st_data_t a,
int existing)
3670 if (existing)
return ST_STOP;
3675static enum rb_id_table_iterator_result
3682 st_update(tbl, (st_data_t)key, cv_i_update, (st_data_t)ce);
3684 return ID_TABLE_CONTINUE;
3687static enum rb_id_table_iterator_result
3688rb_local_constants_i(
ID const_name,
VALUE const_value,
void *ary)
3693 return ID_TABLE_CONTINUE;
3697rb_local_constants(
VALUE mod)
3706 rb_id_table_foreach(tbl, rb_local_constants_i, (
void *)ary);
3717 tbl = st_init_numtable();
3719 if (RCLASS_CONST_TBL(mod)) {
3721 rb_id_table_foreach(RCLASS_CONST_TBL(mod), sv_i, tbl);
3741list_i(st_data_t key, st_data_t value,
VALUE ary)
3784 bool inherit =
true;
3792 return rb_local_constants(mod);
3797rb_const_defined_0(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility)
3806 if ((ce = rb_const_lookup(tmp,
id))) {
3807 if (visibility && RB_CONST_PRIVATE_P(ce)) {
3810 if (UNDEF_P(ce->value) && !check_autoload_required(tmp,
id, 0) &&
3811 !rb_autoloading_value(tmp,
id, NULL, NULL))
3820 if (!recurse)
break;
3834 return rb_const_defined_0(klass,
id, TRUE, TRUE, FALSE);
3840 return rb_const_defined_0(klass,
id, FALSE, TRUE, FALSE);
3846 return rb_const_defined_0(klass,
id, TRUE, FALSE, FALSE);
3850rb_public_const_defined_from(
VALUE klass,
ID id)
3852 return rb_const_defined_0(klass,
id, TRUE, TRUE, TRUE);
3856check_before_mod_set(
VALUE klass,
ID id,
VALUE val,
const char *dest)
3858 rb_check_frozen(klass);
3861static void set_namespace_path(
VALUE named_namespace,
VALUE name);
3863static enum rb_id_table_iterator_result
3864set_namespace_path_i(
ID id,
VALUE v,
void *payload)
3867 VALUE value = ce->value;
3870 return ID_TABLE_CONTINUE;
3873 bool has_permanent_classpath;
3874 classname(value, &has_permanent_classpath);
3875 if (has_permanent_classpath) {
3876 return ID_TABLE_CONTINUE;
3878 set_namespace_path(value, build_const_path(parental_path,
id));
3880 if (!RCLASS_PERMANENT_CLASSPATH_P(value)) {
3881 RCLASS_WRITE_CLASSPATH(value, 0,
false);
3884 return ID_TABLE_CONTINUE;
3893set_namespace_path(
VALUE named_namespace,
VALUE namespace_path)
3895 struct rb_id_table *const_table = RCLASS_CONST_TBL(named_namespace);
3899 RCLASS_WRITE_CLASSPATH(named_namespace, namespace_path,
true);
3902 rb_id_table_foreach(const_table, set_namespace_path_i, &namespace_path);
3908const_added(
VALUE klass,
ID const_name)
3910 if (GET_VM()->running) {
3912 rb_funcallv_uncached(klass, idConst_added, 1, &arg);
3922 rb_raise(
rb_eTypeError,
"no class/module to define constant %"PRIsVALUE
"",
3926 if (UNLIKELY(!rb_class_owned_p(klass))) {
3927 rb_raise(rb_eRactorIsolationError,
"can not set constants of classes/modules created by another Ractor");
3930 check_before_mod_set(klass,
id, val,
"constant");
3933 struct rb_id_table *tbl = RCLASS_WRITABLE_CONST_TBL(klass);
3935 tbl = rb_id_table_create(0);
3936 RCLASS_WRITE_CONST_TBL(klass, tbl,
false);
3939 rb_id_table_insert(tbl,
id, (
VALUE)ce);
3940 setup_const_entry(ce, klass, val, CONST_PUBLIC);
3944 .module = klass, .name = id,
3945 .value = val, .flag = CONST_PUBLIC,
3948 ac.file = rb_source_location(&ac.line);
3949 const_tbl_update(&ac,
false);
3958 bool val_path_permanent;
3959 VALUE val_path = classname(val, &val_path_permanent);
3960 if (
NIL_P(val_path) || !val_path_permanent) {
3962 set_namespace_path(val, rb_id2str(
id));
3965 bool parental_path_permanent;
3966 VALUE parental_path = classname(klass, &parental_path_permanent);
3967 if (
NIL_P(parental_path)) {
3969 parental_path = rb_tmp_class_path(klass, &throwaway, make_temporary_path);
3971 if (parental_path_permanent && !val_path_permanent) {
3972 set_namespace_path(val, build_const_path(parental_path,
id));
3974 else if (!parental_path_permanent &&
NIL_P(val_path)) {
3975 VALUE path = build_const_path(parental_path,
id);
3976 RCLASS_SET_CLASSPATH(val, path,
false);
3986 const_set(klass,
id, val);
3987 const_added(klass,
id);
3991autoload_const_value_for_named_constant(
VALUE module,
ID name,
struct autoload_const **autoload_const_pointer)
3994 if (!autoload_const_value)
return Qfalse;
4001 return autoload_const_value;
4011 VALUE klass = ac->module;
4012 VALUE val = ac->value;
4014 struct rb_id_table *tbl = RCLASS_CONST_TBL(klass);
4015 rb_const_flag_t visibility = ac->flag;
4018 if (rb_id_table_lookup(tbl,
id, &value)) {
4020 if (UNDEF_P(ce->value)) {
4021 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
4022 VALUE file = ac->file;
4023 int line = ac->line;
4024 VALUE autoload_const_value = autoload_const_value_for_named_constant(klass,
id, &ac);
4026 if (!autoload_force && autoload_const_value) {
4030 RB_OBJ_WRITE(autoload_const_value, &ac->file, rb_source_location(&ac->line));
4034 autoload_delete(klass,
id);
4035 ce->flag = visibility;
4040 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
4044 VALUE name = QUOTE_ID(
id);
4045 visibility = ce->flag;
4048 if (!
NIL_P(ce->file) && ce->line) {
4049 previous = rb_sprintf(
"\n%"PRIsVALUE
":%d: warning: previous definition of %"PRIsVALUE
" was here", ce->file, ce->line, name);
4053 rb_warn(
"already initialized constant %"PRIsVALUE
"%"PRIsVALUE
"", name, previous);
4055 rb_warn(
"already initialized constant %"PRIsVALUE
"::%"PRIsVALUE
"%"PRIsVALUE
"",
4059 setup_const_entry(ce, klass, val, visibility);
4062 tbl = RCLASS_WRITABLE_CONST_TBL(klass);
4066 rb_id_table_insert(tbl,
id, (
VALUE)ce);
4067 setup_const_entry(ce, klass, val, visibility);
4073 rb_const_flag_t visibility)
4075 ce->flag = visibility;
4077 RB_OBJ_WRITE(klass, &ce->file, rb_source_location(&ce->line));
4083 ID id = rb_intern(name);
4086 rb_warn(
"rb_define_const: invalid name '%s' for constant", name);
4089 rb_vm_register_global_object(val);
4101set_const_visibility(
VALUE mod,
int argc,
const VALUE *argv,
4102 rb_const_flag_t flag, rb_const_flag_t mask)
4110 rb_warning(
"%"PRIsVALUE
" with no argument is just ignored",
4111 QUOTE_ID(rb_frame_callee()));
4115 for (i = 0; i < argc; i++) {
4117 VALUE val = argv[i];
4120 undefined_constant(mod, val);
4122 if ((ce = rb_const_lookup(mod,
id))) {
4125 if (UNDEF_P(ce->value)) {
4126 if (autoload_const_value_for_named_constant(mod,
id, &ac)) {
4134 undefined_constant(mod,
ID2SYM(
id));
4144 long len = strlen(name);
4148 undefined_constant(mod, rb_fstring_new(name,
len));
4150 if (!(ce = rb_const_lookup(mod,
id))) {
4151 undefined_constant(mod,
ID2SYM(
id));
4153 ce->flag |= CONST_DEPRECATED;
4164rb_mod_private_constant(
int argc,
const VALUE *argv,
VALUE obj)
4166 set_const_visibility(obj, argc, argv, CONST_PRIVATE, CONST_VISIBILITY_MASK);
4178rb_mod_public_constant(
int argc,
const VALUE *argv,
VALUE obj)
4180 set_const_visibility(obj, argc, argv, CONST_PUBLIC, CONST_VISIBILITY_MASK);
4204rb_mod_deprecate_constant(
int argc,
const VALUE *argv,
VALUE obj)
4206 set_const_visibility(obj, argc, argv, CONST_DEPRECATED, CONST_DEPRECATED);
4211original_module(
VALUE c)
4219cvar_lookup_at(
VALUE klass,
ID id, st_data_t *v)
4222 if (RICLASS_IS_ORIGIN_P(klass)) {
4227 klass =
RBASIC(klass)->klass;
4232 if (UNDEF_P(n))
return 0;
4239cvar_front_klass(
VALUE klass)
4241 if (RCLASS_SINGLETON_P(klass)) {
4242 VALUE obj = RCLASS_ATTACHED_OBJECT(klass);
4243 if (rb_namespace_p(obj)) {
4253 if (front && target != front) {
4254 if (original_module(front) != original_module(target)) {
4256 "class variable % "PRIsVALUE
" of %"PRIsVALUE
" is overtaken by %"PRIsVALUE
"",
4262 rb_ivar_delete(front,
id,
Qundef);
4267#define CVAR_FOREACH_ANCESTORS(klass, v, r) \
4268 for (klass = cvar_front_klass(klass); klass; klass = RCLASS_SUPER(klass)) { \
4269 if (cvar_lookup_at(klass, id, (v))) { \
4274#define CVAR_LOOKUP(v,r) do {\
4275 if (cvar_lookup_at(klass, id, (v))) {r;}\
4276 CVAR_FOREACH_ANCESTORS(klass, v, r);\
4296 VALUE tmp, front = 0, target = 0;
4299 CVAR_LOOKUP(0, {
if (!front) front = klass; target = klass;});
4301 cvar_overtaken(front, target,
id);
4308 target =
RBASIC(target)->klass;
4310 cvar_set_ractor_check(target,
id);
4311 check_before_mod_set(target,
id, val,
"class variable");
4313 bool new_cvar = rb_class_ivar_set(target,
id, val);
4315 VALUE cvc_tbl = RCLASS_WRITABLE_CVC_TBL(target);
4320 if (!cvc_tbl || !rb_marked_id_table_lookup(cvc_tbl,
id, &ent_data)) {
4324 ent->global_cvar_state = GET_GLOBAL_CVAR_STATE();
4326 VALUE new_cvc_tbl = cvc_tbl;
4328 new_cvc_tbl = rb_marked_id_table_new(2);
4330 else if (rb_multi_ractor_p()) {
4331 new_cvc_tbl = rb_marked_id_table_dup(cvc_tbl);
4334 rb_marked_id_table_insert(new_cvc_tbl,
id, (
VALUE)ent);
4335 if (new_cvc_tbl != cvc_tbl) {
4336 RCLASS_WRITE_CVC_TBL(target, new_cvc_tbl);
4338 RB_DEBUG_COUNTER_INC(cvar_inline_miss);
4341 ent = (
void *)ent_data;
4342 ent->global_cvar_state = GET_GLOBAL_CVAR_STATE();
4349 ruby_vm_global_cvar_state++;
4359 value = find_cvar(klass, front, &target,
id);
4361 rb_name_err_raise(
"uninitialized class variable %1$s in %2$s",
4364 cvar_overtaken(*front, target,
id);
4366 target =
RBASIC(target)->klass;
4368 cvar_read_ractor_check(target,
id, value);
4369 return (
VALUE)value;
4382 if (!klass)
return Qfalse;
4383 CVAR_LOOKUP(0,
return Qtrue);
4388cv_intern(
VALUE klass,
const char *name)
4390 ID id = rb_intern(name);
4392 rb_name_err_raise(
"wrong class variable name %1$s",
4401 ID id = cv_intern(klass, name);
4408 ID id = cv_intern(klass, name);
4419cv_i(
ID key,
VALUE v, st_data_t a)
4424 st_update(tbl, (st_data_t)key, cv_i_update, 0);
4430mod_cvar_at(
VALUE mod,
void *data)
4434 tbl = st_init_numtable();
4436 mod = original_module(mod);
4443mod_cvar_of(
VALUE mod,
void *data)
4446 if (RCLASS_SINGLETON_P(mod)) {
4447 if (rb_namespace_p(RCLASS_ATTACHED_OBJECT(mod))) {
4448 data = mod_cvar_at(tmp, data);
4449 tmp = cvar_front_klass(tmp);
4453 data = mod_cvar_at(tmp, data);
4461cv_list_i(st_data_t key, st_data_t value,
VALUE ary)
4469cvar_list(
void *data)
4505 bool inherit =
true;
4510 tbl = mod_cvar_of(mod, 0);
4513 tbl = mod_cvar_at(mod, 0);
4515 return cvar_list(tbl);
4540 const ID id = id_for_var_message(mod, name,
class,
"wrong class variable name %1$s");
4546 rb_check_frozen(mod);
4547 cvar_set_ractor_check(mod,
id);
4548 val = rb_ivar_delete(mod,
id,
Qundef);
4549 if (!UNDEF_P(val)) {
4553 rb_name_err_raise(
"cannot remove %1$s for %2$s", mod,
ID2SYM(
id));
4556 rb_name_err_raise(
"class variable %1$s not defined for %2$s",
4575 ID id = rb_intern(name);
4581class_fields_ivar_set(
VALUE klass,
VALUE fields_obj,
ID id,
VALUE val,
bool concurrent,
VALUE *new_fields_obj,
bool *new_ivar_out)
4583 const VALUE original_fields_obj = fields_obj;
4584 fields_obj = original_fields_obj ? original_fields_obj : rb_imemo_fields_new(klass, ROOT_SHAPE_ID,
true);
4586 shape_id_t current_shape_id = RBASIC_SHAPE_ID(fields_obj);
4587 shape_id_t next_shape_id = current_shape_id;
4588 if (UNLIKELY(rb_shape_complex_p(current_shape_id))) {
4593 next_shape_id = generic_shape_ivar(fields_obj,
id, &new_ivar);
4595 if (UNLIKELY(rb_shape_complex_p(next_shape_id))) {
4596 fields_obj = imemo_fields_evacutate_to_complex(klass, fields_obj, next_shape_id, 1);
4600 attr_index_t index = RSHAPE_INDEX(next_shape_id);
4601 if (new_ivar && index >= RSHAPE_CAPACITY(current_shape_id)) {
4604 fields_obj = imemo_fields_copy_append(klass, fields_obj, current_shape_id, next_shape_id, val);
4607 VALUE *fields = rb_imemo_fields_ptr(fields_obj);
4609 if (concurrent && original_fields_obj == fields_obj) {
4615 RB_OBJ_ATOMIC_WRITE(fields_obj, &fields[index], val);
4622 RUBY_ASSERT(rb_shape_layout(next_shape_id) == SHAPE_ID_LAYOUT_ROBJECT);
4623 RBASIC_SET_SHAPE_ID(fields_obj, next_shape_id);
4627 *new_fields_obj = fields_obj;
4628 *new_ivar_out = new_ivar;
4633 if (concurrent && fields_obj == original_fields_obj) {
4637 fields_obj = rb_imemo_fields_clone(fields_obj);
4640 st_table *table = rb_imemo_fields_complex_tbl(fields_obj);
4641 new_ivar = !st_insert(table, (st_data_t)
id, (st_data_t)val);
4644 if (fields_obj != original_fields_obj) {
4645 RUBY_ASSERT(rb_shape_layout(next_shape_id) == SHAPE_ID_LAYOUT_ROBJECT);
4646 RBASIC_SET_SHAPE_ID(fields_obj, next_shape_id);
4650 *new_fields_obj = fields_obj;
4651 *new_ivar_out = new_ivar;
4652 return ATTR_INDEX_NOT_SET;
4656class_ivar_set(
VALUE obj,
ID id,
VALUE val,
bool *new_ivar)
4658 rb_class_ensure_writable(obj);
4660 const VALUE original_fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
4661 VALUE new_fields_obj = 0;
4663 attr_index_t index = class_fields_ivar_set(obj, original_fields_obj,
id, val, rb_multi_ractor_p(), &new_fields_obj, new_ivar);
4665 if (new_fields_obj != original_fields_obj) {
4666 RCLASS_WRITABLE_SET_FIELDS_OBJ(obj, new_fields_obj);
4672 RBASIC_SET_SHAPE_ID(obj, RBASIC_SHAPE_ID(new_fields_obj));
4680 rb_check_frozen(obj);
4683 class_ivar_set(obj,
id, val, &new_ivar);
4692 RUBY_ASSERT(RSHAPE_TYPE_P(RBASIC_SHAPE_ID(dst), SHAPE_ROOT));
4694 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(src);
4696 VALUE dst_fields_obj = rb_imemo_fields_clone(fields_obj);
4699 shape_id_t shape_id = RBASIC_SHAPE_ID(dst_fields_obj) & ~SHAPE_ID_FL_FROZEN;
4700 RBASIC_SET_SHAPE_ID(dst_fields_obj, shape_id);
4701 RCLASS_WRITABLE_SET_FIELDS_OBJ(dst, dst_fields_obj);
4702 RBASIC_SET_SHAPE_ID(dst, shape_id);
4713 r = rb_id_table_lookup(tbl,
id, &val);
4722rb_const_lookup(
VALUE klass,
ID id)
4724 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 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.
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_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(void)
Allocates a new, empty array.
VALUE rb_ary_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_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.
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.
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 RTYPEDDATA_DATA(v)
Convenient getter macro.
#define RUBY_TYPED_FREE_IMMEDIATELY
Macros to see if each corresponding flag is defined.
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
#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.
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.