add Module#constants (15.2.2.4.24)

This commit is contained in:
Yukihiro "Matz" Matsumoto
2013-04-02 01:23:41 +09:00
parent 7c2f68862e
commit 013be527e6
3 changed files with 37 additions and 0 deletions
+35
View File
@@ -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*);
@@ -919,6 +920,40 @@ 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;
ary = mrb_ary_new(mrb);
if (obj_iv_p(mod) && mrb_obj_ptr(mod)->iv) {
iv_foreach(mrb, mrb_obj_ptr(mod)->iv, const_i, &ary);
}
return ary;
}
mrb_value
mrb_gv_get(mrb_state *mrb, mrb_sym sym)
{