diff options
Diffstat (limited to 'src/variable.c')
| -rw-r--r-- | src/variable.c | 44 |
1 files changed, 42 insertions, 2 deletions
diff --git a/src/variable.c b/src/variable.c index 941aaf83e..2057b69ef 100644 --- a/src/variable.c +++ b/src/variable.c @@ -11,6 +11,7 @@ #include "mruby/string.h" #include "mruby/variable.h" #include "error.h" +#include <ctype.h> typedef int (iv_foreach_func)(mrb_state*,mrb_sym,mrb_value,void*); @@ -690,7 +691,7 @@ mrb_mod_cv_get(mrb_state *mrb, struct RClass * c, mrb_sym sym) } c = c->super; } - mrb_raisef(mrb, E_NAME_ERROR, "uninitialized class variable %S in %S", + mrb_name_error(mrb, sym, "uninitialized class variable %S in %S", mrb_sym2str(mrb, sym), cls); /* not reached */ return mrb_nil_value(); @@ -846,7 +847,7 @@ L_RETRY: } c = c->super; } - mrb_raisef(mrb, E_NAME_ERROR, "uninitialized constant %S", + mrb_name_error(mrb, sym, "uninitialized constant %S", mrb_sym2str(mrb, sym)); /* not reached */ return mrb_nil_value(); @@ -919,6 +920,45 @@ mrb_define_global_const(mrb_state *mrb, const char *name, mrb_value val) mrb_define_const(mrb, mrb->object_class, name, val); } +static int +const_i(mrb_state *mrb, mrb_sym sym, mrb_value v, void *p) +{ + mrb_value ary; + const char* s; + size_t len; + + ary = *(mrb_value*)p; + s = mrb_sym2name_len(mrb, sym, &len); + if (len > 1 && ISUPPER(s[0])) { + mrb_ary_push(mrb, ary, mrb_symbol_value(sym)); + } + return 0; +} + +/* 15.2.2.4.24 */ +/* + * 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_value mrb_gv_get(mrb_state *mrb, mrb_sym sym) { |
