Ruby  2.5.0dev(2017-10-22revision60238)
variable.c
Go to the documentation of this file.
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 "internal.h"
15 #include "ruby/st.h"
16 #include "ruby/util.h"
17 #include "id_table.h"
18 #include "constant.h"
19 #include "id.h"
20 #include "ccan/list/list.h"
21 #include "id_table.h"
22 #include "debug_counter.h"
23 
25 static ID autoload, classpath, tmp_classpath, classid;
26 
27 static void check_before_mod_set(VALUE, ID, VALUE, const char *);
28 static void setup_const_entry(rb_const_entry_t *, VALUE, VALUE, rb_const_flag_t);
29 static VALUE rb_const_search(VALUE klass, ID id, int exclude, int recurse, int visibility);
30 static st_table *generic_iv_tbl;
31 static st_table *generic_iv_tbl_compat;
32 
33 /* per-object */
34 struct gen_ivtbl {
36  VALUE ivptr[1]; /* flexible array */
37 };
38 
39 struct ivar_update {
40  union {
42  struct gen_ivtbl *ivtbl;
43  } u;
46 };
47 
48 void
50 {
51  rb_global_tbl = rb_id_table_create(0);
52  generic_iv_tbl = st_init_numtable();
53  autoload = rb_intern_const("__autoload__");
54  /* __classpath__: fully qualified class path */
55  classpath = rb_intern_const("__classpath__");
56  /* __tmp_classpath__: temporary class path which contains anonymous names */
57  tmp_classpath = rb_intern_const("__tmp_classpath__");
58  /* __classid__: name given to class/module under an anonymous namespace */
59  classid = rb_intern_const("__classid__");
60 }
61 
62 struct fc_result {
67  struct fc_result *prev;
68 };
69 
70 static VALUE
71 fc_path(struct fc_result *fc, ID name)
72 {
73  VALUE path, tmp;
74 
75  path = rb_id2str(name);
76  while (fc) {
77  st_data_t n;
78  if (fc->track == rb_cObject) break;
79  if (RCLASS_IV_TBL(fc->track) &&
80  st_lookup(RCLASS_IV_TBL(fc->track), (st_data_t)classpath, &n)) {
81  tmp = rb_str_dup((VALUE)n);
82  rb_str_cat2(tmp, "::");
83  rb_str_append(tmp, path);
84  path = tmp;
85  break;
86  }
87  tmp = rb_str_dup(rb_id2str(fc->name));
88  rb_str_cat2(tmp, "::");
89  rb_str_append(tmp, path);
90  path = tmp;
91  fc = fc->prev;
92  }
93  OBJ_FREEZE(path);
94  return path;
95 }
96 
98 fc_i(ID key, VALUE v, void *a)
99 {
101  struct fc_result *res = a;
102  VALUE value = ce->value;
103  if (!rb_is_const_id(key)) return ID_TABLE_CONTINUE;
104 
105  if (value == res->klass && (!res->preferred || key == res->preferred)) {
106  res->path = fc_path(res, key);
107  return ID_TABLE_STOP;
108  }
109  if (RB_TYPE_P(value, T_MODULE) || RB_TYPE_P(value, T_CLASS)) {
110  if (!RCLASS_CONST_TBL(value)) return ID_TABLE_CONTINUE;
111  else {
112  struct fc_result arg;
113  struct fc_result *list;
114 
115  list = res;
116  while (list) {
117  if (list->track == value) return ID_TABLE_CONTINUE;
118  list = list->prev;
119  }
120 
121  arg.name = key;
122  arg.preferred = res->preferred;
123  arg.path = 0;
124  arg.klass = res->klass;
125  arg.track = value;
126  arg.prev = res;
127  rb_id_table_foreach(RCLASS_CONST_TBL(value), fc_i, &arg);
128  if (arg.path) {
129  res->path = arg.path;
130  return ID_TABLE_STOP;
131  }
132  }
133  }
134  return ID_TABLE_CONTINUE;
135 }
136 
144 static VALUE
145 find_class_path(VALUE klass, ID preferred)
146 {
147  struct fc_result arg;
148 
149  arg.preferred = preferred;
150  arg.name = 0;
151  arg.path = 0;
152  arg.klass = klass;
153  arg.track = rb_cObject;
154  arg.prev = 0;
157  }
158  if (arg.path) {
159  st_data_t tmp = tmp_classpath;
160  if (!RCLASS_IV_TBL(klass)) {
162  }
163  rb_class_ivar_set(klass, classpath, arg.path);
164 
165  st_delete(RCLASS_IV_TBL(klass), &tmp, 0);
166  return arg.path;
167  }
168  return Qnil;
169 }
170 
178 static VALUE
179 classname(VALUE klass, int *permanent)
180 {
181  VALUE path = Qnil;
182  st_data_t n;
183 
184  if (!klass) klass = rb_cObject;
185  *permanent = 1;
186  if (RCLASS_IV_TBL(klass)) {
187  if (!st_lookup(RCLASS_IV_TBL(klass), (st_data_t)classpath, &n)) {
188  ID cid = 0;
189  if (st_lookup(RCLASS_IV_TBL(klass), (st_data_t)classid, &n)) {
190  VALUE cname = (VALUE)n;
191  cid = rb_check_id(&cname);
192  if (cid) path = find_class_path(klass, cid);
193  }
194  if (NIL_P(path)) {
195  path = find_class_path(klass, (ID)0);
196  }
197  if (NIL_P(path)) {
198  if (!cid) {
199  return Qnil;
200  }
201  if (!st_lookup(RCLASS_IV_TBL(klass), (st_data_t)tmp_classpath, &n)) {
202  path = rb_id2str(cid);
203  return path;
204  }
205  *permanent = 0;
206  path = (VALUE)n;
207  return path;
208  }
209  }
210  else {
211  path = (VALUE)n;
212  }
213  if (!RB_TYPE_P(path, T_STRING)) {
214  rb_bug("class path is not set properly");
215  }
216  return path;
217  }
218  return find_class_path(klass, (ID)0);
219 }
220 
221 /*
222  * call-seq:
223  * mod.name -> string
224  *
225  * Returns the name of the module <i>mod</i>. Returns nil for anonymous modules.
226  */
227 
228 VALUE
230 {
231  int permanent;
232  VALUE path = classname(mod, &permanent);
233 
234  if (!NIL_P(path)) return rb_str_dup(path);
235  return path;
236 }
237 
238 static VALUE
239 make_temporary_path(VALUE obj, VALUE klass)
240 {
241  VALUE path;
242  switch (klass) {
243  case Qnil:
244  path = rb_sprintf("#<Class:%p>", (void*)obj);
245  break;
246  case Qfalse:
247  path = rb_sprintf("#<Module:%p>", (void*)obj);
248  break;
249  default:
250  path = rb_sprintf("#<%"PRIsVALUE":%p>", klass, (void*)obj);
251  break;
252  }
253  OBJ_FREEZE(path);
254  return path;
255 }
256 
258 
259 static VALUE
260 rb_tmp_class_path(VALUE klass, int *permanent, path_cache_func cache_path)
261 {
262  VALUE path = classname(klass, permanent);
263  st_data_t n = (st_data_t)path;
264 
265  if (!NIL_P(path)) {
266  return path;
267  }
268  if (RCLASS_IV_TBL(klass) && st_lookup(RCLASS_IV_TBL(klass),
269  (st_data_t)tmp_classpath, &n)) {
270  *permanent = 0;
271  return (VALUE)n;
272  }
273  else {
274  if (RB_TYPE_P(klass, T_MODULE)) {
275  if (rb_obj_class(klass) == rb_cModule) {
276  path = Qfalse;
277  }
278  else {
279  int perm;
280  path = rb_tmp_class_path(RBASIC(klass)->klass, &perm, cache_path);
281  }
282  }
283  *permanent = 0;
284  return cache_path(klass, path);
285  }
286 }
287 
288 static VALUE
289 ivar_cache(VALUE obj, VALUE name)
290 {
291  return rb_ivar_set(obj, tmp_classpath, make_temporary_path(obj, name));
292 }
293 
294 VALUE
296 {
297  int permanent;
298  VALUE path = rb_tmp_class_path(klass, &permanent, ivar_cache);
299  if (!NIL_P(path)) path = rb_str_dup(path);
300  return path;
301 }
302 
303 static VALUE
304 null_cache(VALUE obj, VALUE name)
305 {
306  return make_temporary_path(obj, name);
307 }
308 
309 VALUE
311 {
312  int permanent;
313  VALUE path = rb_tmp_class_path(klass, &permanent, null_cache);
314  if (!NIL_P(path)) path = rb_str_dup(path);
315  return path;
316 }
317 
318 VALUE
320 {
321  st_table *ivtbl = RCLASS_IV_TBL(klass);
322  st_data_t n;
323 
324  if (!ivtbl) return Qnil;
325  if (st_lookup(ivtbl, (st_data_t)classpath, &n)) return (VALUE)n;
326  if (st_lookup(ivtbl, (st_data_t)tmp_classpath, &n)) return (VALUE)n;
327  return Qnil;
328 }
329 
330 static VALUE
331 never_cache(VALUE obj, VALUE name)
332 {
333  return name;
334 }
335 
336 VALUE
338 {
339  int permanent;
340  return rb_tmp_class_path(klass, &permanent, never_cache);
341 }
342 
343 void
345 {
346  VALUE str;
347  ID pathid = classpath;
348 
349  if (under == rb_cObject) {
350  str = rb_str_new_frozen(name);
351  }
352  else {
353  int permanent;
354  str = rb_str_dup(rb_tmp_class_path(under, &permanent, ivar_cache));
355  rb_str_cat2(str, "::");
356  rb_str_append(str, name);
357  OBJ_FREEZE(str);
358  if (!permanent) {
359  pathid = tmp_classpath;
360  rb_ivar_set(klass, classid, rb_str_intern(name));
361  }
362  }
363  rb_ivar_set(klass, pathid, str);
364 }
365 
366 void
367 rb_set_class_path(VALUE klass, VALUE under, const char *name)
368 {
369  VALUE str;
370  ID pathid = classpath;
371 
372  if (under == rb_cObject) {
373  str = rb_str_new2(name);
374  }
375  else {
376  int permanent;
377  str = rb_str_dup(rb_tmp_class_path(under, &permanent, ivar_cache));
378  rb_str_cat2(str, "::");
379  rb_str_cat2(str, name);
380  if (!permanent) {
381  pathid = tmp_classpath;
382  rb_ivar_set(klass, classid, rb_str_intern(rb_str_new_cstr(name)));
383  }
384  }
385  OBJ_FREEZE(str);
386  rb_ivar_set(klass, pathid, str);
387 }
388 
389 VALUE
391 {
392  rb_encoding *enc = rb_enc_get(pathname);
393  const char *pbeg, *pend, *p, *path = RSTRING_PTR(pathname);
394  ID id;
395  VALUE c = rb_cObject;
396 
397  if (!rb_enc_asciicompat(enc)) {
398  rb_raise(rb_eArgError, "invalid class path encoding (non ASCII)");
399  }
400  pbeg = p = path;
401  pend = path + RSTRING_LEN(pathname);
402  if (path == pend || path[0] == '#') {
403  rb_raise(rb_eArgError, "can't retrieve anonymous class %"PRIsVALUE,
404  QUOTE(pathname));
405  }
406  while (p < pend) {
407  while (p < pend && *p != ':') p++;
408  id = rb_check_id_cstr(pbeg, p-pbeg, enc);
409  if (p < pend && p[0] == ':') {
410  if ((size_t)(pend - p) < 2 || p[1] != ':') goto undefined_class;
411  p += 2;
412  pbeg = p;
413  }
414  if (!id) {
415  undefined_class:
416  rb_raise(rb_eArgError, "undefined class/module % "PRIsVALUE,
417  rb_str_subseq(pathname, 0, p-path));
418  }
419  c = rb_const_search(c, id, TRUE, FALSE, FALSE);
420  if (c == Qundef) goto undefined_class;
421  if (!RB_TYPE_P(c, T_MODULE) && !RB_TYPE_P(c, T_CLASS)) {
422  rb_raise(rb_eTypeError, "%"PRIsVALUE" does not refer to class/module",
423  pathname);
424  }
425  }
426  RB_GC_GUARD(pathname);
427 
428  return c;
429 }
430 
431 VALUE
432 rb_path2class(const char *path)
433 {
434  return rb_path_to_class(rb_str_new_cstr(path));
435 }
436 
437 void
439 {
440  rb_ivar_set(klass, classid, ID2SYM(id));
441 }
442 
443 VALUE
445 {
446  return rb_class_path(rb_class_real(klass));
447 }
448 
449 const char *
451 {
452  int permanent;
453  VALUE path = rb_tmp_class_path(rb_class_real(klass), &permanent, ivar_cache);
454  if (NIL_P(path)) return NULL;
455  return RSTRING_PTR(path);
456 }
457 
458 const char *
460 {
461  return rb_class2name(CLASS_OF(obj));
462 }
463 
464 struct trace_var {
465  int removed;
466  void (*func)(VALUE arg, VALUE val);
468  struct trace_var *next;
469 };
470 
472  int counter;
474  void *data;
478  struct trace_var *trace;
479 };
480 
481 struct rb_global_entry*
483 {
484  struct rb_global_entry *entry;
485  VALUE data;
486 
487  if (!rb_id_table_lookup(rb_global_tbl, id, &data)) {
488  struct rb_global_variable *var;
489  entry = ALLOC(struct rb_global_entry);
490  var = ALLOC(struct rb_global_variable);
491  entry->id = id;
492  entry->var = var;
493  var->counter = 1;
494  var->data = 0;
498 
499  var->block_trace = 0;
500  var->trace = 0;
501  rb_id_table_insert(rb_global_tbl, id, (VALUE)entry);
502  }
503  else {
504  entry = (struct rb_global_entry *)data;
505  }
506  return entry;
507 }
508 
509 VALUE
511 {
512  rb_warning("global variable `%"PRIsVALUE"' not initialized", QUOTE_ID(id));
513 
514  return Qnil;
515 }
516 
517 void
519 {
520  var->getter = rb_gvar_val_getter;
521  var->setter = rb_gvar_val_setter;
522  var->marker = rb_gvar_val_marker;
523 
524  var->data = (void*)val;
525 }
526 
527 void
529 {
530 }
531 
532 VALUE
533 rb_gvar_val_getter(ID id, void *data, struct rb_global_variable *var)
534 {
535  return (VALUE)data;
536 }
537 
538 void
540 {
541  var->data = (void*)val;
542 }
543 
544 void
546 {
547  VALUE data = (VALUE)var;
548  if (data) rb_gc_mark_maybe(data);
549 }
550 
551 VALUE
552 rb_gvar_var_getter(ID id, void *data, struct rb_global_variable *gvar)
553 {
554  VALUE *var = data;
555  if (!var) return Qnil;
556  return *var;
557 }
558 
559 void
560 rb_gvar_var_setter(VALUE val, ID id, void *data, struct rb_global_variable *g)
561 {
562  *(VALUE *)data = val;
563 }
564 
565 void
567 {
568  if (var) rb_gc_mark_maybe(*var);
569 }
570 
571 void
573 {
574  rb_name_error(id, "%"PRIsVALUE" is a read-only variable", QUOTE_ID(id));
575 }
576 
577 static enum rb_id_table_iterator_result
578 mark_global_entry(VALUE v, void *ignored)
579 {
580  struct rb_global_entry *entry = (struct rb_global_entry *)v;
581  struct trace_var *trace;
582  struct rb_global_variable *var = entry->var;
583 
584  (*var->marker)(var->data);
585  trace = var->trace;
586  while (trace) {
587  if (trace->data) rb_gc_mark_maybe(trace->data);
588  trace = trace->next;
589  }
590  return ID_TABLE_CONTINUE;
591 }
592 
593 void
595 {
596  if (rb_global_tbl)
597  rb_id_table_foreach_values(rb_global_tbl, mark_global_entry, 0);
598 }
599 
600 static ID
601 global_id(const char *name)
602 {
603  ID id;
604 
605  if (name[0] == '$') id = rb_intern(name);
606  else {
607  size_t len = strlen(name);
608  char *buf = ALLOCA_N(char, len+1);
609  buf[0] = '$';
610  memcpy(buf+1, name, len);
611  id = rb_intern2(buf, len+1);
612  }
613  return id;
614 }
615 
616 void
618  const char *name,
619  VALUE *var,
620  VALUE (*getter)(ANYARGS),
621  void (*setter)(ANYARGS))
622 {
623  volatile VALUE tmp = var ? *var : Qnil;
624  ID id = global_id(name);
625  struct rb_global_variable *gvar = rb_global_entry(id)->var;
626 
627  gvar->data = (void*)var;
630  gvar->marker = rb_gvar_var_marker;
631 
632  RB_GC_GUARD(tmp);
633 }
634 
635 void
636 rb_define_variable(const char *name, VALUE *var)
637 {
638  rb_define_hooked_variable(name, var, 0, 0);
639 }
640 
641 void
642 rb_define_readonly_variable(const char *name, const VALUE *var)
643 {
645 }
646 
647 void
649  const char *name,
650  VALUE (*getter)(ANYARGS),
651  void (*setter)(ANYARGS))
652 {
656 }
657 
658 static void
659 rb_trace_eval(VALUE cmd, VALUE val)
660 {
661  rb_eval_cmd(cmd, rb_ary_new3(1, val), 0);
662 }
663 
664 /*
665  * call-seq:
666  * trace_var(symbol, cmd ) -> nil
667  * trace_var(symbol) {|val| block } -> nil
668  *
669  * Controls tracing of assignments to global variables. The parameter
670  * +symbol+ identifies the variable (as either a string name or a
671  * symbol identifier). _cmd_ (which may be a string or a
672  * +Proc+ object) or block is executed whenever the variable
673  * is assigned. The block or +Proc+ object receives the
674  * variable's new value as a parameter. Also see
675  * <code>Kernel::untrace_var</code>.
676  *
677  * trace_var :$_, proc {|v| puts "$_ is now '#{v}'" }
678  * $_ = "hello"
679  * $_ = ' there'
680  *
681  * <em>produces:</em>
682  *
683  * $_ is now 'hello'
684  * $_ is now ' there'
685  */
686 
687 VALUE
689 {
690  VALUE var, cmd;
691  struct rb_global_entry *entry;
692  struct trace_var *trace;
693 
694  if (rb_scan_args(argc, argv, "11", &var, &cmd) == 1) {
695  cmd = rb_block_proc();
696  }
697  if (NIL_P(cmd)) {
698  return rb_f_untrace_var(argc, argv);
699  }
700  entry = rb_global_entry(rb_to_id(var));
701  if (OBJ_TAINTED(cmd)) {
702  rb_raise(rb_eSecurityError, "Insecure: tainted variable trace");
703  }
704  trace = ALLOC(struct trace_var);
705  trace->next = entry->var->trace;
706  trace->func = rb_trace_eval;
707  trace->data = cmd;
708  trace->removed = 0;
709  entry->var->trace = trace;
710 
711  return Qnil;
712 }
713 
714 static void
715 remove_trace(struct rb_global_variable *var)
716 {
717  struct trace_var *trace = var->trace;
718  struct trace_var t;
719  struct trace_var *next;
720 
721  t.next = trace;
722  trace = &t;
723  while (trace->next) {
724  next = trace->next;
725  if (next->removed) {
726  trace->next = next->next;
727  xfree(next);
728  }
729  else {
730  trace = next;
731  }
732  }
733  var->trace = t.next;
734 }
735 
736 /*
737  * call-seq:
738  * untrace_var(symbol [, cmd] ) -> array or nil
739  *
740  * Removes tracing for the specified command on the given global
741  * variable and returns +nil+. If no command is specified,
742  * removes all tracing for that variable and returns an array
743  * containing the commands actually removed.
744  */
745 
746 VALUE
748 {
749  VALUE var, cmd;
750  ID id;
751  struct rb_global_entry *entry;
752  struct trace_var *trace;
753  VALUE data;
754 
755  rb_scan_args(argc, argv, "11", &var, &cmd);
756  id = rb_check_id(&var);
757  if (!id) {
758  rb_name_error_str(var, "undefined global variable %"PRIsVALUE"", QUOTE(var));
759  }
760  if (!rb_id_table_lookup(rb_global_tbl, id, &data)) {
761  rb_name_error(id, "undefined global variable %"PRIsVALUE"", QUOTE_ID(id));
762  }
763 
764  trace = (entry = (struct rb_global_entry *)data)->var->trace;
765  if (NIL_P(cmd)) {
766  VALUE ary = rb_ary_new();
767 
768  while (trace) {
769  struct trace_var *next = trace->next;
770  rb_ary_push(ary, (VALUE)trace->data);
771  trace->removed = 1;
772  trace = next;
773  }
774 
775  if (!entry->var->block_trace) remove_trace(entry->var);
776  return ary;
777  }
778  else {
779  while (trace) {
780  if (trace->data == cmd) {
781  trace->removed = 1;
782  if (!entry->var->block_trace) remove_trace(entry->var);
783  return rb_ary_new3(1, cmd);
784  }
785  trace = trace->next;
786  }
787  }
788  return Qnil;
789 }
790 
791 VALUE
793 {
794  struct rb_global_variable *var = entry->var;
795  return (*var->getter)(entry->id, var->data, var);
796 }
797 
798 struct trace_data {
799  struct trace_var *trace;
801 };
802 
803 static VALUE
804 trace_ev(struct trace_data *data)
805 {
806  struct trace_var *trace = data->trace;
807 
808  while (trace) {
809  (*trace->func)(trace->data, data->val);
810  trace = trace->next;
811  }
812 
813  return Qnil;
814 }
815 
816 static VALUE
817 trace_en(struct rb_global_variable *var)
818 {
819  var->block_trace = 0;
820  remove_trace(var);
821  return Qnil; /* not reached */
822 }
823 
824 VALUE
825 rb_gvar_set(struct rb_global_entry *entry, VALUE val)
826 {
827  struct trace_data trace;
828  struct rb_global_variable *var = entry->var;
829 
830  (*var->setter)(val, entry->id, var->data, var);
831 
832  if (var->trace && !var->block_trace) {
833  var->block_trace = 1;
834  trace.trace = var->trace;
835  trace.val = val;
836  rb_ensure(trace_ev, (VALUE)&trace, trace_en, (VALUE)var);
837  }
838  return val;
839 }
840 
841 VALUE
842 rb_gv_set(const char *name, VALUE val)
843 {
844  struct rb_global_entry *entry;
845 
846  entry = rb_global_entry(global_id(name));
847  return rb_gvar_set(entry, val);
848 }
849 
850 VALUE
851 rb_gv_get(const char *name)
852 {
853  struct rb_global_entry *entry;
854 
855  entry = rb_global_entry(global_id(name));
856  return rb_gvar_get(entry);
857 }
858 
859 VALUE
861 {
862  if (entry->var->getter == rb_gvar_undef_getter) return Qfalse;
863  return Qtrue;
864 }
865 
866 static enum rb_id_table_iterator_result
867 gvar_i(ID key, VALUE val, void *a)
868 {
869  VALUE ary = (VALUE)a;
870  rb_ary_push(ary, ID2SYM(key));
871  return ID_TABLE_CONTINUE;
872 }
873 
874 /*
875  * call-seq:
876  * global_variables -> array
877  *
878  * Returns an array of the names of global variables.
879  *
880  * global_variables.grep /std/ #=> [:$stdin, :$stdout, :$stderr]
881  */
882 
883 VALUE
885 {
886  VALUE ary = rb_ary_new();
887  VALUE sym, backref = rb_backref_get();
888 
889  rb_id_table_foreach(rb_global_tbl, gvar_i, (void *)ary);
890  if (!NIL_P(backref)) {
891  char buf[2];
892  int i, nmatch = rb_match_count(backref);
893  buf[0] = '$';
894  for (i = 1; i <= nmatch; ++i) {
895  if (!rb_match_nth_defined(i, backref)) continue;
896  if (i < 10) {
897  /* probably reused, make static ID */
898  buf[1] = (char)(i + '0');
899  sym = ID2SYM(rb_intern2(buf, 2));
900  }
901  else {
902  /* dynamic symbol */
903  sym = rb_str_intern(rb_sprintf("$%d", i));
904  }
905  rb_ary_push(ary, sym);
906  }
907  }
908  return ary;
909 }
910 
911 void
912 rb_alias_variable(ID name1, ID name2)
913 {
914  struct rb_global_entry *entry1, *entry2;
915  VALUE data1;
916 
917  entry2 = rb_global_entry(name2);
918  if (!rb_id_table_lookup(rb_global_tbl, name1, &data1)) {
919  entry1 = ALLOC(struct rb_global_entry);
920  entry1->id = name1;
921  rb_id_table_insert(rb_global_tbl, name1, (VALUE)entry1);
922  }
923  else if ((entry1 = (struct rb_global_entry *)data1)->var != entry2->var) {
924  struct rb_global_variable *var = entry1->var;
925  if (var->block_trace) {
926  rb_raise(rb_eRuntimeError, "can't alias in tracer");
927  }
928  var->counter--;
929  if (var->counter == 0) {
930  struct trace_var *trace = var->trace;
931  while (trace) {
932  struct trace_var *next = trace->next;
933  xfree(trace);
934  trace = next;
935  }
936  xfree(var);
937  }
938  }
939  else {
940  return;
941  }
942  entry2->var->counter++;
943  entry1->var = entry2->var;
944 }
945 
946 static int
947 gen_ivtbl_get(VALUE obj, struct gen_ivtbl **ivtbl)
948 {
949  st_data_t data;
950 
951  if (st_lookup(generic_iv_tbl, (st_data_t)obj, &data)) {
952  *ivtbl = (struct gen_ivtbl *)data;
953  return 1;
954  }
955  return 0;
956 }
957 
958 static VALUE
959 generic_ivar_delete(VALUE obj, ID id, VALUE undef)
960 {
961  struct gen_ivtbl *ivtbl;
962 
963  if (gen_ivtbl_get(obj, &ivtbl)) {
964  st_table *iv_index_tbl = RCLASS_IV_INDEX_TBL(rb_obj_class(obj));
965  st_data_t index;
966 
967  if (st_lookup(iv_index_tbl, (st_data_t)id, &index)) {
968  if (index < ivtbl->numiv) {
969  VALUE ret = ivtbl->ivptr[index];
970 
971  ivtbl->ivptr[index] = Qundef;
972  return ret == Qundef ? undef : ret;
973  }
974  }
975  }
976  return undef;
977 }
978 
979 static VALUE
980 generic_ivar_get(VALUE obj, ID id, VALUE undef)
981 {
982  struct gen_ivtbl *ivtbl;
983 
984  if (gen_ivtbl_get(obj, &ivtbl)) {
985  st_table *iv_index_tbl = RCLASS_IV_INDEX_TBL(rb_obj_class(obj));
986  st_data_t index;
987 
988  if (st_lookup(iv_index_tbl, (st_data_t)id, &index)) {
989  if (index < ivtbl->numiv) {
990  VALUE ret = ivtbl->ivptr[index];
991 
992  return ret == Qundef ? undef : ret;
993  }
994  }
995  }
996  return undef;
997 }
998 
999 static size_t
1000 gen_ivtbl_bytes(size_t n)
1001 {
1002  return sizeof(struct gen_ivtbl) + n * sizeof(VALUE) - sizeof(VALUE);
1003 }
1004 
1005 static struct gen_ivtbl *
1006 gen_ivtbl_resize(struct gen_ivtbl *old, uint32_t n)
1007 {
1008  uint32_t len = old ? old->numiv : 0;
1009  struct gen_ivtbl *ivtbl = xrealloc(old, gen_ivtbl_bytes(n));
1010 
1011  ivtbl->numiv = n;
1012  for (; len < n; len++) {
1013  ivtbl->ivptr[len] = Qundef;
1014  }
1015 
1016  return ivtbl;
1017 }
1018 
1019 #if 0
1020 static struct gen_ivtbl *
1021 gen_ivtbl_dup(const struct gen_ivtbl *orig)
1022 {
1023  size_t s = gen_ivtbl_bytes(orig->numiv);
1024  struct gen_ivtbl *ivtbl = xmalloc(s);
1025 
1026  memcpy(ivtbl, orig, s);
1027 
1028  return ivtbl;
1029 }
1030 #endif
1031 
1032 static uint32_t
1033 iv_index_tbl_newsize(struct ivar_update *ivup)
1034 {
1035  uint32_t index = (uint32_t)ivup->index; /* should not overflow */
1036  uint32_t newsize = (index+1) + (index+1)/4; /* (index+1)*1.25 */
1037 
1038  if (!ivup->iv_extended &&
1039  ivup->u.iv_index_tbl->num_entries < (st_index_t)newsize) {
1040  newsize = (uint32_t)ivup->u.iv_index_tbl->num_entries;
1041  }
1042  return newsize;
1043 }
1044 
1045 static int
1046 generic_ivar_update(st_data_t *k, st_data_t *v, st_data_t u, int existing)
1047 {
1048  VALUE obj = (VALUE)*k;
1049  struct ivar_update *ivup = (struct ivar_update *)u;
1050  uint32_t newsize;
1051  int ret = ST_CONTINUE;
1052  struct gen_ivtbl *ivtbl;
1053 
1054  if (existing) {
1055  ivtbl = (struct gen_ivtbl *)*v;
1056  if (ivup->index >= ivtbl->numiv) {
1057  goto resize;
1058  }
1059  ret = ST_STOP;
1060  }
1061  else {
1062  FL_SET(obj, FL_EXIVAR);
1063  ivtbl = 0;
1064 resize:
1065  newsize = iv_index_tbl_newsize(ivup);
1066  ivtbl = gen_ivtbl_resize(ivtbl, newsize);
1067  *v = (st_data_t)ivtbl;
1068  }
1069  ivup->u.ivtbl = ivtbl;
1070  return ret;
1071 }
1072 
1073 static VALUE
1074 generic_ivar_defined(VALUE obj, ID id)
1075 {
1076  struct gen_ivtbl *ivtbl;
1077  st_table *iv_index_tbl = RCLASS_IV_INDEX_TBL(rb_obj_class(obj));
1078  st_data_t index;
1079 
1080  if (!iv_index_tbl) return Qfalse;
1081  if (!st_lookup(iv_index_tbl, (st_data_t)id, &index)) return Qfalse;
1082  if (!gen_ivtbl_get(obj, &ivtbl)) return Qfalse;
1083 
1084  if ((index < ivtbl->numiv) && (ivtbl->ivptr[index] != Qundef))
1085  return Qtrue;
1086 
1087  return Qfalse;
1088 }
1089 
1090 static int
1091 generic_ivar_remove(VALUE obj, ID id, VALUE *valp)
1092 {
1093  struct gen_ivtbl *ivtbl;
1094  st_data_t key = (st_data_t)id;
1095  st_data_t index;
1096  st_table *iv_index_tbl = RCLASS_IV_INDEX_TBL(rb_obj_class(obj));
1097 
1098  if (!iv_index_tbl) return 0;
1099  if (!st_lookup(iv_index_tbl, key, &index)) return 0;
1100  if (!gen_ivtbl_get(obj, &ivtbl)) return 0;
1101 
1102  if (index < ivtbl->numiv) {
1103  if (ivtbl->ivptr[index] != Qundef) {
1104  *valp = ivtbl->ivptr[index];
1105  ivtbl->ivptr[index] = Qundef;
1106  return 1;
1107  }
1108  }
1109  return 0;
1110 }
1111 
1112 static void
1113 gen_ivtbl_mark(const struct gen_ivtbl *ivtbl)
1114 {
1115  uint32_t i;
1116 
1117  for (i = 0; i < ivtbl->numiv; i++) {
1118  rb_gc_mark(ivtbl->ivptr[i]);
1119  }
1120 }
1121 
1122 void
1124 {
1125  struct gen_ivtbl *ivtbl;
1126 
1127  if (gen_ivtbl_get(obj, &ivtbl)) {
1128  gen_ivtbl_mark(ivtbl);
1129  }
1130 }
1131 
1132 void
1134 {
1135  st_data_t key = (st_data_t)obj;
1136  struct gen_ivtbl *ivtbl;
1137 
1138  if (st_delete(generic_iv_tbl, &key, (st_data_t *)&ivtbl))
1139  xfree(ivtbl);
1140 
1141  if (generic_iv_tbl_compat) {
1142  st_table *tbl;
1143 
1144  if (st_delete(generic_iv_tbl_compat, &key, (st_data_t *)&tbl))
1145  st_free_table(tbl);
1146  }
1147 }
1148 
1149 RUBY_FUNC_EXPORTED size_t
1151 {
1152  struct gen_ivtbl *ivtbl;
1153 
1154  if (gen_ivtbl_get(obj, &ivtbl))
1155  return gen_ivtbl_bytes(ivtbl->numiv);
1156  return 0;
1157 }
1158 
1159 static size_t
1160 gen_ivtbl_count(const struct gen_ivtbl *ivtbl)
1161 {
1162  uint32_t i;
1163  size_t n = 0;
1164 
1165  for (i = 0; i < ivtbl->numiv; i++) {
1166  if (ivtbl->ivptr[i] != Qundef) {
1167  n++;
1168  }
1169  }
1170 
1171  return n;
1172 }
1173 
1174 VALUE
1175 rb_ivar_lookup(VALUE obj, ID id, VALUE undef)
1176 {
1177  VALUE val, *ptr;
1178  struct st_table *iv_index_tbl;
1179  uint32_t len;
1180  st_data_t index;
1181 
1182  if (SPECIAL_CONST_P(obj)) return undef;
1183  switch (BUILTIN_TYPE(obj)) {
1184  case T_OBJECT:
1185  len = ROBJECT_NUMIV(obj);
1186  ptr = ROBJECT_IVPTR(obj);
1187  iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj);
1188  if (!iv_index_tbl) break;
1189  if (!st_lookup(iv_index_tbl, (st_data_t)id, &index)) break;
1190  if (len <= index) break;
1191  val = ptr[index];
1192  if (val != Qundef)
1193  return val;
1194  break;
1195  case T_CLASS:
1196  case T_MODULE:
1197  if (RCLASS_IV_TBL(obj) &&
1198  st_lookup(RCLASS_IV_TBL(obj), (st_data_t)id, &index))
1199  return (VALUE)index;
1200  break;
1201  default:
1202  if (FL_TEST(obj, FL_EXIVAR))
1203  return generic_ivar_get(obj, id, undef);
1204  break;
1205  }
1206  return undef;
1207 }
1208 
1209 VALUE
1211 {
1212  VALUE iv = rb_ivar_lookup(obj, id, Qundef);
1213  RB_DEBUG_COUNTER_INC(ivar_get_base);
1214 
1215  if (iv == Qundef) {
1216  if (RTEST(ruby_verbose))
1217  rb_warning("instance variable %"PRIsVALUE" not initialized", QUOTE_ID(id));
1218  iv = Qnil;
1219  }
1220  return iv;
1221 }
1222 
1223 VALUE
1225 {
1226  return rb_ivar_lookup(obj, id, Qnil);
1227 }
1228 
1229 static VALUE
1230 rb_ivar_delete(VALUE obj, ID id, VALUE undef)
1231 {
1232  VALUE val, *ptr;
1233  struct st_table *iv_index_tbl;
1234  uint32_t len;
1235  st_data_t index;
1236 
1237  rb_check_frozen(obj);
1238  switch (BUILTIN_TYPE(obj)) {
1239  case T_OBJECT:
1240  len = ROBJECT_NUMIV(obj);
1241  ptr = ROBJECT_IVPTR(obj);
1242  iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj);
1243  if (!iv_index_tbl) break;
1244  if (!st_lookup(iv_index_tbl, (st_data_t)id, &index)) break;
1245  if (len <= index) break;
1246  val = ptr[index];
1247  ptr[index] = Qundef;
1248  if (val != Qundef)
1249  return val;
1250  break;
1251  case T_CLASS:
1252  case T_MODULE:
1253  if (RCLASS_IV_TBL(obj) &&
1254  st_delete(RCLASS_IV_TBL(obj), (st_data_t *)&id, &index))
1255  return (VALUE)index;
1256  break;
1257  default:
1258  if (FL_TEST(obj, FL_EXIVAR))
1259  return generic_ivar_delete(obj, id, undef);
1260  break;
1261  }
1262  return undef;
1263 }
1264 
1265 VALUE
1267 {
1268  return rb_ivar_delete(obj, id, Qnil);
1269 }
1270 
1271 static st_table *
1272 iv_index_tbl_make(VALUE obj)
1273 {
1274  VALUE klass = rb_obj_class(obj);
1275  st_table *iv_index_tbl = RCLASS_IV_INDEX_TBL(klass);
1276 
1277  if (!iv_index_tbl) {
1278  iv_index_tbl = RCLASS_IV_INDEX_TBL(klass) = st_init_numtable();
1279  }
1280 
1281  return iv_index_tbl;
1282 }
1283 
1284 static void
1285 iv_index_tbl_extend(struct ivar_update *ivup, ID id)
1286 {
1287  if (st_lookup(ivup->u.iv_index_tbl, (st_data_t)id, &ivup->index)) {
1288  return;
1289  }
1290  if (ivup->u.iv_index_tbl->num_entries >= INT_MAX) {
1291  rb_raise(rb_eArgError, "too many instance variables");
1292  }
1293  ivup->index = (st_data_t)ivup->u.iv_index_tbl->num_entries;
1294  st_add_direct(ivup->u.iv_index_tbl, (st_data_t)id, ivup->index);
1295  ivup->iv_extended = 1;
1296 }
1297 
1298 static void
1299 generic_ivar_set(VALUE obj, ID id, VALUE val)
1300 {
1301  struct ivar_update ivup;
1302 
1303  ivup.iv_extended = 0;
1304  ivup.u.iv_index_tbl = iv_index_tbl_make(obj);
1305  iv_index_tbl_extend(&ivup, id);
1306  st_update(generic_iv_tbl, (st_data_t)obj, generic_ivar_update,
1307  (st_data_t)&ivup);
1308 
1309  ivup.u.ivtbl->ivptr[ivup.index] = val;
1310 
1311  RB_OBJ_WRITTEN(obj, Qundef, val);
1312 }
1313 
1314 VALUE
1315 rb_ivar_set(VALUE obj, ID id, VALUE val)
1316 {
1317  struct ivar_update ivup;
1318  uint32_t i, len;
1319 
1320  RB_DEBUG_COUNTER_INC(ivar_set_base);
1321 
1322  rb_check_frozen(obj);
1323 
1324  switch (BUILTIN_TYPE(obj)) {
1325  case T_OBJECT:
1326  ivup.iv_extended = 0;
1327  ivup.u.iv_index_tbl = iv_index_tbl_make(obj);
1328  iv_index_tbl_extend(&ivup, id);
1329  len = ROBJECT_NUMIV(obj);
1330  if (len <= ivup.index) {
1331  VALUE *ptr = ROBJECT_IVPTR(obj);
1332  if (ivup.index < ROBJECT_EMBED_LEN_MAX) {
1333  RBASIC(obj)->flags |= ROBJECT_EMBED;
1334  ptr = ROBJECT(obj)->as.ary;
1335  for (i = 0; i < ROBJECT_EMBED_LEN_MAX; i++) {
1336  ptr[i] = Qundef;
1337  }
1338  }
1339  else {
1340  VALUE *newptr;
1341  uint32_t newsize = iv_index_tbl_newsize(&ivup);
1342 
1343  if (RBASIC(obj)->flags & ROBJECT_EMBED) {
1344  newptr = ALLOC_N(VALUE, newsize);
1345  MEMCPY(newptr, ptr, VALUE, len);
1346  RBASIC(obj)->flags &= ~ROBJECT_EMBED;
1347  ROBJECT(obj)->as.heap.ivptr = newptr;
1348  }
1349  else {
1350  REALLOC_N(ROBJECT(obj)->as.heap.ivptr, VALUE, newsize);
1351  newptr = ROBJECT(obj)->as.heap.ivptr;
1352  }
1353  for (; len < newsize; len++)
1354  newptr[len] = Qundef;
1355  ROBJECT(obj)->as.heap.numiv = newsize;
1356  ROBJECT(obj)->as.heap.iv_index_tbl = ivup.u.iv_index_tbl;
1357  }
1358  }
1359  RB_OBJ_WRITE(obj, &ROBJECT_IVPTR(obj)[ivup.index], val);
1360  break;
1361  case T_CLASS:
1362  case T_MODULE:
1363  if (!RCLASS_IV_TBL(obj)) RCLASS_IV_TBL(obj) = st_init_numtable();
1364  rb_class_ivar_set(obj, id, val);
1365  break;
1366  default:
1367  generic_ivar_set(obj, id, val);
1368  break;
1369  }
1370  return val;
1371 }
1372 
1373 VALUE
1375 {
1376  VALUE val;
1377  struct st_table *iv_index_tbl;
1378  st_data_t index;
1379 
1380  if (SPECIAL_CONST_P(obj)) return Qfalse;
1381  switch (BUILTIN_TYPE(obj)) {
1382  case T_OBJECT:
1383  iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj);
1384  if (!iv_index_tbl) break;
1385  if (!st_lookup(iv_index_tbl, (st_data_t)id, &index)) break;
1386  if (ROBJECT_NUMIV(obj) <= index) break;
1387  val = ROBJECT_IVPTR(obj)[index];
1388  if (val != Qundef)
1389  return Qtrue;
1390  break;
1391  case T_CLASS:
1392  case T_MODULE:
1393  if (RCLASS_IV_TBL(obj) && st_lookup(RCLASS_IV_TBL(obj), (st_data_t)id, 0))
1394  return Qtrue;
1395  break;
1396  default:
1397  if (FL_TEST(obj, FL_EXIVAR))
1398  return generic_ivar_defined(obj, id);
1399  break;
1400  }
1401  return Qfalse;
1402 }
1403 
1406  int (*func)(ID key, VALUE val, st_data_t arg);
1408 };
1409 
1410 static int
1411 obj_ivar_i(st_data_t key, st_data_t index, st_data_t arg)
1412 {
1413  struct obj_ivar_tag *data = (struct obj_ivar_tag *)arg;
1414  if (index < ROBJECT_NUMIV(data->obj)) {
1415  VALUE val = ROBJECT_IVPTR(data->obj)[index];
1416  if (val != Qundef) {
1417  return (data->func)((ID)key, val, data->arg);
1418  }
1419  }
1420  return ST_CONTINUE;
1421 }
1422 
1423 static void
1424 obj_ivar_each(VALUE obj, int (*func)(ANYARGS), st_data_t arg)
1425 {
1426  st_table *tbl;
1427  struct obj_ivar_tag data;
1428 
1429  tbl = ROBJECT_IV_INDEX_TBL(obj);
1430  if (!tbl)
1431  return;
1432 
1433  data.obj = obj;
1434  data.func = (int (*)(ID key, VALUE val, st_data_t arg))func;
1435  data.arg = arg;
1436 
1437  st_foreach_safe(tbl, obj_ivar_i, (st_data_t)&data);
1438 }
1439 
1441  struct gen_ivtbl *ivtbl;
1442  int (*func)(ID key, VALUE val, st_data_t arg);
1444 };
1445 
1446 static int
1447 gen_ivar_each_i(st_data_t key, st_data_t index, st_data_t data)
1448 {
1449  struct gen_ivar_tag *arg = (struct gen_ivar_tag *)data;
1450 
1451  if (index < arg->ivtbl->numiv) {
1452  VALUE val = arg->ivtbl->ivptr[index];
1453  if (val != Qundef) {
1454  return (arg->func)((ID)key, val, arg->arg);
1455  }
1456  }
1457  return ST_CONTINUE;
1458 }
1459 
1460 static void
1461 gen_ivar_each(VALUE obj, int (*func)(ANYARGS), st_data_t arg)
1462 {
1463  struct gen_ivar_tag data;
1464  st_table *iv_index_tbl = RCLASS_IV_INDEX_TBL(rb_obj_class(obj));
1465 
1466  if (!iv_index_tbl) return;
1467  if (!gen_ivtbl_get(obj, &data.ivtbl)) return;
1468 
1469  data.func = (int (*)(ID key, VALUE val, st_data_t arg))func;
1470  data.arg = arg;
1471 
1472  st_foreach_safe(iv_index_tbl, gen_ivar_each_i, (st_data_t)&data);
1473 }
1474 
1475 struct givar_copy {
1478  struct gen_ivtbl *ivtbl;
1479 };
1480 
1481 static int
1482 gen_ivar_copy(ID id, VALUE val, st_data_t arg)
1483 {
1484  struct givar_copy *c = (struct givar_copy *)arg;
1485  struct ivar_update ivup;
1486 
1487  ivup.iv_extended = 0;
1488  ivup.u.iv_index_tbl = c->iv_index_tbl;
1489  iv_index_tbl_extend(&ivup, id);
1490  if (ivup.index >= c->ivtbl->numiv) {
1491  uint32_t newsize = iv_index_tbl_newsize(&ivup);
1492  c->ivtbl = gen_ivtbl_resize(c->ivtbl, newsize);
1493  }
1494  c->ivtbl->ivptr[ivup.index] = val;
1495 
1496  RB_OBJ_WRITTEN(c->obj, Qundef, val);
1497 
1498  return ST_CONTINUE;
1499 }
1500 
1501 void
1503 {
1504  struct gen_ivtbl *ivtbl;
1505 
1506  rb_check_frozen(clone);
1507 
1508  if (!FL_TEST(obj, FL_EXIVAR)) {
1509  clear:
1510  if (FL_TEST(clone, FL_EXIVAR)) {
1511  rb_free_generic_ivar(clone);
1512  FL_UNSET(clone, FL_EXIVAR);
1513  }
1514  return;
1515  }
1516  if (gen_ivtbl_get(obj, &ivtbl)) {
1517  struct givar_copy c;
1518  uint32_t i;
1519 
1520  if (gen_ivtbl_count(ivtbl) == 0)
1521  goto clear;
1522 
1523  if (gen_ivtbl_get(clone, &c.ivtbl)) {
1524  for (i = 0; i < c.ivtbl->numiv; i++)
1525  c.ivtbl->ivptr[i] = Qundef;
1526  }
1527  else {
1528  c.ivtbl = gen_ivtbl_resize(0, ivtbl->numiv);
1529  FL_SET(clone, FL_EXIVAR);
1530  }
1531 
1532  c.iv_index_tbl = iv_index_tbl_make(clone);
1533  c.obj = clone;
1534  gen_ivar_each(obj, gen_ivar_copy, (st_data_t)&c);
1535  /*
1536  * c.ivtbl may change in gen_ivar_copy due to realloc,
1537  * no need to free
1538  */
1539  st_insert(generic_iv_tbl, (st_data_t)clone, (st_data_t)c.ivtbl);
1540  }
1541 }
1542 
1543 void
1544 rb_ivar_foreach(VALUE obj, int (*func)(ANYARGS), st_data_t arg)
1545 {
1546  if (SPECIAL_CONST_P(obj)) return;
1547  switch (BUILTIN_TYPE(obj)) {
1548  case T_OBJECT:
1549  obj_ivar_each(obj, func, arg);
1550  break;
1551  case T_CLASS:
1552  case T_MODULE:
1553  if (RCLASS_IV_TBL(obj)) {
1554  st_foreach_safe(RCLASS_IV_TBL(obj), func, arg);
1555  }
1556  break;
1557  default:
1558  if (FL_TEST(obj, FL_EXIVAR)) {
1559  gen_ivar_each(obj, func, arg);
1560  }
1561  break;
1562  }
1563 }
1564 
1565 st_index_t
1567 {
1568  st_table *tbl;
1569 
1570  if (SPECIAL_CONST_P(obj)) return 0;
1571 
1572  switch (BUILTIN_TYPE(obj)) {
1573  case T_OBJECT:
1574  if ((tbl = ROBJECT_IV_INDEX_TBL(obj)) != 0) {
1575  st_index_t i, count, num = ROBJECT_NUMIV(obj);
1576  const VALUE *const ivptr = ROBJECT_IVPTR(obj);
1577  for (i = count = 0; i < num; ++i) {
1578  if (ivptr[i] != Qundef) {
1579  count++;
1580  }
1581  }
1582  return count;
1583  }
1584  break;
1585  case T_CLASS:
1586  case T_MODULE:
1587  if ((tbl = RCLASS_IV_TBL(obj)) != 0) {
1588  return tbl->num_entries;
1589  }
1590  break;
1591  default:
1592  if (FL_TEST(obj, FL_EXIVAR)) {
1593  struct gen_ivtbl *ivtbl;
1594 
1595  if (gen_ivtbl_get(obj, &ivtbl)) {
1596  return gen_ivtbl_count(ivtbl);
1597  }
1598  }
1599  break;
1600  }
1601  return 0;
1602 }
1603 
1604 static int
1605 ivar_i(st_data_t k, st_data_t v, st_data_t a)
1606 {
1607  ID key = (ID)k;
1608  VALUE ary = (VALUE)a;
1609 
1610  if (rb_is_instance_id(key)) {
1611  rb_ary_push(ary, ID2SYM(key));
1612  }
1613  return ST_CONTINUE;
1614 }
1615 
1616 /*
1617  * call-seq:
1618  * obj.instance_variables -> array
1619  *
1620  * Returns an array of instance variable names for the receiver. Note
1621  * that simply defining an accessor does not create the corresponding
1622  * instance variable.
1623  *
1624  * class Fred
1625  * attr_accessor :a1
1626  * def initialize
1627  * @iv = 3
1628  * end
1629  * end
1630  * Fred.new.instance_variables #=> [:@iv]
1631  */
1632 
1633 VALUE
1635 {
1636  VALUE ary;
1637 
1638  ary = rb_ary_new();
1639  rb_ivar_foreach(obj, ivar_i, ary);
1640  return ary;
1641 }
1642 
1643 #define rb_is_constant_id rb_is_const_id
1644 #define rb_is_constant_name rb_is_const_name
1645 #define id_for_var(obj, name, part, type) \
1646  id_for_var_message(obj, name, type, "`%1$s' is not allowed as "#part" "#type" variable name")
1647 #define id_for_var_message(obj, name, type, message) \
1648  check_id_type(obj, &(name), rb_is_##type##_id, rb_is_##type##_name, message, strlen(message))
1649 static ID
1650 check_id_type(VALUE obj, VALUE *pname,
1651  int (*valid_id_p)(ID), int (*valid_name_p)(VALUE),
1652  const char *message, size_t message_len)
1653 {
1654  ID id = rb_check_id(pname);
1655  VALUE name = *pname;
1656 
1657  if (id ? !valid_id_p(id) : !valid_name_p(name)) {
1658  rb_name_err_raise_str(rb_fstring_new(message, message_len),
1659  obj, name);
1660  }
1661  return id;
1662 }
1663 
1664 /*
1665  * call-seq:
1666  * obj.remove_instance_variable(symbol) -> obj
1667  *
1668  * Removes the named instance variable from <i>obj</i>, returning that
1669  * variable's value.
1670  *
1671  * class Dummy
1672  * attr_reader :var
1673  * def initialize
1674  * @var = 99
1675  * end
1676  * def remove
1677  * remove_instance_variable(:@var)
1678  * end
1679  * end
1680  * d = Dummy.new
1681  * d.var #=> 99
1682  * d.remove #=> 99
1683  * d.var #=> nil
1684  */
1685 
1686 VALUE
1688 {
1689  VALUE val = Qnil;
1690  const ID id = id_for_var(obj, name, an, instance);
1691  st_data_t n, v;
1692  struct st_table *iv_index_tbl;
1693  st_data_t index;
1694 
1695  rb_check_frozen(obj);
1696  if (!id) {
1697  goto not_defined;
1698  }
1699 
1700  switch (BUILTIN_TYPE(obj)) {
1701  case T_OBJECT:
1702  iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj);
1703  if (!iv_index_tbl) break;
1704  if (!st_lookup(iv_index_tbl, (st_data_t)id, &index)) break;
1705  if (ROBJECT_NUMIV(obj) <= index) break;
1706  val = ROBJECT_IVPTR(obj)[index];
1707  if (val != Qundef) {
1708  ROBJECT_IVPTR(obj)[index] = Qundef;
1709  return val;
1710  }
1711  break;
1712  case T_CLASS:
1713  case T_MODULE:
1714  n = id;
1715  if (RCLASS_IV_TBL(obj) && st_delete(RCLASS_IV_TBL(obj), &n, &v)) {
1716  return (VALUE)v;
1717  }
1718  break;
1719  default:
1720  if (FL_TEST(obj, FL_EXIVAR)) {
1721  if (generic_ivar_remove(obj, id, &val)) {
1722  return val;
1723  }
1724  }
1725  break;
1726  }
1727 
1728  not_defined:
1729  rb_name_err_raise("instance variable %1$s not defined",
1730  obj, name);
1731  UNREACHABLE;
1732 }
1733 
1734 NORETURN(static void uninitialized_constant(VALUE, VALUE));
1735 static void
1736 uninitialized_constant(VALUE klass, VALUE name)
1737 {
1738  if (klass && rb_class_real(klass) != rb_cObject)
1739  rb_name_err_raise("uninitialized constant %2$s::%1$s",
1740  klass, name);
1741  else
1742  rb_name_err_raise("uninitialized constant %1$s",
1743  klass, name);
1744 }
1745 
1746 VALUE
1748 {
1749  VALUE value = rb_funcallv(klass, rb_intern("const_missing"), 1, &name);
1751  return value;
1752 }
1753 
1754 
1755 /*
1756  * call-seq:
1757  * mod.const_missing(sym) -> obj
1758  *
1759  * Invoked when a reference is made to an undefined constant in
1760  * <i>mod</i>. It is passed a symbol for the undefined constant, and
1761  * returns a value to be used for that constant. The
1762  * following code is an example of the same:
1763  *
1764  * def Foo.const_missing(name)
1765  * name # return the constant name as Symbol
1766  * end
1767  *
1768  * Foo::UNDEFINED_CONST #=> :UNDEFINED_CONST: symbol returned
1769  *
1770  * In the next example when a reference is made to an undefined constant,
1771  * it attempts to load a file whose name is the lowercase version of the
1772  * constant (thus class <code>Fred</code> is assumed to be in file
1773  * <code>fred.rb</code>). If found, it returns the loaded class. It
1774  * therefore implements an autoload feature similar to Kernel#autoload and
1775  * Module#autoload.
1776  *
1777  * def Object.const_missing(name)
1778  * @looked_for ||= {}
1779  * str_name = name.to_s
1780  * raise "Class not found: #{name}" if @looked_for[str_name]
1781  * @looked_for[str_name] = 1
1782  * file = str_name.downcase
1783  * require file
1784  * klass = const_get(name)
1785  * return klass if klass
1786  * raise "Class not found: #{name}"
1787  * end
1788  *
1789  */
1790 
1791 VALUE
1793 {
1795  uninitialized_constant(klass, name);
1796 
1797  UNREACHABLE;
1798 }
1799 
1800 static void
1801 autoload_mark(void *ptr)
1802 {
1803  rb_mark_tbl((st_table *)ptr);
1804 }
1805 
1806 static void
1807 autoload_free(void *ptr)
1808 {
1809  st_free_table((st_table *)ptr);
1810 }
1811 
1812 static size_t
1813 autoload_memsize(const void *ptr)
1814 {
1815  const st_table *tbl = ptr;
1816  return st_memsize(tbl);
1817 }
1818 
1819 static const rb_data_type_t autoload_data_type = {
1820  "autoload",
1821  {autoload_mark, autoload_free, autoload_memsize,},
1823 };
1824 
1825 #define check_autoload_table(av) \
1826  (struct st_table *)rb_check_typeddata((av), &autoload_data_type)
1827 
1828 static VALUE
1829 autoload_data(VALUE mod, ID id)
1830 {
1831  struct st_table *tbl;
1832  st_data_t val;
1833 
1834  if (!st_lookup(RCLASS_IV_TBL(mod), autoload, &val) ||
1835  !(tbl = check_autoload_table((VALUE)val)) ||
1836  !st_lookup(tbl, (st_data_t)id, &val)) {
1837  return 0;
1838  }
1839  return (VALUE)val;
1840 }
1841 
1842 /* always on stack, no need to mark */
1849  union {
1850  struct list_node node;
1851  struct list_head head;
1852  } waitq;
1853 };
1854 
1859  struct autoload_state *state; /* points to on-stack struct */
1860 };
1861 
1862 static void
1863 autoload_i_mark(void *ptr)
1864 {
1865  struct autoload_data_i *p = ptr;
1866  rb_gc_mark(p->feature);
1867  rb_gc_mark(p->value);
1868 }
1869 
1870 static size_t
1871 autoload_i_memsize(const void *ptr)
1872 {
1873  return sizeof(struct autoload_data_i);
1874 }
1875 
1876 static const rb_data_type_t autoload_data_i_type = {
1877  "autoload_i",
1878  {autoload_i_mark, RUBY_TYPED_DEFAULT_FREE, autoload_i_memsize,},
1879  0, 0, RUBY_TYPED_FREE_IMMEDIATELY
1880 };
1881 
1882 #define check_autoload_data(av) \
1883  (struct autoload_data_i *)rb_check_typeddata((av), &autoload_data_i_type)
1884 
1885 void
1886 rb_autoload(VALUE mod, ID id, const char *file)
1887 {
1888  if (!file || !*file) {
1889  rb_raise(rb_eArgError, "empty file name");
1890  }
1891  rb_autoload_str(mod, id, rb_fstring_cstr(file));
1892 }
1893 
1894 void
1896 {
1897  st_data_t av;
1898  VALUE ad;
1899  struct st_table *tbl;
1900  struct autoload_data_i *ele;
1901  rb_const_entry_t *ce;
1902 
1903  if (!rb_is_const_id(id)) {
1904  rb_raise(rb_eNameError, "autoload must be constant name: %"PRIsVALUE"",
1905  QUOTE_ID(id));
1906  }
1907 
1908  Check_Type(file, T_STRING);
1909  if (!RSTRING_LEN(file)) {
1910  rb_raise(rb_eArgError, "empty file name");
1911  }
1912 
1913  ce = rb_const_lookup(mod, id);
1914  if (ce && ce->value != Qundef) {
1915  return;
1916  }
1917 
1918  rb_const_set(mod, id, Qundef);
1919  tbl = RCLASS_IV_TBL(mod);
1920  if (tbl && st_lookup(tbl, (st_data_t)autoload, &av)) {
1921  tbl = check_autoload_table((VALUE)av);
1922  }
1923  else {
1924  if (!tbl) tbl = RCLASS_IV_TBL(mod) = st_init_numtable();
1925  av = (st_data_t)TypedData_Wrap_Struct(0, &autoload_data_type, 0);
1926  st_add_direct(tbl, (st_data_t)autoload, av);
1927  RB_OBJ_WRITTEN(mod, Qnil, av);
1928  DATA_PTR(av) = tbl = st_init_numtable();
1929  }
1930 
1931  ad = TypedData_Make_Struct(0, struct autoload_data_i, &autoload_data_i_type, ele);
1932  if (OBJ_TAINTED(file)) {
1933  file = rb_str_dup(file);
1934  FL_UNSET(file, FL_TAINT);
1935  }
1936  ele->feature = rb_fstring(file);
1937  ele->safe_level = rb_safe_level();
1938  ele->value = Qundef;
1939  ele->state = 0;
1940  st_insert(tbl, (st_data_t)id, (st_data_t)ad);
1941 }
1942 
1943 static void
1944 autoload_delete(VALUE mod, ID id)
1945 {
1946  st_data_t val, load = 0, n = id;
1947 
1948  if (st_lookup(RCLASS_IV_TBL(mod), (st_data_t)autoload, &val)) {
1949  struct st_table *tbl = check_autoload_table((VALUE)val);
1950 
1951  st_delete(tbl, &n, &load);
1952 
1953  if (tbl->num_entries == 0) {
1954  n = autoload;
1955  st_delete(RCLASS_IV_TBL(mod), &n, &val);
1956  }
1957  }
1958 }
1959 
1960 static VALUE
1961 autoload_provided(VALUE arg)
1962 {
1963  const char **p = (const char **)arg;
1964  return rb_feature_provided(*p, p);
1965 }
1966 
1967 static VALUE
1968 reset_safe(VALUE safe)
1969 {
1970  rb_set_safe_level_force((int)safe);
1971  return safe;
1972 }
1973 
1974 static VALUE
1975 check_autoload_required(VALUE mod, ID id, const char **loadingpath)
1976 {
1977  VALUE file, load;
1978  struct autoload_data_i *ele;
1979  const char *loading;
1980  int safe;
1981 
1982  if (!(load = autoload_data(mod, id)) || !(ele = check_autoload_data(load))) {
1983  return 0;
1984  }
1985  file = ele->feature;
1986  Check_Type(file, T_STRING);
1987  if (!RSTRING_LEN(file) || !*RSTRING_PTR(file)) {
1988  rb_raise(rb_eArgError, "empty file name");
1989  }
1990 
1991  /*
1992  * if somebody else is autoloading, we MUST wait for them, since
1993  * rb_provide_feature can provide a feature before autoload_const_set
1994  * completes. We must wait until autoload_const_set finishes in
1995  * the other thread.
1996  */
1997  if (ele->state && ele->state->thread != rb_thread_current()) {
1998  return load;
1999  }
2000 
2001  loading = RSTRING_PTR(file);
2002  safe = rb_safe_level();
2004  if (!rb_ensure(autoload_provided, (VALUE)&loading, reset_safe, (VALUE)safe)) {
2005  return load;
2006  }
2007  if (loadingpath && loading) {
2008  *loadingpath = loading;
2009  return load;
2010  }
2011  return 0;
2012 }
2013 
2014 int
2016 {
2017  VALUE load;
2018  struct autoload_data_i *ele;
2019 
2020  if (!(load = autoload_data(mod, id)) || !(ele = check_autoload_data(load))) {
2021  return 0;
2022  }
2023  if (ele->state && ele->state->thread == rb_thread_current()) {
2024  if (ele->value != Qundef) {
2025  if (value) {
2026  *value = ele->value;
2027  }
2028  return 1;
2029  }
2030  }
2031  return 0;
2032 }
2033 
2034 static int
2035 autoload_defined_p(VALUE mod, ID id)
2036 {
2037  rb_const_entry_t *ce = rb_const_lookup(mod, id);
2038 
2039  if (!ce || ce->value != Qundef) {
2040  return 0;
2041  }
2042  return !rb_autoloading_value(mod, id, NULL);
2043 }
2044 
2049 };
2050 
2051 static void const_tbl_update(struct autoload_const_set_args *);
2052 
2053 static VALUE
2054 autoload_const_set(VALUE arg)
2055 {
2056  struct autoload_const_set_args* args = (struct autoload_const_set_args *)arg;
2057  VALUE klass = args->mod;
2058  ID id = args->id;
2059  check_before_mod_set(klass, id, args->value, "constant");
2060  const_tbl_update(args);
2061  return 0; /* ignored */
2062 }
2063 
2064 static VALUE
2065 autoload_require(VALUE arg)
2066 {
2067  struct autoload_state *state = (struct autoload_state *)arg;
2068 
2069  /* this may release GVL and switch threads: */
2070  state->result = rb_funcall(rb_vm_top_self(), rb_intern("require"), 1,
2071  state->ele->feature);
2072 
2073  return state->result;
2074 }
2075 
2076 static VALUE
2077 autoload_reset(VALUE arg)
2078 {
2079  struct autoload_state *state = (struct autoload_state *)arg;
2080  int need_wakeups = 0;
2081 
2082  if (state->ele->state == state) {
2083  need_wakeups = 1;
2084  state->ele->state = 0;
2085  }
2086 
2087  /* At the last, move a value defined in autoload to constant table */
2088  if (RTEST(state->result) && state->ele->value != Qundef) {
2089  int safe_backup;
2090  struct autoload_const_set_args args;
2091 
2092  args.mod = state->mod;
2093  args.id = state->id;
2094  args.value = state->ele->value;
2095  safe_backup = rb_safe_level();
2097  rb_ensure(autoload_const_set, (VALUE)&args,
2098  reset_safe, (VALUE)safe_backup);
2099  }
2100 
2101  /* wakeup any waiters we had */
2102  if (need_wakeups) {
2103  struct autoload_state *cur = 0, *nxt;
2104 
2105  list_for_each_safe(&state->waitq.head, cur, nxt, waitq.node) {
2106  VALUE th = cur->thread;
2107 
2108  cur->thread = Qfalse;
2109  list_del_init(&cur->waitq.node); /* idempotent */
2110 
2111  /*
2112  * cur is stored on the stack of cur->waiting_th,
2113  * do not touch cur after waking up waiting_th
2114  */
2116  }
2117  }
2118 
2119  return 0; /* ignored */
2120 }
2121 
2122 static VALUE
2123 autoload_sleep(VALUE arg)
2124 {
2125  struct autoload_state *state = (struct autoload_state *)arg;
2126 
2127  /*
2128  * autoload_reset in other thread will resume us and remove us
2129  * from the waitq list
2130  */
2131  do {
2133  } while (state->thread != Qfalse);
2134 
2135  return Qfalse;
2136 }
2137 
2138 static VALUE
2139 autoload_sleep_done(VALUE arg)
2140 {
2141  struct autoload_state *state = (struct autoload_state *)arg;
2142 
2143  if (state->thread != Qfalse && rb_thread_to_be_killed(state->thread)) {
2144  list_del(&state->waitq.node); /* idempotent after list_del_init */
2145  }
2146 
2147  return Qfalse;
2148 }
2149 
2150 VALUE
2152 {
2153  VALUE load, result;
2154  const char *loading = 0, *src;
2155  struct autoload_data_i *ele;
2156  struct autoload_state state;
2157 
2158  if (!autoload_defined_p(mod, id)) return Qfalse;
2159  load = check_autoload_required(mod, id, &loading);
2160  if (!load) return Qfalse;
2161  src = rb_sourcefile();
2162  if (src && loading && strcmp(src, loading) == 0) return Qfalse;
2163 
2164  /* set ele->state for a marker of autoloading thread */
2165  if (!(ele = check_autoload_data(load))) {
2166  return Qfalse;
2167  }
2168 
2169  state.ele = ele;
2170  state.mod = mod;
2171  state.id = id;
2172  state.thread = rb_thread_current();
2173  if (!ele->state) {
2174  ele->state = &state;
2175 
2176  /*
2177  * autoload_reset will wake up any threads added to this
2178  * iff the GVL is released during autoload_require
2179  */
2180  list_head_init(&state.waitq.head);
2181  }
2182  else if (state.thread == ele->state->thread) {
2183  return Qfalse;
2184  }
2185  else {
2186  list_add_tail(&ele->state->waitq.head, &state.waitq.node);
2187 
2188  rb_ensure(autoload_sleep, (VALUE)&state,
2189  autoload_sleep_done, (VALUE)&state);
2190  }
2191 
2192  /* autoload_data_i can be deleted by another thread while require */
2193  result = rb_ensure(autoload_require, (VALUE)&state,
2194  autoload_reset, (VALUE)&state);
2195 
2196  RB_GC_GUARD(load);
2197  return result;
2198 }
2199 
2200 VALUE
2202 {
2203  VALUE load;
2204  struct autoload_data_i *ele;
2205 
2206  while (!autoload_defined_p(mod, id)) {
2207  mod = RCLASS_SUPER(mod);
2208  if (!mod) return Qnil;
2209  }
2210  load = check_autoload_required(mod, id, 0);
2211  if (!load) return Qnil;
2212  return (ele = check_autoload_data(load)) ? ele->feature : Qnil;
2213 }
2214 
2215 void
2217 {
2218  if (RB_CONST_DEPRECATED_P(ce)) {
2219  if (klass == rb_cObject) {
2220  rb_warn("constant ::%"PRIsVALUE" is deprecated", QUOTE_ID(id));
2221  }
2222  else {
2223  rb_warn("constant %"PRIsVALUE"::%"PRIsVALUE" is deprecated",
2224  rb_class_name(klass), QUOTE_ID(id));
2225  }
2226  }
2227 }
2228 
2229 static VALUE
2230 rb_const_get_0(VALUE klass, ID id, int exclude, int recurse, int visibility)
2231 {
2232  VALUE c = rb_const_search(klass, id, exclude, recurse, visibility);
2233  if (c != Qundef) return c;
2234  return rb_const_missing(klass, ID2SYM(id));
2235 }
2236 
2237 static VALUE
2238 rb_const_search(VALUE klass, ID id, int exclude, int recurse, int visibility)
2239 {
2240  VALUE value, tmp, av;
2241  int mod_retry = 0;
2242 
2243  tmp = klass;
2244  retry:
2245  while (RTEST(tmp)) {
2246  VALUE am = 0;
2247  rb_const_entry_t *ce;
2248 
2249  while ((ce = rb_const_lookup(tmp, id))) {
2250  if (visibility && RB_CONST_PRIVATE_P(ce)) {
2251  rb_name_err_raise("private constant %2$s::%1$s referenced",
2252  tmp, ID2SYM(id));
2253  }
2254  rb_const_warn_if_deprecated(ce, tmp, id);
2255  value = ce->value;
2256  if (value == Qundef) {
2257  if (am == tmp) break;
2258  am = tmp;
2259  if (rb_autoloading_value(tmp, id, &av)) return av;
2260  rb_autoload_load(tmp, id);
2261  continue;
2262  }
2263  if (exclude && tmp == rb_cObject && klass != rb_cObject) {
2264 #if 0
2265  rb_warn("toplevel constant %"PRIsVALUE" referenced by %"PRIsVALUE"::%"PRIsVALUE"",
2266  QUOTE_ID(id), rb_class_name(klass), QUOTE_ID(id));
2267 #else
2268  return Qundef;
2269 #endif
2270  }
2271  return value;
2272  }
2273  if (!recurse) break;
2274  tmp = RCLASS_SUPER(tmp);
2275  }
2276  if (!exclude && !mod_retry && BUILTIN_TYPE(klass) == T_MODULE) {
2277  mod_retry = 1;
2278  tmp = rb_cObject;
2279  goto retry;
2280  }
2281 
2282  return Qundef;
2283 }
2284 
2285 VALUE
2287 {
2288  return rb_const_get_0(klass, id, TRUE, TRUE, FALSE);
2289 }
2290 
2291 VALUE
2293 {
2294  return rb_const_get_0(klass, id, FALSE, TRUE, FALSE);
2295 }
2296 
2297 VALUE
2299 {
2300  return rb_const_get_0(klass, id, TRUE, FALSE, FALSE);
2301 }
2302 
2303 VALUE
2305 {
2306  return rb_const_get_0(klass, id, TRUE, TRUE, TRUE);
2307 }
2308 
2309 VALUE
2311 {
2312  return rb_const_get_0(klass, id, FALSE, TRUE, TRUE);
2313 }
2314 
2315 VALUE
2317 {
2318  return rb_const_get_0(klass, id, TRUE, FALSE, TRUE);
2319 }
2320 
2321 /*
2322  * call-seq:
2323  * remove_const(sym) -> obj
2324  *
2325  * Removes the definition of the given constant, returning that
2326  * constant's previous value. If that constant referred to
2327  * a module, this will not change that module's name and can lead
2328  * to confusion.
2329  */
2330 
2331 VALUE
2333 {
2334  const ID id = id_for_var(mod, name, a, constant);
2335 
2336  if (!id) {
2337  rb_name_err_raise("constant %2$s::%1$s not defined",
2338  mod, name);
2339  }
2340  return rb_const_remove(mod, id);
2341 }
2342 
2343 VALUE
2345 {
2346  VALUE val;
2347  rb_const_entry_t *ce;
2348 
2349  rb_check_frozen(mod);
2350  ce = rb_const_lookup(mod, id);
2351  if (!ce || !rb_id_table_delete(RCLASS_CONST_TBL(mod), id)) {
2352  if (rb_const_defined_at(mod, id)) {
2353  rb_name_err_raise("cannot remove %2$s::%1$s",
2354  mod, ID2SYM(id));
2355  }
2356  rb_name_err_raise("constant %2$s::%1$s not defined",
2357  mod, ID2SYM(id));
2358  }
2359 
2361 
2362  val = ce->value;
2363  if (val == Qundef) {
2364  autoload_delete(mod, id);
2365  val = Qnil;
2366  }
2367  xfree(ce);
2368  return val;
2369 }
2370 
2371 static int
2372 cv_i_update(st_data_t *k, st_data_t *v, st_data_t a, int existing)
2373 {
2374  if (existing) return ST_STOP;
2375  *v = a;
2376  return ST_CONTINUE;
2377 }
2378 
2379 static enum rb_id_table_iterator_result
2380 sv_i(ID key, VALUE v, void *a)
2381 {
2383  st_table *tbl = a;
2384 
2385  if (rb_is_const_id(key)) {
2386  st_update(tbl, (st_data_t)key, cv_i_update, (st_data_t)ce);
2387  }
2388  return ID_TABLE_CONTINUE;
2389 }
2390 
2391 static enum rb_id_table_iterator_result
2392 rb_local_constants_i(ID const_name, VALUE const_value, void *ary)
2393 {
2394  if (rb_is_const_id(const_name) && !RB_CONST_PRIVATE_P((rb_const_entry_t *)const_value)) {
2395  rb_ary_push((VALUE)ary, ID2SYM(const_name));
2396  }
2397  return ID_TABLE_CONTINUE;
2398 }
2399 
2400 static VALUE
2401 rb_local_constants(VALUE mod)
2402 {
2403  struct rb_id_table *tbl = RCLASS_CONST_TBL(mod);
2404  VALUE ary;
2405 
2406  if (!tbl) return rb_ary_new2(0);
2407 
2408  ary = rb_ary_new2(rb_id_table_size(tbl));
2409  rb_id_table_foreach(tbl, rb_local_constants_i, (void *)ary);
2410  return ary;
2411 }
2412 
2413 void*
2414 rb_mod_const_at(VALUE mod, void *data)
2415 {
2416  st_table *tbl = data;
2417  if (!tbl) {
2418  tbl = st_init_numtable();
2419  }
2420  if (RCLASS_CONST_TBL(mod)) {
2421  rb_id_table_foreach(RCLASS_CONST_TBL(mod), sv_i, tbl);
2422  }
2423  return tbl;
2424 }
2425 
2426 void*
2427 rb_mod_const_of(VALUE mod, void *data)
2428 {
2429  VALUE tmp = mod;
2430  for (;;) {
2431  data = rb_mod_const_at(tmp, data);
2432  tmp = RCLASS_SUPER(tmp);
2433  if (!tmp) break;
2434  if (tmp == rb_cObject && mod != rb_cObject) break;
2435  }
2436  return data;
2437 }
2438 
2439 static int
2440 list_i(st_data_t key, st_data_t value, VALUE ary)
2441 {
2442  ID sym = (ID)key;
2443  rb_const_entry_t *ce = (rb_const_entry_t *)value;
2444  if (RB_CONST_PUBLIC_P(ce)) rb_ary_push(ary, ID2SYM(sym));
2445  return ST_CONTINUE;
2446 }
2447 
2448 VALUE
2449 rb_const_list(void *data)
2450 {
2451  st_table *tbl = data;
2452  VALUE ary;
2453 
2454  if (!tbl) return rb_ary_new2(0);
2455  ary = rb_ary_new2(tbl->num_entries);
2456  st_foreach_safe(tbl, list_i, ary);
2457  st_free_table(tbl);
2458 
2459  return ary;
2460 }
2461 
2462 /*
2463  * call-seq:
2464  * mod.constants(inherit=true) -> array
2465  *
2466  * Returns an array of the names of the constants accessible in
2467  * <i>mod</i>. This includes the names of constants in any included
2468  * modules (example at start of section), unless the <i>inherit</i>
2469  * parameter is set to <code>false</code>.
2470  *
2471  * The implementation makes no guarantees about the order in which the
2472  * constants are yielded.
2473  *
2474  * IO.constants.include?(:SYNC) #=> true
2475  * IO.constants(false).include?(:SYNC) #=> false
2476  *
2477  * Also see <code>Module::const_defined?</code>.
2478  */
2479 
2480 VALUE
2482 {
2483  VALUE inherit;
2484 
2485  if (argc == 0) {
2486  inherit = Qtrue;
2487  }
2488  else {
2489  rb_scan_args(argc, argv, "01", &inherit);
2490  }
2491 
2492  if (RTEST(inherit)) {
2493  return rb_const_list(rb_mod_const_of(mod, 0));
2494  }
2495  else {
2496  return rb_local_constants(mod);
2497  }
2498 }
2499 
2500 static int
2501 rb_const_defined_0(VALUE klass, ID id, int exclude, int recurse, int visibility)
2502 {
2503  VALUE tmp;
2504  int mod_retry = 0;
2505  rb_const_entry_t *ce;
2506 
2507  tmp = klass;
2508  retry:
2509  while (tmp) {
2510  if ((ce = rb_const_lookup(tmp, id))) {
2511  if (visibility && RB_CONST_PRIVATE_P(ce)) {
2512  return (int)Qfalse;
2513  }
2514  if (ce->value == Qundef && !check_autoload_required(tmp, id, 0) &&
2515  !rb_autoloading_value(tmp, id, 0))
2516  return (int)Qfalse;
2517  return (int)Qtrue;
2518  }
2519  if (!recurse) break;
2520  tmp = RCLASS_SUPER(tmp);
2521  }
2522  if (!exclude && !mod_retry && BUILTIN_TYPE(klass) == T_MODULE) {
2523  mod_retry = 1;
2524  tmp = rb_cObject;
2525  goto retry;
2526  }
2527  return (int)Qfalse;
2528 }
2529 
2530 int
2532 {
2533  return rb_const_defined_0(klass, id, TRUE, TRUE, FALSE);
2534 }
2535 
2536 int
2538 {
2539  return rb_const_defined_0(klass, id, FALSE, TRUE, FALSE);
2540 }
2541 
2542 int
2544 {
2545  return rb_const_defined_0(klass, id, TRUE, FALSE, FALSE);
2546 }
2547 
2548 int
2550 {
2551  return rb_const_defined_0(klass, id, TRUE, TRUE, TRUE);
2552 }
2553 
2554 int
2556 {
2557  return rb_const_defined_0(klass, id, FALSE, TRUE, TRUE);
2558 }
2559 
2560 int
2562 {
2563  return rb_const_defined_0(klass, id, TRUE, FALSE, TRUE);
2564 }
2565 
2566 static void
2567 check_before_mod_set(VALUE klass, ID id, VALUE val, const char *dest)
2568 {
2569  rb_check_frozen(klass);
2570 }
2571 
2572 void
2573 rb_const_set(VALUE klass, ID id, VALUE val)
2574 {
2575  rb_const_entry_t *ce;
2576  struct rb_id_table *tbl = RCLASS_CONST_TBL(klass);
2577 
2578  if (NIL_P(klass)) {
2579  rb_raise(rb_eTypeError, "no class/module to define constant %"PRIsVALUE"",
2580  QUOTE_ID(id));
2581  }
2582 
2583  check_before_mod_set(klass, id, val, "constant");
2584  if (!tbl) {
2585  RCLASS_CONST_TBL(klass) = tbl = rb_id_table_create(0);
2587  ce = ZALLOC(rb_const_entry_t);
2588  rb_id_table_insert(tbl, id, (VALUE)ce);
2589  setup_const_entry(ce, klass, val, CONST_PUBLIC);
2590  }
2591  else {
2592  struct autoload_const_set_args args;
2593  args.mod = klass;
2594  args.id = id;
2595  args.value = val;
2596  const_tbl_update(&args);
2597  }
2598  /*
2599  * Resolve and cache class name immediately to resolve ambiguity
2600  * and avoid order-dependency on const_tbl
2601  */
2602  if (rb_cObject && (RB_TYPE_P(val, T_MODULE) || RB_TYPE_P(val, T_CLASS))) {
2603  if (NIL_P(rb_class_path_cached(val))) {
2604  if (klass == rb_cObject) {
2605  rb_ivar_set(val, classpath, rb_id2str(id));
2606  rb_name_class(val, id);
2607  }
2608  else {
2609  VALUE path;
2610  ID pathid;
2611  st_data_t n;
2612  st_table *ivtbl = RCLASS_IV_TBL(klass);
2613  if (ivtbl &&
2614  (st_lookup(ivtbl, (st_data_t)(pathid = classpath), &n) ||
2615  st_lookup(ivtbl, (st_data_t)(pathid = tmp_classpath), &n))) {
2616  path = rb_str_dup((VALUE)n);
2617  rb_str_append(rb_str_cat2(path, "::"), rb_id2str(id));
2618  OBJ_FREEZE(path);
2619  rb_ivar_set(val, pathid, path);
2620  rb_name_class(val, id);
2621  }
2622  }
2623  }
2624  }
2625 }
2626 
2627 static void
2628 const_tbl_update(struct autoload_const_set_args *args)
2629 {
2630  VALUE value;
2631  VALUE klass = args->mod;
2632  VALUE val = args->value;
2633  ID id = args->id;
2634  struct rb_id_table *tbl = RCLASS_CONST_TBL(klass);
2635  rb_const_flag_t visibility = CONST_PUBLIC;
2636  rb_const_entry_t *ce;
2637 
2638  if (rb_id_table_lookup(tbl, id, &value)) {
2639  ce = (rb_const_entry_t *)value;
2640  if (ce->value == Qundef) {
2641  VALUE load;
2642  struct autoload_data_i *ele;
2643 
2644  load = autoload_data(klass, id);
2645  /* for autoloading thread, keep the defined value to autoloading storage */
2646  if (load && (ele = check_autoload_data(load)) && ele->state &&
2647  (ele->state->thread == rb_thread_current())) {
2649 
2650  ele->value = val; /* autoload_i is non-WB-protected */
2651  return;
2652  }
2653  /* otherwise, allow to override */
2654  autoload_delete(klass, id);
2655  }
2656  else {
2657  VALUE name = QUOTE_ID(id);
2658  visibility = ce->flag;
2659  if (klass == rb_cObject)
2660  rb_warn("already initialized constant %"PRIsVALUE"", name);
2661  else
2662  rb_warn("already initialized constant %"PRIsVALUE"::%"PRIsVALUE"",
2663  rb_class_name(klass), name);
2664  if (!NIL_P(ce->file) && ce->line) {
2666  "previous definition of %"PRIsVALUE" was here", name);
2667  }
2668  }
2670  setup_const_entry(ce, klass, val, visibility);
2671  }
2672  else {
2674 
2675  ce = ZALLOC(rb_const_entry_t);
2676  rb_id_table_insert(tbl, id, (VALUE)ce);
2677  setup_const_entry(ce, klass, val, visibility);
2678  }
2679 }
2680 
2681 static void
2682 setup_const_entry(rb_const_entry_t *ce, VALUE klass, VALUE val,
2683  rb_const_flag_t visibility)
2684 {
2685  ce->flag = visibility;
2686  RB_OBJ_WRITE(klass, &ce->value, val);
2687  RB_OBJ_WRITE(klass, &ce->file, rb_source_location(&ce->line));
2688 }
2689 
2690 void
2691 rb_define_const(VALUE klass, const char *name, VALUE val)
2692 {
2693  ID id = rb_intern(name);
2694 
2695  if (!rb_is_const_id(id)) {
2696  rb_warn("rb_define_const: invalid name `%s' for constant", name);
2697  }
2698  rb_const_set(klass, id, val);
2699 }
2700 
2701 void
2702 rb_define_global_const(const char *name, VALUE val)
2703 {
2704  rb_define_const(rb_cObject, name, val);
2705 }
2706 
2707 static void
2708 set_const_visibility(VALUE mod, int argc, const VALUE *argv,
2709  rb_const_flag_t flag, rb_const_flag_t mask)
2710 {
2711  int i;
2712  rb_const_entry_t *ce;
2713  ID id;
2714 
2715  rb_frozen_class_p(mod);
2716  if (argc == 0) {
2717  rb_warning("%"PRIsVALUE" with no argument is just ignored",
2719  return;
2720  }
2721 
2722  for (i = 0; i < argc; i++) {
2723  VALUE val = argv[i];
2724  id = rb_check_id(&val);
2725  if (!id) {
2726  if (i > 0) {
2728  }
2729 
2730  rb_name_err_raise("constant %2$s::%1$s not defined",
2731  mod, val);
2732  }
2733  if ((ce = rb_const_lookup(mod, id))) {
2734  ce->flag &= ~mask;
2735  ce->flag |= flag;
2736  }
2737  else {
2738  if (i > 0) {
2740  }
2741  rb_name_err_raise("constant %2$s::%1$s not defined",
2742  mod, ID2SYM(id));
2743  }
2744  }
2746 }
2747 
2748 void
2749 rb_deprecate_constant(VALUE mod, const char *name)
2750 {
2751  rb_const_entry_t *ce;
2752  ID id;
2753  long len = strlen(name);
2754 
2755  rb_frozen_class_p(mod);
2756  if (!(id = rb_check_id_cstr(name, len, NULL)) ||
2757  !(ce = rb_const_lookup(mod, id))) {
2758  rb_name_err_raise("constant %2$s::%1$s not defined",
2759  mod, rb_fstring_new(name, len));
2760  }
2761  ce->flag |= CONST_DEPRECATED;
2762 }
2763 
2764 /*
2765  * call-seq:
2766  * mod.private_constant(symbol, ...) => mod
2767  *
2768  * Makes a list of existing constants private.
2769  */
2770 
2771 VALUE
2772 rb_mod_private_constant(int argc, const VALUE *argv, VALUE obj)
2773 {
2774  set_const_visibility(obj, argc, argv, CONST_PRIVATE, CONST_VISIBILITY_MASK);
2775  return obj;
2776 }
2777 
2778 /*
2779  * call-seq:
2780  * mod.public_constant(symbol, ...) => mod
2781  *
2782  * Makes a list of existing constants public.
2783  */
2784 
2785 VALUE
2786 rb_mod_public_constant(int argc, const VALUE *argv, VALUE obj)
2787 {
2788  set_const_visibility(obj, argc, argv, CONST_PUBLIC, CONST_VISIBILITY_MASK);
2789  return obj;
2790 }
2791 
2792 /*
2793  * call-seq:
2794  * mod.deprecate_constant(symbol, ...) => mod
2795  *
2796  * Makes a list of existing constants deprecated.
2797  */
2798 
2799 VALUE
2800 rb_mod_deprecate_constant(int argc, const VALUE *argv, VALUE obj)
2801 {
2802  set_const_visibility(obj, argc, argv, CONST_DEPRECATED, CONST_DEPRECATED);
2803  return obj;
2804 }
2805 
2806 static VALUE
2807 original_module(VALUE c)
2808 {
2809  if (RB_TYPE_P(c, T_ICLASS))
2810  return RBASIC(c)->klass;
2811  return c;
2812 }
2813 
2814 static int
2815 cvar_lookup_at(VALUE klass, ID id, st_data_t *v)
2816 {
2817  if (!RCLASS_IV_TBL(klass)) return 0;
2818  return st_lookup(RCLASS_IV_TBL(klass), (st_data_t)id, v);
2819 }
2820 
2821 static VALUE
2822 cvar_front_klass(VALUE klass)
2823 {
2824  if (FL_TEST(klass, FL_SINGLETON)) {
2825  VALUE obj = rb_ivar_get(klass, id__attached__);
2826  if (RB_TYPE_P(obj, T_MODULE) || RB_TYPE_P(obj, T_CLASS)) {
2827  return obj;
2828  }
2829  }
2830  return RCLASS_SUPER(klass);
2831 }
2832 
2833 #define CVAR_FOREACH_ANCESTORS(klass, v, r) \
2834  for (klass = cvar_front_klass(klass); klass; klass = RCLASS_SUPER(klass)) { \
2835  if (cvar_lookup_at(klass, id, (v))) { \
2836  r; \
2837  } \
2838  }
2839 
2840 #define CVAR_LOOKUP(v,r) do {\
2841  if (cvar_lookup_at(klass, id, (v))) {r;}\
2842  CVAR_FOREACH_ANCESTORS(klass, v, r);\
2843 } while(0)
2844 
2845 void
2846 rb_cvar_set(VALUE klass, ID id, VALUE val)
2847 {
2848  VALUE tmp, front = 0, target = 0;
2849 
2850  tmp = klass;
2851  CVAR_LOOKUP(0, {if (!front) front = klass; target = klass;});
2852  if (target) {
2853  if (front && target != front) {
2854  st_data_t did = id;
2855 
2856  if (RTEST(ruby_verbose)) {
2857  rb_warning("class variable %"PRIsVALUE" of %"PRIsVALUE" is overtaken by %"PRIsVALUE"",
2858  QUOTE_ID(id), rb_class_name(original_module(front)),
2859  rb_class_name(original_module(target)));
2860  }
2861  if (BUILTIN_TYPE(front) == T_CLASS) {
2862  st_delete(RCLASS_IV_TBL(front),&did,0);
2863  }
2864  }
2865  }
2866  else {
2867  target = tmp;
2868  }
2869 
2870  check_before_mod_set(target, id, val, "class variable");
2871  if (!RCLASS_IV_TBL(target)) {
2872  RCLASS_IV_TBL(target) = st_init_numtable();
2873  }
2874 
2875  rb_class_ivar_set(target, id, val);
2876 }
2877 
2878 VALUE
2880 {
2881  VALUE tmp, front = 0, target = 0;
2882  st_data_t value;
2883 
2884  tmp = klass;
2885  CVAR_LOOKUP(&value, {if (!front) front = klass; target = klass;});
2886  if (!target) {
2887  rb_name_err_raise("uninitialized class variable %1$s in %2$s",
2888  tmp, ID2SYM(id));
2889  }
2890  if (front && target != front) {
2891  st_data_t did = id;
2892 
2893  if (RTEST(ruby_verbose)) {
2894  rb_warning("class variable %"PRIsVALUE" of %"PRIsVALUE" is overtaken by %"PRIsVALUE"",
2895  QUOTE_ID(id), rb_class_name(original_module(front)),
2896  rb_class_name(original_module(target)));
2897  }
2898  if (BUILTIN_TYPE(front) == T_CLASS) {
2899  st_delete(RCLASS_IV_TBL(front),&did,0);
2900  }
2901  }
2902  return (VALUE)value;
2903 }
2904 
2905 VALUE
2907 {
2908  if (!klass) return Qfalse;
2909  CVAR_LOOKUP(0,return Qtrue);
2910  return Qfalse;
2911 }
2912 
2913 static ID
2914 cv_intern(VALUE klass, const char *name)
2915 {
2916  ID id = rb_intern(name);
2917  if (!rb_is_class_id(id)) {
2918  rb_name_err_raise("wrong class variable name %1$s",
2919  klass, rb_str_new_cstr(name));
2920  }
2921  return id;
2922 }
2923 
2924 void
2925 rb_cv_set(VALUE klass, const char *name, VALUE val)
2926 {
2927  ID id = cv_intern(klass, name);
2928  rb_cvar_set(klass, id, val);
2929 }
2930 
2931 VALUE
2932 rb_cv_get(VALUE klass, const char *name)
2933 {
2934  ID id = cv_intern(klass, name);
2935  return rb_cvar_get(klass, id);
2936 }
2937 
2938 void
2939 rb_define_class_variable(VALUE klass, const char *name, VALUE val)
2940 {
2941  ID id = cv_intern(klass, name);
2942  rb_cvar_set(klass, id, val);
2943 }
2944 
2945 static int
2946 cv_i(st_data_t k, st_data_t v, st_data_t a)
2947 {
2948  ID key = (ID)k;
2949  st_table *tbl = (st_table *)a;
2950 
2951  if (rb_is_class_id(key)) {
2952  st_update(tbl, (st_data_t)key, cv_i_update, 0);
2953  }
2954  return ST_CONTINUE;
2955 }
2956 
2957 static void*
2958 mod_cvar_at(VALUE mod, void *data)
2959 {
2960  st_table *tbl = data;
2961  if (!tbl) {
2962  tbl = st_init_numtable();
2963  }
2964  if (RCLASS_IV_TBL(mod)) {
2965  st_foreach_safe(RCLASS_IV_TBL(mod), cv_i, (st_data_t)tbl);
2966  }
2967  return tbl;
2968 }
2969 
2970 static void*
2971 mod_cvar_of(VALUE mod, void *data)
2972 {
2973  VALUE tmp = mod;
2974  for (;;) {
2975  data = mod_cvar_at(tmp, data);
2976  tmp = RCLASS_SUPER(tmp);
2977  if (!tmp) break;
2978  }
2979  return data;
2980 }
2981 
2982 static int
2983 cv_list_i(st_data_t key, st_data_t value, VALUE ary)
2984 {
2985  ID sym = (ID)key;
2986  rb_ary_push(ary, ID2SYM(sym));
2987  return ST_CONTINUE;
2988 }
2989 
2990 static VALUE
2991 cvar_list(void *data)
2992 {
2993  st_table *tbl = data;
2994  VALUE ary;
2995 
2996  if (!tbl) return rb_ary_new2(0);
2997  ary = rb_ary_new2(tbl->num_entries);
2998  st_foreach_safe(tbl, cv_list_i, ary);
2999  st_free_table(tbl);
3000 
3001  return ary;
3002 }
3003 
3004 /*
3005  * call-seq:
3006  * mod.class_variables(inherit=true) -> array
3007  *
3008  * Returns an array of the names of class variables in <i>mod</i>.
3009  * This includes the names of class variables in any included
3010  * modules, unless the <i>inherit</i> parameter is set to
3011  * <code>false</code>.
3012  *
3013  * class One
3014  * @@var1 = 1
3015  * end
3016  * class Two < One
3017  * @@var2 = 2
3018  * end
3019  * One.class_variables #=> [:@@var1]
3020  * Two.class_variables #=> [:@@var2, :@@var1]
3021  * Two.class_variables(false) #=> [:@@var2]
3022  */
3023 
3024 VALUE
3025 rb_mod_class_variables(int argc, const VALUE *argv, VALUE mod)
3026 {
3027  VALUE inherit;
3028  st_table *tbl;
3029 
3030  if (argc == 0) {
3031  inherit = Qtrue;
3032  }
3033  else {
3034  rb_scan_args(argc, argv, "01", &inherit);
3035  }
3036  if (RTEST(inherit)) {
3037  tbl = mod_cvar_of(mod, 0);
3038  }
3039  else {
3040  tbl = mod_cvar_at(mod, 0);
3041  }
3042  return cvar_list(tbl);
3043 }
3044 
3045 /*
3046  * call-seq:
3047  * remove_class_variable(sym) -> obj
3048  *
3049  * Removes the definition of the <i>sym</i>, returning that
3050  * constant's value.
3051  *
3052  * class Dummy
3053  * @@var = 99
3054  * puts @@var
3055  * remove_class_variable(:@@var)
3056  * p(defined? @@var)
3057  * end
3058  *
3059  * <em>produces:</em>
3060  *
3061  * 99
3062  * nil
3063  */
3064 
3065 VALUE
3067 {
3068  const ID id = id_for_var_message(mod, name, class, "wrong class variable name %1$s");
3069  st_data_t val, n = id;
3070 
3071  if (!id) {
3072  not_defined:
3073  rb_name_err_raise("class variable %1$s not defined for %2$s",
3074  mod, name);
3075  }
3076  rb_check_frozen(mod);
3077  if (RCLASS_IV_TBL(mod) && st_delete(RCLASS_IV_TBL(mod), &n, &val)) {
3078  return (VALUE)val;
3079  }
3080  if (rb_cvar_defined(mod, id)) {
3081  rb_name_err_raise("cannot remove %1$s for %2$s", mod, ID2SYM(id));
3082  }
3083  goto not_defined;
3084 }
3085 
3086 VALUE
3087 rb_iv_get(VALUE obj, const char *name)
3088 {
3089  ID id = rb_intern(name);
3090 
3091  return rb_ivar_get(obj, id);
3092 }
3093 
3094 VALUE
3095 rb_iv_set(VALUE obj, const char *name, VALUE val)
3096 {
3097  ID id = rb_intern(name);
3098 
3099  return rb_ivar_set(obj, id, val);
3100 }
3101 
3102 /* tbl = xx(obj); tbl[key] = value; */
3103 int
3105 {
3106  st_table *tbl = RCLASS_IV_TBL(obj);
3107  int result = st_insert(tbl, (st_data_t)key, (st_data_t)value);
3108  RB_OBJ_WRITTEN(obj, Qundef, value);
3109  return result;
3110 }
3111 
3112 static int
3113 tbl_copy_i(st_data_t key, st_data_t value, st_data_t data)
3114 {
3115  RB_OBJ_WRITTEN((VALUE)data, Qundef, (VALUE)value);
3116  return ST_CONTINUE;
3117 }
3118 
3119 st_table *
3120 rb_st_copy(VALUE obj, struct st_table *orig_tbl)
3121 {
3122  st_table *new_tbl = st_copy(orig_tbl);
3123  st_foreach(new_tbl, tbl_copy_i, (st_data_t)obj);
3124  return new_tbl;
3125 }
3126 
3129 {
3130  struct rb_id_table *tbl = RCLASS_CONST_TBL(klass);
3131  VALUE val;
3132 
3133  if (tbl && rb_id_table_lookup(tbl, id, &val)) {
3134  return (rb_const_entry_t *)val;
3135  }
3136  return 0;
3137 }
union autoload_state::@124 waitq
st_table * rb_st_copy(VALUE obj, struct st_table *orig_tbl)
Definition: variable.c:3120
void rb_mark_generic_ivar(VALUE obj)
Definition: variable.c:1123
void rb_define_readonly_variable(const char *name, const VALUE *var)
Definition: variable.c:642
void rb_define_hooked_variable(const char *name, VALUE *var, VALUE(*getter)(ANYARGS), void(*setter)(ANYARGS))
Definition: variable.c:617
#define T_OBJECT
Definition: ruby.h:491
void rb_set_class_path_string(VALUE klass, VALUE under, VALUE name)
Definition: variable.c:344
VALUE rb_gvar_defined(struct rb_global_entry *entry)
Definition: variable.c:860
ID rb_check_id(volatile VALUE *)
Returns ID for the given name if it is interned already, or 0.
Definition: symbol.c:915
int rb_is_instance_id(ID id)
Definition: symbol.c:838
void rb_warn(const char *fmt,...)
Definition: error.c:246
#define FL_EXIVAR
Definition: ruby.h:1215
void rb_bug(const char *fmt,...)
Definition: error.c:521
void rb_vm_inc_const_missing_count(void)
Definition: vm.c:330
int rb_id_table_lookup(struct rb_id_table *tbl, ID id, VALUE *valp)
Definition: id_table.c:226
VALUE rb_eNameError
Definition: error.c:806
rb_const_entry_t * rb_const_lookup(VALUE klass, ID id)
Definition: variable.c:3128
#define FALSE
Definition: nkf.h:174
#define RUBY_TYPED_FREE_IMMEDIATELY
Definition: ruby.h:1138
size_t strlen(const char *)
VALUE rb_mod_const_missing(VALUE klass, VALUE name)
Definition: variable.c:1792
void * rb_mod_const_at(VALUE mod, void *data)
Definition: variable.c:2414
VALUE rb_gvar_undef_getter(ID id, void *data, struct rb_global_variable *var)
Definition: variable.c:510
#define RCLASS_CONST_TBL(c)
Definition: internal.h:790
Definition: constant.h:31
Definition: st.h:79
Definition: st.h:99
VALUE rb_f_global_variables(void)
Definition: variable.c:884
VALUE klass
Definition: variable.c:64
int count
Definition: encoding.c:56
ID rb_intern2(const char *, long)
Definition: symbol.c:604
#define RB_OBJ_WRITTEN(a, oldv, b)
Definition: ruby.h:1438
void rb_define_const(VALUE klass, const char *name, VALUE val)
Definition: variable.c:2691
st_data_t arg
Definition: variable.c:1443
void rb_define_variable(const char *name, VALUE *var)
Definition: variable.c:636
VALUE path
Definition: variable.c:65
#define QUOTE_ID(id)
Definition: internal.h:1636
void rb_thread_sleep_deadly(void)
Definition: thread.c:1167
#define FL_TAINT
Definition: ruby.h:1213
#define CLASS_OF(v)
Definition: ruby.h:453
VALUE rb_fstring_cstr(const char *str)
Definition: string.c:388
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2284
#define RB_CONST_DEPRECATED_P(ce)
Definition: constant.h:28
#define rb_name_err_raise_str(mesg, recv, name)
Definition: internal.h:1166
#define T_MODULE
Definition: ruby.h:494
const VALUE file
Definition: constant.h:35
#define st_foreach
Definition: regint.h:186
VALUE rb_ivar_defined(VALUE obj, ID id)
Definition: variable.c:1374
void rb_autoload_str(VALUE mod, ID id, VALUE file)
Definition: variable.c:1895
#define Qtrue
Definition: ruby.h:437
#define TypedData_Wrap_Struct(klass, data_type, sval)
Definition: ruby.h:1162
#define rb_id2str(id)
Definition: vm_backtrace.c:29
Definition: st.h:99
#define OBJ_FREEZE(x)
Definition: ruby.h:1306
const int id
Definition: nkf.c:209
ID rb_check_id_cstr(const char *ptr, long len, rb_encoding *enc)
Definition: symbol.c:984
int rb_is_const_id(ID id)
Definition: symbol.c:820
rb_gvar_getter_t * getter
Definition: variable.c:475
VALUE(* path_cache_func)(VALUE obj, VALUE name)
Definition: variable.c:257
void rb_autoload(VALUE mod, ID id, const char *file)
Definition: variable.c:1886
#define UNREACHABLE
Definition: ruby.h:46
VALUE rb_autoload_p(VALUE mod, ID id)
Definition: variable.c:2201
void rb_define_virtual_variable(const char *name, VALUE(*getter)(ANYARGS), void(*setter)(ANYARGS))
Definition: variable.c:648
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:924
VALUE rb_ivar_get(VALUE obj, ID id)
Definition: variable.c:1210
VALUE rb_const_list(void *data)
Definition: variable.c:2449
void rb_define_global_const(const char *name, VALUE val)
Definition: variable.c:2702
const char * rb_class2name(VALUE klass)
Definition: variable.c:450
const char * rb_sourcefile(void)
Definition: vm.c:1269
#define check_autoload_table(av)
Definition: variable.c:1825
VALUE rb_public_const_get_at(VALUE klass, ID id)
Definition: variable.c:2316
VALUE rb_mod_remove_const(VALUE mod, VALUE name)
Definition: variable.c:2332
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:774
void rb_gc_mark_global_tbl(void)
Definition: variable.c:594
#define ROBJECT_IV_INDEX_TBL(o)
Definition: ruby.h:908
VALUE rb_backref_get(void)
Definition: vm.c:1229
VALUE rb_mod_name(VALUE mod)
Definition: variable.c:229
#define Check_Type(v, t)
Definition: ruby.h:562
VALUE rb_cv_get(VALUE klass, const char *name)
Definition: variable.c:2932
VALUE rb_path2class(const char *path)
Definition: variable.c:432
rb_gvar_marker_t * marker
Definition: variable.c:477
#define RB_GC_GUARD(v)
Definition: ruby.h:552
void rb_clear_constant_cache(void)
Definition: vm_method.c:84
int rb_public_const_defined(VALUE klass, ID id)
Definition: variable.c:2555
const VALUE value
Definition: constant.h:34
st_table * iv_index_tbl
Definition: variable.c:41
#define DATA_PTR(dta)
Definition: ruby.h:1106
VALUE ivptr[1]
Definition: variable.c:36
void rb_gc_mark(VALUE ptr)
Definition: gc.c:4464
int rb_feature_provided(const char *, const char **)
Definition: load.c:528
#define FL_UNSET(x, f)
Definition: ruby.h:1290
st_data_t st_index_t
Definition: st.h:50
#define st_delete
Definition: regint.h:182
#define st_lookup
Definition: regint.h:185
int st_update(st_table *table, st_data_t key, st_update_callback_func *func, st_data_t arg)
Definition: st.c:1393
VALUE rb_gvar_get(struct rb_global_entry *entry)
Definition: variable.c:792
ID id
Definition: internal.h:1019
#define ROBJECT_NUMIV(o)
Definition: ruby.h:900
#define check_autoload_data(av)
Definition: variable.c:1882
VALUE rb_const_get(VALUE klass, ID id)
Definition: variable.c:2292
int rb_public_const_defined_at(VALUE klass, ID id)
Definition: variable.c:2561
VALUE rb_public_const_get(VALUE klass, ID id)
Definition: variable.c:2310
VALUE rb_ensure(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*e_proc)(ANYARGS), VALUE data2)
An equivalent to ensure clause.
Definition: eval.c:1035
struct rb_global_entry * rb_global_entry(ID id)
Definition: variable.c:482
void rb_name_error(ID id, const char *fmt,...)
Definition: error.c:1243
void rb_deprecate_constant(VALUE mod, const char *name)
Definition: variable.c:2749
#define FL_TEST(x, f)
Definition: ruby.h:1282
VALUE rb_f_untrace_var(int argc, const VALUE *argv)
Definition: variable.c:747
int(* func)(ID key, VALUE val, st_data_t arg)
Definition: variable.c:1442
int rb_match_count(VALUE match)
Definition: re.c:1258
#define rb_name_err_raise(mesg, recv, name)
Definition: internal.h:1168
#define rb_ary_new2
Definition: intern.h:90
RUBY_FUNC_EXPORTED size_t rb_generic_ivar_memsize(VALUE obj)
Definition: variable.c:1150
int line
Definition: constant.h:33
VALUE rb_eArgError
Definition: error.c:802
#define id_for_var(obj, name, part, type)
Definition: variable.c:1645
#define sym(x)
Definition: date_core.c:3721
RUBY_SYMBOL_EXPORT_BEGIN typedef unsigned long st_data_t
Definition: st.h:22
void(* func)(VALUE arg, VALUE val)
Definition: variable.c:466
VALUE rb_autoload_load(VALUE mod, ID id)
Definition: variable.c:2151
rb_const_flag_t
Definition: constant.h:14
#define CVAR_LOOKUP(v, r)
Definition: variable.c:2840
rb_const_flag_t flag
Definition: constant.h:32
#define FL_SINGLETON
Definition: ruby.h:1208
VALUE rb_obj_class(VALUE)
call-seq: obj.class -> class
Definition: object.c:277
#define RB_TYPE_P(obj, type)
Definition: ruby.h:527
VALUE rb_gvar_val_getter(ID id, void *data, struct rb_global_variable *var)
Definition: variable.c:533
VALUE rb_cvar_defined(VALUE klass, ID id)
Definition: variable.c:2906
void rb_const_set(VALUE klass, ID id, VALUE val)
Definition: variable.c:2573
#define id_for_var_message(obj, name, type, message)
Definition: variable.c:1647
st_data_t arg
Definition: variable.c:1407
VALUE rb_search_class_path(VALUE klass)
Definition: variable.c:337
VALUE rb_attr_delete(VALUE obj, ID id)
Definition: variable.c:1266
VALUE rb_class_path(VALUE klass)
Definition: variable.c:295
VALUE rb_gvar_set(struct rb_global_entry *entry, VALUE val)
Definition: variable.c:825
#define ROBJECT_IVPTR(o)
Definition: ruby.h:904
#define ALLOC_N(type, n)
Definition: ruby.h:1587
struct fc_result * prev
Definition: variable.c:67
struct list_head head
Definition: variable.c:1851
#define val
Definition: internal.h:1017
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1893
int rb_is_class_id(ID id)
Definition: symbol.c:826
size_t st_memsize(const st_table *tab)
Definition: st.c:676
VALUE rb_str_cat2(VALUE, const char *)
#define FL_SET(x, f)
Definition: ruby.h:1288
VALUE rb_ary_new(void)
Definition: array.c:499
int iv_extended
Definition: variable.c:45
VALUE rb_thread_current(void)
Definition: thread.c:2494
ID preferred
Definition: variable.c:63
#define NIL_P(v)
Definition: ruby.h:451
struct gen_ivtbl * ivtbl
Definition: variable.c:42
void rb_frozen_class_p(VALUE klass)
Asserts that klass is not a frozen class.
Definition: eval.c:404
int rb_match_nth_defined(int nth, VALUE match)
Definition: re.c:1268
struct list_node node
Definition: variable.c:1850
struct rb_id_table * rb_id_table_create(size_t capa)
Definition: id_table.c:95
#define RCLASS_IV_TBL(c)
Definition: internal.h:789
void rb_compile_warn(const char *file, int line, const char *fmt,...)
Definition: error.c:200
int removed
Definition: variable.c:465
const char * rb_obj_classname(VALUE obj)
Definition: variable.c:459
int argc
Definition: ruby.c:187
#define Qfalse
Definition: ruby.h:436
int rb_class_ivar_set(VALUE obj, ID key, VALUE value)
Definition: variable.c:3104
#define ALLOCA_N(type, n)
Definition: ruby.h:1593
RUBY_EXTERN VALUE rb_cModule
Definition: ruby.h:1916
VALUE rb_gv_get(const char *name)
Definition: variable.c:851
void rb_cv_set(VALUE klass, const char *name, VALUE val)
Definition: variable.c:2925
#define RUBY_FUNC_EXPORTED
Definition: defines.h:263
#define MEMCPY(p1, p2, type, n)
Definition: ruby.h:1661
#define rb_str_new2
Definition: intern.h:835
int rb_const_defined(VALUE klass, ID id)
Definition: variable.c:2537
void rb_set_class_path(VALUE klass, VALUE under, const char *name)
Definition: variable.c:367
VALUE rb_ivar_set(VALUE obj, ID id, VALUE val)
Definition: variable.c:1315
VALUE rb_thread_wakeup_alive(VALUE)
Definition: thread.c:2379
int rb_public_const_defined_from(VALUE klass, ID id)
Definition: variable.c:2549
#define ALLOC(type)
Definition: ruby.h:1588
VALUE rb_gvar_getter_t(ID id, void *data, struct rb_global_variable *gvar)
Definition: ruby.h:1683
void rb_name_error_str(VALUE str, const char *fmt,...)
Definition: error.c:1258
VALUE rb_class_name(VALUE klass)
Definition: variable.c:444
VALUE rb_mod_class_variables(int argc, const VALUE *argv, VALUE mod)
Definition: variable.c:3025
void st_foreach_safe(st_table *table, int(*func)(ANYARGS), st_data_t a)
Definition: hash.c:316
VALUE rb_str_subseq(VALUE, long, long)
Definition: string.c:2406
#define ZALLOC(type)
Definition: ruby.h:1590
#define RSTRING_LEN(str)
Definition: ruby.h:971
int rb_id_table_insert(struct rb_id_table *tbl, ID id, VALUE val)
Definition: id_table.c:256
#define REALLOC_N(var, type, n)
Definition: ruby.h:1591
void rb_id_table_foreach_values(struct rb_id_table *tbl, rb_id_table_foreach_values_func_t *func, void *data)
Definition: id_table.c:289
#define TRUE
Definition: nkf.h:175
VALUE rb_gvar_var_getter(ID id, void *data, struct rb_global_variable *gvar)
Definition: variable.c:552
void rb_gvar_var_setter(VALUE val, ID id, void *data, struct rb_global_variable *g)
Definition: variable.c:560
int rb_const_defined_at(VALUE klass, ID id)
Definition: variable.c:2543
VALUE rb_sprintf(const char *format,...)
Definition: sprintf.c:1452
void rb_gvar_readonly_setter(VALUE v, ID id, void *d, struct rb_global_variable *g)
Definition: variable.c:572
void rb_gvar_val_marker(VALUE *var)
Definition: variable.c:545
ID name
Definition: variable.c:63
VALUE rb_attr_get(VALUE obj, ID id)
Definition: variable.c:1224
int rb_id_table_delete(struct rb_id_table *tbl, ID id)
Definition: id_table.c:262
VALUE rb_obj_remove_instance_variable(VALUE obj, VALUE name)
Definition: variable.c:1687
size_t rb_id_table_size(const struct rb_id_table *tbl)
Definition: id_table.c:117
void rb_gvar_val_setter(VALUE val, ID id, void *data, struct rb_global_variable *var)
Definition: variable.c:539
void Init_var_tables(void)
Definition: variable.c:49
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1908
VALUE rb_mod_deprecate_constant(int argc, const VALUE *argv, VALUE obj)
Definition: variable.c:2800
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4309
void rb_vm_pop_cfunc_frame(void)
Definition: vm.c:532
#define PRIsVALUE
Definition: ruby.h:135
VALUE rb_const_get_at(VALUE klass, ID id)
Definition: variable.c:2298
unsigned long ID
Definition: ruby.h:86
union ivar_update::@123 u
#define Qnil
Definition: ruby.h:438
struct trace_var * trace
Definition: variable.c:478
#define BUILTIN_TYPE(x)
Definition: ruby.h:518
int rb_autoloading_value(VALUE mod, ID id, VALUE *value)
Definition: variable.c:2015
unsigned long VALUE
Definition: ruby.h:85
VALUE rb_vm_top_self(void)
Definition: vm.c:3166
#define OBJ_TAINTED(x)
Definition: ruby.h:1296
#define RB_CONST_PRIVATE_P(ce)
Definition: constant.h:23
#define RBASIC(obj)
Definition: ruby.h:1197
VALUE rb_eSecurityError
Definition: error.c:810
VALUE rb_public_const_get_from(VALUE klass, ID id)
Definition: variable.c:2304
VALUE rb_eTypeError
Definition: error.c:801
void rb_mark_tbl(st_table *tbl)
Definition: gc.c:4302
VALUE val
Definition: variable.c:800
st_data_t index
Definition: variable.c:44
#define rb_ary_new3
Definition: intern.h:91
struct autoload_data_i * ele
Definition: variable.c:1844
#define rb_enc_asciicompat(enc)
Definition: encoding.h:239
VALUE rb_str_new_cstr(const char *)
Definition: string.c:771
void rb_const_warn_if_deprecated(const rb_const_entry_t *ce, VALUE klass, ID id)
Definition: variable.c:2216
VALUE rb_fstring(VALUE)
Definition: string.c:306
VALUE rb_str_dup(VALUE)
Definition: string.c:1488
void rb_define_class_variable(VALUE klass, const char *name, VALUE val)
Definition: variable.c:2939
VALUE rb_fstring_new(const char *ptr, long len)
Definition: string.c:374
struct rb_global_variable * var
Definition: internal.h:1018
#define ROBJECT(obj)
Definition: ruby.h:1198
#define rb_funcallv
Definition: console.c:21
unsigned int uint32_t
Definition: sha2.h:101
int rb_thread_to_be_killed(VALUE thread)
Definition: thread.c:2302
register unsigned int len
Definition: zonetab.h:51
void rb_set_safe_level_force(int)
Definition: safe.c:41
VALUE rb_obj_instance_variables(VALUE obj)
Definition: variable.c:1634
#define RSTRING_PTR(str)
Definition: ruby.h:975
VALUE track
Definition: variable.c:66
#define RB_OBJ_WRITE(a, slot, b)
Definition: eval_intern.h:175
VALUE rb_class_path_cached(VALUE klass)
Definition: variable.c:319
rb_encoding * rb_enc_get(VALUE obj)
Definition: encoding.c:860
st_index_t rb_ivar_count(VALUE obj)
Definition: variable.c:1566
#define RCLASS_SUPER(c)
Definition: classext.h:16
int rb_safe_level(void)
Definition: safe.c:35
void rb_name_class(VALUE klass, ID id)
Definition: variable.c:438
ID rb_frame_callee(void)
The name of the current method.
Definition: eval.c:1120
VALUE obj
Definition: variable.c:1476
VALUE rb_block_proc(void)
Definition: proc.c:780
#define xmalloc
Definition: defines.h:183
#define st_init_numtable
Definition: regint.h:178
#define ANYARGS
Definition: defines.h:173
VALUE rb_eRuntimeError
Definition: error.c:800
void rb_ivar_foreach(VALUE obj, int(*func)(ANYARGS), st_data_t arg)
Definition: variable.c:1544
VALUE rb_iv_set(VALUE obj, const char *name, VALUE val)
Definition: variable.c:3095
VALUE rb_iv_get(VALUE obj, const char *name)
Definition: variable.c:3087
VALUE rb_cvar_get(VALUE klass, ID id)
Definition: variable.c:2879
VALUE rb_gv_set(const char *name, VALUE val)
Definition: variable.c:842
VALUE rb_path_to_class(VALUE pathname)
Definition: variable.c:390
#define RB_CONST_PUBLIC_P(ce)
Definition: constant.h:25
#define RCLASS_IV_INDEX_TBL(c)
Definition: internal.h:793
VALUE rb_const_get_from(VALUE klass, ID id)
Definition: variable.c:2286
void rb_gvar_var_marker(VALUE *var)
Definition: variable.c:566
#define RTEST(v)
Definition: ruby.h:450
void rb_warning(const char *fmt,...)
Definition: error.c:267
void rb_gvar_marker_t(VALUE *var)
Definition: ruby.h:1685
#define T_STRING
Definition: ruby.h:496
struct rb_encoding_entry * list
Definition: encoding.c:55
void rb_alias_variable(ID name1, ID name2)
Definition: variable.c:912
#define st_add_direct
Definition: regint.h:187
#define TypedData_Make_Struct(klass, type, data_type, sval)
Definition: ruby.h:1175
rb_gvar_setter_t * setter
Definition: variable.c:476
VALUE data
Definition: variable.c:467
VALUE rb_const_missing(VALUE klass, VALUE name)
Definition: variable.c:1747
VALUE rb_eval_cmd(VALUE, VALUE, int)
Definition: vm_eval.c:1507
void rb_cvar_set(VALUE klass, ID id, VALUE val)
Definition: variable.c:2846
struct autoload_state * state
Definition: variable.c:1859
#define st_insert
Definition: regint.h:184
#define T_CLASS
Definition: ruby.h:492
VALUE rb_mod_remove_cvar(VALUE mod, VALUE name)
Definition: variable.c:3066
#define RB_DEBUG_COUNTER_INC(type)
struct gen_ivtbl * ivtbl
Definition: variable.c:1478
VALUE rb_mod_constants(int argc, const VALUE *argv, VALUE mod)
Definition: variable.c:2481
void rb_gc_mark_maybe(VALUE obj)
Definition: gc.c:4320
const char * name
Definition: nkf.c:208
#define xrealloc
Definition: defines.h:186
#define ID2SYM(x)
Definition: ruby.h:383
struct gen_ivtbl * ivtbl
Definition: variable.c:1441
VALUE rb_str_new_frozen(VALUE)
Definition: string.c:1158
VALUE rb_source_location(int *pline)
Definition: vm.c:1297
st_table * iv_index_tbl
Definition: variable.c:1477
struct rb_id_table * rb_global_tbl
Definition: variable.c:24
#define st_free_table
Definition: regint.h:188
#define QUOTE(str)
Definition: internal.h:1635
#define rb_check_frozen(obj)
Definition: intern.h:271
VALUE rb_mod_private_constant(int argc, const VALUE *argv, VALUE obj)
Definition: variable.c:2772
rb_id_table_iterator_result
Definition: id_table.h:8
#define RUBY_TYPED_DEFAULT_FREE
Definition: ruby.h:1134
VALUE rb_str_intern(VALUE)
Definition: symbol.c:661
#define rb_intern_const(str)
Definition: ruby.h:1777
VALUE rb_class_path_no_cache(VALUE klass)
Definition: variable.c:310
#define SPECIAL_CONST_P(x)
Definition: ruby.h:1242
struct trace_var * next
Definition: variable.c:468
void void xfree(void *)
void rb_id_table_foreach(struct rb_id_table *tbl, rb_id_table_foreach_func_t *func, void *data)
Definition: id_table.c:270
#define rb_intern(str)
int(* func)(ID key, VALUE val, st_data_t arg)
Definition: variable.c:1406
uint32_t numiv
Definition: variable.c:35
VALUE rb_mod_public_constant(int argc, const VALUE *argv, VALUE obj)
Definition: variable.c:2786
#define mod(x, y)
Definition: date_strftime.c:28
void rb_gvar_setter_t(VALUE val, ID id, void *data, struct rb_global_variable *gvar)
Definition: ruby.h:1684
#define st_copy
Definition: regint.h:190
#define NULL
Definition: _sdbm.c:102
#define Qundef
Definition: ruby.h:439
#define T_ICLASS
Definition: ruby.h:493
void * rb_mod_const_of(VALUE mod, void *data)
Definition: variable.c:2427
VALUE rb_class_real(VALUE cl)
Looks up the nearest ancestor of cl, skipping singleton classes or module inclusions.
Definition: object.c:251
VALUE rb_ivar_lookup(VALUE obj, ID id, VALUE undef)
Definition: variable.c:1175
VALUE rb_f_trace_var(int argc, const VALUE *argv)
Definition: variable.c:688
struct trace_var * trace
Definition: variable.c:799
st_index_t num_entries
Definition: st.h:86
#define ruby_verbose
Definition: ruby.h:1813
VALUE rb_str_append(VALUE, VALUE)
Definition: string.c:2900
VALUE rb_const_remove(VALUE mod, ID id)
Definition: variable.c:2344
ID rb_to_id(VALUE)
Definition: string.c:10496
void rb_free_generic_ivar(VALUE obj)
Definition: variable.c:1133
char ** argv
Definition: ruby.c:188
void rb_copy_generic_ivar(VALUE clone, VALUE obj)
Definition: variable.c:1502
void rb_gvar_undef_marker(VALUE *var)
Definition: variable.c:528
int rb_const_defined_from(VALUE klass, ID id)
Definition: variable.c:2531
void rb_gvar_undef_setter(VALUE val, ID id, void *d, struct rb_global_variable *var)
Definition: variable.c:518
NORETURN(static void uninitialized_constant(VALUE, VALUE))