14#include "ruby/internal/config.h"
17#include "ccan/list/list.h"
19#include "debug_counter.h"
23#include "internal/class.h"
24#include "internal/compilers.h"
25#include "internal/error.h"
26#include "internal/eval.h"
27#include "internal/hash.h"
28#include "internal/object.h"
29#include "internal/re.h"
30#include "internal/symbol.h"
31#include "internal/thread.h"
32#include "internal/variable.h"
40#include "ractor_core.h"
44#define GET_GLOBAL_CVAR_STATE() (ruby_vm_global_cvar_state)
46typedef void rb_gvar_compact_t(
void *var);
54static VALUE autoload_features;
59static VALUE autoload_mutex;
61static void check_before_mod_set(
VALUE,
ID,
VALUE,
const char *);
62static void setup_const_entry(rb_const_entry_t *,
VALUE,
VALUE, rb_const_flag_t);
63static VALUE rb_const_search(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility);
64static st_table *generic_iv_tbl_;
69 rb_global_tbl = rb_id_table_create(0);
70 generic_iv_tbl_ = st_init_numtable();
75 rb_vm_register_global_object(autoload_mutex);
77 autoload_features = rb_ident_hash_new();
79 rb_vm_register_global_object(autoload_features);
83rb_namespace_p(
VALUE obj)
104classname(
VALUE klass,
bool *permanent)
108 VALUE classpath = RCLASS_EXT(klass)->classpath;
109 if (classpath == 0)
return Qnil;
111 *permanent = RCLASS_EXT(klass)->permanent_classpath;
117rb_mod_name0(
VALUE klass,
bool *permanent)
119 return classname(klass, permanent);
134 return classname(mod, &permanent);
139is_constant_path(
VALUE name)
141 const char *path = RSTRING_PTR(name);
143 rb_encoding *enc = rb_enc_get(name);
145 const char *p = path;
147 if (p >= pend || !*p) {
152 if (p + 2 <= pend && p[0] ==
':' && p[1] ==
':') {
156 const char *pbeg = p;
157 while (p < pend && *p !=
':') p++;
159 if (pbeg == p)
return false;
161 if (rb_enc_symname_type(pbeg, p - pbeg, enc, 0) != ID_CONST) {
178set_sub_temporary_name_recursive(
VALUE mod,
VALUE data,
int recursive)
180 if (recursive)
return Qfalse;
185 name = build_const_path(rb_ary_last(0, 0, args->names), args->last);
187 set_sub_temporary_name_foreach(mod, args, name);
192set_sub_temporary_name_topmost(
VALUE mod,
VALUE data,
int recursive)
194 if (recursive)
return Qfalse;
197 VALUE name = args->names;
199 args->names = rb_ary_hidden_new(0);
201 set_sub_temporary_name_foreach(mod, args, name);
205static enum rb_id_table_iterator_result
206set_sub_temporary_name_i(
ID id,
VALUE val,
void *data)
208 val = ((rb_const_entry_t *)val)->value;
209 if (rb_namespace_p(val) && !RCLASS_EXT(val)->permanent_classpath) {
215 return ID_TABLE_CONTINUE;
221 RCLASS_SET_CLASSPATH(mod, name, FALSE);
225 rb_id_table_foreach(tbl, set_sub_temporary_name_i, args);
229 rb_ary_push(args->names, name);
230 rb_id_table_foreach(tbl, set_sub_temporary_name_i, args);
231 rb_ary_set_len(args->names, names_len);
292rb_mod_set_temporary_name(
VALUE mod,
VALUE name)
295 if (RCLASS_EXT(mod)->permanent_classpath) {
302 set_sub_temporary_name(mod, 0);
309 if (RSTRING_LEN(name) == 0) {
310 rb_raise(rb_eArgError,
"empty class/module name");
313 if (is_constant_path(name)) {
314 rb_raise(rb_eArgError,
"the temporary name must not be a constant path to avoid confusion");
321 set_sub_temporary_name(mod, name);
334 path = rb_sprintf(
"#<Class:%p>", (
void*)obj);
337 path = rb_sprintf(
"#<Module:%p>", (
void*)obj);
340 path = rb_sprintf(
"#<%"PRIsVALUE
":%p>", klass, (
void*)obj);
350rb_tmp_class_path(
VALUE klass,
bool *permanent, fallback_func fallback)
352 VALUE path = classname(klass, permanent);
364 path = rb_tmp_class_path(
RBASIC(klass)->klass, &perm, fallback);
369 return fallback(klass, path);
376 VALUE path = rb_tmp_class_path(klass, &permanent, make_temporary_path);
394rb_search_class_path(
VALUE klass)
397 return rb_tmp_class_path(klass, &permanent, no_fallback);
406 return rb_fstring(path);
410build_const_path(
VALUE head,
ID tail)
412 return build_const_pathname(head, rb_id2str(tail));
418 bool permanent =
true;
421 if (under == rb_cObject) {
425 str = rb_tmp_class_path(under, &permanent, make_temporary_path);
426 str = build_const_pathname(str, name);
429 RCLASS_SET_CLASSPATH(klass, str, permanent);
443 rb_encoding *enc = rb_enc_get(pathname);
444 const char *pbeg, *pend, *p, *path = RSTRING_PTR(pathname);
446 VALUE c = rb_cObject;
448 if (!rb_enc_asciicompat(enc)) {
449 rb_raise(rb_eArgError,
"invalid class path encoding (non ASCII)");
452 pend = path + RSTRING_LEN(pathname);
453 if (path == pend || path[0] ==
'#') {
454 rb_raise(rb_eArgError,
"can't retrieve anonymous class %"PRIsVALUE,
458 while (p < pend && *p !=
':') p++;
460 if (p < pend && p[0] ==
':') {
461 if ((
size_t)(pend - p) < 2 || p[1] !=
':')
goto undefined_class;
466 goto undefined_class;
468 c = rb_const_search(c,
id, TRUE, FALSE, FALSE);
469 if (UNDEF_P(c))
goto undefined_class;
470 if (!rb_namespace_p(c)) {
471 rb_raise(
rb_eTypeError,
"%"PRIsVALUE
" does not refer to class/module",
480 rb_raise(rb_eArgError,
"undefined class/module % "PRIsVALUE,
501 VALUE path = rb_tmp_class_path(
rb_class_real(klass), &permanent, make_temporary_path);
502 if (
NIL_P(path))
return NULL;
503 return RSTRING_PTR(path);
526 rb_gvar_compact_t *compactor;
550static enum rb_id_table_iterator_result
551free_global_entry_i(
VALUE val,
void *arg)
554 entry->var->counter--;
555 if (entry->var->counter == 0) {
556 free_global_variable(entry->var);
559 return ID_TABLE_DELETE;
563rb_free_rb_global_tbl(
void)
565 rb_id_table_foreach_values(rb_global_tbl, free_global_entry_i, 0);
566 rb_id_table_free(rb_global_tbl);
570rb_free_generic_iv_tbl_(
void)
572 st_free_table(generic_iv_tbl_);
576rb_find_global_entry(
ID id)
581 if (!rb_id_table_lookup(rb_global_tbl,
id, &data)) {
589 if (UNLIKELY(!rb_ractor_main_p()) && (!entry || !entry->ractor_local)) {
590 rb_raise(rb_eRactorIsolationError,
"can not access global variables %s from non-main Ractors", rb_id2name(
id));
597rb_gvar_ractor_local(
const char *name)
600 entry->ractor_local =
true;
604rb_gvar_undef_compactor(
void *var)
618 entry->ractor_local =
false;
624 var->compactor = rb_gvar_undef_compactor;
626 var->block_trace = 0;
628 rb_id_table_insert(rb_global_tbl,
id, (
VALUE)entry);
636 rb_warning(
"global variable '%"PRIsVALUE
"' not initialized", QUOTE_ID(
id));
642rb_gvar_val_compactor(
void *_var)
649 VALUE new = rb_gc_location(obj);
651 var->data = (
void*)
new;
663 var->compactor = rb_gvar_val_compactor;
665 var->data = (
void*)val;
683 var->data = (
void*)val;
690 if (data) rb_gc_mark_movable(data);
696 if (!var)
return Qnil;
709 if (var) rb_gc_mark_maybe(*var);
715 rb_name_error(
id,
"%"PRIsVALUE
" is a read-only variable", QUOTE_ID(
id));
718static enum rb_id_table_iterator_result
719mark_global_entry(
VALUE v,
void *ignored)
725 (*var->marker)(var->data);
728 if (trace->data) rb_gc_mark_maybe(trace->data);
731 return ID_TABLE_CONTINUE;
734#define gc_mark_table(task) \
735 if (rb_global_tbl) { rb_id_table_foreach_values(rb_global_tbl, task##_global_entry, 0); }
738rb_gc_mark_global_tbl(
void)
743static enum rb_id_table_iterator_result
744update_global_entry(
VALUE v,
void *ignored)
749 (*var->compactor)(var);
750 return ID_TABLE_CONTINUE;
754rb_gc_update_global_tbl(
void)
756 gc_mark_table(update);
760global_id(
const char *name)
764 if (name[0] ==
'$')
id = rb_intern(name);
766 size_t len = strlen(name);
770 memcpy(buf+1, name,
len);
771 id = rb_intern2(buf,
len+1);
778find_global_id(
const char *name)
781 size_t len = strlen(name);
783 if (name[0] ==
'$') {
790 memcpy(buf+1, name,
len);
806 ID id = global_id(name);
809 gvar->data = (
void*)var;
861 trace->next = entry->var->trace;
862 trace->func = rb_trace_eval;
865 entry->var->trace = trace;
879 while (trace->next) {
882 trace->next = next->next;
905 if ((entry = rb_find_global_entry(
id)) == NULL) {
906 rb_name_error(
id,
"undefined global variable %"PRIsVALUE
"", QUOTE_ID(
id));
909 trace = entry->var->trace;
911 VALUE ary = rb_ary_new();
915 rb_ary_push(ary, (
VALUE)trace->data);
920 if (!entry->var->block_trace) remove_trace(entry->var);
925 if (trace->data == cmd) {
927 if (!entry->var->block_trace) remove_trace(entry->var);
948 (*trace->func)(trace->data, data->val);
959 var->block_trace = 0;
970 (*var->setter)(val, entry->id, var->data);
972 if (var->trace && !var->block_trace) {
973 var->block_trace = 1;
974 trace.trace = var->trace;
987 return rb_gvar_set_entry(entry, val);
993 return rb_gvar_set(global_id(name), val);
1001 return (*var->getter)(entry->id, var->data);
1007 ID id = find_global_id(name);
1010 rb_warning(
"global variable '%s' not initialized", name);
1014 return rb_gvar_get(
id);
1018rb_gvar_defined(
ID id)
1025rb_gvar_getter_function_of(
ID id)
1028 return entry->var->getter;
1032rb_gvar_setter_function_of(
ID id)
1035 return entry->var->setter;
1038static enum rb_id_table_iterator_result
1039gvar_i(
ID key,
VALUE val,
void *a)
1042 rb_ary_push(ary,
ID2SYM(key));
1043 return ID_TABLE_CONTINUE;
1049 VALUE ary = rb_ary_new();
1052 if (!rb_ractor_main_p()) {
1053 rb_raise(rb_eRactorIsolationError,
"can not access global variables from non-main Ractors");
1056 rb_id_table_foreach(rb_global_tbl, gvar_i, (
void *)ary);
1057 if (!
NIL_P(backref)) {
1059 int i, nmatch = rb_match_count(backref);
1061 for (i = 1; i <= nmatch; ++i) {
1065 buf[1] = (char)(i +
'0');
1066 sym =
ID2SYM(rb_intern2(buf, 2));
1072 rb_ary_push(ary, sym);
1085 if (!rb_ractor_main_p()) {
1086 rb_raise(rb_eRactorIsolationError,
"can not access global variables from non-main Ractors");
1090 if (!rb_id_table_lookup(gtbl, name1, &data1)) {
1093 rb_id_table_insert(gtbl, name1, (
VALUE)entry1);
1095 else if ((entry1 = (
struct rb_global_entry *)data1)->var != entry2->var) {
1097 if (var->block_trace) {
1101 if (var->counter == 0) {
1102 free_global_variable(var);
1108 entry2->var->counter++;
1109 entry1->var = entry2->var;
1113IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(
ID id)
1115 if (UNLIKELY(!rb_ractor_main_p())) {
1117 rb_raise(rb_eRactorIsolationError,
"can not set instance variables of classes/modules by non-main Ractors");
1122#define CVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR() \
1123 if (UNLIKELY(!rb_ractor_main_p())) { \
1124 rb_raise(rb_eRactorIsolationError, "can not access class variables from non-main Ractors"); \
1127static inline struct st_table *
1128generic_ivtbl(
VALUE obj,
ID id,
bool force_check_ractor)
1130 ASSERT_vm_locking();
1134 UNLIKELY(!rb_ractor_main_p()) &&
1137 rb_raise(rb_eRactorIsolationError,
"can not access instance variables of shareable objects from non-main Ractors");
1139 return generic_iv_tbl_;
1142static inline struct st_table *
1143generic_ivtbl_no_ractor_check(
VALUE obj)
1145 return generic_ivtbl(obj, 0,
false);
1149rb_generic_ivtbl_get(
void)
1151 return generic_iv_tbl_;
1164 if (st_lookup(generic_ivtbl(obj,
id,
false), (st_data_t)obj, &data)) {
1177 return rb_gen_ivtbl_get(obj, 0, ivtbl);
1181gen_ivtbl_bytes(
size_t n)
1183 return offsetof(
struct gen_ivtbl, as.shape.ivptr) + n *
sizeof(
VALUE);
1187gen_ivtbl_resize(
struct gen_ivtbl *old, uint32_t n)
1191 uint32_t
len = old ? old->as.shape.numiv : 0;
1194 ivtbl->as.shape.numiv = n;
1203rb_mark_generic_ivar(
VALUE obj)
1206 if (st_lookup(generic_ivtbl_no_ractor_check(obj), (st_data_t)obj, &data)) {
1208 if (rb_shape_obj_too_complex(obj)) {
1209 rb_mark_tbl_no_pin(ivtbl->as.complex.table);
1212 for (uint32_t i = 0; i < ivtbl->as.shape.numiv; i++) {
1213 rb_gc_mark_movable(ivtbl->as.shape.ivptr[i]);
1220rb_ref_update_generic_ivar(
VALUE obj)
1224 if (rb_gen_ivtbl_get(obj, 0, &ivtbl)) {
1225 if (rb_shape_obj_too_complex(obj)) {
1226 rb_gc_ref_update_table_values_only(ivtbl->as.complex.table);
1229 for (uint32_t i = 0; i < ivtbl->as.shape.numiv; i++) {
1230 ivtbl->as.shape.ivptr[i] = rb_gc_location(ivtbl->as.shape.ivptr[i]);
1239 st_data_t key = (st_data_t)rsrc;
1242 if (st_delete(generic_ivtbl_no_ractor_check(rsrc), &key, &ivtbl))
1243 st_insert(generic_ivtbl_no_ractor_check(dst), (st_data_t)dst, ivtbl);
1249 st_data_t key = (st_data_t)obj, value;
1251 bool too_complex = rb_shape_obj_too_complex(obj);
1253 if (st_delete(generic_ivtbl_no_ractor_check(obj), &key, &value)) {
1256 if (UNLIKELY(too_complex)) {
1257 st_free_table(ivtbl->as.complex.table);
1265rb_generic_ivar_memsize(
VALUE obj)
1269 if (rb_gen_ivtbl_get(obj, 0, &ivtbl)) {
1270 if (rb_shape_obj_too_complex(obj)) {
1271 return sizeof(
struct gen_ivtbl) + st_memsize(ivtbl->as.complex.table);
1274 return gen_ivtbl_bytes(ivtbl->as.shape.numiv);
1280#if !SHAPE_IN_BASIC_FLAGS
1282rb_generic_shape_id(
VALUE obj)
1285 shape_id_t shape_id = 0;
1289 st_table* global_iv_table = generic_ivtbl(obj, 0,
false);
1291 if (global_iv_table && st_lookup(global_iv_table, obj, (st_data_t *)&ivtbl)) {
1292 shape_id = ivtbl->shape_id;
1295 shape_id = SPECIAL_CONST_SHAPE_ID;
1310 if (rb_shape_obj_too_complex(obj)) {
1311 n = st_table_size(ivtbl->as.complex.table);
1314 for (i = 0; i < ivtbl->as.shape.numiv; i++) {
1315 if (!UNDEF_P(ivtbl->as.shape.ivptr[i])) {
1329 shape_id_t shape_id;
1333#if SHAPE_IN_BASIC_FLAGS
1334 shape_id = RBASIC_SHAPE_ID(obj);
1346#if !SHAPE_IN_BASIC_FLAGS
1347 shape_id = RCLASS_SHAPE_ID(obj);
1350 if (rb_shape_obj_too_complex(obj)) {
1351 st_table * iv_table = RCLASS_IV_HASH(obj);
1352 if (rb_st_lookup(iv_table, (st_data_t)
id, (st_data_t *)&val)) {
1360 attr_index_t index = 0;
1361 shape = rb_shape_get_shape_by_id(shape_id);
1362 found = rb_shape_get_iv_index(shape,
id, &index);
1365 ivar_list = RCLASS_IVPTR(obj);
1368 val = ivar_list[index];
1379 UNLIKELY(!rb_ractor_main_p()) &&
1381 rb_raise(rb_eRactorIsolationError,
1382 "can not get unshareable values from instance variables of classes/modules from non-main Ractors");
1388#if !SHAPE_IN_BASIC_FLAGS
1389 shape_id = ROBJECT_SHAPE_ID(obj);
1391 if (rb_shape_obj_too_complex(obj)) {
1392 st_table * iv_table = ROBJECT_IV_HASH(obj);
1394 if (rb_st_lookup(iv_table, (st_data_t)
id, (st_data_t *)&val)) {
1409 rb_gen_ivtbl_get(obj,
id, &ivtbl);
1411 if (rb_shape_obj_too_complex(obj)) {
1413 if (rb_st_lookup(ivtbl->as.complex.table, (st_data_t)
id, (st_data_t *)&val)) {
1421#if !SHAPE_IN_BASIC_FLAGS
1422 shape_id = ivtbl->shape_id;
1424 ivar_list = ivtbl->as.shape.ivptr;
1432 attr_index_t index = 0;
1433 shape = rb_shape_get_shape_by_id(shape_id);
1434 if (rb_shape_get_iv_index(shape,
id, &index)) {
1435 return ivar_list[index];
1444 VALUE iv = rb_ivar_lookup(obj,
id,
Qnil);
1445 RB_DEBUG_COUNTER_INC(ivar_get_base);
1452 return rb_ivar_lookup(obj,
id,
Qnil);
1458 rb_check_frozen(obj);
1461 rb_shape_t *shape = rb_shape_get_shape(obj);
1464 IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(
id);
1467 if (!rb_shape_transition_shape_remove_ivar(obj,
id, shape, &val)) {
1468 if (!rb_shape_obj_too_complex(obj)) {
1469 rb_evict_ivars_to_hash(obj);
1472 st_table *table = NULL;
1476 table = RCLASS_IV_HASH(obj);
1480 table = ROBJECT_IV_HASH(obj);
1485 if (rb_gen_ivtbl_get(obj, 0, &ivtbl)) {
1486 table = ivtbl->as.complex.table;
1493 if (!st_delete(table, (st_data_t *)&
id, (st_data_t *)&val)) {
1503rb_attr_delete(
VALUE obj,
ID id)
1505 return rb_ivar_delete(obj,
id,
Qnil);
1509rb_obj_convert_to_too_complex(
VALUE obj, st_table *table)
1513 VALUE *old_ivptr = NULL;
1517 if (!(
RBASIC(obj)->flags & ROBJECT_EMBED)) {
1520 rb_shape_set_shape_id(obj, OBJ_TOO_COMPLEX_SHAPE_ID);
1521 ROBJECT_SET_IV_HASH(obj, table);
1525 old_ivptr = RCLASS_IVPTR(obj);
1526 rb_shape_set_shape_id(obj, OBJ_TOO_COMPLEX_SHAPE_ID);
1527 RCLASS_SET_IV_HASH(obj, table);
1532 struct st_table *gen_ivs = generic_ivtbl_no_ractor_check(obj);
1535 st_lookup(gen_ivs, (st_data_t)obj, (st_data_t *)&old_ivtbl);
1542#if SHAPE_IN_BASIC_FLAGS
1543 rb_shape_set_shape_id(obj, OBJ_TOO_COMPLEX_SHAPE_ID);
1545 old_ivtbl->shape_id = OBJ_TOO_COMPLEX_SHAPE_ID;
1547 old_ivtbl->as.complex.table = table;
1548 old_ivptr = (
VALUE *)old_ivtbl;
1552 ivtbl->as.complex.table = table;
1553 st_insert(gen_ivs, (st_data_t)obj, (st_data_t)ivtbl);
1554#if SHAPE_IN_BASIC_FLAGS
1555 rb_shape_set_shape_id(obj, OBJ_TOO_COMPLEX_SHAPE_ID);
1557 ivtbl->shape_id = OBJ_TOO_COMPLEX_SHAPE_ID;
1567rb_evict_ivars_to_hash(
VALUE obj)
1571 st_table *table = st_init_numtable_with_size(
rb_ivar_count(obj));
1574 rb_obj_copy_ivs_to_hash_table(obj, table);
1575 rb_obj_convert_to_too_complex(obj, table);
1586general_ivar_set(
VALUE obj,
ID id,
VALUE val, void *data,
1588 void (*shape_resize_ivptr_func)(
VALUE, attr_index_t, attr_index_t,
void *),
1589 void (*set_shape_func)(
VALUE, rb_shape_t *,
void *),
1590 void (*transition_too_complex_func)(
VALUE,
void *),
1591 st_table *(*too_complex_table_func)(
VALUE,
void *))
1598 rb_shape_t *current_shape = rb_shape_get_shape(obj);
1600 if (UNLIKELY(current_shape->type == SHAPE_OBJ_TOO_COMPLEX)) {
1605 if (!rb_shape_get_iv_index(current_shape,
id, &index)) {
1606 result.existing =
false;
1608 index = current_shape->next_iv_index;
1609 if (index >= MAX_IVARS) {
1610 rb_raise(rb_eArgError,
"too many instance variables");
1613 rb_shape_t *next_shape = rb_shape_get_next(current_shape, obj,
id);
1614 if (UNLIKELY(next_shape->type == SHAPE_OBJ_TOO_COMPLEX)) {
1615 transition_too_complex_func(obj, data);
1618 else if (UNLIKELY(next_shape->capacity != current_shape->capacity)) {
1619 RUBY_ASSERT(next_shape->capacity > current_shape->capacity);
1620 shape_resize_ivptr_func(obj, current_shape->capacity, next_shape->capacity, data);
1624 RUBY_ASSERT(index == (next_shape->next_iv_index - 1));
1625 set_shape_func(obj, next_shape, data);
1628 VALUE *table = shape_ivptr_func(obj, data);
1631 result.index = index;
1638 st_table *table = too_complex_table_func(obj, data);
1639 result.existing = st_insert(table, (st_data_t)
id, (st_data_t)val);
1655generic_ivar_lookup_ensure_size(st_data_t *k, st_data_t *v, st_data_t u,
int existing)
1657 ASSERT_vm_locking();
1662 if (!existing || ivar_lookup->resize) {
1664 RUBY_ASSERT(ivar_lookup->shape->type == SHAPE_IVAR);
1665 RUBY_ASSERT(rb_shape_get_shape_by_id(ivar_lookup->shape->parent_id)->capacity < ivar_lookup->shape->capacity);
1671 ivtbl = gen_ivtbl_resize(ivtbl, ivar_lookup->shape->capacity);
1672 *v = (st_data_t)ivtbl;
1677 ivar_lookup->ivtbl = ivtbl;
1678 if (ivar_lookup->shape) {
1679#if SHAPE_IN_BASIC_FLAGS
1680 rb_shape_set_shape(ivar_lookup->obj, ivar_lookup->shape);
1682 ivtbl->shape_id = rb_shape_id(ivar_lookup->shape);
1690generic_ivar_set_shape_ivptr(
VALUE obj,
void *data)
1698 st_update(generic_ivtbl(obj, ivar_lookup->id,
false), (st_data_t)obj, generic_ivar_lookup_ensure_size, (st_data_t)ivar_lookup);
1704 return ivar_lookup->ivtbl->as.shape.ivptr;
1708generic_ivar_set_shape_resize_ivptr(
VALUE obj, attr_index_t _old_capa, attr_index_t new_capa,
void *data)
1712 ivar_lookup->resize =
true;
1716generic_ivar_set_set_shape(
VALUE obj, rb_shape_t *shape,
void *data)
1720 ivar_lookup->shape = shape;
1724generic_ivar_set_transition_too_complex(
VALUE obj,
void *_data)
1726 rb_evict_ivars_to_hash(obj);
1731generic_ivar_set_too_complex_table(
VALUE obj,
void *data)
1736 if (!rb_gen_ivtbl_get(obj, 0, &ivtbl)) {
1738#if !SHAPE_IN_BASIC_FLAGS
1739 ivtbl->shape_id = SHAPE_OBJ_TOO_COMPLEX;
1741 ivtbl->as.complex.table = st_init_numtable_with_size(1);
1745 st_insert(generic_ivtbl(obj, ivar_lookup->id,
false), (st_data_t)obj, (st_data_t)ivtbl);
1754 return ivtbl->as.complex.table;
1767 general_ivar_set(obj,
id, val, &ivar_lookup,
1768 generic_ivar_set_shape_ivptr,
1769 generic_ivar_set_shape_resize_ivptr,
1770 generic_ivar_set_set_shape,
1771 generic_ivar_set_transition_too_complex,
1772 generic_ivar_set_too_complex_table);
1776rb_ensure_iv_list_size(
VALUE obj, uint32_t current_capacity, uint32_t new_capacity)
1780 if (
RBASIC(obj)->flags & ROBJECT_EMBED) {
1785 ROBJECT(obj)->as.heap.ivptr = newptr;
1793rb_obj_copy_ivs_to_hash_table_i(
ID key,
VALUE val, st_data_t arg)
1795 RUBY_ASSERT(!st_lookup((st_table *)arg, (st_data_t)key, NULL));
1797 st_add_direct((st_table *)arg, (st_data_t)key, (st_data_t)val);
1802rb_obj_copy_ivs_to_hash_table(
VALUE obj, st_table *table)
1804 rb_ivar_foreach(obj, rb_obj_copy_ivs_to_hash_table_i, (st_data_t)table);
1808obj_ivar_set_shape_ivptr(
VALUE obj,
void *_data)
1816obj_ivar_set_shape_resize_ivptr(
VALUE obj, attr_index_t old_capa, attr_index_t new_capa,
void *_data)
1818 rb_ensure_iv_list_size(obj, old_capa, new_capa);
1822obj_ivar_set_set_shape(
VALUE obj, rb_shape_t *shape,
void *_data)
1824 rb_shape_set_shape(obj, shape);
1828obj_ivar_set_transition_too_complex(
VALUE obj,
void *_data)
1830 rb_evict_ivars_to_hash(obj);
1834obj_ivar_set_too_complex_table(
VALUE obj,
void *_data)
1838 return ROBJECT_IV_HASH(obj);
1844 return general_ivar_set(obj,
id, val, NULL,
1845 obj_ivar_set_shape_ivptr,
1846 obj_ivar_set_shape_resize_ivptr,
1847 obj_ivar_set_set_shape,
1848 obj_ivar_set_transition_too_complex,
1849 obj_ivar_set_too_complex_table).index;
1859 rb_check_frozen(obj);
1860 rb_obj_ivar_set(obj,
id, val);
1865rb_shape_set_shape_id(
VALUE obj, shape_id_t shape_id)
1867 if (rb_shape_get_shape_id(obj) == shape_id) {
1871#if SHAPE_IN_BASIC_FLAGS
1872 RBASIC_SET_SHAPE_ID(obj, shape_id);
1876 ROBJECT_SET_SHAPE_ID(obj, shape_id);
1880 RCLASS_SET_SHAPE_ID(obj, shape_id);
1883 if (shape_id != SPECIAL_CONST_SHAPE_ID) {
1887 st_table* global_iv_table = generic_ivtbl(obj, 0,
false);
1889 if (st_lookup(global_iv_table, obj, (st_data_t *)&ivtbl)) {
1890 ivtbl->shape_id = shape_id;
1893 rb_bug(
"Expected shape_id entry in global iv table");
1912 rb_shape_t * next_shape = rb_shape_transition_shape_frozen(x);
1916 if (!rb_shape_obj_too_complex(x) && next_shape->type == SHAPE_OBJ_TOO_COMPLEX) {
1917 rb_evict_ivars_to_hash(x);
1919 rb_shape_set_shape(x, next_shape);
1930 RB_DEBUG_COUNTER_INC(ivar_set_base);
1935 rb_obj_ivar_set(obj,
id, val);
1940 IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(
id);
1941 rb_class_ivar_set(obj,
id, val);
1945 generic_ivar_set(obj,
id, val);
1953 rb_check_frozen(obj);
1954 ivar_set(obj,
id, val);
1964 ivar_set(obj,
id, val);
1973 if (rb_shape_obj_too_complex(obj)) {
1975 st_table *table = NULL;
1979 table = (st_table *)RCLASS_IVPTR(obj);
1983 table = ROBJECT_IV_HASH(obj);
1988 if (rb_gen_ivtbl_get(obj, 0, &ivtbl)) {
1989 table = ivtbl->as.complex.table;
1995 if (!table || !rb_st_lookup(table,
id, &idx)) {
2002 return RBOOL(rb_shape_get_iv_index(rb_shape_get_shape(obj),
id, &index));
2006typedef int rb_ivar_foreach_callback_func(
ID key,
VALUE val, st_data_t arg);
2007st_data_t rb_st_nth_key(st_table *tab, st_index_t index);
2013 rb_ivar_foreach_callback_func *func;
2020iterate_over_shapes_with_callback(rb_shape_t *shape, rb_ivar_foreach_callback_func *callback,
struct iv_itr_data * itr_data)
2022 switch ((
enum shape_type)shape->type) {
2024 case SHAPE_T_OBJECT:
2028 if (iterate_over_shapes_with_callback(rb_shape_get_parent(shape), callback, itr_data))
2033 RUBY_ASSERT(!rb_shape_obj_too_complex(itr_data->obj));
2038 iv_list = RCLASS_IVPTR(itr_data->obj);
2041 iv_list = itr_data->ivtbl->as.shape.ivptr;
2044 VALUE val = iv_list[shape->next_iv_index - 1];
2045 if (!UNDEF_P(val)) {
2046 switch (callback(shape->edge_name, val, itr_data->arg)) {
2053 rb_bug(
"unreachable");
2058 return iterate_over_shapes_with_callback(rb_shape_get_parent(shape), callback, itr_data);
2059 case SHAPE_OBJ_TOO_COMPLEX:
2061 rb_bug(
"Unreachable");
2066each_hash_iv(st_data_t
id, st_data_t val, st_data_t data)
2069 rb_ivar_foreach_callback_func *callback = itr_data->func;
2070 return callback((
ID)
id, (
VALUE)val, itr_data->arg);
2074obj_ivar_each(
VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg)
2076 rb_shape_t* shape = rb_shape_get_shape(obj);
2080 itr_data.func = func;
2081 if (rb_shape_obj_too_complex(obj)) {
2082 rb_st_foreach(ROBJECT_IV_HASH(obj), each_hash_iv, (st_data_t)&itr_data);
2085 iterate_over_shapes_with_callback(shape, func, &itr_data);
2090gen_ivar_each(
VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg)
2092 rb_shape_t *shape = rb_shape_get_shape(obj);
2094 if (!rb_gen_ivtbl_get(obj, 0, &ivtbl))
return;
2098 itr_data.ivtbl = ivtbl;
2100 itr_data.func = func;
2101 if (rb_shape_obj_too_complex(obj)) {
2102 rb_st_foreach(ivtbl->as.complex.table, each_hash_iv, (st_data_t)&itr_data);
2105 iterate_over_shapes_with_callback(shape, func, &itr_data);
2110class_ivar_each(
VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg)
2114 rb_shape_t* shape = rb_shape_get_shape(obj);
2118 itr_data.func = func;
2119 if (rb_shape_obj_too_complex(obj)) {
2120 rb_st_foreach(RCLASS_IV_HASH(obj), each_hash_iv, (st_data_t)&itr_data);
2123 iterate_over_shapes_with_callback(shape, func, &itr_data);
2133 rb_check_frozen(clone);
2139 if (rb_gen_ivtbl_get(obj, 0, &obj_ivtbl)) {
2140 if (gen_ivtbl_count(obj, obj_ivtbl) == 0)
2145 if (rb_shape_obj_too_complex(obj)) {
2147#if !SHAPE_IN_BASIC_FLAGS
2148 new_ivtbl->shape_id = SHAPE_OBJ_TOO_COMPLEX;
2150 new_ivtbl->as.complex.table = st_copy(obj_ivtbl->as.complex.table);
2153 new_ivtbl = gen_ivtbl_resize(0, obj_ivtbl->as.shape.numiv);
2155 for (uint32_t i=0; i<obj_ivtbl->as.shape.numiv; i++) {
2156 RB_OBJ_WRITE(clone, &new_ivtbl->as.shape.ivptr[i], obj_ivtbl->as.shape.ivptr[i]);
2166 generic_ivtbl_no_ractor_check(clone);
2167 st_insert(generic_ivtbl_no_ractor_check(obj), (st_data_t)clone, (st_data_t)new_ivtbl);
2171 rb_shape_t * obj_shape = rb_shape_get_shape(obj);
2172 if (rb_shape_frozen_shape_p(obj_shape)) {
2173 rb_shape_set_shape_id(clone, obj_shape->parent_id);
2176 rb_shape_set_shape(clone, obj_shape);
2189rb_replace_generic_ivar(
VALUE clone,
VALUE obj)
2195 st_data_t ivtbl, obj_data = (st_data_t)obj;
2196 if (st_lookup(generic_iv_tbl_, (st_data_t)obj, &ivtbl)) {
2197 st_insert(generic_iv_tbl_, (st_data_t)clone, ivtbl);
2198 st_delete(generic_iv_tbl_, &obj_data, NULL);
2201 rb_bug(
"unreachable");
2215 obj_ivar_each(obj, func, arg);
2219 IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(0);
2222 class_ivar_each(obj, func, arg);
2228 gen_ivar_each(obj, func, arg);
2241 return ROBJECT_IV_COUNT(obj);
2244 return RCLASS_IV_COUNT(obj);
2249 if (rb_gen_ivtbl_get(obj, 0, &ivtbl)) {
2250 return gen_ivtbl_count(obj, ivtbl);
2259ivar_i(
ID key,
VALUE v, st_data_t a)
2264 rb_ary_push(ary,
ID2SYM(key));
2296#define rb_is_constant_id rb_is_const_id
2297#define rb_is_constant_name rb_is_const_name
2298#define id_for_var(obj, name, part, type) \
2299 id_for_var_message(obj, name, type, "'%1$s' is not allowed as "#part" "#type" variable name")
2300#define id_for_var_message(obj, name, type, message) \
2301 check_id_type(obj, &(name), rb_is_##type##_id, rb_is_##type##_name, message, strlen(message))
2304 int (*valid_id_p)(
ID),
int (*valid_name_p)(
VALUE),
2305 const char *message,
size_t message_len)
2308 VALUE name = *pname;
2310 if (
id ? !valid_id_p(
id) : !valid_name_p(name)) {
2311 rb_name_err_raise_str(rb_fstring_new(message, message_len),
2343 const ID id = id_for_var(obj, name, an, instance);
2347 rb_check_frozen(obj);
2352 if (!UNDEF_P(val))
return val;
2355 rb_name_err_raise(
"instance variable %1$s not defined",
2360NORETURN(
static void uninitialized_constant(
VALUE,
VALUE));
2362uninitialized_constant(
VALUE klass,
VALUE name)
2365 rb_name_err_raise(
"uninitialized constant %2$s::%1$s",
2368 rb_name_err_raise(
"uninitialized constant %1$s",
2375 VALUE value = rb_funcallv(klass, idConst_missing, 1, &name);
2376 rb_vm_inc_const_missing_count();
2424 rb_execution_context_t *ec = GET_EC();
2425 VALUE ref = ec->private_const_reference;
2426 rb_vm_pop_cfunc_frame();
2428 ec->private_const_reference = 0;
2429 rb_name_err_raise(
"private constant %2$s::%1$s referenced", ref, name);
2431 uninitialized_constant(klass, name);
2437autoload_table_mark(
void *ptr)
2439 rb_mark_tbl_no_pin((st_table *)ptr);
2443autoload_table_free(
void *ptr)
2445 st_free_table((st_table *)ptr);
2449autoload_table_memsize(
const void *ptr)
2451 const st_table *tbl = ptr;
2452 return st_memsize(tbl);
2456autoload_table_compact(
void *ptr)
2458 rb_gc_ref_update_table_values_only((st_table *)ptr);
2463 {autoload_table_mark, autoload_table_free, autoload_table_memsize, autoload_table_compact,},
2464 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
2467#define check_autoload_table(av) \
2468 (struct st_table *)rb_check_typeddata((av), &autoload_table_type)
2473 struct st_table *tbl;
2483 mod =
RBASIC(mod)->klass;
2491 VALUE tbl_value = rb_ivar_lookup(mod, autoload,
Qfalse);
2492 if (!
RTEST(tbl_value) || !(tbl = check_autoload_table(tbl_value)) || !st_lookup(tbl, (st_data_t)
id, &val)) {
2502 struct ccan_list_node cnode;
2505 VALUE autoload_data_value;
2517 rb_const_flag_t flag;
2533 rb_serial_t fork_gen;
2536 struct ccan_list_head constants;
2540autoload_data_compact(
void *ptr)
2544 p->feature = rb_gc_location(p->feature);
2545 p->mutex = rb_gc_location(p->mutex);
2549autoload_data_mark(
void *ptr)
2553 rb_gc_mark_movable(p->feature);
2554 rb_gc_mark_movable(p->mutex);
2558autoload_data_free(
void *ptr)
2563 ccan_list_for_each_safe(&p->constants,
autoload_const, next, cnode) {
2571autoload_data_memsize(
const void *ptr)
2578 {autoload_data_mark, autoload_data_free, autoload_data_memsize, autoload_data_compact},
2579 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
2583autoload_const_compact(
void *ptr)
2587 ac->module = rb_gc_location(ac->module);
2588 ac->autoload_data_value = rb_gc_location(ac->autoload_data_value);
2589 ac->value = rb_gc_location(ac->value);
2590 ac->file = rb_gc_location(ac->file);
2594autoload_const_mark(
void *ptr)
2598 rb_gc_mark_movable(ac->module);
2599 rb_gc_mark_movable(ac->autoload_data_value);
2600 rb_gc_mark_movable(ac->value);
2601 rb_gc_mark_movable(ac->file);
2605autoload_const_memsize(
const void *ptr)
2611autoload_const_free(
void *ptr)
2621 {autoload_const_mark, autoload_const_free, autoload_const_memsize, autoload_const_compact,},
2622 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
2626get_autoload_data(
VALUE autoload_const_value,
struct autoload_const **autoload_const_pointer)
2639 if (autoload_const_pointer) *autoload_const_pointer =
autoload_const;
2645rb_autoload(
VALUE module,
ID name,
const char *feature)
2647 if (!feature || !*feature) {
2648 rb_raise(rb_eArgError,
"empty feature name");
2651 rb_autoload_str(module, name, rb_fstring_cstr(feature));
2655static void const_added(
VALUE klass,
ID const_name);
2664autoload_feature_lookup_or_create(
VALUE feature,
struct autoload_data **autoload_data_pointer)
2666 RUBY_ASSERT_MUTEX_OWNED(autoload_mutex);
2667 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
2669 VALUE autoload_data_value = rb_hash_aref(autoload_features, feature);
2672 if (
NIL_P(autoload_data_value)) {
2678 if (autoload_data_pointer) *autoload_data_pointer =
autoload_data;
2680 rb_hash_aset(autoload_features, feature, autoload_data_value);
2682 else if (autoload_data_pointer) {
2683 *autoload_data_pointer =
rb_check_typeddata(autoload_data_value, &autoload_data_type);
2686 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
2687 return autoload_data_value;
2691autoload_table_lookup_or_create(
VALUE module)
2693 VALUE autoload_table_value = rb_ivar_lookup(module, autoload,
Qfalse);
2694 if (
RTEST(autoload_table_value)) {
2695 return autoload_table_value;
2699 rb_class_ivar_set(module, autoload, autoload_table_value);
2701 return autoload_table_value;
2706autoload_synchronized(
VALUE _arguments)
2710 rb_const_entry_t *constant_entry = rb_const_lookup(arguments->module, arguments->name);
2711 if (constant_entry && !UNDEF_P(constant_entry->value)) {
2716 const_set(arguments->module, arguments->name,
Qundef);
2718 VALUE autoload_table_value = autoload_table_lookup_or_create(arguments->module);
2719 struct st_table *autoload_table = check_autoload_table(autoload_table_value);
2722 VALUE feature = rb_fstring(arguments->feature);
2725 VALUE autoload_data_value = autoload_feature_lookup_or_create(feature, &
autoload_data);
2736 st_insert(autoload_table, (st_data_t)arguments->name, (st_data_t)autoload_const_value);
2747 rb_raise(
rb_eNameError,
"autoload must be constant name: %"PRIsVALUE
"", QUOTE_ID(name));
2751 if (!RSTRING_LEN(feature)) {
2752 rb_raise(rb_eArgError,
"empty feature name");
2763 if (result ==
Qtrue) {
2764 const_added(module, name);
2769autoload_delete(
VALUE module,
ID name)
2771 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
2773 st_data_t load = 0, key = name;
2777 VALUE table_value = rb_ivar_lookup(module, autoload,
Qfalse);
2778 if (
RTEST(table_value)) {
2779 struct st_table *table = check_autoload_table(table_value);
2781 st_delete(table, &key, &load);
2804 if (table->num_entries == 0) {
2805 rb_attr_delete(module, autoload);
2810 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
2816 return ele->mutex !=
Qnil && !rb_mutex_owned_p(ele->mutex);
2820check_autoload_required(
VALUE mod,
ID id,
const char **loadingpath)
2824 const char *loading;
2826 if (!autoload_const_value || !(
autoload_data = get_autoload_data(autoload_const_value, 0))) {
2839 return autoload_const_value;
2842 loading = RSTRING_PTR(feature);
2845 return autoload_const_value;
2848 if (loadingpath && loading) {
2849 *loadingpath = loading;
2850 return autoload_const_value;
2859rb_autoloading_value(
VALUE mod,
ID id,
VALUE* value, rb_const_flag_t *flag)
2862 if (!ac)
return FALSE;
2878 return ele->mutex !=
Qnil && rb_mutex_owned_p(ele->mutex);
2885autoloading_const_entry(
VALUE mod,
ID id)
2892 if (!load || !(ele = get_autoload_data(load, &ac))) {
2898 if (autoload_by_current(ele)) {
2899 if (!UNDEF_P(ac->value)) {
2908autoload_defined_p(
VALUE mod,
ID id)
2910 rb_const_entry_t *ce = rb_const_lookup(mod,
id);
2913 if (!ce || !UNDEF_P(ce->value)) {
2919 return !rb_autoloading_value(mod,
id, NULL, NULL);
2932 struct autoload_const *autoload_const;
2935 struct autoload_data *autoload_data;
2941 check_before_mod_set(ac->module, ac->name, ac->value,
"constant");
2945 const_tbl_update(ac,
true);
2953autoload_load_needed(
VALUE _arguments)
2957 const char *loading = 0, *src;
2959 if (!autoload_defined_p(arguments->module, arguments->name)) {
2963 VALUE autoload_const_value = check_autoload_required(arguments->module, arguments->name, &loading);
2964 if (!autoload_const_value) {
2969 if (src && loading && strcmp(src, loading) == 0) {
2990 return autoload_const_value;
2994autoload_apply_constants(
VALUE _arguments)
2996 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
3008 ccan_list_for_each_safe(&arguments->autoload_data->constants,
autoload_const, next, cnode) {
3014 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
3020autoload_feature_require(
VALUE _arguments)
3029 VALUE result =
rb_funcall(rb_vm_top_self(), rb_intern(
"require"), 1, arguments->autoload_data->feature);
3031 if (
RTEST(result)) {
3039autoload_try_load(
VALUE _arguments)
3043 VALUE result = autoload_feature_require(_arguments);
3046 rb_const_entry_t *ce = rb_const_lookup(arguments->module, arguments->name);
3048 if (!ce || UNDEF_P(ce->value)) {
3053 if (arguments->module == rb_cObject) {
3055 "Expected %"PRIsVALUE
" to define %"PRIsVALUE
" but it didn't",
3056 arguments->autoload_data->feature,
3062 "Expected %"PRIsVALUE
" to define %"PRIsVALUE
"::%"PRIsVALUE
" but it didn't",
3063 arguments->autoload_data->feature,
3071 ce->flag |= arguments->flag;
3080 rb_const_entry_t *ce = rb_const_lookup(module, name);
3083 if (!ce || !UNDEF_P(ce->value)) {
3088 if (UNLIKELY(!rb_ractor_main_p())) {
3089 return rb_ractor_autoload_load(module, name);
3099 if (autoload_const_value ==
Qfalse)
return autoload_const_value;
3101 arguments.flag = ce->flag & (CONST_DEPRECATED | CONST_VISIBILITY_MASK);
3118 return rb_autoload_at_p(mod,
id, TRUE);
3122rb_autoload_at_p(
VALUE mod,
ID id,
int recur)
3127 while (!autoload_defined_p(mod,
id)) {
3128 if (!recur)
return Qnil;
3130 if (!mod)
return Qnil;
3132 load = check_autoload_required(mod,
id, 0);
3133 if (!load)
return Qnil;
3134 return (ele = get_autoload_data(load, 0)) ? ele->feature :
Qnil;
3138rb_const_warn_if_deprecated(
const rb_const_entry_t *ce,
VALUE klass,
ID id)
3140 if (RB_CONST_DEPRECATED_P(ce) &&
3142 if (klass == rb_cObject) {
3153rb_const_get_0(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility)
3155 VALUE c = rb_const_search(klass,
id, exclude, recurse, visibility);
3157 if (UNLIKELY(!rb_ractor_main_p())) {
3159 rb_raise(rb_eRactorIsolationError,
"can not access non-shareable objects in constant %"PRIsVALUE
"::%s by non-main Ractor.",
rb_class_path(klass), rb_id2name(
id));
3164 return rb_const_missing(klass,
ID2SYM(
id));
3168rb_const_search_from(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility)
3170 VALUE value, current;
3171 bool first_iteration =
true;
3173 for (current = klass;
3175 current =
RCLASS_SUPER(current), first_iteration =
false) {
3178 rb_const_entry_t *ce;
3180 if (!first_iteration && RCLASS_ORIGIN(current) != current) {
3193 while ((ce = rb_const_lookup(tmp,
id))) {
3194 if (visibility && RB_CONST_PRIVATE_P(ce)) {
3195 GET_EC()->private_const_reference = tmp;
3198 rb_const_warn_if_deprecated(ce, tmp,
id);
3200 if (UNDEF_P(value)) {
3202 if (am == tmp)
break;
3204 ac = autoloading_const_entry(tmp,
id);
3205 if (ac)
return ac->value;
3209 if (exclude && tmp == rb_cObject) {
3214 if (!recurse)
break;
3218 GET_EC()->private_const_reference = 0;
3223rb_const_search(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility)
3227 if (klass == rb_cObject) exclude = FALSE;
3228 value = rb_const_search_from(klass,
id, exclude, recurse, visibility);
3229 if (!UNDEF_P(value))
return value;
3230 if (exclude)
return value;
3233 return rb_const_search_from(rb_cObject,
id, FALSE, recurse, visibility);
3239 return rb_const_get_0(klass,
id, TRUE, TRUE, FALSE);
3245 return rb_const_get_0(klass,
id, FALSE, TRUE, FALSE);
3251 return rb_const_get_0(klass,
id, TRUE, FALSE, FALSE);
3255rb_public_const_get_from(
VALUE klass,
ID id)
3257 return rb_const_get_0(klass,
id, TRUE, TRUE, TRUE);
3261rb_public_const_get_at(
VALUE klass,
ID id)
3263 return rb_const_get_0(klass,
id, TRUE, FALSE, TRUE);
3266NORETURN(
static void undefined_constant(
VALUE mod,
VALUE name));
3270 rb_name_err_raise(
"constant %2$s::%1$s not defined",
3275rb_const_location_from(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility)
3277 while (
RTEST(klass)) {
3278 rb_const_entry_t *ce;
3280 while ((ce = rb_const_lookup(klass,
id))) {
3281 if (visibility && RB_CONST_PRIVATE_P(ce)) {
3284 if (exclude && klass == rb_cObject) {
3288 if (UNDEF_P(ce->value)) {
3290 if (
RTEST(autoload_const_value)) {
3300 if (
NIL_P(ce->file))
return rb_ary_new();
3301 return rb_assoc_new(ce->file,
INT2NUM(ce->line));
3303 if (!recurse)
break;
3312rb_const_location(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility)
3316 if (klass == rb_cObject) exclude = FALSE;
3317 loc = rb_const_location_from(klass,
id, exclude, recurse, visibility);
3318 if (!
NIL_P(loc))
return loc;
3319 if (exclude)
return loc;
3322 return rb_const_location_from(rb_cObject,
id, FALSE, recurse, visibility);
3326rb_const_source_location(
VALUE klass,
ID id)
3328 return rb_const_location(klass,
id, FALSE, TRUE, FALSE);
3332rb_const_source_location_at(
VALUE klass,
ID id)
3334 return rb_const_location(klass,
id, TRUE, FALSE, FALSE);
3350 const ID id = id_for_var(mod, name, a, constant);
3353 undefined_constant(mod, name);
3362 rb_const_entry_t *ce;
3364 rb_check_frozen(mod);
3366 ce = rb_const_lookup(mod,
id);
3367 if (!ce || !rb_id_table_delete(RCLASS_CONST_TBL(mod),
id)) {
3369 rb_name_err_raise(
"cannot remove %2$s::%1$s", mod,
ID2SYM(
id));
3372 undefined_constant(mod,
ID2SYM(
id));
3375 rb_const_warn_if_deprecated(ce, mod,
id);
3381 autoload_delete(mod,
id);
3391cv_i_update(st_data_t *k, st_data_t *v, st_data_t a,
int existing)
3393 if (existing)
return ST_STOP;
3398static enum rb_id_table_iterator_result
3401 rb_const_entry_t *ce = (rb_const_entry_t *)v;
3405 st_update(tbl, (st_data_t)key, cv_i_update, (st_data_t)ce);
3407 return ID_TABLE_CONTINUE;
3410static enum rb_id_table_iterator_result
3411rb_local_constants_i(
ID const_name,
VALUE const_value,
void *ary)
3413 if (
rb_is_const_id(const_name) && !RB_CONST_PRIVATE_P((rb_const_entry_t *)const_value)) {
3416 return ID_TABLE_CONTINUE;
3420rb_local_constants(
VALUE mod)
3430 rb_id_table_foreach(tbl, rb_local_constants_i, (
void *)ary);
3440 st_table *tbl = data;
3442 tbl = st_init_numtable();
3444 if (RCLASS_CONST_TBL(mod)) {
3447 rb_id_table_foreach(RCLASS_CONST_TBL(mod), sv_i, tbl);
3462 if (tmp == rb_cObject && mod != rb_cObject)
break;
3468list_i(st_data_t key, st_data_t value,
VALUE ary)
3471 rb_const_entry_t *ce = (rb_const_entry_t *)value;
3472 if (RB_CONST_PUBLIC_P(ce)) rb_ary_push(ary,
ID2SYM(sym));
3479 st_table *tbl = data;
3511 bool inherit =
true;
3519 return rb_local_constants(mod);
3524rb_const_defined_0(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility)
3528 rb_const_entry_t *ce;
3533 if ((ce = rb_const_lookup(tmp,
id))) {
3534 if (visibility && RB_CONST_PRIVATE_P(ce)) {
3537 if (UNDEF_P(ce->value) && !check_autoload_required(tmp,
id, 0) &&
3538 !rb_autoloading_value(tmp,
id, NULL, NULL))
3541 if (exclude && tmp == rb_cObject && klass != rb_cObject) {
3547 if (!recurse)
break;
3561 return rb_const_defined_0(klass,
id, TRUE, TRUE, FALSE);
3567 return rb_const_defined_0(klass,
id, FALSE, TRUE, FALSE);
3573 return rb_const_defined_0(klass,
id, TRUE, FALSE, FALSE);
3577rb_public_const_defined_from(
VALUE klass,
ID id)
3579 return rb_const_defined_0(klass,
id, TRUE, TRUE, TRUE);
3583check_before_mod_set(
VALUE klass,
ID id,
VALUE val,
const char *dest)
3585 rb_check_frozen(klass);
3588static void set_namespace_path(
VALUE named_namespace,
VALUE name);
3590static enum rb_id_table_iterator_result
3591set_namespace_path_i(
ID id,
VALUE v,
void *payload)
3593 rb_const_entry_t *ce = (rb_const_entry_t *)v;
3594 VALUE value = ce->value;
3597 return ID_TABLE_CONTINUE;
3600 bool has_permanent_classpath;
3601 classname(value, &has_permanent_classpath);
3602 if (has_permanent_classpath) {
3603 return ID_TABLE_CONTINUE;
3605 set_namespace_path(value, build_const_path(parental_path,
id));
3607 if (!RCLASS_EXT(value)->permanent_classpath) {
3608 RCLASS_SET_CLASSPATH(value, 0,
false);
3611 return ID_TABLE_CONTINUE;
3620set_namespace_path(
VALUE named_namespace,
VALUE namespace_path)
3622 struct rb_id_table *const_table = RCLASS_CONST_TBL(named_namespace);
3626 RCLASS_SET_CLASSPATH(named_namespace, namespace_path,
true);
3629 rb_id_table_foreach(const_table, set_namespace_path_i, &namespace_path);
3636const_added(
VALUE klass,
ID const_name)
3638 if (GET_VM()->running) {
3640 rb_funcallv(klass, idConst_added, 1, &name);
3647 rb_const_entry_t *ce;
3650 rb_raise(
rb_eTypeError,
"no class/module to define constant %"PRIsVALUE
"",
3655 rb_raise(rb_eRactorIsolationError,
"can not set constants with non-shareable objects by non-main Ractors");
3658 check_before_mod_set(klass,
id, val,
"constant");
3662 struct rb_id_table *tbl = RCLASS_CONST_TBL(klass);
3664 RCLASS_CONST_TBL(klass) = tbl = rb_id_table_create(0);
3666 ce =
ZALLOC(rb_const_entry_t);
3667 rb_id_table_insert(tbl,
id, (
VALUE)ce);
3668 setup_const_entry(ce, klass, val, CONST_PUBLIC);
3672 .module = klass, .name = id,
3673 .value = val, .flag = CONST_PUBLIC,
3676 ac.file = rb_source_location(&ac.line);
3677 const_tbl_update(&ac,
false);
3686 if (rb_cObject && rb_namespace_p(val)) {
3687 bool val_path_permanent;
3688 VALUE val_path = classname(val, &val_path_permanent);
3689 if (
NIL_P(val_path) || !val_path_permanent) {
3690 if (klass == rb_cObject) {
3691 set_namespace_path(val, rb_id2str(
id));
3694 bool parental_path_permanent;
3695 VALUE parental_path = classname(klass, &parental_path_permanent);
3696 if (
NIL_P(parental_path)) {
3698 parental_path = rb_tmp_class_path(klass, &throwaway, make_temporary_path);
3700 if (parental_path_permanent && !val_path_permanent) {
3701 set_namespace_path(val, build_const_path(parental_path,
id));
3703 else if (!parental_path_permanent &&
NIL_P(val_path)) {
3704 RCLASS_SET_CLASSPATH(val, build_const_path(parental_path,
id),
false);
3709 if (klass == rb_cObject &&
id == idRuby) {
3710 rb_warn_reserved_name_at(3.5,
"::Ruby");
3717 const_set(klass,
id, val);
3718 const_added(klass,
id);
3722autoload_data_for_named_constant(
VALUE module,
ID name,
struct autoload_const **autoload_const_pointer)
3725 if (!autoload_data_value)
return 0;
3742 VALUE klass = ac->module;
3743 VALUE val = ac->value;
3745 struct rb_id_table *tbl = RCLASS_CONST_TBL(klass);
3746 rb_const_flag_t visibility = ac->flag;
3747 rb_const_entry_t *ce;
3749 if (rb_id_table_lookup(tbl,
id, &value)) {
3750 ce = (rb_const_entry_t *)value;
3751 if (UNDEF_P(ce->value)) {
3752 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
3753 VALUE file = ac->file;
3754 int line = ac->line;
3755 struct autoload_data *ele = autoload_data_for_named_constant(klass,
id, &ac);
3757 if (!autoload_force && ele) {
3761 ac->file = rb_source_location(&ac->line);
3765 autoload_delete(klass,
id);
3766 ce->flag = visibility;
3771 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
3775 VALUE name = QUOTE_ID(
id);
3776 visibility = ce->flag;
3777 if (klass == rb_cObject)
3778 rb_warn(
"already initialized constant %"PRIsVALUE
"", name);
3780 rb_warn(
"already initialized constant %"PRIsVALUE
"::%"PRIsVALUE
"",
3782 if (!
NIL_P(ce->file) && ce->line) {
3783 rb_compile_warn(RSTRING_PTR(ce->file), ce->line,
3784 "previous definition of %"PRIsVALUE
" was here", name);
3788 setup_const_entry(ce, klass, val, visibility);
3793 ce =
ZALLOC(rb_const_entry_t);
3794 rb_id_table_insert(tbl,
id, (
VALUE)ce);
3795 setup_const_entry(ce, klass, val, visibility);
3800setup_const_entry(rb_const_entry_t *ce,
VALUE klass,
VALUE val,
3801 rb_const_flag_t visibility)
3803 ce->flag = visibility;
3805 RB_OBJ_WRITE(klass, &ce->file, rb_source_location(&ce->line));
3811 ID id = rb_intern(name);
3814 rb_warn(
"rb_define_const: invalid name '%s' for constant", name);
3817 rb_vm_register_global_object(val);
3829set_const_visibility(
VALUE mod,
int argc,
const VALUE *argv,
3830 rb_const_flag_t flag, rb_const_flag_t mask)
3833 rb_const_entry_t *ce;
3838 rb_warning(
"%"PRIsVALUE
" with no argument is just ignored",
3839 QUOTE_ID(rb_frame_callee()));
3843 for (i = 0; i < argc; i++) {
3845 VALUE val = argv[i];
3848 undefined_constant(mod, val);
3850 if ((ce = rb_const_lookup(mod,
id))) {
3853 if (UNDEF_P(ce->value)) {
3856 ele = autoload_data_for_named_constant(mod,
id, &ac);
3865 undefined_constant(mod,
ID2SYM(
id));
3873 rb_const_entry_t *ce;
3875 long len = strlen(name);
3879 undefined_constant(mod, rb_fstring_new(name,
len));
3881 if (!(ce = rb_const_lookup(mod,
id))) {
3882 undefined_constant(mod,
ID2SYM(
id));
3884 ce->flag |= CONST_DEPRECATED;
3895rb_mod_private_constant(
int argc,
const VALUE *argv,
VALUE obj)
3897 set_const_visibility(obj, argc, argv, CONST_PRIVATE, CONST_VISIBILITY_MASK);
3909rb_mod_public_constant(
int argc,
const VALUE *argv,
VALUE obj)
3911 set_const_visibility(obj, argc, argv, CONST_PUBLIC, CONST_VISIBILITY_MASK);
3935rb_mod_deprecate_constant(
int argc,
const VALUE *argv,
VALUE obj)
3937 set_const_visibility(obj, argc, argv, CONST_DEPRECATED, CONST_DEPRECATED);
3942original_module(
VALUE c)
3950cvar_lookup_at(
VALUE klass,
ID id, st_data_t *v)
3958 klass =
RBASIC(klass)->klass;
3963 if (UNDEF_P(n))
return 0;
3970cvar_front_klass(
VALUE klass)
3972 if (RCLASS_SINGLETON_P(klass)) {
3973 VALUE obj = RCLASS_ATTACHED_OBJECT(klass);
3974 if (rb_namespace_p(obj)) {
3984 if (front && target != front) {
3985 if (original_module(front) != original_module(target)) {
3987 "class variable % "PRIsVALUE
" of %"PRIsVALUE
" is overtaken by %"PRIsVALUE
"",
3992 rb_ivar_delete(front,
id,
Qundef);
3997#define CVAR_FOREACH_ANCESTORS(klass, v, r) \
3998 for (klass = cvar_front_klass(klass); klass; klass = RCLASS_SUPER(klass)) { \
3999 if (cvar_lookup_at(klass, id, (v))) { \
4004#define CVAR_LOOKUP(v,r) do {\
4005 CVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(); \
4006 if (cvar_lookup_at(klass, id, (v))) {r;}\
4007 CVAR_FOREACH_ANCESTORS(klass, v, r);\
4025check_for_cvar_table(
VALUE subclass,
VALUE key)
4029 RB_DEBUG_COUNTER_INC(cvar_class_invalidate);
4030 ruby_vm_global_cvar_state++;
4034 rb_class_foreach_subclass(subclass, check_for_cvar_table, key);
4040 VALUE tmp, front = 0, target = 0;
4043 CVAR_LOOKUP(0, {
if (!front) front = klass; target = klass;});
4045 cvar_overtaken(front, target,
id);
4052 target =
RBASIC(target)->klass;
4054 check_before_mod_set(target,
id, val,
"class variable");
4056 int result = rb_class_ivar_set(target,
id, val);
4058 struct rb_id_table *rb_cvc_tbl = RCLASS_CVC_TBL(target);
4061 rb_cvc_tbl = RCLASS_CVC_TBL(target) = rb_id_table_create(2);
4067 if (!rb_id_table_lookup(rb_cvc_tbl,
id, &ent_data)) {
4069 ent->class_value = target;
4070 ent->global_cvar_state = GET_GLOBAL_CVAR_STATE();
4072 rb_id_table_insert(rb_cvc_tbl,
id, (
VALUE)ent);
4073 RB_DEBUG_COUNTER_INC(cvar_inline_miss);
4076 ent = (
void *)ent_data;
4077 ent->global_cvar_state = GET_GLOBAL_CVAR_STATE();
4085 if (RCLASS_SUBCLASSES(target)) {
4086 rb_class_foreach_subclass(target, check_for_cvar_table,
id);
4098 value = find_cvar(klass, front, &target,
id);
4100 rb_name_err_raise(
"uninitialized class variable %1$s in %2$s",
4103 cvar_overtaken(*front, target,
id);
4104 return (
VALUE)value;
4117 if (!klass)
return Qfalse;
4118 CVAR_LOOKUP(0,
return Qtrue);
4123cv_intern(
VALUE klass,
const char *name)
4125 ID id = rb_intern(name);
4127 rb_name_err_raise(
"wrong class variable name %1$s",
4136 ID id = cv_intern(klass, name);
4143 ID id = cv_intern(klass, name);
4154cv_i(
ID key,
VALUE v, st_data_t a)
4156 st_table *tbl = (st_table *)a;
4159 st_update(tbl, (st_data_t)key, cv_i_update, 0);
4165mod_cvar_at(
VALUE mod,
void *data)
4167 st_table *tbl = data;
4169 tbl = st_init_numtable();
4171 mod = original_module(mod);
4178mod_cvar_of(
VALUE mod,
void *data)
4181 if (RCLASS_SINGLETON_P(mod)) {
4182 if (rb_namespace_p(RCLASS_ATTACHED_OBJECT(mod))) {
4183 data = mod_cvar_at(tmp, data);
4184 tmp = cvar_front_klass(tmp);
4188 data = mod_cvar_at(tmp, data);
4196cv_list_i(st_data_t key, st_data_t value,
VALUE ary)
4199 rb_ary_push(ary,
ID2SYM(sym));
4204cvar_list(
void *data)
4206 st_table *tbl = data;
4240 bool inherit =
true;
4245 tbl = mod_cvar_of(mod, 0);
4248 tbl = mod_cvar_at(mod, 0);
4250 return cvar_list(tbl);
4275 const ID id = id_for_var_message(mod, name,
class,
"wrong class variable name %1$s");
4281 rb_check_frozen(mod);
4282 val = rb_ivar_delete(mod,
id,
Qundef);
4283 if (!UNDEF_P(val)) {
4287 rb_name_err_raise(
"cannot remove %1$s for %2$s", mod,
ID2SYM(
id));
4290 rb_name_err_raise(
"class variable %1$s not defined for %2$s",
4309 ID id = rb_intern(name);
4315class_ivar_set_shape_ivptr(
VALUE obj,
void *_data)
4319 return RCLASS_IVPTR(obj);
4323class_ivar_set_shape_resize_ivptr(
VALUE obj, attr_index_t _old_capa, attr_index_t new_capa,
void *_data)
4329class_ivar_set_set_shape(
VALUE obj, rb_shape_t *shape,
void *_data)
4331 rb_shape_set_shape(obj, shape);
4335class_ivar_set_transition_too_complex(
VALUE obj,
void *_data)
4337 rb_evict_ivars_to_hash(obj);
4341class_ivar_set_too_complex_table(
VALUE obj,
void *_data)
4345 return RCLASS_IV_HASH(obj);
4352 bool existing =
false;
4353 rb_check_frozen(obj);
4357 existing = general_ivar_set(obj,
id, val, NULL,
4358 class_ivar_set_shape_ivptr,
4359 class_ivar_set_shape_resize_ivptr,
4360 class_ivar_set_set_shape,
4361 class_ivar_set_transition_too_complex,
4362 class_ivar_set_too_complex_table).existing;
4370tbl_copy_i(
ID key,
VALUE val, st_data_t dest)
4372 rb_class_ivar_set((
VALUE)dest, key, val);
4390rb_const_lookup(
VALUE klass,
ID id)
4392 struct rb_id_table *tbl = RCLASS_CONST_TBL(klass);
4399 r = rb_id_table_lookup(tbl,
id, &val);
4403 if (r)
return (rb_const_entry_t *)val;
#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_EXIVAR
Old name of RUBY_FL_EXIVAR.
#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 OBJ_FROZEN
Old name of RB_OBJ_FROZEN.
#define rb_str_cat2
Old name of rb_str_cat_cstr.
#define xrealloc
Old name of ruby_xrealloc.
#define ID2SYM
Old name of RB_ID2SYM.
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define ZALLOC
Old name of RB_ZALLOC.
#define CLASS_OF
Old name of rb_class_of.
#define xmalloc
Old name of ruby_xmalloc.
#define T_MODULE
Old name of RUBY_T_MODULE.
#define ASSUME
Old name of RBIMPL_ASSUME.
#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 FL_SET
Old name of RB_FL_SET.
#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 FL_TEST
Old name of RB_FL_TEST.
#define FL_UNSET
Old name of RB_FL_UNSET.
#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_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.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
#define st_foreach_safe
Just another name of rb_st_foreach_safe.
int rb_feature_provided(const char *feature, const char **loading)
Identical to rb_provided(), except it additionally returns the "canonical" name of the loaded feature...
VALUE rb_backref_get(void)
Queries the last match, or Regexp.last_match, or the $~.
int rb_is_instance_id(ID id)
Classifies the given ID, then sees if it is an instance variable.
int rb_is_const_id(ID id)
Classifies the given ID, then sees if it is a constant.
int rb_is_class_id(ID id)
Classifies the given ID, then sees if it is a class variable.
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
VALUE rb_reg_nth_defined(int n, VALUE md)
Identical to rb_reg_nth_match(), except it just returns Boolean.
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
VALUE rb_str_subseq(VALUE str, long beg, long len)
Identical to rb_str_substr(), except the numbers are interpreted as byte offsets instead of character...
VALUE rb_str_new_frozen(VALUE str)
Creates a frozen copy of the string, if necessary.
VALUE rb_str_dup(VALUE str)
Duplicates a string.
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
VALUE rb_str_intern(VALUE str)
Identical to rb_to_symbol(), except it assumes the receiver being an instance of RString.
VALUE rb_mutex_new(void)
Creates a mutex.
VALUE rb_mutex_synchronize(VALUE mutex, VALUE(*func)(VALUE arg), VALUE arg)
Obtains the lock, runs the passed function, and releases the lock when it completes.
VALUE rb_exec_recursive_paired(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE p, VALUE h)
Identical to rb_exec_recursive(), except it checks for the recursion on the ordered pair of { g,...
VALUE rb_mod_remove_cvar(VALUE mod, VALUE name)
Resembles Module#remove_class_variable.
VALUE rb_obj_instance_variables(VALUE obj)
Resembles Object#instance_variables.
VALUE rb_f_untrace_var(int argc, const VALUE *argv)
Deletes the passed tracer from the passed global variable, or if omitted, deletes everything.
VALUE rb_const_get(VALUE space, ID name)
Identical to rb_const_defined(), except it returns the actual defined value.
VALUE rb_const_list(void *)
This is another mysterious API that comes with no documents at all.
VALUE rb_path2class(const char *path)
Resolves a Q::W::E::R-style path string to the actual class it points.
VALUE rb_autoload_p(VALUE space, ID name)
Queries if an autoload is defined at a point.
void rb_set_class_path(VALUE klass, VALUE space, const char *name)
Names a class.
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
VALUE rb_mod_remove_const(VALUE space, VALUE name)
Resembles Module#remove_const.
VALUE rb_class_path_cached(VALUE mod)
Just another name of rb_mod_name.
VALUE rb_f_trace_var(int argc, const VALUE *argv)
Traces a global variable.
void rb_cvar_set(VALUE klass, ID name, VALUE val)
Assigns a value to a class variable.
VALUE rb_cvar_get(VALUE klass, ID name)
Obtains a value from a class variable.
VALUE rb_mod_constants(int argc, const VALUE *argv, VALUE recv)
Resembles Module#constants.
VALUE rb_cvar_find(VALUE klass, ID name, VALUE *front)
Identical to rb_cvar_get(), except it takes additional "front" pointer.
VALUE rb_path_to_class(VALUE path)
Identical to rb_path2class(), except it accepts the path as Ruby's string instead of C's.
VALUE rb_ivar_get(VALUE obj, ID name)
Identical to rb_iv_get(), except it accepts the name as an ID instead of a C string.
void rb_const_set(VALUE space, ID name, VALUE val)
Names a constant.
VALUE rb_autoload_load(VALUE space, ID name)
Kicks the autoload procedure as if it was "touched".
VALUE rb_mod_name(VALUE mod)
Queries the name of a module.
VALUE rb_class_name(VALUE obj)
Queries the name of the given object's class.
VALUE rb_const_get_at(VALUE space, ID name)
Identical to rb_const_defined_at(), except it returns the actual defined value.
void rb_set_class_path_string(VALUE klass, VALUE space, VALUE name)
Identical to rb_set_class_path(), except it accepts the name as Ruby's string instead of C's.
void rb_alias_variable(ID dst, ID src)
Aliases a global variable.
void rb_define_class_variable(VALUE, const char *, VALUE)
Just another name of rb_cv_set.
VALUE rb_obj_remove_instance_variable(VALUE obj, VALUE name)
Resembles Object#remove_instance_variable.
void * rb_mod_const_of(VALUE, void *)
This is a variant of rb_mod_const_at().
st_index_t rb_ivar_count(VALUE obj)
Number of instance variables defined on an object.
void * rb_mod_const_at(VALUE, void *)
This API is mysterious.
VALUE rb_const_remove(VALUE space, ID name)
Identical to rb_mod_remove_const(), except it takes the name as ID instead of VALUE.
VALUE rb_const_get_from(VALUE space, ID name)
Identical to rb_const_defined_at(), except it returns the actual defined value.
VALUE rb_ivar_defined(VALUE obj, ID name)
Queries if the instance variable is defined at the object.
VALUE rb_cv_get(VALUE klass, const char *name)
Identical to rb_cvar_get(), except it accepts C's string instead of ID.
int rb_const_defined_at(VALUE space, ID name)
Identical to rb_const_defined(), except it doesn't look for parent classes.
void rb_cv_set(VALUE klass, const char *name, VALUE val)
Identical to rb_cvar_set(), except it accepts C's string instead of ID.
VALUE rb_mod_class_variables(int argc, const VALUE *argv, VALUE recv)
Resembles Module#class_variables.
VALUE rb_f_global_variables(void)
Queries the list of global variables.
VALUE rb_cvar_defined(VALUE klass, ID name)
Queries if the given class has the given class variable.
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
int rb_const_defined_from(VALUE space, ID name)
Identical to rb_const_defined(), except it returns false for private constants.
int rb_const_defined(VALUE space, ID name)
Queries if the constant is defined at the namespace.
void rb_free_generic_ivar(VALUE obj)
Frees the list of instance variables.
const char * rb_sourcefile(void)
Resembles __FILE__.
void rb_clear_constant_cache_for_id(ID id)
Clears the inline constant caches associated with a particular ID.
VALUE rb_eval_cmd_kw(VALUE cmd, VALUE arg, int kw_splat)
This API is practically a variant of rb_proc_call_kw() now.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
rb_gvar_setter_t rb_gvar_var_setter
rb_gvar_marker_t rb_gvar_var_marker
void rb_define_global_const(const char *name, VALUE val)
Identical to rb_define_const(), except it defines that of "global", i.e.
VALUE rb_gv_get(const char *name)
Obtains a global variable.
void rb_define_variable(const char *name, VALUE *var)
"Shares" a global variable between Ruby and C.
void rb_gvar_marker_t(VALUE *var)
Type that represents a global variable marker function.
void rb_deprecate_constant(VALUE mod, const char *name)
Asserts that the given constant is deprecated.
void rb_gvar_setter_t(VALUE val, ID id, VALUE *data)
Type that represents a global variable setter function.
rb_gvar_setter_t rb_gvar_val_setter
This is the setter function that backs global variables defined from a ruby script.
rb_gvar_marker_t rb_gvar_undef_marker
void rb_define_readonly_variable(const char *name, const VALUE *var)
Identical to rb_define_variable(), except it does not allow Ruby programs to assign values to such gl...
rb_gvar_setter_t rb_gvar_readonly_setter
This function just raises rb_eNameError.
rb_gvar_getter_t rb_gvar_undef_getter
VALUE rb_gv_set(const char *name, VALUE val)
Assigns to a global variable.
rb_gvar_marker_t rb_gvar_val_marker
This is the setter function that backs global variables defined from a ruby script.
void rb_define_const(VALUE klass, const char *name, VALUE val)
Defines a Ruby level constant under a namespace.
VALUE rb_gvar_getter_t(ID id, VALUE *data)
Type that represents a global variable getter function.
VALUE rb_iv_get(VALUE obj, const char *name)
Obtains an instance variable.
rb_gvar_setter_t rb_gvar_undef_setter
rb_gvar_getter_t rb_gvar_val_getter
This is the getter function that backs global variables defined from a ruby script.
VALUE rb_iv_set(VALUE obj, const char *name, VALUE val)
Assigns to an instance variable.
rb_gvar_getter_t rb_gvar_var_getter
int len
Length of the buffer.
static bool rb_ractor_shareable_p(VALUE obj)
Queries if multiple Ractors can share the passed object or not.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
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_IVPTR(VALUE obj)
Queries the instance variables.
#define StringValue(v)
Ensures that the parameter object is a String.
static char * RSTRING_END(VALUE str)
Queries the end of the contents pointer of the string.
#define RTYPEDDATA_DATA(v)
Convenient getter macro.
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
#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.
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.