1 #include "mruby.h"
2 #include "mruby/array.h"
3 #include "mruby/hash.h"
4 #include "mruby/variable.h"
5 #include "mruby/proc.h"
6 #include "mruby/class.h"
7 #include "mruby/string.h"
8
9 typedef enum {
10 NOEX_PUBLIC = 0x00,
11 NOEX_NOSUPER = 0x01,
12 NOEX_PRIVATE = 0x02,
13 NOEX_PROTECTED = 0x04,
14 NOEX_MASK = 0x06,
15 NOEX_BASIC = 0x08,
16 NOEX_UNDEF = NOEX_NOSUPER,
17 NOEX_MODFUNC = 0x12,
18 NOEX_SUPER = 0x20,
19 NOEX_VCALL = 0x40,
20 NOEX_RESPONDS = 0x80
21 } mrb_method_flag_t;
22
23 static mrb_value
mrb_f_nil(mrb_state * mrb,mrb_value cv)24 mrb_f_nil(mrb_state *mrb, mrb_value cv)
25 {
26 return mrb_nil_value();
27 }
28
29 /* 15.3.1.3.20 */
30 /*
31 * call-seq:
32 * obj.instance_variable_defined?(symbol) -> true or false
33 *
34 * Returns <code>true</code> if the given instance variable is
35 * defined in <i>obj</i>.
36 *
37 * class Fred
38 * def initialize(p1, p2)
39 * @a, @b = p1, p2
40 * end
41 * end
42 * fred = Fred.new('cat', 99)
43 * fred.instance_variable_defined?(:@a) #=> true
44 * fred.instance_variable_defined?("@b") #=> true
45 * fred.instance_variable_defined?("@c") #=> false
46 */
47 static mrb_value
mrb_obj_ivar_defined(mrb_state * mrb,mrb_value self)48 mrb_obj_ivar_defined(mrb_state *mrb, mrb_value self)
49 {
50 mrb_sym sym;
51
52 mrb_get_args(mrb, "n", &sym);
53 mrb_iv_name_sym_check(mrb, sym);
54 return mrb_bool_value(mrb_iv_defined(mrb, self, sym));
55 }
56
57 /* 15.3.1.3.21 */
58 /*
59 * call-seq:
60 * obj.instance_variable_get(symbol) -> obj
61 *
62 * Returns the value of the given instance variable, or nil if the
63 * instance variable is not set. The <code>@</code> part of the
64 * variable name should be included for regular instance
65 * variables. Throws a <code>NameError</code> exception if the
66 * supplied symbol is not valid as an instance variable name.
67 *
68 * class Fred
69 * def initialize(p1, p2)
70 * @a, @b = p1, p2
71 * end
72 * end
73 * fred = Fred.new('cat', 99)
74 * fred.instance_variable_get(:@a) #=> "cat"
75 * fred.instance_variable_get("@b") #=> 99
76 */
77 static mrb_value
mrb_obj_ivar_get(mrb_state * mrb,mrb_value self)78 mrb_obj_ivar_get(mrb_state *mrb, mrb_value self)
79 {
80 mrb_sym iv_name;
81
82 mrb_get_args(mrb, "n", &iv_name);
83 mrb_iv_name_sym_check(mrb, iv_name);
84 return mrb_iv_get(mrb, self, iv_name);
85 }
86
87 /* 15.3.1.3.22 */
88 /*
89 * call-seq:
90 * obj.instance_variable_set(symbol, obj) -> obj
91 *
92 * Sets the instance variable names by <i>symbol</i> to
93 * <i>object</i>, thereby frustrating the efforts of the class's
94 * author to attempt to provide proper encapsulation. The variable
95 * did not have to exist prior to this call.
96 *
97 * class Fred
98 * def initialize(p1, p2)
99 * @a, @b = p1, p2
100 * end
101 * end
102 * fred = Fred.new('cat', 99)
103 * fred.instance_variable_set(:@a, 'dog') #=> "dog"
104 * fred.instance_variable_set(:@c, 'cat') #=> "cat"
105 * fred.inspect #=> "#<Fred:0x401b3da8 @a=\"dog\", @b=99, @c=\"cat\">"
106 */
107 static mrb_value
mrb_obj_ivar_set(mrb_state * mrb,mrb_value self)108 mrb_obj_ivar_set(mrb_state *mrb, mrb_value self)
109 {
110 mrb_sym iv_name;
111 mrb_value val;
112
113 mrb_get_args(mrb, "no", &iv_name, &val);
114 mrb_iv_name_sym_check(mrb, iv_name);
115 mrb_iv_set(mrb, self, iv_name, val);
116 return val;
117 }
118
119 /* 15.3.1.2.7 */
120 /* 15.3.1.3.28 */
121 /*
122 * call-seq:
123 * local_variables -> array
124 *
125 * Returns the names of local variables in the current scope.
126 *
127 * [mruby limitation]
128 * If variable symbol information was stripped out from
129 * compiled binary files using `mruby-strip -l`, this
130 * method always returns an empty array.
131 */
132 static mrb_value
mrb_local_variables(mrb_state * mrb,mrb_value self)133 mrb_local_variables(mrb_state *mrb, mrb_value self)
134 {
135 struct RProc *proc;
136 mrb_irep *irep;
137 mrb_value vars;
138 size_t i;
139
140 proc = mrb->c->ci[-1].proc;
141
142 if (proc == NULL || MRB_PROC_CFUNC_P(proc)) {
143 return mrb_ary_new(mrb);
144 }
145 vars = mrb_hash_new(mrb);
146 while (proc) {
147 if (MRB_PROC_CFUNC_P(proc)) break;
148 irep = proc->body.irep;
149 if (irep->lv) {
150 for (i = 0; i + 1 < irep->nlocals; ++i) {
151 if (irep->lv[i].name) {
152 mrb_sym sym = irep->lv[i].name;
153 const char *name = mrb_sym_name(mrb, sym);
154 switch (name[0]) {
155 case '*': case '&':
156 break;
157 default:
158 mrb_hash_set(mrb, vars, mrb_symbol_value(sym), mrb_true_value());
159 break;
160 }
161 }
162 }
163 }
164 if (MRB_PROC_SCOPE_P(proc)) break;
165 proc = proc->upper;
166 }
167
168 return mrb_hash_keys(mrb, vars);
169 }
170
KHASH_DECLARE(st,mrb_sym,char,FALSE)171 KHASH_DECLARE(st, mrb_sym, char, FALSE)
172
173 static void
174 method_entry_loop(mrb_state *mrb, struct RClass *klass, khash_t(st) *set, khash_t(st) *undef)
175 {
176 khint_t i;
177
178 khash_t(mt) *h = klass->mt;
179 if (!h || kh_size(h) == 0) return;
180 for (i=0;i<kh_end(h);i++) {
181 if (kh_exist(h, i)) {
182 mrb_method_t m = kh_value(h, i);
183 if (MRB_METHOD_UNDEF_P(m)) {
184 if (undef) {
185 kh_put(st, mrb, undef, kh_key(h, i));
186 }
187 }
188 else if (undef == NULL ||
189 kh_get(st, mrb, undef, kh_key(h, i)) == kh_end(undef)) {
190 kh_put(st, mrb, set, kh_key(h, i));
191 }
192 }
193 }
194 }
195
196 static mrb_value
mrb_class_instance_method_list(mrb_state * mrb,mrb_bool recur,struct RClass * klass,int obj)197 mrb_class_instance_method_list(mrb_state *mrb, mrb_bool recur, struct RClass *klass, int obj)
198 {
199 khint_t i;
200 mrb_value ary;
201 mrb_bool prepended = FALSE;
202 struct RClass *oldklass;
203 khash_t(st) *set = kh_init(st, mrb);
204 khash_t(st) *undef = (recur ? kh_init(st, mrb) : NULL);
205
206 if (!recur && (klass->flags & MRB_FL_CLASS_IS_PREPENDED)) {
207 MRB_CLASS_ORIGIN(klass);
208 prepended = TRUE;
209 }
210
211 oldklass = 0;
212 while (klass && (klass != oldklass)) {
213 method_entry_loop(mrb, klass, set, undef);
214 if ((klass->tt == MRB_TT_ICLASS && !prepended) ||
215 (klass->tt == MRB_TT_SCLASS)) {
216 }
217 else {
218 if (!recur) break;
219 }
220 oldklass = klass;
221 klass = klass->super;
222 }
223
224 ary = mrb_ary_new_capa(mrb, kh_size(set));
225 for (i=0;i<kh_end(set);i++) {
226 if (kh_exist(set, i)) {
227 mrb_ary_push(mrb, ary, mrb_symbol_value(kh_key(set, i)));
228 }
229 }
230 kh_destroy(st, mrb, set);
231 if (undef) kh_destroy(st, mrb, undef);
232
233 return ary;
234 }
235
236 static mrb_value
mrb_obj_methods(mrb_state * mrb,mrb_bool recur,mrb_value obj,mrb_method_flag_t flag)237 mrb_obj_methods(mrb_state *mrb, mrb_bool recur, mrb_value obj, mrb_method_flag_t flag)
238 {
239 return mrb_class_instance_method_list(mrb, recur, mrb_class(mrb, obj), 0);
240 }
241 /* 15.3.1.3.31 */
242 /*
243 * call-seq:
244 * obj.methods -> array
245 *
246 * Returns a list of the names of methods publicly accessible in
247 * <i>obj</i>. This will include all the methods accessible in
248 * <i>obj</i>'s ancestors.
249 *
250 * class Klass
251 * def kMethod()
252 * end
253 * end
254 * k = Klass.new
255 * k.methods[0..9] #=> [:kMethod, :respond_to?, :nil?, :is_a?,
256 * # :class, :instance_variable_set,
257 * # :methods, :extend, :__send__, :instance_eval]
258 * k.methods.length #=> 42
259 */
260 static mrb_value
mrb_obj_methods_m(mrb_state * mrb,mrb_value self)261 mrb_obj_methods_m(mrb_state *mrb, mrb_value self)
262 {
263 mrb_bool recur = TRUE;
264 mrb_get_args(mrb, "|b", &recur);
265 return mrb_obj_methods(mrb, recur, self, (mrb_method_flag_t)0); /* everything but private */
266 }
267
268 /* 15.3.1.3.36 */
269 /*
270 * call-seq:
271 * obj.private_methods(all=true) -> array
272 *
273 * Returns the list of private methods accessible to <i>obj</i>. If
274 * the <i>all</i> parameter is set to <code>false</code>, only those methods
275 * in the receiver will be listed.
276 */
277 static mrb_value
mrb_obj_private_methods(mrb_state * mrb,mrb_value self)278 mrb_obj_private_methods(mrb_state *mrb, mrb_value self)
279 {
280 mrb_bool recur = TRUE;
281 mrb_get_args(mrb, "|b", &recur);
282 return mrb_obj_methods(mrb, recur, self, NOEX_PRIVATE); /* private attribute not define */
283 }
284
285 /* 15.3.1.3.37 */
286 /*
287 * call-seq:
288 * obj.protected_methods(all=true) -> array
289 *
290 * Returns the list of protected methods accessible to <i>obj</i>. If
291 * the <i>all</i> parameter is set to <code>false</code>, only those methods
292 * in the receiver will be listed.
293 */
294 static mrb_value
mrb_obj_protected_methods(mrb_state * mrb,mrb_value self)295 mrb_obj_protected_methods(mrb_state *mrb, mrb_value self)
296 {
297 mrb_bool recur = TRUE;
298 mrb_get_args(mrb, "|b", &recur);
299 return mrb_obj_methods(mrb, recur, self, NOEX_PROTECTED); /* protected attribute not define */
300 }
301
302 /* 15.3.1.3.38 */
303 /*
304 * call-seq:
305 * obj.public_methods(all=true) -> array
306 *
307 * Returns the list of public methods accessible to <i>obj</i>. If
308 * the <i>all</i> parameter is set to <code>false</code>, only those methods
309 * in the receiver will be listed.
310 */
311 static mrb_value
mrb_obj_public_methods(mrb_state * mrb,mrb_value self)312 mrb_obj_public_methods(mrb_state *mrb, mrb_value self)
313 {
314 mrb_bool recur = TRUE;
315 mrb_get_args(mrb, "|b", &recur);
316 return mrb_obj_methods(mrb, recur, self, NOEX_PUBLIC); /* public attribute not define */
317 }
318
319 static mrb_value
mrb_obj_singleton_methods(mrb_state * mrb,mrb_bool recur,mrb_value obj)320 mrb_obj_singleton_methods(mrb_state *mrb, mrb_bool recur, mrb_value obj)
321 {
322 khint_t i;
323 mrb_value ary;
324 struct RClass *klass;
325 khash_t(st) *set = kh_init(st, mrb);
326 khash_t(st) *undef = (recur ? kh_init(st, mrb) : NULL);
327
328 klass = mrb_class(mrb, obj);
329
330 if (klass && (klass->tt == MRB_TT_SCLASS)) {
331 method_entry_loop(mrb, klass, set, undef);
332 klass = klass->super;
333 }
334 if (recur) {
335 while (klass && ((klass->tt == MRB_TT_SCLASS) || (klass->tt == MRB_TT_ICLASS))) {
336 method_entry_loop(mrb, klass, set, undef);
337 klass = klass->super;
338 }
339 }
340
341 ary = mrb_ary_new(mrb);
342 for (i=0;i<kh_end(set);i++) {
343 if (kh_exist(set, i)) {
344 mrb_ary_push(mrb, ary, mrb_symbol_value(kh_key(set, i)));
345 }
346 }
347 kh_destroy(st, mrb, set);
348 if (undef) kh_destroy(st, mrb, undef);
349
350 return ary;
351 }
352
353 /* 15.3.1.3.45 */
354 /*
355 * call-seq:
356 * obj.singleton_methods(all=true) -> array
357 *
358 * Returns an array of the names of singleton methods for <i>obj</i>.
359 * If the optional <i>all</i> parameter is true, the list will include
360 * methods in modules included in <i>obj</i>.
361 * Only public and protected singleton methods are returned.
362 *
363 * module Other
364 * def three() end
365 * end
366 *
367 * class Single
368 * def Single.four() end
369 * end
370 *
371 * a = Single.new
372 *
373 * def a.one()
374 * end
375 *
376 * class << a
377 * include Other
378 * def two()
379 * end
380 * end
381 *
382 * Single.singleton_methods #=> [:four]
383 * a.singleton_methods(false) #=> [:two, :one]
384 * a.singleton_methods #=> [:two, :one, :three]
385 */
386 static mrb_value
mrb_obj_singleton_methods_m(mrb_state * mrb,mrb_value self)387 mrb_obj_singleton_methods_m(mrb_state *mrb, mrb_value self)
388 {
389 mrb_bool recur = TRUE;
390 mrb_get_args(mrb, "|b", &recur);
391 return mrb_obj_singleton_methods(mrb, recur, self);
392 }
393
394 static mrb_value
mod_define_singleton_method(mrb_state * mrb,mrb_value self)395 mod_define_singleton_method(mrb_state *mrb, mrb_value self)
396 {
397 struct RProc *p;
398 mrb_method_t m;
399 mrb_sym mid;
400 mrb_value blk = mrb_nil_value();
401
402 mrb_get_args(mrb, "n&!", &mid, &blk);
403 p = (struct RProc*)mrb_obj_alloc(mrb, MRB_TT_PROC, mrb->proc_class);
404 mrb_proc_copy(p, mrb_proc_ptr(blk));
405 p->flags |= MRB_PROC_STRICT;
406 MRB_METHOD_FROM_PROC(m, p);
407 mrb_define_method_raw(mrb, mrb_class_ptr(mrb_singleton_class(mrb, self)), mid, m);
408 return mrb_symbol_value(mid);
409 }
410
411 static mrb_bool
cv_name_p(mrb_state * mrb,const char * name,mrb_int len)412 cv_name_p(mrb_state *mrb, const char *name, mrb_int len)
413 {
414 return len > 2 && name[0] == '@' && name[1] == '@' &&
415 !ISDIGIT(name[2]) && mrb_ident_p(name+2, len-2);
416 }
417
418 static void
check_cv_name_sym(mrb_state * mrb,mrb_sym id)419 check_cv_name_sym(mrb_state *mrb, mrb_sym id)
420 {
421 mrb_int len;
422 const char *name = mrb_sym_name_len(mrb, id, &len);
423 if (!cv_name_p(mrb, name, len)) {
424 mrb_name_error(mrb, id, "'%n' is not allowed as a class variable name", id);
425 }
426 }
427
428 /* 15.2.2.4.39 */
429 /*
430 * call-seq:
431 * remove_class_variable(sym) -> obj
432 *
433 * Removes the definition of the <i>sym</i>, returning that
434 * constant's value.
435 *
436 * class Dummy
437 * @@var = 99
438 * puts @@var
439 * p class_variables
440 * remove_class_variable(:@@var)
441 * p class_variables
442 * end
443 *
444 * <em>produces:</em>
445 *
446 * 99
447 * [:@@var]
448 * []
449 */
450
451 static mrb_value
mrb_mod_remove_cvar(mrb_state * mrb,mrb_value mod)452 mrb_mod_remove_cvar(mrb_state *mrb, mrb_value mod)
453 {
454 mrb_value val;
455 mrb_sym id;
456
457 mrb_get_args(mrb, "n", &id);
458 check_cv_name_sym(mrb, id);
459
460 val = mrb_iv_remove(mrb, mod, id);
461 if (!mrb_undef_p(val)) return val;
462
463 if (mrb_cv_defined(mrb, mod, id)) {
464 mrb_name_error(mrb, id, "cannot remove %n for %v", id, mod);
465 }
466
467 mrb_name_error(mrb, id, "class variable %n not defined for %v", id, mod);
468
469 /* not reached */
470 return mrb_nil_value();
471 }
472
473 /* 15.2.2.4.16 */
474 /*
475 * call-seq:
476 * obj.class_variable_defined?(symbol) -> true or false
477 *
478 * Returns <code>true</code> if the given class variable is defined
479 * in <i>obj</i>.
480 *
481 * class Fred
482 * @@foo = 99
483 * end
484 * Fred.class_variable_defined?(:@@foo) #=> true
485 * Fred.class_variable_defined?(:@@bar) #=> false
486 */
487
488 static mrb_value
mrb_mod_cvar_defined(mrb_state * mrb,mrb_value mod)489 mrb_mod_cvar_defined(mrb_state *mrb, mrb_value mod)
490 {
491 mrb_sym id;
492
493 mrb_get_args(mrb, "n", &id);
494 check_cv_name_sym(mrb, id);
495 return mrb_bool_value(mrb_cv_defined(mrb, mod, id));
496 }
497
498 /* 15.2.2.4.17 */
499 /*
500 * call-seq:
501 * mod.class_variable_get(symbol) -> obj
502 *
503 * Returns the value of the given class variable (or throws a
504 * <code>NameError</code> exception). The <code>@@</code> part of the
505 * variable name should be included for regular class variables
506 *
507 * class Fred
508 * @@foo = 99
509 * end
510 * Fred.class_variable_get(:@@foo) #=> 99
511 */
512
513 static mrb_value
mrb_mod_cvar_get(mrb_state * mrb,mrb_value mod)514 mrb_mod_cvar_get(mrb_state *mrb, mrb_value mod)
515 {
516 mrb_sym id;
517
518 mrb_get_args(mrb, "n", &id);
519 check_cv_name_sym(mrb, id);
520 return mrb_cv_get(mrb, mod, id);
521 }
522
523 /* 15.2.2.4.18 */
524 /*
525 * call-seq:
526 * obj.class_variable_set(symbol, obj) -> obj
527 *
528 * Sets the class variable names by <i>symbol</i> to
529 * <i>object</i>.
530 *
531 * class Fred
532 * @@foo = 99
533 * def foo
534 * @@foo
535 * end
536 * end
537 * Fred.class_variable_set(:@@foo, 101) #=> 101
538 * Fred.new.foo #=> 101
539 */
540
541 static mrb_value
mrb_mod_cvar_set(mrb_state * mrb,mrb_value mod)542 mrb_mod_cvar_set(mrb_state *mrb, mrb_value mod)
543 {
544 mrb_value value;
545 mrb_sym id;
546
547 mrb_get_args(mrb, "no", &id, &value);
548 check_cv_name_sym(mrb, id);
549 mrb_cv_set(mrb, mod, id, value);
550 return value;
551 }
552
553 static mrb_value
mrb_mod_included_modules(mrb_state * mrb,mrb_value self)554 mrb_mod_included_modules(mrb_state *mrb, mrb_value self)
555 {
556 mrb_value result;
557 struct RClass *c = mrb_class_ptr(self);
558 struct RClass *origin = c;
559
560 MRB_CLASS_ORIGIN(origin);
561 result = mrb_ary_new(mrb);
562 while (c) {
563 if (c != origin && c->tt == MRB_TT_ICLASS) {
564 if (c->c->tt == MRB_TT_MODULE) {
565 mrb_ary_push(mrb, result, mrb_obj_value(c->c));
566 }
567 }
568 c = c->super;
569 }
570
571 return result;
572 }
573
574 /* 15.2.2.4.33 */
575 /*
576 * call-seq:
577 * mod.instance_methods(include_super=true) -> array
578 *
579 * Returns an array containing the names of the public and protected instance
580 * methods in the receiver. For a module, these are the public and protected methods;
581 * for a class, they are the instance (not singleton) methods. With no
582 * argument, or with an argument that is <code>false</code>, the
583 * instance methods in <i>mod</i> are returned, otherwise the methods
584 * in <i>mod</i> and <i>mod</i>'s superclasses are returned.
585 *
586 * module A
587 * def method1() end
588 * end
589 * class B
590 * def method2() end
591 * end
592 * class C < B
593 * def method3() end
594 * end
595 *
596 * A.instance_methods #=> [:method1]
597 * B.instance_methods(false) #=> [:method2]
598 * C.instance_methods(false) #=> [:method3]
599 * C.instance_methods(true).length #=> 43
600 */
601
602 static mrb_value
mrb_mod_instance_methods(mrb_state * mrb,mrb_value mod)603 mrb_mod_instance_methods(mrb_state *mrb, mrb_value mod)
604 {
605 struct RClass *c = mrb_class_ptr(mod);
606 mrb_bool recur = TRUE;
607 mrb_get_args(mrb, "|b", &recur);
608 return mrb_class_instance_method_list(mrb, recur, c, 0);
609 }
610
611 static void
remove_method(mrb_state * mrb,mrb_value mod,mrb_sym mid)612 remove_method(mrb_state *mrb, mrb_value mod, mrb_sym mid)
613 {
614 struct RClass *c = mrb_class_ptr(mod);
615 khash_t(mt) *h;
616 khiter_t k;
617
618 MRB_CLASS_ORIGIN(c);
619 h = c->mt;
620
621 if (h) {
622 k = kh_get(mt, mrb, h, mid);
623 if (k != kh_end(h)) {
624 kh_del(mt, mrb, h, k);
625 mrb_funcall(mrb, mod, "method_removed", 1, mrb_symbol_value(mid));
626 return;
627 }
628 }
629
630 mrb_name_error(mrb, mid, "method '%n' not defined in %v", mid, mod);
631 }
632
633 /* 15.2.2.4.41 */
634 /*
635 * call-seq:
636 * remove_method(symbol) -> self
637 *
638 * Removes the method identified by _symbol_ from the current
639 * class. For an example, see <code>Module.undef_method</code>.
640 */
641
642 static mrb_value
mrb_mod_remove_method(mrb_state * mrb,mrb_value mod)643 mrb_mod_remove_method(mrb_state *mrb, mrb_value mod)
644 {
645 mrb_int argc;
646 mrb_value *argv;
647
648 mrb_get_args(mrb, "*", &argv, &argc);
649 mrb_check_frozen(mrb, mrb_obj_ptr(mod));
650 while (argc--) {
651 remove_method(mrb, mod, mrb_obj_to_sym(mrb, *argv));
652 argv++;
653 }
654 return mod;
655 }
656
657 static mrb_value
mrb_mod_s_constants(mrb_state * mrb,mrb_value mod)658 mrb_mod_s_constants(mrb_state *mrb, mrb_value mod)
659 {
660 mrb_raise(mrb, E_NOTIMP_ERROR, "Module.constants not implemented");
661 return mrb_nil_value(); /* not reached */
662 }
663
664 static mrb_value
mrb_mod_s_nesting(mrb_state * mrb,mrb_value mod)665 mrb_mod_s_nesting(mrb_state *mrb, mrb_value mod)
666 {
667 struct RProc *proc;
668 mrb_value ary;
669 struct RClass *c = NULL;
670
671 ary = mrb_ary_new(mrb);
672 proc = mrb->c->ci[-1].proc; /* callee proc */
673 while (proc && !MRB_PROC_CFUNC_P(proc)) {
674 if (MRB_PROC_SCOPE_P(proc)) {
675 struct RClass *c2 = MRB_PROC_TARGET_CLASS(proc);
676
677 if (c2 != c) {
678 c = c2;
679 mrb_ary_push(mrb, ary, mrb_obj_value(c));
680 }
681 }
682 proc = proc->upper;
683 }
684 return ary;
685 }
686
687 void
mrb_mruby_metaprog_gem_init(mrb_state * mrb)688 mrb_mruby_metaprog_gem_init(mrb_state* mrb)
689 {
690 struct RClass *krn = mrb->kernel_module;
691 struct RClass *mod = mrb->module_class;
692
693 mrb_define_method(mrb, krn, "global_variables", mrb_f_global_variables, MRB_ARGS_NONE()); /* 15.3.1.3.14 (15.3.1.2.4) */
694 mrb_define_method(mrb, krn, "local_variables", mrb_local_variables, MRB_ARGS_NONE()); /* 15.3.1.3.28 (15.3.1.2.7) */
695
696 mrb_define_method(mrb, krn, "singleton_class", mrb_singleton_class, MRB_ARGS_NONE());
697 mrb_define_method(mrb, krn, "instance_variable_defined?", mrb_obj_ivar_defined, MRB_ARGS_REQ(1)); /* 15.3.1.3.20 */
698 mrb_define_method(mrb, krn, "instance_variable_get", mrb_obj_ivar_get, MRB_ARGS_REQ(1)); /* 15.3.1.3.21 */
699 mrb_define_method(mrb, krn, "instance_variable_set", mrb_obj_ivar_set, MRB_ARGS_REQ(2)); /* 15.3.1.3.22 */
700 mrb_define_method(mrb, krn, "instance_variables", mrb_obj_instance_variables, MRB_ARGS_NONE()); /* 15.3.1.3.23 */
701 mrb_define_method(mrb, krn, "methods", mrb_obj_methods_m, MRB_ARGS_OPT(1)); /* 15.3.1.3.31 */
702 mrb_define_method(mrb, krn, "private_methods", mrb_obj_private_methods, MRB_ARGS_OPT(1)); /* 15.3.1.3.36 */
703 mrb_define_method(mrb, krn, "protected_methods", mrb_obj_protected_methods, MRB_ARGS_OPT(1)); /* 15.3.1.3.37 */
704 mrb_define_method(mrb, krn, "public_methods", mrb_obj_public_methods, MRB_ARGS_OPT(1)); /* 15.3.1.3.38 */
705 mrb_define_method(mrb, krn, "singleton_methods", mrb_obj_singleton_methods_m, MRB_ARGS_OPT(1)); /* 15.3.1.3.45 */
706 mrb_define_method(mrb, krn, "define_singleton_method", mod_define_singleton_method, MRB_ARGS_REQ(1)|MRB_ARGS_BLOCK());
707 mrb_define_method(mrb, krn, "send", mrb_f_send, MRB_ARGS_REQ(1)|MRB_ARGS_REST()|MRB_ARGS_BLOCK()); /* 15.3.1.3.44 */
708
709 mrb_define_method(mrb, mod, "class_variables", mrb_mod_class_variables, MRB_ARGS_OPT(1)); /* 15.2.2.4.19 */
710 mrb_define_method(mrb, mod, "remove_class_variable", mrb_mod_remove_cvar, MRB_ARGS_REQ(1)); /* 15.2.2.4.39 */
711 mrb_define_method(mrb, mod, "class_variable_defined?", mrb_mod_cvar_defined, MRB_ARGS_REQ(1)); /* 15.2.2.4.16 */
712 mrb_define_method(mrb, mod, "class_variable_get", mrb_mod_cvar_get, MRB_ARGS_REQ(1)); /* 15.2.2.4.17 */
713 mrb_define_method(mrb, mod, "class_variable_set", mrb_mod_cvar_set, MRB_ARGS_REQ(2)); /* 15.2.2.4.18 */
714 mrb_define_method(mrb, mod, "included_modules", mrb_mod_included_modules, MRB_ARGS_NONE()); /* 15.2.2.4.30 */
715 mrb_define_method(mrb, mod, "instance_methods", mrb_mod_instance_methods, MRB_ARGS_ANY()); /* 15.2.2.4.33 */
716 mrb_define_method(mrb, mod, "remove_method", mrb_mod_remove_method, MRB_ARGS_ANY()); /* 15.2.2.4.41 */
717 mrb_define_method(mrb, mod, "method_removed", mrb_f_nil, MRB_ARGS_REQ(1));
718 mrb_define_method(mrb, mod, "constants", mrb_mod_constants, MRB_ARGS_OPT(1)); /* 15.2.2.4.24 */
719 mrb_define_class_method(mrb, mod, "constants", mrb_mod_s_constants, MRB_ARGS_ANY()); /* 15.2.2.3.1 */
720 mrb_define_class_method(mrb, mod, "nesting", mrb_mod_s_nesting, MRB_ARGS_NONE()); /* 15.2.2.3.2 */
721 }
722
723 void
mrb_mruby_metaprog_gem_final(mrb_state * mrb)724 mrb_mruby_metaprog_gem_final(mrb_state* mrb)
725 {
726 }
727