From 451985d44be9078660bcb43e4c7d91b3398230a4 Mon Sep 17 00:00:00 2001 From: take_cheeze Date: Sun, 13 Jul 2014 20:46:50 +0900 Subject: Implement Module#name. Solves #2132. --- mrbgems/default.gembox | 3 +++ mrbgems/mruby-module-ext/mrbgem.rake | 5 +++++ mrbgems/mruby-module-ext/src/module.c | 23 +++++++++++++++++++++++ mrbgems/mruby-module-ext/test/module.rb | 10 ++++++++++ 4 files changed, 41 insertions(+) create mode 100644 mrbgems/mruby-module-ext/mrbgem.rake create mode 100644 mrbgems/mruby-module-ext/src/module.c create mode 100644 mrbgems/mruby-module-ext/test/module.rb diff --git a/mrbgems/default.gembox b/mrbgems/default.gembox index 0960ba979..9abbd8512 100644 --- a/mrbgems/default.gembox +++ b/mrbgems/default.gembox @@ -71,6 +71,9 @@ MRuby::GemBox.new do |conf| # Use Kernel module extension conf.gem :core => "mruby-kernel-ext" + # Use extensional Module class + conf.gem :core => "mruby-module-ext" + # Use mruby-compiler to build other mrbgems conf.gem :core => "mruby-compiler" end diff --git a/mrbgems/mruby-module-ext/mrbgem.rake b/mrbgems/mruby-module-ext/mrbgem.rake new file mode 100644 index 000000000..d1f3952ba --- /dev/null +++ b/mrbgems/mruby-module-ext/mrbgem.rake @@ -0,0 +1,5 @@ +MRuby::Gem::Specification.new('mruby-module-ext') do |spec| + spec.license = 'MIT' + spec.author = 'mruby developers' + spec.summary = 'extensional Module class' +end diff --git a/mrbgems/mruby-module-ext/src/module.c b/mrbgems/mruby-module-ext/src/module.c new file mode 100644 index 000000000..74bece800 --- /dev/null +++ b/mrbgems/mruby-module-ext/src/module.c @@ -0,0 +1,23 @@ +#include "mruby.h" +#include "mruby/class.h" +#include "mruby/string.h" + +static mrb_value +mrb_mod_name(mrb_state *mrb, mrb_value self) +{ + mrb_value name = mrb_class_path(mrb, mrb_class_ptr(self)); + return mrb_nil_p(name)? name : mrb_str_dup(mrb, name); +} + +void +mrb_mruby_module_ext_gem_init(mrb_state *mrb) +{ + struct RClass *mod = mrb->module_class; + + mrb_define_method(mrb, mod, "name", mrb_mod_name, MRB_ARGS_NONE()); +} + +void +mrb_mruby_module_ext_gem_final(mrb_state *mrb) +{ +} diff --git a/mrbgems/mruby-module-ext/test/module.rb b/mrbgems/mruby-module-ext/test/module.rb new file mode 100644 index 000000000..f721ad0c6 --- /dev/null +++ b/mrbgems/mruby-module-ext/test/module.rb @@ -0,0 +1,10 @@ +assert 'Module#name' do + module A + class B + end + end + + assert_nil A::B.singleton_class.name + assert_equal 'Fixnum', Fixnum.name + assert_equal 'A::B', A::B.name +end -- cgit v1.2.3