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"
13 NOEX_PROTECTED = 0x04,
16 NOEX_UNDEF = NOEX_NOSUPER,
24 mrb_f_nil(mrb_state *mrb, mrb_value cv)
26 return mrb_nil_value();
32 * obj.instance_variable_defined?(symbol) -> true or false
34 * Returns <code>true</code> if the given instance variable is
35 * defined in <i>obj</i>.
38 * def initialize(p1, p2)
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
48 mrb_obj_ivar_defined(mrb_state *mrb, mrb_value self)
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));
60 * obj.instance_variable_get(symbol) -> obj
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.
69 * def initialize(p1, p2)
73 * fred = Fred.new('cat', 99)
74 * fred.instance_variable_get(:@a) #=> "cat"
75 * fred.instance_variable_get("@b") #=> 99
78 mrb_obj_ivar_get(mrb_state *mrb, mrb_value self)
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);
90 * obj.instance_variable_set(symbol, obj) -> obj
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.
98 * def initialize(p1, p2)
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\">"
108 mrb_obj_ivar_set(mrb_state *mrb, mrb_value self)
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);
122 * local_variables -> array
124 * Returns the names of local variables in the current scope.
127 * If variable symbol information was stripped out from
128 * compiled binary files using `mruby-strip -l`, this
129 * method always returns an empty array.
132 mrb_local_variables(mrb_state *mrb, mrb_value self)
139 proc = mrb->c->ci[-1].proc;
141 if (MRB_PROC_CFUNC_P(proc)) {
142 return mrb_ary_new(mrb);
144 vars = mrb_hash_new(mrb);
146 if (MRB_PROC_CFUNC_P(proc)) break;
147 irep = proc->body.irep;
148 if (!irep->lv) break;
149 for (i = 0; i + 1 < irep->nlocals; ++i) {
150 if (irep->lv[i].name) {
151 mrb_sym sym = irep->lv[i].name;
152 const char *name = mrb_sym2name(mrb, sym);
157 mrb_hash_set(mrb, vars, mrb_symbol_value(sym), mrb_true_value());
162 if (!MRB_PROC_ENV_P(proc)) break;
164 //if (MRB_PROC_SCOPE_P(proc)) break;
168 return mrb_hash_keys(mrb, vars);
171 KHASH_DECLARE(st, mrb_sym, char, FALSE)
174 method_entry_loop(mrb_state *mrb, struct RClass* klass, khash_t(st)* set)
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)) continue;
184 kh_put(st, mrb, set, kh_key(h, i));
190 mrb_class_instance_method_list(mrb_state *mrb, mrb_bool recur, struct RClass* klass, int obj)
194 mrb_bool prepended = FALSE;
195 struct RClass* oldklass;
196 khash_t(st)* set = kh_init(st, mrb);
198 if (!recur && (klass->flags & MRB_FL_CLASS_IS_PREPENDED)) {
199 MRB_CLASS_ORIGIN(klass);
204 while (klass && (klass != oldklass)) {
205 method_entry_loop(mrb, klass, set);
206 if ((klass->tt == MRB_TT_ICLASS && !prepended) ||
207 (klass->tt == MRB_TT_SCLASS)) {
213 klass = klass->super;
216 ary = mrb_ary_new_capa(mrb, kh_size(set));
217 for (i=0;i<kh_end(set);i++) {
218 if (kh_exist(set, i)) {
219 mrb_ary_push(mrb, ary, mrb_symbol_value(kh_key(set, i)));
222 kh_destroy(st, mrb, set);
228 mrb_obj_methods(mrb_state *mrb, mrb_bool recur, mrb_value obj, mrb_method_flag_t flag)
230 return mrb_class_instance_method_list(mrb, recur, mrb_class(mrb, obj), 0);
235 * obj.methods -> array
237 * Returns a list of the names of methods publicly accessible in
238 * <i>obj</i>. This will include all the methods accessible in
239 * <i>obj</i>'s ancestors.
246 * k.methods[0..9] #=> [:kMethod, :respond_to?, :nil?, :is_a?,
247 * # :class, :instance_variable_set,
248 * # :methods, :extend, :__send__, :instance_eval]
249 * k.methods.length #=> 42
252 mrb_obj_methods_m(mrb_state *mrb, mrb_value self)
254 mrb_bool recur = TRUE;
255 mrb_get_args(mrb, "|b", &recur);
256 return mrb_obj_methods(mrb, recur, self, (mrb_method_flag_t)0); /* everything but private */
262 * obj.private_methods(all=true) -> array
264 * Returns the list of private methods accessible to <i>obj</i>. If
265 * the <i>all</i> parameter is set to <code>false</code>, only those methods
266 * in the receiver will be listed.
269 mrb_obj_private_methods(mrb_state *mrb, mrb_value self)
271 mrb_bool recur = TRUE;
272 mrb_get_args(mrb, "|b", &recur);
273 return mrb_obj_methods(mrb, recur, self, NOEX_PRIVATE); /* private attribute not define */
279 * obj.protected_methods(all=true) -> array
281 * Returns the list of protected methods accessible to <i>obj</i>. If
282 * the <i>all</i> parameter is set to <code>false</code>, only those methods
283 * in the receiver will be listed.
286 mrb_obj_protected_methods(mrb_state *mrb, mrb_value self)
288 mrb_bool recur = TRUE;
289 mrb_get_args(mrb, "|b", &recur);
290 return mrb_obj_methods(mrb, recur, self, NOEX_PROTECTED); /* protected attribute not define */
296 * obj.public_methods(all=true) -> array
298 * Returns the list of public methods accessible to <i>obj</i>. If
299 * the <i>all</i> parameter is set to <code>false</code>, only those methods
300 * in the receiver will be listed.
303 mrb_obj_public_methods(mrb_state *mrb, mrb_value self)
305 mrb_bool recur = TRUE;
306 mrb_get_args(mrb, "|b", &recur);
307 return mrb_obj_methods(mrb, recur, self, NOEX_PUBLIC); /* public attribute not define */
311 mrb_obj_singleton_methods(mrb_state *mrb, mrb_bool recur, mrb_value obj)
315 struct RClass* klass;
316 khash_t(st)* set = kh_init(st, mrb);
318 klass = mrb_class(mrb, obj);
320 if (klass && (klass->tt == MRB_TT_SCLASS)) {
321 method_entry_loop(mrb, klass, set);
322 klass = klass->super;
325 while (klass && ((klass->tt == MRB_TT_SCLASS) || (klass->tt == MRB_TT_ICLASS))) {
326 method_entry_loop(mrb, klass, set);
327 klass = klass->super;
331 ary = mrb_ary_new(mrb);
332 for (i=0;i<kh_end(set);i++) {
333 if (kh_exist(set, i)) {
334 mrb_ary_push(mrb, ary, mrb_symbol_value(kh_key(set, i)));
337 kh_destroy(st, mrb, set);
345 * obj.singleton_methods(all=true) -> array
347 * Returns an array of the names of singleton methods for <i>obj</i>.
348 * If the optional <i>all</i> parameter is true, the list will include
349 * methods in modules included in <i>obj</i>.
350 * Only public and protected singleton methods are returned.
357 * def Single.four() end
371 * Single.singleton_methods #=> [:four]
372 * a.singleton_methods(false) #=> [:two, :one]
373 * a.singleton_methods #=> [:two, :one, :three]
376 mrb_obj_singleton_methods_m(mrb_state *mrb, mrb_value self)
378 mrb_bool recur = TRUE;
379 mrb_get_args(mrb, "|b", &recur);
380 return mrb_obj_singleton_methods(mrb, recur, self);
384 mod_define_singleton_method(mrb_state *mrb, mrb_value self)
389 mrb_value blk = mrb_nil_value();
391 mrb_get_args(mrb, "n&", &mid, &blk);
392 if (mrb_nil_p(blk)) {
393 mrb_raise(mrb, E_ARGUMENT_ERROR, "no block given");
395 p = (struct RProc*)mrb_obj_alloc(mrb, MRB_TT_PROC, mrb->proc_class);
396 mrb_proc_copy(p, mrb_proc_ptr(blk));
397 p->flags |= MRB_PROC_STRICT;
398 MRB_METHOD_FROM_PROC(m, p);
399 mrb_define_method_raw(mrb, mrb_class_ptr(mrb_singleton_class(mrb, self)), mid, m);
400 return mrb_symbol_value(mid);
404 cv_name_p(mrb_state *mrb, const char *name, mrb_int len)
406 return len > 2 && name[0] == '@' && name[1] == '@' &&
407 !ISDIGIT(name[2]) && mrb_ident_p(name+2, len-2);
411 check_cv_name_sym(mrb_state *mrb, mrb_sym id)
414 const char *name = mrb_sym2name_len(mrb, id, &len);
415 if (!cv_name_p(mrb, name, len)) {
416 mrb_name_error(mrb, id, "'%S' is not allowed as a class variable name", mrb_sym2str(mrb, id));
423 * remove_class_variable(sym) -> obj
425 * Removes the definition of the <i>sym</i>, returning that
432 * remove_class_variable(:@@var)
444 mrb_mod_remove_cvar(mrb_state *mrb, mrb_value mod)
449 mrb_get_args(mrb, "n", &id);
450 check_cv_name_sym(mrb, id);
452 val = mrb_iv_remove(mrb, mod, id);
453 if (!mrb_undef_p(val)) return val;
455 if (mrb_cv_defined(mrb, mod, id)) {
456 mrb_name_error(mrb, id, "cannot remove %S for %S",
457 mrb_sym2str(mrb, id), mod);
460 mrb_name_error(mrb, id, "class variable %S not defined for %S",
461 mrb_sym2str(mrb, id), mod);
464 return mrb_nil_value();
470 * obj.class_variable_defined?(symbol) -> true or false
472 * Returns <code>true</code> if the given class variable is defined
478 * Fred.class_variable_defined?(:@@foo) #=> true
479 * Fred.class_variable_defined?(:@@bar) #=> false
483 mrb_mod_cvar_defined(mrb_state *mrb, mrb_value mod)
487 mrb_get_args(mrb, "n", &id);
488 check_cv_name_sym(mrb, id);
489 return mrb_bool_value(mrb_cv_defined(mrb, mod, id));
495 * mod.class_variable_get(symbol) -> obj
497 * Returns the value of the given class variable (or throws a
498 * <code>NameError</code> exception). The <code>@@</code> part of the
499 * variable name should be included for regular class variables
504 * Fred.class_variable_get(:@@foo) #=> 99
508 mrb_mod_cvar_get(mrb_state *mrb, mrb_value mod)
512 mrb_get_args(mrb, "n", &id);
513 check_cv_name_sym(mrb, id);
514 return mrb_cv_get(mrb, mod, id);
520 * obj.class_variable_set(symbol, obj) -> obj
522 * Sets the class variable names by <i>symbol</i> to
531 * Fred.class_variable_set(:@@foo, 101) #=> 101
532 * Fred.new.foo #=> 101
536 mrb_mod_cvar_set(mrb_state *mrb, mrb_value mod)
541 mrb_get_args(mrb, "no", &id, &value);
542 check_cv_name_sym(mrb, id);
543 mrb_cv_set(mrb, mod, id, value);
548 mrb_mod_included_modules(mrb_state *mrb, mrb_value self)
551 struct RClass *c = mrb_class_ptr(self);
552 struct RClass *origin = c;
554 MRB_CLASS_ORIGIN(origin);
555 result = mrb_ary_new(mrb);
557 if (c != origin && c->tt == MRB_TT_ICLASS) {
558 if (c->c->tt == MRB_TT_MODULE) {
559 mrb_ary_push(mrb, result, mrb_obj_value(c->c));
568 mrb_value mrb_class_instance_method_list(mrb_state*, mrb_bool, struct RClass*, int);
573 * mod.instance_methods(include_super=true) -> array
575 * Returns an array containing the names of the public and protected instance
576 * methods in the receiver. For a module, these are the public and protected methods;
577 * for a class, they are the instance (not singleton) methods. With no
578 * argument, or with an argument that is <code>false</code>, the
579 * instance methods in <i>mod</i> are returned, otherwise the methods
580 * in <i>mod</i> and <i>mod</i>'s superclasses are returned.
592 * A.instance_methods #=> [:method1]
593 * B.instance_methods(false) #=> [:method2]
594 * C.instance_methods(false) #=> [:method3]
595 * C.instance_methods(true).length #=> 43
599 mrb_mod_instance_methods(mrb_state *mrb, mrb_value mod)
601 struct RClass *c = mrb_class_ptr(mod);
602 mrb_bool recur = TRUE;
603 mrb_get_args(mrb, "|b", &recur);
604 return mrb_class_instance_method_list(mrb, recur, c, 0);
608 remove_method(mrb_state *mrb, mrb_value mod, mrb_sym mid)
610 struct RClass *c = mrb_class_ptr(mod);
618 k = kh_get(mt, mrb, h, mid);
619 if (k != kh_end(h)) {
620 kh_del(mt, mrb, h, k);
621 mrb_funcall(mrb, mod, "method_removed", 1, mrb_symbol_value(mid));
626 mrb_name_error(mrb, mid, "method '%S' not defined in %S",
627 mrb_sym2str(mrb, mid), mod);
633 * remove_method(symbol) -> self
635 * Removes the method identified by _symbol_ from the current
636 * class. For an example, see <code>Module.undef_method</code>.
640 mrb_mod_remove_method(mrb_state *mrb, mrb_value mod)
645 mrb_get_args(mrb, "*", &argv, &argc);
647 remove_method(mrb, mod, mrb_obj_to_sym(mrb, *argv));
654 mrb_mod_s_constants(mrb_state *mrb, mrb_value mod)
656 mrb_raise(mrb, E_NOTIMP_ERROR, "Module.constants not implemented");
657 return mrb_nil_value(); /* not reached */
660 /* implementation of Module.nesting */
661 mrb_value mrb_mod_s_nesting(mrb_state*, mrb_value);
664 mrb_mruby_metaprog_gem_init(mrb_state* mrb)
666 struct RClass *krn = mrb->kernel_module;
667 struct RClass *mod = mrb->module_class;
669 mrb_define_method(mrb, krn, "global_variables", mrb_f_global_variables, MRB_ARGS_NONE()); /* 15.3.1.2.4 */
670 mrb_define_method(mrb, krn, "local_variables", mrb_local_variables, MRB_ARGS_NONE()); /* 15.3.1.3.28 */
672 mrb_define_method(mrb, krn, "singleton_class", mrb_singleton_class, MRB_ARGS_NONE());
673 mrb_define_method(mrb, krn, "instance_variable_defined?", mrb_obj_ivar_defined, MRB_ARGS_REQ(1)); /* 15.3.1.3.20 */
674 mrb_define_method(mrb, krn, "instance_variable_get", mrb_obj_ivar_get, MRB_ARGS_REQ(1)); /* 15.3.1.3.21 */
675 mrb_define_method(mrb, krn, "instance_variable_set", mrb_obj_ivar_set, MRB_ARGS_REQ(2)); /* 15.3.1.3.22 */
676 mrb_define_method(mrb, krn, "instance_variables", mrb_obj_instance_variables, MRB_ARGS_NONE()); /* 15.3.1.3.23 */
677 mrb_define_method(mrb, krn, "methods", mrb_obj_methods_m, MRB_ARGS_OPT(1)); /* 15.3.1.3.31 */
678 mrb_define_method(mrb, krn, "private_methods", mrb_obj_private_methods, MRB_ARGS_OPT(1)); /* 15.3.1.3.36 */
679 mrb_define_method(mrb, krn, "protected_methods", mrb_obj_protected_methods, MRB_ARGS_OPT(1)); /* 15.3.1.3.37 */
680 mrb_define_method(mrb, krn, "public_methods", mrb_obj_public_methods, MRB_ARGS_OPT(1)); /* 15.3.1.3.38 */
681 mrb_define_method(mrb, krn, "singleton_methods", mrb_obj_singleton_methods_m, MRB_ARGS_OPT(1)); /* 15.3.1.3.45 */
682 mrb_define_method(mrb, krn, "define_singleton_method", mod_define_singleton_method, MRB_ARGS_ANY());
683 mrb_define_method(mrb, krn, "send", mrb_f_send, MRB_ARGS_ANY()); /* 15.3.1.3.44 */
685 mrb_define_method(mrb, mod, "class_variables", mrb_mod_class_variables, MRB_ARGS_NONE()); /* 15.2.2.4.19 */
686 mrb_define_method(mrb, mod, "remove_class_variable", mrb_mod_remove_cvar, MRB_ARGS_REQ(1)); /* 15.2.2.4.39 */
687 mrb_define_method(mrb, mod, "class_variable_defined?", mrb_mod_cvar_defined, MRB_ARGS_REQ(1)); /* 15.2.2.4.16 */
688 mrb_define_method(mrb, mod, "class_variable_get", mrb_mod_cvar_get, MRB_ARGS_REQ(1)); /* 15.2.2.4.17 */
689 mrb_define_method(mrb, mod, "class_variable_set", mrb_mod_cvar_set, MRB_ARGS_REQ(2)); /* 15.2.2.4.18 */
690 mrb_define_method(mrb, mod, "included_modules", mrb_mod_included_modules, MRB_ARGS_NONE()); /* 15.2.2.4.30 */
691 mrb_define_method(mrb, mod, "instance_methods", mrb_mod_instance_methods, MRB_ARGS_ANY()); /* 15.2.2.4.33 */
692 mrb_define_method(mrb, mod, "remove_method", mrb_mod_remove_method, MRB_ARGS_ANY()); /* 15.2.2.4.41 */
693 mrb_define_method(mrb, mod, "method_removed", mrb_f_nil, MRB_ARGS_REQ(1));
694 mrb_define_method(mrb, mod, "constants", mrb_mod_constants, MRB_ARGS_OPT(1)); /* 15.2.2.4.24 */
695 mrb_define_class_method(mrb, mod, "constants", mrb_mod_s_constants, MRB_ARGS_ANY()); /* 15.2.2.3.1 */
696 mrb_define_class_method(mrb, mod, "nesting", mrb_mod_s_nesting, MRB_ARGS_REQ(0)); /* 15.2.2.3.2 */
700 mrb_mruby_metaprog_gem_final(mrb_state* mrb)