diff options
| -rw-r--r-- | build_config.rb | 3 | ||||
| -rw-r--r-- | mrbgems/mruby-numeric-ext/mrbgem.rake | 4 | ||||
| -rw-r--r-- | mrbgems/mruby-numeric-ext/src/numeric_ext.c | 30 | ||||
| -rw-r--r-- | mrbgems/mruby-numeric-ext/test/numeric.rb | 7 |
4 files changed, 44 insertions, 0 deletions
diff --git a/build_config.rb b/build_config.rb index c54c9d323..f697ec97d 100644 --- a/build_config.rb +++ b/build_config.rb @@ -26,6 +26,9 @@ MRuby::Build.new do |conf| # Use extensional String class conf.gem 'mrbgems/mruby-string-ext' + # Use extensional Numeric class + conf.gem 'mrbgems/mruby-numeric-ext' + # Generate binaries # conf.bins = %w(mrbc mruby mirb) diff --git a/mrbgems/mruby-numeric-ext/mrbgem.rake b/mrbgems/mruby-numeric-ext/mrbgem.rake new file mode 100644 index 000000000..69c4fde4c --- /dev/null +++ b/mrbgems/mruby-numeric-ext/mrbgem.rake @@ -0,0 +1,4 @@ +MRuby::Gem::Specification.new('mruby-numeric-ext') do |spec| + spec.license = 'MIT' + spec.authors = 'mruby developers' +end diff --git a/mrbgems/mruby-numeric-ext/src/numeric_ext.c b/mrbgems/mruby-numeric-ext/src/numeric_ext.c new file mode 100644 index 000000000..b2c0e7986 --- /dev/null +++ b/mrbgems/mruby-numeric-ext/src/numeric_ext.c @@ -0,0 +1,30 @@ +#include "mruby.h" +#include "mruby/numeric.h" + +static mrb_value +mrb_int_chr(mrb_state *mrb, mrb_value x) +{ + mrb_int chr; + char c; + + chr = mrb_fixnum(x); + if (chr >= (1 << CHAR_BIT)) { + mrb_raisef(mrb, E_RANGE_ERROR, "%ld out of char range", chr); + } + c = (char)chr; + + return mrb_str_new(mrb, &c, 1); +} + +void +mrb_mruby_numeric_ext_gem_init(mrb_state* mrb) +{ + struct RClass *i = mrb_class_get(mrb, "Integer"); + + mrb_define_method(mrb, i, "chr", mrb_int_chr, ARGS_NONE()); +} + +void +mrb_mruby_numeric_ext_gem_final(mrb_state* mrb) +{ +} diff --git a/mrbgems/mruby-numeric-ext/test/numeric.rb b/mrbgems/mruby-numeric-ext/test/numeric.rb new file mode 100644 index 000000000..cafa0f1aa --- /dev/null +++ b/mrbgems/mruby-numeric-ext/test/numeric.rb @@ -0,0 +1,7 @@ +## +# Numeric(Ext) Test + +assert('Integer#chr') do + assert_equal(65.chr, "A") + assert_equal(0x42.chr, "B") +end |
