diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2012-11-12 13:00:24 -0800 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2012-11-12 13:00:24 -0800 |
| commit | 1cd9d7e40abb8112fe9f05193f9af6ec357a71eb (patch) | |
| tree | 7260feb794228246b8ffe4563d1f3b012e5cc45e | |
| parent | 191d1391c66ddca71bf9e19f5af18478e73428d7 (diff) | |
| parent | 611cfee3c2ff1a06760cbe4d0d1415f65087b505 (diff) | |
| download | mruby-1cd9d7e40abb8112fe9f05193f9af6ec357a71eb.tar.gz mruby-1cd9d7e40abb8112fe9f05193f9af6ec357a71eb.zip | |
Merge pull request #546 from skandhas/pr-add-class-variables
add Module#class_variables
| -rw-r--r-- | include/mruby/variable.h | 1 | ||||
| -rw-r--r-- | src/class.c | 1 | ||||
| -rw-r--r-- | src/variable.c | 44 | ||||
| -rw-r--r-- | test/t/module.rb | 13 |
4 files changed, 59 insertions, 0 deletions
diff --git a/include/mruby/variable.h b/include/mruby/variable.h index e805b4b2b..5126315c4 100644 --- a/include/mruby/variable.h +++ b/include/mruby/variable.h @@ -53,6 +53,7 @@ void mrb_gv_set(mrb_state *mrb, mrb_sym sym, mrb_value val); mrb_value mrb_obj_instance_variables(mrb_state*, mrb_value); mrb_value mrb_obj_iv_inspect(mrb_state*, struct RObject*); mrb_sym mrb_class_sym(mrb_state *mrb, struct RClass *c, struct RClass *outer); +mrb_value mrb_mod_class_variables(mrb_state*, mrb_value); /* GC functions */ void mrb_gc_mark_gv(mrb_state*); diff --git a/src/class.c b/src/class.c index cf2f6df50..e737a1201 100644 --- a/src/class.c +++ b/src/class.c @@ -1440,6 +1440,7 @@ mrb_init_class(mrb_state *mrb) mrb_define_method(mrb, mod, "const_get", mrb_mod_const_get, ARGS_REQ(1)); /* 15.2.2.4.21 */ mrb_define_method(mrb, mod, "const_set", mrb_mod_const_set, ARGS_REQ(2)); /* 15.2.2.4.23 */ mrb_define_method(mrb, mod, "define_method", mod_define_method, ARGS_REQ(1)); + mrb_define_method(mrb, mod, "class_variables", mrb_mod_class_variables, ARGS_NONE()); /* 15.2.2.4.19 */ mrb_define_method(mrb, mod, "===", mrb_mod_eqq, ARGS_REQ(1)); mrb_undef_method(mrb, cls, "append_features"); diff --git a/src/variable.c b/src/variable.c index b3b3b3d87..19853685c 100644 --- a/src/variable.c +++ b/src/variable.c @@ -615,6 +615,50 @@ mrb_obj_instance_variables(mrb_state *mrb, mrb_value self) return ary; } +static int +cv_i(mrb_state *mrb, mrb_sym sym, mrb_value v, void *p) +{ + mrb_value ary; + const char* s; + int len; + + ary = *(mrb_value*)p; + s = mrb_sym2name_len(mrb, sym, &len); + if (len > 2 && s[0] == '@' && s[1] == '@') { + mrb_ary_push(mrb, ary, mrb_symbol_value(sym)); + } + return 0; +} + +/* 15.2.2.4.19 */ +/* + * 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; +} + + mrb_value mrb_vm_cv_get(mrb_state *mrb, mrb_sym sym) { diff --git a/test/t/module.rb b/test/t/module.rb index 1827d5758..ff8178cb0 100644 --- a/test/t/module.rb +++ b/test/t/module.rb @@ -31,6 +31,19 @@ assert('Module#append_features', '15.2.2.4.10') do Test4AppendFeatures2.const_get(:Const4AppendFeatures2) == Test4AppendFeatures2 end +assert('Module#class_variables', '15.2.2.4.19') do + class Test4ClassVariables1 + @@var1 = 1 + @@var2 = 2 + end + class Test4ClassVariables2 < Test4ClassVariables1 + @@var3 = 2 + end + + Test4ClassVariables1.class_variables == [:@@var1, :@@var2] && + Test4ClassVariables2.class_variables == [:@@var3] +end + assert('Module#const_defined?', '15.2.2.4.20') do module Test4ConstDefined Const4Test4ConstDefined = true |
