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 "internal/hash.h"
29#include "internal/object.h"
30#include "internal/gc.h"
31#include "internal/re.h"
32#include "internal/struct.h"
33#include "internal/symbol.h"
34#include "internal/thread.h"
35#include "internal/variable.h"
43#include "ractor_core.h"
47#define GET_GLOBAL_CVAR_STATE() (ruby_vm_global_cvar_state)
49typedef void rb_gvar_compact_t(
void *var);
57static VALUE autoload_features;
62static VALUE autoload_mutex;
64static void check_before_mod_set(
VALUE,
ID,
VALUE,
const char *);
66static VALUE rb_const_search(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility,
VALUE *found_in);
69typedef int rb_ivar_foreach_callback_func(
ID key,
VALUE val, st_data_t arg);
70static void rb_field_foreach(
VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg,
bool ivar_only);
75 rb_global_tbl = rb_id_table_create(0);
76 generic_fields_tbl_ = st_init_numtable();
81 rb_vm_register_global_object(autoload_mutex);
83 autoload_features = rb_ident_hash_new();
85 rb_vm_register_global_object(autoload_features);
89rb_namespace_p(
VALUE obj)
110classname(
VALUE klass,
bool *permanent)
114 VALUE classpath = RCLASS_CLASSPATH(klass);
115 if (classpath == 0)
return Qnil;
117 *permanent = RCLASS_PERMANENT_CLASSPATH_P(klass);
123rb_mod_name0(
VALUE klass,
bool *permanent)
125 return classname(klass, permanent);
140 return classname(mod, &permanent);
145is_constant_path(
VALUE name)
147 const char *path = RSTRING_PTR(name);
151 const char *p = path;
153 if (p >= pend || !*p) {
158 if (p + 2 <= pend && p[0] ==
':' && p[1] ==
':') {
162 const char *pbeg = p;
163 while (p < pend && *p !=
':') p++;
165 if (pbeg == p)
return false;
167 if (rb_enc_symname_type(pbeg, p - pbeg, enc, 0) != ID_CONST) {
184set_sub_temporary_name_recursive(
VALUE mod,
VALUE data,
int recursive)
186 if (recursive)
return Qfalse;
191 name = build_const_path(rb_ary_last(0, 0, args->names), args->last);
193 set_sub_temporary_name_foreach(mod, args, name);
198set_sub_temporary_name_topmost(
VALUE mod,
VALUE data,
int recursive)
200 if (recursive)
return Qfalse;
203 VALUE name = args->names;
207 set_sub_temporary_name_foreach(mod, args, name);
211static enum rb_id_table_iterator_result
212set_sub_temporary_name_i(
ID id,
VALUE val,
void *data)
215 if (rb_namespace_p(val) && !RCLASS_PERMANENT_CLASSPATH_P(val)) {
221 return ID_TABLE_CONTINUE;
227 RCLASS_WRITE_CLASSPATH(mod, name, FALSE);
231 rb_id_table_foreach(tbl, set_sub_temporary_name_i, args);
236 rb_id_table_foreach(tbl, set_sub_temporary_name_i, args);
237 rb_ary_set_len(args->names, names_len);
298rb_mod_set_temporary_name(
VALUE mod,
VALUE name)
301 if (RCLASS_PERMANENT_CLASSPATH_P(mod)) {
308 set_sub_temporary_name(mod, 0);
315 if (RSTRING_LEN(name) == 0) {
316 rb_raise(rb_eArgError,
"empty class/module name");
319 if (is_constant_path(name)) {
320 rb_raise(rb_eArgError,
"the temporary name must not be a constant path to avoid confusion");
324 RB_OBJ_SET_SHAREABLE(name);
328 set_sub_temporary_name(mod, name);
341 path = rb_sprintf(
"#<Class:%p>", (
void*)obj);
344 path = rb_sprintf(
"#<Module:%p>", (
void*)obj);
347 path = rb_sprintf(
"#<%"PRIsVALUE
":%p>", klass, (
void*)obj);
357rb_tmp_class_path(
VALUE klass,
bool *permanent, fallback_func fallback)
359 VALUE path = classname(klass, permanent);
371 path = rb_tmp_class_path(
RBASIC(klass)->klass, &perm, fallback);
376 return fallback(klass, path);
383 VALUE path = rb_tmp_class_path(klass, &permanent, make_temporary_path);
401rb_search_class_path(
VALUE klass)
404 return rb_tmp_class_path(klass, &permanent, no_fallback);
413 return rb_fstring(path);
417build_const_path(
VALUE head,
ID tail)
419 return build_const_pathname(head, rb_id2str(tail));
425 bool permanent =
true;
432 str = rb_tmp_class_path(under, &permanent, make_temporary_path);
433 str = build_const_pathname(str, name);
436 RB_OBJ_SET_SHAREABLE(str);
437 RCLASS_SET_CLASSPATH(klass, str, permanent);
452 const char *pbeg, *pend, *p, *path = RSTRING_PTR(pathname);
456 if (!rb_enc_asciicompat(enc)) {
457 rb_raise(rb_eArgError,
"invalid class path encoding (non ASCII)");
460 pend = path + RSTRING_LEN(pathname);
461 if (path == pend || path[0] ==
'#') {
462 rb_raise(rb_eArgError,
"can't retrieve anonymous class %"PRIsVALUE,
466 while (p < pend && *p !=
':') p++;
468 if (p < pend && p[0] ==
':') {
469 if ((
size_t)(pend - p) < 2 || p[1] !=
':')
goto undefined_class;
474 goto undefined_class;
476 c = rb_const_search(c,
id, TRUE, FALSE, FALSE, NULL);
477 if (UNDEF_P(c))
goto undefined_class;
478 if (!rb_namespace_p(c)) {
479 rb_raise(
rb_eTypeError,
"%"PRIsVALUE
" does not refer to class/module",
488 rb_raise(rb_eArgError,
"undefined class/module % "PRIsVALUE,
509 VALUE path = rb_tmp_class_path(
rb_class_real(klass), &permanent, make_temporary_path);
510 if (
NIL_P(path))
return NULL;
511 return RSTRING_PTR(path);
534 rb_gvar_compact_t *compactor;
559static enum rb_id_table_iterator_result
560free_global_entry_i(
VALUE val,
void *arg)
563 entry->var->counter--;
564 if (entry->var->counter == 0) {
565 free_global_variable(entry->var);
568 return ID_TABLE_DELETE;
572rb_free_rb_global_tbl(
void)
574 rb_id_table_foreach_values(rb_global_tbl, free_global_entry_i, 0);
575 rb_id_table_free(rb_global_tbl);
579rb_free_generic_fields_tbl_(
void)
581 st_free_table(generic_fields_tbl_);
585rb_find_global_entry(
ID id)
591 if (!rb_id_table_lookup(rb_global_tbl,
id, &data)) {
600 if (UNLIKELY(!rb_ractor_main_p()) && (!entry || !entry->ractor_local)) {
601 rb_raise(rb_eRactorIsolationError,
"can not access global variable %s from non-main Ractor", rb_id2name(
id));
608rb_gvar_ractor_local(
const char *name)
611 entry->ractor_local =
true;
615rb_gvar_box_ready(
const char *name)
618 entry->var->box_ready =
true;
622rb_gvar_undef_compactor(
void *var)
631 entry = rb_find_global_entry(
id);
638 entry->ractor_local =
false;
644 var->compactor = rb_gvar_undef_compactor;
646 var->block_trace = 0;
648 var->box_ready =
false;
649 rb_id_table_insert(rb_global_tbl,
id, (
VALUE)entry);
658 rb_warning(
"global variable '%"PRIsVALUE
"' not initialized", QUOTE_ID(
id));
664rb_gvar_val_compactor(
void *_var)
671 VALUE new = rb_gc_location(obj);
673 var->data = (
void*)
new;
685 var->compactor = rb_gvar_val_compactor;
687 var->data = (
void*)val;
705 var->data = (
void*)val;
712 if (data) rb_gc_mark_movable(data);
718 if (!var)
return Qnil;
731 if (var) rb_gc_mark_maybe(*var);
737 rb_name_error(
id,
"%"PRIsVALUE
" is a read-only variable", QUOTE_ID(
id));
740static enum rb_id_table_iterator_result
741mark_global_entry(
VALUE v,
void *ignored)
747 (*var->marker)(var->data);
750 if (trace->data) rb_gc_mark_maybe(trace->data);
753 return ID_TABLE_CONTINUE;
756#define gc_mark_table(task) \
757 if (rb_global_tbl) { rb_id_table_foreach_values(rb_global_tbl, task##_global_entry, 0); }
760rb_gc_mark_global_tbl(
void)
765static enum rb_id_table_iterator_result
766update_global_entry(
VALUE v,
void *ignored)
771 (*var->compactor)(var);
772 return ID_TABLE_CONTINUE;
776rb_gc_update_global_tbl(
void)
778 gc_mark_table(update);
782global_id(
const char *name)
786 if (name[0] ==
'$')
id = rb_intern(name);
788 size_t len = strlen(name);
792 memcpy(buf+1, name,
len);
793 id = rb_intern2(buf,
len+1);
800find_global_id(
const char *name)
803 size_t len = strlen(name);
805 if (name[0] ==
'$') {
812 memcpy(buf+1, name,
len);
828 ID id = global_id(name);
831 gvar->data = (
void*)var;
883 trace->next = entry->var->trace;
884 trace->func = rb_trace_eval;
887 entry->var->trace = trace;
901 while (trace->next) {
904 trace->next = next->next;
927 if ((entry = rb_find_global_entry(
id)) == NULL) {
928 rb_name_error(
id,
"undefined global variable %"PRIsVALUE
"", QUOTE_ID(
id));
931 trace = entry->var->trace;
942 if (!entry->var->block_trace) remove_trace(entry->var);
947 if (trace->data == cmd) {
949 if (!entry->var->block_trace) remove_trace(entry->var);
970 (*trace->func)(trace->data, data->val);
981 var->block_trace = 0;
992 (*var->setter)(val, entry->id, var->data);
994 if (var->trace && !var->block_trace) {
995 var->block_trace = 1;
996 trace.trace = var->trace;
1003#define USE_BOX_GVAR_TBL(ns,entry) \
1004 (BOX_USER_P(ns) && \
1005 (!entry || !entry->var->box_ready || entry->var->setter != rb_gvar_readonly_setter))
1012 const rb_box_t *box = rb_current_box();
1013 bool use_box_tbl =
false;
1018 if (USE_BOX_GVAR_TBL(box, entry)) {
1020 rb_hash_aset(box->gvar_tbl,
rb_id2sym(entry->id), val);
1027 retval = rb_gvar_set_entry(entry, val);
1035 return rb_gvar_set(global_id(name), val);
1041 VALUE retval, gvars, key;
1042 const rb_box_t *box = rb_current_box();
1043 bool use_box_tbl =
false;
1051 if (USE_BOX_GVAR_TBL(box, entry)) {
1053 gvars = box->gvar_tbl;
1055 if (
RTEST(rb_hash_has_key(gvars, key))) {
1056 retval = rb_hash_aref(gvars, key);
1061 retval = (*var->getter)(entry->id, var->data);
1063 retval =
rb_funcall(retval, rb_intern(
"clone"), 0);
1067 rb_hash_aset(gvars, key, retval);
1072 retval = (*var->getter)(entry->id, var->data);
1080 ID id = find_global_id(name);
1083 rb_warning(
"global variable '%s' not initialized", name);
1087 return rb_gvar_get(
id);
1091rb_gvar_defined(
ID id)
1098rb_gvar_getter_function_of(
ID id)
1101 return entry->var->getter;
1105rb_gvar_setter_function_of(
ID id)
1108 return entry->var->setter;
1111static enum rb_id_table_iterator_result
1112gvar_i(
ID key,
VALUE val,
void *a)
1116 return ID_TABLE_CONTINUE;
1125 if (!rb_ractor_main_p()) {
1126 rb_raise(rb_eRactorIsolationError,
"can not access global variables from non-main Ractors");
1130 rb_id_table_foreach(rb_global_tbl, gvar_i, (
void *)ary);
1131 if (!
NIL_P(backref)) {
1133 int i, nmatch = rb_match_count(backref);
1135 for (i = 1; i <= nmatch; ++i) {
1139 buf[1] = (char)(i +
'0');
1140 sym =
ID2SYM(rb_intern2(buf, 2));
1159 if (!rb_ractor_main_p()) {
1160 rb_raise(rb_eRactorIsolationError,
"can not access global variables from non-main Ractors");
1165 if (!rb_id_table_lookup(gtbl, name1, &data1)) {
1168 rb_id_table_insert(gtbl, name1, (
VALUE)entry1);
1170 else if ((entry1 = (
struct rb_global_entry *)data1)->var != entry2->var) {
1172 if (var->block_trace) {
1177 if (var->counter == 0) {
1178 free_global_variable(var);
1181 if (entry1->var != entry2->var) {
1182 entry2->var->counter++;
1183 entry1->var = entry2->var;
1189IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(
ID id)
1191 if (UNLIKELY(!rb_ractor_main_p())) {
1193 rb_raise(rb_eRactorIsolationError,
"can not set instance variables of classes/modules by non-main Ractors");
1199CVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(
VALUE klass,
ID id)
1201 if (UNLIKELY(!rb_ractor_main_p())) {
1202 rb_raise(rb_eRactorIsolationError,
"can not access class variables from non-main Ractors (%"PRIsVALUE
" from %"PRIsVALUE
")", rb_id2str(
id), klass);
1207ivar_ractor_check(
VALUE obj,
ID id)
1211 UNLIKELY(!rb_ractor_main_p()) &&
1214 rb_raise(rb_eRactorIsolationError,
"can not access instance variables of shareable objects from non-main Ractors");
1219generic_fields_tbl_no_ractor_check(
void)
1221 ASSERT_vm_locking();
1223 return generic_fields_tbl_;
1227rb_generic_fields_tbl_get(
void)
1229 return generic_fields_tbl_;
1233rb_mark_generic_ivar(
VALUE obj)
1237 if (st_lookup(generic_fields_tbl_, (st_data_t)obj, (st_data_t *)&data)) {
1238 rb_gc_mark_movable(data);
1243rb_obj_fields_generic_uncached(
VALUE obj)
1245 VALUE fields_obj = 0;
1247 if (!st_lookup(generic_fields_tbl_, (st_data_t)obj, (st_data_t *)&fields_obj)) {
1248 rb_bug(
"Object is missing entry in generic_fields_tbl");
1255rb_obj_fields(
VALUE obj,
ID field_name)
1258 ivar_ractor_check(obj, field_name);
1260 VALUE fields_obj = 0;
1261 if (rb_shape_obj_has_fields(obj)) {
1268 goto generic_fields;
1270 if (LIKELY(!
FL_TEST_RAW(obj, RSTRUCT_GEN_FIELDS))) {
1271 fields_obj = RSTRUCT_FIELDS_OBJ(obj);
1274 goto generic_fields;
1279 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) {
1280 fields_obj = ec->gen_fields_cache.fields_obj;
1281 RUBY_ASSERT(fields_obj == rb_obj_fields_generic_uncached(obj));
1284 fields_obj = rb_obj_fields_generic_uncached(obj);
1285 ec->gen_fields_cache.fields_obj = fields_obj;
1286 ec->gen_fields_cache.obj = obj;
1297 if (rb_obj_gen_fields_p(obj)) {
1298 st_data_t key = (st_data_t)obj, value;
1305 goto generic_fields;
1307 if (LIKELY(!
FL_TEST_RAW(obj, RSTRUCT_GEN_FIELDS))) {
1308 RSTRUCT_SET_FIELDS_OBJ(obj, 0);
1311 goto generic_fields;
1318 if (ec->gen_fields_cache.obj == obj) {
1319 ec->gen_fields_cache.obj =
Qundef;
1320 ec->gen_fields_cache.fields_obj =
Qundef;
1323 if (!st_delete(generic_fields_tbl_no_ractor_check(), &key, &value)) {
1324 rb_bug(
"Object is missing entry in generic_fields_tbl");
1329 RBASIC_SET_SHAPE_ID(obj, ROOT_SHAPE_ID);
1334rb_obj_set_fields(
VALUE obj,
VALUE fields_obj,
ID field_name,
VALUE original_fields_obj)
1336 ivar_ractor_check(obj, field_name);
1341 rb_imemo_fields_clear(original_fields_obj);
1345 RUBY_ASSERT(IMEMO_TYPE_P(fields_obj, imemo_fields));
1346 RUBY_ASSERT(!original_fields_obj || IMEMO_TYPE_P(original_fields_obj, imemo_fields));
1348 if (fields_obj != original_fields_obj) {
1355 goto generic_fields;
1357 if (LIKELY(!
FL_TEST_RAW(obj, RSTRUCT_GEN_FIELDS))) {
1358 RSTRUCT_SET_FIELDS_OBJ(obj, fields_obj);
1361 goto generic_fields;
1366 st_insert(generic_fields_tbl_, (st_data_t)obj, (st_data_t)fields_obj);
1371 if (ec->gen_fields_cache.fields_obj != fields_obj) {
1372 ec->gen_fields_cache.obj = obj;
1373 ec->gen_fields_cache.fields_obj = fields_obj;
1378 if (original_fields_obj) {
1380 rb_imemo_fields_clear(original_fields_obj);
1384 RBASIC_SET_SHAPE_ID(obj, RBASIC_SHAPE_ID(fields_obj));
1388rb_obj_replace_fields(
VALUE obj,
VALUE fields_obj)
1391 VALUE original_fields_obj = rb_obj_fields_no_ractor_check(obj);
1392 rb_obj_set_fields(obj, fields_obj, 0, original_fields_obj);
1397rb_obj_field_get(
VALUE obj, shape_id_t target_shape_id)
1400 RUBY_ASSERT(RSHAPE_TYPE_P(target_shape_id, SHAPE_IVAR) || RSHAPE_TYPE_P(target_shape_id, SHAPE_OBJ_ID));
1407 fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
1417 fields_obj = rb_obj_fields(obj, RSHAPE_EDGE_NAME(target_shape_id));
1421 if (UNLIKELY(rb_shape_too_complex_p(target_shape_id))) {
1422 st_table *fields_hash = rb_imemo_fields_complex_tbl(fields_obj);
1424 st_lookup(fields_hash, RSHAPE_EDGE_NAME(target_shape_id), &value);
1429 attr_index_t index = RSHAPE_INDEX(target_shape_id);
1430 return rb_imemo_fields_ptr(fields_obj)[index];
1444 VALUE val = rb_ivar_lookup(RCLASS_WRITABLE_FIELDS_OBJ(obj),
id, undef);
1447 UNLIKELY(!rb_ractor_main_p()) &&
1449 rb_raise(rb_eRactorIsolationError,
1450 "can not get unshareable values from instance variables of classes/modules from non-main Ractors (%"PRIsVALUE
" from %"PRIsVALUE
")",
1451 rb_id2str(
id), obj);
1464 fields_obj = rb_obj_fields(obj,
id);
1472 shape_id_t shape_id = RBASIC_SHAPE_ID(fields_obj);
1474 if (UNLIKELY(rb_shape_too_complex_p(shape_id))) {
1475 st_table *iv_table = rb_imemo_fields_complex_tbl(fields_obj);
1477 if (rb_st_lookup(iv_table, (st_data_t)
id, (st_data_t *)&val)) {
1483 attr_index_t index = 0;
1484 if (rb_shape_get_iv_index(shape_id,
id, &index)) {
1485 return rb_imemo_fields_ptr(fields_obj)[index];
1494 VALUE iv = rb_ivar_lookup(obj,
id,
Qnil);
1495 RB_DEBUG_COUNTER_INC(ivar_get_base);
1500rb_ivar_get_at(
VALUE obj, attr_index_t index,
ID id)
1511 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
1512 VALUE val = rb_imemo_fields_ptr(fields_obj)[index];
1515 rb_raise(rb_eRactorIsolationError,
1516 "can not get unshareable values from instance variables of classes/modules from non-main Ractors");
1523 VALUE fields_obj = rb_obj_fields(obj,
id);
1524 return rb_imemo_fields_ptr(fields_obj)[index];
1530rb_ivar_get_at_no_ractor_check(
VALUE obj, attr_index_t index)
1540 fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
1543 fields_obj = rb_obj_fields_no_ractor_check(obj);
1546 return rb_imemo_fields_ptr(fields_obj)[index];
1552 return rb_ivar_lookup(obj,
id,
Qnil);
1555void rb_obj_copy_fields_to_hash_table(
VALUE obj,
st_table *table);
1556static VALUE imemo_fields_complex_from_obj(
VALUE owner,
VALUE source_fields_obj, shape_id_t shape_id);
1562 shape_id_t shape_id = rb_shape_transition_complex(obj);
1567 VALUE *old_fields = NULL;
1574 RBASIC_SET_SHAPE_ID(obj, shape_id);
1575 ROBJECT_SET_FIELDS_HASH(obj, table);
1589 RBASIC_SET_SHAPE_ID(fields_obj, shape_id);
1590 rb_obj_replace_fields(obj, fields_obj);
1599rb_evict_fields_to_hash(
VALUE obj)
1603 st_table *table = st_init_numtable_with_size(RSHAPE_LEN(RBASIC_SHAPE_ID(obj)));
1604 rb_obj_copy_fields_to_hash_table(obj, table);
1605 shape_id_t new_shape_id = obj_transition_too_complex(obj, table);
1608 return new_shape_id;
1612rb_evict_ivars_to_hash(
VALUE obj)
1619 rb_obj_copy_ivs_to_hash_table(obj, table);
1620 obj_transition_too_complex(obj, table);
1628 rb_check_frozen(obj);
1632 bool concurrent =
false;
1638 IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(
id);
1640 fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
1641 if (rb_multi_ractor_p()) {
1649 fields_obj = rb_obj_fields(obj,
id);
1658 const VALUE original_fields_obj = fields_obj;
1660 fields_obj = rb_imemo_fields_clone(fields_obj);
1663 shape_id_t old_shape_id = RBASIC_SHAPE_ID(fields_obj);
1664 shape_id_t removed_shape_id;
1665 shape_id_t next_shape_id = rb_shape_transition_remove_ivar(fields_obj,
id, &removed_shape_id);
1667 if (UNLIKELY(rb_shape_too_complex_p(next_shape_id))) {
1668 if (UNLIKELY(!rb_shape_too_complex_p(old_shape_id))) {
1670 rb_evict_fields_to_hash(obj);
1673 fields_obj = imemo_fields_complex_from_obj(obj, fields_obj, next_shape_id);
1677 if (!st_delete(rb_imemo_fields_complex_tbl(fields_obj), &key, (st_data_t *)&val)) {
1682 if (next_shape_id == old_shape_id) {
1686 RUBY_ASSERT(removed_shape_id != INVALID_SHAPE_ID);
1687 RUBY_ASSERT(RSHAPE_LEN(next_shape_id) == RSHAPE_LEN(old_shape_id) - 1);
1689 VALUE *fields = rb_imemo_fields_ptr(fields_obj);
1690 attr_index_t removed_index = RSHAPE_INDEX(removed_shape_id);
1691 val = fields[removed_index];
1693 attr_index_t new_fields_count = RSHAPE_LEN(next_shape_id);
1694 if (new_fields_count) {
1695 size_t trailing_fields = new_fields_count - removed_index;
1697 MEMMOVE(&fields[removed_index], &fields[removed_index + 1],
VALUE, trailing_fields);
1698 RBASIC_SET_SHAPE_ID(fields_obj, next_shape_id);
1705 MEMCPY(rb_imemo_fields_ptr(fields_obj), fields,
VALUE, new_fields_count);
1715 RBASIC_SET_SHAPE_ID(obj, next_shape_id);
1716 if (fields_obj != original_fields_obj) {
1722 RCLASS_WRITABLE_SET_FIELDS_OBJ(obj, fields_obj);
1725 rb_obj_set_fields(obj, fields_obj,
id, original_fields_obj);
1734rb_attr_delete(
VALUE obj,
ID id)
1736 return rb_ivar_delete(obj,
id,
Qnil);
1744 RUBY_ASSERT(rb_shape_canonical_p(RBASIC_SHAPE_ID(obj)));
1745 RUBY_ASSERT(RSHAPE_LEN(RBASIC_SHAPE_ID(obj)) == 0);
1747 obj_transition_too_complex(obj, table);
1751imemo_fields_complex_from_obj_i(
ID key,
VALUE val, st_data_t arg)
1754 st_table *table = rb_imemo_fields_complex_tbl(fields);
1756 RUBY_ASSERT(!st_lookup(table, (st_data_t)key, NULL));
1757 st_add_direct(table, (st_data_t)key, (st_data_t)val);
1764imemo_fields_complex_from_obj(
VALUE owner,
VALUE source_fields_obj, shape_id_t shape_id)
1766 attr_index_t
len = source_fields_obj ? RSHAPE_LEN(RBASIC_SHAPE_ID(source_fields_obj)) : 0;
1769 rb_field_foreach(source_fields_obj, imemo_fields_complex_from_obj_i, (st_data_t)fields_obj,
false);
1770 RBASIC_SET_SHAPE_ID(fields_obj, shape_id);
1776imemo_fields_copy_capa(
VALUE owner,
VALUE source_fields_obj, attr_index_t new_size)
1779 if (source_fields_obj) {
1780 attr_index_t fields_count = RSHAPE_LEN(RBASIC_SHAPE_ID(source_fields_obj));
1781 VALUE *fields = rb_imemo_fields_ptr(fields_obj);
1782 MEMCPY(fields, rb_imemo_fields_ptr(source_fields_obj),
VALUE, fields_count);
1783 RBASIC_SET_SHAPE_ID(fields_obj, RBASIC_SHAPE_ID(source_fields_obj));
1784 for (attr_index_t i = 0; i < fields_count; i++) {
1792imemo_fields_set(
VALUE owner,
VALUE fields_obj, shape_id_t target_shape_id,
ID field_name,
VALUE val,
bool concurrent)
1794 const VALUE original_fields_obj = fields_obj;
1795 shape_id_t current_shape_id = fields_obj ? RBASIC_SHAPE_ID(fields_obj) : ROOT_SHAPE_ID;
1797 if (UNLIKELY(rb_shape_too_complex_p(target_shape_id))) {
1798 if (rb_shape_too_complex_p(current_shape_id)) {
1803 fields_obj = rb_imemo_fields_clone(fields_obj);
1807 fields_obj = imemo_fields_complex_from_obj(owner, original_fields_obj, target_shape_id);
1808 current_shape_id = target_shape_id;
1811 st_table *table = rb_imemo_fields_complex_tbl(fields_obj);
1814 st_insert(table, (st_data_t)field_name, (st_data_t)val);
1816 RBASIC_SET_SHAPE_ID(fields_obj, target_shape_id);
1819 attr_index_t index = RSHAPE_INDEX(target_shape_id);
1820 if (concurrent || index >= RSHAPE_CAPACITY(current_shape_id)) {
1821 fields_obj = imemo_fields_copy_capa(owner, original_fields_obj, RSHAPE_CAPACITY(target_shape_id));
1824 VALUE *table = rb_imemo_fields_ptr(fields_obj);
1827 if (RSHAPE_LEN(target_shape_id) > RSHAPE_LEN(current_shape_id)) {
1828 RBASIC_SET_SHAPE_ID(fields_obj, target_shape_id);
1836generic_field_set(
VALUE obj, shape_id_t target_shape_id,
ID field_name,
VALUE val)
1839 field_name = RSHAPE_EDGE_NAME(target_shape_id);
1843 const VALUE original_fields_obj = rb_obj_fields(obj, field_name);
1844 VALUE fields_obj = imemo_fields_set(obj, original_fields_obj, target_shape_id, field_name, val,
false);
1846 rb_obj_set_fields(obj, fields_obj, field_name, original_fields_obj);
1847 return rb_shape_too_complex_p(target_shape_id) ? ATTR_INDEX_NOT_SET : RSHAPE_INDEX(target_shape_id);
1851generic_shape_ivar(
VALUE obj,
ID id,
bool *new_ivar_out)
1853 bool new_ivar =
false;
1854 shape_id_t current_shape_id = RBASIC_SHAPE_ID(obj);
1855 shape_id_t target_shape_id = current_shape_id;
1857 if (!rb_shape_too_complex_p(current_shape_id)) {
1858 if (!rb_shape_find_ivar(current_shape_id,
id, &target_shape_id)) {
1859 if (RSHAPE_LEN(current_shape_id) >= SHAPE_MAX_FIELDS) {
1860 rb_raise(rb_eArgError,
"too many instance variables");
1864 target_shape_id = rb_shape_transition_add_ivar(obj,
id);
1868 *new_ivar_out = new_ivar;
1869 return target_shape_id;
1876 shape_id_t target_shape_id = generic_shape_ivar(obj,
id, &dontcare);
1877 return generic_field_set(obj, target_shape_id,
id, val);
1881rb_ensure_iv_list_size(
VALUE obj, uint32_t current_len, uint32_t new_capacity)
1893 ROBJECT(obj)->as.heap.fields = newptr;
1898rb_obj_copy_ivs_to_hash_table_i(
ID key,
VALUE val, st_data_t arg)
1902 st_add_direct((
st_table *)arg, (st_data_t)key, (st_data_t)val);
1909 rb_ivar_foreach(obj, rb_obj_copy_ivs_to_hash_table_i, (st_data_t)table);
1915 rb_field_foreach(obj, rb_obj_copy_ivs_to_hash_table_i, (st_data_t)table,
false);
1919obj_field_set(
VALUE obj, shape_id_t target_shape_id,
ID field_name,
VALUE val)
1921 shape_id_t current_shape_id = RBASIC_SHAPE_ID(obj);
1923 if (UNLIKELY(rb_shape_too_complex_p(target_shape_id))) {
1924 if (UNLIKELY(!rb_shape_too_complex_p(current_shape_id))) {
1925 current_shape_id = rb_evict_fields_to_hash(obj);
1928 if (RSHAPE_LEN(target_shape_id) > RSHAPE_LEN(current_shape_id)) {
1929 RBASIC_SET_SHAPE_ID(obj, target_shape_id);
1933 field_name = RSHAPE_EDGE_NAME(target_shape_id);
1937 st_insert(ROBJECT_FIELDS_HASH(obj), (st_data_t)field_name, (st_data_t)val);
1940 return ATTR_INDEX_NOT_SET;
1943 attr_index_t index = RSHAPE_INDEX(target_shape_id);
1945 if (index >= RSHAPE_LEN(current_shape_id)) {
1946 if (UNLIKELY(index >= RSHAPE_CAPACITY(current_shape_id))) {
1947 rb_ensure_iv_list_size(obj, RSHAPE_CAPACITY(current_shape_id), RSHAPE_CAPACITY(target_shape_id));
1949 RBASIC_SET_SHAPE_ID(obj, target_shape_id);
1962 shape_id_t target_shape_id = generic_shape_ivar(obj,
id, &dontcare);
1963 return obj_field_set(obj, target_shape_id,
id, val);
1973 rb_check_frozen(obj);
1974 obj_ivar_set(obj,
id, val);
1986 RB_SET_SHAPE_ID(x, rb_shape_transition_frozen(x));
1994static attr_index_t class_ivar_set(
VALUE obj,
ID id,
VALUE val,
bool *new_ivar);
1999 RB_DEBUG_COUNTER_INC(ivar_set_base);
2003 return obj_ivar_set(obj,
id, val);
2007 IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(
id);
2009 return class_ivar_set(obj,
id, val, &dontcare);
2012 return generic_ivar_set(obj,
id, val);
2019 rb_check_frozen(obj);
2020 ivar_set(obj,
id, val);
2027 return ivar_set(obj,
id, val);
2036 ivar_set(obj,
id, val);
2040rb_obj_field_set(
VALUE obj, shape_id_t target_shape_id,
ID field_name,
VALUE val)
2044 return obj_field_set(obj, target_shape_id, field_name, val);
2048 rb_bug(
"Unreachable");
2051 return generic_field_set(obj, target_shape_id, field_name, val);
2056ivar_defined0(
VALUE obj,
ID id)
2060 if (rb_shape_obj_too_complex_p(obj)) {
2066 rb_bug(
"Unreachable");
2071 table = rb_imemo_fields_complex_tbl(obj);
2075 table = ROBJECT_FIELDS_HASH(obj);
2079 VALUE fields_obj = rb_obj_fields_no_ractor_check(obj);
2080 table = rb_imemo_fields_complex_tbl(fields_obj);
2084 if (!table || !rb_st_lookup(table,
id, &idx)) {
2091 return RBOOL(rb_shape_get_iv_index(RBASIC_SHAPE_ID(obj),
id, &index));
2105 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
2107 defined = ivar_defined0(fields_obj,
id);
2112 defined = ivar_defined0(obj,
id);
2120 struct gen_fields_tbl *fields_tbl;
2122 rb_ivar_foreach_callback_func *func;
2128iterate_over_shapes_callback(shape_id_t shape_id,
void *data)
2132 if (itr_data->ivar_only && !RSHAPE_TYPE_P(shape_id, SHAPE_IVAR)) {
2139 RUBY_ASSERT(!rb_shape_obj_too_complex_p(itr_data->obj));
2143 RUBY_ASSERT(IMEMO_TYPE_P(itr_data->obj, imemo_fields));
2144 RUBY_ASSERT(!rb_shape_obj_too_complex_p(itr_data->obj));
2146 fields = rb_imemo_fields_ptr(itr_data->obj);
2149 rb_bug(
"Unreachable");
2152 VALUE val = fields[RSHAPE_INDEX(shape_id)];
2153 return itr_data->func(RSHAPE_EDGE_NAME(shape_id), val, itr_data->arg);
2160iterate_over_shapes(shape_id_t shape_id, rb_ivar_foreach_callback_func *callback,
struct iv_itr_data *itr_data)
2162 rb_shape_foreach_field(shape_id, iterate_over_shapes_callback, itr_data);
2166each_hash_iv(st_data_t
id, st_data_t val, st_data_t data)
2169 rb_ivar_foreach_callback_func *callback = itr_data->func;
2170 if (is_internal_id((
ID)
id)) {
2173 return callback((
ID)
id, (
VALUE)val, itr_data->arg);
2177obj_fields_each(
VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg,
bool ivar_only)
2183 .ivar_only = ivar_only,
2186 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
2187 if (rb_shape_too_complex_p(shape_id)) {
2188 rb_st_foreach(ROBJECT_FIELDS_HASH(obj), each_hash_iv, (st_data_t)&itr_data);
2192 iterate_over_shapes(shape_id, func, &itr_data);
2197imemo_fields_each(
VALUE fields_obj, rb_ivar_foreach_callback_func *func, st_data_t arg,
bool ivar_only)
2199 IMEMO_TYPE_P(fields_obj, imemo_fields);
2205 .ivar_only = ivar_only,
2208 shape_id_t shape_id = RBASIC_SHAPE_ID(fields_obj);
2209 if (rb_shape_too_complex_p(shape_id)) {
2210 rb_st_foreach(rb_imemo_fields_complex_tbl(fields_obj), each_hash_iv, (st_data_t)&itr_data);
2213 itr_data.fields = rb_imemo_fields_ptr(fields_obj);
2214 iterate_over_shapes(shape_id, func, &itr_data);
2221 VALUE new_fields_obj;
2223 rb_check_frozen(dest);
2225 if (!rb_obj_gen_fields_p(obj)) {
2229 shape_id_t src_shape_id = rb_obj_shape_id(obj);
2231 VALUE fields_obj = rb_obj_fields_no_ractor_check(obj);
2238 if (rb_shape_too_complex_p(src_shape_id)) {
2239 rb_shape_copy_complex_ivars(dest, obj, src_shape_id, rb_imemo_fields_complex_tbl(fields_obj));
2243 shape_id_t dest_shape_id = src_shape_id;
2244 shape_id_t initial_shape_id = rb_obj_shape_id(dest);
2246 if (!rb_shape_canonical_p(src_shape_id)) {
2247 RUBY_ASSERT(RSHAPE_TYPE_P(initial_shape_id, SHAPE_ROOT));
2249 dest_shape_id = rb_shape_rebuild(initial_shape_id, src_shape_id);
2250 if (UNLIKELY(rb_shape_too_complex_p(dest_shape_id))) {
2251 st_table *table = rb_st_init_numtable_with_size(src_num_ivs);
2252 rb_obj_copy_ivs_to_hash_table(obj, table);
2253 rb_obj_init_too_complex(dest, table);
2258 if (!RSHAPE_LEN(dest_shape_id)) {
2259 RBASIC_SET_SHAPE_ID(dest, dest_shape_id);
2263 new_fields_obj = rb_imemo_fields_new(dest, RSHAPE_CAPACITY(dest_shape_id),
RB_OBJ_SHAREABLE_P(dest));
2264 VALUE *src_buf = rb_imemo_fields_ptr(fields_obj);
2265 VALUE *dest_buf = rb_imemo_fields_ptr(new_fields_obj);
2266 rb_shape_copy_fields(new_fields_obj, dest_buf, dest_shape_id, src_buf, src_shape_id);
2267 RBASIC_SET_SHAPE_ID(new_fields_obj, dest_shape_id);
2269 rb_obj_replace_fields(dest, new_fields_obj);
2278rb_replace_generic_ivar(
VALUE clone,
VALUE obj)
2281 st_data_t fields_tbl, obj_data = (st_data_t)obj;
2282 if (st_delete(generic_fields_tbl_, &obj_data, &fields_tbl)) {
2283 st_insert(generic_fields_tbl_, (st_data_t)clone, fields_tbl);
2287 rb_bug(
"unreachable");
2293rb_field_foreach(
VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg,
bool ivar_only)
2298 if (IMEMO_TYPE_P(obj, imemo_fields)) {
2299 imemo_fields_each(obj, func, arg, ivar_only);
2303 obj_fields_each(obj, func, arg, ivar_only);
2308 IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(0);
2309 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
2311 imemo_fields_each(fields_obj, func, arg, ivar_only);
2317 VALUE fields_obj = rb_obj_fields_no_ractor_check(obj);
2319 imemo_fields_each(fields_obj, func, arg, ivar_only);
2329 rb_field_foreach(obj, func, arg,
true);
2337 st_index_t iv_count = 0;
2340 iv_count = ROBJECT_FIELDS_COUNT(obj);
2346 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
2350 if (rb_shape_obj_too_complex_p(fields_obj)) {
2351 iv_count = rb_st_table_size(rb_imemo_fields_complex_tbl(fields_obj));
2354 iv_count = RBASIC_FIELDS_COUNT(fields_obj);
2362 if (rb_shape_obj_too_complex_p(obj)) {
2363 iv_count = rb_st_table_size(rb_imemo_fields_complex_tbl(obj));
2366 iv_count = RBASIC_FIELDS_COUNT(obj);
2372 VALUE fields_obj = rb_obj_fields_no_ractor_check(obj);
2374 if (rb_shape_obj_too_complex_p(fields_obj)) {
2375 rb_st_table_size(rb_imemo_fields_complex_tbl(fields_obj));
2378 iv_count = RBASIC_FIELDS_COUNT(obj);
2385 if (rb_shape_obj_has_id(obj)) {
2393ivar_i(
ID key,
VALUE v, st_data_t a)
2430#define rb_is_constant_id rb_is_const_id
2431#define rb_is_constant_name rb_is_const_name
2432#define id_for_var(obj, name, part, type) \
2433 id_for_var_message(obj, name, type, "'%1$s' is not allowed as "#part" "#type" variable name")
2434#define id_for_var_message(obj, name, type, message) \
2435 check_id_type(obj, &(name), rb_is_##type##_id, rb_is_##type##_name, message, strlen(message))
2438 int (*valid_id_p)(
ID),
int (*valid_name_p)(
VALUE),
2439 const char *message,
size_t message_len)
2442 VALUE name = *pname;
2444 if (
id ? !valid_id_p(
id) : !valid_name_p(name)) {
2445 rb_name_err_raise_str(rb_fstring_new(message, message_len),
2477 const ID id = id_for_var(obj, name, an, instance);
2481 rb_check_frozen(obj);
2486 if (!UNDEF_P(val))
return val;
2489 rb_name_err_raise(
"instance variable %1$s not defined",
2494NORETURN(
static void uninitialized_constant(
VALUE,
VALUE));
2496uninitialized_constant(
VALUE klass,
VALUE name)
2499 rb_name_err_raise(
"uninitialized constant %2$s::%1$s",
2502 rb_name_err_raise(
"uninitialized constant %1$s",
2509 VALUE value = rb_funcallv(klass, idConst_missing, 1, &name);
2510 rb_vm_inc_const_missing_count();
2559 VALUE ref = ec->private_const_reference;
2560 rb_vm_pop_cfunc_frame();
2562 ec->private_const_reference = 0;
2563 rb_name_err_raise(
"private constant %2$s::%1$s referenced", ref, name);
2565 uninitialized_constant(klass, name);
2571autoload_table_mark(
void *ptr)
2573 rb_mark_tbl_no_pin((
st_table *)ptr);
2577autoload_table_free(
void *ptr)
2583autoload_table_memsize(
const void *ptr)
2586 return st_memsize(tbl);
2590autoload_table_compact(
void *ptr)
2592 rb_gc_ref_update_table_values_only((
st_table *)ptr);
2597 {autoload_table_mark, autoload_table_free, autoload_table_memsize, autoload_table_compact,},
2601#define check_autoload_table(av) \
2602 (struct st_table *)rb_check_typeddata((av), &autoload_table_type)
2613 if (RICLASS_IS_ORIGIN_P(mod)) {
2617 mod =
RBASIC(mod)->klass;
2625 VALUE tbl_value = rb_ivar_lookup(mod, autoload,
Qfalse);
2626 if (!
RTEST(tbl_value) || !(tbl = check_autoload_table(tbl_value)) || !st_lookup(tbl, (st_data_t)
id, &val)) {
2636 struct ccan_list_node cnode;
2639 VALUE autoload_data_value;
2655 rb_const_flag_t flag;
2671 rb_serial_t fork_gen;
2674 struct ccan_list_head constants;
2678autoload_data_mark_and_move(
void *ptr)
2682 rb_gc_mark_and_move(&p->feature);
2683 rb_gc_mark_and_move(&p->mutex);
2687autoload_data_free(
void *ptr)
2692 ccan_list_for_each_safe(&p->constants,
autoload_const, next, cnode) {
2700autoload_data_memsize(
const void *ptr)
2707 {autoload_data_mark_and_move, autoload_data_free, autoload_data_memsize, autoload_data_mark_and_move},
2712autoload_const_mark_and_move(
void *ptr)
2716 rb_gc_mark_and_move(&ac->module);
2717 rb_gc_mark_and_move(&ac->autoload_data_value);
2718 rb_gc_mark_and_move(&ac->value);
2719 rb_gc_mark_and_move(&ac->file);
2720 rb_gc_mark_and_move(&ac->box_value);
2724autoload_const_memsize(
const void *ptr)
2730autoload_const_free(
void *ptr)
2740 {autoload_const_mark_and_move, autoload_const_free, autoload_const_memsize, autoload_const_mark_and_move,},
2745get_autoload_data(
VALUE autoload_const_value,
struct autoload_const **autoload_const_pointer)
2758 if (autoload_const_pointer) *autoload_const_pointer =
autoload_const;
2764 VALUE dst_tbl_value;
2770autoload_copy_table_for_box_i(st_data_t key, st_data_t value, st_data_t arg)
2774 struct st_table *tbl = data->dst_tbl;
2775 VALUE tbl_value = data->dst_tbl_value;
2781 VALUE autoload_data_value = src_const->autoload_data_value;
2793 st_insert(tbl, (st_data_t)
autoload_const->name, (st_data_t)new_value);
2802 struct st_table *src_tbl, *dst_tbl;
2803 VALUE src_tbl_value, dst_tbl_value;
2804 if (!rb_st_lookup(iv_ptr, (st_data_t)autoload, (st_data_t *)&src_tbl_value)) {
2808 if (!
RTEST(src_tbl_value) || !(src_tbl = check_autoload_table(src_tbl_value))) {
2812 src_tbl = check_autoload_table(src_tbl_value);
2818 .dst_tbl_value = dst_tbl_value,
2823 st_foreach(src_tbl, autoload_copy_table_for_box_i, (st_data_t)&data);
2824 st_insert(iv_ptr, (st_data_t)autoload, (st_data_t)dst_tbl_value);
2828rb_autoload(
VALUE module,
ID name,
const char *feature)
2830 if (!feature || !*feature) {
2831 rb_raise(rb_eArgError,
"empty feature name");
2834 rb_autoload_str(module, name, rb_fstring_cstr(feature));
2838static void const_added(
VALUE klass,
ID const_name);
2848autoload_feature_lookup_or_create(
VALUE feature,
struct autoload_data **autoload_data_pointer)
2850 RUBY_ASSERT_MUTEX_OWNED(autoload_mutex);
2851 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
2853 VALUE autoload_data_value = rb_hash_aref(autoload_features, feature);
2856 if (
NIL_P(autoload_data_value)) {
2862 if (autoload_data_pointer) *autoload_data_pointer =
autoload_data;
2864 rb_hash_aset(autoload_features, feature, autoload_data_value);
2866 else if (autoload_data_pointer) {
2867 *autoload_data_pointer =
rb_check_typeddata(autoload_data_value, &autoload_data_type);
2870 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
2871 return autoload_data_value;
2875autoload_table_lookup_or_create(
VALUE module)
2877 VALUE autoload_table_value = rb_ivar_lookup(module, autoload,
Qfalse);
2878 if (
RTEST(autoload_table_value)) {
2879 return autoload_table_value;
2883 rb_class_ivar_set(module, autoload, autoload_table_value);
2885 return autoload_table_value;
2890autoload_synchronized(
VALUE _arguments)
2894 rb_const_entry_t *constant_entry = rb_const_lookup(arguments->module, arguments->name);
2895 if (constant_entry && !UNDEF_P(constant_entry->value)) {
2900 const_set(arguments->module, arguments->name,
Qundef);
2902 VALUE autoload_table_value = autoload_table_lookup_or_create(arguments->module);
2903 struct st_table *autoload_table = check_autoload_table(autoload_table_value);
2906 VALUE feature = rb_fstring(arguments->feature);
2909 VALUE autoload_data_value = autoload_feature_lookup_or_create(feature, &
autoload_data);
2921 st_insert(autoload_table, (st_data_t)arguments->name, (st_data_t)autoload_const_value);
2931 const rb_box_t *box = rb_current_box();
2932 VALUE current_box_value = rb_get_box_object((
rb_box_t *)box);
2935 rb_raise(
rb_eNameError,
"autoload must be constant name: %"PRIsVALUE
"", QUOTE_ID(name));
2939 if (!RSTRING_LEN(feature)) {
2940 rb_raise(rb_eArgError,
"empty feature name");
2947 .box_value = current_box_value,
2952 if (result ==
Qtrue) {
2953 const_added(module, name);
2958autoload_delete(
VALUE module,
ID name)
2960 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
2962 st_data_t load = 0, key = name;
2966 VALUE table_value = rb_ivar_lookup(module, autoload,
Qfalse);
2967 if (
RTEST(table_value)) {
2968 struct st_table *table = check_autoload_table(table_value);
2970 st_delete(table, &key, &load);
2993 if (table->num_entries == 0) {
2994 rb_attr_delete(module, autoload);
2999 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
3005 return ele->mutex !=
Qnil && !rb_mutex_owned_p(ele->mutex);
3009check_autoload_required(
VALUE mod,
ID id,
const char **loadingpath)
3013 const char *loading;
3015 if (!autoload_const_value || !(
autoload_data = get_autoload_data(autoload_const_value, 0))) {
3028 return autoload_const_value;
3031 loading = RSTRING_PTR(feature);
3034 return autoload_const_value;
3037 if (loadingpath && loading) {
3038 *loadingpath = loading;
3039 return autoload_const_value;
3048rb_autoloading_value(
VALUE mod,
ID id,
VALUE* value, rb_const_flag_t *flag)
3051 if (!ac)
return FALSE;
3067 return ele->mutex !=
Qnil && rb_mutex_owned_p(ele->mutex);
3074autoloading_const_entry(
VALUE mod,
ID id)
3081 if (!load || !(ele = get_autoload_data(load, &ac))) {
3087 if (autoload_by_current(ele)) {
3088 if (!UNDEF_P(ac->value)) {
3097autoload_defined_p(
VALUE mod,
ID id)
3102 if (!ce || !UNDEF_P(ce->value)) {
3108 return !rb_autoloading_value(mod,
id, NULL, NULL);
3130 check_before_mod_set(ac->module, ac->name, ac->value,
"constant");
3133 const_tbl_update(ac,
true);
3140autoload_load_needed(
VALUE _arguments)
3144 const char *loading = 0, *src;
3146 if (!autoload_defined_p(arguments->module, arguments->name)) {
3150 VALUE autoload_const_value = check_autoload_required(arguments->module, arguments->name, &loading);
3151 if (!autoload_const_value) {
3156 if (src && loading && strcmp(src, loading) == 0) {
3177 return autoload_const_value;
3181autoload_apply_constants(
VALUE _arguments)
3183 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
3195 ccan_list_for_each_safe(&arguments->autoload_data->constants,
autoload_const, next, cnode) {
3201 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
3207autoload_feature_require(
VALUE _arguments)
3209 VALUE receiver = rb_vm_top_self();
3219 if (rb_box_available() && BOX_OBJ_P(autoload_box_value))
3220 receiver = autoload_box_value;
3228 rb_gccct_clear_table(
Qnil);
3230 VALUE result =
rb_funcall(receiver, rb_intern(
"require"), 1, arguments->autoload_data->feature);
3232 if (
RTEST(result)) {
3239autoload_try_load(
VALUE _arguments)
3243 VALUE result = autoload_feature_require(_arguments);
3246 rb_const_entry_t *ce = rb_const_lookup(arguments->module, arguments->name);
3248 if (!ce || UNDEF_P(ce->value)) {
3255 "Expected %"PRIsVALUE
" to define %"PRIsVALUE
" but it didn't",
3256 arguments->autoload_data->feature,
3262 "Expected %"PRIsVALUE
" to define %"PRIsVALUE
"::%"PRIsVALUE
" but it didn't",
3263 arguments->autoload_data->feature,
3271 ce->flag |= arguments->flag;
3283 if (!ce || !UNDEF_P(ce->value)) {
3288 if (UNLIKELY(!rb_ractor_main_p())) {
3289 return rb_ractor_autoload_load(module, name);
3299 if (autoload_const_value ==
Qfalse)
return autoload_const_value;
3301 arguments.flag = ce->flag & (CONST_DEPRECATED | CONST_VISIBILITY_MASK);
3318 return rb_autoload_at_p(mod,
id, TRUE);
3322rb_autoload_at_p(
VALUE mod,
ID id,
int recur)
3327 while (!autoload_defined_p(mod,
id)) {
3328 if (!recur)
return Qnil;
3330 if (!mod)
return Qnil;
3332 load = check_autoload_required(mod,
id, 0);
3333 if (!load)
return Qnil;
3334 return (ele = get_autoload_data(load, 0)) ? ele->feature :
Qnil;
3340 if (RB_CONST_DEPRECATED_P(ce) &&
3353rb_const_get_0(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility)
3356 VALUE c = rb_const_search(klass,
id, exclude, recurse, visibility, &found_in);
3358 if (UNLIKELY(!rb_ractor_main_p())) {
3360 rb_raise(rb_eRactorIsolationError,
"can not access non-shareable objects in constant %"PRIsVALUE
"::%"PRIsVALUE
" by non-main Ractor.",
rb_class_path(found_in), rb_id2str(
id));
3365 return rb_const_missing(klass,
ID2SYM(
id));
3369rb_const_search_from(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility,
VALUE *found_in)
3371 VALUE value, current;
3372 bool first_iteration =
true;
3374 for (current = klass;
3376 current =
RCLASS_SUPER(current), first_iteration =
false) {
3381 if (!first_iteration && RCLASS_ORIGIN(current) != current) {
3394 while ((ce = rb_const_lookup(tmp,
id))) {
3395 if (visibility && RB_CONST_PRIVATE_P(ce)) {
3396 GET_EC()->private_const_reference = tmp;
3399 rb_const_warn_if_deprecated(ce, tmp,
id);
3401 if (UNDEF_P(value)) {
3403 if (am == tmp)
break;
3405 ac = autoloading_const_entry(tmp,
id);
3407 if (found_in) { *found_in = tmp; }
3416 if (found_in) { *found_in = tmp; }
3419 if (!recurse)
break;
3423 GET_EC()->private_const_reference = 0;
3428rb_const_search(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility,
VALUE *found_in)
3433 value = rb_const_search_from(klass,
id, exclude, recurse, visibility, found_in);
3434 if (!UNDEF_P(value))
return value;
3435 if (exclude)
return value;
3438 return rb_const_search_from(
rb_cObject,
id, FALSE, recurse, visibility, found_in);
3444 return rb_const_get_0(klass,
id, TRUE, TRUE, FALSE);
3450 return rb_const_get_0(klass,
id, FALSE, TRUE, FALSE);
3456 return rb_const_get_0(klass,
id, TRUE, FALSE, FALSE);
3460rb_public_const_get_from(
VALUE klass,
ID id)
3462 return rb_const_get_0(klass,
id, TRUE, TRUE, TRUE);
3466rb_public_const_get_at(
VALUE klass,
ID id)
3468 return rb_const_get_0(klass,
id, TRUE, FALSE, TRUE);
3471NORETURN(
static void undefined_constant(
VALUE mod,
VALUE name));
3475 rb_name_err_raise(
"constant %2$s::%1$s not defined",
3480rb_const_location_from(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility)
3482 while (
RTEST(klass)) {
3485 while ((ce = rb_const_lookup(klass,
id))) {
3486 if (visibility && RB_CONST_PRIVATE_P(ce)) {
3493 if (UNDEF_P(ce->value)) {
3495 if (
RTEST(autoload_const_value)) {
3508 if (!recurse)
break;
3517rb_const_location(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility)
3522 loc = rb_const_location_from(klass,
id, exclude, recurse, visibility);
3523 if (!
NIL_P(loc))
return loc;
3524 if (exclude)
return loc;
3527 return rb_const_location_from(
rb_cObject,
id, FALSE, recurse, visibility);
3531rb_const_source_location(
VALUE klass,
ID id)
3533 return rb_const_location(klass,
id, FALSE, TRUE, FALSE);
3537rb_const_source_location_at(
VALUE klass,
ID id)
3539 return rb_const_location(klass,
id, TRUE, FALSE, FALSE);
3555 const ID id = id_for_var(mod, name, a, constant);
3558 undefined_constant(mod, name);
3571 rb_check_frozen(mod);
3573 ce = rb_const_lookup(mod,
id);
3577 rb_name_err_raise(
"cannot remove %2$s::%1$s", mod,
ID2SYM(
id));
3580 undefined_constant(mod,
ID2SYM(
id));
3583 VALUE writable_ce = 0;
3584 if (rb_id_table_lookup(RCLASS_WRITABLE_CONST_TBL(mod),
id, &writable_ce)) {
3585 rb_id_table_delete(RCLASS_WRITABLE_CONST_TBL(mod),
id);
3591 rb_const_warn_if_deprecated(ce, mod,
id);
3597 autoload_delete(mod,
id);
3601 if (ce != const_lookup(RCLASS_PRIME_CONST_TBL(mod),
id)) {
3610cv_i_update(st_data_t *k, st_data_t *v, st_data_t a,
int existing)
3612 if (existing)
return ST_STOP;
3617static enum rb_id_table_iterator_result
3624 st_update(tbl, (st_data_t)key, cv_i_update, (st_data_t)ce);
3626 return ID_TABLE_CONTINUE;
3629static enum rb_id_table_iterator_result
3630rb_local_constants_i(
ID const_name,
VALUE const_value,
void *ary)
3635 return ID_TABLE_CONTINUE;
3639rb_local_constants(
VALUE mod)
3648 rb_id_table_foreach(tbl, rb_local_constants_i, (
void *)ary);
3659 tbl = st_init_numtable();
3661 if (RCLASS_CONST_TBL(mod)) {
3663 rb_id_table_foreach(RCLASS_CONST_TBL(mod), sv_i, tbl);
3683list_i(st_data_t key, st_data_t value,
VALUE ary)
3726 bool inherit =
true;
3734 return rb_local_constants(mod);
3739rb_const_defined_0(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility)
3748 if ((ce = rb_const_lookup(tmp,
id))) {
3749 if (visibility && RB_CONST_PRIVATE_P(ce)) {
3752 if (UNDEF_P(ce->value) && !check_autoload_required(tmp,
id, 0) &&
3753 !rb_autoloading_value(tmp,
id, NULL, NULL))
3762 if (!recurse)
break;
3776 return rb_const_defined_0(klass,
id, TRUE, TRUE, FALSE);
3782 return rb_const_defined_0(klass,
id, FALSE, TRUE, FALSE);
3788 return rb_const_defined_0(klass,
id, TRUE, FALSE, FALSE);
3792rb_public_const_defined_from(
VALUE klass,
ID id)
3794 return rb_const_defined_0(klass,
id, TRUE, TRUE, TRUE);
3798check_before_mod_set(
VALUE klass,
ID id,
VALUE val,
const char *dest)
3800 rb_check_frozen(klass);
3803static void set_namespace_path(
VALUE named_namespace,
VALUE name);
3805static enum rb_id_table_iterator_result
3806set_namespace_path_i(
ID id,
VALUE v,
void *payload)
3809 VALUE value = ce->value;
3812 return ID_TABLE_CONTINUE;
3815 bool has_permanent_classpath;
3816 classname(value, &has_permanent_classpath);
3817 if (has_permanent_classpath) {
3818 return ID_TABLE_CONTINUE;
3820 set_namespace_path(value, build_const_path(parental_path,
id));
3822 if (!RCLASS_PERMANENT_CLASSPATH_P(value)) {
3823 RCLASS_WRITE_CLASSPATH(value, 0,
false);
3826 return ID_TABLE_CONTINUE;
3835set_namespace_path(
VALUE named_namespace,
VALUE namespace_path)
3837 struct rb_id_table *const_table = RCLASS_CONST_TBL(named_namespace);
3838 RB_OBJ_SET_SHAREABLE(namespace_path);
3841 RCLASS_WRITE_CLASSPATH(named_namespace, namespace_path,
true);
3844 rb_id_table_foreach(const_table, set_namespace_path_i, &namespace_path);
3850const_added(
VALUE klass,
ID const_name)
3852 if (GET_VM()->running) {
3854 rb_funcallv(klass, idConst_added, 1, &name);
3864 rb_raise(
rb_eTypeError,
"no class/module to define constant %"PRIsVALUE
"",
3869 rb_raise(rb_eRactorIsolationError,
"can not set constants with non-shareable objects by non-main Ractors");
3872 check_before_mod_set(klass,
id, val,
"constant");
3875 struct rb_id_table *tbl = RCLASS_WRITABLE_CONST_TBL(klass);
3877 tbl = rb_id_table_create(0);
3878 RCLASS_WRITE_CONST_TBL(klass, tbl,
false);
3881 rb_id_table_insert(tbl,
id, (
VALUE)ce);
3882 setup_const_entry(ce, klass, val, CONST_PUBLIC);
3886 .module = klass, .name = id,
3887 .value = val, .flag = CONST_PUBLIC,
3890 ac.file = rb_source_location(&ac.line);
3891 const_tbl_update(&ac,
false);
3900 bool val_path_permanent;
3901 VALUE val_path = classname(val, &val_path_permanent);
3902 if (
NIL_P(val_path) || !val_path_permanent) {
3904 set_namespace_path(val, rb_id2str(
id));
3907 bool parental_path_permanent;
3908 VALUE parental_path = classname(klass, &parental_path_permanent);
3909 if (
NIL_P(parental_path)) {
3911 parental_path = rb_tmp_class_path(klass, &throwaway, make_temporary_path);
3913 if (parental_path_permanent && !val_path_permanent) {
3914 set_namespace_path(val, build_const_path(parental_path,
id));
3916 else if (!parental_path_permanent &&
NIL_P(val_path)) {
3917 VALUE path = build_const_path(parental_path,
id);
3918 RCLASS_SET_CLASSPATH(val, path,
false);
3928 const_set(klass,
id, val);
3929 const_added(klass,
id);
3933autoload_const_value_for_named_constant(
VALUE module,
ID name,
struct autoload_const **autoload_const_pointer)
3936 if (!autoload_const_value)
return Qfalse;
3943 return autoload_const_value;
3953 VALUE klass = ac->module;
3954 VALUE val = ac->value;
3956 struct rb_id_table *tbl = RCLASS_CONST_TBL(klass);
3957 rb_const_flag_t visibility = ac->flag;
3960 if (rb_id_table_lookup(tbl,
id, &value)) {
3962 if (UNDEF_P(ce->value)) {
3963 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
3964 VALUE file = ac->file;
3965 int line = ac->line;
3966 VALUE autoload_const_value = autoload_const_value_for_named_constant(klass,
id, &ac);
3968 if (!autoload_force && autoload_const_value) {
3972 RB_OBJ_WRITE(autoload_const_value, &ac->file, rb_source_location(&ac->line));
3976 autoload_delete(klass,
id);
3977 ce->flag = visibility;
3982 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
3986 VALUE name = QUOTE_ID(
id);
3987 visibility = ce->flag;
3989 rb_warn(
"already initialized constant %"PRIsVALUE
"", name);
3991 rb_warn(
"already initialized constant %"PRIsVALUE
"::%"PRIsVALUE
"",
3993 if (!
NIL_P(ce->file) && ce->line) {
3994 rb_compile_warn(RSTRING_PTR(ce->file), ce->line,
3995 "previous definition of %"PRIsVALUE
" was here", name);
3999 setup_const_entry(ce, klass, val, visibility);
4002 tbl = RCLASS_WRITABLE_CONST_TBL(klass);
4006 rb_id_table_insert(tbl,
id, (
VALUE)ce);
4007 setup_const_entry(ce, klass, val, visibility);
4013 rb_const_flag_t visibility)
4015 ce->flag = visibility;
4017 RB_OBJ_WRITE(klass, &ce->file, rb_source_location(&ce->line));
4023 ID id = rb_intern(name);
4026 rb_warn(
"rb_define_const: invalid name '%s' for constant", name);
4029 rb_vm_register_global_object(val);
4041set_const_visibility(
VALUE mod,
int argc,
const VALUE *argv,
4042 rb_const_flag_t flag, rb_const_flag_t mask)
4050 rb_warning(
"%"PRIsVALUE
" with no argument is just ignored",
4051 QUOTE_ID(rb_frame_callee()));
4055 for (i = 0; i < argc; i++) {
4057 VALUE val = argv[i];
4060 undefined_constant(mod, val);
4062 if ((ce = rb_const_lookup(mod,
id))) {
4065 if (UNDEF_P(ce->value)) {
4066 if (autoload_const_value_for_named_constant(mod,
id, &ac)) {
4074 undefined_constant(mod,
ID2SYM(
id));
4084 long len = strlen(name);
4088 undefined_constant(mod, rb_fstring_new(name,
len));
4090 if (!(ce = rb_const_lookup(mod,
id))) {
4091 undefined_constant(mod,
ID2SYM(
id));
4093 ce->flag |= CONST_DEPRECATED;
4104rb_mod_private_constant(
int argc,
const VALUE *argv,
VALUE obj)
4106 set_const_visibility(obj, argc, argv, CONST_PRIVATE, CONST_VISIBILITY_MASK);
4118rb_mod_public_constant(
int argc,
const VALUE *argv,
VALUE obj)
4120 set_const_visibility(obj, argc, argv, CONST_PUBLIC, CONST_VISIBILITY_MASK);
4144rb_mod_deprecate_constant(
int argc,
const VALUE *argv,
VALUE obj)
4146 set_const_visibility(obj, argc, argv, CONST_DEPRECATED, CONST_DEPRECATED);
4151original_module(
VALUE c)
4159cvar_lookup_at(
VALUE klass,
ID id, st_data_t *v)
4162 if (RICLASS_IS_ORIGIN_P(klass)) {
4167 klass =
RBASIC(klass)->klass;
4172 if (UNDEF_P(n))
return 0;
4179cvar_front_klass(
VALUE klass)
4181 if (RCLASS_SINGLETON_P(klass)) {
4182 VALUE obj = RCLASS_ATTACHED_OBJECT(klass);
4183 if (rb_namespace_p(obj)) {
4193 if (front && target != front) {
4194 if (original_module(front) != original_module(target)) {
4196 "class variable % "PRIsVALUE
" of %"PRIsVALUE
" is overtaken by %"PRIsVALUE
"",
4201 rb_ivar_delete(front,
id,
Qundef);
4206#define CVAR_FOREACH_ANCESTORS(klass, v, r) \
4207 for (klass = cvar_front_klass(klass); klass; klass = RCLASS_SUPER(klass)) { \
4208 if (cvar_lookup_at(klass, id, (v))) { \
4213#define CVAR_LOOKUP(v,r) do {\
4214 CVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(klass, id); \
4215 if (cvar_lookup_at(klass, id, (v))) {r;}\
4216 CVAR_FOREACH_ANCESTORS(klass, v, r);\
4234check_for_cvar_table(
VALUE subclass,
VALUE key)
4238 RB_DEBUG_COUNTER_INC(cvar_class_invalidate);
4239 ruby_vm_global_cvar_state++;
4243 rb_class_foreach_subclass(subclass, check_for_cvar_table, key);
4249 VALUE tmp, front = 0, target = 0;
4252 CVAR_LOOKUP(0, {
if (!front) front = klass; target = klass;});
4254 cvar_overtaken(front, target,
id);
4261 target =
RBASIC(target)->klass;
4263 check_before_mod_set(target,
id, val,
"class variable");
4265 bool new_cvar = rb_class_ivar_set(target,
id, val);
4267 struct rb_id_table *rb_cvc_tbl = RCLASS_WRITABLE_CVC_TBL(target);
4270 rb_cvc_tbl = rb_id_table_create(2);
4271 RCLASS_WRITE_CVC_TBL(target, rb_cvc_tbl);
4277 if (!rb_id_table_lookup(rb_cvc_tbl,
id, &ent_data)) {
4279 ent->class_value = target;
4280 ent->global_cvar_state = GET_GLOBAL_CVAR_STATE();
4282 rb_id_table_insert(rb_cvc_tbl,
id, (
VALUE)ent);
4283 RB_DEBUG_COUNTER_INC(cvar_inline_miss);
4286 ent = (
void *)ent_data;
4287 ent->global_cvar_state = GET_GLOBAL_CVAR_STATE();
4295 if (RCLASS_SUBCLASSES_FIRST(target)) {
4296 rb_class_foreach_subclass(target, check_for_cvar_table,
id);
4308 value = find_cvar(klass, front, &target,
id);
4310 rb_name_err_raise(
"uninitialized class variable %1$s in %2$s",
4313 cvar_overtaken(*front, target,
id);
4314 return (
VALUE)value;
4327 if (!klass)
return Qfalse;
4328 CVAR_LOOKUP(0,
return Qtrue);
4333cv_intern(
VALUE klass,
const char *name)
4335 ID id = rb_intern(name);
4337 rb_name_err_raise(
"wrong class variable name %1$s",
4346 ID id = cv_intern(klass, name);
4353 ID id = cv_intern(klass, name);
4364cv_i(
ID key,
VALUE v, st_data_t a)
4369 st_update(tbl, (st_data_t)key, cv_i_update, 0);
4375mod_cvar_at(
VALUE mod,
void *data)
4379 tbl = st_init_numtable();
4381 mod = original_module(mod);
4388mod_cvar_of(
VALUE mod,
void *data)
4391 if (RCLASS_SINGLETON_P(mod)) {
4392 if (rb_namespace_p(RCLASS_ATTACHED_OBJECT(mod))) {
4393 data = mod_cvar_at(tmp, data);
4394 tmp = cvar_front_klass(tmp);
4398 data = mod_cvar_at(tmp, data);
4406cv_list_i(st_data_t key, st_data_t value,
VALUE ary)
4414cvar_list(
void *data)
4450 bool inherit =
true;
4455 tbl = mod_cvar_of(mod, 0);
4458 tbl = mod_cvar_at(mod, 0);
4460 return cvar_list(tbl);
4485 const ID id = id_for_var_message(mod, name,
class,
"wrong class variable name %1$s");
4491 rb_check_frozen(mod);
4492 val = rb_ivar_delete(mod,
id,
Qundef);
4493 if (!UNDEF_P(val)) {
4497 rb_name_err_raise(
"cannot remove %1$s for %2$s", mod,
ID2SYM(
id));
4500 rb_name_err_raise(
"class variable %1$s not defined for %2$s",
4519 ID id = rb_intern(name);
4525class_fields_ivar_set(
VALUE klass,
VALUE fields_obj,
ID id,
VALUE val,
bool concurrent,
VALUE *new_fields_obj,
bool *new_ivar_out)
4527 const VALUE original_fields_obj = fields_obj;
4528 fields_obj = original_fields_obj ? original_fields_obj : rb_imemo_fields_new(klass, 1,
true);
4530 shape_id_t current_shape_id = RBASIC_SHAPE_ID(fields_obj);
4531 shape_id_t next_shape_id = current_shape_id;
4532 if (UNLIKELY(rb_shape_too_complex_p(current_shape_id))) {
4537 next_shape_id = generic_shape_ivar(fields_obj,
id, &new_ivar);
4539 if (UNLIKELY(rb_shape_too_complex_p(next_shape_id))) {
4540 fields_obj = imemo_fields_complex_from_obj(klass, fields_obj, next_shape_id);
4544 attr_index_t index = RSHAPE_INDEX(next_shape_id);
4546 if (index >= RSHAPE_CAPACITY(current_shape_id)) {
4549 fields_obj = imemo_fields_copy_capa(klass, fields_obj, RSHAPE_CAPACITY(next_shape_id));
4553 VALUE *fields = rb_imemo_fields_ptr(fields_obj);
4555 if (concurrent && original_fields_obj == fields_obj) {
4561 RB_OBJ_ATOMIC_WRITE(fields_obj, &fields[index], val);
4568 RBASIC_SET_SHAPE_ID(fields_obj, next_shape_id);
4571 *new_fields_obj = fields_obj;
4572 *new_ivar_out = new_ivar;
4577 if (concurrent && fields_obj == original_fields_obj) {
4581 fields_obj = rb_imemo_fields_clone(fields_obj);
4584 st_table *table = rb_imemo_fields_complex_tbl(fields_obj);
4585 new_ivar = !st_insert(table, (st_data_t)
id, (st_data_t)val);
4588 if (fields_obj != original_fields_obj) {
4589 RBASIC_SET_SHAPE_ID(fields_obj, next_shape_id);
4593 *new_fields_obj = fields_obj;
4594 *new_ivar_out = new_ivar;
4595 return ATTR_INDEX_NOT_SET;
4599class_ivar_set(
VALUE obj,
ID id,
VALUE val,
bool *new_ivar)
4601 rb_class_ensure_writable(obj);
4603 const VALUE original_fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
4604 VALUE new_fields_obj = 0;
4606 attr_index_t index = class_fields_ivar_set(obj, original_fields_obj,
id, val, rb_multi_ractor_p(), &new_fields_obj, new_ivar);
4608 if (new_fields_obj != original_fields_obj) {
4609 RCLASS_WRITABLE_SET_FIELDS_OBJ(obj, new_fields_obj);
4615 RBASIC_SET_SHAPE_ID(obj, RBASIC_SHAPE_ID(new_fields_obj));
4623 rb_check_frozen(obj);
4626 class_ivar_set(obj,
id, val, &new_ivar);
4635 RUBY_ASSERT(RSHAPE_TYPE_P(RBASIC_SHAPE_ID(dst), SHAPE_ROOT));
4637 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(src);
4639 RCLASS_WRITABLE_SET_FIELDS_OBJ(dst, rb_imemo_fields_clone(fields_obj));
4640 RBASIC_SET_SHAPE_ID(dst, RBASIC_SHAPE_ID(src));
4651 r = rb_id_table_lookup(tbl,
id, &val);
4660rb_const_lookup(
VALUE klass,
ID id)
4662 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 x)
This is an implementation detail of RB_OBJ_FREEZE().
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define TYPE(_)
Old name of rb_type.
#define FL_UNSET_RAW
Old name of RB_FL_UNSET_RAW.
#define FL_USER3
Old name of RUBY_FL_USER3.
#define REALLOC_N
Old name of RB_REALLOC_N.
#define ALLOC
Old name of RB_ALLOC.
#define T_STRING
Old name of RUBY_T_STRING.
#define xfree
Old name of ruby_xfree.
#define Qundef
Old name of RUBY_Qundef.
#define rb_str_cat2
Old name of rb_str_cat_cstr.
#define UNREACHABLE
Old name of RBIMPL_UNREACHABLE.
#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 ALLOC_N
Old name of RB_ALLOC_N.
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
#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 FL_SET_RAW
Old name of RB_FL_SET_RAW.
#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.
size_t rb_obj_embedded_size(uint32_t fields_count)
Internal header for Object.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
ID rb_check_id_cstr(const char *ptr, long len, rb_encoding *enc)
Identical to rb_check_id(), except it takes a pointer to a memory region instead of Ruby's string.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_hidden_new(long capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Identical to rb_ary_new_from_values(), except it expects exactly two parameters.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
#define st_foreach_safe
Just another name of rb_st_foreach_safe.
int rb_feature_provided(const char *feature, const char **loading)
Identical to rb_provided(), except it additionally returns the "canonical" name of the loaded feature...
VALUE rb_backref_get(void)
Queries the last match, or Regexp.last_match, or the $~.
int rb_is_instance_id(ID id)
Classifies the given ID, then sees if it is an instance variable.
int rb_is_const_id(ID id)
Classifies the given ID, then sees if it is a constant.
int rb_is_class_id(ID id)
Classifies the given ID, then sees if it is a class variable.
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
VALUE rb_reg_nth_defined(int n, VALUE md)
Identical to rb_reg_nth_match(), except it just returns Boolean.
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
VALUE rb_str_subseq(VALUE str, long beg, long len)
Identical to rb_str_substr(), except the numbers are interpreted as byte offsets instead of character...
VALUE rb_str_new_frozen(VALUE str)
Creates a frozen copy of the string, if necessary.
VALUE rb_str_dup(VALUE str)
Duplicates a string.
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
VALUE rb_str_intern(VALUE str)
Identical to rb_to_symbol(), except it assumes the receiver being an instance of RString.
VALUE rb_mutex_new(void)
Creates a mutex.
VALUE rb_mutex_synchronize(VALUE mutex, VALUE(*func)(VALUE arg), VALUE arg)
Obtains the lock, runs the passed function, and releases the lock when it completes.
VALUE rb_exec_recursive_paired(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE p, VALUE h)
Identical to rb_exec_recursive(), except it checks for the recursion on the ordered pair of { g,...
VALUE rb_mod_remove_cvar(VALUE mod, VALUE name)
Resembles Module#remove_class_variable.
VALUE rb_obj_instance_variables(VALUE obj)
Resembles Object#instance_variables.
VALUE rb_f_untrace_var(int argc, const VALUE *argv)
Deletes the passed tracer from the passed global variable, or if omitted, deletes everything.
VALUE rb_const_get(VALUE space, ID name)
Identical to rb_const_defined(), except it returns the actual defined value.
VALUE rb_const_list(void *)
This is another mysterious API that comes with no documents at all.
VALUE rb_path2class(const char *path)
Resolves a Q::W::E::R-style path string to the actual class it points.
VALUE rb_autoload_p(VALUE space, ID name)
Queries if an autoload is defined at a point.
void rb_set_class_path(VALUE klass, VALUE space, const char *name)
Names a class.
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
VALUE rb_mod_remove_const(VALUE space, VALUE name)
Resembles Module#remove_const.
VALUE rb_class_path_cached(VALUE mod)
Just another name of rb_mod_name.
VALUE rb_f_trace_var(int argc, const VALUE *argv)
Traces a global variable.
void rb_cvar_set(VALUE klass, ID name, VALUE val)
Assigns a value to a class variable.
VALUE rb_cvar_get(VALUE klass, ID name)
Obtains a value from a class variable.
VALUE rb_mod_constants(int argc, const VALUE *argv, VALUE recv)
Resembles Module#constants.
VALUE rb_cvar_find(VALUE klass, ID name, VALUE *front)
Identical to rb_cvar_get(), except it takes additional "front" pointer.
VALUE rb_path_to_class(VALUE path)
Identical to rb_path2class(), except it accepts the path as Ruby's string instead of C's.
VALUE rb_ivar_get(VALUE obj, ID name)
Identical to rb_iv_get(), except it accepts the name as an ID instead of a C string.
void rb_const_set(VALUE space, ID name, VALUE val)
Names a constant.
VALUE rb_autoload_load(VALUE space, ID name)
Kicks the autoload procedure as if it was "touched".
VALUE rb_mod_name(VALUE mod)
Queries the name of a module.
VALUE rb_class_name(VALUE obj)
Queries the name of the given object's class.
VALUE rb_const_get_at(VALUE space, ID name)
Identical to rb_const_defined_at(), except it returns the actual defined value.
void rb_set_class_path_string(VALUE klass, VALUE space, VALUE name)
Identical to rb_set_class_path(), except it accepts the name as Ruby's string instead of C's.
void rb_alias_variable(ID dst, ID src)
Aliases a global variable.
void rb_define_class_variable(VALUE, const char *, VALUE)
Just another name of rb_cv_set.
VALUE rb_obj_remove_instance_variable(VALUE obj, VALUE name)
Resembles Object#remove_instance_variable.
void * rb_mod_const_of(VALUE, void *)
This is a variant of rb_mod_const_at().
st_index_t rb_ivar_count(VALUE obj)
Number of instance variables defined on an object.
void * rb_mod_const_at(VALUE, void *)
This API is mysterious.
VALUE rb_const_remove(VALUE space, ID name)
Identical to rb_mod_remove_const(), except it takes the name as ID instead of VALUE.
VALUE rb_const_get_from(VALUE space, ID name)
Identical to rb_const_defined_at(), except it returns the actual defined value.
VALUE rb_ivar_defined(VALUE obj, ID name)
Queries if the instance variable is defined at the object.
VALUE rb_cv_get(VALUE klass, const char *name)
Identical to rb_cvar_get(), except it accepts C's string instead of ID.
int rb_const_defined_at(VALUE space, ID name)
Identical to rb_const_defined(), except it doesn't look for parent classes.
void rb_cv_set(VALUE klass, const char *name, VALUE val)
Identical to rb_cvar_set(), except it accepts C's string instead of ID.
VALUE rb_mod_class_variables(int argc, const VALUE *argv, VALUE recv)
Resembles Module#class_variables.
VALUE rb_f_global_variables(void)
Queries the list of global variables.
VALUE rb_cvar_defined(VALUE klass, ID name)
Queries if the given class has the given class variable.
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
int rb_const_defined_from(VALUE space, ID name)
Identical to rb_const_defined(), except it returns false for private constants.
int rb_const_defined(VALUE space, ID name)
Queries if the constant is defined at the namespace.
void rb_free_generic_ivar(VALUE obj)
Frees the list of instance variables.
const char * rb_sourcefile(void)
Resembles __FILE__.
void rb_clear_constant_cache_for_id(ID id)
Clears the inline constant caches associated with a particular ID.
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 len
Length of the buffer.
static bool rb_ractor_shareable_p(VALUE obj)
Queries if multiple Ractors can share the passed object or not.
#define RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
#define MEMMOVE(p1, p2, type, n)
Handy macro to call memmove.
void rb_define_hooked_variable(const char *q, VALUE *w, type *e, void_type *r)
Define a function-backended global variable.
VALUE type(ANYARGS)
ANYARGS-ed function type.
void rb_define_virtual_variable(const char *q, type *w, void_type *e)
Define a function-backended global variable.
void rb_ivar_foreach(VALUE q, int_type *w, VALUE e)
Iteration over each instance variable of the object.
VALUE rb_ensure(type *q, VALUE w, type *e, VALUE r)
An equivalent of ensure clause.
void rb_copy_generic_ivar(VALUE clone, VALUE obj)
Copies the list of instance variables.
#define RARRAY_LEN
Just another name of rb_array_len.
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
#define RBASIC(obj)
Convenient casting macro.
#define RCLASS_SUPER
Just another name of rb_class_get_superclass.
#define ROBJECT(obj)
Convenient casting macro.
static VALUE * ROBJECT_FIELDS(VALUE obj)
Queries the instance variables.
#define StringValue(v)
Ensures that the parameter object is a String.
static char * RSTRING_END(VALUE str)
Queries the end of the contents pointer of the string.
static bool RTYPEDDATA_P(VALUE obj)
Checks whether the passed object is RTypedData or RData.
#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.
This is the struct that holds necessary info for a struct.
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.