static void mark_tbl(mrb_state *mrb, iv_tbl *t) { if (t) { iv_foreach(mrb, t, iv_mark_i, 0); } }
/* * call-seq: * mod.class_variables -> array * * Returns an array of the names of class variables in <i>mod</i>. * * class One * @@var1 = 1 * end * class Two < One * @@var2 = 2 * end * One.class_variables #=> [:@@var1] * Two.class_variables #=> [:@@var2] */ mrb_value mrb_mod_class_variables(mrb_state *mrb, mrb_value mod) { mrb_value ary; ary = mrb_ary_new(mrb); if (obj_iv_p(mod) && mrb_obj_ptr(mod)->iv) { iv_foreach(mrb, mrb_obj_ptr(mod)->iv, cv_i, &ary); } return ary; }
/* * call-seq: * obj.instance_variables -> array * * Returns an array of instance variable names for the receiver. Note * that simply defining an accessor does not create the corresponding * instance variable. * * class Fred * attr_accessor :a1 * def initialize * @iv = 3 * end * end * Fred.new.instance_variables #=> [:@iv] */ mrb_value mrb_obj_instance_variables(mrb_state *mrb, mrb_value self) { mrb_value ary; ary = mrb_ary_new(mrb); if (obj_iv_p(self) && mrb_obj_ptr(self)->iv) { iv_foreach(mrb, mrb_obj_ptr(self)->iv, iv_i, &ary); } return ary; }
static mrb_sym find_class_sym(mrb_state *mrb, struct RClass *outer, struct RClass *c) { struct csym_arg arg; if (!outer) return 0; arg.c = c; arg.sym = 0; iv_foreach(mrb, outer->iv, csym_i, &arg); return arg.sym; }
/* * call-seq: * mod.class_variables -> array * * Returns an array of the names of class variables in <i>mod</i>. * * class One * @@var1 = 1 * end * class Two < One * @@var2 = 2 * end * One.class_variables #=> [:@@var1] * Two.class_variables #=> [:@@var2] */ mrb_value mrb_mod_class_variables(mrb_state *mrb, mrb_value mod) { mrb_value ary; struct RClass *c; ary = mrb_ary_new(mrb); c = mrb_class_ptr(mod); while (c) { iv_foreach(mrb, c->iv, cv_i, &ary); c = c->super; } return ary; }
mrb_value mrb_obj_iv_inspect(mrb_state *mrb, struct RObject *obj) { iv_tbl *t = obj->iv; int len = iv_size(mrb, t); if (len > 0) { const char *cn = mrb_obj_classname(mrb, mrb_obj_value(obj)); mrb_value str = mrb_sprintf(mrb, "-<%s:%p", cn, (void*)obj); iv_foreach(mrb, t, inspect_i, &str); return str; } return mrb_any_to_s(mrb, mrb_obj_value(obj)); }
/* * call-seq: * mod.constants -> array * * Returns an array of all names of contants defined in the receiver. */ mrb_value mrb_mod_constants(mrb_state *mrb, mrb_value mod) { mrb_value ary; struct RClass *c = mrb_class_ptr(mod); ary = mrb_ary_new(mrb); while (c) { if (c->iv) { iv_foreach(mrb, c->iv, const_i, &ary); } c = c->super; if (c == mrb->object_class) break; } return ary; }
mrb_sym mrb_class_sym(mrb_state *mrb, struct RClass *c, struct RClass *outer) { mrb_value name; name = mrb_obj_iv_get(mrb, (struct RObject*)c, mrb_intern(mrb, "__classid__")); if (mrb_nil_p(name)) { struct csym_arg arg; arg.c = c; arg.sym = 0; iv_foreach(mrb, outer->iv, csym_i, &arg); return arg.sym; } return SYM2ID(name); }
/* * call-seq: * global_variables -> array * * Returns an array of the names of global variables. * * global_variables.grep /std/ #=> [:$stdin, :$stdout, :$stderr] */ mrb_value mrb_f_global_variables(mrb_state *mrb, mrb_value self) { iv_tbl *t = mrb->globals; mrb_value ary = mrb_ary_new(mrb); size_t i; char buf[3]; iv_foreach(mrb, t, gv_i, &ary); buf[0] = '$'; buf[2] = 0; for (i = 1; i <= 9; ++i) { buf[1] = (char)(i + '0'); mrb_ary_push(mrb, ary, mrb_symbol_value(mrb_intern(mrb, buf, 2))); } return ary; }
/* * call-seq: * mod.constants -> array * * Returns an array of all names of contants defined in the receiver. */ mrb_value mrb_mod_constants(mrb_state *mrb, mrb_value mod) { mrb_value ary; mrb_bool inherit = TRUE; struct RClass *c = mrb_class_ptr(mod); mrb_get_args(mrb, "|b", &inherit); ary = mrb_ary_new(mrb); while (c) { iv_foreach(mrb, c->iv, const_i, &ary); if (!inherit) break; c = c->super; if (c == mrb->object_class) break; } return ary; }
mrb_value mrb_obj_iv_inspect(mrb_state *mrb, struct RObject *obj) { iv_tbl *t = obj->iv; size_t len = iv_size(mrb, t); if (len > 0) { const char *cn = mrb_obj_classname(mrb, mrb_obj_value(obj)); mrb_value str = mrb_str_buf_new(mrb, 30); mrb_str_buf_cat(mrb, str, "-<", 2); mrb_str_cat2(mrb, str, cn); mrb_str_cat(mrb, str, ":", 1); mrb_str_concat(mrb, str, mrb_ptr_to_str(mrb, obj)); iv_foreach(mrb, t, inspect_i, &str); mrb_str_cat(mrb, str, ">", 1); return str; } return mrb_any_to_s(mrb, mrb_obj_value(obj)); }
/* Iterates over the instance variable table. */ MRB_API void mrb_iv_foreach(mrb_state *mrb, mrb_value obj, mrb_iv_foreach_func *func, void *p) { if (!obj_iv_p(obj)) return; iv_foreach(mrb, mrb_obj_ptr(obj)->iv, func, p); }