Ruby 4.1.0dev (2026-05-05 revision 138b4ba2fb455a9e9617259b59d11566a08b711a)
variable.c (138b4ba2fb455a9e9617259b59d11566a08b711a)
1/**********************************************************************
2
3 variable.c -
4
5 $Author$
6 created at: Tue Apr 19 23:55:15 JST 1994
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9 Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
10 Copyright (C) 2000 Information-technology Promotion Agency, Japan
11
12**********************************************************************/
13
14#include "ruby/internal/config.h"
15#include <stddef.h>
17#include "ccan/list/list.h"
18#include "constant.h"
19#include "debug_counter.h"
20#include "id.h"
21#include "id_table.h"
22#include "internal.h"
23#include "internal/box.h"
24#include "internal/class.h"
25#include "internal/compilers.h"
26#include "internal/error.h"
27#include "internal/eval.h"
28#include "internal/hash.h"
29#include "internal/object.h"
30#include "internal/gc.h"
31#include "internal/re.h"
32#include "internal/struct.h"
33#include "internal/symbol.h"
34#include "internal/thread.h"
35#include "internal/variable.h"
36#include "ruby/encoding.h"
37#include "ruby/st.h"
38#include "ruby/util.h"
39#include "shape.h"
40#include "symbol.h"
41#include "variable.h"
42#include "vm_core.h"
43#include "ractor_core.h"
44#include "vm_sync.h"
45
46RUBY_EXTERN rb_serial_t ruby_vm_global_cvar_state;
47#define GET_GLOBAL_CVAR_STATE() (ruby_vm_global_cvar_state)
48
49typedef void rb_gvar_compact_t(void *var);
50
51static struct rb_id_table *rb_global_tbl;
52static ID autoload;
53
54// This hash table maps file paths to loadable features. We use this to track
55// autoload state until it's no longer needed.
56// feature (file path) => struct autoload_data
57static VALUE autoload_features;
58
59// This mutex is used to protect autoloading state. We use a global mutex which
60// is held until a per-feature mutex can be created. This ensures there are no
61// race conditions relating to autoload state.
62static VALUE autoload_mutex;
63
64static void check_before_mod_set(VALUE, ID, VALUE, const char *);
65static void setup_const_entry(rb_const_entry_t *, VALUE, VALUE, rb_const_flag_t);
66static VALUE rb_const_search(VALUE klass, ID id, int exclude, int recurse, int visibility, VALUE *found_in);
67static st_table *generic_fields_tbl_;
68
69typedef int rb_ivar_foreach_callback_func(ID key, VALUE val, st_data_t arg);
70static void rb_field_foreach(VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg, bool ivar_only);
71
72void
73Init_var_tables(void)
74{
75 rb_global_tbl = rb_id_table_create(0);
76 generic_fields_tbl_ = st_init_numtable();
77 autoload = rb_intern_const("__autoload__");
78
79 autoload_mutex = rb_mutex_new();
80 rb_obj_hide(autoload_mutex);
81 rb_vm_register_global_object(autoload_mutex);
82
83 autoload_features = rb_ident_hash_new();
84 rb_obj_hide(autoload_features);
85 rb_vm_register_global_object(autoload_features);
86}
87
88static inline bool
89rb_namespace_p(VALUE obj)
90{
91 if (RB_SPECIAL_CONST_P(obj)) return false;
92 switch (RB_BUILTIN_TYPE(obj)) {
93 case T_MODULE: case T_CLASS: return true;
94 default: break;
95 }
96 return false;
97}
98
109static VALUE
110classname(VALUE klass, bool *permanent)
111{
112 *permanent = false;
113
114 VALUE classpath = RCLASS_CLASSPATH(klass);
115 if (classpath == 0) return Qnil;
116
117 *permanent = RCLASS_PERMANENT_CLASSPATH_P(klass);
118
119 return classpath;
120}
121
122VALUE
123rb_mod_name0(VALUE klass, bool *permanent)
124{
125 return classname(klass, permanent);
126}
127
128/*
129 * call-seq:
130 * mod.name -> string or nil
131 *
132 * Returns the name of the module <i>mod</i>. Returns +nil+ for anonymous modules.
133 */
134
135VALUE
137{
138 // YJIT needs this function to not allocate.
139 bool permanent;
140 return classname(mod, &permanent);
141}
142
143// Similar to logic in rb_mod_const_get().
144static bool
145is_constant_path(VALUE name)
146{
147 const char *path = RSTRING_PTR(name);
148 const char *pend = RSTRING_END(name);
149 rb_encoding *enc = rb_enc_get(name);
150
151 const char *p = path;
152
153 if (p >= pend || !*p) {
154 return false;
155 }
156
157 while (p < pend) {
158 if (p + 2 <= pend && p[0] == ':' && p[1] == ':') {
159 p += 2;
160 }
161
162 const char *pbeg = p;
163 while (p < pend && *p != ':') p++;
164
165 if (pbeg == p) return false;
166
167 if (rb_enc_symname_type(pbeg, p - pbeg, enc, 0) != ID_CONST) {
168 return false;
169 }
170 }
171
172 return true;
173}
174
176 VALUE names;
177 ID last;
178};
179
180static VALUE build_const_path(VALUE head, ID tail);
181static void set_sub_temporary_name_foreach(VALUE mod, struct sub_temporary_name_args *args, VALUE name);
182
183static VALUE
184set_sub_temporary_name_recursive(VALUE mod, VALUE data, int recursive)
185{
186 if (recursive) return Qfalse;
187
188 struct sub_temporary_name_args *args = (void *)data;
189 VALUE name = 0;
190 if (args->names) {
191 name = build_const_path(rb_ary_last(0, 0, args->names), args->last);
192 }
193 set_sub_temporary_name_foreach(mod, args, name);
194 return Qtrue;
195}
196
197static VALUE
198set_sub_temporary_name_topmost(VALUE mod, VALUE data, int recursive)
199{
200 if (recursive) return Qfalse;
201
202 struct sub_temporary_name_args *args = (void *)data;
203 VALUE name = args->names;
204 if (name) {
205 args->names = rb_ary_hidden_new(0);
206 }
207 set_sub_temporary_name_foreach(mod, args, name);
208 return Qtrue;
209}
210
211static enum rb_id_table_iterator_result
212set_sub_temporary_name_i(ID id, VALUE val, void *data)
213{
214 val = ((rb_const_entry_t *)val)->value;
215 if (rb_namespace_p(val) && !RCLASS_PERMANENT_CLASSPATH_P(val)) {
216 VALUE arg = (VALUE)data;
217 struct sub_temporary_name_args *args = data;
218 args->last = id;
219 rb_exec_recursive_paired(set_sub_temporary_name_recursive, val, arg, arg);
220 }
221 return ID_TABLE_CONTINUE;
222}
223
224static void
225set_sub_temporary_name_foreach(VALUE mod, struct sub_temporary_name_args *args, VALUE name)
226{
227 RCLASS_WRITE_CLASSPATH(mod, name, FALSE);
228 struct rb_id_table *tbl = RCLASS_CONST_TBL(mod);
229 if (!tbl) return;
230 if (!name) {
231 rb_id_table_foreach(tbl, set_sub_temporary_name_i, args);
232 }
233 else {
234 long names_len = RARRAY_LEN(args->names); // paranoiac check?
235 rb_ary_push(args->names, name);
236 rb_id_table_foreach(tbl, set_sub_temporary_name_i, args);
237 rb_ary_set_len(args->names, names_len);
238 }
239}
240
241static void
242set_sub_temporary_name(VALUE mod, VALUE name)
243{
244 struct sub_temporary_name_args args = {name};
245 VALUE arg = (VALUE)&args;
246 rb_exec_recursive_paired(set_sub_temporary_name_topmost, mod, arg, arg);
247}
248
249/*
250 * call-seq:
251 * mod.set_temporary_name(string) -> self
252 * mod.set_temporary_name(nil) -> self
253 *
254 * Sets the temporary name of the module. This name is reflected in
255 * introspection of the module and the values that are related to it, such
256 * as instances, constants, and methods.
257 *
258 * The name should be +nil+ or a non-empty string that is not a valid constant
259 * path (to avoid confusing between permanent and temporary names).
260 *
261 * The method can be useful to distinguish dynamically generated classes and
262 * modules without assigning them to constants.
263 *
264 * If the module is given a permanent name by assigning it to a constant,
265 * the temporary name is discarded. A temporary name can't be assigned to
266 * modules that have a permanent name.
267 *
268 * If the given name is +nil+, the module becomes anonymous again.
269 *
270 * Example:
271 *
272 * m = Module.new # => #<Module:0x0000000102c68f38>
273 * m.name #=> nil
274 *
275 * m.set_temporary_name("fake_name") # => fake_name
276 * m.name #=> "fake_name"
277 *
278 * m.set_temporary_name(nil) # => #<Module:0x0000000102c68f38>
279 * m.name #=> nil
280 *
281 * c = Class.new
282 * c.set_temporary_name("MyClass(with description)") # => MyClass(with description)
283 *
284 * c.new # => #<MyClass(with description):0x0....>
285 *
286 * c::M = m
287 * c::M.name #=> "MyClass(with description)::M"
288 *
289 * # Assigning to a constant replaces the name with a permanent one
290 * C = c
291 *
292 * C.name #=> "C"
293 * C::M.name #=> "C::M"
294 * c.new # => #<C:0x0....>
295 */
296
297VALUE
298rb_mod_set_temporary_name(VALUE mod, VALUE name)
299{
300 // We don't allow setting the name if the classpath is already permanent:
301 if (RCLASS_PERMANENT_CLASSPATH_P(mod)) {
302 rb_raise(rb_eRuntimeError, "can't change permanent name");
303 }
304
305 if (NIL_P(name)) {
306 // Set the temporary classpath to NULL (anonymous):
307 RB_VM_LOCKING() {
308 set_sub_temporary_name(mod, 0);
309 }
310 }
311 else {
312 // Ensure the name is a string:
313 StringValue(name);
314
315 if (RSTRING_LEN(name) == 0) {
316 rb_raise(rb_eArgError, "empty class/module name");
317 }
318
319 if (is_constant_path(name)) {
320 rb_raise(rb_eArgError, "the temporary name must not be a constant path to avoid confusion");
321 }
322
323 name = rb_str_new_frozen(name);
324 RB_OBJ_SET_SHAREABLE(name);
325
326 // Set the temporary classpath to the given name:
327 RB_VM_LOCKING() {
328 set_sub_temporary_name(mod, name);
329 }
330 }
331
332 return mod;
333}
334
335static VALUE
336make_temporary_path(VALUE obj, VALUE klass)
337{
338 VALUE path;
339 switch (klass) {
340 case Qnil:
341 path = rb_sprintf("#<Class:%p>", (void*)obj);
342 break;
343 case Qfalse:
344 path = rb_sprintf("#<Module:%p>", (void*)obj);
345 break;
346 default:
347 path = rb_sprintf("#<%"PRIsVALUE":%p>", klass, (void*)obj);
348 break;
349 }
350 OBJ_FREEZE(path);
351 return path;
352}
353
354typedef VALUE (*fallback_func)(VALUE obj, VALUE name);
355
356static VALUE
357rb_tmp_class_path(VALUE klass, bool *permanent, fallback_func fallback)
358{
359 VALUE path = classname(klass, permanent);
360
361 if (!NIL_P(path)) {
362 return path;
363 }
364
365 if (RB_TYPE_P(klass, T_MODULE)) {
366 if (rb_obj_class(klass) == rb_cModule) {
367 path = Qfalse;
368 }
369 else {
370 bool perm;
371 path = rb_tmp_class_path(RBASIC(klass)->klass, &perm, fallback);
372 }
373 }
374
375 *permanent = false;
376 return fallback(klass, path);
377}
378
379VALUE
381{
382 bool permanent;
383 VALUE path = rb_tmp_class_path(klass, &permanent, make_temporary_path);
384 if (!NIL_P(path)) path = rb_str_dup(path);
385 return path;
386}
387
388VALUE
390{
391 return rb_mod_name(klass);
392}
393
394static VALUE
395no_fallback(VALUE obj, VALUE name)
396{
397 return name;
398}
399
400VALUE
401rb_search_class_path(VALUE klass)
402{
403 bool permanent;
404 return rb_tmp_class_path(klass, &permanent, no_fallback);
405}
406
407static VALUE
408build_const_pathname(VALUE head, VALUE tail)
409{
410 VALUE path = rb_str_dup(head);
411 rb_str_cat2(path, "::");
412 rb_str_append(path, tail);
413 return rb_fstring(path);
414}
415
416static VALUE
417build_const_path(VALUE head, ID tail)
418{
419 return build_const_pathname(head, rb_id2str(tail));
420}
421
422void
424{
425 bool permanent = true;
426
427 VALUE str;
428 if (under == rb_cObject) {
429 str = rb_str_new_frozen(name);
430 }
431 else {
432 str = rb_tmp_class_path(under, &permanent, make_temporary_path);
433 str = build_const_pathname(str, name);
434 }
435
436 RB_OBJ_SET_SHAREABLE(str);
437 RCLASS_SET_CLASSPATH(klass, str, permanent);
438}
439
440void
441rb_set_class_path(VALUE klass, VALUE under, const char *name)
442{
443 VALUE str = rb_str_new2(name);
444 OBJ_FREEZE(str);
445 rb_set_class_path_string(klass, under, str);
446}
447
448VALUE
450{
451 rb_encoding *enc = rb_enc_get(pathname);
452 const char *pbeg, *pend, *p, *path = RSTRING_PTR(pathname);
453 ID id;
454 VALUE c = rb_cObject;
455
456 if (!rb_enc_asciicompat(enc)) {
457 rb_raise(rb_eArgError, "invalid class path encoding (non ASCII)");
458 }
459 pbeg = p = path;
460 pend = path + RSTRING_LEN(pathname);
461 if (path == pend || path[0] == '#') {
462 rb_raise(rb_eArgError, "can't retrieve anonymous class %"PRIsVALUE,
463 QUOTE(pathname));
464 }
465 while (p < pend) {
466 while (p < pend && *p != ':') p++;
467 id = rb_check_id_cstr(pbeg, p-pbeg, enc);
468 if (p < pend && p[0] == ':') {
469 if ((size_t)(pend - p) < 2 || p[1] != ':') goto undefined_class;
470 p += 2;
471 pbeg = p;
472 }
473 if (!id) {
474 goto undefined_class;
475 }
476 c = rb_const_search(c, id, TRUE, FALSE, FALSE, NULL);
477 if (UNDEF_P(c)) goto undefined_class;
478 if (!rb_namespace_p(c)) {
479 rb_raise(rb_eTypeError, "%"PRIsVALUE" does not refer to class/module",
480 pathname);
481 }
482 }
483 RB_GC_GUARD(pathname);
484
485 return c;
486
487 undefined_class:
488 rb_raise(rb_eArgError, "undefined class/module % "PRIsVALUE,
489 rb_str_subseq(pathname, 0, p-path));
491}
492
493VALUE
494rb_path2class(const char *path)
495{
496 return rb_path_to_class(rb_str_new_cstr(path));
497}
498
499VALUE
501{
502 return rb_class_path(rb_class_real(klass));
503}
504
505const char *
507{
508 bool permanent;
509 VALUE path = rb_tmp_class_path(rb_class_real(klass), &permanent, make_temporary_path);
510 if (NIL_P(path)) return NULL;
511 return RSTRING_PTR(path);
512}
513
514const char *
516{
517 return rb_class2name(CLASS_OF(obj));
518}
519
520struct trace_var {
521 int removed;
522 void (*func)(VALUE arg, VALUE val);
523 VALUE data;
524 struct trace_var *next;
525};
526
528 int counter;
529 int block_trace;
530 VALUE *data;
531 rb_gvar_getter_t *getter;
532 rb_gvar_setter_t *setter;
533 rb_gvar_marker_t *marker;
534 rb_gvar_compact_t *compactor;
535 struct trace_var *trace;
536 bool box_ready;
537 bool box_dynamic;
538};
539
541 struct rb_global_variable *var;
542 ID id;
543 bool ractor_local;
544};
545
546static void
547free_global_variable(struct rb_global_variable *var)
548{
549 RUBY_ASSERT(var->counter == 0);
550
551 struct trace_var *trace = var->trace;
552 while (trace) {
553 struct trace_var *next = trace->next;
554 SIZED_FREE(trace);
555 trace = next;
556 }
557 SIZED_FREE(var);
558}
559
560static enum rb_id_table_iterator_result
561free_global_entry_i(VALUE val, void *arg)
562{
563 struct rb_global_entry *entry = (struct rb_global_entry *)val;
564 entry->var->counter--;
565 if (entry->var->counter == 0) {
566 free_global_variable(entry->var);
567 }
568 SIZED_FREE(entry);
569 return ID_TABLE_DELETE;
570}
571
572void
573rb_free_rb_global_tbl(void)
574{
575 rb_id_table_foreach_values(rb_global_tbl, free_global_entry_i, 0);
576 rb_id_table_free(rb_global_tbl);
577}
578
579void
580rb_free_generic_fields_tbl_(void)
581{
582 st_free_table(generic_fields_tbl_);
583}
584
585static struct rb_global_entry*
586rb_find_global_entry(ID id)
587{
588 struct rb_global_entry *entry;
589 VALUE data;
590
591 RB_VM_LOCKING() {
592 if (!rb_id_table_lookup(rb_global_tbl, id, &data)) {
593 entry = NULL;
594 }
595 else {
596 entry = (struct rb_global_entry *)data;
597 RUBY_ASSERT(entry != NULL);
598 }
599 }
600
601 if (UNLIKELY(!rb_ractor_main_p()) && (!entry || !entry->ractor_local)) {
602 rb_raise(rb_eRactorIsolationError, "can not access global variable %s from non-main Ractor", rb_id2name(id));
603 }
604
605 return entry;
606}
607
608void
609rb_gvar_ractor_local(const char *name)
610{
611 struct rb_global_entry *entry = rb_find_global_entry(rb_intern(name));
612 entry->ractor_local = true;
613}
614
615void
616rb_gvar_box_ready(const char *name)
617{
618 struct rb_global_entry *entry = rb_find_global_entry(rb_intern(name));
619 entry->var->box_ready = true;
620}
621
622void
623rb_gvar_box_dynamic(const char *name)
624{
625 struct rb_global_entry *entry = rb_find_global_entry(rb_intern(name));
626 entry->var->box_dynamic = true;
627}
628
629static void
630rb_gvar_undef_compactor(void *var)
631{
632}
633
634static struct rb_global_entry*
636{
637 struct rb_global_entry *entry;
638 RB_VM_LOCKING() {
639 entry = rb_find_global_entry(id);
640 if (!entry) {
641 struct rb_global_variable *var;
642 entry = ALLOC(struct rb_global_entry);
643 var = ALLOC(struct rb_global_variable);
644 entry->id = id;
645 entry->var = var;
646 entry->ractor_local = false;
647 var->counter = 1;
648 var->data = 0;
649 var->getter = rb_gvar_undef_getter;
650 var->setter = rb_gvar_undef_setter;
651 var->marker = rb_gvar_undef_marker;
652 var->compactor = rb_gvar_undef_compactor;
653
654 var->block_trace = 0;
655 var->trace = 0;
656 var->box_ready = false;
657 var->box_dynamic = false;
658 rb_id_table_insert(rb_global_tbl, id, (VALUE)entry);
659 }
660 }
661 return entry;
662}
663
664VALUE
666{
667 rb_warning("global variable '%"PRIsVALUE"' not initialized", QUOTE_ID(id));
668
669 return Qnil;
670}
671
672static void
673rb_gvar_val_compactor(void *_var)
674{
675 struct rb_global_variable *var = (struct rb_global_variable *)_var;
676
677 VALUE obj = (VALUE)var->data;
678
679 if (obj) {
680 VALUE new = rb_gc_location(obj);
681 if (new != obj) {
682 var->data = (void*)new;
683 }
684 }
685}
686
687void
689{
690 struct rb_global_variable *var = rb_global_entry(id)->var;
691 var->getter = rb_gvar_val_getter;
692 var->setter = rb_gvar_val_setter;
693 var->marker = rb_gvar_val_marker;
694 var->compactor = rb_gvar_val_compactor;
695
696 var->data = (void*)val;
697}
698
699void
701{
702}
703
704VALUE
705rb_gvar_val_getter(ID id, VALUE *data)
706{
707 return (VALUE)data;
708}
709
710void
712{
713 struct rb_global_variable *var = rb_global_entry(id)->var;
714 var->data = (void*)val;
715}
716
717void
719{
720 VALUE data = (VALUE)var;
721 if (data) rb_gc_mark_movable(data);
722}
723
724VALUE
726{
727 if (!var) return Qnil;
728 return *var;
729}
730
731void
732rb_gvar_var_setter(VALUE val, ID id, VALUE *data)
733{
734 *data = val;
735}
736
737void
739{
740 if (var) rb_gc_mark_maybe(*var);
741}
742
743void
745{
746 rb_name_error(id, "%"PRIsVALUE" is a read-only variable", QUOTE_ID(id));
747}
748
749static enum rb_id_table_iterator_result
750mark_global_entry(VALUE v, void *ignored)
751{
752 struct rb_global_entry *entry = (struct rb_global_entry *)v;
753 struct trace_var *trace;
754 struct rb_global_variable *var = entry->var;
755
756 (*var->marker)(var->data);
757 trace = var->trace;
758 while (trace) {
759 if (trace->data) rb_gc_mark_maybe(trace->data);
760 trace = trace->next;
761 }
762 return ID_TABLE_CONTINUE;
763}
764
765#define gc_mark_table(task) \
766 if (rb_global_tbl) { rb_id_table_foreach_values(rb_global_tbl, task##_global_entry, 0); }
767
768void
769rb_gc_mark_global_tbl(void)
770{
771 gc_mark_table(mark);
772}
773
774static enum rb_id_table_iterator_result
775update_global_entry(VALUE v, void *ignored)
776{
777 struct rb_global_entry *entry = (struct rb_global_entry *)v;
778 struct rb_global_variable *var = entry->var;
779
780 (*var->compactor)(var);
781 return ID_TABLE_CONTINUE;
782}
783
784void
785rb_gc_update_global_tbl(void)
786{
787 gc_mark_table(update);
788}
789
790static ID
791global_id(const char *name)
792{
793 ID id;
794
795 if (name[0] == '$') id = rb_intern(name);
796 else {
797 size_t len = strlen(name);
798 VALUE vbuf = 0;
799 char *buf = ALLOCV_N(char, vbuf, len+1);
800 buf[0] = '$';
801 memcpy(buf+1, name, len);
802 id = rb_intern2(buf, len+1);
803 ALLOCV_END(vbuf);
804 }
805 return id;
806}
807
808static ID
809find_global_id(const char *name)
810{
811 ID id;
812 size_t len = strlen(name);
813
814 if (name[0] == '$') {
815 id = rb_check_id_cstr(name, len, NULL);
816 }
817 else {
818 VALUE vbuf = 0;
819 char *buf = ALLOCV_N(char, vbuf, len+1);
820 buf[0] = '$';
821 memcpy(buf+1, name, len);
822 id = rb_check_id_cstr(buf, len+1, NULL);
823 ALLOCV_END(vbuf);
824 }
825
826 return id;
827}
828
829void
831 const char *name,
832 VALUE *var,
833 rb_gvar_getter_t *getter,
834 rb_gvar_setter_t *setter)
835{
836 volatile VALUE tmp = var ? *var : Qnil;
837 ID id = global_id(name);
838 struct rb_global_variable *gvar = rb_global_entry(id)->var;
839
840 gvar->data = (void*)var;
841 gvar->getter = getter ? (rb_gvar_getter_t *)getter : rb_gvar_var_getter;
842 gvar->setter = setter ? (rb_gvar_setter_t *)setter : rb_gvar_var_setter;
843 gvar->marker = rb_gvar_var_marker;
844
845 RB_GC_GUARD(tmp);
846}
847
848void
849rb_define_variable(const char *name, VALUE *var)
850{
851 rb_define_hooked_variable(name, var, 0, 0);
852}
853
854void
855rb_define_readonly_variable(const char *name, const VALUE *var)
856{
858}
859
860void
862 const char *name,
863 rb_gvar_getter_t *getter,
864 rb_gvar_setter_t *setter)
865{
866 if (!getter) getter = rb_gvar_val_getter;
867 if (!setter) setter = rb_gvar_readonly_setter;
868 rb_define_hooked_variable(name, 0, getter, setter);
869}
870
871static void
872rb_trace_eval(VALUE cmd, VALUE val)
873{
874 rb_eval_cmd_call_kw(cmd, 1, &val, RB_NO_KEYWORDS);
875}
876
877VALUE
878rb_f_trace_var(int argc, const VALUE *argv)
879{
880 VALUE var, cmd;
881 struct rb_global_entry *entry;
882 struct trace_var *trace;
883
884 if (rb_scan_args(argc, argv, "11", &var, &cmd) == 1) {
885 cmd = rb_block_proc();
886 }
887 if (NIL_P(cmd)) {
888 return rb_f_untrace_var(argc, argv);
889 }
890 entry = rb_global_entry(rb_to_id(var));
891 trace = ALLOC(struct trace_var);
892 trace->next = entry->var->trace;
893 trace->func = rb_trace_eval;
894 trace->data = cmd;
895 trace->removed = 0;
896 entry->var->trace = trace;
897
898 return Qnil;
899}
900
901static void
902remove_trace(struct rb_global_variable *var)
903{
904 struct trace_var *trace = var->trace;
905 struct trace_var t;
906 struct trace_var *next;
907
908 t.next = trace;
909 trace = &t;
910 while (trace->next) {
911 next = trace->next;
912 if (next->removed) {
913 trace->next = next->next;
914 SIZED_FREE(next);
915 }
916 else {
917 trace = next;
918 }
919 }
920 var->trace = t.next;
921}
922
923VALUE
924rb_f_untrace_var(int argc, const VALUE *argv)
925{
926 VALUE var, cmd;
927 ID id;
928 struct rb_global_entry *entry;
929 struct trace_var *trace;
930
931 rb_scan_args(argc, argv, "11", &var, &cmd);
932 id = rb_check_id(&var);
933 if (!id) {
934 rb_name_error_str(var, "undefined global variable %"PRIsVALUE"", QUOTE(var));
935 }
936 if ((entry = rb_find_global_entry(id)) == NULL) {
937 rb_name_error(id, "undefined global variable %"PRIsVALUE"", QUOTE_ID(id));
938 }
939
940 trace = entry->var->trace;
941 if (NIL_P(cmd)) {
942 VALUE ary = rb_ary_new();
943
944 while (trace) {
945 struct trace_var *next = trace->next;
946 rb_ary_push(ary, (VALUE)trace->data);
947 trace->removed = 1;
948 trace = next;
949 }
950
951 if (!entry->var->block_trace) remove_trace(entry->var);
952 return ary;
953 }
954 else {
955 while (trace) {
956 if (trace->data == cmd) {
957 trace->removed = 1;
958 if (!entry->var->block_trace) remove_trace(entry->var);
959 return rb_ary_new3(1, cmd);
960 }
961 trace = trace->next;
962 }
963 }
964 return Qnil;
965}
966
968 struct trace_var *trace;
969 VALUE val;
970};
971
972static VALUE
973trace_ev(VALUE v)
974{
975 struct trace_data *data = (void *)v;
976 struct trace_var *trace = data->trace;
977
978 while (trace) {
979 (*trace->func)(trace->data, data->val);
980 trace = trace->next;
981 }
982
983 return Qnil;
984}
985
986static VALUE
987trace_en(VALUE v)
988{
989 struct rb_global_variable *var = (void *)v;
990 var->block_trace = 0;
991 remove_trace(var);
992 return Qnil; /* not reached */
993}
994
995static VALUE
996rb_gvar_set_entry(struct rb_global_entry *entry, VALUE val)
997{
998 struct trace_data trace;
999 struct rb_global_variable *var = entry->var;
1000
1001 (*var->setter)(val, entry->id, var->data);
1002
1003 if (var->trace && !var->block_trace) {
1004 var->block_trace = 1;
1005 trace.trace = var->trace;
1006 trace.val = val;
1007 rb_ensure(trace_ev, (VALUE)&trace, trace_en, (VALUE)var);
1008 }
1009 return val;
1010}
1011
1012static inline bool
1013gvar_use_box_tbl(const rb_box_t *box, const struct rb_global_entry *entry)
1014{
1015 return BOX_USER_P(box) &&
1016 !entry->var->box_dynamic &&
1017 (!entry->var->box_ready || entry->var->setter != rb_gvar_readonly_setter);
1018}
1019
1020VALUE
1021rb_gvar_set(ID id, VALUE val)
1022{
1023 VALUE retval;
1024 struct rb_global_entry *entry = NULL;
1025 const rb_box_t *box = rb_current_box();
1026 bool use_box_tbl = false;
1027
1028 RB_VM_LOCKING() {
1029 entry = rb_global_entry(id);
1030
1031 if (gvar_use_box_tbl(box, entry)) {
1032 use_box_tbl = true;
1033 rb_hash_aset(box->gvar_tbl, rb_id2sym(entry->id), val);
1034 retval = val;
1035 // TODO: think about trace
1036 }
1037 }
1038
1039 if (!use_box_tbl) {
1040 retval = rb_gvar_set_entry(entry, val);
1041 }
1042 return retval;
1043}
1044
1045VALUE
1046rb_gv_set(const char *name, VALUE val)
1047{
1048 return rb_gvar_set(global_id(name), val);
1049}
1050
1051VALUE
1052rb_gvar_get(ID id)
1053{
1054 VALUE retval, gvars, key;
1055 const rb_box_t *box = rb_current_box();
1056 bool use_box_tbl = false;
1057 struct rb_global_entry *entry = NULL;
1058 struct rb_global_variable *var = NULL;
1059 // TODO: use lock-free rb_id_table when it's available for use (doesn't yet exist)
1060 RB_VM_LOCKING() {
1061 entry = rb_global_entry(id);
1062 var = entry->var;
1063
1064 if (gvar_use_box_tbl(box, entry)) {
1065 use_box_tbl = true;
1066 gvars = box->gvar_tbl;
1067 key = rb_id2sym(entry->id);
1068 if (RTEST(rb_hash_has_key(gvars, key))) { // this gvar is already cached
1069 retval = rb_hash_aref(gvars, key);
1070 }
1071 else {
1072 RB_VM_UNLOCK();
1073 {
1074 retval = (*var->getter)(entry->id, var->data);
1075 if (rb_obj_respond_to(retval, rb_intern("clone"), 1)) {
1076 retval = rb_funcall(retval, rb_intern("clone"), 0);
1077 }
1078 }
1079 RB_VM_LOCK();
1080 rb_hash_aset(gvars, key, retval);
1081 }
1082 }
1083 }
1084 if (!use_box_tbl) {
1085 retval = (*var->getter)(entry->id, var->data);
1086 }
1087 return retval;
1088}
1089
1090VALUE
1091rb_gv_get(const char *name)
1092{
1093 ID id = find_global_id(name);
1094
1095 if (!id) {
1096 rb_warning("global variable '%s' not initialized", name);
1097 return Qnil;
1098 }
1099
1100 return rb_gvar_get(id);
1101}
1102
1103VALUE
1104rb_gvar_defined(ID id)
1105{
1106 struct rb_global_entry *entry = rb_global_entry(id);
1107 return RBOOL(entry->var->getter != rb_gvar_undef_getter);
1108}
1109
1111rb_gvar_getter_function_of(ID id)
1112{
1113 const struct rb_global_entry *entry = rb_global_entry(id);
1114 return entry->var->getter;
1115}
1116
1118rb_gvar_setter_function_of(ID id)
1119{
1120 const struct rb_global_entry *entry = rb_global_entry(id);
1121 return entry->var->setter;
1122}
1123
1124static enum rb_id_table_iterator_result
1125gvar_i(ID key, VALUE val, void *a)
1126{
1127 VALUE ary = (VALUE)a;
1128 rb_ary_push(ary, ID2SYM(key));
1129 return ID_TABLE_CONTINUE;
1130}
1131
1132VALUE
1134{
1135 VALUE ary = rb_ary_new();
1136 VALUE sym, backref = rb_backref_get();
1137
1138 if (!rb_ractor_main_p()) {
1139 rb_raise(rb_eRactorIsolationError, "can not access global variables from non-main Ractors");
1140 }
1141 /* gvar access (get/set) in boxes creates gvar entries globally */
1142
1143 rb_id_table_foreach(rb_global_tbl, gvar_i, (void *)ary);
1144 if (!NIL_P(backref)) {
1145 char buf[2];
1146 int i, nmatch = rb_match_count(backref);
1147 buf[0] = '$';
1148 for (i = 1; i <= nmatch; ++i) {
1149 if (!RTEST(rb_reg_nth_defined(i, backref))) continue;
1150 if (i < 10) {
1151 /* probably reused, make static ID */
1152 buf[1] = (char)(i + '0');
1153 sym = ID2SYM(rb_intern2(buf, 2));
1154 }
1155 else {
1156 /* dynamic symbol */
1157 sym = rb_str_intern(rb_sprintf("$%d", i));
1158 }
1159 rb_ary_push(ary, sym);
1160 }
1161 }
1162 return ary;
1163}
1164
1165void
1167{
1168 struct rb_global_entry *entry1 = NULL, *entry2;
1169 VALUE data1;
1170 struct rb_id_table *gtbl = rb_global_tbl;
1171
1172 if (!rb_ractor_main_p()) {
1173 rb_raise(rb_eRactorIsolationError, "can not access global variables from non-main Ractors");
1174 }
1175
1176 RB_VM_LOCKING() {
1177 entry2 = rb_global_entry(name2);
1178 if (!rb_id_table_lookup(gtbl, name1, &data1)) {
1179 entry1 = ZALLOC(struct rb_global_entry);
1180 entry1->id = name1;
1181 rb_id_table_insert(gtbl, name1, (VALUE)entry1);
1182 }
1183 else if ((entry1 = (struct rb_global_entry *)data1)->var != entry2->var) {
1184 struct rb_global_variable *var = entry1->var;
1185 if (var->block_trace) {
1186 RB_VM_UNLOCK();
1187 rb_raise(rb_eRuntimeError, "can't alias in tracer");
1188 }
1189 var->counter--;
1190 if (var->counter == 0) {
1191 free_global_variable(var);
1192 }
1193 }
1194 if (entry1->var != entry2->var) {
1195 entry2->var->counter++;
1196 entry1->var = entry2->var;
1197 }
1198 }
1199}
1200
1201static void
1202IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(ID id)
1203{
1204 if (UNLIKELY(!rb_ractor_main_p())) {
1205 if (rb_is_instance_id(id)) { // check only normal ivars
1206 rb_raise(rb_eRactorIsolationError, "can not set instance variables of classes/modules by non-main Ractors");
1207 }
1208 }
1209}
1210
1211static void
1212CVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(VALUE klass, ID id)
1213{
1214 if (UNLIKELY(!rb_ractor_main_p())) {
1215 rb_raise(rb_eRactorIsolationError, "can not set class variables from non-main Ractors (%"PRIsVALUE" from %"PRIsVALUE")", rb_id2str(id), klass);
1216 }
1217}
1218
1219static void
1220cvar_read_ractor_check(VALUE klass, ID id, VALUE val)
1221{
1222 if (UNLIKELY(!rb_ractor_main_p()) && !rb_ractor_shareable_p(val)) {
1223 rb_raise(rb_eRactorIsolationError,
1224 "can not read non-shareable class variable %"PRIsVALUE" from non-main Ractors (%"PRIsVALUE")",
1225 rb_id2str(id), klass);
1226 }
1227}
1228
1229static inline void
1230ivar_ractor_check(VALUE obj, ID id)
1231{
1232 if (LIKELY(rb_is_instance_id(id)) /* not internal ID */ &&
1233 !RB_OBJ_FROZEN_RAW(obj) &&
1234 UNLIKELY(!rb_ractor_main_p()) &&
1235 UNLIKELY(rb_ractor_shareable_p(obj))) {
1236
1237 rb_raise(rb_eRactorIsolationError, "can not access instance variables of shareable objects from non-main Ractors");
1238 }
1239}
1240
1241static inline struct st_table *
1242generic_fields_tbl_no_ractor_check(void)
1243{
1244 ASSERT_vm_locking();
1245
1246 return generic_fields_tbl_;
1247}
1248
1249struct st_table *
1250rb_generic_fields_tbl_get(void)
1251{
1252 return generic_fields_tbl_;
1253}
1254
1255void
1256rb_mark_generic_ivar(VALUE obj)
1257{
1258 VALUE data;
1259 // Bypass ASSERT_vm_locking() check because marking may happen concurrently with mmtk
1260 if (st_lookup(generic_fields_tbl_, (st_data_t)obj, (st_data_t *)&data)) {
1261 rb_gc_mark_movable(data);
1262 }
1263}
1264
1265VALUE
1266rb_obj_fields_generic_uncached(VALUE obj)
1267{
1268 VALUE fields_obj = 0;
1269 RB_VM_LOCKING() {
1270 if (!st_lookup(generic_fields_tbl_, (st_data_t)obj, (st_data_t *)&fields_obj)) {
1271 rb_bug("Object is missing entry in generic_fields_tbl");
1272 }
1273 }
1274 return fields_obj;
1275}
1276
1277VALUE
1278rb_obj_fields(VALUE obj, ID field_name)
1279{
1281 ivar_ractor_check(obj, field_name);
1282
1283 VALUE fields_obj = 0;
1284 if (rb_shape_obj_has_fields(obj)) {
1285 switch (BUILTIN_TYPE(obj)) {
1286 case T_DATA:
1287 if (LIKELY(RTYPEDDATA_P(obj))) {
1288 fields_obj = RTYPEDDATA(obj)->fields_obj;
1289 break;
1290 }
1291 goto generic_fields;
1292 case T_STRUCT:
1293 if (LIKELY(!FL_TEST_RAW(obj, RSTRUCT_GEN_FIELDS))) {
1294 fields_obj = RSTRUCT_FIELDS_OBJ(obj);
1295 break;
1296 }
1297 goto generic_fields;
1298 default:
1299 generic_fields:
1300 {
1301 rb_execution_context_t *ec = GET_EC();
1302 if (ec->gen_fields_cache.obj == obj && !UNDEF_P(ec->gen_fields_cache.fields_obj) && rb_imemo_fields_owner(ec->gen_fields_cache.fields_obj) == obj) {
1303 fields_obj = ec->gen_fields_cache.fields_obj;
1304 RUBY_ASSERT(fields_obj == rb_obj_fields_generic_uncached(obj));
1305 }
1306 else {
1307 fields_obj = rb_obj_fields_generic_uncached(obj);
1308 ec->gen_fields_cache.fields_obj = fields_obj;
1309 ec->gen_fields_cache.obj = obj;
1310 }
1311 }
1312 }
1313 }
1314 return fields_obj;
1315}
1316
1317void
1319{
1320 if (rb_obj_gen_fields_p(obj)) {
1321 st_data_t key = (st_data_t)obj, value;
1322 switch (BUILTIN_TYPE(obj)) {
1323 case T_DATA:
1324 if (LIKELY(RTYPEDDATA_P(obj))) {
1325 RB_OBJ_WRITE(obj, &RTYPEDDATA(obj)->fields_obj, 0);
1326 break;
1327 }
1328 goto generic_fields;
1329 case T_STRUCT:
1330 if (LIKELY(!FL_TEST_RAW(obj, RSTRUCT_GEN_FIELDS))) {
1331 RSTRUCT_SET_FIELDS_OBJ(obj, 0);
1332 break;
1333 }
1334 goto generic_fields;
1335 default:
1336 generic_fields:
1337 {
1338 // Other EC may have stale caches, so fields_obj should be
1339 // invalidated and the GC will replace with Qundef
1340 rb_execution_context_t *ec = GET_EC();
1341 if (ec->gen_fields_cache.obj == obj) {
1342 ec->gen_fields_cache.obj = Qundef;
1343 ec->gen_fields_cache.fields_obj = Qundef;
1344 }
1345 RB_VM_LOCKING() {
1346 if (!st_delete(generic_fields_tbl_no_ractor_check(), &key, &value)) {
1347 rb_bug("Object is missing entry in generic_fields_tbl");
1348 }
1349 }
1350 }
1351 }
1352 RBASIC_SET_SHAPE_ID(obj, ROOT_SHAPE_ID);
1353 }
1354}
1355
1356static void
1357rb_obj_set_fields(VALUE obj, VALUE fields_obj, ID field_name, VALUE original_fields_obj)
1358{
1359 ivar_ractor_check(obj, field_name);
1360
1361 if (!fields_obj) {
1362 RUBY_ASSERT(original_fields_obj);
1364 rb_imemo_fields_clear(original_fields_obj);
1365 return;
1366 }
1367
1368 RUBY_ASSERT(IMEMO_TYPE_P(fields_obj, imemo_fields));
1369 RUBY_ASSERT(!original_fields_obj || IMEMO_TYPE_P(original_fields_obj, imemo_fields));
1370
1371 if (fields_obj != original_fields_obj) {
1372 switch (BUILTIN_TYPE(obj)) {
1373 case T_DATA:
1374 if (LIKELY(RTYPEDDATA_P(obj))) {
1375 RB_OBJ_WRITE(obj, &RTYPEDDATA(obj)->fields_obj, fields_obj);
1376 break;
1377 }
1378 goto generic_fields;
1379 case T_STRUCT:
1380 if (LIKELY(!FL_TEST_RAW(obj, RSTRUCT_GEN_FIELDS))) {
1381 RSTRUCT_SET_FIELDS_OBJ(obj, fields_obj);
1382 break;
1383 }
1384 goto generic_fields;
1385 default:
1386 generic_fields:
1387 {
1388 RB_VM_LOCKING() {
1389 st_insert(generic_fields_tbl_, (st_data_t)obj, (st_data_t)fields_obj);
1390 }
1391 RB_OBJ_WRITTEN(obj, original_fields_obj, fields_obj);
1392
1393 rb_execution_context_t *ec = GET_EC();
1394 if (ec->gen_fields_cache.fields_obj != fields_obj) {
1395 ec->gen_fields_cache.obj = obj;
1396 ec->gen_fields_cache.fields_obj = fields_obj;
1397 }
1398 }
1399 }
1400
1401 if (original_fields_obj) {
1402 // Clear root shape to avoid triggering cleanup such as free_object_id.
1403 rb_imemo_fields_clear(original_fields_obj);
1404 }
1405 }
1406
1407 RBASIC_SET_SHAPE_ID(obj, RBASIC_SHAPE_ID(fields_obj));
1408}
1409
1410void
1411rb_obj_replace_fields(VALUE obj, VALUE fields_obj)
1412{
1413 RB_VM_LOCKING() {
1414 VALUE original_fields_obj = rb_obj_fields_no_ractor_check(obj);
1415 rb_obj_set_fields(obj, fields_obj, 0, original_fields_obj);
1416 }
1417}
1418
1419VALUE
1420rb_obj_field_get(VALUE obj, shape_id_t target_shape_id)
1421{
1423 RUBY_ASSERT(RSHAPE_TYPE_P(target_shape_id, SHAPE_IVAR) || RSHAPE_TYPE_P(target_shape_id, SHAPE_OBJ_ID));
1424
1425 VALUE fields_obj;
1426
1427 switch (BUILTIN_TYPE(obj)) {
1428 case T_CLASS:
1429 case T_MODULE:
1430 fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
1431 break;
1432 case T_OBJECT:
1433 fields_obj = obj;
1434 break;
1435 case T_IMEMO:
1436 RUBY_ASSERT(IMEMO_TYPE_P(obj, imemo_fields));
1437 fields_obj = obj;
1438 break;
1439 default:
1440 fields_obj = rb_obj_fields(obj, RSHAPE_EDGE_NAME(target_shape_id));
1441 break;
1442 }
1443
1444 if (UNLIKELY(rb_shape_too_complex_p(target_shape_id))) {
1445 st_table *fields_hash = rb_imemo_fields_complex_tbl(fields_obj);
1446 VALUE value = Qundef;
1447 st_lookup(fields_hash, RSHAPE_EDGE_NAME(target_shape_id), &value);
1448 RUBY_ASSERT(!UNDEF_P(value));
1449 return value;
1450 }
1451
1452 attr_index_t index = RSHAPE_INDEX(target_shape_id);
1453 return rb_imemo_fields_ptr(fields_obj)[index];
1454}
1455
1456VALUE
1457rb_ivar_lookup(VALUE obj, ID id, VALUE undef)
1458{
1459 if (SPECIAL_CONST_P(obj)) return undef;
1460
1461 VALUE fields_obj;
1462
1463 switch (BUILTIN_TYPE(obj)) {
1464 case T_CLASS:
1465 case T_MODULE:
1466 {
1467 VALUE val = rb_ivar_lookup(RCLASS_WRITABLE_FIELDS_OBJ(obj), id, undef);
1468 if (val != undef &&
1469 rb_is_instance_id(id) &&
1470 UNLIKELY(!rb_ractor_main_p()) &&
1471 !rb_ractor_shareable_p(val)) {
1472 rb_raise(rb_eRactorIsolationError,
1473 "can not get unshareable values from instance variables of classes/modules from non-main Ractors (%"PRIsVALUE" from %"PRIsVALUE")",
1474 rb_id2str(id), obj);
1475 }
1476 return val;
1477 }
1478 case T_IMEMO:
1479 // Handled like T_OBJECT
1480 RUBY_ASSERT(IMEMO_TYPE_P(obj, imemo_fields));
1481 fields_obj = obj;
1482 break;
1483 case T_OBJECT:
1484 fields_obj = obj;
1485 break;
1486 default:
1487 fields_obj = rb_obj_fields(obj, id);
1488 break;
1489 }
1490
1491 if (!fields_obj) {
1492 return undef;
1493 }
1494
1495 shape_id_t shape_id = RBASIC_SHAPE_ID(fields_obj);
1496
1497 if (UNLIKELY(rb_shape_too_complex_p(shape_id))) {
1498 st_table *iv_table = rb_imemo_fields_complex_tbl(fields_obj);
1499 VALUE val;
1500 if (rb_st_lookup(iv_table, (st_data_t)id, (st_data_t *)&val)) {
1501 return val;
1502 }
1503 return undef;
1504 }
1505
1506 attr_index_t index = 0;
1507 if (rb_shape_get_iv_index(shape_id, id, &index)) {
1508 return rb_imemo_fields_ptr(fields_obj)[index];
1509 }
1510
1511 return undef;
1512}
1513
1514VALUE
1516{
1517 VALUE iv = rb_ivar_lookup(obj, id, Qnil);
1518 RB_DEBUG_COUNTER_INC(ivar_get_base);
1519 return iv;
1520}
1521
1522VALUE
1523rb_ivar_get_at(VALUE obj, attr_index_t index, ID id)
1524{
1526 // Used by JITs, but never for T_OBJECT.
1527
1528 switch (BUILTIN_TYPE(obj)) {
1529 case T_OBJECT:
1531 case T_CLASS:
1532 case T_MODULE:
1533 {
1534 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
1535 VALUE val = rb_imemo_fields_ptr(fields_obj)[index];
1536
1537 if (UNLIKELY(!rb_ractor_main_p()) && !rb_ractor_shareable_p(val)) {
1538 rb_raise(rb_eRactorIsolationError,
1539 "can not get unshareable values from instance variables of classes/modules from non-main Ractors");
1540 }
1541
1542 return val;
1543 }
1544 default:
1545 {
1546 VALUE fields_obj = rb_obj_fields(obj, id);
1547 return rb_imemo_fields_ptr(fields_obj)[index];
1548 }
1549 }
1550}
1551
1552VALUE
1553rb_ivar_get_at_no_ractor_check(VALUE obj, attr_index_t index)
1554{
1555 // Used by JITs, but never for T_OBJECT.
1556
1557 VALUE fields_obj;
1558 switch (BUILTIN_TYPE(obj)) {
1559 case T_OBJECT:
1561 case T_CLASS:
1562 case T_MODULE:
1563 fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
1564 break;
1565 default:
1566 fields_obj = rb_obj_fields_no_ractor_check(obj);
1567 break;
1568 }
1569 return rb_imemo_fields_ptr(fields_obj)[index];
1570}
1571
1572VALUE
1573rb_attr_get(VALUE obj, ID id)
1574{
1575 return rb_ivar_lookup(obj, id, Qnil);
1576}
1577
1578void rb_obj_copy_fields_to_hash_table(VALUE obj, st_table *table);
1579static VALUE imemo_fields_complex_from_obj(VALUE owner, VALUE source_fields_obj, shape_id_t shape_id);
1580
1581static shape_id_t
1582obj_transition_too_complex(VALUE obj, st_table *table)
1583{
1584 RUBY_ASSERT(!rb_shape_obj_too_complex_p(obj));
1585 shape_id_t shape_id = rb_obj_shape_transition_complex(obj);
1586
1587 switch (BUILTIN_TYPE(obj)) {
1588 case T_OBJECT:
1589 {
1590 VALUE *old_fields = NULL;
1591 uint32_t old_fields_len = 0;
1592 if (FL_TEST_RAW(obj, ROBJECT_HEAP)) {
1593 old_fields = ROBJECT_FIELDS(obj);
1594 old_fields_len = ROBJECT_FIELDS_CAPACITY(obj);
1595 }
1596 else {
1597 FL_SET_RAW(obj, ROBJECT_HEAP);
1598 }
1599 RBASIC_SET_SHAPE_ID(obj, shape_id);
1600 ROBJECT_SET_FIELDS_HASH(obj, table);
1601 if (old_fields) {
1602 SIZED_FREE_N(old_fields, old_fields_len);
1603 }
1604 }
1605 break;
1606 case T_CLASS:
1607 case T_MODULE:
1608 case T_IMEMO:
1610 break;
1611 default:
1612 {
1613 VALUE fields_obj = rb_imemo_fields_new_complex_tbl(obj, shape_id, table, RB_OBJ_SHAREABLE_P(obj));
1614 rb_obj_replace_fields(obj, fields_obj);
1615 }
1616 }
1617
1618 return shape_id;
1619}
1620
1621// Copy all object fields, including ivars and internal object_id, etc
1622static shape_id_t
1623rb_evict_fields_to_hash(VALUE obj)
1624{
1625 RUBY_ASSERT(!rb_shape_obj_too_complex_p(obj));
1626
1627 st_table *table = st_init_numtable_with_size(RSHAPE_LEN(RBASIC_SHAPE_ID(obj)));
1628 rb_obj_copy_fields_to_hash_table(obj, table);
1629 shape_id_t new_shape_id = obj_transition_too_complex(obj, table);
1630
1631 RUBY_ASSERT(rb_shape_obj_too_complex_p(obj));
1632 return new_shape_id;
1633}
1634
1635void
1636rb_evict_ivars_to_hash(VALUE obj)
1637{
1638 RUBY_ASSERT(!rb_shape_obj_too_complex_p(obj));
1639
1640 st_table *table = st_init_numtable_with_size(rb_ivar_count(obj));
1641
1642 // Evacuate all previous values from shape into id_table
1643 rb_obj_copy_ivs_to_hash_table(obj, table);
1644 obj_transition_too_complex(obj, table);
1645
1646 RUBY_ASSERT(rb_shape_obj_too_complex_p(obj));
1647}
1648
1649static VALUE
1650rb_ivar_delete(VALUE obj, ID id, VALUE undef)
1651{
1652 rb_check_frozen(obj);
1653
1654 VALUE val = undef;
1655 VALUE fields_obj;
1656 bool concurrent = false;
1657 int type = BUILTIN_TYPE(obj);
1658
1659 switch(type) {
1660 case T_CLASS:
1661 case T_MODULE:
1662 IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(id);
1663
1664 fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
1665 if (rb_multi_ractor_p()) {
1666 concurrent = true;
1667 }
1668 break;
1669 case T_OBJECT:
1670 fields_obj = obj;
1671 break;
1672 default: {
1673 fields_obj = rb_obj_fields(obj, id);
1674 break;
1675 }
1676 }
1677
1678 if (!fields_obj) {
1679 return undef;
1680 }
1681
1682 const VALUE original_fields_obj = fields_obj;
1683 if (concurrent) {
1684 fields_obj = rb_imemo_fields_clone(fields_obj);
1685 }
1686
1687 shape_id_t old_shape_id = RBASIC_SHAPE_ID(fields_obj);
1688 shape_id_t removed_shape_id;
1689 shape_id_t next_shape_id = rb_obj_shape_transition_remove_ivar(fields_obj, id, &removed_shape_id);
1690
1691 if (UNLIKELY(rb_shape_too_complex_p(next_shape_id))) {
1692 if (UNLIKELY(!rb_shape_too_complex_p(old_shape_id))) {
1693 if (type == T_OBJECT) {
1694 rb_evict_fields_to_hash(obj);
1695 }
1696 else {
1697 fields_obj = imemo_fields_complex_from_obj(obj, fields_obj, next_shape_id);
1698 }
1699 }
1700 st_data_t key = id;
1701 if (!st_delete(rb_imemo_fields_complex_tbl(fields_obj), &key, (st_data_t *)&val)) {
1702 val = undef;
1703 }
1704 }
1705 else {
1706 if (next_shape_id == old_shape_id) {
1707 return undef;
1708 }
1709
1710 RUBY_ASSERT(removed_shape_id != INVALID_SHAPE_ID);
1711 RUBY_ASSERT(RSHAPE_LEN(next_shape_id) == RSHAPE_LEN(old_shape_id) - 1);
1712
1713 VALUE *fields = rb_imemo_fields_ptr(fields_obj);
1714 attr_index_t removed_index = RSHAPE_INDEX(removed_shape_id);
1715 val = fields[removed_index];
1716
1717 attr_index_t new_fields_count = RSHAPE_LEN(next_shape_id);
1718 if (new_fields_count) {
1719 size_t trailing_fields = new_fields_count - removed_index;
1720
1721 MEMMOVE(&fields[removed_index], &fields[removed_index + 1], VALUE, trailing_fields);
1722 RBASIC_SET_SHAPE_ID(fields_obj, next_shape_id);
1723
1724 if (FL_TEST_RAW(fields_obj, OBJ_FIELD_HEAP)) {
1725 if (rb_obj_embedded_size(new_fields_count) <= rb_gc_obj_slot_size(fields_obj)) {
1726 // Re-embed objects when instances become small enough
1727 // This is necessary because YJIT assumes that objects with the same shape
1728 // have the same embeddedness for efficiency (avoid extra checks)
1729 FL_UNSET_RAW(fields_obj, ROBJECT_HEAP);
1730 MEMCPY(rb_imemo_fields_ptr(fields_obj), fields, VALUE, new_fields_count);
1731 SIZED_FREE_N(fields, RSHAPE_CAPACITY(old_shape_id));
1732 }
1733 else if (RSHAPE_CAPACITY(old_shape_id) != RSHAPE_CAPACITY(next_shape_id)) {
1734 IMEMO_OBJ_FIELDS(fields_obj)->as.external.ptr = ruby_xrealloc_sized(fields, RSHAPE_CAPACITY(next_shape_id) * sizeof(VALUE), RSHAPE_CAPACITY(old_shape_id) * sizeof(VALUE));
1735 }
1736 }
1737 }
1738 else {
1739 fields_obj = 0;
1741 }
1742 }
1743
1744 RBASIC_SET_SHAPE_ID(obj, next_shape_id);
1745 if (fields_obj != original_fields_obj) {
1746 switch (type) {
1747 case T_OBJECT:
1748 break;
1749 case T_CLASS:
1750 case T_MODULE:
1751 RCLASS_WRITABLE_SET_FIELDS_OBJ(obj, fields_obj);
1752 break;
1753 default:
1754 rb_obj_set_fields(obj, fields_obj, id, original_fields_obj);
1755 break;
1756 }
1757 }
1758
1759 return val;
1760}
1761
1762VALUE
1763rb_attr_delete(VALUE obj, ID id)
1764{
1765 return rb_ivar_delete(obj, id, Qnil);
1766}
1767
1768void
1769rb_obj_init_too_complex(VALUE obj, st_table *table)
1770{
1771 // This method is meant to be called on newly allocated object.
1772 RUBY_ASSERT(!rb_shape_obj_too_complex_p(obj));
1773 RUBY_ASSERT(rb_shape_canonical_p(RBASIC_SHAPE_ID(obj)));
1774 RUBY_ASSERT(RSHAPE_LEN(RBASIC_SHAPE_ID(obj)) == 0);
1775
1776 obj_transition_too_complex(obj, table);
1777}
1778
1779static int
1780imemo_fields_complex_from_obj_i(ID key, VALUE val, st_data_t arg)
1781{
1782 VALUE fields = (VALUE)arg;
1783 st_table *table = rb_imemo_fields_complex_tbl(fields);
1784
1785 RUBY_ASSERT(!st_lookup(table, (st_data_t)key, NULL));
1786 st_add_direct(table, (st_data_t)key, (st_data_t)val);
1787 RB_OBJ_WRITTEN(fields, Qundef, val);
1788
1789 return ST_CONTINUE;
1790}
1791
1792static VALUE
1793imemo_fields_complex_from_obj(VALUE owner, VALUE source_fields_obj, shape_id_t shape_id)
1794{
1795 attr_index_t len = source_fields_obj ? RSHAPE_LEN(RBASIC_SHAPE_ID(source_fields_obj)) : 0;
1796 VALUE fields_obj = rb_imemo_fields_new_complex(owner, shape_id, len + 1, RB_OBJ_SHAREABLE_P(owner));
1797
1798 rb_field_foreach(source_fields_obj, imemo_fields_complex_from_obj_i, (st_data_t)fields_obj, false);
1799
1800 return fields_obj;
1801}
1802
1803static VALUE
1804imemo_fields_copy_append(VALUE owner, VALUE source_fields_obj, shape_id_t current_shape_id, shape_id_t target_shape_id, VALUE val)
1805{
1806 attr_index_t fields_count = RSHAPE_LEN(current_shape_id);
1807
1808 VALUE fields_obj = rb_imemo_fields_new(owner, target_shape_id, RB_OBJ_SHAREABLE_P(owner));
1809
1810 VALUE *fields = rb_imemo_fields_ptr(fields_obj);
1811
1812 if (source_fields_obj) {
1813 MEMCPY(fields, rb_imemo_fields_ptr(source_fields_obj), VALUE, fields_count);
1814 for (attr_index_t i = 0; i < fields_count; i++) {
1815 RB_OBJ_WRITTEN(fields_obj, Qundef, fields[i]);
1816 }
1817 }
1818
1819 RB_OBJ_WRITE(fields_obj, &fields[fields_count], val);
1820
1821 return fields_obj;
1822}
1823
1824static VALUE
1825imemo_fields_set(VALUE owner, VALUE fields_obj, shape_id_t target_shape_id, ID field_name, VALUE val, bool concurrent)
1826{
1827 const VALUE original_fields_obj = fields_obj;
1828 shape_id_t current_shape_id = fields_obj ? RBASIC_SHAPE_ID(fields_obj) : ROOT_SHAPE_ID;
1829
1830 if (UNLIKELY(rb_shape_too_complex_p(target_shape_id))) {
1831 if (rb_shape_too_complex_p(current_shape_id)) {
1832 if (concurrent) {
1833 // In multi-ractor case, we must always work on a copy because
1834 // even if the field already exist, inserting in a st_table may
1835 // cause a rebuild.
1836 fields_obj = rb_imemo_fields_clone(fields_obj);
1837 }
1838 }
1839 else {
1840 fields_obj = imemo_fields_complex_from_obj(owner, original_fields_obj, target_shape_id);
1841 current_shape_id = target_shape_id;
1842 }
1843
1844 st_table *table = rb_imemo_fields_complex_tbl(fields_obj);
1845
1846 RUBY_ASSERT(field_name);
1847 st_insert(table, (st_data_t)field_name, (st_data_t)val);
1848 RB_OBJ_WRITTEN(fields_obj, Qundef, val);
1849 RBASIC_SET_SHAPE_ID(fields_obj, target_shape_id);
1850 }
1851 else {
1852 attr_index_t index = RSHAPE_INDEX(target_shape_id);
1853 if (concurrent || index >= RSHAPE_CAPACITY(current_shape_id)) {
1854 return imemo_fields_copy_append(owner, original_fields_obj, current_shape_id, target_shape_id, val);
1855 }
1856
1857 VALUE *table = rb_imemo_fields_ptr(fields_obj);
1858 RB_OBJ_WRITE(fields_obj, &table[index], val);
1859
1860 if (index >= RSHAPE_LEN(current_shape_id)) {
1861 RBASIC_SET_SHAPE_ID(fields_obj, target_shape_id);
1862 }
1863 }
1864
1865 return fields_obj;
1866}
1867
1868static attr_index_t
1869generic_field_set(VALUE obj, shape_id_t target_shape_id, ID field_name, VALUE val)
1870{
1871 if (!field_name) {
1872 field_name = RSHAPE_EDGE_NAME(target_shape_id);
1873 RUBY_ASSERT(field_name);
1874 }
1875
1876 const VALUE original_fields_obj = rb_obj_fields(obj, field_name);
1877 VALUE fields_obj = imemo_fields_set(obj, original_fields_obj, target_shape_id, field_name, val, false);
1878
1879 rb_obj_set_fields(obj, fields_obj, field_name, original_fields_obj);
1880 return rb_shape_too_complex_p(target_shape_id) ? ATTR_INDEX_NOT_SET : RSHAPE_INDEX(target_shape_id);
1881}
1882
1883static shape_id_t
1884generic_shape_ivar(VALUE obj, ID id, bool *new_ivar_out)
1885{
1886 bool new_ivar = false;
1887 shape_id_t current_shape_id = RBASIC_SHAPE_ID(obj);
1888 shape_id_t target_shape_id = current_shape_id;
1889
1890 if (!rb_shape_too_complex_p(current_shape_id)) {
1891 if (!rb_shape_find_ivar(current_shape_id, id, &target_shape_id)) {
1892 if (RSHAPE_LEN(current_shape_id) >= SHAPE_MAX_FIELDS) {
1893 rb_raise(rb_eArgError, "too many instance variables");
1894 }
1895
1896 new_ivar = true;
1897 target_shape_id = rb_obj_shape_transition_add_ivar(obj, id);
1898 }
1899 }
1900
1901 *new_ivar_out = new_ivar;
1902 return target_shape_id;
1903}
1904
1905static attr_index_t
1906generic_ivar_set(VALUE obj, ID id, VALUE val)
1907{
1908 bool dontcare;
1909 shape_id_t target_shape_id = generic_shape_ivar(obj, id, &dontcare);
1910 return generic_field_set(obj, target_shape_id, id, val);
1911}
1912
1913void
1914rb_ensure_iv_list_size(VALUE obj, uint32_t current_len, uint32_t new_capacity)
1915{
1916 RUBY_ASSERT(!rb_shape_obj_too_complex_p(obj));
1917
1918 if (FL_TEST_RAW(obj, ROBJECT_HEAP)) {
1919 SIZED_REALLOC_N(ROBJECT(obj)->as.heap.fields, VALUE, new_capacity, current_len);
1920 }
1921 else {
1922 VALUE *ptr = ROBJECT_FIELDS(obj);
1923 VALUE *newptr = ALLOC_N(VALUE, new_capacity);
1924 MEMCPY(newptr, ptr, VALUE, current_len);
1925 FL_SET_RAW(obj, ROBJECT_HEAP);
1926 ROBJECT(obj)->as.heap.fields = newptr;
1927 }
1928}
1929
1930static int
1931rb_obj_copy_ivs_to_hash_table_i(ID key, VALUE val, st_data_t arg)
1932{
1933 RUBY_ASSERT(!st_lookup((st_table *)arg, (st_data_t)key, NULL));
1934
1935 st_add_direct((st_table *)arg, (st_data_t)key, (st_data_t)val);
1936 return ST_CONTINUE;
1937}
1938
1939void
1940rb_obj_copy_ivs_to_hash_table(VALUE obj, st_table *table)
1941{
1942 rb_ivar_foreach(obj, rb_obj_copy_ivs_to_hash_table_i, (st_data_t)table);
1943}
1944
1945void
1946rb_obj_copy_fields_to_hash_table(VALUE obj, st_table *table)
1947{
1948 rb_field_foreach(obj, rb_obj_copy_ivs_to_hash_table_i, (st_data_t)table, false);
1949}
1950
1951static attr_index_t
1952obj_field_set(VALUE obj, shape_id_t target_shape_id, ID field_name, VALUE val)
1953{
1954 shape_id_t current_shape_id = RBASIC_SHAPE_ID(obj);
1955
1956 if (UNLIKELY(rb_shape_too_complex_p(target_shape_id))) {
1957 if (UNLIKELY(!rb_shape_too_complex_p(current_shape_id))) {
1958 current_shape_id = rb_evict_fields_to_hash(obj);
1959 }
1960
1961 if (RSHAPE_LEN(target_shape_id) > RSHAPE_LEN(current_shape_id)) {
1962 RBASIC_SET_SHAPE_ID(obj, target_shape_id);
1963 }
1964
1965 if (!field_name) {
1966 field_name = RSHAPE_EDGE_NAME(target_shape_id);
1967 RUBY_ASSERT(field_name);
1968 }
1969
1970 st_insert(ROBJECT_FIELDS_HASH(obj), (st_data_t)field_name, (st_data_t)val);
1971 RB_OBJ_WRITTEN(obj, Qundef, val);
1972
1973 return ATTR_INDEX_NOT_SET;
1974 }
1975 else {
1976 attr_index_t index = RSHAPE_INDEX(target_shape_id);
1977
1978 if (index >= RSHAPE_LEN(current_shape_id)) {
1979 if (UNLIKELY(index >= RSHAPE_CAPACITY(current_shape_id))) {
1980 rb_ensure_iv_list_size(obj, RSHAPE_CAPACITY(current_shape_id), RSHAPE_CAPACITY(target_shape_id));
1981 }
1982 RBASIC_SET_SHAPE_ID(obj, target_shape_id);
1983 }
1984
1985 RB_OBJ_WRITE(obj, &ROBJECT_FIELDS(obj)[index], val);
1986
1987 return index;
1988 }
1989}
1990
1991static attr_index_t
1992obj_ivar_set(VALUE obj, ID id, VALUE val)
1993{
1994 bool dontcare;
1995 shape_id_t target_shape_id = generic_shape_ivar(obj, id, &dontcare);
1996 return obj_field_set(obj, target_shape_id, id, val);
1997}
1998
1999/* Set the instance variable +val+ on object +obj+ at ivar name +id+.
2000 * This function only works with T_OBJECT objects, so make sure
2001 * +obj+ is of type T_OBJECT before using this function.
2002 */
2003VALUE
2004rb_vm_set_ivar_id(VALUE obj, ID id, VALUE val)
2005{
2006 rb_check_frozen(obj);
2007 obj_ivar_set(obj, id, val);
2008 return val;
2009}
2010
2011void
2013{
2014 if (RB_FL_ABLE(x)) {
2016 if (TYPE(x) == T_STRING) {
2017 RB_FL_UNSET_RAW(x, FL_USER2 | FL_USER3); // STR_CHILLED
2018 }
2019
2020 shape_id_t shape_id = rb_obj_shape_transition_frozen(x);
2021 switch (BUILTIN_TYPE(x)) {
2022 case T_CLASS:
2023 case T_MODULE:
2024 RBASIC_SET_SHAPE_ID(RCLASS_WRITABLE_ENSURE_FIELDS_OBJ(x), shape_id);
2025 // FIXME: How to do multi-shape?
2026 RBASIC_SET_SHAPE_ID(x, shape_id);
2027 break;
2028 default:
2029 RBASIC_SET_SHAPE_ID(x, shape_id);
2030 break;
2031 }
2032
2033 if (RBASIC_CLASS(x) && RCLASS_SINGLETON_P(RBASIC_CLASS(x))) {
2035 }
2036 }
2037}
2038
2039static attr_index_t class_ivar_set(VALUE obj, ID id, VALUE val, bool *new_ivar);
2040
2041static attr_index_t
2042ivar_set(VALUE obj, ID id, VALUE val)
2043{
2044 RB_DEBUG_COUNTER_INC(ivar_set_base);
2045
2046 switch (BUILTIN_TYPE(obj)) {
2047 case T_OBJECT:
2048 return obj_ivar_set(obj, id, val);
2049 case T_CLASS:
2050 case T_MODULE:
2051 {
2052 IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(id);
2053 bool dontcare;
2054 return class_ivar_set(obj, id, val, &dontcare);
2055 }
2056 default:
2057 return generic_ivar_set(obj, id, val);
2058 }
2059}
2060
2061VALUE
2063{
2064 rb_check_frozen(obj);
2065 ivar_set(obj, id, val);
2066 return val;
2067}
2068
2069attr_index_t
2070rb_ivar_set_index(VALUE obj, ID id, VALUE val)
2071{
2072 return ivar_set(obj, id, val);
2073}
2074
2075void
2076rb_ivar_set_internal(VALUE obj, ID id, VALUE val)
2077{
2078 // should be internal instance variable name (no @ prefix)
2079 VM_ASSERT(!rb_is_instance_id(id));
2080
2081 ivar_set(obj, id, val);
2082}
2083
2084attr_index_t
2085rb_obj_field_set(VALUE obj, shape_id_t target_shape_id, ID field_name, VALUE val)
2086{
2087 switch (BUILTIN_TYPE(obj)) {
2088 case T_OBJECT:
2089 return obj_field_set(obj, target_shape_id, field_name, val);
2090 case T_CLASS:
2091 case T_MODULE:
2092 // The only field is object_id and T_CLASS handle it differently.
2093 rb_bug("Unreachable");
2094 break;
2095 default:
2096 return generic_field_set(obj, target_shape_id, field_name, val);
2097 }
2098}
2099
2100static VALUE
2101ivar_defined0(VALUE obj, ID id)
2102{
2103 attr_index_t index;
2104
2105 if (rb_shape_obj_too_complex_p(obj)) {
2106 VALUE idx;
2107 st_table *table = NULL;
2108 switch (BUILTIN_TYPE(obj)) {
2109 case T_CLASS:
2110 case T_MODULE:
2111 rb_bug("Unreachable");
2112 break;
2113
2114 case T_IMEMO:
2115 RUBY_ASSERT(IMEMO_TYPE_P(obj, imemo_fields));
2116 table = rb_imemo_fields_complex_tbl(obj);
2117 break;
2118
2119 case T_OBJECT:
2120 table = ROBJECT_FIELDS_HASH(obj);
2121 break;
2122
2123 default: {
2124 VALUE fields_obj = rb_obj_fields_no_ractor_check(obj); // defined? doesn't require ractor checks
2125 table = rb_imemo_fields_complex_tbl(fields_obj);
2126 }
2127 }
2128
2129 if (!table || !rb_st_lookup(table, id, &idx)) {
2130 return Qfalse;
2131 }
2132
2133 return Qtrue;
2134 }
2135 else {
2136 return RBOOL(rb_shape_get_iv_index(RBASIC_SHAPE_ID(obj), id, &index));
2137 }
2138}
2139
2140VALUE
2142{
2143 if (SPECIAL_CONST_P(obj)) return Qfalse;
2144
2145 VALUE defined = Qfalse;
2146 switch (BUILTIN_TYPE(obj)) {
2147 case T_CLASS:
2148 case T_MODULE:
2149 {
2150 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
2151 if (fields_obj) {
2152 defined = ivar_defined0(fields_obj, id);
2153 }
2154 }
2155 break;
2156 default:
2157 defined = ivar_defined0(obj, id);
2158 break;
2159 }
2160 return defined;
2161}
2162
2164 VALUE obj;
2165 struct gen_fields_tbl *fields_tbl;
2166 st_data_t arg;
2167 rb_ivar_foreach_callback_func *func;
2168 VALUE *fields;
2169 shape_id_t shape_id;
2170 bool ivar_only;
2171};
2172
2173static int
2174iterate_over_shapes_callback(shape_id_t shape_id, void *data)
2175{
2176 struct iv_itr_data *itr_data = data;
2177
2178 if (itr_data->ivar_only && !RSHAPE_TYPE_P(shape_id, SHAPE_IVAR)) {
2179 return ST_CONTINUE;
2180 }
2181
2182 VALUE *fields;
2183 switch (BUILTIN_TYPE(itr_data->obj)) {
2184 case T_OBJECT:
2185 RUBY_ASSERT(!rb_shape_obj_too_complex_p(itr_data->obj));
2186 fields = ROBJECT_FIELDS(itr_data->obj);
2187 break;
2188 case T_IMEMO:
2189 RUBY_ASSERT(IMEMO_TYPE_P(itr_data->obj, imemo_fields));
2190 RUBY_ASSERT(!rb_shape_obj_too_complex_p(itr_data->obj));
2191
2192 fields = rb_imemo_fields_ptr(itr_data->obj);
2193 break;
2194 default:
2195 rb_bug("Unreachable");
2196 }
2197
2198 RUBY_ASSERT(itr_data->shape_id == RBASIC_SHAPE_ID(itr_data->obj));
2199
2200 VALUE val = fields[RSHAPE_INDEX(shape_id)];
2201 int ret = itr_data->func(RSHAPE_EDGE_NAME(shape_id), val, itr_data->arg);
2202
2203 RUBY_ASSERT(itr_data->shape_id == RBASIC_SHAPE_ID(itr_data->obj));
2204
2205 return ret;
2206}
2207
2208/*
2209 * Returns a flag to stop iterating depending on the result of +callback+.
2210 */
2211static void
2212iterate_over_shapes(shape_id_t shape_id, rb_ivar_foreach_callback_func *callback, struct iv_itr_data *itr_data)
2213{
2214 rb_shape_foreach_field(shape_id, iterate_over_shapes_callback, itr_data);
2215}
2216
2217static int
2218each_hash_iv(st_data_t id, st_data_t val, st_data_t data)
2219{
2220 struct iv_itr_data * itr_data = (struct iv_itr_data *)data;
2221 rb_ivar_foreach_callback_func *callback = itr_data->func;
2222 if (is_internal_id((ID)id)) {
2223 return ST_CONTINUE;
2224 }
2225 return callback((ID)id, (VALUE)val, itr_data->arg);
2226}
2227
2228static void
2229obj_fields_each(VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg, bool ivar_only)
2230{
2231 struct iv_itr_data itr_data = {
2232 .obj = obj,
2233 .arg = arg,
2234 .func = func,
2235 .ivar_only = ivar_only,
2236 };
2237
2238 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
2239 if (rb_shape_too_complex_p(shape_id)) {
2240 st_foreach_safe(ROBJECT_FIELDS_HASH(obj), each_hash_iv, (st_data_t)&itr_data);
2241 }
2242 else {
2243 itr_data.fields = ROBJECT_FIELDS(obj);
2244 itr_data.shape_id = shape_id;
2245 iterate_over_shapes(shape_id, func, &itr_data);
2246 }
2247}
2248
2249static void
2250imemo_fields_each(VALUE fields_obj, rb_ivar_foreach_callback_func *func, st_data_t arg, bool ivar_only)
2251{
2252 IMEMO_TYPE_P(fields_obj, imemo_fields);
2253
2254 struct iv_itr_data itr_data = {
2255 .obj = fields_obj,
2256 .arg = arg,
2257 .func = func,
2258 .ivar_only = ivar_only,
2259 };
2260
2261 shape_id_t shape_id = RBASIC_SHAPE_ID(fields_obj);
2262 if (rb_shape_too_complex_p(shape_id)) {
2263 rb_st_foreach(rb_imemo_fields_complex_tbl(fields_obj), each_hash_iv, (st_data_t)&itr_data);
2264 }
2265 else {
2266 itr_data.fields = rb_imemo_fields_ptr(fields_obj);
2267 itr_data.shape_id = shape_id;
2268 iterate_over_shapes(shape_id, func, &itr_data);
2269 }
2270}
2271
2272void
2274{
2275 VALUE new_fields_obj;
2276
2277 rb_check_frozen(dest);
2278
2279 if (!rb_obj_gen_fields_p(obj)) {
2280 return;
2281 }
2282
2283 shape_id_t src_shape_id = rb_obj_shape_id(obj);
2284
2285 VALUE fields_obj = rb_obj_fields_no_ractor_check(obj);
2286 if (fields_obj) {
2287 unsigned long src_num_ivs = rb_ivar_count(fields_obj);
2288 if (!src_num_ivs) {
2289 goto clear;
2290 }
2291
2292 if (rb_shape_too_complex_p(src_shape_id)) {
2293 rb_shape_copy_complex_ivars(dest, obj, src_shape_id, rb_imemo_fields_complex_tbl(fields_obj));
2294 return;
2295 }
2296
2297 shape_id_t dest_shape_id = src_shape_id;
2298 shape_id_t initial_shape_id = rb_obj_shape_id(dest);
2299
2300 if (!rb_shape_canonical_p(src_shape_id)) {
2301 RUBY_ASSERT(RSHAPE_TYPE_P(initial_shape_id, SHAPE_ROOT));
2302
2303 dest_shape_id = rb_shape_rebuild(initial_shape_id, src_shape_id);
2304 if (UNLIKELY(rb_shape_too_complex_p(dest_shape_id))) {
2305 st_table *table = rb_st_init_numtable_with_size(src_num_ivs);
2306 rb_obj_copy_ivs_to_hash_table(obj, table);
2307 rb_obj_init_too_complex(dest, table);
2308 return;
2309 }
2310 }
2311
2312 if (!RSHAPE_LEN(dest_shape_id)) {
2313 RBASIC_SET_SHAPE_ID(dest, dest_shape_id);
2314 return;
2315 }
2316
2317 new_fields_obj = rb_imemo_fields_new(dest, dest_shape_id, RB_OBJ_SHAREABLE_P(dest));
2318 VALUE *src_buf = rb_imemo_fields_ptr(fields_obj);
2319 VALUE *dest_buf = rb_imemo_fields_ptr(new_fields_obj);
2320 rb_shape_copy_fields(new_fields_obj, dest_buf, dest_shape_id, src_buf, src_shape_id);
2321
2322 rb_obj_replace_fields(dest, new_fields_obj);
2323 }
2324 return;
2325
2326 clear:
2328}
2329
2330void
2331rb_replace_generic_ivar(VALUE clone, VALUE obj)
2332{
2333 RB_VM_LOCKING() {
2334 st_data_t fields_tbl, obj_data = (st_data_t)obj;
2335 if (st_delete(generic_fields_tbl_, &obj_data, &fields_tbl)) {
2336 st_insert(generic_fields_tbl_, (st_data_t)clone, fields_tbl);
2337 RB_OBJ_WRITTEN(clone, Qundef, fields_tbl);
2338 }
2339 else {
2340 rb_bug("unreachable");
2341 }
2342 }
2343}
2344
2345void
2346rb_field_foreach(VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg, bool ivar_only)
2347{
2348 if (SPECIAL_CONST_P(obj)) return;
2349 switch (BUILTIN_TYPE(obj)) {
2350 case T_IMEMO:
2351 if (IMEMO_TYPE_P(obj, imemo_fields)) {
2352 imemo_fields_each(obj, func, arg, ivar_only);
2353 }
2354 break;
2355 case T_OBJECT:
2356 obj_fields_each(obj, func, arg, ivar_only);
2357 break;
2358 case T_CLASS:
2359 case T_MODULE:
2360 {
2361 IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(0);
2362 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
2363 if (fields_obj) {
2364 imemo_fields_each(fields_obj, func, arg, ivar_only);
2365 }
2366 }
2367 break;
2368 default:
2369 {
2370 VALUE fields_obj = rb_obj_fields_no_ractor_check(obj);
2371 if (fields_obj) {
2372 imemo_fields_each(fields_obj, func, arg, ivar_only);
2373 }
2374 }
2375 break;
2376 }
2377}
2378
2380 ID name;
2381 VALUE val;
2382};
2383
2384static int
2385collect_ivar_i(ID id, VALUE val, st_data_t arg)
2386{
2387 struct ivar_buf_entry **pos = (struct ivar_buf_entry **)arg;
2388 (*pos)->name = id;
2389 (*pos)->val = val;
2390 (*pos)++;
2391 return ST_CONTINUE;
2392}
2393
2394void
2395rb_ivar_foreach(VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg)
2396{
2397 rb_field_foreach(obj, func, arg, true);
2398}
2399
2400void
2401rb_ivar_foreach_buffered(VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg)
2402{
2403 st_index_t count = rb_ivar_count(obj);
2404 if (count == 0) return;
2405
2406 VALUE tmpbuf;
2407 struct ivar_buf_entry *buf = ALLOCV_N(struct ivar_buf_entry, tmpbuf, count);
2408 struct ivar_buf_entry *pos = buf;
2409
2410 rb_field_foreach(obj, collect_ivar_i, (st_data_t)&pos, true);
2411 RUBY_ASSERT((st_index_t)(pos - buf) == count);
2412
2413 for (st_index_t i = 0; i < count; i++) {
2414 if (func(buf[i].name, buf[i].val, arg) == ST_STOP) break;
2415 }
2416
2417 ALLOCV_END(tmpbuf);
2418}
2419
2420st_index_t
2422{
2423 if (SPECIAL_CONST_P(obj)) return 0;
2424
2425 st_index_t iv_count = 0;
2426 switch (BUILTIN_TYPE(obj)) {
2427 case T_OBJECT:
2428 iv_count = ROBJECT_FIELDS_COUNT(obj);
2429 break;
2430
2431 case T_CLASS:
2432 case T_MODULE:
2433 {
2434 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
2435 if (!fields_obj) {
2436 return 0;
2437 }
2438 if (rb_shape_obj_too_complex_p(fields_obj)) {
2439 iv_count = rb_st_table_size(rb_imemo_fields_complex_tbl(fields_obj));
2440 }
2441 else {
2442 iv_count = RBASIC_FIELDS_COUNT(fields_obj);
2443 }
2444 }
2445 break;
2446
2447 case T_IMEMO:
2448 RUBY_ASSERT(IMEMO_TYPE_P(obj, imemo_fields));
2449
2450 if (rb_shape_obj_too_complex_p(obj)) {
2451 iv_count = rb_st_table_size(rb_imemo_fields_complex_tbl(obj));
2452 }
2453 else {
2454 iv_count = RBASIC_FIELDS_COUNT(obj);
2455 }
2456 break;
2457
2458 default:
2459 {
2460 VALUE fields_obj = rb_obj_fields_no_ractor_check(obj);
2461 if (fields_obj) {
2462 if (rb_shape_obj_too_complex_p(fields_obj)) {
2463 iv_count = rb_st_table_size(rb_imemo_fields_complex_tbl(fields_obj));
2464 }
2465 else {
2466 iv_count = RBASIC_FIELDS_COUNT(obj);
2467 }
2468 }
2469 }
2470 break;
2471 }
2472
2473 if (rb_shape_obj_has_id(obj)) {
2474 iv_count--;
2475 }
2476
2477 return iv_count;
2478}
2479
2480static int
2481ivar_i(ID key, VALUE v, st_data_t a)
2482{
2483 VALUE ary = (VALUE)a;
2484
2485 if (rb_is_instance_id(key)) {
2486 rb_ary_push(ary, ID2SYM(key));
2487 }
2488 return ST_CONTINUE;
2489}
2490
2491/*
2492 * call-seq:
2493 * obj.instance_variables -> array
2494 *
2495 * Returns an array of instance variable names for the receiver. Note
2496 * that simply defining an accessor does not create the corresponding
2497 * instance variable.
2498 *
2499 * class Fred
2500 * attr_accessor :a1
2501 * def initialize
2502 * @iv = 3
2503 * end
2504 * end
2505 * Fred.new.instance_variables #=> [:@iv]
2506 */
2507
2508VALUE
2510{
2512 rb_ivar_foreach(obj, ivar_i, ary);
2513 return ary;
2514}
2515
2516#define rb_is_constant_id rb_is_const_id
2517#define rb_is_constant_name rb_is_const_name
2518#define id_for_var(obj, name, part, type) \
2519 id_for_var_message(obj, name, type, "'%1$s' is not allowed as "#part" "#type" variable name")
2520#define id_for_var_message(obj, name, type, message) \
2521 check_id_type(obj, &(name), rb_is_##type##_id, rb_is_##type##_name, message, strlen(message))
2522static ID
2523check_id_type(VALUE obj, VALUE *pname,
2524 int (*valid_id_p)(ID), int (*valid_name_p)(VALUE),
2525 const char *message, size_t message_len)
2526{
2527 ID id = rb_check_id(pname);
2528 VALUE name = *pname;
2529
2530 if (id ? !valid_id_p(id) : !valid_name_p(name)) {
2531 rb_name_err_raise_str(rb_fstring_new(message, message_len),
2532 obj, name);
2533 }
2534 return id;
2535}
2536
2537/*
2538 * call-seq:
2539 * obj.remove_instance_variable(symbol) -> obj
2540 * obj.remove_instance_variable(string) -> obj
2541 *
2542 * Removes the named instance variable from <i>obj</i>, returning that
2543 * variable's value. The name can be passed as a symbol or as a string.
2544 *
2545 * class Dummy
2546 * attr_reader :var
2547 * def initialize
2548 * @var = 99
2549 * end
2550 * def remove
2551 * remove_instance_variable(:@var)
2552 * end
2553 * end
2554 * d = Dummy.new
2555 * d.var #=> 99
2556 * d.remove #=> 99
2557 * d.var #=> nil
2558 */
2559
2560VALUE
2562{
2563 const ID id = id_for_var(obj, name, an, instance);
2564
2565 // Frozen check comes here because it's expected that we raise a
2566 // NameError (from the id_for_var check) before we raise a FrozenError
2567 rb_check_frozen(obj);
2568
2569 if (id) {
2570 VALUE val = rb_ivar_delete(obj, id, Qundef);
2571
2572 if (!UNDEF_P(val)) return val;
2573 }
2574
2575 rb_name_err_raise("instance variable %1$s not defined",
2576 obj, name);
2578}
2579
2580NORETURN(static void uninitialized_constant(VALUE, VALUE));
2581static void
2582uninitialized_constant(VALUE klass, VALUE name)
2583{
2584 if (klass && rb_class_real(klass) != rb_cObject)
2585 rb_name_err_raise("uninitialized constant %2$s::%1$s",
2586 klass, name);
2587 else
2588 rb_name_err_raise("uninitialized constant %1$s",
2589 klass, name);
2590}
2591
2592VALUE
2593rb_const_missing(VALUE klass, VALUE name)
2594{
2595 VALUE value = rb_funcallv(klass, idConst_missing, 1, &name);
2596 rb_vm_inc_const_missing_count();
2597 return value;
2598}
2599
2600
2601/*
2602 * call-seq:
2603 * mod.const_missing(sym) -> obj
2604 *
2605 * Invoked when a reference is made to an undefined constant in
2606 * <i>mod</i>. It is passed a symbol for the undefined constant, and
2607 * returns a value to be used for that constant. For example, consider:
2608 *
2609 * def Foo.const_missing(name)
2610 * name # return the constant name as Symbol
2611 * end
2612 *
2613 * Foo::UNDEFINED_CONST #=> :UNDEFINED_CONST: symbol returned
2614 *
2615 * As the example above shows, +const_missing+ is not required to create the
2616 * missing constant in <i>mod</i>, though that is often a side-effect. The
2617 * caller gets its return value when triggered. If the constant is also defined,
2618 * further lookups won't hit +const_missing+ and will return the value stored in
2619 * the constant as usual. Otherwise, +const_missing+ will be invoked again.
2620 *
2621 * In the next example, when a reference is made to an undefined constant,
2622 * +const_missing+ attempts to load a file whose path is the lowercase version
2623 * of the constant name (thus class <code>Fred</code> is assumed to be in file
2624 * <code>fred.rb</code>). If defined as a side-effect of loading the file, the
2625 * method returns the value stored in the constant. This implements an autoload
2626 * feature similar to Kernel#autoload and Module#autoload, though it differs in
2627 * important ways.
2628 *
2629 * def Object.const_missing(name)
2630 * @looked_for ||= {}
2631 * str_name = name.to_s
2632 * raise "Constant not found: #{name}" if @looked_for[str_name]
2633 * @looked_for[str_name] = 1
2634 * file = str_name.downcase
2635 * require file
2636 * const_get(name, false)
2637 * end
2638 *
2639 */
2640
2641VALUE
2642rb_mod_const_missing(VALUE klass, VALUE name)
2643{
2644 rb_execution_context_t *ec = GET_EC();
2645 VALUE ref = ec->private_const_reference;
2646 rb_vm_pop_cfunc_frame();
2647 if (ref) {
2648 ec->private_const_reference = 0;
2649 rb_name_err_raise("private constant %2$s::%1$s referenced", ref, name);
2650 }
2651 uninitialized_constant(klass, name);
2652
2654}
2655
2656static void
2657autoload_table_mark(void *ptr)
2658{
2659 rb_mark_tbl_no_pin((st_table *)ptr);
2660}
2661
2662static void
2663autoload_table_free(void *ptr)
2664{
2665 st_free_table((st_table *)ptr);
2666}
2667
2668static size_t
2669autoload_table_memsize(const void *ptr)
2670{
2671 const st_table *tbl = ptr;
2672 return st_memsize(tbl);
2673}
2674
2675static void
2676autoload_table_compact(void *ptr)
2677{
2678 rb_gc_ref_update_table_values_only((st_table *)ptr);
2679}
2680
2681static const rb_data_type_t autoload_table_type = {
2682 "autoload_table",
2683 {autoload_table_mark, autoload_table_free, autoload_table_memsize, autoload_table_compact,},
2684 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
2685};
2686
2687#define check_autoload_table(av) \
2688 (struct st_table *)rb_check_typeddata((av), &autoload_table_type)
2689
2690static VALUE
2691autoload_data(VALUE mod, ID id)
2692{
2693 struct st_table *tbl;
2694 st_data_t val;
2695
2696 // If we are called with a non-origin ICLASS, fetch the autoload data from
2697 // the original module.
2698 if (RB_TYPE_P(mod, T_ICLASS)) {
2699 if (RICLASS_IS_ORIGIN_P(mod)) {
2700 return 0;
2701 }
2702 else {
2703 mod = RBASIC(mod)->klass;
2704 }
2705 }
2706
2708
2709 // Look up the instance variable table for `autoload`, then index into that table with the given constant name `id`.
2710
2711 VALUE tbl_value = rb_ivar_lookup(mod, autoload, Qfalse);
2712 if (!RTEST(tbl_value) || !(tbl = check_autoload_table(tbl_value)) || !st_lookup(tbl, (st_data_t)id, &val)) {
2713 return 0;
2714 }
2715
2716 return (VALUE)val;
2717}
2718
2719// Every autoload constant has exactly one instance of autoload_const, stored in `autoload_features`. Since multiple autoload constants can refer to the same file, every `autoload_const` refers to a de-duplicated `autoload_data`.
2721 // The linked list node of all constants which are loaded by the related autoload feature.
2722 struct ccan_list_node cnode; /* <=> autoload_data.constants */
2723
2724 // The shared "autoload_data" if multiple constants are defined from the same feature.
2725 VALUE autoload_data_value;
2726
2727 // The box object when the autoload is called in a user box
2728 // Otherwise, Qnil means the root box
2729 VALUE box_value;
2730
2731 // The module we are loading a constant into.
2732 VALUE module;
2733
2734 // The name of the constant we are loading.
2735 ID name;
2736
2737 // The value of the constant (after it's loaded).
2738 VALUE value;
2739
2740 // The constant entry flags which need to be re-applied after autoloading the feature.
2741 rb_const_flag_t flag;
2742
2743 // The source file and line number that defined this constant (different from feature path).
2744 VALUE file;
2745 int line;
2746};
2747
2748// Each `autoload_data` uniquely represents a specific feature which can be loaded, and a list of constants which it is able to define. We use a mutex to coordinate multiple threads trying to load the same feature.
2750 // The feature path to require to load this constant.
2751 VALUE feature;
2752
2753 // The mutex which is protecting autoloading this feature.
2754 VALUE mutex;
2755
2756 // The process fork serial number since the autoload mutex will become invalid on fork.
2757 rb_serial_t fork_gen;
2758
2759 // The linked list of all constants that are going to be loaded by this autoload.
2760 struct ccan_list_head constants; /* <=> autoload_const.cnode */
2761};
2762
2763static void
2764autoload_data_mark_and_move(void *ptr)
2765{
2766 struct autoload_data *p = ptr;
2767
2768 rb_gc_mark_and_move(&p->feature);
2769 rb_gc_mark_and_move(&p->mutex);
2770}
2771
2772static void
2773autoload_data_free(void *ptr)
2774{
2775 struct autoload_data *p = ptr;
2776
2777 struct autoload_const *autoload_const, *next;
2778 ccan_list_for_each_safe(&p->constants, autoload_const, next, cnode) {
2779 ccan_list_del_init(&autoload_const->cnode);
2780 }
2781
2782 SIZED_FREE(p);
2783}
2784
2785static size_t
2786autoload_data_memsize(const void *ptr)
2787{
2788 return sizeof(struct autoload_data);
2789}
2790
2791static const rb_data_type_t autoload_data_type = {
2792 "autoload_data",
2793 {autoload_data_mark_and_move, autoload_data_free, autoload_data_memsize, autoload_data_mark_and_move},
2794 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
2795};
2796
2797static void
2798autoload_const_mark_and_move(void *ptr)
2799{
2800 struct autoload_const *ac = ptr;
2801
2802 rb_gc_mark_and_move(&ac->module);
2803 rb_gc_mark_and_move(&ac->autoload_data_value);
2804 rb_gc_mark_and_move(&ac->value);
2805 rb_gc_mark_and_move(&ac->file);
2806 rb_gc_mark_and_move(&ac->box_value);
2807}
2808
2809static size_t
2810autoload_const_memsize(const void *ptr)
2811{
2812 return sizeof(struct autoload_const);
2813}
2814
2815static void
2816autoload_const_free(void *ptr)
2817{
2818 struct autoload_const *autoload_const = ptr;
2819
2820 ccan_list_del(&autoload_const->cnode);
2821 SIZED_FREE(autoload_const);
2822}
2823
2824static const rb_data_type_t autoload_const_type = {
2825 "autoload_const",
2826 {autoload_const_mark_and_move, autoload_const_free, autoload_const_memsize, autoload_const_mark_and_move,},
2827 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
2828};
2829
2830static struct autoload_data *
2831get_autoload_data(VALUE autoload_const_value, struct autoload_const **autoload_const_pointer)
2832{
2833 struct autoload_const *autoload_const = rb_check_typeddata(autoload_const_value, &autoload_const_type);
2834
2835 VALUE autoload_data_value = autoload_const->autoload_data_value;
2836 struct autoload_data *autoload_data = rb_check_typeddata(autoload_data_value, &autoload_data_type);
2837
2838 /* do not reach across stack for ->state after forking: */
2839 if (autoload_data && autoload_data->fork_gen != GET_VM()->fork_gen) {
2840 RB_OBJ_WRITE(autoload_data_value, &autoload_data->mutex, Qnil);
2841 autoload_data->fork_gen = 0;
2842 }
2843
2844 if (autoload_const_pointer) *autoload_const_pointer = autoload_const;
2845
2846 return autoload_data;
2847}
2848
2850 VALUE dst_tbl_value;
2851 struct st_table *dst_tbl;
2852 const rb_box_t *box;
2853};
2854
2855static int
2856autoload_copy_table_for_box_i(st_data_t key, st_data_t value, st_data_t arg)
2857{
2859 struct autoload_copy_table_data *data = (struct autoload_copy_table_data *)arg;
2860 struct st_table *tbl = data->dst_tbl;
2861 VALUE tbl_value = data->dst_tbl_value;
2862 const rb_box_t *box = data->box;
2863
2864 VALUE src_value = (VALUE)value;
2865 struct autoload_const *src_const = rb_check_typeddata(src_value, &autoload_const_type);
2866 // autoload_data can be shared between copies because the feature is equal between copies.
2867 VALUE autoload_data_value = src_const->autoload_data_value;
2868 struct autoload_data *autoload_data = rb_check_typeddata(autoload_data_value, &autoload_data_type);
2869
2870 VALUE new_value = TypedData_Make_Struct(0, struct autoload_const, &autoload_const_type, autoload_const);
2871 RB_OBJ_WRITE(new_value, &autoload_const->box_value, rb_get_box_object((rb_box_t *)box));
2872 RB_OBJ_WRITE(new_value, &autoload_const->module, src_const->module);
2873 autoload_const->name = src_const->name;
2874 RB_OBJ_WRITE(new_value, &autoload_const->value, src_const->value);
2875 autoload_const->flag = src_const->flag;
2876 RB_OBJ_WRITE(new_value, &autoload_const->autoload_data_value, autoload_data_value);
2877 ccan_list_add_tail(&autoload_data->constants, &autoload_const->cnode);
2878
2879 st_insert(tbl, (st_data_t)autoload_const->name, (st_data_t)new_value);
2880 RB_OBJ_WRITTEN(tbl_value, Qundef, new_value);
2881
2882 return ST_CONTINUE;
2883}
2884
2885void
2886rb_autoload_copy_table_for_box(st_table *iv_ptr, const rb_box_t *box)
2887{
2888 struct st_table *src_tbl, *dst_tbl;
2889 VALUE src_tbl_value, dst_tbl_value;
2890 if (!rb_st_lookup(iv_ptr, (st_data_t)autoload, (st_data_t *)&src_tbl_value)) {
2891 // the class has no autoload table yet.
2892 return;
2893 }
2894 if (!RTEST(src_tbl_value) || !(src_tbl = check_autoload_table(src_tbl_value))) {
2895 // the __autoload__ ivar value isn't autoload table value.
2896 return;
2897 }
2898 src_tbl = check_autoload_table(src_tbl_value);
2899
2900 dst_tbl_value = TypedData_Wrap_Struct(0, &autoload_table_type, NULL);
2901 RTYPEDDATA_DATA(dst_tbl_value) = dst_tbl = st_init_numtable();
2902
2903 struct autoload_copy_table_data data = {
2904 .dst_tbl_value = dst_tbl_value,
2905 .dst_tbl = dst_tbl,
2906 .box = box,
2907 };
2908
2909 st_foreach(src_tbl, autoload_copy_table_for_box_i, (st_data_t)&data);
2910 st_insert(iv_ptr, (st_data_t)autoload, (st_data_t)dst_tbl_value);
2911}
2912
2913void
2914rb_autoload(VALUE module, ID name, const char *feature)
2915{
2916 if (!feature || !*feature) {
2917 rb_raise(rb_eArgError, "empty feature name");
2918 }
2919
2920 rb_autoload_str(module, name, rb_fstring_cstr(feature));
2921}
2922
2923static void const_set(VALUE klass, ID id, VALUE val);
2924static void const_added(VALUE klass, ID const_name);
2925
2927 VALUE module;
2928 ID name;
2929 VALUE feature;
2930 VALUE box_value;
2931};
2932
2933static VALUE
2934autoload_feature_lookup_or_create(VALUE feature, struct autoload_data **autoload_data_pointer)
2935{
2936 RUBY_ASSERT_MUTEX_OWNED(autoload_mutex);
2937 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
2938
2939 VALUE autoload_data_value = rb_hash_aref(autoload_features, feature);
2941
2942 if (NIL_P(autoload_data_value)) {
2943 autoload_data_value = TypedData_Make_Struct(0, struct autoload_data, &autoload_data_type, autoload_data);
2944 RB_OBJ_WRITE(autoload_data_value, &autoload_data->feature, feature);
2945 RB_OBJ_WRITE(autoload_data_value, &autoload_data->mutex, Qnil);
2946 ccan_list_head_init(&autoload_data->constants);
2947
2948 if (autoload_data_pointer) *autoload_data_pointer = autoload_data;
2949
2950 rb_hash_aset(autoload_features, feature, autoload_data_value);
2951 }
2952 else if (autoload_data_pointer) {
2953 *autoload_data_pointer = rb_check_typeddata(autoload_data_value, &autoload_data_type);
2954 }
2955
2956 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
2957 return autoload_data_value;
2958}
2959
2960static VALUE
2961autoload_table_lookup_or_create(VALUE module)
2962{
2963 VALUE autoload_table_value = rb_ivar_lookup(module, autoload, Qfalse);
2964 if (RTEST(autoload_table_value)) {
2965 return autoload_table_value;
2966 }
2967 else {
2968 autoload_table_value = TypedData_Wrap_Struct(0, &autoload_table_type, NULL);
2969 rb_class_ivar_set(module, autoload, autoload_table_value);
2970 RTYPEDDATA_DATA(autoload_table_value) = st_init_numtable();
2971 return autoload_table_value;
2972 }
2973}
2974
2975static VALUE
2976autoload_synchronized(VALUE _arguments)
2977{
2978 struct autoload_arguments *arguments = (struct autoload_arguments *)_arguments;
2979
2980 rb_const_entry_t *constant_entry = rb_const_lookup(arguments->module, arguments->name);
2981 if (constant_entry && !UNDEF_P(constant_entry->value)) {
2982 return Qfalse;
2983 }
2984
2985 // Reset any state associated with any previous constant:
2986 const_set(arguments->module, arguments->name, Qundef);
2987
2988 VALUE autoload_table_value = autoload_table_lookup_or_create(arguments->module);
2989 struct st_table *autoload_table = check_autoload_table(autoload_table_value);
2990
2991 // Ensure the string is uniqued since we use an identity lookup:
2992 VALUE feature = rb_fstring(arguments->feature);
2993
2995 VALUE autoload_data_value = autoload_feature_lookup_or_create(feature, &autoload_data);
2996
2997 {
2999 VALUE autoload_const_value = TypedData_Make_Struct(0, struct autoload_const, &autoload_const_type, autoload_const);
3000 RB_OBJ_WRITE(autoload_const_value, &autoload_const->box_value, arguments->box_value);
3001 RB_OBJ_WRITE(autoload_const_value, &autoload_const->module, arguments->module);
3002 autoload_const->name = arguments->name;
3003 autoload_const->value = Qundef;
3004 autoload_const->flag = CONST_PUBLIC;
3005 RB_OBJ_WRITE(autoload_const_value, &autoload_const->autoload_data_value, autoload_data_value);
3006 ccan_list_add_tail(&autoload_data->constants, &autoload_const->cnode);
3007 st_insert(autoload_table, (st_data_t)arguments->name, (st_data_t)autoload_const_value);
3008 RB_OBJ_WRITTEN(autoload_table_value, Qundef, autoload_const_value);
3009 }
3010
3011 return Qtrue;
3012}
3013
3014void
3015rb_autoload_str(VALUE module, ID name, VALUE feature)
3016{
3017 const rb_box_t *box = rb_current_box();
3018 VALUE current_box_value = rb_get_box_object((rb_box_t *)box);
3019
3020 if (!rb_is_const_id(name)) {
3021 rb_raise(rb_eNameError, "autoload must be constant name: %"PRIsVALUE"", QUOTE_ID(name));
3022 }
3023
3024 Check_Type(feature, T_STRING);
3025 if (!RSTRING_LEN(feature)) {
3026 rb_raise(rb_eArgError, "empty feature name");
3027 }
3028
3029 struct autoload_arguments arguments = {
3030 .module = module,
3031 .name = name,
3032 .feature = feature,
3033 .box_value = current_box_value,
3034 };
3035
3036 VALUE result = rb_mutex_synchronize(autoload_mutex, autoload_synchronized, (VALUE)&arguments);
3037
3038 if (result == Qtrue) {
3039 const_added(module, name);
3040 }
3041}
3042
3043static void
3044autoload_delete(VALUE module, ID name)
3045{
3046 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
3047
3048 st_data_t load = 0, key = name;
3049
3050 RUBY_ASSERT(RB_TYPE_P(module, T_CLASS) || RB_TYPE_P(module, T_MODULE));
3051
3052 VALUE table_value = rb_ivar_lookup(module, autoload, Qfalse);
3053 if (RTEST(table_value)) {
3054 struct st_table *table = check_autoload_table(table_value);
3055
3056 st_delete(table, &key, &load);
3057 RB_OBJ_WRITTEN(table_value, load, Qundef);
3058
3059 /* Qfalse can indicate already deleted */
3060 if (load != Qfalse) {
3062 struct autoload_data *autoload_data = get_autoload_data((VALUE)load, &autoload_const);
3063
3064 VM_ASSERT(autoload_data);
3065 VM_ASSERT(!ccan_list_empty(&autoload_data->constants));
3066
3067 /*
3068 * we must delete here to avoid "already initialized" warnings
3069 * with parallel autoload. Using list_del_init here so list_del
3070 * works in autoload_const_free
3071 */
3072 ccan_list_del_init(&autoload_const->cnode);
3073
3074 if (ccan_list_empty(&autoload_data->constants)) {
3075 rb_hash_delete(autoload_features, autoload_data->feature);
3076 }
3077
3078 // If the autoload table is empty, we can delete it.
3079 if (table->num_entries == 0) {
3080 rb_attr_delete(module, autoload);
3081 }
3082 }
3083 }
3084
3085 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
3086}
3087
3088static int
3089autoload_by_someone_else(struct autoload_data *ele)
3090{
3091 return ele->mutex != Qnil && !rb_mutex_owned_p(ele->mutex);
3092}
3093
3094static VALUE
3095check_autoload_required(VALUE mod, ID id, const char **loadingpath)
3096{
3097 VALUE autoload_const_value = autoload_data(mod, id);
3099 const char *loading;
3100
3101 if (!autoload_const_value || !(autoload_data = get_autoload_data(autoload_const_value, 0))) {
3102 return 0;
3103 }
3104
3105 VALUE feature = autoload_data->feature;
3106
3107 /*
3108 * if somebody else is autoloading, we MUST wait for them, since
3109 * rb_provide_feature can provide a feature before autoload_const_set
3110 * completes. We must wait until autoload_const_set finishes in
3111 * the other thread.
3112 */
3113 if (autoload_by_someone_else(autoload_data)) {
3114 return autoload_const_value;
3115 }
3116
3117 loading = RSTRING_PTR(feature);
3118
3119 if (!rb_feature_provided(loading, &loading)) {
3120 return autoload_const_value;
3121 }
3122
3123 if (loadingpath && loading) {
3124 *loadingpath = loading;
3125 return autoload_const_value;
3126 }
3127
3128 return 0;
3129}
3130
3131static struct autoload_const *autoloading_const_entry(VALUE mod, ID id);
3132
3133int
3134rb_autoloading_value(VALUE mod, ID id, VALUE* value, rb_const_flag_t *flag)
3135{
3136 struct autoload_const *ac = autoloading_const_entry(mod, id);
3137 if (!ac) return FALSE;
3138
3139 if (value) {
3140 *value = ac->value;
3141 }
3142
3143 if (flag) {
3144 *flag = ac->flag;
3145 }
3146
3147 return TRUE;
3148}
3149
3150static int
3151autoload_by_current(struct autoload_data *ele)
3152{
3153 return ele->mutex != Qnil && rb_mutex_owned_p(ele->mutex);
3154}
3155
3156// If there is an autoloading constant and it has been set by the current
3157// execution context, return it. This allows threads which are loading code to
3158// refer to their own autoloaded constants.
3159struct autoload_const *
3160autoloading_const_entry(VALUE mod, ID id)
3161{
3162 VALUE load = autoload_data(mod, id);
3163 struct autoload_data *ele;
3164 struct autoload_const *ac;
3165
3166 // Find the autoloading state:
3167 if (!load || !(ele = get_autoload_data(load, &ac))) {
3168 // Couldn't be found:
3169 return 0;
3170 }
3171
3172 // Check if it's being loaded by the current thread/fiber:
3173 if (autoload_by_current(ele)) {
3174 if (!UNDEF_P(ac->value)) {
3175 return ac;
3176 }
3177 }
3178
3179 return 0;
3180}
3181
3182static int
3183autoload_defined_p(VALUE mod, ID id)
3184{
3185 rb_const_entry_t *ce = rb_const_lookup(mod, id);
3186
3187 // If there is no constant or the constant is not undefined (special marker for autoloading):
3188 if (!ce || !UNDEF_P(ce->value)) {
3189 // We are not autoloading:
3190 return 0;
3191 }
3192
3193 // Otherwise check if there is an autoload in flight right now:
3194 return !rb_autoloading_value(mod, id, NULL, NULL);
3195}
3196
3197static void const_tbl_update(struct autoload_const *, int);
3198
3200 VALUE module;
3201 ID name;
3202 int flag;
3203
3204 VALUE mutex;
3205
3206 // The specific constant which triggered the autoload code to fire:
3208
3209 // The parent autoload data which is shared between multiple constants:
3211};
3212
3213static VALUE
3214autoload_const_set(struct autoload_const *ac)
3215{
3216 check_before_mod_set(ac->module, ac->name, ac->value, "constant");
3217
3218 RB_VM_LOCKING() {
3219 const_tbl_update(ac, true);
3220 }
3221
3222 return 0; /* ignored */
3223}
3224
3225static VALUE
3226autoload_load_needed(VALUE _arguments)
3227{
3228 struct autoload_load_arguments *arguments = (struct autoload_load_arguments*)_arguments;
3229
3230 const char *loading = 0, *src;
3231
3232 if (!autoload_defined_p(arguments->module, arguments->name)) {
3233 return Qfalse;
3234 }
3235
3236 VALUE autoload_const_value = check_autoload_required(arguments->module, arguments->name, &loading);
3237 if (!autoload_const_value) {
3238 return Qfalse;
3239 }
3240
3241 src = rb_sourcefile();
3242 if (src && loading && strcmp(src, loading) == 0) {
3243 return Qfalse;
3244 }
3245
3248 if (!(autoload_data = get_autoload_data(autoload_const_value, &autoload_const))) {
3249 return Qfalse;
3250 }
3251
3252 if (NIL_P(autoload_data->mutex)) {
3253 RB_OBJ_WRITE(autoload_const->autoload_data_value, &autoload_data->mutex, rb_mutex_new());
3254 autoload_data->fork_gen = GET_VM()->fork_gen;
3255 }
3256 else if (rb_mutex_owned_p(autoload_data->mutex)) {
3257 return Qfalse;
3258 }
3259
3260 arguments->mutex = autoload_data->mutex;
3261 arguments->autoload_const = autoload_const;
3262
3263 return autoload_const_value;
3264}
3265
3266static VALUE
3267autoload_apply_constants(VALUE _arguments)
3268{
3269 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
3270
3271 struct autoload_load_arguments *arguments = (struct autoload_load_arguments*)_arguments;
3272
3273 struct autoload_const *autoload_const = 0; // for ccan_container_off_var()
3274 struct autoload_const *next;
3275
3276 // We use safe iteration here because `autoload_const_set` will eventually invoke
3277 // `autoload_delete` which will remove the constant from the linked list. In theory, once
3278 // the `autoload_data->constants` linked list is empty, we can remove it.
3279
3280 // Iterate over all constants and assign them:
3281 ccan_list_for_each_safe(&arguments->autoload_data->constants, autoload_const, next, cnode) {
3282 if (!UNDEF_P(autoload_const->value)) {
3283 autoload_const_set(autoload_const);
3284 }
3285 }
3286
3287 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
3288
3289 return Qtrue;
3290}
3291
3292static VALUE
3293autoload_feature_require(VALUE _arguments)
3294{
3295 VALUE receiver = rb_vm_top_self();
3296
3297 struct autoload_load_arguments *arguments = (struct autoload_load_arguments*)_arguments;
3298
3299 struct autoload_const *autoload_const = arguments->autoload_const;
3300 VALUE autoload_box_value = autoload_const->box_value;
3301
3302 // We save this for later use in autoload_apply_constants:
3303 arguments->autoload_data = rb_check_typeddata(autoload_const->autoload_data_value, &autoload_data_type);
3304
3305 if (rb_box_available() && BOX_OBJ_P(autoload_box_value))
3306 receiver = autoload_box_value;
3307
3308 /*
3309 * Clear the global cc cache table because the require method can be different from the current
3310 * box's one and it may cause inconsistent cc-cme states.
3311 * For example, the assertion below may fail in gccct_method_search();
3312 * VM_ASSERT(vm_cc_check_cme(cc, rb_callable_method_entry(klass, mid)))
3313 */
3314 rb_gccct_clear_table();
3315
3316 VALUE result = rb_funcall(receiver, rb_intern("require"), 1, arguments->autoload_data->feature);
3317
3318 if (RTEST(result)) {
3319 return rb_mutex_synchronize(autoload_mutex, autoload_apply_constants, _arguments);
3320 }
3321 return result;
3322}
3323
3324static VALUE
3325autoload_try_load(VALUE _arguments)
3326{
3327 struct autoload_load_arguments *arguments = (struct autoload_load_arguments*)_arguments;
3328
3329 VALUE result = autoload_feature_require(_arguments);
3330
3331 // After we loaded the feature, if the constant is not defined, we remove it completely:
3332 rb_const_entry_t *ce = rb_const_lookup(arguments->module, arguments->name);
3333
3334 if (!ce || UNDEF_P(ce->value)) {
3335 result = Qfalse;
3336
3337 rb_const_remove(arguments->module, arguments->name);
3338
3339 if (arguments->module == rb_cObject) {
3340 rb_warning(
3341 "Expected %"PRIsVALUE" to define %"PRIsVALUE" but it didn't",
3342 arguments->autoload_data->feature,
3343 ID2SYM(arguments->name)
3344 );
3345 }
3346 else {
3347 rb_warning(
3348 "Expected %"PRIsVALUE" to define %"PRIsVALUE"::%"PRIsVALUE" but it didn't",
3349 arguments->autoload_data->feature,
3350 arguments->module,
3351 ID2SYM(arguments->name)
3352 );
3353 }
3354 }
3355 else {
3356 // Otherwise, it was loaded, copy the flags from the autoload constant:
3357 ce->flag |= arguments->flag;
3358 }
3359
3360 return result;
3361}
3362
3363VALUE
3365{
3366 rb_const_entry_t *ce = rb_const_lookup(module, name);
3367
3368 // We bail out as early as possible without any synchronisation:
3369 if (!ce || !UNDEF_P(ce->value)) {
3370 return Qfalse;
3371 }
3372
3373 // At this point, we assume there might be autoloading, so fail if it's ractor:
3374 if (UNLIKELY(!rb_ractor_main_p())) {
3375 return rb_ractor_autoload_load(module, name);
3376 }
3377
3378 // This state is stored on the stack and is used during the autoload process.
3379 struct autoload_load_arguments arguments = {.module = module, .name = name, .mutex = Qnil};
3380
3381 // Figure out whether we can autoload the named constant:
3382 VALUE autoload_const_value = rb_mutex_synchronize(autoload_mutex, autoload_load_needed, (VALUE)&arguments);
3383
3384 // This confirms whether autoloading is required or not:
3385 if (autoload_const_value == Qfalse) return autoload_const_value;
3386
3387 arguments.flag = ce->flag & (CONST_DEPRECATED | CONST_VISIBILITY_MASK);
3388
3389 // Only one thread will enter here at a time:
3390 VALUE result = rb_mutex_synchronize(arguments.mutex, autoload_try_load, (VALUE)&arguments);
3391
3392 // If you don't guard this value, it's possible for the autoload constant to
3393 // be freed by another thread which loads multiple constants, one of which
3394 // resolves to the constant this thread is trying to load, so proteect this
3395 // so that it is not freed until we are done with it in `autoload_try_load`:
3396 RB_GC_GUARD(autoload_const_value);
3397
3398 return result;
3399}
3400
3401VALUE
3403{
3404 return rb_autoload_at_p(mod, id, TRUE);
3405}
3406
3407VALUE
3408rb_autoload_at_p(VALUE mod, ID id, int recur)
3409{
3410 VALUE load;
3411 struct autoload_data *ele;
3412
3413 while (!autoload_defined_p(mod, id)) {
3414 if (!recur) return Qnil;
3415 mod = RCLASS_SUPER(mod);
3416 if (!mod) return Qnil;
3417 }
3418 load = check_autoload_required(mod, id, 0);
3419 if (!load) return Qnil;
3420 return (ele = get_autoload_data(load, 0)) ? ele->feature : Qnil;
3421}
3422
3423void
3424rb_const_warn_if_deprecated(const rb_const_entry_t *ce, VALUE klass, ID id)
3425{
3426 if (RB_CONST_DEPRECATED_P(ce) &&
3427 rb_warning_category_enabled_p(RB_WARN_CATEGORY_DEPRECATED)) {
3428 if (klass == rb_cObject) {
3429 rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "constant ::%"PRIsVALUE" is deprecated", QUOTE_ID(id));
3430 }
3431 else {
3432 rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "constant %"PRIsVALUE"::%"PRIsVALUE" is deprecated",
3433 rb_class_name(klass), QUOTE_ID(id));
3434 }
3435 }
3436}
3437
3438static VALUE
3439rb_const_get_0(VALUE klass, ID id, int exclude, int recurse, int visibility)
3440{
3441 VALUE found_in;
3442 VALUE c = rb_const_search(klass, id, exclude, recurse, visibility, &found_in);
3443 if (!UNDEF_P(c)) {
3444 if (UNLIKELY(!rb_ractor_main_p())) {
3445 if (!rb_ractor_shareable_p(c)) {
3446 rb_raise(rb_eRactorIsolationError, "can not access non-shareable objects in constant %"PRIsVALUE"::%"PRIsVALUE" by non-main Ractor.", rb_class_path(found_in), rb_id2str(id));
3447 }
3448 }
3449 return c;
3450 }
3451 return rb_const_missing(klass, ID2SYM(id));
3452}
3453
3454static VALUE
3455rb_const_search_from(VALUE klass, ID id, int exclude, int recurse, int visibility, VALUE *found_in)
3456{
3457 VALUE value, current;
3458 bool first_iteration = true;
3459
3460 for (current = klass;
3461 RTEST(current);
3462 current = RCLASS_SUPER(current), first_iteration = false) {
3463 VALUE tmp;
3464 VALUE am = 0;
3465 rb_const_entry_t *ce;
3466
3467 if (!first_iteration && RCLASS_ORIGIN(current) != current) {
3468 // This item in the super chain has an origin iclass
3469 // that comes later in the chain. Skip this item so
3470 // prepended modules take precedence.
3471 continue;
3472 }
3473
3474 // Do lookup in original class or module in case we are at an origin
3475 // iclass in the chain.
3476 tmp = current;
3477 if (BUILTIN_TYPE(tmp) == T_ICLASS) tmp = RBASIC(tmp)->klass;
3478
3479 // Do the lookup. Loop in case of autoload.
3480 while ((ce = rb_const_lookup(tmp, id))) {
3481 if (visibility && RB_CONST_PRIVATE_P(ce)) {
3482 GET_EC()->private_const_reference = tmp;
3483 return Qundef;
3484 }
3485 rb_const_warn_if_deprecated(ce, tmp, id);
3486 value = ce->value;
3487 if (UNDEF_P(value)) {
3488 struct autoload_const *ac;
3489 if (am == tmp) break;
3490 am = tmp;
3491 ac = autoloading_const_entry(tmp, id);
3492 if (ac) {
3493 if (found_in) { *found_in = tmp; }
3494 return ac->value;
3495 }
3496 rb_autoload_load(tmp, id);
3497 continue;
3498 }
3499 if (exclude && tmp == rb_cObject) {
3500 goto not_found;
3501 }
3502 if (found_in) { *found_in = tmp; }
3503 return value;
3504 }
3505 if (!recurse) break;
3506 }
3507
3508 not_found:
3509 GET_EC()->private_const_reference = 0;
3510 return Qundef;
3511}
3512
3513static VALUE
3514rb_const_search(VALUE klass, ID id, int exclude, int recurse, int visibility, VALUE *found_in)
3515{
3516 VALUE value;
3517
3518 if (klass == rb_cObject) exclude = FALSE;
3519 value = rb_const_search_from(klass, id, exclude, recurse, visibility, found_in);
3520 if (!UNDEF_P(value)) return value;
3521 if (exclude) return value;
3522 if (BUILTIN_TYPE(klass) != T_MODULE) return value;
3523 /* search global const too, if klass is a module */
3524 return rb_const_search_from(rb_cObject, id, FALSE, recurse, visibility, found_in);
3525}
3526
3527VALUE
3529{
3530 return rb_const_get_0(klass, id, TRUE, TRUE, FALSE);
3531}
3532
3533VALUE
3535{
3536 return rb_const_get_0(klass, id, FALSE, TRUE, FALSE);
3537}
3538
3539VALUE
3541{
3542 return rb_const_get_0(klass, id, TRUE, FALSE, FALSE);
3543}
3544
3545VALUE
3546rb_public_const_get_from(VALUE klass, ID id)
3547{
3548 return rb_const_get_0(klass, id, TRUE, TRUE, TRUE);
3549}
3550
3551VALUE
3552rb_public_const_get_at(VALUE klass, ID id)
3553{
3554 return rb_const_get_0(klass, id, TRUE, FALSE, TRUE);
3555}
3556
3557NORETURN(static void undefined_constant(VALUE mod, VALUE name));
3558static void
3559undefined_constant(VALUE mod, VALUE name)
3560{
3561 rb_name_err_raise("constant %2$s::%1$s not defined",
3562 mod, name);
3563}
3564
3565static VALUE
3566rb_const_location_from(VALUE klass, ID id, int exclude, int recurse, int visibility)
3567{
3568 while (RTEST(klass)) {
3569 rb_const_entry_t *ce;
3570
3571 while ((ce = rb_const_lookup(klass, id))) {
3572 if (visibility && RB_CONST_PRIVATE_P(ce)) {
3573 return Qnil;
3574 }
3575 if (exclude && klass == rb_cObject) {
3576 goto not_found;
3577 }
3578
3579 if (UNDEF_P(ce->value)) { // autoload
3580 VALUE autoload_const_value = autoload_data(klass, id);
3581 if (RTEST(autoload_const_value)) {
3583 struct autoload_data *autoload_data = get_autoload_data(autoload_const_value, &autoload_const);
3584
3585 if (!UNDEF_P(autoload_const->value) && RTEST(rb_mutex_owned_p(autoload_data->mutex))) {
3586 return rb_assoc_new(autoload_const->file, INT2NUM(autoload_const->line));
3587 }
3588 }
3589 }
3590
3591 if (NIL_P(ce->file)) return rb_ary_new();
3592 return rb_assoc_new(ce->file, INT2NUM(ce->line));
3593 }
3594 if (!recurse) break;
3595 klass = RCLASS_SUPER(klass);
3596 }
3597
3598 not_found:
3599 return Qnil;
3600}
3601
3602static VALUE
3603rb_const_location(VALUE klass, ID id, int exclude, int recurse, int visibility)
3604{
3605 VALUE loc;
3606
3607 if (klass == rb_cObject) exclude = FALSE;
3608 loc = rb_const_location_from(klass, id, exclude, recurse, visibility);
3609 if (!NIL_P(loc)) return loc;
3610 if (exclude) return loc;
3611 if (BUILTIN_TYPE(klass) != T_MODULE) return loc;
3612 /* search global const too, if klass is a module */
3613 return rb_const_location_from(rb_cObject, id, FALSE, recurse, visibility);
3614}
3615
3616VALUE
3617rb_const_source_location(VALUE klass, ID id)
3618{
3619 return rb_const_location(klass, id, FALSE, TRUE, FALSE);
3620}
3621
3622VALUE
3623rb_const_source_location_at(VALUE klass, ID id)
3624{
3625 return rb_const_location(klass, id, TRUE, FALSE, FALSE);
3626}
3627
3628/*
3629 * call-seq:
3630 * remove_const(sym) -> obj
3631 *
3632 * Removes the definition of the given constant, returning that
3633 * constant's previous value. If that constant referred to
3634 * a module, this will not change that module's name and can lead
3635 * to confusion.
3636 */
3637
3638VALUE
3640{
3641 const ID id = id_for_var(mod, name, a, constant);
3642
3643 if (!id) {
3644 undefined_constant(mod, name);
3645 }
3646 return rb_const_remove(mod, id);
3647}
3648
3649static rb_const_entry_t * const_lookup(struct rb_id_table *tbl, ID id);
3650
3651VALUE
3653{
3654 VALUE val;
3655 rb_const_entry_t *ce;
3656
3657 rb_check_frozen(mod);
3658
3659 ce = rb_const_lookup(mod, id);
3660
3661 if (!ce) {
3662 if (rb_const_defined_at(mod, id)) {
3663 rb_name_err_raise("cannot remove %2$s::%1$s", mod, ID2SYM(id));
3664 }
3665
3666 undefined_constant(mod, ID2SYM(id));
3667 }
3668
3669 VALUE writable_ce = 0;
3670 if (rb_id_table_lookup(RCLASS_WRITABLE_CONST_TBL(mod), id, &writable_ce)) {
3671 rb_id_table_delete(RCLASS_WRITABLE_CONST_TBL(mod), id);
3672 if ((rb_const_entry_t *)writable_ce != ce) {
3673 SIZED_FREE((rb_const_entry_t *)writable_ce);
3674 }
3675 }
3676
3677 rb_const_warn_if_deprecated(ce, mod, id);
3679
3680 val = ce->value;
3681
3682 if (UNDEF_P(val)) {
3683 autoload_delete(mod, id);
3684 val = Qnil;
3685 }
3686
3687 if (ce != const_lookup(RCLASS_PRIME_CONST_TBL(mod), id)) {
3688 SIZED_FREE(ce);
3689 }
3690 // else - skip free'ing the ce because it still exists in the prime classext
3691
3692 return val;
3693}
3694
3695static int
3696cv_i_update(st_data_t *k, st_data_t *v, st_data_t a, int existing)
3697{
3698 if (existing) return ST_STOP;
3699 *v = a;
3700 return ST_CONTINUE;
3701}
3702
3703static enum rb_id_table_iterator_result
3704sv_i(ID key, VALUE v, void *a)
3705{
3707 st_table *tbl = a;
3708
3709 if (rb_is_const_id(key)) {
3710 st_update(tbl, (st_data_t)key, cv_i_update, (st_data_t)ce);
3711 }
3712 return ID_TABLE_CONTINUE;
3713}
3714
3715static enum rb_id_table_iterator_result
3716rb_local_constants_i(ID const_name, VALUE const_value, void *ary)
3717{
3718 if (rb_is_const_id(const_name) && !RB_CONST_PRIVATE_P((rb_const_entry_t *)const_value)) {
3719 rb_ary_push((VALUE)ary, ID2SYM(const_name));
3720 }
3721 return ID_TABLE_CONTINUE;
3722}
3723
3724static VALUE
3725rb_local_constants(VALUE mod)
3726{
3727 struct rb_id_table *tbl = RCLASS_CONST_TBL(mod);
3728 VALUE ary;
3729
3730 if (!tbl) return rb_ary_new2(0);
3731
3732 RB_VM_LOCKING() {
3733 ary = rb_ary_new2(rb_id_table_size(tbl));
3734 rb_id_table_foreach(tbl, rb_local_constants_i, (void *)ary);
3735 }
3736
3737 return ary;
3738}
3739
3740void*
3741rb_mod_const_at(VALUE mod, void *data)
3742{
3743 st_table *tbl = data;
3744 if (!tbl) {
3745 tbl = st_init_numtable();
3746 }
3747 if (RCLASS_CONST_TBL(mod)) {
3748 RB_VM_LOCKING() {
3749 rb_id_table_foreach(RCLASS_CONST_TBL(mod), sv_i, tbl);
3750 }
3751 }
3752 return tbl;
3753}
3754
3755void*
3756rb_mod_const_of(VALUE mod, void *data)
3757{
3758 VALUE tmp = mod;
3759 for (;;) {
3760 data = rb_mod_const_at(tmp, data);
3761 tmp = RCLASS_SUPER(tmp);
3762 if (!tmp) break;
3763 if (tmp == rb_cObject && mod != rb_cObject) break;
3764 }
3765 return data;
3766}
3767
3768static int
3769list_i(st_data_t key, st_data_t value, VALUE ary)
3770{
3771 ID sym = (ID)key;
3772 rb_const_entry_t *ce = (rb_const_entry_t *)value;
3773 if (RB_CONST_PUBLIC_P(ce)) rb_ary_push(ary, ID2SYM(sym));
3774 return ST_CONTINUE;
3775}
3776
3777VALUE
3778rb_const_list(void *data)
3779{
3780 st_table *tbl = data;
3781 VALUE ary;
3782
3783 if (!tbl) return rb_ary_new2(0);
3784 ary = rb_ary_new2(tbl->num_entries);
3785 st_foreach_safe(tbl, list_i, ary);
3786 st_free_table(tbl);
3787
3788 return ary;
3789}
3790
3791/*
3792 * call-seq:
3793 * mod.constants(inherit=true) -> array
3794 *
3795 * Returns an array of the names of the constants accessible in
3796 * <i>mod</i>. This includes the names of constants in any included
3797 * modules (example at start of section), unless the <i>inherit</i>
3798 * parameter is set to <code>false</code>.
3799 *
3800 * The implementation makes no guarantees about the order in which the
3801 * constants are yielded.
3802 *
3803 * IO.constants.include?(:SYNC) #=> true
3804 * IO.constants(false).include?(:SYNC) #=> false
3805 *
3806 * Also see Module#const_defined?.
3807 */
3808
3809VALUE
3810rb_mod_constants(int argc, const VALUE *argv, VALUE mod)
3811{
3812 bool inherit = true;
3813
3814 if (rb_check_arity(argc, 0, 1)) inherit = RTEST(argv[0]);
3815
3816 if (inherit) {
3817 return rb_const_list(rb_mod_const_of(mod, 0));
3818 }
3819 else {
3820 return rb_local_constants(mod);
3821 }
3822}
3823
3824static int
3825rb_const_defined_0(VALUE klass, ID id, int exclude, int recurse, int visibility)
3826{
3827 VALUE tmp;
3828 int mod_retry = 0;
3829 rb_const_entry_t *ce;
3830
3831 tmp = klass;
3832 retry:
3833 while (tmp) {
3834 if ((ce = rb_const_lookup(tmp, id))) {
3835 if (visibility && RB_CONST_PRIVATE_P(ce)) {
3836 return (int)Qfalse;
3837 }
3838 if (UNDEF_P(ce->value) && !check_autoload_required(tmp, id, 0) &&
3839 !rb_autoloading_value(tmp, id, NULL, NULL))
3840 return (int)Qfalse;
3841
3842 if (exclude && tmp == rb_cObject && klass != rb_cObject) {
3843 return (int)Qfalse;
3844 }
3845
3846 return (int)Qtrue;
3847 }
3848 if (!recurse) break;
3849 tmp = RCLASS_SUPER(tmp);
3850 }
3851 if (!exclude && !mod_retry && BUILTIN_TYPE(klass) == T_MODULE) {
3852 mod_retry = 1;
3853 tmp = rb_cObject;
3854 goto retry;
3855 }
3856 return (int)Qfalse;
3857}
3858
3859int
3861{
3862 return rb_const_defined_0(klass, id, TRUE, TRUE, FALSE);
3863}
3864
3865int
3867{
3868 return rb_const_defined_0(klass, id, FALSE, TRUE, FALSE);
3869}
3870
3871int
3873{
3874 return rb_const_defined_0(klass, id, TRUE, FALSE, FALSE);
3875}
3876
3877int
3878rb_public_const_defined_from(VALUE klass, ID id)
3879{
3880 return rb_const_defined_0(klass, id, TRUE, TRUE, TRUE);
3881}
3882
3883static void
3884check_before_mod_set(VALUE klass, ID id, VALUE val, const char *dest)
3885{
3886 rb_check_frozen(klass);
3887}
3888
3889static void set_namespace_path(VALUE named_namespace, VALUE name);
3890
3891static enum rb_id_table_iterator_result
3892set_namespace_path_i(ID id, VALUE v, void *payload)
3893{
3895 VALUE value = ce->value;
3896 VALUE parental_path = *((VALUE *) payload);
3897 if (!rb_is_const_id(id) || !rb_namespace_p(value)) {
3898 return ID_TABLE_CONTINUE;
3899 }
3900
3901 bool has_permanent_classpath;
3902 classname(value, &has_permanent_classpath);
3903 if (has_permanent_classpath) {
3904 return ID_TABLE_CONTINUE;
3905 }
3906 set_namespace_path(value, build_const_path(parental_path, id));
3907
3908 if (!RCLASS_PERMANENT_CLASSPATH_P(value)) {
3909 RCLASS_WRITE_CLASSPATH(value, 0, false);
3910 }
3911
3912 return ID_TABLE_CONTINUE;
3913}
3914
3915/*
3916 * Assign permanent classpaths to all namespaces that are directly or indirectly
3917 * nested under +named_namespace+. +named_namespace+ must have a permanent
3918 * classpath.
3919 */
3920static void
3921set_namespace_path(VALUE named_namespace, VALUE namespace_path)
3922{
3923 struct rb_id_table *const_table = RCLASS_CONST_TBL(named_namespace);
3924 RB_OBJ_SET_SHAREABLE(namespace_path);
3925
3926 RB_VM_LOCKING() {
3927 RCLASS_WRITE_CLASSPATH(named_namespace, namespace_path, true);
3928
3929 if (const_table) {
3930 rb_id_table_foreach(const_table, set_namespace_path_i, &namespace_path);
3931 }
3932 }
3933}
3934
3935static void
3936const_added(VALUE klass, ID const_name)
3937{
3938 if (GET_VM()->running) {
3939 VALUE name = ID2SYM(const_name);
3940 rb_funcallv(klass, idConst_added, 1, &name);
3941 }
3942}
3943
3944static void
3945const_set(VALUE klass, ID id, VALUE val)
3946{
3947 rb_const_entry_t *ce;
3948
3949 if (NIL_P(klass)) {
3950 rb_raise(rb_eTypeError, "no class/module to define constant %"PRIsVALUE"",
3951 QUOTE_ID(id));
3952 }
3953
3954 if (!rb_ractor_main_p() && !rb_ractor_shareable_p(val)) {
3955 rb_raise(rb_eRactorIsolationError, "can not set constants with non-shareable objects by non-main Ractors");
3956 }
3957
3958 check_before_mod_set(klass, id, val, "constant");
3959
3960 RB_VM_LOCKING() {
3961 struct rb_id_table *tbl = RCLASS_WRITABLE_CONST_TBL(klass);
3962 if (!tbl) {
3963 tbl = rb_id_table_create(0);
3964 RCLASS_WRITE_CONST_TBL(klass, tbl, false);
3967 rb_id_table_insert(tbl, id, (VALUE)ce);
3968 setup_const_entry(ce, klass, val, CONST_PUBLIC);
3969 }
3970 else {
3971 struct autoload_const ac = {
3972 .module = klass, .name = id,
3973 .value = val, .flag = CONST_PUBLIC,
3974 /* fill the rest with 0 */
3975 };
3976 ac.file = rb_source_location(&ac.line);
3977 const_tbl_update(&ac, false);
3978 }
3979 }
3980
3981 /*
3982 * Resolve and cache class name immediately to resolve ambiguity
3983 * and avoid order-dependency on const_tbl
3984 */
3985 if (rb_cObject && rb_namespace_p(val)) {
3986 bool val_path_permanent;
3987 VALUE val_path = classname(val, &val_path_permanent);
3988 if (NIL_P(val_path) || !val_path_permanent) {
3989 if (klass == rb_cObject) {
3990 set_namespace_path(val, rb_id2str(id));
3991 }
3992 else {
3993 bool parental_path_permanent;
3994 VALUE parental_path = classname(klass, &parental_path_permanent);
3995 if (NIL_P(parental_path)) {
3996 bool throwaway;
3997 parental_path = rb_tmp_class_path(klass, &throwaway, make_temporary_path);
3998 }
3999 if (parental_path_permanent && !val_path_permanent) {
4000 set_namespace_path(val, build_const_path(parental_path, id));
4001 }
4002 else if (!parental_path_permanent && NIL_P(val_path)) {
4003 VALUE path = build_const_path(parental_path, id);
4004 RCLASS_SET_CLASSPATH(val, path, false);
4005 }
4006 }
4007 }
4008 }
4009}
4010
4011void
4013{
4014 const_set(klass, id, val);
4015 const_added(klass, id);
4016}
4017
4018static VALUE
4019autoload_const_value_for_named_constant(VALUE module, ID name, struct autoload_const **autoload_const_pointer)
4020{
4021 VALUE autoload_const_value = autoload_data(module, name);
4022 if (!autoload_const_value) return Qfalse;
4023
4024 struct autoload_data *autoload_data = get_autoload_data(autoload_const_value, autoload_const_pointer);
4025 if (!autoload_data) return Qfalse;
4026
4027 /* for autoloading thread, keep the defined value to autoloading storage */
4028 if (autoload_by_current(autoload_data)) {
4029 return autoload_const_value;
4030 }
4031
4032 return Qfalse;
4033}
4034
4035static void
4036const_tbl_update(struct autoload_const *ac, int autoload_force)
4037{
4038 VALUE value;
4039 VALUE klass = ac->module;
4040 VALUE val = ac->value;
4041 ID id = ac->name;
4042 struct rb_id_table *tbl = RCLASS_CONST_TBL(klass);
4043 rb_const_flag_t visibility = ac->flag;
4044 rb_const_entry_t *ce;
4045
4046 if (rb_id_table_lookup(tbl, id, &value)) {
4047 ce = (rb_const_entry_t *)value;
4048 if (UNDEF_P(ce->value)) {
4049 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
4050 VALUE file = ac->file;
4051 int line = ac->line;
4052 VALUE autoload_const_value = autoload_const_value_for_named_constant(klass, id, &ac);
4053
4054 if (!autoload_force && autoload_const_value) {
4056
4057 RB_OBJ_WRITE(autoload_const_value, &ac->value, val);
4058 RB_OBJ_WRITE(autoload_const_value, &ac->file, rb_source_location(&ac->line));
4059 }
4060 else {
4061 /* otherwise autoloaded constant, allow to override */
4062 autoload_delete(klass, id);
4063 ce->flag = visibility;
4064 RB_OBJ_WRITE(klass, &ce->value, val);
4065 RB_OBJ_WRITE(klass, &ce->file, file);
4066 ce->line = line;
4067 }
4068 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
4069 return;
4070 }
4071 else {
4072 VALUE name = QUOTE_ID(id);
4073 visibility = ce->flag;
4074
4075 VALUE previous = Qnil;
4076 if (!NIL_P(ce->file) && ce->line) {
4077 previous = rb_sprintf("\n%"PRIsVALUE":%d: warning: previous definition of %"PRIsVALUE" was here", ce->file, ce->line, name);
4078 }
4079
4080 if (klass == rb_cObject)
4081 rb_warn("already initialized constant %"PRIsVALUE"%"PRIsVALUE"", name, previous);
4082 else
4083 rb_warn("already initialized constant %"PRIsVALUE"::%"PRIsVALUE"%"PRIsVALUE"",
4084 rb_class_name(klass), name, previous);
4085 }
4087 setup_const_entry(ce, klass, val, visibility);
4088 }
4089 else {
4090 tbl = RCLASS_WRITABLE_CONST_TBL(klass);
4092
4094 rb_id_table_insert(tbl, id, (VALUE)ce);
4095 setup_const_entry(ce, klass, val, visibility);
4096 }
4097}
4098
4099static void
4100setup_const_entry(rb_const_entry_t *ce, VALUE klass, VALUE val,
4101 rb_const_flag_t visibility)
4102{
4103 ce->flag = visibility;
4104 RB_OBJ_WRITE(klass, &ce->value, val);
4105 RB_OBJ_WRITE(klass, &ce->file, rb_source_location(&ce->line));
4106}
4107
4108void
4109rb_define_const(VALUE klass, const char *name, VALUE val)
4110{
4111 ID id = rb_intern(name);
4112
4113 if (!rb_is_const_id(id)) {
4114 rb_warn("rb_define_const: invalid name '%s' for constant", name);
4115 }
4116 if (!RB_SPECIAL_CONST_P(val)) {
4117 rb_vm_register_global_object(val);
4118 }
4119 rb_const_set(klass, id, val);
4120}
4121
4122void
4123rb_define_global_const(const char *name, VALUE val)
4124{
4125 rb_define_const(rb_cObject, name, val);
4126}
4127
4128static void
4129set_const_visibility(VALUE mod, int argc, const VALUE *argv,
4130 rb_const_flag_t flag, rb_const_flag_t mask)
4131{
4132 int i;
4133 rb_const_entry_t *ce;
4134 ID id;
4135
4137 if (argc == 0) {
4138 rb_warning("%"PRIsVALUE" with no argument is just ignored",
4139 QUOTE_ID(rb_frame_callee()));
4140 return;
4141 }
4142
4143 for (i = 0; i < argc; i++) {
4144 struct autoload_const *ac;
4145 VALUE val = argv[i];
4146 id = rb_check_id(&val);
4147 if (!id) {
4148 undefined_constant(mod, val);
4149 }
4150 if ((ce = rb_const_lookup(mod, id))) {
4151 ce->flag &= ~mask;
4152 ce->flag |= flag;
4153 if (UNDEF_P(ce->value)) {
4154 if (autoload_const_value_for_named_constant(mod, id, &ac)) {
4155 ac->flag &= ~mask;
4156 ac->flag |= flag;
4157 }
4158 }
4160 }
4161 else {
4162 undefined_constant(mod, ID2SYM(id));
4163 }
4164 }
4165}
4166
4167void
4168rb_deprecate_constant(VALUE mod, const char *name)
4169{
4170 rb_const_entry_t *ce;
4171 ID id;
4172 long len = strlen(name);
4173
4175 if (!(id = rb_check_id_cstr(name, len, NULL))) {
4176 undefined_constant(mod, rb_fstring_new(name, len));
4177 }
4178 if (!(ce = rb_const_lookup(mod, id))) {
4179 undefined_constant(mod, ID2SYM(id));
4180 }
4181 ce->flag |= CONST_DEPRECATED;
4182}
4183
4184/*
4185 * call-seq:
4186 * mod.private_constant(symbol, ...) => mod
4187 *
4188 * Makes a list of existing constants private.
4189 */
4190
4191VALUE
4192rb_mod_private_constant(int argc, const VALUE *argv, VALUE obj)
4193{
4194 set_const_visibility(obj, argc, argv, CONST_PRIVATE, CONST_VISIBILITY_MASK);
4195 return obj;
4196}
4197
4198/*
4199 * call-seq:
4200 * mod.public_constant(symbol, ...) => mod
4201 *
4202 * Makes a list of existing constants public.
4203 */
4204
4205VALUE
4206rb_mod_public_constant(int argc, const VALUE *argv, VALUE obj)
4207{
4208 set_const_visibility(obj, argc, argv, CONST_PUBLIC, CONST_VISIBILITY_MASK);
4209 return obj;
4210}
4211
4212/*
4213 * call-seq:
4214 * mod.deprecate_constant(symbol, ...) => mod
4215 *
4216 * Makes a list of existing constants deprecated. Attempt
4217 * to refer to them will produce a warning.
4218 *
4219 * module HTTP
4220 * NotFound = Exception.new
4221 * NOT_FOUND = NotFound # previous version of the library used this name
4222 *
4223 * deprecate_constant :NOT_FOUND
4224 * end
4225 *
4226 * HTTP::NOT_FOUND
4227 * # warning: constant HTTP::NOT_FOUND is deprecated
4228 *
4229 */
4230
4231VALUE
4232rb_mod_deprecate_constant(int argc, const VALUE *argv, VALUE obj)
4233{
4234 set_const_visibility(obj, argc, argv, CONST_DEPRECATED, CONST_DEPRECATED);
4235 return obj;
4236}
4237
4238static VALUE
4239original_module(VALUE c)
4240{
4241 if (RB_TYPE_P(c, T_ICLASS))
4242 return RBASIC(c)->klass;
4243 return c;
4244}
4245
4246static int
4247cvar_lookup_at(VALUE klass, ID id, st_data_t *v)
4248{
4249 if (RB_TYPE_P(klass, T_ICLASS)) {
4250 if (RICLASS_IS_ORIGIN_P(klass)) {
4251 return 0;
4252 }
4253 else {
4254 // check the original module
4255 klass = RBASIC(klass)->klass;
4256 }
4257 }
4258
4259 VALUE n = rb_ivar_lookup(klass, id, Qundef);
4260 if (UNDEF_P(n)) return 0;
4261
4262 if (v) *v = n;
4263 return 1;
4264}
4265
4266static VALUE
4267cvar_front_klass(VALUE klass)
4268{
4269 if (RCLASS_SINGLETON_P(klass)) {
4270 VALUE obj = RCLASS_ATTACHED_OBJECT(klass);
4271 if (rb_namespace_p(obj)) {
4272 return obj;
4273 }
4274 }
4275 return RCLASS_SUPER(klass);
4276}
4277
4278static void
4279cvar_overtaken(VALUE front, VALUE target, ID id)
4280{
4281 if (front && target != front) {
4282 if (original_module(front) != original_module(target)) {
4283 rb_raise(rb_eRuntimeError,
4284 "class variable % "PRIsVALUE" of %"PRIsVALUE" is overtaken by %"PRIsVALUE"",
4285 ID2SYM(id), rb_class_name(original_module(front)),
4286 rb_class_name(original_module(target)));
4287 }
4288 if (BUILTIN_TYPE(front) == T_CLASS) {
4289 rb_ivar_delete(front, id, Qundef);
4290 }
4291 }
4292}
4293
4294#define CVAR_FOREACH_ANCESTORS(klass, v, r) \
4295 for (klass = cvar_front_klass(klass); klass; klass = RCLASS_SUPER(klass)) { \
4296 if (cvar_lookup_at(klass, id, (v))) { \
4297 r; \
4298 } \
4299 }
4300
4301#define CVAR_LOOKUP(v,r) do {\
4302 if (cvar_lookup_at(klass, id, (v))) {r;}\
4303 CVAR_FOREACH_ANCESTORS(klass, v, r);\
4304} while(0)
4305
4306static VALUE
4307find_cvar(VALUE klass, VALUE * front, VALUE * target, ID id)
4308{
4309 VALUE v = Qundef;
4310 CVAR_LOOKUP(&v, {
4311 if (!*front) {
4312 *front = klass;
4313 }
4314 *target = klass;
4315 });
4316
4317 return v;
4318}
4319
4320void
4321rb_cvar_set(VALUE klass, ID id, VALUE val)
4322{
4323 CVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(klass, id);
4324
4325 VALUE tmp, front = 0, target = 0;
4326
4327 tmp = klass;
4328 CVAR_LOOKUP(0, {if (!front) front = klass; target = klass;});
4329 if (target) {
4330 cvar_overtaken(front, target, id);
4331 }
4332 else {
4333 target = tmp;
4334 }
4335
4336 if (RB_TYPE_P(target, T_ICLASS)) {
4337 target = RBASIC(target)->klass;
4338 }
4339 check_before_mod_set(target, id, val, "class variable");
4340
4341 bool new_cvar = rb_class_ivar_set(target, id, val);
4342
4343 VALUE cvc_tbl = RCLASS_WRITABLE_CVC_TBL(target);
4344
4345 struct rb_cvar_class_tbl_entry *ent;
4346 VALUE ent_data;
4347
4348 if (!cvc_tbl || !rb_marked_id_table_lookup(cvc_tbl, id, &ent_data)) {
4349 ent = (struct rb_cvar_class_tbl_entry *)SHAREABLE_IMEMO_NEW(struct rb_cvar_class_tbl_entry, imemo_cvar_entry, 0);
4350 RB_OBJ_WRITE((VALUE)ent, &ent->class_value, target);
4351 RB_OBJ_WRITE((VALUE)ent, &ent->cref, 0);
4352 ent->global_cvar_state = GET_GLOBAL_CVAR_STATE();
4353
4354 VALUE new_cvc_tbl = cvc_tbl;
4355 if (!new_cvc_tbl) {
4356 new_cvc_tbl = rb_marked_id_table_new(2);
4357 }
4358 else if (rb_multi_ractor_p()) {
4359 new_cvc_tbl = rb_marked_id_table_new(rb_marked_id_table_size(cvc_tbl) + 1);
4360 }
4361
4362 rb_marked_id_table_insert(new_cvc_tbl, id, (VALUE)ent);
4363 if (new_cvc_tbl != cvc_tbl) {
4364 RCLASS_WRITE_CVC_TBL(target, new_cvc_tbl);
4365 }
4366 RB_DEBUG_COUNTER_INC(cvar_inline_miss);
4367 }
4368 else {
4369 ent = (void *)ent_data;
4370 ent->global_cvar_state = GET_GLOBAL_CVAR_STATE();
4371 }
4372
4373 // Break the cvar cache if this is a new class variable.
4374 // Existing caches may have resolved this name to a different
4375 // location in the hierarchy, so we must invalidate globally.
4376 if (new_cvar) {
4377 ruby_vm_global_cvar_state++;
4378 }
4379}
4380
4381VALUE
4382rb_cvar_find(VALUE klass, ID id, VALUE *front)
4383{
4384 VALUE target = 0;
4385 VALUE value;
4386
4387 value = find_cvar(klass, front, &target, id);
4388 if (!target) {
4389 rb_name_err_raise("uninitialized class variable %1$s in %2$s",
4390 klass, ID2SYM(id));
4391 }
4392 cvar_overtaken(*front, target, id);
4393 cvar_read_ractor_check(klass, id, value);
4394 return (VALUE)value;
4395}
4396
4397VALUE
4399{
4400 VALUE front = 0;
4401 return rb_cvar_find(klass, id, &front);
4402}
4403
4404VALUE
4406{
4407 if (!klass) return Qfalse;
4408 CVAR_LOOKUP(0,return Qtrue);
4409 return Qfalse;
4410}
4411
4412static ID
4413cv_intern(VALUE klass, const char *name)
4414{
4415 ID id = rb_intern(name);
4416 if (!rb_is_class_id(id)) {
4417 rb_name_err_raise("wrong class variable name %1$s",
4418 klass, rb_str_new_cstr(name));
4419 }
4420 return id;
4421}
4422
4423void
4424rb_cv_set(VALUE klass, const char *name, VALUE val)
4425{
4426 ID id = cv_intern(klass, name);
4427 rb_cvar_set(klass, id, val);
4428}
4429
4430VALUE
4431rb_cv_get(VALUE klass, const char *name)
4432{
4433 ID id = cv_intern(klass, name);
4434 return rb_cvar_get(klass, id);
4435}
4436
4437void
4438rb_define_class_variable(VALUE klass, const char *name, VALUE val)
4439{
4440 rb_cv_set(klass, name, val);
4441}
4442
4443static int
4444cv_i(ID key, VALUE v, st_data_t a)
4445{
4446 st_table *tbl = (st_table *)a;
4447
4448 if (rb_is_class_id(key)) {
4449 st_update(tbl, (st_data_t)key, cv_i_update, 0);
4450 }
4451 return ST_CONTINUE;
4452}
4453
4454static void*
4455mod_cvar_at(VALUE mod, void *data)
4456{
4457 st_table *tbl = data;
4458 if (!tbl) {
4459 tbl = st_init_numtable();
4460 }
4461 mod = original_module(mod);
4462
4463 rb_ivar_foreach(mod, cv_i, (st_data_t)tbl);
4464 return tbl;
4465}
4466
4467static void*
4468mod_cvar_of(VALUE mod, void *data)
4469{
4470 VALUE tmp = mod;
4471 if (RCLASS_SINGLETON_P(mod)) {
4472 if (rb_namespace_p(RCLASS_ATTACHED_OBJECT(mod))) {
4473 data = mod_cvar_at(tmp, data);
4474 tmp = cvar_front_klass(tmp);
4475 }
4476 }
4477 for (;;) {
4478 data = mod_cvar_at(tmp, data);
4479 tmp = RCLASS_SUPER(tmp);
4480 if (!tmp) break;
4481 }
4482 return data;
4483}
4484
4485static int
4486cv_list_i(st_data_t key, st_data_t value, VALUE ary)
4487{
4488 ID sym = (ID)key;
4489 rb_ary_push(ary, ID2SYM(sym));
4490 return ST_CONTINUE;
4491}
4492
4493static VALUE
4494cvar_list(void *data)
4495{
4496 st_table *tbl = data;
4497 VALUE ary;
4498
4499 if (!tbl) return rb_ary_new2(0);
4500 ary = rb_ary_new2(tbl->num_entries);
4501 st_foreach_safe(tbl, cv_list_i, ary);
4502 st_free_table(tbl);
4503
4504 return ary;
4505}
4506
4507/*
4508 * call-seq:
4509 * mod.class_variables(inherit=true) -> array
4510 *
4511 * Returns an array of the names of class variables in <i>mod</i>.
4512 * This includes the names of class variables in any included
4513 * modules, unless the <i>inherit</i> parameter is set to
4514 * <code>false</code>.
4515 *
4516 * class One
4517 * @@var1 = 1
4518 * end
4519 * class Two < One
4520 * @@var2 = 2
4521 * end
4522 * One.class_variables #=> [:@@var1]
4523 * Two.class_variables #=> [:@@var2, :@@var1]
4524 * Two.class_variables(false) #=> [:@@var2]
4525 */
4526
4527VALUE
4528rb_mod_class_variables(int argc, const VALUE *argv, VALUE mod)
4529{
4530 bool inherit = true;
4531 st_table *tbl;
4532
4533 if (rb_check_arity(argc, 0, 1)) inherit = RTEST(argv[0]);
4534 if (inherit) {
4535 tbl = mod_cvar_of(mod, 0);
4536 }
4537 else {
4538 tbl = mod_cvar_at(mod, 0);
4539 }
4540 return cvar_list(tbl);
4541}
4542
4543/*
4544 * call-seq:
4545 * remove_class_variable(sym) -> obj
4546 *
4547 * Removes the named class variable from the receiver, returning that
4548 * variable's value.
4549 *
4550 * class Example
4551 * @@var = 99
4552 * puts remove_class_variable(:@@var)
4553 * p(defined? @@var)
4554 * end
4555 *
4556 * <em>produces:</em>
4557 *
4558 * 99
4559 * nil
4560 */
4561
4562VALUE
4564{
4565 const ID id = id_for_var_message(mod, name, class, "wrong class variable name %1$s");
4566 st_data_t val;
4567
4568 if (!id) {
4569 goto not_defined;
4570 }
4571 rb_check_frozen(mod);
4572 val = rb_ivar_delete(mod, id, Qundef);
4573 if (!UNDEF_P(val)) {
4574 return (VALUE)val;
4575 }
4576 if (rb_cvar_defined(mod, id)) {
4577 rb_name_err_raise("cannot remove %1$s for %2$s", mod, ID2SYM(id));
4578 }
4579 not_defined:
4580 rb_name_err_raise("class variable %1$s not defined for %2$s",
4581 mod, name);
4583}
4584
4585VALUE
4586rb_iv_get(VALUE obj, const char *name)
4587{
4588 ID id = rb_check_id_cstr(name, strlen(name), rb_usascii_encoding());
4589
4590 if (!id) {
4591 return Qnil;
4592 }
4593 return rb_ivar_get(obj, id);
4594}
4595
4596VALUE
4597rb_iv_set(VALUE obj, const char *name, VALUE val)
4598{
4599 ID id = rb_intern(name);
4600
4601 return rb_ivar_set(obj, id, val);
4602}
4603
4604static attr_index_t
4605class_fields_ivar_set(VALUE klass, VALUE fields_obj, ID id, VALUE val, bool concurrent, VALUE *new_fields_obj, bool *new_ivar_out)
4606{
4607 const VALUE original_fields_obj = fields_obj;
4608 fields_obj = original_fields_obj ? original_fields_obj : rb_imemo_fields_new(klass, ROOT_SHAPE_ID, true);
4609
4610 shape_id_t current_shape_id = RBASIC_SHAPE_ID(fields_obj);
4611 shape_id_t next_shape_id = current_shape_id; // for too_complex
4612 if (UNLIKELY(rb_shape_too_complex_p(current_shape_id))) {
4613 goto too_complex;
4614 }
4615
4616 bool new_ivar;
4617 next_shape_id = generic_shape_ivar(fields_obj, id, &new_ivar);
4618
4619 if (UNLIKELY(rb_shape_too_complex_p(next_shape_id))) {
4620 fields_obj = imemo_fields_complex_from_obj(klass, fields_obj, next_shape_id);
4621 goto too_complex;
4622 }
4623
4624 attr_index_t index = RSHAPE_INDEX(next_shape_id);
4625 if (new_ivar && index >= RSHAPE_CAPACITY(current_shape_id)) {
4626 // We allocate a new fields_obj even when concurrency isn't a concern
4627 // so that we're embedded as long as possible.
4628 fields_obj = imemo_fields_copy_append(klass, fields_obj, current_shape_id, next_shape_id, val);
4629 }
4630 else {
4631 VALUE *fields = rb_imemo_fields_ptr(fields_obj);
4632
4633 if (concurrent && original_fields_obj == fields_obj) {
4634 // In the concurrent case, if we're mutating the existing
4635 // fields_obj, we must use an atomic write, because if we're
4636 // adding a new field, the shape_id must be written after the field
4637 // and if we're updating an existing field, we at least need a relaxed
4638 // write to avoid reaping.
4639 RB_OBJ_ATOMIC_WRITE(fields_obj, &fields[index], val);
4640 }
4641 else {
4642 RB_OBJ_WRITE(fields_obj, &fields[index], val);
4643 }
4644
4645 if (new_ivar) {
4646 RBASIC_SET_SHAPE_ID(fields_obj, next_shape_id);
4647 }
4648 }
4649
4650 *new_fields_obj = fields_obj;
4651 *new_ivar_out = new_ivar;
4652 return index;
4653
4654too_complex:
4655 {
4656 if (concurrent && fields_obj == original_fields_obj) {
4657 // In multi-ractor case, we must always work on a copy because
4658 // even if the field already exist, inserting in a st_table may
4659 // cause a rebuild.
4660 fields_obj = rb_imemo_fields_clone(fields_obj);
4661 }
4662
4663 st_table *table = rb_imemo_fields_complex_tbl(fields_obj);
4664 new_ivar = !st_insert(table, (st_data_t)id, (st_data_t)val);
4665 RB_OBJ_WRITTEN(fields_obj, Qundef, val);
4666
4667 if (fields_obj != original_fields_obj) {
4668 RBASIC_SET_SHAPE_ID(fields_obj, next_shape_id);
4669 }
4670 }
4671
4672 *new_fields_obj = fields_obj;
4673 *new_ivar_out = new_ivar;
4674 return ATTR_INDEX_NOT_SET;
4675}
4676
4677static attr_index_t
4678class_ivar_set(VALUE obj, ID id, VALUE val, bool *new_ivar)
4679{
4680 rb_class_ensure_writable(obj);
4681
4682 const VALUE original_fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
4683 VALUE new_fields_obj = 0;
4684
4685 attr_index_t index = class_fields_ivar_set(obj, original_fields_obj, id, val, rb_multi_ractor_p(), &new_fields_obj, new_ivar);
4686
4687 if (new_fields_obj != original_fields_obj) {
4688 RCLASS_WRITABLE_SET_FIELDS_OBJ(obj, new_fields_obj);
4689 }
4690
4691 // TODO: What should we set as the T_CLASS shape_id?
4692 // In most case we can replicate the single `fields_obj` shape
4693 // but in namespaced case? Perhaps INVALID_SHAPE_ID?
4694 RBASIC_SET_SHAPE_ID(obj, RBASIC_SHAPE_ID(new_fields_obj));
4695 return index;
4696}
4697
4698bool
4699rb_class_ivar_set(VALUE obj, ID id, VALUE val)
4700{
4702 rb_check_frozen(obj);
4703
4704 bool new_ivar;
4705 class_ivar_set(obj, id, val, &new_ivar);
4706 return new_ivar;
4707}
4708
4709void
4710rb_fields_tbl_copy(VALUE dst, VALUE src)
4711{
4712 RUBY_ASSERT(rb_type(dst) == rb_type(src));
4714 RUBY_ASSERT(RSHAPE_TYPE_P(RBASIC_SHAPE_ID(dst), SHAPE_ROOT));
4715
4716 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(src);
4717 if (fields_obj) {
4718 RCLASS_WRITABLE_SET_FIELDS_OBJ(dst, rb_imemo_fields_clone(fields_obj));
4719 RBASIC_SET_SHAPE_ID(dst, RBASIC_SHAPE_ID(src));
4720 }
4721}
4722
4723static rb_const_entry_t *
4724const_lookup(struct rb_id_table *tbl, ID id)
4725{
4726 if (tbl) {
4727 VALUE val;
4728 bool r;
4729 RB_VM_LOCKING() {
4730 r = rb_id_table_lookup(tbl, id, &val);
4731 }
4732
4733 if (r) return (rb_const_entry_t *)val;
4734 }
4735 return NULL;
4736}
4737
4739rb_const_lookup(VALUE klass, ID id)
4740{
4741 return const_lookup(RCLASS_CONST_TBL(klass), id);
4742}
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition dllexport.h:45
static VALUE RB_OBJ_FROZEN_RAW(VALUE obj)
This is an implementation detail of RB_OBJ_FROZEN().
Definition fl_type.h:696
static bool RB_FL_ABLE(VALUE obj)
Checks if the object is flaggable.
Definition fl_type.h:381
static void RB_FL_SET_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_SET().
Definition fl_type.h:541
void rb_obj_freeze_inline(VALUE obj)
Prevents further modifications to the given object.
Definition variable.c:2012
static void RB_FL_UNSET_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_UNSET().
Definition fl_type.h:601
@ RUBY_FL_FREEZE
This flag has something to do with data immutability.
Definition fl_type.h:278
void rb_class_modify_check(VALUE klass)
Asserts that klass is not a frozen class.
Definition eval.c:429
void rb_freeze_singleton_class(VALUE attached_object)
This is an implementation detail of RB_OBJ_FREEZE().
Definition class.c:2783
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.
Definition class.c:3153
#define rb_str_new2
Old name of rb_str_new_cstr.
Definition string.h:1676
#define TYPE(_)
Old name of rb_type.
Definition value_type.h:108
#define FL_UNSET_RAW
Old name of RB_FL_UNSET_RAW.
Definition fl_type.h:130
#define FL_USER3
Old name of RUBY_FL_USER3.
Definition fl_type.h:72
#define ALLOC
Old name of RB_ALLOC.
Definition memory.h:400
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
#define Qundef
Old name of RUBY_Qundef.
#define rb_str_cat2
Old name of rb_str_cat_cstr.
Definition string.h:1684
#define UNREACHABLE
Old name of RBIMPL_UNREACHABLE.
Definition assume.h:28
#define T_IMEMO
Old name of RUBY_T_IMEMO.
Definition value_type.h:67
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define T_STRUCT
Old name of RUBY_T_STRUCT.
Definition value_type.h:79
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
Definition fl_type.h:131
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
Definition assume.h:29
#define T_DATA
Old name of RUBY_T_DATA.
Definition value_type.h:60
#define ZALLOC
Old name of RB_ZALLOC.
Definition memory.h:402
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:205
#define T_MODULE
Old name of RUBY_T_MODULE.
Definition value_type.h:70
#define T_ICLASS
Old name of RUBY_T_ICLASS.
Definition value_type.h:66
#define ALLOC_N
Old name of RB_ALLOC_N.
Definition memory.h:399
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:128
#define rb_ary_new3
Old name of rb_ary_new_from_args.
Definition array.h:658
#define FL_USER2
Old name of RUBY_FL_USER2.
Definition fl_type.h:71
#define Qtrue
Old name of RUBY_Qtrue.
#define INT2NUM
Old name of RB_INT2NUM.
Definition int.h:43
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_OBJECT
Old name of RUBY_T_OBJECT.
Definition value_type.h:75
#define NIL_P
Old name of RB_NIL_P.
#define ALLOCV_N
Old name of RB_ALLOCV_N.
Definition memory.h:405
#define T_CLASS
Old name of RUBY_T_CLASS.
Definition value_type.h:58
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition value_type.h:85
#define rb_ary_new2
Old name of rb_ary_new_capa.
Definition array.h:657
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
Definition fl_type.h:126
#define ALLOCV_END
Old name of RB_ALLOCV_END.
Definition memory.h:406
void rb_category_warn(rb_warning_category_t category, const char *fmt,...)
Identical to rb_category_warning(), except it reports unless $VERBOSE is nil.
Definition error.c:477
void rb_name_error(ID id, const char *fmt,...)
Raises an instance of rb_eNameError.
Definition error.c:2341
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1427
void rb_name_error_str(VALUE str, const char *fmt,...)
Identical to rb_name_error(), except it takes a VALUE instead of ID.
Definition error.c:2356
VALUE rb_eNameError
NameError exception.
Definition error.c:1432
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1425
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.
Definition error.c:1413
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
Definition error.c:467
void rb_warning(const char *fmt,...)
Issues a warning.
Definition error.c:498
@ RB_WARN_CATEGORY_DEPRECATED
Warning is for deprecated features.
Definition error.h:48
VALUE rb_cObject
Object class.
Definition object.c:61
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
Definition object.c:95
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:235
VALUE rb_cModule
Module class.
Definition object.c:62
VALUE rb_class_real(VALUE klass)
Finds a "real" class.
Definition object.c:226
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
Definition gc.h:468
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:456
Encoding relates APIs.
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.
Definition symbol.c:1254
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1121
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_hidden_new(long capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Identical to rb_ary_new_from_values(), except it expects exactly two parameters.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition error.h:284
#define st_foreach_safe
Just another name of rb_st_foreach_safe.
Definition hash.h:51
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...
Definition load.c:666
VALUE rb_backref_get(void)
Queries the last match, or Regexp.last_match, or the $~.
Definition vm.c:2043
int rb_is_instance_id(ID id)
Classifies the given ID, then sees if it is an instance variable.
Definition symbol.c:1128
int rb_is_const_id(ID id)
Classifies the given ID, then sees if it is a constant.
Definition symbol.c:1110
int rb_is_class_id(ID id)
Classifies the given ID, then sees if it is a class variable.
Definition symbol.c:1116
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
Definition proc.c:988
VALUE rb_reg_nth_defined(int n, VALUE md)
Identical to rb_reg_nth_match(), except it just returns Boolean.
Definition re.c:1893
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
Definition string.c:3839
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...
Definition string.c:3192
VALUE rb_str_new_frozen(VALUE str)
Creates a frozen copy of the string, if necessary.
Definition string.c:1523
VALUE rb_str_dup(VALUE str)
Duplicates a string.
Definition string.c:1999
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1515
VALUE rb_str_intern(VALUE str)
Identical to rb_to_symbol(), except it assumes the receiver being an instance of RString.
Definition symbol.c:968
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.
Definition variable.c:4563
VALUE rb_obj_instance_variables(VALUE obj)
Resembles Object#instance_variables.
Definition variable.c:2509
VALUE rb_f_untrace_var(int argc, const VALUE *argv)
Deletes the passed tracer from the passed global variable, or if omitted, deletes everything.
Definition variable.c:924
VALUE rb_const_get(VALUE space, ID name)
Identical to rb_const_defined(), except it returns the actual defined value.
Definition variable.c:3534
VALUE rb_const_list(void *)
This is another mysterious API that comes with no documents at all.
Definition variable.c:3778
VALUE rb_path2class(const char *path)
Resolves a Q::W::E::R-style path string to the actual class it points.
Definition variable.c:494
VALUE rb_autoload_p(VALUE space, ID name)
Queries if an autoload is defined at a point.
Definition variable.c:3402
void rb_set_class_path(VALUE klass, VALUE space, const char *name)
Names a class.
Definition variable.c:441
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.
Definition variable.c:2062
VALUE rb_mod_remove_const(VALUE space, VALUE name)
Resembles Module#remove_const.
Definition variable.c:3639
VALUE rb_class_path_cached(VALUE mod)
Just another name of rb_mod_name.
Definition variable.c:389
VALUE rb_f_trace_var(int argc, const VALUE *argv)
Traces a global variable.
Definition variable.c:878
void rb_cvar_set(VALUE klass, ID name, VALUE val)
Assigns a value to a class variable.
Definition variable.c:4321
VALUE rb_cvar_get(VALUE klass, ID name)
Obtains a value from a class variable.
Definition variable.c:4398
VALUE rb_mod_constants(int argc, const VALUE *argv, VALUE recv)
Resembles Module#constants.
Definition variable.c:3810
VALUE rb_cvar_find(VALUE klass, ID name, VALUE *front)
Identical to rb_cvar_get(), except it takes additional "front" pointer.
Definition variable.c:4382
VALUE rb_path_to_class(VALUE path)
Identical to rb_path2class(), except it accepts the path as Ruby's string instead of C's.
Definition variable.c:449
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.
Definition variable.c:1515
void rb_const_set(VALUE space, ID name, VALUE val)
Names a constant.
Definition variable.c:4012
VALUE rb_autoload_load(VALUE space, ID name)
Kicks the autoload procedure as if it was "touched".
Definition variable.c:3364
VALUE rb_mod_name(VALUE mod)
Queries the name of a module.
Definition variable.c:136
VALUE rb_class_name(VALUE obj)
Queries the name of the given object's class.
Definition variable.c:500
VALUE rb_const_get_at(VALUE space, ID name)
Identical to rb_const_defined_at(), except it returns the actual defined value.
Definition variable.c:3540
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.
Definition variable.c:423
void rb_alias_variable(ID dst, ID src)
Aliases a global variable.
Definition variable.c:1166
void rb_define_class_variable(VALUE, const char *, VALUE)
Just another name of rb_cv_set.
Definition variable.c:4438
VALUE rb_obj_remove_instance_variable(VALUE obj, VALUE name)
Resembles Object#remove_instance_variable.
Definition variable.c:2561
void * rb_mod_const_of(VALUE, void *)
This is a variant of rb_mod_const_at().
Definition variable.c:3756
st_index_t rb_ivar_count(VALUE obj)
Number of instance variables defined on an object.
Definition variable.c:2421
void * rb_mod_const_at(VALUE, void *)
This API is mysterious.
Definition variable.c:3741
VALUE rb_const_remove(VALUE space, ID name)
Identical to rb_mod_remove_const(), except it takes the name as ID instead of VALUE.
Definition variable.c:3652
VALUE rb_const_get_from(VALUE space, ID name)
Identical to rb_const_defined_at(), except it returns the actual defined value.
Definition variable.c:3528
VALUE rb_ivar_defined(VALUE obj, ID name)
Queries if the instance variable is defined at the object.
Definition variable.c:2141
VALUE rb_cv_get(VALUE klass, const char *name)
Identical to rb_cvar_get(), except it accepts C's string instead of ID.
Definition variable.c:4431
int rb_const_defined_at(VALUE space, ID name)
Identical to rb_const_defined(), except it doesn't look for parent classes.
Definition variable.c:3872
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.
Definition variable.c:4424
VALUE rb_mod_class_variables(int argc, const VALUE *argv, VALUE recv)
Resembles Module#class_variables.
Definition variable.c:4528
VALUE rb_f_global_variables(void)
Queries the list of global variables.
Definition variable.c:1133
VALUE rb_cvar_defined(VALUE klass, ID name)
Queries if the given class has the given class variable.
Definition variable.c:4405
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
Definition variable.c:380
int rb_const_defined_from(VALUE space, ID name)
Identical to rb_const_defined(), except it returns false for private constants.
Definition variable.c:3860
int rb_const_defined(VALUE space, ID name)
Queries if the constant is defined at the namespace.
Definition variable.c:3866
void rb_free_generic_ivar(VALUE obj)
Frees the list of instance variables.
Definition variable.c:1318
const char * rb_sourcefile(void)
Resembles __FILE__.
Definition vm.c:2080
void rb_clear_constant_cache_for_id(ID id)
Clears the inline constant caches associated with a particular ID.
Definition vm_method.c:329
int rb_obj_respond_to(VALUE obj, ID mid, int private_p)
Identical to rb_respond_to(), except it additionally takes the visibility parameter.
Definition vm_method.c:3458
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
Definition symbol.h:285
VALUE rb_id2sym(ID id)
Allocates an instance of rb_cSymbol that has the given id.
Definition symbol.c:1005
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
Definition symbol.c:1164
ID rb_to_id(VALUE str)
Identical to rb_intern_str(), except it tries to convert the parameter object to an instance of rb_cS...
Definition string.c:12714
rb_gvar_setter_t rb_gvar_var_setter
Definition variable.h:119
rb_gvar_marker_t rb_gvar_var_marker
Definition variable.h:128
void rb_define_global_const(const char *name, VALUE val)
Identical to rb_define_const(), except it defines that of "global", i.e.
Definition variable.c:4123
VALUE rb_gv_get(const char *name)
Obtains a global variable.
Definition variable.c:1091
void rb_define_variable(const char *name, VALUE *var)
"Shares" a global variable between Ruby and C.
Definition variable.c:849
void rb_gvar_marker_t(VALUE *var)
Type that represents a global variable marker function.
Definition variable.h:53
void rb_deprecate_constant(VALUE mod, const char *name)
Asserts that the given constant is deprecated.
Definition variable.c:4168
void rb_gvar_setter_t(VALUE val, ID id, VALUE *data)
Type that represents a global variable setter function.
Definition variable.h:46
rb_gvar_setter_t rb_gvar_val_setter
This is the setter function that backs global variables defined from a ruby script.
Definition variable.h:94
rb_gvar_marker_t rb_gvar_undef_marker
Definition variable.h:80
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...
Definition variable.c:855
rb_gvar_setter_t rb_gvar_readonly_setter
This function just raises rb_eNameError.
Definition variable.h:135
rb_gvar_getter_t rb_gvar_undef_getter
Definition variable.h:62
VALUE rb_gv_set(const char *name, VALUE val)
Assigns to a global variable.
Definition variable.c:1046
rb_gvar_marker_t rb_gvar_val_marker
This is the setter function that backs global variables defined from a ruby script.
Definition variable.h:101
VALUE rb_gvar_getter_t(ID id, VALUE *data)
Type that represents a global variable getter function.
Definition variable.h:37
VALUE rb_iv_get(VALUE obj, const char *name)
Obtains an instance variable.
Definition variable.c:4586
rb_gvar_setter_t rb_gvar_undef_setter
Definition variable.h:71
rb_gvar_getter_t rb_gvar_val_getter
This is the getter function that backs global variables defined from a ruby script.
Definition variable.h:87
VALUE rb_iv_set(VALUE obj, const char *name, VALUE val)
Assigns to an instance variable.
Definition variable.c:4597
rb_gvar_getter_t rb_gvar_var_getter
Definition variable.h:110
int len
Length of the buffer.
Definition io.h:8
static bool rb_ractor_shareable_p(VALUE obj)
Queries if multiple Ractors can share the passed object or not.
Definition ractor.h:249
#define RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
Definition ractor.h:235
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
Definition memory.h:372
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:167
#define MEMMOVE(p1, p2, type, n)
Handy macro to call memmove.
Definition memory.h:384
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.
Definition variable.c:2273
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:51
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
Definition rbasic.h:166
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
#define RCLASS_SUPER
Just another name of rb_class_get_superclass.
Definition rclass.h:44
#define ROBJECT(obj)
Convenient casting macro.
Definition robject.h:43
static VALUE * ROBJECT_FIELDS(VALUE obj)
Queries the instance variables.
Definition robject.h:133
#define StringValue(v)
Ensures that the parameter object is a String.
Definition rstring.h:66
static char * RSTRING_END(VALUE str)
Queries the end of the contents pointer of the string.
Definition rstring.h:409
static bool RTYPEDDATA_P(VALUE obj)
Checks whether the passed object is RTypedData or RData.
Definition rtypeddata.h:669
#define RTYPEDDATA_DATA(v)
Convenient getter macro.
Definition rtypeddata.h:106
#define RUBY_TYPED_FREE_IMMEDIATELY
Macros to see if each corresponding flag is defined.
Definition rtypeddata.h:122
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
Definition rtypeddata.h:531
#define RTYPEDDATA(obj)
Convenient casting macro.
Definition rtypeddata.h:96
#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...
Definition rtypeddata.h:578
const char * rb_class2name(VALUE klass)
Queries the name of the passed class.
Definition variable.c:506
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
Definition variable.c:515
#define RB_NO_KEYWORDS
Do not pass keywords.
Definition scan_args.h:69
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.
Definition stdarg.h:35
C99 shim for <stdbool.h>
Definition variable.c:2379
Internal header for Ruby Box.
Definition box.h:14
Definition constant.h:33
Definition class.h:37
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:229
Definition variable.c:540
Definition st.h:79
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static enum ruby_value_type rb_type(VALUE obj)
Identical to RB_BUILTIN_TYPE(), except it can also accept special constants.
Definition value_type.h:225
static enum ruby_value_type RB_BUILTIN_TYPE(VALUE obj)
Queries the type of the object.
Definition value_type.h:182
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
Definition value_type.h:433
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376