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)
46 typedef void rb_gvar_compact_t(
void *var);
54 static VALUE autoload_features;
59 static VALUE autoload_mutex;
61 static void check_before_mod_set(
VALUE,
ID,
VALUE,
const char *);
63 static VALUE rb_const_search(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility);
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);
83 rb_namespace_p(
VALUE obj)
104 classname(
VALUE klass,
bool *permanent)
108 VALUE classpath = RCLASS_EXT(klass)->classpath;
109 if (classpath == 0)
return Qnil;
111 *permanent = RCLASS_EXT(klass)->permanent_classpath;
117 rb_mod_name0(
VALUE klass,
bool *permanent)
119 return classname(klass, permanent);
134 return classname(mod, &permanent);
139 is_constant_path(
VALUE 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) {
218 rb_mod_set_temporary_name(
VALUE mod,
VALUE name)
221 if (RCLASS_EXT(mod)->permanent_classpath) {
227 RCLASS_SET_CLASSPATH(mod, 0, FALSE);
237 if (is_constant_path(name)) {
242 RCLASS_SET_CLASSPATH(mod, name, FALSE);
257 path =
rb_sprintf(
"#<Module:%p>", (
void*)obj);
260 path =
rb_sprintf(
"#<%"PRIsVALUE
":%p>", klass, (
void*)obj);
270 rb_tmp_class_path(
VALUE klass,
bool *permanent, fallback_func fallback)
272 VALUE path = classname(klass, permanent);
284 path = rb_tmp_class_path(
RBASIC(klass)->klass, &perm, fallback);
289 return fallback(klass, path);
296 VALUE path = rb_tmp_class_path(klass, &permanent, make_temporary_path);
314 rb_search_class_path(
VALUE klass)
317 return rb_tmp_class_path(klass, &permanent, no_fallback);
326 return rb_fstring(path);
330 build_const_path(
VALUE head,
ID tail)
332 return build_const_pathname(head,
rb_id2str(tail));
338 bool permanent =
true;
341 if (under == rb_cObject) {
345 str = rb_tmp_class_path(under, &permanent, make_temporary_path);
346 str = build_const_pathname(str, name);
349 RCLASS_SET_CLASSPATH(klass, str, permanent);
364 const char *pbeg, *pend, *p, *path =
RSTRING_PTR(pathname);
366 VALUE c = rb_cObject;
373 if (path == pend || path[0] ==
'#') {
378 while (p < pend && *p !=
':') p++;
380 if (p < pend && p[0] ==
':') {
381 if ((
size_t)(pend - p) < 2 || p[1] !=
':')
goto undefined_class;
386 goto undefined_class;
388 c = rb_const_search(c,
id, TRUE, FALSE, FALSE);
389 if (UNDEF_P(c))
goto undefined_class;
390 if (!rb_namespace_p(c)) {
421 VALUE path = rb_tmp_class_path(
rb_class_real(klass), &permanent, make_temporary_path);
422 if (
NIL_P(path))
return NULL;
446 rb_gvar_compact_t *compactor;
456 static enum rb_id_table_iterator_result
457 free_global_entry_i(
VALUE val,
void *arg)
460 if (entry->var->counter == 1) {
464 entry->var->counter--;
467 return ID_TABLE_DELETE;
471 rb_free_rb_global_tbl(
void)
473 rb_id_table_foreach_values(rb_global_tbl, free_global_entry_i, 0);
474 rb_id_table_free(rb_global_tbl);
478 rb_free_generic_iv_tbl_(
void)
480 st_free_table(generic_iv_tbl_);
484 rb_find_global_entry(
ID id)
489 if (!rb_id_table_lookup(rb_global_tbl,
id, &data)) {
497 if (UNLIKELY(!rb_ractor_main_p()) && (!entry || !entry->ractor_local)) {
498 rb_raise(rb_eRactorIsolationError,
"can not access global variables %s from non-main Ractors",
rb_id2name(
id));
505 rb_gvar_ractor_local(
const char *name)
508 entry->ractor_local =
true;
512 rb_gvar_undef_compactor(
void *var)
526 entry->ractor_local =
false;
532 var->compactor = rb_gvar_undef_compactor;
534 var->block_trace = 0;
536 rb_id_table_insert(rb_global_tbl,
id, (
VALUE)entry);
544 rb_warning(
"global variable '%"PRIsVALUE
"' not initialized", QUOTE_ID(
id));
550 rb_gvar_val_compactor(
void *_var)
559 var->data = (
void*)
new;
571 var->compactor = rb_gvar_val_compactor;
573 var->data = (
void*)val;
591 var->data = (
void*)val;
604 if (!var)
return Qnil;
623 rb_name_error(
id,
"%"PRIsVALUE
" is a read-only variable", QUOTE_ID(
id));
626 static enum rb_id_table_iterator_result
627 mark_global_entry(
VALUE v,
void *ignored)
633 (*var->marker)(var->data);
639 return ID_TABLE_CONTINUE;
642 #define gc_mark_table(task) \
643 if (rb_global_tbl) { rb_id_table_foreach_values(rb_global_tbl, task##_global_entry, 0); }
646 rb_gc_mark_global_tbl(
void)
651 static enum rb_id_table_iterator_result
652 update_global_entry(
VALUE v,
void *ignored)
657 (*var->compactor)(var);
658 return ID_TABLE_CONTINUE;
662 rb_gc_update_global_tbl(
void)
664 gc_mark_table(update);
668 global_id(
const char *name)
672 if (name[0] ==
'$')
id =
rb_intern(name);
674 size_t len = strlen(name);
678 memcpy(buf+1, name,
len);
686 find_global_id(
const char *name)
689 size_t len = strlen(name);
691 if (name[0] ==
'$') {
698 memcpy(buf+1, name,
len);
714 ID id = global_id(name);
717 gvar->data = (
void*)var;
769 trace->next = entry->var->trace;
770 trace->func = rb_trace_eval;
773 entry->var->trace = trace;
787 while (trace->next) {
790 trace->next = next->next;
813 if ((entry = rb_find_global_entry(
id)) == NULL) {
814 rb_name_error(
id,
"undefined global variable %"PRIsVALUE
"", QUOTE_ID(
id));
817 trace = entry->var->trace;
828 if (!entry->var->block_trace) remove_trace(entry->var);
833 if (trace->data == cmd) {
835 if (!entry->var->block_trace) remove_trace(entry->var);
856 (*trace->func)(trace->data, data->val);
867 var->block_trace = 0;
878 (*var->setter)(val, entry->id, var->data);
880 if (var->trace && !var->block_trace) {
881 var->block_trace = 1;
882 trace.trace = var->trace;
895 return rb_gvar_set_entry(entry, val);
901 return rb_gvar_set(global_id(name), val);
909 return (*var->getter)(entry->id, var->data);
915 ID id = find_global_id(name);
918 rb_warning(
"global variable '%s' not initialized", name);
922 return rb_gvar_get(
id);
926 rb_gvar_defined(
ID id)
933 rb_gvar_getter_function_of(
ID id)
936 return entry->var->getter;
940 rb_gvar_setter_function_of(
ID id)
943 return entry->var->setter;
946 static enum rb_id_table_iterator_result
947 gvar_i(
ID key,
VALUE val,
void *a)
951 return ID_TABLE_CONTINUE;
960 if (!rb_ractor_main_p()) {
961 rb_raise(rb_eRactorIsolationError,
"can not access global variables from non-main Ractors");
964 rb_id_table_foreach(rb_global_tbl, gvar_i, (
void *)ary);
965 if (!
NIL_P(backref)) {
967 int i, nmatch = rb_match_count(backref);
969 for (i = 1; i <= nmatch; ++i) {
973 buf[1] = (char)(i +
'0');
993 if (!rb_ractor_main_p()) {
994 rb_raise(rb_eRactorIsolationError,
"can not access global variables from non-main Ractors");
998 if (!rb_id_table_lookup(gtbl, name1, &data1)) {
1001 rb_id_table_insert(gtbl, name1, (
VALUE)entry1);
1003 else if ((entry1 = (
struct rb_global_entry *)data1)->var != entry2->var) {
1005 if (var->block_trace) {
1009 if (var->counter == 0) {
1022 entry2->var->counter++;
1023 entry1->var = entry2->var;
1027 IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(
ID id)
1029 if (UNLIKELY(!rb_ractor_main_p())) {
1031 rb_raise(rb_eRactorIsolationError,
"can not set instance variables of classes/modules by non-main Ractors");
1036 #define CVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR() \
1037 if (UNLIKELY(!rb_ractor_main_p())) { \
1038 rb_raise(rb_eRactorIsolationError, "can not access class variables from non-main Ractors"); \
1042 generic_ivtbl(
VALUE obj,
ID id,
bool force_check_ractor)
1044 ASSERT_vm_locking();
1048 UNLIKELY(!rb_ractor_main_p()) &&
1051 rb_raise(rb_eRactorIsolationError,
"can not access instance variables of shareable objects from non-main Ractors");
1053 return generic_iv_tbl_;
1057 generic_ivtbl_no_ractor_check(
VALUE obj)
1059 return generic_ivtbl(obj, 0,
false);
1072 if (st_lookup(generic_ivtbl(obj,
id,
false), (st_data_t)obj, &data)) {
1083 rb_ivar_generic_ivtbl_lookup(
VALUE obj,
struct gen_ivtbl **ivtbl)
1085 return rb_gen_ivtbl_get(obj, 0, ivtbl);
1089 gen_ivtbl_bytes(
size_t n)
1091 return offsetof(
struct gen_ivtbl, as.shape.ivptr) + n *
sizeof(
VALUE);
1095 gen_ivtbl_resize(
struct gen_ivtbl *old, uint32_t n)
1099 uint32_t
len = old ? old->as.shape.numiv : 0;
1102 ivtbl->as.shape.numiv = n;
1111 rb_mark_generic_ivar(
VALUE obj)
1114 if (st_lookup(generic_ivtbl_no_ractor_check(obj), (st_data_t)obj, &data)) {
1116 if (rb_shape_obj_too_complex(obj)) {
1120 for (uint32_t i = 0; i < ivtbl->as.shape.numiv; i++) {
1128 rb_ref_update_generic_ivar(
VALUE obj)
1132 if (rb_gen_ivtbl_get(obj, 0, &ivtbl)) {
1133 if (rb_shape_obj_too_complex(obj)) {
1134 rb_gc_ref_update_table_values_only(ivtbl->as.complex.table);
1137 for (uint32_t i = 0; i < ivtbl->as.shape.numiv; i++) {
1138 ivtbl->as.shape.ivptr[i] =
rb_gc_location(ivtbl->as.shape.ivptr[i]);
1147 st_data_t key = (st_data_t)rsrc;
1150 if (st_delete(generic_ivtbl_no_ractor_check(rsrc), &key, &ivtbl))
1151 st_insert(generic_ivtbl_no_ractor_check(dst), (st_data_t)dst, ivtbl);
1157 st_data_t key = (st_data_t)obj, value;
1159 bool too_complex = rb_shape_obj_too_complex(obj);
1161 if (st_delete(generic_ivtbl_no_ractor_check(obj), &key, &value)) {
1164 if (UNLIKELY(too_complex)) {
1165 st_free_table(ivtbl->as.complex.table);
1173 rb_generic_ivar_memsize(
VALUE obj)
1177 if (rb_gen_ivtbl_get(obj, 0, &ivtbl)) {
1178 if (rb_shape_obj_too_complex(obj)) {
1179 return sizeof(
struct gen_ivtbl) + st_memsize(ivtbl->as.complex.table);
1182 return gen_ivtbl_bytes(ivtbl->as.shape.numiv);
1188 #if !SHAPE_IN_BASIC_FLAGS
1190 rb_generic_shape_id(
VALUE obj)
1193 shape_id_t shape_id = 0;
1197 st_table* global_iv_table = generic_ivtbl(obj, 0,
false);
1199 if (global_iv_table && st_lookup(global_iv_table, obj, (st_data_t *)&ivtbl)) {
1200 shape_id = ivtbl->shape_id;
1203 shape_id = SPECIAL_CONST_SHAPE_ID;
1218 if (rb_shape_obj_too_complex(obj)) {
1219 n = st_table_size(ivtbl->as.complex.table);
1222 for (i = 0; i < ivtbl->as.shape.numiv; i++) {
1223 if (!UNDEF_P(ivtbl->as.shape.ivptr[i])) {
1237 shape_id_t shape_id;
1241 #if SHAPE_IN_BASIC_FLAGS
1242 shape_id = RBASIC_SHAPE_ID(obj);
1254 #if !SHAPE_IN_BASIC_FLAGS
1255 shape_id = RCLASS_SHAPE_ID(obj);
1258 if (rb_shape_obj_too_complex(obj)) {
1259 st_table * iv_table = RCLASS_IV_HASH(obj);
1260 if (rb_st_lookup(iv_table, (st_data_t)
id, (st_data_t *)&val)) {
1268 attr_index_t index = 0;
1269 shape = rb_shape_get_shape_by_id(shape_id);
1270 found = rb_shape_get_iv_index(shape,
id, &index);
1273 ivar_list = RCLASS_IVPTR(obj);
1276 val = ivar_list[index];
1287 UNLIKELY(!rb_ractor_main_p()) &&
1290 "can not get unshareable values from instance variables of classes/modules from non-main Ractors");
1296 #if !SHAPE_IN_BASIC_FLAGS
1297 shape_id = ROBJECT_SHAPE_ID(obj);
1299 if (rb_shape_obj_too_complex(obj)) {
1300 st_table * iv_table = ROBJECT_IV_HASH(obj);
1302 if (rb_st_lookup(iv_table, (st_data_t)
id, (st_data_t *)&val)) {
1317 rb_gen_ivtbl_get(obj,
id, &ivtbl);
1319 if (rb_shape_obj_too_complex(obj)) {
1321 if (rb_st_lookup(ivtbl->as.complex.table, (st_data_t)
id, (st_data_t *)&val)) {
1329 #if !SHAPE_IN_BASIC_FLAGS
1330 shape_id = ivtbl->shape_id;
1332 ivar_list = ivtbl->as.shape.ivptr;
1340 attr_index_t index = 0;
1341 shape = rb_shape_get_shape_by_id(shape_id);
1342 if (rb_shape_get_iv_index(shape,
id, &index)) {
1343 return ivar_list[index];
1352 VALUE iv = rb_ivar_lookup(obj,
id,
Qnil);
1353 RB_DEBUG_COUNTER_INC(ivar_get_base);
1360 return rb_ivar_lookup(obj,
id,
Qnil);
1366 rb_check_frozen(obj);
1372 IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(
id);
1375 if (!rb_shape_transition_shape_remove_ivar(obj,
id, shape, &val)) {
1376 if (!rb_shape_obj_too_complex(obj)) {
1377 rb_evict_ivars_to_hash(obj);
1384 table = RCLASS_IV_HASH(obj);
1388 table = ROBJECT_IV_HASH(obj);
1393 if (rb_gen_ivtbl_get(obj, 0, &ivtbl)) {
1394 table = ivtbl->as.complex.table;
1401 if (!st_delete(table, (st_data_t *)&
id, (st_data_t *)&val)) {
1411 rb_attr_delete(
VALUE obj,
ID id)
1413 return rb_ivar_delete(obj,
id,
Qnil);
1421 VALUE *old_ivptr = NULL;
1425 if (!(
RBASIC(obj)->flags & ROBJECT_EMBED)) {
1428 rb_shape_set_shape_id(obj, OBJ_TOO_COMPLEX_SHAPE_ID);
1429 ROBJECT_SET_IV_HASH(obj, table);
1433 old_ivptr = RCLASS_IVPTR(obj);
1434 rb_shape_set_shape_id(obj, OBJ_TOO_COMPLEX_SHAPE_ID);
1435 RCLASS_SET_IV_HASH(obj, table);
1440 struct st_table *gen_ivs = generic_ivtbl_no_ractor_check(obj);
1443 st_lookup(gen_ivs, (st_data_t)obj, (st_data_t *)&old_ivtbl);
1450 #if SHAPE_IN_BASIC_FLAGS
1451 rb_shape_set_shape_id(obj, OBJ_TOO_COMPLEX_SHAPE_ID);
1453 old_ivtbl->shape_id = OBJ_TOO_COMPLEX_SHAPE_ID;
1455 old_ivtbl->as.complex.table = table;
1456 old_ivptr = (
VALUE *)old_ivtbl;
1460 ivtbl->as.complex.table = table;
1461 st_insert(gen_ivs, (st_data_t)obj, (st_data_t)ivtbl);
1462 #if SHAPE_IN_BASIC_FLAGS
1463 rb_shape_set_shape_id(obj, OBJ_TOO_COMPLEX_SHAPE_ID);
1465 ivtbl->shape_id = OBJ_TOO_COMPLEX_SHAPE_ID;
1475 rb_evict_ivars_to_hash(
VALUE obj)
1482 rb_obj_copy_ivs_to_hash_table(obj, table);
1483 rb_obj_convert_to_too_complex(obj, table);
1494 general_ivar_set(
VALUE obj,
ID id,
VALUE val, void *data,
1496 void (*shape_resize_ivptr_func)(
VALUE, attr_index_t, attr_index_t,
void *),
1498 void (*transition_too_complex_func)(
VALUE,
void *),
1506 rb_shape_t *current_shape = rb_shape_get_shape(obj);
1508 if (UNLIKELY(current_shape->type == SHAPE_OBJ_TOO_COMPLEX)) {
1513 if (!rb_shape_get_iv_index(current_shape,
id, &index)) {
1514 result.existing =
false;
1516 index = current_shape->next_iv_index;
1517 if (index >= MAX_IVARS) {
1521 rb_shape_t *next_shape = rb_shape_get_next(current_shape, obj,
id);
1522 if (UNLIKELY(next_shape->type == SHAPE_OBJ_TOO_COMPLEX)) {
1523 transition_too_complex_func(obj, data);
1526 else if (UNLIKELY(next_shape->capacity != current_shape->capacity)) {
1527 RUBY_ASSERT(next_shape->capacity > current_shape->capacity);
1528 shape_resize_ivptr_func(obj, current_shape->capacity, next_shape->capacity, data);
1532 RUBY_ASSERT(index == (next_shape->next_iv_index - 1));
1533 set_shape_func(obj, next_shape, data);
1536 VALUE *table = shape_ivptr_func(obj, data);
1539 result.index = index;
1546 st_table *table = too_complex_table_func(obj, data);
1547 result.existing = st_insert(table, (st_data_t)
id, (st_data_t)val);
1563 generic_ivar_lookup_ensure_size(st_data_t *k, st_data_t *v, st_data_t u,
int existing)
1565 ASSERT_vm_locking();
1570 if (!existing || ivar_lookup->resize) {
1572 RUBY_ASSERT(ivar_lookup->shape->type == SHAPE_IVAR);
1573 RUBY_ASSERT(rb_shape_get_shape_by_id(ivar_lookup->shape->parent_id)->capacity < ivar_lookup->shape->capacity);
1579 ivtbl = gen_ivtbl_resize(ivtbl, ivar_lookup->shape->capacity);
1580 *v = (st_data_t)ivtbl;
1585 ivar_lookup->ivtbl = ivtbl;
1586 if (ivar_lookup->shape) {
1587 #if SHAPE_IN_BASIC_FLAGS
1588 rb_shape_set_shape(ivar_lookup->obj, ivar_lookup->shape);
1590 ivtbl->shape_id = rb_shape_id(ivar_lookup->shape);
1598 generic_ivar_set_shape_ivptr(
VALUE obj,
void *data)
1606 st_update(generic_ivtbl(obj, ivar_lookup->id,
false), (st_data_t)obj, generic_ivar_lookup_ensure_size, (st_data_t)ivar_lookup);
1612 return ivar_lookup->ivtbl->as.shape.ivptr;
1616 generic_ivar_set_shape_resize_ivptr(
VALUE obj, attr_index_t _old_capa, attr_index_t new_capa,
void *data)
1620 ivar_lookup->resize =
true;
1628 ivar_lookup->shape = shape;
1632 generic_ivar_set_transition_too_complex(
VALUE obj,
void *_data)
1634 rb_evict_ivars_to_hash(obj);
1639 generic_ivar_set_too_complex_table(
VALUE obj,
void *data)
1644 if (!rb_gen_ivtbl_get(obj, 0, &ivtbl)) {
1646 #if !SHAPE_IN_BASIC_FLAGS
1647 ivtbl->shape_id = SHAPE_OBJ_TOO_COMPLEX;
1649 ivtbl->as.complex.table = st_init_numtable_with_size(1);
1653 st_insert(generic_ivtbl(obj, ivar_lookup->id,
false), (st_data_t)obj, (st_data_t)ivtbl);
1662 return ivtbl->as.complex.table;
1675 general_ivar_set(obj,
id, val, &ivar_lookup,
1676 generic_ivar_set_shape_ivptr,
1677 generic_ivar_set_shape_resize_ivptr,
1678 generic_ivar_set_set_shape,
1679 generic_ivar_set_transition_too_complex,
1680 generic_ivar_set_too_complex_table);
1684 rb_ensure_iv_list_size(
VALUE obj, uint32_t current_capacity, uint32_t new_capacity)
1688 if (
RBASIC(obj)->flags & ROBJECT_EMBED) {
1693 ROBJECT(obj)->as.heap.ivptr = newptr;
1701 rb_obj_copy_ivs_to_hash_table_i(
ID key,
VALUE val, st_data_t arg)
1705 st_add_direct((
st_table *)arg, (st_data_t)key, (st_data_t)val);
1712 rb_ivar_foreach(obj, rb_obj_copy_ivs_to_hash_table_i, (st_data_t)table);
1716 obj_ivar_set_shape_ivptr(
VALUE obj,
void *_data)
1724 obj_ivar_set_shape_resize_ivptr(
VALUE obj, attr_index_t old_capa, attr_index_t new_capa,
void *_data)
1726 rb_ensure_iv_list_size(obj, old_capa, new_capa);
1732 rb_shape_set_shape(obj, shape);
1736 obj_ivar_set_transition_too_complex(
VALUE obj,
void *_data)
1738 rb_evict_ivars_to_hash(obj);
1742 obj_ivar_set_too_complex_table(
VALUE obj,
void *_data)
1746 return ROBJECT_IV_HASH(obj);
1752 return general_ivar_set(obj,
id, val, NULL,
1753 obj_ivar_set_shape_ivptr,
1754 obj_ivar_set_shape_resize_ivptr,
1755 obj_ivar_set_set_shape,
1756 obj_ivar_set_transition_too_complex,
1757 obj_ivar_set_too_complex_table).index;
1767 rb_check_frozen(obj);
1768 rb_obj_ivar_set(obj,
id, val);
1773 rb_shape_set_shape_id(
VALUE obj, shape_id_t shape_id)
1775 if (rb_shape_get_shape_id(obj) == shape_id) {
1779 #if SHAPE_IN_BASIC_FLAGS
1780 RBASIC_SET_SHAPE_ID(obj, shape_id);
1784 ROBJECT_SET_SHAPE_ID(obj, shape_id);
1788 RCLASS_SET_SHAPE_ID(obj, shape_id);
1791 if (shape_id != SPECIAL_CONST_SHAPE_ID) {
1795 st_table* global_iv_table = generic_ivtbl(obj, 0,
false);
1797 if (st_lookup(global_iv_table, obj, (st_data_t *)&ivtbl)) {
1798 ivtbl->shape_id = shape_id;
1801 rb_bug(
"Expected shape_id entry in global iv table");
1820 rb_shape_t * next_shape = rb_shape_transition_shape_frozen(x);
1824 if (!rb_shape_obj_too_complex(x) && next_shape->type == SHAPE_OBJ_TOO_COMPLEX) {
1825 rb_evict_ivars_to_hash(x);
1827 rb_shape_set_shape(x, next_shape);
1838 RB_DEBUG_COUNTER_INC(ivar_set_base);
1843 rb_obj_ivar_set(obj,
id, val);
1848 IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(
id);
1849 rb_class_ivar_set(obj,
id, val);
1853 generic_ivar_set(obj,
id, val);
1861 rb_check_frozen(obj);
1862 ivar_set(obj,
id, val);
1872 ivar_set(obj,
id, val);
1881 if (rb_shape_obj_too_complex(obj)) {
1887 table = (
st_table *)RCLASS_IVPTR(obj);
1891 table = ROBJECT_IV_HASH(obj);
1896 if (rb_gen_ivtbl_get(obj, 0, &ivtbl)) {
1897 table = ivtbl->as.complex.table;
1903 if (!table || !rb_st_lookup(table,
id, &idx)) {
1910 return RBOOL(rb_shape_get_iv_index(rb_shape_get_shape(obj),
id, &index));
1914 typedef int rb_ivar_foreach_callback_func(
ID key,
VALUE val, st_data_t arg);
1915 st_data_t rb_st_nth_key(
st_table *tab, st_index_t index);
1921 rb_ivar_foreach_callback_func *func;
1928 iterate_over_shapes_with_callback(
rb_shape_t *shape, rb_ivar_foreach_callback_func *callback,
struct iv_itr_data * itr_data)
1930 switch ((
enum shape_type)shape->type) {
1932 case SHAPE_T_OBJECT:
1936 if (iterate_over_shapes_with_callback(rb_shape_get_parent(shape), callback, itr_data))
1941 RUBY_ASSERT(!rb_shape_obj_too_complex(itr_data->obj));
1946 iv_list = RCLASS_IVPTR(itr_data->obj);
1949 iv_list = itr_data->ivtbl->as.shape.ivptr;
1952 VALUE val = iv_list[shape->next_iv_index - 1];
1953 if (!UNDEF_P(val)) {
1954 switch (callback(shape->edge_name, val, itr_data->arg)) {
1966 return iterate_over_shapes_with_callback(rb_shape_get_parent(shape), callback, itr_data);
1967 case SHAPE_OBJ_TOO_COMPLEX:
1974 each_hash_iv(st_data_t
id, st_data_t val, st_data_t data)
1977 rb_ivar_foreach_callback_func *callback = itr_data->func;
1978 return callback((
ID)
id, (
VALUE)val, itr_data->arg);
1982 obj_ivar_each(
VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg)
1988 itr_data.func = func;
1989 if (rb_shape_obj_too_complex(obj)) {
1990 rb_st_foreach(ROBJECT_IV_HASH(obj), each_hash_iv, (st_data_t)&itr_data);
1993 iterate_over_shapes_with_callback(shape, func, &itr_data);
1998 gen_ivar_each(
VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg)
2002 if (!rb_gen_ivtbl_get(obj, 0, &ivtbl))
return;
2006 itr_data.ivtbl = ivtbl;
2008 itr_data.func = func;
2009 if (rb_shape_obj_too_complex(obj)) {
2010 rb_st_foreach(ivtbl->as.complex.table, each_hash_iv, (st_data_t)&itr_data);
2013 iterate_over_shapes_with_callback(shape, func, &itr_data);
2018 class_ivar_each(
VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg)
2026 itr_data.func = func;
2027 if (rb_shape_obj_too_complex(obj)) {
2028 rb_st_foreach(RCLASS_IV_HASH(obj), each_hash_iv, (st_data_t)&itr_data);
2031 iterate_over_shapes_with_callback(shape, func, &itr_data);
2041 rb_check_frozen(clone);
2047 if (rb_gen_ivtbl_get(obj, 0, &obj_ivtbl)) {
2048 if (gen_ivtbl_count(obj, obj_ivtbl) == 0)
2053 if (rb_shape_obj_too_complex(obj)) {
2055 #if !SHAPE_IN_BASIC_FLAGS
2056 new_ivtbl->shape_id = SHAPE_OBJ_TOO_COMPLEX;
2058 new_ivtbl->as.complex.table = st_copy(obj_ivtbl->as.complex.table);
2061 new_ivtbl = gen_ivtbl_resize(0, obj_ivtbl->as.shape.numiv);
2063 for (uint32_t i=0; i<obj_ivtbl->as.shape.numiv; i++) {
2064 RB_OBJ_WRITE(clone, &new_ivtbl->as.shape.ivptr[i], obj_ivtbl->as.shape.ivptr[i]);
2074 generic_ivtbl_no_ractor_check(clone);
2075 st_insert(generic_ivtbl_no_ractor_check(obj), (st_data_t)clone, (st_data_t)new_ivtbl);
2079 rb_shape_t * obj_shape = rb_shape_get_shape(obj);
2080 if (rb_shape_frozen_shape_p(obj_shape)) {
2081 rb_shape_set_shape_id(clone, obj_shape->parent_id);
2084 rb_shape_set_shape(clone, obj_shape);
2097 rb_replace_generic_ivar(
VALUE clone,
VALUE obj)
2103 st_data_t ivtbl, obj_data = (st_data_t)obj;
2104 if (st_lookup(generic_iv_tbl_, (st_data_t)obj, &ivtbl)) {
2105 st_insert(generic_iv_tbl_, (st_data_t)clone, ivtbl);
2106 st_delete(generic_iv_tbl_, &obj_data, NULL);
2123 obj_ivar_each(obj, func, arg);
2127 IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(0);
2130 class_ivar_each(obj, func, arg);
2136 gen_ivar_each(obj, func, arg);
2149 return ROBJECT_IV_COUNT(obj);
2152 return RCLASS_IV_COUNT(obj);
2157 if (rb_gen_ivtbl_get(obj, 0, &ivtbl)) {
2158 return gen_ivtbl_count(obj, ivtbl);
2167 ivar_i(
ID key,
VALUE v, st_data_t a)
2204 #define rb_is_constant_id rb_is_const_id
2205 #define rb_is_constant_name rb_is_const_name
2206 #define id_for_var(obj, name, part, type) \
2207 id_for_var_message(obj, name, type, "'%1$s' is not allowed as "#part" "#type" variable name")
2208 #define id_for_var_message(obj, name, type, message) \
2209 check_id_type(obj, &(name), rb_is_##type##_id, rb_is_##type##_name, message, strlen(message))
2212 int (*valid_id_p)(
ID),
int (*valid_name_p)(
VALUE),
2213 const char *message,
size_t message_len)
2216 VALUE name = *pname;
2218 if (
id ? !valid_id_p(
id) : !valid_name_p(name)) {
2219 rb_name_err_raise_str(rb_fstring_new(message, message_len),
2251 const ID id = id_for_var(obj, name, an, instance);
2255 rb_check_frozen(obj);
2260 if (!UNDEF_P(val))
return val;
2263 rb_name_err_raise(
"instance variable %1$s not defined",
2268 NORETURN(
static void uninitialized_constant(
VALUE,
VALUE));
2270 uninitialized_constant(
VALUE klass,
VALUE name)
2273 rb_name_err_raise(
"uninitialized constant %2$s::%1$s",
2276 rb_name_err_raise(
"uninitialized constant %1$s",
2284 rb_vm_inc_const_missing_count();
2330 rb_mod_const_missing(
VALUE klass,
VALUE name)
2333 VALUE ref = ec->private_const_reference;
2334 rb_vm_pop_cfunc_frame();
2336 ec->private_const_reference = 0;
2337 rb_name_err_raise(
"private constant %2$s::%1$s referenced", ref, name);
2339 uninitialized_constant(klass, name);
2345 autoload_table_mark(
void *
ptr)
2351 autoload_table_free(
void *
ptr)
2357 autoload_table_memsize(
const void *
ptr)
2360 return st_memsize(tbl);
2364 autoload_table_compact(
void *
ptr)
2371 {autoload_table_mark, autoload_table_free, autoload_table_memsize, autoload_table_compact,},
2372 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
2375 #define check_autoload_table(av) \
2376 (struct st_table *)rb_check_typeddata((av), &autoload_table_type)
2391 mod =
RBASIC(mod)->klass;
2399 VALUE tbl_value = rb_ivar_lookup(mod, autoload,
Qfalse);
2400 if (!
RTEST(tbl_value) || !(tbl = check_autoload_table(tbl_value)) || !st_lookup(tbl, (st_data_t)
id, &val)) {
2410 struct ccan_list_node cnode;
2413 VALUE autoload_data_value;
2425 rb_const_flag_t flag;
2441 rb_serial_t fork_gen;
2444 struct ccan_list_head constants;
2448 autoload_data_compact(
void *
ptr)
2457 autoload_data_mark(
void *
ptr)
2466 autoload_data_free(
void *
ptr)
2471 ccan_list_for_each_safe(&p->constants,
autoload_const, next, cnode) {
2479 autoload_data_memsize(
const void *
ptr)
2486 {autoload_data_mark, autoload_data_free, autoload_data_memsize, autoload_data_compact},
2487 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
2491 autoload_const_compact(
void *
ptr)
2496 ac->autoload_data_value =
rb_gc_location(ac->autoload_data_value);
2502 autoload_const_mark(
void *
ptr)
2513 autoload_const_memsize(
const void *
ptr)
2519 autoload_const_free(
void *
ptr)
2529 {autoload_const_mark, autoload_const_free, autoload_const_memsize, autoload_const_compact,},
2530 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
2534 get_autoload_data(
VALUE autoload_const_value,
struct autoload_const **autoload_const_pointer)
2547 if (autoload_const_pointer) *autoload_const_pointer =
autoload_const;
2553 rb_autoload(
VALUE module,
ID name,
const char *feature)
2555 if (!feature || !*feature) {
2559 rb_autoload_str(module, name, rb_fstring_cstr(feature));
2563 static void const_added(
VALUE klass,
ID const_name);
2572 autoload_feature_lookup_or_create(
VALUE feature,
struct autoload_data **autoload_data_pointer)
2574 RUBY_ASSERT_MUTEX_OWNED(autoload_mutex);
2575 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
2580 if (
NIL_P(autoload_data_value)) {
2586 if (autoload_data_pointer) *autoload_data_pointer =
autoload_data;
2588 rb_hash_aset(autoload_features, feature, autoload_data_value);
2590 else if (autoload_data_pointer) {
2591 *autoload_data_pointer =
rb_check_typeddata(autoload_data_value, &autoload_data_type);
2594 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
2595 return autoload_data_value;
2599 autoload_table_lookup_or_create(
VALUE module)
2601 VALUE autoload_table_value = rb_ivar_lookup(module, autoload,
Qfalse);
2602 if (
RTEST(autoload_table_value)) {
2603 return autoload_table_value;
2607 rb_class_ivar_set(module, autoload, autoload_table_value);
2609 return autoload_table_value;
2614 autoload_synchronized(
VALUE _arguments)
2618 rb_const_entry_t *constant_entry = rb_const_lookup(arguments->module, arguments->name);
2619 if (constant_entry && !UNDEF_P(constant_entry->value)) {
2624 const_set(arguments->module, arguments->name,
Qundef);
2626 VALUE autoload_table_value = autoload_table_lookup_or_create(arguments->module);
2627 struct st_table *autoload_table = check_autoload_table(autoload_table_value);
2630 VALUE feature = rb_fstring(arguments->feature);
2633 VALUE autoload_data_value = autoload_feature_lookup_or_create(feature, &
autoload_data);
2644 st_insert(autoload_table, (st_data_t)arguments->name, (st_data_t)autoload_const_value);
2671 if (result ==
Qtrue) {
2672 const_added(module, name);
2677 autoload_delete(
VALUE module,
ID name)
2679 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
2681 st_data_t load = 0, key = name;
2685 VALUE table_value = rb_ivar_lookup(module, autoload,
Qfalse);
2686 if (
RTEST(table_value)) {
2687 struct st_table *table = check_autoload_table(table_value);
2689 st_delete(table, &key, &load);
2712 if (table->num_entries == 0) {
2713 rb_attr_delete(module, autoload);
2718 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
2724 return ele->mutex !=
Qnil && !rb_mutex_owned_p(ele->mutex);
2728 check_autoload_required(
VALUE mod,
ID id,
const char **loadingpath)
2732 const char *loading;
2734 if (!autoload_const_value || !(
autoload_data = get_autoload_data(autoload_const_value, 0))) {
2747 return autoload_const_value;
2753 return autoload_const_value;
2756 if (loadingpath && loading) {
2757 *loadingpath = loading;
2758 return autoload_const_value;
2767 rb_autoloading_value(
VALUE mod,
ID id,
VALUE* value, rb_const_flag_t *flag)
2770 if (!ac)
return FALSE;
2786 return ele->mutex !=
Qnil && rb_mutex_owned_p(ele->mutex);
2793 autoloading_const_entry(
VALUE mod,
ID id)
2800 if (!load || !(ele = get_autoload_data(load, &ac))) {
2806 if (autoload_by_current(ele)) {
2807 if (!UNDEF_P(ac->value)) {
2816 autoload_defined_p(
VALUE mod,
ID id)
2821 if (!ce || !UNDEF_P(ce->value)) {
2827 return !rb_autoloading_value(mod,
id, NULL, NULL);
2849 check_before_mod_set(ac->module, ac->name, ac->value,
"constant");
2853 const_tbl_update(ac,
true);
2861 autoload_load_needed(
VALUE _arguments)
2865 const char *loading = 0, *src;
2867 if (!autoload_defined_p(arguments->module, arguments->name)) {
2871 VALUE autoload_const_value = check_autoload_required(arguments->module, arguments->name, &loading);
2872 if (!autoload_const_value) {
2877 if (src && loading && strcmp(src, loading) == 0) {
2898 return autoload_const_value;
2902 autoload_apply_constants(
VALUE _arguments)
2904 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
2916 ccan_list_for_each_safe(&arguments->autoload_data->constants,
autoload_const, next, cnode) {
2922 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
2928 autoload_feature_require(
VALUE _arguments)
2939 if (
RTEST(result)) {
2947 autoload_try_load(
VALUE _arguments)
2951 VALUE result = autoload_feature_require(_arguments);
2954 rb_const_entry_t *ce = rb_const_lookup(arguments->module, arguments->name);
2956 if (!ce || UNDEF_P(ce->value)) {
2961 if (arguments->module == rb_cObject) {
2963 "Expected %"PRIsVALUE
" to define %"PRIsVALUE
" but it didn't",
2964 arguments->autoload_data->feature,
2970 "Expected %"PRIsVALUE
" to define %"PRIsVALUE
"::%"PRIsVALUE
" but it didn't",
2971 arguments->autoload_data->feature,
2979 ce->flag |= arguments->flag;
2991 if (!ce || !UNDEF_P(ce->value)) {
2996 if (UNLIKELY(!rb_ractor_main_p())) {
2997 rb_raise(rb_eRactorUnsafeError,
"require by autoload on non-main Ractor is not supported (%s)",
rb_id2name(name));
3007 if (autoload_const_value ==
Qfalse)
return autoload_const_value;
3009 arguments.flag = ce->flag & (CONST_DEPRECATED | CONST_VISIBILITY_MASK);
3026 return rb_autoload_at_p(mod,
id, TRUE);
3030 rb_autoload_at_p(
VALUE mod,
ID id,
int recur)
3035 while (!autoload_defined_p(mod,
id)) {
3036 if (!recur)
return Qnil;
3038 if (!mod)
return Qnil;
3040 load = check_autoload_required(mod,
id, 0);
3041 if (!load)
return Qnil;
3042 return (ele = get_autoload_data(load, 0)) ? ele->feature :
Qnil;
3048 if (RB_CONST_DEPRECATED_P(ce) &&
3050 if (klass == rb_cObject) {
3061 rb_const_get_0(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility)
3063 VALUE c = rb_const_search(klass,
id, exclude, recurse, visibility);
3065 if (UNLIKELY(!rb_ractor_main_p())) {
3067 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));
3072 return rb_const_missing(klass,
ID2SYM(
id));
3076 rb_const_search_from(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility)
3078 VALUE value, current;
3079 bool first_iteration =
true;
3081 for (current = klass;
3083 current =
RCLASS_SUPER(current), first_iteration =
false) {
3088 if (!first_iteration && RCLASS_ORIGIN(current) != current) {
3101 while ((ce = rb_const_lookup(tmp,
id))) {
3102 if (visibility && RB_CONST_PRIVATE_P(ce)) {
3103 GET_EC()->private_const_reference = tmp;
3106 rb_const_warn_if_deprecated(ce, tmp,
id);
3108 if (UNDEF_P(value)) {
3110 if (am == tmp)
break;
3112 ac = autoloading_const_entry(tmp,
id);
3113 if (ac)
return ac->value;
3117 if (exclude && tmp == rb_cObject) {
3122 if (!recurse)
break;
3126 GET_EC()->private_const_reference = 0;
3131 rb_const_search(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility)
3135 if (klass == rb_cObject) exclude = FALSE;
3136 value = rb_const_search_from(klass,
id, exclude, recurse, visibility);
3137 if (!UNDEF_P(value))
return value;
3138 if (exclude)
return value;
3141 return rb_const_search_from(rb_cObject,
id, FALSE, recurse, visibility);
3147 return rb_const_get_0(klass,
id, TRUE, TRUE, FALSE);
3153 return rb_const_get_0(klass,
id, FALSE, TRUE, FALSE);
3159 return rb_const_get_0(klass,
id, TRUE, FALSE, FALSE);
3163 rb_public_const_get_from(
VALUE klass,
ID id)
3165 return rb_const_get_0(klass,
id, TRUE, TRUE, TRUE);
3169 rb_public_const_get_at(
VALUE klass,
ID id)
3171 return rb_const_get_0(klass,
id, TRUE, FALSE, TRUE);
3174 NORETURN(
static void undefined_constant(
VALUE mod,
VALUE name));
3178 rb_name_err_raise(
"constant %2$s::%1$s not defined",
3183 rb_const_location_from(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility)
3185 while (
RTEST(klass)) {
3188 while ((ce = rb_const_lookup(klass,
id))) {
3189 if (visibility && RB_CONST_PRIVATE_P(ce)) {
3192 if (exclude && klass == rb_cObject) {
3196 if (UNDEF_P(ce->value)) {
3198 if (
RTEST(autoload_const_value)) {
3211 if (!recurse)
break;
3220 rb_const_location(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility)
3224 if (klass == rb_cObject) exclude = FALSE;
3225 loc = rb_const_location_from(klass,
id, exclude, recurse, visibility);
3226 if (!
NIL_P(loc))
return loc;
3227 if (exclude)
return loc;
3230 return rb_const_location_from(rb_cObject,
id, FALSE, recurse, visibility);
3234 rb_const_source_location(
VALUE klass,
ID id)
3236 return rb_const_location(klass,
id, FALSE, TRUE, FALSE);
3240 rb_const_source_location_at(
VALUE klass,
ID id)
3242 return rb_const_location(klass,
id, TRUE, FALSE, FALSE);
3258 const ID id = id_for_var(mod, name, a, constant);
3261 undefined_constant(mod, name);
3272 rb_check_frozen(mod);
3274 ce = rb_const_lookup(mod,
id);
3275 if (!ce || !rb_id_table_delete(RCLASS_CONST_TBL(mod),
id)) {
3277 rb_name_err_raise(
"cannot remove %2$s::%1$s", mod,
ID2SYM(
id));
3280 undefined_constant(mod,
ID2SYM(
id));
3288 autoload_delete(mod,
id);
3298 cv_i_update(st_data_t *k, st_data_t *v, st_data_t a,
int existing)
3300 if (existing)
return ST_STOP;
3305 static enum rb_id_table_iterator_result
3306 sv_i(
ID key,
VALUE v,
void *a)
3312 st_update(tbl, (st_data_t)key, cv_i_update, (st_data_t)ce);
3314 return ID_TABLE_CONTINUE;
3317 static enum rb_id_table_iterator_result
3318 rb_local_constants_i(
ID const_name,
VALUE const_value,
void *ary)
3323 return ID_TABLE_CONTINUE;
3327 rb_local_constants(
VALUE mod)
3337 rb_id_table_foreach(tbl, rb_local_constants_i, (
void *)ary);
3349 tbl = st_init_numtable();
3351 if (RCLASS_CONST_TBL(mod)) {
3354 rb_id_table_foreach(RCLASS_CONST_TBL(mod), sv_i, tbl);
3369 if (tmp == rb_cObject && mod != rb_cObject)
break;
3375 list_i(st_data_t key, st_data_t value,
VALUE ary)
3418 bool inherit =
true;
3426 return rb_local_constants(mod);
3431 rb_const_defined_0(
VALUE klass,
ID id,
int exclude,
int recurse,
int visibility)
3440 if ((ce = rb_const_lookup(tmp,
id))) {
3441 if (visibility && RB_CONST_PRIVATE_P(ce)) {
3444 if (UNDEF_P(ce->value) && !check_autoload_required(tmp,
id, 0) &&
3445 !rb_autoloading_value(tmp,
id, NULL, NULL))
3448 if (exclude && tmp == rb_cObject && klass != rb_cObject) {
3454 if (!recurse)
break;
3468 return rb_const_defined_0(klass,
id, TRUE, TRUE, FALSE);
3474 return rb_const_defined_0(klass,
id, FALSE, TRUE, FALSE);
3480 return rb_const_defined_0(klass,
id, TRUE, FALSE, FALSE);
3484 rb_public_const_defined_from(
VALUE klass,
ID id)
3486 return rb_const_defined_0(klass,
id, TRUE, TRUE, TRUE);
3490 check_before_mod_set(
VALUE klass,
ID id,
VALUE val,
const char *dest)
3492 rb_check_frozen(klass);
3495 static void set_namespace_path(
VALUE named_namespace,
VALUE name);
3497 static enum rb_id_table_iterator_result
3498 set_namespace_path_i(
ID id,
VALUE v,
void *payload)
3501 VALUE value = ce->value;
3504 return ID_TABLE_CONTINUE;
3507 bool has_permanent_classpath;
3508 classname(value, &has_permanent_classpath);
3509 if (has_permanent_classpath) {
3510 return ID_TABLE_CONTINUE;
3512 set_namespace_path(value, build_const_path(parental_path,
id));
3514 if (!RCLASS_EXT(value)->permanent_classpath) {
3515 RCLASS_SET_CLASSPATH(value, 0,
false);
3518 return ID_TABLE_CONTINUE;
3527 set_namespace_path(
VALUE named_namespace,
VALUE namespace_path)
3529 struct rb_id_table *const_table = RCLASS_CONST_TBL(named_namespace);
3533 RCLASS_SET_CLASSPATH(named_namespace, namespace_path,
true);
3536 rb_id_table_foreach(const_table, set_namespace_path_i, &namespace_path);
3543 const_added(
VALUE klass,
ID const_name)
3545 if (GET_VM()->running) {
3562 rb_raise(rb_eRactorIsolationError,
"can not set constants with non-shareable objects by non-main Ractors");
3565 check_before_mod_set(klass,
id, val,
"constant");
3569 struct rb_id_table *tbl = RCLASS_CONST_TBL(klass);
3571 RCLASS_CONST_TBL(klass) = tbl = rb_id_table_create(0);
3574 rb_id_table_insert(tbl,
id, (
VALUE)ce);
3575 setup_const_entry(ce, klass, val, CONST_PUBLIC);
3579 .module = klass, .name = id,
3580 .value = val, .flag = CONST_PUBLIC,
3583 ac.file = rb_source_location(&ac.line);
3584 const_tbl_update(&ac,
false);
3593 if (rb_cObject && rb_namespace_p(val)) {
3594 bool val_path_permanent;
3595 VALUE val_path = classname(val, &val_path_permanent);
3596 if (
NIL_P(val_path) || !val_path_permanent) {
3597 if (klass == rb_cObject) {
3601 bool parental_path_permanent;
3602 VALUE parental_path = classname(klass, &parental_path_permanent);
3603 if (
NIL_P(parental_path)) {
3605 parental_path = rb_tmp_class_path(klass, &throwaway, make_temporary_path);
3607 if (parental_path_permanent && !val_path_permanent) {
3608 set_namespace_path(val, build_const_path(parental_path,
id));
3610 else if (!parental_path_permanent &&
NIL_P(val_path)) {
3611 RCLASS_SET_CLASSPATH(val, build_const_path(parental_path,
id),
false);
3621 const_set(klass,
id, val);
3622 const_added(klass,
id);
3626 autoload_data_for_named_constant(
VALUE module,
ID name,
struct autoload_const **autoload_const_pointer)
3629 if (!autoload_data_value)
return 0;
3646 VALUE klass = ac->module;
3647 VALUE val = ac->value;
3649 struct rb_id_table *tbl = RCLASS_CONST_TBL(klass);
3650 rb_const_flag_t visibility = ac->flag;
3653 if (rb_id_table_lookup(tbl,
id, &value)) {
3655 if (UNDEF_P(ce->value)) {
3656 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
3657 VALUE file = ac->file;
3658 int line = ac->line;
3659 struct autoload_data *ele = autoload_data_for_named_constant(klass,
id, &ac);
3661 if (!autoload_force && ele) {
3665 ac->file = rb_source_location(&ac->line);
3669 autoload_delete(klass,
id);
3670 ce->flag = visibility;
3675 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
3679 VALUE name = QUOTE_ID(
id);
3680 visibility = ce->flag;
3681 if (klass == rb_cObject)
3682 rb_warn(
"already initialized constant %"PRIsVALUE
"", name);
3684 rb_warn(
"already initialized constant %"PRIsVALUE
"::%"PRIsVALUE
"",
3686 if (!
NIL_P(ce->file) && ce->line) {
3688 "previous definition of %"PRIsVALUE
" was here", name);
3692 setup_const_entry(ce, klass, val, visibility);
3698 rb_id_table_insert(tbl,
id, (
VALUE)ce);
3699 setup_const_entry(ce, klass, val, visibility);
3705 rb_const_flag_t visibility)
3707 ce->flag = visibility;
3709 RB_OBJ_WRITE(klass, &ce->file, rb_source_location(&ce->line));
3718 rb_warn(
"rb_define_const: invalid name '%s' for constant", name);
3721 rb_vm_register_global_object(val);
3733 set_const_visibility(
VALUE mod,
int argc,
const VALUE *argv,
3734 rb_const_flag_t flag, rb_const_flag_t mask)
3742 rb_warning(
"%"PRIsVALUE
" with no argument is just ignored",
3747 for (i = 0; i < argc; i++) {
3749 VALUE val = argv[i];
3752 undefined_constant(mod, val);
3754 if ((ce = rb_const_lookup(mod,
id))) {
3757 if (UNDEF_P(ce->value)) {
3760 ele = autoload_data_for_named_constant(mod,
id, &ac);
3769 undefined_constant(mod,
ID2SYM(
id));
3779 long len = strlen(name);
3783 undefined_constant(mod, rb_fstring_new(name,
len));
3785 if (!(ce = rb_const_lookup(mod,
id))) {
3786 undefined_constant(mod,
ID2SYM(
id));
3788 ce->flag |= CONST_DEPRECATED;
3799 rb_mod_private_constant(
int argc,
const VALUE *argv,
VALUE obj)
3801 set_const_visibility(obj, argc, argv, CONST_PRIVATE, CONST_VISIBILITY_MASK);
3813 rb_mod_public_constant(
int argc,
const VALUE *argv,
VALUE obj)
3815 set_const_visibility(obj, argc, argv, CONST_PUBLIC, CONST_VISIBILITY_MASK);
3839 rb_mod_deprecate_constant(
int argc,
const VALUE *argv,
VALUE obj)
3841 set_const_visibility(obj, argc, argv, CONST_DEPRECATED, CONST_DEPRECATED);
3846 original_module(
VALUE c)
3854 cvar_lookup_at(
VALUE klass,
ID id, st_data_t *v)
3862 klass =
RBASIC(klass)->klass;
3867 if (UNDEF_P(n))
return 0;
3874 cvar_front_klass(
VALUE klass)
3876 if (RCLASS_SINGLETON_P(klass)) {
3877 VALUE obj = RCLASS_ATTACHED_OBJECT(klass);
3878 if (rb_namespace_p(obj)) {
3888 if (front && target != front) {
3889 if (original_module(front) != original_module(target)) {
3891 "class variable % "PRIsVALUE
" of %"PRIsVALUE
" is overtaken by %"PRIsVALUE
"",
3896 rb_ivar_delete(front,
id,
Qundef);
3901 #define CVAR_FOREACH_ANCESTORS(klass, v, r) \
3902 for (klass = cvar_front_klass(klass); klass; klass = RCLASS_SUPER(klass)) { \
3903 if (cvar_lookup_at(klass, id, (v))) { \
3908 #define CVAR_LOOKUP(v,r) do {\
3909 CVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(); \
3910 if (cvar_lookup_at(klass, id, (v))) {r;}\
3911 CVAR_FOREACH_ANCESTORS(klass, v, r);\
3929 check_for_cvar_table(
VALUE subclass,
VALUE key)
3933 RB_DEBUG_COUNTER_INC(cvar_class_invalidate);
3934 ruby_vm_global_cvar_state++;
3938 rb_class_foreach_subclass(subclass, check_for_cvar_table, key);
3944 VALUE tmp, front = 0, target = 0;
3947 CVAR_LOOKUP(0, {
if (!front) front = klass; target = klass;});
3949 cvar_overtaken(front, target,
id);
3956 target =
RBASIC(target)->klass;
3958 check_before_mod_set(target,
id, val,
"class variable");
3960 int result = rb_class_ivar_set(target,
id, val);
3962 struct rb_id_table *rb_cvc_tbl = RCLASS_CVC_TBL(target);
3965 rb_cvc_tbl = RCLASS_CVC_TBL(target) = rb_id_table_create(2);
3971 if (!rb_id_table_lookup(rb_cvc_tbl,
id, &ent_data)) {
3973 ent->class_value = target;
3974 ent->global_cvar_state = GET_GLOBAL_CVAR_STATE();
3976 rb_id_table_insert(rb_cvc_tbl,
id, (
VALUE)ent);
3977 RB_DEBUG_COUNTER_INC(cvar_inline_miss);
3980 ent = (
void *)ent_data;
3981 ent->global_cvar_state = GET_GLOBAL_CVAR_STATE();
3989 if (RCLASS_SUBCLASSES(target)) {
3990 rb_class_foreach_subclass(target, check_for_cvar_table,
id);
4002 value = find_cvar(klass, front, &target,
id);
4004 rb_name_err_raise(
"uninitialized class variable %1$s in %2$s",
4007 cvar_overtaken(*front, target,
id);
4008 return (
VALUE)value;
4021 if (!klass)
return Qfalse;
4022 CVAR_LOOKUP(0,
return Qtrue);
4027 cv_intern(
VALUE klass,
const char *name)
4031 rb_name_err_raise(
"wrong class variable name %1$s",
4040 ID id = cv_intern(klass, name);
4047 ID id = cv_intern(klass, name);
4058 cv_i(
ID key,
VALUE v, st_data_t a)
4063 st_update(tbl, (st_data_t)key, cv_i_update, 0);
4069 mod_cvar_at(
VALUE mod,
void *data)
4073 tbl = st_init_numtable();
4075 mod = original_module(mod);
4082 mod_cvar_of(
VALUE mod,
void *data)
4085 if (RCLASS_SINGLETON_P(mod)) {
4086 if (rb_namespace_p(RCLASS_ATTACHED_OBJECT(mod))) {
4087 data = mod_cvar_at(tmp, data);
4088 tmp = cvar_front_klass(tmp);
4092 data = mod_cvar_at(tmp, data);
4100 cv_list_i(st_data_t key, st_data_t value,
VALUE ary)
4108 cvar_list(
void *data)
4144 bool inherit =
true;
4149 tbl = mod_cvar_of(mod, 0);
4152 tbl = mod_cvar_at(mod, 0);
4154 return cvar_list(tbl);
4179 const ID id = id_for_var_message(mod, name,
class,
"wrong class variable name %1$s");
4185 rb_check_frozen(mod);
4186 val = rb_ivar_delete(mod,
id,
Qundef);
4187 if (!UNDEF_P(val)) {
4191 rb_name_err_raise(
"cannot remove %1$s for %2$s", mod,
ID2SYM(
id));
4194 rb_name_err_raise(
"class variable %1$s not defined for %2$s",
4219 class_ivar_set_shape_ivptr(
VALUE obj,
void *_data)
4223 return RCLASS_IVPTR(obj);
4227 class_ivar_set_shape_resize_ivptr(
VALUE obj, attr_index_t _old_capa, attr_index_t new_capa,
void *_data)
4235 rb_shape_set_shape(obj, shape);
4239 class_ivar_set_transition_too_complex(
VALUE obj,
void *_data)
4241 rb_evict_ivars_to_hash(obj);
4245 class_ivar_set_too_complex_table(
VALUE obj,
void *_data)
4249 return RCLASS_IV_HASH(obj);
4256 bool existing =
false;
4257 rb_check_frozen(obj);
4261 existing = general_ivar_set(obj,
id, val, NULL,
4262 class_ivar_set_shape_ivptr,
4263 class_ivar_set_shape_resize_ivptr,
4264 class_ivar_set_set_shape,
4265 class_ivar_set_transition_too_complex,
4266 class_ivar_set_too_complex_table).existing;
4274 tbl_copy_i(
ID key,
VALUE val, st_data_t dest)
4276 rb_class_ivar_set((
VALUE)dest, key, val);
4294 rb_const_lookup(
VALUE klass,
ID id)
4296 struct rb_id_table *tbl = RCLASS_CONST_TBL(klass);
4303 r = rb_id_table_lookup(tbl,
id, &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 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 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_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_category_warn(rb_warning_category_t category, const char *fmt,...)
Identical to rb_category_warning(), except it reports unless $VERBOSE is nil.
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
void rb_compile_warn(const char *file, int line, const char *fmt,...)
Identical to rb_compile_warning(), except it reports unless $VERBOSE is nil.
void rb_bug(const char *fmt,...)
Interpreter panic switch.
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_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_eArgError
ArgumentError exception.
VALUE rb_ensure(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*e_proc)(VALUE), VALUE data2)
An equivalent to ensure clause.
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.
rb_encoding * rb_usascii_encoding(void)
Queries the encoding that represents US-ASCII.
static bool rb_enc_asciicompat(rb_encoding *enc)
Queries if the passed encoding is in some sense compatible with ASCII.
rb_encoding * rb_enc_get(VALUE obj)
Identical to rb_enc_get_index(), except the return type.
ID rb_check_id_cstr(const char *ptr, long len, rb_encoding *enc)
Identical to rb_check_id(), except it takes a pointer to a memory region instead of Ruby's string.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_funcallv(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcall(), except it takes the method arguments as a C array.
void rb_mark_tbl_no_pin(struct st_table *tbl)
Identical to rb_mark_tbl(), except it marks objects using rb_gc_mark_movable().
void rb_gc_mark_movable(VALUE obj)
Maybe this is the only function provided for C extensions to control the pinning of objects,...
void rb_gc_mark_maybe(VALUE obj)
Identical to rb_gc_mark(), except it allows the passed value be a non-object.
VALUE rb_gc_location(VALUE obj)
Finds a new "location" of an object.
void rb_gc_update_tbl_refs(st_table *ptr)
Updates references inside of tables.
VALUE rb_ary_new(void)
Allocates a new, 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.
ID rb_frame_callee(void)
Identical to rb_frame_this_func(), except it returns the named used to call the method.
#define st_foreach_safe
Just another name of rb_st_foreach_safe.
VALUE rb_hash_delete(VALUE hash, VALUE key)
Deletes the passed key from the passed hash table, if any.
VALUE rb_hash_aref(VALUE hash, VALUE key)
Queries the given key in the given hash table.
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Inserts or replaces ("upsert"s) the objects into the given hash table.
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_cat2(VALUE, const char *)
Just another name of rb_str_cat_cstr.
VALUE rb_str_dup(VALUE str)
Duplicates a string.
VALUE rb_str_new_cstr(const char *ptr)
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_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.
VALUE rb_attr_get(VALUE obj, ID name)
Identical to rb_ivar_get()
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_mod_const_at(VALUE, void *)
This API is mysterious.
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_ivar_foreach(VALUE obj, int(*func)(ID name, VALUE val, st_data_t arg), st_data_t arg)
Iterates over an object's instance variables.
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.
st_index_t rb_ivar_count(VALUE obj)
Number of instance variables defined on an object.
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.
void * rb_mod_const_of(VALUE, void *)
This is a variant of rb_mod_const_at().
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.
ID rb_intern2(const char *name, long len)
Identical to rb_intern(), except it additionally takes the length of the string.
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.
const char * rb_id2name(ID id)
Retrieves the name mapped to the given id.
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
ID rb_to_id(VALUE str)
Identical to rb_intern(), except it takes an instance of rb_cString.
VALUE rb_id2str(ID id)
Identical to rb_id2name(), except it returns a Ruby's String instead of C's.
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_virtual_variable(const char *name, rb_gvar_getter_t *getter, rb_gvar_setter_t *setter)
Defines a global variable that is purely function-backended.
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.
void rb_define_hooked_variable(const char *name, VALUE *var, rb_gvar_getter_t *getter, rb_gvar_setter_t *setter)
Identical to rb_define_virtual_variable(), but can also specify a storage.
rb_gvar_getter_t rb_gvar_var_getter
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
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.
VALUE rb_sprintf(const char *fmt,...)
Ruby's extended sprintf(3).
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
VALUE type(ANYARGS)
ANYARGS-ed function type.
void rb_copy_generic_ivar(VALUE clone, VALUE obj)
Copies the list of instance variables.
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.
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
#define RTYPEDDATA_DATA(v)
Convenient getter macro.
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
const char * rb_class2name(VALUE klass)
Queries the name of the passed class.
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
#define RB_NO_KEYWORDS
Do not pass keywords.
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
This is the struct that holds necessary info for a struct.
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.
void ruby_xfree(void *ptr)
Deallocates a storage instance.