├── bin └── .gitkeep ├── src ├── ext │ └── .gitkeep ├── opcode.h ├── error.h ├── compar.c ├── enum.c ├── value_array.h ├── mruby_core.rake ├── version.c ├── crc.c ├── print.c └── init.c ├── benchmark ├── bm_fib.rb ├── plot.gpl ├── build_config_cc.rb ├── build_config_boxing.rb └── bm_so_lists.rb ├── test ├── t │ ├── regexperror.rb │ ├── indexerror.rb │ ├── rangeerror.rb │ ├── typeerror.rb │ ├── runtimeerror.rb │ ├── standarderror.rb │ ├── basicobject.rb │ ├── object.rb │ ├── argumenterror.rb │ ├── localjumperror.rb │ ├── symbol.rb │ ├── nameerror.rb │ ├── true.rb │ ├── bs_literal.rb │ ├── false.rb │ ├── nil.rb │ ├── numeric.rb │ ├── gc.rb │ ├── ensure.rb │ ├── unicode.rb │ ├── nomethoderror.rb │ ├── comparable.rb │ ├── lang.rb │ ├── superclass.rb │ ├── range.rb │ ├── codegen.rb │ └── methods.rb ├── report.rb └── bintest.rb ├── examples ├── mrbgems │ ├── c_extension_example │ │ ├── test │ │ │ ├── example.rb │ │ │ └── example.c │ │ ├── README.md │ │ ├── src │ │ │ └── example.c │ │ └── mrbgem.rake │ ├── ruby_extension_example │ │ ├── test │ │ │ └── example.rb │ │ ├── README.md │ │ ├── mrblib │ │ │ └── example.rb │ │ └── mrbgem.rake │ └── c_and_ruby_extension_example │ │ ├── README.md │ │ ├── mrblib │ │ └── example.rb │ │ ├── test │ │ └── example.rb │ │ ├── src │ │ └── example.c │ │ └── mrbgem.rake └── targets │ ├── build_config_RX630.rb │ ├── build_config_IntelEdison.rb │ ├── build_config_chipKITMax32.rb │ └── build_config_ArduinoDue.rb ├── mrbgems ├── mruby-test │ ├── README.md │ └── init_mrbtest.c ├── mruby-fiber │ └── mrbgem.rake ├── mruby-random │ ├── mrbgem.rake │ ├── src │ │ ├── random.h │ │ └── mt19937ar.h │ └── test │ │ └── random.rb ├── mruby-exit │ ├── mrbgem.rake │ └── src │ │ └── mruby-exit.c ├── mruby-math │ └── mrbgem.rake ├── mruby-time │ ├── mrbgem.rake │ └── mrblib │ │ └── time.rb ├── mruby-print │ ├── mrbgem.rake │ ├── mrblib │ │ └── print.rb │ └── src │ │ └── print.c ├── mruby-struct │ ├── mrbgem.rake │ └── mrblib │ │ └── struct.rb ├── mruby-array-ext │ └── mrbgem.rake ├── mruby-objectspace │ ├── mrbgem.rake │ └── test │ │ └── objectspace.rb ├── mruby-proc-ext │ ├── mrbgem.rake │ ├── mrblib │ │ └── proc.rb │ └── test │ │ ├── proc.c │ │ └── proc.rb ├── mruby-range-ext │ ├── mrbgem.rake │ ├── test │ │ └── range.rb │ └── mrblib │ │ └── range.rb ├── mruby-sprintf │ ├── mrblib │ │ └── string.rb │ ├── mrbgem.rake │ ├── src │ │ └── kernel.c │ └── test │ │ └── sprintf.rb ├── mruby-class-ext │ ├── mrbgem.rake │ ├── test │ │ └── module.rb │ └── src │ │ └── class.c ├── mruby-enum-ext │ └── mrbgem.rake ├── mruby-inline-struct │ ├── mrbgem.rake │ └── test │ │ └── inline.c ├── mruby-kernel-ext │ ├── mrbgem.rake │ └── test │ │ └── kernel.rb ├── mruby-object-ext │ ├── mrbgem.rake │ ├── test │ │ ├── nil.rb │ │ └── object.rb │ ├── mrblib │ │ └── object.rb │ └── src │ │ └── object.c ├── mruby-string-ext │ └── mrbgem.rake ├── mruby-symbol-ext │ ├── mrbgem.rake │ ├── test │ │ └── symbol.rb │ ├── mrblib │ │ └── symbol.rb │ └── src │ │ └── symbol.c ├── mruby-numeric-ext │ ├── mrbgem.rake │ ├── mrblib │ │ └── numeric_ext.rb │ ├── test │ │ └── numeric.rb │ └── src │ │ └── numeric_ext.c ├── mruby-toplevel-ext │ ├── mrbgem.rake │ ├── mrblib │ │ └── toplevel.rb │ └── test │ │ └── toplevel.rb ├── mruby-bin-strip │ ├── mrbgem.rake │ └── bintest │ │ └── mruby-strip.rb ├── mruby-eval │ ├── mrbgem.rake │ └── test │ │ └── eval.rb ├── mruby-bin-mruby │ ├── mrbgem.rake │ └── bintest │ │ └── mruby.rb ├── mruby-bin-debugger │ ├── tools │ │ └── mrdb │ │ │ ├── apiprint.h │ │ │ ├── mrdbconf.h │ │ │ ├── apilist.h │ │ │ ├── mrdberror.h │ │ │ ├── apibreak.h │ │ │ ├── cmdprint.c │ │ │ ├── cmdrun.c │ │ │ └── apiprint.c │ └── mrbgem.rake ├── full-core.gembox ├── mruby-enumerator │ └── mrbgem.rake ├── mruby-hash-ext │ ├── mrbgem.rake │ └── src │ │ └── hash-ext.c ├── mruby-enum-lazy │ ├── mrbgem.rake │ └── test │ │ └── lazy.rb ├── mruby-bin-mirb │ ├── bintest │ │ └── mirb.rb │ └── mrbgem.rake ├── mruby-error │ ├── mrbgem.rake │ ├── test │ │ ├── exception.rb │ │ └── exception.c │ └── src │ │ └── exception.c ├── mruby-bin-mrbc │ └── mrbgem.rake ├── mruby-bin-mruby-config │ ├── mruby-config │ ├── mruby-config.bat │ └── mrbgem.rake ├── mruby-compiler │ ├── bintest │ │ └── mrbc.rb │ ├── mrbgem.rake │ └── core │ │ ├── node.h │ │ └── keywords └── default.gembox ├── LEGAL ├── mrblib ├── init_mrblib.c ├── 00class.rb ├── mrblib.rake ├── kernel.rb ├── 10error.rb ├── range.rb ├── compar.rb └── numeric.rb ├── include └── mruby │ ├── re.h │ ├── istruct.h │ ├── object.h │ ├── throw.h │ ├── irep.h │ ├── range.h │ ├── boxing_no.h │ ├── common.h │ ├── debug.h │ ├── error.h │ ├── gc.h │ ├── data.h │ ├── proc.h │ ├── version.h │ └── class.h ├── TODO ├── tasks ├── toolchains │ ├── clang.rake │ ├── openwrt.rake │ ├── gcc.rake │ └── visualcpp.rake ├── libmruby.rake ├── ruby_ext.rake └── benchmark.rake ├── .yardopts ├── .travis.yml ├── .gitignore ├── Makefile ├── NEWS ├── appveyor.yml ├── mruby-source.gemspec ├── AUTHORS ├── MITL ├── appveyor_config.rb ├── travis_config.rb ├── lib └── mruby │ └── source.rb └── CONTRIBUTING.md /bin/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/ext/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/opcode.h: -------------------------------------------------------------------------------- 1 | /* this header file is to be removed soon. */ 2 | #include 3 | -------------------------------------------------------------------------------- /benchmark/bm_fib.rb: -------------------------------------------------------------------------------- 1 | 2 | def fib n 3 | return n if n < 2 4 | fib(n-2) + fib(n-1) 5 | end 6 | 7 | puts fib(37) 8 | -------------------------------------------------------------------------------- /test/t/regexperror.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # RegexpError ISO Test 3 | 4 | # TODO broken ATM assert('RegexpError', '15.2.27') do 5 | -------------------------------------------------------------------------------- /examples/mrbgems/c_extension_example/test/example.rb: -------------------------------------------------------------------------------- 1 | assert('C Extension Example') do 2 | CExtension.respond_to? :c_method 3 | end 4 | -------------------------------------------------------------------------------- /src/error.h: -------------------------------------------------------------------------------- 1 | /* this header file is to be removed soon. 2 | added for compatibility purpose (1.0.0) */ 3 | #include 4 | -------------------------------------------------------------------------------- /test/report.rb: -------------------------------------------------------------------------------- 1 | report 2 | if $ko_test > 0 or $kill_test > 0 3 | raise "mrbtest failed (KO:#{$ko_test}, Crash:#{$kill_test})" 4 | end 5 | -------------------------------------------------------------------------------- /examples/mrbgems/c_extension_example/README.md: -------------------------------------------------------------------------------- 1 | C Extension Example 2 | ========= 3 | 4 | This is an example gem which implements a C extension. 5 | -------------------------------------------------------------------------------- /examples/mrbgems/ruby_extension_example/test/example.rb: -------------------------------------------------------------------------------- 1 | assert('Ruby Extension Example') do 2 | RubyExtension.respond_to? :ruby_method 3 | end 4 | -------------------------------------------------------------------------------- /test/t/indexerror.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # IndexError ISO Test 3 | 4 | assert('IndexError', '15.2.33') do 5 | assert_equal Class, IndexError.class 6 | end 7 | -------------------------------------------------------------------------------- /test/t/rangeerror.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # RangeError ISO Test 3 | 4 | assert('RangeError', '15.2.26') do 5 | assert_equal Class, RangeError.class 6 | end 7 | -------------------------------------------------------------------------------- /test/t/typeerror.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # TypeError ISO Test 3 | 4 | assert('TypeError', '15.2.29') do 5 | assert_equal Class, TypeError.class 6 | end 7 | -------------------------------------------------------------------------------- /test/t/runtimeerror.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # RuntimeError ISO Test 3 | 4 | assert('RuntimeError', '15.2.28') do 5 | assert_equal Class, RuntimeError.class 6 | end 7 | -------------------------------------------------------------------------------- /test/t/standarderror.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # StandardError ISO Test 3 | 4 | assert('StandardError', '15.2.23') do 5 | assert_equal Class, StandardError.class 6 | end 7 | -------------------------------------------------------------------------------- /examples/mrbgems/ruby_extension_example/README.md: -------------------------------------------------------------------------------- 1 | Pure Ruby Extension Example 2 | ========= 3 | 4 | This is an example gem which implements a pure Ruby extension. 5 | -------------------------------------------------------------------------------- /examples/mrbgems/ruby_extension_example/mrblib/example.rb: -------------------------------------------------------------------------------- 1 | class RubyExtension 2 | def RubyExtension.ruby_method 3 | puts "A Ruby Extension" 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /examples/mrbgems/c_and_ruby_extension_example/README.md: -------------------------------------------------------------------------------- 1 | C and Ruby Extension Example 2 | ========= 3 | 4 | This is an example gem which implements a C and Ruby extension. 5 | -------------------------------------------------------------------------------- /examples/mrbgems/c_and_ruby_extension_example/mrblib/example.rb: -------------------------------------------------------------------------------- 1 | module CRubyExtension 2 | def CRubyExtension.ruby_method 3 | puts "A Ruby Extension" 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /mrbgems/mruby-test/README.md: -------------------------------------------------------------------------------- 1 | Running Tests 2 | ============= 3 | 4 | To run the tests, execute the following from the project's root directory. 5 | 6 | $ make test 7 | 8 | -------------------------------------------------------------------------------- /examples/mrbgems/c_extension_example/test/example.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void 4 | mrb_c_extension_example_gem_test(mrb_state *mrb) 5 | { 6 | /* test initializer in C */ 7 | } 8 | -------------------------------------------------------------------------------- /mrbgems/mruby-fiber/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('mruby-fiber') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.summary = 'Fiber class' 5 | end 6 | -------------------------------------------------------------------------------- /mrbgems/mruby-random/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('mruby-random') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.summary = 'Random class' 5 | end 6 | -------------------------------------------------------------------------------- /LEGAL: -------------------------------------------------------------------------------- 1 | LEGAL NOTICE INFORMATION 2 | ------------------------ 3 | 4 | All the files in this distribution are covered under the MIT license 5 | (see the file MITL) except some files mentioned below: 6 | 7 | -------------------------------------------------------------------------------- /benchmark/plot.gpl: -------------------------------------------------------------------------------- 1 | set yrange [0:] 2 | set terminal pngcairo font 'Sans, 8' lw 1 size 1400,1024 3 | set xtics rotate by -45 4 | set style histogram errorbars gap 2 lw 1 5 | set style fill solid border -1 6 | -------------------------------------------------------------------------------- /mrbgems/mruby-exit/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('mruby-exit') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.summary = 'Kernel#exit method' 5 | end 6 | -------------------------------------------------------------------------------- /mrbgems/mruby-math/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('mruby-math') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.summary = 'standard Math module' 5 | end 6 | -------------------------------------------------------------------------------- /mrbgems/mruby-time/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('mruby-time') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.summary = 'standard Time class' 5 | end 6 | -------------------------------------------------------------------------------- /mrbgems/mruby-print/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('mruby-print') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.summary = 'standard print/puts/p' 5 | end 6 | -------------------------------------------------------------------------------- /mrbgems/mruby-struct/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('mruby-struct') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.summary = 'standard Struct class' 5 | end 6 | -------------------------------------------------------------------------------- /mrbgems/mruby-array-ext/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('mruby-array-ext') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.summary = 'Array class extension' 5 | end 6 | -------------------------------------------------------------------------------- /mrbgems/mruby-objectspace/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('mruby-objectspace') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.summary = 'ObjectSpace class' 5 | end 6 | -------------------------------------------------------------------------------- /mrbgems/mruby-proc-ext/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('mruby-proc-ext') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.summary = 'Proc class extension' 5 | end 6 | -------------------------------------------------------------------------------- /mrbgems/mruby-range-ext/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('mruby-range-ext') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.summary = 'Range class extension' 5 | end 6 | -------------------------------------------------------------------------------- /mrbgems/mruby-sprintf/mrblib/string.rb: -------------------------------------------------------------------------------- 1 | class String 2 | def %(args) 3 | if args.is_a? Array 4 | sprintf(self, *args) 5 | else 6 | sprintf(self, args) 7 | end 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /mrbgems/mruby-class-ext/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('mruby-class-ext') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.summary = 'class/module extension' 5 | end 6 | -------------------------------------------------------------------------------- /mrbgems/mruby-enum-ext/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('mruby-enum-ext') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.summary = 'Enumerable module extension' 5 | end 6 | -------------------------------------------------------------------------------- /mrbgems/mruby-inline-struct/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('mruby-inline-struct') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.summary = 'inline structure' 5 | end 6 | -------------------------------------------------------------------------------- /mrbgems/mruby-kernel-ext/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('mruby-kernel-ext') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.summary = 'Kernel module extension' 5 | end 6 | -------------------------------------------------------------------------------- /mrbgems/mruby-object-ext/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('mruby-object-ext') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.summary = 'Object class extension' 5 | end 6 | -------------------------------------------------------------------------------- /mrbgems/mruby-sprintf/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('mruby-sprintf') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.summary = 'standard Kernel#sprintf method' 5 | end 6 | -------------------------------------------------------------------------------- /mrbgems/mruby-string-ext/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('mruby-string-ext') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.summary = 'String class extension' 5 | end 6 | -------------------------------------------------------------------------------- /mrbgems/mruby-symbol-ext/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('mruby-symbol-ext') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.summary = 'Symbol class extension' 5 | end 6 | -------------------------------------------------------------------------------- /mrbgems/mruby-numeric-ext/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('mruby-numeric-ext') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.summary = 'Numeric class extension' 5 | end 6 | -------------------------------------------------------------------------------- /mrblib/init_mrblib.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | extern const uint8_t mrblib_irep[]; 5 | 6 | void 7 | mrb_init_mrblib(mrb_state *mrb) 8 | { 9 | mrb_load_irep(mrb, mrblib_irep); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /mrbgems/mruby-toplevel-ext/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('mruby-toplevel-ext') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.summary = 'toplevel object (main) methods extension' 5 | end 6 | -------------------------------------------------------------------------------- /test/t/basicobject.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # BasicObject 3 | 4 | assert('BasicObject') do 5 | assert_equal(Class, BasicObject.class) 6 | end 7 | 8 | assert('BasicObject superclass') do 9 | assert_nil(BasicObject.superclass) 10 | end 11 | 12 | -------------------------------------------------------------------------------- /test/t/object.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # Object ISO Test 3 | 4 | assert('Object', '15.2.1') do 5 | assert_equal Class, Object.class 6 | end 7 | 8 | assert('Object superclass', '15.2.1.2') do 9 | assert_equal BasicObject, Object.superclass 10 | end 11 | 12 | -------------------------------------------------------------------------------- /mrbgems/mruby-toplevel-ext/mrblib/toplevel.rb: -------------------------------------------------------------------------------- 1 | 2 | def self.include (*modules) 3 | self.class.include(*modules) 4 | end 5 | 6 | def self.private(*methods) 7 | end 8 | def self.protected(*methods) 9 | end 10 | def self.public(*methods) 11 | end 12 | -------------------------------------------------------------------------------- /examples/mrbgems/c_and_ruby_extension_example/test/example.rb: -------------------------------------------------------------------------------- 1 | assert('C and Ruby Extension Example 1') do 2 | CRubyExtension.respond_to? :c_method 3 | end 4 | 5 | assert('C and Ruby Extension Example 2') do 6 | CRubyExtension.respond_to? :ruby_method 7 | end 8 | -------------------------------------------------------------------------------- /mrbgems/mruby-class-ext/test/module.rb: -------------------------------------------------------------------------------- 1 | assert 'Module#name' do 2 | module A 3 | class B 4 | end 5 | end 6 | 7 | assert_nil A::B.singleton_class.name 8 | assert_equal 'Fixnum', Fixnum.name 9 | assert_equal 'A::B', A::B.name 10 | end 11 | -------------------------------------------------------------------------------- /mrbgems/mruby-bin-strip/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('mruby-bin-strip') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.summary = 'irep dump debug section remover command' 5 | spec.bins = %w(mruby-strip) 6 | end 7 | -------------------------------------------------------------------------------- /mrbgems/mruby-object-ext/test/nil.rb: -------------------------------------------------------------------------------- 1 | assert('NilClass#to_a') do 2 | assert_equal [], nil.to_a 3 | end 4 | 5 | assert('NilClass#to_f') do 6 | assert_equal 0.0, nil.to_f 7 | end 8 | 9 | assert('NilClass#to_i') do 10 | assert_equal 0, nil.to_i 11 | end 12 | -------------------------------------------------------------------------------- /mrbgems/mruby-random/src/random.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** random.h - Random module 3 | ** 4 | ** See Copyright Notice in mruby.h 5 | */ 6 | 7 | #ifndef MRUBY_RANDOM_H 8 | #define MRUBY_RANDOM_H 9 | 10 | void mrb_mruby_random_gem_init(mrb_state *mrb); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /src/compar.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** compar.c - Comparable module 3 | ** 4 | ** See Copyright Notice in mruby.h 5 | */ 6 | 7 | #include 8 | 9 | void 10 | mrb_init_comparable(mrb_state *mrb) 11 | { 12 | mrb_define_module(mrb, "Comparable"); /* 15.3.3 */ 13 | } 14 | -------------------------------------------------------------------------------- /src/enum.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** enum.c - Enumerable module 3 | ** 4 | ** See Copyright Notice in mruby.h 5 | */ 6 | 7 | #include 8 | 9 | void 10 | mrb_init_enumerable(mrb_state *mrb) 11 | { 12 | mrb_define_module(mrb, "Enumerable"); /* 15.3.2 */ 13 | } 14 | 15 | -------------------------------------------------------------------------------- /mrbgems/mruby-eval/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('mruby-eval') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.summary = 'standard Kernel#eval method' 5 | 6 | add_dependency 'mruby-compiler', :core => 'mruby-compiler' 7 | end 8 | -------------------------------------------------------------------------------- /include/mruby/re.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** mruby/re.h - Regexp class 3 | ** 4 | ** See Copyright Notice in mruby.h 5 | */ 6 | 7 | #ifndef MRUBY_RE_H 8 | #define MRUBY_RE_H 9 | 10 | MRB_BEGIN_DECL 11 | 12 | #define REGEXP_CLASS "Regexp" 13 | 14 | MRB_END_DECL 15 | 16 | #endif /* RE_H */ 17 | -------------------------------------------------------------------------------- /TODO: -------------------------------------------------------------------------------- 1 | Things to do (Things that are not done yet) 2 | 3 | * special variables ($1,$2..) 4 | * super in aliased methods 5 | * multi-assignment decomposing 6 | * keyword arguments in def statement 7 | 8 | Things to improve (Done but things to fix) 9 | 10 | * Make additions as they are noticed. 11 | -------------------------------------------------------------------------------- /benchmark/build_config_cc.rb: -------------------------------------------------------------------------------- 1 | MRuby::Build.new do |conf| 2 | toolchain :gcc 3 | end 4 | 5 | MRuby::Build.new('gcc') do |conf| 6 | toolchain :gcc 7 | conf.gembox 'default' 8 | end 9 | 10 | MRuby::Build.new('clang') do |conf| 11 | toolchain :clang 12 | conf.gembox 'default' 13 | end 14 | -------------------------------------------------------------------------------- /mrbgems/mruby-bin-mruby/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('mruby-bin-mruby') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.summary = 'mruby command' 5 | spec.bins = %w(mruby) 6 | spec.add_dependency('mruby-compiler', :core => 'mruby-compiler') 7 | end 8 | -------------------------------------------------------------------------------- /mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb: -------------------------------------------------------------------------------- 1 | module Integral 2 | def div(other) 3 | self.divmod(other)[0] 4 | end 5 | 6 | def zero? 7 | self == 0 8 | end 9 | 10 | def nonzero? 11 | if self == 0 12 | nil 13 | else 14 | self 15 | end 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /mrbgems/mruby-time/mrblib/time.rb: -------------------------------------------------------------------------------- 1 | class Time 2 | def sunday?; wday == 0 end 3 | def monday?; wday == 1 end 4 | def tuesday?; wday == 2 end 5 | def wednesday?; wday == 3 end 6 | def thursday?; wday == 4 end 7 | def friday?; wday == 5 end 8 | def saturday?; wday == 6 end 9 | end 10 | -------------------------------------------------------------------------------- /tasks/toolchains/clang.rake: -------------------------------------------------------------------------------- 1 | MRuby::Toolchain.new(:clang) do |conf, _params| 2 | toolchain :gcc 3 | 4 | [conf.cc, conf.objc, conf.asm].each do |cc| 5 | cc.command = ENV['CC'] || 'clang' 6 | end 7 | conf.cxx.command = ENV['CXX'] || 'clang++' 8 | conf.linker.command = ENV['LD'] || 'clang' 9 | end 10 | -------------------------------------------------------------------------------- /.yardopts: -------------------------------------------------------------------------------- 1 | --markup markdown 2 | --plugin mruby 3 | --plugin coderay 4 | --output-dir doc/api 5 | 6 | src/**/*.c 7 | mrblib/**/*.rb 8 | include/**/*.h 9 | 10 | mrbgems/*/src/**/*.c 11 | mrbgems/*/mrblib/**/*.rb 12 | mrbgems/*/include/**/*.h 13 | - 14 | AUTHORS 15 | MITL 16 | CONTRIBUTING.md 17 | doc/guides/*.md 18 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: c 2 | 3 | sudo: false 4 | 5 | matrix: 6 | include: 7 | - os: linux 8 | sudo: 9000 9 | - os: osx 10 | osx_image: xcode7.1 11 | 12 | addons: 13 | apt: 14 | packages: 15 | - gperf 16 | 17 | env: MRUBY_CONFIG=travis_config.rb 18 | script: "./minirake all test" 19 | -------------------------------------------------------------------------------- /mrbgems/mruby-bin-debugger/tools/mrdb/apiprint.h: -------------------------------------------------------------------------------- 1 | /* 2 | * apiprint.h 3 | */ 4 | 5 | #ifndef APIPRINT_H_ 6 | #define APIPRINT_H_ 7 | 8 | #include 9 | #include "mrdb.h" 10 | 11 | mrb_value mrb_debug_eval(mrb_state*, mrb_debug_context*, const char*, size_t, mrb_bool*); 12 | 13 | #endif /* APIPRINT_H_ */ 14 | -------------------------------------------------------------------------------- /mrbgems/mruby-bin-debugger/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('mruby-bin-debugger') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.summary = 'mruby debugger command' 5 | 6 | spec.add_dependency('mruby-eval', :core => 'mruby-eval') 7 | 8 | spec.bins = %w(mrdb) 9 | end 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # / 2 | *.bak 3 | *.d 4 | *.o 5 | /benchmark/**/*.dat 6 | /benchmark/*.pdf 7 | /benchmark/*.png 8 | *.orig 9 | *.pdb 10 | *.rej 11 | *.sav 12 | *.swp 13 | *.tmp 14 | *~ 15 | .DS_Store 16 | .ccmalloc 17 | .svn 18 | /.git 19 | cscope.out 20 | /src/y.tab.c 21 | /bin 22 | /build 23 | /mruby-source-*.gem 24 | doc/api 25 | .yardoc 26 | -------------------------------------------------------------------------------- /mrbgems/full-core.gembox: -------------------------------------------------------------------------------- 1 | MRuby::GemBox.new do |conf| 2 | conf.gem :core => "mruby-sprintf" 3 | conf.gem :core => "mruby-print" 4 | 5 | Dir.glob("#{root}/mrbgems/mruby-*/mrbgem.rake") do |x| 6 | g = File.basename File.dirname x 7 | conf.gem :core => g unless g =~ /^mruby-(print|sprintf|bin-debugger|test)$/ 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /mrbgems/mruby-enumerator/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('mruby-enumerator') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.add_dependency('mruby-fiber', :core => 'mruby-fiber') 5 | spec.add_dependency 'mruby-enum-ext', :core => 'mruby-enum-ext' 6 | spec.summary = 'Enumerator class' 7 | end 8 | -------------------------------------------------------------------------------- /mrbgems/mruby-hash-ext/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('mruby-hash-ext') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.summary = 'Hash class extension' 5 | spec.add_dependency 'mruby-enum-ext', :core => 'mruby-enum-ext' 6 | spec.add_dependency 'mruby-array-ext', :core => 'mruby-array-ext' 7 | end 8 | -------------------------------------------------------------------------------- /mrbgems/mruby-enum-lazy/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('mruby-enum-lazy') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.summary = 'Enumerator::Lazy class' 5 | spec.add_dependency('mruby-enumerator', :core => 'mruby-enumerator') 6 | spec.add_dependency('mruby-enum-ext', :core => 'mruby-enum-ext') 7 | end 8 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # mruby is using Rake (http://rake.rubyforge.org) as a build tool. 2 | # We provide a minimalistic version called minirake inside of our 3 | # codebase. 4 | 5 | RAKE = ruby ./minirake 6 | 7 | all : 8 | $(RAKE) 9 | .PHONY : all 10 | 11 | test : all 12 | $(RAKE) test 13 | .PHONY : test 14 | 15 | clean : 16 | $(RAKE) clean 17 | .PHONY : clean 18 | -------------------------------------------------------------------------------- /test/t/argumenterror.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # ArgumentError ISO Test 3 | 4 | assert('ArgumentError', '15.2.24') do 5 | e2 = nil 6 | a = [] 7 | begin 8 | # this will cause an exception due to the wrong arguments 9 | a[] 10 | rescue => e1 11 | e2 = e1 12 | end 13 | 14 | assert_equal(Class, ArgumentError.class) 15 | assert_equal(ArgumentError, e2.class) 16 | end 17 | -------------------------------------------------------------------------------- /mrbgems/mruby-bin-debugger/tools/mrdb/mrdbconf.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** mrdbconf.h - mruby debugger configuration 3 | ** 4 | */ 5 | 6 | #ifndef MRDBCONF_H 7 | #define MRDBCONF_H 8 | 9 | /* configuration options: */ 10 | /* maximum size for command buffer */ 11 | #define MAX_COMMAND_LINE 1024 12 | 13 | /* maximum number of setable breakpoint */ 14 | #define MAX_BREAKPOINT 5 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /test/t/localjumperror.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # LocalJumpError ISO Test 3 | 4 | assert('LocalJumpError', '15.2.25') do 5 | assert_equal Class, LocalJumpError.class 6 | # assert_raise LocalJumpError do 7 | # # this will cause an exception due to the wrong location 8 | # retry 9 | # end 10 | end 11 | 12 | # TODO 15.2.25.2.1 LocalJumpError#exit_value 13 | # TODO 15.2.25.2.2 LocalJumpError#reason 14 | -------------------------------------------------------------------------------- /NEWS: -------------------------------------------------------------------------------- 1 | * NEWS 2 | 3 | This document is a list of user visible feature changes made between 4 | releases except for bug fixes. 5 | 6 | Note that each entry is kept so brief that no reason behind or 7 | reference information is supplied with. For a full list of changes 8 | with all sufficient information, see the ChangeLog file. 9 | 10 | 11 | ** Information about first release v1.0.0 12 | 13 | 14 | -------------------------------------------------------------------------------- /mrbgems/mruby-bin-debugger/tools/mrdb/apilist.h: -------------------------------------------------------------------------------- 1 | /* 2 | * apilist.h 3 | */ 4 | 5 | #ifndef APILIST_H_ 6 | #define APILIST_H_ 7 | 8 | #include 9 | #include "mrdb.h" 10 | 11 | int32_t mrb_debug_list(mrb_state *, mrb_debug_context *, char *, uint16_t, uint16_t); 12 | char* mrb_debug_get_source(mrb_state *, mrdb_state *, const char *, const char *); 13 | 14 | #endif /* APILIST_H_ */ 15 | -------------------------------------------------------------------------------- /mrbgems/mruby-bin-mirb/bintest/mirb.rb: -------------------------------------------------------------------------------- 1 | require 'open3' 2 | 3 | assert('mirb normal operations') do 4 | o, s = Open3.capture2('bin/mirb', :stdin_data => "a=1\nb=2\na+b\n") 5 | assert_true o.include?('=> 3') 6 | assert_true o.include?('=> 2') 7 | end 8 | 9 | assert('regression for #1563') do 10 | o, s = Open3.capture2('bin/mirb', :stdin_data => "a=1;b=2;c=3\nb\nc") 11 | assert_true o.include?('=> 3') 12 | end 13 | -------------------------------------------------------------------------------- /mrbgems/mruby-error/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('mruby-error') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.summary = 'extensional error handling' 5 | 6 | if build.cxx_abi_enabled? 7 | @objs << build.compile_as_cxx("#{spec.dir}/src/exception.c", "#{spec.build_dir}/src/exception.cxx") 8 | @objs.delete_if { |v| v == objfile("#{spec.build_dir}/src/exception") } 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /mrbgems/mruby-exit/src/mruby-exit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static mrb_value 5 | f_exit(mrb_state *mrb, mrb_value self) 6 | { 7 | mrb_int i = EXIT_SUCCESS; 8 | 9 | mrb_get_args(mrb, "|i", &i); 10 | exit(i); 11 | /* not reached */ 12 | return mrb_nil_value(); 13 | } 14 | 15 | void 16 | mrb_mruby_exit_gem_init(mrb_state* mrb) 17 | { 18 | mrb_define_method(mrb, mrb->kernel_module, "exit", f_exit, MRB_ARGS_OPT(1)); 19 | } 20 | 21 | void 22 | mrb_mruby_exit_gem_final(mrb_state* mrb) 23 | { 24 | } 25 | -------------------------------------------------------------------------------- /src/value_array.h: -------------------------------------------------------------------------------- 1 | #ifndef MRB_VALUE_ARRAY_H__ 2 | #define MRB_VALUE_ARRAY_H__ 3 | 4 | #include 5 | 6 | static inline void 7 | value_move(mrb_value *s1, const mrb_value *s2, size_t n) 8 | { 9 | if (s1 > s2 && s1 < s2 + n) 10 | { 11 | s1 += n; 12 | s2 += n; 13 | while (n-- > 0) { 14 | *--s1 = *--s2; 15 | } 16 | } 17 | else if (s1 != s2) { 18 | while (n-- > 0) { 19 | *s1++ = *s2++; 20 | } 21 | } 22 | else { 23 | /* nothing to do. */ 24 | } 25 | } 26 | 27 | #endif /* MRB_VALUE_ARRAY_H__ */ 28 | -------------------------------------------------------------------------------- /mrblib/00class.rb: -------------------------------------------------------------------------------- 1 | class Module 2 | # 15.2.2.4.12 3 | def attr_accessor(*names) 4 | attr_reader(*names) 5 | attr_writer(*names) 6 | end 7 | # 15.2.2.4.11 8 | def attr(name) 9 | attr_reader(name) 10 | end 11 | 12 | # 15.2.2.4.27 13 | def include(*args) 14 | args.reverse.each do |m| 15 | m.append_features(self) 16 | m.included(self) 17 | end 18 | end 19 | 20 | def prepend(*args) 21 | args.reverse.each do |m| 22 | m.prepend_features(self) 23 | m.prepended(self) 24 | end 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /examples/mrbgems/c_extension_example/src/example.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static mrb_value 5 | mrb_c_method(mrb_state *mrb, mrb_value self) 6 | { 7 | puts("A C Extension"); 8 | return self; 9 | } 10 | 11 | void 12 | mrb_c_extension_example_gem_init(mrb_state* mrb) { 13 | struct RClass *class_cextension = mrb_define_module(mrb, "CExtension"); 14 | mrb_define_class_method(mrb, class_cextension, "c_method", mrb_c_method, MRB_ARGS_NONE()); 15 | } 16 | 17 | void 18 | mrb_c_extension_example_gem_final(mrb_state* mrb) { 19 | /* finalizer */ 20 | } 21 | -------------------------------------------------------------------------------- /mrbgems/mruby-bin-debugger/tools/mrdb/mrdberror.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** mrdberror.h - mruby debugger error code 3 | ** 4 | */ 5 | 6 | #ifndef MRDBERROR_H 7 | #define MRDBERROR_H 8 | 9 | #define MRB_DEBUG_OK (0) 10 | #define MRB_DEBUG_NOBUF (-1) 11 | #define MRB_DEBUG_INVALID_ARGUMENT (-2) 12 | 13 | #define MRB_DEBUG_BREAK_INVALID_LINENO (-11) 14 | #define MRB_DEBUG_BREAK_INVALID_FILE (-12) 15 | #define MRB_DEBUG_BREAK_INVALID_NO (-13) 16 | #define MRB_DEBUG_BREAK_NUM_OVER (-14) 17 | #define MRB_DEBUG_BREAK_NO_OVER (-15) 18 | 19 | #endif 20 | 21 | -------------------------------------------------------------------------------- /benchmark/build_config_boxing.rb: -------------------------------------------------------------------------------- 1 | MRuby::Build.new do |conf| 2 | toolchain :gcc 3 | end 4 | 5 | MRuby::Build.new('no_boxing') do |conf| 6 | toolchain :gcc 7 | 8 | conf.gembox 'default' 9 | end 10 | 11 | MRuby::Build.new('word_boxing') do |conf| 12 | toolchain :gcc 13 | 14 | conf.gembox 'default' 15 | conf.compilers.each do |c| 16 | c.defines += %w(MRB_WORD_BOXING) 17 | end 18 | end 19 | 20 | MRuby::Build.new('nan_boxing') do |conf| 21 | toolchain :gcc 22 | 23 | conf.gembox 'default' 24 | conf.compilers.each do |c| 25 | c.defines += %w(MRB_NAN_BOXING) 26 | end 27 | end 28 | 29 | -------------------------------------------------------------------------------- /examples/mrbgems/c_and_ruby_extension_example/src/example.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static mrb_value 5 | mrb_c_method(mrb_state *mrb, mrb_value self) 6 | { 7 | puts("A C Extension"); 8 | return self; 9 | } 10 | 11 | void 12 | mrb_c_and_ruby_extension_example_gem_init(mrb_state* mrb) { 13 | struct RClass *class_cextension = mrb_define_module(mrb, "CRubyExtension"); 14 | mrb_define_class_method(mrb, class_cextension, "c_method", mrb_c_method, MRB_ARGS_NONE()); 15 | } 16 | 17 | void 18 | mrb_c_and_ruby_extension_example_gem_final(mrb_state* mrb) { 19 | /* finalizer */ 20 | } 21 | -------------------------------------------------------------------------------- /mrbgems/mruby-class-ext/src/class.c: -------------------------------------------------------------------------------- 1 | #include "mruby.h" 2 | #include "mruby/class.h" 3 | #include "mruby/string.h" 4 | 5 | static mrb_value 6 | mrb_mod_name(mrb_state *mrb, mrb_value self) 7 | { 8 | mrb_value name = mrb_class_path(mrb, mrb_class_ptr(self)); 9 | return mrb_nil_p(name)? name : mrb_str_dup(mrb, name); 10 | } 11 | 12 | void 13 | mrb_mruby_class_ext_gem_init(mrb_state *mrb) 14 | { 15 | struct RClass *mod = mrb->module_class; 16 | 17 | mrb_define_method(mrb, mod, "name", mrb_mod_name, MRB_ARGS_NONE()); 18 | } 19 | 20 | void 21 | mrb_mruby_class_ext_gem_final(mrb_state *mrb) 22 | { 23 | } 24 | -------------------------------------------------------------------------------- /mrbgems/mruby-toplevel-ext/test/toplevel.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # Toplevel Self(Ext) Test 3 | 4 | assert('Toplevel#include') do 5 | module ToplevelTestModule1 6 | def method_foo 7 | :foo 8 | end 9 | 10 | CONST_BAR = :bar 11 | end 12 | 13 | module ToplevelTestModule2 14 | CONST_BAR = :bar2 15 | end 16 | 17 | self.include ToplevelTestModule2, ToplevelTestModule1 18 | 19 | assert_true self.class.included_modules.include?( ToplevelTestModule1 ) 20 | assert_true self.class.included_modules.include?( ToplevelTestModule2 ) 21 | assert_equal :foo, method_foo 22 | assert_equal :bar2, CONST_BAR 23 | end 24 | 25 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | version: "{build}" 2 | 3 | os: Visual Studio 2015 4 | 5 | clone_depth: 1 6 | 7 | 8 | environment: 9 | matrix: 10 | # Visual Studio 2015 64bit 11 | - visualcpp: C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat 12 | machine: amd64 13 | 14 | # Visual Studio 2013 64bit 15 | - visualcpp: C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat 16 | machine: amd64 17 | 18 | 19 | init: 20 | - call "%visualcpp%" %machine% 21 | # For using bison.exe 22 | - set PATH=%PATH%;C:\cygwin\bin; 23 | 24 | 25 | build_script: 26 | - set MRUBY_CONFIG=appveyor_config.rb 27 | - ruby .\minirake test 28 | -------------------------------------------------------------------------------- /mrbgems/mruby-numeric-ext/test/numeric.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # Numeric(Ext) Test 3 | 4 | assert('Integer#chr') do 5 | assert_equal("A", 65.chr) 6 | assert_equal("B", 0x42.chr) 7 | 8 | # multibyte encoding (not support yet) 9 | assert_raise(RangeError) { 256.chr } 10 | end 11 | 12 | assert('Integer#div') do 13 | assert_equal 52, 365.div(7) 14 | end 15 | 16 | assert('Float#div') do 17 | assert_float 52, 365.2425.div(7) 18 | end 19 | 20 | assert('Integer#zero?') do 21 | assert_equal true, 0.zero? 22 | assert_equal false, 1.zero? 23 | end 24 | 25 | assert('Integer#nonzero?') do 26 | assert_equal nil, 0.nonzero? 27 | assert_equal 1000, 1000.nonzero? 28 | end 29 | -------------------------------------------------------------------------------- /mrbgems/mruby-bin-mrbc/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new 'mruby-bin-mrbc' do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.summary = 'mruby compiler executable' 5 | 6 | spec.add_dependency 'mruby-compiler', :core => 'mruby-compiler' 7 | 8 | exec = exefile("#{build.build_dir}/bin/mrbc") 9 | mrbc_objs = Dir.glob("#{spec.dir}/tools/mrbc/*.c").map { |f| objfile(f.pathmap("#{spec.build_dir}/tools/mrbc/%n")) }.flatten 10 | 11 | file exec => mrbc_objs + [libfile("#{build.build_dir}/lib/libmruby_core")] do |t| 12 | build.linker.run t.name, t.prerequisites 13 | end 14 | 15 | build.bins << 'mrbc' unless build.bins.find { |v| v == 'mrbc' } 16 | end 17 | -------------------------------------------------------------------------------- /mruby-source.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'mruby/source' 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = "mruby-source" 8 | spec.version = MRuby::Source::MRUBY_VERSION 9 | spec.authors = [ MRuby::Source::MRUBY_AUTHOR ] 10 | 11 | spec.summary = %q{MRuby source code wrapper.} 12 | spec.description = %q{MRuby source code wrapper for use with Ruby libs.} 13 | spec.homepage = "http://www.mruby.org/" 14 | spec.license = "MIT" 15 | 16 | spec.files = `git ls-files -z`.split("\x0") 17 | spec.require_paths = ["lib"] 18 | end 19 | -------------------------------------------------------------------------------- /mrbgems/mruby-numeric-ext/src/numeric_ext.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static mrb_value 5 | mrb_int_chr(mrb_state *mrb, mrb_value x) 6 | { 7 | mrb_int chr; 8 | char c; 9 | 10 | chr = mrb_fixnum(x); 11 | if (chr >= (1 << CHAR_BIT)) { 12 | mrb_raisef(mrb, E_RANGE_ERROR, "%S out of char range", x); 13 | } 14 | c = (char)chr; 15 | 16 | return mrb_str_new(mrb, &c, 1); 17 | } 18 | 19 | void 20 | mrb_mruby_numeric_ext_gem_init(mrb_state* mrb) 21 | { 22 | struct RClass *i = mrb_class_get(mrb, "Integer"); 23 | 24 | mrb_define_method(mrb, i, "chr", mrb_int_chr, MRB_ARGS_NONE()); 25 | } 26 | 27 | void 28 | mrb_mruby_numeric_ext_gem_final(mrb_state* mrb) 29 | { 30 | } 31 | -------------------------------------------------------------------------------- /test/t/symbol.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # Symbol ISO Test 3 | 4 | assert('Symbol') do 5 | assert_equal :"a", :a 6 | assert_equal :"a#{1}", :a1 7 | assert_equal :'a', :a 8 | assert_equal :'a#{1}', :"a\#{1}" 9 | end 10 | 11 | assert('Symbol', '15.2.11') do 12 | assert_equal Class, Symbol.class 13 | end 14 | 15 | assert('Symbol#===', '15.2.11.3.1') do 16 | assert_true :abc == :abc 17 | assert_false :abc == :cba 18 | end 19 | 20 | assert('Symbol#id2name', '15.2.11.3.2') do 21 | assert_equal 'abc', :abc.id2name 22 | end 23 | 24 | assert('Symbol#to_s', '15.2.11.3.3') do 25 | assert_equal 'abc', :abc.to_s 26 | end 27 | 28 | assert('Symbol#to_sym', '15.2.11.3.4') do 29 | assert_equal :abc, :abc.to_sym 30 | end 31 | -------------------------------------------------------------------------------- /test/t/nameerror.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # NameError ISO Test 3 | 4 | assert('NameError', '15.2.31') do 5 | assert_equal Class, NameError.class 6 | end 7 | 8 | assert('NameError#name', '15.2.31.2.1') do 9 | 10 | # This check is not duplicate with 15.2.31.2.2 check. 11 | # Because the NameError in this test is generated in 12 | # C API. 13 | class TestDummy 14 | alias foo bar 15 | rescue NameError => e 16 | $test_dummy_result = e.name 17 | end 18 | 19 | assert_equal :bar, $test_dummy_result 20 | end 21 | 22 | assert('NameError#initialize', '15.2.31.2.2') do 23 | e = NameError.new('a', :foo) 24 | 25 | assert_equal NameError, e.class 26 | assert_equal 'a', e.message 27 | assert_equal :foo, e.name 28 | end 29 | -------------------------------------------------------------------------------- /mrbgems/mruby-object-ext/mrblib/object.rb: -------------------------------------------------------------------------------- 1 | class Object 2 | ## 3 | # call-seq: 4 | # obj.tap{|x|...} -> obj 5 | # 6 | # Yields x to the block, and then returns x. 7 | # The primary purpose of this method is to "tap into" a method chain, 8 | # in order to perform operations on intermediate results within the chain. 9 | # 10 | # (1..10) .tap {|x| puts "original: #{x.inspect}"} 11 | # .to_a .tap {|x| puts "array: #{x.inspect}"} 12 | # .select {|x| x%2==0} .tap {|x| puts "evens: #{x.inspect}"} 13 | # .map { |x| x*x } .tap {|x| puts "squares: #{x.inspect}"} 14 | # 15 | def tap 16 | yield self 17 | self 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /test/bintest.rb: -------------------------------------------------------------------------------- 1 | $:.unshift File.dirname(File.dirname(File.expand_path(__FILE__))) 2 | require 'test/assert.rb' 3 | 4 | def cmd(s) 5 | case RbConfig::CONFIG['host_os'] 6 | when /mswin(?!ce)|mingw|cygwin|bccwin/ 7 | "bin\\#{s}.exe" 8 | else 9 | "bin/#{s}" 10 | end 11 | end 12 | 13 | def shellquote(s) 14 | case RbConfig::CONFIG['host_os'] 15 | when /mswin(?!ce)|mingw|cygwin|bccwin/ 16 | "\"#{s}\"" 17 | else 18 | "'#{s}'" 19 | end 20 | end 21 | 22 | ARGV.each do |gem| 23 | case RbConfig::CONFIG['host_os'] 24 | when /mswin(?!ce)|mingw|cygwin|bccwin/ 25 | gem = gem.gsub('\\', '/') 26 | end 27 | 28 | Dir["#{gem}/bintest/**/*.rb"].each do |file| 29 | load file 30 | end 31 | end 32 | 33 | load 'test/report.rb' 34 | -------------------------------------------------------------------------------- /src/mruby_core.rake: -------------------------------------------------------------------------------- 1 | MRuby.each_target do 2 | current_dir = File.dirname(__FILE__).relative_path_from(Dir.pwd) 3 | relative_from_root = File.dirname(__FILE__).relative_path_from(MRUBY_ROOT) 4 | current_build_dir = "#{build_dir}/#{relative_from_root}" 5 | 6 | objs = Dir.glob("#{current_dir}/*.c").map { |f| 7 | next nil if cxx_abi_enabled? and f =~ /(error|vm).c$/ 8 | objfile(f.pathmap("#{current_build_dir}/%n")) 9 | }.compact 10 | 11 | if cxx_abi_enabled? 12 | objs += %w(vm error).map { |v| compile_as_cxx "#{current_dir}/#{v}.c", "#{current_build_dir}/#{v}.cxx" } 13 | end 14 | self.libmruby << objs 15 | 16 | file libfile("#{build_dir}/lib/libmruby_core") => objs do |t| 17 | archiver.run t.name, t.prerequisites 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /mrbgems/mruby-bin-mruby-config/mruby-config: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | while [ $# -gt 0 ]; do 4 | case $1 in 5 | --cflags) echo MRUBY_CFLAGS;; 6 | --ldflags) echo MRUBY_LDFLAGS;; 7 | --ldflags-before-libs) echo MRUBY_LDFLAGS_BEFORE_LIBS;; 8 | --libs) echo MRUBY_LIBS;; 9 | --help) echo "Usage: mruby-config [switches]" 10 | echo " switches:" 11 | echo " --cflags print flags passed to compiler" 12 | echo " --ldflags print flags passed to linker" 13 | echo " --ldflags-before-libs print flags passwd to linker before linked libraries" 14 | echo " --libs print linked libraries" 15 | exit 0;; 16 | esac 17 | shift 18 | done 19 | -------------------------------------------------------------------------------- /test/t/true.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # TrueClass ISO Test 3 | 4 | assert('TrueClass', '15.2.5') do 5 | assert_equal Class, TrueClass.class 6 | end 7 | 8 | assert('TrueClass true', '15.2.5.1') do 9 | assert_true true 10 | assert_equal TrueClass, true.class 11 | assert_false TrueClass.method_defined? :new 12 | end 13 | 14 | assert('TrueClass#&', '15.2.5.3.1') do 15 | assert_true true.&(true) 16 | assert_false true.&(false) 17 | end 18 | 19 | assert('TrueClass#^', '15.2.5.3.2') do 20 | assert_false true.^(true) 21 | assert_true true.^(false) 22 | end 23 | 24 | assert('TrueClass#to_s', '15.2.5.3.3') do 25 | assert_equal 'true', true.to_s 26 | end 27 | 28 | assert('TrueClass#|', '15.2.5.3.4') do 29 | assert_true true.|(true) 30 | assert_true true.|(false) 31 | end 32 | -------------------------------------------------------------------------------- /test/t/bs_literal.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # Bootstrap test for literals 3 | 4 | assert('BS Literal 1') do 5 | assert_true true 6 | end 7 | 8 | assert('BS Literal 2') do 9 | assert_equal TrueClass, true.class 10 | end 11 | 12 | assert('BS Literal 3') do 13 | assert_false false 14 | end 15 | 16 | assert('BS Literal 4') do 17 | assert_equal FalseClass, false.class 18 | end 19 | 20 | assert('BS Literal 5') do 21 | assert_equal 'nil', nil.inspect 22 | end 23 | 24 | assert('BS Literal 6') do 25 | assert_equal NilClass, nil.class 26 | end 27 | 28 | assert('BS Literal 7') do 29 | assert_equal Symbol, :sym.class 30 | end 31 | 32 | assert('BS Literal 8') do 33 | assert_equal 1234, 1234 34 | end 35 | 36 | assert('BS Literal 9') do 37 | assert_equal Fixnum, 1234.class 38 | end 39 | -------------------------------------------------------------------------------- /mrbgems/mruby-sprintf/src/kernel.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** kernel.c - Kernel module suppliment 3 | ** 4 | ** See Copyright Notice in mruby.h 5 | */ 6 | 7 | #include 8 | 9 | mrb_value mrb_f_sprintf(mrb_state *mrb, mrb_value obj); /* in sprintf.c */ 10 | 11 | void 12 | mrb_mruby_sprintf_gem_init(mrb_state* mrb) 13 | { 14 | struct RClass *krn; 15 | 16 | if (mrb->kernel_module == NULL) { 17 | mrb->kernel_module = mrb_define_module(mrb, "Kernel"); /* Might be PARANOID. */ 18 | } 19 | krn = mrb->kernel_module; 20 | 21 | mrb_define_method(mrb, krn, "sprintf", mrb_f_sprintf, MRB_ARGS_ANY()); 22 | mrb_define_method(mrb, krn, "format", mrb_f_sprintf, MRB_ARGS_ANY()); 23 | } 24 | 25 | void 26 | mrb_mruby_sprintf_gem_final(mrb_state* mrb) 27 | { 28 | /* nothing to do. */ 29 | } 30 | 31 | -------------------------------------------------------------------------------- /test/t/false.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # FalseClass ISO Test 3 | 4 | assert('FalseClass', '15.2.6') do 5 | assert_equal Class, FalseClass.class 6 | end 7 | 8 | assert('FalseClass false', '15.2.6.1') do 9 | assert_false false 10 | assert_equal FalseClass, false.class 11 | assert_false FalseClass.method_defined? :new 12 | end 13 | 14 | assert('FalseClass#&', '15.2.6.3.1') do 15 | assert_false false.&(true) 16 | assert_false false.&(false) 17 | end 18 | 19 | assert('FalseClass#^', '15.2.6.3.2') do 20 | assert_true false.^(true) 21 | assert_false false.^(false) 22 | end 23 | 24 | assert('FalseClass#to_s', '15.2.6.3.3') do 25 | assert_equal 'false', false.to_s 26 | end 27 | 28 | assert('FalseClass#|', '15.2.6.3.4') do 29 | assert_true false.|(true) 30 | assert_false false.|(false) 31 | end 32 | -------------------------------------------------------------------------------- /mrblib/mrblib.rake: -------------------------------------------------------------------------------- 1 | MRuby.each_target do 2 | current_dir = File.dirname(__FILE__) 3 | relative_from_root = File.dirname(__FILE__).relative_path_from(MRUBY_ROOT) 4 | current_build_dir = "#{build_dir}/#{relative_from_root}" 5 | 6 | self.libmruby << objfile("#{current_build_dir}/mrblib") 7 | 8 | file objfile("#{current_build_dir}/mrblib") => "#{current_build_dir}/mrblib.c" 9 | file "#{current_build_dir}/mrblib.c" => [mrbcfile, __FILE__] + Dir.glob("#{current_dir}/*.rb").sort do |t| 10 | _, _, *rbfiles = t.prerequisites 11 | FileUtils.mkdir_p File.dirname(t.name) 12 | open(t.name, 'w') do |f| 13 | _pp "GEN", "*.rb", "#{t.name.relative_path}" 14 | f.puts File.read("#{current_dir}/init_mrblib.c") 15 | mrbc.run f, rbfiles, 'mrblib_irep' 16 | end 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /mrbgems/mruby-sprintf/test/sprintf.rb: -------------------------------------------------------------------------------- 1 | #assert('Kernel.sprintf') do 2 | #end 3 | 4 | assert('String#%') do 5 | assert_equal "one=1", "one=%d" % 1 6 | assert_equal "1 one 1.0", "%d %s %3.1f" % [ 1, "one", 1.01 ] 7 | assert_equal "123 < 456", "%{num} < %s" % { num: 123, str: "456" } 8 | assert_equal 15, ("%b" % (1<<14)).size 9 | end 10 | 11 | assert("String#% with invalid chr") do 12 | begin 13 | class Fixnum 14 | alias_method :chr_, :chr if method_defined?(:chr) 15 | 16 | def chr 17 | nil 18 | end 19 | end 20 | 21 | assert_raise TypeError do 22 | "%c" % 0 23 | end 24 | ensure 25 | class Fixnum 26 | if method_defined?(:chr_) 27 | alias_method :chr, :chr_ 28 | remove_method :chr_ 29 | end 30 | end 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /mrbgems/mruby-compiler/bintest/mrbc.rb: -------------------------------------------------------------------------------- 1 | require 'tempfile' 2 | 3 | assert('Compiling multiple files without new line in last line. #2361') do 4 | a, b, out = Tempfile.new('a.rb'), Tempfile.new('b.rb'), Tempfile.new('out.mrb') 5 | a.write('module A; end') 6 | a.flush 7 | b.write('module B; end') 8 | b.flush 9 | result = `#{cmd('mrbc')} -c -o #{out.path} #{a.path} #{b.path} 2>&1` 10 | assert_equal "#{cmd('mrbc')}:#{a.path}:Syntax OK", result.chomp 11 | assert_equal 0, $?.exitstatus 12 | end 13 | 14 | assert('parsing function with void argument') do 15 | a, out = Tempfile.new('a.rb'), Tempfile.new('out.mrb') 16 | a.write('f ()') 17 | a.flush 18 | result = `#{cmd('mrbc')} -c -o #{out.path} #{a.path} 2>&1` 19 | assert_equal "#{cmd('mrbc')}:#{a.path}:Syntax OK", result.chomp 20 | assert_equal 0, $?.exitstatus 21 | end 22 | -------------------------------------------------------------------------------- /src/version.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void 5 | mrb_init_version(mrb_state* mrb) 6 | { 7 | mrb_value mruby_version = mrb_str_new_lit(mrb, MRUBY_VERSION); 8 | 9 | mrb_define_global_const(mrb, "RUBY_VERSION", mrb_str_new_lit(mrb, MRUBY_RUBY_VERSION)); 10 | mrb_define_global_const(mrb, "RUBY_ENGINE", mrb_str_new_lit(mrb, MRUBY_RUBY_ENGINE)); 11 | mrb_define_global_const(mrb, "RUBY_ENGINE_VERSION", mruby_version); 12 | mrb_define_global_const(mrb, "MRUBY_VERSION", mruby_version); 13 | mrb_define_global_const(mrb, "MRUBY_RELEASE_NO", mrb_fixnum_value(MRUBY_RELEASE_NO)); 14 | mrb_define_global_const(mrb, "MRUBY_RELEASE_DATE", mrb_str_new_lit(mrb, MRUBY_RELEASE_DATE)); 15 | mrb_define_global_const(mrb, "MRUBY_DESCRIPTION", mrb_str_new_lit(mrb, MRUBY_DESCRIPTION)); 16 | mrb_define_global_const(mrb, "MRUBY_COPYRIGHT", mrb_str_new_lit(mrb, MRUBY_COPYRIGHT)); 17 | } 18 | -------------------------------------------------------------------------------- /test/t/nil.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # NilClass ISO Test 3 | 4 | assert('NilClass', '15.2.4') do 5 | assert_equal Class, NilClass.class 6 | end 7 | 8 | assert('NilClass', '15.2.4.1') do 9 | assert_equal NilClass, nil.class 10 | assert_false NilClass.method_defined? :new 11 | end 12 | 13 | assert('NilClass#&', '15.2.4.3.1') do 14 | assert_false nil.&(true) 15 | assert_false nil.&(nil) 16 | end 17 | 18 | assert('NilClass#^', '15.2.4.3.2') do 19 | assert_true nil.^(true) 20 | assert_false nil.^(false) 21 | end 22 | 23 | assert('NilClass#|', '15.2.4.3.3') do 24 | assert_true nil.|(true) 25 | assert_false nil.|(false) 26 | end 27 | 28 | assert('NilClass#nil?', '15.2.4.3.4') do 29 | assert_true nil.nil? 30 | end 31 | 32 | assert('NilClass#to_s', '15.2.4.3.5') do 33 | assert_equal '', nil.to_s 34 | end 35 | 36 | assert('safe navigation') do 37 | assert_nil nil&.size 38 | assert_equal 0, []&.size 39 | end 40 | -------------------------------------------------------------------------------- /examples/mrbgems/c_extension_example/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('c_extension_example') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | 5 | # Add compile flags 6 | # spec.cc.flags << '-g' 7 | 8 | # Add cflags to all 9 | # spec.mruby.cc.flags << '-g' 10 | 11 | # Add libraries 12 | # spec.linker.libraries << 'external_lib' 13 | 14 | # Default build files 15 | # spec.rbfiles = Dir.glob("#{dir}/mrblib/*.rb") 16 | # spec.objs = Dir.glob("#{dir}/src/*.{c,cpp,m,asm,S}").map { |f| objfile(f.relative_path_from(dir).pathmap("#{build_dir}/%X")) } 17 | # spec.test_rbfiles = Dir.glob("#{dir}/test/*.rb") 18 | # spec.test_objs = Dir.glob("#{dir}/test/*.{c,cpp,m,asm,S}").map { |f| objfile(f.relative_path_from(dir).pathmap("#{build_dir}/%X")) } 19 | # spec.test_preload = 'test/assert.rb' 20 | 21 | # Values accessible as TEST_ARGS inside test scripts 22 | # spec.test_args = {'tmp_dir' => Dir::tmpdir} 23 | end 24 | -------------------------------------------------------------------------------- /mrbgems/mruby-bin-mruby-config/mruby-config.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :top 4 | shift 5 | if "%0" equ "" goto :eof 6 | if "%0" equ "--cflags" goto cflags 7 | if "%0" equ "--ldflags" goto ldflags 8 | if "%0" equ "--ldflags-before-libs" goto ldflagsbeforelibs 9 | if "%0" equ "--libs" goto libs 10 | if "%0" equ "--help" goto showhelp 11 | echo Invalid Option 12 | goto :eof 13 | 14 | :cflags 15 | echo MRUBY_CFLAGS 16 | goto top 17 | 18 | :libs 19 | echo MRUBY_LIBS 20 | goto top 21 | 22 | :ldflags 23 | echo MRUBY_LDFLAGS 24 | goto top 25 | 26 | :ldflagsbeforelibs 27 | echo MRUBY_LDFLAGS_BEFORE_LIBS 28 | goto top 29 | 30 | :showhelp 31 | echo Usage: mruby-config [switches] 32 | echo switches: 33 | echo --cflags print flags passed to compiler 34 | echo --ldflags print flags passed to linker 35 | echo --ldflags-before-libs print flags passwd to linker before linked libraries 36 | echo --libs print linked libraries 37 | -------------------------------------------------------------------------------- /examples/mrbgems/c_and_ruby_extension_example/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('c_and_ruby_extension_example') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | 5 | # Add compile flags 6 | # spec.cc.flags << '' 7 | 8 | # Add cflags to all 9 | # spec.mruby.cc.flags << '-g' 10 | 11 | # Add libraries 12 | # spec.linker.libraries << 'external_lib' 13 | 14 | # Default build files 15 | # spec.rbfiles = Dir.glob("#{dir}/mrblib/*.rb") 16 | # spec.objs = Dir.glob("#{dir}/src/*.{c,cpp,m,asm,S}").map { |f| objfile(f.relative_path_from(dir).pathmap("#{build_dir}/%X")) } 17 | # spec.test_rbfiles = Dir.glob("#{dir}/test/*.rb") 18 | # spec.test_objs = Dir.glob("#{dir}/test/*.{c,cpp,m,asm,S}").map { |f| objfile(f.relative_path_from(dir).pathmap("#{build_dir}/%X")) } 19 | # spec.test_preload = 'test/assert.rb' 20 | 21 | # Values accessible as TEST_ARGS inside test scripts 22 | # spec.test_args = {'tmp_dir' => Dir::tmpdir} 23 | end 24 | -------------------------------------------------------------------------------- /src/crc.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** crc.c - calculate CRC 3 | ** 4 | ** See Copyright Notice in mruby.h 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | /* Calculate CRC (CRC-16-CCITT) 12 | ** 13 | ** 0000_0000_0000_0000_0000_0000_0000_0000 14 | ** ^|------- CRC -------|- work --| 15 | ** carry 16 | */ 17 | #define CRC_16_CCITT 0x11021ul /* x^16+x^12+x^5+1 */ 18 | #define CRC_XOR_PATTERN (CRC_16_CCITT << 8) 19 | #define CRC_CARRY_BIT (0x01000000) 20 | 21 | uint16_t 22 | calc_crc_16_ccitt(const uint8_t *src, size_t nbytes, uint16_t crc) 23 | { 24 | size_t ibyte; 25 | uint32_t ibit; 26 | uint32_t crcwk = crc << 8; 27 | 28 | for (ibyte = 0; ibyte < nbytes; ibyte++) { 29 | crcwk |= *src++; 30 | for (ibit = 0; ibit < CHAR_BIT; ibit++) { 31 | crcwk <<= 1; 32 | if (crcwk & CRC_CARRY_BIT) { 33 | crcwk ^= CRC_XOR_PATTERN; 34 | } 35 | } 36 | } 37 | return (uint16_t)(crcwk >> 8); 38 | } 39 | 40 | -------------------------------------------------------------------------------- /mrbgems/mruby-range-ext/test/range.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # Range(Ext) Test 3 | 4 | assert('Range#cover?') do 5 | assert_true ("a".."z").cover?("c") 6 | assert_true !("a".."z").cover?("5") 7 | assert_true ("a".."z").cover?("cc") 8 | end 9 | 10 | assert('Range#first') do 11 | assert_equal 10, (10..20).first 12 | assert_equal [10, 11, 12], (10..20).first(3) 13 | assert_equal [0, 1, 2], (0..Float::INFINITY).first(3) 14 | end 15 | 16 | assert('Range#last') do 17 | assert_equal 20, (10..20).last 18 | assert_equal 20, (10...20).last 19 | assert_equal [18, 19, 20], (10..20).last(3) 20 | assert_equal [17, 18, 19], (10...20).last(3) 21 | end 22 | 23 | assert('Range#size') do 24 | assert_equal 42, (1..42).size 25 | assert_equal 41, (1...42).size 26 | assert_equal 6, (1...6.3).size 27 | assert_equal 5, (1...6.0).size 28 | assert_equal 5, (1.1...6).size 29 | assert_equal 15, (1.0..15.9).size 30 | assert_equal Float::INFINITY, (0..Float::INFINITY).size 31 | assert_nil ('a'..'z').size 32 | end 33 | -------------------------------------------------------------------------------- /test/t/numeric.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # Numeric ISO Test 3 | 4 | assert('Numeric', '15.2.7') do 5 | assert_equal Class, Numeric.class 6 | end 7 | 8 | assert('Numeric#+@', '15.2.7.4.1') do 9 | assert_equal(+1, +1) 10 | end 11 | 12 | assert('Numeric#-@', '15.2.7.4.2') do 13 | assert_equal(-1, -1) 14 | end 15 | 16 | assert('Numeric#abs', '15.2.7.4.3') do 17 | assert_equal(1, 1.abs) 18 | assert_equal(1.0, -1.abs) 19 | end 20 | 21 | assert('Numeric#pow') do 22 | assert_equal(8, 2 ** 3) 23 | assert_equal(-8, -2 ** 3) 24 | assert_equal(1, 2 ** 0) 25 | assert_equal(1, 2.2 ** 0) 26 | assert_equal(0.5, 2 ** -1) 27 | end 28 | 29 | assert('Numeric#/', '15.2.8.3.4') do 30 | n = Class.new(Numeric){ def /(x); 15.1;end }.new 31 | 32 | assert_equal(2, 10/5) 33 | assert_equal(0.0625, 1/16) 34 | assert_equal(15.1, n/10) 35 | assert_raise(TypeError){ 1/n } 36 | assert_raise(TypeError){ 1/nil } 37 | end 38 | 39 | # Not ISO specified 40 | 41 | assert('Numeric#**') do 42 | assert_equal 8.0, 2.0**3 43 | end 44 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | Original Authors "mruby developers" are: 2 | Yukihiro Matsumoto 3 | SCSK KYUSHU CORPORATION 4 | Kyushu Institute of Technology 5 | Network Applied Communication Laboratory, Inc. 6 | Daniel Bovensiepen 7 | Jon Maken 8 | Bjorn De Meyer 9 | Yuichiro MASUI 10 | Masamitsu MURASE 11 | Masaki Muranaka 12 | Internet Initiative Japan Inc. 13 | Tadashi FUKUZAWA 14 | MATSUMOTO Ryosuke 15 | Yasuhiro Matsumoto 16 | Koji Yoshioka 17 | Jun Hiroe 18 | Narihiro Nakamura 19 | Yuichi Nishiwaki 20 | Tatsuhiko Kubo 21 | Takeshi Watanabe 22 | Yuki Kurihara 23 | specified non-profit corporation mruby Forum 24 | Kazuaki Tanaka 25 | Hiromasa Ishii 26 | Hiroshi Mimaki 27 | Satoshi Odawara 28 | Mitsubishi Electric Micro-Computer Application Software Co.,Ltd. 29 | Ralph Desir(Mav7) 30 | Hiroyuki Matsuzaki 31 | Yuhei Okazaki 32 | Manycolors, Inc. 33 | Shota Nakano 34 | Yuichi Osawa 35 | Terence Lee 36 | Zachary Scott 37 | Tomasz Dąbrowski 38 | -------------------------------------------------------------------------------- /mrbgems/mruby-proc-ext/mrblib/proc.rb: -------------------------------------------------------------------------------- 1 | class Proc 2 | 3 | def ===(*args) 4 | call(*args) 5 | end 6 | 7 | def yield(*args) 8 | call(*args) 9 | end 10 | 11 | def to_proc 12 | self 13 | end 14 | 15 | def curry(arity=self.arity) 16 | type = :proc 17 | abs = lambda {|a| a < 0 ? -a - 1 : a} 18 | arity = abs[arity] 19 | if lambda? 20 | type = :lambda 21 | self_arity = self.arity 22 | if (self_arity >= 0 && arity != self_arity) || 23 | (self_arity < 0 && abs[self_arity] > arity) 24 | raise ArgumentError, "wrong number of arguments (#{arity} for #{abs[self_arity]})" 25 | end 26 | end 27 | 28 | pproc = self 29 | make_curry = proc do |given_args=[]| 30 | send(type) do |*args| 31 | new_args = given_args + args 32 | if new_args.size >= arity 33 | pproc[*new_args] 34 | else 35 | make_curry[new_args] 36 | end 37 | end 38 | end 39 | make_curry.call 40 | end 41 | 42 | end 43 | -------------------------------------------------------------------------------- /mrblib/kernel.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # Kernel 3 | # 4 | # ISO 15.3.1 5 | module Kernel 6 | 7 | # 15.3.1.2.1 Kernel.` 8 | # provided by Kernel#` 9 | # 15.3.1.3.5 10 | def `(s) 11 | raise NotImplementedError.new("backquotes not implemented") 12 | end 13 | 14 | ## 15 | # 15.3.1.2.3 Kernel.eval 16 | # 15.3.1.3.12 Kernel#eval 17 | # NotImplemented by mruby core; use mruby-eval gem 18 | 19 | ## 20 | # ISO 15.3.1.2.8 Kernel.loop 21 | # provided by Kernel#loop 22 | 23 | ## 24 | # Calls the given block repetitively. 25 | # 26 | # ISO 15.3.1.3.29 27 | def loop(&block) 28 | return to_enum :loop unless block 29 | 30 | while true 31 | yield 32 | end 33 | rescue StopIteration => e 34 | e.result 35 | end 36 | 37 | # 11.4.4 Step c) 38 | def !~(y) 39 | !(self =~ y) 40 | end 41 | 42 | # internal method for inspect 43 | def _inspect 44 | self.inspect 45 | end 46 | 47 | def to_enum(*a) 48 | raise NotImplementedError.new("fiber required for enumerator") 49 | end 50 | end 51 | -------------------------------------------------------------------------------- /test/t/gc.rb: -------------------------------------------------------------------------------- 1 | # Not ISO specified 2 | 3 | assert('GC.enable') do 4 | assert_false GC.disable 5 | assert_true GC.enable 6 | assert_false GC.enable 7 | end 8 | 9 | assert('GC.disable') do 10 | begin 11 | assert_false GC.disable 12 | assert_true GC.disable 13 | ensure 14 | GC.enable 15 | end 16 | end 17 | 18 | assert('GC.interval_ratio=') do 19 | origin = GC.interval_ratio 20 | begin 21 | assert_equal 150, (GC.interval_ratio = 150) 22 | ensure 23 | GC.interval_ratio = origin 24 | end 25 | end 26 | 27 | assert('GC.step_ratio=') do 28 | origin = GC.step_ratio 29 | begin 30 | assert_equal 150, (GC.step_ratio = 150) 31 | ensure 32 | GC.step_ratio = origin 33 | end 34 | end 35 | 36 | assert('GC.generational_mode=') do 37 | origin = GC.generational_mode 38 | begin 39 | assert_false (GC.generational_mode = false) 40 | assert_true (GC.generational_mode = true) 41 | assert_true (GC.generational_mode = true) 42 | ensure 43 | GC.generational_mode = origin 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /examples/mrbgems/ruby_extension_example/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('ruby_extension_example') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | 5 | # Add compile flags 6 | # spec.cc.flags << '' 7 | 8 | # Add cflags to all 9 | # spec.mruby.cc.flags << '-g' 10 | 11 | # Add libraries 12 | # spec.linker.libraries << 'external_lib' 13 | 14 | spec.add_dependency('mruby-print', :core => 'mruby-print') 15 | 16 | # Default build files 17 | # spec.rbfiles = Dir.glob("#{dir}/mrblib/*.rb") 18 | # spec.objs = Dir.glob("#{dir}/src/*.{c,cpp,m,asm,S}").map { |f| objfile(f.relative_path_from(dir).pathmap("#{build_dir}/%X")) } 19 | # spec.test_rbfiles = Dir.glob("#{dir}/test/*.rb") 20 | # spec.test_objs = Dir.glob("#{dir}/test/*.{c,cpp,m,asm,S}").map { |f| objfile(f.relative_path_from(dir).pathmap("#{build_dir}/%X")) } 21 | # spec.test_preload = 'test/assert.rb' 22 | 23 | # Values accessible as TEST_ARGS inside test scripts 24 | # spec.test_args = {'tmp_dir' => Dir::tmpdir} 25 | end 26 | -------------------------------------------------------------------------------- /mrblib/10error.rb: -------------------------------------------------------------------------------- 1 | # ISO 15.2.24 2 | class ArgumentError < StandardError 3 | end 4 | 5 | # ISO 15.2.25 6 | class LocalJumpError < StandardError 7 | end 8 | 9 | # ISO 15.2.26 10 | class RangeError < StandardError 11 | end 12 | 13 | class FloatDomainError < RangeError 14 | end 15 | 16 | # ISO 15.2.26 17 | class RegexpError < StandardError 18 | end 19 | 20 | # ISO 15.2.29 21 | class TypeError < StandardError 22 | end 23 | 24 | # ISO 15.2.31 25 | class NameError < StandardError 26 | attr_accessor :name 27 | 28 | def initialize(message=nil, name=nil) 29 | @name = name 30 | super(message) 31 | end 32 | end 33 | 34 | # ISO 15.2.32 35 | class NoMethodError < NameError 36 | attr_reader :args 37 | 38 | def initialize(message=nil, name=nil, args=nil) 39 | @args = args 40 | super message, name 41 | end 42 | end 43 | 44 | # ISO 15.2.33 45 | class IndexError < StandardError 46 | end 47 | 48 | class KeyError < IndexError 49 | end 50 | 51 | class NotImplementedError < ScriptError 52 | end 53 | 54 | class StopIteration < IndexError 55 | attr_accessor :result 56 | end 57 | -------------------------------------------------------------------------------- /mrbgems/mruby-range-ext/mrblib/range.rb: -------------------------------------------------------------------------------- 1 | class Range 2 | ## 3 | # call-seq: 4 | # rng.first -> obj 5 | # rng.first(n) -> an_array 6 | # 7 | # Returns the first object in the range, or an array of the first +n+ 8 | # elements. 9 | # 10 | # (10..20).first #=> 10 11 | # (10..20).first(3) #=> [10, 11, 12] 12 | # 13 | def first(*args) 14 | return self.begin if args.empty? 15 | 16 | raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 1)" unless args.length == 1 17 | nv = args[0] 18 | raise TypeError, "no implicit conversion from nil to integer" if nv.nil? 19 | raise TypeError, "no implicit conversion of #{nv.class} into Integer" unless nv.respond_to?(:to_int) 20 | n = nv.to_int 21 | raise TypeError, "no implicit conversion of #{nv.class} into Integer" unless n.kind_of?(Integer) 22 | raise ArgumentError, "negative array size (or size too big)" unless 0 <= n 23 | ary = [] 24 | each do |i| 25 | break if n <= 0 26 | ary.push(i) 27 | n -= 1 28 | end 29 | ary 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /tasks/libmruby.rake: -------------------------------------------------------------------------------- 1 | MRuby.each_target do 2 | file libfile("#{build_dir}/lib/libmruby") => libmruby.flatten do |t| 3 | archiver.run t.name, t.prerequisites 4 | end 5 | 6 | file "#{build_dir}/lib/libmruby.flags.mak" => [__FILE__, libfile("#{build_dir}/lib/libmruby")] do |t| 7 | open(t.name, 'w') do |f| 8 | f.puts "MRUBY_CFLAGS = #{cc.all_flags}" 9 | 10 | gem_flags = gems.map { |g| g.linker.flags } 11 | gem_library_paths = gems.map { |g| g.linker.library_paths } 12 | f.puts "MRUBY_LDFLAGS = #{linker.all_flags(gem_library_paths, gem_flags)} #{linker.option_library_path % "#{build_dir}/lib"}" 13 | 14 | gem_flags_before_libraries = gems.map { |g| g.linker.flags_before_libraries } 15 | f.puts "MRUBY_LDFLAGS_BEFORE_LIBS = #{[linker.flags_before_libraries, gem_flags_before_libraries].flatten.join(' ')}" 16 | 17 | gem_libraries = gems.map { |g| g.linker.libraries } 18 | f.puts "MRUBY_LIBS = #{linker.option_library % 'mruby'} #{linker.library_flags(gem_libraries)}" 19 | end 20 | end 21 | task :all => "#{build_dir}/lib/libmruby.flags.mak" 22 | end 23 | -------------------------------------------------------------------------------- /include/mruby/istruct.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** mruby/istruct.h - Inline structures 3 | ** 4 | ** See Copyright Notice in mruby.h 5 | */ 6 | 7 | #ifndef MRUBY_ISTRUCT_H 8 | #define MRUBY_ISTRUCT_H 9 | 10 | #include "common.h" 11 | #include 12 | 13 | /** 14 | * Inline structures that fit in RVALUE 15 | * 16 | * They cannot have finalizer, and cannot have instance variables. 17 | */ 18 | MRB_BEGIN_DECL 19 | 20 | #define ISTRUCT_DATA_SIZE (sizeof(void*) * 3) 21 | 22 | struct RIstruct { 23 | MRB_OBJECT_HEADER; 24 | char inline_data[ISTRUCT_DATA_SIZE]; 25 | }; 26 | 27 | #define RISTRUCT(obj) ((struct RIstruct*)(mrb_ptr(obj))) 28 | #define ISTRUCT_PTR(obj) (RISTRUCT(obj)->inline_data) 29 | 30 | MRB_INLINE mrb_int mrb_istruct_size() 31 | { 32 | return ISTRUCT_DATA_SIZE; 33 | } 34 | 35 | MRB_INLINE void* mrb_istruct_ptr(mrb_value object) 36 | { 37 | return ISTRUCT_PTR(object); 38 | } 39 | 40 | MRB_INLINE void mrb_istruct_copy(mrb_value dest, mrb_value src) 41 | { 42 | memcpy(ISTRUCT_PTR(dest), ISTRUCT_PTR(src), ISTRUCT_DATA_SIZE); 43 | } 44 | 45 | MRB_END_DECL 46 | 47 | #endif /* MRUBY_ISTRUCT_H */ 48 | -------------------------------------------------------------------------------- /mrbgems/mruby-test/init_mrbtest.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | extern const uint8_t mrbtest_assert_irep[]; 7 | 8 | void mrbgemtest_init(mrb_state* mrb); 9 | void mrb_init_test_driver(mrb_state* mrb, mrb_bool verbose); 10 | void mrb_t_pass_result(mrb_state *mrb_dst, mrb_state *mrb_src); 11 | 12 | void 13 | mrb_init_mrbtest(mrb_state *mrb) 14 | { 15 | mrb_state *core_test; 16 | 17 | mrb_load_irep(mrb, mrbtest_assert_irep); 18 | 19 | core_test = mrb_open_core(mrb_default_allocf, NULL); 20 | if (core_test == NULL) { 21 | fprintf(stderr, "Invalid mrb_state, exiting %s", __FUNCTION__); 22 | exit(EXIT_FAILURE); 23 | } 24 | mrb_init_test_driver(core_test, mrb_test(mrb_gv_get(mrb, mrb_intern_lit(mrb, "$mrbtest_verbose")))); 25 | mrb_load_irep(core_test, mrbtest_assert_irep); 26 | mrb_t_pass_result(mrb, core_test); 27 | 28 | #ifndef DISABLE_GEMS 29 | mrbgemtest_init(mrb); 30 | #endif 31 | 32 | if (mrb->exc) { 33 | mrb_print_error(mrb); 34 | exit(EXIT_FAILURE); 35 | } 36 | mrb_close(core_test); 37 | } 38 | 39 | -------------------------------------------------------------------------------- /MITL: -------------------------------------------------------------------------------- 1 | Copyright (c) 2017 mruby developers 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a 4 | copy of this software and associated documentation files (the "Software"), 5 | to deal in the Software without restriction, including without limitation 6 | the rights to use, copy, modify, merge, publish, distribute, sublicense, 7 | and/or sell copies of the Software, and to permit persons to whom the 8 | Software is furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 19 | DEALINGS IN THE SOFTWARE. 20 | 21 | -------------------------------------------------------------------------------- /mrbgems/mruby-bin-mruby-config/mrbgem.rake: -------------------------------------------------------------------------------- 1 | module MRuby 2 | class Build 3 | def exefile(name) 4 | if name.is_a?(Array) 5 | name.flatten.map { |n| exefile(n) } 6 | elsif name !~ /\./ 7 | "#{name}#{exts.executable}" 8 | else 9 | name 10 | end 11 | end 12 | end 13 | end 14 | 15 | MRuby.each_target do 16 | next if kind_of? MRuby::CrossBuild 17 | 18 | mruby_config = 'mruby-config' + (ENV['OS'] == 'Windows_NT' ? '.bat' : '') 19 | mruby_config_path = "#{build_dir}/bin/#{mruby_config}" 20 | @bins << mruby_config 21 | 22 | file mruby_config_path => libfile("#{build_dir}/lib/libmruby") do |t| 23 | FileUtils.copy "#{File.dirname(__FILE__)}/#{mruby_config}", t.name 24 | config = Hash[open("#{build_dir}/lib/libmruby.flags.mak").read.split("\n").map {|x| a = x.split(/\s*=\s*/, 2); [a[0], a[1].gsub('\\"', '"') ]}] 25 | IO.write(t.name, File.open(t.name) {|f| 26 | f.read.gsub (/echo (MRUBY_CFLAGS|MRUBY_LIBS|MRUBY_LDFLAGS_BEFORE_LIBS|MRUBY_LDFLAGS)/) {|x| config[$1].empty? ? '' : "echo #{config[$1]}"} 27 | }) 28 | FileUtils.chmod(0755, t.name) 29 | end 30 | end 31 | -------------------------------------------------------------------------------- /appveyor_config.rb: -------------------------------------------------------------------------------- 1 | MRuby::Build.new('debug') do |conf| 2 | toolchain :visualcpp 3 | enable_debug 4 | 5 | # include all core GEMs 6 | conf.gembox 'full-core' 7 | conf.compilers.each do |c| 8 | c.defines += %w(MRB_GC_STRESS MRB_GC_FIXED_ARENA) 9 | end 10 | 11 | build_mrbc_exec 12 | end 13 | 14 | MRuby::Build.new('full-debug') do |conf| 15 | toolchain :visualcpp 16 | enable_debug 17 | 18 | # include all core GEMs 19 | conf.gembox 'full-core' 20 | conf.cc.defines = %w(MRB_ENABLE_DEBUG_HOOK) 21 | 22 | conf.enable_test 23 | end 24 | 25 | MRuby::Build.new do |conf| 26 | toolchain :visualcpp 27 | 28 | # include all core GEMs 29 | conf.gembox 'full-core' 30 | conf.compilers.each do |c| 31 | c.defines += %w(MRB_GC_FIXED_ARENA) 32 | end 33 | conf.enable_bintest 34 | conf.enable_test 35 | end 36 | 37 | MRuby::Build.new('cxx_abi') do |conf| 38 | toolchain :visualcpp 39 | 40 | conf.gembox 'full-core' 41 | conf.compilers.each do |c| 42 | c.defines += %w(MRB_GC_FIXED_ARENA) 43 | end 44 | conf.enable_bintest 45 | conf.enable_test 46 | 47 | enable_cxx_abi 48 | 49 | build_mrbc_exec 50 | end 51 | -------------------------------------------------------------------------------- /include/mruby/object.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** mruby/object.h - mruby object definition 3 | ** 4 | ** See Copyright Notice in mruby.h 5 | */ 6 | 7 | #ifndef MRUBY_OBJECT_H 8 | #define MRUBY_OBJECT_H 9 | 10 | #define MRB_OBJECT_HEADER \ 11 | enum mrb_vtype tt:8;\ 12 | uint32_t color:3;\ 13 | uint32_t flags:21;\ 14 | struct RClass *c;\ 15 | struct RBasic *gcnext 16 | 17 | #define MRB_FLAG_TEST(obj, flag) ((obj)->flags & flag) 18 | 19 | 20 | struct RBasic { 21 | MRB_OBJECT_HEADER; 22 | }; 23 | #define mrb_basic_ptr(v) ((struct RBasic*)(mrb_ptr(v))) 24 | 25 | #define MRB_FROZEN_P(o) ((o)->flags & MRB_FLAG_IS_FROZEN) 26 | #define MRB_SET_FROZEN_FLAG(o) ((o)->flags |= MRB_FLAG_IS_FROZEN) 27 | #define MRB_UNSET_FROZEN_FLAG(o) ((o)->flags &= ~MRB_FLAG_IS_FROZEN) 28 | 29 | struct RObject { 30 | MRB_OBJECT_HEADER; 31 | struct iv_tbl *iv; 32 | }; 33 | #define mrb_obj_ptr(v) ((struct RObject*)(mrb_ptr(v))) 34 | 35 | #define mrb_immediate_p(x) (mrb_type(x) < MRB_TT_HAS_BASIC) 36 | #define mrb_special_const_p(x) mrb_immediate_p(x) 37 | 38 | struct RFiber { 39 | MRB_OBJECT_HEADER; 40 | struct mrb_context *cxt; 41 | }; 42 | 43 | #endif /* MRUBY_OBJECT_H */ 44 | -------------------------------------------------------------------------------- /src/print.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** print.c - Kernel.#p 3 | ** 4 | ** See Copyright Notice in mruby.h 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | #ifndef MRB_DISABLE_STDIO 12 | static void 13 | printstr(mrb_value obj, FILE *stream) 14 | { 15 | if (mrb_string_p(obj)) { 16 | fwrite(RSTRING_PTR(obj), RSTRING_LEN(obj), 1, stream); 17 | putc('\n', stream); 18 | } 19 | } 20 | #else 21 | # define printstr(obj, stream) (void)0 22 | #endif 23 | 24 | MRB_API void 25 | mrb_p(mrb_state *mrb, mrb_value obj) 26 | { 27 | printstr(mrb_inspect(mrb, obj), stdout); 28 | } 29 | 30 | MRB_API void 31 | mrb_print_error(mrb_state *mrb) 32 | { 33 | mrb_print_backtrace(mrb); 34 | printstr(mrb_funcall(mrb, mrb_obj_value(mrb->exc), "inspect", 0), stderr); 35 | } 36 | 37 | MRB_API void 38 | mrb_show_version(mrb_state *mrb) 39 | { 40 | printstr(mrb_const_get(mrb, mrb_obj_value(mrb->object_class), mrb_intern_lit(mrb, "MRUBY_DESCRIPTION")), stdout); 41 | } 42 | 43 | MRB_API void 44 | mrb_show_copyright(mrb_state *mrb) 45 | { 46 | printstr(mrb_const_get(mrb, mrb_obj_value(mrb->object_class), mrb_intern_lit(mrb, "MRUBY_COPYRIGHT")), stdout); 47 | } 48 | -------------------------------------------------------------------------------- /test/t/ensure.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # ensure Test 3 | 4 | assert('ensure - context - yield') do 5 | class EnsureYieldBreak 6 | attr_reader :ensure_context 7 | def try 8 | yield 9 | ensure 10 | @ensure_context = self 11 | end 12 | end 13 | 14 | yielder = EnsureYieldBreak.new 15 | yielder.try do 16 | end 17 | assert_equal yielder, yielder.ensure_context 18 | end 19 | 20 | assert('ensure - context - yield and break') do 21 | class EnsureYieldBreak 22 | attr_reader :ensure_context 23 | def try 24 | yield 25 | ensure 26 | @ensure_context = self 27 | end 28 | end 29 | 30 | yielder = EnsureYieldBreak.new 31 | yielder.try do 32 | break 33 | end 34 | assert_equal yielder, yielder.ensure_context 35 | end 36 | 37 | assert('ensure - context - yield and return') do 38 | class EnsureYieldBreak 39 | attr_reader :ensure_context 40 | def try 41 | yield 42 | ensure 43 | @ensure_context = self 44 | end 45 | end 46 | 47 | yielder = EnsureYieldBreak.new 48 | lambda do 49 | yielder.try do 50 | return 51 | end 52 | end.call 53 | assert_equal yielder, yielder.ensure_context 54 | end 55 | -------------------------------------------------------------------------------- /benchmark/bm_so_lists.rb: -------------------------------------------------------------------------------- 1 | #from http://www.bagley.org/~doug/shootout/bench/lists/lists.ruby 2 | 3 | NUM = 300 4 | SIZE = 10000 5 | 6 | def test_lists() 7 | # create a list of integers (Li1) from 1 to SIZE 8 | li1 = (1..SIZE).to_a 9 | # copy the list to li2 (not by individual items) 10 | li2 = li1.dup 11 | # remove each individual item from left side of li2 and 12 | # append to right side of li3 (preserving order) 13 | li3 = Array.new 14 | while (not li2.empty?) 15 | li3.push(li2.shift) 16 | end 17 | # li2 must now be empty 18 | # remove each individual item from right side of li3 and 19 | # append to right side of li2 (reversing list) 20 | while (not li3.empty?) 21 | li2.push(li3.pop) 22 | end 23 | # li3 must now be empty 24 | # reverse li1 in place 25 | li1.reverse! 26 | # check that first item is now SIZE 27 | if li1[0] != SIZE then 28 | p "not SIZE" 29 | 0 30 | else 31 | # compare li1 and li2 for equality 32 | if li1 != li2 then 33 | return(0) 34 | else 35 | # return the length of the list 36 | li1.length 37 | end 38 | end 39 | end 40 | 41 | i = 0 42 | while i last) > 0 32 | 33 | while (val <=> last) < 0 34 | block.call(val) 35 | val = val.succ 36 | end 37 | 38 | block.call(val) if !exclude_end? && (val <=> last) == 0 39 | self 40 | end 41 | 42 | # redefine #hash 15.3.1.3.15 43 | def hash 44 | h = first.hash ^ last.hash 45 | h += 1 if self.exclude_end? 46 | h 47 | end 48 | end 49 | 50 | ## 51 | # Range is enumerable 52 | # 53 | # ISO 15.2.14.3 54 | class Range 55 | include Enumerable 56 | end 57 | -------------------------------------------------------------------------------- /mrbgems/mruby-bin-mirb/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('mruby-bin-mirb') do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.summary = 'mirb command' 5 | 6 | if spec.build.cc.search_header_path 'readline/readline.h' 7 | spec.cc.defines << "ENABLE_READLINE" 8 | if spec.build.cc.search_header_path 'termcap.h' 9 | if MRUBY_BUILD_HOST_IS_CYGWIN || MRUBY_BUILD_HOST_IS_OPENBSD 10 | if spec.build.cc.search_header_path 'termcap.h' 11 | if MRUBY_BUILD_HOST_IS_CYGWIN then 12 | spec.linker.libraries << 'ncurses' 13 | else 14 | spec.linker.libraries << 'termcap' 15 | end 16 | end 17 | end 18 | end 19 | if RUBY_PLATFORM.include?('netbsd') 20 | spec.linker.libraries << 'edit' 21 | else 22 | spec.linker.libraries << 'readline' 23 | if spec.build.cc.search_header_path 'curses.h' 24 | spec.linker.libraries << 'ncurses' 25 | end 26 | end 27 | elsif spec.build.cc.search_header_path 'linenoise.h' 28 | spec.cc.defines << "ENABLE_LINENOISE" 29 | end 30 | 31 | spec.bins = %w(mirb) 32 | spec.add_dependency('mruby-compiler', :core => 'mruby-compiler') 33 | end 34 | -------------------------------------------------------------------------------- /test/t/unicode.rb: -------------------------------------------------------------------------------- 1 | # Test of the \u notation 2 | 3 | assert('bare \u notation test') do 4 | # Mininum and maximum one byte characters 5 | assert_equal("\x00", "\u0000") 6 | assert_equal("\x7F", "\u007F") 7 | 8 | # Mininum and maximum two byte characters 9 | assert_equal("\xC2\x80", "\u0080") 10 | assert_equal("\xDF\xBF", "\u07FF") 11 | 12 | # Mininum and maximum three byte characters 13 | assert_equal("\xE0\xA0\x80", "\u0800") 14 | assert_equal("\xEF\xBF\xBF", "\uFFFF") 15 | 16 | # Four byte characters require the \U notation 17 | end 18 | 19 | assert('braced \u notation test') do 20 | # Mininum and maximum one byte characters 21 | assert_equal("\x00", "\u{0000}") 22 | assert_equal("\x7F", "\u{007F}") 23 | 24 | # Mininum and maximum two byte characters 25 | assert_equal("\xC2\x80", "\u{0080}") 26 | assert_equal("\xDF\xBF", "\u{07FF}") 27 | 28 | # Mininum and maximum three byte characters 29 | assert_equal("\xE0\xA0\x80", "\u{0800}") 30 | assert_equal("\xEF\xBF\xBF", "\u{FFFF}") 31 | 32 | # Mininum and maximum four byte characters 33 | assert_equal("\xF0\x90\x80\x80", "\u{10000}") 34 | assert_equal("\xF4\x8F\xBF\xBF", "\u{10FFFF}") 35 | end 36 | 37 | assert('braced multiple \u notation test') do 38 | assert_equal("ABC", "\u{41 42 43}") 39 | end 40 | -------------------------------------------------------------------------------- /travis_config.rb: -------------------------------------------------------------------------------- 1 | MRuby::Build.new('debug') do |conf| 2 | toolchain :gcc 3 | enable_debug 4 | 5 | # include all core GEMs 6 | conf.gembox 'full-core' 7 | conf.cc.flags += %w(-Werror=declaration-after-statement) 8 | conf.compilers.each do |c| 9 | c.defines += %w(MRB_GC_STRESS MRB_GC_FIXED_ARENA) 10 | end 11 | 12 | build_mrbc_exec 13 | end 14 | 15 | MRuby::Build.new('full-debug') do |conf| 16 | toolchain :gcc 17 | enable_debug 18 | 19 | # include all core GEMs 20 | conf.gembox 'full-core' 21 | conf.cc.defines = %w(MRB_ENABLE_DEBUG_HOOK) 22 | 23 | conf.enable_test 24 | end 25 | 26 | MRuby::Build.new do |conf| 27 | toolchain :gcc 28 | 29 | # include all core GEMs 30 | conf.gembox 'full-core' 31 | conf.cc.flags += %w(-Werror=declaration-after-statement) 32 | conf.compilers.each do |c| 33 | c.defines += %w(MRB_GC_FIXED_ARENA) 34 | end 35 | conf.enable_bintest 36 | conf.enable_test 37 | end 38 | 39 | MRuby::Build.new('cxx_abi') do |conf| 40 | toolchain :gcc 41 | 42 | conf.gembox 'full-core' 43 | conf.cc.flags += %w(-Werror=declaration-after-statement) 44 | conf.compilers.each do |c| 45 | c.defines += %w(MRB_GC_FIXED_ARENA) 46 | end 47 | conf.enable_bintest 48 | conf.enable_test 49 | 50 | enable_cxx_abi 51 | 52 | build_mrbc_exec 53 | end 54 | -------------------------------------------------------------------------------- /mrbgems/mruby-hash-ext/src/hash-ext.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** hash.c - Hash class 3 | ** 4 | ** See Copyright Notice in mruby.h 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | /* 12 | * call-seq: 13 | * hsh.values_at(key, ...) -> array 14 | * 15 | * Return an array containing the values associated with the given keys. 16 | * Also see Hash.select. 17 | * 18 | * h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" } 19 | * h.values_at("cow", "cat") #=> ["bovine", "feline"] 20 | */ 21 | 22 | static mrb_value 23 | hash_values_at(mrb_state *mrb, mrb_value hash) 24 | { 25 | mrb_value *argv, result; 26 | mrb_int argc, i; 27 | int ai; 28 | 29 | mrb_get_args(mrb, "*", &argv, &argc); 30 | result = mrb_ary_new_capa(mrb, argc); 31 | ai = mrb_gc_arena_save(mrb); 32 | for (i = 0; i < argc; i++) { 33 | mrb_ary_push(mrb, result, mrb_hash_get(mrb, hash, argv[i])); 34 | mrb_gc_arena_restore(mrb, ai); 35 | } 36 | return result; 37 | } 38 | 39 | void 40 | mrb_mruby_hash_ext_gem_init(mrb_state *mrb) 41 | { 42 | struct RClass *h; 43 | 44 | h = mrb->hash_class; 45 | mrb_define_method(mrb, h, "values_at", hash_values_at, MRB_ARGS_ANY()); 46 | } 47 | 48 | void 49 | mrb_mruby_hash_ext_gem_final(mrb_state *mrb) 50 | { 51 | } 52 | -------------------------------------------------------------------------------- /mrbgems/mruby-symbol-ext/test/symbol.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # Symbol(Ext) Test 3 | 4 | assert('Symbol#to_proc') do 5 | assert_equal 5, :abs.to_proc[-5] 6 | end 7 | 8 | assert('Symbol.all_symbols') do 9 | foo = [:__symbol_test_1, :__symbol_test_2, :__symbol_test_3].sort 10 | symbols = Symbol.all_symbols.select{|sym|sym.to_s.include? '__symbol_test'}.sort 11 | assert_equal foo, symbols 12 | end 13 | 14 | assert("Symbol#length") do 15 | assert_equal 5, :hello.size 16 | assert_equal 5, :mruby.length 17 | end 18 | 19 | assert("Symbol#capitalize") do 20 | assert_equal :Hello, :hello.capitalize 21 | assert_equal :Hello, :HELLO.capitalize 22 | assert_equal :Hello, :Hello.capitalize 23 | end 24 | 25 | assert("Symbol#downcase") do 26 | assert_equal :hello, :hEllO.downcase 27 | assert_equal :hello, :hello.downcase 28 | end 29 | 30 | assert("Symbol#upcase") do 31 | assert_equal :HELLO, :hEllO.upcase 32 | assert_equal :HELLO, :HELLO.upcase 33 | end 34 | 35 | assert("Symbol#casecmp") do 36 | assert_equal 0, :HELLO.casecmp(:hEllO) 37 | assert_equal 1, :HELLO.casecmp(:hEllN) 38 | assert_equal(-1, :HELLO.casecmp(:hEllP)) 39 | assert_nil :HELLO.casecmp("hEllO") 40 | end 41 | 42 | assert("Symbol#empty?") do 43 | assert_true :''.empty? 44 | end 45 | 46 | assert('Symbol#intern') do 47 | assert_equal :test, :test.intern 48 | end 49 | -------------------------------------------------------------------------------- /mrbgems/mruby-bin-debugger/tools/mrdb/apibreak.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** apibreak.h 3 | ** 4 | */ 5 | 6 | #ifndef APIBREAK_H_ 7 | #define APIBREAK_H_ 8 | 9 | #include 10 | #include "mrdb.h" 11 | 12 | int32_t mrb_debug_set_break_line( mrb_state *, mrb_debug_context *, const char *, uint16_t ); 13 | int32_t mrb_debug_set_break_method( mrb_state *, mrb_debug_context *, const char *, const char * ); 14 | int32_t mrb_debug_get_breaknum( mrb_state *, mrb_debug_context * ); 15 | int32_t mrb_debug_get_break_all( mrb_state *, mrb_debug_context *, uint32_t, mrb_debug_breakpoint bp[]); 16 | int32_t mrb_debug_get_break( mrb_state *, mrb_debug_context *, uint32_t, mrb_debug_breakpoint * ); 17 | int32_t mrb_debug_delete_break( mrb_state *, mrb_debug_context *, uint32_t ); 18 | int32_t mrb_debug_delete_break_all( mrb_state *, mrb_debug_context * ); 19 | int32_t mrb_debug_enable_break( mrb_state *, mrb_debug_context *, uint32_t ); 20 | int32_t mrb_debug_enable_break_all( mrb_state *, mrb_debug_context * ); 21 | int32_t mrb_debug_disable_break( mrb_state *, mrb_debug_context *, uint32_t ); 22 | int32_t mrb_debug_disable_break_all( mrb_state *, mrb_debug_context * ); 23 | int32_t mrb_debug_check_breakpoint_line( mrb_state *, mrb_debug_context *, const char *, uint16_t ); 24 | int32_t mrb_debug_check_breakpoint_method( mrb_state *, mrb_debug_context *, struct RClass *, mrb_sym, mrb_bool* ); 25 | 26 | #endif /* APIBREAK_H_ */ 27 | -------------------------------------------------------------------------------- /mrbgems/mruby-symbol-ext/mrblib/symbol.rb: -------------------------------------------------------------------------------- 1 | class Symbol 2 | include Comparable 3 | 4 | alias intern to_sym 5 | 6 | def to_proc 7 | ->(obj,*args,&block) do 8 | obj.__send__(self, *args, &block) 9 | end 10 | end 11 | 12 | ## 13 | # call-seq: 14 | # sym.capitalize -> symbol 15 | # 16 | # Same as sym.to_s.capitalize.intern. 17 | 18 | def capitalize 19 | (self.to_s.capitalize! || self).to_sym 20 | end 21 | 22 | ## 23 | # call-seq: 24 | # sym.downcase -> symbol 25 | # 26 | # Same as sym.to_s.downcase.intern. 27 | 28 | def downcase 29 | (self.to_s.downcase! || self).to_sym 30 | end 31 | 32 | ## 33 | # call-seq: 34 | # sym.upcase -> symbol 35 | # 36 | # Same as sym.to_s.upcase.intern. 37 | 38 | def upcase 39 | (self.to_s.upcase! || self).to_sym 40 | end 41 | 42 | ## 43 | # call-seq: 44 | # sym.casecmp(other) -> -1, 0, +1 or nil 45 | # 46 | # Case-insensitive version of Symbol#<=>. 47 | 48 | def casecmp(other) 49 | return nil unless other.kind_of?(Symbol) 50 | lhs = self.to_s; lhs.upcase! 51 | rhs = other.to_s; rhs.upcase! 52 | lhs <=> rhs 53 | end 54 | 55 | # 56 | # call-seq: 57 | # sym.empty? -> true or false 58 | # 59 | # Returns that _sym_ is :"" or not. 60 | 61 | def empty? 62 | self.length == 0 63 | end 64 | 65 | end 66 | -------------------------------------------------------------------------------- /mrbgems/mruby-object-ext/test/object.rb: -------------------------------------------------------------------------------- 1 | assert('Object#instance_exec') do 2 | class KlassWithSecret 3 | def initialize 4 | @secret = 99 5 | end 6 | end 7 | k = KlassWithSecret.new 8 | assert_equal 104, k.instance_exec(5) {|x| @secret+x } 9 | end 10 | 11 | assert('Object#tap') do 12 | ret = [] 13 | (1..10) .tap {|x| ret << "original: #{x.inspect}"} 14 | .to_a .tap {|x| ret << "array: #{x.inspect}"} 15 | .select {|x| x%2==0} .tap {|x| ret << "evens: #{x.inspect}"} 16 | .map { |x| x*x } .tap {|x| ret << "squares: #{x.inspect}"} 17 | 18 | assert_equal [ 19 | "original: 1..10", 20 | "array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]", 21 | "evens: [2, 4, 6, 8, 10]", 22 | "squares: [4, 16, 36, 64, 100]" 23 | ], ret 24 | assert_equal(:tap_ok, Class.new {def m; tap{return :tap_ok}; end}.new.m) 25 | end 26 | 27 | assert('instance_exec on primitives with class and module definition') do 28 | begin 29 | class A 30 | 1.instance_exec do 31 | class B 32 | end 33 | end 34 | end 35 | 36 | assert_kind_of Class, A::B 37 | ensure 38 | Object.remove_const :A 39 | end 40 | 41 | begin 42 | class A 43 | 1.instance_exec do 44 | module B 45 | end 46 | end 47 | end 48 | 49 | assert_kind_of Module, A::B 50 | ensure 51 | Object.remove_const :A 52 | end 53 | end 54 | -------------------------------------------------------------------------------- /mrbgems/mruby-print/mrblib/print.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # Kernel 3 | # 4 | # ISO 15.3.1 5 | module Kernel 6 | ## 7 | # Invoke method +print+ on STDOUT and passing +*args+ 8 | # 9 | # ISO 15.3.1.2.10 10 | def print(*args) 11 | i = 0 12 | len = args.size 13 | while i < len 14 | __printstr__ args[i].to_s 15 | i += 1 16 | end 17 | end 18 | 19 | ## 20 | # Invoke method +puts+ on STDOUT and passing +*args*+ 21 | # 22 | # ISO 15.3.1.2.11 23 | def puts(*args) 24 | i = 0 25 | len = args.size 26 | while i < len 27 | s = args[i].to_s 28 | __printstr__ s 29 | __printstr__ "\n" if (s[-1] != "\n") 30 | i += 1 31 | end 32 | __printstr__ "\n" if len == 0 33 | nil 34 | end 35 | 36 | ## 37 | # Print human readable object description 38 | # 39 | # ISO 15.3.1.3.34 40 | def p(*args) 41 | i = 0 42 | len = args.size 43 | while i < len 44 | __printstr__ args[i].inspect 45 | __printstr__ "\n" 46 | i += 1 47 | end 48 | args[0] 49 | end 50 | 51 | unless Kernel.respond_to?(:sprintf) 52 | def printf(*args) 53 | raise NotImplementedError.new('printf not available') 54 | end 55 | def sprintf(*args) 56 | raise NotImplementedError.new('sprintf not available') 57 | end 58 | else 59 | def printf(*args) 60 | __printstr__(sprintf(*args)) 61 | nil 62 | end 63 | end 64 | end 65 | -------------------------------------------------------------------------------- /mrbgems/mruby-enum-lazy/test/lazy.rb: -------------------------------------------------------------------------------- 1 | assert("Enumerator::Lazy") do 2 | a = [1, 2] 3 | assert_equal Enumerator::Lazy, a.lazy.class 4 | end 5 | 6 | assert("Enumerator::Lazy laziness") do 7 | a = Object.new 8 | def a.each 9 | return to_enum :each unless block_given? 10 | self.b << 10 11 | yield 1 12 | self.b << 20 13 | yield 2 14 | self.b << 30 15 | yield 3 16 | self.b << 40 17 | yield 4 18 | self.b << 50 19 | yield 5 20 | end 21 | def a.b(b=nil) 22 | @b = b if b 23 | @b 24 | end 25 | 26 | a.b([]) 27 | assert_equal [1,2], a.each.lazy.take(2).force 28 | assert_equal [10,20], a.b 29 | 30 | a.b([]) 31 | assert_equal [2,4], a.each.lazy.select{|x|x%2==0}.take(2).force 32 | assert_equal [10,20,30,40], a.b 33 | 34 | a.b([]) 35 | assert_equal [1], a.each.lazy.take_while{|x|x<2}.take(1).force 36 | assert_equal [10], a.b 37 | 38 | a.b([]) 39 | assert_equal [1], a.each.lazy.take_while{|x|x<2}.take(4).force 40 | assert_equal [10,20], a.b 41 | end 42 | 43 | assert("Enumrator::Lazy#to_enum") do 44 | lazy_enum = (0..Float::INFINITY).lazy.to_enum(:each_slice, 2) 45 | assert_kind_of Enumerator::Lazy, lazy_enum 46 | assert_equal [0*1, 2*3, 4*5, 6*7], lazy_enum.map { |a| a.first * a.last }.first(4) 47 | end 48 | 49 | assert("Enumerator::Lazy#zip with cycle") do 50 | e1 = [1, 2, 3].cycle 51 | e2 = [:a, :b].cycle 52 | assert_equal [[1,:a],[2,:b],[3,:a]], e1.lazy.zip(e2).first(3) 53 | end 54 | -------------------------------------------------------------------------------- /tasks/toolchains/openwrt.rake: -------------------------------------------------------------------------------- 1 | # usage of environmental variables to set the 2 | # cross compiling toolchain proper 3 | MRuby::Toolchain.new(:openwrt) do |conf| 4 | [conf.cc, conf.objc, conf.asm].each do |cc| 5 | cc.command = ENV['TARGET_CC'] 6 | cc.flags = ENV['TARGET_CFLAGS'] 7 | cc.include_paths = ["#{MRUBY_ROOT}/include"] 8 | cc.defines = %w(DISABLE_GEMS) 9 | cc.option_include_path = '-I%s' 10 | cc.option_define = '-D%s' 11 | cc.compile_options = '%{flags} -MMD -o %{outfile} -c %{infile}' 12 | end 13 | 14 | [conf.cxx].each do |cxx| 15 | cxx.command = ENV['TARGET_CXX'] 16 | cxx.flags = ENV['TARGET_CXXFLAGS'] 17 | cxx.include_paths = ["#{MRUBY_ROOT}/include"] 18 | cxx.defines = %w(DISABLE_GEMS) 19 | cxx.option_include_path = '-I%s' 20 | cxx.option_define = '-D%s' 21 | cxx.compile_options = '%{flags} -MMD -o %{outfile} -c %{infile}' 22 | end 23 | 24 | conf.linker do |linker| 25 | linker.command = ENV['TARGET_CC'] 26 | linker.flags = ENV['TARGET_LDFLAGS'] 27 | linker.libraries = %w(m) 28 | linker.library_paths = [] 29 | linker.option_library = '-l%s' 30 | linker.option_library_path = '-L%s' 31 | linker.link_options = '%{flags} -o %{outfile} %{objs} %{flags_before_libraries} %{libs} %{flags_after_libraries}' 32 | end 33 | 34 | conf.archiver do |archiver| 35 | archiver.command = ENV['TARGET_AR'] 36 | archiver.archive_options = 'rs %{outfile} %{objs}' 37 | end 38 | end 39 | -------------------------------------------------------------------------------- /include/mruby/throw.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** mruby/throw.h - mruby exception throwing handler 3 | ** 4 | ** See Copyright Notice in mruby.h 5 | */ 6 | 7 | #ifndef MRB_THROW_H 8 | #define MRB_THROW_H 9 | 10 | #if defined(MRB_ENABLE_CXX_EXCEPTION) && !defined(__cplusplus) 11 | #error Trying to use C++ exception handling in C code 12 | #endif 13 | 14 | #if defined(MRB_ENABLE_CXX_EXCEPTION) && defined(__cplusplus) 15 | 16 | #define MRB_TRY(buf) do { try { 17 | #define MRB_CATCH(buf) } catch(mrb_jmpbuf_impl e) { if (e != (buf)->impl) { throw e; } 18 | #define MRB_END_EXC(buf) } } while(0) 19 | 20 | #define MRB_THROW(buf) throw((buf)->impl) 21 | typedef mrb_int mrb_jmpbuf_impl; 22 | 23 | #else 24 | 25 | #include 26 | 27 | #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 28 | #define MRB_SETJMP _setjmp 29 | #define MRB_LONGJMP _longjmp 30 | #else 31 | #define MRB_SETJMP setjmp 32 | #define MRB_LONGJMP longjmp 33 | #endif 34 | 35 | #define MRB_TRY(buf) do { if (MRB_SETJMP((buf)->impl) == 0) { 36 | #define MRB_CATCH(buf) } else { 37 | #define MRB_END_EXC(buf) } } while(0) 38 | 39 | #define MRB_THROW(buf) MRB_LONGJMP((buf)->impl, 1); 40 | #define mrb_jmpbuf_impl jmp_buf 41 | 42 | #endif 43 | 44 | struct mrb_jmpbuf { 45 | mrb_jmpbuf_impl impl; 46 | 47 | #if defined(MRB_ENABLE_CXX_EXCEPTION) && defined(__cplusplus) 48 | static mrb_int jmpbuf_id; 49 | mrb_jmpbuf() : impl(jmpbuf_id++) {} 50 | #endif 51 | }; 52 | 53 | #endif /* MRB_THROW_H */ 54 | -------------------------------------------------------------------------------- /mrbgems/mruby-bin-debugger/tools/mrdb/cmdprint.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** cmdprint.c - mruby debugger print command functions 3 | ** 4 | */ 5 | 6 | #include 7 | #include "mrdb.h" 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "apiprint.h" 15 | 16 | dbgcmd_state 17 | dbgcmd_print(mrb_state *mrb, mrdb_state *mrdb) 18 | { 19 | mrb_value expr; 20 | mrb_value result; 21 | mrb_value s; 22 | uint8_t wcnt; 23 | int ai; 24 | 25 | if (mrdb->wcnt <= 1) { 26 | puts("Parameter not specified."); 27 | return DBGST_PROMPT; 28 | } 29 | 30 | ai = mrb_gc_arena_save(mrb); 31 | 32 | /* eval expr */ 33 | expr = mrb_str_new_cstr(mrb, NULL); 34 | for (wcnt=1; wcntwcnt; wcnt++) { 35 | expr = mrb_str_cat_lit(mrb, expr, " "); 36 | expr = mrb_str_cat_cstr(mrb, expr, mrdb->words[wcnt]); 37 | } 38 | 39 | result = mrb_debug_eval(mrb, mrdb->dbg, RSTRING_PTR(expr), RSTRING_LEN(expr), NULL); 40 | 41 | /* $print_no = result */ 42 | s = mrb_str_cat_lit(mrb, result, "\0"); 43 | printf("$%lu = %s\n", (unsigned long)mrdb->print_no++, RSTRING_PTR(s)); 44 | 45 | if (mrdb->print_no == 0) { 46 | mrdb->print_no = 1; 47 | } 48 | 49 | mrb_gc_arena_restore(mrb, ai); 50 | 51 | return DBGST_PROMPT; 52 | } 53 | 54 | dbgcmd_state 55 | dbgcmd_eval(mrb_state *mrb, mrdb_state *mrdb) 56 | { 57 | return dbgcmd_print(mrb, mrdb); 58 | } 59 | -------------------------------------------------------------------------------- /include/mruby/irep.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** mruby/irep.h - mrb_irep structure 3 | ** 4 | ** See Copyright Notice in mruby.h 5 | */ 6 | 7 | #ifndef MRUBY_IREP_H 8 | #define MRUBY_IREP_H 9 | 10 | #include "common.h" 11 | #include 12 | 13 | /** 14 | * Compiled mruby scripts. 15 | */ 16 | MRB_BEGIN_DECL 17 | 18 | enum irep_pool_type { 19 | IREP_TT_STRING, 20 | IREP_TT_FIXNUM, 21 | IREP_TT_FLOAT, 22 | }; 23 | 24 | struct mrb_locals { 25 | mrb_sym name; 26 | uint16_t r; 27 | }; 28 | 29 | /* Program data array struct */ 30 | typedef struct mrb_irep { 31 | uint16_t nlocals; /* Number of local variables */ 32 | uint16_t nregs; /* Number of register variables */ 33 | uint8_t flags; 34 | 35 | mrb_code *iseq; 36 | mrb_value *pool; 37 | mrb_sym *syms; 38 | struct mrb_irep **reps; 39 | 40 | struct mrb_locals *lv; 41 | /* debug info */ 42 | const char *filename; 43 | uint16_t *lines; 44 | struct mrb_irep_debug_info* debug_info; 45 | 46 | size_t ilen, plen, slen, rlen, refcnt; 47 | } mrb_irep; 48 | 49 | #define MRB_ISEQ_NO_FREE 1 50 | 51 | MRB_API mrb_irep *mrb_add_irep(mrb_state *mrb); 52 | MRB_API mrb_value mrb_load_irep(mrb_state*, const uint8_t*); 53 | MRB_API mrb_value mrb_load_irep_cxt(mrb_state*, const uint8_t*, mrbc_context*); 54 | void mrb_irep_free(mrb_state*, struct mrb_irep*); 55 | void mrb_irep_incref(mrb_state*, struct mrb_irep*); 56 | void mrb_irep_decref(mrb_state*, struct mrb_irep*); 57 | 58 | MRB_END_DECL 59 | 60 | #endif /* MRUBY_IREP_H */ 61 | -------------------------------------------------------------------------------- /include/mruby/range.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** mruby/range.h - Range class 3 | ** 4 | ** See Copyright Notice in mruby.h 5 | */ 6 | 7 | #ifndef MRUBY_RANGE_H 8 | #define MRUBY_RANGE_H 9 | 10 | #include "common.h" 11 | 12 | /** 13 | * Range class 14 | */ 15 | MRB_BEGIN_DECL 16 | 17 | typedef struct mrb_range_edges { 18 | mrb_value beg; 19 | mrb_value end; 20 | } mrb_range_edges; 21 | 22 | struct RRange { 23 | MRB_OBJECT_HEADER; 24 | mrb_range_edges *edges; 25 | mrb_bool excl : 1; 26 | }; 27 | 28 | MRB_API struct RRange* mrb_range_ptr(mrb_state *mrb, mrb_value v); 29 | #define mrb_range_raw_ptr(v) ((struct RRange*)mrb_ptr(v)) 30 | #define mrb_range_value(p) mrb_obj_value((void*)(p)) 31 | 32 | /* 33 | * Initializes a Range. 34 | * 35 | * If the third parameter is FALSE then it includes the last value in the range. 36 | * If the third parameter is TRUE then it excludes the last value in the range. 37 | * 38 | * @param start the beginning value. 39 | * @param end the ending value. 40 | * @param exclude represents the inclusion or exclusion of the last value. 41 | */ 42 | MRB_API mrb_value mrb_range_new(mrb_state *mrb, mrb_value start, mrb_value end, mrb_bool exclude); 43 | 44 | MRB_API mrb_bool mrb_range_beg_len(mrb_state *mrb, mrb_value range, mrb_int *begp, mrb_int *lenp, mrb_int len); 45 | mrb_value mrb_get_values_at(mrb_state *mrb, mrb_value obj, mrb_int olen, mrb_int argc, const mrb_value *argv, mrb_value (*func)(mrb_state*, mrb_value, mrb_int)); 46 | 47 | MRB_END_DECL 48 | 49 | #endif /* MRUBY_RANGE_H */ 50 | -------------------------------------------------------------------------------- /src/init.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** init.c - initialize mruby core 3 | ** 4 | ** See Copyright Notice in mruby.h 5 | */ 6 | 7 | #include 8 | 9 | void mrb_init_symtbl(mrb_state*); 10 | void mrb_init_class(mrb_state*); 11 | void mrb_init_object(mrb_state*); 12 | void mrb_init_kernel(mrb_state*); 13 | void mrb_init_comparable(mrb_state*); 14 | void mrb_init_enumerable(mrb_state*); 15 | void mrb_init_symbol(mrb_state*); 16 | void mrb_init_exception(mrb_state*); 17 | void mrb_init_proc(mrb_state*); 18 | void mrb_init_string(mrb_state*); 19 | void mrb_init_array(mrb_state*); 20 | void mrb_init_hash(mrb_state*); 21 | void mrb_init_numeric(mrb_state*); 22 | void mrb_init_range(mrb_state*); 23 | void mrb_init_gc(mrb_state*); 24 | void mrb_init_math(mrb_state*); 25 | void mrb_init_version(mrb_state*); 26 | void mrb_init_mrblib(mrb_state*); 27 | 28 | #define DONE mrb_gc_arena_restore(mrb, 0); 29 | void 30 | mrb_init_core(mrb_state *mrb) 31 | { 32 | mrb_init_symtbl(mrb); DONE; 33 | 34 | mrb_init_class(mrb); DONE; 35 | mrb_init_object(mrb); DONE; 36 | mrb_init_kernel(mrb); DONE; 37 | mrb_init_comparable(mrb); DONE; 38 | mrb_init_enumerable(mrb); DONE; 39 | 40 | mrb_init_symbol(mrb); DONE; 41 | mrb_init_exception(mrb); DONE; 42 | mrb_init_proc(mrb); DONE; 43 | mrb_init_string(mrb); DONE; 44 | mrb_init_array(mrb); DONE; 45 | mrb_init_hash(mrb); DONE; 46 | mrb_init_numeric(mrb); DONE; 47 | mrb_init_range(mrb); DONE; 48 | mrb_init_gc(mrb); DONE; 49 | mrb_init_version(mrb); DONE; 50 | mrb_init_mrblib(mrb); DONE; 51 | } 52 | -------------------------------------------------------------------------------- /lib/mruby/source.rb: -------------------------------------------------------------------------------- 1 | require "pathname" 2 | 3 | module MRuby 4 | module Source 5 | # MRuby's source root directory 6 | ROOT = Pathname.new(File.expand_path('../../../',__FILE__)) 7 | 8 | # Reads a constant defined at version.h 9 | MRUBY_READ_VERSION_CONSTANT = Proc.new { |name| ROOT.join('include','mruby','version.h').read.match(/^#define #{name} +"?([\w\. ]+)"?$/)[1] } 10 | 11 | MRUBY_RUBY_VERSION = MRUBY_READ_VERSION_CONSTANT['MRUBY_RUBY_VERSION'] 12 | MRUBY_RUBY_ENGINE = MRUBY_READ_VERSION_CONSTANT['MRUBY_RUBY_ENGINE'] 13 | 14 | MRUBY_RELEASE_MAJOR = Integer(MRUBY_READ_VERSION_CONSTANT['MRUBY_RELEASE_MAJOR']) 15 | MRUBY_RELEASE_MINOR = Integer(MRUBY_READ_VERSION_CONSTANT['MRUBY_RELEASE_MINOR']) 16 | MRUBY_RELEASE_TEENY = Integer(MRUBY_READ_VERSION_CONSTANT['MRUBY_RELEASE_TEENY']) 17 | 18 | MRUBY_VERSION = [MRUBY_RELEASE_MAJOR,MRUBY_RELEASE_MINOR,MRUBY_RELEASE_TEENY].join('.') 19 | MRUBY_RELEASE_NO = (MRUBY_RELEASE_MAJOR * 100 * 100 + MRUBY_RELEASE_MINOR * 100 + MRUBY_RELEASE_TEENY) 20 | 21 | MRUBY_RELEASE_YEAR = Integer(MRUBY_READ_VERSION_CONSTANT['MRUBY_RELEASE_YEAR']) 22 | MRUBY_RELEASE_MONTH = Integer(MRUBY_READ_VERSION_CONSTANT['MRUBY_RELEASE_MONTH']) 23 | MRUBY_RELEASE_DAY = Integer(MRUBY_READ_VERSION_CONSTANT['MRUBY_RELEASE_DAY']) 24 | MRUBY_RELEASE_DATE = [MRUBY_RELEASE_YEAR,MRUBY_RELEASE_MONTH,MRUBY_RELEASE_DAY].join('.') 25 | 26 | MRUBY_BIRTH_YEAR = Integer(MRUBY_READ_VERSION_CONSTANT['MRUBY_BIRTH_YEAR']) 27 | 28 | MRUBY_AUTHOR = MRUBY_READ_VERSION_CONSTANT['MRUBY_AUTHOR'] 29 | end 30 | end 31 | -------------------------------------------------------------------------------- /mrbgems/mruby-bin-debugger/tools/mrdb/cmdrun.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** cmdrun.c - mruby debugger run command functions 3 | ** 4 | */ 5 | 6 | #include 7 | #include "mrdb.h" 8 | 9 | dbgcmd_state 10 | dbgcmd_run(mrb_state *mrb, mrdb_state *mrdb) 11 | { 12 | mrb_debug_context *dbg = mrdb->dbg; 13 | 14 | if( dbg->xm == DBG_INIT ){ 15 | dbg->xm = DBG_RUN; 16 | } else { 17 | dbg->xm = DBG_QUIT; 18 | if( dbg->xphase == DBG_PHASE_RUNNING ){ 19 | struct RClass *exc; 20 | puts("Start it from the beginning."); 21 | exc = mrb_define_class(mrb, "DebuggerRestart", mrb_class_get(mrb, "Exception")); 22 | mrb_raise(mrb, exc, "Restart mrdb."); 23 | } 24 | } 25 | 26 | return DBGST_RESTART; 27 | } 28 | 29 | dbgcmd_state 30 | dbgcmd_continue(mrb_state *mrb, mrdb_state *mrdb) 31 | { 32 | mrb_debug_context *dbg = mrdb->dbg; 33 | int ccnt = 1; 34 | 35 | if( mrdb->wcnt > 1 ){ 36 | sscanf(mrdb->words[1], "%d", &ccnt); 37 | } 38 | dbg->ccnt = (uint16_t)(ccnt > 0 ? ccnt : 1); /* count of continue */ 39 | 40 | if( dbg->xphase == DBG_PHASE_AFTER_RUN ){ 41 | puts("The program is not running."); 42 | dbg->xm = DBG_QUIT; 43 | } else { 44 | dbg->xm = DBG_RUN; 45 | } 46 | return DBGST_CONTINUE; 47 | } 48 | 49 | dbgcmd_state 50 | dbgcmd_step(mrb_state *mrb, mrdb_state *mrdb) 51 | { 52 | mrdb->dbg->xm = DBG_STEP; 53 | return DBGST_CONTINUE; 54 | } 55 | 56 | dbgcmd_state 57 | dbgcmd_next(mrb_state *mrb, mrdb_state *mrdb) 58 | { 59 | mrdb->dbg->xm = DBG_NEXT; 60 | mrdb->dbg->prvci = mrb->c->ci; 61 | return DBGST_CONTINUE; 62 | } 63 | -------------------------------------------------------------------------------- /test/t/nomethoderror.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # NoMethodError ISO Test 3 | 4 | assert('NoMethodError', '15.2.32') do 5 | NoMethodError.class == Class 6 | assert_raise NoMethodError do 7 | doesNotExistAsAMethodNameForVerySure("") 8 | end 9 | end 10 | 11 | assert('NoMethodError#args', '15.2.32.2.1') do 12 | a = NoMethodError.new 'test', :test, [1, 2] 13 | assert_equal [1, 2], a.args 14 | 15 | assert_nothing_raised do 16 | begin 17 | doesNotExistAsAMethodNameForVerySure 3, 1, 4 18 | rescue NoMethodError => e 19 | assert_equal [3, 1, 4], e.args 20 | end 21 | end 22 | end 23 | 24 | assert('Can still raise when BasicObject#method_missing is removed') do 25 | assert_raise(NoMethodError) do 26 | begin 27 | BasicObject.alias_method(:old_method_missing, :method_missing) 28 | BasicObject.remove_method(:method_missing) 29 | 1.__send__(:foo) 30 | ensure 31 | BasicObject.alias_method(:method_missing, :old_method_missing) 32 | BasicObject.remove_method(:old_method_missing) 33 | end 34 | end 35 | end 36 | 37 | assert('Can still call super when BasicObject#method_missing is removed') do 38 | assert_raise(NoMethodError) do 39 | class A 40 | def foo 41 | super 42 | end 43 | end 44 | begin 45 | BasicObject.alias_method(:old_method_missing, :method_missing) 46 | BasicObject.remove_method(:method_missing) 47 | A.new.foo 48 | ensure 49 | BasicObject.alias_method(:method_missing, :old_method_missing) 50 | BasicObject.remove_method(:old_method_missing) 51 | end 52 | end 53 | end 54 | -------------------------------------------------------------------------------- /mrbgems/mruby-compiler/mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new 'mruby-compiler' do |spec| 2 | spec.license = 'MIT' 3 | spec.author = 'mruby developers' 4 | spec.summary = 'mruby compiler library' 5 | 6 | current_dir = spec.dir 7 | current_build_dir = spec.build_dir 8 | 9 | lex_def = "#{current_dir}/core/lex.def" 10 | core_objs = Dir.glob("#{current_dir}/core/*.c").map { |f| 11 | next nil if build.cxx_abi_enabled? and f =~ /(codegen).c$/ 12 | objfile(f.pathmap("#{current_build_dir}/core/%n")) 13 | }.compact 14 | 15 | if build.cxx_abi_enabled? 16 | core_objs << 17 | build.compile_as_cxx("#{current_build_dir}/core/y.tab.c", "#{current_build_dir}/core/y.tab.cxx", 18 | objfile("#{current_build_dir}/y.tab"), ["#{current_dir}/core"]) << 19 | build.compile_as_cxx("#{current_dir}/core/codegen.c", "#{current_build_dir}/core/codegen.cxx") 20 | else 21 | core_objs << objfile("#{current_build_dir}/core/y.tab") 22 | file objfile("#{current_build_dir}/core/y.tab") => "#{current_build_dir}/core/y.tab.c" do |t| 23 | cc.run t.name, t.prerequisites.first, [], ["#{current_dir}/core"] 24 | end 25 | end 26 | file objfile("#{current_build_dir}/core/y.tab") => lex_def 27 | 28 | # Parser 29 | file "#{current_build_dir}/core/y.tab.c" => ["#{current_dir}/core/parse.y"] do |t| 30 | yacc.run t.name, t.prerequisites.first 31 | end 32 | 33 | # Lexical analyzer 34 | file lex_def => "#{current_dir}/core/keywords" do |t| 35 | gperf.run t.name, t.prerequisites.first 36 | end 37 | 38 | file libfile("#{build.build_dir}/lib/libmruby_core") => core_objs 39 | build.libmruby << core_objs 40 | end 41 | -------------------------------------------------------------------------------- /mrbgems/mruby-print/src/print.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #if defined(_WIN32) 7 | # include 8 | # include 9 | #ifdef _MSC_VER 10 | # define isatty(x) _isatty(x) 11 | # define fileno(x) _fileno(x) 12 | #endif 13 | #endif 14 | 15 | static void 16 | printstr(mrb_state *mrb, mrb_value obj) 17 | { 18 | if (mrb_string_p(obj)) { 19 | #if defined(_WIN32) 20 | if (isatty(fileno(stdout))) { 21 | DWORD written; 22 | int mlen = RSTRING_LEN(obj); 23 | char* utf8 = RSTRING_PTR(obj); 24 | int wlen = MultiByteToWideChar(CP_UTF8, 0, utf8, mlen, NULL, 0); 25 | wchar_t* utf16 = (wchar_t*)mrb_malloc(mrb, (wlen+1) * sizeof(wchar_t)); 26 | if (utf16 == NULL) return; 27 | if (MultiByteToWideChar(CP_UTF8, 0, utf8, mlen, utf16, wlen) > 0) { 28 | utf16[wlen] = 0; 29 | WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), 30 | utf16, wlen, &written, NULL); 31 | } 32 | mrb_free(mrb, utf16); 33 | } else 34 | #endif 35 | fwrite(RSTRING_PTR(obj), RSTRING_LEN(obj), 1, stdout); 36 | } 37 | } 38 | 39 | /* 15.3.1.2.9 */ 40 | /* 15.3.1.3.34 */ 41 | mrb_value 42 | mrb_printstr(mrb_state *mrb, mrb_value self) 43 | { 44 | mrb_value argv; 45 | 46 | mrb_get_args(mrb, "o", &argv); 47 | printstr(mrb, argv); 48 | 49 | return argv; 50 | } 51 | 52 | void 53 | mrb_mruby_print_gem_init(mrb_state* mrb) 54 | { 55 | struct RClass *krn; 56 | krn = mrb->kernel_module; 57 | mrb_define_method(mrb, krn, "__printstr__", mrb_printstr, MRB_ARGS_REQ(1)); 58 | } 59 | 60 | void 61 | mrb_mruby_print_gem_final(mrb_state* mrb) 62 | { 63 | } 64 | -------------------------------------------------------------------------------- /mrbgems/mruby-error/test/exception.rb: -------------------------------------------------------------------------------- 1 | assert 'mrb_protect' do 2 | # no failure in protect returns [result, false] 3 | assert_equal ['test', false] do 4 | ExceptionTest.mrb_protect { 'test' } 5 | end 6 | # failure in protect returns [exception, true] 7 | result = ExceptionTest.mrb_protect { raise 'test' } 8 | assert_kind_of RuntimeError, result[0] 9 | assert_true result[1] 10 | end 11 | 12 | assert 'mrb_ensure' do 13 | a = false 14 | assert_equal 'test' do 15 | ExceptionTest.mrb_ensure Proc.new { 'test' }, Proc.new { a = true } 16 | end 17 | assert_true a 18 | 19 | a = false 20 | assert_raise RuntimeError do 21 | ExceptionTest.mrb_ensure Proc.new { raise 'test' }, Proc.new { a = true } 22 | end 23 | assert_true a 24 | end 25 | 26 | assert 'mrb_rescue' do 27 | assert_equal 'test' do 28 | ExceptionTest.mrb_rescue Proc.new { 'test' }, Proc.new {} 29 | end 30 | 31 | class CustomExp < Exception 32 | end 33 | 34 | assert_raise CustomExp do 35 | ExceptionTest.mrb_rescue Proc.new { raise CustomExp.new 'test' }, Proc.new { 'rescue' } 36 | end 37 | 38 | assert_equal 'rescue' do 39 | ExceptionTest.mrb_rescue Proc.new { raise 'test' }, Proc.new { 'rescue' } 40 | end 41 | end 42 | 43 | assert 'mrb_rescue_exceptions' do 44 | assert_equal 'test' do 45 | ExceptionTest.mrb_rescue_exceptions Proc.new { 'test' }, Proc.new {} 46 | end 47 | 48 | assert_raise RangeError do 49 | ExceptionTest.mrb_rescue_exceptions Proc.new { raise RangeError.new 'test' }, Proc.new { 'rescue' } 50 | end 51 | 52 | assert_equal 'rescue' do 53 | ExceptionTest.mrb_rescue_exceptions Proc.new { raise TypeError.new 'test' }, Proc.new { 'rescue' } 54 | end 55 | end 56 | -------------------------------------------------------------------------------- /include/mruby/boxing_no.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** mruby/boxing_no.h - unboxed mrb_value definition 3 | ** 4 | ** See Copyright Notice in mruby.h 5 | */ 6 | 7 | #ifndef MRUBY_BOXING_NO_H 8 | #define MRUBY_BOXING_NO_H 9 | 10 | #define MRB_FIXNUM_SHIFT 0 11 | #define MRB_TT_HAS_BASIC MRB_TT_OBJECT 12 | 13 | typedef struct mrb_value { 14 | union { 15 | mrb_float f; 16 | void *p; 17 | mrb_int i; 18 | mrb_sym sym; 19 | } value; 20 | enum mrb_vtype tt; 21 | } mrb_value; 22 | 23 | #define mrb_float_pool(mrb,f) mrb_float_value(mrb,f) 24 | 25 | #define mrb_ptr(o) (o).value.p 26 | #define mrb_cptr(o) mrb_ptr(o) 27 | #define mrb_float(o) (o).value.f 28 | #define mrb_fixnum(o) (o).value.i 29 | #define mrb_symbol(o) (o).value.sym 30 | #define mrb_type(o) (o).tt 31 | 32 | #define BOXNIX_SET_VALUE(o, ttt, attr, v) do {\ 33 | (o).tt = ttt;\ 34 | (o).attr = v;\ 35 | } while (0) 36 | 37 | #define SET_NIL_VALUE(r) BOXNIX_SET_VALUE(r, MRB_TT_FALSE, value.i, 0) 38 | #define SET_FALSE_VALUE(r) BOXNIX_SET_VALUE(r, MRB_TT_FALSE, value.i, 1) 39 | #define SET_TRUE_VALUE(r) BOXNIX_SET_VALUE(r, MRB_TT_TRUE, value.i, 1) 40 | #define SET_BOOL_VALUE(r,b) BOXNIX_SET_VALUE(r, b ? MRB_TT_TRUE : MRB_TT_FALSE, value.i, 1) 41 | #define SET_INT_VALUE(r,n) BOXNIX_SET_VALUE(r, MRB_TT_FIXNUM, value.i, (n)) 42 | #define SET_FLOAT_VALUE(mrb,r,v) BOXNIX_SET_VALUE(r, MRB_TT_FLOAT, value.f, (v)) 43 | #define SET_SYM_VALUE(r,v) BOXNIX_SET_VALUE(r, MRB_TT_SYMBOL, value.sym, (v)) 44 | #define SET_OBJ_VALUE(r,v) BOXNIX_SET_VALUE(r, (((struct RObject*)(v))->tt), value.p, (v)) 45 | #define SET_CPTR_VALUE(mrb,r,v) BOXNIX_SET_VALUE(r, MRB_TT_CPTR, value.p, v) 46 | #define SET_UNDEF_VALUE(r) BOXNIX_SET_VALUE(r, MRB_TT_UNDEF, value.i, 0) 47 | 48 | #endif /* MRUBY_BOXING_NO_H */ 49 | -------------------------------------------------------------------------------- /mrbgems/mruby-proc-ext/test/proc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | static mrb_value 6 | return_func_name(mrb_state *mrb, mrb_value self) 7 | { 8 | return mrb_cfunc_env_get(mrb, 0); 9 | } 10 | 11 | static mrb_value 12 | proc_new_cfunc_with_env(mrb_state *mrb, mrb_value self) 13 | { 14 | mrb_sym n; 15 | mrb_value n_val; 16 | mrb_get_args(mrb, "n", &n); 17 | n_val = mrb_symbol_value(n); 18 | mrb_define_method_raw(mrb, mrb_class_ptr(self), n, 19 | mrb_proc_new_cfunc_with_env(mrb, return_func_name, 1, &n_val)); 20 | return self; 21 | } 22 | 23 | static mrb_value 24 | return_env(mrb_state *mrb, mrb_value self) 25 | { 26 | mrb_int idx; 27 | mrb_get_args(mrb, "i", &idx); 28 | return mrb_cfunc_env_get(mrb, idx); 29 | } 30 | 31 | static mrb_value 32 | cfunc_env_get(mrb_state *mrb, mrb_value self) 33 | { 34 | mrb_sym n; 35 | mrb_value *argv; mrb_int argc; 36 | mrb_get_args(mrb, "na", &n, &argv, &argc); 37 | mrb_define_method_raw(mrb, mrb_class_ptr(self), n, 38 | mrb_proc_new_cfunc_with_env(mrb, return_env, argc, argv)); 39 | return self; 40 | } 41 | 42 | static mrb_value 43 | cfunc_without_env(mrb_state *mrb, mrb_value self) 44 | { 45 | return mrb_cfunc_env_get(mrb, 0); 46 | } 47 | 48 | void mrb_mruby_proc_ext_gem_test(mrb_state *mrb) 49 | { 50 | struct RClass *cls; 51 | 52 | cls = mrb_define_class(mrb, "ProcExtTest", mrb->object_class); 53 | mrb_define_module_function(mrb, cls, "mrb_proc_new_cfunc_with_env", proc_new_cfunc_with_env, MRB_ARGS_REQ(1)); 54 | mrb_define_module_function(mrb, cls, "mrb_cfunc_env_get", cfunc_env_get, MRB_ARGS_REQ(2)); 55 | mrb_define_module_function(mrb, cls, "cfunc_without_env", cfunc_without_env, MRB_ARGS_NONE()); 56 | } 57 | -------------------------------------------------------------------------------- /mrbgems/mruby-kernel-ext/test/kernel.rb: -------------------------------------------------------------------------------- 1 | assert('Kernel.fail, Kernel#fail') do 2 | assert_raise(RuntimeError) { fail } 3 | assert_raise(RuntimeError) { Kernel.fail } 4 | end 5 | 6 | assert('Kernel#__method__') do 7 | assert_equal(:m, Class.new {def m; __method__; end}.new.m) 8 | assert_equal(:m, Class.new {define_method(:m) {__method__}}.new.m) 9 | c = Class.new do 10 | [:m1, :m2].each do |m| 11 | define_method(m) do 12 | __method__ 13 | end 14 | end 15 | end 16 | assert_equal(:m1, c.new.m1) 17 | assert_equal(:m2, c.new.m2) 18 | end 19 | 20 | assert('Kernel#Integer') do 21 | assert_equal(123, Integer(123.999)) 22 | assert_equal(26, Integer("0x1a")) 23 | assert_equal(930, Integer("0930", 10)) 24 | assert_equal(7, Integer("111", 2)) 25 | assert_equal(0, Integer("0")) 26 | assert_equal(0, Integer("00000")) 27 | assert_raise(TypeError) { Integer(nil) } 28 | end 29 | 30 | assert('Kernel#Float') do 31 | assert_equal(1.0, Float(1)) 32 | assert_equal(123.456, Float(123.456)) 33 | assert_equal(123.456, Float("123.456")) 34 | assert_raise(TypeError) { Float(nil) } 35 | end 36 | 37 | assert('Kernel#String') do 38 | assert_equal("main", String(self)) 39 | assert_equal("Object", String(self.class)) 40 | assert_equal("123456", String(123456)) 41 | end 42 | 43 | assert('Kernel#Array') do 44 | assert_equal([1], Kernel.Array(1)) 45 | assert_equal([1, 2, 3, 4, 5], Kernel.Array([1, 2, 3, 4, 5])) 46 | assert_equal([1, 2, 3, 4, 5], Kernel.Array(1..5)) 47 | assert_equal([[:a, 1], [:b, 2], [:c, 3]], Kernel.Array({a:1, b:2, c:3})) 48 | end 49 | 50 | assert('Kernel#Hash') do 51 | assert_equal({}, Hash([])) 52 | assert_equal({}, Hash(nil)) 53 | assert_equal({:key => :value}, Hash(key: :value)) 54 | assert_raise(TypeError) { Hash([1, 2, 3]) } 55 | end 56 | -------------------------------------------------------------------------------- /mrbgems/mruby-bin-mruby/bintest/mruby.rb: -------------------------------------------------------------------------------- 1 | require 'tempfile' 2 | 3 | assert('regression for #1564') do 4 | o = `#{cmd('mruby')} -e #{shellquote('<<')} 2>&1` 5 | assert_equal o, "-e:1:2: syntax error, unexpected tLSHFT\n" 6 | o = `#{cmd('mruby')} -e #{shellquote('<<-')} 2>&1` 7 | assert_equal o, "-e:1:3: syntax error, unexpected tLSHFT\n" 8 | end 9 | 10 | assert('regression for #1572') do 11 | script, bin = Tempfile.new('test.rb'), Tempfile.new('test.mrb') 12 | File.write script.path, 'p "ok"' 13 | system "#{cmd('mrbc')} -g -o #{bin.path} #{script.path}" 14 | o = `#{cmd('mruby')} -b #{bin.path}`.strip 15 | assert_equal o, '"ok"' 16 | end 17 | 18 | assert '$0 value' do 19 | script, bin = Tempfile.new('test.rb'), Tempfile.new('test.mrb') 20 | 21 | # .rb script 22 | script.write "p $0\n" 23 | script.flush 24 | assert_equal "\"#{script.path}\"", `#{cmd('mruby')} "#{script.path}"`.chomp 25 | 26 | # .mrb file 27 | `#{cmd('mrbc')} -o "#{bin.path}" "#{script.path}"` 28 | assert_equal "\"#{bin.path}\"", `#{cmd('mruby')} -b "#{bin.path}"`.chomp 29 | 30 | # one liner 31 | assert_equal '"-e"', `#{cmd('mruby')} -e #{shellquote('p $0')}`.chomp 32 | end 33 | 34 | assert '__END__', '8.6' do 35 | script = Tempfile.new('test.rb') 36 | 37 | script.write <= 201112L 33 | # define mrb_noreturn _Noreturn 34 | #elif defined __GNUC__ && !defined __STRICT_ANSI__ 35 | # define mrb_noreturn __attribute__((noreturn)) 36 | #elif defined _MSC_VER 37 | # define mrb_noreturn __declspec(noreturn) 38 | #else 39 | # define mrb_noreturn 40 | #endif 41 | 42 | /** Mark a function as deprecated. */ 43 | #if defined __GNUC__ && !defined __STRICT_ANSI__ 44 | # define mrb_deprecated __attribute__((deprecated)) 45 | #elif defined _MSC_VER 46 | # define mrb_deprecated __declspec(deprecated) 47 | #else 48 | # define mrb_deprecated 49 | #endif 50 | 51 | /** Declare a function as always inlined. */ 52 | #if defined(_MSC_VER) 53 | # define MRB_INLINE static __inline 54 | #else 55 | # define MRB_INLINE static inline 56 | #endif 57 | 58 | 59 | /** Declare a public MRuby API function. */ 60 | #if defined(MRB_BUILD_AS_DLL) 61 | #if defined(MRB_CORE) || defined(MRB_LIB) 62 | # define MRB_API __declspec(dllexport) 63 | #else 64 | # define MRB_API __declspec(dllimport) 65 | #endif 66 | #else 67 | # define MRB_API extern 68 | #endif 69 | 70 | MRB_END_DECL 71 | 72 | #endif /* MRUBY_COMMON_H */ 73 | -------------------------------------------------------------------------------- /include/mruby/debug.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** mruby/debug.h - mruby debug info 3 | ** 4 | ** See Copyright Notice in mruby.h 5 | */ 6 | 7 | #ifndef MRUBY_DEBUG_H 8 | #define MRUBY_DEBUG_H 9 | 10 | #include "common.h" 11 | 12 | /** 13 | * MRuby Debugging. 14 | */ 15 | MRB_BEGIN_DECL 16 | 17 | typedef enum mrb_debug_line_type { 18 | mrb_debug_line_ary = 0, 19 | mrb_debug_line_flat_map = 1 20 | } mrb_debug_line_type; 21 | 22 | typedef struct mrb_irep_debug_info_line { 23 | uint32_t start_pos; 24 | uint16_t line; 25 | } mrb_irep_debug_info_line; 26 | 27 | typedef struct mrb_irep_debug_info_file { 28 | uint32_t start_pos; 29 | const char *filename; 30 | mrb_sym filename_sym; 31 | uint32_t line_entry_count; 32 | mrb_debug_line_type line_type; 33 | union { 34 | void *ptr; 35 | mrb_irep_debug_info_line *flat_map; 36 | uint16_t *ary; 37 | } lines; 38 | } mrb_irep_debug_info_file; 39 | 40 | typedef struct mrb_irep_debug_info { 41 | uint32_t pc_count; 42 | uint16_t flen; 43 | mrb_irep_debug_info_file **files; 44 | } mrb_irep_debug_info; 45 | 46 | /* 47 | * get line from irep's debug info and program counter 48 | * @return returns NULL if not found 49 | */ 50 | MRB_API const char *mrb_debug_get_filename(mrb_irep *irep, uint32_t pc); 51 | 52 | /* 53 | * get line from irep's debug info and program counter 54 | * @return returns -1 if not found 55 | */ 56 | MRB_API int32_t mrb_debug_get_line(mrb_irep *irep, uint32_t pc); 57 | 58 | MRB_API mrb_irep_debug_info_file *mrb_debug_info_append_file( 59 | mrb_state *mrb, mrb_irep *irep, 60 | uint32_t start_pos, uint32_t end_pos); 61 | MRB_API mrb_irep_debug_info *mrb_debug_info_alloc(mrb_state *mrb, mrb_irep *irep); 62 | MRB_API void mrb_debug_info_free(mrb_state *mrb, mrb_irep_debug_info *d); 63 | 64 | MRB_END_DECL 65 | 66 | #endif /* MRUBY_DEBUG_H */ 67 | -------------------------------------------------------------------------------- /test/t/comparable.rb: -------------------------------------------------------------------------------- 1 | 2 | assert('Comparable#<', '15.3.3.2.1') do 3 | class Foo 4 | include Comparable 5 | def <=>(x) 6 | x 7 | end 8 | end 9 | assert_false(Foo.new < 0) 10 | assert_false(Foo.new < 1) 11 | assert_true(Foo.new < -1) 12 | assert_raise(ArgumentError){ Foo.new < nil } 13 | end 14 | 15 | assert('Comparable#<=', '15.3.3.2.2') do 16 | class Foo 17 | include Comparable 18 | def <=>(x) 19 | x 20 | end 21 | end 22 | assert_true(Foo.new <= 0) 23 | assert_false(Foo.new <= 1) 24 | assert_true(Foo.new <= -1) 25 | assert_raise(ArgumentError){ Foo.new <= nil } 26 | end 27 | 28 | assert('Comparable#==', '15.3.3.2.3') do 29 | class Foo 30 | include Comparable 31 | def <=>(x) 32 | 0 33 | end 34 | end 35 | 36 | assert_true(Foo.new == Foo.new) 37 | end 38 | 39 | assert('Comparable#>', '15.3.3.2.4') do 40 | class Foo 41 | include Comparable 42 | def <=>(x) 43 | x 44 | end 45 | end 46 | assert_false(Foo.new > 0) 47 | assert_true(Foo.new > 1) 48 | assert_false(Foo.new > -1) 49 | assert_raise(ArgumentError){ Foo.new > nil } 50 | end 51 | 52 | assert('Comparable#>=', '15.3.3.2.5') do 53 | class Foo 54 | include Comparable 55 | def <=>(x) 56 | x 57 | end 58 | end 59 | assert_true(Foo.new >= 0) 60 | assert_true(Foo.new >= 1) 61 | assert_false(Foo.new >= -1) 62 | assert_raise(ArgumentError){ Foo.new >= nil } 63 | end 64 | 65 | assert('Comparable#between?', '15.3.3.2.6') do 66 | class Foo 67 | include Comparable 68 | def <=>(x) 69 | x 70 | end 71 | end 72 | 73 | c = Foo.new 74 | 75 | assert_false(c.between?(-1, 1)) 76 | assert_false(c.between?(-1, -1)) 77 | assert_false(c.between?( 1, 1)) 78 | assert_true(c.between?( 1, -1)) 79 | assert_true(c.between?(0, 0)) 80 | end 81 | -------------------------------------------------------------------------------- /mrbgems/mruby-objectspace/test/objectspace.rb: -------------------------------------------------------------------------------- 1 | assert('ObjectSpace.count_objects') do 2 | h = {} 3 | f = Fiber.new {} if Object.const_defined? :Fiber 4 | ObjectSpace.count_objects(h) 5 | assert_kind_of(Hash, h) 6 | assert_true(h.keys.all? {|x| x.is_a?(Symbol) || x.is_a?(Integer) }) 7 | assert_true(h.values.all? {|x| x.is_a?(Integer) }) 8 | 9 | assert_true(h.has_key?(:TOTAL)) 10 | assert_true(h.has_key?(:FREE)) 11 | assert_true(h.has_key?(:T_FIBER)) if Object.const_defined? :Fiber 12 | 13 | assert_equal(h[:TOTAL] * 2, h.values.reduce(:+)) 14 | 15 | h = ObjectSpace.count_objects 16 | assert_kind_of(Hash, h) 17 | assert_true(h.keys.all? {|x| x.is_a?(Symbol) || x.is_a?(Integer) }) 18 | assert_true(h.values.all? {|x| x.is_a?(Integer) }) 19 | 20 | assert_raise(TypeError) { ObjectSpace.count_objects(1) } 21 | 22 | h0 = {:T_FOO=>1000} 23 | h = ObjectSpace.count_objects(h0) 24 | assert_false(h0.has_key?(:T_FOO)) 25 | 26 | GC.start 27 | h_after = {} 28 | h_before = ObjectSpace.count_objects 29 | 30 | objs = [] 31 | 1000.times do 32 | objs << {} 33 | end 34 | objs = nil 35 | ObjectSpace.count_objects(h) 36 | GC.start 37 | ObjectSpace.count_objects(h_after) 38 | 39 | assert_equal(h[:T_HASH], h_before[:T_HASH] + 1000) 40 | assert_equal(h_after[:T_HASH], h_before[:T_HASH]) 41 | end 42 | 43 | assert('ObjectSpace.each_object') do 44 | objs = [] 45 | objs_count = ObjectSpace.each_object { |obj| 46 | objs << obj 47 | } 48 | assert_equal objs.length, objs_count 49 | 50 | arys = [] 51 | arys_count = ObjectSpace.each_object(Array) { |obj| 52 | arys << obj 53 | } 54 | assert_equal arys.length, arys_count 55 | assert_true arys.length < objs.length 56 | end 57 | 58 | assert 'Check class pointer of ObjectSpace.each_object.' do 59 | ObjectSpace.each_object { |obj| !obj } 60 | end 61 | -------------------------------------------------------------------------------- /mrbgems/mruby-error/test/exception.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | static mrb_value 6 | protect_cb(mrb_state *mrb, mrb_value b) 7 | { 8 | return mrb_yield_argv(mrb, b, 0, NULL); 9 | } 10 | 11 | static mrb_value 12 | run_protect(mrb_state *mrb, mrb_value self) 13 | { 14 | mrb_value b; 15 | mrb_value ret[2]; 16 | mrb_bool state; 17 | mrb_get_args(mrb, "&", &b); 18 | ret[0] = mrb_protect(mrb, protect_cb, b, &state); 19 | ret[1] = mrb_bool_value(state); 20 | return mrb_ary_new_from_values(mrb, 2, ret); 21 | } 22 | 23 | static mrb_value 24 | run_ensure(mrb_state *mrb, mrb_value self) 25 | { 26 | mrb_value b, e; 27 | mrb_get_args(mrb, "oo", &b, &e); 28 | return mrb_ensure(mrb, protect_cb, b, protect_cb, e); 29 | } 30 | 31 | static mrb_value 32 | run_rescue(mrb_state *mrb, mrb_value self) 33 | { 34 | mrb_value b, r; 35 | mrb_get_args(mrb, "oo", &b, &r); 36 | return mrb_rescue(mrb, protect_cb, b, protect_cb, r); 37 | } 38 | 39 | static mrb_value 40 | run_rescue_exceptions(mrb_state *mrb, mrb_value self) 41 | { 42 | mrb_value b, r; 43 | struct RClass *cls[1]; 44 | mrb_get_args(mrb, "oo", &b, &r); 45 | cls[0] = E_TYPE_ERROR; 46 | return mrb_rescue_exceptions(mrb, protect_cb, b, protect_cb, r, 1, cls); 47 | } 48 | 49 | void 50 | mrb_mruby_error_gem_test(mrb_state *mrb) 51 | { 52 | struct RClass *cls; 53 | 54 | cls = mrb_define_class(mrb, "ExceptionTest", mrb->object_class); 55 | mrb_define_module_function(mrb, cls, "mrb_protect", run_protect, MRB_ARGS_NONE() | MRB_ARGS_BLOCK()); 56 | mrb_define_module_function(mrb, cls, "mrb_ensure", run_ensure, MRB_ARGS_REQ(2)); 57 | mrb_define_module_function(mrb, cls, "mrb_rescue", run_rescue, MRB_ARGS_REQ(2)); 58 | mrb_define_module_function(mrb, cls, "mrb_rescue_exceptions", run_rescue_exceptions, MRB_ARGS_REQ(2)); 59 | } 60 | -------------------------------------------------------------------------------- /test/t/lang.rb: -------------------------------------------------------------------------------- 1 | # The aim of these tests is to detect pitfall for optimized VM. 2 | 3 | # Test for or/and 4 | # 5 | # You may think instruction fusion(OP_EQ and OP_JMPIF) for avoiding 6 | # generate intermediate boolean value. 7 | # But and/or is pitfall for this fusioning. 8 | # 9 | # For example, the following mruby code: 10 | # 11 | # if i > 0 and i < 10 then 12 | # 13 | # compiles to the following byte code: 14 | # 15 | # 1 000 OP_LOADI R1 0 ; R1:i 16 | # 2 001 OP_MOVE R2 R1 ; R1:i 17 | # 2 002 OP_LOADI R3 0 18 | # 2 003 OP_GT R2 :> 1 19 | # 2 004 OP_JMPNOT R2 008 20 | # 2 005 OP_MOVE R2 R1 ; R1:i 21 | # 2 006 OP_LOADI R3 10 22 | # 2 007 OP_LT R2 :< 1 23 | # 2 008 OP_JMPNOT R2 (The address of end of then part) 24 | # 25 | # When the instruction fusion the OP_GT and OP_JMPNOT you fell into the pitfalls. 26 | # The deleted intermediate boolean value is used in OP_JMPNOT (address 008). 27 | 28 | assert('and', '11.2.3') do 29 | a = 1 30 | if a > 0 and a < 10 then 31 | b = 1 32 | else 33 | b = 0 34 | end 35 | assert_equal 1, b 36 | 37 | if a < 0 and a < 10 then 38 | b = 1 39 | else 40 | b = 0 41 | end 42 | assert_equal 0, b 43 | 44 | if a < 0 and a > 10 then 45 | b = 1 46 | else 47 | b = 0 48 | end 49 | assert_equal 0, b 50 | end 51 | 52 | assert('or','11.2.4') do 53 | a = 1 54 | if a > 0 or a < 10 then 55 | b = 1 56 | else 57 | b = 0 58 | end 59 | assert_equal 1, b 60 | 61 | if a < 0 or a < 10 then 62 | b = 1 63 | else 64 | b = 0 65 | end 66 | assert_equal 1, b 67 | 68 | if a < 0 or a > 10 then 69 | b = 1 70 | else 71 | b = 0 72 | end 73 | assert_equal 0, b 74 | end 75 | -------------------------------------------------------------------------------- /mrbgems/mruby-symbol-ext/src/symbol.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | typedef struct symbol_name { 6 | size_t len; 7 | const char *name; 8 | } symbol_name; 9 | 10 | /* 11 | * call-seq: 12 | * Symbol.all_symbols => array 13 | * 14 | * Returns an array of all the symbols currently in Ruby's symbol 15 | * table. 16 | * 17 | * Symbol.all_symbols.size #=> 903 18 | * Symbol.all_symbols[1,20] #=> [:floor, :ARGV, :Binding, :symlink, 19 | * :chown, :EOFError, :$;, :String, 20 | * :LOCK_SH, :"setuid?", :$<, 21 | * :default_proc, :compact, :extend, 22 | * :Tms, :getwd, :$=, :ThreadGroup, 23 | * :wait2, :$>] 24 | */ 25 | static mrb_value 26 | mrb_sym_all_symbols(mrb_state *mrb, mrb_value self) 27 | { 28 | mrb_sym i, lim; 29 | mrb_value ary = mrb_ary_new_capa(mrb, mrb->symidx); 30 | 31 | for (i=1, lim=mrb->symidx+1; i integer 41 | * 42 | * Same as sym.to_s.length. 43 | */ 44 | static mrb_value 45 | mrb_sym_length(mrb_state *mrb, mrb_value self) 46 | { 47 | mrb_int len; 48 | mrb_sym2name_len(mrb, mrb_symbol(self), &len); 49 | return mrb_fixnum_value(len); 50 | } 51 | 52 | void 53 | mrb_mruby_symbol_ext_gem_init(mrb_state* mrb) 54 | { 55 | struct RClass *s = mrb->symbol_class; 56 | mrb_define_class_method(mrb, s, "all_symbols", mrb_sym_all_symbols, MRB_ARGS_NONE()); 57 | mrb_define_method(mrb, s, "length", mrb_sym_length, MRB_ARGS_NONE()); 58 | mrb_define_method(mrb, s, "size", mrb_sym_length, MRB_ARGS_NONE()); 59 | } 60 | 61 | void 62 | mrb_mruby_symbol_ext_gem_final(mrb_state* mrb) 63 | { 64 | } 65 | -------------------------------------------------------------------------------- /mrblib/compar.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # Comparable 3 | # 4 | # ISO 15.3.3 5 | module Comparable 6 | 7 | ## 8 | # Return true if +self+ is less 9 | # than +other+. Otherwise return 10 | # false. 11 | # 12 | # ISO 15.3.3.2.1 13 | def < other 14 | cmp = self <=> other 15 | if cmp.nil? 16 | raise ArgumentError, "comparison of #{self.class} with #{other.class} failed" 17 | end 18 | cmp < 0 19 | end 20 | 21 | ## 22 | # Return true if +self+ is less 23 | # than or equal to +other+. 24 | # Otherwise return false. 25 | # 26 | # ISO 15.3.3.2.2 27 | def <= other 28 | cmp = self <=> other 29 | if cmp.nil? 30 | raise ArgumentError, "comparison of #{self.class} with #{other.class} failed" 31 | end 32 | cmp <= 0 33 | end 34 | 35 | ## 36 | # Return true if +self+ is equal 37 | # to +other+. Otherwise return 38 | # false. 39 | # 40 | # ISO 15.3.3.2.3 41 | def == other 42 | cmp = self <=> other 43 | cmp == 0 44 | end 45 | 46 | ## 47 | # Return true if +self+ is greater 48 | # than +other+. Otherwise return 49 | # false. 50 | # 51 | # ISO 15.3.3.2.4 52 | def > other 53 | cmp = self <=> other 54 | if cmp.nil? 55 | raise ArgumentError, "comparison of #{self.class} with #{other.class} failed" 56 | end 57 | cmp > 0 58 | end 59 | 60 | ## 61 | # Return true if +self+ is greater 62 | # than or equal to +other+. 63 | # Otherwise return false. 64 | # 65 | # ISO 15.3.3.2.5 66 | def >= other 67 | cmp = self <=> other 68 | if cmp.nil? 69 | raise ArgumentError, "comparison of #{self.class} with #{other.class} failed" 70 | end 71 | cmp >= 0 72 | end 73 | 74 | ## 75 | # Return true if +self+ is greater 76 | # than or equal to +min+ and 77 | # less than or equal to +max+. 78 | # Otherwise return false. 79 | # 80 | # ISO 15.3.3.2.6 81 | def between?(min, max) 82 | self >= min and self <= max 83 | end 84 | end 85 | -------------------------------------------------------------------------------- /tasks/ruby_ext.rake: -------------------------------------------------------------------------------- 1 | class Object 2 | class << self 3 | def attr_block(*syms) 4 | syms.flatten.each do |sym| 5 | class_eval "def #{sym}(&block);block.call(@#{sym}) if block_given?;@#{sym};end" 6 | end 7 | end 8 | end 9 | end 10 | 11 | class String 12 | def relative_path_from(dir) 13 | Pathname.new(File.expand_path(self)).relative_path_from(Pathname.new(File.expand_path(dir))).to_s 14 | end 15 | 16 | def relative_path 17 | relative_path_from(Dir.pwd) 18 | end 19 | 20 | # Compatible with 1.9 on 1.8 21 | def %(params) 22 | if params.is_a?(Hash) 23 | str = self.clone 24 | params.each do |k, v| 25 | str.gsub!("%{#{k}}") { v } 26 | end 27 | str 28 | else 29 | if params.is_a?(Array) 30 | sprintf(self, *params) 31 | else 32 | sprintf(self, params) 33 | end 34 | end 35 | end 36 | end 37 | 38 | class Symbol 39 | # Compatible with 1.9 on 1.8 40 | def to_proc 41 | proc { |obj, *args| obj.send(self, *args) } 42 | end 43 | end 44 | 45 | module Enumerable 46 | # Compatible with 1.9 on 1.8 47 | def each_with_object(memo) 48 | return to_enum :each_with_object, memo unless block_given? 49 | each { |obj| yield obj, memo } 50 | memo 51 | end 52 | end 53 | 54 | $pp_show = true 55 | 56 | if $verbose.nil? 57 | if Rake.respond_to?(:verbose) && !Rake.verbose.nil? 58 | if Rake.verbose.class == TrueClass 59 | # verbose message logging 60 | $pp_show = false 61 | else 62 | $pp_show = true 63 | Rake.verbose(false) 64 | end 65 | else 66 | # could not identify rake version 67 | $pp_show = false 68 | end 69 | else 70 | $pp_show = false if $verbose 71 | end 72 | 73 | def _pp(cmd, src, tgt=nil, options={}) 74 | return unless $pp_show 75 | 76 | width = 5 77 | template = options[:indent] ? "%#{width*options[:indent]}s %s %s" : "%-#{width}s %s %s" 78 | puts template % [cmd, src, tgt ? "-> #{tgt}" : nil] 79 | end 80 | -------------------------------------------------------------------------------- /mrbgems/mruby-bin-debugger/tools/mrdb/apiprint.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** apiprint.c 3 | ** 4 | */ 5 | 6 | #include 7 | #include "mrdb.h" 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "apiprint.h" 15 | 16 | static void 17 | mrdb_check_syntax(mrb_state *mrb, mrb_debug_context *dbg, const char *expr, size_t len) 18 | { 19 | mrbc_context *c; 20 | 21 | c = mrbc_context_new(mrb); 22 | c->no_exec = TRUE; 23 | c->capture_errors = TRUE; 24 | c->filename = (char*)dbg->prvfile; 25 | c->lineno = dbg->prvline; 26 | 27 | /* Load program */ 28 | mrb_load_nstring_cxt(mrb, expr, len, c); 29 | 30 | mrbc_context_free(mrb, c); 31 | } 32 | 33 | mrb_value 34 | mrb_debug_eval(mrb_state *mrb, mrb_debug_context *dbg, const char *expr, size_t len, mrb_bool *exc) 35 | { 36 | void (*tmp)(struct mrb_state *, struct mrb_irep *, mrb_code *, mrb_value *); 37 | mrb_value ruby_code; 38 | mrb_value s; 39 | mrb_value v; 40 | mrb_value recv; 41 | 42 | /* disable code_fetch_hook */ 43 | tmp = mrb->code_fetch_hook; 44 | mrb->code_fetch_hook = NULL; 45 | 46 | mrdb_check_syntax(mrb, dbg, expr, len); 47 | if (mrb->exc) { 48 | v = mrb_obj_value(mrb->exc); 49 | mrb->exc = 0; 50 | } 51 | else { 52 | /* 53 | * begin 54 | * expr 55 | * rescue => e 56 | * e 57 | * end 58 | */ 59 | ruby_code = mrb_str_new_lit(mrb, "begin\n"); 60 | ruby_code = mrb_str_cat(mrb, ruby_code, expr, len); 61 | ruby_code = mrb_str_cat_lit(mrb, ruby_code, "\nrescue => e\ne\nend"); 62 | 63 | recv = dbg->regs[0]; 64 | 65 | v = mrb_funcall(mrb, recv, "instance_eval", 1, ruby_code); 66 | } 67 | 68 | if (exc) { 69 | *exc = mrb_obj_is_kind_of(mrb, v, mrb->eException_class); 70 | } 71 | 72 | s = mrb_funcall(mrb, v, "inspect", 0); 73 | 74 | /* enable code_fetch_hook */ 75 | mrb->code_fetch_hook = tmp; 76 | 77 | return s; 78 | } 79 | -------------------------------------------------------------------------------- /test/t/superclass.rb: -------------------------------------------------------------------------------- 1 | [ 2 | # [:Object, :implementation_defined_value, '15.2.2.1'], 3 | [:Module, :Object, '15.2.2.2'], 4 | [:Class, :Module, '15.2.3.2'], 5 | [:NilClass, :Object, '15.2.4.2'], 6 | [:TrueClass, :Object, '15.2.5.2'], 7 | [:FalseClass, :Object, '15.2.6.2'], 8 | [:Numeric, :Object, '15.2.7.2'], 9 | [:Integer, :Numeric, '15.2.8.2'], 10 | [:Float, :Numeric, '15.2.9.2'], 11 | [:String, :Object, '15.2.10.2'], 12 | [:Symbol, :Object, '15.2.11.2'], 13 | [:Array, :Object, '15.2.12.2'], 14 | [:Hash, :Object, '15.2.13.2'], 15 | [:Range, :Object, '15.2.14.2'], 16 | # [:Regexp, :Object, '15.2.15.2'], #No Regexp in mruby core 17 | # [:MatchData, :Object, '15.2.16.2'], 18 | [:Proc, :Object, '15.2.17.2'], 19 | # [:Struct, :Object, '15.2.18.2'], 20 | # [:Time, :Object, '15.2.19.2'], 21 | # [:IO, :Object, '15.2.20.2'], 22 | # [:File, :IO, '15.2.21.2'], 23 | [:Exception, :Object, '15.2.22.2'], 24 | [:StandardError, :Exception, '15.2.23.2'], 25 | [:ArgumentError, :StandardError, '15.2.24.2'], 26 | [:LocalJumpError, :StandardError, '15.2.25.2'], 27 | [:RangeError, :StandardError, '12.2.26.2'], 28 | [:RegexpError, :StandardError, '12.2.27.2'], 29 | [:RuntimeError, :StandardError, '12.2.28.2'], 30 | [:TypeError, :StandardError, '12.2.29.2'], 31 | # [:ZeroDivisionError, :StandardError, '12.2.30.2'], # No ZeroDivisionError in mruby 32 | [:NameError, :StandardError, '15.2.31.2'], 33 | [:NoMethodError, :NameError, '15.2.32.2'], 34 | [:IndexError, :StandardError, '15.2.33.2'], 35 | # [:IOError, :StandardError, '12.2.34.2'], 36 | # [:EOFError, :IOError, '12.2.35.2'], 37 | # [:SystemCallError, :StandardError, '15.2.36.2'], 38 | [:ScriptError, :Exception, '12.2.37.2'], 39 | [:SyntaxError, :ScriptError, '12.2.38.2'], 40 | # [:LoadError, :ScriptError, '12.2.39,2'], 41 | ].each do |cls, super_cls, iso| 42 | assert "Direct superclass of #{cls}", iso do 43 | skip "#{cls} isn't defined" unless Object.const_defined? cls 44 | assert_equal Object.const_get(super_cls), Object.const_get(cls).superclass 45 | end 46 | end 47 | -------------------------------------------------------------------------------- /include/mruby/error.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** mruby/error.h - Exception class 3 | ** 4 | ** See Copyright Notice in mruby.h 5 | */ 6 | 7 | #ifndef MRUBY_ERROR_H 8 | #define MRUBY_ERROR_H 9 | 10 | #include "common.h" 11 | 12 | /** 13 | * MRuby error handling. 14 | */ 15 | MRB_BEGIN_DECL 16 | 17 | struct RException { 18 | MRB_OBJECT_HEADER; 19 | struct iv_tbl *iv; 20 | }; 21 | 22 | #define mrb_exc_ptr(v) ((struct RException*)mrb_ptr(v)) 23 | 24 | MRB_API void mrb_sys_fail(mrb_state *mrb, const char *mesg); 25 | MRB_API mrb_value mrb_exc_new_str(mrb_state *mrb, struct RClass* c, mrb_value str); 26 | #define mrb_exc_new_str_lit(mrb, c, lit) mrb_exc_new_str(mrb, c, mrb_str_new_lit(mrb, lit)) 27 | MRB_API mrb_value mrb_make_exception(mrb_state *mrb, int argc, const mrb_value *argv); 28 | MRB_API mrb_value mrb_exc_backtrace(mrb_state *mrb, mrb_value exc); 29 | MRB_API mrb_value mrb_get_backtrace(mrb_state *mrb); 30 | MRB_API mrb_noreturn void mrb_no_method_error(mrb_state *mrb, mrb_sym id, mrb_value args, const char *fmt, ...); 31 | 32 | /* declaration for fail method */ 33 | MRB_API mrb_value mrb_f_raise(mrb_state*, mrb_value); 34 | 35 | /** 36 | * Protect 37 | * 38 | * @mrbgem mruby-error 39 | */ 40 | MRB_API mrb_value mrb_protect(mrb_state *mrb, mrb_func_t body, mrb_value data, mrb_bool *state); 41 | 42 | /** 43 | * Ensure 44 | * 45 | * @mrbgem mruby-error 46 | */ 47 | MRB_API mrb_value mrb_ensure(mrb_state *mrb, mrb_func_t body, mrb_value b_data, 48 | mrb_func_t ensure, mrb_value e_data); 49 | 50 | /** 51 | * Rescue 52 | * 53 | * @mrbgem mruby-error 54 | */ 55 | MRB_API mrb_value mrb_rescue(mrb_state *mrb, mrb_func_t body, mrb_value b_data, 56 | mrb_func_t rescue, mrb_value r_data); 57 | 58 | /** 59 | * Rescue exception 60 | * 61 | * @mrbgem mruby-error 62 | */ 63 | MRB_API mrb_value mrb_rescue_exceptions(mrb_state *mrb, mrb_func_t body, mrb_value b_data, 64 | mrb_func_t rescue, mrb_value r_data, 65 | mrb_int len, struct RClass **classes); 66 | 67 | MRB_END_DECL 68 | 69 | #endif /* MRUBY_ERROR_H */ 70 | -------------------------------------------------------------------------------- /mrbgems/default.gembox: -------------------------------------------------------------------------------- 1 | MRuby::GemBox.new do |conf| 2 | # Use standard Kernel#sprintf method 3 | conf.gem :core => "mruby-sprintf" 4 | 5 | # Use standard print/puts/p 6 | conf.gem :core => "mruby-print" 7 | 8 | # Use standard Math module 9 | conf.gem :core => "mruby-math" 10 | 11 | # Use standard Time class 12 | conf.gem :core => "mruby-time" 13 | 14 | # Use standard Struct class 15 | conf.gem :core => "mruby-struct" 16 | 17 | # Use Enumerable module extension 18 | conf.gem :core => "mruby-enum-ext" 19 | 20 | # Use String class extension 21 | conf.gem :core => "mruby-string-ext" 22 | 23 | # Use Numeric class extension 24 | conf.gem :core => "mruby-numeric-ext" 25 | 26 | # Use Array class extension 27 | conf.gem :core => "mruby-array-ext" 28 | 29 | # Use Hash class extension 30 | conf.gem :core => "mruby-hash-ext" 31 | 32 | # Use Range class extension 33 | conf.gem :core => "mruby-range-ext" 34 | 35 | # Use Proc class extension 36 | conf.gem :core => "mruby-proc-ext" 37 | 38 | # Use Symbol class extension 39 | conf.gem :core => "mruby-symbol-ext" 40 | 41 | # Use Random class 42 | conf.gem :core => "mruby-random" 43 | 44 | # Use Object class extension 45 | conf.gem :core => "mruby-object-ext" 46 | 47 | # Use ObjectSpace class 48 | conf.gem :core => "mruby-objectspace" 49 | 50 | # Use Fiber class 51 | conf.gem :core => "mruby-fiber" 52 | 53 | # Use Enumerator class (require mruby-fiber) 54 | conf.gem :core => "mruby-enumerator" 55 | 56 | # Use Enumerator::Lazy class (require mruby-enumerator) 57 | conf.gem :core => "mruby-enum-lazy" 58 | 59 | # Use toplevel object (main) methods extension 60 | conf.gem :core => "mruby-toplevel-ext" 61 | 62 | # Generate mirb command 63 | conf.gem :core => "mruby-bin-mirb" 64 | 65 | # Generate mruby command 66 | conf.gem :core => "mruby-bin-mruby" 67 | 68 | # Generate mruby-strip command 69 | conf.gem :core => "mruby-bin-strip" 70 | 71 | # Use Kernel module extension 72 | conf.gem :core => "mruby-kernel-ext" 73 | 74 | # Use class/module extension 75 | conf.gem :core => "mruby-class-ext" 76 | 77 | # Use mruby-compiler to build other mrbgems 78 | conf.gem :core => "mruby-compiler" 79 | end 80 | -------------------------------------------------------------------------------- /include/mruby/gc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** mruby/gc.h - garbage collector for mruby 3 | ** 4 | ** See Copyright Notice in mruby.h 5 | */ 6 | 7 | #ifndef MRUBY_GC_H 8 | #define MRUBY_GC_H 9 | 10 | #include "common.h" 11 | 12 | /** 13 | * Uncommon memory management stuffs. 14 | */ 15 | MRB_BEGIN_DECL 16 | 17 | 18 | struct mrb_state; 19 | 20 | typedef void (mrb_each_object_callback)(struct mrb_state *mrb, struct RBasic *obj, void *data); 21 | void mrb_objspace_each_objects(struct mrb_state *mrb, mrb_each_object_callback *callback, void *data); 22 | MRB_API void mrb_free_context(struct mrb_state *mrb, struct mrb_context *c); 23 | 24 | #ifndef MRB_GC_ARENA_SIZE 25 | #define MRB_GC_ARENA_SIZE 100 26 | #endif 27 | 28 | typedef enum { 29 | MRB_GC_STATE_ROOT = 0, 30 | MRB_GC_STATE_MARK, 31 | MRB_GC_STATE_SWEEP 32 | } mrb_gc_state; 33 | 34 | typedef struct mrb_heap_page { 35 | struct RBasic *freelist; 36 | struct mrb_heap_page *prev; 37 | struct mrb_heap_page *next; 38 | struct mrb_heap_page *free_next; 39 | struct mrb_heap_page *free_prev; 40 | mrb_bool old:1; 41 | void *objects[]; 42 | } mrb_heap_page; 43 | 44 | typedef struct mrb_gc { 45 | mrb_heap_page *heaps; /* heaps for GC */ 46 | mrb_heap_page *sweeps; 47 | mrb_heap_page *free_heaps; 48 | size_t live; /* count of live objects */ 49 | #ifdef MRB_GC_FIXED_ARENA 50 | struct RBasic *arena[MRB_GC_ARENA_SIZE]; /* GC protection array */ 51 | #else 52 | struct RBasic **arena; /* GC protection array */ 53 | int arena_capa; 54 | #endif 55 | int arena_idx; 56 | 57 | mrb_gc_state state; /* state of gc */ 58 | int current_white_part; /* make white object by white_part */ 59 | struct RBasic *gray_list; /* list of gray objects to be traversed incrementally */ 60 | struct RBasic *atomic_gray_list; /* list of objects to be traversed atomically */ 61 | size_t live_after_mark; 62 | size_t threshold; 63 | int interval_ratio; 64 | int step_ratio; 65 | mrb_bool disabled :1; 66 | mrb_bool full :1; 67 | mrb_bool generational :1; 68 | mrb_bool out_of_memory :1; 69 | size_t majorgc_old_threshold; 70 | } mrb_gc; 71 | 72 | MRB_API mrb_bool 73 | mrb_object_dead_p(struct mrb_state *mrb, struct RBasic *object); 74 | 75 | MRB_END_DECL 76 | 77 | #endif /* MRUBY_GC_H */ 78 | -------------------------------------------------------------------------------- /mrbgems/mruby-compiler/core/node.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** node.h - nodes of abstract syntax tree 3 | ** 4 | ** See Copyright Notice in mruby.h 5 | */ 6 | 7 | #ifndef MRUBY_COMPILER_NODE_H 8 | #define MRUBY_COMPILER_NODE_H 9 | 10 | enum node_type { 11 | NODE_METHOD, 12 | NODE_FBODY, 13 | NODE_CFUNC, 14 | NODE_SCOPE, 15 | NODE_BLOCK, 16 | NODE_IF, 17 | NODE_CASE, 18 | NODE_WHEN, 19 | NODE_OPT_N, 20 | NODE_WHILE, 21 | NODE_UNTIL, 22 | NODE_ITER, 23 | NODE_FOR, 24 | NODE_BREAK, 25 | NODE_NEXT, 26 | NODE_REDO, 27 | NODE_RETRY, 28 | NODE_BEGIN, 29 | NODE_RESCUE, 30 | NODE_ENSURE, 31 | NODE_AND, 32 | NODE_OR, 33 | NODE_NOT, 34 | NODE_MASGN, 35 | NODE_ASGN, 36 | NODE_CDECL, 37 | NODE_CVASGN, 38 | NODE_CVDECL, 39 | NODE_OP_ASGN, 40 | NODE_CALL, 41 | NODE_SCALL, 42 | NODE_FCALL, 43 | NODE_VCALL, 44 | NODE_SUPER, 45 | NODE_ZSUPER, 46 | NODE_ARRAY, 47 | NODE_ZARRAY, 48 | NODE_HASH, 49 | NODE_RETURN, 50 | NODE_YIELD, 51 | NODE_LVAR, 52 | NODE_DVAR, 53 | NODE_GVAR, 54 | NODE_IVAR, 55 | NODE_CONST, 56 | NODE_CVAR, 57 | NODE_NTH_REF, 58 | NODE_BACK_REF, 59 | NODE_MATCH, 60 | NODE_MATCH2, 61 | NODE_MATCH3, 62 | NODE_INT, 63 | NODE_FLOAT, 64 | NODE_NEGATE, 65 | NODE_LAMBDA, 66 | NODE_SYM, 67 | NODE_STR, 68 | NODE_DSTR, 69 | NODE_XSTR, 70 | NODE_DXSTR, 71 | NODE_REGX, 72 | NODE_DREGX, 73 | NODE_DREGX_ONCE, 74 | NODE_LIST, 75 | NODE_ARG, 76 | NODE_ARGSCAT, 77 | NODE_ARGSPUSH, 78 | NODE_SPLAT, 79 | NODE_TO_ARY, 80 | NODE_SVALUE, 81 | NODE_BLOCK_ARG, 82 | NODE_DEF, 83 | NODE_SDEF, 84 | NODE_ALIAS, 85 | NODE_UNDEF, 86 | NODE_CLASS, 87 | NODE_MODULE, 88 | NODE_SCLASS, 89 | NODE_COLON2, 90 | NODE_COLON3, 91 | NODE_CREF, 92 | NODE_DOT2, 93 | NODE_DOT3, 94 | NODE_FLIP2, 95 | NODE_FLIP3, 96 | NODE_ATTRSET, 97 | NODE_SELF, 98 | NODE_NIL, 99 | NODE_TRUE, 100 | NODE_FALSE, 101 | NODE_DEFINED, 102 | NODE_NEWLINE, 103 | NODE_POSTEXE, 104 | NODE_ALLOCA, 105 | NODE_DMETHOD, 106 | NODE_BMETHOD, 107 | NODE_MEMO, 108 | NODE_IFUNC, 109 | NODE_DSYM, 110 | NODE_ATTRASGN, 111 | NODE_HEREDOC, 112 | NODE_LITERAL_DELIM, 113 | NODE_WORDS, 114 | NODE_SYMBOLS, 115 | NODE_LAST 116 | }; 117 | 118 | #endif /* MRUBY_COMPILER_NODE_H */ 119 | -------------------------------------------------------------------------------- /mrbgems/mruby-random/test/random.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # Random Test 3 | 4 | assert("Random#srand") do 5 | r1 = Random.new(123) 6 | r2 = Random.new(123) 7 | r1.rand == r2.rand 8 | end 9 | 10 | assert("Kernel::srand") do 11 | srand(234) 12 | r1 = rand 13 | srand(234) 14 | r2 = rand 15 | r1 == r2 16 | end 17 | 18 | assert("Random::srand") do 19 | Random.srand(345) 20 | r1 = rand 21 | srand(345) 22 | r2 = Random.rand 23 | r1 == r2 24 | end 25 | 26 | assert("fixnum") do 27 | rand(3).class == Fixnum 28 | end 29 | 30 | assert("float") do 31 | rand.class == Float 32 | end 33 | 34 | assert("Array#shuffle") do 35 | ary = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 36 | shuffled = ary.shuffle 37 | 38 | ary == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and shuffled != ary and 10.times { |x| ary.include? x } 39 | end 40 | 41 | assert('Array#shuffle!') do 42 | ary = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 43 | ary.shuffle! 44 | 45 | ary != [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and 10.times { |x| ary.include? x } 46 | end 47 | 48 | assert("Array#shuffle(random)") do 49 | assert_raise(TypeError) do 50 | # this will cause an exception due to the wrong argument 51 | [1, 2].shuffle "Not a Random instance" 52 | end 53 | 54 | # verify that the same seed causes the same results 55 | ary1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 56 | shuffle1 = ary1.shuffle Random.new 345 57 | ary2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 58 | shuffle2 = ary2.shuffle Random.new 345 59 | 60 | ary1 != shuffle1 and 10.times { |x| shuffle1.include? x } and shuffle1 == shuffle2 61 | end 62 | 63 | assert('Array#shuffle!(random)') do 64 | assert_raise(TypeError) do 65 | # this will cause an exception due to the wrong argument 66 | [1, 2].shuffle! "Not a Random instance" 67 | end 68 | 69 | # verify that the same seed causes the same results 70 | ary1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 71 | ary1.shuffle! Random.new 345 72 | ary2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 73 | ary2.shuffle! Random.new 345 74 | 75 | ary1 != [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and 10.times { |x| ary1.include? x } and ary1 == ary2 76 | end 77 | 78 | assert('Array#sample checks input length after reading arguments') do 79 | $ary = [1, 2, 3] 80 | class ArrayChange 81 | def to_i 82 | $ary << 4 83 | 4 84 | end 85 | end 86 | 87 | assert_equal [1, 2, 3, 4], $ary.sample(ArrayChange.new).sort 88 | end 89 | -------------------------------------------------------------------------------- /tasks/benchmark.rake: -------------------------------------------------------------------------------- 1 | module MRuby 2 | BENCHMARK_REPEAT = 4 3 | end 4 | 5 | $dat_files = [] 6 | 7 | def bm_files 8 | Dir.glob("#{MRUBY_ROOT}/benchmark/bm_*.rb") 9 | end 10 | 11 | def build_config_name 12 | if ENV['MRUBY_CONFIG'] 13 | File.basename(ENV['MRUBY_CONFIG'], '.rb').gsub('build_config_', '') 14 | else 15 | "build" 16 | end 17 | end 18 | 19 | def plot_file 20 | File.join(MRUBY_ROOT, 'benchmark', "#{build_config_name}.png") 21 | end 22 | 23 | def plot 24 | opts_file = "#{MRUBY_ROOT}/benchmark/plot.gpl" 25 | opts = File.read(opts_file).each_line.to_a.map(&:strip).join(';') 26 | 27 | dat_files = $dat_files.group_by {|f| File.dirname(f).split(File::SEPARATOR)[-1]} 28 | 29 | opts += ";set output '#{plot_file}'" 30 | 31 | opts += ';plot ' 32 | 33 | opts += dat_files.keys.map do |data_file| 34 | %Q['-' u 2:3:4:xtic(1) w hist title columnheader(1)] 35 | end.join(',') 36 | opts += ';' 37 | 38 | cmd = %Q{gnuplot -p -e "#{opts}"} 39 | 40 | IO.popen(cmd, 'w') do |p| 41 | dat_files.each do |target_name, bm_files| 42 | p.puts target_name.gsub('_', '-') 43 | bm_files.each do |bm_file| 44 | p.write File.read(bm_file) 45 | end 46 | p.puts "e" 47 | end 48 | end 49 | end 50 | 51 | 52 | MRuby.each_target do |target| 53 | next if target.name == 'host' 54 | mruby_bin = "#{target.build_dir}/bin/mruby" 55 | 56 | bm_files.each do |bm_file| 57 | bm_name = File.basename bm_file, ".rb" 58 | 59 | dat_dir = File.join('benchmark', build_config_name, target.name) 60 | dat_file = File.join(dat_dir, "#{bm_name}.dat") 61 | $dat_files << dat_file 62 | 63 | directory dat_dir 64 | 65 | file dat_file => [bm_file, dat_dir, mruby_bin] do |task| 66 | print bm_name 67 | puts "..." 68 | 69 | data = (0...MRuby::BENCHMARK_REPEAT).map do |n| 70 | str = %x{(time -f "%e %S %U" #{mruby_bin} #{bm_file}) 2>&1 >/dev/null} 71 | str.split(' ').map(&:to_f) 72 | end 73 | 74 | File.open(task.name, "w") do |f| 75 | data = data.map {|_,r,s| (r + s) / 2.0} 76 | min = data.min 77 | max = data.max 78 | avg = data.inject(&:+) / data.size 79 | f.puts "#{bm_name.gsub('_', '-')} #{avg} #{min} #{max}" 80 | end 81 | end 82 | end 83 | end 84 | 85 | file plot_file => $dat_files do 86 | plot 87 | end 88 | 89 | task :benchmark => plot_file do 90 | plot 91 | end 92 | -------------------------------------------------------------------------------- /tasks/toolchains/gcc.rake: -------------------------------------------------------------------------------- 1 | MRuby::Toolchain.new(:gcc) do |conf, _params| 2 | [conf.cc, conf.objc, conf.asm].each do |cc| 3 | cc.command = ENV['CC'] || 'gcc' 4 | cc.flags = [ENV['CFLAGS'] || %w(-g -std=gnu99 -O3 -Wall -Werror-implicit-function-declaration -Wdeclaration-after-statement -Wwrite-strings)] 5 | cc.defines = %w(DISABLE_GEMS) 6 | cc.option_include_path = '-I%s' 7 | cc.option_define = '-D%s' 8 | cc.compile_options = '%{flags} -MMD -o %{outfile} -c %{infile}' 9 | cc.cxx_compile_flag = '-x c++ -std=c++03' 10 | end 11 | 12 | [conf.cxx].each do |cxx| 13 | cxx.command = ENV['CXX'] || 'g++' 14 | cxx.flags = [ENV['CXXFLAGS'] || ENV['CFLAGS'] || %w(-g -O3 -Wall -Werror-implicit-function-declaration)] 15 | cxx.defines = %w(DISABLE_GEMS) 16 | cxx.option_include_path = '-I%s' 17 | cxx.option_define = '-D%s' 18 | cxx.compile_options = '%{flags} -MMD -o %{outfile} -c %{infile}' 19 | cxx.cxx_compile_flag = '-x c++ -std=c++03' 20 | end 21 | 22 | conf.linker do |linker| 23 | linker.command = ENV['LD'] || 'gcc' 24 | linker.flags = [ENV['LDFLAGS'] || %w()] 25 | linker.libraries = %w(m) 26 | linker.library_paths = [] 27 | linker.option_library = '-l%s' 28 | linker.option_library_path = '-L%s' 29 | linker.link_options = '%{flags} -o %{outfile} %{objs} %{flags_before_libraries} %{libs} %{flags_after_libraries}' 30 | end 31 | 32 | [[conf.cc, 'c'], [conf.cxx, 'c++']].each do |cc, lang| 33 | cc.instance_variable_set :@header_search_language, lang 34 | def cc.header_search_paths 35 | if @header_search_command != command 36 | result = `echo | #{build.filename command} -x#{@header_search_language} -Wp,-v - -fsyntax-only 2>&1` 37 | result = `echo | #{command} -x#{@header_search_language} -Wp,-v - -fsyntax-only 2>&1` if $?.exitstatus != 0 38 | return include_paths if $?.exitstatus != 0 39 | 40 | @frameworks = [] 41 | @header_search_paths = result.lines.map { |v| 42 | framework = v.match(/^ (.*)(?: \(framework directory\))$/) 43 | if framework 44 | @frameworks << framework[1] 45 | next nil 46 | end 47 | 48 | v.match(/^ (.*)$/) 49 | }.compact.map { |v| v[1] }.select { |v| File.directory? v } 50 | @header_search_paths += include_paths 51 | @header_search_command = command 52 | end 53 | @header_search_paths 54 | end 55 | end 56 | end 57 | -------------------------------------------------------------------------------- /examples/targets/build_config_RX630.rb: -------------------------------------------------------------------------------- 1 | MRuby::Build.new do |conf| 2 | 3 | # Gets set by the VS command prompts. 4 | if ENV['VisualStudioVersion'] || ENV['VSINSTALLDIR'] 5 | toolchain :visualcpp 6 | else 7 | toolchain :gcc 8 | end 9 | 10 | enable_debug 11 | 12 | # include the default GEMs 13 | conf.gembox 'default' 14 | 15 | end 16 | 17 | # Cross Compiling configuration for RX630 18 | # http://gadget.renesas.com/ 19 | # 20 | # Requires gnurx_v14.03 21 | MRuby::CrossBuild.new("RX630") do |conf| 22 | toolchain :gcc 23 | 24 | # Linux 25 | BIN_PATH = "/usr/share/gnurx_v14.03_elf-1/bin" 26 | 27 | conf.cc do |cc| 28 | cc.command = "#{BIN_PATH}/rx-elf-gcc" 29 | cc.flags = "-Wall -g -O2 -flto -mcpu=rx600 -m64bit-doubles" 30 | cc.compile_options = "%{flags} -o %{outfile} -c %{infile}" 31 | 32 | #configuration for low memory environment 33 | cc.defines << %w(MRB_USE_FLOAT) 34 | cc.defines << %w(MRB_HEAP_PAGE_SIZE=64) 35 | cc.defines << %w(MRB_USE_IV_SEGLIST) 36 | cc.defines << %w(KHASH_DEFAULT_SIZE=8) 37 | cc.defines << %w(MRB_STR_BUF_MIN_SIZE=20) 38 | cc.defines << %w(MRB_GC_STRESS) 39 | cc.defines << %w(MRB_DISABLE_STDIO) #if you dont need stdio. 40 | #cc.defines << %w(POOL_PAGE_SIZE=1000) #effective only for use with mruby-eval 41 | end 42 | 43 | conf.cxx do |cxx| 44 | cxx.command = conf.cc.command.dup 45 | cxx.include_paths = conf.cc.include_paths.dup 46 | cxx.flags = conf.cc.flags.dup 47 | cxx.defines = conf.cc.defines.dup 48 | cxx.compile_options = conf.cc.compile_options.dup 49 | end 50 | 51 | conf.linker do |linker| 52 | linker.command="#{BIN_PATH}/rx-elf-ld" 53 | end 54 | 55 | conf.archiver do |archiver| 56 | archiver.command = "#{BIN_PATH}/rx-elf-ar" 57 | archiver.archive_options = 'rcs %{outfile} %{objs}' 58 | end 59 | 60 | #no executables 61 | conf.bins = [] 62 | 63 | #do not build executable test 64 | conf.build_mrbtest_lib_only 65 | 66 | #disable C++ exception 67 | conf.disable_cxx_exception 68 | 69 | #gems from core 70 | conf.gem :core => "mruby-sprintf" 71 | conf.gem :core => "mruby-print" 72 | conf.gem :core => "mruby-math" 73 | conf.gem :core => "mruby-enum-ext" 74 | conf.gem :core => "mruby-numeric-ext" 75 | 76 | #light-weight regular expression 77 | #conf.gem :github => "masamitsu-murase/mruby-hs-regexp", :branch => "master" 78 | 79 | #Arduino API 80 | #conf.gem :github =>"kyab/mruby-arduino", :branch => "master" 81 | 82 | end 83 | -------------------------------------------------------------------------------- /tasks/toolchains/visualcpp.rake: -------------------------------------------------------------------------------- 1 | MRuby::Toolchain.new(:visualcpp) do |conf, _params| 2 | conf.cc do |cc| 3 | cc.command = ENV['CC'] || 'cl.exe' 4 | # C4013: implicit function declaration 5 | cc.flags = [ENV['CFLAGS'] || %w(/c /nologo /W3 /we4013 /Zi /MD /O2 /D_CRT_SECURE_NO_WARNINGS)] 6 | cc.defines = %w(DISABLE_GEMS MRB_STACK_EXTEND_DOUBLING) 7 | cc.option_include_path = '/I%s' 8 | cc.option_define = '/D%s' 9 | cc.compile_options = "%{flags} /Fo%{outfile} %{infile}" 10 | cc.cxx_compile_flag = '/TP' 11 | end 12 | 13 | conf.cxx do |cxx| 14 | cxx.command = ENV['CXX'] || 'cl.exe' 15 | cxx.flags = [ENV['CXXFLAGS'] || ENV['CFLAGS'] || %w(/c /nologo /W3 /Zi /MD /O2 /EHs /D_CRT_SECURE_NO_WARNINGS)] 16 | cxx.defines = %w(DISABLE_GEMS MRB_STACK_EXTEND_DOUBLING) 17 | cxx.option_include_path = '/I%s' 18 | cxx.option_define = '/D%s' 19 | cxx.compile_options = "%{flags} /Fo%{outfile} %{infile}" 20 | cxx.cxx_compile_flag = '/TP' 21 | end 22 | 23 | conf.linker do |linker| 24 | linker.command = ENV['LD'] || 'link.exe' 25 | linker.flags = [ENV['LDFLAGS'] || %w(/NOLOGO /DEBUG /INCREMENTAL:NO /OPT:ICF /OPT:REF)] 26 | linker.libraries = %w() 27 | linker.library_paths = %w() 28 | linker.option_library = '%s.lib' 29 | linker.option_library_path = '/LIBPATH:%s' 30 | linker.link_options = "%{flags} /OUT:%{outfile} %{objs} %{flags_before_libraries} %{libs} %{flags_after_libraries}" 31 | end 32 | 33 | conf.archiver do |archiver| 34 | archiver.command = ENV['AR'] || 'lib.exe' 35 | archiver.archive_options = '/nologo /OUT:%{outfile} %{objs}' 36 | end 37 | 38 | conf.yacc do |yacc| 39 | yacc.command = ENV['YACC'] || 'bison.exe' 40 | yacc.compile_options = '-o %{outfile} %{infile}' 41 | end 42 | 43 | conf.gperf do |gperf| 44 | gperf.command = 'gperf.exe' 45 | gperf.compile_options = '-L ANSI-C -C -p -j1 -i 1 -g -o -t -N mrb_reserved_word -k"1,3,$" %{infile} > %{outfile}' 46 | end 47 | 48 | conf.exts do |exts| 49 | exts.object = '.obj' 50 | exts.executable = '.exe' 51 | exts.library = '.lib' 52 | end 53 | 54 | conf.file_separator = '\\' 55 | 56 | if require 'open3' 57 | Open3.popen3 conf.cc.command do |_, _, e, _| 58 | if /Version (\d{2})\.\d{2}\.\d{5}/ =~ e.gets && $1.to_i <= 17 59 | m = "# VS2010/2012 support will be dropped after the next release! #" 60 | h = "#" * m.length 61 | puts h, m, h 62 | end 63 | end 64 | end 65 | 66 | end 67 | -------------------------------------------------------------------------------- /include/mruby/data.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** mruby/data.h - Data class 3 | ** 4 | ** See Copyright Notice in mruby.h 5 | */ 6 | 7 | #ifndef MRUBY_DATA_H 8 | #define MRUBY_DATA_H 9 | 10 | #include "common.h" 11 | 12 | /** 13 | * Custom C wrapped data. 14 | * 15 | * Defining Ruby wrappers around native objects. 16 | */ 17 | MRB_BEGIN_DECL 18 | 19 | /** 20 | * Custom data type description. 21 | */ 22 | typedef struct mrb_data_type { 23 | /** data type name */ 24 | const char *struct_name; 25 | 26 | /** data type release function pointer */ 27 | void (*dfree)(mrb_state *mrb, void*); 28 | } mrb_data_type; 29 | 30 | struct RData { 31 | MRB_OBJECT_HEADER; 32 | struct iv_tbl *iv; 33 | const mrb_data_type *type; 34 | void *data; 35 | }; 36 | 37 | MRB_API struct RData *mrb_data_object_alloc(mrb_state *mrb, struct RClass* klass, void *datap, const mrb_data_type *type); 38 | 39 | #define Data_Wrap_Struct(mrb,klass,type,ptr)\ 40 | mrb_data_object_alloc(mrb,klass,ptr,type) 41 | 42 | #define Data_Make_Struct(mrb,klass,strct,type,sval,data) do { \ 43 | sval = mrb_malloc(mrb, sizeof(strct)); \ 44 | { static const strct zero = { 0 }; *sval = zero; };\ 45 | data = Data_Wrap_Struct(mrb,klass,type,sval);\ 46 | } while (0) 47 | 48 | #define RDATA(obj) ((struct RData *)(mrb_ptr(obj))) 49 | #define DATA_PTR(d) (RDATA(d)->data) 50 | #define DATA_TYPE(d) (RDATA(d)->type) 51 | MRB_API void mrb_data_check_type(mrb_state *mrb, mrb_value, const mrb_data_type*); 52 | MRB_API void *mrb_data_get_ptr(mrb_state *mrb, mrb_value, const mrb_data_type*); 53 | #define DATA_GET_PTR(mrb,obj,dtype,type) (type*)mrb_data_get_ptr(mrb,obj,dtype) 54 | MRB_API void *mrb_data_check_get_ptr(mrb_state *mrb, mrb_value, const mrb_data_type*); 55 | #define DATA_CHECK_GET_PTR(mrb,obj,dtype,type) (type*)mrb_data_check_get_ptr(mrb,obj,dtype) 56 | 57 | /* obsolete functions and macros */ 58 | #define mrb_data_check_and_get(mrb,obj,dtype) mrb_data_get_ptr(mrb,obj,dtype) 59 | #define mrb_get_datatype(mrb,val,type) mrb_data_get_ptr(mrb, val, type) 60 | #define mrb_check_datatype(mrb,val,type) mrb_data_get_ptr(mrb, val, type) 61 | #define Data_Get_Struct(mrb,obj,type,sval) do {\ 62 | *(void**)&sval = mrb_data_get_ptr(mrb, obj, type); \ 63 | } while (0) 64 | 65 | static inline void 66 | mrb_data_init(mrb_value v, void *ptr, const mrb_data_type *type) 67 | { 68 | mrb_assert(mrb_type(v) == MRB_TT_DATA); 69 | DATA_PTR(v) = ptr; 70 | DATA_TYPE(v) = type; 71 | } 72 | 73 | MRB_END_DECL 74 | 75 | #endif /* MRUBY_DATA_H */ 76 | -------------------------------------------------------------------------------- /test/t/range.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # Range ISO Test 3 | 4 | assert('Range', '15.2.14') do 5 | assert_equal Class, Range.class 6 | end 7 | 8 | assert('Range#==', '15.2.14.4.1') do 9 | assert_true (1..10) == (1..10) 10 | assert_false (1..10) == (1..100) 11 | assert_true (1..10) == Range.new(1.0, 10.0) 12 | end 13 | 14 | assert('Range#===', '15.2.14.4.2') do 15 | a = (1..10) 16 | 17 | assert_true a === 5 18 | assert_false a === 20 19 | end 20 | 21 | assert('Range#begin', '15.2.14.4.3') do 22 | assert_equal 1, (1..10).begin 23 | end 24 | 25 | assert('Range#each', '15.2.14.4.4') do 26 | a = (1..3) 27 | b = 0 28 | a.each {|i| b += i} 29 | assert_equal 6, b 30 | end 31 | 32 | assert('Range#end', '15.2.14.4.5') do 33 | assert_equal 10, (1..10).end 34 | end 35 | 36 | assert('Range#exclude_end?', '15.2.14.4.6') do 37 | assert_true (1...10).exclude_end? 38 | assert_false (1..10).exclude_end? 39 | end 40 | 41 | assert('Range#first', '15.2.14.4.7') do 42 | assert_equal 1, (1..10).first 43 | end 44 | 45 | assert('Range#include?', '15.2.14.4.8') do 46 | assert_true (1..10).include?(10) 47 | assert_false (1..10).include?(11) 48 | 49 | assert_true (1...10).include?(9) 50 | assert_false (1...10).include?(10) 51 | end 52 | 53 | assert('Range#initialize', '15.2.14.4.9') do 54 | a = Range.new(1, 10, true) 55 | b = Range.new(1, 10, false) 56 | 57 | assert_equal (1...10), a 58 | assert_true a.exclude_end? 59 | assert_equal (1..10), b 60 | assert_false b.exclude_end? 61 | 62 | assert_raise(NameError) { (0..1).send(:initialize, 1, 3) } 63 | end 64 | 65 | assert('Range#last', '15.2.14.4.10') do 66 | assert_equal 10, (1..10).last 67 | end 68 | 69 | assert('Range#member?', '15.2.14.4.11') do 70 | a = (1..10) 71 | 72 | assert_true a.member?(5) 73 | assert_false a.member?(20) 74 | end 75 | 76 | assert('Range#to_s', '15.2.14.4.12') do 77 | assert_equal "0..1", (0..1).to_s 78 | assert_equal "0...1", (0...1).to_s 79 | assert_equal "a..b", ("a".."b").to_s 80 | assert_equal "a...b", ("a"..."b").to_s 81 | end 82 | 83 | assert('Range#inspect', '15.2.14.4.13') do 84 | assert_equal "0..1", (0..1).inspect 85 | assert_equal "0...1", (0...1).inspect 86 | assert_equal "\"a\"..\"b\"", ("a".."b").inspect 87 | assert_equal "\"a\"...\"b\"", ("a"..."b").inspect 88 | end 89 | 90 | assert('Range#eql?', '15.2.14.4.14') do 91 | assert_true (1..10).eql? (1..10) 92 | assert_false (1..10).eql? (1..100) 93 | assert_false (1..10).eql? (Range.new(1.0, 10.0)) 94 | assert_false (1..10).eql? "1..10" 95 | end 96 | -------------------------------------------------------------------------------- /include/mruby/proc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** mruby/proc.h - Proc class 3 | ** 4 | ** See Copyright Notice in mruby.h 5 | */ 6 | 7 | #ifndef MRUBY_PROC_H 8 | #define MRUBY_PROC_H 9 | 10 | #include "common.h" 11 | #include 12 | 13 | /** 14 | * Proc class 15 | */ 16 | MRB_BEGIN_DECL 17 | 18 | struct REnv { 19 | MRB_OBJECT_HEADER; 20 | mrb_value *stack; 21 | mrb_sym mid; 22 | ptrdiff_t cioff; 23 | }; 24 | 25 | #define MRB_SET_ENV_STACK_LEN(e,len) (e)->flags = (unsigned int)(len) 26 | #define MRB_ENV_STACK_LEN(e) ((mrb_int)(e)->flags) 27 | #define MRB_ENV_UNSHARE_STACK(e) ((e)->cioff = -1) 28 | #define MRB_ENV_STACK_SHARED_P(e) ((e)->cioff >= 0) 29 | 30 | MRB_API void mrb_env_unshare(mrb_state*, struct REnv*); 31 | 32 | struct RProc { 33 | MRB_OBJECT_HEADER; 34 | union { 35 | mrb_irep *irep; 36 | mrb_func_t func; 37 | } body; 38 | struct RClass *target_class; 39 | struct REnv *env; 40 | }; 41 | 42 | /* aspec access */ 43 | #define MRB_ASPEC_REQ(a) (((a) >> 18) & 0x1f) 44 | #define MRB_ASPEC_OPT(a) (((a) >> 13) & 0x1f) 45 | #define MRB_ASPEC_REST(a) (((a) >> 12) & 0x1) 46 | #define MRB_ASPEC_POST(a) (((a) >> 7) & 0x1f) 47 | #define MRB_ASPEC_KEY(a) (((a) >> 2) & 0x1f) 48 | #define MRB_ASPEC_KDICT(a) ((a) & (1<<1)) 49 | #define MRB_ASPEC_BLOCK(a) ((a) & 1) 50 | 51 | #define MRB_PROC_CFUNC 128 52 | #define MRB_PROC_CFUNC_P(p) (((p)->flags & MRB_PROC_CFUNC) != 0) 53 | #define MRB_PROC_STRICT 256 54 | #define MRB_PROC_STRICT_P(p) (((p)->flags & MRB_PROC_STRICT) != 0) 55 | 56 | #define mrb_proc_ptr(v) ((struct RProc*)(mrb_ptr(v))) 57 | 58 | struct RProc *mrb_proc_new(mrb_state*, mrb_irep*); 59 | struct RProc *mrb_closure_new(mrb_state*, mrb_irep*); 60 | MRB_API struct RProc *mrb_proc_new_cfunc(mrb_state*, mrb_func_t); 61 | MRB_API struct RProc *mrb_closure_new_cfunc(mrb_state *mrb, mrb_func_t func, int nlocals); 62 | void mrb_proc_copy(struct RProc *a, struct RProc *b); 63 | 64 | /* implementation of #send method */ 65 | MRB_API mrb_value mrb_f_send(mrb_state *mrb, mrb_value self); 66 | 67 | /* following functions are defined in mruby-proc-ext so please include it when using */ 68 | MRB_API struct RProc *mrb_proc_new_cfunc_with_env(mrb_state*, mrb_func_t, mrb_int, const mrb_value*); 69 | MRB_API mrb_value mrb_proc_cfunc_env_get(mrb_state*, mrb_int); 70 | /* old name */ 71 | #define mrb_cfunc_env_get(mrb, idx) mrb_proc_cfunc_env_get(mrb, idx) 72 | 73 | #include 74 | KHASH_DECLARE(mt, mrb_sym, struct RProc*, TRUE) 75 | 76 | MRB_END_DECL 77 | 78 | #endif /* MRUBY_PROC_H */ 79 | -------------------------------------------------------------------------------- /include/mruby/version.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** mruby/version.h - mruby version definition 3 | ** 4 | ** See Copyright Notice in mruby.h 5 | */ 6 | 7 | #ifndef MRUBY_VERSION_H 8 | #define MRUBY_VERSION_H 9 | 10 | #include "common.h" 11 | 12 | /** 13 | * mruby version definition macros 14 | */ 15 | MRB_BEGIN_DECL 16 | 17 | /* 18 | * A passed in expression. 19 | */ 20 | #define MRB_STRINGIZE0(expr) #expr 21 | 22 | /* 23 | * Passes in an expression to MRB_STRINGIZE0. 24 | */ 25 | #define MRB_STRINGIZE(expr) MRB_STRINGIZE0(expr) 26 | 27 | /* 28 | * The version of Ruby used by mruby. 29 | */ 30 | #define MRUBY_RUBY_VERSION "1.9" 31 | 32 | /* 33 | * Ruby engine. 34 | */ 35 | #define MRUBY_RUBY_ENGINE "mruby" 36 | 37 | /* 38 | * Major release version number. 39 | */ 40 | #define MRUBY_RELEASE_MAJOR 1 41 | 42 | /* 43 | * Minor release version number. 44 | */ 45 | #define MRUBY_RELEASE_MINOR 2 46 | 47 | /* 48 | * Tiny release version number. 49 | */ 50 | #define MRUBY_RELEASE_TEENY 0 51 | 52 | /* 53 | * The mruby version. 54 | */ 55 | #define MRUBY_VERSION MRB_STRINGIZE(MRUBY_RELEASE_MAJOR) "." MRB_STRINGIZE(MRUBY_RELEASE_MINOR) "." MRB_STRINGIZE(MRUBY_RELEASE_TEENY) 56 | 57 | /* 58 | * Release number. 59 | */ 60 | #define MRUBY_RELEASE_NO (MRUBY_RELEASE_MAJOR * 100 * 100 + MRUBY_RELEASE_MINOR * 100 + MRUBY_RELEASE_TEENY) 61 | 62 | /* 63 | * Release year. 64 | */ 65 | #define MRUBY_RELEASE_YEAR 2015 66 | 67 | /* 68 | * Release month. 69 | */ 70 | #define MRUBY_RELEASE_MONTH 11 71 | 72 | /* 73 | * Release day. 74 | */ 75 | #define MRUBY_RELEASE_DAY 17 76 | 77 | /* 78 | * Release date as a string. 79 | */ 80 | #define MRUBY_RELEASE_DATE MRB_STRINGIZE(MRUBY_RELEASE_YEAR) "-" MRB_STRINGIZE(MRUBY_RELEASE_MONTH) "-" MRB_STRINGIZE(MRUBY_RELEASE_DAY) 81 | 82 | /* 83 | * The year mruby was first created. 84 | */ 85 | #define MRUBY_BIRTH_YEAR 2010 86 | 87 | /* 88 | * MRuby's authors. 89 | */ 90 | #define MRUBY_AUTHOR "mruby developers" 91 | 92 | /* 93 | * mruby's version, and release date. 94 | */ 95 | #define MRUBY_DESCRIPTION \ 96 | "mruby " MRUBY_VERSION \ 97 | " (" MRUBY_RELEASE_DATE ") " \ 98 | 99 | /* 100 | * mruby's copyright information. 101 | */ 102 | #define MRUBY_COPYRIGHT \ 103 | "mruby - Copyright (c) " \ 104 | MRB_STRINGIZE(MRUBY_BIRTH_YEAR)"-" \ 105 | MRB_STRINGIZE(MRUBY_RELEASE_YEAR)" " \ 106 | MRUBY_AUTHOR \ 107 | 108 | MRB_END_DECL 109 | 110 | #endif /* MRUBY_VERSION_H */ 111 | -------------------------------------------------------------------------------- /mrbgems/mruby-struct/mrblib/struct.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # Struct 3 | # 4 | # ISO 15.2.18 5 | 6 | if Object.const_defined?(:Struct) 7 | class Struct 8 | 9 | ## 10 | # Calls the given block for each element of +self+ 11 | # and pass the respective element. 12 | # 13 | # ISO 15.2.18.4.4 14 | def each(&block) 15 | self.class.members.each{|field| 16 | block.call(self[field]) 17 | } 18 | self 19 | end 20 | 21 | ## 22 | # Calls the given block for each element of +self+ 23 | # and pass the name and value of the respectiev 24 | # element. 25 | # 26 | # ISO 15.2.18.4.5 27 | def each_pair(&block) 28 | self.class.members.each{|field| 29 | block.call(field.to_sym, self[field]) 30 | } 31 | self 32 | end 33 | 34 | ## 35 | # Calls the given block for each element of +self+ 36 | # and returns an array with all elements of which 37 | # block is not false. 38 | # 39 | # ISO 15.2.18.4.7 40 | def select(&block) 41 | ary = [] 42 | self.class.members.each{|field| 43 | val = self[field] 44 | ary.push(val) if block.call(val) 45 | } 46 | ary 47 | end 48 | 49 | def _inspect 50 | name = self.class.to_s 51 | if name[0] == "#" 52 | str = "#" 61 | end 62 | 63 | ## 64 | # call-seq: 65 | # struct.to_s -> string 66 | # struct.inspect -> string 67 | # 68 | # Describe the contents of this struct in a string. 69 | # 70 | # 15.2.18.4.10(x) 71 | # 72 | def inspect 73 | begin 74 | self._inspect 75 | rescue SystemStackError 76 | "#" 77 | end 78 | end 79 | 80 | ## 81 | # 15.2.18.4.11(x) 82 | # 83 | alias to_s inspect 84 | end 85 | 86 | ## 87 | # call-seq: 88 | # hsh.dig(key,...) -> object 89 | # 90 | # Extracts the nested value specified by the sequence of key 91 | # objects by calling +dig+ at each step, returning +nil+ if any 92 | # intermediate step is +nil+. 93 | # 94 | def dig(idx,*args) 95 | n = self[idx] 96 | if args.size > 0 97 | n&.dig(*args) 98 | else 99 | n 100 | end 101 | end 102 | end 103 | 104 | -------------------------------------------------------------------------------- /test/t/codegen.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # Codegen tests 3 | 4 | assert('peephole optimization does not eliminate move whose result is reused') do 5 | assert_raise LocalJumpError do 6 | def method 7 | yield 8 | end 9 | method(&a &&= 0) 10 | end 11 | end 12 | 13 | assert('empty condition in ternary expression parses correctly') do 14 | assert_equal(() ? 1 : 2, 2) 15 | end 16 | 17 | assert('codegen absorbs arguments to redo and retry if they are the argument of a call') do 18 | assert_nothing_raised do 19 | a=*"1", case nil 20 | when 1 21 | redo | 22 | 1 23 | end 24 | end 25 | 26 | assert_nothing_raised do 27 | a=*"1", case nil 28 | when 1 29 | retry | 30 | 1 31 | end 32 | end 33 | end 34 | 35 | assert('method call with exactly 127 arguments') do 36 | def args_to_ary(*args) 37 | args 38 | end 39 | 40 | assert_equal [0]*127, args_to_ary( 41 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, \ 42 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, \ 43 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, \ 44 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, \ 45 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, \ 46 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 47 | ) 48 | end 49 | 50 | assert('nested empty heredoc') do 51 | _, a = nil, <&1` 5 | assert_equal 1, $?.exitstatus 6 | assert_equal "no files to strip", o.split("\n")[0] 7 | end 8 | 9 | assert('file not found') do 10 | o = `#{cmd('mruby-strip')} not_found.mrb 2>&1` 11 | assert_equal 1, $?.exitstatus 12 | assert_equal "can't open file for reading not_found.mrb\n", o 13 | end 14 | 15 | assert('not irep file') do 16 | t = Tempfile.new('script.rb') 17 | t.write 'p test\n' 18 | t.flush 19 | o = `#{cmd('mruby-strip')} #{t.path} 2>&1` 20 | assert_equal 1, $?.exitstatus 21 | assert_equal "can't read irep file #{t.path}\n", o 22 | end 23 | 24 | assert('success') do 25 | script_file, compiled1, compiled2 = 26 | Tempfile.new('script.rb'), Tempfile.new('c1.mrb'), Tempfile.new('c2.mrb') 27 | script_file.write "p 'test'\n" 28 | script_file.flush 29 | `#{cmd('mrbc')} -g -o #{compiled1.path} #{script_file.path}` 30 | `#{cmd('mrbc')} -g -o #{compiled2.path} #{script_file.path}` 31 | 32 | o = `#{cmd('mruby-strip')} #{compiled1.path}` 33 | assert_equal 0, $?.exitstatus 34 | assert_equal "", o 35 | assert_equal `#{cmd('mruby')} #{script_file.path}`, `#{cmd('mruby')} -b #{compiled1.path}` 36 | 37 | o = `#{cmd('mruby-strip')} #{compiled1.path} #{compiled2.path}` 38 | assert_equal 0, $?.exitstatus 39 | assert_equal "", o 40 | end 41 | 42 | assert('check debug section') do 43 | script_file, with_debug, without_debug = 44 | Tempfile.new('script.rb'), Tempfile.new('c1.mrb'), Tempfile.new('c2.mrb') 45 | script_file.write "p 'test'\n" 46 | script_file.flush 47 | `#{cmd('mrbc')} -o #{without_debug.path} #{script_file.path}` 48 | `#{cmd('mrbc')} -g -o #{with_debug.path} #{script_file.path}` 49 | 50 | assert_true with_debug.size >= without_debug.size 51 | 52 | `#{cmd('mruby-strip')} #{with_debug.path}` 53 | assert_equal without_debug.size, with_debug.size 54 | end 55 | 56 | assert('check lv section') do 57 | script_file, with_lv, without_lv = 58 | Tempfile.new('script.rb'), Tempfile.new('c1.mrb'), Tempfile.new('c2.mrb') 59 | script_file.write < "pbosetti/mruby-hs-regexp", :branch => "master" 13 | 14 | end 15 | 16 | # Define cross build settings 17 | MRuby::CrossBuild.new('core2-32-poky-linux') do |conf| 18 | toolchain :gcc 19 | 20 | # Mac OS X 21 | # 22 | POKY_EDISON_PATH = '/opt/poky-edison/1.7.2' 23 | 24 | POKY_EDISON_SYSROOT = "#{POKY_EDISON_PATH}/sysroots/core2-32-poky-linux" 25 | POKY_EDISON_X86_PATH = "#{POKY_EDISON_PATH}/sysroots/i386-pokysdk-darwin" 26 | POKY_EDISON_BIN_PATH = "#{POKY_EDISON_X86_PATH}/usr/bin/i586-poky-linux" 27 | 28 | 29 | conf.cc do |cc| 30 | cc.command = "#{POKY_EDISON_BIN_PATH}/i586-poky-linux-gcc" 31 | cc.include_paths << ["#{POKY_EDISON_SYSROOT}/usr/include", "#{POKY_EDISON_X86_PATH}/usr/include"] 32 | cc.flags = %w(-m32 -march=core2 -mtune=core2 -msse3 -mfpmath=sse -mstackrealign -fno-omit-frame-pointer) 33 | cc.flags << %w(-O2 -pipe -g -feliminate-unused-debug-types) 34 | cc.flags << "--sysroot=#{POKY_EDISON_SYSROOT}" 35 | cc.compile_options = "%{flags} -o %{outfile} -c %{infile}" 36 | cc.defines = %w(ENABLE_READLINE) 37 | end 38 | 39 | conf.cxx do |cxx| 40 | cxx.command = "#{POKY_EDISON_BIN_PATH}/i586-poky-linux-g++" 41 | cxx.include_paths = conf.cc.include_paths.dup 42 | cxx.include_paths << ["#{POKY_EDISON_SYSROOT}/usr/include/c++/4.9.1"] 43 | cxx.flags = conf.cc.flags.dup 44 | cxx.defines = conf.cc.defines.dup 45 | cxx.compile_options = conf.cc.compile_options.dup 46 | end 47 | 48 | conf.archiver do |archiver| 49 | archiver.command = "#{POKY_EDISON_BIN_PATH}/i586-poky-linux-ar" 50 | archiver.archive_options = 'rcs %{outfile} %{objs}' 51 | end 52 | 53 | conf.linker do |linker| 54 | linker.command = "#{POKY_EDISON_BIN_PATH}/i586-poky-linux-g++" 55 | linker.flags = %w(-m32 -march=i586) 56 | linker.flags << "--sysroot=#{POKY_EDISON_SYSROOT}" 57 | linker.flags << %w(-O1) 58 | linker.libraries = %w(m pthread) 59 | end 60 | 61 | #do not build executable test 62 | conf.build_mrbtest_lib_only 63 | 64 | conf.gembox 'default' 65 | 66 | #lightweight regular expression 67 | conf.gem :github => "pbosetti/mruby-hs-regexp", :branch => "master" 68 | 69 | end 70 | -------------------------------------------------------------------------------- /mrbgems/mruby-object-ext/src/object.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /* 6 | * call-seq: 7 | * nil.to_a -> [] 8 | * 9 | * Always returns an empty array. 10 | */ 11 | 12 | static mrb_value 13 | nil_to_a(mrb_state *mrb, mrb_value obj) 14 | { 15 | return mrb_ary_new(mrb); 16 | } 17 | 18 | /* 19 | * call-seq: 20 | * nil.to_f -> 0.0 21 | * 22 | * Always returns zero. 23 | */ 24 | 25 | static mrb_value 26 | nil_to_f(mrb_state *mrb, mrb_value obj) 27 | { 28 | return mrb_float_value(mrb, 0.0); 29 | } 30 | 31 | /* 32 | * call-seq: 33 | * nil.to_i -> 0 34 | * 35 | * Always returns zero. 36 | */ 37 | 38 | static mrb_value 39 | nil_to_i(mrb_state *mrb, mrb_value obj) 40 | { 41 | return mrb_fixnum_value(0); 42 | } 43 | 44 | /* 45 | * call-seq: 46 | * obj.instance_exec(arg...) {|var...| block } -> obj 47 | * 48 | * Executes the given block within the context of the receiver 49 | * (_obj_). In order to set the context, the variable +self+ is set 50 | * to _obj_ while the code is executing, giving the code access to 51 | * _obj_'s instance variables. Arguments are passed as block parameters. 52 | * 53 | * class KlassWithSecret 54 | * def initialize 55 | * @secret = 99 56 | * end 57 | * end 58 | * k = KlassWithSecret.new 59 | * k.instance_exec(5) {|x| @secret+x } #=> 104 60 | */ 61 | 62 | static mrb_value 63 | mrb_obj_instance_exec(mrb_state *mrb, mrb_value self) 64 | { 65 | mrb_value *argv; 66 | mrb_int argc; 67 | mrb_value blk; 68 | struct RClass *c; 69 | 70 | mrb_get_args(mrb, "*&", &argv, &argc, &blk); 71 | 72 | if (mrb_nil_p(blk)) { 73 | mrb_raise(mrb, E_ARGUMENT_ERROR, "no block given"); 74 | } 75 | 76 | switch (mrb_type(self)) { 77 | case MRB_TT_SYMBOL: 78 | case MRB_TT_FIXNUM: 79 | case MRB_TT_FLOAT: 80 | c = NULL; 81 | break; 82 | default: 83 | c = mrb_class_ptr(mrb_singleton_class(mrb, self)); 84 | break; 85 | } 86 | 87 | return mrb_yield_with_class(mrb, blk, argc, argv, self, c); 88 | } 89 | 90 | void 91 | mrb_mruby_object_ext_gem_init(mrb_state* mrb) 92 | { 93 | struct RClass * n = mrb->nil_class; 94 | 95 | mrb_define_method(mrb, n, "to_a", nil_to_a, MRB_ARGS_NONE()); 96 | mrb_define_method(mrb, n, "to_f", nil_to_f, MRB_ARGS_NONE()); 97 | mrb_define_method(mrb, n, "to_i", nil_to_i, MRB_ARGS_NONE()); 98 | 99 | mrb_define_method(mrb, mrb->object_class, "instance_exec", mrb_obj_instance_exec, MRB_ARGS_ANY() | MRB_ARGS_BLOCK()); 100 | } 101 | 102 | void 103 | mrb_mruby_object_ext_gem_final(mrb_state* mrb) 104 | { 105 | } 106 | -------------------------------------------------------------------------------- /mrbgems/mruby-inline-struct/test/inline.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | static mrb_value 7 | istruct_test_initialize(mrb_state *mrb, mrb_value self) 8 | { 9 | char *string = (char*)mrb_istruct_ptr(self); 10 | mrb_int size = mrb_istruct_size(); 11 | mrb_value object; 12 | mrb_get_args(mrb, "o", &object); 13 | 14 | if (mrb_float_p(object)) 15 | { 16 | snprintf(string, size, "float(%.3f)", mrb_float(object)); 17 | } 18 | else if (mrb_fixnum_p(object)) 19 | { 20 | snprintf(string, size, "fixnum(%" MRB_PRId ")", mrb_fixnum(object)); 21 | } 22 | else if (mrb_string_p(object)) 23 | { 24 | snprintf(string, size, "string(%s)", mrb_string_value_cstr(mrb, &object)); 25 | } 26 | 27 | string[size - 1] = 0; // force NULL at the end 28 | return self; 29 | } 30 | 31 | static mrb_value 32 | istruct_test_to_s(mrb_state *mrb, mrb_value self) 33 | { 34 | return mrb_str_new_cstr(mrb, (const char*)mrb_istruct_ptr(self)); 35 | } 36 | 37 | static mrb_value 38 | istruct_test_length(mrb_state *mrb, mrb_value self) 39 | { 40 | return mrb_fixnum_value(mrb_istruct_size()); 41 | } 42 | 43 | static mrb_value 44 | istruct_test_test_receive(mrb_state *mrb, mrb_value self) 45 | { 46 | mrb_value object; 47 | mrb_get_args(mrb, "o", &object); 48 | if (mrb_obj_class(mrb, object) != mrb_class_get(mrb, "InlineStructTest")) 49 | { 50 | mrb_raisef(mrb, E_TYPE_ERROR, "Expected InlineStructTest"); 51 | } 52 | return mrb_bool_value(((char*)mrb_istruct_ptr(object))[0] == 's'); 53 | } 54 | 55 | static mrb_value 56 | istruct_test_test_receive_direct(mrb_state *mrb, mrb_value self) 57 | { 58 | char *ptr; 59 | mrb_get_args(mrb, "I", &ptr); 60 | return mrb_bool_value(ptr[0] == 's'); 61 | } 62 | 63 | static mrb_value 64 | istruct_test_mutate(mrb_state *mrb, mrb_value self) 65 | { 66 | char *ptr = (char*)mrb_istruct_ptr(self); 67 | memcpy(ptr, "mutate", 6); 68 | return mrb_nil_value(); 69 | } 70 | 71 | void mrb_mruby_inline_struct_gem_test(mrb_state *mrb) 72 | { 73 | struct RClass *cls; 74 | 75 | cls = mrb_define_class(mrb, "InlineStructTest", mrb->object_class); 76 | MRB_SET_INSTANCE_TT(cls, MRB_TT_ISTRUCT); 77 | mrb_define_method(mrb, cls, "initialize", istruct_test_initialize, MRB_ARGS_REQ(1)); 78 | mrb_define_method(mrb, cls, "to_s", istruct_test_to_s, MRB_ARGS_NONE()); 79 | mrb_define_method(mrb, cls, "mutate", istruct_test_mutate, MRB_ARGS_NONE()); 80 | mrb_define_class_method(mrb, cls, "length", istruct_test_length, MRB_ARGS_NONE()); 81 | mrb_define_class_method(mrb, cls, "test_receive", istruct_test_test_receive, MRB_ARGS_REQ(1)); 82 | mrb_define_class_method(mrb, cls, "test_receive_direct", istruct_test_test_receive_direct, MRB_ARGS_REQ(1)); 83 | } 84 | -------------------------------------------------------------------------------- /mrbgems/mruby-error/src/exception.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | MRB_API mrb_value 6 | mrb_protect(mrb_state *mrb, mrb_func_t body, mrb_value data, mrb_bool *state) 7 | { 8 | struct mrb_jmpbuf *prev_jmp = mrb->jmp; 9 | struct mrb_jmpbuf c_jmp; 10 | mrb_value result = mrb_nil_value(); 11 | 12 | if (state) { *state = FALSE; } 13 | 14 | MRB_TRY(&c_jmp) { 15 | mrb->jmp = &c_jmp; 16 | result = body(mrb, data); 17 | mrb->jmp = prev_jmp; 18 | } MRB_CATCH(&c_jmp) { 19 | mrb->jmp = prev_jmp; 20 | result = mrb_obj_value(mrb->exc); 21 | mrb->exc = NULL; 22 | if (state) { *state = TRUE; } 23 | } MRB_END_EXC(&c_jmp); 24 | 25 | mrb_gc_protect(mrb, result); 26 | return result; 27 | } 28 | 29 | MRB_API mrb_value 30 | mrb_ensure(mrb_state *mrb, mrb_func_t body, mrb_value b_data, mrb_func_t ensure, mrb_value e_data) 31 | { 32 | struct mrb_jmpbuf *prev_jmp = mrb->jmp; 33 | struct mrb_jmpbuf c_jmp; 34 | mrb_value result; 35 | 36 | MRB_TRY(&c_jmp) { 37 | mrb->jmp = &c_jmp; 38 | result = body(mrb, b_data); 39 | mrb->jmp = prev_jmp; 40 | } MRB_CATCH(&c_jmp) { 41 | mrb->jmp = prev_jmp; 42 | ensure(mrb, e_data); 43 | MRB_THROW(mrb->jmp); /* rethrow catched exceptions */ 44 | } MRB_END_EXC(&c_jmp); 45 | 46 | ensure(mrb, e_data); 47 | mrb_gc_protect(mrb, result); 48 | return result; 49 | } 50 | 51 | MRB_API mrb_value 52 | mrb_rescue(mrb_state *mrb, mrb_func_t body, mrb_value b_data, 53 | mrb_func_t rescue, mrb_value r_data) 54 | { 55 | return mrb_rescue_exceptions(mrb, body, b_data, rescue, r_data, 1, &mrb->eStandardError_class); 56 | } 57 | 58 | MRB_API mrb_value 59 | mrb_rescue_exceptions(mrb_state *mrb, mrb_func_t body, mrb_value b_data, mrb_func_t rescue, mrb_value r_data, 60 | mrb_int len, struct RClass **classes) 61 | { 62 | struct mrb_jmpbuf *prev_jmp = mrb->jmp; 63 | struct mrb_jmpbuf c_jmp; 64 | mrb_value result; 65 | mrb_bool error_matched = FALSE; 66 | mrb_int i; 67 | 68 | MRB_TRY(&c_jmp) { 69 | mrb->jmp = &c_jmp; 70 | result = body(mrb, b_data); 71 | mrb->jmp = prev_jmp; 72 | } MRB_CATCH(&c_jmp) { 73 | mrb->jmp = prev_jmp; 74 | 75 | for (i = 0; i < len; ++i) { 76 | if (mrb_obj_is_kind_of(mrb, mrb_obj_value(mrb->exc), classes[i])) { 77 | error_matched = TRUE; 78 | break; 79 | } 80 | } 81 | 82 | if (!error_matched) { MRB_THROW(mrb->jmp); } 83 | 84 | mrb->exc = NULL; 85 | result = rescue(mrb, r_data); 86 | } MRB_END_EXC(&c_jmp); 87 | 88 | mrb_gc_protect(mrb, result); 89 | return result; 90 | } 91 | 92 | void 93 | mrb_mruby_error_gem_init(mrb_state *mrb) 94 | { 95 | } 96 | 97 | void 98 | mrb_mruby_error_gem_final(mrb_state *mrb) 99 | { 100 | } 101 | -------------------------------------------------------------------------------- /mrbgems/mruby-eval/test/eval.rb: -------------------------------------------------------------------------------- 1 | assert('Kernel.eval', '15.3.1.2.3') do 2 | assert_equal(10) { Kernel.eval '1 * 10' } 3 | assert_equal('aaa') { Kernel.eval "'a' * 3" } 4 | assert_equal(10) { 5 | a = 10 6 | Kernel.eval "a" 7 | } 8 | assert_equal(20) { 9 | a = 10 10 | Kernel.eval "a = 20" 11 | a 12 | } 13 | assert_equal(15) { 14 | c = 5 15 | lambda { 16 | a = 10 17 | Kernel.eval "c = a + c" 18 | }.call 19 | c 20 | } 21 | assert_equal(5) { 22 | c = 5 23 | lambda { 24 | Kernel.eval 'lambda { c }.call' 25 | }.call 26 | } 27 | assert_equal(15) { 28 | c = 5 29 | lambda { 30 | a = 10 31 | Kernel.eval 'lambda { c = a + c }.call' 32 | }.call 33 | c 34 | } 35 | assert_equal(2) { 36 | a = 10 37 | Kernel.eval 'def f(a); b=a.send(:+, 1); end' 38 | f(1) 39 | } 40 | end 41 | 42 | assert('Kernel#eval', '15.3.1.3.12') do 43 | assert_equal(10) { eval '1 * 10' } 44 | end 45 | 46 | assert('rest arguments of eval') do 47 | assert_raise(ArgumentError) { Kernel.eval('0', 0, 'test', 0) } 48 | assert_equal ['test', 'test.rb', 10] do 49 | Kernel.eval('[\'test\', __FILE__, __LINE__]', nil, 'test.rb', 10) 50 | end 51 | end 52 | 53 | assert 'eval syntax error' do 54 | assert_raise(SyntaxError) do 55 | eval 'p "test' 56 | end 57 | end 58 | 59 | assert('String instance_eval') do 60 | obj = Object.new 61 | obj.instance_variable_set :@test, 'test' 62 | assert_raise(ArgumentError) { obj.instance_eval(0) { } } 63 | assert_raise(ArgumentError) { obj.instance_eval('0', 'test', 0, 'test') } 64 | assert_equal(['test.rb', 10]) { obj.instance_eval('[__FILE__, __LINE__]', 'test.rb', 10)} 65 | assert_equal('test') { obj.instance_eval('@test') } 66 | assert_equal('test') { obj.instance_eval { @test } } 67 | o = Object.new 68 | assert_equal ['', o, o], o.instance_eval("[''].each { |s| break [s, o, self] }") 69 | end 70 | 71 | assert('Kernel.#eval(string) context') do 72 | class TestEvalConstScope 73 | EVAL_CONST_CLASS = 'class' 74 | def const_string 75 | eval 'EVAL_CONST_CLASS' 76 | end 77 | end 78 | obj = TestEvalConstScope.new 79 | assert_raise(NameError) { eval 'EVAL_CONST_CLASS' } 80 | assert_equal('class') { obj.const_string } 81 | end 82 | 83 | assert('Object#instance_eval with begin-rescue-ensure execution order') do 84 | class HellRaiser 85 | def raise_hell 86 | order = [:enter_raise_hell] 87 | begin 88 | order.push :begin 89 | self.instance_eval("raise 'error'") 90 | rescue 91 | order.push :rescue 92 | ensure 93 | order.push :ensure 94 | end 95 | order 96 | end 97 | end 98 | 99 | hell_raiser = HellRaiser.new 100 | assert_equal([:enter_raise_hell, :begin, :rescue, :ensure], hell_raiser.raise_hell) 101 | end 102 | -------------------------------------------------------------------------------- /examples/targets/build_config_chipKITMax32.rb: -------------------------------------------------------------------------------- 1 | MRuby::Build.new do |conf| 2 | 3 | # Gets set by the VS command prompts. 4 | if ENV['VisualStudioVersion'] || ENV['VSINSTALLDIR'] 5 | toolchain :visualcpp 6 | else 7 | toolchain :gcc 8 | end 9 | 10 | enable_debug 11 | 12 | # include the default GEMs 13 | conf.gembox 'default' 14 | 15 | end 16 | 17 | # Cross Compiling configuration for Digilent chipKIT Max32 18 | # http://www.digilentinc.com/Products/Detail.cfm?Prod=CHIPKIT-MAX32 19 | # 20 | # Requires MPIDE (https://github.com/chipKIT32/chipKIT32-MAX) 21 | # 22 | # This configuration is based on @kyab's version 23 | # http://d.hatena.ne.jp/kyab/20130201 24 | MRuby::CrossBuild.new("chipKITMax32") do |conf| 25 | toolchain :gcc 26 | 27 | # Mac OS X 28 | # MPIDE_PATH = '/Applications/Mpide.app/Contents/Resources/Java' 29 | # GNU Linux 30 | MPIDE_PATH = '/opt/mpide-0023-linux-20120903' 31 | 32 | PIC32_PATH = "#{MPIDE_PATH}/hardware/pic32" 33 | 34 | conf.cc do |cc| 35 | cc.command = "#{PIC32_PATH}/compiler/pic32-tools/bin/pic32-gcc" 36 | cc.include_paths << ["#{PIC32_PATH}/cores/pic32", 37 | "#{PIC32_PATH}/variants/Max32", 38 | "#{PIC32_PATH}/libraries"] 39 | cc.flags = %w(-O2 -mno-smart-io -w -ffunction-sections -fdata-sections -g -mdebugger -Wcast-align 40 | -fno-short-double -mprocessor=32MX795F512L -DF_CPU=80000000L -DARDUINO=23 -D_BOARD_MEGA_ 41 | -DMPIDEVER=0x01000202 -DMPIDE=23) 42 | cc.compile_options = "%{flags} -o %{outfile} -c %{infile}" 43 | 44 | #configuration for low memory environment 45 | cc.defines << %w(MRB_HEAP_PAGE_SIZE=64) 46 | cc.defines << %w(MRB_USE_IV_SEGLIST) 47 | cc.defines << %w(KHASH_DEFAULT_SIZE=8) 48 | cc.defines << %w(MRB_STR_BUF_MIN_SIZE=20) 49 | cc.defines << %w(MRB_GC_STRESS) 50 | #cc.defines << %w(MRB_DISABLE_STDIO) #if you dont need stdio. 51 | #cc.defines << %w(POOL_PAGE_SIZE=1000) #effective only for use with mruby-eval 52 | end 53 | 54 | conf.cxx do |cxx| 55 | cxx.command = conf.cc.command.dup 56 | cxx.include_paths = conf.cc.include_paths.dup 57 | cxx.flags = conf.cc.flags.dup 58 | cxx.defines = conf.cc.defines.dup 59 | cxx.compile_options = conf.cc.compile_options.dup 60 | end 61 | 62 | conf.archiver do |archiver| 63 | archiver.command = "#{PIC32_PATH}/compiler/pic32-tools/bin/pic32-ar" 64 | archiver.archive_options = 'rcs %{outfile} %{objs}' 65 | end 66 | 67 | #no executables 68 | conf.bins = [] 69 | 70 | #do not build test executable 71 | conf.build_mrbtest_lib_only 72 | 73 | #disable C++ exception 74 | conf.disable_cxx_exception 75 | 76 | #gems from core 77 | conf.gem :core => "mruby-print" 78 | conf.gem :core => "mruby-math" 79 | conf.gem :core => "mruby-enum-ext" 80 | 81 | #light-weight regular expression 82 | conf.gem :github => "masamitsu-murase/mruby-hs-regexp", :branch => "master" 83 | 84 | #Arduino API 85 | #conf.gem :github =>"kyab/mruby-arduino", :branch => "master" 86 | 87 | end 88 | -------------------------------------------------------------------------------- /include/mruby/class.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** mruby/class.h - Class class 3 | ** 4 | ** See Copyright Notice in mruby.h 5 | */ 6 | 7 | #ifndef MRUBY_CLASS_H 8 | #define MRUBY_CLASS_H 9 | 10 | #include "common.h" 11 | 12 | /** 13 | * Class class 14 | */ 15 | MRB_BEGIN_DECL 16 | 17 | struct RClass { 18 | MRB_OBJECT_HEADER; 19 | struct iv_tbl *iv; 20 | struct kh_mt *mt; 21 | struct RClass *super; 22 | }; 23 | 24 | #define mrb_class_ptr(v) ((struct RClass*)(mrb_ptr(v))) 25 | #define RCLASS_SUPER(v) (((struct RClass*)(mrb_ptr(v)))->super) 26 | #define RCLASS_IV_TBL(v) (((struct RClass*)(mrb_ptr(v)))->iv) 27 | #define RCLASS_M_TBL(v) (((struct RClass*)(mrb_ptr(v)))->mt) 28 | 29 | static inline struct RClass* 30 | mrb_class(mrb_state *mrb, mrb_value v) 31 | { 32 | switch (mrb_type(v)) { 33 | case MRB_TT_FALSE: 34 | if (mrb_fixnum(v)) 35 | return mrb->false_class; 36 | return mrb->nil_class; 37 | case MRB_TT_TRUE: 38 | return mrb->true_class; 39 | case MRB_TT_SYMBOL: 40 | return mrb->symbol_class; 41 | case MRB_TT_FIXNUM: 42 | return mrb->fixnum_class; 43 | case MRB_TT_FLOAT: 44 | return mrb->float_class; 45 | case MRB_TT_CPTR: 46 | return mrb->object_class; 47 | case MRB_TT_ENV: 48 | return NULL; 49 | default: 50 | return mrb_obj_ptr(v)->c; 51 | } 52 | } 53 | 54 | // TODO: figure out where to put user flags 55 | #define MRB_FLAG_IS_FROZEN (1 << 18) 56 | #define MRB_FLAG_IS_PREPENDED (1 << 19) 57 | #define MRB_FLAG_IS_ORIGIN (1 << 20) 58 | #define MRB_CLASS_ORIGIN(c) do {\ 59 | if (c->flags & MRB_FLAG_IS_PREPENDED) {\ 60 | c = c->super;\ 61 | while (!(c->flags & MRB_FLAG_IS_ORIGIN)) {\ 62 | c = c->super;\ 63 | }\ 64 | }\ 65 | } while (0) 66 | #define MRB_INSTANCE_TT_MASK (0xFF) 67 | #define MRB_SET_INSTANCE_TT(c, tt) c->flags = ((c->flags & ~MRB_INSTANCE_TT_MASK) | (char)tt) 68 | #define MRB_INSTANCE_TT(c) (enum mrb_vtype)(c->flags & MRB_INSTANCE_TT_MASK) 69 | 70 | MRB_API struct RClass* mrb_define_class_id(mrb_state*, mrb_sym, struct RClass*); 71 | MRB_API struct RClass* mrb_define_module_id(mrb_state*, mrb_sym); 72 | MRB_API struct RClass *mrb_vm_define_class(mrb_state*, mrb_value, mrb_value, mrb_sym); 73 | MRB_API struct RClass *mrb_vm_define_module(mrb_state*, mrb_value, mrb_sym); 74 | MRB_API void mrb_define_method_raw(mrb_state*, struct RClass*, mrb_sym, struct RProc *); 75 | MRB_API void mrb_define_method_id(mrb_state *mrb, struct RClass *c, mrb_sym mid, mrb_func_t func, mrb_aspec aspec); 76 | MRB_API void mrb_alias_method(mrb_state *mrb, struct RClass *c, mrb_sym a, mrb_sym b); 77 | 78 | MRB_API struct RClass *mrb_class_outer_module(mrb_state*, struct RClass *); 79 | MRB_API struct RProc *mrb_method_search_vm(mrb_state*, struct RClass**, mrb_sym); 80 | MRB_API struct RProc *mrb_method_search(mrb_state*, struct RClass*, mrb_sym); 81 | 82 | MRB_API struct RClass* mrb_class_real(struct RClass* cl); 83 | 84 | void mrb_gc_mark_mt(mrb_state*, struct RClass*); 85 | size_t mrb_gc_mark_mt_size(mrb_state*, struct RClass*); 86 | void mrb_gc_free_mt(mrb_state*, struct RClass*); 87 | 88 | MRB_END_DECL 89 | 90 | #endif /* MRUBY_CLASS_H */ 91 | -------------------------------------------------------------------------------- /mrbgems/mruby-random/src/mt19937ar.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** mt19937ar.h - MT Random functions 3 | ** 4 | ** Copyright (C) 1997 - 2016, Makoto Matsumoto and Takuji Nishimura, 5 | ** All rights reserved. 6 | ** 7 | ** Permission is hereby granted, free of charge, to any person obtaining 8 | ** a copy of this software and associated documentation files (the 9 | ** "Software"), to deal in the Software without restriction, including 10 | ** without limitation the rights to use, copy, modify, merge, publish, 11 | ** distribute, sublicense, and/or sell copies of the Software, and to 12 | ** permit persons to whom the Software is furnished to do so, subject to 13 | ** the following conditions: 14 | ** 15 | ** The above copyright notice and this permission notice shall be 16 | ** included in all copies or substantial portions of the Software. 17 | ** 18 | ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 21 | ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 | ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 | ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | ** 26 | ** [ MIT license: http://www.opensource.org/licenses/mit-license.php ] 27 | ** 28 | ** Any feedback is very welcome. 29 | ** http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html 30 | ** email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space) 31 | ** 32 | ** This version is modified by mruby developers. If you see any problem, 33 | ** contact us first at https://github.com/mruby/mruby/issues 34 | */ 35 | 36 | #define N 624 37 | 38 | typedef struct { 39 | unsigned long mt[N]; 40 | int mti; 41 | union { 42 | unsigned long int_; 43 | double double_; 44 | } gen; 45 | 46 | mrb_int seed; 47 | mrb_bool has_seed : 1; 48 | } mt_state; 49 | 50 | void mrb_random_init_genrand(mt_state *, unsigned long); 51 | unsigned long mrb_random_genrand_int32(mt_state *); 52 | double mrb_random_genrand_real1(mt_state *t); 53 | 54 | /* initializes mt[N] with a seed */ 55 | void init_genrand(unsigned long s); 56 | 57 | /* initialize by an array with array-length */ 58 | /* init_key is the array for initializing keys */ 59 | /* key_length is its length */ 60 | /* slight change for C++, 2004/2/26 */ 61 | void init_by_array(unsigned long init_key[], int key_length); 62 | 63 | /* generates a random number on [0,0xffffffff]-interval */ 64 | unsigned long genrand_int32(void); 65 | 66 | /* generates a random number on [0,0x7fffffff]-interval */ 67 | long genrand_int31(void); 68 | 69 | /* These real versions are due to Isaku Wada, 2002/01/09 added */ 70 | /* generates a random number on [0,1]-real-interval */ 71 | double genrand_real1(void); 72 | 73 | /* generates a random number on [0,1)-real-interval */ 74 | double genrand_real2(void); 75 | 76 | /* generates a random number on (0,1)-real-interval */ 77 | double genrand_real3(void); 78 | 79 | /* generates a random number on [0,1) with 53-bit resolution*/ 80 | double genrand_res53(void); 81 | -------------------------------------------------------------------------------- /mrbgems/mruby-compiler/core/keywords: -------------------------------------------------------------------------------- 1 | %{ 2 | struct kwtable {const char *name; int id[2]; enum mrb_lex_state_enum state;}; 3 | const struct kwtable *mrb_reserved_word(const char *, unsigned int); 4 | static const struct kwtable *reserved_word(const char *, unsigned int); 5 | #define mrb_reserved_word(str, len) reserved_word(str, len) 6 | %} 7 | 8 | struct kwtable; 9 | %% 10 | __ENCODING__, {keyword__ENCODING__, keyword__ENCODING__}, EXPR_END 11 | __FILE__, {keyword__FILE__, keyword__FILE__}, EXPR_END 12 | __LINE__, {keyword__LINE__, keyword__LINE__}, EXPR_END 13 | BEGIN, {keyword_BEGIN, keyword_BEGIN}, EXPR_END 14 | END, {keyword_END, keyword_END}, EXPR_END 15 | alias, {keyword_alias, keyword_alias}, EXPR_FNAME 16 | and, {keyword_and, keyword_and}, EXPR_VALUE 17 | begin, {keyword_begin, keyword_begin}, EXPR_BEG 18 | break, {keyword_break, keyword_break}, EXPR_MID 19 | case, {keyword_case, keyword_case}, EXPR_VALUE 20 | class, {keyword_class, keyword_class}, EXPR_CLASS 21 | def, {keyword_def, keyword_def}, EXPR_FNAME 22 | do, {keyword_do, keyword_do}, EXPR_BEG 23 | else, {keyword_else, keyword_else}, EXPR_BEG 24 | elsif, {keyword_elsif, keyword_elsif}, EXPR_VALUE 25 | end, {keyword_end, keyword_end}, EXPR_END 26 | ensure, {keyword_ensure, keyword_ensure}, EXPR_BEG 27 | false, {keyword_false, keyword_false}, EXPR_END 28 | for, {keyword_for, keyword_for}, EXPR_VALUE 29 | if, {keyword_if, modifier_if}, EXPR_VALUE 30 | in, {keyword_in, keyword_in}, EXPR_VALUE 31 | module, {keyword_module, keyword_module}, EXPR_VALUE 32 | next, {keyword_next, keyword_next}, EXPR_MID 33 | nil, {keyword_nil, keyword_nil}, EXPR_END 34 | not, {keyword_not, keyword_not}, EXPR_ARG 35 | or, {keyword_or, keyword_or}, EXPR_VALUE 36 | redo, {keyword_redo, keyword_redo}, EXPR_END 37 | rescue, {keyword_rescue, modifier_rescue}, EXPR_MID 38 | retry, {keyword_retry, keyword_retry}, EXPR_END 39 | return, {keyword_return, keyword_return}, EXPR_MID 40 | self, {keyword_self, keyword_self}, EXPR_END 41 | super, {keyword_super, keyword_super}, EXPR_ARG 42 | then, {keyword_then, keyword_then}, EXPR_BEG 43 | true, {keyword_true, keyword_true}, EXPR_END 44 | undef, {keyword_undef, keyword_undef}, EXPR_FNAME 45 | unless, {keyword_unless, modifier_unless}, EXPR_VALUE 46 | until, {keyword_until, modifier_until}, EXPR_VALUE 47 | when, {keyword_when, keyword_when}, EXPR_VALUE 48 | while, {keyword_while, modifier_while}, EXPR_VALUE 49 | yield, {keyword_yield, keyword_yield}, EXPR_ARG 50 | %% 51 | -------------------------------------------------------------------------------- /mrbgems/mruby-proc-ext/test/proc.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # Proc(Ext) Test 3 | 4 | assert('Proc#source_location') do 5 | loc = Proc.new {}.source_location 6 | next true if loc.nil? 7 | assert_equal loc[0][-7, 7], 'proc.rb' 8 | assert_equal loc[1], 5 9 | end 10 | 11 | assert('Proc#inspect') do 12 | ins = Proc.new{}.inspect 13 | assert_kind_of String, ins 14 | end 15 | 16 | assert('Proc#lambda?') do 17 | assert_true lambda{}.lambda? 18 | assert_true !Proc.new{}.lambda? 19 | end 20 | 21 | assert('Proc#===') do 22 | proc = Proc.new {|a| a * 2} 23 | assert_equal 20, (proc === 10) 24 | end 25 | 26 | assert('Proc#yield') do 27 | proc = Proc.new {|a| a * 2} 28 | assert_equal 20, proc.yield(10) 29 | end 30 | 31 | assert('Proc#curry') do 32 | b = proc {|x, y, z| (x||0) + (y||0) + (z||0) } 33 | assert_equal 6, b.curry[1][2][3] 34 | assert_equal 6, b.curry[1, 2][3, 4] 35 | assert_equal 6, b.curry(5)[1][2][3][4][5] 36 | assert_equal 6, b.curry(5)[1, 2][3, 4][5] 37 | assert_equal 1, b.curry(1)[1] 38 | 39 | b = lambda {|x, y, z| (x||0) + (y||0) + (z||0) } 40 | assert_equal 6, b.curry[1][2][3] 41 | assert_raise(ArgumentError) { b.curry[1, 2][3, 4] } 42 | assert_raise(ArgumentError) { b.curry(5) } 43 | assert_raise(ArgumentError) { b.curry(1) } 44 | 45 | assert_false(proc{}.curry.lambda?) 46 | assert_true(lambda{}.curry.lambda?) 47 | end 48 | 49 | assert('Proc#parameters') do 50 | assert_equal([], Proc.new {}.parameters) 51 | assert_equal([], Proc.new {||}.parameters) 52 | assert_equal([[:opt, :a]], Proc.new {|a|}.parameters) 53 | assert_equal([[:req, :a]], lambda {|a|}.parameters) 54 | assert_equal([[:opt, :a]], lambda {|a=nil|}.parameters) 55 | assert_equal([[:req, :a]], ->(a){}.parameters) 56 | assert_equal([[:rest, :a]], Proc.new {|*a|}.parameters) 57 | assert_equal([[:opt, :a], [:opt, :b], [:opt, :c], [:opt, :d], [:rest, :e], [:opt, :f], [:opt, :g], [:block, :h]], Proc.new {|a,b,c=:c,d=:d,*e,f,g,&h|}.parameters) 58 | assert_equal([[:req, :a], [:req, :b], [:opt, :c], [:opt, :d], [:rest, :e], [:req, :f], [:req, :g], [:block, :h]], lambda {|a,b,c=:c,d=:d,*e,f,g,&h|}.parameters) 59 | end 60 | 61 | assert('Proc#parameters with uninitialized Proc') do 62 | begin 63 | Proc.alias_method(:original_initialize, :initialize) 64 | Proc.remove_method(:initialize) 65 | assert_equal [], Proc.new{|a, b, c| 1}.parameters 66 | ensure 67 | Proc.alias_method(:initialize, :original_initialize) 68 | Proc.remove_method(:original_initialize) 69 | end 70 | end 71 | 72 | assert('Proc#to_proc') do 73 | proc = Proc.new {} 74 | assert_equal proc, proc.to_proc 75 | end 76 | 77 | assert('Kernel#proc') do 78 | assert_true !proc{|a|}.lambda? 79 | end 80 | 81 | assert('mrb_proc_new_cfunc_with_env') do 82 | ProcExtTest.mrb_proc_new_cfunc_with_env(:test) 83 | ProcExtTest.mrb_proc_new_cfunc_with_env(:mruby) 84 | 85 | t = ProcExtTest.new 86 | 87 | assert_equal :test, t.test 88 | assert_equal :mruby, t.mruby 89 | end 90 | 91 | assert('mrb_cfunc_env_get') do 92 | ProcExtTest.mrb_cfunc_env_get :get_int, [0, 1, 2] 93 | 94 | t = ProcExtTest.new 95 | 96 | assert_raise(TypeError) { t.cfunc_without_env } 97 | 98 | assert_raise(IndexError) { t.get_int(-1) } 99 | assert_raise(IndexError) { t.get_int(3) } 100 | 101 | assert_equal 1, t.get_int(1) 102 | end 103 | -------------------------------------------------------------------------------- /test/t/methods.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # Chapter 13.3 "Methods" ISO Test 3 | 4 | assert('The alias statement', '13.3.6 a) 4)') do 5 | # check aliasing in all possible ways 6 | 7 | def alias_test_method_original; true; end 8 | 9 | alias alias_test_method_a alias_test_method_original 10 | alias :alias_test_method_b :alias_test_method_original 11 | 12 | assert_true(alias_test_method_original) 13 | assert_true(alias_test_method_a) 14 | assert_true(alias_test_method_b) 15 | end 16 | 17 | assert('The alias statement (overwrite original)', '13.3.6 a) 4)') do 18 | # check that an aliased method can be overwritten 19 | # without side effect 20 | 21 | def alias_test_method_original; true; end 22 | 23 | alias alias_test_method_a alias_test_method_original 24 | alias :alias_test_method_b :alias_test_method_original 25 | 26 | assert_true(alias_test_method_original) 27 | 28 | def alias_test_method_original; false; end 29 | 30 | assert_false(alias_test_method_original) 31 | assert_true(alias_test_method_a) 32 | assert_true(alias_test_method_b) 33 | end 34 | 35 | assert('The alias statement', '13.3.6 a) 5)') do 36 | # check that alias is raising NameError if 37 | # non-existing method should be undefined 38 | 39 | assert_raise(NameError) do 40 | alias new_name_a non_existing_method 41 | end 42 | 43 | assert_raise(NameError) do 44 | alias :new_name_b :non_existing_method 45 | end 46 | end 47 | 48 | assert('The undef statement', '13.3.7 a) 4)') do 49 | # check that undef is undefining method 50 | # based on the method name 51 | 52 | def existing_method_a; true; end 53 | def existing_method_b; true; end 54 | def existing_method_c; true; end 55 | def existing_method_d; true; end 56 | def existing_method_e; true; end 57 | def existing_method_f; true; end 58 | 59 | # check that methods are defined 60 | 61 | assert_true(existing_method_a, 'Method should be defined') 62 | assert_true(existing_method_b, 'Method should be defined') 63 | assert_true(existing_method_c, 'Method should be defined') 64 | assert_true(existing_method_d, 'Method should be defined') 65 | assert_true(existing_method_e, 'Method should be defined') 66 | assert_true(existing_method_f, 'Method should be defined') 67 | 68 | # undefine in all possible ways and check that method 69 | # is undefined 70 | 71 | undef existing_method_a 72 | assert_raise(NoMethodError) do 73 | existing_method_a 74 | end 75 | 76 | undef :existing_method_b 77 | assert_raise(NoMethodError) do 78 | existing_method_b 79 | end 80 | 81 | undef existing_method_c, existing_method_d 82 | assert_raise(NoMethodError) do 83 | existing_method_c 84 | end 85 | assert_raise(NoMethodError) do 86 | existing_method_d 87 | end 88 | 89 | undef :existing_method_e, :existing_method_f 90 | assert_raise(NoMethodError) do 91 | existing_method_e 92 | end 93 | assert_raise(NoMethodError) do 94 | existing_method_f 95 | end 96 | end 97 | 98 | assert('The undef statement (method undefined)', '13.3.7 a) 5)') do 99 | # check that undef is raising NameError if 100 | # non-existing method should be undefined 101 | 102 | assert_raise(NameError) do 103 | undef non_existing_method 104 | end 105 | 106 | assert_raise(NameError) do 107 | undef :non_existing_method 108 | end 109 | end 110 | -------------------------------------------------------------------------------- /mrblib/numeric.rb: -------------------------------------------------------------------------------- 1 | ## 2 | # Numeric 3 | # 4 | # ISO 15.2.7 5 | class Numeric 6 | include Comparable 7 | ## 8 | # Returns the receiver simply. 9 | # 10 | # ISO 15.2.7.4.1 11 | def +@ 12 | self 13 | end 14 | 15 | ## 16 | # Returns the receiver's value, negated. 17 | # 18 | # ISO 15.2.7.4.2 19 | def -@ 20 | 0 - self 21 | end 22 | 23 | ## 24 | # Returns the absolute value of the receiver. 25 | # 26 | # ISO 15.2.7.4.3 27 | def abs 28 | if self < 0 29 | -self 30 | else 31 | self 32 | end 33 | end 34 | end 35 | 36 | ## 37 | # Integral 38 | # 39 | # mruby special - module to share methods between Floats and Integers 40 | # to make them compatible 41 | module Integral 42 | ## 43 | # Calls the given block once for each Integer 44 | # from +self+ downto +num+. 45 | # 46 | # ISO 15.2.8.3.15 47 | def downto(num, &block) 48 | return to_enum(:downto, num) unless block_given? 49 | 50 | i = self.to_i 51 | while i >= num 52 | block.call(i) 53 | i -= 1 54 | end 55 | self 56 | end 57 | 58 | ## 59 | # Returns self + 1 60 | # 61 | # ISO 15.2.8.3.19 62 | def next 63 | self + 1 64 | end 65 | # ISO 15.2.8.3.21 66 | alias succ next 67 | 68 | ## 69 | # Calls the given block +self+ times. 70 | # 71 | # ISO 15.2.8.3.22 72 | def times &block 73 | return to_enum :times unless block_given? 74 | 75 | i = 0 76 | while i < self 77 | block.call i 78 | i += 1 79 | end 80 | self 81 | end 82 | 83 | ## 84 | # Calls the given block once for each Integer 85 | # from +self+ upto +num+. 86 | # 87 | # ISO 15.2.8.3.27 88 | def upto(num, &block) 89 | return to_enum(:upto, num) unless block_given? 90 | 91 | i = self.to_i 92 | while i <= num 93 | block.call(i) 94 | i += 1 95 | end 96 | self 97 | end 98 | 99 | ## 100 | # Calls the given block from +self+ to +num+ 101 | # incremented by +step+ (default 1). 102 | # 103 | def step(num, step = 1, &block) 104 | raise ArgumentError, "step can't be 0" if step == 0 105 | return to_enum(:step, num, step) unless block_given? 106 | 107 | i = if num.kind_of? Float then self.to_f else self end 108 | if step > 0 109 | while i <= num 110 | block.call(i) 111 | i += step 112 | end 113 | else 114 | while i >= num 115 | block.call(i) 116 | i += step 117 | end 118 | end 119 | self 120 | end 121 | end 122 | 123 | ## 124 | # Integer 125 | # 126 | # ISO 15.2.8 127 | class Integer 128 | include Integral 129 | ## 130 | # Returns the receiver simply. 131 | # 132 | # ISO 15.2.8.3.14 133 | def ceil 134 | self 135 | end 136 | 137 | ## 138 | # Returns the receiver simply. 139 | # 140 | # ISO 15.2.8.3.17 141 | def floor 142 | self 143 | end 144 | 145 | ## 146 | # Returns the receiver simply. 147 | # 148 | # ISO 15.2.8.3.24 149 | alias round floor 150 | 151 | ## 152 | # Returns the receiver simply. 153 | # 154 | # ISO 15.2.8.3.26 155 | alias truncate floor 156 | end 157 | 158 | ## 159 | # Float 160 | # 161 | # ISO 15.2.9 162 | class Float 163 | # mruby special - since mruby integers may be upgraded to floats, 164 | # floats should be compatible to integers. 165 | include Integral 166 | end 167 | -------------------------------------------------------------------------------- /examples/targets/build_config_ArduinoDue.rb: -------------------------------------------------------------------------------- 1 | MRuby::Build.new do |conf| 2 | 3 | # Gets set by the VS command prompts. 4 | if ENV['VisualStudioVersion'] || ENV['VSINSTALLDIR'] 5 | toolchain :visualcpp 6 | else 7 | toolchain :gcc 8 | end 9 | 10 | enable_debug 11 | 12 | # include the default GEMs 13 | conf.gembox 'default' 14 | 15 | end 16 | 17 | # Cross Compiling configuration for Arduino Due 18 | # http://arduino.cc/en/Main/ArduinoBoardDue 19 | # 20 | # Requires Arduino IDE >= 1.5 21 | MRuby::CrossBuild.new("ArduinoDue") do |conf| 22 | toolchain :gcc 23 | 24 | # Mac OS X, Arduino IDE <= 1.5.6 25 | # ARDUINO_PATH = '/Applications/Arduino.app/Contents/Resources/Java' 26 | # Mac OS X, Arduino IDE >= 1.5.7 27 | # ARDUINO_PATH = '/Applications/Arduino.app/Contents/Java' 28 | # GNU Linux 29 | ARDUINO_PATH = '/opt/arduino' 30 | # Arduino IDE <= 1.5.6 31 | BIN_PATH = "#{ARDUINO_PATH}/hardware/tools/g++_arm_none_eabi/bin" 32 | # Arduino IDE >= 1.5.7 33 | # BIN_PATH = "#{ARDUINO_PATH}/hardware/tools/gcc-arm-none-eabi-4.8.3-2014q1/bin" 34 | SAM_PATH = "#{ARDUINO_PATH}/hardware/arduino/sam" 35 | TARGET_PATH = "#{SAM_PATH}/variants/arduino_due_x" 36 | 37 | conf.cc do |cc| 38 | cc.command = "#{BIN_PATH}/arm-none-eabi-gcc" 39 | cc.include_paths << ["#{SAM_PATH}/system/libsam", "#{SAM_PATH}/system/CMSIS/CMSIS/Include/", 40 | "#{SAM_PATH}/system/CMSIS/Device/ATMEL/", 41 | "#{SAM_PATH}/cores/arduino", "#{SAM_PATH}/libraries","#{TARGET_PATH}"] 42 | cc.flags = %w(-g -Os -w -ffunction-sections -fdata-sections -nostdlib --param max-inline-insns-single=500 43 | -Dprintf=iprintf -mcpu=cortex-m3 -DF_CPU=84000000L -DARDUINO=156 -DARDUINO_SAM_DUE -DARDUINO_ARCH_SAM 44 | -D__SAM3X8E__ -mthumb -DUSB_PID=0x003e -DUSB_VID=0x2341 -DUSBCON -DUSB_MANUFACTURER="Unknown" -DUSB_PRODUCT="Arduino Due") 45 | cc.compile_options = "%{flags} -o %{outfile} -c %{infile}" 46 | 47 | #configuration for low memory environment 48 | cc.defines << %w(MRB_HEAP_PAGE_SIZE=64) 49 | cc.defines << %w(MRB_USE_IV_SEGLIST) 50 | cc.defines << %w(KHASH_DEFAULT_SIZE=8) 51 | cc.defines << %w(MRB_STR_BUF_MIN_SIZE=20) 52 | cc.defines << %w(MRB_GC_STRESS) 53 | #cc.defines << %w(MRB_DISABLE_STDIO) #if you dont need stdio. 54 | #cc.defines << %w(POOL_PAGE_SIZE=1000) #effective only for use with mruby-eval 55 | end 56 | 57 | conf.cxx do |cxx| 58 | cxx.command = conf.cc.command.dup 59 | cxx.include_paths = conf.cc.include_paths.dup 60 | cxx.flags = conf.cc.flags.dup 61 | cxx.flags << %w(-fno-rtti -fno-exceptions) 62 | cxx.defines = conf.cc.defines.dup 63 | cxx.compile_options = conf.cc.compile_options.dup 64 | end 65 | 66 | conf.archiver do |archiver| 67 | archiver.command = "#{BIN_PATH}/arm-none-eabi-ar" 68 | archiver.archive_options = 'rcs %{outfile} %{objs}' 69 | end 70 | 71 | #no executables 72 | conf.bins = [] 73 | 74 | #do not build executable test 75 | conf.build_mrbtest_lib_only 76 | 77 | #disable C++ exception 78 | conf.disable_cxx_exception 79 | 80 | #gems from core 81 | conf.gem :core => "mruby-print" 82 | conf.gem :core => "mruby-math" 83 | conf.gem :core => "mruby-enum-ext" 84 | 85 | #light-weight regular expression 86 | conf.gem :github => "masamitsu-murase/mruby-hs-regexp", :branch => "master" 87 | 88 | #Arduino API 89 | #conf.gem :github =>"kyab/mruby-arduino", :branch => "master" 90 | 91 | end 92 | --------------------------------------------------------------------------------