Debian Codegraph @

vendor/mruby-source/src/variable.c

raw
1 /*
2 ** variable.c - mruby variables
3 **
4 ** See Copyright Notice in mruby.h
5 */
6
7 #include <mruby.h>
8 #include <mruby/array.h>
9 #include <mruby/class.h>
10 #include <mruby/proc.h>
11 #include <mruby/string.h>
12 #include <mruby/variable.h>
13 #include <mruby/internal.h>
14 #include <mruby/presym.h>
15
16 /* Instance variable table structure */
17 typedef struct iv_tbl {
18 int size, alloc;
19 mrb_value *ptr;
20 } iv_tbl;
21
22 #define IV_EMPTY 0
23 #define IV_DELETED (1UL<<31)
24 #define IV_KEY_P(k) (((k)&~((uint32_t)IV_DELETED))!=0)
25
26 /* Creates the instance variable table. */
27 static iv_tbl*
28 iv_new(mrb_state *mrb)
29 {
30 iv_tbl *t;
31
32 t = (iv_tbl*)mrb_malloc(mrb, sizeof(iv_tbl));
33 t->size = 0;
34 t->alloc = 0;
35 t->ptr = NULL;
36
37 return t;
38 }
39
40 static void iv_put(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value val);
41
42 static void
43 iv_rehash(mrb_state *mrb, iv_tbl *t)
44 {
45 int old_alloc = t->alloc;
46 int new_alloc = old_alloc+4;
47 mrb_value *old_ptr = t->ptr;
48
49 khash_power2(new_alloc);
50
51 t->ptr = (mrb_value*)mrb_calloc(mrb, sizeof(mrb_value)+sizeof(mrb_sym), new_alloc);
52 t->size = 0;
53 t->alloc = new_alloc;
54 if (old_alloc == 0) return;
55
56 mrb_sym *keys = (mrb_sym*)&old_ptr[old_alloc];
57 mrb_value *vals = old_ptr;
58 for (int i = 0; i < old_alloc; i++) {
59 if (IV_KEY_P(keys[i])) {
60 iv_put(mrb, t, keys[i], vals[i]);
61 }
62 }
63 mrb_free(mrb, old_ptr);
64 }
65
66 /* Set the value for the symbol in the instance variable table. */
67 static void
68 iv_put(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value val)
69 {
70 int hash, pos, start, dpos = -1;
71
72 if (t->alloc == 0) {
73 iv_rehash(mrb, t);
74 }
75
76 mrb_sym *keys = (mrb_sym*)&t->ptr[t->alloc];
77 mrb_value *vals = t->ptr;
78 hash = kh_int_hash_func(mrb, sym);
79 start = pos = hash & (t->alloc-1);
80 for (;;) {
81 mrb_sym key = keys[pos];
82 if (key == sym) {
83 vals[pos] = val;
84 return;
85 }
86 else if (key == IV_EMPTY) {
87 t->size++;
88 keys[pos] = sym;
89 vals[pos] = val;
90 return;
91 }
92 else if (key == IV_DELETED && dpos < 0) {
93 dpos = pos;
94 }
95 pos = (pos+1) & (t->alloc-1);
96 if (pos == start) { /* not found */
97 if (dpos >= 0) {
98 t->size++;
99 keys[dpos] = sym;
100 vals[dpos] = val;
101 return;
102 }
103 /* no room */
104 iv_rehash(mrb, t);
105 keys = (mrb_sym*)&t->ptr[t->alloc];
106 vals = t->ptr;
107 start = pos = hash & (t->alloc-1);
108 }
109 }
110 }
111
112 /* Get a value for a symbol from the instance variable table. */
113 static int
114 iv_get(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value *vp)
115 {
116 int hash, pos, start;
117
118 if (t == NULL) return FALSE;
119 if (t->alloc == 0) return FALSE;
120 if (t->size == 0) return FALSE;
121
122 mrb_sym *keys = (mrb_sym*)&t->ptr[t->alloc];
123 mrb_value *vals = t->ptr;
124 hash = kh_int_hash_func(mrb, sym);
125 start = pos = hash & (t->alloc-1);
126 for (;;) {
127 mrb_sym key = keys[pos];
128 if (key == sym) {
129 if (vp) *vp = vals[pos];
130 return pos+1;
131 }
132 else if (key == IV_EMPTY) {
133 return 0;
134 }
135 pos = (pos+1) & (t->alloc-1);
136 if (pos == start) { /* not found */
137 return 0;
138 }
139 }
140 }
141
142 /* Deletes the value for the symbol from the instance variable table. */
143 static mrb_bool
144 iv_del(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value *vp)
145 {
146 int hash, pos, start;
147
148 if (t == NULL) return FALSE;
149 if (t->alloc == 0) return FALSE;
150 if (t->size == 0) return FALSE;
151
152 mrb_sym *keys = (mrb_sym*)&t->ptr[t->alloc];
153 mrb_value *vals = t->ptr;
154 hash = kh_int_hash_func(mrb, sym);
155 start = pos = hash & (t->alloc-1);
156 for (;;) {
157 mrb_sym key = keys[pos];
158 if (key == sym) {
159 if (vp) *vp = vals[pos];
160 t->size--;
161 keys[pos] = IV_DELETED;
162 return TRUE;
163 }
164 else if (key == IV_EMPTY) {
165 return FALSE;
166 }
167 pos = (pos+1) & (t->alloc-1);
168 if (pos == start) { /* not found */
169 return FALSE;
170 }
171 }
172 }
173
174 /* Iterates over the instance variable table. */
175 static void
176 iv_foreach(mrb_state *mrb, iv_tbl *t, mrb_iv_foreach_func *func, void *p)
177 {
178 if (t == NULL) return;
179 if (t->alloc == 0) return;
180 if (t->size == 0) return;
181
182 mrb_sym *keys = (mrb_sym*)&t->ptr[t->alloc];
183 mrb_value *vals = t->ptr;
184 for (int i=0; i<t->alloc; i++) {
185 if (IV_KEY_P(keys[i])) {
186 if ((*func)(mrb, keys[i], vals[i], p) != 0) {
187 return;
188 }
189 }
190 }
191 return;
192 }
193
194 /* Get the size of the instance variable table. */
195 /* Size is approximated by the allocated table size. */
196 static size_t
197 iv_size(mrb_state *mrb, iv_tbl *t)
198 {
199 if (t == NULL) return 0;
200 return (size_t)t->size;
201 }
202
203 /* Copy the instance variable table. */
204 static iv_tbl*
205 iv_copy(mrb_state *mrb, iv_tbl *t)
206 {
207 iv_tbl *t2;
208
209 if (t == NULL) return NULL;
210 if (t->alloc == 0) return NULL;
211 if (t->size == 0) return NULL;
212
213 mrb_sym *keys = (mrb_sym*)&t->ptr[t->alloc];
214 mrb_value *vals = t->ptr;
215 t2 = iv_new(mrb);
216 for (int i=0; i<t->alloc; i++) {
217 if (IV_KEY_P(keys[i])) {
218 iv_put(mrb, t2, keys[i], vals[i]);
219 }
220 }
221 return t2;
222 }
223
224 /* Free memory of the instance variable table. */
225 static void
226 iv_free(mrb_state *mrb, iv_tbl *t)
227 {
228 mrb_free(mrb, t->ptr);
229 mrb_free(mrb, t);
230 }
231
232 static int
233 iv_mark_i(mrb_state *mrb, mrb_sym sym, mrb_value v, void *p)
234 {
235 mrb_gc_mark_value(mrb, v);
236 return 0;
237 }
238
239 static void
240 mark_tbl(mrb_state *mrb, iv_tbl *t)
241 {
242 iv_foreach(mrb, t, iv_mark_i, 0);
243 }
244
245 void
246 mrb_gc_mark_gv(mrb_state *mrb)
247 {
248 mark_tbl(mrb, mrb->globals);
249 }
250
251 void
252 mrb_gc_free_gv(mrb_state *mrb)
253 {
254 if (mrb->globals)
255 iv_free(mrb, mrb->globals);
256 }
257
258 void
259 mrb_gc_mark_iv(mrb_state *mrb, struct RObject *obj)
260 {
261 mark_tbl(mrb, obj->iv);
262 }
263
264 size_t
265 mrb_gc_mark_iv_size(mrb_state *mrb, struct RObject *obj)
266 {
267 return iv_size(mrb, obj->iv);
268 }
269
270 void
271 mrb_gc_free_iv(mrb_state *mrb, struct RObject *obj)
272 {
273 if (obj->iv) {
274 iv_free(mrb, obj->iv);
275 }
276 }
277
278 mrb_value
279 mrb_vm_special_get(mrb_state *mrb, mrb_sym i)
280 {
281 return mrb_fixnum_value(0);
282 }
283
284 void
285 mrb_vm_special_set(mrb_state *mrb, mrb_sym i, mrb_value v)
286 {
287 }
288
289 static mrb_bool
290 obj_iv_p(mrb_value obj)
291 {
292 switch (mrb_type(obj)) {
293 case MRB_TT_OBJECT:
294 case MRB_TT_CLASS:
295 case MRB_TT_MODULE:
296 case MRB_TT_SCLASS:
297 case MRB_TT_HASH:
298 case MRB_TT_CDATA:
299 case MRB_TT_EXCEPTION:
300 return TRUE;
301 default:
302 return FALSE;
303 }
304 }
305
306 MRB_API mrb_value
307 mrb_obj_iv_get(mrb_state *mrb, struct RObject *obj, mrb_sym sym)
308 {
309 mrb_value v;
310
311 if (obj->iv && iv_get(mrb, obj->iv, sym, &v))
312 return v;
313 return mrb_nil_value();
314 }
315
316 MRB_API mrb_value
317 mrb_iv_get(mrb_state *mrb, mrb_value obj, mrb_sym sym)
318 {
319 if (obj_iv_p(obj)) {
320 return mrb_obj_iv_get(mrb, mrb_obj_ptr(obj), sym);
321 }
322 return mrb_nil_value();
323 }
324
325 static inline mrb_bool
326 namespace_p(enum mrb_vtype tt)
327 {
328 return tt == MRB_TT_CLASS || tt == MRB_TT_MODULE ? TRUE : FALSE;
329 }
330
331 static inline void
332 assign_class_name(mrb_state *mrb, struct RObject *obj, mrb_sym sym, mrb_value v)
333 {
334 if (namespace_p(mrb_type(v))) {
335 struct RObject *c = mrb_obj_ptr(v);
336 if (obj != c && ISUPPER(mrb_sym_name_len(mrb, sym, NULL)[0])) {
337 mrb_sym id_classname = MRB_SYM(__classname__);
338 mrb_value o = mrb_obj_iv_get(mrb, c, id_classname);
339
340 if (mrb_nil_p(o)) {
341 mrb_sym id_outer = MRB_SYM(__outer__);
342 o = mrb_obj_iv_get(mrb, c, id_outer);
343
344 if (mrb_nil_p(o)) {
345 if ((struct RClass*)obj == mrb->object_class) {
346 mrb_obj_iv_set_force(mrb, c, id_classname, mrb_symbol_value(sym));
347 }
348 else {
349 mrb_obj_iv_set_force(mrb, c, id_outer, mrb_obj_value(obj));
350 }
351 }
352 }
353 }
354 }
355 }
356
357 void
358 mrb_obj_iv_set_force(mrb_state *mrb, struct RObject *obj, mrb_sym sym, mrb_value v)
359 {
360 if (namespace_p(obj->tt)) {
361 assign_class_name(mrb, obj, sym, v);
362 }
363 if (!obj->iv) {
364 obj->iv = iv_new(mrb);
365 }
366 iv_put(mrb, obj->iv, sym, v);
367 mrb_field_write_barrier_value(mrb, (struct RBasic*)obj, v);
368 }
369
370 MRB_API void
371 mrb_obj_iv_set(mrb_state *mrb, struct RObject *obj, mrb_sym sym, mrb_value v)
372 {
373 mrb_check_frozen(mrb, obj);
374 mrb_obj_iv_set_force(mrb, obj, sym, v);
375 }
376
377 /* Iterates over the instance variable table. */
378 MRB_API void
379 mrb_iv_foreach(mrb_state *mrb, mrb_value obj, mrb_iv_foreach_func *func, void *p)
380 {
381 if (!obj_iv_p(obj)) return;
382 iv_foreach(mrb, mrb_obj_ptr(obj)->iv, func, p);
383 }
384
385 MRB_API void
386 mrb_iv_set(mrb_state *mrb, mrb_value obj, mrb_sym sym, mrb_value v)
387 {
388 if (obj_iv_p(obj)) {
389 mrb_obj_iv_set(mrb, mrb_obj_ptr(obj), sym, v);
390 }
391 else {
392 mrb_raise(mrb, E_ARGUMENT_ERROR, "cannot set instance variable");
393 }
394 }
395
396 MRB_API mrb_bool
397 mrb_obj_iv_defined(mrb_state *mrb, struct RObject *obj, mrb_sym sym)
398 {
399 iv_tbl *t;
400
401 t = obj->iv;
402 if (t && iv_get(mrb, t, sym, NULL)) return TRUE;
403 return FALSE;
404 }
405
406 MRB_API mrb_bool
407 mrb_iv_defined(mrb_state *mrb, mrb_value obj, mrb_sym sym)
408 {
409 if (!obj_iv_p(obj)) return FALSE;
410 return mrb_obj_iv_defined(mrb, mrb_obj_ptr(obj), sym);
411 }
412
413 MRB_API mrb_bool
414 mrb_iv_name_sym_p(mrb_state *mrb, mrb_sym iv_name)
415 {
416 const char *s;
417 mrb_int len;
418
419 s = mrb_sym_name_len(mrb, iv_name, &len);
420 if (len < 2) return FALSE;
421 if (s[0] != '@') return FALSE;
422 if (ISDIGIT(s[1])) return FALSE;
423 return mrb_ident_p(s+1, len-1);
424 }
425
426 MRB_API void
427 mrb_iv_name_sym_check(mrb_state *mrb, mrb_sym iv_name)
428 {
429 if (!mrb_iv_name_sym_p(mrb, iv_name)) {
430 mrb_name_error(mrb, iv_name, "'%n' is not allowed as an instance variable name", iv_name);
431 }
432 }
433
434 MRB_API void
435 mrb_iv_copy(mrb_state *mrb, mrb_value dest, mrb_value src)
436 {
437 struct RObject *d = mrb_obj_ptr(dest);
438 struct RObject *s = mrb_obj_ptr(src);
439
440 if (d->iv) {
441 iv_free(mrb, d->iv);
442 d->iv = 0;
443 }
444 if (s->iv) {
445 mrb_write_barrier(mrb, (struct RBasic*)d);
446 d->iv = iv_copy(mrb, s->iv);
447 }
448 }
449
450 static int
451 inspect_i(mrb_state *mrb, mrb_sym sym, mrb_value v, void *p)
452 {
453 mrb_value str = *(mrb_value*)p;
454 const char *s;
455 mrb_int len;
456 mrb_value ins;
457 char *sp = RSTRING_PTR(str);
458
459 /* need not to show internal data */
460 if (sp[0] == '-') { /* first element */
461 sp[0] = '#';
462 mrb_str_cat_lit(mrb, str, " ");
463 }
464 else {
465 mrb_str_cat_lit(mrb, str, ", ");
466 }
467 s = mrb_sym_name_len(mrb, sym, &len);
468 mrb_str_cat(mrb, str, s, len);
469 mrb_str_cat_lit(mrb, str, "=");
470 ins = mrb_inspect(mrb, v);
471 mrb_str_cat_str(mrb, str, ins);
472 return 0;
473 }
474
475 mrb_value
476 mrb_obj_iv_inspect(mrb_state *mrb, struct RObject *obj)
477 {
478 iv_tbl *t = obj->iv;
479 size_t len = iv_size(mrb, t);
480
481 if (len > 0) {
482 const char *cn = mrb_obj_classname(mrb, mrb_obj_value(obj));
483 mrb_value str = mrb_str_new_capa(mrb, 30);
484
485 mrb_str_cat_lit(mrb, str, "-<");
486 mrb_str_cat_cstr(mrb, str, cn);
487 mrb_str_cat_lit(mrb, str, ":");
488 mrb_str_cat_str(mrb, str, mrb_ptr_to_str(mrb, obj));
489
490 if (mrb_inspect_recursive_p(mrb, mrb_obj_value(obj))) {
491 mrb_str_cat_lit(mrb, str, " ...>");
492 return str;
493 }
494 iv_foreach(mrb, t, inspect_i, &str);
495 mrb_str_cat_lit(mrb, str, ">");
496 return str;
497 }
498 return mrb_any_to_s(mrb, mrb_obj_value(obj));
499 }
500
501 MRB_API mrb_value
502 mrb_iv_remove(mrb_state *mrb, mrb_value obj, mrb_sym sym)
503 {
504 if (obj_iv_p(obj)) {
505 struct RObject *o = mrb_obj_ptr(obj);
506 iv_tbl *t = o->iv;
507 mrb_value val;
508
509 mrb_check_frozen(mrb, o);
510 if (iv_del(mrb, t, sym, &val)) {
511 return val;
512 }
513 }
514 return mrb_undef_value();
515 }
516
517 static int
518 iv_i(mrb_state *mrb, mrb_sym sym, mrb_value v, void *p)
519 {
520 mrb_value ary;
521 const char* s;
522 mrb_int len;
523
524 ary = *(mrb_value*)p;
525 s = mrb_sym_name_len(mrb, sym, &len);
526 if (len > 1 && s[0] == '@' && s[1] != '@') {
527 mrb_ary_push(mrb, ary, mrb_symbol_value(sym));
528 }
529 return 0;
530 }
531
532 /* 15.3.1.3.23 */
533 /*
534 * call-seq:
535 * obj.instance_variables -> array
536 *
537 * Returns an array of instance variable names for the receiver. Note
538 * that simply defining an accessor does not create the corresponding
539 * instance variable.
540 *
541 * class Fred
542 * attr_accessor :a1
543 * def initialize
544 * @iv = 3
545 * end
546 * end
547 * Fred.new.instance_variables #=> [:@iv]
548 */
549 mrb_value
550 mrb_obj_instance_variables(mrb_state *mrb, mrb_value self)
551 {
552 mrb_value ary;
553
554 ary = mrb_ary_new(mrb);
555 if (obj_iv_p(self)) {
556 iv_foreach(mrb, mrb_obj_ptr(self)->iv, iv_i, &ary);
557 }
558 return ary;
559 }
560
561 static int
562 cv_i(mrb_state *mrb, mrb_sym sym, mrb_value v, void *p)
563 {
564 mrb_value ary;
565 const char* s;
566 mrb_int len;
567
568 ary = *(mrb_value*)p;
569 s = mrb_sym_name_len(mrb, sym, &len);
570 if (len > 2 && s[0] == '@' && s[1] == '@') {
571 mrb_ary_push(mrb, ary, mrb_symbol_value(sym));
572 }
573 return 0;
574 }
575
576 /* 15.2.2.4.19 */
577 /*
578 * call-seq:
579 * mod.class_variables(inherit=true) -> array
580 *
581 * Returns an array of the names of class variables in <i>mod</i>.
582 *
583 * class One
584 * @@var1 = 1
585 * end
586 * class Two < One
587 * @@var2 = 2
588 * end
589 * One.class_variables #=> [:@@var1]
590 * Two.class_variables #=> [:@@var2]
591 */
592 mrb_value
593 mrb_mod_class_variables(mrb_state *mrb, mrb_value mod)
594 {
595 mrb_value ary;
596 struct RClass *c;
597 mrb_bool inherit = TRUE;
598
599 mrb_get_args(mrb, "|b", &inherit);
600 ary = mrb_ary_new(mrb);
601 c = mrb_class_ptr(mod);
602 while (c) {
603 iv_foreach(mrb, c->iv, cv_i, &ary);
604 if (!inherit) break;
605 c = c->super;
606 }
607 return ary;
608 }
609
610 mrb_value
611 mrb_mod_cv_get(mrb_state *mrb, struct RClass *c, mrb_sym sym)
612 {
613 struct RClass * cls = c;
614 mrb_value v;
615 int given = FALSE;
616
617 while (c) {
618 if (c->iv && iv_get(mrb, c->iv, sym, &v)) {
619 given = TRUE;
620 }
621 c = c->super;
622 }
623 if (given) return v;
624 if (cls && cls->tt == MRB_TT_SCLASS) {
625 mrb_value klass;
626
627 klass = mrb_obj_iv_get(mrb, (struct RObject*)cls, MRB_SYM(__attached__));
628 c = mrb_class_ptr(klass);
629 if (c->tt == MRB_TT_CLASS || c->tt == MRB_TT_MODULE) {
630 given = FALSE;
631 while (c) {
632 if (c->iv && iv_get(mrb, c->iv, sym, &v)) {
633 given = TRUE;
634 }
635 c = c->super;
636 }
637 if (given) return v;
638 }
639 }
640 mrb_name_error(mrb, sym, "uninitialized class variable %n in %C", sym, cls);
641 /* not reached */
642 return mrb_nil_value();
643 }
644
645 MRB_API mrb_value
646 mrb_cv_get(mrb_state *mrb, mrb_value mod, mrb_sym sym)
647 {
648 return mrb_mod_cv_get(mrb, mrb_class_ptr(mod), sym);
649 }
650
651 MRB_API void
652 mrb_mod_cv_set(mrb_state *mrb, struct RClass *c, mrb_sym sym, mrb_value v)
653 {
654 struct RClass * cls = c;
655
656 while (c) {
657 iv_tbl *t = c->iv;
658 int pos = iv_get(mrb, t, sym, NULL);
659
660 if (pos) {
661 mrb_check_frozen(mrb, c);
662 t->ptr[pos-1] = v; /* iv_get returns pos+1 to put */
663 mrb_field_write_barrier_value(mrb, (struct RBasic*)c, v);
664 return;
665 }
666 c = c->super;
667 }
668
669 if (cls && cls->tt == MRB_TT_SCLASS) {
670 mrb_value klass;
671
672 klass = mrb_obj_iv_get(mrb, (struct RObject*)cls, MRB_SYM(__attached__));
673 switch (mrb_type(klass)) {
674 case MRB_TT_CLASS:
675 case MRB_TT_MODULE:
676 case MRB_TT_SCLASS:
677 c = mrb_class_ptr(klass);
678 break;
679 default:
680 c = cls;
681 break;
682 }
683 }
684 else{
685 c = cls;
686 }
687
688 mrb_check_frozen(mrb, c);
689 if (!c->iv) {
690 c->iv = iv_new(mrb);
691 }
692
693 iv_put(mrb, c->iv, sym, v);
694 mrb_field_write_barrier_value(mrb, (struct RBasic*)c, v);
695 }
696
697 MRB_API void
698 mrb_cv_set(mrb_state *mrb, mrb_value mod, mrb_sym sym, mrb_value v)
699 {
700 mrb_mod_cv_set(mrb, mrb_class_ptr(mod), sym, v);
701 }
702
703 mrb_bool
704 mrb_mod_cv_defined(mrb_state *mrb, struct RClass * c, mrb_sym sym)
705 {
706 while (c) {
707 iv_tbl *t = c->iv;
708 if (iv_get(mrb, t, sym, NULL)) return TRUE;
709 c = c->super;
710 }
711
712 return FALSE;
713 }
714
715 MRB_API mrb_bool
716 mrb_cv_defined(mrb_state *mrb, mrb_value mod, mrb_sym sym)
717 {
718 return mrb_mod_cv_defined(mrb, mrb_class_ptr(mod), sym);
719 }
720
721 mrb_value
722 mrb_vm_cv_get(mrb_state *mrb, mrb_sym sym)
723 {
724 struct RClass *c;
725
726 const struct RProc *p = mrb->c->ci->proc;
727
728 for (;;) {
729 c = MRB_PROC_TARGET_CLASS(p);
730 if (c && c->tt != MRB_TT_SCLASS) break;
731 p = p->upper;
732 }
733 return mrb_mod_cv_get(mrb, c, sym);
734 }
735
736 void
737 mrb_vm_cv_set(mrb_state *mrb, mrb_sym sym, mrb_value v)
738 {
739 struct RClass *c;
740 const struct RProc *p = mrb->c->ci->proc;
741
742 for (;;) {
743 c = MRB_PROC_TARGET_CLASS(p);
744 if (c && c->tt != MRB_TT_SCLASS) break;
745 p = p->upper;
746 }
747 mrb_mod_cv_set(mrb, c, sym, v);
748 }
749
750 static void
751 mod_const_check(mrb_state *mrb, mrb_value mod)
752 {
753 switch (mrb_type(mod)) {
754 case MRB_TT_CLASS:
755 case MRB_TT_MODULE:
756 case MRB_TT_SCLASS:
757 break;
758 default:
759 mrb_raise(mrb, E_TYPE_ERROR, "constant look-up for non class/module");
760 break;
761 }
762 }
763
764 static mrb_value
765 const_get(mrb_state *mrb, struct RClass *base, mrb_sym sym, mrb_bool skip)
766 {
767 struct RClass *c = base;
768 mrb_value v;
769 mrb_bool retry = FALSE;
770
771 /* if skip then skip the current class (already searched) */
772 if (skip) c = c->super;
773 L_RETRY:
774 while (c) {
775 if (!MRB_FLAG_TEST(c, MRB_FL_CLASS_IS_PREPENDED) && c->iv && iv_get(mrb, c->iv, sym, &v)) {
776 return v;
777 }
778 c = c->super;
779 if (!skip && c == mrb->object_class) break;
780 }
781 if (!retry && base->tt == MRB_TT_MODULE && skip) {
782 c = mrb->object_class;
783 retry = TRUE;
784 goto L_RETRY;
785 }
786 mrb_value mod = mrb_obj_value(base);
787 if (mrb_func_basic_p(mrb, mod, MRB_SYM(const_missing), mrb_mod_const_missing)) {
788 return mrb_const_missing(mrb, mod, sym);
789 }
790 mrb_value name = mrb_symbol_value(sym);
791 return mrb_funcall_argv(mrb, mod, MRB_SYM(const_missing), 1, &name);
792 }
793
794 MRB_API mrb_value
795 mrb_const_get(mrb_state *mrb, mrb_value mod, mrb_sym sym)
796 {
797 mod_const_check(mrb, mod);
798 return const_get(mrb, mrb_class_ptr(mod), sym, FALSE);
799 }
800
801 mrb_value
802 mrb_vm_const_get(mrb_state *mrb, mrb_sym sym)
803 {
804 struct RClass *c;
805 struct RClass *c2;
806 mrb_value v;
807 const struct RProc *proc = mrb->c->ci->proc;
808
809 c = MRB_PROC_TARGET_CLASS(proc);
810 if (!c) c = mrb->object_class;
811 if (iv_get(mrb, c->iv, sym, &v)) {
812 return v;
813 }
814 c2 = c;
815 while (c2 && c2->tt == MRB_TT_SCLASS) {
816 mrb_value klass;
817
818 if (!iv_get(mrb, c2->iv, MRB_SYM(__attached__), &klass)) {
819 c2 = NULL;
820 break;
821 }
822 c2 = mrb_class_ptr(klass);
823 }
824 if (c2 && (c2->tt == MRB_TT_CLASS || c2->tt == MRB_TT_MODULE)) c = c2;
825 proc = proc->upper;
826 while (proc) {
827 c2 = MRB_PROC_TARGET_CLASS(proc);
828 if (!c2) c2 = mrb->object_class;
829 if (c2 && iv_get(mrb, c2->iv, sym, &v)) {
830 return v;
831 }
832 proc = proc->upper;
833 }
834 return const_get(mrb, c, sym, TRUE);
835 }
836
837 MRB_API void
838 mrb_const_set(mrb_state *mrb, mrb_value mod, mrb_sym sym, mrb_value v)
839 {
840 mod_const_check(mrb, mod);
841 if (mrb_type(v) == MRB_TT_CLASS || mrb_type(v) == MRB_TT_MODULE) {
842 mrb_class_name_class(mrb, mrb_class_ptr(mod), mrb_class_ptr(v), sym);
843 }
844 mrb_iv_set(mrb, mod, sym, v);
845 }
846
847 void
848 mrb_vm_const_set(mrb_state *mrb, mrb_sym sym, mrb_value v)
849 {
850 struct RClass *c;
851
852 c = MRB_PROC_TARGET_CLASS(mrb->c->ci->proc);
853 if (!c) c = mrb->object_class;
854 mrb_obj_iv_set(mrb, (struct RObject*)c, sym, v);
855 }
856
857 MRB_API void
858 mrb_const_remove(mrb_state *mrb, mrb_value mod, mrb_sym sym)
859 {
860 mod_const_check(mrb, mod);
861 mrb_iv_remove(mrb, mod, sym);
862 }
863
864 MRB_API void
865 mrb_define_const_id(mrb_state *mrb, struct RClass *mod, mrb_sym name, mrb_value v)
866 {
867 mrb_obj_iv_set(mrb, (struct RObject*)mod, name, v);
868 }
869
870 MRB_API void
871 mrb_define_const(mrb_state *mrb, struct RClass *mod, const char *name, mrb_value v)
872 {
873 mrb_obj_iv_set(mrb, (struct RObject*)mod, mrb_intern_cstr(mrb, name), v);
874 }
875
876 MRB_API void
877 mrb_define_global_const(mrb_state *mrb, const char *name, mrb_value val)
878 {
879 mrb_define_const(mrb, mrb->object_class, name, val);
880 }
881
882 static int
883 const_i(mrb_state *mrb, mrb_sym sym, mrb_value v, void *p)
884 {
885 mrb_value ary;
886 const char* s;
887 mrb_int len;
888
889 ary = *(mrb_value*)p;
890 s = mrb_sym_name_len(mrb, sym, &len);
891 if (len >= 1 && ISUPPER(s[0])) {
892 mrb_int i, alen = RARRAY_LEN(ary);
893
894 for (i=0; i<alen; i++) {
895 if (mrb_symbol(RARRAY_PTR(ary)[i]) == sym)
896 break;
897 }
898 if (i==alen) {
899 mrb_ary_push(mrb, ary, mrb_symbol_value(sym));
900 }
901 }
902 return 0;
903 }
904
905 /* 15.2.2.4.24 */
906 /*
907 * call-seq:
908 * mod.constants -> array
909 *
910 * Returns an array of all names of constants defined in the receiver.
911 */
912 mrb_value
913 mrb_mod_constants(mrb_state *mrb, mrb_value mod)
914 {
915 mrb_value ary;
916 mrb_bool inherit = TRUE;
917 struct RClass *c = mrb_class_ptr(mod);
918
919 mrb_get_args(mrb, "|b", &inherit);
920 ary = mrb_ary_new(mrb);
921 while (c) {
922 iv_foreach(mrb, c->iv, const_i, &ary);
923 if (!inherit) break;
924 c = c->super;
925 if (c == mrb->object_class) break;
926 }
927 return ary;
928 }
929
930 MRB_API mrb_value
931 mrb_gv_get(mrb_state *mrb, mrb_sym sym)
932 {
933 mrb_value v;
934
935 if (iv_get(mrb, mrb->globals, sym, &v))
936 return v;
937 return mrb_nil_value();
938 }
939
940 MRB_API void
941 mrb_gv_set(mrb_state *mrb, mrb_sym sym, mrb_value v)
942 {
943 iv_tbl *t;
944
945 if (!mrb->globals) {
946 mrb->globals = iv_new(mrb);
947 }
948 t = mrb->globals;
949 iv_put(mrb, t, sym, v);
950 }
951
952 MRB_API void
953 mrb_gv_remove(mrb_state *mrb, mrb_sym sym)
954 {
955 iv_del(mrb, mrb->globals, sym, NULL);
956 }
957
958 static int
959 gv_i(mrb_state *mrb, mrb_sym sym, mrb_value v, void *p)
960 {
961 mrb_value ary;
962
963 ary = *(mrb_value*)p;
964 mrb_ary_push(mrb, ary, mrb_symbol_value(sym));
965 return 0;
966 }
967
968 /* 15.3.1.2.4 */
969 /* 15.3.1.3.14 */
970 /*
971 * call-seq:
972 * global_variables -> array
973 *
974 * Returns an array of the names of global variables.
975 *
976 * global_variables.grep /std/ #=> [:$stdin, :$stdout, :$stderr]
977 */
978 mrb_value
979 mrb_f_global_variables(mrb_state *mrb, mrb_value self)
980 {
981 iv_tbl *t = mrb->globals;
982 mrb_value ary = mrb_ary_new(mrb);
983
984 iv_foreach(mrb, t, gv_i, &ary);
985 return ary;
986 }
987
988 static mrb_bool
989 const_defined_0(mrb_state *mrb, mrb_value mod, mrb_sym id, mrb_bool exclude, mrb_bool recurse)
990 {
991 struct RClass *klass = mrb_class_ptr(mod);
992 struct RClass *tmp;
993 mrb_bool mod_retry = FALSE;
994
995 tmp = klass;
996 retry:
997 while (tmp) {
998 if (iv_get(mrb, tmp->iv, id, NULL)) {
999 return TRUE;
1000 }
1001 if (!recurse && (klass != mrb->object_class)) break;
1002 tmp = tmp->super;
1003 }
1004 if (!exclude && !mod_retry && (klass->tt == MRB_TT_MODULE)) {
1005 mod_retry = TRUE;
1006 tmp = mrb->object_class;
1007 goto retry;
1008 }
1009 return FALSE;
1010 }
1011
1012 MRB_API mrb_bool
1013 mrb_const_defined(mrb_state *mrb, mrb_value mod, mrb_sym id)
1014 {
1015 return const_defined_0(mrb, mod, id, TRUE, TRUE);
1016 }
1017
1018 MRB_API mrb_bool
1019 mrb_const_defined_at(mrb_state *mrb, mrb_value mod, mrb_sym id)
1020 {
1021 return const_defined_0(mrb, mod, id, TRUE, FALSE);
1022 }
1023
1024 MRB_API mrb_value
1025 mrb_attr_get(mrb_state *mrb, mrb_value obj, mrb_sym id)
1026 {
1027 return mrb_iv_get(mrb, obj, id);
1028 }
1029
1030 struct csym_arg {
1031 struct RClass *c;
1032 mrb_sym sym;
1033 };
1034
1035 static int
1036 csym_i(mrb_state *mrb, mrb_sym sym, mrb_value v, void *p)
1037 {
1038 struct csym_arg *a = (struct csym_arg*)p;
1039 struct RClass *c = a->c;
1040
1041 if (mrb_type(v) == c->tt && mrb_class_ptr(v) == c) {
1042 a->sym = sym;
1043 return 1; /* stop iteration */
1044 }
1045 return 0;
1046 }
1047
1048 static mrb_sym
1049 find_class_sym(mrb_state *mrb, struct RClass *outer, struct RClass *c)
1050 {
1051 struct csym_arg arg;
1052
1053 if (!outer) return 0;
1054 if (outer == c) return 0;
1055 arg.c = c;
1056 arg.sym = 0;
1057 iv_foreach(mrb, outer->iv, csym_i, &arg);
1058 return arg.sym;
1059 }
1060
1061 static struct RClass*
1062 outer_class(mrb_state *mrb, struct RClass *c)
1063 {
1064 mrb_value ov;
1065
1066 ov = mrb_obj_iv_get(mrb, (struct RObject*)c, MRB_SYM(__outer__));
1067 if (mrb_nil_p(ov)) return NULL;
1068 switch (mrb_type(ov)) {
1069 case MRB_TT_CLASS:
1070 case MRB_TT_MODULE:
1071 return mrb_class_ptr(ov);
1072 default:
1073 break;
1074 }
1075 return NULL;
1076 }
1077
1078 static mrb_bool
1079 detect_outer_loop(mrb_state *mrb, struct RClass *c)
1080 {
1081 struct RClass *t = c; /* tortoise */
1082 struct RClass *h = c; /* hare */
1083
1084 for (;;) {
1085 if (h == NULL) return FALSE;
1086 h = outer_class(mrb, h);
1087 if (h == NULL) return FALSE;
1088 h = outer_class(mrb, h);
1089 t = outer_class(mrb, t);
1090 if (t == h) return TRUE;
1091 }
1092 }
1093
1094 mrb_value
1095 mrb_class_find_path(mrb_state *mrb, struct RClass *c)
1096 {
1097 struct RClass *outer;
1098 mrb_value path;
1099 mrb_sym name;
1100 const char *str;
1101 mrb_int len;
1102
1103 if (detect_outer_loop(mrb, c)) return mrb_nil_value();
1104 outer = outer_class(mrb, c);
1105 if (outer == NULL) return mrb_nil_value();
1106 name = find_class_sym(mrb, outer, c);
1107 if (name == 0) return mrb_nil_value();
1108 path = mrb_str_new_capa(mrb, 40);
1109 str = mrb_class_name(mrb, outer);
1110 mrb_str_cat_cstr(mrb, path, str);
1111 mrb_str_cat_cstr(mrb, path, "::");
1112
1113 str = mrb_sym_name_len(mrb, name, &len);
1114 mrb_str_cat(mrb, path, str, len);
1115 if (RSTRING_PTR(path)[0] != '#') {
1116 iv_del(mrb, c->iv, MRB_SYM(__outer__), NULL);
1117 iv_put(mrb, c->iv, MRB_SYM(__classname__), path);
1118 mrb_field_write_barrier_value(mrb, (struct RBasic*)c, path);
1119 path = mrb_str_dup(mrb, path);
1120 }
1121 return path;
1122 }
1123
1124 size_t
1125 mrb_obj_iv_tbl_memsize(mrb_value obj)
1126 {
1127 iv_tbl *t = mrb_obj_ptr(obj)->iv;
1128 if (t == NULL) return 0;
1129 return sizeof(iv_tbl) + t->alloc*(sizeof(mrb_value)+sizeof(mrb_sym));
1130 }
1131
1132 #define identchar(c) (ISALNUM(c) || (c) == '_' || !ISASCII(c))
1133
1134 mrb_bool
1135 mrb_ident_p(const char *s, mrb_int len)
1136 {
1137 for (mrb_int i = 0; i < len; i++) {
1138 if (!identchar(s[i])) return FALSE;
1139 }
1140 return TRUE;
1141 }