Line | Count | Source (jump to first uncovered line) |
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/class.h" |
24 | | #include "internal/compilers.h" |
25 | | #include "internal/error.h" |
26 | | #include "internal/eval.h" |
27 | | #include "internal/hash.h" |
28 | | #include "internal/object.h" |
29 | | #include "internal/re.h" |
30 | | #include "internal/symbol.h" |
31 | | #include "internal/thread.h" |
32 | | #include "internal/variable.h" |
33 | | #include "ruby/encoding.h" |
34 | | #include "ruby/st.h" |
35 | | #include "ruby/util.h" |
36 | | #include "shape.h" |
37 | | #include "symbol.h" |
38 | | #include "variable.h" |
39 | | #include "vm_core.h" |
40 | | #include "ractor_core.h" |
41 | | #include "vm_sync.h" |
42 | | |
43 | | RUBY_EXTERN rb_serial_t ruby_vm_global_cvar_state; |
44 | 0 | #define GET_GLOBAL_CVAR_STATE() (ruby_vm_global_cvar_state) |
45 | | |
46 | | typedef void rb_gvar_compact_t(void *var); |
47 | | |
48 | | static struct rb_id_table *rb_global_tbl; |
49 | | static ID autoload; |
50 | | |
51 | | // This hash table maps file paths to loadable features. We use this to track |
52 | | // autoload state until it's no longer needed. |
53 | | // feature (file path) => struct autoload_data |
54 | | static VALUE autoload_features; |
55 | | |
56 | | // This mutex is used to protect autoloading state. We use a global mutex which |
57 | | // is held until a per-feature mutex can be created. This ensures there are no |
58 | | // race conditions relating to autoload state. |
59 | | static VALUE autoload_mutex; |
60 | | |
61 | | static void check_before_mod_set(VALUE, ID, VALUE, const char *); |
62 | | static void setup_const_entry(rb_const_entry_t *, VALUE, VALUE, rb_const_flag_t); |
63 | | static VALUE rb_const_search(VALUE klass, ID id, int exclude, int recurse, int visibility); |
64 | | static st_table *generic_iv_tbl_; |
65 | | |
66 | | void |
67 | | Init_var_tables(void) |
68 | 0 | { |
69 | 0 | rb_global_tbl = rb_id_table_create(0); |
70 | 0 | generic_iv_tbl_ = st_init_numtable(); |
71 | 0 | autoload = rb_intern_const("__autoload__"); |
72 | |
|
73 | 0 | autoload_mutex = rb_mutex_new(); |
74 | 0 | rb_obj_hide(autoload_mutex); |
75 | 0 | rb_gc_register_mark_object(autoload_mutex); |
76 | |
|
77 | 0 | autoload_features = rb_ident_hash_new(); |
78 | 0 | rb_obj_hide(autoload_features); |
79 | 0 | rb_gc_register_mark_object(autoload_features); |
80 | 0 | } |
81 | | |
82 | | static inline bool |
83 | | rb_namespace_p(VALUE obj) |
84 | 0 | { |
85 | 0 | if (RB_SPECIAL_CONST_P(obj)) return false; |
86 | 0 | switch (RB_BUILTIN_TYPE(obj)) { |
87 | 0 | case T_MODULE: case T_CLASS: return true; |
88 | 0 | default: break; |
89 | 0 | } |
90 | 0 | return false; |
91 | 0 | } |
92 | | |
93 | | /** |
94 | | * Returns +classpath+ of _klass_, if it is named, or +nil+ for |
95 | | * anonymous +class+/+module+. A named +classpath+ may contain |
96 | | * an anonymous component, but the last component is guaranteed |
97 | | * to not be anonymous. <code>*permanent</code> is set to 1 |
98 | | * if +classpath+ has no anonymous components. There is no builtin |
99 | | * Ruby level APIs that can change a permanent +classpath+. |
100 | | */ |
101 | | static VALUE |
102 | | classname(VALUE klass, bool *permanent) |
103 | 0 | { |
104 | 0 | *permanent = false; |
105 | |
|
106 | 0 | VALUE classpath = RCLASS_EXT(klass)->classpath; |
107 | 0 | if (classpath == 0) return Qnil; |
108 | | |
109 | 0 | *permanent = RCLASS_EXT(klass)->permanent_classpath; |
110 | |
|
111 | 0 | return classpath; |
112 | 0 | } |
113 | | |
114 | | /* |
115 | | * call-seq: |
116 | | * mod.name -> string |
117 | | * |
118 | | * Returns the name of the module <i>mod</i>. Returns nil for anonymous modules. |
119 | | */ |
120 | | |
121 | | VALUE |
122 | | rb_mod_name(VALUE mod) |
123 | 0 | { |
124 | 0 | bool permanent; |
125 | 0 | return classname(mod, &permanent); |
126 | 0 | } |
127 | | |
128 | | // Similar to logic in rb_mod_const_get() |
129 | | static bool |
130 | | is_constant_path(VALUE name) |
131 | 0 | { |
132 | 0 | const char *path = RSTRING_PTR(name); |
133 | 0 | const char *pend = RSTRING_END(name); |
134 | 0 | rb_encoding *enc = rb_enc_get(name); |
135 | |
|
136 | 0 | const char *p = path; |
137 | |
|
138 | 0 | if (p >= pend || !*p) { |
139 | 0 | return false; |
140 | 0 | } |
141 | | |
142 | 0 | while (p < pend) { |
143 | 0 | if (p + 2 <= pend && p[0] == ':' && p[1] == ':') { |
144 | 0 | p += 2; |
145 | 0 | } |
146 | |
|
147 | 0 | const char *pbeg = p; |
148 | 0 | while (p < pend && *p != ':') p++; |
149 | |
|
150 | 0 | if (pbeg == p) return false; |
151 | | |
152 | 0 | if (rb_enc_symname_type(pbeg, p - pbeg, enc, 0) != ID_CONST) { |
153 | 0 | return false; |
154 | 0 | } |
155 | 0 | } |
156 | | |
157 | 0 | return true; |
158 | 0 | } |
159 | | |
160 | | /* |
161 | | * call-seq: |
162 | | * mod.set_temporary_name(string) -> self |
163 | | * mod.set_temporary_name(nil) -> self |
164 | | * |
165 | | * Sets the temporary name of the module. This name is reflected in |
166 | | * introspection of the module and the values that are related to it, such |
167 | | * as instances, constants, and methods. |
168 | | * |
169 | | * The name should be +nil+ or non-empty string that is not a valid constant |
170 | | * name (to avoid confusing between permanent and temporary names). |
171 | | * |
172 | | * The method can be useful to distinguish dynamically generated classes and |
173 | | * modules without assigning them to constants. |
174 | | * |
175 | | * If the module is given a permanent name by assigning it to a constant, |
176 | | * the temporary name is discarded. A temporary name can't be assigned to |
177 | | * modules that have a permanent name. |
178 | | * |
179 | | * If the given name is +nil+, the module becomes anonymous again. |
180 | | * |
181 | | * Example: |
182 | | * |
183 | | * m = Module.new # => #<Module:0x0000000102c68f38> |
184 | | * m.name #=> nil |
185 | | * |
186 | | * m.set_temporary_name("fake_name") # => fake_name |
187 | | * m.name #=> "fake_name" |
188 | | * |
189 | | * m.set_temporary_name(nil) # => #<Module:0x0000000102c68f38> |
190 | | * m.name #=> nil |
191 | | * |
192 | | * c = Class.new |
193 | | * c.set_temporary_name("MyClass(with description)") |
194 | | * |
195 | | * c.new # => #<MyClass(with description):0x0....> |
196 | | * |
197 | | * c::M = m |
198 | | * c::M.name #=> "MyClass(with description)::M" |
199 | | * |
200 | | * # Assigning to a constant replaces the name with a permanent one |
201 | | * C = c |
202 | | * |
203 | | * C.name #=> "C" |
204 | | * C::M.name #=> "C::M" |
205 | | * c.new # => #<C:0x0....> |
206 | | */ |
207 | | |
208 | | VALUE |
209 | | rb_mod_set_temporary_name(VALUE mod, VALUE name) |
210 | 0 | { |
211 | | // We don't allow setting the name if the classpath is already permanent: |
212 | 0 | if (RCLASS_EXT(mod)->permanent_classpath) { |
213 | 0 | rb_raise(rb_eRuntimeError, "can't change permanent name"); |
214 | 0 | } |
215 | | |
216 | 0 | if (NIL_P(name)) { |
217 | | // Set the temporary classpath to NULL (anonymous): |
218 | 0 | RCLASS_SET_CLASSPATH(mod, 0, FALSE); |
219 | 0 | } |
220 | 0 | else { |
221 | | // Ensure the name is a string: |
222 | 0 | StringValue(name); |
223 | |
|
224 | 0 | if (RSTRING_LEN(name) == 0) { |
225 | 0 | rb_raise(rb_eArgError, "empty class/module name"); |
226 | 0 | } |
227 | | |
228 | 0 | if (is_constant_path(name)) { |
229 | 0 | rb_raise(rb_eArgError, "the temporary name must not be a constant path to avoid confusion"); |
230 | 0 | } |
231 | | |
232 | | // Set the temporary classpath to the given name: |
233 | 0 | RCLASS_SET_CLASSPATH(mod, name, FALSE); |
234 | 0 | } |
235 | | |
236 | 0 | return mod; |
237 | 0 | } |
238 | | |
239 | | static VALUE |
240 | | make_temporary_path(VALUE obj, VALUE klass) |
241 | 0 | { |
242 | 0 | VALUE path; |
243 | 0 | switch (klass) { |
244 | 0 | case Qnil: |
245 | 0 | path = rb_sprintf("#<Class:%p>", (void*)obj); |
246 | 0 | break; |
247 | 0 | case Qfalse: |
248 | 0 | path = rb_sprintf("#<Module:%p>", (void*)obj); |
249 | 0 | break; |
250 | 0 | default: |
251 | 0 | path = rb_sprintf("#<%"PRIsVALUE":%p>", klass, (void*)obj); |
252 | 0 | break; |
253 | 0 | } |
254 | 0 | OBJ_FREEZE(path); |
255 | 0 | return path; |
256 | 0 | } |
257 | | |
258 | | typedef VALUE (*fallback_func)(VALUE obj, VALUE name); |
259 | | |
260 | | static VALUE |
261 | | rb_tmp_class_path(VALUE klass, bool *permanent, fallback_func fallback) |
262 | 0 | { |
263 | 0 | VALUE path = classname(klass, permanent); |
264 | |
|
265 | 0 | if (!NIL_P(path)) { |
266 | 0 | return path; |
267 | 0 | } |
268 | 0 | else { |
269 | 0 | if (RB_TYPE_P(klass, T_MODULE)) { |
270 | 0 | if (rb_obj_class(klass) == rb_cModule) { |
271 | 0 | path = Qfalse; |
272 | 0 | } |
273 | 0 | else { |
274 | 0 | bool perm; |
275 | 0 | path = rb_tmp_class_path(RBASIC(klass)->klass, &perm, fallback); |
276 | 0 | } |
277 | 0 | } |
278 | 0 | *permanent = false; |
279 | 0 | return fallback(klass, path); |
280 | 0 | } |
281 | 0 | } |
282 | | |
283 | | VALUE |
284 | | rb_class_path(VALUE klass) |
285 | 0 | { |
286 | 0 | bool permanent; |
287 | 0 | VALUE path = rb_tmp_class_path(klass, &permanent, make_temporary_path); |
288 | 0 | if (!NIL_P(path)) path = rb_str_dup(path); |
289 | 0 | return path; |
290 | 0 | } |
291 | | |
292 | | VALUE |
293 | | rb_class_path_cached(VALUE klass) |
294 | 0 | { |
295 | 0 | return rb_mod_name(klass); |
296 | 0 | } |
297 | | |
298 | | static VALUE |
299 | | no_fallback(VALUE obj, VALUE name) |
300 | 0 | { |
301 | 0 | return name; |
302 | 0 | } |
303 | | |
304 | | VALUE |
305 | | rb_search_class_path(VALUE klass) |
306 | 0 | { |
307 | 0 | bool permanent; |
308 | 0 | return rb_tmp_class_path(klass, &permanent, no_fallback); |
309 | 0 | } |
310 | | |
311 | | static VALUE |
312 | | build_const_pathname(VALUE head, VALUE tail) |
313 | 0 | { |
314 | 0 | VALUE path = rb_str_dup(head); |
315 | 0 | rb_str_cat2(path, "::"); |
316 | 0 | rb_str_append(path, tail); |
317 | 0 | return rb_fstring(path); |
318 | 0 | } |
319 | | |
320 | | static VALUE |
321 | | build_const_path(VALUE head, ID tail) |
322 | 0 | { |
323 | 0 | return build_const_pathname(head, rb_id2str(tail)); |
324 | 0 | } |
325 | | |
326 | | void |
327 | | rb_set_class_path_string(VALUE klass, VALUE under, VALUE name) |
328 | 0 | { |
329 | 0 | bool permanent = true; |
330 | |
|
331 | 0 | VALUE str; |
332 | 0 | if (under == rb_cObject) { |
333 | 0 | str = rb_str_new_frozen(name); |
334 | 0 | } |
335 | 0 | else { |
336 | 0 | str = rb_tmp_class_path(under, &permanent, make_temporary_path); |
337 | 0 | str = build_const_pathname(str, name); |
338 | 0 | } |
339 | |
|
340 | 0 | RCLASS_SET_CLASSPATH(klass, str, permanent); |
341 | 0 | } |
342 | | |
343 | | void |
344 | | rb_set_class_path(VALUE klass, VALUE under, const char *name) |
345 | 0 | { |
346 | 0 | VALUE str = rb_str_new2(name); |
347 | 0 | OBJ_FREEZE(str); |
348 | 0 | rb_set_class_path_string(klass, under, str); |
349 | 0 | } |
350 | | |
351 | | VALUE |
352 | | rb_path_to_class(VALUE pathname) |
353 | 0 | { |
354 | 0 | rb_encoding *enc = rb_enc_get(pathname); |
355 | 0 | const char *pbeg, *pend, *p, *path = RSTRING_PTR(pathname); |
356 | 0 | ID id; |
357 | 0 | VALUE c = rb_cObject; |
358 | |
|
359 | 0 | if (!rb_enc_asciicompat(enc)) { |
360 | 0 | rb_raise(rb_eArgError, "invalid class path encoding (non ASCII)"); |
361 | 0 | } |
362 | 0 | pbeg = p = path; |
363 | 0 | pend = path + RSTRING_LEN(pathname); |
364 | 0 | if (path == pend || path[0] == '#') { |
365 | 0 | rb_raise(rb_eArgError, "can't retrieve anonymous class %"PRIsVALUE, |
366 | 0 | QUOTE(pathname)); |
367 | 0 | } |
368 | 0 | while (p < pend) { |
369 | 0 | while (p < pend && *p != ':') p++; |
370 | 0 | id = rb_check_id_cstr(pbeg, p-pbeg, enc); |
371 | 0 | if (p < pend && p[0] == ':') { |
372 | 0 | if ((size_t)(pend - p) < 2 || p[1] != ':') goto undefined_class; |
373 | 0 | p += 2; |
374 | 0 | pbeg = p; |
375 | 0 | } |
376 | 0 | if (!id) { |
377 | 0 | goto undefined_class; |
378 | 0 | } |
379 | 0 | c = rb_const_search(c, id, TRUE, FALSE, FALSE); |
380 | 0 | if (UNDEF_P(c)) goto undefined_class; |
381 | 0 | if (!rb_namespace_p(c)) { |
382 | 0 | rb_raise(rb_eTypeError, "%"PRIsVALUE" does not refer to class/module", |
383 | 0 | pathname); |
384 | 0 | } |
385 | 0 | } |
386 | 0 | RB_GC_GUARD(pathname); |
387 | |
|
388 | 0 | return c; |
389 | | |
390 | 0 | undefined_class: |
391 | 0 | rb_raise(rb_eArgError, "undefined class/module % "PRIsVALUE, |
392 | 0 | rb_str_subseq(pathname, 0, p-path)); |
393 | 0 | UNREACHABLE_RETURN(Qundef); |
394 | 0 | } |
395 | | |
396 | | VALUE |
397 | | rb_path2class(const char *path) |
398 | 0 | { |
399 | 0 | return rb_path_to_class(rb_str_new_cstr(path)); |
400 | 0 | } |
401 | | |
402 | | VALUE |
403 | | rb_class_name(VALUE klass) |
404 | 0 | { |
405 | 0 | return rb_class_path(rb_class_real(klass)); |
406 | 0 | } |
407 | | |
408 | | const char * |
409 | | rb_class2name(VALUE klass) |
410 | 0 | { |
411 | 0 | bool permanent; |
412 | 0 | VALUE path = rb_tmp_class_path(rb_class_real(klass), &permanent, make_temporary_path); |
413 | 0 | if (NIL_P(path)) return NULL; |
414 | 0 | return RSTRING_PTR(path); |
415 | 0 | } |
416 | | |
417 | | const char * |
418 | | rb_obj_classname(VALUE obj) |
419 | 0 | { |
420 | 0 | return rb_class2name(CLASS_OF(obj)); |
421 | 0 | } |
422 | | |
423 | | struct trace_var { |
424 | | int removed; |
425 | | void (*func)(VALUE arg, VALUE val); |
426 | | VALUE data; |
427 | | struct trace_var *next; |
428 | | }; |
429 | | |
430 | | struct rb_global_variable { |
431 | | int counter; |
432 | | int block_trace; |
433 | | VALUE *data; |
434 | | rb_gvar_getter_t *getter; |
435 | | rb_gvar_setter_t *setter; |
436 | | rb_gvar_marker_t *marker; |
437 | | rb_gvar_compact_t *compactor; |
438 | | struct trace_var *trace; |
439 | | }; |
440 | | |
441 | | struct rb_global_entry { |
442 | | struct rb_global_variable *var; |
443 | | ID id; |
444 | | bool ractor_local; |
445 | | }; |
446 | | |
447 | | static enum rb_id_table_iterator_result |
448 | | free_global_entry_i(ID key, VALUE val, void *arg) |
449 | 0 | { |
450 | 0 | struct rb_global_entry *entry = (struct rb_global_entry *)val; |
451 | 0 | if (entry->var->counter == 1) { |
452 | 0 | ruby_xfree(entry->var); |
453 | 0 | } |
454 | 0 | else { |
455 | 0 | entry->var->counter--; |
456 | 0 | } |
457 | 0 | ruby_xfree(entry); |
458 | 0 | return ID_TABLE_DELETE; |
459 | 0 | } |
460 | | |
461 | | void |
462 | | rb_free_rb_global_tbl(void) |
463 | 0 | { |
464 | 0 | rb_id_table_foreach(rb_global_tbl, free_global_entry_i, 0); |
465 | 0 | rb_id_table_free(rb_global_tbl); |
466 | 0 | } |
467 | | |
468 | | void |
469 | | rb_free_generic_iv_tbl_(void) |
470 | 0 | { |
471 | 0 | st_free_table(generic_iv_tbl_); |
472 | 0 | } |
473 | | |
474 | | static struct rb_global_entry* |
475 | | rb_find_global_entry(ID id) |
476 | 0 | { |
477 | 0 | struct rb_global_entry *entry; |
478 | 0 | VALUE data; |
479 | |
|
480 | 0 | if (!rb_id_table_lookup(rb_global_tbl, id, &data)) { |
481 | 0 | entry = NULL; |
482 | 0 | } |
483 | 0 | else { |
484 | 0 | entry = (struct rb_global_entry *)data; |
485 | 0 | RUBY_ASSERT(entry != NULL); |
486 | 0 | } |
487 | |
|
488 | 0 | if (UNLIKELY(!rb_ractor_main_p()) && (!entry || !entry->ractor_local)) { |
489 | 0 | rb_raise(rb_eRactorIsolationError, "can not access global variables %s from non-main Ractors", rb_id2name(id)); |
490 | 0 | } |
491 | | |
492 | 0 | return entry; |
493 | 0 | } |
494 | | |
495 | | void |
496 | | rb_gvar_ractor_local(const char *name) |
497 | 0 | { |
498 | 0 | struct rb_global_entry *entry = rb_find_global_entry(rb_intern(name)); |
499 | 0 | entry->ractor_local = true; |
500 | 0 | } |
501 | | |
502 | | static void |
503 | | rb_gvar_undef_compactor(void *var) |
504 | 0 | { |
505 | 0 | } |
506 | | |
507 | | static struct rb_global_entry* |
508 | | rb_global_entry(ID id) |
509 | 0 | { |
510 | 0 | struct rb_global_entry *entry = rb_find_global_entry(id); |
511 | 0 | if (!entry) { |
512 | 0 | struct rb_global_variable *var; |
513 | 0 | entry = ALLOC(struct rb_global_entry); |
514 | 0 | var = ALLOC(struct rb_global_variable); |
515 | 0 | entry->id = id; |
516 | 0 | entry->var = var; |
517 | 0 | entry->ractor_local = false; |
518 | 0 | var->counter = 1; |
519 | 0 | var->data = 0; |
520 | 0 | var->getter = rb_gvar_undef_getter; |
521 | 0 | var->setter = rb_gvar_undef_setter; |
522 | 0 | var->marker = rb_gvar_undef_marker; |
523 | 0 | var->compactor = rb_gvar_undef_compactor; |
524 | |
|
525 | 0 | var->block_trace = 0; |
526 | 0 | var->trace = 0; |
527 | 0 | rb_id_table_insert(rb_global_tbl, id, (VALUE)entry); |
528 | 0 | } |
529 | 0 | return entry; |
530 | 0 | } |
531 | | |
532 | | VALUE |
533 | | rb_gvar_undef_getter(ID id, VALUE *_) |
534 | 0 | { |
535 | 0 | rb_warning("global variable `%"PRIsVALUE"' not initialized", QUOTE_ID(id)); |
536 | |
|
537 | 0 | return Qnil; |
538 | 0 | } |
539 | | |
540 | | static void |
541 | | rb_gvar_val_compactor(void *_var) |
542 | 0 | { |
543 | 0 | struct rb_global_variable *var = (struct rb_global_variable *)_var; |
544 | |
|
545 | 0 | VALUE obj = (VALUE)var->data; |
546 | |
|
547 | 0 | if (obj) { |
548 | 0 | VALUE new = rb_gc_location(obj); |
549 | 0 | if (new != obj) { |
550 | 0 | var->data = (void*)new; |
551 | 0 | } |
552 | 0 | } |
553 | 0 | } |
554 | | |
555 | | void |
556 | | rb_gvar_undef_setter(VALUE val, ID id, VALUE *_) |
557 | 0 | { |
558 | 0 | struct rb_global_variable *var = rb_global_entry(id)->var; |
559 | 0 | var->getter = rb_gvar_val_getter; |
560 | 0 | var->setter = rb_gvar_val_setter; |
561 | 0 | var->marker = rb_gvar_val_marker; |
562 | 0 | var->compactor = rb_gvar_val_compactor; |
563 | |
|
564 | 0 | var->data = (void*)val; |
565 | 0 | } |
566 | | |
567 | | void |
568 | | rb_gvar_undef_marker(VALUE *var) |
569 | 0 | { |
570 | 0 | } |
571 | | |
572 | | VALUE |
573 | | rb_gvar_val_getter(ID id, VALUE *data) |
574 | 0 | { |
575 | 0 | return (VALUE)data; |
576 | 0 | } |
577 | | |
578 | | void |
579 | | rb_gvar_val_setter(VALUE val, ID id, VALUE *_) |
580 | 0 | { |
581 | 0 | struct rb_global_variable *var = rb_global_entry(id)->var; |
582 | 0 | var->data = (void*)val; |
583 | 0 | } |
584 | | |
585 | | void |
586 | | rb_gvar_val_marker(VALUE *var) |
587 | 0 | { |
588 | 0 | VALUE data = (VALUE)var; |
589 | 0 | if (data) rb_gc_mark_movable(data); |
590 | 0 | } |
591 | | |
592 | | VALUE |
593 | | rb_gvar_var_getter(ID id, VALUE *var) |
594 | 0 | { |
595 | 0 | if (!var) return Qnil; |
596 | 0 | return *var; |
597 | 0 | } |
598 | | |
599 | | void |
600 | | rb_gvar_var_setter(VALUE val, ID id, VALUE *data) |
601 | 0 | { |
602 | 0 | *data = val; |
603 | 0 | } |
604 | | |
605 | | void |
606 | | rb_gvar_var_marker(VALUE *var) |
607 | 0 | { |
608 | 0 | if (var) rb_gc_mark_maybe(*var); |
609 | 0 | } |
610 | | |
611 | | void |
612 | | rb_gvar_readonly_setter(VALUE v, ID id, VALUE *_) |
613 | 0 | { |
614 | 0 | rb_name_error(id, "%"PRIsVALUE" is a read-only variable", QUOTE_ID(id)); |
615 | 0 | } |
616 | | |
617 | | static enum rb_id_table_iterator_result |
618 | | mark_global_entry(VALUE v, void *ignored) |
619 | 0 | { |
620 | 0 | struct rb_global_entry *entry = (struct rb_global_entry *)v; |
621 | 0 | struct trace_var *trace; |
622 | 0 | struct rb_global_variable *var = entry->var; |
623 | |
|
624 | 0 | (*var->marker)(var->data); |
625 | 0 | trace = var->trace; |
626 | 0 | while (trace) { |
627 | 0 | if (trace->data) rb_gc_mark_maybe(trace->data); |
628 | 0 | trace = trace->next; |
629 | 0 | } |
630 | 0 | return ID_TABLE_CONTINUE; |
631 | 0 | } |
632 | | |
633 | | #define gc_mark_table(task) \ |
634 | 0 | if (rb_global_tbl) { rb_id_table_foreach_values(rb_global_tbl, task##_global_entry, 0); } |
635 | | |
636 | | void |
637 | | rb_gc_mark_global_tbl(void) |
638 | 0 | { |
639 | 0 | gc_mark_table(mark); |
640 | 0 | } |
641 | | |
642 | | static enum rb_id_table_iterator_result |
643 | | update_global_entry(VALUE v, void *ignored) |
644 | 0 | { |
645 | 0 | struct rb_global_entry *entry = (struct rb_global_entry *)v; |
646 | 0 | struct rb_global_variable *var = entry->var; |
647 | |
|
648 | 0 | (*var->compactor)(var); |
649 | 0 | return ID_TABLE_CONTINUE; |
650 | 0 | } |
651 | | |
652 | | void |
653 | | rb_gc_update_global_tbl(void) |
654 | 0 | { |
655 | 0 | gc_mark_table(update); |
656 | 0 | } |
657 | | |
658 | | static ID |
659 | | global_id(const char *name) |
660 | 0 | { |
661 | 0 | ID id; |
662 | |
|
663 | 0 | if (name[0] == '$') id = rb_intern(name); |
664 | 0 | else { |
665 | 0 | size_t len = strlen(name); |
666 | 0 | VALUE vbuf = 0; |
667 | 0 | char *buf = ALLOCV_N(char, vbuf, len+1); |
668 | 0 | buf[0] = '$'; |
669 | 0 | memcpy(buf+1, name, len); |
670 | 0 | id = rb_intern2(buf, len+1); |
671 | 0 | ALLOCV_END(vbuf); |
672 | 0 | } |
673 | 0 | return id; |
674 | 0 | } |
675 | | |
676 | | static ID |
677 | | find_global_id(const char *name) |
678 | 0 | { |
679 | 0 | ID id; |
680 | 0 | size_t len = strlen(name); |
681 | |
|
682 | 0 | if (name[0] == '$') { |
683 | 0 | id = rb_check_id_cstr(name, len, NULL); |
684 | 0 | } |
685 | 0 | else { |
686 | 0 | VALUE vbuf = 0; |
687 | 0 | char *buf = ALLOCV_N(char, vbuf, len+1); |
688 | 0 | buf[0] = '$'; |
689 | 0 | memcpy(buf+1, name, len); |
690 | 0 | id = rb_check_id_cstr(buf, len+1, NULL); |
691 | 0 | ALLOCV_END(vbuf); |
692 | 0 | } |
693 | |
|
694 | 0 | return id; |
695 | 0 | } |
696 | | |
697 | | void |
698 | | rb_define_hooked_variable( |
699 | | const char *name, |
700 | | VALUE *var, |
701 | | rb_gvar_getter_t *getter, |
702 | | rb_gvar_setter_t *setter) |
703 | 0 | { |
704 | 0 | volatile VALUE tmp = var ? *var : Qnil; |
705 | 0 | ID id = global_id(name); |
706 | 0 | struct rb_global_variable *gvar = rb_global_entry(id)->var; |
707 | |
|
708 | 0 | gvar->data = (void*)var; |
709 | 0 | gvar->getter = getter ? (rb_gvar_getter_t *)getter : rb_gvar_var_getter; |
710 | 0 | gvar->setter = setter ? (rb_gvar_setter_t *)setter : rb_gvar_var_setter; |
711 | 0 | gvar->marker = rb_gvar_var_marker; |
712 | |
|
713 | 0 | RB_GC_GUARD(tmp); |
714 | 0 | } |
715 | | |
716 | | void |
717 | | rb_define_variable(const char *name, VALUE *var) |
718 | 0 | { |
719 | 0 | rb_define_hooked_variable(name, var, 0, 0); |
720 | 0 | } |
721 | | |
722 | | void |
723 | | rb_define_readonly_variable(const char *name, const VALUE *var) |
724 | 0 | { |
725 | 0 | rb_define_hooked_variable(name, (VALUE *)var, 0, rb_gvar_readonly_setter); |
726 | 0 | } |
727 | | |
728 | | void |
729 | | rb_define_virtual_variable( |
730 | | const char *name, |
731 | | rb_gvar_getter_t *getter, |
732 | | rb_gvar_setter_t *setter) |
733 | 0 | { |
734 | 0 | if (!getter) getter = rb_gvar_val_getter; |
735 | 0 | if (!setter) setter = rb_gvar_readonly_setter; |
736 | 0 | rb_define_hooked_variable(name, 0, getter, setter); |
737 | 0 | } |
738 | | |
739 | | static void |
740 | | rb_trace_eval(VALUE cmd, VALUE val) |
741 | 0 | { |
742 | 0 | rb_eval_cmd_kw(cmd, rb_ary_new3(1, val), RB_NO_KEYWORDS); |
743 | 0 | } |
744 | | |
745 | | VALUE |
746 | | rb_f_trace_var(int argc, const VALUE *argv) |
747 | 0 | { |
748 | 0 | VALUE var, cmd; |
749 | 0 | struct rb_global_entry *entry; |
750 | 0 | struct trace_var *trace; |
751 | |
|
752 | 0 | if (rb_scan_args(argc, argv, "11", &var, &cmd) == 1) { |
753 | 0 | cmd = rb_block_proc(); |
754 | 0 | } |
755 | 0 | if (NIL_P(cmd)) { |
756 | 0 | return rb_f_untrace_var(argc, argv); |
757 | 0 | } |
758 | 0 | entry = rb_global_entry(rb_to_id(var)); |
759 | 0 | trace = ALLOC(struct trace_var); |
760 | 0 | trace->next = entry->var->trace; |
761 | 0 | trace->func = rb_trace_eval; |
762 | 0 | trace->data = cmd; |
763 | 0 | trace->removed = 0; |
764 | 0 | entry->var->trace = trace; |
765 | |
|
766 | 0 | return Qnil; |
767 | 0 | } |
768 | | |
769 | | static void |
770 | | remove_trace(struct rb_global_variable *var) |
771 | 0 | { |
772 | 0 | struct trace_var *trace = var->trace; |
773 | 0 | struct trace_var t; |
774 | 0 | struct trace_var *next; |
775 | |
|
776 | 0 | t.next = trace; |
777 | 0 | trace = &t; |
778 | 0 | while (trace->next) { |
779 | 0 | next = trace->next; |
780 | 0 | if (next->removed) { |
781 | 0 | trace->next = next->next; |
782 | 0 | xfree(next); |
783 | 0 | } |
784 | 0 | else { |
785 | 0 | trace = next; |
786 | 0 | } |
787 | 0 | } |
788 | 0 | var->trace = t.next; |
789 | 0 | } |
790 | | |
791 | | VALUE |
792 | | rb_f_untrace_var(int argc, const VALUE *argv) |
793 | 0 | { |
794 | 0 | VALUE var, cmd; |
795 | 0 | ID id; |
796 | 0 | struct rb_global_entry *entry; |
797 | 0 | struct trace_var *trace; |
798 | |
|
799 | 0 | rb_scan_args(argc, argv, "11", &var, &cmd); |
800 | 0 | id = rb_check_id(&var); |
801 | 0 | if (!id) { |
802 | 0 | rb_name_error_str(var, "undefined global variable %"PRIsVALUE"", QUOTE(var)); |
803 | 0 | } |
804 | 0 | if ((entry = rb_find_global_entry(id)) == NULL) { |
805 | 0 | rb_name_error(id, "undefined global variable %"PRIsVALUE"", QUOTE_ID(id)); |
806 | 0 | } |
807 | | |
808 | 0 | trace = entry->var->trace; |
809 | 0 | if (NIL_P(cmd)) { |
810 | 0 | VALUE ary = rb_ary_new(); |
811 | |
|
812 | 0 | while (trace) { |
813 | 0 | struct trace_var *next = trace->next; |
814 | 0 | rb_ary_push(ary, (VALUE)trace->data); |
815 | 0 | trace->removed = 1; |
816 | 0 | trace = next; |
817 | 0 | } |
818 | |
|
819 | 0 | if (!entry->var->block_trace) remove_trace(entry->var); |
820 | 0 | return ary; |
821 | 0 | } |
822 | 0 | else { |
823 | 0 | while (trace) { |
824 | 0 | if (trace->data == cmd) { |
825 | 0 | trace->removed = 1; |
826 | 0 | if (!entry->var->block_trace) remove_trace(entry->var); |
827 | 0 | return rb_ary_new3(1, cmd); |
828 | 0 | } |
829 | 0 | trace = trace->next; |
830 | 0 | } |
831 | 0 | } |
832 | 0 | return Qnil; |
833 | 0 | } |
834 | | |
835 | | struct trace_data { |
836 | | struct trace_var *trace; |
837 | | VALUE val; |
838 | | }; |
839 | | |
840 | | static VALUE |
841 | | trace_ev(VALUE v) |
842 | 0 | { |
843 | 0 | struct trace_data *data = (void *)v; |
844 | 0 | struct trace_var *trace = data->trace; |
845 | |
|
846 | 0 | while (trace) { |
847 | 0 | (*trace->func)(trace->data, data->val); |
848 | 0 | trace = trace->next; |
849 | 0 | } |
850 | |
|
851 | 0 | return Qnil; |
852 | 0 | } |
853 | | |
854 | | static VALUE |
855 | | trace_en(VALUE v) |
856 | 0 | { |
857 | 0 | struct rb_global_variable *var = (void *)v; |
858 | 0 | var->block_trace = 0; |
859 | 0 | remove_trace(var); |
860 | 0 | return Qnil; /* not reached */ |
861 | 0 | } |
862 | | |
863 | | static VALUE |
864 | | rb_gvar_set_entry(struct rb_global_entry *entry, VALUE val) |
865 | 0 | { |
866 | 0 | struct trace_data trace; |
867 | 0 | struct rb_global_variable *var = entry->var; |
868 | |
|
869 | 0 | (*var->setter)(val, entry->id, var->data); |
870 | |
|
871 | 0 | if (var->trace && !var->block_trace) { |
872 | 0 | var->block_trace = 1; |
873 | 0 | trace.trace = var->trace; |
874 | 0 | trace.val = val; |
875 | 0 | rb_ensure(trace_ev, (VALUE)&trace, trace_en, (VALUE)var); |
876 | 0 | } |
877 | 0 | return val; |
878 | 0 | } |
879 | | |
880 | | VALUE |
881 | | rb_gvar_set(ID id, VALUE val) |
882 | 0 | { |
883 | 0 | struct rb_global_entry *entry; |
884 | 0 | entry = rb_global_entry(id); |
885 | |
|
886 | 0 | return rb_gvar_set_entry(entry, val); |
887 | 0 | } |
888 | | |
889 | | VALUE |
890 | | rb_gv_set(const char *name, VALUE val) |
891 | 0 | { |
892 | 0 | return rb_gvar_set(global_id(name), val); |
893 | 0 | } |
894 | | |
895 | | VALUE |
896 | | rb_gvar_get(ID id) |
897 | 0 | { |
898 | 0 | struct rb_global_entry *entry = rb_global_entry(id); |
899 | 0 | struct rb_global_variable *var = entry->var; |
900 | 0 | return (*var->getter)(entry->id, var->data); |
901 | 0 | } |
902 | | |
903 | | VALUE |
904 | | rb_gv_get(const char *name) |
905 | 0 | { |
906 | 0 | ID id = find_global_id(name); |
907 | |
|
908 | 0 | if (!id) { |
909 | 0 | rb_warning("global variable `%s' not initialized", name); |
910 | 0 | return Qnil; |
911 | 0 | } |
912 | | |
913 | 0 | return rb_gvar_get(id); |
914 | 0 | } |
915 | | |
916 | | VALUE |
917 | | rb_gvar_defined(ID id) |
918 | 0 | { |
919 | 0 | struct rb_global_entry *entry = rb_global_entry(id); |
920 | 0 | return RBOOL(entry->var->getter != rb_gvar_undef_getter); |
921 | 0 | } |
922 | | |
923 | | rb_gvar_getter_t * |
924 | | rb_gvar_getter_function_of(ID id) |
925 | 0 | { |
926 | 0 | const struct rb_global_entry *entry = rb_global_entry(id); |
927 | 0 | return entry->var->getter; |
928 | 0 | } |
929 | | |
930 | | rb_gvar_setter_t * |
931 | | rb_gvar_setter_function_of(ID id) |
932 | 0 | { |
933 | 0 | const struct rb_global_entry *entry = rb_global_entry(id); |
934 | 0 | return entry->var->setter; |
935 | 0 | } |
936 | | |
937 | | static enum rb_id_table_iterator_result |
938 | | gvar_i(ID key, VALUE val, void *a) |
939 | 0 | { |
940 | 0 | VALUE ary = (VALUE)a; |
941 | 0 | rb_ary_push(ary, ID2SYM(key)); |
942 | 0 | return ID_TABLE_CONTINUE; |
943 | 0 | } |
944 | | |
945 | | VALUE |
946 | | rb_f_global_variables(void) |
947 | 0 | { |
948 | 0 | VALUE ary = rb_ary_new(); |
949 | 0 | VALUE sym, backref = rb_backref_get(); |
950 | |
|
951 | 0 | if (!rb_ractor_main_p()) { |
952 | 0 | rb_raise(rb_eRactorIsolationError, "can not access global variables from non-main Ractors"); |
953 | 0 | } |
954 | | |
955 | 0 | rb_id_table_foreach(rb_global_tbl, gvar_i, (void *)ary); |
956 | 0 | if (!NIL_P(backref)) { |
957 | 0 | char buf[2]; |
958 | 0 | int i, nmatch = rb_match_count(backref); |
959 | 0 | buf[0] = '$'; |
960 | 0 | for (i = 1; i <= nmatch; ++i) { |
961 | 0 | if (!RTEST(rb_reg_nth_defined(i, backref))) continue; |
962 | 0 | if (i < 10) { |
963 | | /* probably reused, make static ID */ |
964 | 0 | buf[1] = (char)(i + '0'); |
965 | 0 | sym = ID2SYM(rb_intern2(buf, 2)); |
966 | 0 | } |
967 | 0 | else { |
968 | | /* dynamic symbol */ |
969 | 0 | sym = rb_str_intern(rb_sprintf("$%d", i)); |
970 | 0 | } |
971 | 0 | rb_ary_push(ary, sym); |
972 | 0 | } |
973 | 0 | } |
974 | 0 | return ary; |
975 | 0 | } |
976 | | |
977 | | void |
978 | | rb_alias_variable(ID name1, ID name2) |
979 | 0 | { |
980 | 0 | struct rb_global_entry *entry1, *entry2; |
981 | 0 | VALUE data1; |
982 | 0 | struct rb_id_table *gtbl = rb_global_tbl; |
983 | |
|
984 | 0 | if (!rb_ractor_main_p()) { |
985 | 0 | rb_raise(rb_eRactorIsolationError, "can not access global variables from non-main Ractors"); |
986 | 0 | } |
987 | | |
988 | 0 | entry2 = rb_global_entry(name2); |
989 | 0 | if (!rb_id_table_lookup(gtbl, name1, &data1)) { |
990 | 0 | entry1 = ALLOC(struct rb_global_entry); |
991 | 0 | entry1->id = name1; |
992 | 0 | rb_id_table_insert(gtbl, name1, (VALUE)entry1); |
993 | 0 | } |
994 | 0 | else if ((entry1 = (struct rb_global_entry *)data1)->var != entry2->var) { |
995 | 0 | struct rb_global_variable *var = entry1->var; |
996 | 0 | if (var->block_trace) { |
997 | 0 | rb_raise(rb_eRuntimeError, "can't alias in tracer"); |
998 | 0 | } |
999 | 0 | var->counter--; |
1000 | 0 | if (var->counter == 0) { |
1001 | 0 | struct trace_var *trace = var->trace; |
1002 | 0 | while (trace) { |
1003 | 0 | struct trace_var *next = trace->next; |
1004 | 0 | xfree(trace); |
1005 | 0 | trace = next; |
1006 | 0 | } |
1007 | 0 | xfree(var); |
1008 | 0 | } |
1009 | 0 | } |
1010 | 0 | else { |
1011 | 0 | return; |
1012 | 0 | } |
1013 | 0 | entry2->var->counter++; |
1014 | 0 | entry1->var = entry2->var; |
1015 | 0 | } |
1016 | | |
1017 | | static void |
1018 | | IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(ID id) |
1019 | 0 | { |
1020 | 0 | if (UNLIKELY(!rb_ractor_main_p())) { |
1021 | 0 | if (rb_is_instance_id(id)) { // check only normal ivars |
1022 | 0 | rb_raise(rb_eRactorIsolationError, "can not set instance variables of classes/modules by non-main Ractors"); |
1023 | 0 | } |
1024 | 0 | } |
1025 | 0 | } |
1026 | | |
1027 | | #define CVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR() \ |
1028 | 0 | if (UNLIKELY(!rb_ractor_main_p())) { \ |
1029 | 0 | rb_raise(rb_eRactorIsolationError, "can not access class variables from non-main Ractors"); \ |
1030 | 0 | } |
1031 | | |
1032 | | static inline struct st_table * |
1033 | | generic_ivtbl(VALUE obj, ID id, bool force_check_ractor) |
1034 | 0 | { |
1035 | 0 | ASSERT_vm_locking(); |
1036 | |
|
1037 | 0 | if ((force_check_ractor || LIKELY(rb_is_instance_id(id)) /* not internal ID */ ) && |
1038 | 0 | !RB_OBJ_FROZEN_RAW(obj) && |
1039 | 0 | UNLIKELY(!rb_ractor_main_p()) && |
1040 | 0 | UNLIKELY(rb_ractor_shareable_p(obj))) { |
1041 | |
|
1042 | 0 | rb_raise(rb_eRactorIsolationError, "can not access instance variables of shareable objects from non-main Ractors"); |
1043 | 0 | } |
1044 | 0 | return generic_iv_tbl_; |
1045 | 0 | } |
1046 | | |
1047 | | static inline struct st_table * |
1048 | | generic_ivtbl_no_ractor_check(VALUE obj) |
1049 | 0 | { |
1050 | 0 | return generic_ivtbl(obj, 0, false); |
1051 | 0 | } |
1052 | | |
1053 | | int |
1054 | | rb_gen_ivtbl_get(VALUE obj, ID id, struct gen_ivtbl **ivtbl) |
1055 | 0 | { |
1056 | 0 | RUBY_ASSERT(!RB_TYPE_P(obj, T_ICLASS)); |
1057 | |
|
1058 | 0 | st_data_t data; |
1059 | 0 | int r = 0; |
1060 | |
|
1061 | 0 | RB_VM_LOCK_ENTER(); |
1062 | 0 | { |
1063 | 0 | if (st_lookup(generic_ivtbl(obj, id, false), (st_data_t)obj, &data)) { |
1064 | 0 | *ivtbl = (struct gen_ivtbl *)data; |
1065 | 0 | r = 1; |
1066 | 0 | } |
1067 | 0 | } |
1068 | 0 | RB_VM_LOCK_LEAVE(); |
1069 | |
|
1070 | 0 | return r; |
1071 | 0 | } |
1072 | | |
1073 | | int |
1074 | | rb_ivar_generic_ivtbl_lookup(VALUE obj, struct gen_ivtbl **ivtbl) |
1075 | 0 | { |
1076 | 0 | return rb_gen_ivtbl_get(obj, 0, ivtbl); |
1077 | 0 | } |
1078 | | |
1079 | | static size_t |
1080 | | gen_ivtbl_bytes(size_t n) |
1081 | 0 | { |
1082 | 0 | return offsetof(struct gen_ivtbl, as.shape.ivptr) + n * sizeof(VALUE); |
1083 | 0 | } |
1084 | | |
1085 | | static struct gen_ivtbl * |
1086 | | gen_ivtbl_resize(struct gen_ivtbl *old, uint32_t n) |
1087 | 0 | { |
1088 | 0 | RUBY_ASSERT(n > 0); |
1089 | |
|
1090 | 0 | uint32_t len = old ? old->as.shape.numiv : 0; |
1091 | 0 | struct gen_ivtbl *ivtbl = xrealloc(old, gen_ivtbl_bytes(n)); |
1092 | |
|
1093 | 0 | ivtbl->as.shape.numiv = n; |
1094 | 0 | for (; len < n; len++) { |
1095 | 0 | ivtbl->as.shape.ivptr[len] = Qundef; |
1096 | 0 | } |
1097 | |
|
1098 | 0 | return ivtbl; |
1099 | 0 | } |
1100 | | |
1101 | | void |
1102 | | rb_mark_generic_ivar(VALUE obj) |
1103 | 0 | { |
1104 | 0 | struct gen_ivtbl *ivtbl; |
1105 | |
|
1106 | 0 | if (rb_gen_ivtbl_get(obj, 0, &ivtbl)) { |
1107 | 0 | if (rb_shape_obj_too_complex(obj)) { |
1108 | 0 | rb_mark_tbl_no_pin(ivtbl->as.complex.table); |
1109 | 0 | } |
1110 | 0 | else { |
1111 | 0 | for (uint32_t i = 0; i < ivtbl->as.shape.numiv; i++) { |
1112 | 0 | rb_gc_mark_movable(ivtbl->as.shape.ivptr[i]); |
1113 | 0 | } |
1114 | 0 | } |
1115 | 0 | } |
1116 | 0 | } |
1117 | | |
1118 | | void |
1119 | | rb_ref_update_generic_ivar(VALUE obj) |
1120 | 0 | { |
1121 | 0 | struct gen_ivtbl *ivtbl; |
1122 | |
|
1123 | 0 | if (rb_gen_ivtbl_get(obj, 0, &ivtbl)) { |
1124 | 0 | if (rb_shape_obj_too_complex(obj)) { |
1125 | 0 | rb_gc_ref_update_table_values_only(ivtbl->as.complex.table); |
1126 | 0 | } |
1127 | 0 | else { |
1128 | 0 | for (uint32_t i = 0; i < ivtbl->as.shape.numiv; i++) { |
1129 | 0 | ivtbl->as.shape.ivptr[i] = rb_gc_location(ivtbl->as.shape.ivptr[i]); |
1130 | 0 | } |
1131 | 0 | } |
1132 | 0 | } |
1133 | 0 | } |
1134 | | |
1135 | | void |
1136 | | rb_mv_generic_ivar(VALUE rsrc, VALUE dst) |
1137 | 0 | { |
1138 | 0 | st_data_t key = (st_data_t)rsrc; |
1139 | 0 | st_data_t ivtbl; |
1140 | |
|
1141 | 0 | if (st_delete(generic_ivtbl_no_ractor_check(rsrc), &key, &ivtbl)) |
1142 | 0 | st_insert(generic_ivtbl_no_ractor_check(dst), (st_data_t)dst, ivtbl); |
1143 | 0 | } |
1144 | | |
1145 | | void |
1146 | | rb_free_generic_ivar(VALUE obj) |
1147 | 0 | { |
1148 | 0 | st_data_t key = (st_data_t)obj, value; |
1149 | |
|
1150 | 0 | bool too_complex = rb_shape_obj_too_complex(obj); |
1151 | |
|
1152 | 0 | if (st_delete(generic_ivtbl_no_ractor_check(obj), &key, &value)) { |
1153 | 0 | struct gen_ivtbl *ivtbl = (struct gen_ivtbl *)value; |
1154 | |
|
1155 | 0 | if (UNLIKELY(too_complex)) { |
1156 | 0 | st_free_table(ivtbl->as.complex.table); |
1157 | 0 | } |
1158 | |
|
1159 | 0 | xfree(ivtbl); |
1160 | 0 | } |
1161 | 0 | } |
1162 | | |
1163 | | RUBY_FUNC_EXPORTED size_t |
1164 | | rb_generic_ivar_memsize(VALUE obj) |
1165 | 0 | { |
1166 | 0 | struct gen_ivtbl *ivtbl; |
1167 | |
|
1168 | 0 | if (rb_gen_ivtbl_get(obj, 0, &ivtbl)) { |
1169 | 0 | if (rb_shape_obj_too_complex(obj)) { |
1170 | 0 | return sizeof(struct gen_ivtbl) + st_memsize(ivtbl->as.complex.table); |
1171 | 0 | } |
1172 | 0 | else { |
1173 | 0 | return gen_ivtbl_bytes(ivtbl->as.shape.numiv); |
1174 | 0 | } |
1175 | 0 | } |
1176 | 0 | return 0; |
1177 | 0 | } |
1178 | | |
1179 | | #if !SHAPE_IN_BASIC_FLAGS |
1180 | | shape_id_t |
1181 | | rb_generic_shape_id(VALUE obj) |
1182 | | { |
1183 | | struct gen_ivtbl *ivtbl = 0; |
1184 | | shape_id_t shape_id = 0; |
1185 | | |
1186 | | RB_VM_LOCK_ENTER(); |
1187 | | { |
1188 | | st_table* global_iv_table = generic_ivtbl(obj, 0, false); |
1189 | | |
1190 | | if (global_iv_table && st_lookup(global_iv_table, obj, (st_data_t *)&ivtbl)) { |
1191 | | shape_id = ivtbl->shape_id; |
1192 | | } |
1193 | | else if (OBJ_FROZEN(obj)) { |
1194 | | shape_id = SPECIAL_CONST_SHAPE_ID; |
1195 | | } |
1196 | | } |
1197 | | RB_VM_LOCK_LEAVE(); |
1198 | | |
1199 | | return shape_id; |
1200 | | } |
1201 | | #endif |
1202 | | |
1203 | | static size_t |
1204 | | gen_ivtbl_count(VALUE obj, const struct gen_ivtbl *ivtbl) |
1205 | 0 | { |
1206 | 0 | uint32_t i; |
1207 | 0 | size_t n = 0; |
1208 | |
|
1209 | 0 | if (rb_shape_obj_too_complex(obj)) { |
1210 | 0 | n = st_table_size(ivtbl->as.complex.table); |
1211 | 0 | } |
1212 | 0 | else { |
1213 | 0 | for (i = 0; i < ivtbl->as.shape.numiv; i++) { |
1214 | 0 | if (!UNDEF_P(ivtbl->as.shape.ivptr[i])) { |
1215 | 0 | n++; |
1216 | 0 | } |
1217 | 0 | } |
1218 | 0 | } |
1219 | |
|
1220 | 0 | return n; |
1221 | 0 | } |
1222 | | |
1223 | | VALUE |
1224 | | rb_ivar_lookup(VALUE obj, ID id, VALUE undef) |
1225 | 0 | { |
1226 | 0 | if (SPECIAL_CONST_P(obj)) return undef; |
1227 | | |
1228 | 0 | shape_id_t shape_id; |
1229 | 0 | VALUE * ivar_list; |
1230 | 0 | rb_shape_t * shape; |
1231 | |
|
1232 | 0 | #if SHAPE_IN_BASIC_FLAGS |
1233 | 0 | shape_id = RBASIC_SHAPE_ID(obj); |
1234 | 0 | #endif |
1235 | |
|
1236 | 0 | switch (BUILTIN_TYPE(obj)) { |
1237 | 0 | case T_CLASS: |
1238 | 0 | case T_MODULE: |
1239 | 0 | { |
1240 | 0 | bool found = false; |
1241 | 0 | VALUE val; |
1242 | |
|
1243 | 0 | RB_VM_LOCK_ENTER(); |
1244 | 0 | { |
1245 | | #if !SHAPE_IN_BASIC_FLAGS |
1246 | | shape_id = RCLASS_SHAPE_ID(obj); |
1247 | | #endif |
1248 | |
|
1249 | 0 | if (rb_shape_obj_too_complex(obj)) { |
1250 | 0 | st_table * iv_table = RCLASS_IV_HASH(obj); |
1251 | 0 | if (rb_st_lookup(iv_table, (st_data_t)id, (st_data_t *)&val)) { |
1252 | 0 | found = true; |
1253 | 0 | } |
1254 | 0 | else { |
1255 | 0 | val = undef; |
1256 | 0 | } |
1257 | 0 | } |
1258 | 0 | else { |
1259 | 0 | attr_index_t index = 0; |
1260 | 0 | shape = rb_shape_get_shape_by_id(shape_id); |
1261 | 0 | found = rb_shape_get_iv_index(shape, id, &index); |
1262 | |
|
1263 | 0 | if (found) { |
1264 | 0 | ivar_list = RCLASS_IVPTR(obj); |
1265 | 0 | RUBY_ASSERT(ivar_list); |
1266 | |
|
1267 | 0 | val = ivar_list[index]; |
1268 | 0 | } |
1269 | 0 | else { |
1270 | 0 | val = undef; |
1271 | 0 | } |
1272 | 0 | } |
1273 | 0 | } |
1274 | 0 | RB_VM_LOCK_LEAVE(); |
1275 | |
|
1276 | 0 | if (found && |
1277 | 0 | rb_is_instance_id(id) && |
1278 | 0 | UNLIKELY(!rb_ractor_main_p()) && |
1279 | 0 | !rb_ractor_shareable_p(val)) { |
1280 | 0 | rb_raise(rb_eRactorIsolationError, |
1281 | 0 | "can not get unshareable values from instance variables of classes/modules from non-main Ractors"); |
1282 | 0 | } |
1283 | 0 | return val; |
1284 | 0 | } |
1285 | 0 | case T_OBJECT: |
1286 | 0 | { |
1287 | | #if !SHAPE_IN_BASIC_FLAGS |
1288 | | shape_id = ROBJECT_SHAPE_ID(obj); |
1289 | | #endif |
1290 | 0 | if (rb_shape_obj_too_complex(obj)) { |
1291 | 0 | st_table * iv_table = ROBJECT_IV_HASH(obj); |
1292 | 0 | VALUE val; |
1293 | 0 | if (rb_st_lookup(iv_table, (st_data_t)id, (st_data_t *)&val)) { |
1294 | 0 | return val; |
1295 | 0 | } |
1296 | 0 | else { |
1297 | 0 | return undef; |
1298 | 0 | } |
1299 | 0 | } |
1300 | | |
1301 | 0 | RUBY_ASSERT(!rb_shape_obj_too_complex(obj)); |
1302 | 0 | ivar_list = ROBJECT_IVPTR(obj); |
1303 | 0 | break; |
1304 | 0 | } |
1305 | 0 | default: |
1306 | 0 | if (FL_TEST_RAW(obj, FL_EXIVAR)) { |
1307 | 0 | struct gen_ivtbl *ivtbl; |
1308 | 0 | rb_gen_ivtbl_get(obj, id, &ivtbl); |
1309 | |
|
1310 | 0 | if (rb_shape_obj_too_complex(obj)) { |
1311 | 0 | VALUE val; |
1312 | 0 | if (rb_st_lookup(ivtbl->as.complex.table, (st_data_t)id, (st_data_t *)&val)) { |
1313 | 0 | return val; |
1314 | 0 | } |
1315 | 0 | else { |
1316 | 0 | return undef; |
1317 | 0 | } |
1318 | 0 | } |
1319 | | |
1320 | | #if !SHAPE_IN_BASIC_FLAGS |
1321 | | shape_id = ivtbl->shape_id; |
1322 | | #endif |
1323 | 0 | ivar_list = ivtbl->as.shape.ivptr; |
1324 | 0 | } |
1325 | 0 | else { |
1326 | 0 | return undef; |
1327 | 0 | } |
1328 | 0 | break; |
1329 | 0 | } |
1330 | | |
1331 | 0 | attr_index_t index = 0; |
1332 | 0 | shape = rb_shape_get_shape_by_id(shape_id); |
1333 | 0 | if (rb_shape_get_iv_index(shape, id, &index)) { |
1334 | 0 | return ivar_list[index]; |
1335 | 0 | } |
1336 | | |
1337 | 0 | return undef; |
1338 | 0 | } |
1339 | | |
1340 | | VALUE |
1341 | | rb_ivar_get(VALUE obj, ID id) |
1342 | 0 | { |
1343 | 0 | VALUE iv = rb_ivar_lookup(obj, id, Qnil); |
1344 | 0 | RB_DEBUG_COUNTER_INC(ivar_get_base); |
1345 | 0 | return iv; |
1346 | 0 | } |
1347 | | |
1348 | | VALUE |
1349 | | rb_attr_get(VALUE obj, ID id) |
1350 | 0 | { |
1351 | 0 | return rb_ivar_lookup(obj, id, Qnil); |
1352 | 0 | } |
1353 | | |
1354 | | static VALUE |
1355 | | rb_ivar_delete(VALUE obj, ID id, VALUE undef) |
1356 | 0 | { |
1357 | 0 | rb_check_frozen(obj); |
1358 | |
|
1359 | 0 | VALUE val = undef; |
1360 | 0 | rb_shape_t *shape = rb_shape_get_shape(obj); |
1361 | |
|
1362 | 0 | if (BUILTIN_TYPE(obj) == T_CLASS || BUILTIN_TYPE(obj) == T_MODULE) { |
1363 | 0 | IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(id); |
1364 | 0 | } |
1365 | |
|
1366 | 0 | if (!rb_shape_transition_shape_remove_ivar(obj, id, shape, &val)) { |
1367 | 0 | if (!rb_shape_obj_too_complex(obj)) { |
1368 | 0 | rb_evict_ivars_to_hash(obj); |
1369 | 0 | } |
1370 | |
|
1371 | 0 | st_table *table = NULL; |
1372 | 0 | switch (BUILTIN_TYPE(obj)) { |
1373 | 0 | case T_CLASS: |
1374 | 0 | case T_MODULE: |
1375 | 0 | table = RCLASS_IV_HASH(obj); |
1376 | 0 | break; |
1377 | | |
1378 | 0 | case T_OBJECT: |
1379 | 0 | table = ROBJECT_IV_HASH(obj); |
1380 | 0 | break; |
1381 | | |
1382 | 0 | default: { |
1383 | 0 | struct gen_ivtbl *ivtbl; |
1384 | 0 | if (rb_gen_ivtbl_get(obj, 0, &ivtbl)) { |
1385 | 0 | table = ivtbl->as.complex.table; |
1386 | 0 | } |
1387 | 0 | break; |
1388 | 0 | } |
1389 | 0 | } |
1390 | | |
1391 | 0 | if (table) { |
1392 | 0 | if (!st_delete(table, (st_data_t *)&id, (st_data_t *)&val)) { |
1393 | 0 | val = undef; |
1394 | 0 | } |
1395 | 0 | } |
1396 | 0 | } |
1397 | | |
1398 | 0 | return val; |
1399 | 0 | } |
1400 | | |
1401 | | VALUE |
1402 | | rb_attr_delete(VALUE obj, ID id) |
1403 | 0 | { |
1404 | 0 | return rb_ivar_delete(obj, id, Qnil); |
1405 | 0 | } |
1406 | | |
1407 | | void |
1408 | | rb_obj_convert_to_too_complex(VALUE obj, st_table *table) |
1409 | 0 | { |
1410 | 0 | RUBY_ASSERT(!rb_shape_obj_too_complex(obj)); |
1411 | |
|
1412 | 0 | VALUE *old_ivptr = NULL; |
1413 | |
|
1414 | 0 | switch (BUILTIN_TYPE(obj)) { |
1415 | 0 | case T_OBJECT: |
1416 | 0 | if (!(RBASIC(obj)->flags & ROBJECT_EMBED)) { |
1417 | 0 | old_ivptr = ROBJECT_IVPTR(obj); |
1418 | 0 | } |
1419 | 0 | rb_shape_set_shape_id(obj, OBJ_TOO_COMPLEX_SHAPE_ID); |
1420 | 0 | ROBJECT_SET_IV_HASH(obj, table); |
1421 | 0 | break; |
1422 | 0 | case T_CLASS: |
1423 | 0 | case T_MODULE: |
1424 | 0 | old_ivptr = RCLASS_IVPTR(obj); |
1425 | 0 | rb_shape_set_shape_id(obj, OBJ_TOO_COMPLEX_SHAPE_ID); |
1426 | 0 | RCLASS_SET_IV_HASH(obj, table); |
1427 | 0 | break; |
1428 | 0 | default: |
1429 | 0 | RB_VM_LOCK_ENTER(); |
1430 | 0 | { |
1431 | 0 | struct st_table *gen_ivs = generic_ivtbl_no_ractor_check(obj); |
1432 | |
|
1433 | 0 | struct gen_ivtbl *old_ivtbl = NULL; |
1434 | 0 | st_lookup(gen_ivs, (st_data_t)obj, (st_data_t *)&old_ivtbl); |
1435 | |
|
1436 | 0 | if (old_ivtbl) { |
1437 | | /* We need to modify old_ivtbl to have the too complex shape |
1438 | | * and hold the table because the xmalloc could trigger a GC |
1439 | | * compaction. We want the table to be updated rather than than |
1440 | | * the original ivptr. */ |
1441 | 0 | #if SHAPE_IN_BASIC_FLAGS |
1442 | 0 | rb_shape_set_shape_id(obj, OBJ_TOO_COMPLEX_SHAPE_ID); |
1443 | | #else |
1444 | | old_ivtbl->shape_id = OBJ_TOO_COMPLEX_SHAPE_ID; |
1445 | | #endif |
1446 | 0 | old_ivtbl->as.complex.table = table; |
1447 | 0 | old_ivptr = (VALUE *)old_ivtbl; |
1448 | 0 | } |
1449 | |
|
1450 | 0 | struct gen_ivtbl *ivtbl = xmalloc(sizeof(struct gen_ivtbl)); |
1451 | 0 | ivtbl->as.complex.table = table; |
1452 | 0 | st_insert(gen_ivs, (st_data_t)obj, (st_data_t)ivtbl); |
1453 | 0 | #if SHAPE_IN_BASIC_FLAGS |
1454 | 0 | rb_shape_set_shape_id(obj, OBJ_TOO_COMPLEX_SHAPE_ID); |
1455 | | #else |
1456 | | ivtbl->shape_id = OBJ_TOO_COMPLEX_SHAPE_ID; |
1457 | | #endif |
1458 | 0 | } |
1459 | 0 | RB_VM_LOCK_LEAVE(); |
1460 | 0 | } |
1461 | | |
1462 | 0 | xfree(old_ivptr); |
1463 | 0 | } |
1464 | | |
1465 | | void |
1466 | | rb_evict_ivars_to_hash(VALUE obj) |
1467 | 0 | { |
1468 | 0 | RUBY_ASSERT(!rb_shape_obj_too_complex(obj)); |
1469 | |
|
1470 | 0 | st_table *table = st_init_numtable_with_size(rb_ivar_count(obj)); |
1471 | | |
1472 | | // Evacuate all previous values from shape into id_table |
1473 | 0 | rb_obj_copy_ivs_to_hash_table(obj, table); |
1474 | 0 | rb_obj_convert_to_too_complex(obj, table); |
1475 | |
|
1476 | 0 | RUBY_ASSERT(rb_shape_obj_too_complex(obj)); |
1477 | 0 | } |
1478 | | |
1479 | | struct general_ivar_set_result { |
1480 | | attr_index_t index; |
1481 | | bool existing; |
1482 | | }; |
1483 | | |
1484 | | static struct general_ivar_set_result |
1485 | | general_ivar_set(VALUE obj, ID id, VALUE val, void *data, |
1486 | | VALUE *(*shape_ivptr_func)(VALUE, void *), |
1487 | | void (*shape_resize_ivptr_func)(VALUE, attr_index_t, attr_index_t, void *), |
1488 | | void (*set_shape_func)(VALUE, rb_shape_t *, void *), |
1489 | | void (*transition_too_complex_func)(VALUE, void *), |
1490 | | st_table *(*too_complex_table_func)(VALUE, void *)) |
1491 | 0 | { |
1492 | 0 | struct general_ivar_set_result result = { |
1493 | 0 | .index = 0, |
1494 | 0 | .existing = true |
1495 | 0 | }; |
1496 | |
|
1497 | 0 | rb_shape_t *current_shape = rb_shape_get_shape(obj); |
1498 | |
|
1499 | 0 | if (UNLIKELY(current_shape->type == SHAPE_OBJ_TOO_COMPLEX)) { |
1500 | 0 | goto too_complex; |
1501 | 0 | } |
1502 | | |
1503 | 0 | attr_index_t index; |
1504 | 0 | if (!rb_shape_get_iv_index(current_shape, id, &index)) { |
1505 | 0 | result.existing = false; |
1506 | |
|
1507 | 0 | index = current_shape->next_iv_index; |
1508 | 0 | if (index >= MAX_IVARS) { |
1509 | 0 | rb_raise(rb_eArgError, "too many instance variables"); |
1510 | 0 | } |
1511 | | |
1512 | 0 | rb_shape_t *next_shape = rb_shape_get_next(current_shape, obj, id); |
1513 | 0 | if (UNLIKELY(next_shape->type == SHAPE_OBJ_TOO_COMPLEX)) { |
1514 | 0 | transition_too_complex_func(obj, data); |
1515 | 0 | goto too_complex; |
1516 | 0 | } |
1517 | 0 | else if (UNLIKELY(next_shape->capacity != current_shape->capacity)) { |
1518 | 0 | RUBY_ASSERT(next_shape->capacity > current_shape->capacity); |
1519 | 0 | shape_resize_ivptr_func(obj, current_shape->capacity, next_shape->capacity, data); |
1520 | 0 | } |
1521 | | |
1522 | 0 | RUBY_ASSERT(next_shape->type == SHAPE_IVAR); |
1523 | 0 | RUBY_ASSERT(index == (next_shape->next_iv_index - 1)); |
1524 | 0 | set_shape_func(obj, next_shape, data); |
1525 | 0 | } |
1526 | | |
1527 | 0 | VALUE *table = shape_ivptr_func(obj, data); |
1528 | 0 | RB_OBJ_WRITE(obj, &table[index], val); |
1529 | |
|
1530 | 0 | result.index = index; |
1531 | 0 | return result; |
1532 | | |
1533 | 0 | too_complex: |
1534 | 0 | { |
1535 | 0 | RUBY_ASSERT(rb_shape_obj_too_complex(obj)); |
1536 | |
|
1537 | 0 | st_table *table = too_complex_table_func(obj, data); |
1538 | 0 | result.existing = st_insert(table, (st_data_t)id, (st_data_t)val); |
1539 | 0 | result.index = 0; |
1540 | 0 | RB_OBJ_WRITTEN(obj, Qundef, val); |
1541 | 0 | } |
1542 | 0 | return result; |
1543 | 0 | } |
1544 | | |
1545 | | struct gen_ivar_lookup_ensure_size { |
1546 | | VALUE obj; |
1547 | | ID id; |
1548 | | struct gen_ivtbl *ivtbl; |
1549 | | rb_shape_t *shape; |
1550 | | bool resize; |
1551 | | }; |
1552 | | |
1553 | | static int |
1554 | | generic_ivar_lookup_ensure_size(st_data_t *k, st_data_t *v, st_data_t u, int existing) |
1555 | 0 | { |
1556 | 0 | ASSERT_vm_locking(); |
1557 | |
|
1558 | 0 | struct gen_ivar_lookup_ensure_size *ivar_lookup = (struct gen_ivar_lookup_ensure_size *)u; |
1559 | 0 | struct gen_ivtbl *ivtbl = existing ? (struct gen_ivtbl *)*v : NULL; |
1560 | |
|
1561 | 0 | if (!existing || ivar_lookup->resize) { |
1562 | 0 | if (existing) { |
1563 | 0 | RUBY_ASSERT(ivar_lookup->shape->type == SHAPE_IVAR); |
1564 | 0 | RUBY_ASSERT(rb_shape_get_shape_by_id(ivar_lookup->shape->parent_id)->capacity < ivar_lookup->shape->capacity); |
1565 | 0 | } |
1566 | 0 | else { |
1567 | 0 | FL_SET_RAW((VALUE)*k, FL_EXIVAR); |
1568 | 0 | } |
1569 | |
|
1570 | 0 | ivtbl = gen_ivtbl_resize(ivtbl, ivar_lookup->shape->capacity); |
1571 | 0 | *v = (st_data_t)ivtbl; |
1572 | 0 | } |
1573 | |
|
1574 | 0 | RUBY_ASSERT(FL_TEST((VALUE)*k, FL_EXIVAR)); |
1575 | |
|
1576 | 0 | ivar_lookup->ivtbl = ivtbl; |
1577 | 0 | if (ivar_lookup->shape) { |
1578 | 0 | #if SHAPE_IN_BASIC_FLAGS |
1579 | 0 | rb_shape_set_shape(ivar_lookup->obj, ivar_lookup->shape); |
1580 | | #else |
1581 | | ivtbl->shape_id = rb_shape_id(ivar_lookup->shape); |
1582 | | #endif |
1583 | 0 | } |
1584 | |
|
1585 | 0 | return ST_CONTINUE; |
1586 | 0 | } |
1587 | | |
1588 | | static VALUE * |
1589 | | generic_ivar_set_shape_ivptr(VALUE obj, void *data) |
1590 | 0 | { |
1591 | 0 | RUBY_ASSERT(!rb_shape_obj_too_complex(obj)); |
1592 | |
|
1593 | 0 | struct gen_ivar_lookup_ensure_size *ivar_lookup = data; |
1594 | |
|
1595 | 0 | RB_VM_LOCK_ENTER(); |
1596 | 0 | { |
1597 | 0 | st_update(generic_ivtbl(obj, ivar_lookup->id, false), (st_data_t)obj, generic_ivar_lookup_ensure_size, (st_data_t)ivar_lookup); |
1598 | 0 | } |
1599 | 0 | RB_VM_LOCK_LEAVE(); |
1600 | |
|
1601 | 0 | FL_SET_RAW(obj, FL_EXIVAR); |
1602 | |
|
1603 | 0 | return ivar_lookup->ivtbl->as.shape.ivptr; |
1604 | 0 | } |
1605 | | |
1606 | | static void |
1607 | | generic_ivar_set_shape_resize_ivptr(VALUE obj, attr_index_t _old_capa, attr_index_t new_capa, void *data) |
1608 | 0 | { |
1609 | 0 | struct gen_ivar_lookup_ensure_size *ivar_lookup = data; |
1610 | |
|
1611 | 0 | ivar_lookup->resize = true; |
1612 | 0 | } |
1613 | | |
1614 | | static void |
1615 | | generic_ivar_set_set_shape(VALUE obj, rb_shape_t *shape, void *data) |
1616 | 0 | { |
1617 | 0 | struct gen_ivar_lookup_ensure_size *ivar_lookup = data; |
1618 | |
|
1619 | 0 | ivar_lookup->shape = shape; |
1620 | 0 | } |
1621 | | |
1622 | | static void |
1623 | | generic_ivar_set_transition_too_complex(VALUE obj, void *_data) |
1624 | 0 | { |
1625 | 0 | rb_evict_ivars_to_hash(obj); |
1626 | 0 | FL_SET_RAW(obj, FL_EXIVAR); |
1627 | 0 | } |
1628 | | |
1629 | | static st_table * |
1630 | | generic_ivar_set_too_complex_table(VALUE obj, void *data) |
1631 | 0 | { |
1632 | 0 | struct gen_ivar_lookup_ensure_size *ivar_lookup = data; |
1633 | |
|
1634 | 0 | struct gen_ivtbl *ivtbl; |
1635 | 0 | if (!rb_gen_ivtbl_get(obj, 0, &ivtbl)) { |
1636 | 0 | ivtbl = xmalloc(sizeof(struct gen_ivtbl)); |
1637 | | #if !SHAPE_IN_BASIC_FLAGS |
1638 | | ivtbl->shape_id = SHAPE_OBJ_TOO_COMPLEX; |
1639 | | #endif |
1640 | 0 | ivtbl->as.complex.table = st_init_numtable_with_size(1); |
1641 | |
|
1642 | 0 | RB_VM_LOCK_ENTER(); |
1643 | 0 | { |
1644 | 0 | st_insert(generic_ivtbl(obj, ivar_lookup->id, false), (st_data_t)obj, (st_data_t)ivtbl); |
1645 | 0 | } |
1646 | 0 | RB_VM_LOCK_LEAVE(); |
1647 | |
|
1648 | 0 | FL_SET_RAW(obj, FL_EXIVAR); |
1649 | 0 | } |
1650 | |
|
1651 | 0 | RUBY_ASSERT(rb_shape_obj_too_complex(obj)); |
1652 | |
|
1653 | 0 | return ivtbl->as.complex.table; |
1654 | 0 | } |
1655 | | |
1656 | | static void |
1657 | | generic_ivar_set(VALUE obj, ID id, VALUE val) |
1658 | 0 | { |
1659 | 0 | struct gen_ivar_lookup_ensure_size ivar_lookup = { |
1660 | 0 | .obj = obj, |
1661 | 0 | .id = id, |
1662 | 0 | .resize = false, |
1663 | 0 | .shape = NULL, |
1664 | 0 | }; |
1665 | |
|
1666 | 0 | general_ivar_set(obj, id, val, &ivar_lookup, |
1667 | 0 | generic_ivar_set_shape_ivptr, |
1668 | 0 | generic_ivar_set_shape_resize_ivptr, |
1669 | 0 | generic_ivar_set_set_shape, |
1670 | 0 | generic_ivar_set_transition_too_complex, |
1671 | 0 | generic_ivar_set_too_complex_table); |
1672 | 0 | } |
1673 | | |
1674 | | void |
1675 | | rb_ensure_iv_list_size(VALUE obj, uint32_t current_capacity, uint32_t new_capacity) |
1676 | 0 | { |
1677 | 0 | RUBY_ASSERT(!rb_shape_obj_too_complex(obj)); |
1678 | |
|
1679 | 0 | if (RBASIC(obj)->flags & ROBJECT_EMBED) { |
1680 | 0 | VALUE *ptr = ROBJECT_IVPTR(obj); |
1681 | 0 | VALUE *newptr = ALLOC_N(VALUE, new_capacity); |
1682 | 0 | MEMCPY(newptr, ptr, VALUE, current_capacity); |
1683 | 0 | RB_FL_UNSET_RAW(obj, ROBJECT_EMBED); |
1684 | 0 | ROBJECT(obj)->as.heap.ivptr = newptr; |
1685 | 0 | } |
1686 | 0 | else { |
1687 | 0 | REALLOC_N(ROBJECT(obj)->as.heap.ivptr, VALUE, new_capacity); |
1688 | 0 | } |
1689 | 0 | } |
1690 | | |
1691 | | static int |
1692 | | rb_obj_copy_ivs_to_hash_table_i(ID key, VALUE val, st_data_t arg) |
1693 | 0 | { |
1694 | 0 | RUBY_ASSERT(!st_lookup((st_table *)arg, (st_data_t)key, NULL)); |
1695 | |
|
1696 | 0 | st_add_direct((st_table *)arg, (st_data_t)key, (st_data_t)val); |
1697 | 0 | return ST_CONTINUE; |
1698 | 0 | } |
1699 | | |
1700 | | void |
1701 | | rb_obj_copy_ivs_to_hash_table(VALUE obj, st_table *table) |
1702 | 0 | { |
1703 | 0 | rb_ivar_foreach(obj, rb_obj_copy_ivs_to_hash_table_i, (st_data_t)table); |
1704 | 0 | } |
1705 | | |
1706 | | static VALUE * |
1707 | | obj_ivar_set_shape_ivptr(VALUE obj, void *_data) |
1708 | 0 | { |
1709 | 0 | RUBY_ASSERT(!rb_shape_obj_too_complex(obj)); |
1710 | |
|
1711 | 0 | return ROBJECT_IVPTR(obj); |
1712 | 0 | } |
1713 | | |
1714 | | static void |
1715 | | obj_ivar_set_shape_resize_ivptr(VALUE obj, attr_index_t old_capa, attr_index_t new_capa, void *_data) |
1716 | 0 | { |
1717 | 0 | rb_ensure_iv_list_size(obj, old_capa, new_capa); |
1718 | 0 | } |
1719 | | |
1720 | | static void |
1721 | | obj_ivar_set_set_shape(VALUE obj, rb_shape_t *shape, void *_data) |
1722 | 0 | { |
1723 | 0 | rb_shape_set_shape(obj, shape); |
1724 | 0 | } |
1725 | | |
1726 | | static void |
1727 | | obj_ivar_set_transition_too_complex(VALUE obj, void *_data) |
1728 | 0 | { |
1729 | 0 | rb_evict_ivars_to_hash(obj); |
1730 | 0 | } |
1731 | | |
1732 | | static st_table * |
1733 | | obj_ivar_set_too_complex_table(VALUE obj, void *_data) |
1734 | 0 | { |
1735 | 0 | RUBY_ASSERT(rb_shape_obj_too_complex(obj)); |
1736 | |
|
1737 | 0 | return ROBJECT_IV_HASH(obj); |
1738 | 0 | } |
1739 | | |
1740 | | attr_index_t |
1741 | | rb_obj_ivar_set(VALUE obj, ID id, VALUE val) |
1742 | 0 | { |
1743 | 0 | return general_ivar_set(obj, id, val, NULL, |
1744 | 0 | obj_ivar_set_shape_ivptr, |
1745 | 0 | obj_ivar_set_shape_resize_ivptr, |
1746 | 0 | obj_ivar_set_set_shape, |
1747 | 0 | obj_ivar_set_transition_too_complex, |
1748 | 0 | obj_ivar_set_too_complex_table).index; |
1749 | 0 | } |
1750 | | |
1751 | | /* Set the instance variable +val+ on object +obj+ at ivar name +id+. |
1752 | | * This function only works with T_OBJECT objects, so make sure |
1753 | | * +obj+ is of type T_OBJECT before using this function. |
1754 | | */ |
1755 | | VALUE |
1756 | | rb_vm_set_ivar_id(VALUE obj, ID id, VALUE val) |
1757 | 0 | { |
1758 | 0 | rb_check_frozen_internal(obj); |
1759 | 0 | rb_obj_ivar_set(obj, id, val); |
1760 | 0 | return val; |
1761 | 0 | } |
1762 | | |
1763 | | bool |
1764 | | rb_shape_set_shape_id(VALUE obj, shape_id_t shape_id) |
1765 | 0 | { |
1766 | 0 | if (rb_shape_get_shape_id(obj) == shape_id) { |
1767 | 0 | return false; |
1768 | 0 | } |
1769 | | |
1770 | 0 | #if SHAPE_IN_BASIC_FLAGS |
1771 | 0 | RBASIC_SET_SHAPE_ID(obj, shape_id); |
1772 | | #else |
1773 | | switch (BUILTIN_TYPE(obj)) { |
1774 | | case T_OBJECT: |
1775 | | ROBJECT_SET_SHAPE_ID(obj, shape_id); |
1776 | | break; |
1777 | | case T_CLASS: |
1778 | | case T_MODULE: |
1779 | | RCLASS_SET_SHAPE_ID(obj, shape_id); |
1780 | | break; |
1781 | | default: |
1782 | | if (shape_id != SPECIAL_CONST_SHAPE_ID) { |
1783 | | struct gen_ivtbl *ivtbl = 0; |
1784 | | RB_VM_LOCK_ENTER(); |
1785 | | { |
1786 | | st_table* global_iv_table = generic_ivtbl(obj, 0, false); |
1787 | | |
1788 | | if (st_lookup(global_iv_table, obj, (st_data_t *)&ivtbl)) { |
1789 | | ivtbl->shape_id = shape_id; |
1790 | | } |
1791 | | else { |
1792 | | rb_bug("Expected shape_id entry in global iv table"); |
1793 | | } |
1794 | | } |
1795 | | RB_VM_LOCK_LEAVE(); |
1796 | | } |
1797 | | } |
1798 | | #endif |
1799 | |
|
1800 | 0 | return true; |
1801 | 0 | } |
1802 | | |
1803 | | /** |
1804 | | * Prevents further modifications to the given object. ::rb_eFrozenError shall |
1805 | | * be raised if modification is attempted. |
1806 | | * |
1807 | | * @param[out] x Object in question. |
1808 | | */ |
1809 | | void rb_obj_freeze_inline(VALUE x) |
1810 | 0 | { |
1811 | 0 | if (RB_FL_ABLE(x)) { |
1812 | 0 | RB_OBJ_FREEZE_RAW(x); |
1813 | |
|
1814 | 0 | rb_shape_t * next_shape = rb_shape_transition_shape_frozen(x); |
1815 | | |
1816 | | // If we're transitioning from "not complex" to "too complex" |
1817 | | // then evict ivars. This can happen if we run out of shapes |
1818 | 0 | if (!rb_shape_obj_too_complex(x) && next_shape->type == SHAPE_OBJ_TOO_COMPLEX) { |
1819 | 0 | rb_evict_ivars_to_hash(x); |
1820 | 0 | } |
1821 | 0 | rb_shape_set_shape(x, next_shape); |
1822 | |
|
1823 | 0 | if (RBASIC_CLASS(x) && !(RBASIC(x)->flags & RUBY_FL_SINGLETON)) { |
1824 | 0 | rb_freeze_singleton_class(x); |
1825 | 0 | } |
1826 | 0 | } |
1827 | 0 | } |
1828 | | |
1829 | | static void |
1830 | | ivar_set(VALUE obj, ID id, VALUE val) |
1831 | 0 | { |
1832 | 0 | RB_DEBUG_COUNTER_INC(ivar_set_base); |
1833 | |
|
1834 | 0 | switch (BUILTIN_TYPE(obj)) { |
1835 | 0 | case T_OBJECT: |
1836 | 0 | { |
1837 | 0 | rb_obj_ivar_set(obj, id, val); |
1838 | 0 | break; |
1839 | 0 | } |
1840 | 0 | case T_CLASS: |
1841 | 0 | case T_MODULE: |
1842 | 0 | IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(id); |
1843 | 0 | rb_class_ivar_set(obj, id, val); |
1844 | |
|
1845 | 0 | break; |
1846 | 0 | default: |
1847 | 0 | generic_ivar_set(obj, id, val); |
1848 | 0 | break; |
1849 | 0 | } |
1850 | 0 | } |
1851 | | |
1852 | | VALUE |
1853 | | rb_ivar_set(VALUE obj, ID id, VALUE val) |
1854 | 0 | { |
1855 | 0 | rb_check_frozen(obj); |
1856 | 0 | ivar_set(obj, id, val); |
1857 | 0 | return val; |
1858 | 0 | } |
1859 | | |
1860 | | void |
1861 | | rb_ivar_set_internal(VALUE obj, ID id, VALUE val) |
1862 | 0 | { |
1863 | | // should be internal instance variable name (no @ prefix) |
1864 | 0 | VM_ASSERT(!rb_is_instance_id(id)); |
1865 | |
|
1866 | 0 | ivar_set(obj, id, val); |
1867 | 0 | } |
1868 | | |
1869 | | VALUE |
1870 | | rb_ivar_defined(VALUE obj, ID id) |
1871 | 0 | { |
1872 | 0 | attr_index_t index; |
1873 | |
|
1874 | 0 | if (SPECIAL_CONST_P(obj)) return Qfalse; |
1875 | 0 | if (rb_shape_obj_too_complex(obj)) { |
1876 | 0 | VALUE idx; |
1877 | 0 | st_table *table = NULL; |
1878 | 0 | switch (BUILTIN_TYPE(obj)) { |
1879 | 0 | case T_CLASS: |
1880 | 0 | case T_MODULE: |
1881 | 0 | table = (st_table *)RCLASS_IVPTR(obj); |
1882 | 0 | break; |
1883 | | |
1884 | 0 | case T_OBJECT: |
1885 | 0 | table = ROBJECT_IV_HASH(obj); |
1886 | 0 | break; |
1887 | | |
1888 | 0 | default: { |
1889 | 0 | struct gen_ivtbl *ivtbl; |
1890 | 0 | if (rb_gen_ivtbl_get(obj, 0, &ivtbl)) { |
1891 | 0 | table = ivtbl->as.complex.table; |
1892 | 0 | } |
1893 | 0 | break; |
1894 | 0 | } |
1895 | 0 | } |
1896 | | |
1897 | 0 | if (!table || !rb_st_lookup(table, id, &idx)) { |
1898 | 0 | return Qfalse; |
1899 | 0 | } |
1900 | | |
1901 | 0 | return Qtrue; |
1902 | 0 | } |
1903 | 0 | else { |
1904 | 0 | return RBOOL(rb_shape_get_iv_index(rb_shape_get_shape(obj), id, &index)); |
1905 | 0 | } |
1906 | 0 | } |
1907 | | |
1908 | | typedef int rb_ivar_foreach_callback_func(ID key, VALUE val, st_data_t arg); |
1909 | | st_data_t rb_st_nth_key(st_table *tab, st_index_t index); |
1910 | | |
1911 | | struct iv_itr_data { |
1912 | | VALUE obj; |
1913 | | struct gen_ivtbl * ivtbl; |
1914 | | st_data_t arg; |
1915 | | rb_ivar_foreach_callback_func *func; |
1916 | | }; |
1917 | | |
1918 | | /* |
1919 | | * Returns a flag to stop iterating depending on the result of +callback+. |
1920 | | */ |
1921 | | static bool |
1922 | | iterate_over_shapes_with_callback(rb_shape_t *shape, rb_ivar_foreach_callback_func *callback, struct iv_itr_data * itr_data) |
1923 | 0 | { |
1924 | 0 | switch ((enum shape_type)shape->type) { |
1925 | 0 | case SHAPE_ROOT: |
1926 | 0 | return false; |
1927 | 0 | case SHAPE_IVAR: |
1928 | 0 | ASSUME(callback); |
1929 | 0 | if (iterate_over_shapes_with_callback(rb_shape_get_parent(shape), callback, itr_data)) |
1930 | 0 | return true; |
1931 | 0 | VALUE * iv_list; |
1932 | 0 | switch (BUILTIN_TYPE(itr_data->obj)) { |
1933 | 0 | case T_OBJECT: |
1934 | 0 | RUBY_ASSERT(!rb_shape_obj_too_complex(itr_data->obj)); |
1935 | 0 | iv_list = ROBJECT_IVPTR(itr_data->obj); |
1936 | 0 | break; |
1937 | 0 | case T_CLASS: |
1938 | 0 | case T_MODULE: |
1939 | 0 | iv_list = RCLASS_IVPTR(itr_data->obj); |
1940 | 0 | break; |
1941 | 0 | default: |
1942 | 0 | iv_list = itr_data->ivtbl->as.shape.ivptr; |
1943 | 0 | break; |
1944 | 0 | } |
1945 | 0 | VALUE val = iv_list[shape->next_iv_index - 1]; |
1946 | 0 | if (!UNDEF_P(val)) { |
1947 | 0 | switch (callback(shape->edge_name, val, itr_data->arg)) { |
1948 | 0 | case ST_CHECK: |
1949 | 0 | case ST_CONTINUE: |
1950 | 0 | break; |
1951 | 0 | case ST_STOP: |
1952 | 0 | return true; |
1953 | 0 | default: |
1954 | 0 | rb_bug("unreachable"); |
1955 | 0 | } |
1956 | 0 | } |
1957 | 0 | return false; |
1958 | 0 | case SHAPE_FROZEN: |
1959 | 0 | case SHAPE_T_OBJECT: |
1960 | 0 | return iterate_over_shapes_with_callback(rb_shape_get_parent(shape), callback, itr_data); |
1961 | 0 | case SHAPE_OBJ_TOO_COMPLEX: |
1962 | 0 | default: |
1963 | 0 | rb_bug("Unreachable"); |
1964 | 0 | } |
1965 | 0 | } |
1966 | | |
1967 | | static int |
1968 | | each_hash_iv(st_data_t id, st_data_t val, st_data_t data) |
1969 | 0 | { |
1970 | 0 | struct iv_itr_data * itr_data = (struct iv_itr_data *)data; |
1971 | 0 | rb_ivar_foreach_callback_func *callback = itr_data->func; |
1972 | 0 | return callback((ID)id, (VALUE)val, itr_data->arg); |
1973 | 0 | } |
1974 | | |
1975 | | static void |
1976 | | obj_ivar_each(VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg) |
1977 | 0 | { |
1978 | 0 | rb_shape_t* shape = rb_shape_get_shape(obj); |
1979 | 0 | struct iv_itr_data itr_data; |
1980 | 0 | itr_data.obj = obj; |
1981 | 0 | itr_data.arg = arg; |
1982 | 0 | itr_data.func = func; |
1983 | 0 | if (rb_shape_obj_too_complex(obj)) { |
1984 | 0 | rb_st_foreach(ROBJECT_IV_HASH(obj), each_hash_iv, (st_data_t)&itr_data); |
1985 | 0 | } |
1986 | 0 | else { |
1987 | 0 | iterate_over_shapes_with_callback(shape, func, &itr_data); |
1988 | 0 | } |
1989 | 0 | } |
1990 | | |
1991 | | static void |
1992 | | gen_ivar_each(VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg) |
1993 | 0 | { |
1994 | 0 | rb_shape_t *shape = rb_shape_get_shape(obj); |
1995 | 0 | struct gen_ivtbl *ivtbl; |
1996 | 0 | if (!rb_gen_ivtbl_get(obj, 0, &ivtbl)) return; |
1997 | | |
1998 | 0 | struct iv_itr_data itr_data; |
1999 | 0 | itr_data.obj = obj; |
2000 | 0 | itr_data.ivtbl = ivtbl; |
2001 | 0 | itr_data.arg = arg; |
2002 | 0 | itr_data.func = func; |
2003 | 0 | if (rb_shape_obj_too_complex(obj)) { |
2004 | 0 | rb_st_foreach(ivtbl->as.complex.table, each_hash_iv, (st_data_t)&itr_data); |
2005 | 0 | } |
2006 | 0 | else { |
2007 | 0 | iterate_over_shapes_with_callback(shape, func, &itr_data); |
2008 | 0 | } |
2009 | 0 | } |
2010 | | |
2011 | | static void |
2012 | | class_ivar_each(VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg) |
2013 | 0 | { |
2014 | 0 | RUBY_ASSERT(RB_TYPE_P(obj, T_CLASS) || RB_TYPE_P(obj, T_MODULE)); |
2015 | |
|
2016 | 0 | rb_shape_t* shape = rb_shape_get_shape(obj); |
2017 | 0 | struct iv_itr_data itr_data; |
2018 | 0 | itr_data.obj = obj; |
2019 | 0 | itr_data.arg = arg; |
2020 | 0 | itr_data.func = func; |
2021 | 0 | if (rb_shape_obj_too_complex(obj)) { |
2022 | 0 | rb_st_foreach(RCLASS_IV_HASH(obj), each_hash_iv, (st_data_t)&itr_data); |
2023 | 0 | } |
2024 | 0 | else { |
2025 | 0 | iterate_over_shapes_with_callback(shape, func, &itr_data); |
2026 | 0 | } |
2027 | 0 | } |
2028 | | |
2029 | | void |
2030 | | rb_copy_generic_ivar(VALUE clone, VALUE obj) |
2031 | 0 | { |
2032 | 0 | struct gen_ivtbl *obj_ivtbl; |
2033 | 0 | struct gen_ivtbl *new_ivtbl; |
2034 | |
|
2035 | 0 | rb_check_frozen(clone); |
2036 | |
|
2037 | 0 | if (!FL_TEST(obj, FL_EXIVAR)) { |
2038 | 0 | goto clear; |
2039 | 0 | } |
2040 | | |
2041 | 0 | if (rb_gen_ivtbl_get(obj, 0, &obj_ivtbl)) { |
2042 | 0 | if (gen_ivtbl_count(obj, obj_ivtbl) == 0) |
2043 | 0 | goto clear; |
2044 | | |
2045 | 0 | FL_SET(clone, FL_EXIVAR); |
2046 | |
|
2047 | 0 | if (rb_shape_obj_too_complex(obj)) { |
2048 | 0 | new_ivtbl = xmalloc(sizeof(struct gen_ivtbl)); |
2049 | | #if !SHAPE_IN_BASIC_FLAGS |
2050 | | new_ivtbl->shape_id = SHAPE_OBJ_TOO_COMPLEX; |
2051 | | #endif |
2052 | 0 | new_ivtbl->as.complex.table = st_copy(obj_ivtbl->as.complex.table); |
2053 | 0 | } |
2054 | 0 | else { |
2055 | 0 | new_ivtbl = gen_ivtbl_resize(0, obj_ivtbl->as.shape.numiv); |
2056 | |
|
2057 | 0 | for (uint32_t i=0; i<obj_ivtbl->as.shape.numiv; i++) { |
2058 | 0 | RB_OBJ_WRITE(clone, &new_ivtbl->as.shape.ivptr[i], obj_ivtbl->as.shape.ivptr[i]); |
2059 | 0 | } |
2060 | 0 | } |
2061 | | |
2062 | | /* |
2063 | | * c.ivtbl may change in gen_ivar_copy due to realloc, |
2064 | | * no need to free |
2065 | | */ |
2066 | 0 | RB_VM_LOCK_ENTER(); |
2067 | 0 | { |
2068 | 0 | generic_ivtbl_no_ractor_check(clone); |
2069 | 0 | st_insert(generic_ivtbl_no_ractor_check(obj), (st_data_t)clone, (st_data_t)new_ivtbl); |
2070 | 0 | } |
2071 | 0 | RB_VM_LOCK_LEAVE(); |
2072 | |
|
2073 | 0 | rb_shape_t * obj_shape = rb_shape_get_shape(obj); |
2074 | 0 | if (rb_shape_frozen_shape_p(obj_shape)) { |
2075 | 0 | rb_shape_set_shape_id(clone, obj_shape->parent_id); |
2076 | 0 | } |
2077 | 0 | else { |
2078 | 0 | rb_shape_set_shape(clone, obj_shape); |
2079 | 0 | } |
2080 | 0 | } |
2081 | 0 | return; |
2082 | | |
2083 | 0 | clear: |
2084 | 0 | if (FL_TEST(clone, FL_EXIVAR)) { |
2085 | 0 | rb_free_generic_ivar(clone); |
2086 | 0 | FL_UNSET(clone, FL_EXIVAR); |
2087 | 0 | } |
2088 | 0 | } |
2089 | | |
2090 | | void |
2091 | | rb_replace_generic_ivar(VALUE clone, VALUE obj) |
2092 | 0 | { |
2093 | 0 | RUBY_ASSERT(FL_TEST(obj, FL_EXIVAR)); |
2094 | |
|
2095 | 0 | RB_VM_LOCK_ENTER(); |
2096 | 0 | { |
2097 | 0 | st_data_t ivtbl, obj_data = (st_data_t)obj; |
2098 | 0 | if (st_lookup(generic_iv_tbl_, (st_data_t)obj, &ivtbl)) { |
2099 | 0 | st_insert(generic_iv_tbl_, (st_data_t)clone, ivtbl); |
2100 | 0 | st_delete(generic_iv_tbl_, &obj_data, NULL); |
2101 | 0 | } |
2102 | 0 | else { |
2103 | 0 | rb_bug("unreachable"); |
2104 | 0 | } |
2105 | 0 | } |
2106 | 0 | RB_VM_LOCK_LEAVE(); |
2107 | |
|
2108 | 0 | FL_SET(clone, FL_EXIVAR); |
2109 | 0 | } |
2110 | | |
2111 | | void |
2112 | | rb_ivar_foreach(VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg) |
2113 | 0 | { |
2114 | 0 | if (SPECIAL_CONST_P(obj)) return; |
2115 | 0 | switch (BUILTIN_TYPE(obj)) { |
2116 | 0 | case T_OBJECT: |
2117 | 0 | obj_ivar_each(obj, func, arg); |
2118 | 0 | break; |
2119 | 0 | case T_CLASS: |
2120 | 0 | case T_MODULE: |
2121 | 0 | IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(0); |
2122 | 0 | RB_VM_LOCK_ENTER(); |
2123 | 0 | { |
2124 | 0 | class_ivar_each(obj, func, arg); |
2125 | 0 | } |
2126 | 0 | RB_VM_LOCK_LEAVE(); |
2127 | 0 | break; |
2128 | 0 | default: |
2129 | 0 | if (FL_TEST(obj, FL_EXIVAR)) { |
2130 | 0 | gen_ivar_each(obj, func, arg); |
2131 | 0 | } |
2132 | 0 | break; |
2133 | 0 | } |
2134 | 0 | } |
2135 | | |
2136 | | st_index_t |
2137 | | rb_ivar_count(VALUE obj) |
2138 | 0 | { |
2139 | 0 | if (SPECIAL_CONST_P(obj)) return 0; |
2140 | | |
2141 | 0 | switch (BUILTIN_TYPE(obj)) { |
2142 | 0 | case T_OBJECT: |
2143 | 0 | return ROBJECT_IV_COUNT(obj); |
2144 | 0 | case T_CLASS: |
2145 | 0 | case T_MODULE: |
2146 | 0 | return RCLASS_IV_COUNT(obj); |
2147 | 0 | default: |
2148 | 0 | if (FL_TEST(obj, FL_EXIVAR)) { |
2149 | 0 | struct gen_ivtbl *ivtbl; |
2150 | |
|
2151 | 0 | if (rb_gen_ivtbl_get(obj, 0, &ivtbl)) { |
2152 | 0 | return gen_ivtbl_count(obj, ivtbl); |
2153 | 0 | } |
2154 | 0 | } |
2155 | 0 | break; |
2156 | 0 | } |
2157 | 0 | return 0; |
2158 | 0 | } |
2159 | | |
2160 | | static int |
2161 | | ivar_i(ID key, VALUE v, st_data_t a) |
2162 | 0 | { |
2163 | 0 | VALUE ary = (VALUE)a; |
2164 | |
|
2165 | 0 | if (rb_is_instance_id(key)) { |
2166 | 0 | rb_ary_push(ary, ID2SYM(key)); |
2167 | 0 | } |
2168 | 0 | return ST_CONTINUE; |
2169 | 0 | } |
2170 | | |
2171 | | /* |
2172 | | * call-seq: |
2173 | | * obj.instance_variables -> array |
2174 | | * |
2175 | | * Returns an array of instance variable names for the receiver. Note |
2176 | | * that simply defining an accessor does not create the corresponding |
2177 | | * instance variable. |
2178 | | * |
2179 | | * class Fred |
2180 | | * attr_accessor :a1 |
2181 | | * def initialize |
2182 | | * @iv = 3 |
2183 | | * end |
2184 | | * end |
2185 | | * Fred.new.instance_variables #=> [:@iv] |
2186 | | */ |
2187 | | |
2188 | | VALUE |
2189 | | rb_obj_instance_variables(VALUE obj) |
2190 | 0 | { |
2191 | 0 | VALUE ary; |
2192 | |
|
2193 | 0 | ary = rb_ary_new(); |
2194 | 0 | rb_ivar_foreach(obj, ivar_i, ary); |
2195 | 0 | return ary; |
2196 | 0 | } |
2197 | | |
2198 | 0 | #define rb_is_constant_id rb_is_const_id |
2199 | 0 | #define rb_is_constant_name rb_is_const_name |
2200 | | #define id_for_var(obj, name, part, type) \ |
2201 | 0 | id_for_var_message(obj, name, type, "`%1$s' is not allowed as "#part" "#type" variable name") |
2202 | | #define id_for_var_message(obj, name, type, message) \ |
2203 | 0 | check_id_type(obj, &(name), rb_is_##type##_id, rb_is_##type##_name, message, strlen(message)) |
2204 | | static ID |
2205 | | check_id_type(VALUE obj, VALUE *pname, |
2206 | | int (*valid_id_p)(ID), int (*valid_name_p)(VALUE), |
2207 | | const char *message, size_t message_len) |
2208 | 0 | { |
2209 | 0 | ID id = rb_check_id(pname); |
2210 | 0 | VALUE name = *pname; |
2211 | |
|
2212 | 0 | if (id ? !valid_id_p(id) : !valid_name_p(name)) { |
2213 | 0 | rb_name_err_raise_str(rb_fstring_new(message, message_len), |
2214 | 0 | obj, name); |
2215 | 0 | } |
2216 | 0 | return id; |
2217 | 0 | } |
2218 | | |
2219 | | /* |
2220 | | * call-seq: |
2221 | | * obj.remove_instance_variable(symbol) -> obj |
2222 | | * obj.remove_instance_variable(string) -> obj |
2223 | | * |
2224 | | * Removes the named instance variable from <i>obj</i>, returning that |
2225 | | * variable's value. |
2226 | | * String arguments are converted to symbols. |
2227 | | * |
2228 | | * class Dummy |
2229 | | * attr_reader :var |
2230 | | * def initialize |
2231 | | * @var = 99 |
2232 | | * end |
2233 | | * def remove |
2234 | | * remove_instance_variable(:@var) |
2235 | | * end |
2236 | | * end |
2237 | | * d = Dummy.new |
2238 | | * d.var #=> 99 |
2239 | | * d.remove #=> 99 |
2240 | | * d.var #=> nil |
2241 | | */ |
2242 | | |
2243 | | VALUE |
2244 | | rb_obj_remove_instance_variable(VALUE obj, VALUE name) |
2245 | 0 | { |
2246 | 0 | const ID id = id_for_var(obj, name, an, instance); |
2247 | | |
2248 | | // Frozen check comes here because it's expected that we raise a |
2249 | | // NameError (from the id_for_var check) before we raise a FrozenError |
2250 | 0 | rb_check_frozen(obj); |
2251 | |
|
2252 | 0 | if (id) { |
2253 | 0 | VALUE val = rb_ivar_delete(obj, id, Qundef); |
2254 | |
|
2255 | 0 | if (!UNDEF_P(val)) return val; |
2256 | 0 | } |
2257 | | |
2258 | 0 | rb_name_err_raise("instance variable %1$s not defined", |
2259 | 0 | obj, name); |
2260 | 0 | UNREACHABLE_RETURN(Qnil); |
2261 | 0 | } |
2262 | | |
2263 | | NORETURN(static void uninitialized_constant(VALUE, VALUE)); |
2264 | | static void |
2265 | | uninitialized_constant(VALUE klass, VALUE name) |
2266 | 0 | { |
2267 | 0 | if (klass && rb_class_real(klass) != rb_cObject) |
2268 | 0 | rb_name_err_raise("uninitialized constant %2$s::%1$s", |
2269 | 0 | klass, name); |
2270 | 0 | else |
2271 | 0 | rb_name_err_raise("uninitialized constant %1$s", |
2272 | 0 | klass, name); |
2273 | 0 | } |
2274 | | |
2275 | | VALUE |
2276 | | rb_const_missing(VALUE klass, VALUE name) |
2277 | 0 | { |
2278 | 0 | VALUE value = rb_funcallv(klass, idConst_missing, 1, &name); |
2279 | 0 | rb_vm_inc_const_missing_count(); |
2280 | 0 | return value; |
2281 | 0 | } |
2282 | | |
2283 | | |
2284 | | /* |
2285 | | * call-seq: |
2286 | | * mod.const_missing(sym) -> obj |
2287 | | * |
2288 | | * Invoked when a reference is made to an undefined constant in |
2289 | | * <i>mod</i>. It is passed a symbol for the undefined constant, and |
2290 | | * returns a value to be used for that constant. The |
2291 | | * following code is an example of the same: |
2292 | | * |
2293 | | * def Foo.const_missing(name) |
2294 | | * name # return the constant name as Symbol |
2295 | | * end |
2296 | | * |
2297 | | * Foo::UNDEFINED_CONST #=> :UNDEFINED_CONST: symbol returned |
2298 | | * |
2299 | | * In the next example when a reference is made to an undefined constant, |
2300 | | * it attempts to load a file whose name is the lowercase version of the |
2301 | | * constant (thus class <code>Fred</code> is assumed to be in file |
2302 | | * <code>fred.rb</code>). If found, it returns the loaded class. It |
2303 | | * therefore implements an autoload feature similar to Kernel#autoload and |
2304 | | * Module#autoload. |
2305 | | * |
2306 | | * def Object.const_missing(name) |
2307 | | * @looked_for ||= {} |
2308 | | * str_name = name.to_s |
2309 | | * raise "Class not found: #{name}" if @looked_for[str_name] |
2310 | | * @looked_for[str_name] = 1 |
2311 | | * file = str_name.downcase |
2312 | | * require file |
2313 | | * klass = const_get(name) |
2314 | | * return klass if klass |
2315 | | * raise "Class not found: #{name}" |
2316 | | * end |
2317 | | * |
2318 | | */ |
2319 | | |
2320 | | VALUE |
2321 | | rb_mod_const_missing(VALUE klass, VALUE name) |
2322 | 0 | { |
2323 | 0 | rb_execution_context_t *ec = GET_EC(); |
2324 | 0 | VALUE ref = ec->private_const_reference; |
2325 | 0 | rb_vm_pop_cfunc_frame(); |
2326 | 0 | if (ref) { |
2327 | 0 | ec->private_const_reference = 0; |
2328 | 0 | rb_name_err_raise("private constant %2$s::%1$s referenced", ref, name); |
2329 | 0 | } |
2330 | 0 | uninitialized_constant(klass, name); |
2331 | | |
2332 | 0 | UNREACHABLE_RETURN(Qnil); |
2333 | 0 | } |
2334 | | |
2335 | | static void |
2336 | | autoload_table_mark(void *ptr) |
2337 | 0 | { |
2338 | 0 | rb_mark_tbl_no_pin((st_table *)ptr); |
2339 | 0 | } |
2340 | | |
2341 | | static void |
2342 | | autoload_table_free(void *ptr) |
2343 | 0 | { |
2344 | 0 | st_free_table((st_table *)ptr); |
2345 | 0 | } |
2346 | | |
2347 | | static size_t |
2348 | | autoload_table_memsize(const void *ptr) |
2349 | 0 | { |
2350 | 0 | const st_table *tbl = ptr; |
2351 | 0 | return st_memsize(tbl); |
2352 | 0 | } |
2353 | | |
2354 | | static void |
2355 | | autoload_table_compact(void *ptr) |
2356 | 0 | { |
2357 | 0 | rb_gc_update_tbl_refs((st_table *)ptr); |
2358 | 0 | } |
2359 | | |
2360 | | static const rb_data_type_t autoload_table_type = { |
2361 | | "autoload_table", |
2362 | | {autoload_table_mark, autoload_table_free, autoload_table_memsize, autoload_table_compact,}, |
2363 | | 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED |
2364 | | }; |
2365 | | |
2366 | | #define check_autoload_table(av) \ |
2367 | 0 | (struct st_table *)rb_check_typeddata((av), &autoload_table_type) |
2368 | | |
2369 | | static VALUE |
2370 | | autoload_data(VALUE mod, ID id) |
2371 | 0 | { |
2372 | 0 | struct st_table *tbl; |
2373 | 0 | st_data_t val; |
2374 | | |
2375 | | // If we are called with a non-origin ICLASS, fetch the autoload data from |
2376 | | // the original module. |
2377 | 0 | if (RB_TYPE_P(mod, T_ICLASS)) { |
2378 | 0 | if (FL_TEST_RAW(mod, RICLASS_IS_ORIGIN)) { |
2379 | 0 | return 0; |
2380 | 0 | } |
2381 | 0 | else { |
2382 | 0 | mod = RBASIC(mod)->klass; |
2383 | 0 | } |
2384 | 0 | } |
2385 | | |
2386 | 0 | RUBY_ASSERT(RB_TYPE_P(mod, T_CLASS) || RB_TYPE_P(mod, T_MODULE)); |
2387 | | |
2388 | | // Look up the instance variable table for `autoload`, then index into that table with the given constant name `id`. |
2389 | |
|
2390 | 0 | VALUE tbl_value = rb_ivar_lookup(mod, autoload, Qfalse); |
2391 | 0 | if (!RTEST(tbl_value) || !(tbl = check_autoload_table(tbl_value)) || !st_lookup(tbl, (st_data_t)id, &val)) { |
2392 | 0 | return 0; |
2393 | 0 | } |
2394 | | |
2395 | 0 | return (VALUE)val; |
2396 | 0 | } |
2397 | | |
2398 | | // 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`. |
2399 | | struct autoload_const { |
2400 | | // The linked list node of all constants which are loaded by the related autoload feature. |
2401 | | struct ccan_list_node cnode; /* <=> autoload_data.constants */ |
2402 | | |
2403 | | // The shared "autoload_data" if multiple constants are defined from the same feature. |
2404 | | VALUE autoload_data_value; |
2405 | | |
2406 | | // The module we are loading a constant into. |
2407 | | VALUE module; |
2408 | | |
2409 | | // The name of the constant we are loading. |
2410 | | ID name; |
2411 | | |
2412 | | // The value of the constant (after it's loaded). |
2413 | | VALUE value; |
2414 | | |
2415 | | // The constant entry flags which need to be re-applied after autoloading the feature. |
2416 | | rb_const_flag_t flag; |
2417 | | |
2418 | | // The source file and line number that defined this constant (different from feature path). |
2419 | | VALUE file; |
2420 | | int line; |
2421 | | }; |
2422 | | |
2423 | | // 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. |
2424 | | struct autoload_data { |
2425 | | // The feature path to require to load this constant. |
2426 | | VALUE feature; |
2427 | | |
2428 | | // The mutex which is protecting autoloading this feature. |
2429 | | VALUE mutex; |
2430 | | |
2431 | | // The process fork serial number since the autoload mutex will become invalid on fork. |
2432 | | rb_serial_t fork_gen; |
2433 | | |
2434 | | // The linked list of all constants that are going to be loaded by this autoload. |
2435 | | struct ccan_list_head constants; /* <=> autoload_const.cnode */ |
2436 | | }; |
2437 | | |
2438 | | static void |
2439 | | autoload_data_compact(void *ptr) |
2440 | 0 | { |
2441 | 0 | struct autoload_data *p = ptr; |
2442 | |
|
2443 | 0 | p->feature = rb_gc_location(p->feature); |
2444 | 0 | p->mutex = rb_gc_location(p->mutex); |
2445 | 0 | } |
2446 | | |
2447 | | static void |
2448 | | autoload_data_mark(void *ptr) |
2449 | 0 | { |
2450 | 0 | struct autoload_data *p = ptr; |
2451 | |
|
2452 | 0 | rb_gc_mark_movable(p->feature); |
2453 | 0 | rb_gc_mark_movable(p->mutex); |
2454 | 0 | } |
2455 | | |
2456 | | static void |
2457 | | autoload_data_free(void *ptr) |
2458 | 0 | { |
2459 | 0 | struct autoload_data *p = ptr; |
2460 | |
|
2461 | 0 | struct autoload_const *autoload_const, *next; |
2462 | 0 | ccan_list_for_each_safe(&p->constants, autoload_const, next, cnode) { |
2463 | 0 | ccan_list_del_init(&autoload_const->cnode); |
2464 | 0 | } |
2465 | |
|
2466 | 0 | ruby_xfree(p); |
2467 | 0 | } |
2468 | | |
2469 | | static size_t |
2470 | | autoload_data_memsize(const void *ptr) |
2471 | 0 | { |
2472 | 0 | return sizeof(struct autoload_data); |
2473 | 0 | } |
2474 | | |
2475 | | static const rb_data_type_t autoload_data_type = { |
2476 | | "autoload_data", |
2477 | | {autoload_data_mark, autoload_data_free, autoload_data_memsize, autoload_data_compact}, |
2478 | | 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED |
2479 | | }; |
2480 | | |
2481 | | static void |
2482 | | autoload_const_compact(void *ptr) |
2483 | 0 | { |
2484 | 0 | struct autoload_const *ac = ptr; |
2485 | |
|
2486 | 0 | ac->module = rb_gc_location(ac->module); |
2487 | 0 | ac->autoload_data_value = rb_gc_location(ac->autoload_data_value); |
2488 | 0 | ac->value = rb_gc_location(ac->value); |
2489 | 0 | ac->file = rb_gc_location(ac->file); |
2490 | 0 | } |
2491 | | |
2492 | | static void |
2493 | | autoload_const_mark(void *ptr) |
2494 | 0 | { |
2495 | 0 | struct autoload_const *ac = ptr; |
2496 | |
|
2497 | 0 | rb_gc_mark_movable(ac->module); |
2498 | 0 | rb_gc_mark_movable(ac->autoload_data_value); |
2499 | 0 | rb_gc_mark_movable(ac->value); |
2500 | 0 | rb_gc_mark_movable(ac->file); |
2501 | 0 | } |
2502 | | |
2503 | | static size_t |
2504 | | autoload_const_memsize(const void *ptr) |
2505 | 0 | { |
2506 | 0 | return sizeof(struct autoload_const); |
2507 | 0 | } |
2508 | | |
2509 | | static void |
2510 | | autoload_const_free(void *ptr) |
2511 | 0 | { |
2512 | 0 | struct autoload_const *autoload_const = ptr; |
2513 | |
|
2514 | 0 | ccan_list_del(&autoload_const->cnode); |
2515 | 0 | ruby_xfree(ptr); |
2516 | 0 | } |
2517 | | |
2518 | | static const rb_data_type_t autoload_const_type = { |
2519 | | "autoload_const", |
2520 | | {autoload_const_mark, autoload_const_free, autoload_const_memsize, autoload_const_compact,}, |
2521 | | 0, 0, RUBY_TYPED_FREE_IMMEDIATELY |
2522 | | }; |
2523 | | |
2524 | | static struct autoload_data * |
2525 | | get_autoload_data(VALUE autoload_const_value, struct autoload_const **autoload_const_pointer) |
2526 | 0 | { |
2527 | 0 | struct autoload_const *autoload_const = rb_check_typeddata(autoload_const_value, &autoload_const_type); |
2528 | |
|
2529 | 0 | VALUE autoload_data_value = autoload_const->autoload_data_value; |
2530 | 0 | struct autoload_data *autoload_data = rb_check_typeddata(autoload_data_value, &autoload_data_type); |
2531 | | |
2532 | | /* do not reach across stack for ->state after forking: */ |
2533 | 0 | if (autoload_data && autoload_data->fork_gen != GET_VM()->fork_gen) { |
2534 | 0 | RB_OBJ_WRITE(autoload_data_value, &autoload_data->mutex, Qnil); |
2535 | 0 | autoload_data->fork_gen = 0; |
2536 | 0 | } |
2537 | |
|
2538 | 0 | if (autoload_const_pointer) *autoload_const_pointer = autoload_const; |
2539 | |
|
2540 | 0 | return autoload_data; |
2541 | 0 | } |
2542 | | |
2543 | | RUBY_FUNC_EXPORTED void |
2544 | | rb_autoload(VALUE module, ID name, const char *feature) |
2545 | 0 | { |
2546 | 0 | if (!feature || !*feature) { |
2547 | 0 | rb_raise(rb_eArgError, "empty feature name"); |
2548 | 0 | } |
2549 | | |
2550 | 0 | rb_autoload_str(module, name, rb_fstring_cstr(feature)); |
2551 | 0 | } |
2552 | | |
2553 | | static void const_set(VALUE klass, ID id, VALUE val); |
2554 | | static void const_added(VALUE klass, ID const_name); |
2555 | | |
2556 | | struct autoload_arguments { |
2557 | | VALUE module; |
2558 | | ID name; |
2559 | | VALUE feature; |
2560 | | }; |
2561 | | |
2562 | | static VALUE |
2563 | | autoload_feature_lookup_or_create(VALUE feature, struct autoload_data **autoload_data_pointer) |
2564 | 0 | { |
2565 | 0 | RUBY_ASSERT_MUTEX_OWNED(autoload_mutex); |
2566 | 0 | RUBY_ASSERT_CRITICAL_SECTION_ENTER(); |
2567 | |
|
2568 | 0 | VALUE autoload_data_value = rb_hash_aref(autoload_features, feature); |
2569 | 0 | struct autoload_data *autoload_data; |
2570 | |
|
2571 | 0 | if (NIL_P(autoload_data_value)) { |
2572 | 0 | autoload_data_value = TypedData_Make_Struct(0, struct autoload_data, &autoload_data_type, autoload_data); |
2573 | 0 | RB_OBJ_WRITE(autoload_data_value, &autoload_data->feature, feature); |
2574 | 0 | RB_OBJ_WRITE(autoload_data_value, &autoload_data->mutex, Qnil); |
2575 | 0 | ccan_list_head_init(&autoload_data->constants); |
2576 | |
|
2577 | 0 | if (autoload_data_pointer) *autoload_data_pointer = autoload_data; |
2578 | |
|
2579 | 0 | rb_hash_aset(autoload_features, feature, autoload_data_value); |
2580 | 0 | } |
2581 | 0 | else if (autoload_data_pointer) { |
2582 | 0 | *autoload_data_pointer = rb_check_typeddata(autoload_data_value, &autoload_data_type); |
2583 | 0 | } |
2584 | |
|
2585 | 0 | RUBY_ASSERT_CRITICAL_SECTION_LEAVE(); |
2586 | 0 | return autoload_data_value; |
2587 | 0 | } |
2588 | | |
2589 | | static VALUE |
2590 | | autoload_table_lookup_or_create(VALUE module) |
2591 | 0 | { |
2592 | 0 | VALUE autoload_table_value = rb_ivar_lookup(module, autoload, Qfalse); |
2593 | 0 | if (RTEST(autoload_table_value)) { |
2594 | 0 | return autoload_table_value; |
2595 | 0 | } |
2596 | 0 | else { |
2597 | 0 | autoload_table_value = TypedData_Wrap_Struct(0, &autoload_table_type, NULL); |
2598 | 0 | rb_class_ivar_set(module, autoload, autoload_table_value); |
2599 | 0 | RTYPEDDATA_DATA(autoload_table_value) = st_init_numtable(); |
2600 | 0 | return autoload_table_value; |
2601 | 0 | } |
2602 | 0 | } |
2603 | | |
2604 | | static VALUE |
2605 | | autoload_synchronized(VALUE _arguments) |
2606 | 0 | { |
2607 | 0 | struct autoload_arguments *arguments = (struct autoload_arguments *)_arguments; |
2608 | |
|
2609 | 0 | rb_const_entry_t *constant_entry = rb_const_lookup(arguments->module, arguments->name); |
2610 | 0 | if (constant_entry && !UNDEF_P(constant_entry->value)) { |
2611 | 0 | return Qfalse; |
2612 | 0 | } |
2613 | | |
2614 | | // Reset any state associated with any previous constant: |
2615 | 0 | const_set(arguments->module, arguments->name, Qundef); |
2616 | |
|
2617 | 0 | VALUE autoload_table_value = autoload_table_lookup_or_create(arguments->module); |
2618 | 0 | struct st_table *autoload_table = check_autoload_table(autoload_table_value); |
2619 | | |
2620 | | // Ensure the string is uniqued since we use an identity lookup: |
2621 | 0 | VALUE feature = rb_fstring(arguments->feature); |
2622 | |
|
2623 | 0 | struct autoload_data *autoload_data; |
2624 | 0 | VALUE autoload_data_value = autoload_feature_lookup_or_create(feature, &autoload_data); |
2625 | |
|
2626 | 0 | { |
2627 | 0 | struct autoload_const *autoload_const; |
2628 | 0 | VALUE autoload_const_value = TypedData_Make_Struct(0, struct autoload_const, &autoload_const_type, autoload_const); |
2629 | 0 | autoload_const->module = arguments->module; |
2630 | 0 | autoload_const->name = arguments->name; |
2631 | 0 | autoload_const->value = Qundef; |
2632 | 0 | autoload_const->flag = CONST_PUBLIC; |
2633 | 0 | autoload_const->autoload_data_value = autoload_data_value; |
2634 | 0 | ccan_list_add_tail(&autoload_data->constants, &autoload_const->cnode); |
2635 | 0 | st_insert(autoload_table, (st_data_t)arguments->name, (st_data_t)autoload_const_value); |
2636 | 0 | RB_OBJ_WRITTEN(autoload_table_value, Qundef, autoload_const_value); |
2637 | 0 | } |
2638 | |
|
2639 | 0 | return Qtrue; |
2640 | 0 | } |
2641 | | |
2642 | | void |
2643 | | rb_autoload_str(VALUE module, ID name, VALUE feature) |
2644 | 0 | { |
2645 | 0 | if (!rb_is_const_id(name)) { |
2646 | 0 | rb_raise(rb_eNameError, "autoload must be constant name: %"PRIsVALUE"", QUOTE_ID(name)); |
2647 | 0 | } |
2648 | | |
2649 | 0 | Check_Type(feature, T_STRING); |
2650 | 0 | if (!RSTRING_LEN(feature)) { |
2651 | 0 | rb_raise(rb_eArgError, "empty feature name"); |
2652 | 0 | } |
2653 | | |
2654 | 0 | struct autoload_arguments arguments = { |
2655 | 0 | .module = module, |
2656 | 0 | .name = name, |
2657 | 0 | .feature = feature, |
2658 | 0 | }; |
2659 | |
|
2660 | 0 | VALUE result = rb_mutex_synchronize(autoload_mutex, autoload_synchronized, (VALUE)&arguments); |
2661 | |
|
2662 | 0 | if (result == Qtrue) { |
2663 | 0 | const_added(module, name); |
2664 | 0 | } |
2665 | 0 | } |
2666 | | |
2667 | | static void |
2668 | | autoload_delete(VALUE module, ID name) |
2669 | 0 | { |
2670 | 0 | RUBY_ASSERT_CRITICAL_SECTION_ENTER(); |
2671 | |
|
2672 | 0 | st_data_t load = 0, key = name; |
2673 | |
|
2674 | 0 | RUBY_ASSERT(RB_TYPE_P(module, T_CLASS) || RB_TYPE_P(module, T_MODULE)); |
2675 | |
|
2676 | 0 | VALUE table_value = rb_ivar_lookup(module, autoload, Qfalse); |
2677 | 0 | if (RTEST(table_value)) { |
2678 | 0 | struct st_table *table = check_autoload_table(table_value); |
2679 | |
|
2680 | 0 | st_delete(table, &key, &load); |
2681 | 0 | RB_OBJ_WRITTEN(table_value, load, Qundef); |
2682 | | |
2683 | | /* Qfalse can indicate already deleted */ |
2684 | 0 | if (load != Qfalse) { |
2685 | 0 | struct autoload_const *autoload_const; |
2686 | 0 | struct autoload_data *autoload_data = get_autoload_data((VALUE)load, &autoload_const); |
2687 | |
|
2688 | 0 | VM_ASSERT(autoload_data); |
2689 | 0 | VM_ASSERT(!ccan_list_empty(&autoload_data->constants)); |
2690 | | |
2691 | | /* |
2692 | | * we must delete here to avoid "already initialized" warnings |
2693 | | * with parallel autoload. Using list_del_init here so list_del |
2694 | | * works in autoload_const_free |
2695 | | */ |
2696 | 0 | ccan_list_del_init(&autoload_const->cnode); |
2697 | |
|
2698 | 0 | if (ccan_list_empty(&autoload_data->constants)) { |
2699 | 0 | rb_hash_delete(autoload_features, autoload_data->feature); |
2700 | 0 | } |
2701 | | |
2702 | | // If the autoload table is empty, we can delete it. |
2703 | 0 | if (table->num_entries == 0) { |
2704 | 0 | rb_attr_delete(module, autoload); |
2705 | 0 | } |
2706 | 0 | } |
2707 | 0 | } |
2708 | |
|
2709 | 0 | RUBY_ASSERT_CRITICAL_SECTION_LEAVE(); |
2710 | 0 | } |
2711 | | |
2712 | | static int |
2713 | | autoload_by_someone_else(struct autoload_data *ele) |
2714 | 0 | { |
2715 | 0 | return ele->mutex != Qnil && !rb_mutex_owned_p(ele->mutex); |
2716 | 0 | } |
2717 | | |
2718 | | static VALUE |
2719 | | check_autoload_required(VALUE mod, ID id, const char **loadingpath) |
2720 | 0 | { |
2721 | 0 | VALUE autoload_const_value = autoload_data(mod, id); |
2722 | 0 | struct autoload_data *autoload_data; |
2723 | 0 | const char *loading; |
2724 | |
|
2725 | 0 | if (!autoload_const_value || !(autoload_data = get_autoload_data(autoload_const_value, 0))) { |
2726 | 0 | return 0; |
2727 | 0 | } |
2728 | | |
2729 | 0 | VALUE feature = autoload_data->feature; |
2730 | | |
2731 | | /* |
2732 | | * if somebody else is autoloading, we MUST wait for them, since |
2733 | | * rb_provide_feature can provide a feature before autoload_const_set |
2734 | | * completes. We must wait until autoload_const_set finishes in |
2735 | | * the other thread. |
2736 | | */ |
2737 | 0 | if (autoload_by_someone_else(autoload_data)) { |
2738 | 0 | return autoload_const_value; |
2739 | 0 | } |
2740 | | |
2741 | 0 | loading = RSTRING_PTR(feature); |
2742 | |
|
2743 | 0 | if (!rb_feature_provided(loading, &loading)) { |
2744 | 0 | return autoload_const_value; |
2745 | 0 | } |
2746 | | |
2747 | 0 | if (loadingpath && loading) { |
2748 | 0 | *loadingpath = loading; |
2749 | 0 | return autoload_const_value; |
2750 | 0 | } |
2751 | | |
2752 | 0 | return 0; |
2753 | 0 | } |
2754 | | |
2755 | | static struct autoload_const *autoloading_const_entry(VALUE mod, ID id); |
2756 | | |
2757 | | int |
2758 | | rb_autoloading_value(VALUE mod, ID id, VALUE* value, rb_const_flag_t *flag) |
2759 | 0 | { |
2760 | 0 | struct autoload_const *ac = autoloading_const_entry(mod, id); |
2761 | 0 | if (!ac) return FALSE; |
2762 | | |
2763 | 0 | if (value) { |
2764 | 0 | *value = ac->value; |
2765 | 0 | } |
2766 | |
|
2767 | 0 | if (flag) { |
2768 | 0 | *flag = ac->flag; |
2769 | 0 | } |
2770 | |
|
2771 | 0 | return TRUE; |
2772 | 0 | } |
2773 | | |
2774 | | static int |
2775 | | autoload_by_current(struct autoload_data *ele) |
2776 | 0 | { |
2777 | 0 | return ele->mutex != Qnil && rb_mutex_owned_p(ele->mutex); |
2778 | 0 | } |
2779 | | |
2780 | | // If there is an autoloading constant and it has been set by the current |
2781 | | // execution context, return it. This allows threads which are loading code to |
2782 | | // refer to their own autoloaded constants. |
2783 | | struct autoload_const * |
2784 | | autoloading_const_entry(VALUE mod, ID id) |
2785 | 0 | { |
2786 | 0 | VALUE load = autoload_data(mod, id); |
2787 | 0 | struct autoload_data *ele; |
2788 | 0 | struct autoload_const *ac; |
2789 | | |
2790 | | // Find the autoloading state: |
2791 | 0 | if (!load || !(ele = get_autoload_data(load, &ac))) { |
2792 | | // Couldn't be found: |
2793 | 0 | return 0; |
2794 | 0 | } |
2795 | | |
2796 | | // Check if it's being loaded by the current thread/fiber: |
2797 | 0 | if (autoload_by_current(ele)) { |
2798 | 0 | if (!UNDEF_P(ac->value)) { |
2799 | 0 | return ac; |
2800 | 0 | } |
2801 | 0 | } |
2802 | | |
2803 | 0 | return 0; |
2804 | 0 | } |
2805 | | |
2806 | | static int |
2807 | | autoload_defined_p(VALUE mod, ID id) |
2808 | 0 | { |
2809 | 0 | rb_const_entry_t *ce = rb_const_lookup(mod, id); |
2810 | | |
2811 | | // If there is no constant or the constant is not undefined (special marker for autoloading): |
2812 | 0 | if (!ce || !UNDEF_P(ce->value)) { |
2813 | | // We are not autoloading: |
2814 | 0 | return 0; |
2815 | 0 | } |
2816 | | |
2817 | | // Otherwise check if there is an autoload in flight right now: |
2818 | 0 | return !rb_autoloading_value(mod, id, NULL, NULL); |
2819 | 0 | } |
2820 | | |
2821 | | static void const_tbl_update(struct autoload_const *, int); |
2822 | | |
2823 | | struct autoload_load_arguments { |
2824 | | VALUE module; |
2825 | | ID name; |
2826 | | int flag; |
2827 | | |
2828 | | VALUE mutex; |
2829 | | |
2830 | | // The specific constant which triggered the autoload code to fire: |
2831 | | struct autoload_const *autoload_const; |
2832 | | |
2833 | | // The parent autoload data which is shared between multiple constants: |
2834 | | struct autoload_data *autoload_data; |
2835 | | }; |
2836 | | |
2837 | | static VALUE |
2838 | | autoload_const_set(struct autoload_const *ac) |
2839 | 0 | { |
2840 | 0 | check_before_mod_set(ac->module, ac->name, ac->value, "constant"); |
2841 | |
|
2842 | 0 | RB_VM_LOCK_ENTER(); |
2843 | 0 | { |
2844 | 0 | const_tbl_update(ac, true); |
2845 | 0 | } |
2846 | 0 | RB_VM_LOCK_LEAVE(); |
2847 | |
|
2848 | 0 | return 0; /* ignored */ |
2849 | 0 | } |
2850 | | |
2851 | | static VALUE |
2852 | | autoload_load_needed(VALUE _arguments) |
2853 | 0 | { |
2854 | 0 | struct autoload_load_arguments *arguments = (struct autoload_load_arguments*)_arguments; |
2855 | |
|
2856 | 0 | const char *loading = 0, *src; |
2857 | |
|
2858 | 0 | if (!autoload_defined_p(arguments->module, arguments->name)) { |
2859 | 0 | return Qfalse; |
2860 | 0 | } |
2861 | | |
2862 | 0 | VALUE autoload_const_value = check_autoload_required(arguments->module, arguments->name, &loading); |
2863 | 0 | if (!autoload_const_value) { |
2864 | 0 | return Qfalse; |
2865 | 0 | } |
2866 | | |
2867 | 0 | src = rb_sourcefile(); |
2868 | 0 | if (src && loading && strcmp(src, loading) == 0) { |
2869 | 0 | return Qfalse; |
2870 | 0 | } |
2871 | | |
2872 | 0 | struct autoload_const *autoload_const; |
2873 | 0 | struct autoload_data *autoload_data; |
2874 | 0 | if (!(autoload_data = get_autoload_data(autoload_const_value, &autoload_const))) { |
2875 | 0 | return Qfalse; |
2876 | 0 | } |
2877 | | |
2878 | 0 | if (NIL_P(autoload_data->mutex)) { |
2879 | 0 | RB_OBJ_WRITE(autoload_const->autoload_data_value, &autoload_data->mutex, rb_mutex_new()); |
2880 | 0 | autoload_data->fork_gen = GET_VM()->fork_gen; |
2881 | 0 | } |
2882 | 0 | else if (rb_mutex_owned_p(autoload_data->mutex)) { |
2883 | 0 | return Qfalse; |
2884 | 0 | } |
2885 | | |
2886 | 0 | arguments->mutex = autoload_data->mutex; |
2887 | 0 | arguments->autoload_const = autoload_const; |
2888 | |
|
2889 | 0 | return autoload_const_value; |
2890 | 0 | } |
2891 | | |
2892 | | static VALUE |
2893 | | autoload_apply_constants(VALUE _arguments) |
2894 | 0 | { |
2895 | 0 | RUBY_ASSERT_CRITICAL_SECTION_ENTER(); |
2896 | |
|
2897 | 0 | struct autoload_load_arguments *arguments = (struct autoload_load_arguments*)_arguments; |
2898 | |
|
2899 | 0 | struct autoload_const *autoload_const = 0; // for ccan_container_off_var() |
2900 | 0 | struct autoload_const *next; |
2901 | | |
2902 | | // We use safe iteration here because `autoload_const_set` will eventually invoke |
2903 | | // `autoload_delete` which will remove the constant from the linked list. In theory, once |
2904 | | // the `autoload_data->constants` linked list is empty, we can remove it. |
2905 | | |
2906 | | // Iterate over all constants and assign them: |
2907 | 0 | ccan_list_for_each_safe(&arguments->autoload_data->constants, autoload_const, next, cnode) { |
2908 | 0 | if (!UNDEF_P(autoload_const->value)) { |
2909 | 0 | autoload_const_set(autoload_const); |
2910 | 0 | } |
2911 | 0 | } |
2912 | |
|
2913 | 0 | RUBY_ASSERT_CRITICAL_SECTION_LEAVE(); |
2914 | |
|
2915 | 0 | return Qtrue; |
2916 | 0 | } |
2917 | | |
2918 | | static VALUE |
2919 | | autoload_feature_require(VALUE _arguments) |
2920 | 0 | { |
2921 | 0 | struct autoload_load_arguments *arguments = (struct autoload_load_arguments*)_arguments; |
2922 | |
|
2923 | 0 | struct autoload_const *autoload_const = arguments->autoload_const; |
2924 | | |
2925 | | // We save this for later use in autoload_apply_constants: |
2926 | 0 | arguments->autoload_data = rb_check_typeddata(autoload_const->autoload_data_value, &autoload_data_type); |
2927 | |
|
2928 | 0 | VALUE result = rb_funcall(rb_vm_top_self(), rb_intern("require"), 1, arguments->autoload_data->feature); |
2929 | |
|
2930 | 0 | if (RTEST(result)) { |
2931 | 0 | return rb_mutex_synchronize(autoload_mutex, autoload_apply_constants, _arguments); |
2932 | 0 | } |
2933 | | |
2934 | 0 | return result; |
2935 | 0 | } |
2936 | | |
2937 | | static VALUE |
2938 | | autoload_try_load(VALUE _arguments) |
2939 | 0 | { |
2940 | 0 | struct autoload_load_arguments *arguments = (struct autoload_load_arguments*)_arguments; |
2941 | |
|
2942 | 0 | VALUE result = autoload_feature_require(_arguments); |
2943 | | |
2944 | | // After we loaded the feature, if the constant is not defined, we remove it completely: |
2945 | 0 | rb_const_entry_t *ce = rb_const_lookup(arguments->module, arguments->name); |
2946 | |
|
2947 | 0 | if (!ce || UNDEF_P(ce->value)) { |
2948 | 0 | result = Qfalse; |
2949 | |
|
2950 | 0 | rb_const_remove(arguments->module, arguments->name); |
2951 | |
|
2952 | 0 | if (arguments->module == rb_cObject) { |
2953 | 0 | rb_warning( |
2954 | 0 | "Expected %"PRIsVALUE" to define %"PRIsVALUE" but it didn't", |
2955 | 0 | arguments->autoload_data->feature, |
2956 | 0 | ID2SYM(arguments->name) |
2957 | 0 | ); |
2958 | 0 | } |
2959 | 0 | else { |
2960 | 0 | rb_warning( |
2961 | 0 | "Expected %"PRIsVALUE" to define %"PRIsVALUE"::%"PRIsVALUE" but it didn't", |
2962 | 0 | arguments->autoload_data->feature, |
2963 | 0 | arguments->module, |
2964 | 0 | ID2SYM(arguments->name) |
2965 | 0 | ); |
2966 | 0 | } |
2967 | 0 | } |
2968 | 0 | else { |
2969 | | // Otherwise, it was loaded, copy the flags from the autoload constant: |
2970 | 0 | ce->flag |= arguments->flag; |
2971 | 0 | } |
2972 | |
|
2973 | 0 | return result; |
2974 | 0 | } |
2975 | | |
2976 | | VALUE |
2977 | | rb_autoload_load(VALUE module, ID name) |
2978 | 0 | { |
2979 | 0 | rb_const_entry_t *ce = rb_const_lookup(module, name); |
2980 | | |
2981 | | // We bail out as early as possible without any synchronisation: |
2982 | 0 | if (!ce || !UNDEF_P(ce->value)) { |
2983 | 0 | return Qfalse; |
2984 | 0 | } |
2985 | | |
2986 | | // At this point, we assume there might be autoloading, so fail if it's ractor: |
2987 | 0 | if (UNLIKELY(!rb_ractor_main_p())) { |
2988 | 0 | rb_raise(rb_eRactorUnsafeError, "require by autoload on non-main Ractor is not supported (%s)", rb_id2name(name)); |
2989 | 0 | } |
2990 | | |
2991 | | // This state is stored on the stack and is used during the autoload process. |
2992 | 0 | struct autoload_load_arguments arguments = {.module = module, .name = name, .mutex = Qnil}; |
2993 | | |
2994 | | // Figure out whether we can autoload the named constant: |
2995 | 0 | VALUE autoload_const_value = rb_mutex_synchronize(autoload_mutex, autoload_load_needed, (VALUE)&arguments); |
2996 | | |
2997 | | // This confirms whether autoloading is required or not: |
2998 | 0 | if (autoload_const_value == Qfalse) return autoload_const_value; |
2999 | | |
3000 | 0 | arguments.flag = ce->flag & (CONST_DEPRECATED | CONST_VISIBILITY_MASK); |
3001 | | |
3002 | | // Only one thread will enter here at a time: |
3003 | 0 | VALUE result = rb_mutex_synchronize(arguments.mutex, autoload_try_load, (VALUE)&arguments); |
3004 | | |
3005 | | // If you don't guard this value, it's possible for the autoload constant to |
3006 | | // be freed by another thread which loads multiple constants, one of which |
3007 | | // resolves to the constant this thread is trying to load, so proteect this |
3008 | | // so that it is not freed until we are done with it in `autoload_try_load`: |
3009 | 0 | RB_GC_GUARD(autoload_const_value); |
3010 | |
|
3011 | 0 | return result; |
3012 | 0 | } |
3013 | | |
3014 | | VALUE |
3015 | | rb_autoload_p(VALUE mod, ID id) |
3016 | 0 | { |
3017 | 0 | return rb_autoload_at_p(mod, id, TRUE); |
3018 | 0 | } |
3019 | | |
3020 | | VALUE |
3021 | | rb_autoload_at_p(VALUE mod, ID id, int recur) |
3022 | 0 | { |
3023 | 0 | VALUE load; |
3024 | 0 | struct autoload_data *ele; |
3025 | |
|
3026 | 0 | while (!autoload_defined_p(mod, id)) { |
3027 | 0 | if (!recur) return Qnil; |
3028 | 0 | mod = RCLASS_SUPER(mod); |
3029 | 0 | if (!mod) return Qnil; |
3030 | 0 | } |
3031 | 0 | load = check_autoload_required(mod, id, 0); |
3032 | 0 | if (!load) return Qnil; |
3033 | 0 | return (ele = get_autoload_data(load, 0)) ? ele->feature : Qnil; |
3034 | 0 | } |
3035 | | |
3036 | | void |
3037 | | rb_const_warn_if_deprecated(const rb_const_entry_t *ce, VALUE klass, ID id) |
3038 | 0 | { |
3039 | 0 | if (RB_CONST_DEPRECATED_P(ce) && |
3040 | 0 | rb_warning_category_enabled_p(RB_WARN_CATEGORY_DEPRECATED)) { |
3041 | 0 | if (klass == rb_cObject) { |
3042 | 0 | rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "constant ::%"PRIsVALUE" is deprecated", QUOTE_ID(id)); |
3043 | 0 | } |
3044 | 0 | else { |
3045 | 0 | rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "constant %"PRIsVALUE"::%"PRIsVALUE" is deprecated", |
3046 | 0 | rb_class_name(klass), QUOTE_ID(id)); |
3047 | 0 | } |
3048 | 0 | } |
3049 | 0 | } |
3050 | | |
3051 | | static VALUE |
3052 | | rb_const_get_0(VALUE klass, ID id, int exclude, int recurse, int visibility) |
3053 | 0 | { |
3054 | 0 | VALUE c = rb_const_search(klass, id, exclude, recurse, visibility); |
3055 | 0 | if (!UNDEF_P(c)) { |
3056 | 0 | if (UNLIKELY(!rb_ractor_main_p())) { |
3057 | 0 | if (!rb_ractor_shareable_p(c)) { |
3058 | 0 | rb_raise(rb_eRactorIsolationError, "can not access non-shareable objects in constant %"PRIsVALUE"::%s by non-main Ractor.", rb_class_path(klass), rb_id2name(id)); |
3059 | 0 | } |
3060 | 0 | } |
3061 | 0 | return c; |
3062 | 0 | } |
3063 | 0 | return rb_const_missing(klass, ID2SYM(id)); |
3064 | 0 | } |
3065 | | |
3066 | | static VALUE |
3067 | | rb_const_search_from(VALUE klass, ID id, int exclude, int recurse, int visibility) |
3068 | 0 | { |
3069 | 0 | VALUE value, current; |
3070 | 0 | bool first_iteration = true; |
3071 | |
|
3072 | 0 | for (current = klass; |
3073 | 0 | RTEST(current); |
3074 | 0 | current = RCLASS_SUPER(current), first_iteration = false) { |
3075 | 0 | VALUE tmp; |
3076 | 0 | VALUE am = 0; |
3077 | 0 | rb_const_entry_t *ce; |
3078 | |
|
3079 | 0 | if (!first_iteration && RCLASS_ORIGIN(current) != current) { |
3080 | | // This item in the super chain has an origin iclass |
3081 | | // that comes later in the chain. Skip this item so |
3082 | | // prepended modules take precedence. |
3083 | 0 | continue; |
3084 | 0 | } |
3085 | | |
3086 | | // Do lookup in original class or module in case we are at an origin |
3087 | | // iclass in the chain. |
3088 | 0 | tmp = current; |
3089 | 0 | if (BUILTIN_TYPE(tmp) == T_ICLASS) tmp = RBASIC(tmp)->klass; |
3090 | | |
3091 | | // Do the lookup. Loop in case of autoload. |
3092 | 0 | while ((ce = rb_const_lookup(tmp, id))) { |
3093 | 0 | if (visibility && RB_CONST_PRIVATE_P(ce)) { |
3094 | 0 | GET_EC()->private_const_reference = tmp; |
3095 | 0 | return Qundef; |
3096 | 0 | } |
3097 | 0 | rb_const_warn_if_deprecated(ce, tmp, id); |
3098 | 0 | value = ce->value; |
3099 | 0 | if (UNDEF_P(value)) { |
3100 | 0 | struct autoload_const *ac; |
3101 | 0 | if (am == tmp) break; |
3102 | 0 | am = tmp; |
3103 | 0 | ac = autoloading_const_entry(tmp, id); |
3104 | 0 | if (ac) return ac->value; |
3105 | 0 | rb_autoload_load(tmp, id); |
3106 | 0 | continue; |
3107 | 0 | } |
3108 | 0 | if (exclude && tmp == rb_cObject) { |
3109 | 0 | goto not_found; |
3110 | 0 | } |
3111 | 0 | return value; |
3112 | 0 | } |
3113 | 0 | if (!recurse) break; |
3114 | 0 | } |
3115 | | |
3116 | 0 | not_found: |
3117 | 0 | GET_EC()->private_const_reference = 0; |
3118 | 0 | return Qundef; |
3119 | 0 | } |
3120 | | |
3121 | | static VALUE |
3122 | | rb_const_search(VALUE klass, ID id, int exclude, int recurse, int visibility) |
3123 | 0 | { |
3124 | 0 | VALUE value; |
3125 | |
|
3126 | 0 | if (klass == rb_cObject) exclude = FALSE; |
3127 | 0 | value = rb_const_search_from(klass, id, exclude, recurse, visibility); |
3128 | 0 | if (!UNDEF_P(value)) return value; |
3129 | 0 | if (exclude) return value; |
3130 | 0 | if (BUILTIN_TYPE(klass) != T_MODULE) return value; |
3131 | | /* search global const too, if klass is a module */ |
3132 | 0 | return rb_const_search_from(rb_cObject, id, FALSE, recurse, visibility); |
3133 | 0 | } |
3134 | | |
3135 | | VALUE |
3136 | | rb_const_get_from(VALUE klass, ID id) |
3137 | 0 | { |
3138 | 0 | return rb_const_get_0(klass, id, TRUE, TRUE, FALSE); |
3139 | 0 | } |
3140 | | |
3141 | | VALUE |
3142 | | rb_const_get(VALUE klass, ID id) |
3143 | 0 | { |
3144 | 0 | return rb_const_get_0(klass, id, FALSE, TRUE, FALSE); |
3145 | 0 | } |
3146 | | |
3147 | | VALUE |
3148 | | rb_const_get_at(VALUE klass, ID id) |
3149 | 0 | { |
3150 | 0 | return rb_const_get_0(klass, id, TRUE, FALSE, FALSE); |
3151 | 0 | } |
3152 | | |
3153 | | VALUE |
3154 | | rb_public_const_get_from(VALUE klass, ID id) |
3155 | 0 | { |
3156 | 0 | return rb_const_get_0(klass, id, TRUE, TRUE, TRUE); |
3157 | 0 | } |
3158 | | |
3159 | | VALUE |
3160 | | rb_public_const_get_at(VALUE klass, ID id) |
3161 | 0 | { |
3162 | 0 | return rb_const_get_0(klass, id, TRUE, FALSE, TRUE); |
3163 | 0 | } |
3164 | | |
3165 | | NORETURN(static void undefined_constant(VALUE mod, VALUE name)); |
3166 | | static void |
3167 | | undefined_constant(VALUE mod, VALUE name) |
3168 | 0 | { |
3169 | 0 | rb_name_err_raise("constant %2$s::%1$s not defined", |
3170 | 0 | mod, name); |
3171 | 0 | } |
3172 | | |
3173 | | static VALUE |
3174 | | rb_const_location_from(VALUE klass, ID id, int exclude, int recurse, int visibility) |
3175 | 0 | { |
3176 | 0 | while (RTEST(klass)) { |
3177 | 0 | rb_const_entry_t *ce; |
3178 | |
|
3179 | 0 | while ((ce = rb_const_lookup(klass, id))) { |
3180 | 0 | if (visibility && RB_CONST_PRIVATE_P(ce)) { |
3181 | 0 | return Qnil; |
3182 | 0 | } |
3183 | 0 | if (exclude && klass == rb_cObject) { |
3184 | 0 | goto not_found; |
3185 | 0 | } |
3186 | 0 | if (NIL_P(ce->file)) return rb_ary_new(); |
3187 | 0 | return rb_assoc_new(ce->file, INT2NUM(ce->line)); |
3188 | 0 | } |
3189 | 0 | if (!recurse) break; |
3190 | 0 | klass = RCLASS_SUPER(klass); |
3191 | 0 | } |
3192 | | |
3193 | 0 | not_found: |
3194 | 0 | return Qnil; |
3195 | 0 | } |
3196 | | |
3197 | | static VALUE |
3198 | | rb_const_location(VALUE klass, ID id, int exclude, int recurse, int visibility) |
3199 | 0 | { |
3200 | 0 | VALUE loc; |
3201 | |
|
3202 | 0 | if (klass == rb_cObject) exclude = FALSE; |
3203 | 0 | loc = rb_const_location_from(klass, id, exclude, recurse, visibility); |
3204 | 0 | if (!NIL_P(loc)) return loc; |
3205 | 0 | if (exclude) return loc; |
3206 | 0 | if (BUILTIN_TYPE(klass) != T_MODULE) return loc; |
3207 | | /* search global const too, if klass is a module */ |
3208 | 0 | return rb_const_location_from(rb_cObject, id, FALSE, recurse, visibility); |
3209 | 0 | } |
3210 | | |
3211 | | VALUE |
3212 | | rb_const_source_location(VALUE klass, ID id) |
3213 | 0 | { |
3214 | 0 | return rb_const_location(klass, id, FALSE, TRUE, FALSE); |
3215 | 0 | } |
3216 | | |
3217 | | VALUE |
3218 | | rb_const_source_location_at(VALUE klass, ID id) |
3219 | 0 | { |
3220 | 0 | return rb_const_location(klass, id, TRUE, FALSE, FALSE); |
3221 | 0 | } |
3222 | | |
3223 | | /* |
3224 | | * call-seq: |
3225 | | * remove_const(sym) -> obj |
3226 | | * |
3227 | | * Removes the definition of the given constant, returning that |
3228 | | * constant's previous value. If that constant referred to |
3229 | | * a module, this will not change that module's name and can lead |
3230 | | * to confusion. |
3231 | | */ |
3232 | | |
3233 | | VALUE |
3234 | | rb_mod_remove_const(VALUE mod, VALUE name) |
3235 | 0 | { |
3236 | 0 | const ID id = id_for_var(mod, name, a, constant); |
3237 | |
|
3238 | 0 | if (!id) { |
3239 | 0 | undefined_constant(mod, name); |
3240 | 0 | } |
3241 | 0 | return rb_const_remove(mod, id); |
3242 | 0 | } |
3243 | | |
3244 | | VALUE |
3245 | | rb_const_remove(VALUE mod, ID id) |
3246 | 0 | { |
3247 | 0 | VALUE val; |
3248 | 0 | rb_const_entry_t *ce; |
3249 | |
|
3250 | 0 | rb_check_frozen(mod); |
3251 | |
|
3252 | 0 | ce = rb_const_lookup(mod, id); |
3253 | 0 | if (!ce || !rb_id_table_delete(RCLASS_CONST_TBL(mod), id)) { |
3254 | 0 | if (rb_const_defined_at(mod, id)) { |
3255 | 0 | rb_name_err_raise("cannot remove %2$s::%1$s", mod, ID2SYM(id)); |
3256 | 0 | } |
3257 | | |
3258 | 0 | undefined_constant(mod, ID2SYM(id)); |
3259 | 0 | } |
3260 | | |
3261 | 0 | rb_clear_constant_cache_for_id(id); |
3262 | |
|
3263 | 0 | val = ce->value; |
3264 | |
|
3265 | 0 | if (UNDEF_P(val)) { |
3266 | 0 | autoload_delete(mod, id); |
3267 | 0 | val = Qnil; |
3268 | 0 | } |
3269 | |
|
3270 | 0 | ruby_xfree(ce); |
3271 | |
|
3272 | 0 | return val; |
3273 | 0 | } |
3274 | | |
3275 | | static int |
3276 | | cv_i_update(st_data_t *k, st_data_t *v, st_data_t a, int existing) |
3277 | 0 | { |
3278 | 0 | if (existing) return ST_STOP; |
3279 | 0 | *v = a; |
3280 | 0 | return ST_CONTINUE; |
3281 | 0 | } |
3282 | | |
3283 | | static enum rb_id_table_iterator_result |
3284 | | sv_i(ID key, VALUE v, void *a) |
3285 | 0 | { |
3286 | 0 | rb_const_entry_t *ce = (rb_const_entry_t *)v; |
3287 | 0 | st_table *tbl = a; |
3288 | |
|
3289 | 0 | if (rb_is_const_id(key)) { |
3290 | 0 | st_update(tbl, (st_data_t)key, cv_i_update, (st_data_t)ce); |
3291 | 0 | } |
3292 | 0 | return ID_TABLE_CONTINUE; |
3293 | 0 | } |
3294 | | |
3295 | | static enum rb_id_table_iterator_result |
3296 | | rb_local_constants_i(ID const_name, VALUE const_value, void *ary) |
3297 | 0 | { |
3298 | 0 | if (rb_is_const_id(const_name) && !RB_CONST_PRIVATE_P((rb_const_entry_t *)const_value)) { |
3299 | 0 | rb_ary_push((VALUE)ary, ID2SYM(const_name)); |
3300 | 0 | } |
3301 | 0 | return ID_TABLE_CONTINUE; |
3302 | 0 | } |
3303 | | |
3304 | | static VALUE |
3305 | | rb_local_constants(VALUE mod) |
3306 | 0 | { |
3307 | 0 | struct rb_id_table *tbl = RCLASS_CONST_TBL(mod); |
3308 | 0 | VALUE ary; |
3309 | |
|
3310 | 0 | if (!tbl) return rb_ary_new2(0); |
3311 | | |
3312 | 0 | RB_VM_LOCK_ENTER(); |
3313 | 0 | { |
3314 | 0 | ary = rb_ary_new2(rb_id_table_size(tbl)); |
3315 | 0 | rb_id_table_foreach(tbl, rb_local_constants_i, (void *)ary); |
3316 | 0 | } |
3317 | 0 | RB_VM_LOCK_LEAVE(); |
3318 | |
|
3319 | 0 | return ary; |
3320 | 0 | } |
3321 | | |
3322 | | void* |
3323 | | rb_mod_const_at(VALUE mod, void *data) |
3324 | 0 | { |
3325 | 0 | st_table *tbl = data; |
3326 | 0 | if (!tbl) { |
3327 | 0 | tbl = st_init_numtable(); |
3328 | 0 | } |
3329 | 0 | if (RCLASS_CONST_TBL(mod)) { |
3330 | 0 | RB_VM_LOCK_ENTER(); |
3331 | 0 | { |
3332 | 0 | rb_id_table_foreach(RCLASS_CONST_TBL(mod), sv_i, tbl); |
3333 | 0 | } |
3334 | 0 | RB_VM_LOCK_LEAVE(); |
3335 | 0 | } |
3336 | 0 | return tbl; |
3337 | 0 | } |
3338 | | |
3339 | | void* |
3340 | | rb_mod_const_of(VALUE mod, void *data) |
3341 | 0 | { |
3342 | 0 | VALUE tmp = mod; |
3343 | 0 | for (;;) { |
3344 | 0 | data = rb_mod_const_at(tmp, data); |
3345 | 0 | tmp = RCLASS_SUPER(tmp); |
3346 | 0 | if (!tmp) break; |
3347 | 0 | if (tmp == rb_cObject && mod != rb_cObject) break; |
3348 | 0 | } |
3349 | 0 | return data; |
3350 | 0 | } |
3351 | | |
3352 | | static int |
3353 | | list_i(st_data_t key, st_data_t value, VALUE ary) |
3354 | 0 | { |
3355 | 0 | ID sym = (ID)key; |
3356 | 0 | rb_const_entry_t *ce = (rb_const_entry_t *)value; |
3357 | 0 | if (RB_CONST_PUBLIC_P(ce)) rb_ary_push(ary, ID2SYM(sym)); |
3358 | 0 | return ST_CONTINUE; |
3359 | 0 | } |
3360 | | |
3361 | | VALUE |
3362 | | rb_const_list(void *data) |
3363 | 0 | { |
3364 | 0 | st_table *tbl = data; |
3365 | 0 | VALUE ary; |
3366 | |
|
3367 | 0 | if (!tbl) return rb_ary_new2(0); |
3368 | 0 | ary = rb_ary_new2(tbl->num_entries); |
3369 | 0 | st_foreach_safe(tbl, list_i, ary); |
3370 | 0 | st_free_table(tbl); |
3371 | |
|
3372 | 0 | return ary; |
3373 | 0 | } |
3374 | | |
3375 | | /* |
3376 | | * call-seq: |
3377 | | * mod.constants(inherit=true) -> array |
3378 | | * |
3379 | | * Returns an array of the names of the constants accessible in |
3380 | | * <i>mod</i>. This includes the names of constants in any included |
3381 | | * modules (example at start of section), unless the <i>inherit</i> |
3382 | | * parameter is set to <code>false</code>. |
3383 | | * |
3384 | | * The implementation makes no guarantees about the order in which the |
3385 | | * constants are yielded. |
3386 | | * |
3387 | | * IO.constants.include?(:SYNC) #=> true |
3388 | | * IO.constants(false).include?(:SYNC) #=> false |
3389 | | * |
3390 | | * Also see Module#const_defined?. |
3391 | | */ |
3392 | | |
3393 | | VALUE |
3394 | | rb_mod_constants(int argc, const VALUE *argv, VALUE mod) |
3395 | 0 | { |
3396 | 0 | bool inherit = true; |
3397 | |
|
3398 | 0 | if (rb_check_arity(argc, 0, 1)) inherit = RTEST(argv[0]); |
3399 | |
|
3400 | 0 | if (inherit) { |
3401 | 0 | return rb_const_list(rb_mod_const_of(mod, 0)); |
3402 | 0 | } |
3403 | 0 | else { |
3404 | 0 | return rb_local_constants(mod); |
3405 | 0 | } |
3406 | 0 | } |
3407 | | |
3408 | | static int |
3409 | | rb_const_defined_0(VALUE klass, ID id, int exclude, int recurse, int visibility) |
3410 | 0 | { |
3411 | 0 | VALUE tmp; |
3412 | 0 | int mod_retry = 0; |
3413 | 0 | rb_const_entry_t *ce; |
3414 | |
|
3415 | 0 | tmp = klass; |
3416 | 0 | retry: |
3417 | 0 | while (tmp) { |
3418 | 0 | if ((ce = rb_const_lookup(tmp, id))) { |
3419 | 0 | if (visibility && RB_CONST_PRIVATE_P(ce)) { |
3420 | 0 | return (int)Qfalse; |
3421 | 0 | } |
3422 | 0 | if (UNDEF_P(ce->value) && !check_autoload_required(tmp, id, 0) && |
3423 | 0 | !rb_autoloading_value(tmp, id, NULL, NULL)) |
3424 | 0 | return (int)Qfalse; |
3425 | | |
3426 | 0 | if (exclude && tmp == rb_cObject && klass != rb_cObject) { |
3427 | 0 | return (int)Qfalse; |
3428 | 0 | } |
3429 | | |
3430 | 0 | return (int)Qtrue; |
3431 | 0 | } |
3432 | 0 | if (!recurse) break; |
3433 | 0 | tmp = RCLASS_SUPER(tmp); |
3434 | 0 | } |
3435 | 0 | if (!exclude && !mod_retry && BUILTIN_TYPE(klass) == T_MODULE) { |
3436 | 0 | mod_retry = 1; |
3437 | 0 | tmp = rb_cObject; |
3438 | 0 | goto retry; |
3439 | 0 | } |
3440 | 0 | return (int)Qfalse; |
3441 | 0 | } |
3442 | | |
3443 | | int |
3444 | | rb_const_defined_from(VALUE klass, ID id) |
3445 | 0 | { |
3446 | 0 | return rb_const_defined_0(klass, id, TRUE, TRUE, FALSE); |
3447 | 0 | } |
3448 | | |
3449 | | int |
3450 | | rb_const_defined(VALUE klass, ID id) |
3451 | 0 | { |
3452 | 0 | return rb_const_defined_0(klass, id, FALSE, TRUE, FALSE); |
3453 | 0 | } |
3454 | | |
3455 | | int |
3456 | | rb_const_defined_at(VALUE klass, ID id) |
3457 | 0 | { |
3458 | 0 | return rb_const_defined_0(klass, id, TRUE, FALSE, FALSE); |
3459 | 0 | } |
3460 | | |
3461 | | int |
3462 | | rb_public_const_defined_from(VALUE klass, ID id) |
3463 | 0 | { |
3464 | 0 | return rb_const_defined_0(klass, id, TRUE, TRUE, TRUE); |
3465 | 0 | } |
3466 | | |
3467 | | static void |
3468 | | check_before_mod_set(VALUE klass, ID id, VALUE val, const char *dest) |
3469 | 0 | { |
3470 | 0 | rb_check_frozen(klass); |
3471 | 0 | } |
3472 | | |
3473 | | static void set_namespace_path(VALUE named_namespace, VALUE name); |
3474 | | |
3475 | | static enum rb_id_table_iterator_result |
3476 | | set_namespace_path_i(ID id, VALUE v, void *payload) |
3477 | 0 | { |
3478 | 0 | rb_const_entry_t *ce = (rb_const_entry_t *)v; |
3479 | 0 | VALUE value = ce->value; |
3480 | 0 | VALUE parental_path = *((VALUE *) payload); |
3481 | 0 | if (!rb_is_const_id(id) || !rb_namespace_p(value)) { |
3482 | 0 | return ID_TABLE_CONTINUE; |
3483 | 0 | } |
3484 | | |
3485 | 0 | bool has_permanent_classpath; |
3486 | 0 | classname(value, &has_permanent_classpath); |
3487 | 0 | if (has_permanent_classpath) { |
3488 | 0 | return ID_TABLE_CONTINUE; |
3489 | 0 | } |
3490 | 0 | set_namespace_path(value, build_const_path(parental_path, id)); |
3491 | |
|
3492 | 0 | if (!RCLASS_EXT(value)->permanent_classpath) { |
3493 | 0 | RCLASS_SET_CLASSPATH(value, 0, false); |
3494 | 0 | } |
3495 | |
|
3496 | 0 | return ID_TABLE_CONTINUE; |
3497 | 0 | } |
3498 | | |
3499 | | /* |
3500 | | * Assign permanent classpaths to all namespaces that are directly or indirectly |
3501 | | * nested under +named_namespace+. +named_namespace+ must have a permanent |
3502 | | * classpath. |
3503 | | */ |
3504 | | static void |
3505 | | set_namespace_path(VALUE named_namespace, VALUE namespace_path) |
3506 | 0 | { |
3507 | 0 | struct rb_id_table *const_table = RCLASS_CONST_TBL(named_namespace); |
3508 | |
|
3509 | 0 | RB_VM_LOCK_ENTER(); |
3510 | 0 | { |
3511 | 0 | RCLASS_SET_CLASSPATH(named_namespace, namespace_path, true); |
3512 | |
|
3513 | 0 | if (const_table) { |
3514 | 0 | rb_id_table_foreach(const_table, set_namespace_path_i, &namespace_path); |
3515 | 0 | } |
3516 | 0 | } |
3517 | 0 | RB_VM_LOCK_LEAVE(); |
3518 | 0 | } |
3519 | | |
3520 | | static void |
3521 | | const_added(VALUE klass, ID const_name) |
3522 | 0 | { |
3523 | 0 | if (GET_VM()->running) { |
3524 | 0 | VALUE name = ID2SYM(const_name); |
3525 | 0 | rb_funcallv(klass, idConst_added, 1, &name); |
3526 | 0 | } |
3527 | 0 | } |
3528 | | |
3529 | | static void |
3530 | | const_set(VALUE klass, ID id, VALUE val) |
3531 | 0 | { |
3532 | 0 | rb_const_entry_t *ce; |
3533 | |
|
3534 | 0 | if (NIL_P(klass)) { |
3535 | 0 | rb_raise(rb_eTypeError, "no class/module to define constant %"PRIsVALUE"", |
3536 | 0 | QUOTE_ID(id)); |
3537 | 0 | } |
3538 | | |
3539 | 0 | if (!rb_ractor_main_p() && !rb_ractor_shareable_p(val)) { |
3540 | 0 | rb_raise(rb_eRactorIsolationError, "can not set constants with non-shareable objects by non-main Ractors"); |
3541 | 0 | } |
3542 | | |
3543 | 0 | check_before_mod_set(klass, id, val, "constant"); |
3544 | |
|
3545 | 0 | RB_VM_LOCK_ENTER(); |
3546 | 0 | { |
3547 | 0 | struct rb_id_table *tbl = RCLASS_CONST_TBL(klass); |
3548 | 0 | if (!tbl) { |
3549 | 0 | RCLASS_CONST_TBL(klass) = tbl = rb_id_table_create(0); |
3550 | 0 | rb_clear_constant_cache_for_id(id); |
3551 | 0 | ce = ZALLOC(rb_const_entry_t); |
3552 | 0 | rb_id_table_insert(tbl, id, (VALUE)ce); |
3553 | 0 | setup_const_entry(ce, klass, val, CONST_PUBLIC); |
3554 | 0 | } |
3555 | 0 | else { |
3556 | 0 | struct autoload_const ac = { |
3557 | 0 | .module = klass, .name = id, |
3558 | 0 | .value = val, .flag = CONST_PUBLIC, |
3559 | | /* fill the rest with 0 */ |
3560 | 0 | }; |
3561 | 0 | ac.file = rb_source_location(&ac.line); |
3562 | 0 | const_tbl_update(&ac, false); |
3563 | 0 | } |
3564 | 0 | } |
3565 | 0 | RB_VM_LOCK_LEAVE(); |
3566 | | |
3567 | | /* |
3568 | | * Resolve and cache class name immediately to resolve ambiguity |
3569 | | * and avoid order-dependency on const_tbl |
3570 | | */ |
3571 | 0 | if (rb_cObject && rb_namespace_p(val)) { |
3572 | 0 | bool val_path_permanent; |
3573 | 0 | VALUE val_path = classname(val, &val_path_permanent); |
3574 | 0 | if (NIL_P(val_path) || !val_path_permanent) { |
3575 | 0 | if (klass == rb_cObject) { |
3576 | 0 | set_namespace_path(val, rb_id2str(id)); |
3577 | 0 | } |
3578 | 0 | else { |
3579 | 0 | bool parental_path_permanent; |
3580 | 0 | VALUE parental_path = classname(klass, &parental_path_permanent); |
3581 | 0 | if (NIL_P(parental_path)) { |
3582 | 0 | bool throwaway; |
3583 | 0 | parental_path = rb_tmp_class_path(klass, &throwaway, make_temporary_path); |
3584 | 0 | } |
3585 | 0 | if (parental_path_permanent && !val_path_permanent) { |
3586 | 0 | set_namespace_path(val, build_const_path(parental_path, id)); |
3587 | 0 | } |
3588 | 0 | else if (!parental_path_permanent && NIL_P(val_path)) { |
3589 | 0 | RCLASS_SET_CLASSPATH(val, build_const_path(parental_path, id), false); |
3590 | 0 | } |
3591 | 0 | } |
3592 | 0 | } |
3593 | 0 | } |
3594 | 0 | } |
3595 | | |
3596 | | void |
3597 | | rb_const_set(VALUE klass, ID id, VALUE val) |
3598 | 0 | { |
3599 | 0 | const_set(klass, id, val); |
3600 | 0 | const_added(klass, id); |
3601 | 0 | } |
3602 | | |
3603 | | static struct autoload_data * |
3604 | | autoload_data_for_named_constant(VALUE module, ID name, struct autoload_const **autoload_const_pointer) |
3605 | 0 | { |
3606 | 0 | VALUE autoload_data_value = autoload_data(module, name); |
3607 | 0 | if (!autoload_data_value) return 0; |
3608 | | |
3609 | 0 | struct autoload_data *autoload_data = get_autoload_data(autoload_data_value, autoload_const_pointer); |
3610 | 0 | if (!autoload_data) return 0; |
3611 | | |
3612 | | /* for autoloading thread, keep the defined value to autoloading storage */ |
3613 | 0 | if (autoload_by_current(autoload_data)) { |
3614 | 0 | return autoload_data; |
3615 | 0 | } |
3616 | | |
3617 | 0 | return 0; |
3618 | 0 | } |
3619 | | |
3620 | | static void |
3621 | | const_tbl_update(struct autoload_const *ac, int autoload_force) |
3622 | 0 | { |
3623 | 0 | VALUE value; |
3624 | 0 | VALUE klass = ac->module; |
3625 | 0 | VALUE val = ac->value; |
3626 | 0 | ID id = ac->name; |
3627 | 0 | struct rb_id_table *tbl = RCLASS_CONST_TBL(klass); |
3628 | 0 | rb_const_flag_t visibility = ac->flag; |
3629 | 0 | rb_const_entry_t *ce; |
3630 | |
|
3631 | 0 | if (rb_id_table_lookup(tbl, id, &value)) { |
3632 | 0 | ce = (rb_const_entry_t *)value; |
3633 | 0 | if (UNDEF_P(ce->value)) { |
3634 | 0 | RUBY_ASSERT_CRITICAL_SECTION_ENTER(); |
3635 | 0 | VALUE file = ac->file; |
3636 | 0 | int line = ac->line; |
3637 | 0 | struct autoload_data *ele = autoload_data_for_named_constant(klass, id, &ac); |
3638 | |
|
3639 | 0 | if (!autoload_force && ele) { |
3640 | 0 | rb_clear_constant_cache_for_id(id); |
3641 | |
|
3642 | 0 | ac->value = val; /* autoload_data is non-WB-protected */ |
3643 | 0 | ac->file = rb_source_location(&ac->line); |
3644 | 0 | } |
3645 | 0 | else { |
3646 | | /* otherwise autoloaded constant, allow to override */ |
3647 | 0 | autoload_delete(klass, id); |
3648 | 0 | ce->flag = visibility; |
3649 | 0 | RB_OBJ_WRITE(klass, &ce->value, val); |
3650 | 0 | RB_OBJ_WRITE(klass, &ce->file, file); |
3651 | 0 | ce->line = line; |
3652 | 0 | } |
3653 | 0 | RUBY_ASSERT_CRITICAL_SECTION_LEAVE(); |
3654 | 0 | return; |
3655 | 0 | } |
3656 | 0 | else { |
3657 | 0 | VALUE name = QUOTE_ID(id); |
3658 | 0 | visibility = ce->flag; |
3659 | 0 | if (klass == rb_cObject) |
3660 | 0 | rb_warn("already initialized constant %"PRIsVALUE"", name); |
3661 | 0 | else |
3662 | 0 | rb_warn("already initialized constant %"PRIsVALUE"::%"PRIsVALUE"", |
3663 | 0 | rb_class_name(klass), name); |
3664 | 0 | if (!NIL_P(ce->file) && ce->line) { |
3665 | 0 | rb_compile_warn(RSTRING_PTR(ce->file), ce->line, |
3666 | 0 | "previous definition of %"PRIsVALUE" was here", name); |
3667 | 0 | } |
3668 | 0 | } |
3669 | 0 | rb_clear_constant_cache_for_id(id); |
3670 | 0 | setup_const_entry(ce, klass, val, visibility); |
3671 | 0 | } |
3672 | 0 | else { |
3673 | 0 | rb_clear_constant_cache_for_id(id); |
3674 | |
|
3675 | 0 | ce = ZALLOC(rb_const_entry_t); |
3676 | 0 | rb_id_table_insert(tbl, id, (VALUE)ce); |
3677 | 0 | setup_const_entry(ce, klass, val, visibility); |
3678 | 0 | } |
3679 | 0 | } |
3680 | | |
3681 | | static void |
3682 | | setup_const_entry(rb_const_entry_t *ce, VALUE klass, VALUE val, |
3683 | | rb_const_flag_t visibility) |
3684 | 0 | { |
3685 | 0 | ce->flag = visibility; |
3686 | 0 | RB_OBJ_WRITE(klass, &ce->value, val); |
3687 | 0 | RB_OBJ_WRITE(klass, &ce->file, rb_source_location(&ce->line)); |
3688 | 0 | } |
3689 | | |
3690 | | void |
3691 | | rb_define_const(VALUE klass, const char *name, VALUE val) |
3692 | 0 | { |
3693 | 0 | ID id = rb_intern(name); |
3694 | |
|
3695 | 0 | if (!rb_is_const_id(id)) { |
3696 | 0 | rb_warn("rb_define_const: invalid name `%s' for constant", name); |
3697 | 0 | } |
3698 | 0 | rb_gc_register_mark_object(val); |
3699 | 0 | rb_const_set(klass, id, val); |
3700 | 0 | } |
3701 | | |
3702 | | void |
3703 | | rb_define_global_const(const char *name, VALUE val) |
3704 | 0 | { |
3705 | 0 | rb_define_const(rb_cObject, name, val); |
3706 | 0 | } |
3707 | | |
3708 | | static void |
3709 | | set_const_visibility(VALUE mod, int argc, const VALUE *argv, |
3710 | | rb_const_flag_t flag, rb_const_flag_t mask) |
3711 | 0 | { |
3712 | 0 | int i; |
3713 | 0 | rb_const_entry_t *ce; |
3714 | 0 | ID id; |
3715 | |
|
3716 | 0 | rb_class_modify_check(mod); |
3717 | 0 | if (argc == 0) { |
3718 | 0 | rb_warning("%"PRIsVALUE" with no argument is just ignored", |
3719 | 0 | QUOTE_ID(rb_frame_callee())); |
3720 | 0 | return; |
3721 | 0 | } |
3722 | | |
3723 | 0 | for (i = 0; i < argc; i++) { |
3724 | 0 | struct autoload_const *ac; |
3725 | 0 | VALUE val = argv[i]; |
3726 | 0 | id = rb_check_id(&val); |
3727 | 0 | if (!id) { |
3728 | 0 | undefined_constant(mod, val); |
3729 | 0 | } |
3730 | 0 | if ((ce = rb_const_lookup(mod, id))) { |
3731 | 0 | ce->flag &= ~mask; |
3732 | 0 | ce->flag |= flag; |
3733 | 0 | if (UNDEF_P(ce->value)) { |
3734 | 0 | struct autoload_data *ele; |
3735 | |
|
3736 | 0 | ele = autoload_data_for_named_constant(mod, id, &ac); |
3737 | 0 | if (ele) { |
3738 | 0 | ac->flag &= ~mask; |
3739 | 0 | ac->flag |= flag; |
3740 | 0 | } |
3741 | 0 | } |
3742 | 0 | rb_clear_constant_cache_for_id(id); |
3743 | 0 | } |
3744 | 0 | else { |
3745 | 0 | undefined_constant(mod, ID2SYM(id)); |
3746 | 0 | } |
3747 | 0 | } |
3748 | 0 | } |
3749 | | |
3750 | | void |
3751 | | rb_deprecate_constant(VALUE mod, const char *name) |
3752 | 0 | { |
3753 | 0 | rb_const_entry_t *ce; |
3754 | 0 | ID id; |
3755 | 0 | long len = strlen(name); |
3756 | |
|
3757 | 0 | rb_class_modify_check(mod); |
3758 | 0 | if (!(id = rb_check_id_cstr(name, len, NULL))) { |
3759 | 0 | undefined_constant(mod, rb_fstring_new(name, len)); |
3760 | 0 | } |
3761 | 0 | if (!(ce = rb_const_lookup(mod, id))) { |
3762 | 0 | undefined_constant(mod, ID2SYM(id)); |
3763 | 0 | } |
3764 | 0 | ce->flag |= CONST_DEPRECATED; |
3765 | 0 | } |
3766 | | |
3767 | | /* |
3768 | | * call-seq: |
3769 | | * mod.private_constant(symbol, ...) => mod |
3770 | | * |
3771 | | * Makes a list of existing constants private. |
3772 | | */ |
3773 | | |
3774 | | VALUE |
3775 | | rb_mod_private_constant(int argc, const VALUE *argv, VALUE obj) |
3776 | 0 | { |
3777 | 0 | set_const_visibility(obj, argc, argv, CONST_PRIVATE, CONST_VISIBILITY_MASK); |
3778 | 0 | return obj; |
3779 | 0 | } |
3780 | | |
3781 | | /* |
3782 | | * call-seq: |
3783 | | * mod.public_constant(symbol, ...) => mod |
3784 | | * |
3785 | | * Makes a list of existing constants public. |
3786 | | */ |
3787 | | |
3788 | | VALUE |
3789 | | rb_mod_public_constant(int argc, const VALUE *argv, VALUE obj) |
3790 | 0 | { |
3791 | 0 | set_const_visibility(obj, argc, argv, CONST_PUBLIC, CONST_VISIBILITY_MASK); |
3792 | 0 | return obj; |
3793 | 0 | } |
3794 | | |
3795 | | /* |
3796 | | * call-seq: |
3797 | | * mod.deprecate_constant(symbol, ...) => mod |
3798 | | * |
3799 | | * Makes a list of existing constants deprecated. Attempt |
3800 | | * to refer to them will produce a warning. |
3801 | | * |
3802 | | * module HTTP |
3803 | | * NotFound = Exception.new |
3804 | | * NOT_FOUND = NotFound # previous version of the library used this name |
3805 | | * |
3806 | | * deprecate_constant :NOT_FOUND |
3807 | | * end |
3808 | | * |
3809 | | * HTTP::NOT_FOUND |
3810 | | * # warning: constant HTTP::NOT_FOUND is deprecated |
3811 | | * |
3812 | | */ |
3813 | | |
3814 | | VALUE |
3815 | | rb_mod_deprecate_constant(int argc, const VALUE *argv, VALUE obj) |
3816 | 0 | { |
3817 | 0 | set_const_visibility(obj, argc, argv, CONST_DEPRECATED, CONST_DEPRECATED); |
3818 | 0 | return obj; |
3819 | 0 | } |
3820 | | |
3821 | | static VALUE |
3822 | | original_module(VALUE c) |
3823 | 0 | { |
3824 | 0 | if (RB_TYPE_P(c, T_ICLASS)) |
3825 | 0 | return RBASIC(c)->klass; |
3826 | 0 | return c; |
3827 | 0 | } |
3828 | | |
3829 | | static int |
3830 | | cvar_lookup_at(VALUE klass, ID id, st_data_t *v) |
3831 | 0 | { |
3832 | 0 | if (RB_TYPE_P(klass, T_ICLASS)) { |
3833 | 0 | if (FL_TEST_RAW(klass, RICLASS_IS_ORIGIN)) { |
3834 | 0 | return 0; |
3835 | 0 | } |
3836 | 0 | else { |
3837 | | // check the original module |
3838 | 0 | klass = RBASIC(klass)->klass; |
3839 | 0 | } |
3840 | 0 | } |
3841 | | |
3842 | 0 | VALUE n = rb_ivar_lookup(klass, id, Qundef); |
3843 | 0 | if (UNDEF_P(n)) return 0; |
3844 | | |
3845 | 0 | if (v) *v = n; |
3846 | 0 | return 1; |
3847 | 0 | } |
3848 | | |
3849 | | static VALUE |
3850 | | cvar_front_klass(VALUE klass) |
3851 | 0 | { |
3852 | 0 | if (FL_TEST(klass, FL_SINGLETON)) { |
3853 | 0 | VALUE obj = RCLASS_ATTACHED_OBJECT(klass); |
3854 | 0 | if (rb_namespace_p(obj)) { |
3855 | 0 | return obj; |
3856 | 0 | } |
3857 | 0 | } |
3858 | 0 | return RCLASS_SUPER(klass); |
3859 | 0 | } |
3860 | | |
3861 | | static void |
3862 | | cvar_overtaken(VALUE front, VALUE target, ID id) |
3863 | 0 | { |
3864 | 0 | if (front && target != front) { |
3865 | 0 | if (original_module(front) != original_module(target)) { |
3866 | 0 | rb_raise(rb_eRuntimeError, |
3867 | 0 | "class variable % "PRIsVALUE" of %"PRIsVALUE" is overtaken by %"PRIsVALUE"", |
3868 | 0 | ID2SYM(id), rb_class_name(original_module(front)), |
3869 | 0 | rb_class_name(original_module(target))); |
3870 | 0 | } |
3871 | 0 | if (BUILTIN_TYPE(front) == T_CLASS) { |
3872 | 0 | rb_ivar_delete(front, id, Qundef); |
3873 | 0 | } |
3874 | 0 | } |
3875 | 0 | } |
3876 | | |
3877 | | #define CVAR_FOREACH_ANCESTORS(klass, v, r) \ |
3878 | 0 | for (klass = cvar_front_klass(klass); klass; klass = RCLASS_SUPER(klass)) { \ |
3879 | 0 | if (cvar_lookup_at(klass, id, (v))) { \ |
3880 | 0 | r; \ |
3881 | 0 | } \ |
3882 | 0 | } |
3883 | | |
3884 | 0 | #define CVAR_LOOKUP(v,r) do {\ |
3885 | 0 | CVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(); \ |
3886 | 0 | if (cvar_lookup_at(klass, id, (v))) {r;}\ |
3887 | 0 | CVAR_FOREACH_ANCESTORS(klass, v, r);\ |
3888 | 0 | } while(0) |
3889 | | |
3890 | | static VALUE |
3891 | | find_cvar(VALUE klass, VALUE * front, VALUE * target, ID id) |
3892 | 0 | { |
3893 | 0 | VALUE v = Qundef; |
3894 | 0 | CVAR_LOOKUP(&v, { |
3895 | 0 | if (!*front) { |
3896 | 0 | *front = klass; |
3897 | 0 | } |
3898 | 0 | *target = klass; |
3899 | 0 | }); |
3900 | | |
3901 | 0 | return v; |
3902 | 0 | } |
3903 | | |
3904 | | static void |
3905 | | check_for_cvar_table(VALUE subclass, VALUE key) |
3906 | 0 | { |
3907 | | // Must not check ivar on ICLASS |
3908 | 0 | if (!RB_TYPE_P(subclass, T_ICLASS) && RTEST(rb_ivar_defined(subclass, key))) { |
3909 | 0 | RB_DEBUG_COUNTER_INC(cvar_class_invalidate); |
3910 | 0 | ruby_vm_global_cvar_state++; |
3911 | 0 | return; |
3912 | 0 | } |
3913 | | |
3914 | 0 | rb_class_foreach_subclass(subclass, check_for_cvar_table, key); |
3915 | 0 | } |
3916 | | |
3917 | | void |
3918 | | rb_cvar_set(VALUE klass, ID id, VALUE val) |
3919 | 0 | { |
3920 | 0 | VALUE tmp, front = 0, target = 0; |
3921 | |
|
3922 | 0 | tmp = klass; |
3923 | 0 | CVAR_LOOKUP(0, {if (!front) front = klass; target = klass;}); |
3924 | 0 | if (target) { |
3925 | 0 | cvar_overtaken(front, target, id); |
3926 | 0 | } |
3927 | 0 | else { |
3928 | 0 | target = tmp; |
3929 | 0 | } |
3930 | |
|
3931 | 0 | if (RB_TYPE_P(target, T_ICLASS)) { |
3932 | 0 | target = RBASIC(target)->klass; |
3933 | 0 | } |
3934 | 0 | check_before_mod_set(target, id, val, "class variable"); |
3935 | |
|
3936 | 0 | int result = rb_class_ivar_set(target, id, val); |
3937 | |
|
3938 | 0 | struct rb_id_table *rb_cvc_tbl = RCLASS_CVC_TBL(target); |
3939 | |
|
3940 | 0 | if (!rb_cvc_tbl) { |
3941 | 0 | rb_cvc_tbl = RCLASS_CVC_TBL(target) = rb_id_table_create(2); |
3942 | 0 | } |
3943 | |
|
3944 | 0 | struct rb_cvar_class_tbl_entry *ent; |
3945 | 0 | VALUE ent_data; |
3946 | |
|
3947 | 0 | if (!rb_id_table_lookup(rb_cvc_tbl, id, &ent_data)) { |
3948 | 0 | ent = ALLOC(struct rb_cvar_class_tbl_entry); |
3949 | 0 | ent->class_value = target; |
3950 | 0 | ent->global_cvar_state = GET_GLOBAL_CVAR_STATE(); |
3951 | 0 | ent->cref = 0; |
3952 | 0 | rb_id_table_insert(rb_cvc_tbl, id, (VALUE)ent); |
3953 | 0 | RB_DEBUG_COUNTER_INC(cvar_inline_miss); |
3954 | 0 | } |
3955 | 0 | else { |
3956 | 0 | ent = (void *)ent_data; |
3957 | 0 | ent->global_cvar_state = GET_GLOBAL_CVAR_STATE(); |
3958 | 0 | } |
3959 | | |
3960 | | // Break the cvar cache if this is a new class variable |
3961 | | // and target is a module or a subclass with the same |
3962 | | // cvar in this lookup. |
3963 | 0 | if (result == 0) { |
3964 | 0 | if (RB_TYPE_P(target, T_CLASS)) { |
3965 | 0 | if (RCLASS_SUBCLASSES(target)) { |
3966 | 0 | rb_class_foreach_subclass(target, check_for_cvar_table, id); |
3967 | 0 | } |
3968 | 0 | } |
3969 | 0 | } |
3970 | 0 | } |
3971 | | |
3972 | | VALUE |
3973 | | rb_cvar_find(VALUE klass, ID id, VALUE *front) |
3974 | 0 | { |
3975 | 0 | VALUE target = 0; |
3976 | 0 | VALUE value; |
3977 | |
|
3978 | 0 | value = find_cvar(klass, front, &target, id); |
3979 | 0 | if (!target) { |
3980 | 0 | rb_name_err_raise("uninitialized class variable %1$s in %2$s", |
3981 | 0 | klass, ID2SYM(id)); |
3982 | 0 | } |
3983 | 0 | cvar_overtaken(*front, target, id); |
3984 | 0 | return (VALUE)value; |
3985 | 0 | } |
3986 | | |
3987 | | VALUE |
3988 | | rb_cvar_get(VALUE klass, ID id) |
3989 | 0 | { |
3990 | 0 | VALUE front = 0; |
3991 | 0 | return rb_cvar_find(klass, id, &front); |
3992 | 0 | } |
3993 | | |
3994 | | VALUE |
3995 | | rb_cvar_defined(VALUE klass, ID id) |
3996 | 0 | { |
3997 | 0 | if (!klass) return Qfalse; |
3998 | 0 | CVAR_LOOKUP(0,return Qtrue); |
3999 | 0 | return Qfalse; |
4000 | 0 | } |
4001 | | |
4002 | | static ID |
4003 | | cv_intern(VALUE klass, const char *name) |
4004 | 0 | { |
4005 | 0 | ID id = rb_intern(name); |
4006 | 0 | if (!rb_is_class_id(id)) { |
4007 | 0 | rb_name_err_raise("wrong class variable name %1$s", |
4008 | 0 | klass, rb_str_new_cstr(name)); |
4009 | 0 | } |
4010 | 0 | return id; |
4011 | 0 | } |
4012 | | |
4013 | | void |
4014 | | rb_cv_set(VALUE klass, const char *name, VALUE val) |
4015 | 0 | { |
4016 | 0 | ID id = cv_intern(klass, name); |
4017 | 0 | rb_cvar_set(klass, id, val); |
4018 | 0 | } |
4019 | | |
4020 | | VALUE |
4021 | | rb_cv_get(VALUE klass, const char *name) |
4022 | 0 | { |
4023 | 0 | ID id = cv_intern(klass, name); |
4024 | 0 | return rb_cvar_get(klass, id); |
4025 | 0 | } |
4026 | | |
4027 | | void |
4028 | | rb_define_class_variable(VALUE klass, const char *name, VALUE val) |
4029 | 0 | { |
4030 | 0 | rb_cv_set(klass, name, val); |
4031 | 0 | } |
4032 | | |
4033 | | static int |
4034 | | cv_i(ID key, VALUE v, st_data_t a) |
4035 | 0 | { |
4036 | 0 | st_table *tbl = (st_table *)a; |
4037 | |
|
4038 | 0 | if (rb_is_class_id(key)) { |
4039 | 0 | st_update(tbl, (st_data_t)key, cv_i_update, 0); |
4040 | 0 | } |
4041 | 0 | return ST_CONTINUE; |
4042 | 0 | } |
4043 | | |
4044 | | static void* |
4045 | | mod_cvar_at(VALUE mod, void *data) |
4046 | 0 | { |
4047 | 0 | st_table *tbl = data; |
4048 | 0 | if (!tbl) { |
4049 | 0 | tbl = st_init_numtable(); |
4050 | 0 | } |
4051 | 0 | mod = original_module(mod); |
4052 | |
|
4053 | 0 | rb_ivar_foreach(mod, cv_i, (st_data_t)tbl); |
4054 | 0 | return tbl; |
4055 | 0 | } |
4056 | | |
4057 | | static void* |
4058 | | mod_cvar_of(VALUE mod, void *data) |
4059 | 0 | { |
4060 | 0 | VALUE tmp = mod; |
4061 | 0 | if (FL_TEST(mod, FL_SINGLETON)) { |
4062 | 0 | if (rb_namespace_p(RCLASS_ATTACHED_OBJECT(mod))) { |
4063 | 0 | data = mod_cvar_at(tmp, data); |
4064 | 0 | tmp = cvar_front_klass(tmp); |
4065 | 0 | } |
4066 | 0 | } |
4067 | 0 | for (;;) { |
4068 | 0 | data = mod_cvar_at(tmp, data); |
4069 | 0 | tmp = RCLASS_SUPER(tmp); |
4070 | 0 | if (!tmp) break; |
4071 | 0 | } |
4072 | 0 | return data; |
4073 | 0 | } |
4074 | | |
4075 | | static int |
4076 | | cv_list_i(st_data_t key, st_data_t value, VALUE ary) |
4077 | 0 | { |
4078 | 0 | ID sym = (ID)key; |
4079 | 0 | rb_ary_push(ary, ID2SYM(sym)); |
4080 | 0 | return ST_CONTINUE; |
4081 | 0 | } |
4082 | | |
4083 | | static VALUE |
4084 | | cvar_list(void *data) |
4085 | 0 | { |
4086 | 0 | st_table *tbl = data; |
4087 | 0 | VALUE ary; |
4088 | |
|
4089 | 0 | if (!tbl) return rb_ary_new2(0); |
4090 | 0 | ary = rb_ary_new2(tbl->num_entries); |
4091 | 0 | st_foreach_safe(tbl, cv_list_i, ary); |
4092 | 0 | st_free_table(tbl); |
4093 | |
|
4094 | 0 | return ary; |
4095 | 0 | } |
4096 | | |
4097 | | /* |
4098 | | * call-seq: |
4099 | | * mod.class_variables(inherit=true) -> array |
4100 | | * |
4101 | | * Returns an array of the names of class variables in <i>mod</i>. |
4102 | | * This includes the names of class variables in any included |
4103 | | * modules, unless the <i>inherit</i> parameter is set to |
4104 | | * <code>false</code>. |
4105 | | * |
4106 | | * class One |
4107 | | * @@var1 = 1 |
4108 | | * end |
4109 | | * class Two < One |
4110 | | * @@var2 = 2 |
4111 | | * end |
4112 | | * One.class_variables #=> [:@@var1] |
4113 | | * Two.class_variables #=> [:@@var2, :@@var1] |
4114 | | * Two.class_variables(false) #=> [:@@var2] |
4115 | | */ |
4116 | | |
4117 | | VALUE |
4118 | | rb_mod_class_variables(int argc, const VALUE *argv, VALUE mod) |
4119 | 0 | { |
4120 | 0 | bool inherit = true; |
4121 | 0 | st_table *tbl; |
4122 | |
|
4123 | 0 | if (rb_check_arity(argc, 0, 1)) inherit = RTEST(argv[0]); |
4124 | 0 | if (inherit) { |
4125 | 0 | tbl = mod_cvar_of(mod, 0); |
4126 | 0 | } |
4127 | 0 | else { |
4128 | 0 | tbl = mod_cvar_at(mod, 0); |
4129 | 0 | } |
4130 | 0 | return cvar_list(tbl); |
4131 | 0 | } |
4132 | | |
4133 | | /* |
4134 | | * call-seq: |
4135 | | * remove_class_variable(sym) -> obj |
4136 | | * |
4137 | | * Removes the named class variable from the receiver, returning that |
4138 | | * variable's value. |
4139 | | * |
4140 | | * class Example |
4141 | | * @@var = 99 |
4142 | | * puts remove_class_variable(:@@var) |
4143 | | * p(defined? @@var) |
4144 | | * end |
4145 | | * |
4146 | | * <em>produces:</em> |
4147 | | * |
4148 | | * 99 |
4149 | | * nil |
4150 | | */ |
4151 | | |
4152 | | VALUE |
4153 | | rb_mod_remove_cvar(VALUE mod, VALUE name) |
4154 | 0 | { |
4155 | 0 | const ID id = id_for_var_message(mod, name, class, "wrong class variable name %1$s"); |
4156 | 0 | st_data_t val; |
4157 | |
|
4158 | 0 | if (!id) { |
4159 | 0 | goto not_defined; |
4160 | 0 | } |
4161 | 0 | rb_check_frozen(mod); |
4162 | 0 | val = rb_ivar_delete(mod, id, Qundef); |
4163 | 0 | if (!UNDEF_P(val)) { |
4164 | 0 | return (VALUE)val; |
4165 | 0 | } |
4166 | 0 | if (rb_cvar_defined(mod, id)) { |
4167 | 0 | rb_name_err_raise("cannot remove %1$s for %2$s", mod, ID2SYM(id)); |
4168 | 0 | } |
4169 | 0 | not_defined: |
4170 | 0 | rb_name_err_raise("class variable %1$s not defined for %2$s", |
4171 | 0 | mod, name); |
4172 | 0 | UNREACHABLE_RETURN(Qundef); |
4173 | 0 | } |
4174 | | |
4175 | | VALUE |
4176 | | rb_iv_get(VALUE obj, const char *name) |
4177 | 0 | { |
4178 | 0 | ID id = rb_check_id_cstr(name, strlen(name), rb_usascii_encoding()); |
4179 | |
|
4180 | 0 | if (!id) { |
4181 | 0 | return Qnil; |
4182 | 0 | } |
4183 | 0 | return rb_ivar_get(obj, id); |
4184 | 0 | } |
4185 | | |
4186 | | VALUE |
4187 | | rb_iv_set(VALUE obj, const char *name, VALUE val) |
4188 | 0 | { |
4189 | 0 | ID id = rb_intern(name); |
4190 | |
|
4191 | 0 | return rb_ivar_set(obj, id, val); |
4192 | 0 | } |
4193 | | |
4194 | | static VALUE * |
4195 | | class_ivar_set_shape_ivptr(VALUE obj, void *_data) |
4196 | 0 | { |
4197 | 0 | RUBY_ASSERT(!rb_shape_obj_too_complex(obj)); |
4198 | |
|
4199 | 0 | return RCLASS_IVPTR(obj); |
4200 | 0 | } |
4201 | | |
4202 | | static void |
4203 | | class_ivar_set_shape_resize_ivptr(VALUE obj, attr_index_t _old_capa, attr_index_t new_capa, void *_data) |
4204 | 0 | { |
4205 | 0 | REALLOC_N(RCLASS_IVPTR(obj), VALUE, new_capa); |
4206 | 0 | } |
4207 | | |
4208 | | static void |
4209 | | class_ivar_set_set_shape(VALUE obj, rb_shape_t *shape, void *_data) |
4210 | 0 | { |
4211 | 0 | rb_shape_set_shape(obj, shape); |
4212 | 0 | } |
4213 | | |
4214 | | static void |
4215 | | class_ivar_set_transition_too_complex(VALUE obj, void *_data) |
4216 | 0 | { |
4217 | 0 | rb_evict_ivars_to_hash(obj); |
4218 | 0 | } |
4219 | | |
4220 | | static st_table * |
4221 | | class_ivar_set_too_complex_table(VALUE obj, void *_data) |
4222 | 0 | { |
4223 | 0 | RUBY_ASSERT(rb_shape_obj_too_complex(obj)); |
4224 | |
|
4225 | 0 | return RCLASS_IV_HASH(obj); |
4226 | 0 | } |
4227 | | |
4228 | | int |
4229 | | rb_class_ivar_set(VALUE obj, ID id, VALUE val) |
4230 | 0 | { |
4231 | 0 | RUBY_ASSERT(RB_TYPE_P(obj, T_CLASS) || RB_TYPE_P(obj, T_MODULE)); |
4232 | 0 | bool existing = false; |
4233 | 0 | rb_check_frozen(obj); |
4234 | |
|
4235 | 0 | RB_VM_LOCK_ENTER(); |
4236 | 0 | { |
4237 | 0 | existing = general_ivar_set(obj, id, val, NULL, |
4238 | 0 | class_ivar_set_shape_ivptr, |
4239 | 0 | class_ivar_set_shape_resize_ivptr, |
4240 | 0 | class_ivar_set_set_shape, |
4241 | 0 | class_ivar_set_transition_too_complex, |
4242 | 0 | class_ivar_set_too_complex_table).existing; |
4243 | 0 | } |
4244 | 0 | RB_VM_LOCK_LEAVE(); |
4245 | |
|
4246 | 0 | return existing; |
4247 | 0 | } |
4248 | | |
4249 | | static int |
4250 | | tbl_copy_i(ID key, VALUE val, st_data_t dest) |
4251 | 0 | { |
4252 | 0 | rb_class_ivar_set((VALUE)dest, key, val); |
4253 | |
|
4254 | 0 | return ST_CONTINUE; |
4255 | 0 | } |
4256 | | |
4257 | | void |
4258 | | rb_iv_tbl_copy(VALUE dst, VALUE src) |
4259 | 0 | { |
4260 | 0 | RUBY_ASSERT(rb_type(dst) == rb_type(src)); |
4261 | 0 | RUBY_ASSERT(RB_TYPE_P(dst, T_CLASS) || RB_TYPE_P(dst, T_MODULE)); |
4262 | |
|
4263 | 0 | RUBY_ASSERT(rb_shape_get_shape(dst)->type == SHAPE_ROOT); |
4264 | 0 | RUBY_ASSERT(!RCLASS_IVPTR(dst)); |
4265 | |
|
4266 | 0 | rb_ivar_foreach(src, tbl_copy_i, dst); |
4267 | 0 | } |
4268 | | |
4269 | | rb_const_entry_t * |
4270 | | rb_const_lookup(VALUE klass, ID id) |
4271 | 0 | { |
4272 | 0 | struct rb_id_table *tbl = RCLASS_CONST_TBL(klass); |
4273 | |
|
4274 | 0 | if (tbl) { |
4275 | 0 | VALUE val; |
4276 | 0 | bool r; |
4277 | 0 | RB_VM_LOCK_ENTER(); |
4278 | 0 | { |
4279 | 0 | r = rb_id_table_lookup(tbl, id, &val); |
4280 | 0 | } |
4281 | 0 | RB_VM_LOCK_LEAVE(); |
4282 | |
|
4283 | 0 | if (r) return (rb_const_entry_t *)val; |
4284 | 0 | } |
4285 | 0 | return NULL; |
4286 | 0 | } |