Ruby: variable.c Source File

Ruby 4.0.5p0 (2026-05-20 revision 64336ffd0ee9e1f4c05891695a3d7b49cb709721)

variable.c
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>
16#include "ruby/internal/stdbool.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
46 RUBY_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
122 VALUE
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
135 VALUE
136rb_mod_name(VALUE mod)
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
175 struct sub_temporary_name_args {
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
297 VALUE
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
379 VALUE
380rb_class_path(VALUE klass)
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
388 VALUE
389rb_class_path_cached(VALUE klass)
390{
391 return rb_mod_name(klass);
392}
393
394static VALUE
395no_fallback(VALUE obj, VALUE name)
396{
397 return name;
398}
399
400 VALUE
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
423rb_set_class_path_string(VALUE klass, VALUE under, VALUE name)
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
448 VALUE
449rb_path_to_class(VALUE pathname)
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));
490 UNREACHABLE_RETURN(Qundef);
491}
492
493 VALUE
494rb_path2class(const char *path)
495{
496 return rb_path_to_class(rb_str_new_cstr(path));
497}
498
499 VALUE
500rb_class_name(VALUE klass)
501{
502 return rb_class_path(rb_class_real(klass));
503}
504
505const char *
506rb_class2name(VALUE klass)
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 *
515rb_obj_classname(VALUE obj)
516{
517 return rb_class2name(CLASS_OF(obj));
518}
519
520 struct trace_var {
521 int removed;
522 void (*func)(VALUE arg, VALUE val);
523 VALUE data;
524 struct trace_var *next;
525};
526
527 struct rb_global_variable {
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
540 struct rb_global_entry {
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 xfree(trace);
555 trace = next;
556 }
557 xfree(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 ruby_xfree(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*
635 rb_global_entry(ID id)
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
664 VALUE
665 rb_gvar_undef_getter(ID id, VALUE *_)
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
688 rb_gvar_undef_setter(VALUE val, ID id, VALUE *_)
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
700 rb_gvar_undef_marker(VALUE *var)
701{
702}
703
704 VALUE
705 rb_gvar_val_getter(ID id, VALUE *data)
706{
707 return (VALUE)data;
708}
709
710void
711 rb_gvar_val_setter(VALUE val, ID id, VALUE *_)
712{
713 struct rb_global_variable *var = rb_global_entry(id)->var;
714 var->data = (void*)val;
715}
716
717void
718 rb_gvar_val_marker(VALUE *var)
719{
720 VALUE data = (VALUE)var;
721 if (data) rb_gc_mark_movable(data);
722}
723
724 VALUE
725 rb_gvar_var_getter(ID id, VALUE *var)
726{
727 if (!var) return Qnil;
728 return *var;
729}
730
731void
732 rb_gvar_var_setter(VALUE val, ID id, VALUE *data)
733{
734 *data = val;
735}
736
737void
738 rb_gvar_var_marker(VALUE *var)
739{
740 if (var) rb_gc_mark_maybe(*var);
741}
742
743void
744 rb_gvar_readonly_setter(VALUE v, ID id, VALUE *_)
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
830 rb_define_hooked_variable(
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{
857 rb_define_hooked_variable(name, (VALUE *)var, 0, rb_gvar_readonly_setter);
858}
859
860void
861 rb_define_virtual_variable(
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
877 VALUE
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 xfree(next);
915 }
916 else {
917 trace = next;
918 }
919 }
920 var->trace = t.next;
921}
922
923 VALUE
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
967 struct trace_data {
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
1020 VALUE
1021rb_gvar_set(ID id, VALUE val)
1022{
1023 VALUE retval;
1024 struct rb_global_entry *entry;
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
1045 VALUE
1046rb_gv_set(const char *name, VALUE val)
1047{
1048 return rb_gvar_set(global_id(name), val);
1049}
1050
1051 VALUE
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;
1058 struct rb_global_variable *var;
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
1090 VALUE
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
1103 VALUE
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
1110 rb_gvar_getter_t *
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
1117 rb_gvar_setter_t *
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
1132 VALUE
1133rb_f_global_variables(void)
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
1166rb_alias_variable(ID name1, ID name2)
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 access class variables from non-main Ractors (%"PRIsVALUE" from %"PRIsVALUE")", rb_id2str(id), klass);
1216 }
1217}
1218
1219static inline void
1220ivar_ractor_check(VALUE obj, ID id)
1221{
1222 if (LIKELY(rb_is_instance_id(id)) /* not internal ID */ &&
1223 !RB_OBJ_FROZEN_RAW(obj) &&
1224 UNLIKELY(!rb_ractor_main_p()) &&
1225 UNLIKELY(rb_ractor_shareable_p(obj))) {
1226
1227 rb_raise(rb_eRactorIsolationError, "can not access instance variables of shareable objects from non-main Ractors");
1228 }
1229}
1230
1231static inline struct st_table *
1232generic_fields_tbl_no_ractor_check(void)
1233{
1234 ASSERT_vm_locking();
1235
1236 return generic_fields_tbl_;
1237}
1238
1239struct st_table *
1240rb_generic_fields_tbl_get(void)
1241{
1242 return generic_fields_tbl_;
1243}
1244
1245void
1246rb_mark_generic_ivar(VALUE obj)
1247{
1248 VALUE data;
1249 // Bypass ASSERT_vm_locking() check because marking may happen concurrently with mmtk
1250 if (st_lookup(generic_fields_tbl_, (st_data_t)obj, (st_data_t *)&data)) {
1251 rb_gc_mark_movable(data);
1252 }
1253}
1254
1255 VALUE
1256rb_obj_fields_generic_uncached(VALUE obj)
1257{
1258 VALUE fields_obj = 0;
1259 RB_VM_LOCKING() {
1260 if (!st_lookup(generic_fields_tbl_, (st_data_t)obj, (st_data_t *)&fields_obj)) {
1261 rb_bug("Object is missing entry in generic_fields_tbl");
1262 }
1263 }
1264 return fields_obj;
1265}
1266
1267 VALUE
1268rb_obj_fields(VALUE obj, ID field_name)
1269{
1270 RUBY_ASSERT(!RB_TYPE_P(obj, T_IMEMO));
1271 ivar_ractor_check(obj, field_name);
1272
1273 VALUE fields_obj = 0;
1274 if (rb_shape_obj_has_fields(obj)) {
1275 switch (BUILTIN_TYPE(obj)) {
1276 case T_DATA:
1277 if (LIKELY(RTYPEDDATA_P(obj))) {
1278 fields_obj = RTYPEDDATA(obj)->fields_obj;
1279 break;
1280 }
1281 goto generic_fields;
1282 case T_STRUCT:
1283 if (LIKELY(!FL_TEST_RAW(obj, RSTRUCT_GEN_FIELDS))) {
1284 fields_obj = RSTRUCT_FIELDS_OBJ(obj);
1285 break;
1286 }
1287 goto generic_fields;
1288 default:
1289 generic_fields:
1290 {
1291 rb_execution_context_t *ec = GET_EC();
1292 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) {
1293 fields_obj = ec->gen_fields_cache.fields_obj;
1294 RUBY_ASSERT(fields_obj == rb_obj_fields_generic_uncached(obj));
1295 }
1296 else {
1297 fields_obj = rb_obj_fields_generic_uncached(obj);
1298 ec->gen_fields_cache.fields_obj = fields_obj;
1299 ec->gen_fields_cache.obj = obj;
1300 }
1301 }
1302 }
1303 }
1304 return fields_obj;
1305}
1306
1307void
1308rb_free_generic_ivar(VALUE obj)
1309{
1310 if (rb_obj_gen_fields_p(obj)) {
1311 st_data_t key = (st_data_t)obj, value;
1312 switch (BUILTIN_TYPE(obj)) {
1313 case T_DATA:
1314 if (LIKELY(RTYPEDDATA_P(obj))) {
1315 RB_OBJ_WRITE(obj, &RTYPEDDATA(obj)->fields_obj, 0);
1316 break;
1317 }
1318 goto generic_fields;
1319 case T_STRUCT:
1320 if (LIKELY(!FL_TEST_RAW(obj, RSTRUCT_GEN_FIELDS))) {
1321 RSTRUCT_SET_FIELDS_OBJ(obj, 0);
1322 break;
1323 }
1324 goto generic_fields;
1325 default:
1326 generic_fields:
1327 {
1328 // Other EC may have stale caches, so fields_obj should be
1329 // invalidated and the GC will replace with Qundef
1330 rb_execution_context_t *ec = GET_EC();
1331 if (ec->gen_fields_cache.obj == obj) {
1332 ec->gen_fields_cache.obj = Qundef;
1333 ec->gen_fields_cache.fields_obj = Qundef;
1334 }
1335 RB_VM_LOCKING() {
1336 if (!st_delete(generic_fields_tbl_no_ractor_check(), &key, &value)) {
1337 rb_bug("Object is missing entry in generic_fields_tbl");
1338 }
1339 }
1340 }
1341 }
1342 RBASIC_SET_SHAPE_ID(obj, ROOT_SHAPE_ID);
1343 }
1344}
1345
1346static void
1347rb_obj_set_fields(VALUE obj, VALUE fields_obj, ID field_name, VALUE original_fields_obj)
1348{
1349 ivar_ractor_check(obj, field_name);
1350
1351 if (!fields_obj) {
1352 RUBY_ASSERT(original_fields_obj);
1353 rb_free_generic_ivar(obj);
1354 rb_imemo_fields_clear(original_fields_obj);
1355 return;
1356 }
1357
1358 RUBY_ASSERT(IMEMO_TYPE_P(fields_obj, imemo_fields));
1359 RUBY_ASSERT(!original_fields_obj || IMEMO_TYPE_P(original_fields_obj, imemo_fields));
1360
1361 if (fields_obj != original_fields_obj) {
1362 switch (BUILTIN_TYPE(obj)) {
1363 case T_DATA:
1364 if (LIKELY(RTYPEDDATA_P(obj))) {
1365 RB_OBJ_WRITE(obj, &RTYPEDDATA(obj)->fields_obj, fields_obj);
1366 break;
1367 }
1368 goto generic_fields;
1369 case T_STRUCT:
1370 if (LIKELY(!FL_TEST_RAW(obj, RSTRUCT_GEN_FIELDS))) {
1371 RSTRUCT_SET_FIELDS_OBJ(obj, fields_obj);
1372 break;
1373 }
1374 goto generic_fields;
1375 default:
1376 generic_fields:
1377 {
1378 RB_VM_LOCKING() {
1379 st_insert(generic_fields_tbl_, (st_data_t)obj, (st_data_t)fields_obj);
1380 }
1381 RB_OBJ_WRITTEN(obj, original_fields_obj, fields_obj);
1382
1383 rb_execution_context_t *ec = GET_EC();
1384 if (ec->gen_fields_cache.fields_obj != fields_obj) {
1385 ec->gen_fields_cache.obj = obj;
1386 ec->gen_fields_cache.fields_obj = fields_obj;
1387 }
1388 }
1389 }
1390
1391 if (original_fields_obj) {
1392 // Clear root shape to avoid triggering cleanup such as free_object_id.
1393 rb_imemo_fields_clear(original_fields_obj);
1394 }
1395 }
1396
1397 RBASIC_SET_SHAPE_ID(obj, RBASIC_SHAPE_ID(fields_obj));
1398}
1399
1400void
1401rb_obj_replace_fields(VALUE obj, VALUE fields_obj)
1402{
1403 RB_VM_LOCKING() {
1404 VALUE original_fields_obj = rb_obj_fields_no_ractor_check(obj);
1405 rb_obj_set_fields(obj, fields_obj, 0, original_fields_obj);
1406 }
1407}
1408
1409 VALUE
1410rb_obj_field_get(VALUE obj, shape_id_t target_shape_id)
1411{
1412 RUBY_ASSERT(!SPECIAL_CONST_P(obj));
1413 RUBY_ASSERT(RSHAPE_TYPE_P(target_shape_id, SHAPE_IVAR) || RSHAPE_TYPE_P(target_shape_id, SHAPE_OBJ_ID));
1414
1415 VALUE fields_obj;
1416
1417 switch (BUILTIN_TYPE(obj)) {
1418 case T_CLASS:
1419 case T_MODULE:
1420 fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
1421 break;
1422 case T_OBJECT:
1423 fields_obj = obj;
1424 break;
1425 case T_IMEMO:
1426 RUBY_ASSERT(IMEMO_TYPE_P(obj, imemo_fields));
1427 fields_obj = obj;
1428 break;
1429 default:
1430 fields_obj = rb_obj_fields(obj, RSHAPE_EDGE_NAME(target_shape_id));
1431 break;
1432 }
1433
1434 if (UNLIKELY(rb_shape_too_complex_p(target_shape_id))) {
1435 st_table *fields_hash = rb_imemo_fields_complex_tbl(fields_obj);
1436 VALUE value = Qundef;
1437 st_lookup(fields_hash, RSHAPE_EDGE_NAME(target_shape_id), &value);
1438 RUBY_ASSERT(!UNDEF_P(value));
1439 return value;
1440 }
1441
1442 attr_index_t index = RSHAPE_INDEX(target_shape_id);
1443 return rb_imemo_fields_ptr(fields_obj)[index];
1444}
1445
1446 VALUE
1447rb_ivar_lookup(VALUE obj, ID id, VALUE undef)
1448{
1449 if (SPECIAL_CONST_P(obj)) return undef;
1450
1451 VALUE fields_obj;
1452
1453 switch (BUILTIN_TYPE(obj)) {
1454 case T_CLASS:
1455 case T_MODULE:
1456 {
1457 VALUE val = rb_ivar_lookup(RCLASS_WRITABLE_FIELDS_OBJ(obj), id, undef);
1458 if (val != undef &&
1459 rb_is_instance_id(id) &&
1460 UNLIKELY(!rb_ractor_main_p()) &&
1461 !rb_ractor_shareable_p(val)) {
1462 rb_raise(rb_eRactorIsolationError,
1463 "can not get unshareable values from instance variables of classes/modules from non-main Ractors (%"PRIsVALUE" from %"PRIsVALUE")",
1464 rb_id2str(id), obj);
1465 }
1466 return val;
1467 }
1468 case T_IMEMO:
1469 // Handled like T_OBJECT
1470 RUBY_ASSERT(IMEMO_TYPE_P(obj, imemo_fields));
1471 fields_obj = obj;
1472 break;
1473 case T_OBJECT:
1474 fields_obj = obj;
1475 break;
1476 default:
1477 fields_obj = rb_obj_fields(obj, id);
1478 break;
1479 }
1480
1481 if (!fields_obj) {
1482 return undef;
1483 }
1484
1485 shape_id_t shape_id = RBASIC_SHAPE_ID(fields_obj);
1486
1487 if (UNLIKELY(rb_shape_too_complex_p(shape_id))) {
1488 st_table *iv_table = rb_imemo_fields_complex_tbl(fields_obj);
1489 VALUE val;
1490 if (rb_st_lookup(iv_table, (st_data_t)id, (st_data_t *)&val)) {
1491 return val;
1492 }
1493 return undef;
1494 }
1495
1496 attr_index_t index = 0;
1497 if (rb_shape_get_iv_index(shape_id, id, &index)) {
1498 return rb_imemo_fields_ptr(fields_obj)[index];
1499 }
1500
1501 return undef;
1502}
1503
1504 VALUE
1505rb_ivar_get(VALUE obj, ID id)
1506{
1507 VALUE iv = rb_ivar_lookup(obj, id, Qnil);
1508 RB_DEBUG_COUNTER_INC(ivar_get_base);
1509 return iv;
1510}
1511
1512 VALUE
1513rb_ivar_get_at(VALUE obj, attr_index_t index, ID id)
1514{
1515 RUBY_ASSERT(rb_is_instance_id(id));
1516 // Used by JITs, but never for T_OBJECT.
1517
1518 switch (BUILTIN_TYPE(obj)) {
1519 case T_OBJECT:
1520 UNREACHABLE_RETURN(Qundef);
1521 case T_CLASS:
1522 case T_MODULE:
1523 {
1524 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
1525 VALUE val = rb_imemo_fields_ptr(fields_obj)[index];
1526
1527 if (UNLIKELY(!rb_ractor_main_p()) && !rb_ractor_shareable_p(val)) {
1528 rb_raise(rb_eRactorIsolationError,
1529 "can not get unshareable values from instance variables of classes/modules from non-main Ractors");
1530 }
1531
1532 return val;
1533 }
1534 default:
1535 {
1536 VALUE fields_obj = rb_obj_fields(obj, id);
1537 return rb_imemo_fields_ptr(fields_obj)[index];
1538 }
1539 }
1540}
1541
1542 VALUE
1543rb_ivar_get_at_no_ractor_check(VALUE obj, attr_index_t index)
1544{
1545 // Used by JITs, but never for T_OBJECT.
1546
1547 VALUE fields_obj;
1548 switch (BUILTIN_TYPE(obj)) {
1549 case T_OBJECT:
1550 UNREACHABLE_RETURN(Qundef);
1551 case T_CLASS:
1552 case T_MODULE:
1553 fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
1554 break;
1555 default:
1556 fields_obj = rb_obj_fields_no_ractor_check(obj);
1557 break;
1558 }
1559 return rb_imemo_fields_ptr(fields_obj)[index];
1560}
1561
1562 VALUE
1563rb_attr_get(VALUE obj, ID id)
1564{
1565 return rb_ivar_lookup(obj, id, Qnil);
1566}
1567
1568void rb_obj_copy_fields_to_hash_table(VALUE obj, st_table *table);
1569static VALUE imemo_fields_complex_from_obj(VALUE owner, VALUE source_fields_obj, shape_id_t shape_id);
1570
1571static shape_id_t
1572obj_transition_too_complex(VALUE obj, st_table *table)
1573{
1574 RUBY_ASSERT(!rb_shape_obj_too_complex_p(obj));
1575 shape_id_t shape_id = rb_shape_transition_complex(obj);
1576
1577 switch (BUILTIN_TYPE(obj)) {
1578 case T_OBJECT:
1579 {
1580 VALUE *old_fields = NULL;
1581 if (FL_TEST_RAW(obj, ROBJECT_HEAP)) {
1582 old_fields = ROBJECT_FIELDS(obj);
1583 }
1584 else {
1585 FL_SET_RAW(obj, ROBJECT_HEAP);
1586 }
1587 RBASIC_SET_SHAPE_ID(obj, shape_id);
1588 ROBJECT_SET_FIELDS_HASH(obj, table);
1589 if (old_fields) {
1590 xfree(old_fields);
1591 }
1592 }
1593 break;
1594 case T_CLASS:
1595 case T_MODULE:
1596 case T_IMEMO:
1597 UNREACHABLE;
1598 break;
1599 default:
1600 {
1601 VALUE fields_obj = rb_imemo_fields_new_complex_tbl(obj, table, RB_OBJ_SHAREABLE_P(obj));
1602 RBASIC_SET_SHAPE_ID(fields_obj, shape_id);
1603 rb_obj_replace_fields(obj, fields_obj);
1604 }
1605 }
1606
1607 return shape_id;
1608}
1609
1610// Copy all object fields, including ivars and internal object_id, etc
1611static shape_id_t
1612rb_evict_fields_to_hash(VALUE obj)
1613{
1614 RUBY_ASSERT(!rb_shape_obj_too_complex_p(obj));
1615
1616 st_table *table = st_init_numtable_with_size(RSHAPE_LEN(RBASIC_SHAPE_ID(obj)));
1617 rb_obj_copy_fields_to_hash_table(obj, table);
1618 shape_id_t new_shape_id = obj_transition_too_complex(obj, table);
1619
1620 RUBY_ASSERT(rb_shape_obj_too_complex_p(obj));
1621 return new_shape_id;
1622}
1623
1624void
1625rb_evict_ivars_to_hash(VALUE obj)
1626{
1627 RUBY_ASSERT(!rb_shape_obj_too_complex_p(obj));
1628
1629 st_table *table = st_init_numtable_with_size(rb_ivar_count(obj));
1630
1631 // Evacuate all previous values from shape into id_table
1632 rb_obj_copy_ivs_to_hash_table(obj, table);
1633 obj_transition_too_complex(obj, table);
1634
1635 RUBY_ASSERT(rb_shape_obj_too_complex_p(obj));
1636}
1637
1638static VALUE
1639rb_ivar_delete(VALUE obj, ID id, VALUE undef)
1640{
1641 rb_check_frozen(obj);
1642
1643 VALUE val = undef;
1644 VALUE fields_obj;
1645 bool concurrent = false;
1646 int type = BUILTIN_TYPE(obj);
1647
1648 switch(type) {
1649 case T_CLASS:
1650 case T_MODULE:
1651 IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(id);
1652
1653 fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
1654 if (rb_multi_ractor_p()) {
1655 concurrent = true;
1656 }
1657 break;
1658 case T_OBJECT:
1659 fields_obj = obj;
1660 break;
1661 default: {
1662 fields_obj = rb_obj_fields(obj, id);
1663 break;
1664 }
1665 }
1666
1667 if (!fields_obj) {
1668 return undef;
1669 }
1670
1671 const VALUE original_fields_obj = fields_obj;
1672 if (concurrent) {
1673 fields_obj = rb_imemo_fields_clone(fields_obj);
1674 }
1675
1676 shape_id_t old_shape_id = RBASIC_SHAPE_ID(fields_obj);
1677 shape_id_t removed_shape_id;
1678 shape_id_t next_shape_id = rb_shape_transition_remove_ivar(fields_obj, id, &removed_shape_id);
1679
1680 if (UNLIKELY(rb_shape_too_complex_p(next_shape_id))) {
1681 if (UNLIKELY(!rb_shape_too_complex_p(old_shape_id))) {
1682 if (type == T_OBJECT) {
1683 rb_evict_fields_to_hash(obj);
1684 }
1685 else {
1686 fields_obj = imemo_fields_complex_from_obj(obj, fields_obj, next_shape_id);
1687 }
1688 }
1689 st_data_t key = id;
1690 if (!st_delete(rb_imemo_fields_complex_tbl(fields_obj), &key, (st_data_t *)&val)) {
1691 val = undef;
1692 }
1693 }
1694 else {
1695 if (next_shape_id == old_shape_id) {
1696 return undef;
1697 }
1698
1699 RUBY_ASSERT(removed_shape_id != INVALID_SHAPE_ID);
1700 RUBY_ASSERT(RSHAPE_LEN(next_shape_id) == RSHAPE_LEN(old_shape_id) - 1);
1701
1702 VALUE *fields = rb_imemo_fields_ptr(fields_obj);
1703 attr_index_t removed_index = RSHAPE_INDEX(removed_shape_id);
1704 val = fields[removed_index];
1705
1706 attr_index_t new_fields_count = RSHAPE_LEN(next_shape_id);
1707 if (new_fields_count) {
1708 size_t trailing_fields = new_fields_count - removed_index;
1709
1710 MEMMOVE(&fields[removed_index], &fields[removed_index + 1], VALUE, trailing_fields);
1711 RBASIC_SET_SHAPE_ID(fields_obj, next_shape_id);
1712
1713 if (FL_TEST_RAW(fields_obj, OBJ_FIELD_HEAP) && rb_obj_embedded_size(new_fields_count) <= rb_gc_obj_slot_size(fields_obj)) {
1714 // Re-embed objects when instances become small enough
1715 // This is necessary because YJIT assumes that objects with the same shape
1716 // have the same embeddedness for efficiency (avoid extra checks)
1717 FL_UNSET_RAW(fields_obj, ROBJECT_HEAP);
1718 MEMCPY(rb_imemo_fields_ptr(fields_obj), fields, VALUE, new_fields_count);
1719 xfree(fields);
1720 }
1721 }
1722 else {
1723 fields_obj = 0;
1724 rb_free_generic_ivar(obj);
1725 }
1726 }
1727
1728 RBASIC_SET_SHAPE_ID(obj, next_shape_id);
1729 if (fields_obj != original_fields_obj) {
1730 switch (type) {
1731 case T_OBJECT:
1732 break;
1733 case T_CLASS:
1734 case T_MODULE:
1735 RCLASS_WRITABLE_SET_FIELDS_OBJ(obj, fields_obj);
1736 break;
1737 default:
1738 rb_obj_set_fields(obj, fields_obj, id, original_fields_obj);
1739 break;
1740 }
1741 }
1742
1743 return val;
1744}
1745
1746 VALUE
1747rb_attr_delete(VALUE obj, ID id)
1748{
1749 return rb_ivar_delete(obj, id, Qnil);
1750}
1751
1752void
1753rb_obj_init_too_complex(VALUE obj, st_table *table)
1754{
1755 // This method is meant to be called on newly allocated object.
1756 RUBY_ASSERT(!rb_shape_obj_too_complex_p(obj));
1757 RUBY_ASSERT(rb_shape_canonical_p(RBASIC_SHAPE_ID(obj)));
1758 RUBY_ASSERT(RSHAPE_LEN(RBASIC_SHAPE_ID(obj)) == 0);
1759
1760 obj_transition_too_complex(obj, table);
1761}
1762
1763static int
1764imemo_fields_complex_from_obj_i(ID key, VALUE val, st_data_t arg)
1765{
1766 VALUE fields = (VALUE)arg;
1767 st_table *table = rb_imemo_fields_complex_tbl(fields);
1768
1769 RUBY_ASSERT(!st_lookup(table, (st_data_t)key, NULL));
1770 st_add_direct(table, (st_data_t)key, (st_data_t)val);
1771 RB_OBJ_WRITTEN(fields, Qundef, val);
1772
1773 return ST_CONTINUE;
1774}
1775
1776static VALUE
1777imemo_fields_complex_from_obj(VALUE owner, VALUE source_fields_obj, shape_id_t shape_id)
1778{
1779 attr_index_t len = source_fields_obj ? RSHAPE_LEN(RBASIC_SHAPE_ID(source_fields_obj)) : 0;
1780 VALUE fields_obj = rb_imemo_fields_new_complex(owner, len + 1, RB_OBJ_SHAREABLE_P(owner));
1781
1782 rb_field_foreach(source_fields_obj, imemo_fields_complex_from_obj_i, (st_data_t)fields_obj, false);
1783 RBASIC_SET_SHAPE_ID(fields_obj, shape_id);
1784
1785 return fields_obj;
1786}
1787
1788static VALUE
1789imemo_fields_copy_capa(VALUE owner, VALUE source_fields_obj, attr_index_t new_size)
1790{
1791 VALUE fields_obj = rb_imemo_fields_new(owner, new_size, RB_OBJ_SHAREABLE_P(owner));
1792 if (source_fields_obj) {
1793 attr_index_t fields_count = RSHAPE_LEN(RBASIC_SHAPE_ID(source_fields_obj));
1794 VALUE *fields = rb_imemo_fields_ptr(fields_obj);
1795 MEMCPY(fields, rb_imemo_fields_ptr(source_fields_obj), VALUE, fields_count);
1796 RBASIC_SET_SHAPE_ID(fields_obj, RBASIC_SHAPE_ID(source_fields_obj));
1797 for (attr_index_t i = 0; i < fields_count; i++) {
1798 RB_OBJ_WRITTEN(fields_obj, Qundef, fields[i]);
1799 }
1800 }
1801 return fields_obj;
1802}
1803
1804static VALUE
1805imemo_fields_set(VALUE owner, VALUE fields_obj, shape_id_t target_shape_id, ID field_name, VALUE val, bool concurrent)
1806{
1807 const VALUE original_fields_obj = fields_obj;
1808 shape_id_t current_shape_id = fields_obj ? RBASIC_SHAPE_ID(fields_obj) : ROOT_SHAPE_ID;
1809
1810 if (UNLIKELY(rb_shape_too_complex_p(target_shape_id))) {
1811 if (rb_shape_too_complex_p(current_shape_id)) {
1812 if (concurrent) {
1813 // In multi-ractor case, we must always work on a copy because
1814 // even if the field already exist, inserting in a st_table may
1815 // cause a rebuild.
1816 fields_obj = rb_imemo_fields_clone(fields_obj);
1817 }
1818 }
1819 else {
1820 fields_obj = imemo_fields_complex_from_obj(owner, original_fields_obj, target_shape_id);
1821 current_shape_id = target_shape_id;
1822 }
1823
1824 st_table *table = rb_imemo_fields_complex_tbl(fields_obj);
1825
1826 RUBY_ASSERT(field_name);
1827 st_insert(table, (st_data_t)field_name, (st_data_t)val);
1828 RB_OBJ_WRITTEN(fields_obj, Qundef, val);
1829 RBASIC_SET_SHAPE_ID(fields_obj, target_shape_id);
1830 }
1831 else {
1832 attr_index_t index = RSHAPE_INDEX(target_shape_id);
1833 if (concurrent || index >= RSHAPE_CAPACITY(current_shape_id)) {
1834 fields_obj = imemo_fields_copy_capa(owner, original_fields_obj, RSHAPE_CAPACITY(target_shape_id));
1835 }
1836
1837 VALUE *table = rb_imemo_fields_ptr(fields_obj);
1838 RB_OBJ_WRITE(fields_obj, &table[index], val);
1839
1840 if (RSHAPE_LEN(target_shape_id) > RSHAPE_LEN(current_shape_id)) {
1841 RBASIC_SET_SHAPE_ID(fields_obj, target_shape_id);
1842 }
1843 }
1844
1845 return fields_obj;
1846}
1847
1848static attr_index_t
1849generic_field_set(VALUE obj, shape_id_t target_shape_id, ID field_name, VALUE val)
1850{
1851 if (!field_name) {
1852 field_name = RSHAPE_EDGE_NAME(target_shape_id);
1853 RUBY_ASSERT(field_name);
1854 }
1855
1856 const VALUE original_fields_obj = rb_obj_fields(obj, field_name);
1857 VALUE fields_obj = imemo_fields_set(obj, original_fields_obj, target_shape_id, field_name, val, false);
1858
1859 rb_obj_set_fields(obj, fields_obj, field_name, original_fields_obj);
1860 return rb_shape_too_complex_p(target_shape_id) ? ATTR_INDEX_NOT_SET : RSHAPE_INDEX(target_shape_id);
1861}
1862
1863static shape_id_t
1864generic_shape_ivar(VALUE obj, ID id, bool *new_ivar_out)
1865{
1866 bool new_ivar = false;
1867 shape_id_t current_shape_id = RBASIC_SHAPE_ID(obj);
1868 shape_id_t target_shape_id = current_shape_id;
1869
1870 if (!rb_shape_too_complex_p(current_shape_id)) {
1871 if (!rb_shape_find_ivar(current_shape_id, id, &target_shape_id)) {
1872 if (RSHAPE_LEN(current_shape_id) >= SHAPE_MAX_FIELDS) {
1873 rb_raise(rb_eArgError, "too many instance variables");
1874 }
1875
1876 new_ivar = true;
1877 target_shape_id = rb_shape_transition_add_ivar(obj, id);
1878 }
1879 }
1880
1881 *new_ivar_out = new_ivar;
1882 return target_shape_id;
1883}
1884
1885static attr_index_t
1886generic_ivar_set(VALUE obj, ID id, VALUE val)
1887{
1888 bool dontcare;
1889 shape_id_t target_shape_id = generic_shape_ivar(obj, id, &dontcare);
1890 return generic_field_set(obj, target_shape_id, id, val);
1891}
1892
1893void
1894rb_ensure_iv_list_size(VALUE obj, uint32_t current_len, uint32_t new_capacity)
1895{
1896 RUBY_ASSERT(!rb_shape_obj_too_complex_p(obj));
1897
1898 if (FL_TEST_RAW(obj, ROBJECT_HEAP)) {
1899 REALLOC_N(ROBJECT(obj)->as.heap.fields, VALUE, new_capacity);
1900 }
1901 else {
1902 VALUE *ptr = ROBJECT_FIELDS(obj);
1903 VALUE *newptr = ALLOC_N(VALUE, new_capacity);
1904 MEMCPY(newptr, ptr, VALUE, current_len);
1905 FL_SET_RAW(obj, ROBJECT_HEAP);
1906 ROBJECT(obj)->as.heap.fields = newptr;
1907 }
1908}
1909
1910static int
1911rb_obj_copy_ivs_to_hash_table_i(ID key, VALUE val, st_data_t arg)
1912{
1913 RUBY_ASSERT(!st_lookup((st_table *)arg, (st_data_t)key, NULL));
1914
1915 st_add_direct((st_table *)arg, (st_data_t)key, (st_data_t)val);
1916 return ST_CONTINUE;
1917}
1918
1919void
1920rb_obj_copy_ivs_to_hash_table(VALUE obj, st_table *table)
1921{
1922 rb_ivar_foreach(obj, rb_obj_copy_ivs_to_hash_table_i, (st_data_t)table);
1923}
1924
1925void
1926rb_obj_copy_fields_to_hash_table(VALUE obj, st_table *table)
1927{
1928 rb_field_foreach(obj, rb_obj_copy_ivs_to_hash_table_i, (st_data_t)table, false);
1929}
1930
1931static attr_index_t
1932obj_field_set(VALUE obj, shape_id_t target_shape_id, ID field_name, VALUE val)
1933{
1934 shape_id_t current_shape_id = RBASIC_SHAPE_ID(obj);
1935
1936 if (UNLIKELY(rb_shape_too_complex_p(target_shape_id))) {
1937 if (UNLIKELY(!rb_shape_too_complex_p(current_shape_id))) {
1938 current_shape_id = rb_evict_fields_to_hash(obj);
1939 }
1940
1941 if (RSHAPE_LEN(target_shape_id) > RSHAPE_LEN(current_shape_id)) {
1942 RBASIC_SET_SHAPE_ID(obj, target_shape_id);
1943 }
1944
1945 if (!field_name) {
1946 field_name = RSHAPE_EDGE_NAME(target_shape_id);
1947 RUBY_ASSERT(field_name);
1948 }
1949
1950 st_insert(ROBJECT_FIELDS_HASH(obj), (st_data_t)field_name, (st_data_t)val);
1951 RB_OBJ_WRITTEN(obj, Qundef, val);
1952
1953 return ATTR_INDEX_NOT_SET;
1954 }
1955 else {
1956 attr_index_t index = RSHAPE_INDEX(target_shape_id);
1957
1958 if (index >= RSHAPE_LEN(current_shape_id)) {
1959 if (UNLIKELY(index >= RSHAPE_CAPACITY(current_shape_id))) {
1960 rb_ensure_iv_list_size(obj, RSHAPE_CAPACITY(current_shape_id), RSHAPE_CAPACITY(target_shape_id));
1961 }
1962 RBASIC_SET_SHAPE_ID(obj, target_shape_id);
1963 }
1964
1965 RB_OBJ_WRITE(obj, &ROBJECT_FIELDS(obj)[index], val);
1966
1967 return index;
1968 }
1969}
1970
1971static attr_index_t
1972obj_ivar_set(VALUE obj, ID id, VALUE val)
1973{
1974 bool dontcare;
1975 shape_id_t target_shape_id = generic_shape_ivar(obj, id, &dontcare);
1976 return obj_field_set(obj, target_shape_id, id, val);
1977}
1978
1979/* Set the instance variable +val+ on object +obj+ at ivar name +id+.
1980 * This function only works with T_OBJECT objects, so make sure
1981 * +obj+ is of type T_OBJECT before using this function.
1982 */
1983 VALUE
1984rb_vm_set_ivar_id(VALUE obj, ID id, VALUE val)
1985{
1986 rb_check_frozen(obj);
1987 obj_ivar_set(obj, id, val);
1988 return val;
1989}
1990
1991 void rb_obj_freeze_inline(VALUE x)
1992{
1993 if (RB_FL_ABLE(x)) {
1994 RB_FL_SET_RAW(x, RUBY_FL_FREEZE);
1995 if (TYPE(x) == T_STRING) {
1996 RB_FL_UNSET_RAW(x, FL_USER2 | FL_USER3); // STR_CHILLED
1997 }
1998
1999 RB_SET_SHAPE_ID(x, rb_shape_transition_frozen(x));
2000
2001 if (RBASIC_CLASS(x)) {
2002 rb_freeze_singleton_class(x);
2003 }
2004 }
2005}
2006
2007static attr_index_t class_ivar_set(VALUE obj, ID id, VALUE val, bool *new_ivar);
2008
2009static attr_index_t
2010ivar_set(VALUE obj, ID id, VALUE val)
2011{
2012 RB_DEBUG_COUNTER_INC(ivar_set_base);
2013
2014 switch (BUILTIN_TYPE(obj)) {
2015 case T_OBJECT:
2016 return obj_ivar_set(obj, id, val);
2017 case T_CLASS:
2018 case T_MODULE:
2019 {
2020 IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(id);
2021 bool dontcare;
2022 return class_ivar_set(obj, id, val, &dontcare);
2023 }
2024 default:
2025 return generic_ivar_set(obj, id, val);
2026 }
2027}
2028
2029 VALUE
2030rb_ivar_set(VALUE obj, ID id, VALUE val)
2031{
2032 rb_check_frozen(obj);
2033 ivar_set(obj, id, val);
2034 return val;
2035}
2036
2037attr_index_t
2038rb_ivar_set_index(VALUE obj, ID id, VALUE val)
2039{
2040 return ivar_set(obj, id, val);
2041}
2042
2043void
2044rb_ivar_set_internal(VALUE obj, ID id, VALUE val)
2045{
2046 // should be internal instance variable name (no @ prefix)
2047 VM_ASSERT(!rb_is_instance_id(id));
2048
2049 ivar_set(obj, id, val);
2050}
2051
2052attr_index_t
2053rb_obj_field_set(VALUE obj, shape_id_t target_shape_id, ID field_name, VALUE val)
2054{
2055 switch (BUILTIN_TYPE(obj)) {
2056 case T_OBJECT:
2057 return obj_field_set(obj, target_shape_id, field_name, val);
2058 case T_CLASS:
2059 case T_MODULE:
2060 // The only field is object_id and T_CLASS handle it differently.
2061 rb_bug("Unreachable");
2062 break;
2063 default:
2064 return generic_field_set(obj, target_shape_id, field_name, val);
2065 }
2066}
2067
2068static VALUE
2069ivar_defined0(VALUE obj, ID id)
2070{
2071 attr_index_t index;
2072
2073 if (rb_shape_obj_too_complex_p(obj)) {
2074 VALUE idx;
2075 st_table *table = NULL;
2076 switch (BUILTIN_TYPE(obj)) {
2077 case T_CLASS:
2078 case T_MODULE:
2079 rb_bug("Unreachable");
2080 break;
2081
2082 case T_IMEMO:
2083 RUBY_ASSERT(IMEMO_TYPE_P(obj, imemo_fields));
2084 table = rb_imemo_fields_complex_tbl(obj);
2085 break;
2086
2087 case T_OBJECT:
2088 table = ROBJECT_FIELDS_HASH(obj);
2089 break;
2090
2091 default: {
2092 VALUE fields_obj = rb_obj_fields_no_ractor_check(obj); // defined? doesn't require ractor checks
2093 table = rb_imemo_fields_complex_tbl(fields_obj);
2094 }
2095 }
2096
2097 if (!table || !rb_st_lookup(table, id, &idx)) {
2098 return Qfalse;
2099 }
2100
2101 return Qtrue;
2102 }
2103 else {
2104 return RBOOL(rb_shape_get_iv_index(RBASIC_SHAPE_ID(obj), id, &index));
2105 }
2106}
2107
2108 VALUE
2109rb_ivar_defined(VALUE obj, ID id)
2110{
2111 if (SPECIAL_CONST_P(obj)) return Qfalse;
2112
2113 VALUE defined = Qfalse;
2114 switch (BUILTIN_TYPE(obj)) {
2115 case T_CLASS:
2116 case T_MODULE:
2117 {
2118 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
2119 if (fields_obj) {
2120 defined = ivar_defined0(fields_obj, id);
2121 }
2122 }
2123 break;
2124 default:
2125 defined = ivar_defined0(obj, id);
2126 break;
2127 }
2128 return defined;
2129}
2130
2131 struct iv_itr_data {
2132 VALUE obj;
2133 struct gen_fields_tbl *fields_tbl;
2134 st_data_t arg;
2135 rb_ivar_foreach_callback_func *func;
2136 VALUE *fields;
2137 bool ivar_only;
2138};
2139
2140static int
2141iterate_over_shapes_callback(shape_id_t shape_id, void *data)
2142{
2143 struct iv_itr_data *itr_data = data;
2144
2145 if (itr_data->ivar_only && !RSHAPE_TYPE_P(shape_id, SHAPE_IVAR)) {
2146 return ST_CONTINUE;
2147 }
2148
2149 VALUE *fields;
2150 switch (BUILTIN_TYPE(itr_data->obj)) {
2151 case T_OBJECT:
2152 RUBY_ASSERT(!rb_shape_obj_too_complex_p(itr_data->obj));
2153 fields = ROBJECT_FIELDS(itr_data->obj);
2154 break;
2155 case T_IMEMO:
2156 RUBY_ASSERT(IMEMO_TYPE_P(itr_data->obj, imemo_fields));
2157 RUBY_ASSERT(!rb_shape_obj_too_complex_p(itr_data->obj));
2158
2159 fields = rb_imemo_fields_ptr(itr_data->obj);
2160 break;
2161 default:
2162 rb_bug("Unreachable");
2163 }
2164
2165 VALUE val = fields[RSHAPE_INDEX(shape_id)];
2166 return itr_data->func(RSHAPE_EDGE_NAME(shape_id), val, itr_data->arg);
2167}
2168
2169/*
2170 * Returns a flag to stop iterating depending on the result of +callback+.
2171 */
2172static void
2173iterate_over_shapes(shape_id_t shape_id, rb_ivar_foreach_callback_func *callback, struct iv_itr_data *itr_data)
2174{
2175 rb_shape_foreach_field(shape_id, iterate_over_shapes_callback, itr_data);
2176}
2177
2178static int
2179each_hash_iv(st_data_t id, st_data_t val, st_data_t data)
2180{
2181 struct iv_itr_data * itr_data = (struct iv_itr_data *)data;
2182 rb_ivar_foreach_callback_func *callback = itr_data->func;
2183 if (is_internal_id((ID)id)) {
2184 return ST_CONTINUE;
2185 }
2186 return callback((ID)id, (VALUE)val, itr_data->arg);
2187}
2188
2189static void
2190obj_fields_each(VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg, bool ivar_only)
2191{
2192 struct iv_itr_data itr_data = {
2193 .obj = obj,
2194 .arg = arg,
2195 .func = func,
2196 .ivar_only = ivar_only,
2197 };
2198
2199 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
2200 if (rb_shape_too_complex_p(shape_id)) {
2201 rb_st_foreach(ROBJECT_FIELDS_HASH(obj), each_hash_iv, (st_data_t)&itr_data);
2202 }
2203 else {
2204 itr_data.fields = ROBJECT_FIELDS(obj);
2205 iterate_over_shapes(shape_id, func, &itr_data);
2206 }
2207}
2208
2209static void
2210imemo_fields_each(VALUE fields_obj, rb_ivar_foreach_callback_func *func, st_data_t arg, bool ivar_only)
2211{
2212 IMEMO_TYPE_P(fields_obj, imemo_fields);
2213
2214 struct iv_itr_data itr_data = {
2215 .obj = fields_obj,
2216 .arg = arg,
2217 .func = func,
2218 .ivar_only = ivar_only,
2219 };
2220
2221 shape_id_t shape_id = RBASIC_SHAPE_ID(fields_obj);
2222 if (rb_shape_too_complex_p(shape_id)) {
2223 rb_st_foreach(rb_imemo_fields_complex_tbl(fields_obj), each_hash_iv, (st_data_t)&itr_data);
2224 }
2225 else {
2226 itr_data.fields = rb_imemo_fields_ptr(fields_obj);
2227 iterate_over_shapes(shape_id, func, &itr_data);
2228 }
2229}
2230
2231void
2232rb_copy_generic_ivar(VALUE dest, VALUE obj)
2233{
2234 VALUE new_fields_obj;
2235
2236 rb_check_frozen(dest);
2237
2238 if (!rb_obj_gen_fields_p(obj)) {
2239 return;
2240 }
2241
2242 shape_id_t src_shape_id = rb_obj_shape_id(obj);
2243
2244 VALUE fields_obj = rb_obj_fields_no_ractor_check(obj);
2245 if (fields_obj) {
2246 unsigned long src_num_ivs = rb_ivar_count(fields_obj);
2247 if (!src_num_ivs) {
2248 goto clear;
2249 }
2250
2251 if (rb_shape_too_complex_p(src_shape_id)) {
2252 rb_shape_copy_complex_ivars(dest, obj, src_shape_id, rb_imemo_fields_complex_tbl(fields_obj));
2253 return;
2254 }
2255
2256 shape_id_t dest_shape_id = src_shape_id;
2257 shape_id_t initial_shape_id = rb_obj_shape_id(dest);
2258
2259 if (!rb_shape_canonical_p(src_shape_id)) {
2260 RUBY_ASSERT(RSHAPE_TYPE_P(initial_shape_id, SHAPE_ROOT));
2261
2262 dest_shape_id = rb_shape_rebuild(initial_shape_id, src_shape_id);
2263 if (UNLIKELY(rb_shape_too_complex_p(dest_shape_id))) {
2264 st_table *table = rb_st_init_numtable_with_size(src_num_ivs);
2265 rb_obj_copy_ivs_to_hash_table(obj, table);
2266 rb_obj_init_too_complex(dest, table);
2267 return;
2268 }
2269 }
2270
2271 if (!RSHAPE_LEN(dest_shape_id)) {
2272 RBASIC_SET_SHAPE_ID(dest, dest_shape_id);
2273 return;
2274 }
2275
2276 new_fields_obj = rb_imemo_fields_new(dest, RSHAPE_CAPACITY(dest_shape_id), RB_OBJ_SHAREABLE_P(dest));
2277 VALUE *src_buf = rb_imemo_fields_ptr(fields_obj);
2278 VALUE *dest_buf = rb_imemo_fields_ptr(new_fields_obj);
2279 rb_shape_copy_fields(new_fields_obj, dest_buf, dest_shape_id, src_buf, src_shape_id);
2280 RBASIC_SET_SHAPE_ID(new_fields_obj, dest_shape_id);
2281
2282 rb_obj_replace_fields(dest, new_fields_obj);
2283 }
2284 return;
2285
2286 clear:
2287 rb_free_generic_ivar(dest);
2288}
2289
2290void
2291rb_replace_generic_ivar(VALUE clone, VALUE obj)
2292{
2293 RB_VM_LOCKING() {
2294 st_data_t fields_tbl, obj_data = (st_data_t)obj;
2295 if (st_delete(generic_fields_tbl_, &obj_data, &fields_tbl)) {
2296 st_insert(generic_fields_tbl_, (st_data_t)clone, fields_tbl);
2297 RB_OBJ_WRITTEN(clone, Qundef, fields_tbl);
2298 }
2299 else {
2300 rb_bug("unreachable");
2301 }
2302 }
2303}
2304
2305void
2306rb_field_foreach(VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg, bool ivar_only)
2307{
2308 if (SPECIAL_CONST_P(obj)) return;
2309 switch (BUILTIN_TYPE(obj)) {
2310 case T_IMEMO:
2311 if (IMEMO_TYPE_P(obj, imemo_fields)) {
2312 imemo_fields_each(obj, func, arg, ivar_only);
2313 }
2314 break;
2315 case T_OBJECT:
2316 obj_fields_each(obj, func, arg, ivar_only);
2317 break;
2318 case T_CLASS:
2319 case T_MODULE:
2320 {
2321 IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(0);
2322 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
2323 if (fields_obj) {
2324 imemo_fields_each(fields_obj, func, arg, ivar_only);
2325 }
2326 }
2327 break;
2328 default:
2329 {
2330 VALUE fields_obj = rb_obj_fields_no_ractor_check(obj);
2331 if (fields_obj) {
2332 imemo_fields_each(fields_obj, func, arg, ivar_only);
2333 }
2334 }
2335 break;
2336 }
2337}
2338
2339void
2340 rb_ivar_foreach(VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg)
2341{
2342 rb_field_foreach(obj, func, arg, true);
2343}
2344
2345st_index_t
2346rb_ivar_count(VALUE obj)
2347{
2348 if (SPECIAL_CONST_P(obj)) return 0;
2349
2350 st_index_t iv_count = 0;
2351 switch (BUILTIN_TYPE(obj)) {
2352 case T_OBJECT:
2353 iv_count = ROBJECT_FIELDS_COUNT(obj);
2354 break;
2355
2356 case T_CLASS:
2357 case T_MODULE:
2358 {
2359 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
2360 if (!fields_obj) {
2361 return 0;
2362 }
2363 if (rb_shape_obj_too_complex_p(fields_obj)) {
2364 iv_count = rb_st_table_size(rb_imemo_fields_complex_tbl(fields_obj));
2365 }
2366 else {
2367 iv_count = RBASIC_FIELDS_COUNT(fields_obj);
2368 }
2369 }
2370 break;
2371
2372 case T_IMEMO:
2373 RUBY_ASSERT(IMEMO_TYPE_P(obj, imemo_fields));
2374
2375 if (rb_shape_obj_too_complex_p(obj)) {
2376 iv_count = rb_st_table_size(rb_imemo_fields_complex_tbl(obj));
2377 }
2378 else {
2379 iv_count = RBASIC_FIELDS_COUNT(obj);
2380 }
2381 break;
2382
2383 default:
2384 {
2385 VALUE fields_obj = rb_obj_fields_no_ractor_check(obj);
2386 if (fields_obj) {
2387 if (rb_shape_obj_too_complex_p(fields_obj)) {
2388 rb_st_table_size(rb_imemo_fields_complex_tbl(fields_obj));
2389 }
2390 else {
2391 iv_count = RBASIC_FIELDS_COUNT(obj);
2392 }
2393 }
2394 }
2395 break;
2396 }
2397
2398 if (rb_shape_obj_has_id(obj)) {
2399 iv_count--;
2400 }
2401
2402 return iv_count;
2403}
2404
2405static int
2406ivar_i(ID key, VALUE v, st_data_t a)
2407{
2408 VALUE ary = (VALUE)a;
2409
2410 if (rb_is_instance_id(key)) {
2411 rb_ary_push(ary, ID2SYM(key));
2412 }
2413 return ST_CONTINUE;
2414}
2415
2416/*
2417 * call-seq:
2418 * obj.instance_variables -> array
2419 *
2420 * Returns an array of instance variable names for the receiver. Note
2421 * that simply defining an accessor does not create the corresponding
2422 * instance variable.
2423 *
2424 * class Fred
2425 * attr_accessor :a1
2426 * def initialize
2427 * @iv = 3
2428 * end
2429 * end
2430 * Fred.new.instance_variables #=> [:@iv]
2431 */
2432
2433 VALUE
2434rb_obj_instance_variables(VALUE obj)
2435{
2436 VALUE ary;
2437
2438 ary = rb_ary_new();
2439 rb_ivar_foreach(obj, ivar_i, ary);
2440 return ary;
2441}
2442
2443#define rb_is_constant_id rb_is_const_id
2444#define rb_is_constant_name rb_is_const_name
2445#define id_for_var(obj, name, part, type) \
2446 id_for_var_message(obj, name, type, "'%1$s' is not allowed as "#part" "#type" variable name")
2447#define id_for_var_message(obj, name, type, message) \
2448 check_id_type(obj, &(name), rb_is_##type##_id, rb_is_##type##_name, message, strlen(message))
2449static ID
2450check_id_type(VALUE obj, VALUE *pname,
2451 int (*valid_id_p)(ID), int (*valid_name_p)(VALUE),
2452 const char *message, size_t message_len)
2453{
2454 ID id = rb_check_id(pname);
2455 VALUE name = *pname;
2456
2457 if (id ? !valid_id_p(id) : !valid_name_p(name)) {
2458 rb_name_err_raise_str(rb_fstring_new(message, message_len),
2459 obj, name);
2460 }
2461 return id;
2462}
2463
2464/*
2465 * call-seq:
2466 * obj.remove_instance_variable(symbol) -> obj
2467 * obj.remove_instance_variable(string) -> obj
2468 *
2469 * Removes the named instance variable from <i>obj</i>, returning that
2470 * variable's value. The name can be passed as a symbol or as a string.
2471 *
2472 * class Dummy
2473 * attr_reader :var
2474 * def initialize
2475 * @var = 99
2476 * end
2477 * def remove
2478 * remove_instance_variable(:@var)
2479 * end
2480 * end
2481 * d = Dummy.new
2482 * d.var #=> 99
2483 * d.remove #=> 99
2484 * d.var #=> nil
2485 */
2486
2487 VALUE
2488rb_obj_remove_instance_variable(VALUE obj, VALUE name)
2489{
2490 const ID id = id_for_var(obj, name, an, instance);
2491
2492 // Frozen check comes here because it's expected that we raise a
2493 // NameError (from the id_for_var check) before we raise a FrozenError
2494 rb_check_frozen(obj);
2495
2496 if (id) {
2497 VALUE val = rb_ivar_delete(obj, id, Qundef);
2498
2499 if (!UNDEF_P(val)) return val;
2500 }
2501
2502 rb_name_err_raise("instance variable %1$s not defined",
2503 obj, name);
2504 UNREACHABLE_RETURN(Qnil);
2505}
2506
2507NORETURN(static void uninitialized_constant(VALUE, VALUE));
2508static void
2509uninitialized_constant(VALUE klass, VALUE name)
2510{
2511 if (klass && rb_class_real(klass) != rb_cObject)
2512 rb_name_err_raise("uninitialized constant %2$s::%1$s",
2513 klass, name);
2514 else
2515 rb_name_err_raise("uninitialized constant %1$s",
2516 klass, name);
2517}
2518
2519 VALUE
2520rb_const_missing(VALUE klass, VALUE name)
2521{
2522 VALUE value = rb_funcallv(klass, idConst_missing, 1, &name);
2523 rb_vm_inc_const_missing_count();
2524 return value;
2525}
2526
2527
2528/*
2529 * call-seq:
2530 * mod.const_missing(sym) -> obj
2531 *
2532 * Invoked when a reference is made to an undefined constant in
2533 * <i>mod</i>. It is passed a symbol for the undefined constant, and
2534 * returns a value to be used for that constant. For example, consider:
2535 *
2536 * def Foo.const_missing(name)
2537 * name # return the constant name as Symbol
2538 * end
2539 *
2540 * Foo::UNDEFINED_CONST #=> :UNDEFINED_CONST: symbol returned
2541 *
2542 * As the example above shows, +const_missing+ is not required to create the
2543 * missing constant in <i>mod</i>, though that is often a side-effect. The
2544 * caller gets its return value when triggered. If the constant is also defined,
2545 * further lookups won't hit +const_missing+ and will return the value stored in
2546 * the constant as usual. Otherwise, +const_missing+ will be invoked again.
2547 *
2548 * In the next example, when a reference is made to an undefined constant,
2549 * +const_missing+ attempts to load a file whose path is the lowercase version
2550 * of the constant name (thus class <code>Fred</code> is assumed to be in file
2551 * <code>fred.rb</code>). If defined as a side-effect of loading the file, the
2552 * method returns the value stored in the constant. This implements an autoload
2553 * feature similar to Kernel#autoload and Module#autoload, though it differs in
2554 * important ways.
2555 *
2556 * def Object.const_missing(name)
2557 * @looked_for ||= {}
2558 * str_name = name.to_s
2559 * raise "Constant not found: #{name}" if @looked_for[str_name]
2560 * @looked_for[str_name] = 1
2561 * file = str_name.downcase
2562 * require file
2563 * const_get(name, false)
2564 * end
2565 *
2566 */
2567
2568 VALUE
2569rb_mod_const_missing(VALUE klass, VALUE name)
2570{
2571 rb_execution_context_t *ec = GET_EC();
2572 VALUE ref = ec->private_const_reference;
2573 rb_vm_pop_cfunc_frame();
2574 if (ref) {
2575 ec->private_const_reference = 0;
2576 rb_name_err_raise("private constant %2$s::%1$s referenced", ref, name);
2577 }
2578 uninitialized_constant(klass, name);
2579
2580 UNREACHABLE_RETURN(Qnil);
2581}
2582
2583static void
2584autoload_table_mark(void *ptr)
2585{
2586 rb_mark_tbl_no_pin((st_table *)ptr);
2587}
2588
2589static void
2590autoload_table_free(void *ptr)
2591{
2592 st_free_table((st_table *)ptr);
2593}
2594
2595static size_t
2596autoload_table_memsize(const void *ptr)
2597{
2598 const st_table *tbl = ptr;
2599 return st_memsize(tbl);
2600}
2601
2602static void
2603autoload_table_compact(void *ptr)
2604{
2605 rb_gc_ref_update_table_values_only((st_table *)ptr);
2606}
2607
2608static const rb_data_type_t autoload_table_type = {
2609 "autoload_table",
2610 {autoload_table_mark, autoload_table_free, autoload_table_memsize, autoload_table_compact,},
2611 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
2612};
2613
2614#define check_autoload_table(av) \
2615 (struct st_table *)rb_check_typeddata((av), &autoload_table_type)
2616
2617static VALUE
2618 autoload_data(VALUE mod, ID id)
2619{
2620 struct st_table *tbl;
2621 st_data_t val;
2622
2623 // If we are called with a non-origin ICLASS, fetch the autoload data from
2624 // the original module.
2625 if (RB_TYPE_P(mod, T_ICLASS)) {
2626 if (RICLASS_IS_ORIGIN_P(mod)) {
2627 return 0;
2628 }
2629 else {
2630 mod = RBASIC(mod)->klass;
2631 }
2632 }
2633
2634 RUBY_ASSERT(RB_TYPE_P(mod, T_CLASS) || RB_TYPE_P(mod, T_MODULE));
2635
2636 // Look up the instance variable table for `autoload`, then index into that table with the given constant name `id`.
2637
2638 VALUE tbl_value = rb_ivar_lookup(mod, autoload, Qfalse);
2639 if (!RTEST(tbl_value) || !(tbl = check_autoload_table(tbl_value)) || !st_lookup(tbl, (st_data_t)id, &val)) {
2640 return 0;
2641 }
2642
2643 return (VALUE)val;
2644}
2645
2646// 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`.
2647 struct autoload_const {
2648 // The linked list node of all constants which are loaded by the related autoload feature.
2649 struct ccan_list_node cnode; /* <=> autoload_data.constants */
2650
2651 // The shared "autoload_data" if multiple constants are defined from the same feature.
2652 VALUE autoload_data_value;
2653
2654 // The box object when the autoload is called in a user box
2655 // Otherwise, Qnil means the root box
2656 VALUE box_value;
2657
2658 // The module we are loading a constant into.
2659 VALUE module;
2660
2661 // The name of the constant we are loading.
2662 ID name;
2663
2664 // The value of the constant (after it's loaded).
2665 VALUE value;
2666
2667 // The constant entry flags which need to be re-applied after autoloading the feature.
2668 rb_const_flag_t flag;
2669
2670 // The source file and line number that defined this constant (different from feature path).
2671 VALUE file;
2672 int line;
2673};
2674
2675// 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.
2676 struct autoload_data {
2677 // The feature path to require to load this constant.
2678 VALUE feature;
2679
2680 // The mutex which is protecting autoloading this feature.
2681 VALUE mutex;
2682
2683 // The process fork serial number since the autoload mutex will become invalid on fork.
2684 rb_serial_t fork_gen;
2685
2686 // The linked list of all constants that are going to be loaded by this autoload.
2687 struct ccan_list_head constants; /* <=> autoload_const.cnode */
2688};
2689
2690static void
2691autoload_data_mark_and_move(void *ptr)
2692{
2693 struct autoload_data *p = ptr;
2694
2695 rb_gc_mark_and_move(&p->feature);
2696 rb_gc_mark_and_move(&p->mutex);
2697}
2698
2699static void
2700autoload_data_free(void *ptr)
2701{
2702 struct autoload_data *p = ptr;
2703
2704 struct autoload_const *autoload_const, *next;
2705 ccan_list_for_each_safe(&p->constants, autoload_const, next, cnode) {
2706 ccan_list_del_init(&autoload_const->cnode);
2707 }
2708
2709 ruby_xfree(p);
2710}
2711
2712static size_t
2713autoload_data_memsize(const void *ptr)
2714{
2715 return sizeof(struct autoload_data);
2716}
2717
2718static const rb_data_type_t autoload_data_type = {
2719 "autoload_data",
2720 {autoload_data_mark_and_move, autoload_data_free, autoload_data_memsize, autoload_data_mark_and_move},
2721 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
2722};
2723
2724static void
2725autoload_const_mark_and_move(void *ptr)
2726{
2727 struct autoload_const *ac = ptr;
2728
2729 rb_gc_mark_and_move(&ac->module);
2730 rb_gc_mark_and_move(&ac->autoload_data_value);
2731 rb_gc_mark_and_move(&ac->value);
2732 rb_gc_mark_and_move(&ac->file);
2733 rb_gc_mark_and_move(&ac->box_value);
2734}
2735
2736static size_t
2737autoload_const_memsize(const void *ptr)
2738{
2739 return sizeof(struct autoload_const);
2740}
2741
2742static void
2743autoload_const_free(void *ptr)
2744{
2745 struct autoload_const *autoload_const = ptr;
2746
2747 ccan_list_del(&autoload_const->cnode);
2748 ruby_xfree(ptr);
2749}
2750
2751static const rb_data_type_t autoload_const_type = {
2752 "autoload_const",
2753 {autoload_const_mark_and_move, autoload_const_free, autoload_const_memsize, autoload_const_mark_and_move,},
2754 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
2755};
2756
2757static struct autoload_data *
2758get_autoload_data(VALUE autoload_const_value, struct autoload_const **autoload_const_pointer)
2759{
2760 struct autoload_const *autoload_const = rb_check_typeddata(autoload_const_value, &autoload_const_type);
2761
2762 VALUE autoload_data_value = autoload_const->autoload_data_value;
2763 struct autoload_data *autoload_data = rb_check_typeddata(autoload_data_value, &autoload_data_type);
2764
2765 /* do not reach across stack for ->state after forking: */
2766 if (autoload_data && autoload_data->fork_gen != GET_VM()->fork_gen) {
2767 RB_OBJ_WRITE(autoload_data_value, &autoload_data->mutex, Qnil);
2768 autoload_data->fork_gen = 0;
2769 }
2770
2771 if (autoload_const_pointer) *autoload_const_pointer = autoload_const;
2772
2773 return autoload_data;
2774}
2775
2776 struct autoload_copy_table_data {
2777 VALUE dst_tbl_value;
2778 struct st_table *dst_tbl;
2779 const rb_box_t *box;
2780};
2781
2782static int
2783autoload_copy_table_for_box_i(st_data_t key, st_data_t value, st_data_t arg)
2784{
2785 struct autoload_const *autoload_const;
2786 struct autoload_copy_table_data *data = (struct autoload_copy_table_data *)arg;
2787 struct st_table *tbl = data->dst_tbl;
2788 VALUE tbl_value = data->dst_tbl_value;
2789 const rb_box_t *box = data->box;
2790
2791 VALUE src_value = (VALUE)value;
2792 struct autoload_const *src_const = rb_check_typeddata(src_value, &autoload_const_type);
2793 // autoload_data can be shared between copies because the feature is equal between copies.
2794 VALUE autoload_data_value = src_const->autoload_data_value;
2795 struct autoload_data *autoload_data = rb_check_typeddata(autoload_data_value, &autoload_data_type);
2796
2797 VALUE new_value = TypedData_Make_Struct(0, struct autoload_const, &autoload_const_type, autoload_const);
2798 RB_OBJ_WRITE(new_value, &autoload_const->box_value, rb_get_box_object((rb_box_t *)box));
2799 RB_OBJ_WRITE(new_value, &autoload_const->module, src_const->module);
2800 autoload_const->name = src_const->name;
2801 RB_OBJ_WRITE(new_value, &autoload_const->value, src_const->value);
2802 autoload_const->flag = src_const->flag;
2803 RB_OBJ_WRITE(new_value, &autoload_const->autoload_data_value, autoload_data_value);
2804 ccan_list_add_tail(&autoload_data->constants, &autoload_const->cnode);
2805
2806 st_insert(tbl, (st_data_t)autoload_const->name, (st_data_t)new_value);
2807 RB_OBJ_WRITTEN(tbl_value, Qundef, new_value);
2808
2809 return ST_CONTINUE;
2810}
2811
2812void
2813rb_autoload_copy_table_for_box(st_table *iv_ptr, const rb_box_t *box)
2814{
2815 struct st_table *src_tbl, *dst_tbl;
2816 VALUE src_tbl_value, dst_tbl_value;
2817 if (!rb_st_lookup(iv_ptr, (st_data_t)autoload, (st_data_t *)&src_tbl_value)) {
2818 // the class has no autoload table yet.
2819 return;
2820 }
2821 if (!RTEST(src_tbl_value) || !(src_tbl = check_autoload_table(src_tbl_value))) {
2822 // the __autoload__ ivar value isn't autoload table value.
2823 return;
2824 }
2825 src_tbl = check_autoload_table(src_tbl_value);
2826
2827 dst_tbl_value = TypedData_Wrap_Struct(0, &autoload_table_type, NULL);
2828 RTYPEDDATA_DATA(dst_tbl_value) = dst_tbl = st_init_numtable();
2829
2830 struct autoload_copy_table_data data = {
2831 .dst_tbl_value = dst_tbl_value,
2832 .dst_tbl = dst_tbl,
2833 .box = box,
2834 };
2835
2836 st_foreach(src_tbl, autoload_copy_table_for_box_i, (st_data_t)&data);
2837 st_insert(iv_ptr, (st_data_t)autoload, (st_data_t)dst_tbl_value);
2838}
2839
2840void
2841rb_autoload(VALUE module, ID name, const char *feature)
2842{
2843 if (!feature || !*feature) {
2844 rb_raise(rb_eArgError, "empty feature name");
2845 }
2846
2847 rb_autoload_str(module, name, rb_fstring_cstr(feature));
2848}
2849
2850static void const_set(VALUE klass, ID id, VALUE val);
2851static void const_added(VALUE klass, ID const_name);
2852
2853 struct autoload_arguments {
2854 VALUE module;
2855 ID name;
2856 VALUE feature;
2857 VALUE box_value;
2858};
2859
2860static VALUE
2861autoload_feature_lookup_or_create(VALUE feature, struct autoload_data **autoload_data_pointer)
2862{
2863 RUBY_ASSERT_MUTEX_OWNED(autoload_mutex);
2864 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
2865
2866 VALUE autoload_data_value = rb_hash_aref(autoload_features, feature);
2867 struct autoload_data *autoload_data;
2868
2869 if (NIL_P(autoload_data_value)) {
2870 autoload_data_value = TypedData_Make_Struct(0, struct autoload_data, &autoload_data_type, autoload_data);
2871 RB_OBJ_WRITE(autoload_data_value, &autoload_data->feature, feature);
2872 RB_OBJ_WRITE(autoload_data_value, &autoload_data->mutex, Qnil);
2873 ccan_list_head_init(&autoload_data->constants);
2874
2875 if (autoload_data_pointer) *autoload_data_pointer = autoload_data;
2876
2877 rb_hash_aset(autoload_features, feature, autoload_data_value);
2878 }
2879 else if (autoload_data_pointer) {
2880 *autoload_data_pointer = rb_check_typeddata(autoload_data_value, &autoload_data_type);
2881 }
2882
2883 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
2884 return autoload_data_value;
2885}
2886
2887static VALUE
2888autoload_table_lookup_or_create(VALUE module)
2889{
2890 VALUE autoload_table_value = rb_ivar_lookup(module, autoload, Qfalse);
2891 if (RTEST(autoload_table_value)) {
2892 return autoload_table_value;
2893 }
2894 else {
2895 autoload_table_value = TypedData_Wrap_Struct(0, &autoload_table_type, NULL);
2896 rb_class_ivar_set(module, autoload, autoload_table_value);
2897 RTYPEDDATA_DATA(autoload_table_value) = st_init_numtable();
2898 return autoload_table_value;
2899 }
2900}
2901
2902static VALUE
2903autoload_synchronized(VALUE _arguments)
2904{
2905 struct autoload_arguments *arguments = (struct autoload_arguments *)_arguments;
2906
2907 rb_const_entry_t *constant_entry = rb_const_lookup(arguments->module, arguments->name);
2908 if (constant_entry && !UNDEF_P(constant_entry->value)) {
2909 return Qfalse;
2910 }
2911
2912 // Reset any state associated with any previous constant:
2913 const_set(arguments->module, arguments->name, Qundef);
2914
2915 VALUE autoload_table_value = autoload_table_lookup_or_create(arguments->module);
2916 struct st_table *autoload_table = check_autoload_table(autoload_table_value);
2917
2918 // Ensure the string is uniqued since we use an identity lookup:
2919 VALUE feature = rb_fstring(arguments->feature);
2920
2921 struct autoload_data *autoload_data;
2922 VALUE autoload_data_value = autoload_feature_lookup_or_create(feature, &autoload_data);
2923
2924 {
2925 struct autoload_const *autoload_const;
2926 VALUE autoload_const_value = TypedData_Make_Struct(0, struct autoload_const, &autoload_const_type, autoload_const);
2927 RB_OBJ_WRITE(autoload_const_value, &autoload_const->box_value, arguments->box_value);
2928 RB_OBJ_WRITE(autoload_const_value, &autoload_const->module, arguments->module);
2929 autoload_const->name = arguments->name;
2930 autoload_const->value = Qundef;
2931 autoload_const->flag = CONST_PUBLIC;
2932 RB_OBJ_WRITE(autoload_const_value, &autoload_const->autoload_data_value, autoload_data_value);
2933 ccan_list_add_tail(&autoload_data->constants, &autoload_const->cnode);
2934 st_insert(autoload_table, (st_data_t)arguments->name, (st_data_t)autoload_const_value);
2935 RB_OBJ_WRITTEN(autoload_table_value, Qundef, autoload_const_value);
2936 }
2937
2938 return Qtrue;
2939}
2940
2941void
2942rb_autoload_str(VALUE module, ID name, VALUE feature)
2943{
2944 const rb_box_t *box = rb_current_box();
2945 VALUE current_box_value = rb_get_box_object((rb_box_t *)box);
2946
2947 if (!rb_is_const_id(name)) {
2948 rb_raise(rb_eNameError, "autoload must be constant name: %"PRIsVALUE"", QUOTE_ID(name));
2949 }
2950
2951 Check_Type(feature, T_STRING);
2952 if (!RSTRING_LEN(feature)) {
2953 rb_raise(rb_eArgError, "empty feature name");
2954 }
2955
2956 struct autoload_arguments arguments = {
2957 .module = module,
2958 .name = name,
2959 .feature = feature,
2960 .box_value = current_box_value,
2961 };
2962
2963 VALUE result = rb_mutex_synchronize(autoload_mutex, autoload_synchronized, (VALUE)&arguments);
2964
2965 if (result == Qtrue) {
2966 const_added(module, name);
2967 }
2968}
2969
2970static void
2971autoload_delete(VALUE module, ID name)
2972{
2973 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
2974
2975 st_data_t load = 0, key = name;
2976
2977 RUBY_ASSERT(RB_TYPE_P(module, T_CLASS) || RB_TYPE_P(module, T_MODULE));
2978
2979 VALUE table_value = rb_ivar_lookup(module, autoload, Qfalse);
2980 if (RTEST(table_value)) {
2981 struct st_table *table = check_autoload_table(table_value);
2982
2983 st_delete(table, &key, &load);
2984 RB_OBJ_WRITTEN(table_value, load, Qundef);
2985
2986 /* Qfalse can indicate already deleted */
2987 if (load != Qfalse) {
2988 struct autoload_const *autoload_const;
2989 struct autoload_data *autoload_data = get_autoload_data((VALUE)load, &autoload_const);
2990
2991 VM_ASSERT(autoload_data);
2992 VM_ASSERT(!ccan_list_empty(&autoload_data->constants));
2993
2994 /*
2995 * we must delete here to avoid "already initialized" warnings
2996 * with parallel autoload. Using list_del_init here so list_del
2997 * works in autoload_const_free
2998 */
2999 ccan_list_del_init(&autoload_const->cnode);
3000
3001 if (ccan_list_empty(&autoload_data->constants)) {
3002 rb_hash_delete(autoload_features, autoload_data->feature);
3003 }
3004
3005 // If the autoload table is empty, we can delete it.
3006 if (table->num_entries == 0) {
3007 rb_attr_delete(module, autoload);
3008 }
3009 }
3010 }
3011
3012 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
3013}
3014
3015static int
3016autoload_by_someone_else(struct autoload_data *ele)
3017{
3018 return ele->mutex != Qnil && !rb_mutex_owned_p(ele->mutex);
3019}
3020
3021static VALUE
3022check_autoload_required(VALUE mod, ID id, const char **loadingpath)
3023{
3024 VALUE autoload_const_value = autoload_data(mod, id);
3025 struct autoload_data *autoload_data;
3026 const char *loading;
3027
3028 if (!autoload_const_value || !(autoload_data = get_autoload_data(autoload_const_value, 0))) {
3029 return 0;
3030 }
3031
3032 VALUE feature = autoload_data->feature;
3033
3034 /*
3035 * if somebody else is autoloading, we MUST wait for them, since
3036 * rb_provide_feature can provide a feature before autoload_const_set
3037 * completes. We must wait until autoload_const_set finishes in
3038 * the other thread.
3039 */
3040 if (autoload_by_someone_else(autoload_data)) {
3041 return autoload_const_value;
3042 }
3043
3044 loading = RSTRING_PTR(feature);
3045
3046 if (!rb_feature_provided(loading, &loading)) {
3047 return autoload_const_value;
3048 }
3049
3050 if (loadingpath && loading) {
3051 *loadingpath = loading;
3052 return autoload_const_value;
3053 }
3054
3055 return 0;
3056}
3057
3058static struct autoload_const *autoloading_const_entry(VALUE mod, ID id);
3059
3060int
3061rb_autoloading_value(VALUE mod, ID id, VALUE* value, rb_const_flag_t *flag)
3062{
3063 struct autoload_const *ac = autoloading_const_entry(mod, id);
3064 if (!ac) return FALSE;
3065
3066 if (value) {
3067 *value = ac->value;
3068 }
3069
3070 if (flag) {
3071 *flag = ac->flag;
3072 }
3073
3074 return TRUE;
3075}
3076
3077static int
3078autoload_by_current(struct autoload_data *ele)
3079{
3080 return ele->mutex != Qnil && rb_mutex_owned_p(ele->mutex);
3081}
3082
3083// If there is an autoloading constant and it has been set by the current
3084// execution context, return it. This allows threads which are loading code to
3085// refer to their own autoloaded constants.
3086struct autoload_const *
3087autoloading_const_entry(VALUE mod, ID id)
3088{
3089 VALUE load = autoload_data(mod, id);
3090 struct autoload_data *ele;
3091 struct autoload_const *ac;
3092
3093 // Find the autoloading state:
3094 if (!load || !(ele = get_autoload_data(load, &ac))) {
3095 // Couldn't be found:
3096 return 0;
3097 }
3098
3099 // Check if it's being loaded by the current thread/fiber:
3100 if (autoload_by_current(ele)) {
3101 if (!UNDEF_P(ac->value)) {
3102 return ac;
3103 }
3104 }
3105
3106 return 0;
3107}
3108
3109static int
3110autoload_defined_p(VALUE mod, ID id)
3111{
3112 rb_const_entry_t *ce = rb_const_lookup(mod, id);
3113
3114 // If there is no constant or the constant is not undefined (special marker for autoloading):
3115 if (!ce || !UNDEF_P(ce->value)) {
3116 // We are not autoloading:
3117 return 0;
3118 }
3119
3120 // Otherwise check if there is an autoload in flight right now:
3121 return !rb_autoloading_value(mod, id, NULL, NULL);
3122}
3123
3124static void const_tbl_update(struct autoload_const *, int);
3125
3126 struct autoload_load_arguments {
3127 VALUE module;
3128 ID name;
3129 int flag;
3130
3131 VALUE mutex;
3132
3133 // The specific constant which triggered the autoload code to fire:
3134 struct autoload_const *autoload_const;
3135
3136 // The parent autoload data which is shared between multiple constants:
3137 struct autoload_data *autoload_data;
3138};
3139
3140static VALUE
3141autoload_const_set(struct autoload_const *ac)
3142{
3143 check_before_mod_set(ac->module, ac->name, ac->value, "constant");
3144
3145 RB_VM_LOCKING() {
3146 const_tbl_update(ac, true);
3147 }
3148
3149 return 0; /* ignored */
3150}
3151
3152static VALUE
3153autoload_load_needed(VALUE _arguments)
3154{
3155 struct autoload_load_arguments *arguments = (struct autoload_load_arguments*)_arguments;
3156
3157 const char *loading = 0, *src;
3158
3159 if (!autoload_defined_p(arguments->module, arguments->name)) {
3160 return Qfalse;
3161 }
3162
3163 VALUE autoload_const_value = check_autoload_required(arguments->module, arguments->name, &loading);
3164 if (!autoload_const_value) {
3165 return Qfalse;
3166 }
3167
3168 src = rb_sourcefile();
3169 if (src && loading && strcmp(src, loading) == 0) {
3170 return Qfalse;
3171 }
3172
3173 struct autoload_const *autoload_const;
3174 struct autoload_data *autoload_data;
3175 if (!(autoload_data = get_autoload_data(autoload_const_value, &autoload_const))) {
3176 return Qfalse;
3177 }
3178
3179 if (NIL_P(autoload_data->mutex)) {
3180 RB_OBJ_WRITE(autoload_const->autoload_data_value, &autoload_data->mutex, rb_mutex_new());
3181 autoload_data->fork_gen = GET_VM()->fork_gen;
3182 }
3183 else if (rb_mutex_owned_p(autoload_data->mutex)) {
3184 return Qfalse;
3185 }
3186
3187 arguments->mutex = autoload_data->mutex;
3188 arguments->autoload_const = autoload_const;
3189
3190 return autoload_const_value;
3191}
3192
3193static VALUE
3194autoload_apply_constants(VALUE _arguments)
3195{
3196 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
3197
3198 struct autoload_load_arguments *arguments = (struct autoload_load_arguments*)_arguments;
3199
3200 struct autoload_const *autoload_const = 0; // for ccan_container_off_var()
3201 struct autoload_const *next;
3202
3203 // We use safe iteration here because `autoload_const_set` will eventually invoke
3204 // `autoload_delete` which will remove the constant from the linked list. In theory, once
3205 // the `autoload_data->constants` linked list is empty, we can remove it.
3206
3207 // Iterate over all constants and assign them:
3208 ccan_list_for_each_safe(&arguments->autoload_data->constants, autoload_const, next, cnode) {
3209 if (!UNDEF_P(autoload_const->value)) {
3210 autoload_const_set(autoload_const);
3211 }
3212 }
3213
3214 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
3215
3216 return Qtrue;
3217}
3218
3219static VALUE
3220autoload_feature_require(VALUE _arguments)
3221{
3222 VALUE receiver = rb_vm_top_self();
3223
3224 struct autoload_load_arguments *arguments = (struct autoload_load_arguments*)_arguments;
3225
3226 struct autoload_const *autoload_const = arguments->autoload_const;
3227 VALUE autoload_box_value = autoload_const->box_value;
3228
3229 // We save this for later use in autoload_apply_constants:
3230 arguments->autoload_data = rb_check_typeddata(autoload_const->autoload_data_value, &autoload_data_type);
3231
3232 if (rb_box_available() && BOX_OBJ_P(autoload_box_value))
3233 receiver = autoload_box_value;
3234
3235 /*
3236 * Clear the global cc cache table because the require method can be different from the current
3237 * box's one and it may cause inconsistent cc-cme states.
3238 * For example, the assertion below may fail in gccct_method_search();
3239 * VM_ASSERT(vm_cc_check_cme(cc, rb_callable_method_entry(klass, mid)))
3240 */
3241 rb_gccct_clear_table(Qnil);
3242
3243 VALUE result = rb_funcall(receiver, rb_intern("require"), 1, arguments->autoload_data->feature);
3244
3245 if (RTEST(result)) {
3246 return rb_mutex_synchronize(autoload_mutex, autoload_apply_constants, _arguments);
3247 }
3248 return result;
3249}
3250
3251static VALUE
3252autoload_try_load(VALUE _arguments)
3253{
3254 struct autoload_load_arguments *arguments = (struct autoload_load_arguments*)_arguments;
3255
3256 VALUE result = autoload_feature_require(_arguments);
3257
3258 // After we loaded the feature, if the constant is not defined, we remove it completely:
3259 rb_const_entry_t *ce = rb_const_lookup(arguments->module, arguments->name);
3260
3261 if (!ce || UNDEF_P(ce->value)) {
3262 result = Qfalse;
3263
3264 rb_const_remove(arguments->module, arguments->name);
3265
3266 if (arguments->module == rb_cObject) {
3267 rb_warning(
3268 "Expected %"PRIsVALUE" to define %"PRIsVALUE" but it didn't",
3269 arguments->autoload_data->feature,
3270 ID2SYM(arguments->name)
3271 );
3272 }
3273 else {
3274 rb_warning(
3275 "Expected %"PRIsVALUE" to define %"PRIsVALUE"::%"PRIsVALUE" but it didn't",
3276 arguments->autoload_data->feature,
3277 arguments->module,
3278 ID2SYM(arguments->name)
3279 );
3280 }
3281 }
3282 else {
3283 // Otherwise, it was loaded, copy the flags from the autoload constant:
3284 ce->flag |= arguments->flag;
3285 }
3286
3287 return result;
3288}
3289
3290 VALUE
3291rb_autoload_load(VALUE module, ID name)
3292{
3293 rb_const_entry_t *ce = rb_const_lookup(module, name);
3294
3295 // We bail out as early as possible without any synchronisation:
3296 if (!ce || !UNDEF_P(ce->value)) {
3297 return Qfalse;
3298 }
3299
3300 // At this point, we assume there might be autoloading, so fail if it's ractor:
3301 if (UNLIKELY(!rb_ractor_main_p())) {
3302 return rb_ractor_autoload_load(module, name);
3303 }
3304
3305 // This state is stored on the stack and is used during the autoload process.
3306 struct autoload_load_arguments arguments = {.module = module, .name = name, .mutex = Qnil};
3307
3308 // Figure out whether we can autoload the named constant:
3309 VALUE autoload_const_value = rb_mutex_synchronize(autoload_mutex, autoload_load_needed, (VALUE)&arguments);
3310
3311 // This confirms whether autoloading is required or not:
3312 if (autoload_const_value == Qfalse) return autoload_const_value;
3313
3314 arguments.flag = ce->flag & (CONST_DEPRECATED | CONST_VISIBILITY_MASK);
3315
3316 // Only one thread will enter here at a time:
3317 VALUE result = rb_mutex_synchronize(arguments.mutex, autoload_try_load, (VALUE)&arguments);
3318
3319 // If you don't guard this value, it's possible for the autoload constant to
3320 // be freed by another thread which loads multiple constants, one of which
3321 // resolves to the constant this thread is trying to load, so proteect this
3322 // so that it is not freed until we are done with it in `autoload_try_load`:
3323 RB_GC_GUARD(autoload_const_value);
3324
3325 return result;
3326}
3327
3328 VALUE
3329rb_autoload_p(VALUE mod, ID id)
3330{
3331 return rb_autoload_at_p(mod, id, TRUE);
3332}
3333
3334 VALUE
3335rb_autoload_at_p(VALUE mod, ID id, int recur)
3336{
3337 VALUE load;
3338 struct autoload_data *ele;
3339
3340 while (!autoload_defined_p(mod, id)) {
3341 if (!recur) return Qnil;
3342 mod = RCLASS_SUPER(mod);
3343 if (!mod) return Qnil;
3344 }
3345 load = check_autoload_required(mod, id, 0);
3346 if (!load) return Qnil;
3347 return (ele = get_autoload_data(load, 0)) ? ele->feature : Qnil;
3348}
3349
3350void
3351rb_const_warn_if_deprecated(const rb_const_entry_t *ce, VALUE klass, ID id)
3352{
3353 if (RB_CONST_DEPRECATED_P(ce) &&
3354 rb_warning_category_enabled_p(RB_WARN_CATEGORY_DEPRECATED)) {
3355 if (klass == rb_cObject) {
3356 rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "constant ::%"PRIsVALUE" is deprecated", QUOTE_ID(id));
3357 }
3358 else {
3359 rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "constant %"PRIsVALUE"::%"PRIsVALUE" is deprecated",
3360 rb_class_name(klass), QUOTE_ID(id));
3361 }
3362 }
3363}
3364
3365static VALUE
3366rb_const_get_0(VALUE klass, ID id, int exclude, int recurse, int visibility)
3367{
3368 VALUE found_in;
3369 VALUE c = rb_const_search(klass, id, exclude, recurse, visibility, &found_in);
3370 if (!UNDEF_P(c)) {
3371 if (UNLIKELY(!rb_ractor_main_p())) {
3372 if (!rb_ractor_shareable_p(c)) {
3373 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));
3374 }
3375 }
3376 return c;
3377 }
3378 return rb_const_missing(klass, ID2SYM(id));
3379}
3380
3381static VALUE
3382rb_const_search_from(VALUE klass, ID id, int exclude, int recurse, int visibility, VALUE *found_in)
3383{
3384 VALUE value, current;
3385 bool first_iteration = true;
3386
3387 for (current = klass;
3388 RTEST(current);
3389 current = RCLASS_SUPER(current), first_iteration = false) {
3390 VALUE tmp;
3391 VALUE am = 0;
3392 rb_const_entry_t *ce;
3393
3394 if (!first_iteration && RCLASS_ORIGIN(current) != current) {
3395 // This item in the super chain has an origin iclass
3396 // that comes later in the chain. Skip this item so
3397 // prepended modules take precedence.
3398 continue;
3399 }
3400
3401 // Do lookup in original class or module in case we are at an origin
3402 // iclass in the chain.
3403 tmp = current;
3404 if (BUILTIN_TYPE(tmp) == T_ICLASS) tmp = RBASIC(tmp)->klass;
3405
3406 // Do the lookup. Loop in case of autoload.
3407 while ((ce = rb_const_lookup(tmp, id))) {
3408 if (visibility && RB_CONST_PRIVATE_P(ce)) {
3409 GET_EC()->private_const_reference = tmp;
3410 return Qundef;
3411 }
3412 rb_const_warn_if_deprecated(ce, tmp, id);
3413 value = ce->value;
3414 if (UNDEF_P(value)) {
3415 struct autoload_const *ac;
3416 if (am == tmp) break;
3417 am = tmp;
3418 ac = autoloading_const_entry(tmp, id);
3419 if (ac) {
3420 if (found_in) { *found_in = tmp; }
3421 return ac->value;
3422 }
3423 rb_autoload_load(tmp, id);
3424 continue;
3425 }
3426 if (exclude && tmp == rb_cObject) {
3427 goto not_found;
3428 }
3429 if (found_in) { *found_in = tmp; }
3430 return value;
3431 }
3432 if (!recurse) break;
3433 }
3434
3435 not_found:
3436 GET_EC()->private_const_reference = 0;
3437 return Qundef;
3438}
3439
3440static VALUE
3441rb_const_search(VALUE klass, ID id, int exclude, int recurse, int visibility, VALUE *found_in)
3442{
3443 VALUE value;
3444
3445 if (klass == rb_cObject) exclude = FALSE;
3446 value = rb_const_search_from(klass, id, exclude, recurse, visibility, found_in);
3447 if (!UNDEF_P(value)) return value;
3448 if (exclude) return value;
3449 if (BUILTIN_TYPE(klass) != T_MODULE) return value;
3450 /* search global const too, if klass is a module */
3451 return rb_const_search_from(rb_cObject, id, FALSE, recurse, visibility, found_in);
3452}
3453
3454 VALUE
3455rb_const_get_from(VALUE klass, ID id)
3456{
3457 return rb_const_get_0(klass, id, TRUE, TRUE, FALSE);
3458}
3459
3460 VALUE
3461rb_const_get(VALUE klass, ID id)
3462{
3463 return rb_const_get_0(klass, id, FALSE, TRUE, FALSE);
3464}
3465
3466 VALUE
3467rb_const_get_at(VALUE klass, ID id)
3468{
3469 return rb_const_get_0(klass, id, TRUE, FALSE, FALSE);
3470}
3471
3472 VALUE
3473rb_public_const_get_from(VALUE klass, ID id)
3474{
3475 return rb_const_get_0(klass, id, TRUE, TRUE, TRUE);
3476}
3477
3478 VALUE
3479rb_public_const_get_at(VALUE klass, ID id)
3480{
3481 return rb_const_get_0(klass, id, TRUE, FALSE, TRUE);
3482}
3483
3484NORETURN(static void undefined_constant(VALUE mod, VALUE name));
3485static void
3486undefined_constant(VALUE mod, VALUE name)
3487{
3488 rb_name_err_raise("constant %2$s::%1$s not defined",
3489 mod, name);
3490}
3491
3492static VALUE
3493rb_const_location_from(VALUE klass, ID id, int exclude, int recurse, int visibility)
3494{
3495 while (RTEST(klass)) {
3496 rb_const_entry_t *ce;
3497
3498 while ((ce = rb_const_lookup(klass, id))) {
3499 if (visibility && RB_CONST_PRIVATE_P(ce)) {
3500 return Qnil;
3501 }
3502 if (exclude && klass == rb_cObject) {
3503 goto not_found;
3504 }
3505
3506 if (UNDEF_P(ce->value)) { // autoload
3507 VALUE autoload_const_value = autoload_data(klass, id);
3508 if (RTEST(autoload_const_value)) {
3509 struct autoload_const *autoload_const;
3510 struct autoload_data *autoload_data = get_autoload_data(autoload_const_value, &autoload_const);
3511
3512 if (!UNDEF_P(autoload_const->value) && RTEST(rb_mutex_owned_p(autoload_data->mutex))) {
3513 return rb_assoc_new(autoload_const->file, INT2NUM(autoload_const->line));
3514 }
3515 }
3516 }
3517
3518 if (NIL_P(ce->file)) return rb_ary_new();
3519 return rb_assoc_new(ce->file, INT2NUM(ce->line));
3520 }
3521 if (!recurse) break;
3522 klass = RCLASS_SUPER(klass);
3523 }
3524
3525 not_found:
3526 return Qnil;
3527}
3528
3529static VALUE
3530rb_const_location(VALUE klass, ID id, int exclude, int recurse, int visibility)
3531{
3532 VALUE loc;
3533
3534 if (klass == rb_cObject) exclude = FALSE;
3535 loc = rb_const_location_from(klass, id, exclude, recurse, visibility);
3536 if (!NIL_P(loc)) return loc;
3537 if (exclude) return loc;
3538 if (BUILTIN_TYPE(klass) != T_MODULE) return loc;
3539 /* search global const too, if klass is a module */
3540 return rb_const_location_from(rb_cObject, id, FALSE, recurse, visibility);
3541}
3542
3543 VALUE
3544rb_const_source_location(VALUE klass, ID id)
3545{
3546 return rb_const_location(klass, id, FALSE, TRUE, FALSE);
3547}
3548
3549 VALUE
3550rb_const_source_location_at(VALUE klass, ID id)
3551{
3552 return rb_const_location(klass, id, TRUE, FALSE, FALSE);
3553}
3554
3555/*
3556 * call-seq:
3557 * remove_const(sym) -> obj
3558 *
3559 * Removes the definition of the given constant, returning that
3560 * constant's previous value. If that constant referred to
3561 * a module, this will not change that module's name and can lead
3562 * to confusion.
3563 */
3564
3565 VALUE
3566rb_mod_remove_const(VALUE mod, VALUE name)
3567{
3568 const ID id = id_for_var(mod, name, a, constant);
3569
3570 if (!id) {
3571 undefined_constant(mod, name);
3572 }
3573 return rb_const_remove(mod, id);
3574}
3575
3576static rb_const_entry_t * const_lookup(struct rb_id_table *tbl, ID id);
3577
3578 VALUE
3579rb_const_remove(VALUE mod, ID id)
3580{
3581 VALUE val;
3582 rb_const_entry_t *ce;
3583
3584 rb_check_frozen(mod);
3585
3586 ce = rb_const_lookup(mod, id);
3587
3588 if (!ce) {
3589 if (rb_const_defined_at(mod, id)) {
3590 rb_name_err_raise("cannot remove %2$s::%1$s", mod, ID2SYM(id));
3591 }
3592
3593 undefined_constant(mod, ID2SYM(id));
3594 }
3595
3596 VALUE writable_ce = 0;
3597 if (rb_id_table_lookup(RCLASS_WRITABLE_CONST_TBL(mod), id, &writable_ce)) {
3598 rb_id_table_delete(RCLASS_WRITABLE_CONST_TBL(mod), id);
3599 if ((rb_const_entry_t *)writable_ce != ce) {
3600 xfree((rb_const_entry_t *)writable_ce);
3601 }
3602 }
3603
3604 rb_const_warn_if_deprecated(ce, mod, id);
3605 rb_clear_constant_cache_for_id(id);
3606
3607 val = ce->value;
3608
3609 if (UNDEF_P(val)) {
3610 autoload_delete(mod, id);
3611 val = Qnil;
3612 }
3613
3614 if (ce != const_lookup(RCLASS_PRIME_CONST_TBL(mod), id)) {
3615 ruby_xfree(ce);
3616 }
3617 // else - skip free'ing the ce because it still exists in the prime classext
3618
3619 return val;
3620}
3621
3622static int
3623cv_i_update(st_data_t *k, st_data_t *v, st_data_t a, int existing)
3624{
3625 if (existing) return ST_STOP;
3626 *v = a;
3627 return ST_CONTINUE;
3628}
3629
3630static enum rb_id_table_iterator_result
3631sv_i(ID key, VALUE v, void *a)
3632{
3633 rb_const_entry_t *ce = (rb_const_entry_t *)v;
3634 st_table *tbl = a;
3635
3636 if (rb_is_const_id(key)) {
3637 st_update(tbl, (st_data_t)key, cv_i_update, (st_data_t)ce);
3638 }
3639 return ID_TABLE_CONTINUE;
3640}
3641
3642static enum rb_id_table_iterator_result
3643rb_local_constants_i(ID const_name, VALUE const_value, void *ary)
3644{
3645 if (rb_is_const_id(const_name) && !RB_CONST_PRIVATE_P((rb_const_entry_t *)const_value)) {
3646 rb_ary_push((VALUE)ary, ID2SYM(const_name));
3647 }
3648 return ID_TABLE_CONTINUE;
3649}
3650
3651static VALUE
3652rb_local_constants(VALUE mod)
3653{
3654 struct rb_id_table *tbl = RCLASS_CONST_TBL(mod);
3655 VALUE ary;
3656
3657 if (!tbl) return rb_ary_new2(0);
3658
3659 RB_VM_LOCKING() {
3660 ary = rb_ary_new2(rb_id_table_size(tbl));
3661 rb_id_table_foreach(tbl, rb_local_constants_i, (void *)ary);
3662 }
3663
3664 return ary;
3665}
3666
3667void*
3668rb_mod_const_at(VALUE mod, void *data)
3669{
3670 st_table *tbl = data;
3671 if (!tbl) {
3672 tbl = st_init_numtable();
3673 }
3674 if (RCLASS_CONST_TBL(mod)) {
3675 RB_VM_LOCKING() {
3676 rb_id_table_foreach(RCLASS_CONST_TBL(mod), sv_i, tbl);
3677 }
3678 }
3679 return tbl;
3680}
3681
3682void*
3683rb_mod_const_of(VALUE mod, void *data)
3684{
3685 VALUE tmp = mod;
3686 for (;;) {
3687 data = rb_mod_const_at(tmp, data);
3688 tmp = RCLASS_SUPER(tmp);
3689 if (!tmp) break;
3690 if (tmp == rb_cObject && mod != rb_cObject) break;
3691 }
3692 return data;
3693}
3694
3695static int
3696list_i(st_data_t key, st_data_t value, VALUE ary)
3697{
3698 ID sym = (ID)key;
3699 rb_const_entry_t *ce = (rb_const_entry_t *)value;
3700 if (RB_CONST_PUBLIC_P(ce)) rb_ary_push(ary, ID2SYM(sym));
3701 return ST_CONTINUE;
3702}
3703
3704 VALUE
3705rb_const_list(void *data)
3706{
3707 st_table *tbl = data;
3708 VALUE ary;
3709
3710 if (!tbl) return rb_ary_new2(0);
3711 ary = rb_ary_new2(tbl->num_entries);
3712 st_foreach_safe(tbl, list_i, ary);
3713 st_free_table(tbl);
3714
3715 return ary;
3716}
3717
3718/*
3719 * call-seq:
3720 * mod.constants(inherit=true) -> array
3721 *
3722 * Returns an array of the names of the constants accessible in
3723 * <i>mod</i>. This includes the names of constants in any included
3724 * modules (example at start of section), unless the <i>inherit</i>
3725 * parameter is set to <code>false</code>.
3726 *
3727 * The implementation makes no guarantees about the order in which the
3728 * constants are yielded.
3729 *
3730 * IO.constants.include?(:SYNC) #=> true
3731 * IO.constants(false).include?(:SYNC) #=> false
3732 *
3733 * Also see Module#const_defined?.
3734 */
3735
3736 VALUE
3737rb_mod_constants(int argc, const VALUE *argv, VALUE mod)
3738{
3739 bool inherit = true;
3740
3741 if (rb_check_arity(argc, 0, 1)) inherit = RTEST(argv[0]);
3742
3743 if (inherit) {
3744 return rb_const_list(rb_mod_const_of(mod, 0));
3745 }
3746 else {
3747 return rb_local_constants(mod);
3748 }
3749}
3750
3751static int
3752rb_const_defined_0(VALUE klass, ID id, int exclude, int recurse, int visibility)
3753{
3754 VALUE tmp;
3755 int mod_retry = 0;
3756 rb_const_entry_t *ce;
3757
3758 tmp = klass;
3759 retry:
3760 while (tmp) {
3761 if ((ce = rb_const_lookup(tmp, id))) {
3762 if (visibility && RB_CONST_PRIVATE_P(ce)) {
3763 return (int)Qfalse;
3764 }
3765 if (UNDEF_P(ce->value) && !check_autoload_required(tmp, id, 0) &&
3766 !rb_autoloading_value(tmp, id, NULL, NULL))
3767 return (int)Qfalse;
3768
3769 if (exclude && tmp == rb_cObject && klass != rb_cObject) {
3770 return (int)Qfalse;
3771 }
3772
3773 return (int)Qtrue;
3774 }
3775 if (!recurse) break;
3776 tmp = RCLASS_SUPER(tmp);
3777 }
3778 if (!exclude && !mod_retry && BUILTIN_TYPE(klass) == T_MODULE) {
3779 mod_retry = 1;
3780 tmp = rb_cObject;
3781 goto retry;
3782 }
3783 return (int)Qfalse;
3784}
3785
3786int
3787rb_const_defined_from(VALUE klass, ID id)
3788{
3789 return rb_const_defined_0(klass, id, TRUE, TRUE, FALSE);
3790}
3791
3792int
3793rb_const_defined(VALUE klass, ID id)
3794{
3795 return rb_const_defined_0(klass, id, FALSE, TRUE, FALSE);
3796}
3797
3798int
3799rb_const_defined_at(VALUE klass, ID id)
3800{
3801 return rb_const_defined_0(klass, id, TRUE, FALSE, FALSE);
3802}
3803
3804int
3805rb_public_const_defined_from(VALUE klass, ID id)
3806{
3807 return rb_const_defined_0(klass, id, TRUE, TRUE, TRUE);
3808}
3809
3810static void
3811check_before_mod_set(VALUE klass, ID id, VALUE val, const char *dest)
3812{
3813 rb_check_frozen(klass);
3814}
3815
3816static void set_namespace_path(VALUE named_namespace, VALUE name);
3817
3818static enum rb_id_table_iterator_result
3819set_namespace_path_i(ID id, VALUE v, void *payload)
3820{
3821 rb_const_entry_t *ce = (rb_const_entry_t *)v;
3822 VALUE value = ce->value;
3823 VALUE parental_path = *((VALUE *) payload);
3824 if (!rb_is_const_id(id) || !rb_namespace_p(value)) {
3825 return ID_TABLE_CONTINUE;
3826 }
3827
3828 bool has_permanent_classpath;
3829 classname(value, &has_permanent_classpath);
3830 if (has_permanent_classpath) {
3831 return ID_TABLE_CONTINUE;
3832 }
3833 set_namespace_path(value, build_const_path(parental_path, id));
3834
3835 if (!RCLASS_PERMANENT_CLASSPATH_P(value)) {
3836 RCLASS_WRITE_CLASSPATH(value, 0, false);
3837 }
3838
3839 return ID_TABLE_CONTINUE;
3840}
3841
3842/*
3843 * Assign permanent classpaths to all namespaces that are directly or indirectly
3844 * nested under +named_namespace+. +named_namespace+ must have a permanent
3845 * classpath.
3846 */
3847static void
3848set_namespace_path(VALUE named_namespace, VALUE namespace_path)
3849{
3850 struct rb_id_table *const_table = RCLASS_CONST_TBL(named_namespace);
3851 RB_OBJ_SET_SHAREABLE(namespace_path);
3852
3853 RB_VM_LOCKING() {
3854 RCLASS_WRITE_CLASSPATH(named_namespace, namespace_path, true);
3855
3856 if (const_table) {
3857 rb_id_table_foreach(const_table, set_namespace_path_i, &namespace_path);
3858 }
3859 }
3860}
3861
3862static void
3863const_added(VALUE klass, ID const_name)
3864{
3865 if (GET_VM()->running) {
3866 VALUE name = ID2SYM(const_name);
3867 rb_funcallv(klass, idConst_added, 1, &name);
3868 }
3869}
3870
3871static void
3872const_set(VALUE klass, ID id, VALUE val)
3873{
3874 rb_const_entry_t *ce;
3875
3876 if (NIL_P(klass)) {
3877 rb_raise(rb_eTypeError, "no class/module to define constant %"PRIsVALUE"",
3878 QUOTE_ID(id));
3879 }
3880
3881 if (!rb_ractor_main_p() && !rb_ractor_shareable_p(val)) {
3882 rb_raise(rb_eRactorIsolationError, "can not set constants with non-shareable objects by non-main Ractors");
3883 }
3884
3885 check_before_mod_set(klass, id, val, "constant");
3886
3887 RB_VM_LOCKING() {
3888 struct rb_id_table *tbl = RCLASS_WRITABLE_CONST_TBL(klass);
3889 if (!tbl) {
3890 tbl = rb_id_table_create(0);
3891 RCLASS_WRITE_CONST_TBL(klass, tbl, false);
3892 rb_clear_constant_cache_for_id(id);
3893 ce = ZALLOC(rb_const_entry_t);
3894 rb_id_table_insert(tbl, id, (VALUE)ce);
3895 setup_const_entry(ce, klass, val, CONST_PUBLIC);
3896 }
3897 else {
3898 struct autoload_const ac = {
3899 .module = klass, .name = id,
3900 .value = val, .flag = CONST_PUBLIC,
3901 /* fill the rest with 0 */
3902 };
3903 ac.file = rb_source_location(&ac.line);
3904 const_tbl_update(&ac, false);
3905 }
3906 }
3907
3908 /*
3909 * Resolve and cache class name immediately to resolve ambiguity
3910 * and avoid order-dependency on const_tbl
3911 */
3912 if (rb_cObject && rb_namespace_p(val)) {
3913 bool val_path_permanent;
3914 VALUE val_path = classname(val, &val_path_permanent);
3915 if (NIL_P(val_path) || !val_path_permanent) {
3916 if (klass == rb_cObject) {
3917 set_namespace_path(val, rb_id2str(id));
3918 }
3919 else {
3920 bool parental_path_permanent;
3921 VALUE parental_path = classname(klass, &parental_path_permanent);
3922 if (NIL_P(parental_path)) {
3923 bool throwaway;
3924 parental_path = rb_tmp_class_path(klass, &throwaway, make_temporary_path);
3925 }
3926 if (parental_path_permanent && !val_path_permanent) {
3927 set_namespace_path(val, build_const_path(parental_path, id));
3928 }
3929 else if (!parental_path_permanent && NIL_P(val_path)) {
3930 VALUE path = build_const_path(parental_path, id);
3931 RCLASS_SET_CLASSPATH(val, path, false);
3932 }
3933 }
3934 }
3935 }
3936}
3937
3938void
3939rb_const_set(VALUE klass, ID id, VALUE val)
3940{
3941 const_set(klass, id, val);
3942 const_added(klass, id);
3943}
3944
3945static VALUE
3946autoload_const_value_for_named_constant(VALUE module, ID name, struct autoload_const **autoload_const_pointer)
3947{
3948 VALUE autoload_const_value = autoload_data(module, name);
3949 if (!autoload_const_value) return Qfalse;
3950
3951 struct autoload_data *autoload_data = get_autoload_data(autoload_const_value, autoload_const_pointer);
3952 if (!autoload_data) return Qfalse;
3953
3954 /* for autoloading thread, keep the defined value to autoloading storage */
3955 if (autoload_by_current(autoload_data)) {
3956 return autoload_const_value;
3957 }
3958
3959 return Qfalse;
3960}
3961
3962static void
3963const_tbl_update(struct autoload_const *ac, int autoload_force)
3964{
3965 VALUE value;
3966 VALUE klass = ac->module;
3967 VALUE val = ac->value;
3968 ID id = ac->name;
3969 struct rb_id_table *tbl = RCLASS_CONST_TBL(klass);
3970 rb_const_flag_t visibility = ac->flag;
3971 rb_const_entry_t *ce;
3972
3973 if (rb_id_table_lookup(tbl, id, &value)) {
3974 ce = (rb_const_entry_t *)value;
3975 if (UNDEF_P(ce->value)) {
3976 RUBY_ASSERT_CRITICAL_SECTION_ENTER();
3977 VALUE file = ac->file;
3978 int line = ac->line;
3979 VALUE autoload_const_value = autoload_const_value_for_named_constant(klass, id, &ac);
3980
3981 if (!autoload_force && autoload_const_value) {
3982 rb_clear_constant_cache_for_id(id);
3983
3984 RB_OBJ_WRITE(autoload_const_value, &ac->value, val);
3985 RB_OBJ_WRITE(autoload_const_value, &ac->file, rb_source_location(&ac->line));
3986 }
3987 else {
3988 /* otherwise autoloaded constant, allow to override */
3989 autoload_delete(klass, id);
3990 ce->flag = visibility;
3991 RB_OBJ_WRITE(klass, &ce->value, val);
3992 RB_OBJ_WRITE(klass, &ce->file, file);
3993 ce->line = line;
3994 }
3995 RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
3996 return;
3997 }
3998 else {
3999 VALUE name = QUOTE_ID(id);
4000 visibility = ce->flag;
4001 if (klass == rb_cObject)
4002 rb_warn("already initialized constant %"PRIsVALUE"", name);
4003 else
4004 rb_warn("already initialized constant %"PRIsVALUE"::%"PRIsVALUE"",
4005 rb_class_name(klass), name);
4006 if (!NIL_P(ce->file) && ce->line) {
4007 rb_compile_warn(RSTRING_PTR(ce->file), ce->line,
4008 "previous definition of %"PRIsVALUE" was here", name);
4009 }
4010 }
4011 rb_clear_constant_cache_for_id(id);
4012 setup_const_entry(ce, klass, val, visibility);
4013 }
4014 else {
4015 tbl = RCLASS_WRITABLE_CONST_TBL(klass);
4016 rb_clear_constant_cache_for_id(id);
4017
4018 ce = ZALLOC(rb_const_entry_t);
4019 rb_id_table_insert(tbl, id, (VALUE)ce);
4020 setup_const_entry(ce, klass, val, visibility);
4021 }
4022}
4023
4024static void
4025setup_const_entry(rb_const_entry_t *ce, VALUE klass, VALUE val,
4026 rb_const_flag_t visibility)
4027{
4028 ce->flag = visibility;
4029 RB_OBJ_WRITE(klass, &ce->value, val);
4030 RB_OBJ_WRITE(klass, &ce->file, rb_source_location(&ce->line));
4031}
4032
4033void
4034rb_define_const(VALUE klass, const char *name, VALUE val)
4035{
4036 ID id = rb_intern(name);
4037
4038 if (!rb_is_const_id(id)) {
4039 rb_warn("rb_define_const: invalid name '%s' for constant", name);
4040 }
4041 if (!RB_SPECIAL_CONST_P(val)) {
4042 rb_vm_register_global_object(val);
4043 }
4044 rb_const_set(klass, id, val);
4045}
4046
4047void
4048rb_define_global_const(const char *name, VALUE val)
4049{
4050 rb_define_const(rb_cObject, name, val);
4051}
4052
4053static void
4054set_const_visibility(VALUE mod, int argc, const VALUE *argv,
4055 rb_const_flag_t flag, rb_const_flag_t mask)
4056{
4057 int i;
4058 rb_const_entry_t *ce;
4059 ID id;
4060
4061 rb_class_modify_check(mod);
4062 if (argc == 0) {
4063 rb_warning("%"PRIsVALUE" with no argument is just ignored",
4064 QUOTE_ID(rb_frame_callee()));
4065 return;
4066 }
4067
4068 for (i = 0; i < argc; i++) {
4069 struct autoload_const *ac;
4070 VALUE val = argv[i];
4071 id = rb_check_id(&val);
4072 if (!id) {
4073 undefined_constant(mod, val);
4074 }
4075 if ((ce = rb_const_lookup(mod, id))) {
4076 ce->flag &= ~mask;
4077 ce->flag |= flag;
4078 if (UNDEF_P(ce->value)) {
4079 if (autoload_const_value_for_named_constant(mod, id, &ac)) {
4080 ac->flag &= ~mask;
4081 ac->flag |= flag;
4082 }
4083 }
4084 rb_clear_constant_cache_for_id(id);
4085 }
4086 else {
4087 undefined_constant(mod, ID2SYM(id));
4088 }
4089 }
4090}
4091
4092void
4093rb_deprecate_constant(VALUE mod, const char *name)
4094{
4095 rb_const_entry_t *ce;
4096 ID id;
4097 long len = strlen(name);
4098
4099 rb_class_modify_check(mod);
4100 if (!(id = rb_check_id_cstr(name, len, NULL))) {
4101 undefined_constant(mod, rb_fstring_new(name, len));
4102 }
4103 if (!(ce = rb_const_lookup(mod, id))) {
4104 undefined_constant(mod, ID2SYM(id));
4105 }
4106 ce->flag |= CONST_DEPRECATED;
4107}
4108
4109/*
4110 * call-seq:
4111 * mod.private_constant(symbol, ...) => mod
4112 *
4113 * Makes a list of existing constants private.
4114 */
4115
4116 VALUE
4117rb_mod_private_constant(int argc, const VALUE *argv, VALUE obj)
4118{
4119 set_const_visibility(obj, argc, argv, CONST_PRIVATE, CONST_VISIBILITY_MASK);
4120 return obj;
4121}
4122
4123/*
4124 * call-seq:
4125 * mod.public_constant(symbol, ...) => mod
4126 *
4127 * Makes a list of existing constants public.
4128 */
4129
4130 VALUE
4131rb_mod_public_constant(int argc, const VALUE *argv, VALUE obj)
4132{
4133 set_const_visibility(obj, argc, argv, CONST_PUBLIC, CONST_VISIBILITY_MASK);
4134 return obj;
4135}
4136
4137/*
4138 * call-seq:
4139 * mod.deprecate_constant(symbol, ...) => mod
4140 *
4141 * Makes a list of existing constants deprecated. Attempt
4142 * to refer to them will produce a warning.
4143 *
4144 * module HTTP
4145 * NotFound = Exception.new
4146 * NOT_FOUND = NotFound # previous version of the library used this name
4147 *
4148 * deprecate_constant :NOT_FOUND
4149 * end
4150 *
4151 * HTTP::NOT_FOUND
4152 * # warning: constant HTTP::NOT_FOUND is deprecated
4153 *
4154 */
4155
4156 VALUE
4157rb_mod_deprecate_constant(int argc, const VALUE *argv, VALUE obj)
4158{
4159 set_const_visibility(obj, argc, argv, CONST_DEPRECATED, CONST_DEPRECATED);
4160 return obj;
4161}
4162
4163static VALUE
4164original_module(VALUE c)
4165{
4166 if (RB_TYPE_P(c, T_ICLASS))
4167 return RBASIC(c)->klass;
4168 return c;
4169}
4170
4171static int
4172cvar_lookup_at(VALUE klass, ID id, st_data_t *v)
4173{
4174 if (RB_TYPE_P(klass, T_ICLASS)) {
4175 if (RICLASS_IS_ORIGIN_P(klass)) {
4176 return 0;
4177 }
4178 else {
4179 // check the original module
4180 klass = RBASIC(klass)->klass;
4181 }
4182 }
4183
4184 VALUE n = rb_ivar_lookup(klass, id, Qundef);
4185 if (UNDEF_P(n)) return 0;
4186
4187 if (v) *v = n;
4188 return 1;
4189}
4190
4191static VALUE
4192cvar_front_klass(VALUE klass)
4193{
4194 if (RCLASS_SINGLETON_P(klass)) {
4195 VALUE obj = RCLASS_ATTACHED_OBJECT(klass);
4196 if (rb_namespace_p(obj)) {
4197 return obj;
4198 }
4199 }
4200 return RCLASS_SUPER(klass);
4201}
4202
4203static void
4204cvar_overtaken(VALUE front, VALUE target, ID id)
4205{
4206 if (front && target != front) {
4207 if (original_module(front) != original_module(target)) {
4208 rb_raise(rb_eRuntimeError,
4209 "class variable % "PRIsVALUE" of %"PRIsVALUE" is overtaken by %"PRIsVALUE"",
4210 ID2SYM(id), rb_class_name(original_module(front)),
4211 rb_class_name(original_module(target)));
4212 }
4213 if (BUILTIN_TYPE(front) == T_CLASS) {
4214 rb_ivar_delete(front, id, Qundef);
4215 }
4216 }
4217}
4218
4219#define CVAR_FOREACH_ANCESTORS(klass, v, r) \
4220 for (klass = cvar_front_klass(klass); klass; klass = RCLASS_SUPER(klass)) { \
4221 if (cvar_lookup_at(klass, id, (v))) { \
4222 r; \
4223 } \
4224 }
4225
4226#define CVAR_LOOKUP(v,r) do {\
4227 CVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(klass, id); \
4228 if (cvar_lookup_at(klass, id, (v))) {r;}\
4229 CVAR_FOREACH_ANCESTORS(klass, v, r);\
4230} while(0)
4231
4232static VALUE
4233find_cvar(VALUE klass, VALUE * front, VALUE * target, ID id)
4234{
4235 VALUE v = Qundef;
4236 CVAR_LOOKUP(&v, {
4237 if (!*front) {
4238 *front = klass;
4239 }
4240 *target = klass;
4241 });
4242
4243 return v;
4244}
4245
4246static void
4247check_for_cvar_table(VALUE subclass, VALUE key)
4248{
4249 if (RB_TYPE_P(subclass, T_ICLASS)) return; // skip refinement ICLASSes
4250
4251 if (RTEST(rb_ivar_defined(subclass, key))) {
4252 RB_DEBUG_COUNTER_INC(cvar_class_invalidate);
4253 ruby_vm_global_cvar_state++;
4254 return;
4255 }
4256
4257 rb_class_foreach_subclass(subclass, check_for_cvar_table, key);
4258}
4259
4260void
4261rb_cvar_set(VALUE klass, ID id, VALUE val)
4262{
4263 VALUE tmp, front = 0, target = 0;
4264
4265 tmp = klass;
4266 CVAR_LOOKUP(0, {if (!front) front = klass; target = klass;});
4267 if (target) {
4268 cvar_overtaken(front, target, id);
4269 }
4270 else {
4271 target = tmp;
4272 }
4273
4274 if (RB_TYPE_P(target, T_ICLASS)) {
4275 target = RBASIC(target)->klass;
4276 }
4277 check_before_mod_set(target, id, val, "class variable");
4278
4279 bool new_cvar = rb_class_ivar_set(target, id, val);
4280
4281 VALUE cvc_tbl = RCLASS_WRITABLE_CVC_TBL(target);
4282
4283 struct rb_cvar_class_tbl_entry *ent;
4284 VALUE ent_data;
4285
4286 if (!cvc_tbl || !rb_marked_id_table_lookup(cvc_tbl, id, &ent_data)) {
4287 ent = (struct rb_cvar_class_tbl_entry *)SHAREABLE_IMEMO_NEW(struct rb_cvar_class_tbl_entry, imemo_cvar_entry, 0);
4288 RB_OBJ_WRITE((VALUE)ent, &ent->class_value, target);
4289 RB_OBJ_WRITE((VALUE)ent, &ent->cref, 0);
4290 ent->global_cvar_state = GET_GLOBAL_CVAR_STATE();
4291
4292 VALUE new_cvc_tbl = cvc_tbl;
4293 if (!new_cvc_tbl) {
4294 new_cvc_tbl = rb_marked_id_table_new(2);
4295 }
4296 else if (rb_multi_ractor_p()) {
4297 new_cvc_tbl = rb_marked_id_table_new(rb_marked_id_table_size(cvc_tbl) + 1);
4298 }
4299
4300 rb_marked_id_table_insert(new_cvc_tbl, id, (VALUE)ent);
4301 if (new_cvc_tbl != cvc_tbl) {
4302 RCLASS_WRITE_CVC_TBL(target, new_cvc_tbl);
4303 }
4304 RB_DEBUG_COUNTER_INC(cvar_inline_miss);
4305 }
4306 else {
4307 ent = (void *)ent_data;
4308 ent->global_cvar_state = GET_GLOBAL_CVAR_STATE();
4309 }
4310
4311 // Break the cvar cache if this is a new class variable
4312 // and target is a module or a subclass with the same
4313 // cvar in this lookup.
4314 if (new_cvar) {
4315 if (RB_TYPE_P(target, T_CLASS)) {
4316 if (RCLASS_SUBCLASSES_FIRST(target)) {
4317 rb_class_foreach_subclass(target, check_for_cvar_table, id);
4318 }
4319 }
4320 }
4321}
4322
4323 VALUE
4324rb_cvar_find(VALUE klass, ID id, VALUE *front)
4325{
4326 VALUE target = 0;
4327 VALUE value;
4328
4329 value = find_cvar(klass, front, &target, id);
4330 if (!target) {
4331 rb_name_err_raise("uninitialized class variable %1$s in %2$s",
4332 klass, ID2SYM(id));
4333 }
4334 cvar_overtaken(*front, target, id);
4335 return (VALUE)value;
4336}
4337
4338 VALUE
4339rb_cvar_get(VALUE klass, ID id)
4340{
4341 VALUE front = 0;
4342 return rb_cvar_find(klass, id, &front);
4343}
4344
4345 VALUE
4346rb_cvar_defined(VALUE klass, ID id)
4347{
4348 if (!klass) return Qfalse;
4349 CVAR_LOOKUP(0,return Qtrue);
4350 return Qfalse;
4351}
4352
4353static ID
4354cv_intern(VALUE klass, const char *name)
4355{
4356 ID id = rb_intern(name);
4357 if (!rb_is_class_id(id)) {
4358 rb_name_err_raise("wrong class variable name %1$s",
4359 klass, rb_str_new_cstr(name));
4360 }
4361 return id;
4362}
4363
4364void
4365rb_cv_set(VALUE klass, const char *name, VALUE val)
4366{
4367 ID id = cv_intern(klass, name);
4368 rb_cvar_set(klass, id, val);
4369}
4370
4371 VALUE
4372rb_cv_get(VALUE klass, const char *name)
4373{
4374 ID id = cv_intern(klass, name);
4375 return rb_cvar_get(klass, id);
4376}
4377
4378void
4379rb_define_class_variable(VALUE klass, const char *name, VALUE val)
4380{
4381 rb_cv_set(klass, name, val);
4382}
4383
4384static int
4385cv_i(ID key, VALUE v, st_data_t a)
4386{
4387 st_table *tbl = (st_table *)a;
4388
4389 if (rb_is_class_id(key)) {
4390 st_update(tbl, (st_data_t)key, cv_i_update, 0);
4391 }
4392 return ST_CONTINUE;
4393}
4394
4395static void*
4396mod_cvar_at(VALUE mod, void *data)
4397{
4398 st_table *tbl = data;
4399 if (!tbl) {
4400 tbl = st_init_numtable();
4401 }
4402 mod = original_module(mod);
4403
4404 rb_ivar_foreach(mod, cv_i, (st_data_t)tbl);
4405 return tbl;
4406}
4407
4408static void*
4409mod_cvar_of(VALUE mod, void *data)
4410{
4411 VALUE tmp = mod;
4412 if (RCLASS_SINGLETON_P(mod)) {
4413 if (rb_namespace_p(RCLASS_ATTACHED_OBJECT(mod))) {
4414 data = mod_cvar_at(tmp, data);
4415 tmp = cvar_front_klass(tmp);
4416 }
4417 }
4418 for (;;) {
4419 data = mod_cvar_at(tmp, data);
4420 tmp = RCLASS_SUPER(tmp);
4421 if (!tmp) break;
4422 }
4423 return data;
4424}
4425
4426static int
4427cv_list_i(st_data_t key, st_data_t value, VALUE ary)
4428{
4429 ID sym = (ID)key;
4430 rb_ary_push(ary, ID2SYM(sym));
4431 return ST_CONTINUE;
4432}
4433
4434static VALUE
4435cvar_list(void *data)
4436{
4437 st_table *tbl = data;
4438 VALUE ary;
4439
4440 if (!tbl) return rb_ary_new2(0);
4441 ary = rb_ary_new2(tbl->num_entries);
4442 st_foreach_safe(tbl, cv_list_i, ary);
4443 st_free_table(tbl);
4444
4445 return ary;
4446}
4447
4448/*
4449 * call-seq:
4450 * mod.class_variables(inherit=true) -> array
4451 *
4452 * Returns an array of the names of class variables in <i>mod</i>.
4453 * This includes the names of class variables in any included
4454 * modules, unless the <i>inherit</i> parameter is set to
4455 * <code>false</code>.
4456 *
4457 * class One
4458 * @@var1 = 1
4459 * end
4460 * class Two < One
4461 * @@var2 = 2
4462 * end
4463 * One.class_variables #=> [:@@var1]
4464 * Two.class_variables #=> [:@@var2, :@@var1]
4465 * Two.class_variables(false) #=> [:@@var2]
4466 */
4467
4468 VALUE
4469rb_mod_class_variables(int argc, const VALUE *argv, VALUE mod)
4470{
4471 bool inherit = true;
4472 st_table *tbl;
4473
4474 if (rb_check_arity(argc, 0, 1)) inherit = RTEST(argv[0]);
4475 if (inherit) {
4476 tbl = mod_cvar_of(mod, 0);
4477 }
4478 else {
4479 tbl = mod_cvar_at(mod, 0);
4480 }
4481 return cvar_list(tbl);
4482}
4483
4484/*
4485 * call-seq:
4486 * remove_class_variable(sym) -> obj
4487 *
4488 * Removes the named class variable from the receiver, returning that
4489 * variable's value.
4490 *
4491 * class Example
4492 * @@var = 99
4493 * puts remove_class_variable(:@@var)
4494 * p(defined? @@var)
4495 * end
4496 *
4497 * <em>produces:</em>
4498 *
4499 * 99
4500 * nil
4501 */
4502
4503 VALUE
4504rb_mod_remove_cvar(VALUE mod, VALUE name)
4505{
4506 const ID id = id_for_var_message(mod, name, class, "wrong class variable name %1$s");
4507 st_data_t val;
4508
4509 if (!id) {
4510 goto not_defined;
4511 }
4512 rb_check_frozen(mod);
4513 val = rb_ivar_delete(mod, id, Qundef);
4514 if (!UNDEF_P(val)) {
4515 return (VALUE)val;
4516 }
4517 if (rb_cvar_defined(mod, id)) {
4518 rb_name_err_raise("cannot remove %1$s for %2$s", mod, ID2SYM(id));
4519 }
4520 not_defined:
4521 rb_name_err_raise("class variable %1$s not defined for %2$s",
4522 mod, name);
4523 UNREACHABLE_RETURN(Qundef);
4524}
4525
4526 VALUE
4527rb_iv_get(VALUE obj, const char *name)
4528{
4529 ID id = rb_check_id_cstr(name, strlen(name), rb_usascii_encoding());
4530
4531 if (!id) {
4532 return Qnil;
4533 }
4534 return rb_ivar_get(obj, id);
4535}
4536
4537 VALUE
4538rb_iv_set(VALUE obj, const char *name, VALUE val)
4539{
4540 ID id = rb_intern(name);
4541
4542 return rb_ivar_set(obj, id, val);
4543}
4544
4545static attr_index_t
4546class_fields_ivar_set(VALUE klass, VALUE fields_obj, ID id, VALUE val, bool concurrent, VALUE *new_fields_obj, bool *new_ivar_out)
4547{
4548 const VALUE original_fields_obj = fields_obj;
4549 fields_obj = original_fields_obj ? original_fields_obj : rb_imemo_fields_new(klass, 1, true);
4550
4551 shape_id_t current_shape_id = RBASIC_SHAPE_ID(fields_obj);
4552 shape_id_t next_shape_id = current_shape_id; // for too_complex
4553 if (UNLIKELY(rb_shape_too_complex_p(current_shape_id))) {
4554 goto too_complex;
4555 }
4556
4557 bool new_ivar;
4558 next_shape_id = generic_shape_ivar(fields_obj, id, &new_ivar);
4559
4560 if (UNLIKELY(rb_shape_too_complex_p(next_shape_id))) {
4561 fields_obj = imemo_fields_complex_from_obj(klass, fields_obj, next_shape_id);
4562 goto too_complex;
4563 }
4564
4565 attr_index_t index = RSHAPE_INDEX(next_shape_id);
4566 if (new_ivar) {
4567 if (index >= RSHAPE_CAPACITY(current_shape_id)) {
4568 // We allocate a new fields_obj even when concurrency isn't a concern
4569 // so that we're embedded as long as possible.
4570 fields_obj = imemo_fields_copy_capa(klass, fields_obj, RSHAPE_CAPACITY(next_shape_id));
4571 }
4572 }
4573
4574 VALUE *fields = rb_imemo_fields_ptr(fields_obj);
4575
4576 if (concurrent && original_fields_obj == fields_obj) {
4577 // In the concurrent case, if we're mutating the existing
4578 // fields_obj, we must use an atomic write, because if we're
4579 // adding a new field, the shape_id must be written after the field
4580 // and if we're updating an existing field, we at least need a relaxed
4581 // write to avoid reaping.
4582 RB_OBJ_ATOMIC_WRITE(fields_obj, &fields[index], val);
4583 }
4584 else {
4585 RB_OBJ_WRITE(fields_obj, &fields[index], val);
4586 }
4587
4588 if (new_ivar) {
4589 RBASIC_SET_SHAPE_ID(fields_obj, next_shape_id);
4590 }
4591
4592 *new_fields_obj = fields_obj;
4593 *new_ivar_out = new_ivar;
4594 return index;
4595
4596too_complex:
4597 {
4598 if (concurrent && fields_obj == original_fields_obj) {
4599 // In multi-ractor case, we must always work on a copy because
4600 // even if the field already exist, inserting in a st_table may
4601 // cause a rebuild.
4602 fields_obj = rb_imemo_fields_clone(fields_obj);
4603 }
4604
4605 st_table *table = rb_imemo_fields_complex_tbl(fields_obj);
4606 new_ivar = !st_insert(table, (st_data_t)id, (st_data_t)val);
4607 RB_OBJ_WRITTEN(fields_obj, Qundef, val);
4608
4609 if (fields_obj != original_fields_obj) {
4610 RBASIC_SET_SHAPE_ID(fields_obj, next_shape_id);
4611 }
4612 }
4613
4614 *new_fields_obj = fields_obj;
4615 *new_ivar_out = new_ivar;
4616 return ATTR_INDEX_NOT_SET;
4617}
4618
4619static attr_index_t
4620class_ivar_set(VALUE obj, ID id, VALUE val, bool *new_ivar)
4621{
4622 rb_class_ensure_writable(obj);
4623
4624 const VALUE original_fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
4625 VALUE new_fields_obj = 0;
4626
4627 attr_index_t index = class_fields_ivar_set(obj, original_fields_obj, id, val, rb_multi_ractor_p(), &new_fields_obj, new_ivar);
4628
4629 if (new_fields_obj != original_fields_obj) {
4630 RCLASS_WRITABLE_SET_FIELDS_OBJ(obj, new_fields_obj);
4631 }
4632
4633 // TODO: What should we set as the T_CLASS shape_id?
4634 // In most case we can replicate the single `fields_obj` shape
4635 // but in namespaced case? Perhaps INVALID_SHAPE_ID?
4636 RBASIC_SET_SHAPE_ID(obj, RBASIC_SHAPE_ID(new_fields_obj));
4637 return index;
4638}
4639
4640bool
4641rb_class_ivar_set(VALUE obj, ID id, VALUE val)
4642{
4643 RUBY_ASSERT(RB_TYPE_P(obj, T_CLASS) || RB_TYPE_P(obj, T_MODULE));
4644 rb_check_frozen(obj);
4645
4646 bool new_ivar;
4647 class_ivar_set(obj, id, val, &new_ivar);
4648 return new_ivar;
4649}
4650
4651void
4652rb_fields_tbl_copy(VALUE dst, VALUE src)
4653{
4654 RUBY_ASSERT(rb_type(dst) == rb_type(src));
4655 RUBY_ASSERT(RB_TYPE_P(dst, T_CLASS) || RB_TYPE_P(dst, T_MODULE));
4656 RUBY_ASSERT(RSHAPE_TYPE_P(RBASIC_SHAPE_ID(dst), SHAPE_ROOT));
4657
4658 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(src);
4659 if (fields_obj) {
4660 RCLASS_WRITABLE_SET_FIELDS_OBJ(dst, rb_imemo_fields_clone(fields_obj));
4661 RBASIC_SET_SHAPE_ID(dst, RBASIC_SHAPE_ID(src));
4662 }
4663}
4664
4665static rb_const_entry_t *
4666const_lookup(struct rb_id_table *tbl, ID id)
4667{
4668 if (tbl) {
4669 VALUE val;
4670 bool r;
4671 RB_VM_LOCKING() {
4672 r = rb_id_table_lookup(tbl, id, &val);
4673 }
4674
4675 if (r) return (rb_const_entry_t *)val;
4676 }
4677 return NULL;
4678}
4679
4680rb_const_entry_t *
4681rb_const_lookup(VALUE klass, ID id)
4682{
4683 return const_lookup(RCLASS_CONST_TBL(klass), id);
4684}
RUBY_ASSERT
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
RUBY_EXTERN
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition dllexport.h:45
RB_OBJ_FROZEN_RAW
static VALUE RB_OBJ_FROZEN_RAW(VALUE obj)
This is an implementation detail of RB_OBJ_FROZEN().
Definition fl_type.h:877
RB_FL_ABLE
static bool RB_FL_ABLE(VALUE obj)
Checks if the object is flaggable.
Definition fl_type.h:440
RB_FL_SET_RAW
static void RB_FL_SET_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_SET().
Definition fl_type.h:600
rb_obj_freeze_inline
void rb_obj_freeze_inline(VALUE obj)
Prevents further modifications to the given object.
Definition variable.c:1991
RB_FL_UNSET_RAW
static void RB_FL_UNSET_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_UNSET().
Definition fl_type.h:660
RUBY_FL_FREEZE
@ RUBY_FL_FREEZE
This flag has something to do with data immutability.
Definition fl_type.h:320
rb_class_modify_check
void rb_class_modify_check(VALUE klass)
Asserts that klass is not a frozen class.
Definition eval.c:421
rb_freeze_singleton_class
void rb_freeze_singleton_class(VALUE x)
This is an implementation detail of RB_OBJ_FREEZE().
Definition class.c:2765
rb_scan_args
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:3133
rb_str_new2
#define rb_str_new2
Old name of rb_str_new_cstr.
Definition string.h:1676
TYPE
#define TYPE(_)
Old name of rb_type.
Definition value_type.h:108
FL_UNSET_RAW
#define FL_UNSET_RAW
Old name of RB_FL_UNSET_RAW.
Definition fl_type.h:133
FL_USER3
#define FL_USER3
Old name of RUBY_FL_USER3.
Definition fl_type.h:73
REALLOC_N
#define REALLOC_N
Old name of RB_REALLOC_N.
Definition memory.h:403
ALLOC
#define ALLOC
Old name of RB_ALLOC.
Definition memory.h:400
T_STRING
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
xfree
#define xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
Qundef
#define Qundef
Old name of RUBY_Qundef.
Definition special_consts.h:62
rb_str_cat2
#define rb_str_cat2
Old name of rb_str_cat_cstr.
Definition string.h:1684
UNREACHABLE
#define UNREACHABLE
Old name of RBIMPL_UNREACHABLE.
Definition assume.h:28
T_IMEMO
#define T_IMEMO
Old name of RUBY_T_IMEMO.
Definition value_type.h:67
ID2SYM
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
SPECIAL_CONST_P
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
Definition special_consts.h:56
T_STRUCT
#define T_STRUCT
Old name of RUBY_T_STRUCT.
Definition value_type.h:79
OBJ_FREEZE
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
Definition fl_type.h:134
UNREACHABLE_RETURN
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
Definition assume.h:29
T_DATA
#define T_DATA
Old name of RUBY_T_DATA.
Definition value_type.h:60
ZALLOC
#define ZALLOC
Old name of RB_ZALLOC.
Definition memory.h:402
CLASS_OF
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:205
T_MODULE
#define T_MODULE
Old name of RUBY_T_MODULE.
Definition value_type.h:70
T_ICLASS
#define T_ICLASS
Old name of RUBY_T_ICLASS.
Definition value_type.h:66
ALLOC_N
#define ALLOC_N
Old name of RB_ALLOC_N.
Definition memory.h:399
FL_TEST_RAW
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:131
rb_ary_new3
#define rb_ary_new3
Old name of rb_ary_new_from_args.
Definition array.h:658
FL_USER2
#define FL_USER2
Old name of RUBY_FL_USER2.
Definition fl_type.h:72
Qtrue
#define Qtrue
Old name of RUBY_Qtrue.
Definition special_consts.h:61
INT2NUM
#define INT2NUM
Old name of RB_INT2NUM.
Definition int.h:43
Qnil
#define Qnil
Old name of RUBY_Qnil.
Definition special_consts.h:60
Qfalse
#define Qfalse
Old name of RUBY_Qfalse.
Definition special_consts.h:59
T_OBJECT
#define T_OBJECT
Old name of RUBY_T_OBJECT.
Definition value_type.h:75
NIL_P
#define NIL_P
Old name of RB_NIL_P.
Definition special_consts.h:55
ALLOCV_N
#define ALLOCV_N
Old name of RB_ALLOCV_N.
Definition memory.h:405
T_CLASS
#define T_CLASS
Old name of RUBY_T_CLASS.
Definition value_type.h:58
BUILTIN_TYPE
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition value_type.h:85
rb_ary_new2
#define rb_ary_new2
Old name of rb_ary_new_capa.
Definition array.h:657
FL_SET_RAW
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
Definition fl_type.h:129
ALLOCV_END
#define ALLOCV_END
Old name of RB_ALLOCV_END.
Definition memory.h:406
rb_category_warn
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:476
rb_name_error
void rb_name_error(ID id, const char *fmt,...)
Raises an instance of rb_eNameError.
Definition error.c:2345
rb_eTypeError
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1431
rb_name_error_str
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:2360
rb_eNameError
VALUE rb_eNameError
NameError exception.
Definition error.c:1436
rb_eRuntimeError
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1429
rb_check_typeddata
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:1398
rb_warn
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
Definition error.c:466
rb_warning
void rb_warning(const char *fmt,...)
Issues a warning.
Definition error.c:497
RB_WARN_CATEGORY_DEPRECATED
@ RB_WARN_CATEGORY_DEPRECATED
Warning is for deprecated features.
Definition error.h:48
rb_obj_hide
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
Definition object.c:100
rb_obj_class
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:264
rb_cModule
VALUE rb_cModule
Module class.
Definition object.c:62
rb_class_real
VALUE rb_class_real(VALUE klass)
Finds a "real" class.
Definition object.c:255
rb_obj_embedded_size
size_t rb_obj_embedded_size(uint32_t fields_count)
Internal header for Object.
Definition object.c:94
RB_OBJ_WRITTEN
#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:615
RB_OBJ_WRITE
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:603
encoding.h
Encoding relates APIs.
rb_usascii_encoding
rb_encoding * rb_usascii_encoding(void)
Queries the encoding that represents US-ASCII.
Definition encoding.c:1547
rb_check_id_cstr
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:1223
rb_funcall
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1117
rb_ary_new
VALUE rb_ary_new(void)
Allocates a new, empty array.
rb_ary_hidden_new
VALUE rb_ary_hidden_new(long capa)
Allocates a hidden (no class) empty array.
rb_ary_push
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
rb_assoc_new
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Identical to rb_ary_new_from_values(), except it expects exactly two parameters.
rb_check_arity
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
rb_frame_callee
ID rb_frame_callee(void)
Identical to rb_frame_this_func(), except it returns the named used to call the method.
Definition eval.c:1199
st_foreach_safe
#define st_foreach_safe
Just another name of rb_st_foreach_safe.
Definition hash.h:51
rb_feature_provided
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
rb_backref_get
VALUE rb_backref_get(void)
Queries the last match, or Regexp.last_match, or the $~.
Definition vm.c:2030
rb_is_instance_id
int rb_is_instance_id(ID id)
Classifies the given ID, then sees if it is an instance variable.
Definition symbol.c:1097
rb_is_const_id
int rb_is_const_id(ID id)
Classifies the given ID, then sees if it is a constant.
Definition symbol.c:1079
rb_is_class_id
int rb_is_class_id(ID id)
Classifies the given ID, then sees if it is a class variable.
Definition symbol.c:1085
rb_block_proc
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
Definition proc.c:983
rb_reg_nth_defined
VALUE rb_reg_nth_defined(int n, VALUE md)
Identical to rb_reg_nth_match(), except it just returns Boolean.
Definition re.c:1905
rb_str_append
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:3799
rb_str_subseq
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:3155
rb_str_new_frozen
VALUE rb_str_new_frozen(VALUE str)
Creates a frozen copy of the string, if necessary.
Definition string.c:1518
rb_str_dup
VALUE rb_str_dup(VALUE str)
Duplicates a string.
Definition string.c:1996
rb_str_new_cstr
#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
rb_str_intern
VALUE rb_str_intern(VALUE str)
Identical to rb_to_symbol(), except it assumes the receiver being an instance of RString.
Definition symbol.c:937
rb_mutex_new
VALUE rb_mutex_new(void)
Creates a mutex.
Definition thread_sync.c:183
rb_mutex_synchronize
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.
Definition thread_sync.c:655
rb_exec_recursive_paired
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,...
Definition thread.c:5594
rb_mod_remove_cvar
VALUE rb_mod_remove_cvar(VALUE mod, VALUE name)
Resembles Module#remove_class_variable.
Definition variable.c:4504
rb_obj_instance_variables
VALUE rb_obj_instance_variables(VALUE obj)
Resembles Object#instance_variables.
Definition variable.c:2434
rb_f_untrace_var
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
rb_const_get
VALUE rb_const_get(VALUE space, ID name)
Identical to rb_const_defined(), except it returns the actual defined value.
Definition variable.c:3461
rb_const_list
VALUE rb_const_list(void *)
This is another mysterious API that comes with no documents at all.
Definition variable.c:3705
rb_path2class
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
rb_autoload_p
VALUE rb_autoload_p(VALUE space, ID name)
Queries if an autoload is defined at a point.
Definition variable.c:3329
rb_set_class_path
void rb_set_class_path(VALUE klass, VALUE space, const char *name)
Names a class.
Definition variable.c:441
rb_ivar_set
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:2030
rb_mod_remove_const
VALUE rb_mod_remove_const(VALUE space, VALUE name)
Resembles Module#remove_const.
Definition variable.c:3566
rb_class_path_cached
VALUE rb_class_path_cached(VALUE mod)
Just another name of rb_mod_name.
Definition variable.c:389
rb_f_trace_var
VALUE rb_f_trace_var(int argc, const VALUE *argv)
Traces a global variable.
Definition variable.c:878
rb_cvar_set
void rb_cvar_set(VALUE klass, ID name, VALUE val)
Assigns a value to a class variable.
Definition variable.c:4261
rb_cvar_get
VALUE rb_cvar_get(VALUE klass, ID name)
Obtains a value from a class variable.
Definition variable.c:4339
rb_mod_constants
VALUE rb_mod_constants(int argc, const VALUE *argv, VALUE recv)
Resembles Module#constants.
Definition variable.c:3737
rb_cvar_find
VALUE rb_cvar_find(VALUE klass, ID name, VALUE *front)
Identical to rb_cvar_get(), except it takes additional "front" pointer.
Definition variable.c:4324
rb_path_to_class
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
rb_ivar_get
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:1505
rb_const_set
void rb_const_set(VALUE space, ID name, VALUE val)
Names a constant.
Definition variable.c:3939
rb_autoload_load
VALUE rb_autoload_load(VALUE space, ID name)
Kicks the autoload procedure as if it was "touched".
Definition variable.c:3291
rb_mod_name
VALUE rb_mod_name(VALUE mod)
Queries the name of a module.
Definition variable.c:136
rb_class_name
VALUE rb_class_name(VALUE obj)
Queries the name of the given object's class.
Definition variable.c:500
rb_const_get_at
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:3467
rb_set_class_path_string
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
rb_alias_variable
void rb_alias_variable(ID dst, ID src)
Aliases a global variable.
Definition variable.c:1166
rb_define_class_variable
void rb_define_class_variable(VALUE, const char *, VALUE)
Just another name of rb_cv_set.
Definition variable.c:4379
rb_obj_remove_instance_variable
VALUE rb_obj_remove_instance_variable(VALUE obj, VALUE name)
Resembles Object#remove_instance_variable.
Definition variable.c:2488
rb_mod_const_of
void * rb_mod_const_of(VALUE, void *)
This is a variant of rb_mod_const_at().
Definition variable.c:3683
rb_ivar_count
st_index_t rb_ivar_count(VALUE obj)
Number of instance variables defined on an object.
Definition variable.c:2346
rb_mod_const_at
void * rb_mod_const_at(VALUE, void *)
This API is mysterious.
Definition variable.c:3668
rb_const_remove
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:3579
rb_const_get_from
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:3455
rb_ivar_defined
VALUE rb_ivar_defined(VALUE obj, ID name)
Queries if the instance variable is defined at the object.
Definition variable.c:2109
rb_cv_get
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:4372
rb_const_defined_at
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:3799
rb_cv_set
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:4365
rb_mod_class_variables
VALUE rb_mod_class_variables(int argc, const VALUE *argv, VALUE recv)
Resembles Module#class_variables.
Definition variable.c:4469
rb_f_global_variables
VALUE rb_f_global_variables(void)
Queries the list of global variables.
Definition variable.c:1133
rb_cvar_defined
VALUE rb_cvar_defined(VALUE klass, ID name)
Queries if the given class has the given class variable.
Definition variable.c:4346
rb_class_path
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
Definition variable.c:380
rb_const_defined_from
int rb_const_defined_from(VALUE space, ID name)
Identical to rb_const_defined(), except it returns false for private constants.
Definition variable.c:3787
rb_const_defined
int rb_const_defined(VALUE space, ID name)
Queries if the constant is defined at the namespace.
Definition variable.c:3793
rb_free_generic_ivar
void rb_free_generic_ivar(VALUE obj)
Frees the list of instance variables.
Definition variable.c:1308
rb_sourcefile
const char * rb_sourcefile(void)
Resembles __FILE__.
Definition vm.c:2067
rb_clear_constant_cache_for_id
void rb_clear_constant_cache_for_id(ID id)
Clears the inline constant caches associated with a particular ID.
Definition vm_method.c:332
rb_obj_respond_to
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:3400
rb_intern_const
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
Definition symbol.h:285
rb_id2sym
VALUE rb_id2sym(ID id)
Allocates an instance of rb_cSymbol that has the given id.
Definition symbol.c:974
rb_check_id
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
Definition symbol.c:1133
rb_to_id
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:12664
rb_gvar_var_setter
rb_gvar_setter_t rb_gvar_var_setter
Definition variable.h:119
rb_gvar_var_marker
rb_gvar_marker_t rb_gvar_var_marker
Definition variable.h:128
rb_define_global_const
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:4048
rb_gv_get
VALUE rb_gv_get(const char *name)
Obtains a global variable.
Definition variable.c:1091
rb_define_variable
void rb_define_variable(const char *name, VALUE *var)
"Shares" a global variable between Ruby and C.
Definition variable.c:849
rb_gvar_marker_t
void rb_gvar_marker_t(VALUE *var)
Type that represents a global variable marker function.
Definition variable.h:53
rb_deprecate_constant
void rb_deprecate_constant(VALUE mod, const char *name)
Asserts that the given constant is deprecated.
Definition variable.c:4093
rb_gvar_setter_t
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_val_setter
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_undef_marker
rb_gvar_marker_t rb_gvar_undef_marker
Definition variable.h:80
rb_define_readonly_variable
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_readonly_setter
rb_gvar_setter_t rb_gvar_readonly_setter
This function just raises rb_eNameError.
Definition variable.h:135
rb_gvar_undef_getter
rb_gvar_getter_t rb_gvar_undef_getter
Definition variable.h:62
rb_gv_set
VALUE rb_gv_set(const char *name, VALUE val)
Assigns to a global variable.
Definition variable.c:1046
rb_gvar_val_marker
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
rb_gvar_getter_t
VALUE rb_gvar_getter_t(ID id, VALUE *data)
Type that represents a global variable getter function.
Definition variable.h:37
rb_iv_get
VALUE rb_iv_get(VALUE obj, const char *name)
Obtains an instance variable.
Definition variable.c:4527
rb_gvar_undef_setter
rb_gvar_setter_t rb_gvar_undef_setter
Definition variable.h:71
rb_gvar_val_getter
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
rb_iv_set
VALUE rb_iv_set(VALUE obj, const char *name, VALUE val)
Assigns to an instance variable.
Definition variable.c:4538
rb_gvar_var_getter
rb_gvar_getter_t rb_gvar_var_getter
Definition variable.h:110
len
int len
Length of the buffer.
Definition io.h:8
rb_ractor_shareable_p
static bool rb_ractor_shareable_p(VALUE obj)
Queries if multiple Ractors can share the passed object or not.
Definition ractor.h:249
RB_OBJ_SHAREABLE_P
#define RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
Definition ractor.h:235
util.h
MEMCPY
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
Definition memory.h:372
RB_GC_GUARD
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:167
MEMMOVE
#define MEMMOVE(p1, p2, type, n)
Handy macro to call memmove.
Definition memory.h:384
ruby::backward::cxxanyargs::rb_define_hooked_variable
void rb_define_hooked_variable(const char *q, VALUE *w, type *e, void_type *r)
Define a function-backended global variable.
Definition cxxanyargs.hpp:136
ruby::backward::cxxanyargs::type
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition cxxanyargs.hpp:56
ruby::backward::cxxanyargs::st_foreach
int st_foreach(st_table *q, int_type *w, st_data_t e)
Iteration over the given table.
Definition cxxanyargs.hpp:432
ruby::backward::cxxanyargs::rb_define_virtual_variable
void rb_define_virtual_variable(const char *q, type *w, void_type *e)
Define a function-backended global variable.
Definition cxxanyargs.hpp:73
ruby::backward::cxxanyargs::rb_ivar_foreach
void rb_ivar_foreach(VALUE q, int_type *w, VALUE e)
Iteration over each instance variable of the object.
Definition cxxanyargs.hpp:498
ruby::backward::cxxanyargs::rb_ensure
VALUE rb_ensure(type *q, VALUE w, type *e, VALUE r)
An equivalent of ensure clause.
Definition cxxanyargs.hpp:310
rb_copy_generic_ivar
void rb_copy_generic_ivar(VALUE clone, VALUE obj)
Copies the list of instance variables.
Definition variable.c:2232
RARRAY_LEN
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:51
RBASIC_CLASS
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
Definition rbasic.h:166
RBASIC
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
RCLASS_SUPER
#define RCLASS_SUPER
Just another name of rb_class_get_superclass.
Definition rclass.h:44
ROBJECT
#define ROBJECT(obj)
Convenient casting macro.
Definition robject.h:43
ROBJECT_FIELDS
static VALUE * ROBJECT_FIELDS(VALUE obj)
Queries the instance variables.
Definition robject.h:128
StringValue
#define StringValue(v)
Ensures that the parameter object is a String.
Definition rstring.h:66
RSTRING_END
static char * RSTRING_END(VALUE str)
Queries the end of the contents pointer of the string.
Definition rstring.h:409
RTYPEDDATA_P
static bool RTYPEDDATA_P(VALUE obj)
Checks whether the passed object is RTypedData or RData.
Definition rtypeddata.h:575
RTYPEDDATA_DATA
#define RTYPEDDATA_DATA(v)
Convenient getter macro.
Definition rtypeddata.h:103
TypedData_Wrap_Struct
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
Definition rtypeddata.h:461
rb_data_type_t
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:205
RTYPEDDATA
#define RTYPEDDATA(obj)
Convenient casting macro.
Definition rtypeddata.h:95
TypedData_Make_Struct
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
Definition rtypeddata.h:508
rb_class2name
const char * rb_class2name(VALUE klass)
Queries the name of the passed class.
Definition variable.c:506
rb_obj_classname
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
Definition variable.c:515
RB_NO_KEYWORDS
#define RB_NO_KEYWORDS
Do not pass keywords.
Definition scan_args.h:69
RB_SPECIAL_CONST_P
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
Definition special_consts.h:327
RTEST
#define RTEST
This is an old name of RB_TEST.
Definition special_consts.h:51
_
#define _(args)
This was a transition path from K&R to ANSI.
Definition stdarg.h:35
stdbool.h
C99 shim for <stdbool.h>.
autoload_arguments
Definition variable.c:2853
autoload_const
Definition variable.c:2647
autoload_copy_table_data
Definition variable.c:2776
autoload_data
Definition variable.c:2676
autoload_load_arguments
Definition variable.c:3126
iv_itr_data
Definition variable.c:2131
rb_cvar_class_tbl_entry
Definition class.h:37
rb_global_entry
Definition variable.c:540
rb_global_variable
Definition variable.c:527
rb_id_table
Definition id_table.c:41
st_table
Definition st.h:79
sub_temporary_name_args
Definition variable.c:175
trace_data
Definition variable.c:967
trace_var
Definition variable.c:520
ID
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
rb_type
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
RB_BUILTIN_TYPE
static enum ruby_value_type RB_BUILTIN_TYPE(VALUE obj)
Queries the type of the object.
Definition value_type.h:182
Check_Type
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
RB_TYPE_P
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