diff options
122 files changed, 1646 insertions, 1209 deletions
@@ -16,3 +16,4 @@ Original Authors "mruby developers" are: Koji Yoshioka Jun Hiroe Narihiro Nakamura + Yuichi Nishiwaki @@ -2,9 +2,11 @@ This is a preliminary release for internal team review. The URLs and addresses described below are not available yet. The official release will be announced later. - Any suggestion for modification is welcome. + Any suggestions for modification are welcome. Delays in replies are to be expected. Sorry in advance. +[](https://travis-ci.org/mruby/mruby) + ## What's mruby mruby is the lightweight implementation of the Ruby language complying to (part of) @@ -113,4 +115,4 @@ Please ask us if you want to distribute your code under another license. See the [contribution guidelines](https://github.com/mruby/mruby/blob/master/CONTRIBUTING.md) then send a pull request to <http://github.com/mruby/mruby>. We consider you have granted non-exclusive right to your contributed code under MIT license. If you want to be named -as one of mruby developers, include update to the AUTHORS file in your pull request. +as one of mruby developers, please include an update to the AUTHORS file in your pull request. diff --git a/build_config.rb b/build_config.rb index 8367f4ca5..fce424686 100644 --- a/build_config.rb +++ b/build_config.rb @@ -42,25 +42,25 @@ MRuby::Build.new do |conf| # linker.option_library_path = '-L%s' # linker.link_options = "%{flags} -o %{outfile} %{objs} %{libs}" # end - + # Archiver settings # conf.archiver do |archiver| # archiver.command = ENV['AR'] || 'ar' # archiver.archive_options = 'rs %{outfile} %{objs}' # end - + # Parser generator settings # conf.yacc do |yacc| # yacc.command = ENV['YACC'] || 'bison' # yacc.compile_options = '-o %{outfile} %{infile}' # end - + # gperf settings # conf.gperf do |gperf| # gperf.command = 'gperf' # gperf.compile_options = '-L ANSI-C -C -p -j1 -i 1 -g -o -t -N mrb_reserved_word -k"1,3,$" %{infile} > %{outfile}' # end - + # file extensions # conf.exts do |exts| # exts.object = '.o' @@ -75,12 +75,12 @@ end # Define cross build settings # MRuby::CrossBuild.new('32bit') do |conf| # toolchain :gcc -# +# # conf.cc.flags << "-m32" # conf.linker.flags << "-m32" # # conf.build_mrbtest_lib_only -# +# # conf.gem 'examples/mrbgems/c_and_ruby_extension_example' # # conf.test_runner.command = 'env' diff --git a/doc/mrbgems/README.md b/doc/mrbgems/README.md index 1062adc6b..65b794dc0 100644 --- a/doc/mrbgems/README.md +++ b/doc/mrbgems/README.md @@ -26,7 +26,7 @@ A remote GIT repository location for a GEM is also supported: conf.gem :bitbucket => 'mruby/mrbgems-example', :branch => 'master' To pull all gems from remote GIT repository on build, call ```./minirake -p```, -or ```./minirake --pull_gems```. +or ```./minirake --pull-gems```. NOTE: `:bitbucket` option supports only git. Hg is unsupported in this version. @@ -99,7 +99,7 @@ GEM directory. A typical GEM specification could look like this for example: MRuby::Gem::Specification.new('c_and_ruby_extension_example') do |spec| spec.license = 'MIT' - spec.authors = 'mruby developers' + spec.author = 'mruby developers' end The mrbgems build process will use this specification to compile Object and Ruby @@ -124,7 +124,7 @@ In case your GEM is depending on other GEMs please use MRuby::Gem::Specification.new('c_and_ruby_extension_example') do |spec| spec.license = 'MIT' - spec.authors = 'mruby developers' + spec.author = 'mruby developers' # add GEM dependency mruby-parser. # Version has to be between 1.0.0 and 1.5.2 @@ -156,7 +156,7 @@ the following options additionally inside of your GEM specification: mruby can be extended with C. This is possible by using the C API to integrate C libraries into mruby. -### Pre-Conditions +### Preconditions mrbgems expects that you have implemented a C method called `mrb_YOURGEMNAME_gem_init(mrb_state)`. `YOURGEMNAME` will be replaced diff --git a/examples/mrbgems/c_and_ruby_extension_example/mrbgem.rake b/examples/mrbgems/c_and_ruby_extension_example/mrbgem.rake index 35dbe5e18..a1f964b36 100644 --- a/examples/mrbgems/c_and_ruby_extension_example/mrbgem.rake +++ b/examples/mrbgems/c_and_ruby_extension_example/mrbgem.rake @@ -1,6 +1,6 @@ MRuby::Gem::Specification.new('c_and_ruby_extension_example') do |spec| spec.license = 'MIT' - spec.authors = 'mruby developers' + spec.author = 'mruby developers' # Add compile flags # spec.cc.flags << '' diff --git a/examples/mrbgems/c_extension_example/mrbgem.rake b/examples/mrbgems/c_extension_example/mrbgem.rake index 8c132b233..3524e22f4 100644 --- a/examples/mrbgems/c_extension_example/mrbgem.rake +++ b/examples/mrbgems/c_extension_example/mrbgem.rake @@ -1,6 +1,6 @@ MRuby::Gem::Specification.new('c_extension_example') do |spec| spec.license = 'MIT' - spec.authors = 'mruby developers' + spec.author = 'mruby developers' # Add compile flags # spec.cc.flags << '-g' diff --git a/examples/mrbgems/ruby_extension_example/mrbgem.rake b/examples/mrbgems/ruby_extension_example/mrbgem.rake index 3a80b5cd7..5624c33e9 100644 --- a/examples/mrbgems/ruby_extension_example/mrbgem.rake +++ b/examples/mrbgems/ruby_extension_example/mrbgem.rake @@ -1,6 +1,6 @@ MRuby::Gem::Specification.new('ruby_extension_example') do |spec| spec.license = 'MIT' - spec.authors = 'mruby developers' + spec.author = 'mruby developers' # Add compile flags # spec.cc.flags << '' diff --git a/include/mrbconf.h b/include/mrbconf.h index 57148d428..a10a1d04e 100644 --- a/include/mrbconf.h +++ b/include/mrbconf.h @@ -41,6 +41,9 @@ /* initial size for IREP array */ //#define MRB_IREP_ARRAY_INIT_SIZE (256u) +/* turn off generational GC by default */ +//#define MRB_GC_TURN_OFF_GENERATIONAL + /* default size of khash table bucket */ //#define KHASH_DEFAULT_SIZE 32 diff --git a/include/mruby.h b/include/mruby.h index 85b464b8e..ba3b09a45 100644 --- a/include/mruby.h +++ b/include/mruby.h @@ -107,7 +107,6 @@ typedef struct mrb_state { struct mrb_irep **irep; /* program data array */ size_t irep_len, irep_capa; - mrb_sym init_sym; struct RObject *top_self; struct RClass *object_class; /* Object class */ struct RClass *class_class; @@ -134,8 +133,8 @@ typedef struct mrb_state { enum gc_state gc_state; /* state of gc */ int current_white_part; /* make white object by white_part */ - struct RBasic *gray_list; /* list of gray objects */ - struct RBasic *variable_gray_list; /* list of objects to be traversed atomically */ + struct RBasic *gray_list; /* list of gray objects to be traversed incrementally */ + struct RBasic *atomic_gray_list; /* list of objects to be traversed atomically */ size_t gc_live_after_mark; size_t gc_threshold; int gc_interval_ratio; @@ -173,6 +172,9 @@ void mrb_define_module_function(mrb_state*, struct RClass*, const char*, mrb_fun void mrb_define_const(mrb_state*, struct RClass*, const char *name, mrb_value); void mrb_undef_method(mrb_state*, struct RClass*, const char*); void mrb_undef_class_method(mrb_state*, struct RClass*, const char*); +mrb_value mrb_obj_new(mrb_state *mrb, struct RClass *c, int argc, mrb_value *argv); +#define mrb_class_new_instance(mrb,argc,argv,c) mrb_obj_new(mrb,c,argc,argv) +mrb_value mrb_class_obj_new(mrb_state *mrb, struct RClass *c, int argc, mrb_value *argv); mrb_value mrb_instance_new(mrb_state *mrb, mrb_value cv); struct RClass * mrb_class_new(mrb_state *mrb, struct RClass *super); struct RClass * mrb_module_new(mrb_state *mrb); @@ -273,6 +275,7 @@ mrb_value mrb_inspect(mrb_state *mrb, mrb_value obj); mrb_bool mrb_eql(mrb_state *mrb, mrb_value obj1, mrb_value obj2); void mrb_garbage_collect(mrb_state*); +void mrb_full_gc(mrb_state*); void mrb_incremental_gc(mrb_state *); int mrb_gc_arena_save(mrb_state*); void mrb_gc_arena_restore(mrb_state*,int); @@ -348,8 +351,6 @@ void mrb_print_error(mrb_state *mrb); mrb_value mrb_yield(mrb_state *mrb, mrb_value b, mrb_value arg); mrb_value mrb_yield_argv(mrb_state *mrb, mrb_value b, int argc, mrb_value *argv); -mrb_value mrb_class_new_instance(mrb_state *mrb, int, mrb_value*, struct RClass *); -mrb_value mrb_class_new_instance_m(mrb_state *mrb, mrb_value klass); void mrb_gc_protect(mrb_state *mrb, mrb_value obj); mrb_value mrb_to_int(mrb_state *mrb, mrb_value val); @@ -381,6 +382,13 @@ void* mrb_pool_realloc(struct mrb_pool*, void*, size_t oldlen, size_t newlen); mrb_bool mrb_pool_can_realloc(struct mrb_pool*, void*, size_t); void* mrb_alloca(mrb_state *mrb, size_t); +#ifdef MRB_DEBUG +#include <assert.h> +#define mrb_assert(p) assert(p) +#else +#define mrb_assert(p) ((void)0) +#endif + #if defined(__cplusplus) } /* extern "C" { */ #endif diff --git a/include/mruby/array.h b/include/mruby/array.h index d52ca1596..29f9be55e 100644 --- a/include/mruby/array.h +++ b/include/mruby/array.h @@ -27,9 +27,9 @@ struct RArray { mrb_value *ptr; }; -#define mrb_ary_ptr(v) ((struct RArray*)((v).value.p)) +#define mrb_ary_ptr(v) ((struct RArray*)(mrb_ptr(v))) #define mrb_ary_value(p) mrb_obj_value((void*)(p)) -#define RARRAY(v) ((struct RArray*)((v).value.p)) +#define RARRAY(v) ((struct RArray*)(mrb_ptr(v))) #define RARRAY_LEN(a) (RARRAY(a)->len) #define RARRAY_PTR(a) (RARRAY(a)->ptr) diff --git a/include/mruby/class.h b/include/mruby/class.h index 4ab954d9e..787445544 100644 --- a/include/mruby/class.h +++ b/include/mruby/class.h @@ -18,10 +18,10 @@ struct RClass { struct RClass *super; }; -#define mrb_class_ptr(v) ((struct RClass*)((v).value.p)) -#define RCLASS_SUPER(v) (((struct RClass*)((v).value.p))->super) -#define RCLASS_IV_TBL(v) (((struct RClass*)((v).value.p))->iv) -#define RCLASS_M_TBL(v) (((struct RClass*)((v).value.p))->mt) +#define mrb_class_ptr(v) ((struct RClass*)(mrb_ptr(v))) +#define RCLASS_SUPER(v) (((struct RClass*)(mrb_ptr(v)))->super) +#define RCLASS_IV_TBL(v) (((struct RClass*)(mrb_ptr(v)))->iv) +#define RCLASS_M_TBL(v) (((struct RClass*)(mrb_ptr(v)))->mt) static inline struct RClass* mrb_class(mrb_state *mrb, mrb_value v) diff --git a/include/mruby/compile.h b/include/mruby/compile.h index c1c646508..7c5884e58 100644 --- a/include/mruby/compile.h +++ b/include/mruby/compile.h @@ -23,6 +23,7 @@ typedef struct mrbc_context { short lineno; int (*partial_hook)(struct mrb_parser_state*); void *partial_data; + struct RClass *target_class; mrb_bool capture_errors:1; mrb_bool dump_result:1; mrb_bool no_exec:1; diff --git a/include/mruby/data.h b/include/mruby/data.h index c6b380f28..f0420a0c6 100644 --- a/include/mruby/data.h +++ b/include/mruby/data.h @@ -34,16 +34,21 @@ struct RData *mrb_data_object_alloc(mrb_state *mrb, struct RClass* klass, void * data = Data_Wrap_Struct(mrb,klass,type,sval);\ } while (0) -#define RDATA(obj) ((struct RData *)((obj).value.p)) +#define RDATA(obj) ((struct RData *)(mrb_ptr(obj))) #define DATA_PTR(d) (RDATA(d)->data) #define DATA_TYPE(d) (RDATA(d)->type) void mrb_data_check_type(mrb_state *mrb, mrb_value, const mrb_data_type*); void *mrb_data_get_ptr(mrb_state *mrb, mrb_value, const mrb_data_type*); -void *mrb_data_check_and_get(mrb_state *mrb, mrb_value, const mrb_data_type*); +#define DATA_GET_PTR(mrb,obj,dtype,type) (type*)mrb_data_get_ptr(mrb,obj,dtype) +void *mrb_data_check_get_ptr(mrb_state *mrb, mrb_value, const mrb_data_type*); +#define DATA_CHECK_GET_PTR(mrb,obj,dtype,type) (type*)mrb_data_check_get_ptr(mrb,obj,dtype) + +/* obsolete functions and macros */ +#define mrb_data_check_and_get(mrb,obj,dtype) mrb_data_get_ptr(mrb,obj,dtype) #define mrb_get_datatype(mrb,val,type) mrb_data_get_ptr(mrb, val, type) -#define mrb_check_datatype(mrb,val,type) mrb_data_check_and_get(mrb, val, type) +#define mrb_check_datatype(mrb,val,type) mrb_data_get_ptr(mrb, val, type) #define Data_Get_Struct(mrb,obj,type,sval) do {\ - *(void**)&sval = mrb_data_check_and_get(mrb, obj, type); \ + *(void**)&sval = mrb_data_get_ptr(mrb, obj, type); \ } while (0) #if defined(__cplusplus) diff --git a/include/mruby/hash.h b/include/mruby/hash.h index c331db157..ba3f84c41 100644 --- a/include/mruby/hash.h +++ b/include/mruby/hash.h @@ -17,7 +17,7 @@ struct RHash { struct kh_ht *ht; }; -#define mrb_hash_ptr(v) ((struct RHash*)((v).value.p)) +#define mrb_hash_ptr(v) ((struct RHash*)(mrb_ptr(v))) #define mrb_hash_value(p) mrb_obj_value((void*)(p)) mrb_value mrb_hash_new_capa(mrb_state*, int); @@ -33,7 +33,7 @@ mrb_value mrb_hash_empty_p(mrb_state *mrb, mrb_value self); mrb_value mrb_hash_clear(mrb_state *mrb, mrb_value hash); /* RHASH_TBL allocates st_table if not available. */ -#define RHASH(obj) ((struct RHash*)((obj).value.p)) +#define RHASH(obj) ((struct RHash*)(mrb_ptr(obj))) #define RHASH_TBL(h) (RHASH(h)->ht) #define RHASH_IFNONE(h) mrb_iv_get(mrb, (h), mrb_intern2(mrb, "ifnone", 6)) #define RHASH_PROCDEFAULT(h) RHASH_IFNONE(h) diff --git a/include/mruby/proc.h b/include/mruby/proc.h index 8c6146860..602b52288 100644 --- a/include/mruby/proc.h +++ b/include/mruby/proc.h @@ -44,7 +44,7 @@ struct RProc { #define MRB_PROC_STRICT 256 #define MRB_PROC_STRICT_P(p) (((p)->flags & MRB_PROC_STRICT) != 0) -#define mrb_proc_ptr(v) ((struct RProc*)((v).value.p)) +#define mrb_proc_ptr(v) ((struct RProc*)(mrb_ptr(v))) struct RProc *mrb_proc_new(mrb_state*, mrb_irep*); struct RProc *mrb_proc_new_cfunc(mrb_state*, mrb_func_t); diff --git a/include/mruby/range.h b/include/mruby/range.h index a87f1cc6a..6030410ba 100644 --- a/include/mruby/range.h +++ b/include/mruby/range.h @@ -22,7 +22,7 @@ struct RRange { int excl; }; -#define mrb_range_ptr(v) ((struct RRange*)((v).value.p)) +#define mrb_range_ptr(v) ((struct RRange*)(mrb_ptr(v))) #define mrb_range_value(p) mrb_obj_value((void*)(p)) mrb_value mrb_range_new(mrb_state*, mrb_value, mrb_value, int); diff --git a/include/mruby/string.h b/include/mruby/string.h index 000c80b09..903c036f2 100644 --- a/include/mruby/string.h +++ b/include/mruby/string.h @@ -25,8 +25,8 @@ struct RString { char *ptr; }; -#define mrb_str_ptr(s) ((struct RString*)((s).value.p)) -#define RSTRING(s) ((struct RString*)((s).value.p)) +#define mrb_str_ptr(s) ((struct RString*)(mrb_ptr(s))) +#define RSTRING(s) ((struct RString*)(mrb_ptr(s))) #define RSTRING_PTR(s) (RSTRING(s)->ptr) #define RSTRING_LEN(s) (RSTRING(s)->len) #define RSTRING_CAPA(s) (RSTRING(s)->aux.capa) diff --git a/include/mruby/value.h b/include/mruby/value.h index e78035b5f..8c1ffa42c 100644 --- a/include/mruby/value.h +++ b/include/mruby/value.h @@ -129,27 +129,45 @@ enum mrb_vtype { typedef struct mrb_value { union { mrb_float f; - struct { - MRB_ENDIAN_LOHI( - uint32_t ttt; - ,union { - void *p; - mrb_int i; - mrb_sym sym; - } value; - ) - }; + union { + void *p; + struct { + MRB_ENDIAN_LOHI( + uint32_t ttt; + ,union { + mrb_int i; + mrb_sym sym; + }; + ) + }; + } value; }; } mrb_value; -#define mrb_tt(o) ((o).ttt & 0xff) -#define mrb_mktt(tt) (0xfff00000|(tt)) -#define mrb_type(o) ((uint32_t)0xfff00000 < (o).ttt ? mrb_tt(o) : MRB_TT_FLOAT) -#define mrb_float(o) (o).f +/* value representation by nan-boxing: + * float : FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF + * object: 111111111111TTTT TTPPPPPPPPPPPPPP PPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPP + * int : 1111111111110001 0000000000000000 IIIIIIIIIIIIIIII IIIIIIIIIIIIIIII + * sym : 1111111111110001 0100000000000000 SSSSSSSSSSSSSSSS SSSSSSSSSSSSSSSS + * In order to get enough bit size to save TT, all pointers are shifted 2 bits + * in the right direction. + */ +#define mrb_tt(o) (((o).value.ttt & 0xfc000)>>14) +#define mrb_mktt(tt) (0xfff00000|((tt)<<14)) +#define mrb_type(o) ((uint32_t)0xfff00000 < (o).value.ttt ? mrb_tt(o) : MRB_TT_FLOAT) +#define mrb_ptr(o) ((void*)((((intptr_t)0x3fffffffffff)&((intptr_t)((o).value.p)))<<2)) +#define mrb_float(o) (o).f #define MRB_SET_VALUE(o, tt, attr, v) do {\ - (o).ttt = mrb_mktt(tt);\ - (o).attr = v;\ + (o).value.ttt = mrb_mktt(tt);\ + switch (tt) {\ + case MRB_TT_FALSE:\ + case MRB_TT_TRUE:\ + case MRB_TT_UNDEF:\ + case MRB_TT_FIXNUM:\ + case MRB_TT_SYMBOL: (o).attr = (v); break;\ + default: (o).value.i = 0; (o).value.p = (void*)((intptr_t)(o).value.p | (((intptr_t)(v))>>2)); break;\ + }\ } while (0) static inline mrb_value @@ -158,7 +176,7 @@ mrb_float_value(struct mrb_state *mrb, mrb_float f) mrb_value v; if (f != f) { - v.ttt = 0x7ff80000; + v.value.ttt = 0x7ff80000; v.value.i = 0; } else { v.f = f; @@ -216,11 +234,11 @@ typedef union mrb_value { void *p; struct { unsigned int i_flag : MRB_FIXNUM_SHIFT; - mrb_int i : (sizeof(mrb_int) * 8 - MRB_FIXNUM_SHIFT); + mrb_int i : (sizeof(mrb_int) * CHAR_BIT - MRB_FIXNUM_SHIFT); }; struct { unsigned int sym_flag : MRB_SPECIAL_SHIFT; - int sym : (sizeof(mrb_sym) * 8); + int sym : (sizeof(mrb_sym) * CHAR_BIT); }; struct RBasic *bp; struct RFloat *fp; @@ -229,7 +247,8 @@ typedef union mrb_value { unsigned long w; } mrb_value; -#define mrb_float(o) (o).value.fp->f +#define mrb_ptr(o) (o).value.p +#define mrb_float(o) (o).value.fp->f #define MRB_SET_VALUE(o, ttt, attr, v) do {\ (o).w = 0;\ @@ -261,8 +280,9 @@ typedef struct mrb_value { enum mrb_vtype tt; } mrb_value; -#define mrb_type(o) (o).tt -#define mrb_float(o) (o).value.f +#define mrb_type(o) (o).tt +#define mrb_ptr(o) (o).value.p +#define mrb_float(o) (o).value.f #define MRB_SET_VALUE(o, ttt, attr, v) do {\ (o).tt = ttt;\ @@ -273,6 +293,7 @@ static inline mrb_value mrb_float_value(struct mrb_state *mrb, mrb_float f) { mrb_value v; + (void) mrb; MRB_SET_VALUE(v, MRB_TT_FLOAT, value.f, f); return v; @@ -291,8 +312,7 @@ mrb_float_value(struct mrb_state *mrb, mrb_float f) #define mrb_bool(o) ((o).w != MRB_Qnil && (o).w != MRB_Qfalse) #else - -#define mrb_voidp(o) (o).value.p +#define mrb_voidp(o) mrb_ptr(o) #define mrb_fixnum_p(o) (mrb_type(o) == MRB_TT_FIXNUM) #define mrb_undef_p(o) (mrb_type(o) == MRB_TT_UNDEF) #define mrb_nil_p(o) (mrb_type(o) == MRB_TT_FALSE && !(o).value.i) @@ -339,8 +359,7 @@ mrb_float_value(struct mrb_state *mrb, mrb_float f) struct RBasic { MRB_OBJECT_HEADER; }; - -#define mrb_basic_ptr(v) ((struct RBasic*)((v).value.p)) +#define mrb_basic_ptr(v) ((struct RBasic*)(mrb_ptr(v))) /* obsolete macro mrb_basic; will be removed soon */ #define mrb_basic(v) mrb_basic_ptr(v) @@ -348,8 +367,7 @@ struct RObject { MRB_OBJECT_HEADER; struct iv_tbl *iv; }; - -#define mrb_obj_ptr(v) ((struct RObject*)((v).value.p)) +#define mrb_obj_ptr(v) ((struct RObject*)(mrb_ptr(v))) /* obsolete macro mrb_object; will be removed soon */ #define mrb_object(o) mrb_obj_ptr(o) #define mrb_immediate_p(x) (mrb_type(x) <= MRB_TT_VOIDP) @@ -429,6 +447,7 @@ static inline mrb_value mrb_voidp_value(struct mrb_state *mrb, void *p) { mrb_value v; + (void) mrb; MRB_SET_VALUE(v, MRB_TT_VOIDP, value.p, p); return v; @@ -317,7 +317,7 @@ class RakeApp "Require MODULE before executing rakefile."], ['--tasks', '-T', GetoptLong::NO_ARGUMENT, "Display the tasks and dependencies, then exit."], - ['--pull_gems','-p', GetoptLong::NO_ARGUMENT, + ['--pull-gems','-p', GetoptLong::NO_ARGUMENT, "Pull all git mrbgems."], ['--trace', '-t', GetoptLong::NO_ARGUMENT, "Turn on invoke/execute tracing."], @@ -403,7 +403,7 @@ class RakeApp require value when '--tasks' $show_tasks = true - when '--pull_gems' + when '--pull-gems' $pull_gems = true when '--trace' $trace = true diff --git a/mrbgems/mruby-array-ext/mrbgem.rake b/mrbgems/mruby-array-ext/mrbgem.rake index 38e0ad267..18f92ad65 100644 --- a/mrbgems/mruby-array-ext/mrbgem.rake +++ b/mrbgems/mruby-array-ext/mrbgem.rake @@ -1,4 +1,4 @@ MRuby::Gem::Specification.new('mruby-array-ext') do |spec| spec.license = 'MIT' - spec.authors = 'mruby developers' + spec.author = 'mruby developers' end diff --git a/mrbgems/mruby-bin-mirb/mrbgem.rake b/mrbgems/mruby-bin-mirb/mrbgem.rake index fd0b20254..846a3b077 100644 --- a/mrbgems/mruby-bin-mirb/mrbgem.rake +++ b/mrbgems/mruby-bin-mirb/mrbgem.rake @@ -1,5 +1,8 @@ MRuby::Gem::Specification.new('mruby-bin-mirb') do |spec| spec.license = 'MIT' - spec.authors = 'mruby developers' + spec.author = 'mruby developers' + + spec.linker.libraries << 'readline' if spec.cc.defines.include? "ENABLE_READLINE" + spec.bins = %w(mirb) end diff --git a/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c b/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c index 3111eea8f..1c746db21 100644 --- a/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c +++ b/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c @@ -244,6 +244,8 @@ main(int argc, char **argv) #ifndef ENABLE_READLINE int last_char; int char_index; +#else + char *home = NULL; #endif mrbc_context *cxt; struct mrb_parser_state *parser; @@ -253,7 +255,6 @@ main(int argc, char **argv) int n; int code_block_open = FALSE; int ai; - char *home = NULL; /* new interpreter instance */ mrb = mrb_open(); @@ -274,6 +275,8 @@ main(int argc, char **argv) cxt = mrbc_context_new(mrb); cxt->capture_errors = 1; + cxt->lineno = 1; + mrbc_filename(mrb, cxt, "(mirb)"); if (args.verbose) cxt->dump_result = 1; ai = mrb_gc_arena_save(mrb); @@ -285,10 +288,12 @@ main(int argc, char **argv) if (!home) home = getenv("USERPROFILE"); #endif - strcpy(history_path, home); - strcat(history_path, "/"); - strcat(history_path, history_file_name); - read_history(history_path); + if (home) { + strcpy(history_path, home); + strcat(history_path, "/"); + strcat(history_path, history_file_name); + read_history(history_path); + } #endif @@ -342,7 +347,7 @@ main(int argc, char **argv) parser = mrb_parser_new(mrb); parser->s = ruby_code; parser->send = ruby_code + strlen(ruby_code); - parser->lineno = 1; + parser->lineno = cxt->lineno; mrb_parser_parse(parser, cxt); code_block_open = is_code_block_open(parser); @@ -381,6 +386,7 @@ main(int argc, char **argv) mrb_gc_arena_restore(mrb, ai); } mrb_parser_free(parser); + cxt->lineno++; } mrbc_context_free(mrb, cxt); mrb_close(mrb); diff --git a/mrbgems/mruby-bin-mruby/mrbgem.rake b/mrbgems/mruby-bin-mruby/mrbgem.rake index 2e173a291..08ba89855 100644 --- a/mrbgems/mruby-bin-mruby/mrbgem.rake +++ b/mrbgems/mruby-bin-mruby/mrbgem.rake @@ -1,5 +1,5 @@ MRuby::Gem::Specification.new('mruby-bin-mruby') do |spec| spec.license = 'MIT' - spec.authors = 'mruby developers' + spec.author = 'mruby developers' spec.bins = %w(mruby) end diff --git a/mrbgems/mruby-bin-mruby/tools/mruby/mruby.c b/mrbgems/mruby-bin-mruby/tools/mruby/mruby.c index 97b55687f..baeb95993 100644 --- a/mrbgems/mruby-bin-mruby/tools/mruby/mruby.c +++ b/mrbgems/mruby-bin-mruby/tools/mruby/mruby.c @@ -91,16 +91,24 @@ parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args) item = argv[0]; append_cmdline: if (!args->cmdline) { + size_t buflen; char *buf; - buf = (char *)mrb_malloc(mrb, strlen(item)+1); - strcpy(buf, item); + buflen = strlen(item) + 1; + buf = (char *)mrb_malloc(mrb, buflen); + memcpy(buf, item, buflen); args->cmdline = buf; } else { - args->cmdline = (char *)mrb_realloc(mrb, args->cmdline, strlen(args->cmdline)+strlen(item)+2); - strcat(args->cmdline, "\n"); - strcat(args->cmdline, item); + size_t cmdlinelen; + size_t itemlen; + + cmdlinelen = strlen(args->cmdline); + itemlen = strlen(item); + args->cmdline = + (char *)mrb_realloc(mrb, args->cmdline, cmdlinelen + itemlen + 2); + args->cmdline[cmdlinelen] = '\n'; + memcpy(args->cmdline + cmdlinelen + 1, item, itemlen + 1); } } else { diff --git a/mrbgems/mruby-enum-ext/mrbgem.rake b/mrbgems/mruby-enum-ext/mrbgem.rake index 758d298dc..e054f4318 100644 --- a/mrbgems/mruby-enum-ext/mrbgem.rake +++ b/mrbgems/mruby-enum-ext/mrbgem.rake @@ -1,4 +1,4 @@ MRuby::Gem::Specification.new('mruby-enum-ext') do |spec| spec.license = 'MIT' - spec.authors = 'mruby developers' + spec.author = 'mruby developers' end diff --git a/mrbgems/mruby-enum-ext/test/enum.rb b/mrbgems/mruby-enum-ext/test/enum.rb index aa56cdf84..a7762231b 100644 --- a/mrbgems/mruby-enum-ext/test/enum.rb +++ b/mrbgems/mruby-enum-ext/test/enum.rb @@ -4,41 +4,40 @@ assert("Enumerable#drop") do a = [1, 2, 3, 4, 5, 0] - assert_equal a.drop(3), [4, 5, 0] - assert_equal a.drop(6), [] + assert_equal [4, 5, 0], a.drop(3) + assert_equal [], a.drop(6) end assert("Enumerable#drop_while") do a = [1, 2, 3, 4, 5, 0] - assert_equal a.drop_while {|i| i < 3 }, [3, 4, 5, 0] + assert_equal [3, 4, 5, 0], a.drop_while {|i| i < 3 } end assert("Enumerable#take") do a = [1, 2, 3, 4, 5, 0] - assert_equal a.take(3), [1, 2, 3] + assert_equal [1, 2, 3], a.take(3) end assert("Enumerable#take_while") do a = [1, 2, 3, 4, 5, 0] - assert_equal a.take_while {|i| i < 3 }, [1, 2] + assert_equal [1, 2], a.take_while {|i| i < 3} end assert("Enumerable#each_cons") do a = [] (1..5).each_cons(3){|e| a << e} - assert_equal a, [[1, 2, 3], [2, 3, 4], [3, 4, 5]] + assert_equal [[1, 2, 3], [2, 3, 4], [3, 4, 5]], a end assert("Enumerable#each_slice") do a = [] (1..10).each_slice(3){|e| a << e} - assert_equal a, [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]] + assert_equal [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]], a end assert("Enumerable#group_by") do r = (1..6).group_by {|i| i % 3 } - assert_equal r[0], [3, 6] - assert_equal r[1], [1, 4] - assert_equal r[2], [2, 5] + assert_equal [3, 6], r[0] + assert_equal [1, 4], r[1] + assert_equal [2, 5], r[2] end - diff --git a/mrbgems/mruby-eval/mrbgem.rake b/mrbgems/mruby-eval/mrbgem.rake index f80cf1b9e..217a9e604 100644 --- a/mrbgems/mruby-eval/mrbgem.rake +++ b/mrbgems/mruby-eval/mrbgem.rake @@ -1,4 +1,4 @@ MRuby::Gem::Specification.new('mruby-eval') do |spec| spec.license = 'MIT' - spec.authors = 'mruby developers' + spec.author = 'mruby developers' end diff --git a/mrbgems/mruby-exit/mrbgem.rake b/mrbgems/mruby-exit/mrbgem.rake new file mode 100644 index 000000000..910b3b4f9 --- /dev/null +++ b/mrbgems/mruby-exit/mrbgem.rake @@ -0,0 +1,4 @@ +MRuby::Gem::Specification.new('mruby-exit') do |spec| + spec.license = 'MIT' + spec.author = 'mruby developers' +end diff --git a/mrbgems/mruby-exit/src/mruby-exit.c b/mrbgems/mruby-exit/src/mruby-exit.c new file mode 100644 index 000000000..d81657592 --- /dev/null +++ b/mrbgems/mruby-exit/src/mruby-exit.c @@ -0,0 +1,24 @@ +#include <stdlib.h> +#include "mruby.h" + +static mrb_value +f_exit(mrb_state *mrb, mrb_value self) +{ + mrb_int i = EXIT_SUCCESS; + + mrb_get_args(mrb, "|i", &i); + exit(i); + /* not reached */ + return mrb_nil_value(); +} + +void +mrb_mruby_exit_gem_init(mrb_state* mrb) +{ + mrb_define_method(mrb, mrb->kernel_module, "exit", f_exit, MRB_ARGS_REQ(1)); +} + +void +mrb_mruby_exit_gem_final(mrb_state* mrb) +{ +} diff --git a/mrbgems/mruby-fiber/mrbgem.rake b/mrbgems/mruby-fiber/mrbgem.rake index cb258adcb..25009c47f 100644 --- a/mrbgems/mruby-fiber/mrbgem.rake +++ b/mrbgems/mruby-fiber/mrbgem.rake @@ -1,4 +1,4 @@ MRuby::Gem::Specification.new('mruby-fiber') do |spec| spec.license = 'MIT' - spec.authors = 'mruby developers' + spec.author = 'mruby developers' end diff --git a/mrbgems/mruby-fiber/src/fiber.c b/mrbgems/mruby-fiber/src/fiber.c index d5855caa3..c764b4786 100644 --- a/mrbgems/mruby-fiber/src/fiber.c +++ b/mrbgems/mruby-fiber/src/fiber.c @@ -62,7 +62,7 @@ static mrb_value fiber_init(mrb_state *mrb, mrb_value self) { static const struct mrb_context mrb_context_zero = { 0 }; - struct RFiber *f = (struct RFiber*)self.value.p; + struct RFiber *f = (struct RFiber*)mrb_ptr(self); struct mrb_context *c; struct RProc *p; mrb_callinfo *ci; @@ -113,7 +113,7 @@ fiber_init(mrb_state *mrb, mrb_value self) static struct mrb_context* fiber_check(mrb_state *mrb, mrb_value fib) { - struct RFiber *f = (struct RFiber*)fib.value.p; + struct RFiber *f = (struct RFiber*)mrb_ptr(fib); if (!f->cxt) { mrb_raise(mrb, E_ARGUMENT_ERROR, "uninitialized Fiber"); @@ -173,6 +173,7 @@ fiber_resume(mrb_state *mrb, mrb_value self) c->prev = mrb->c; if (c->prev->fib) mrb_field_write_barrier(mrb, (struct RBasic*)c->fib, (struct RBasic*)c->prev->fib); + mrb_write_barrier(mrb, (struct RBasic*)c->fib); c->status = MRB_FIBER_RUNNING; mrb->c = c; @@ -183,6 +184,7 @@ fiber_resume(mrb_state *mrb, mrb_value self) c->prev = mrb->c; if (c->prev->fib) mrb_field_write_barrier(mrb, (struct RBasic*)c->fib, (struct RBasic*)c->prev->fib); + mrb_write_barrier(mrb, (struct RBasic*)c->fib); c->status = MRB_FIBER_RUNNING; mrb->c = c; return fiber_result(mrb, a, len); diff --git a/mrbgems/mruby-hash-ext/mrbgem.rake b/mrbgems/mruby-hash-ext/mrbgem.rake index 3163c8c88..25d4f8e61 100644 --- a/mrbgems/mruby-hash-ext/mrbgem.rake +++ b/mrbgems/mruby-hash-ext/mrbgem.rake @@ -1,4 +1,4 @@ MRuby::Gem::Specification.new('mruby-hash-ext') do |spec| spec.license = 'MIT' - spec.authors = 'mruby developers' + spec.author = 'mruby developers' end diff --git a/mrbgems/mruby-hash-ext/test/hash.rb b/mrbgems/mruby-hash-ext/test/hash.rb index 73e12d8f2..40f6ac8bf 100644 --- a/mrbgems/mruby-hash-ext/test/hash.rb +++ b/mrbgems/mruby-hash-ext/test/hash.rb @@ -12,15 +12,13 @@ assert('Hash#merge!') do original end - result_1 == {'abc_key' => 'abc_value', 'cba_key' => 'XXX', - 'xyz_key' => 'xyz_value' } and - result_2 == {'abc_key' => 'abc_value', 'cba_key' => 'cba_value', - 'xyz_key' => 'xyz_value' } + assert_equal({'abc_key' => 'abc_value', 'cba_key' => 'XXX', + 'xyz_key' => 'xyz_value' }, result_1) + assert_equal({'abc_key' => 'abc_value', 'cba_key' => 'cba_value', + 'xyz_key' => 'xyz_value' }, result_2) end assert('Hash#values_at') do h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" } - result = h.values_at("cow", "cat") - - result == ["bovine", "feline"] + assert_equal ["bovine", "feline"], h.values_at("cow", "cat") end diff --git a/mrbgems/mruby-math/mrbgem.rake b/mrbgems/mruby-math/mrbgem.rake index 4b0fa40fd..75d3d1fff 100644 --- a/mrbgems/mruby-math/mrbgem.rake +++ b/mrbgems/mruby-math/mrbgem.rake @@ -1,4 +1,4 @@ MRuby::Gem::Specification.new('mruby-math') do |spec| spec.license = 'MIT' - spec.authors = 'mruby developers' + spec.author = 'mruby developers' end diff --git a/mrbgems/mruby-math/src/math.c b/mrbgems/mruby-math/src/math.c index bdc7767f7..4960de6dd 100644 --- a/mrbgems/mruby-math/src/math.c +++ b/mrbgems/mruby-math/src/math.c @@ -87,6 +87,10 @@ erfc(double x) return one_sqrtpi*exp(-x*x)*q2; } +#endif + +#if (defined _MSC_VER && _MSC_VER < 1800) || defined __ANDROID__ + double log2(double x) { diff --git a/mrbgems/mruby-numeric-ext/mrbgem.rake b/mrbgems/mruby-numeric-ext/mrbgem.rake index 69c4fde4c..010804058 100644 --- a/mrbgems/mruby-numeric-ext/mrbgem.rake +++ b/mrbgems/mruby-numeric-ext/mrbgem.rake @@ -1,4 +1,4 @@ MRuby::Gem::Specification.new('mruby-numeric-ext') do |spec| spec.license = 'MIT' - spec.authors = 'mruby developers' + spec.author = 'mruby developers' end diff --git a/mrbgems/mruby-numeric-ext/test/numeric.rb b/mrbgems/mruby-numeric-ext/test/numeric.rb index 6c1cf0fce..7d85eaaa2 100644 --- a/mrbgems/mruby-numeric-ext/test/numeric.rb +++ b/mrbgems/mruby-numeric-ext/test/numeric.rb @@ -2,8 +2,8 @@ # Numeric(Ext) Test assert('Integer#chr') do - assert_equal(65.chr, "A") - assert_equal(0x42.chr, "B") + assert_equal("A", 65.chr) + assert_equal("B", 0x42.chr) # multibyte encoding (not support yet) assert_raise(RangeError) { 12345.chr } diff --git a/mrbgems/mruby-object-ext/mrbgem.rake b/mrbgems/mruby-object-ext/mrbgem.rake index 1f232949e..980f1667a 100644 --- a/mrbgems/mruby-object-ext/mrbgem.rake +++ b/mrbgems/mruby-object-ext/mrbgem.rake @@ -1,4 +1,4 @@ MRuby::Gem::Specification.new('mruby-object-ext') do |spec| spec.license = 'MIT' - spec.authors = 'mruby developers' + spec.author = 'mruby developers' end diff --git a/mrbgems/mruby-object-ext/src/object.c b/mrbgems/mruby-object-ext/src/object.c index 598ebe91d..927a23d66 100644 --- a/mrbgems/mruby-object-ext/src/object.c +++ b/mrbgems/mruby-object-ext/src/object.c @@ -59,7 +59,7 @@ nil_to_i(mrb_state *mrb, mrb_value obj) * k.instance_exec(5) {|x| @secret+x } #=> 104 */ -mrb_value +mrb_value mrb_yield_internal(mrb_state *mrb, mrb_value b, int argc, mrb_value *argv, mrb_value self, struct RClass *c); static mrb_value @@ -69,7 +69,7 @@ mrb_obj_instance_exec(mrb_state *mrb, mrb_value self) int argc; mrb_value blk; struct RClass *c; - + mrb_get_args(mrb, "*&", &argv, &argc, &blk); if (mrb_nil_p(blk)) { @@ -80,7 +80,7 @@ mrb_obj_instance_exec(mrb_state *mrb, mrb_value self) case MRB_TT_SYMBOL: case MRB_TT_FIXNUM: case MRB_TT_FLOAT: - c = 0; + c = NULL; break; default: c = mrb_class_ptr(mrb_singleton_class(mrb, self)); diff --git a/mrbgems/mruby-object-ext/test/nil.rb b/mrbgems/mruby-object-ext/test/nil.rb index e385dec6a..5cd1cf4ed 100644 --- a/mrbgems/mruby-object-ext/test/nil.rb +++ b/mrbgems/mruby-object-ext/test/nil.rb @@ -1,11 +1,11 @@ assert('NilClass#to_a') do - assert_equal nil.to_a, [] + assert_equal [], nil.to_a end assert('NilClass#to_f') do - assert_equal nil.to_f, 0.0 + assert_equal 0.0, nil.to_f end assert('NilClass#to_i') do - assert_equal nil.to_i, 0 + assert_equal 0, nil.to_i end diff --git a/mrbgems/mruby-object-ext/test/object.rb b/mrbgems/mruby-object-ext/test/object.rb index 1a75622f6..1f2dd1b64 100644 --- a/mrbgems/mruby-object-ext/test/object.rb +++ b/mrbgems/mruby-object-ext/test/object.rb @@ -5,5 +5,5 @@ assert('Object#instance_exec') do end end k = KlassWithSecret.new - assert_equal k.instance_exec(5) {|x| @secret+x }, 104 + assert_equal 104, k.instance_exec(5) {|x| @secret+x } end diff --git a/mrbgems/mruby-objectspace/mrbgem.rake b/mrbgems/mruby-objectspace/mrbgem.rake index 100df4cdc..6a6a3e778 100644 --- a/mrbgems/mruby-objectspace/mrbgem.rake +++ b/mrbgems/mruby-objectspace/mrbgem.rake @@ -1,4 +1,4 @@ MRuby::Gem::Specification.new('mruby-objectspace') do |spec| spec.license = 'MIT' - spec.authors = 'mruby developers' + spec.author = 'mruby developers' end diff --git a/mrbgems/mruby-objectspace/test/objectspace.rb b/mrbgems/mruby-objectspace/test/objectspace.rb index 37137eb04..612137019 100644 --- a/mrbgems/mruby-objectspace/test/objectspace.rb +++ b/mrbgems/mruby-objectspace/test/objectspace.rb @@ -1,38 +1,37 @@ assert('ObjectSpace.count_objects') do - h = {} - ObjectSpace.count_objects(h) - assert_kind_of(Hash, h) - assert_true(h.keys.all? {|x| x.is_a?(Symbol) || x.is_a?(Integer) }) - assert_true(h.values.all? {|x| x.is_a?(Integer) }) - - assert_true(h.has_key?(:TOTAL)) - assert_true(h.has_key?(:FREE)) - - h = ObjectSpace.count_objects - assert_kind_of(Hash, h) - assert_true(h.keys.all? {|x| x.is_a?(Symbol) || x.is_a?(Integer) }) - assert_true(h.values.all? {|x| x.is_a?(Integer) }) - - assert_raise(TypeError) { ObjectSpace.count_objects(1) } - - h0 = {:MRB_TT_FOO=>1000} - h = ObjectSpace.count_objects(h0) - assert_false(h0.has_key?(:MRB_TT_FOO)) - - GC.start - h_after = {} - h_before = ObjectSpace.count_objects - - objs = [] - 1000.times do - objs << {} - end - objs = nil - ObjectSpace.count_objects(h) - GC.start - ObjectSpace.count_objects(h_after) - - assert_equal(h_before[:MRB_TT_HASH] + 1000, h[:MRB_TT_HASH]) - assert_equal(h_before[:MRB_TT_HASH], h_after[:MRB_TT_HASH]) - -end
\ No newline at end of file + h = {} + ObjectSpace.count_objects(h) + assert_kind_of(Hash, h) + assert_true(h.keys.all? {|x| x.is_a?(Symbol) || x.is_a?(Integer) }) + assert_true(h.values.all? {|x| x.is_a?(Integer) }) + + assert_true(h.has_key?(:TOTAL)) + assert_true(h.has_key?(:FREE)) + + h = ObjectSpace.count_objects + assert_kind_of(Hash, h) + assert_true(h.keys.all? {|x| x.is_a?(Symbol) || x.is_a?(Integer) }) + assert_true(h.values.all? {|x| x.is_a?(Integer) }) + + assert_raise(TypeError) { ObjectSpace.count_objects(1) } + + h0 = {:MRB_TT_FOO=>1000} + h = ObjectSpace.count_objects(h0) + assert_false(h0.has_key?(:MRB_TT_FOO)) + + GC.start + h_after = {} + h_before = ObjectSpace.count_objects + + objs = [] + 1000.times do + objs << {} + end + objs = nil + ObjectSpace.count_objects(h) + GC.start + ObjectSpace.count_objects(h_after) + + assert_equal(h[:MRB_TT_HASH], h_before[:MRB_TT_HASH] + 1000) + assert_equal(h_after[:MRB_TT_HASH], h_before[:MRB_TT_HASH]) +end diff --git a/mrbgems/mruby-print/mrbgem.rake b/mrbgems/mruby-print/mrbgem.rake index dc7831280..768fc2e18 100644 --- a/mrbgems/mruby-print/mrbgem.rake +++ b/mrbgems/mruby-print/mrbgem.rake @@ -1,4 +1,4 @@ MRuby::Gem::Specification.new('mruby-print') do |spec| spec.license = 'MIT' - spec.authors = 'mruby developers' + spec.author = 'mruby developers' end diff --git a/mrbgems/mruby-proc-ext/mrbgem.rake b/mrbgems/mruby-proc-ext/mrbgem.rake index 7f979431d..b75fceda3 100644 --- a/mrbgems/mruby-proc-ext/mrbgem.rake +++ b/mrbgems/mruby-proc-ext/mrbgem.rake @@ -1,4 +1,4 @@ MRuby::Gem::Specification.new('mruby-proc-ext') do |spec| spec.license = 'MIT' - spec.authors = 'mruby developers' + spec.author = 'mruby developers' end diff --git a/mrbgems/mruby-proc-ext/test/proc.rb b/mrbgems/mruby-proc-ext/test/proc.rb index a868a39a5..abbd7a9d9 100644 --- a/mrbgems/mruby-proc-ext/test/proc.rb +++ b/mrbgems/mruby-proc-ext/test/proc.rb @@ -8,24 +8,24 @@ end assert('Proc#===') do proc = Proc.new {|a| a * 2} - assert_equal (proc === 10), 20 + assert_equal 20, (proc === 10) end assert('Proc#yield') do proc = Proc.new {|a| a * 2} - assert_equal proc.yield(10), 20 + assert_equal 20, proc.yield(10) end assert('Proc#curry') do b = proc {|x, y, z| (x||0) + (y||0) + (z||0) } - assert_equal b.curry[1][2][3], 6 - assert_equal b.curry[1, 2][3, 4], 6 - assert_equal b.curry(5)[1][2][3][4][5], 6 - assert_equal b.curry(5)[1, 2][3, 4][5], 6 - assert_equal b.curry(1)[1], 1 + assert_equal 6, b.curry[1][2][3] + assert_equal 6, b.curry[1, 2][3, 4] + assert_equal 6, b.curry(5)[1][2][3][4][5] + assert_equal 6, b.curry(5)[1, 2][3, 4][5] + assert_equal 1, b.curry(1)[1] b = lambda {|x, y, z| (x||0) + (y||0) + (z||0) } - assert_equal b.curry[1][2][3], 6 + assert_equal 6, b.curry[1][2][3] assert_raise(ArgumentError) { b.curry[1, 2][3, 4] } assert_raise(ArgumentError) { b.curry(5) } assert_raise(ArgumentError) { b.curry(1) } diff --git a/mrbgems/mruby-random/mrbgem.rake b/mrbgems/mruby-random/mrbgem.rake index 38e93e4b1..7ad3c855c 100644 --- a/mrbgems/mruby-random/mrbgem.rake +++ b/mrbgems/mruby-random/mrbgem.rake @@ -1,4 +1,4 @@ MRuby::Gem::Specification.new('mruby-random') do |spec| spec.license = 'MIT' - spec.authors = 'mruby developers' + spec.author = 'mruby developers' end diff --git a/mrbgems/mruby-random/src/random.c b/mrbgems/mruby-random/src/random.c index 1c89ca21d..771167f2a 100644 --- a/mrbgems/mruby-random/src/random.c +++ b/mrbgems/mruby-random/src/random.c @@ -30,9 +30,7 @@ static mt_state *mrb_mt_get_context(mrb_state *mrb, mrb_value self) mrb_value context; context = mrb_iv_get(mrb, self, mrb_intern2(mrb, MT_STATE_KEY, MT_STATE_KEY_CSTR_LEN)); - t = (mt_state*)mrb_data_get_ptr(mrb, context, &mt_state_type); - if (!t) - mrb_raise(mrb, E_RUNTIME_ERROR, "mt_state get from mrb_iv_get failed"); + t = DATA_GET_PTR(mrb, context, &mt_state_type, mt_state); return t; } @@ -228,4 +226,3 @@ void mrb_mruby_random_gem_init(mrb_state *mrb) void mrb_mruby_random_gem_final(mrb_state *mrb) { } - diff --git a/mrbgems/mruby-range-ext/mrbgem.rake b/mrbgems/mruby-range-ext/mrbgem.rake index 5ed521a44..a7970aa51 100644 --- a/mrbgems/mruby-range-ext/mrbgem.rake +++ b/mrbgems/mruby-range-ext/mrbgem.rake @@ -1,4 +1,4 @@ MRuby::Gem::Specification.new('mruby-range-ext') do |spec| spec.license = 'MIT' - spec.authors = 'mruby developers' + spec.author = 'mruby developers' end diff --git a/mrbgems/mruby-range-ext/test/range.rb b/mrbgems/mruby-range-ext/test/range.rb index 6442d6e16..2a170a66a 100644 --- a/mrbgems/mruby-range-ext/test/range.rb +++ b/mrbgems/mruby-range-ext/test/range.rb @@ -8,13 +8,13 @@ assert('Range#cover?') do end assert('Range#first') do - assert_equal (10..20).first, 10 - assert_equal (10..20).first(3), [10, 11, 12] + assert_equal 10, (10..20).first + assert_equal [10, 11, 12], (10..20).first(3) end assert('Range#last') do - assert_equal (10..20).last, 20 - assert_equal (10...20).last, 20 - assert_equal (10..20).last(3), [18, 19, 20] - assert_equal (10...20).last(3), [17, 18, 19] + assert_equal 20, (10..20).last + assert_equal 20, (10...20).last + assert_equal [18, 19, 20], (10..20).last(3) + assert_equal [17, 18, 19], (10...20).last(3) end diff --git a/mrbgems/mruby-sprintf/mrbgem.rake b/mrbgems/mruby-sprintf/mrbgem.rake index 8772a5174..3e15ee7a9 100644 --- a/mrbgems/mruby-sprintf/mrbgem.rake +++ b/mrbgems/mruby-sprintf/mrbgem.rake @@ -1,4 +1,4 @@ MRuby::Gem::Specification.new('mruby-sprintf') do |spec| spec.license = 'MIT' - spec.authors = 'mruby developers' + spec.author = 'mruby developers' end diff --git a/mrbgems/mruby-string-ext/mrbgem.rake b/mrbgems/mruby-string-ext/mrbgem.rake index 83db97eb4..4a3369998 100644 --- a/mrbgems/mruby-string-ext/mrbgem.rake +++ b/mrbgems/mruby-string-ext/mrbgem.rake @@ -1,4 +1,4 @@ MRuby::Gem::Specification.new('mruby-string-ext') do |spec| spec.license = 'MIT' - spec.authors = 'mruby developers' + spec.author = 'mruby developers' end diff --git a/mrbgems/mruby-string-ext/test/string.rb b/mrbgems/mruby-string-ext/test/string.rb index 2bb32cef3..9eb6f6aaa 100644 --- a/mrbgems/mruby-string-ext/test/string.rb +++ b/mrbgems/mruby-string-ext/test/string.rb @@ -74,8 +74,8 @@ assert('String#rstrip!') do end assert('String#swapcase') do - assert_equal "Hello".swapcase, "hELLO" - assert_equal "cYbEr_PuNk11".swapcase, "CyBeR_pUnK11" + assert_equal "hELLO", "Hello".swapcase + assert_equal "CyBeR_pUnK11", "cYbEr_PuNk11".swapcase end assert('String#swapcase!') do @@ -95,10 +95,10 @@ assert('String#concat') do end assert('String#casecmp') do - assert_equal "abcdef".casecmp("abcde"), 1 - assert_equal "aBcDeF".casecmp("abcdef"), 0 - assert_equal "abcdef".casecmp("abcdefg"),-1 - assert_equal "abcdef".casecmp("ABCDEF"), 0 + assert_equal 1, "abcdef".casecmp("abcde") + assert_equal 0, "aBcDeF".casecmp("abcdef") + assert_equal(-1, "abcdef".casecmp("abcdefg")) + assert_equal 0, "abcdef".casecmp("ABCDEF") end assert('String#start_with?') do diff --git a/mrbgems/mruby-struct/mrbgem.rake b/mrbgems/mruby-struct/mrbgem.rake index 476e990da..3e9eab8d7 100644 --- a/mrbgems/mruby-struct/mrbgem.rake +++ b/mrbgems/mruby-struct/mrbgem.rake @@ -1,4 +1,4 @@ MRuby::Gem::Specification.new('mruby-struct') do |spec| spec.license = 'MIT' - spec.authors = 'mruby developers' + spec.author = 'mruby developers' end diff --git a/mrbgems/mruby-symbol-ext/mrbgem.rake b/mrbgems/mruby-symbol-ext/mrbgem.rake index 8e40c743b..6294e7a46 100644 --- a/mrbgems/mruby-symbol-ext/mrbgem.rake +++ b/mrbgems/mruby-symbol-ext/mrbgem.rake @@ -1,4 +1,4 @@ MRuby::Gem::Specification.new('mruby-symbol-ext') do |spec| spec.license = 'MIT' - spec.authors = 'mruby developers' + spec.author = 'mruby developers' end diff --git a/mrbgems/mruby-symbol-ext/test/symbol.rb b/mrbgems/mruby-symbol-ext/test/symbol.rb index b04847b3f..741315d74 100644 --- a/mrbgems/mruby-symbol-ext/test/symbol.rb +++ b/mrbgems/mruby-symbol-ext/test/symbol.rb @@ -2,11 +2,11 @@ # Symbol(Ext) Test assert('Symbol#to_proc') do - assert_equal :abs.to_proc[-5], 5 + assert_equal 5, :abs.to_proc[-5] end assert('Symbol.all_symbols') do foo = [:__symbol_test_1, :__symbol_test_2, :__symbol_test_3].sort symbols = Symbol.all_symbols.select{|sym|sym.to_s.include? '__symbol_test'}.sort - assert_equal symbols, foo + assert_equal foo, symbols end diff --git a/mrbgems/mruby-time/mrbgem.rake b/mrbgems/mruby-time/mrbgem.rake index 0f0b4899d..edf2aa3fc 100644 --- a/mrbgems/mruby-time/mrbgem.rake +++ b/mrbgems/mruby-time/mrbgem.rake @@ -1,4 +1,4 @@ MRuby::Gem::Specification.new('mruby-time') do |spec| spec.license = 'MIT' - spec.authors = 'mruby developers' + spec.author = 'mruby developers' end diff --git a/mrbgems/mruby-time/src/time.c b/mrbgems/mruby-time/src/time.c index 8b797f047..2ec352925 100644 --- a/mrbgems/mruby-time/src/time.c +++ b/mrbgems/mruby-time/src/time.c @@ -115,13 +115,7 @@ struct mrb_time { struct tm datetime; }; -static void -mrb_time_free(mrb_state *mrb, void *ptr) -{ - mrb_free(mrb, ptr); -} - -static struct mrb_data_type mrb_time_type = { "Time", mrb_time_free }; +static struct mrb_data_type mrb_time_type = { "Time", mrb_free }; /** Updates the datetime of a mrb_time based on it's timezone and seconds setting. Returns self on success, NULL of failure. */ @@ -153,7 +147,7 @@ mrb_time_wrap(mrb_state *mrb, struct RClass *tc, struct mrb_time *tm) /* Allocates a mrb_time object and initializes it. */ static struct mrb_time* -mrb_time_alloc(mrb_state *mrb, double sec, double usec, enum mrb_timezone timezone) +time_alloc(mrb_state *mrb, double sec, double usec, enum mrb_timezone timezone) { struct mrb_time *tm; @@ -177,7 +171,7 @@ mrb_time_alloc(mrb_state *mrb, double sec, double usec, enum mrb_timezone timezo static mrb_value mrb_time_make(mrb_state *mrb, struct RClass *c, double sec, double usec, enum mrb_timezone timezone) { - return mrb_time_wrap(mrb, c, mrb_time_alloc(mrb, sec, usec, timezone)); + return mrb_time_wrap(mrb, c, time_alloc(mrb, sec, usec, timezone)); } static struct mrb_time* @@ -259,7 +253,7 @@ time_mktime(mrb_state *mrb, mrb_int ayear, mrb_int amonth, mrb_int aday, mrb_raise(mrb, E_ARGUMENT_ERROR, "Not a valid time."); } - return mrb_time_alloc(mrb, nowsecs, ausec, timezone); + return time_alloc(mrb, nowsecs, ausec, timezone); } /* 15.2.19.6.2 */ @@ -298,8 +292,8 @@ mrb_time_eq(mrb_state *mrb, mrb_value self) mrb_bool eq_p; mrb_get_args(mrb, "o", &other); - tm1 = (struct mrb_time*)mrb_data_get_ptr(mrb, self, &mrb_time_type); - tm2 = (struct mrb_time*)mrb_data_get_ptr(mrb, other, &mrb_time_type); + tm1 = DATA_CHECK_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time); + tm2 = DATA_CHECK_GET_PTR(mrb, other, &mrb_time_type, struct mrb_time); eq_p = tm1 && tm2 && tm1->sec == tm2->sec && tm1->usec == tm2->usec; return mrb_bool_value(eq_p); @@ -312,8 +306,8 @@ mrb_time_cmp(mrb_state *mrb, mrb_value self) struct mrb_time *tm1, *tm2; mrb_get_args(mrb, "o", &other); - tm1 = (struct mrb_time*)mrb_data_get_ptr(mrb, self, &mrb_time_type); - tm2 = (struct mrb_time*)mrb_data_get_ptr(mrb, other, &mrb_time_type); + tm1 = DATA_CHECK_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time); + tm2 = DATA_CHECK_GET_PTR(mrb, other, &mrb_time_type, struct mrb_time); if (!tm1 || !tm2) return mrb_nil_value(); if (tm1->sec > tm2->sec) { return mrb_fixnum_value(1); @@ -338,8 +332,7 @@ mrb_time_plus(mrb_state *mrb, mrb_value self) struct mrb_time *tm; mrb_get_args(mrb, "f", &f); - tm = (struct mrb_time*)mrb_data_get_ptr(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); + tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time); return mrb_time_make(mrb, mrb_obj_class(mrb, self), (double)tm->sec+f, tm->usec, tm->timezone); } @@ -351,10 +344,9 @@ mrb_time_minus(mrb_state *mrb, mrb_value self) struct mrb_time *tm, *tm2; mrb_get_args(mrb, "o", &other); - tm = (struct mrb_time*)mrb_data_get_ptr(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); + tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time); - tm2 = (struct mrb_time*)mrb_data_get_ptr(mrb, other, &mrb_time_type); + tm2 = DATA_CHECK_GET_PTR(mrb, other, &mrb_time_type, struct mrb_time); if (tm2) { f = (mrb_float)(tm->sec - tm2->sec) + (mrb_float)(tm->usec - tm2->usec) / 1.0e6; @@ -373,8 +365,7 @@ mrb_time_wday(mrb_state *mrb, mrb_value self) { struct mrb_time *tm; - tm = (struct mrb_time*)mrb_data_get_ptr(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); + tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time); return mrb_fixnum_value(tm->datetime.tm_wday); } @@ -385,8 +376,7 @@ mrb_time_yday(mrb_state *mrb, mrb_value self) { struct mrb_time *tm; - tm = (struct mrb_time*)mrb_data_get_ptr(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); + tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time); return mrb_fixnum_value(tm->datetime.tm_yday + 1); } @@ -397,8 +387,7 @@ mrb_time_year(mrb_state *mrb, mrb_value self) { struct mrb_time *tm; - tm = (struct mrb_time*)mrb_data_get_ptr(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); + tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time); return mrb_fixnum_value(tm->datetime.tm_year + 1900); } @@ -409,8 +398,7 @@ mrb_time_zone(mrb_state *mrb, mrb_value self) { struct mrb_time *tm; - tm = (struct mrb_time*)mrb_data_get_ptr(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); + tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time); if (tm->timezone <= MRB_TIMEZONE_NONE) return mrb_nil_value(); if (tm->timezone >= MRB_TIMEZONE_LAST) return mrb_nil_value(); return mrb_str_new_cstr(mrb, timezone_names[tm->timezone]); @@ -426,8 +414,7 @@ mrb_time_asctime(mrb_state *mrb, mrb_value self) char buf[256]; int len; - tm = (struct mrb_time*)mrb_data_get_ptr(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); + tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time); d = &tm->datetime; len = snprintf(buf, sizeof(buf), "%s %s %02d %02d:%02d:%02d %s%d", wday_names[d->tm_wday], mon_names[d->tm_mon], d->tm_mday, @@ -444,7 +431,7 @@ mrb_time_day(mrb_state *mrb, mrb_value self) { struct mrb_time *tm; - tm = (struct mrb_time*)mrb_data_get_ptr(mrb, self, &mrb_time_type); + tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time); if (!tm) return mrb_nil_value(); return mrb_fixnum_value(tm->datetime.tm_mday); } @@ -453,12 +440,11 @@ mrb_time_day(mrb_state *mrb, mrb_value self) /* 15.2.19.7.7 */ /* Returns true if daylight saving was applied for this time. */ static mrb_value -mrb_time_dstp(mrb_state *mrb, mrb_value self) +mrb_time_dst_p(mrb_state *mrb, mrb_value self) { struct mrb_time *tm; - tm = (struct mrb_time*)mrb_data_get_ptr(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); + tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time); return mrb_bool_value(tm->datetime.tm_isdst); } @@ -470,8 +456,7 @@ mrb_time_getutc(mrb_state *mrb, mrb_value self) { struct mrb_time *tm, *tm2; - tm = (struct mrb_time*)mrb_data_get_ptr(mrb, self, &mrb_time_type); - if (!tm) return self; + tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time); tm2 = (struct mrb_time *)mrb_malloc(mrb, sizeof(*tm)); *tm2 = *tm; tm2->timezone = MRB_TIMEZONE_UTC; @@ -486,8 +471,7 @@ mrb_time_getlocal(mrb_state *mrb, mrb_value self) { struct mrb_time *tm, *tm2; - tm = (struct mrb_time*)mrb_data_get_ptr(mrb, self, &mrb_time_type); - if (!tm) return self; + tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time); tm2 = (struct mrb_time *)mrb_malloc(mrb, sizeof(*tm)); *tm2 = *tm; tm2->timezone = MRB_TIMEZONE_LOCAL; @@ -502,8 +486,7 @@ mrb_time_hour(mrb_state *mrb, mrb_value self) { struct mrb_time *tm; - tm = (struct mrb_time*)mrb_data_get_ptr(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); + tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time); return mrb_fixnum_value(tm->datetime.tm_hour); } @@ -519,7 +502,7 @@ mrb_time_initialize(mrb_state *mrb, mrb_value self) tm = (struct mrb_time*)DATA_PTR(self); if (tm) { - mrb_time_free(mrb, tm); + mrb_free(mrb, tm); } DATA_TYPE(self) = &mrb_time_type; DATA_PTR(self) = NULL; @@ -563,8 +546,7 @@ mrb_time_localtime(mrb_state *mrb, mrb_value self) { struct mrb_time *tm; - tm = (struct mrb_time*)mrb_data_get_ptr(mrb, self, &mrb_time_type); - if (!tm) return self; + tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time); tm->timezone = MRB_TIMEZONE_LOCAL; mrb_time_update_datetime(tm); return self; @@ -577,8 +559,7 @@ mrb_time_mday(mrb_state *mrb, mrb_value self) { struct mrb_time *tm; - tm = (struct mrb_time*)mrb_data_get_ptr(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); + tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time); return mrb_fixnum_value(tm->datetime.tm_mday); } @@ -589,8 +570,7 @@ mrb_time_min(mrb_state *mrb, mrb_value self) { struct mrb_time *tm; - tm = (struct mrb_time*)mrb_data_get_ptr(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); + tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time); return mrb_fixnum_value(tm->datetime.tm_min); } @@ -601,8 +581,7 @@ mrb_time_mon(mrb_state *mrb, mrb_value self) { struct mrb_time *tm; - tm = (struct mrb_time*)mrb_data_get_ptr(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); + tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time); return mrb_fixnum_value(tm->datetime.tm_mon + 1); } @@ -613,8 +592,7 @@ mrb_time_sec(mrb_state *mrb, mrb_value self) { struct mrb_time *tm; - tm = (struct mrb_time*)mrb_data_get_ptr(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); + tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time); return mrb_fixnum_value(tm->datetime.tm_sec); } @@ -626,8 +604,7 @@ mrb_time_to_f(mrb_state *mrb, mrb_value self) { struct mrb_time *tm; - tm = (struct mrb_time*)mrb_data_get_ptr(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); + tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time); return mrb_float_value(mrb, (mrb_float)tm->sec + (mrb_float)tm->usec/1.0e6); } @@ -638,8 +615,7 @@ mrb_time_to_i(mrb_state *mrb, mrb_value self) { struct mrb_time *tm; - tm = (struct mrb_time*)mrb_data_get_ptr(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); + tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time); return mrb_fixnum_value(tm->sec); } @@ -650,8 +626,7 @@ mrb_time_usec(mrb_state *mrb, mrb_value self) { struct mrb_time *tm; - tm = (struct mrb_time*)mrb_data_get_ptr(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); + tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time); return mrb_fixnum_value(tm->usec); } @@ -662,27 +637,24 @@ mrb_time_utc(mrb_state *mrb, mrb_value self) { struct mrb_time *tm; - tm = (struct mrb_time*)mrb_data_get_ptr(mrb, self, &mrb_time_type); - if (tm) { - tm->timezone = MRB_TIMEZONE_UTC; - mrb_time_update_datetime(tm); - } + tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time); + tm->timezone = MRB_TIMEZONE_UTC; + mrb_time_update_datetime(tm); return self; } /* 15.2.19.7.28 */ /* Returns true if this time is in the UTC timezone false if not. */ static mrb_value -mrb_time_utcp(mrb_state *mrb, mrb_value self) +mrb_time_utc_p(mrb_state *mrb, mrb_value self) { struct mrb_time *tm; - tm = (struct mrb_time*)mrb_data_get_ptr(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); + + tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time); return mrb_bool_value(tm->timezone == MRB_TIMEZONE_UTC); } - void mrb_mruby_time_gem_init(mrb_state* mrb) { @@ -707,11 +679,11 @@ mrb_mruby_time_gem_init(mrb_state* mrb) mrb_define_method(mrb, tc, "asctime", mrb_time_asctime, MRB_ARGS_NONE()); /* 15.2.19.7.4 */ mrb_define_method(mrb, tc, "ctime" , mrb_time_asctime, MRB_ARGS_NONE()); /* 15.2.19.7.5 */ mrb_define_method(mrb, tc, "day" , mrb_time_day , MRB_ARGS_NONE()); /* 15.2.19.7.6 */ - mrb_define_method(mrb, tc, "dst?" , mrb_time_dstp , MRB_ARGS_NONE()); /* 15.2.19.7.7 */ + mrb_define_method(mrb, tc, "dst?" , mrb_time_dst_p , MRB_ARGS_NONE()); /* 15.2.19.7.7 */ mrb_define_method(mrb, tc, "getgm" , mrb_time_getutc , MRB_ARGS_NONE()); /* 15.2.19.7.8 */ mrb_define_method(mrb, tc, "getlocal",mrb_time_getlocal,MRB_ARGS_NONE()); /* 15.2.19.7.9 */ mrb_define_method(mrb, tc, "getutc" , mrb_time_getutc , MRB_ARGS_NONE()); /* 15.2.19.7.10 */ - mrb_define_method(mrb, tc, "gmt?" , mrb_time_utcp , MRB_ARGS_NONE()); /* 15.2.19.7.11 */ + mrb_define_method(mrb, tc, "gmt?" , mrb_time_utc_p , MRB_ARGS_NONE()); /* 15.2.19.7.11 */ mrb_define_method(mrb, tc, "gmtime" , mrb_time_utc , MRB_ARGS_NONE()); /* 15.2.19.7.13 */ mrb_define_method(mrb, tc, "hour" , mrb_time_hour, MRB_ARGS_NONE()); /* 15.2.19.7.15 */ mrb_define_method(mrb, tc, "localtime", mrb_time_localtime, MRB_ARGS_NONE()); /* 15.2.19.7.18 */ @@ -726,7 +698,7 @@ mrb_mruby_time_gem_init(mrb_state* mrb) mrb_define_method(mrb, tc, "to_f", mrb_time_to_f, MRB_ARGS_NONE()); /* 15.2.19.7.24 */ mrb_define_method(mrb, tc, "usec", mrb_time_usec, MRB_ARGS_NONE()); /* 15.2.19.7.26 */ mrb_define_method(mrb, tc, "utc" , mrb_time_utc, MRB_ARGS_NONE()); /* 15.2.19.7.27 */ - mrb_define_method(mrb, tc, "utc?", mrb_time_utcp, MRB_ARGS_NONE()); /* 15.2.19.7.28 */ + mrb_define_method(mrb, tc, "utc?", mrb_time_utc_p,MRB_ARGS_NONE()); /* 15.2.19.7.28 */ mrb_define_method(mrb, tc, "wday", mrb_time_wday, MRB_ARGS_NONE()); /* 15.2.19.7.30 */ mrb_define_method(mrb, tc, "yday", mrb_time_yday, MRB_ARGS_NONE()); /* 15.2.19.7.31 */ mrb_define_method(mrb, tc, "year", mrb_time_year, MRB_ARGS_NONE()); /* 15.2.19.7.32 */ diff --git a/mrbgems/mruby-toplevel-ext/mrbgem.rake b/mrbgems/mruby-toplevel-ext/mrbgem.rake index 457047e8d..78eb73a26 100644 --- a/mrbgems/mruby-toplevel-ext/mrbgem.rake +++ b/mrbgems/mruby-toplevel-ext/mrbgem.rake @@ -1,4 +1,4 @@ MRuby::Gem::Specification.new('mruby-toplevel-ext') do |spec| spec.license = 'MIT' - spec.authors = 'mruby developers' + spec.author = 'mruby developers' end diff --git a/mrbgems/mruby-toplevel-ext/test/toplevel.rb b/mrbgems/mruby-toplevel-ext/test/toplevel.rb index 4401beba1..9630fe3dd 100644 --- a/mrbgems/mruby-toplevel-ext/test/toplevel.rb +++ b/mrbgems/mruby-toplevel-ext/test/toplevel.rb @@ -1,19 +1,19 @@ ## # Toplevel Self(Ext) Test -module ToplevelTestModule1 - def method_foo - :foo - end +assert('Toplevel#include') do + module ToplevelTestModule1 + def method_foo + :foo + end - CONST_BAR = :bar -end + CONST_BAR = :bar + end -module ToplevelTestModule2 - CONST_BAR = :bar2 -end + module ToplevelTestModule2 + CONST_BAR = :bar2 + end -assert('Toplevel#include') do self.include ToplevelTestModule2, ToplevelTestModule1 assert_true self.class.included_modules.include?( ToplevelTestModule1 ) diff --git a/mrblib/class.rb b/mrblib/class.rb index 4f268b6c8..ebfb1f76d 100644 --- a/mrblib/class.rb +++ b/mrblib/class.rb @@ -1,18 +1,24 @@ class Module # 15.2.2.4.13 def attr_reader(*names) - names.each{|name| - name2 = ('@'+name.to_s).intern - define_method(name){self.instance_variable_get(name2)} - } + names.each do |name| + name = name.to_s + raise(NameError, "#{name.inspect} is not allowed as an instance variable name") if name.include?('@') || name.include?('?') || name.include?('$') + + attr_name = '@'+name + define_method(name){self.instance_variable_get(attr_name)} + end end # 15.2.2.4.14 def attr_writer(*names) - names.each{|name| - name2 = ('@'+name.to_s).intern - name = (name.to_s+"=").intern - define_method(name){|v|self.instance_variable_set(name2,v)} - } + names.each do |name| + name = name.to_s + raise(NameError, "#{name.inspect} is not allowed as an instance variable name") if name.include?('@') || name.include?('?') || name.include?('$') + + attr_name = '@'+name + name = (name+"=").intern + define_method(name){|v|self.instance_variable_set(attr_name,v)} + end end # 15.2.2.4.12 def attr_accessor(*names) diff --git a/mrblib/string.rb b/mrblib/string.rb index 131d30446..49f87be8b 100644 --- a/mrblib/string.rb +++ b/mrblib/string.rb @@ -133,6 +133,18 @@ class String a = self[pos+1..-1] self.replace([b, value, a].join('')) end + + ## + # ISO 15.2.10.5.5 + def =~(re) + re =~ self + end + + ## + # ISO 15.2.10.5.27 + def match(re, &block) + re.match(self, &block) + end end ## diff --git a/src/array.c b/src/array.c index b5bbdf0fa..526cf4973 100644 --- a/src/array.c +++ b/src/array.c @@ -926,7 +926,8 @@ inspect_ary(mrb_state *mrb, mrb_value ary, mrb_value list) } if (mrb_array_p(RARRAY_PTR(ary)[i])) { s = inspect_ary(mrb, RARRAY_PTR(ary)[i], list); - } else { + } + else { s = mrb_inspect(mrb, RARRAY_PTR(ary)[i]); } mrb_str_buf_cat(mrb, arystr, RSTRING_PTR(s), RSTRING_LEN(s)); diff --git a/src/backtrace.c b/src/backtrace.c index e05ad4326..77906e843 100644 --- a/src/backtrace.c +++ b/src/backtrace.c @@ -7,18 +7,58 @@ #include "mruby.h" #include "mruby/variable.h" #include "mruby/proc.h" +#include "mruby/array.h" +#include "mruby/string.h" +#include <stdarg.h> + +typedef void (*output_stream_func)(mrb_state*, void*, int, const char*, ...); -void -mrb_print_backtrace(mrb_state *mrb) -{ #ifdef ENABLE_STDIO +static void +print_backtrace_i(mrb_state *mrb, void *stream, int level, const char *format, ...) +{ + va_list ap; + + va_start(ap, format); + vfprintf((FILE*)stream, format, ap); + va_end(ap); +} +#endif + +#define MIN_BUFSIZE 127 + +static void +get_backtrace_i(mrb_state *mrb, void *stream, int level, const char *format, ...) +{ + va_list ap; + mrb_value ary, str; + int len, ai; + + if (level > 0) { + return; + } + + ai = mrb_gc_arena_save(mrb); + ary = mrb_obj_value((struct RArray*)stream); + va_start(ap, format); + len = vsnprintf(NULL, 0, format, ap); + str = mrb_str_new(mrb, 0, len); + vsnprintf(RSTRING_PTR(str), len, format, ap); + mrb_ary_push(mrb, ary, str); + va_end(ap); + mrb_gc_arena_restore(mrb, ai); +} + +static void +mrb_output_backtrace(mrb_state *mrb, struct RObject *exc, output_stream_func func, void *stream) +{ mrb_callinfo *ci; mrb_int ciidx; const char *filename, *method, *sep; int i, line; - fputs("trace:\n", stderr); - ciidx = mrb_fixnum(mrb_obj_iv_get(mrb, mrb->exc, mrb_intern2(mrb, "ciidx", 5))); + func(mrb, stream, 1, "trace:\n"); + ciidx = mrb_fixnum(mrb_obj_iv_get(mrb, exc, mrb_intern2(mrb, "ciidx", 5))); if (ciidx >= mrb->c->ciend - mrb->c->cibase) ciidx = 10; /* ciidx is broken... */ @@ -41,7 +81,7 @@ mrb_print_backtrace(mrb_state *mrb) pc = mrb->c->cibase[i+1].pc; } else { - pc = (mrb_code*)mrb_voidp(mrb_obj_iv_get(mrb, mrb->exc, mrb_intern2(mrb, "lastpc", 6))); + pc = (mrb_code*)mrb_voidp(mrb_obj_iv_get(mrb, exc, mrb_intern2(mrb, "lastpc", 6))); } if (irep->iseq <= pc && pc < irep->iseq + irep->ilen) { line = irep->lines[pc - irep->iseq - 1]; @@ -59,15 +99,39 @@ mrb_print_backtrace(mrb_state *mrb) const char *cn = mrb_class_name(mrb, ci->proc->target_class); if (cn) { - fprintf(stderr, "\t[%d] %s:%d:in %s%s%s\n", i, filename, line, cn, sep, method); + func(mrb, stream, 1, "\t[%d] ", i); + func(mrb, stream, 0, "%s:%d:in %s%s%s", filename, line, cn, sep, method); + func(mrb, stream, 1, "\n"); } else { - fprintf(stderr, "\t[%d] %s:%d:in %s\n", i, filename, line, method); + func(mrb, stream, 1, "\t[%d] ", i); + func(mrb, stream, 0, "%s:%d:in %s", filename, line, method); + func(mrb, stream, 1, "\n"); } } else { - fprintf(stderr, "\t[%d] %s:%d\n", i, filename, line); + func(mrb, stream, 1, "\t[%d] ", i); + func(mrb, stream, 0, "%s:%d", filename, line); + func(mrb, stream, 1, "\n"); } } +} + +void +mrb_print_backtrace(mrb_state *mrb) +{ +#ifdef ENABLE_STDIO + mrb_output_backtrace(mrb, mrb->exc, print_backtrace_i, (void*)stderr); #endif } + +mrb_value +mrb_get_backtrace(mrb_state *mrb, mrb_value self) +{ + mrb_value ary; + + ary = mrb_ary_new(mrb); + mrb_output_backtrace(mrb, mrb_obj_ptr(self), get_backtrace_i, (void*)mrb_ary_ptr(ary)); + + return ary; +} diff --git a/src/class.c b/src/class.c index 606a54a7c..1870b94ba 100644 --- a/src/class.c +++ b/src/class.c @@ -311,7 +311,6 @@ mrb_define_method_id(mrb_state *mrb, struct RClass *c, mrb_sym mid, mrb_func_t f int ai = mrb_gc_arena_save(mrb); p = mrb_proc_new_cfunc(mrb, func); - p->target_class = c; mrb_define_method_raw(mrb, c, mid, p); mrb_gc_arena_restore(mrb, ai); } @@ -490,14 +489,19 @@ mrb_get_args(mrb_state *mrb, const char *format, ...) mrb_value ss; struct RString *s; char **ps; + mrb_int len; ps = va_arg(ap, char**); if (i < argc) { ss = to_str(mrb, *sp++); s = mrb_str_ptr(ss); - if (strlen(s->ptr) < s->len) { + len = (mrb_int)strlen(s->ptr); + if (len < s->len) { mrb_raise(mrb, E_ARGUMENT_ERROR, "String contains NUL"); } + else if (len > s->len) { + mrb_str_modify(mrb, s); + } *ps = s->ptr; i++; } @@ -1004,10 +1008,19 @@ mrb_method_search(mrb_state *mrb, struct RClass* c, mrb_sym mid) return m; } -void -mrb_obj_call_init(mrb_state *mrb, mrb_value obj, int argc, mrb_value *argv) +static mrb_value +mrb_instance_alloc(mrb_state *mrb, mrb_value cv) { - mrb_funcall_argv(mrb, obj, mrb->init_sym, argc, argv); + struct RClass *c = mrb_class_ptr(cv); + struct RObject *o; + enum mrb_vtype ttype = MRB_INSTANCE_TT(c); + + if (c->tt == MRB_TT_SCLASS) + mrb_raise(mrb, E_TYPE_ERROR, "can't create instance of singleton class"); + + if (ttype == 0) ttype = MRB_TT_OBJECT; + o = (struct RObject*)mrb_obj_alloc(mrb, ttype, c); + return mrb_obj_value(o); } /* @@ -1021,64 +1034,38 @@ mrb_obj_call_init(mrb_state *mrb, mrb_value obj, int argc, mrb_value *argv) * an object is constructed using .new. * */ -mrb_value -mrb_class_new_instance(mrb_state *mrb, int argc, mrb_value *argv, struct RClass * klass) -{ - mrb_value obj; - struct RClass * c = (struct RClass*)mrb_obj_alloc(mrb, klass->tt, klass); - c->super = klass; - obj = mrb_obj_value(c); - mrb_obj_call_init(mrb, obj, argc, argv); - return obj; -} mrb_value -mrb_class_new_instance_m(mrb_state *mrb, mrb_value klass) +mrb_instance_new(mrb_state *mrb, mrb_value cv) { + mrb_value obj, blk; mrb_value *argv; - mrb_value blk; - struct RClass *k = mrb_class_ptr(klass); - struct RClass *c; int argc; - mrb_value obj; + obj = mrb_instance_alloc(mrb, cv); mrb_get_args(mrb, "*&", &argv, &argc, &blk); - c = (struct RClass*)mrb_obj_alloc(mrb, k->tt, k); - c->super = k; - obj = mrb_obj_value(c); - mrb_funcall_with_block(mrb, obj, mrb->init_sym, argc, argv, blk); + mrb_funcall_with_block(mrb, obj, mrb_intern(mrb, "initialize"), argc, argv, blk); return obj; } mrb_value -mrb_instance_new(mrb_state *mrb, mrb_value cv) +mrb_obj_new(mrb_state *mrb, struct RClass *c, int argc, mrb_value *argv) { - struct RClass *c = mrb_class_ptr(cv); - struct RObject *o; - enum mrb_vtype ttype = MRB_INSTANCE_TT(c); - mrb_value obj, blk; - mrb_value *argv; - int argc; - - if (c->tt == MRB_TT_SCLASS) - mrb_raise(mrb, E_TYPE_ERROR, "can't create instance of singleton class"); + mrb_value obj; - if (ttype == 0) ttype = MRB_TT_OBJECT; - o = (struct RObject*)mrb_obj_alloc(mrb, ttype, c); - obj = mrb_obj_value(o); - mrb_get_args(mrb, "*&", &argv, &argc, &blk); - mrb_funcall_with_block(mrb, obj, mrb->init_sym, argc, argv, blk); + obj = mrb_instance_alloc(mrb, mrb_obj_value(c)); + mrb_funcall_argv(mrb, obj, mrb_intern(mrb, "initialize"), argc, argv); return obj; } -mrb_value +static mrb_value mrb_class_new_class(mrb_state *mrb, mrb_value cv) { mrb_value super; struct RClass *new_class; - + if (mrb_get_args(mrb, "|o", &super) == 0) { super = mrb_obj_value(mrb->object_class); } @@ -1437,7 +1424,8 @@ undef_method(mrb_state *mrb, struct RClass *c, mrb_sym a) if (!mrb_obj_respond_to(c, a)) { mrb_name_error(mrb, a, "undefined method '%S' for class '%S'", mrb_sym2str(mrb, a), mrb_obj_value(c)); - } else { + } + else { MRB_SET_VALUE(m, MRB_TT_PROC, value.p, 0); mrb_define_method_vm(mrb, c, a, m); } @@ -1484,8 +1472,9 @@ mod_define_method(mrb_state *mrb, mrb_value self) } p = (struct RProc*)mrb_obj_alloc(mrb, MRB_TT_PROC, mrb->proc_class); mrb_proc_copy(p, mrb_proc_ptr(blk)); + p->flags |= MRB_PROC_STRICT; mrb_define_method_raw(mrb, c, mid, p); - return blk; + return mrb_symbol_value(mid); } static void @@ -1519,7 +1508,8 @@ get_sym_or_str_arg(mrb_state *mrb) if (mrb_symbol_p(sym_or_str) || mrb_string_p(sym_or_str)) { return sym_or_str; - } else { + } + else { mrb_value obj = mrb_funcall(mrb, sym_or_str, "inspect", 0); mrb_raisef(mrb, E_TYPE_ERROR, "%S is not a symbol", obj); return mrb_nil_value(); @@ -1551,7 +1541,8 @@ mrb_mod_cvar_defined(mrb_state *mrb, mrb_value mod) if (mrb_symbol_p(id)) { check_cv_name_sym(mrb, mrb_symbol(id)); defined_p = mrb_cv_defined(mrb, mod, mrb_symbol(id)); - } else { + } + else { mrb_value sym; check_cv_name_str(mrb, id); sym = mrb_check_intern_str(mrb, id); @@ -1703,7 +1694,8 @@ mrb_mod_method_defined(mrb_state *mrb, mrb_value mod) id = get_sym_or_str_arg(mrb); if (mrb_symbol_p(id)) { method_defined_p = mrb_obj_respond_to(mrb_class_ptr(mod), mrb_symbol(id)); - } else { + } + else { mrb_value sym = mrb_check_intern_str(mrb, id); if (mrb_nil_p(sym)) { method_defined_p = FALSE; @@ -1787,7 +1779,8 @@ mrb_mod_const_defined(mrb_state *mrb, mrb_value mod) if (mrb_type(id) == MRB_TT_SYMBOL) { check_const_name_sym(mrb, mrb_symbol(id)); const_defined_p = mrb_const_defined(mrb, mod, mrb_symbol(id)); - } else { + } + else { mrb_value sym; check_const_name_str(mrb, id); sym = mrb_check_intern_str(mrb, id); diff --git a/src/codegen.c b/src/codegen.c index 37176653b..50916a473 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -959,6 +959,9 @@ gen_vmassignment(codegen_scope *s, node *tree, int rhs, int val) } } } + else { + pop(); + } } static void @@ -1551,7 +1554,6 @@ codegen(codegen_scope *s, node *tree, int val) // variable rhs codegen(s, t, VAL); gen_vmassignment(s, tree->car, rhs, val); - if (!val) pop(); } } break; @@ -2318,7 +2320,7 @@ codegen(codegen_scope *s, node *tree, int val) pop(); genop(s, MKOP_AB(OP_METHOD, cursp(), sym)); if (val) { - genop(s, MKOP_A(OP_LOADNIL, cursp())); + genop(s, MKOP_ABx(OP_LOADSYM, cursp(), sym)); push(); } } @@ -2338,7 +2340,7 @@ codegen(codegen_scope *s, node *tree, int val) pop(); genop(s, MKOP_AB(OP_METHOD, cursp(), sym)); if (val) { - genop(s, MKOP_A(OP_LOADNIL, cursp())); + genop(s, MKOP_ABx(OP_LOADSYM, cursp(), sym)); push(); } } @@ -2842,13 +2844,15 @@ codegen_start(mrb_state *mrb, parser_state *p) if (p->filename) { scope->filename = p->filename; } - if (setjmp(scope->jmp) != 0) { + if (setjmp(scope->jmp) == 0) { + // prepare irep + codegen(scope, p->tree, NOVAL); + mrb_pool_close(scope->mpool); + return 0; + } + else { return -1; } - // prepare irep - codegen(scope, p->tree, NOVAL); - mrb_pool_close(scope->mpool); - return 0; } int diff --git a/src/error.c b/src/error.c index bd3afd2e1..03f587a38 100644 --- a/src/error.c +++ b/src/error.c @@ -5,7 +5,6 @@ */ #include <errno.h> -#include <setjmp.h> #include <stdarg.h> #include <stdlib.h> #include <string.h> @@ -147,7 +146,8 @@ exc_inspect(mrb_state *mrb, mrb_value exc) if (!mrb_nil_p(mesg) && RSTRING_LEN(mesg) > 0) { mrb_str_cat(mrb, str, ": ", 2); mrb_str_append(mrb, str, mesg); - } else { + } + else { mrb_str_cat(mrb, str, ": ", 2); mrb_str_cat_cstr(mrb, str, mrb_obj_classname(mrb, exc)); } @@ -218,7 +218,7 @@ mrb_exc_raise(mrb_state *mrb, mrb_value exc) mrb_p(mrb, exc); abort(); } - longjmp(*(jmp_buf*)mrb->jmp, 1); + mrb_longjmp(mrb); } void @@ -307,7 +307,7 @@ mrb_name_error(mrb_state *mrb, mrb_sym id, const char *fmt, ...) va_end(args); argv[1] = mrb_symbol_value(id); - exc = mrb_class_new_instance(mrb, 2, argv, E_NAME_ERROR); + exc = mrb_obj_new(mrb, E_NAME_ERROR, 2, argv); mrb_exc_raise(mrb, exc); } @@ -425,14 +425,18 @@ mrb_sys_fail(mrb_state *mrb, const char *mesg) sce = mrb_class_get(mrb, "SystemCallError"); if (mesg != NULL) { mrb_funcall(mrb, mrb_obj_value(sce), "_sys_fail", 2, mrb_fixnum_value(no), mrb_str_new_cstr(mrb, mesg)); - } else { + } + else { mrb_funcall(mrb, mrb_obj_value(sce), "_sys_fail", 1, mrb_fixnum_value(no)); } - } else { + } + else { mrb_raise(mrb, E_RUNTIME_ERROR, mesg); } } +mrb_value mrb_get_backtrace(mrb_state*, mrb_value); + void mrb_init_exception(mrb_state *mrb) { @@ -446,6 +450,7 @@ mrb_init_exception(mrb_state *mrb) mrb_define_method(mrb, e, "to_s", exc_to_s, MRB_ARGS_NONE()); mrb_define_method(mrb, e, "message", exc_message, MRB_ARGS_NONE()); mrb_define_method(mrb, e, "inspect", exc_inspect, MRB_ARGS_NONE()); + mrb_define_method(mrb, e, "backtrace", mrb_get_backtrace, MRB_ARGS_NONE()); mrb->eStandardError_class = mrb_define_class(mrb, "StandardError", mrb->eException_class); /* 15.2.23 */ mrb_define_class(mrb, "RuntimeError", mrb->eStandardError_class); /* 15.2.28 */ diff --git a/src/error.h b/src/error.h index 3726cb4c4..5aa4ca374 100644 --- a/src/error.h +++ b/src/error.h @@ -14,5 +14,6 @@ mrb_value make_exception(mrb_state *mrb, int argc, mrb_value *argv, int isstr); mrb_value mrb_make_exception(mrb_state *mrb, int argc, mrb_value *argv); mrb_value mrb_format(mrb_state *mrb, const char *format, ...); void mrb_exc_print(mrb_state *mrb, struct RObject *exc); +void mrb_longjmp(mrb_state *mrb); #endif /* MRUBY_ERROR_H */ @@ -9,6 +9,7 @@ #include "error.h" #include "mruby/numeric.h" #include "mruby/data.h" +#include "mruby/class.h" struct RData* mrb_data_object_alloc(mrb_state *mrb, struct RClass *klass, void *ptr, const mrb_data_type *type) @@ -35,18 +36,17 @@ mrb_data_check_type(mrb_state *mrb, mrb_value obj, const mrb_data_type *type) mrb_raisef(mrb, E_TYPE_ERROR, "wrong argument type %S (expected %S)", mrb_str_new_cstr(mrb, t2->struct_name), mrb_str_new_cstr(mrb, type->struct_name)); } - } -} + else { + struct RClass *c = mrb_class(mrb, obj); -void * -mrb_data_check_and_get(mrb_state *mrb, mrb_value obj, const mrb_data_type *type) -{ - mrb_data_check_type(mrb, obj, type); - return DATA_PTR(obj); + mrb_raisef(mrb, E_TYPE_ERROR, "uninitialized %S (expected %S)", + mrb_obj_value(c), mrb_str_new_cstr(mrb, type->struct_name)); + } + } } void * -mrb_data_get_ptr(mrb_state *mrb, mrb_value obj, const mrb_data_type *type) +mrb_data_check_get_ptr(mrb_state *mrb, mrb_value obj, const mrb_data_type *type) { if (mrb_special_const_p(obj) || (mrb_type(obj) != MRB_TT_DATA)) { return NULL; @@ -57,6 +57,13 @@ mrb_data_get_ptr(mrb_state *mrb, mrb_value obj, const mrb_data_type *type) return DATA_PTR(obj); } +void * +mrb_data_get_ptr(mrb_state *mrb, mrb_value obj, const mrb_data_type *type) +{ + mrb_data_check_type(mrb, obj, type); + return DATA_PTR(obj); +} + mrb_value mrb_lastline_get(mrb_state *mrb) { @@ -176,7 +183,7 @@ mrb_obj_id(mrb_value obj) case MRB_TT_FILE: case MRB_TT_DATA: default: - return MakeID(obj.value.p); + return MakeID(mrb_ptr(obj)); } } @@ -38,18 +38,23 @@ * Gray - Marked, But the child objects are unmarked. * Black - Marked, the child objects are also marked. - == Two white part + == Two White Types - The white has a different part of A and B. - In sweep phase, the sweep target white is either A or B. - The sweep target white is switched just before sweep phase. - e.g. A -> B -> A -> B ... + There're two white color types in a flip-flop fassion: White-A and White-B, + which respectively represent the Current White color (the newly allocated + objects in the current GC cycle) and the Sweep Target White color (the + dead objects to be swept). - All objects are painted white when allocated. - This white is another the sweep target white. - For example, if the sweep target white is A, it's B. - So objects when allocated in sweep phase will be next sweep phase target. - Therefore, these objects will not be released accidentally in sweep phase. + A and B will be switched just at the beginning of the next GC cycle. At + that time, all the dead objects have been swept, while the newly created + objects in the current GC cycle which finally remains White are now + regarded as dead objects. Instead of traversing all the White-A objects and + paint them as White-B, just switch the meaning of White-A and White-B would + be much cheaper. + + As a result, the objects we sweep in the current GC cycle are always + left from the previous GC cycle. This allows us to sweep objects + incrementally, without the disturbance of the newly created objects. == Execution Timing @@ -61,7 +66,7 @@ For details, see the comments for each function. - = Write Barrier + == Write Barrier mruby implementer, C extension library writer must write a write barrier when writing a pointer to an object on object's field. @@ -70,6 +75,23 @@ * mrb_field_write_barrier * mrb_write_barrier + == Generational Mode + + mruby's GC offers an Generational Mode while re-using the tri-color GC + infrastructure. It will treat the Black objects as Old objects after each + sweep phase, instead of paint them to White. The key idea are still same as + the traditional generational GC: + + * Minor GC - just traverse the Young objects (Gray objects) in the mark + phase, then only sweep the newly created objects, and leave + the Old objects live. + + * Major GC - same as a full regular GC cycle. + + the difference between "tranditional" generational GC is that, the major GC + in mruby is triggered incrementally in a tri-color manner. + + For details, see the comments for each function. */ @@ -125,9 +147,9 @@ gettimeofday_time(void) gc_time = gettimeofday_time() - gc_time;\ gc_total_time += gc_time;\ fprintf(stderr, "gc_state: %d\n", mrb->gc_state);\ - fprintf(stderr, "live: %d\n", mrb->live);\ - fprintf(stderr, "majorgc_old_threshold: %d\n", mrb->majorgc_old_threshold);\ - fprintf(stderr, "gc_threshold: %d\n", mrb->gc_threshold);\ + fprintf(stderr, "live: %zu\n", mrb->live);\ + fprintf(stderr, "majorgc_old_threshold: %zu\n", mrb->majorgc_old_threshold);\ + fprintf(stderr, "gc_threshold: %zu\n", mrb->gc_threshold);\ fprintf(stderr, "gc_time: %30.20f\n", gc_time);\ fprintf(stderr, "gc_total_time: %30.20f\n\n", gc_total_time);\ } while(0) @@ -138,11 +160,8 @@ gettimeofday_time(void) #endif #ifdef GC_DEBUG -#include <assert.h> -#define gc_assert(expect) assert(expect) #define DEBUG(x) (x) #else -#define gc_assert(expect) ((void)0) #define DEBUG(x) #endif @@ -156,7 +175,7 @@ mrb_realloc_simple(mrb_state *mrb, void *p, size_t len) p2 = (mrb->allocf)(mrb, p, len, mrb->ud); if (!p2 && len > 0 && mrb->heaps) { - mrb_garbage_collect(mrb); + mrb_full_gc(mrb); p2 = (mrb->allocf)(mrb, p, len, mrb->ud); } @@ -212,7 +231,8 @@ mrb_calloc(mrb_state *mrb, size_t nelem, size_t len) if (p) { memset(p, 0, size); } - } else { + } + else { p = NULL; } @@ -317,8 +337,10 @@ mrb_init_heap(mrb_state *mrb) add_heap(mrb); mrb->gc_interval_ratio = DEFAULT_GC_INTERVAL_RATIO; mrb->gc_step_ratio = DEFAULT_GC_STEP_RATIO; +#ifndef MRB_GC_TURN_OFF_GENERATIONAL mrb->is_generational_gc_mode = TRUE; mrb->gc_full = TRUE; +#endif #ifdef GC_PROFILE program_invoke_time = gettimeofday_time(); @@ -370,7 +392,7 @@ mrb_obj_alloc(mrb_state *mrb, enum mrb_vtype ttype, struct RClass *cls) static const RVALUE RVALUE_zero = { { { MRB_TT_FALSE } } }; #ifdef MRB_GC_STRESS - mrb_garbage_collect(mrb); + mrb_full_gc(mrb); #endif if (mrb->gc_threshold < mrb->live) { mrb_incremental_gc(mrb); @@ -451,7 +473,7 @@ mark_context(mrb_state *mrb, struct mrb_context *c) static void gc_mark_children(mrb_state *mrb, struct RBasic *obj) { - gc_assert(is_gray(obj)); + mrb_assert(is_gray(obj)); paint_black(obj); mrb->gray_list = obj->gcnext; mrb_gc_mark(mrb, (struct RBasic*)obj->c); @@ -548,7 +570,7 @@ mrb_gc_mark(mrb_state *mrb, struct RBasic *obj) { if (obj == 0) return; if (!is_white(obj)) return; - gc_assert((obj)->tt != MRB_TT_FREE); + mrb_assert((obj)->tt != MRB_TT_FREE); add_gray_list(mrb, obj); } @@ -624,7 +646,7 @@ obj_free(mrb_state *mrb, struct RBasic *obj) case MRB_TT_DATA: { struct RData *d = (struct RData*)obj; - if (d->type->dfree) { + if (d->type && d->type->dfree) { d->type->dfree(mrb, d->data); } mrb_gc_free_iv(mrb, (struct RObject*)obj); @@ -644,7 +666,7 @@ root_scan_phase(mrb_state *mrb) if (!is_minor_gc(mrb)) { mrb->gray_list = NULL; - mrb->variable_gray_list = NULL; + mrb->atomic_gray_list = NULL; } mrb_gc_mark_gv(mrb); @@ -660,6 +682,10 @@ root_scan_phase(mrb_state *mrb) mrb_gc_mark(mrb, (struct RBasic*)mrb->exc); mark_context(mrb, mrb->root_c); + if (mrb->root_c != mrb->c) { + mark_context(mrb, mrb->c); + } + /* mark irep pool */ if (mrb->irep) { size_t len = mrb->irep_len; @@ -754,6 +780,18 @@ gc_gray_mark(mrb_state *mrb, struct RBasic *obj) return children; } + +static void +gc_mark_gray_list(mrb_state *mrb) { + while (mrb->gray_list) { + if (is_gray(mrb->gray_list)) + gc_mark_children(mrb, mrb->gray_list); + else + mrb->gray_list = mrb->gray_list->gcnext; + } +} + + static size_t incremental_marking_phase(mrb_state *mrb, size_t limit) { @@ -770,22 +808,12 @@ static void final_marking_phase(mrb_state *mrb) { mark_context_stack(mrb, mrb->root_c); - while (mrb->gray_list) { - if (is_gray(mrb->gray_list)) - gc_mark_children(mrb, mrb->gray_list); - else - mrb->gray_list = mrb->gray_list->gcnext; - } - gc_assert(mrb->gray_list == NULL); - mrb->gray_list = mrb->variable_gray_list; - mrb->variable_gray_list = NULL; - while (mrb->gray_list) { - if (is_gray(mrb->gray_list)) - gc_mark_children(mrb, mrb->gray_list); - else - mrb->gray_list = mrb->gray_list->gcnext; - } - gc_assert(mrb->gray_list == NULL); + gc_mark_gray_list(mrb); + mrb_assert(mrb->gray_list == NULL); + mrb->gray_list = mrb->atomic_gray_list; + mrb->atomic_gray_list = NULL; + gc_mark_gray_list(mrb); + mrb_assert(mrb->gray_list == NULL); } static void @@ -885,17 +913,31 @@ incremental_gc(mrb_state *mrb, size_t limit) } default: /* unknown state */ - gc_assert(0); + mrb_assert(0); return 0; } } static void -advance_phase(mrb_state *mrb, enum gc_state to_state) +incremental_gc_until(mrb_state *mrb, enum gc_state to_state) { - while (mrb->gc_state != to_state) { + do { incremental_gc(mrb, ~0); + } while (mrb->gc_state != to_state); +} + +static void +incremental_gc_step(mrb_state *mrb) +{ + size_t limit = 0, result = 0; + limit = (GC_STEP_SIZE/100) * mrb->gc_step_ratio; + while (result < limit) { + result += incremental_gc(mrb, limit); + if (mrb->gc_state == GC_STATE_NONE) + break; } + + mrb->gc_threshold = mrb->live + GC_STEP_SIZE; } static void @@ -903,16 +945,21 @@ clear_all_old(mrb_state *mrb) { size_t origin_mode = mrb->is_generational_gc_mode; - gc_assert(is_generational(mrb)); + mrb_assert(is_generational(mrb)); if (is_major_gc(mrb)) { - advance_phase(mrb, GC_STATE_NONE); + /* finish the half baked GC */ + incremental_gc_until(mrb, GC_STATE_NONE); } + /* Sweep the dead objects, then reset all the live objects + * (including all the old objects, of course) to white. */ mrb->is_generational_gc_mode = FALSE; prepare_incremental_sweep(mrb); - advance_phase(mrb, GC_STATE_NONE); - mrb->variable_gray_list = mrb->gray_list = NULL; + incremental_gc_until(mrb, GC_STATE_NONE); mrb->is_generational_gc_mode = origin_mode; + + /* The gray objects has already been painted as white */ + mrb->atomic_gray_list = mrb->gray_list = NULL; } void @@ -924,26 +971,19 @@ mrb_incremental_gc(mrb_state *mrb) GC_TIME_START; if (is_minor_gc(mrb)) { - do { - incremental_gc(mrb, ~0); - } while (mrb->gc_state != GC_STATE_NONE); + incremental_gc_until(mrb, GC_STATE_NONE); } else { - size_t limit = 0, result = 0; - limit = (GC_STEP_SIZE/100) * mrb->gc_step_ratio; - while (result < limit) { - result += incremental_gc(mrb, limit); - if (mrb->gc_state == GC_STATE_NONE) - break; - } + incremental_gc_step(mrb); } if (mrb->gc_state == GC_STATE_NONE) { - gc_assert(mrb->live >= mrb->gc_live_after_mark); + mrb_assert(mrb->live >= mrb->gc_live_after_mark); mrb->gc_threshold = (mrb->gc_live_after_mark/100) * mrb->gc_interval_ratio; if (mrb->gc_threshold < GC_STEP_SIZE) { mrb->gc_threshold = GC_STEP_SIZE; } + if (is_major_gc(mrb)) { mrb->majorgc_old_threshold = mrb->gc_live_after_mark/100 * DEFAULT_MAJOR_GC_INC_RATIO; mrb->gc_full = FALSE; @@ -955,40 +995,29 @@ mrb_incremental_gc(mrb_state *mrb) } } } - else { - mrb->gc_threshold = mrb->live + GC_STEP_SIZE; - } - GC_TIME_STOP_AND_REPORT; } +/* Perform a full gc cycle */ void -mrb_garbage_collect(mrb_state *mrb) +mrb_full_gc(mrb_state *mrb) { - size_t max_limit = ~0; - if (mrb->gc_disabled) return; - GC_INVOKE_TIME_REPORT("mrb_garbage_collect()"); + GC_INVOKE_TIME_REPORT("mrb_full_gc()"); GC_TIME_START; - if (mrb->gc_state == GC_STATE_SWEEP) { - /* finish sweep phase */ - while (mrb->gc_state != GC_STATE_NONE) { - incremental_gc(mrb, max_limit); - } - } - - /* clean all black object as old */ if (is_generational(mrb)) { + /* clear all the old objects back to young */ clear_all_old(mrb); mrb->gc_full = TRUE; } + else if (mrb->gc_state != GC_STATE_NONE) { + /* finish half baked GC cycle */ + incremental_gc_until(mrb, GC_STATE_NONE); + } - do { - incremental_gc(mrb, max_limit); - } while (mrb->gc_state != GC_STATE_NONE); - + incremental_gc_until(mrb, GC_STATE_NONE); mrb->gc_threshold = (mrb->gc_live_after_mark/100) * mrb->gc_interval_ratio; if (is_generational(mrb)) { @@ -999,6 +1028,12 @@ mrb_garbage_collect(mrb_state *mrb) GC_TIME_STOP_AND_REPORT; } +void +mrb_garbage_collect(mrb_state *mrb) +{ + mrb_full_gc(mrb); +} + int mrb_gc_arena_save(mrb_state *mrb) { @@ -1022,14 +1057,14 @@ mrb_field_write_barrier(mrb_state *mrb, struct RBasic *obj, struct RBasic *value if (!is_black(obj)) return; if (!is_white(value)) return; - gc_assert(!is_dead(mrb, value) && !is_dead(mrb, obj)); - gc_assert(is_generational(mrb) || mrb->gc_state != GC_STATE_NONE); + mrb_assert(!is_dead(mrb, value) && !is_dead(mrb, obj)); + mrb_assert(is_generational(mrb) || mrb->gc_state != GC_STATE_NONE); if (is_generational(mrb) || mrb->gc_state == GC_STATE_MARK) { add_gray_list(mrb, value); } else { - gc_assert(mrb->gc_state == GC_STATE_SWEEP); + mrb_assert(mrb->gc_state == GC_STATE_SWEEP); paint_partial_white(mrb, obj); /* for never write barriers */ } } @@ -1048,11 +1083,11 @@ mrb_write_barrier(mrb_state *mrb, struct RBasic *obj) { if (!is_black(obj)) return; - gc_assert(!is_dead(mrb, obj)); - gc_assert(is_generational(mrb) || mrb->gc_state != GC_STATE_NONE); + mrb_assert(!is_dead(mrb, obj)); + mrb_assert(is_generational(mrb) || mrb->gc_state != GC_STATE_NONE); paint_gray(obj); - obj->gcnext = mrb->variable_gray_list; - mrb->variable_gray_list = obj; + obj->gcnext = mrb->atomic_gray_list; + mrb->atomic_gray_list = obj; } /* @@ -1066,7 +1101,7 @@ mrb_write_barrier(mrb_state *mrb, struct RBasic *obj) static mrb_value gc_start(mrb_state *mrb, mrb_value obj) { - mrb_garbage_collect(mrb); + mrb_full_gc(mrb); return mrb_nil_value(); } @@ -1186,11 +1221,11 @@ change_gen_gc_mode(mrb_state *mrb, mrb_int enable) { if (is_generational(mrb) && !enable) { clear_all_old(mrb); - gc_assert(mrb->gc_state == GC_STATE_NONE); + mrb_assert(mrb->gc_state == GC_STATE_NONE); mrb->gc_full = FALSE; } else if (!is_generational(mrb) && enable) { - advance_phase(mrb, GC_STATE_NONE); + incremental_gc_until(mrb, GC_STATE_NONE); mrb->majorgc_old_threshold = mrb->gc_live_after_mark/100 * DEFAULT_MAJOR_GC_INC_RATIO; mrb->gc_full = FALSE; } @@ -1298,7 +1333,7 @@ test_mrb_field_write_barrier(void) mrb->gc_state = GC_STATE_MARK; mrb_field_write_barrier(mrb, obj, value); - gc_assert(is_gray(value)); + mrb_assert(is_gray(value)); puts(" in GC_STATE_SWEEP"); @@ -1306,8 +1341,8 @@ test_mrb_field_write_barrier(void) mrb->gc_state = GC_STATE_SWEEP; mrb_field_write_barrier(mrb, obj, value); - gc_assert(obj->color & mrb->current_white_part); - gc_assert(value->color & mrb->current_white_part); + mrb_assert(obj->color & mrb->current_white_part); + mrb_assert(value->color & mrb->current_white_part); puts(" fail with black"); @@ -1316,7 +1351,7 @@ test_mrb_field_write_barrier(void) paint_partial_white(mrb,value); mrb_field_write_barrier(mrb, obj, value); - gc_assert(obj->color & mrb->current_white_part); + mrb_assert(obj->color & mrb->current_white_part); puts(" fail with gray"); @@ -1325,7 +1360,7 @@ test_mrb_field_write_barrier(void) paint_gray(value); mrb_field_write_barrier(mrb, obj, value); - gc_assert(is_gray(value)); + mrb_assert(is_gray(value)); { @@ -1338,7 +1373,7 @@ test_mrb_field_write_barrier(void) mrb->gc_state = GC_STATE_MARK; mrb_field_write_barrier_value(mrb, obj, value); - gc_assert(is_gray(mrb_basic_ptr(value))); + mrb_assert(is_gray(mrb_basic_ptr(value))); } mrb_close(mrb); @@ -1358,15 +1393,15 @@ test_mrb_write_barrier(void) mrb->gc_state = GC_STATE_MARK; mrb_write_barrier(mrb, obj); - gc_assert(is_gray(obj)); - gc_assert(mrb->variable_gray_list == obj); + mrb_assert(is_gray(obj)); + mrb_assert(mrb->atomic_gray_list == obj); puts(" fail with gray"); paint_gray(obj); mrb_write_barrier(mrb, obj); - gc_assert(is_gray(obj)); + mrb_assert(is_gray(obj)); mrb_close(mrb); } @@ -1379,17 +1414,17 @@ test_add_gray_list(void) puts("test_add_gray_list"); change_gen_gc_mode(mrb, FALSE); - gc_assert(mrb->gray_list == NULL); + mrb_assert(mrb->gray_list == NULL); obj1 = mrb_basic_ptr(mrb_str_new_cstr(mrb, "test")); add_gray_list(mrb, obj1); - gc_assert(mrb->gray_list == obj1); - gc_assert(is_gray(obj1)); + mrb_assert(mrb->gray_list == obj1); + mrb_assert(is_gray(obj1)); obj2 = mrb_basic_ptr(mrb_str_new_cstr(mrb, "test")); add_gray_list(mrb, obj2); - gc_assert(mrb->gray_list == obj2); - gc_assert(mrb->gray_list->gcnext == obj1); - gc_assert(is_gray(obj2)); + mrb_assert(mrb->gray_list == obj2); + mrb_assert(mrb->gray_list->gcnext == obj1); + mrb_assert(is_gray(obj2)); mrb_close(mrb); } @@ -1408,8 +1443,8 @@ test_gc_gray_mark(void) obj = (struct RBasic*)mrb->object_class; paint_gray(obj); gray_num = gc_gray_mark(mrb, obj); - gc_assert(is_black(obj)); - gc_assert(gray_num > 1); + mrb_assert(is_black(obj)); + mrb_assert(gray_num > 1); puts(" in MRB_TT_ARRAY"); obj_v = mrb_ary_new(mrb); @@ -1418,9 +1453,9 @@ test_gc_gray_mark(void) paint_partial_white(mrb, mrb_basic_ptr(value_v)); mrb_ary_push(mrb, obj_v, value_v); gray_num = gc_gray_mark(mrb, mrb_basic_ptr(obj_v)); - gc_assert(is_black(mrb_basic_ptr(obj_v))); - gc_assert(is_gray(mrb_basic_ptr(value_v))); - gc_assert(gray_num == 1); + mrb_assert(is_black(mrb_basic_ptr(obj_v))); + mrb_assert(is_gray(mrb_basic_ptr(value_v))); + mrb_assert(gray_num == 1); mrb_close(mrb); } @@ -1436,16 +1471,16 @@ test_incremental_gc(void) puts("test_incremental_gc"); change_gen_gc_mode(mrb, FALSE); - puts(" in mrb_garbage_collect"); - mrb_garbage_collect(mrb); + puts(" in mrb_full_gc"); + mrb_full_gc(mrb); - gc_assert(mrb->gc_state == GC_STATE_NONE); + mrb_assert(mrb->gc_state == GC_STATE_NONE); puts(" in GC_STATE_NONE"); incremental_gc(mrb, max); - gc_assert(mrb->gc_state == GC_STATE_MARK); + mrb_assert(mrb->gc_state == GC_STATE_MARK); puts(" in GC_STATE_MARK"); - advance_phase(mrb, GC_STATE_SWEEP); - gc_assert(mrb->gc_state == GC_STATE_SWEEP); + incremental_gc_until(mrb, GC_STATE_SWEEP); + mrb_assert(mrb->gc_state == GC_STATE_SWEEP); puts(" in GC_STATE_SWEEP"); page = mrb->heaps; @@ -1465,13 +1500,13 @@ test_incremental_gc(void) total += MRB_HEAP_PAGE_SIZE; } - gc_assert(mrb->gray_list == NULL); + mrb_assert(mrb->gray_list == NULL); incremental_gc(mrb, max); - gc_assert(mrb->gc_state == GC_STATE_SWEEP); + mrb_assert(mrb->gc_state == GC_STATE_SWEEP); incremental_gc(mrb, max); - gc_assert(mrb->gc_state == GC_STATE_NONE); + mrb_assert(mrb->gc_state == GC_STATE_NONE); free = (RVALUE*)mrb->heaps->freelist; while (free) { @@ -1479,30 +1514,30 @@ test_incremental_gc(void) free = (RVALUE*)free->as.free.next; } - gc_assert(mrb->live == live); - gc_assert(mrb->live == total-freed); + mrb_assert(mrb->live == live); + mrb_assert(mrb->live == total-freed); puts("test_incremental_gc(gen)"); - advance_phase(mrb, GC_STATE_SWEEP); + incremental_gc_until(mrb, GC_STATE_SWEEP); change_gen_gc_mode(mrb, TRUE); - gc_assert(mrb->gc_full == FALSE); - gc_assert(mrb->gc_state == GC_STATE_NONE); + mrb_assert(mrb->gc_full == FALSE); + mrb_assert(mrb->gc_state == GC_STATE_NONE); puts(" in minor"); - gc_assert(is_minor_gc(mrb)); - gc_assert(mrb->majorgc_old_threshold > 0); + mrb_assert(is_minor_gc(mrb)); + mrb_assert(mrb->majorgc_old_threshold > 0); mrb->majorgc_old_threshold = 0; mrb_incremental_gc(mrb); - gc_assert(mrb->gc_full == TRUE); - gc_assert(mrb->gc_state == GC_STATE_NONE); + mrb_assert(mrb->gc_full == TRUE); + mrb_assert(mrb->gc_state == GC_STATE_NONE); puts(" in major"); - gc_assert(is_major_gc(mrb)); + mrb_assert(is_major_gc(mrb)); do { mrb_incremental_gc(mrb); } while (mrb->gc_state != GC_STATE_NONE); - gc_assert(mrb->gc_full == FALSE); + mrb_assert(mrb->gc_full == FALSE); mrb_close(mrb); } @@ -1517,12 +1552,12 @@ test_incremental_sweep_phase(void) add_heap(mrb); mrb->sweeps = mrb->heaps; - gc_assert(mrb->heaps->next->next == NULL); - gc_assert(mrb->free_heaps->next->next == NULL); + mrb_assert(mrb->heaps->next->next == NULL); + mrb_assert(mrb->free_heaps->next->next == NULL); incremental_sweep_phase(mrb, MRB_HEAP_PAGE_SIZE*3); - gc_assert(mrb->heaps->next == NULL); - gc_assert(mrb->heaps == mrb->free_heaps); + mrb_assert(mrb->heaps->next == NULL); + mrb_assert(mrb->heaps == mrb->free_heaps); mrb_close(mrb); } diff --git a/src/kernel.c b/src/kernel.c index ae8d24710..ce3aa89f0 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -549,15 +549,40 @@ obj_is_instance_of(mrb_state *mrb, mrb_value self) } static void -check_iv_name(mrb_state *mrb, mrb_sym id) +valid_iv_name(mrb_state *mrb, mrb_sym iv_name_id, const char* s, size_t len) +{ + if (len < 2 || !(s[0] == '@' && s[1] != '@')) { + mrb_name_error(mrb, iv_name_id, "`%S' is not allowed as an instance variable name", mrb_sym2str(mrb, iv_name_id)); + } +} + +static void +check_iv_name(mrb_state *mrb, mrb_sym iv_name_id) { const char *s; size_t len; - s = mrb_sym2name_len(mrb, id, &len); - if (len < 2 || !(s[0] == '@' && s[1] != '@')) { - mrb_name_error(mrb, id, "`%S' is not allowed as an instance variable name", mrb_sym2str(mrb, id)); + s = mrb_sym2name_len(mrb, iv_name_id, &len); + valid_iv_name(mrb, iv_name_id, s, len); +} + +static mrb_sym +get_valid_iv_sym(mrb_state *mrb, mrb_value iv_name) +{ + mrb_sym iv_name_id; + + mrb_assert(mrb_symbol_p(iv_name) || mrb_string_p(iv_name)); + + if (mrb_string_p(iv_name)) { + iv_name_id = mrb_intern_cstr(mrb, RSTRING_PTR(iv_name)); + valid_iv_name(mrb, iv_name_id, RSTRING_PTR(iv_name), RSTRING_LEN(iv_name)); + } + else { + iv_name_id = mrb_symbol(iv_name); + check_iv_name(mrb, iv_name_id); } + + return iv_name_id; } /* 15.3.1.3.20 */ @@ -582,10 +607,11 @@ mrb_value mrb_obj_ivar_defined(mrb_state *mrb, mrb_value self) { mrb_sym mid; + mrb_value sym; mrb_bool defined_p; - mrb_get_args(mrb, "n", &mid); - check_iv_name(mrb, mid); + mrb_get_args(mrb, "o", &sym); + mid = get_valid_iv_sym(mrb, sym); defined_p = mrb_obj_iv_defined(mrb, mrb_obj_ptr(self), mid); return mrb_bool_value(defined_p); @@ -614,12 +640,13 @@ mrb_obj_ivar_defined(mrb_state *mrb, mrb_value self) mrb_value mrb_obj_ivar_get(mrb_state *mrb, mrb_value self) { - mrb_sym id; + mrb_sym iv_name_id; + mrb_value iv_name; - mrb_get_args(mrb, "n", &id); + mrb_get_args(mrb, "o", &iv_name); - check_iv_name(mrb, id); - return mrb_iv_get(mrb, self, id); + iv_name_id = get_valid_iv_sym(mrb, iv_name); + return mrb_iv_get(mrb, self, iv_name_id); } /* 15.3.1.3.22 */ @@ -645,12 +672,13 @@ mrb_obj_ivar_get(mrb_state *mrb, mrb_value self) mrb_value mrb_obj_ivar_set(mrb_state *mrb, mrb_value self) { - mrb_sym id; - mrb_value val; + mrb_sym iv_name_id; + mrb_value iv_name, val; + + mrb_get_args(mrb, "oo", &iv_name, &val); - mrb_get_args(mrb, "no", &id, &val); - check_iv_name(mrb, id); - mrb_iv_set(mrb, self, id, val); + iv_name_id = get_valid_iv_sym(mrb, iv_name); + mrb_iv_set(mrb, self, iv_name_id, val); return val; } diff --git a/src/numeric.c b/src/numeric.c index e1543c089..c309abf89 100644 --- a/src/numeric.c +++ b/src/numeric.c @@ -4,7 +4,6 @@ ** See Copyright Notice in mruby.h */ -#include <assert.h> #include <float.h> #if defined(__FreeBSD__) && __FreeBSD__ < 4 # include <floatingpoint.h> @@ -201,7 +200,7 @@ mrb_flo_to_str(mrb_state *mrb, mrb_value flo, int max_digit) *(c++) = '-'; } - exp = log10(n); + exp = (int)log10(n); if ((exp < 0 ? -exp : exp) > max_digit) { /* exponent representation */ @@ -224,7 +223,7 @@ mrb_flo_to_str(mrb_state *mrb, mrb_value flo, int max_digit) /* puts digits */ while (max_digit >= 0) { mrb_float weight = pow(10.0, m); - digit = floor(n / weight + FLT_EPSILON); + digit = (int)floor(n / weight + FLT_EPSILON); *(c++) = '0' + digit; n -= (digit * weight); max_digit--; @@ -240,7 +239,8 @@ mrb_flo_to_str(mrb_state *mrb, mrb_value flo, int max_digit) *(c++) = 'e'; if (exp > 0) { *(c++) = '+'; - } else { + } + else { *(c++) = '-'; exp = -exp; } @@ -781,7 +781,7 @@ fixdivmod(mrb_state *mrb, mrb_int x, mrb_int y, mrb_int *divp, mrb_int *modp) { mrb_int div, mod; - /* TODO: add assert(y != 0) to make sure */ + /* TODO: add mrb_assert(y != 0) to make sure */ if (y < 0) { if (x < 0) @@ -1253,12 +1253,14 @@ mrb_fixnum_to_str(mrb_state *mrb, mrb_value x, int base) if (val == 0) { *--b = '0'; - } else if (val < 0) { + } + else if (val < 0) { do { *--b = mrb_digitmap[-(val % base)]; } while (val /= base); *--b = '-'; - } else { + } + else { do { *--b = mrb_digitmap[(int)(val % base)]; } while (val /= base); diff --git a/src/object.c b/src/object.c index 5b2278767..363f474f7 100644 --- a/src/object.c +++ b/src/object.c @@ -29,7 +29,7 @@ mrb_obj_eq(mrb_state *mrb, mrb_value v1, mrb_value v2) return (mrb_float(v1) == mrb_float(v2)); default: - return (v1.value.p == v2.value.p); + return (mrb_ptr(v1) == mrb_ptr(v2)); } } diff --git a/src/parse.y b/src/parse.y index fda66e92c..bbad88949 100644 --- a/src/parse.y +++ b/src/parse.y @@ -1,6 +1,6 @@ /* ** parse.y - mruby parser -** +** ** See Copyright Notice in mruby.h */ @@ -272,7 +272,7 @@ new_scope(parser_state *p, node *body) static node* new_begin(parser_state *p, node *body) { - if (body) + if (body) return list2((node*)NODE_BEGIN, body); return cons((node*)NODE_BEGIN, 0); } @@ -857,6 +857,7 @@ ret_args(parser_state *p, node *n) { if (n->cdr) { yyerror(p, "block argument should not be given"); + return NULL; } if (!n->car->cdr) return n->car->car; return new_array(p, n->car); @@ -909,7 +910,7 @@ parsing_heredoc_inf(parser_state *p) node *nd = p->parsing_heredoc; if (nd == NULL) return NULL; - /* assert(nd->car->car == NODE_HEREDOC); */ + /* mrb_assert(nd->car->car == NODE_HEREDOC); */ return (parser_heredoc_info*)nd->car->cdr; } @@ -933,7 +934,7 @@ heredoc_end(parser_state *p) %} -%pure_parser +%pure-parser %parse-param {parser_state *p} %lex-param {parser_state *p} @@ -2744,7 +2745,7 @@ var_ref : variable { $$ = var_reference(p, $1); } - | keyword_nil + | keyword_nil { $$ = new_nil(p); } @@ -3408,7 +3409,7 @@ scan_oct(const int *start, int len, int *retlen) const int *s = start; int retval = 0; - /* assert(len <= 3) */ + /* mrb_assert(len <= 3) */ while (len-- && *s >= '0' && *s <= '7') { retval <<= 3; retval |= *s++ - '0'; @@ -3426,7 +3427,7 @@ scan_hex(const int *start, int len, int *retlen) register int retval = 0; char *tmp; - /* assert(len <= 2) */ + /* mrb_assert(len <= 2) */ while (len-- && *s && (tmp = (char*)strchr(hexdigit, *s))) { retval <<= 4; retval |= (tmp - hexdigit) & 15; @@ -3686,7 +3687,7 @@ parse_string(parser_state *p) tokadd(p, c); - } + } tokfix(p); p->lstate = EXPR_END; @@ -3701,10 +3702,12 @@ parse_string(parser_state *p) int f = 0; int c; char *s = strndup(tok(p), toklen(p)); - char flag[4] = { '\0' }; + char flags[3]; + char *flag = flags; + char *dup; newtok(p); - while (c = nextc(p), ISALPHA(c)) { + while (c = nextc(p), c != -1 && ISALPHA(c)) { switch (c) { case 'i': f |= 1; break; case 'x': f |= 2; break; @@ -3720,10 +3723,16 @@ parse_string(parser_state *p) toklen(p) > 1 ? "s" : "", tok(p)); yyerror(p, msg); } - if (f & 1) strcat(flag, "i"); - if (f & 2) strcat(flag, "x"); - if (f & 4) strcat(flag, "m"); - yylval.nd = new_regx(p, s, strdup(flag)); + if (f != 0) { + if (f & 1) *flag++ = 'i'; + if (f & 2) *flag++ = 'x'; + if (f & 4) *flag++ = 'm'; + dup = strndup(flags, (size_t)(flag - flags)); + } + else { + dup = NULL; + } + yylval.nd = new_regx(p, s, dup); return tREGEXP; } @@ -3731,7 +3740,7 @@ parse_string(parser_state *p) yylval.nd = new_str(p, tok(p), toklen(p)); return tSTRING; } - + static int heredoc_identifier(parser_state *p) @@ -4813,7 +4822,7 @@ parser_yylex(parser_state *p) pushback(p, c); if (last_state == EXPR_FNAME) goto gvar; tokfix(p); - yylval.nd = new_nth_ref(p, atoi(tok(p))); + yylval.nd = new_nth_ref(p, atoi(tok(p))); return tNTH_REF; default: @@ -5159,7 +5168,6 @@ mrbc_filename(mrb_state *mrb, mrbc_context *c, const char *s) memcpy(p, s, len + 1); c->filename = p; - c->lineno = 1; } return c->filename; } @@ -5176,7 +5184,7 @@ parser_state* mrb_parse_file(mrb_state *mrb, FILE *f, mrbc_context *c) { parser_state *p; - + p = mrb_parser_new(mrb); if (!p) return 0; p->s = p->send = NULL; @@ -5210,6 +5218,8 @@ mrb_parse_string(mrb_state *mrb, const char *s, mrbc_context *c) static mrb_value load_exec(mrb_state *mrb, parser_state *p, mrbc_context *c) { + struct RClass *target = mrb->object_class; + struct RProc *proc; int n; mrb_value v; @@ -5243,8 +5253,16 @@ load_exec(mrb_state *mrb, parser_state *p, mrbc_context *c) if (c) { if (c->dump_result) codedump_all(mrb, n); if (c->no_exec) return mrb_fixnum_value(n); + if (c->target_class) { + target = c->target_class; + } + } + proc = mrb_proc_new(mrb, mrb->irep[n]); + proc->target_class = target; + if (mrb->c->ci) { + mrb->c->ci->target_class = target; } - v = mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb)); + v = mrb_run(mrb, proc, mrb_top_self(mrb)); if (mrb->exc) return mrb_nil_value(); return v; } @@ -5561,7 +5579,7 @@ parser_dump(mrb_state *mrb, node *tree, int offset) printf("NODE_CALL:\n"); parser_dump(mrb, tree->car, offset+1); dump_prefix(offset+1); - printf("method='%s' (%d)\n", + printf("method='%s' (%d)\n", mrb_sym2name(mrb, sym(tree->cdr->car)), (int)(intptr_t)tree->cdr->car); tree = tree->cdr->cdr->car; diff --git a/src/range.c b/src/range.c index 6e313df51..a27ced4cb 100644 --- a/src/range.c +++ b/src/range.c @@ -315,29 +315,6 @@ range_to_s(mrb_state *mrb, mrb_value range) return str; } -static mrb_value -inspect_range(mrb_state *mrb, mrb_value range, int recur) -{ - mrb_value str, str2; - struct RRange *r = mrb_range_ptr(range); - - if (recur) { - static const char s[2][14] = { "(... ... ...)", "(... .. ...)" }; - static const int n[] = { 13, 12 }; - int idx; - - idx = (r->excl) ? 0 : 1; - return mrb_str_new(mrb, s[idx], n[idx]); - } - str = mrb_inspect(mrb, r->edges->beg); - str2 = mrb_inspect(mrb, r->edges->end); - str = mrb_str_dup(mrb, str); - mrb_str_cat(mrb, str, "...", r->excl ? 3 : 2); - mrb_str_append(mrb, str, str2); - - return str; -} - /* 15.2.14.4.13(x) */ /* * call-seq: @@ -351,7 +328,16 @@ inspect_range(mrb_state *mrb, mrb_value range, int recur) static mrb_value range_inspect(mrb_state *mrb, mrb_value range) { - return inspect_range(mrb, range, 0); + mrb_value str, str2; + struct RRange *r = mrb_range_ptr(range); + + str = mrb_inspect(mrb, r->edges->beg); + str2 = mrb_inspect(mrb, r->edges->end); + str = mrb_str_dup(mrb, str); + mrb_str_cat(mrb, str, "...", r->excl ? 3 : 2); + mrb_str_append(mrb, str, str2); + + return str; } /* 15.2.14.4.14(x) */ diff --git a/src/state.c b/src/state.c index 34de1e45e..5dcc4a40a 100644 --- a/src/state.c +++ b/src/state.c @@ -21,10 +21,6 @@ inspect_main(mrb_state *mrb, mrb_value mod) return mrb_str_new(mrb, "main", 4); } -#ifdef MRB_NAN_BOXING -#include <assert.h> -#endif - mrb_state* mrb_open_allocf(mrb_allocf f, void *ud) { @@ -33,7 +29,7 @@ mrb_open_allocf(mrb_allocf f, void *ud) mrb_state *mrb; #ifdef MRB_NAN_BOXING - assert(sizeof(void*) == 4); + mrb_assert(sizeof(void*) == 4); #endif mrb = (mrb_state *)(f)(NULL, NULL, sizeof(mrb_state), ud); @@ -192,7 +188,7 @@ mrb_value mrb_top_self(mrb_state *mrb) { if (!mrb->top_self) { - mrb->top_self = (struct RObject*)mrb_obj_alloc(mrb, MRB_TT_OBJECT, mrb->object_class); + mrb->top_self = (struct RObject*)mrb_obj_alloc(mrb, MRB_TT_OBJECT, mrb->object_class); mrb_define_singleton_method(mrb, mrb->top_self, "inspect", inspect_main, MRB_ARGS_NONE()); mrb_define_singleton_method(mrb, mrb->top_self, "to_s", inspect_main, MRB_ARGS_NONE()); } diff --git a/src/string.c b/src/string.c index 975f6cdb9..88dcfc63e 100644 --- a/src/string.c +++ b/src/string.c @@ -62,6 +62,7 @@ mrb_str_modify(mrb_state *mrb, struct RString *s) if (shared->refcnt == 1 && s->ptr == shared->ptr) { s->ptr = shared->ptr; s->aux.capa = shared->len; + s->ptr[s->len] = '\0'; mrb_free(mrb, shared); } else { @@ -2347,47 +2348,47 @@ mrb_str_dump(mrb_state *mrb, mrb_value str) *q++ = '\\'; *q++ = 'n'; break; - + case '\r': *q++ = '\\'; *q++ = 'r'; break; - + case '\t': *q++ = '\\'; *q++ = 't'; break; - + case '\f': *q++ = '\\'; *q++ = 'f'; break; - + case '\013': *q++ = '\\'; *q++ = 'v'; break; - + case '\010': *q++ = '\\'; *q++ = 'b'; break; - + case '\007': *q++ = '\\'; *q++ = 'a'; break; - + case '\033': *q++ = '\\'; *q++ = 'e'; break; - + case '#': if (IS_EVSTR(p, pend)) *q++ = '\\'; *q++ = '#'; break; - + default: if (ISPRINT(c)) { *q++ = c; @@ -2527,14 +2528,13 @@ mrb_init_string(mrb_state *mrb) MRB_SET_INSTANCE_TT(s, MRB_TT_STRING); mrb_include_module(mrb, s, mrb_class_get(mrb, "Comparable")); - mrb_define_method(mrb, s, "+", mrb_str_plus_m, MRB_ARGS_REQ(1)); /* 15.2.10.5.2 */ + mrb_define_method(mrb, s, "bytesize", mrb_str_bytesize, MRB_ARGS_NONE()); - mrb_define_method(mrb, s, "size", mrb_str_size, MRB_ARGS_NONE()); /* 15.2.10.5.33 */ - mrb_define_method(mrb, s, "length", mrb_str_size, MRB_ARGS_NONE()); /* 15.2.10.5.26 */ - mrb_define_method(mrb, s, "*", mrb_str_times, MRB_ARGS_REQ(1)); /* 15.2.10.5.1 */ - mrb_define_method(mrb, s, "<=>", mrb_str_cmp_m, MRB_ARGS_REQ(1)); /* 15.2.10.5.3 */ - mrb_define_method(mrb, s, "==", mrb_str_equal_m, MRB_ARGS_REQ(1)); /* 15.2.10.5.4 */ - mrb_define_method(mrb, s, "=~", noregexp, MRB_ARGS_REQ(1)); /* 15.2.10.5.5 */ + + mrb_define_method(mrb, s, "<=>", mrb_str_cmp_m, MRB_ARGS_REQ(1)); /* 15.2.10.5.1 */ + mrb_define_method(mrb, s, "==", mrb_str_equal_m, MRB_ARGS_REQ(1)); /* 15.2.10.5.2 */ + mrb_define_method(mrb, s, "+", mrb_str_plus_m, MRB_ARGS_REQ(1)); /* 15.2.10.5.4 */ + mrb_define_method(mrb, s, "*", mrb_str_times, MRB_ARGS_REQ(1)); /* 15.2.10.5.5 */ mrb_define_method(mrb, s, "[]", mrb_str_aref_m, MRB_ARGS_ANY()); /* 15.2.10.5.6 */ mrb_define_method(mrb, s, "capitalize", mrb_str_capitalize, MRB_ARGS_NONE()); /* 15.2.10.5.7 */ mrb_define_method(mrb, s, "capitalize!", mrb_str_capitalize_bang, MRB_ARGS_REQ(1)); /* 15.2.10.5.8 */ @@ -2547,31 +2547,25 @@ mrb_init_string(mrb_state *mrb) mrb_define_method(mrb, s, "empty?", mrb_str_empty_p, MRB_ARGS_NONE()); /* 15.2.10.5.16 */ mrb_define_method(mrb, s, "eql?", mrb_str_eql, MRB_ARGS_REQ(1)); /* 15.2.10.5.17 */ - // NOTE: Regexp not implemented - mrb_define_method(mrb, s, "gsub", noregexp, MRB_ARGS_REQ(1)); /* 15.2.10.5.18 */ - mrb_define_method(mrb, s, "gsub!", noregexp, MRB_ARGS_REQ(1)); /* 15.2.10.5.19 */ - mrb_define_method(mrb, s, "hash", mrb_str_hash_m, MRB_ARGS_REQ(1)); /* 15.2.10.5.20 */ mrb_define_method(mrb, s, "include?", mrb_str_include, MRB_ARGS_REQ(1)); /* 15.2.10.5.21 */ mrb_define_method(mrb, s, "index", mrb_str_index_m, MRB_ARGS_ANY()); /* 15.2.10.5.22 */ mrb_define_method(mrb, s, "initialize", mrb_str_init, MRB_ARGS_REQ(1)); /* 15.2.10.5.23 */ mrb_define_method(mrb, s, "initialize_copy", mrb_str_replace, MRB_ARGS_REQ(1)); /* 15.2.10.5.24 */ mrb_define_method(mrb, s, "intern", mrb_str_intern, MRB_ARGS_NONE()); /* 15.2.10.5.25 */ - mrb_define_method(mrb, s, "match", noregexp, MRB_ARGS_REQ(1)); /* 15.2.10.5.27 */ + mrb_define_method(mrb, s, "length", mrb_str_size, MRB_ARGS_NONE()); /* 15.2.10.5.26 */ mrb_define_method(mrb, s, "replace", mrb_str_replace, MRB_ARGS_REQ(1)); /* 15.2.10.5.28 */ mrb_define_method(mrb, s, "reverse", mrb_str_reverse, MRB_ARGS_NONE()); /* 15.2.10.5.29 */ mrb_define_method(mrb, s, "reverse!", mrb_str_reverse_bang, MRB_ARGS_NONE()); /* 15.2.10.5.30 */ mrb_define_method(mrb, s, "rindex", mrb_str_rindex_m, MRB_ARGS_ANY()); /* 15.2.10.5.31 */ - mrb_define_method(mrb, s, "scan", noregexp, MRB_ARGS_REQ(1)); /* 15.2.10.5.32 */ + mrb_define_method(mrb, s, "size", mrb_str_size, MRB_ARGS_NONE()); /* 15.2.10.5.33 */ mrb_define_method(mrb, s, "slice", mrb_str_aref_m, MRB_ARGS_ANY()); /* 15.2.10.5.34 */ mrb_define_method(mrb, s, "split", mrb_str_split_m, MRB_ARGS_ANY()); /* 15.2.10.5.35 */ - mrb_define_method(mrb, s, "sub", noregexp, MRB_ARGS_REQ(1)); /* 15.2.10.5.36 */ - mrb_define_method(mrb, s, "sub!", noregexp, MRB_ARGS_REQ(1)); /* 15.2.10.5.37 */ - mrb_define_method(mrb, s, "to_i", mrb_str_to_i, MRB_ARGS_ANY()); /* 15.2.10.5.38 */ - mrb_define_method(mrb, s, "to_f", mrb_str_to_f, MRB_ARGS_NONE()); /* 15.2.10.5.39 */ + mrb_define_method(mrb, s, "to_f", mrb_str_to_f, MRB_ARGS_NONE()); /* 15.2.10.5.38 */ + mrb_define_method(mrb, s, "to_i", mrb_str_to_i, MRB_ARGS_ANY()); /* 15.2.10.5.39 */ mrb_define_method(mrb, s, "to_s", mrb_str_to_s, MRB_ARGS_NONE()); /* 15.2.10.5.40 */ - mrb_define_method(mrb, s, "to_str", mrb_str_to_s, MRB_ARGS_NONE()); /* 15.2.10.5.40 */ + mrb_define_method(mrb, s, "to_str", mrb_str_to_s, MRB_ARGS_NONE()); mrb_define_method(mrb, s, "to_sym", mrb_str_intern, MRB_ARGS_NONE()); /* 15.2.10.5.41 */ mrb_define_method(mrb, s, "upcase", mrb_str_upcase, MRB_ARGS_REQ(1)); /* 15.2.10.5.42 */ mrb_define_method(mrb, s, "upcase!", mrb_str_upcase_bang, MRB_ARGS_REQ(1)); /* 15.2.10.5.43 */ diff --git a/src/symbol.c b/src/symbol.c index bd8dce7df..e49462e27 100644 --- a/src/symbol.c +++ b/src/symbol.c @@ -452,5 +452,4 @@ mrb_init_symbol(mrb_state *mrb) mrb_define_method(mrb, sym, "to_sym", sym_to_sym, MRB_ARGS_NONE()); /* 15.2.11.3.4 */ mrb_define_method(mrb, sym, "inspect", sym_inspect, MRB_ARGS_NONE()); /* 15.2.11.3.5(x) */ mrb_define_method(mrb, sym, "<=>", sym_cmp, MRB_ARGS_REQ(1)); - mrb->init_sym = mrb_intern2(mrb, "initialize", 10); } diff --git a/src/variable.c b/src/variable.c index 52ffb42e7..f2a474201 100644 --- a/src/variable.c +++ b/src/variable.c @@ -35,7 +35,7 @@ typedef struct iv_tbl { } iv_tbl; /* - * Creates instance variable table. + * Creates the instance variable table. * * Parameters * mrb @@ -118,7 +118,6 @@ iv_put(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value val) else { t->rootseg = seg; } - return; } /* @@ -128,7 +127,7 @@ iv_put(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value val) * mrb * t the variable table to be searched. * sym the symbol to be used as the key. - * vp the value pointer. Recieves the value if if the specified symbol contains + * vp the value pointer. Recieves the value if the specified symbol contains * in the instance variable table. * Returns * true if the specfiyed symbol contains in the instance variable table. @@ -1,4 +1,4 @@ -/* + /* ** vm.c - virtual machine for mruby ** ** See Copyright Notice in mruby.h @@ -92,12 +92,12 @@ stack_init(mrb_state *mrb) { struct mrb_context *c = mrb->c; - /* assert(mrb->stack == NULL); */ + /* mrb_assert(mrb->stack == NULL); */ c->stbase = (mrb_value *)mrb_calloc(mrb, STACK_INIT_SIZE, sizeof(mrb_value)); c->stend = c->stbase + STACK_INIT_SIZE; c->stack = c->stbase; - /* assert(ci == NULL); */ + /* mrb_assert(ci == NULL); */ c->cibase = (mrb_callinfo *)mrb_calloc(mrb, CALLINFO_INIT_SIZE, sizeof(mrb_callinfo)); c->ciend = c->cibase + CALLINFO_INIT_SIZE; c->ci = c->cibase; @@ -205,6 +205,9 @@ top_env(mrb_state *mrb, struct RProc *proc) return e; } +#define CI_ACC_SKIP -1 +#define CI_ACC_DIRECT -2 + static mrb_callinfo* cipush(mrb_state *mrb) { @@ -260,7 +263,7 @@ ecall(mrb_state *mrb, int i) ci = cipush(mrb); ci->stackidx = mrb->c->stack - mrb->c->stbase; ci->mid = ci[-1].mid; - ci->acc = -1; + ci->acc = CI_ACC_SKIP; ci->argc = 0; ci->proc = p; ci->nregs = p->body.irep->nregs; @@ -362,14 +365,13 @@ mrb_funcall_with_block(mrb_state *mrb, mrb_value self, mrb_sym mid, int argc, mr ci->proc = p; ci->stackidx = mrb->c->stack - mrb->c->stbase; ci->argc = argc; - ci->target_class = p->target_class; + ci->target_class = c; if (MRB_PROC_CFUNC_P(p)) { ci->nregs = argc + 2; } else { ci->nregs = p->body.irep->nregs + n; } - ci->acc = -1; mrb->c->stack = mrb->c->stack + n; stack_extend(mrb, ci->nregs, 0); @@ -385,12 +387,15 @@ mrb_funcall_with_block(mrb_state *mrb, mrb_value self, mrb_sym mid, int argc, mr if (MRB_PROC_CFUNC_P(p)) { int ai = mrb_gc_arena_save(mrb); + + ci->acc = CI_ACC_DIRECT; val = p->body.func(mrb, self); - mrb_gc_arena_restore(mrb, ai); mrb->c->stack = mrb->c->stbase + mrb->c->ci->stackidx; cipop(mrb); + mrb_gc_arena_restore(mrb, ai); } else { + ci->acc = CI_ACC_SKIP; val = mrb_run(mrb, p, self); } } @@ -429,7 +434,7 @@ mrb_yield_internal(mrb_state *mrb, mrb_value b, int argc, mrb_value *argv, mrb_v else { ci->nregs = p->body.irep->nregs + 1; } - ci->acc = -1; + ci->acc = CI_ACC_SKIP; mrb->c->stack = mrb->c->stack + n; stack_extend(mrb, ci->nregs, 0); @@ -544,7 +549,7 @@ void mrb_gv_val_set(mrb_state *mrb, mrb_sym sym, mrb_value val); mrb_value mrb_run(mrb_state *mrb, struct RProc *proc, mrb_value self) { - /* assert(mrb_proc_cfunc_p(proc)) */ + /* mrb_assert(mrb_proc_cfunc_p(proc)) */ mrb_irep *irep = proc->body.irep; mrb_code *pc = irep->iseq; mrb_value *pool = irep->pool; @@ -897,12 +902,7 @@ mrb_run(mrb_state *mrb, struct RProc *proc, mrb_value self) else { ci->argc = n; } - if (c->tt == MRB_TT_ICLASS) { - ci->target_class = c->c; - } - else { - ci->target_class = c; - } + ci->target_class = c; ci->pc = pc + 1; ci->acc = a; @@ -1054,7 +1054,7 @@ mrb_run(mrb_state *mrb, struct RProc *proc, mrb_value self) else { ci->argc = n; } - ci->target_class = m->target_class; + ci->target_class = c; ci->pc = pc + 1; /* prepare stack */ @@ -1259,9 +1259,9 @@ mrb_run(mrb_state *mrb, struct RProc *proc, mrb_value self) cipop(mrb); ci = mrb->c->ci; mrb->c->stack = mrb->c->stbase + ci[1].stackidx; - if (ci[1].acc < 0 && prev_jmp) { + if (ci[1].acc == CI_ACC_SKIP && prev_jmp) { mrb->jmp = prev_jmp; - longjmp(*(jmp_buf*)mrb->jmp, 1); + mrb_longjmp(mrb); } while (eidx > ci->eidx) { ecall(mrb, --eidx); @@ -1339,7 +1339,7 @@ mrb_run(mrb_state *mrb, struct RProc *proc, mrb_value self) acc = ci->acc; pc = ci->pc; regs = mrb->c->stack = mrb->c->stbase + ci->stackidx; - if (acc < 0) { + if (acc == CI_ACC_SKIP) { mrb->jmp = prev_jmp; return v; } @@ -1384,7 +1384,7 @@ mrb_run(mrb_state *mrb, struct RProc *proc, mrb_value self) /* replace callinfo */ ci = mrb->c->ci; ci->mid = mid; - ci->target_class = m->target_class; + ci->target_class = c; if (n == CALL_MAXARGS) { ci->argc = -1; } @@ -2127,3 +2127,9 @@ mrb_run(mrb_state *mrb, struct RProc *proc, mrb_value self) } END_DISPATCH; } + +void +mrb_longjmp(mrb_state *mrb) +{ + longjmp(*(jmp_buf*)mrb->jmp, 1); +} diff --git a/tasks/mrbgem_spec.rake b/tasks/mrbgem_spec.rake index 6ff4286ce..94f24af26 100644 --- a/tasks/mrbgem_spec.rake +++ b/tasks/mrbgem_spec.rake @@ -275,13 +275,20 @@ module MRuby req_versions = dep[:requirements] # check each GEM dependency against all available GEMs + found_dep_gem = false each do |dep_g| if name == dep_g.name unless dep_g.version_ok?(req_versions) fail "#{name} version should be #{req_versions.join(' and ')} but was '#{dep_g.version}'" end + + found_dep_gem = true + break end end + + fail "The GEM '#{g.name}' depends on the GEM '#{name}' but it could not be found" unless found_dep_gem + end end end diff --git a/tasks/mrbgems.rake b/tasks/mrbgems.rake index 5f156c45a..d8f56f88e 100644 --- a/tasks/mrbgems.rake +++ b/tasks/mrbgems.rake @@ -74,9 +74,10 @@ please check the following additional licenses too: GEMS_LEGAL gems.map do |g| + authors = [g.authors].flatten.sort.join(", ") f.puts f.puts "GEM: #{g.name}" - f.puts "Copyright (c) #{Time.now.year} #{g.authors}" + f.puts "Copyright (c) #{Time.now.year} #{authors}" f.puts "License: #{g.licenses}" end end diff --git a/tasks/mrbgems_test.rake b/tasks/mrbgems_test.rake index 717f0d76b..1050b84bb 100644 --- a/tasks/mrbgems_test.rake +++ b/tasks/mrbgems_test.rake @@ -38,7 +38,7 @@ MRuby.each_target do f.puts %Q[ }] f.puts %Q[ mrb_const_set(mrb2, mrb_obj_value(mrb2->object_class), mrb_intern(mrb2, "GEMNAME"), mrb_str_new(mrb2, "#{g.name}", #{g.name.length}));] - if not g.test_args.empty? + unless g.test_args.empty? f.puts %Q[ test_args_hash = mrb_hash_new_capa(mrb, #{g.test_args.length}); ] g.test_args.each do |arg_name, arg_value| escaped_arg_name = arg_name.gsub('\\', '\\\\\\\\').gsub('"', '\"') diff --git a/tasks/mruby_build.rake b/tasks/mruby_build.rake index ef7f06a81..ff6110884 100644 --- a/tasks/mruby_build.rake +++ b/tasks/mruby_build.rake @@ -43,7 +43,7 @@ module MRuby end include Rake::DSL include LoadGems - attr_accessor :name, :bins, :exts, :file_separator, :build_dir + attr_accessor :name, :bins, :exts, :file_separator, :build_dir, :gem_clone_dir attr_reader :libmruby, :gems COMPILERS = %w(cc cxx objc asm) @@ -64,6 +64,7 @@ module MRuby @file_separator = '/' @build_dir = "#{MRUBY_ROOT}/build/#{@name}" + @gem_clone_dir = "#{MRUBY_ROOT}/build/mrbgems" @cc = Command::Compiler.new(self, %w(.c)) @cxx = Command::Compiler.new(self, %w(.cc .cxx .cpp)) @objc = Command::Compiler.new(self, %w(.m)) @@ -194,8 +195,8 @@ module MRuby attr_block %w(test_runner) def initialize(name, &block) - @test_runner = Command::CrossTestRunner.new(self) - super + @test_runner = Command::CrossTestRunner.new(self) + super end def run_test diff --git a/tasks/mruby_build_commands.rake b/tasks/mruby_build_commands.rake index b678b1f0e..349b8717d 100644 --- a/tasks/mruby_build_commands.rake +++ b/tasks/mruby_build_commands.rake @@ -259,9 +259,14 @@ module MRuby infiles.each do |f| _pp "MRBC", f.relative_path, nil, :indent => 2 end - IO.popen("#{filename @command} #{@compile_options % {:funcname => funcname}} #{infiles.join(' ')}", 'r+') do |io| + IO.popen("#{filename @command} #{@compile_options % {:funcname => funcname}} #{filename(infiles).join(' ')}", 'r+') do |io| out.puts io.read end + # if mrbc execution fail, drop the file + unless $?.exitstatus + File.delete(out.path) + exit -1 + end end end diff --git a/tasks/mruby_build_gem.rake b/tasks/mruby_build_gem.rake index 24aea71ab..39be984ad 100644 --- a/tasks/mruby_build_gem.rake +++ b/tasks/mruby_build_gem.rake @@ -39,7 +39,7 @@ module MRuby gemdir = "#{root}/mrbgems/#{params[:core]}" elsif params[:git] url = params[:git] - gemdir = "#{build_dir}/mrbgems/#{url.match(/([-\w]+)(\.[-\w]+|)$/).to_a[1]}" + gemdir = "#{gem_clone_dir}/#{url.match(/([-\w]+)(\.[-\w]+|)$/).to_a[1]}" if File.exists?(gemdir) if $pull_gems @@ -50,7 +50,7 @@ module MRuby else options = [params[:options]] || [] options << "--branch \"#{params[:branch]}\"" if params[:branch] - FileUtils.mkdir_p "#{build_dir}/mrbgems" + FileUtils.mkdir_p "#{gem_clone_dir}" git.run_clone gemdir, url, options end else diff --git a/tasks/toolchains/androideabi.rake b/tasks/toolchains/androideabi.rake index 51897667e..61881ca31 100644 --- a/tasks/toolchains/androideabi.rake +++ b/tasks/toolchains/androideabi.rake @@ -44,8 +44,12 @@ MRuby::Toolchain.new(:androideabi) do |conf| case RUBY_PLATFORM when /cygwin|mswin|mingw|bccwin|wince|emx/i HOST_PLATFORM = 'windows' + when /x86_64-darwin/i + HOST_PLATFORM = 'darwin-x86_64' when /darwin/i HOST_PLATFORM = 'darwin-x86' + when /x86_64-linux/i + HOST_PLATFORM = 'linux-x86_64' when /linux/i HOST_PLATFORM = 'linux-x86' else diff --git a/test/assert.rb b/test/assert.rb index 98057331b..3d35fc267 100644 --- a/test/assert.rb +++ b/test/assert.rb @@ -101,7 +101,7 @@ end def assert_equal(arg1, arg2 = nil, arg3 = nil) if block_given? - exp, act, msg = yield, arg1, arg2 + exp, act, msg = arg1, yield, arg2 else exp, act, msg = arg1, arg2, arg3 end @@ -113,7 +113,7 @@ end def assert_not_equal(arg1, arg2 = nil, arg3 = nil) if block_given? - exp, act, msg = yield, arg1, arg2 + exp, act, msg = arg1, yield, arg2 else exp, act, msg = arg1, arg2, arg3 end diff --git a/test/t/argumenterror.rb b/test/t/argumenterror.rb index ac254afcd..c8d277cc5 100644 --- a/test/t/argumenterror.rb +++ b/test/t/argumenterror.rb @@ -11,11 +11,11 @@ assert('ArgumentError', '15.2.24') do e2 = e1 end - assert_equal(ArgumentError.class, Class) - assert_equal(e2.class, ArgumentError) + assert_equal(Class, ArgumentError.class) + assert_equal(ArgumentError, e2.class) end assert('ArgumentError superclass', '15.2.24.2') do - assert_equal(ArgumentError.superclass, StandardError) + assert_equal(StandardError, ArgumentError.superclass) end diff --git a/test/t/array.rb b/test/t/array.rb index 9878e2cbf..cb2415ffb 100644 --- a/test/t/array.rb +++ b/test/t/array.rb @@ -2,15 +2,19 @@ # Array ISO Test assert('Array', '15.2.12') do - assert_equal(Array.class, Class) + assert_equal(Class, Array.class) end assert('Array superclass', '15.2.12.2') do - assert_equal(Array.superclass, Object) + assert_equal(Object, Array.superclass) +end + +assert('Array inclueded modules', '15.2.12.3') do + assert_true(Array.include?(Enumerable)) end assert('Array.[]', '15.2.12.4.1') do - assert_equal(Array.[](1,2,3), [1, 2, 3]) + assert_equal([1, 2, 3], Array.[](1,2,3)) end assert('Array#*', '15.2.12.5.1') do @@ -18,16 +22,16 @@ assert('Array#*', '15.2.12.5.1') do # this will cause an exception due to the wrong argument [1].*(-1) end - assert_equal([1].*(3), [1, 1, 1]) - assert_equal([1].*(0), []) + assert_equal([1, 1, 1], [1].*(3)) + assert_equal([], [1].*(0)) end assert('Array#+', '15.2.12.5.2') do - assert_equal([1].+([1]), [1, 1]) + assert_equal([1, 1], [1].+([1])) end assert('Array#<<', '15.2.12.5.3') do - assert_equal([1].<<(1), [1, 1]) + assert_equal([1, 1], [1].<<(1)) end assert('Array#[]', '15.2.12.5.4') do @@ -41,7 +45,7 @@ assert('Array#[]', '15.2.12.5.4') do a.[](1,2,3) end - assert_equal([1,2,3].[](1), 2) + assert_equal(2, [1,2,3].[](1)) end assert('Array#[]=', '15.2.12.5.5') do @@ -55,44 +59,44 @@ assert('Array#[]=', '15.2.12.5.5') do a.[]=(1,2,3,4) end - assert_equal([1,2,3].[]=(1,4), 4) - assert_equal([1,2,3].[]=(1,2,3), 3) + assert_equal(4, [1,2,3].[]=(1,4)) + assert_equal(3, [1,2,3].[]=(1,2,3)) end assert('Array#clear', '15.2.12.5.6') do a = [1] a.clear - assert_equal(a, []) + assert_equal([], a) end assert('Array#collect!', '15.2.12.5.7') do a = [1,2,3] a.collect! { |i| i + i } - assert_equal(a, [2,4,6]) + assert_equal([2,4,6], a) end assert('Array#concat', '15.2.12.5.8') do - assert_equal([1, 2].concat([3, 4]), [1,2,3,4]) + assert_equal([1,2,3,4], [1, 2].concat([3, 4])) end assert('Array#delete_at', '15.2.12.5.9') do a = [1,2,3] a.delete_at(1) - assert_equal(a, [1,3]) + assert_equal([1,3], a) end assert('Array#each', '15.2.12.5.10') do a = [1,2,3] b = 0 a.each {|i| b += i} - assert_equal(b, 6) + assert_equal(6, b) end assert('Array#each_index', '15.2.12.5.11') do a = [1] b = nil a.each_index {|i| b = i} - assert_equal(b, 0) + assert_equal(0, b) end assert('Array#empty?', '15.2.12.5.12') do @@ -115,16 +119,16 @@ assert('Array#first', '15.2.12.5.13') do assert_nil([].first) b = [1,2,3] - assert_equal(b.first, 1) - assert_equal(b.first(0), []) - assert_equal(b.first(1), [1]) - assert_equal(b.first(4), [1,2,3]) + assert_equal(1, b.first) + assert_equal([], b.first(0)) + assert_equal([1], b.first(1)) + assert_equal([1,2,3], b.first(4)) end assert('Array#index', '15.2.12.5.14') do a = [1,2,3] - assert_equal(a.index(2), 1) + assert_equal(1, a.index(2)) end assert('Array#initialize', '15.2.12.5.15') do @@ -133,25 +137,25 @@ assert('Array#initialize', '15.2.12.5.15') do c = [].initialize(2, 1) d = [].initialize(2) {|i| i} - assert_equal(a, [nil]) - assert_equal(b, [nil,nil]) - assert_equal(c, [1,1]) - assert_equal(d, [0,1]) + assert_equal([nil], a) + assert_equal([nil,nil], b) + assert_equal([1,1], c) + assert_equal([0,1], d) end assert('Array#initialize_copy', '15.2.12.5.16') do a = [1,2,3] b = [].initialize_copy(a) - assert_equal(b, [1,2,3]) + assert_equal([1,2,3], b) end assert('Array#join', '15.2.12.5.17') do a = [1,2,3].join b = [1,2,3].join(',') - assert_equal(a, '123') - assert_equal(b, '1,2,3') + assert_equal('123', a) + assert_equal('1,2,3', b) end assert('Array#last', '15.2.12.5.18') do @@ -161,20 +165,20 @@ assert('Array#last', '15.2.12.5.18') do end a = [1,2,3] - assert_equal(a.last, 3) + assert_equal(3, a.last) assert_nil([].last) end assert('Array#length', '15.2.12.5.19') do a = [1,2,3] - assert_equal(a.length, 3) + assert_equal(3, a.length) end assert('Array#map!', '15.2.12.5.20') do a = [1,2,3] a.map! { |i| i + i } - assert_equal(a, [2,4,6]) + assert_equal([2,4,6], a) end assert('Array#pop', '15.2.12.5.21') do @@ -182,45 +186,45 @@ assert('Array#pop', '15.2.12.5.21') do b = a.pop assert_nil([].pop) - assert_equal(a, [1,2]) - assert_equal(b, 3) + assert_equal([1,2], a) + assert_equal(3, b) end assert('Array#push', '15.2.12.5.22') do a = [1,2,3] b = a.push(4) - assert_equal(a, [1,2,3,4]) - assert_equal(b, [1,2,3,4]) + assert_equal([1,2,3,4], a) + assert_equal([1,2,3,4], b) end assert('Array#replace', '15.2.12.5.23') do a = [1,2,3] b = [].replace(a) - assert_equal(b, [1,2,3]) + assert_equal([1,2,3], b) end assert('Array#reverse', '15.2.12.5.24') do a = [1,2,3] b = a.reverse - assert_equal(a, [1,2,3]) - assert_equal(b, [3,2,1]) + assert_equal([1,2,3], a) + assert_equal([3,2,1], b) end assert('Array#reverse!', '15.2.12.5.25') do a = [1,2,3] b = a.reverse! - assert_equal(a, [3,2,1]) - assert_equal(b, [3,2,1]) + assert_equal([3,2,1], a) + assert_equal([3,2,1], b) end assert('Array#rindex', '15.2.12.5.26') do a = [1,2,3] - assert_equal(a.rindex(2), 1) + assert_equal(1, a.rindex(2)) end assert('Array#shift', '15.2.12.5.27') do @@ -228,22 +232,22 @@ assert('Array#shift', '15.2.12.5.27') do b = a.shift assert_nil([].shift) - assert_equal(a, [2,3]) - assert_equal(b, 1) + assert_equal([2,3], a) + assert_equal(1, b) end assert('Array#size', '15.2.12.5.28') do a = [1,2,3] - assert_equal(a.size, 3) + assert_equal(3, a.size) end assert('Array#slice', '15.2.12.5.29') do a = "12345".slice(1, 3) b = a.slice(0) - assert_equal("#{b}:", "2:") - assert_equal([1,2,3].[](1), 2) + assert_equal("2:", "#{b}:") + assert_equal(2, [1,2,3].[](1)) end assert('Array#unshift', '15.2.12.5.30') do @@ -252,10 +256,10 @@ assert('Array#unshift', '15.2.12.5.30') do c = [2,3] d = c.unshift(0, 1) - assert_equal(a, [1,2,3]) - assert_equal(b, [1,2,3]) - assert_equal(c, [0,1,2,3]) - assert_equal(d, [0,1,2,3]) + assert_equal([1,2,3], a) + assert_equal([1,2,3], b) + assert_equal([0,1,2,3], c) + assert_equal([0,1,2,3], d) end assert('Array#to_s', '15.2.12.5.31 / 15.2.12.5.32') do @@ -263,8 +267,8 @@ assert('Array#to_s', '15.2.12.5.31 / 15.2.12.5.32') do r1 = a.to_s r2 = a.inspect - assert_equal(r1, r2) - assert_equal(r1, "[2, 3, 4, 5]") + assert_equal(r2, r1) + assert_equal("[2, 3, 4, 5]", r1) end assert('Array#==', '15.2.12.5.33') do @@ -293,9 +297,9 @@ assert('Array#<=>', '15.2.12.5.36') do r2 = [ 1, 2, 3, 4, 5, 6 ] <=> [ 1, 2 ] #=> +1 r3 = [ "a", "b", "c" ] <=> [ "a", "b", "c" ] #=> 0 - assert_equal(r1, -1) - assert_equal(r2, +1) - assert_equal(r3, 0) + assert_equal(-1, r1) + assert_equal(+1, r2) + assert_equal(0, r3) end # Not ISO specified diff --git a/test/t/basicobject.rb b/test/t/basicobject.rb index d4c344ca9..f33171266 100644 --- a/test/t/basicobject.rb +++ b/test/t/basicobject.rb @@ -2,7 +2,7 @@ # BasicObject assert('BasicObject') do - assert_equal(BasicObject.class, Class) + assert_equal(Class, BasicObject.class) end assert('BasicObject superclass') do diff --git a/test/t/bs_block.rb b/test/t/bs_block.rb index 47ecece86..91de9141d 100644 --- a/test/t/bs_block.rb +++ b/test/t/bs_block.rb @@ -14,7 +14,7 @@ assert('BS Block 1') do end assert('BS Block 2') do - assert_equal [1,2,3].find{|x| x == 2}, 2 + assert_equal 2, [1,2,3].find{|x| x == 2} end assert('BS Block 3') do @@ -24,7 +24,7 @@ assert('BS Block 3') do [1, 2, 3].each(&block) end end - assert_equal E.new.find {|x| x == 2 }, 2 + assert_equal 2, E.new.find {|x| x == 2 } end assert('BS Block 3') do @@ -32,7 +32,7 @@ assert('BS Block 3') do for x in [1, 2, 3] sum += x end - assert_equal sum, 6 + assert_equal 6, sum end assert('BS Block 4') do @@ -40,7 +40,7 @@ assert('BS Block 4') do for x in (1..5) sum += x end - assert_equal sum, 15 + assert_equal 15, sum end assert('BS Block 5') do @@ -48,7 +48,7 @@ assert('BS Block 5') do for x in [] sum += x end - assert_equal sum, 0 + assert_equal 0, sum end assert('BS Block 6') do @@ -76,7 +76,7 @@ assert('BS Block 7') do end assert('BS Block 8') do - assert_equal (1..3).to_a, [1, 2, 3] + assert_equal [1, 2, 3], (1..3).to_a end assert('BS Block 9') do @@ -196,7 +196,7 @@ assert('BS Block 16') do m{|ib,jb| ib*2+jb } - end + end end assert('BS Block 17') do @@ -217,7 +217,7 @@ assert('BS Block 18') do yield 10 end - assert_equal(21) do + assert_equal(21) do iter{|a| iter{|a| a + 1 @@ -404,7 +404,7 @@ end assert('BS Block 32') do r = false; 1.times{|&b| r = b} - assert_equal r.class, NilClass + assert_equal NilClass, r.class end assert('BS Block [ruby-core:14395]') do @@ -457,7 +457,7 @@ assert("BS Block 33") do :bad end end - assert_equal TestReturnFromNestedBlock.test, :ok + assert_equal :ok, TestReturnFromNestedBlock.test end assert("BS Block 34") do @@ -471,7 +471,7 @@ assert("BS Block 34") do :bad end end - assert_equal TestReturnFromNestedBlock_BSBlock34.test, :ok + assert_equal :ok, TestReturnFromNestedBlock_BSBlock34.test end assert("BS Block 35") do @@ -485,5 +485,5 @@ assert("BS Block 35") do :bad end end - assert_equal TestReturnFromNestedBlock_BSBlock35.test, :ok + assert_equal :ok, TestReturnFromNestedBlock_BSBlock35.test end diff --git a/test/t/class.rb b/test/t/class.rb index 9ba490d93..3dea2152f 100644 --- a/test/t/class.rb +++ b/test/t/class.rb @@ -2,11 +2,11 @@ # Class ISO Test assert('Class', '15.2.3') do - assert_equal(Class.class, Class) + assert_equal(Class, Class.class) end assert('Class superclass', '15.2.3.2') do - assert_equal(Class.superclass, Module) + assert_equal(Module, Class.superclass) end # Class#initialize '15.2.3.3.1' is tested in Class#new @@ -52,48 +52,48 @@ assert('Class#new', '15.2.3.3.3') do def result; @result; end end - assert_equal(TestClass.new(:arg).result, :only_args) + assert_equal(:only_args, TestClass.new(:arg).result) # with block doesn't work yet end assert('Class#superclass', '15.2.3.3.4') do class SubClass < String; end - assert_equal(SubClass.superclass, String) + assert_equal(String, SubClass.superclass) end # Not ISO specified assert('Class 1') do class C1; end - assert_equal(C1.class, Class) + assert_equal(Class, C1.class) end assert('Class 2') do class C2; end - assert_equal(C2.new.class, C2) + assert_equal(C2, C2.new.class) end assert('Class 3') do class C3; end - assert_equal(C3.new.class.class, Class) + assert_equal(Class, C3.new.class.class) end assert('Class 4') do class C4_A; end class C4 < C4_A; end - assert_equal(C4.class, Class) + assert_equal(Class, C4.class) end assert('Class 5') do class C5_A; end class C5 < C5_A; end - assert_equal(C5.new.class, C5) + assert_equal(C5, C5.new.class) end assert('Class 6') do class C6_A; end class C6 < C6_A; end - assert_equal(C6.new.class.class, Class) + assert_equal(Class, C6.new.class.class) end assert('Class 7') do @@ -129,13 +129,13 @@ end assert('Class Module 1') do module M; end - assert_equal(M.class, Module) + assert_equal(Module, M.class) end assert('Class Module 2') do module M; end class C; include M; end - assert_equal(C.new.class, C) + assert_equal(C, C.new.class) end # nested class @@ -148,13 +148,13 @@ end assert('Class Nested 2') do class A; end class A::B; end - assert_equal(A::B.new.class, A::B) + assert_equal(A::B, A::B.new.class) end assert('Class Nested 3') do class A; end class A::B; end - assert_equal(A::B.new.class.class, Class) + assert_equal(Class, A::B.new.class.class) end assert('Class Nested 4') do @@ -168,14 +168,14 @@ assert('Class Nested 5') do class A; end class A::B; end class A::B::C; end - assert_equal(A::B::C.class, Class) + assert_equal(Class, A::B::C.class) end assert('Class Nested 6') do class A; end class A::B; end class A::B::C; end - assert_equal(A::B::C.new.class, A::B::C) + assert_equal(A::B::C, A::B::C.new.class) end assert('Class Nested 7') do @@ -189,13 +189,13 @@ assert('Class Nested 8') do class A; end class A::B; end class A::B2 < A::B; end - assert_equal(A::B2.class, Class) + assert_equal(Class, A::B2.class) end assert('Class Colon 1') do class A; end A::C = 1 - assert_equal(A::C, 1) + assert_equal(1, A::C) end assert('Class Colon 2') do @@ -205,15 +205,43 @@ end assert('Class Colon 3') do class A; class ::C; end end - assert_equal(C.class, Class) + assert_equal(Class, C.class) end assert('Class Dup 1') do class C; end - assert_equal(C.dup.class, Class) + assert_equal(Class, C.dup.class) end assert('Class Dup 2') do module M; end - assert_equal(M.dup.class, Module) + assert_equal(Module, M.dup.class) +end + +assert('Class new') do + assert_equal(Class, Class.new.class) +end + +assert('Class#inherited') do + class Foo + @@subclass_name = nil + def self.inherited(subclass) + @@subclass_name = subclass + end + def self.subclass_name + @@subclass_name + end + end + + assert_equal(nil, Foo.subclass_name) + + class Bar < Foo + end + + assert_equal(Bar, Foo.subclass_name) + + class Baz < Bar + end + + assert_equal(Baz, Foo.subclass_name) end diff --git a/test/t/enumerable.rb b/test/t/enumerable.rb index 494490a9f..ed062823c 100644 --- a/test/t/enumerable.rb +++ b/test/t/enumerable.rb @@ -2,7 +2,7 @@ # Enumerable ISO Test assert('Enumerable', '15.3.2') do - assert_equal(Enumerable.class, Module) + assert_equal(Module, Enumerable.class) end assert('Enumerable#all?', '15.3.2.2.1') do @@ -21,7 +21,7 @@ end assert('Enumerable#detect', '15.3.2.2.4') do assert_true [1,2,3].detect() { true } - assert_equal [1,2,3].detect("a") { false }, 'a' + assert_equal 'a', [1,2,3].detect("a") { false } end assert('Array#each_with_index', '15.3.2.2.5') do @@ -30,17 +30,17 @@ assert('Array#each_with_index', '15.3.2.2.5') do [1].each_with_index {|e,i| a = e; b = i} - assert_equal(a, 1) - assert_equal(b, 0) + assert_equal(1, a) + assert_equal(0, b) end assert('Enumerable#entries', '15.3.2.2.6') do - assert_equal([1].entries, [1]) + assert_equal([1], [1].entries) end assert('Enumerable#find', '15.3.2.2.7') do assert_true [1,2,3].find() { true } - assert_equal [1,2,3].find("a") { false }, 'a' + assert_equal 'a', [1,2,3].find("a") { false } end assert('Enumerable#find_all', '15.3.2.2.8') do @@ -48,7 +48,7 @@ assert('Enumerable#find_all', '15.3.2.2.8') do end assert('Enumerable#grep', '15.3.2.2.9') do - assert_equal [1,2,3,4,5,6,7,8,9].grep(4..6), [4,5,6] + assert_equal [4,5,6], [1,2,3,4,5,6,7,8,9].grep(4..6) end assert('Enumerable#include?', '15.3.2.2.10') do @@ -57,24 +57,24 @@ assert('Enumerable#include?', '15.3.2.2.10') do end assert('Enumerable#inject', '15.3.2.2.11') do - assert_equal [1,2,3,4,5,6].inject() {|s, n| s + n}, 21 - assert_equal [1,2,3,4,5,6].inject(1) {|s, n| s + n}, 22 + assert_equal 21, [1,2,3,4,5,6].inject() {|s, n| s + n} + assert_equal 22, [1,2,3,4,5,6].inject(1) {|s, n| s + n} end assert('Enumerable#map', '15.3.2.2.12') do - assert_equal [1,2,3].map { |i| i + i }, [2,4,6] + assert_equal [2,4,6], [1,2,3].map { |i| i + i } end assert('Enumerable#max', '15.3.2.2.13') do a = ['aaa', 'bb', 'c'] - assert_equal a.max, 'c' - assert_equal a.max {|i1,i2| i1.length <=> i2.length}, 'aaa' + assert_equal 'c', a.max + assert_equal 'aaa', a.max {|i1,i2| i1.length <=> i2.length} end assert('Enumerable#min', '15.3.2.2.14') do a = ['aaa', 'bb', 'c'] - assert_equal a.min, 'aaa' - assert_equal a.min {|i1,i2| i1.length <=> i2.length}, 'c' + assert_equal 'aaa', a.min + assert_equal 'c', a.min {|i1,i2| i1.length <=> i2.length} end assert('Enumerable#member?', '15.3.2.2.15') do @@ -95,14 +95,14 @@ assert('Enumerable#reject', '15.3.2.2.17') do end assert('Enumerable#select', '15.3.2.2.18') do - assert_equal [1,2,3,4,5,6,7,8,9].select() {|i| i%2 == 0}, [2,4,6,8] + assert_equal [2,4,6,8], [1,2,3,4,5,6,7,8,9].select() {|i| i%2 == 0} end assert('Enumerable#sort', '15.3.2.2.19') do - assert_equal [7,3,1,2,6,4].sort, [1,2,3,4,6,7] - assert_equal [7,3,1,2,6,4].sort {|e1,e2|e2<=>e1}, [7,6,4,3,2,1] + assert_equal [1,2,3,4,6,7], [7,3,1,2,6,4].sort + assert_equal [7,6,4,3,2,1], [7,3,1,2,6,4].sort {|e1,e2|e2<=>e1} end assert('Enumerable#to_a', '15.3.2.2.20') do - assert_equal [1].to_a, [1] + assert_equal [1], [1].to_a end diff --git a/test/t/exception.rb b/test/t/exception.rb index 4b4a7c31d..96b369e93 100644 --- a/test/t/exception.rb +++ b/test/t/exception.rb @@ -2,44 +2,44 @@ # Exception ISO Test assert('Exception', '15.2.22') do - assert_equal Exception.class, Class + assert_equal Class, Exception.class end assert('Exception superclass', '15.2.22.2') do - assert_equal Exception.superclass, Object + assert_equal Object, Exception.superclass end assert('Exception.exception', '15.2.22.4.1') do e = Exception.exception('a') - assert_equal e.class, Exception + assert_equal Exception, e.class end assert('Exception#exception', '15.2.22.5.1') do e1 = Exception.exception() e2 = Exception.exception('b') - assert_equal e1.class, Exception - assert_equal e2.class, Exception + assert_equal Exception, e1.class + assert_equal Exception, e2.class end assert('Exception#message', '15.2.22.5.2') do e = Exception.exception('a') - assert_equal e.message, 'a' + assert_equal 'a', e.message end assert('Exception#to_s', '15.2.22.5.3') do e = Exception.exception('a') - assert_equal e.to_s, 'a' + assert_equal 'a', e.to_s end assert('Exception.exception', '15.2.22.4.1') do e = Exception.exception() e.initialize('a') - assert_equal e.message, 'a' + assert_equal 'a', e.message end assert('ScriptError', '15.2.37') do @@ -103,7 +103,7 @@ assert('Exception 4') do end a = err.class } - assert_equal a, NilClass + assert_equal NilClass, a end assert('Exception 5') do @@ -121,7 +121,7 @@ assert('Exception 5') do } end m2 - assert_equal $ans, [nil] + assert_equal [nil], $ans end assert('Exception 6') do @@ -147,7 +147,7 @@ assert('Exception 6') do yield end m - assert_equal $i, 7 + assert_equal 7, $i end assert('Exception 7') do @@ -167,7 +167,7 @@ assert('Exception 7') do p :end end m - assert_equal $i, 10 + assert_equal 10, $i end assert('Exception 8') do @@ -221,7 +221,7 @@ assert('Exception 11') do end rescue Exception end - assert_equal a, :ok + assert_equal :ok, a end assert('Exception 12') do @@ -230,7 +230,7 @@ assert('Exception 12') do raise Exception rescue a = :ng rescue Exception end - assert_equal a, :ok + assert_equal :ok, a end assert('Exception 13') do @@ -244,7 +244,7 @@ assert('Exception 13') do else a = :ng end - assert_equal a, :ok + assert_equal :ok, a end assert('Exception 14') do @@ -256,7 +256,7 @@ assert('Exception 14') do a = :ok end - assert_equal a, :ok + assert_equal :ok, a end assert('Exception 15') do @@ -265,7 +265,7 @@ assert('Exception 15') do rescue :ko end - assert_equal a, :ok + assert_equal :ok, a end assert('Exception 16') do @@ -316,7 +316,7 @@ assert('Exception 19') do end [ r, @e ] end - + def b begin 1 * "b" @@ -324,12 +324,12 @@ assert('Exception 19') do @e = self.z end end - + def z true end end - assert_equal Class4Exception19.new.a, [true, true] + assert_equal [true, true], Class4Exception19.new.a end assert('Exception#inspect without message') do diff --git a/test/t/false.rb b/test/t/false.rb index 97a3b8780..dbef985e5 100644 --- a/test/t/false.rb +++ b/test/t/false.rb @@ -2,11 +2,11 @@ # FalseClass ISO Test assert('FalseClass', '15.2.6') do - assert_equal FalseClass.class, Class + assert_equal Class, FalseClass.class end assert('FalseClass superclass', '15.2.6.2') do - assert_equal FalseClass.superclass, Object + assert_equal Object, FalseClass.superclass end assert('FalseClass false', '15.2.6.1') do @@ -24,7 +24,7 @@ assert('FalseClass#^', '15.2.6.3.2') do end assert('FalseClass#to_s', '15.2.6.3.3') do - assert_equal false.to_s, 'false' + assert_equal 'false', false.to_s end assert('FalseClass#|', '15.2.6.3.4') do diff --git a/test/t/float.rb b/test/t/float.rb index 97399b76f..f70bf2d66 100644 --- a/test/t/float.rb +++ b/test/t/float.rb @@ -2,51 +2,51 @@ # Float ISO Test assert('Float', '15.2.9') do - assert_equal Float.class, Class + assert_equal Class, Float.class end assert('Float superclass', '15.2.9.2') do - assert_equal Float.superclass, Numeric + assert_equal Numeric, Float.superclass end assert('Float#+', '15.2.9.3.1') do a = 3.123456788 + 0.000000001 b = 3.123456789 + 1 - assert_float(a, 3.123456789) - assert_float(b, 4.123456789) + assert_float(3.123456789, a) + assert_float(4.123456789, b) end assert('Float#-', '15.2.9.3.2') do a = 3.123456790 - 0.000000001 b = 5.123456789 - 1 - assert_float(a, 3.123456789) - assert_float(b, 4.123456789) + assert_float(3.123456789, a) + assert_float(4.123456789, b) end assert('Float#*', '15.2.9.3.3') do a = 3.125 * 3.125 b = 3.125 * 1 - assert_float(a, 9.765625) - assert_float(b, 3.125) + assert_float(9.765625, a) + assert_float(3.125 , b) end assert('Float#/', '15.2.9.3.4') do a = 3.123456789 / 3.123456789 b = 3.123456789 / 1 - assert_float(a, 1.0) - assert_float(b, 3.123456789) + assert_float(1.0 , a) + assert_float(3.123456789, b) end assert('Float#%', '15.2.9.3.5') do a = 3.125 % 3.125 b = 3.125 % 1 - assert_float(a, 0.0) - assert_float(b, 0.125) + assert_float(0.0 , a) + assert_float(0.125, b) end assert('Float#<=>', '15.2.9.3.6') do @@ -56,11 +56,11 @@ assert('Float#<=>', '15.2.9.3.6') do a2 = 3.125 <=> 3 c2 = 3.125 <=> 4 - assert_equal a, 1 - assert_equal b, 0 - assert_equal c, -1 - assert_equal a2, 1 - assert_equal c2, -1 + assert_equal( 1, a) + assert_equal( 0, b) + assert_equal(-1, c) + assert_equal( 1, a2) + assert_equal(-1, c2) end assert('Float#==', '15.2.9.3.7') do @@ -74,10 +74,10 @@ assert('Float#ceil', '15.2.9.3.8') do c = -3.123456789.ceil d = -3.0.ceil - assert_equal a, 4 - assert_equal b, 3 - assert_equal c, -3 - assert_equal d, -3 + assert_equal( 4, a) + assert_equal( 3, b) + assert_equal(-3, c) + assert_equal(-3, d) end assert('Float#finite?', '15.2.9.3.9') do @@ -91,10 +91,10 @@ assert('Float#floor', '15.2.9.3.10') do c = -3.123456789.floor d = -3.0.floor - assert_equal a, 3 - assert_equal b, 3 - assert_equal c, -4 - assert_equal d, -3 + assert_equal( 3, a) + assert_equal( 3, b) + assert_equal(-4, c) + assert_equal(-3, d) end assert('Float#infinite?', '15.2.9.3.11') do @@ -103,8 +103,8 @@ assert('Float#infinite?', '15.2.9.3.11') do c = (-1.0 / 0.0).infinite? assert_nil a - assert_equal b, 1 - assert_equal c, -1 + assert_equal( 1, b) + assert_equal(-1, c) end assert('Float#round', '15.2.9.3.12') do @@ -118,28 +118,28 @@ assert('Float#round', '15.2.9.3.12') do h = 3.423456789.round(1) i = 3.423456789.round(3) - assert_equal a, 3 - assert_equal b, 4 - assert_equal c, 3 - assert_equal d, -3 - assert_equal e, -4 - assert_equal f, 12350 - assert_equal g, 3 - assert_float(h, 3.4) - assert_float(i, 3.423) + assert_equal( 3, a) + assert_equal( 4, b) + assert_equal( 3, c) + assert_equal( -3, d) + assert_equal( -4, e) + assert_equal(12350, f) + assert_equal( 3, g) + assert_float( 3.4, h) + assert_float(3.423, i) end assert('Float#to_f', '15.2.9.3.13') do a = 3.123456789 - assert_float(a.to_f, a) + assert_float(a, a.to_f) end assert('Float#to_i', '15.2.9.3.14') do - assert_equal 3.123456789.to_i, 3 + assert_equal(3, 3.123456789.to_i) end assert('Float#truncate', '15.2.9.3.15') do - assert_equal 3.123456789.truncate, 3 - assert_equal(-3.1.truncate, -3) + assert_equal( 3, 3.123456789.truncate) + assert_equal(-3, -3.1.truncate) end diff --git a/test/t/gc.rb b/test/t/gc.rb index 4a1bd0c1a..4b800e945 100644 --- a/test/t/gc.rb +++ b/test/t/gc.rb @@ -1,15 +1,15 @@ # Not ISO specified assert('GC.enable') do - assert_equal GC.disable, false - assert_equal GC.enable, true - assert_equal GC.enable, false + assert_false GC.disable + assert_true GC.enable + assert_false GC.enable end assert('GC.disable') do begin - assert_equal GC.disable, false - assert_equal GC.disable, true + assert_false GC.disable + assert_true GC.disable ensure GC.enable end @@ -18,7 +18,7 @@ end assert('GC.interval_ratio=') do origin = GC.interval_ratio begin - assert_equal (GC.interval_ratio = 150), 150 + assert_equal 150, (GC.interval_ratio = 150) ensure GC.interval_ratio = origin end @@ -27,7 +27,7 @@ end assert('GC.step_ratio=') do origin = GC.step_ratio begin - assert_equal (GC.step_ratio = 150), 150 + assert_equal 150, (GC.step_ratio = 150) ensure GC.step_ratio = origin end @@ -36,9 +36,9 @@ end assert('GC.generational_mode=') do origin = GC.generational_mode begin - assert_equal (GC.generational_mode = false), false - assert_equal (GC.generational_mode = true), true - assert_equal (GC.generational_mode = true), true + assert_false (GC.generational_mode = false) + assert_true (GC.generational_mode = true) + assert_true (GC.generational_mode = true) ensure GC.generational_mode = origin end diff --git a/test/t/hash.rb b/test/t/hash.rb index 6f05b25ac..39f9ae9d8 100644 --- a/test/t/hash.rb +++ b/test/t/hash.rb @@ -2,11 +2,11 @@ # Hash ISO Test assert('Hash', '15.2.13') do - assert_equal Hash.class, Class + assert_equal Class, Hash.class end assert('Hash superclass', '15.2.13.2') do - assert_equal Hash.superclass, Object + assert_equal Object, Hash.superclass end assert('Hash#==', '15.2.13.4.1') do @@ -17,21 +17,21 @@ end assert('Hash#[]', '15.2.13.4.2') do a = { 'abc' => 'abc' } - assert_equal a['abc'], 'abc' + assert_equal 'abc', a['abc'] end assert('Hash#[]=', '15.2.13.4.3') do a = Hash.new a['abc'] = 'abc' - assert_equal a['abc'], 'abc' + assert_equal 'abc', a['abc'] end assert('Hash#clear', '15.2.13.4.4') do a = { 'abc' => 'abc' } a.clear - assert_equal a, { } + assert_equal({ }, a) end assert('Hash#default', '15.2.13.4.5') do @@ -40,17 +40,17 @@ assert('Hash#default', '15.2.13.4.5') do c = Hash.new {|s,k| s[k] = k} assert_nil a.default - assert_equal b.default, 'abc' + assert_equal 'abc', b.default assert_nil c.default - assert_equal c.default('abc'), 'abc' + assert_equal 'abc', c.default('abc') end assert('Hash#default=', '15.2.13.4.6') do a = { 'abc' => 'abc' } a.default = 'cba' - assert_equal a['abc'], 'abc' - assert_equal a['notexist'], 'cba' + assert_equal 'abc', a['abc'] + assert_equal 'cba', a['notexist'] end assert('Hash#default_proc', '15.2.13.4.7') do @@ -60,9 +60,9 @@ assert('Hash#default_proc', '15.2.13.4.7') do d = b['cat'] assert_nil a.default_proc - assert_equal b.default_proc.class, Proc - assert_equal c, 4 - assert_equal d, 'catcat' + assert_equal Proc, b.default_proc.class + assert_equal 4, c + assert_equal 'catcat', d end assert('Hash#delete', '15.2.13.4.8') do @@ -95,8 +95,8 @@ assert('Hash#each', '15.2.13.4.9') do value = v end - assert_equal key, 'abc_key' - assert_equal value, 'abc_value' + assert_equal 'abc_key', key + assert_equal 'abc_value', value end assert('Hash#each_key', '15.2.13.4.10') do @@ -107,7 +107,7 @@ assert('Hash#each_key', '15.2.13.4.10') do key = k end - assert_equal key, 'abc_key' + assert_equal 'abc_key', key end assert('Hash#each_value', '15.2.13.4.11') do @@ -118,7 +118,7 @@ assert('Hash#each_value', '15.2.13.4.11') do value = v end - assert_equal value, 'abc_value' + assert_equal 'abc_value', value end assert('Hash#empty?', '15.2.13.4.12') do @@ -159,16 +159,16 @@ assert('Hash#initialize', '15.2.13.4.16') do h2 = Hash.new(:not_found) assert_true h.is_a? Hash - assert_equal h, { } + assert_equal({ }, h) assert_nil h["hello"] - assert_equal h2["hello"], :not_found + assert_equal :not_found, h2["hello"] end assert('Hash#initialize_copy', '15.2.13.4.17') do a = { 'abc_key' => 'abc_value' } b = Hash.new.initialize_copy(a) - assert_equal b, { 'abc_key' => 'abc_value' } + assert_equal({ 'abc_key' => 'abc_value' }, b) end assert('Hash#key?', '15.2.13.4.18') do @@ -182,15 +182,15 @@ end assert('Hash#keys', '15.2.13.4.19') do a = { 'abc_key' => 'abc_value' } - assert_equal a.keys, ['abc_key'] + assert_equal ['abc_key'], a.keys end assert('Hash#length', '15.2.13.4.20') do a = { 'abc_key' => 'abc_value' } b = Hash.new - assert_equal a.length, 1 - assert_equal b.length, 0 + assert_equal 1, a.length + assert_equal 0, b.length end assert('Hash#member?', '15.2.13.4.21') do @@ -210,40 +210,40 @@ assert('Hash#merge', '15.2.13.4.22') do original end - assert_equal result_1, {'abc_key' => 'abc_value', 'cba_key' => 'XXX', - 'xyz_key' => 'xyz_value' } - assert_equal result_2, {'abc_key' => 'abc_value', 'cba_key' => 'cba_value', - 'xyz_key' => 'xyz_value' } + assert_equal({'abc_key' => 'abc_value', 'cba_key' => 'XXX', + 'xyz_key' => 'xyz_value' }, result_1) + assert_equal({'abc_key' => 'abc_value', 'cba_key' => 'cba_value', + 'xyz_key' => 'xyz_value' }, result_2) end assert('Hash#replace', '15.2.13.4.23') do a = { 'abc_key' => 'abc_value' } b = Hash.new.replace(a) - assert_equal b, { 'abc_key' => 'abc_value' } + assert_equal({ 'abc_key' => 'abc_value' }, b) end assert('Hash#shift', '15.2.13.4.24') do a = { 'abc_key' => 'abc_value', 'cba_key' => 'cba_value' } b = a.shift - assert_equal a, { 'abc_key' => 'abc_value' } - assert_equal b, [ 'cba_key', 'cba_value' ] + assert_equal({ 'abc_key' => 'abc_value' }, a) + assert_equal [ 'cba_key', 'cba_value' ], b end assert('Hash#size', '15.2.13.4.25') do a = { 'abc_key' => 'abc_value' } b = Hash.new - assert_equal a.size, 1 - assert_equal b.size, 0 + assert_equal 1, a.size + assert_equal 0, b.size end assert('Hash#store', '15.2.13.4.26') do a = Hash.new a.store('abc', 'abc') - assert_equal a['abc'], 'abc' + assert_equal 'abc', a['abc'] end assert('Hash#value?', '15.2.13.4.27') do @@ -257,7 +257,7 @@ end assert('Hash#values', '15.2.13.4.28') do a = { 'abc_key' => 'abc_value' } - assert_equal a.values, ['abc_value'] + assert_equal ['abc_value'], a.values end # Not ISO specified @@ -267,8 +267,8 @@ assert('Hash#reject') do ret = h.reject do |k,v| v % 2 == 0 end - assert_equal ret, {:one => 1, :three => 3} - assert_equal h, {:one => 1, :two => 2, :three => 3, :four => 4} + assert_equal({:one => 1, :three => 3}, ret) + assert_equal({:one => 1, :two => 2, :three => 3, :four => 4}, h) end assert('Hash#reject!') do @@ -276,8 +276,8 @@ assert('Hash#reject!') do ret = h.reject! do |k,v| v % 2 == 0 end - assert_equal ret, {:one => 1, :three => 3} - assert_equal h, {:one => 1, :three => 3} + assert_equal({:one => 1, :three => 3}, ret) + assert_equal({:one => 1, :three => 3}, h) end assert('Hash#select') do @@ -285,8 +285,8 @@ assert('Hash#select') do ret = h.select do |k,v| v % 2 == 0 end - assert_equal ret, {:two => 2, :four => 4} - assert_equal h, {:one => 1, :two => 2, :three => 3, :four => 4} + assert_equal({:two => 2, :four => 4}, ret) + assert_equal({:one => 1, :two => 2, :three => 3, :four => 4}, h) end assert('Hash#select!') do @@ -294,8 +294,8 @@ assert('Hash#select!') do ret = h.select! do |k,v| v % 2 == 0 end - assert_equal ret, {:two => 2, :four => 4} - assert_equal h, {:two => 2, :four => 4} + assert_equal({:two => 2, :four => 4}, ret) + assert_equal({:two => 2, :four => 4}, h) end # Not ISO specified diff --git a/test/t/indexerror.rb b/test/t/indexerror.rb index b71766c95..ea008a227 100644 --- a/test/t/indexerror.rb +++ b/test/t/indexerror.rb @@ -2,9 +2,9 @@ # IndexError ISO Test assert('IndexError', '15.2.33') do - assert_equal IndexError.class, Class + assert_equal Class, IndexError.class end assert('IndexError superclass', '15.2.33.2') do - assert_equal IndexError.superclass, StandardError + assert_equal StandardError, IndexError.superclass end diff --git a/test/t/integer.rb b/test/t/integer.rb index 9b19216d9..79ee1e790 100644 --- a/test/t/integer.rb +++ b/test/t/integer.rb @@ -2,43 +2,43 @@ # Integer ISO Test assert('Integer', '15.2.8') do - assert_equal Integer.class, Class + assert_equal Class, Integer.class end assert('Integer superclass', '15.2.8.2') do - assert_equal Integer.superclass, Numeric + assert_equal Numeric, Integer.superclass end assert('Integer#+', '15.2.8.3.1') do a = 1+1 b = 1+1.0 - assert_equal a, 2 - assert_equal b, 2.0 + assert_equal 2, a + assert_equal 2.0, b end assert('Integer#-', '15.2.8.3.2') do a = 2-1 b = 2-1.0 - assert_equal a, 1 - assert_equal b, 1.0 + assert_equal 1, a + assert_equal 1.0, b end assert('Integer#*', '15.2.8.3.3') do a = 1*1 b = 1*1.0 - assert_equal a, 1 - assert_equal b, 1.0 + assert_equal 1, a + assert_equal 1.0, b end assert('Integer#/', '15.2.8.3.4') do a = 2/1 b = 2/1.0 - assert_equal a, 2 - assert_equal b, 2.0 + assert_equal 2, a + assert_equal 2.0, b end assert('Integer#%', '15.2.8.3.5') do @@ -46,9 +46,9 @@ assert('Integer#%', '15.2.8.3.5') do b = 1%1.0 c = 2%4 - assert_equal a, 0 - assert_equal b, 0.0 - assert_equal c, 2 + assert_equal 0, a + assert_equal 0.0, b + assert_equal 2, c end assert('Integer#<=>', '15.2.8.3.6') do @@ -56,9 +56,9 @@ assert('Integer#<=>', '15.2.8.3.6') do b = 1<=>1 c = 1<=>2 - assert_equal a, 1 - assert_equal b, 0 - assert_equal c, -1 + assert_equal 1, a + assert_equal 0, b + assert_equal(-1, c) end assert('Integer#==', '15.2.8.3.7') do @@ -71,8 +71,8 @@ end assert('Integer#~', '15.2.8.3.8') do # Complement - assert_equal ~0, -1 - assert_equal ~2, -3 + assert_equal(-1, ~0) + assert_equal(-3, ~2) end assert('Integer#&', '15.2.8.3.9') do @@ -80,7 +80,7 @@ assert('Integer#&', '15.2.8.3.9') do # 0101 (5) # & 0011 (3) # = 0001 (1) - assert_equal 5 & 3, 1 + assert_equal 1, 5 & 3 end assert('Integer#|', '15.2.8.3.10') do @@ -88,7 +88,7 @@ assert('Integer#|', '15.2.8.3.10') do # 0101 (5) # | 0011 (3) # = 0111 (7) - assert_equal 5 | 3, 7 + assert_equal 7, 5 | 3 end assert('Integer#^', '15.2.8.3.11') do @@ -96,17 +96,17 @@ assert('Integer#^', '15.2.8.3.11') do # 0101 (5) # ^ 0011 (3) # = 0110 (6) - assert_equal 5 ^ 3, 6 + assert_equal 6, 5 ^ 3 end assert('Integer#<<', '15.2.8.3.12') do # Left Shift by one # 00010111 (23) # = 00101110 (46) - assert_equal 23 << 1, 46 + assert_equal 46, 23 << 1 # Left Shift by a negative is Right Shift - assert_equal 46 << -1, 23 + assert_equal 23, 46 << -1 # Raise when shift is too large assert_raise(RangeError) do @@ -118,13 +118,13 @@ assert('Integer#>>', '15.2.8.3.13') do # Right Shift by one # 00101110 (46) # = 00010111 (23) - assert_equal 46 >> 1, 23 + assert_equal 23, 46 >> 1 # Right Shift by a negative is Left Shift - assert_equal 23 >> -1, 46 + assert_equal 46, 23 >> -1 # Don't raise on large Right Shift - assert_equal 23 >> 128, 0 + assert_equal 0, 23 >> 128 # Raise when shift is too large assert_raise(RangeError) do @@ -133,7 +133,7 @@ assert('Integer#>>', '15.2.8.3.13') do end assert('Integer#ceil', '15.2.8.3.14') do - assert_equal 10.ceil, 10 + assert_equal 10, 10.ceil end assert('Integer#downto', '15.2.8.3.15') do @@ -141,7 +141,7 @@ assert('Integer#downto', '15.2.8.3.15') do 3.downto(1) do |i| a += i end - assert_equal a, 6 + assert_equal 6, a end assert('Integer#eql?', '15.2.8.3.16') do @@ -157,19 +157,19 @@ end assert('Integer#floor', '15.2.8.3.17') do a = 1.floor - assert_equal a, 1 + assert_equal 1, a end assert('Integer#next', '15.2.8.3.19') do - assert_equal 1.next, 2 + assert_equal 2, 1.next end assert('Integer#round', '15.2.8.3.20') do - assert_equal 1.round, 1 + assert_equal 1, 1.round end assert('Integer#succ', '15.2.8.3.21') do - assert_equal 1.succ, 2 + assert_equal 2, 1.succ end assert('Integer#times', '15.2.8.3.22') do @@ -177,24 +177,24 @@ assert('Integer#times', '15.2.8.3.22') do 3.times do a += 1 end - assert_equal a, 3 + assert_equal 3, a end assert('Integer#to_f', '15.2.8.3.23') do - assert_equal 1.to_f, 1.0 + assert_equal 1.0, 1.to_f end assert('Integer#to_i', '15.2.8.3.24') do - assert_equal 1.to_i, 1 + assert_equal 1, 1.to_i end assert('Integer#to_s', '15.2.8.3.25') do - assert_equal 1.to_s, '1' - assert_equal(-1.to_s, "-1") + assert_equal '1', 1.to_s + assert_equal("-1", -1.to_s) end assert('Integer#truncate', '15.2.8.3.26') do - assert_equal 1.truncate, 1 + assert_equal 1, 1.truncate end assert('Integer#upto', '15.2.8.3.27') do @@ -202,7 +202,7 @@ assert('Integer#upto', '15.2.8.3.27') do 1.upto(3) do |i| a += i end - assert_equal a, 6 + assert_equal 6, a end # Not ISO specified @@ -217,6 +217,6 @@ assert('Integer#step') do b << i end - assert_equal a, [1, 2, 3] - assert_equal b, [1, 3, 5] + assert_equal [1, 2, 3], a + assert_equal [1, 3, 5], b end diff --git a/test/t/kernel.rb b/test/t/kernel.rb index f92fa3b8a..81c111053 100644 --- a/test/t/kernel.rb +++ b/test/t/kernel.rb @@ -2,7 +2,7 @@ # Kernel ISO Test assert('Kernel', '15.3.1') do - assert_equal Kernel.class, Module + assert_equal Module, Kernel.class end assert('Kernel.block_given?', '15.3.1.2.2') do @@ -16,7 +16,7 @@ assert('Kernel.block_given?', '15.3.1.2.2') do assert_false Kernel.block_given? # test without block - assert_equal bg_try, "no block" + assert_equal "no block", bg_try # test with block assert_equal "block" do bg_try { "block" } @@ -32,7 +32,7 @@ end # Kernel.eval is provided by the mruby-gem mrbgem. '15.3.1.2.3' assert('Kernel.global_variables', '15.3.1.2.4') do - assert_equal Kernel.global_variables.class, Array + assert_equal Array, Kernel.global_variables.class end assert('Kernel.iterator?', '15.3.1.2.5') do @@ -47,9 +47,9 @@ assert('Kernel.lambda', '15.3.1.2.6') do m = Kernel.lambda(&l) assert_true l.call - assert_equal l.class, Proc + assert_equal Proc, l.class assert_true m.call - assert_equal m.class, Proc + assert_equal Proc, m.class end # Not implemented at the moment @@ -65,7 +65,7 @@ assert('Kernel.loop', '15.3.1.2.8') do break if i == 100 end - assert_equal i, 100 + assert_equal 100, i end assert('Kernel.p', '15.3.1.2.9') do @@ -94,7 +94,7 @@ assert('Kernel.raise', '15.3.1.2.12') do end assert('Kernel#__id__', '15.3.1.3.3') do - assert_equal __id__.class, Fixnum + assert_equal Fixnum, __id__.class end assert('Kernel#__send__', '15.3.1.3.4') do @@ -102,13 +102,13 @@ assert('Kernel#__send__', '15.3.1.3.4') do l = __send__(:lambda) do true end - + assert_true l.call - assert_equal l.class, Proc + assert_equal Proc, l.class # test with argument assert_true __send__(:respond_to?, :nil?) # test without argument and without block - assert_equal __send__(:public_methods).class, Array + assert_equal Array, __send__(:public_methods).class end assert('Kernel#block_given?', '15.3.1.3.6') do @@ -121,7 +121,7 @@ assert('Kernel#block_given?', '15.3.1.3.6') do end assert_false block_given? - assert_equal bg_try, "no block" + assert_equal "no block", bg_try assert_equal "block" do bg_try { "block" } end @@ -133,7 +133,7 @@ assert('Kernel#block_given?', '15.3.1.3.6') do end assert('Kernel#class', '15.3.1.3.7') do - assert_equal Kernel.class, Module + assert_equal Module, Kernel.class end assert('Kernel#clone', '15.3.1.3.8') do @@ -170,9 +170,9 @@ assert('Kernel#clone', '15.3.1.3.8') do end end - assert_equal a.get, 2 - assert_equal b.get, 1 - assert_equal c.get, 2 + assert_equal 2, a.get + assert_equal 1, b.get + assert_equal 2, c.get assert_true a.respond_to?(:test) assert_false b.respond_to?(:test) assert_true c.respond_to?(:test) @@ -212,10 +212,10 @@ assert('Kernel#dup', '15.3.1.3.9') do end end - assert_equal error_count, immutables.size - assert_equal a.get, 2 - assert_equal b.get, 1 - assert_equal c.get, 2 + assert_equal immutables.size, error_count + assert_equal 2, a.get + assert_equal 1, b.get + assert_equal 2, c.get assert_true a.respond_to?(:test) assert_false b.respond_to?(:test) assert_false c.respond_to?(:test) @@ -243,14 +243,14 @@ assert('Kernel#extend works on toplevel', '15.3.1.3.13') do module Test4ExtendModule def test_method; end end - # This would crash... + # This would crash... extend(Test4ExtendModule) assert_true respond_to?(:test_method) end assert('Kernel#global_variables', '15.3.1.3.14') do - assert_equal global_variables.class, Array + assert_equal Array, global_variables.class end assert('Kernel#hash', '15.3.1.3.15') do @@ -260,8 +260,8 @@ end assert('Kernel#inspect', '15.3.1.3.17') do s = inspect - assert_equal s.class, String - assert_equal s, "main" + assert_equal String, s.class + assert_equal "main", s end assert('Kernel#instance_variables', '15.3.1.3.23') do @@ -272,8 +272,8 @@ assert('Kernel#instance_variables', '15.3.1.3.23') do end ivars = o.instance_variables - assert_equal ivars.class, Array - assert_equal ivars.size, 2 + assert_equal Array, ivars.class, + assert_equal(2, ivars.size) assert_true ivars.include?(:@a) assert_true ivars.include?(:@b) end @@ -300,9 +300,9 @@ assert('Kernel#lambda', '15.3.1.3.27') do m = lambda(&l) assert_true l.call - assert_equal l.class, Proc + assert_equal Proc, l.class assert_true m.call - assert_equal m.class, Proc + assert_equal Proc, m.class end # Not implemented yet @@ -322,7 +322,7 @@ assert('Kernel#loop', '15.3.1.3.29') do end assert('Kernel#methods', '15.3.1.3.31') do - assert_equal methods.class, Array + assert_equal Array, methods.class end assert('Kernel#nil?', '15.3.1.3.32') do @@ -330,7 +330,7 @@ assert('Kernel#nil?', '15.3.1.3.32') do end assert('Kernel#object_id', '15.3.1.3.33') do - assert_equal object_id.class, Fixnum + assert_equal Fixnum, object_id.class end # Kernel#p is defined in mruby-print mrbgem. '15.3.1.3.34' @@ -338,15 +338,15 @@ end # Kernel#print is defined in mruby-print mrbgem. '15.3.1.3.35' assert('Kernel#private_methods', '15.3.1.3.36') do - assert_equal private_methods.class, Array + assert_equal Array, private_methods.class end assert('Kernel#protected_methods', '15.3.1.3.37') do - assert_equal protected_methods.class, Array + assert_equal Array, protected_methods.class end assert('Kernel#public_methods', '15.3.1.3.38') do - assert_equal public_methods.class, Array + assert_equal Array, public_methods.class end # Kernel#puts is defined in mruby-print mrbgem. '15.3.1.3.39' @@ -378,7 +378,7 @@ assert('Kernel#respond_to?', '15.3.1.3.43') do assert_true respond_to?(:nil?) assert_true Test4RespondTo.new.respond_to?(:valid_method) assert_true Test4RespondTo.new.respond_to?('valid_method') - assert_false Test4RespondTo.new.respond_to?(:test_method) + assert_false Test4RespondTo.new.respond_to?(:test_method) end assert('Kernel#send', '15.3.1.3.44') do diff --git a/test/t/literals.rb b/test/t/literals.rb index c4fe9c993..524ceb440 100644 --- a/test/t/literals.rb +++ b/test/t/literals.rb @@ -10,29 +10,29 @@ assert('Literals Numerical', '8.7.6.2') do assert_equal 1.0, 1.0 assert_equal(-1.0, -1.0) # binary - assert_equal 0b10000000, 128 - assert_equal 0B10000000, 128 + assert_equal 128, 0b10000000 + assert_equal 128, 0B10000000 # octal - assert_equal 0o10, 8 - assert_equal 0O10, 8 - assert_equal 0_10, 8 + assert_equal 8, 0o10 + assert_equal 8, 0O10 + assert_equal 8, 0_10 # hex - assert_equal 0xff, 255 - assert_equal 0Xff, 255 + assert_equal 255, 0xff + assert_equal 255, 0Xff # decimal - assert_equal 0d999, 999 - assert_equal 0D999, 999 + assert_equal 999, 0d999 + assert_equal 999, 0D999 # decimal seperator - assert_equal 10_000_000, 10000000 - assert_equal 1_0, 10 + assert_equal 10000000, 10_000_000 + assert_equal 10, 1_0 # integer with exponent - assert_equal 1e1, 10.0 - assert_equal 1e-1, 0.1 - assert_equal 1e+1, 10.0 + assert_equal 10.0, 1e1, + assert_equal(0.1, 1e-1) + assert_equal 10.0, 1e+1 # float with exponent - assert_equal 1.0e1, 10.0 - assert_equal 1.0e-1, 0.1 - assert_equal 1.0e+1, 10.0 + assert_equal 10.0, 1.0e1 + assert_equal(0.1, 1.0e-1) + assert_equal 10.0, 1.0e+1 end assert('Literals Strings Single Quoted', '8.7.6.3.2') do @@ -47,7 +47,7 @@ assert('Literals Strings Double Quoted', '8.7.6.3.3') do assert_equal "abc", "abc" assert_equal "\"", "\"" assert_equal "\\", "\\" - assert_equal "#{a}", "abc" + assert_equal "abc", "#{a}" end assert('Literals Strings Quoted Non-Expanded', '8.7.6.3.4') do @@ -59,13 +59,13 @@ assert('Literals Strings Quoted Non-Expanded', '8.7.6.3.4') do f = %q/ab\/c/ g = %q{#{a}} - assert_equal a, 'abc' - assert_equal b, 'abc' - assert_equal c, 'abc' - assert_equal d, 'abc' - assert_equal e, 'abc' - assert_equal f, 'ab/c' - assert_equal g, '#{a}' + assert_equal 'abc', a + assert_equal 'abc', b + assert_equal 'abc', c + assert_equal 'abc', d + assert_equal 'abc', e + assert_equal 'ab/c', f + assert_equal '#{a}', g end assert('Literals Strings Quoted Expanded', '8.7.6.3.5') do @@ -77,13 +77,13 @@ assert('Literals Strings Quoted Expanded', '8.7.6.3.5') do f = %Q/ab\/c/ g = %Q{#{a}} - assert_equal a, 'abc' - assert_equal b, 'abc' - assert_equal c, 'abc' - assert_equal d, 'abc' - assert_equal e, 'abc' - assert_equal f, 'ab/c' - assert_equal g, 'abc' + assert_equal 'abc', a + assert_equal 'abc', b + assert_equal 'abc', c + assert_equal 'abc', d + assert_equal 'abc', e + assert_equal 'ab/c', f + assert_equal 'abc', g end assert('Literals Strings Here documents', '8.7.6.3.6') do @@ -141,18 +141,18 @@ KKK z = <<'ZZZ' ZZZ - assert_equal a, "aaa\n" - assert_equal b, "bbb\n" - assert_equal c, ["c1\n", "c 2\n", "c 3\n"] - assert_equal d, "d3DDD\nd\t\nDDD\n\n" - assert_equal e, "e\#{1+2}EEE\ne\\t\nEEE\\n\n" - assert_equal f, "F\nFFfFFF\nF\n" - assert_equal g, " ggg\n" - assert_equal h, " hhh\n" - assert_equal i, " iii\n" - assert_equal j, [" j1j\n", " j2j\n", " j\#{3}j\n"] - assert_equal k, 123 - assert_equal z, "" + assert_equal "aaa\n", a + assert_equal "bbb\n", b + assert_equal ["c1\n", "c 2\n", "c 3\n"], c + assert_equal "d3DDD\nd\t\nDDD\n\n", d + assert_equal "e\#{1+2}EEE\ne\\t\nEEE\\n\n", e + assert_equal "F\nFFfFFF\nF\n", f + assert_equal " ggg\n", g + assert_equal " hhh\n", h + assert_equal " iii\n", i + assert_equal [" j1j\n", " j2j\n", " j\#{3}j\n"], j + assert_equal 123, k + assert_equal "", z end assert('Literals Array', '8.7.6.4') do @@ -173,14 +173,14 @@ assert('Literals Array', '8.7.6.4') do d x\y x\\y x\\\y) - assert_equal a, ['abc3def', '}g'] - assert_equal b, ['abc', '5', 'def', '(g'] - assert_equal c, ['7'] - assert_equal d, ['9'] - assert_equal e, [] - assert_equal f, ['[ab', 'cd][ef]'] - assert_equal g, ['ab', '-11', '22'] - assert_equal h, ["a\nb", 'test abc', "c\nd", "xy", "x\\y", "x\\y"] + assert_equal ['abc3def', '}g'], a + assert_equal ['abc', '5', 'def', '(g'], b + assert_equal ['7'],c + assert_equal ['9'], d + assert_equal [], e + assert_equal ['[ab', 'cd][ef]'], f + assert_equal ['ab', '-11', '22'], g + assert_equal ["a\nb", 'test abc', "c\nd", "xy", "x\\y", "x\\y"], h a = %w{abc#{1+2}def \}g} b = %w(abc #{2+3} def \(g) @@ -199,14 +199,14 @@ d d x\y x\\y x\\\y) - assert_equal a, ['abc#{1+2}def', '}g'] - assert_equal b, ['abc', '#{2+3}', 'def', '(g'] - assert_equal c, ['#{3+4}'] - assert_equal d, ['#{4+5}'] - assert_equal e, [] - assert_equal f, ['[ab', 'cd][ef]'] - assert_equal g, ['ab', '#{-1}1', '2#{2}'] - assert_equal h, ["a\\nb", "test abc", "c\nd", "x\\y", "x\\y", "x\\\\y"] + assert_equal ['abc#{1+2}def', '}g'], a + assert_equal ['abc', '#{2+3}', 'def', '(g'], b + assert_equal ['#{3+4}'], c + assert_equal ['#{4+5}'], d + assert_equal [], e + assert_equal ['[ab', 'cd][ef]'], f + assert_equal ['ab', '#{-1}1', '2#{2}'], g + assert_equal ["a\\nb", "test abc", "c\nd", "x\\y", "x\\y", "x\\\\y"], h end assert('Literals Array of symbols') do @@ -222,13 +222,13 @@ assert('Literals Array of symbols') do 2#{2} } - assert_equal a, [:'abc3def', :'}g'] - assert_equal b, [:'abc', :'5', :'def', :'(g'] - assert_equal c, [:'7'] - assert_equal d, [:'9'] - assert_equal e, [] - assert_equal f, [:'[ab', :'cd][ef]'] - assert_equal g, [:'ab', :'-11', :'22'] + assert_equal [:'abc3def', :'}g'], a + assert_equal [:'abc', :'5', :'def', :'(g'], b + assert_equal [:'7'],c + assert_equal [:'9'], d + assert_equal [], e + assert_equal [:'[ab', :'cd][ef]'], f + assert_equal [:'ab', :'-11', :'22'], g a = %i{abc#{1+2}def \}g} b = %i(abc #{2+3} def \(g) @@ -242,13 +242,13 @@ assert('Literals Array of symbols') do 2#{2} } - assert_equal a, [:'abc#{1+2}def', :'}g'] - assert_equal b, [:'abc', :'#{2+3}', :'def', :'(g'] - assert_equal c, [:'#{3+4}'] - assert_equal d, [:'#{4+5}'] - assert_equal e, [] - assert_equal f, [:'[ab', :'cd][ef]'] - assert_equal g, [:'ab', :'#{-1}1', :'2#{2}'] + assert_equal [:'abc#{1+2}def', :'}g'], a + assert_equal [:'abc', :'#{2+3}', :'def', :'(g'], b + assert_equal [:'#{3+4}'], c + assert_equal [:'#{4+5}'], d + assert_equal [] ,e + assert_equal [:'[ab', :'cd][ef]'], f + assert_equal [:'ab', :'#{-1}1', :'2#{2}'], g end assert('Literals Symbol', '8.7.6.6') do @@ -274,14 +274,14 @@ qwe] g = %s/foo#{1+2}bar/ h = %s{{foo bar}} - assert_equal a, :'asd qwe' - assert_equal b, :"foo bar" - assert_equal c, :a3b - assert_equal d, :asd - assert_equal e, :' foo )' - assert_equal f, :"asd [\nqwe" - assert_equal g, :'foo#{1+2}bar' - assert_equal h, :'{foo bar}' + assert_equal :'asd qwe', a + assert_equal :"foo bar", b + assert_equal :a3b, c + assert_equal :asd, d + assert_equal :' foo )', e + assert_equal :"asd [\nqwe", f + assert_equal :'foo#{1+2}bar', g + assert_equal :'{foo bar}', h end # Not Implemented ATM assert('Literals Regular expression', '8.7.6.5') do diff --git a/test/t/localjumperror.rb b/test/t/localjumperror.rb index 857b0ce65..a7d18b3b1 100644 --- a/test/t/localjumperror.rb +++ b/test/t/localjumperror.rb @@ -2,7 +2,7 @@ # LocalJumpError ISO Test assert('LocalJumpError', '15.2.25') do - assert_equal LocalJumpError.class, Class + assert_equal Class, LocalJumpError.class assert_raise LocalJumpError do # this will cause an exception due to the wrong location retry diff --git a/test/t/module.rb b/test/t/module.rb index 9d735f5da..8655db391 100644 --- a/test/t/module.rb +++ b/test/t/module.rb @@ -2,11 +2,11 @@ # Module ISO Test assert('Module', '15.2.2') do - assert_equal Module.class, Class + assert_equal Class, Module.class end assert('Module superclass', '15.2.2.2') do - assert_equal Module.superclass, Object + assert_equal Object, Module.superclass end # TODO not implemented ATM assert('Module.constants', '15.2.2.3.1') do @@ -19,7 +19,7 @@ assert('Module#ancestors', '15.2.2.4.9') do sc = Test4ModuleAncestors.singleton_class r = String.ancestors - assert_equal r.class, Array + assert_equal Array, r.class assert_true r.include?(String) assert_true r.include?(Object) end @@ -34,7 +34,146 @@ assert('Module#append_features', '15.2.2.4.10') do include Test4AppendFeatures end - assert_equal Test4AppendFeatures2.const_get(:Const4AppendFeatures2), Test4AppendFeatures2 + assert_equal Test4AppendFeatures2, Test4AppendFeatures2.const_get(:Const4AppendFeatures2) +end + +assert('Module#attr NameError') do + %w[ + foo? + @foo + @@foo + $foo + ].each do |name| + module NameTest; end + + assert_raise(NameError) do + NameTest.module_eval { attr_reader name.to_sym } + end + + assert_raise(NameError) do + NameTest.module_eval { attr_writer name.to_sym } + end + + assert_raise(NameError) do + NameTest.module_eval { attr name.to_sym } + end + + assert_raise(NameError) do + NameTest.module_eval { attr_accessor name.to_sym } + end + end + +end + +assert('Module#attr', '15.2.2.4.11') do + class AttrTest + class << self + attr :cattr + def cattr_val=(val) + @cattr = val + end + end + attr :iattr + def iattr_val=(val) + @iattr = val + end + end + + test = AttrTest.new + assert_true AttrTest.respond_to?(:cattr) + assert_true test.respond_to?(:iattr) + + assert_false AttrTest.respond_to?(:vattr=) + assert_false test.respond_to?(:iattr=) + + test.iattr_val = 'test' + assert_equal 'test', test.iattr + + AttrTest.cattr_val = 'test' + assert_equal 'test', AttrTest.cattr +end + +assert('Module#attr_accessor', '15.2.2.4.12') do + class AttrTestAccessor + class << self + attr_accessor :cattr + end + attr_accessor :iattr, 'iattr2' + end + + attr_instance = AttrTestAccessor.new + assert_true AttrTestAccessor.respond_to?(:cattr=) + assert_true attr_instance.respond_to?(:iattr=) + assert_true attr_instance.respond_to?(:iattr2=) + assert_true AttrTestAccessor.respond_to?(:cattr) + assert_true attr_instance.respond_to?(:iattr) + assert_true attr_instance.respond_to?(:iattr2) + + attr_instance.iattr = 'test' + assert_equal 'test', attr_instance.iattr + + AttrTestAccessor.cattr = 'test' + assert_equal 'test', AttrTestAccessor.cattr +end + +assert('Module#attr_reader', '15.2.2.4.13') do + class AttrTestReader + class << self + attr_reader :cattr + def cattr_val=(val) + @cattr = val + end + end + attr_reader :iattr, 'iattr2' + def iattr_val=(val) + @iattr = val + end + end + + attr_instance = AttrTestReader.new + assert_true AttrTestReader.respond_to?(:cattr) + assert_true attr_instance.respond_to?(:iattr) + assert_true attr_instance.respond_to?(:iattr2) + + assert_false AttrTestReader.respond_to?(:cattr=) + assert_false attr_instance.respond_to?(:iattr=) + assert_false attr_instance.respond_to?(:iattr2=) + + attr_instance.iattr_val = 'test' + assert_equal 'test', attr_instance.iattr + + AttrTestReader.cattr_val = 'test' + assert_equal 'test', AttrTestReader.cattr +end + +assert('Module#attr_writer', '15.2.2.4.14') do + class AttrTestWriter + class << self + attr_writer :cattr + def cattr_val + @cattr + end + end + attr_writer :iattr, 'iattr2' + def iattr_val + @iattr + end + end + + attr_instance = AttrTestWriter.new + assert_true AttrTestWriter.respond_to?(:cattr=) + assert_true attr_instance.respond_to?(:iattr=) + assert_true attr_instance.respond_to?(:iattr2=) + + assert_false AttrTestWriter.respond_to?(:cattr) + assert_false attr_instance.respond_to?(:iattr) + assert_false attr_instance.respond_to?(:iattr2) + + attr_instance.iattr = 'test' + assert_equal 'test', attr_instance.iattr_val + + AttrTestWriter.cattr = 'test' + assert_equal 'test', AttrTestWriter.cattr_val end assert('Module#class_eval', '15.2.2.4.15') do @@ -48,9 +187,9 @@ assert('Module#class_eval', '15.2.2.4.15') do end r = Test4ClassEval.instance_methods - assert_equal Test4ClassEval.class_eval{ @a }, 11 - assert_equal Test4ClassEval.class_eval{ @b }, 12 - assert_equal r.class, Array + assert_equal 11, Test4ClassEval.class_eval{ @a } + assert_equal 12, Test4ClassEval.class_eval{ @b } + assert_equal Array, r.class assert_true r.include?(:method1) end @@ -68,7 +207,7 @@ assert('Module#class_variable_get', '15.2.2.4.17') do @@cv = 99 end - assert_equal Test4ClassVariableGet.class_variable_get(:@@cv), 99 + assert_equal 99, Test4ClassVariableGet.class_variable_get(:@@cv) end assert('Module#class_variable_set', '15.2.2.4.18') do @@ -82,8 +221,8 @@ assert('Module#class_variable_set', '15.2.2.4.18') do assert_true Test4ClassVariableSet.class_variable_set(:@@cv, 99) assert_true Test4ClassVariableSet.class_variable_set(:@@foo, 101) assert_true Test4ClassVariableSet.class_variables.include? :@@cv - assert_equal Test4ClassVariableSet.class_variable_get(:@@cv), 99 - assert_equal Test4ClassVariableSet.new.foo, 101 + assert_equal 99, Test4ClassVariableSet.class_variable_get(:@@cv) + assert_equal 101, Test4ClassVariableSet.new.foo end assert('Module#class_variables', '15.2.2.4.19') do @@ -94,8 +233,8 @@ assert('Module#class_variables', '15.2.2.4.19') do @@var2 = 2 end - assert_equal Test4ClassVariables1.class_variables, [:@@var1] - assert_equal Test4ClassVariables2.class_variables, [:@@var2, :@@var1] + assert_equal [:@@var1], Test4ClassVariables1.class_variables + assert_equal [:@@var2, :@@var1], Test4ClassVariables2.class_variables end assert('Module#const_defined?', '15.2.2.4.20') do @@ -112,7 +251,7 @@ assert('Module#const_get', '15.2.2.4.21') do Const4Test4ConstGet = 42 end - assert_equal Test4ConstGet.const_get(:Const4Test4ConstGet), 42 + assert_equal 42, Test4ConstGet.const_get(:Const4Test4ConstGet) end assert('Module.const_missing', '15.2.2.4.22') do @@ -122,7 +261,7 @@ assert('Module.const_missing', '15.2.2.4.22') do end end - assert_equal Test4ConstMissing.const_get(:ConstDoesntExist), 42 + assert_equal 42, Test4ConstMissing.const_get(:ConstDoesntExist) end assert('Module#const_get', '15.2.2.4.23') do @@ -131,7 +270,7 @@ assert('Module#const_get', '15.2.2.4.23') do end assert_true Test4ConstSet.const_set(:Const4Test4ConstSet, 23) - assert_equal Test4ConstSet.const_get(:Const4Test4ConstSet), 23 + assert_equal 23, Test4ConstSet.const_get(:Const4Test4ConstSet) end assert('Module.constants', '15.2.2.4.24') do @@ -145,8 +284,8 @@ assert('Module.constants', '15.2.2.4.24') do $n = constants.sort end - assert_equal TestA.constants, [ :Const ] - assert_equal $n, [ :Const, :Const2 ] + assert_equal [ :Const ], TestA.constants + assert_equal [ :Const, :Const2 ], $n end assert('Module#include', '15.2.2.4.27') do @@ -157,7 +296,7 @@ assert('Module#include', '15.2.2.4.27') do include Test4Include end - assert_equal Test4Include2.const_get(:Const4Include), 42 + assert_equal 42, Test4Include2.const_get(:Const4Include) end assert('Module#include?', '15.2.2.4.28') do @@ -185,8 +324,8 @@ assert('Module#included', '15.2.2.4.29') do include Test4Included end - assert_equal Test4Included2.const_get(:Const4Included), 42 - assert_equal Test4Included2.const_get(:Const4Included2), Test4Included2 + assert_equal 42, Test4Included2.const_get(:Const4Included) + assert_equal Test4Included2, Test4Included2.const_get(:Const4Included2) end assert('Module#included_modules', '15.2.2.4.30') do @@ -197,7 +336,7 @@ assert('Module#included_modules', '15.2.2.4.30') do end r = Test4includedModules2.included_modules - assert_equal r.class, Array + assert_equal Array, r.class assert_true r.include?(Test4includedModules) end @@ -214,10 +353,10 @@ assert('Module#instance_methods', '15.2.2.4.33') do r = Test4InstanceMethodsC.instance_methods(true) - assert_equal Test4InstanceMethodsA.instance_methods, [:method1] - assert_equal Test4InstanceMethodsB.instance_methods(false), [:method2] - assert_equal Test4InstanceMethodsC.instance_methods(false), [:method3] - assert_equal r.class, Array + assert_equal [:method1], Test4InstanceMethodsA.instance_methods + assert_equal [:method2], Test4InstanceMethodsB.instance_methods(false) + assert_equal [:method3], Test4InstanceMethodsC.instance_methods(false) + assert_equal Array, r.class assert_true r.include?(:method3) assert_true r.include?(:method2) end @@ -252,8 +391,8 @@ assert('Module#module_eval', '15.2.2.4.35') do @b = 12 end - assert_equal Test4ModuleEval.module_eval{ @a }, 11 - assert_equal Test4ModuleEval.module_eval{ @b }, 12 + assert_equal 11, Test4ModuleEval.module_eval{ @a } + assert_equal 12, Test4ModuleEval.module_eval{ @b } end assert('Module#remove_class_variable', '15.2.2.4.39') do @@ -261,7 +400,7 @@ assert('Module#remove_class_variable', '15.2.2.4.39') do @@cv = 99 end - assert_equal Test4RemoveClassVariable.remove_class_variable(:@@cv), 99 + assert_equal 99, Test4RemoveClassVariable.remove_class_variable(:@@cv) assert_false Test4RemoveClassVariable.class_variables.include? :@@cv end @@ -282,7 +421,7 @@ assert('Module#remove_const', '15.2.2.4.40') do # Constant removed from Module assert_false Test4RemoveConst.const_defined? :ExistingConst # Return value of binding - assert_equal result, 23 + assert_equal 23, result # Name Error raised when Constant doesn't exist assert_true name_error end @@ -333,12 +472,34 @@ assert('Module#to_s') do module Test4to_sModules end - assert_equal Test4to_sModules.to_s, 'Test4to_sModules' + assert_equal 'Test4to_sModules', Test4to_sModules.to_s end assert('Module#inspect') do module Test4to_sModules end - assert_equal Test4to_sModules.inspect, 'Test4to_sModules' + assert_equal 'Test4to_sModules', Test4to_sModules.inspect +end + +assert('Issue 1467') do + module M1 + def initialize() + super() + end + end + + class C1 + include M1 + def initialize() + super() + end + end + + class C2 + include M1 + end + + C1.new + C2.new end diff --git a/test/t/nameerror.rb b/test/t/nameerror.rb index 8b54f3c52..644c6a3cb 100644 --- a/test/t/nameerror.rb +++ b/test/t/nameerror.rb @@ -2,11 +2,11 @@ # NameError ISO Test assert('NameError', '15.2.31') do - assert_equal NameError.class, Class + assert_equal Class, NameError.class end assert('NameError superclass', '15.2.31.2') do - assert_equal NameError.superclass, StandardError + assert_equal StandardError, NameError.superclass end assert('NameError#name', '15.2.31.2.1') do @@ -26,7 +26,7 @@ end assert('NameError#initialize', '15.2.31.2.2') do e = NameError.new('a', :foo) - assert_equal e.class, NameError - assert_equal e.message, 'a' - assert_equal e.name, :foo + assert_equal NameError, e.class + assert_equal 'a', e.message + assert_equal :foo, e.name end diff --git a/test/t/nil.rb b/test/t/nil.rb index 08d2acdf1..443178c81 100644 --- a/test/t/nil.rb +++ b/test/t/nil.rb @@ -2,7 +2,7 @@ # NilClass ISO Test assert('NilClass', '15.2.4') do - assert_equal NilClass.class, Class + assert_equal Class, NilClass.class end assert('NilClass#&', '15.2.4.3.1') do @@ -25,5 +25,5 @@ assert('NilClass#nil?', '15.2.4.3.4') do end assert('NilClass#to_s', '15.2.4.3.5') do - assert_equal nil.to_s, '' + assert_equal '', nil.to_s end diff --git a/test/t/nomethoderror.rb b/test/t/nomethoderror.rb index 2b1ac8e88..561e545f9 100644 --- a/test/t/nomethoderror.rb +++ b/test/t/nomethoderror.rb @@ -9,5 +9,5 @@ assert('NoMethodError', '15.2.32') do end assert('NoMethodError superclass', '15.2.32.2') do - assert_equal NoMethodError.superclass, NameError + assert_equal NameError, NoMethodError.superclass end diff --git a/test/t/numeric.rb b/test/t/numeric.rb index 1fa92b662..7dfec3e82 100644 --- a/test/t/numeric.rb +++ b/test/t/numeric.rb @@ -2,11 +2,11 @@ # Numeric ISO Test assert('Numeric', '15.2.7') do - assert_equal Numeric.class, Class + assert_equal Class, Numeric.class end assert('Numeric superclass', '15.2.7.2') do - assert_equal Numeric.superclass, Object + assert_equal Object, Numeric.superclass end assert('Numeric#+@', '15.2.7.4.1') do @@ -18,12 +18,12 @@ assert('Numeric#-@', '15.2.7.4.2') do end assert('Numeric#abs', '15.2.7.4.3') do - assert_equal(1.abs, 1) - assert_equal(-1.abs, 1.0) + assert_equal(1, 1.abs) + assert_equal(1.0, -1.abs) end # Not ISO specified assert('Numeric#**') do - assert_equal 2.0**3, 8.0 + assert_equal 8.0, 2.0**3 end diff --git a/test/t/object.rb b/test/t/object.rb index 2586f1761..6a755d3ba 100644 --- a/test/t/object.rb +++ b/test/t/object.rb @@ -2,10 +2,10 @@ # Object ISO Test assert('Object', '15.2.1') do - assert_equal Object.class, Class + assert_equal Class, Object.class end assert('Object superclass', '15.2.1.2') do - assert_equal Object.superclass, BasicObject + assert_equal BasicObject, Object.superclass end diff --git a/test/t/proc.rb b/test/t/proc.rb index 56bab8c64..4227772dd 100644 --- a/test/t/proc.rb +++ b/test/t/proc.rb @@ -2,11 +2,11 @@ # Proc ISO Test assert('Proc', '15.2.17') do - assert_equal Proc.class, Class + assert_equal Class, Proc.class end assert('Proc superclass', '15.2.17.2') do - assert_equal Proc.superclass, Object + assert_equal Object, Proc.superclass end assert('Proc.new', '15.2.17.3.1') do @@ -36,10 +36,10 @@ assert('Proc#arity', '15.2.17.4.2') do c = Proc.new {|x=0, y|}.arity d = Proc.new {|(x, y), z=0|}.arity - assert_equal a, 2 - assert_equal b, -3 - assert_equal c, 1 - assert_equal d, 1 + assert_equal 2, a + assert_equal(-3, b) + assert_equal 1, c + assert_equal 1, d end assert('Proc#call', '15.2.17.4.3') do @@ -51,6 +51,6 @@ assert('Proc#call', '15.2.17.4.3') do b2 = Proc.new { |i| a2 += i } b2.call(5) - assert_equal a, 1 - assert_equal a2, 5 + assert_equal 1, a + assert_equal 5, a2 end diff --git a/test/t/range.rb b/test/t/range.rb index da25abc0e..d7294cc1b 100644 --- a/test/t/range.rb +++ b/test/t/range.rb @@ -2,11 +2,11 @@ # Range ISO Test assert('Range', '15.2.14') do - assert_equal Range.class, Class + assert_equal Class, Range.class end assert('Range superclass', '15.2.14.2') do - assert_equal Range.superclass, Object + assert_equal Object, Range.superclass end assert('Range#==', '15.2.14.4.1') do @@ -23,18 +23,18 @@ assert('Range#===', '15.2.14.4.2') do end assert('Range#begin', '15.2.14.4.3') do - assert_equal (1..10).begin, 1 + assert_equal 1, (1..10).begin end assert('Range#each', '15.2.14.4.4') do a = (1..3) b = 0 a.each {|i| b += i} - assert_equal b, 6 + assert_equal 6, b end assert('Range#end', '15.2.14.4.5') do - assert_equal (1..10).end, 10 + assert_equal 10, (1..10).end end assert('Range#exclude_end?', '15.2.14.4.6') do @@ -43,7 +43,7 @@ assert('Range#exclude_end?', '15.2.14.4.6') do end assert('Range#first', '15.2.14.4.7') do - assert_equal (1..10).first, 1 + assert_equal 1, (1..10).first end assert('Range#include', '15.2.14.4.8') do @@ -57,14 +57,14 @@ assert('Range#initialize', '15.2.14.4.9') do a = Range.new(1, 10, true) b = Range.new(1, 10, false) - assert_equal a, (1...10) + assert_equal (1...10), a assert_true a.exclude_end? - assert_equal b, (1..10) + assert_equal (1..10), b assert_false b.exclude_end? end assert('Range#last', '15.2.14.4.10') do - assert_equal (1..10).last, 10 + assert_equal 10, (1..10).last end assert('Range#member?', '15.2.14.4.11') do diff --git a/test/t/rangeerror.rb b/test/t/rangeerror.rb index 2a15ce225..8dc683745 100644 --- a/test/t/rangeerror.rb +++ b/test/t/rangeerror.rb @@ -2,9 +2,9 @@ # RangeError ISO Test assert('RangeError', '15.2.26') do - assert_equal RangeError.class, Class + assert_equal Class, RangeError.class end assert('RangeError superclass', '15.2.26.2') do - assert_equal RangeError.superclass, StandardError + assert_equal StandardError, RangeError.superclass end diff --git a/test/t/runtimeerror.rb b/test/t/runtimeerror.rb index b8d64a726..d02cba96d 100644 --- a/test/t/runtimeerror.rb +++ b/test/t/runtimeerror.rb @@ -2,5 +2,5 @@ # RuntimeError ISO Test assert('RuntimeError', '15.2.28') do - assert_equal RuntimeError.class, Class + assert_equal Class, RuntimeError.class end diff --git a/test/t/standarderror.rb b/test/t/standarderror.rb index 42b06d911..cab99834e 100644 --- a/test/t/standarderror.rb +++ b/test/t/standarderror.rb @@ -2,9 +2,9 @@ # StandardError ISO Test assert('StandardError', '15.2.23') do - assert_equal StandardError.class, Class + assert_equal Class, StandardError.class end assert('StandardError superclass', '15.2.23.2') do - assert_equal StandardError.superclass, Exception + assert_equal Exception, StandardError.superclass end diff --git a/test/t/string.rb b/test/t/string.rb index c208835b4..4c3689b3a 100644 --- a/test/t/string.rb +++ b/test/t/string.rb @@ -2,40 +2,40 @@ # String ISO Test assert('String', '15.2.10') do - assert_equal String.class, Class + assert_equal Class, String.class end assert('String superclass', '15.2.10.2') do - assert_equal String.superclass, Object + assert_equal Object, String.superclass end -assert('String#*', '15.2.10.5.1') do - assert_equal 'a' * 5, 'aaaaa' -end - -assert('String#+', '15.2.10.5.2') do - assert_equal 'a' + 'b', 'ab' -end - -assert('String#<=>', '15.2.10.5.3') do +assert('String#<=>', '15.2.10.5.1') do a = '' <=> '' b = '' <=> 'not empty' c = 'not empty' <=> '' d = 'abc' <=> 'cba' e = 'cba' <=> 'abc' - assert_equal a, 0 - assert_equal b, -1 - assert_equal c, 1 - assert_equal d, -1 - assert_equal e, 1 + assert_equal 0, a + assert_equal(-1, b) + assert_equal 1, c + assert_equal(-1, d) + assert_equal 1, e end -assert('String#==', '15.2.10.5.4') do +assert('String#==', '15.2.10.5.2') do assert_equal 'abc', 'abc' assert_not_equal 'abc', 'cba' end +assert('String#+', '15.2.10.5.4') do + assert_equal 'ab', 'a' + 'b' +end + +assert('String#*', '15.2.10.5.5') do + assert_equal 'aaaaa', 'a' * 5 +end + # 'String#=~', '15.2.10.5.5' will be tested in mrbgems. assert('String#[]', '15.2.10.5.6') do @@ -59,16 +59,16 @@ assert('String#[]', '15.2.10.5.6') do a3 = 'abc'['bc'] b3 = 'abc'['XX'] - assert_equal a, 'a' - assert_equal b, 'c' + assert_equal 'a', a + assert_equal 'c', b assert_nil c assert_nil d assert_nil a1 assert_nil b1 assert_nil c1 - assert_equal d1, '' - assert_equal e1, 'bc' - assert_equal a3, 'bc' + assert_equal '', d1 + assert_equal 'bc', e1 + assert_equal 'bc', a3 assert_nil b3 end @@ -92,23 +92,23 @@ assert('String#[] with Range') do h2 = 'abc'[3...4] i2 = 'abc'[4...5] - assert_equal a1, '' - assert_equal b1, 'b' - assert_equal c1, 'bc' - assert_equal d1, 'bc' - assert_equal e1, 'bc' - assert_equal f1, 'ab' - assert_equal g1, 'bc' - assert_equal h1, '' + assert_equal '', a1 + assert_equal 'b', b1 + assert_equal 'bc', c1 + assert_equal 'bc', d1 + assert_equal 'bc', e1 + assert_equal 'ab', f1 + assert_equal 'bc', g1 + assert_equal '', h1 assert_nil i2 - assert_equal a2, '' - assert_equal b2, '' - assert_equal c2, 'b' - assert_equal d2, 'bc' - assert_equal e2, 'bc' - assert_equal f2, 'a' - assert_equal g2, 'bc' - assert_equal h2, '' + assert_equal '', a2 + assert_equal '', b2 + assert_equal 'b', c2 + assert_equal 'bc', d2 + assert_equal 'bc', e2 + assert_equal 'a', f2 + assert_equal 'bc', g2 + assert_equal '', h2 assert_nil i2 end @@ -116,15 +116,15 @@ assert('String#capitalize', '15.2.10.5.7') do a = 'abc' a.capitalize - assert_equal a, 'abc' - assert_equal 'abc'.capitalize, 'Abc' + assert_equal 'abc', a + assert_equal 'Abc', 'abc'.capitalize end assert('String#capitalize!', '15.2.10.5.8') do a = 'abc' a.capitalize! - assert_equal a, 'Abc' + assert_equal 'Abc', a end assert('String#chomp', '15.2.10.5.9') do @@ -137,12 +137,12 @@ assert('String#chomp', '15.2.10.5.9') do f.chomp - assert_equal a, 'abc' - assert_equal b, '' - assert_equal c, 'abc' - assert_equal d, "abc\n" - assert_equal e, 'abc' - assert_equal f, "abc\n" + assert_equal 'abc', a + assert_equal '', b + assert_equal 'abc', c + assert_equal "abc\n", d + assert_equal 'abc', e + assert_equal "abc\n", f end assert('String#chomp!', '15.2.10.5.10') do @@ -158,11 +158,11 @@ assert('String#chomp!', '15.2.10.5.10') do d.chomp! e.chomp!("\t") - assert_equal a, 'abc' - assert_equal b, '' - assert_equal c, 'abc' - assert_equal d, "abc\n" - assert_equal e, 'abc' + assert_equal 'abc', a + assert_equal '', b + assert_equal 'abc', c + assert_equal "abc\n", d + assert_equal 'abc', e end assert('String#chop', '15.2.10.5.11') do @@ -172,9 +172,9 @@ assert('String#chop', '15.2.10.5.11') do c.chop - assert_equal a, '' - assert_equal b, 'ab' - assert_equal c, 'abc' + assert_equal '', a + assert_equal 'ab', b + assert_equal 'abc', c end assert('String#chop!', '15.2.10.5.12') do @@ -194,8 +194,8 @@ assert('String#downcase', '15.2.10.5.13') do b.downcase - assert_equal a, 'abc' - assert_equal b, 'ABC' + assert_equal 'abc', a + assert_equal 'ABC', b end assert('String#downcase!', '15.2.10.5.14') do @@ -203,7 +203,7 @@ assert('String#downcase!', '15.2.10.5.14') do a.downcase! - assert_equal a, 'abc' + assert_equal 'abc', a end assert('String#each_line', '15.2.10.5.15') do @@ -249,14 +249,14 @@ assert('String#gsub!', '15.2.10.5.19') do b = 'abcabc' b.gsub!('b') { |w| w.capitalize } - assert_equal a, 'aBcaBc' - assert_equal b, 'aBcaBc' + assert_equal 'aBcaBc', a + assert_equal 'aBcaBc', b end assert('String#hash', '15.2.10.5.20') do a = 'abc' - assert_equal a.hash, 'abc'.hash + assert_equal 'abc'.hash, a.hash end assert('String#include?', '15.2.10.5.21') do @@ -267,31 +267,31 @@ assert('String#include?', '15.2.10.5.21') do end assert('String#index', '15.2.10.5.22') do - assert_equal 'abc'.index('a'), 0 + assert_equal 0, 'abc'.index('a') assert_nil 'abc'.index('d') - assert_equal 'abcabc'.index('a', 1), 3 + assert_equal 3, 'abcabc'.index('a', 1) end assert('String#initialize', '15.2.10.5.23') do a = '' a.initialize('abc') - assert_equal a, 'abc' + assert_equal 'abc', a end assert('String#initialize_copy', '15.2.10.5.24') do a = '' a.initialize_copy('abc') - assert_equal a, 'abc' + assert_equal 'abc', a end assert('String#intern', '15.2.10.5.25') do - assert_equal 'abc'.intern, :abc + assert_equal :abc, 'abc'.intern end assert('String#length', '15.2.10.5.26') do - assert_equal 'abc'.length, 3 + assert_equal 3, 'abc'.length end # 'String#match', '15.2.10.5.27' will be tested in mrbgems. @@ -300,36 +300,36 @@ assert('String#replace', '15.2.10.5.28') do a = '' a.replace('abc') - assert_equal a, 'abc' + assert_equal 'abc', a end assert('String#reverse', '15.2.10.5.29') do a = 'abc' a.reverse - assert_equal a, 'abc' - assert_equal 'abc'.reverse, 'cba' + assert_equal 'abc', a + assert_equal 'cba', 'abc'.reverse end assert('String#reverse!', '15.2.10.5.30') do a = 'abc' a.reverse! - assert_equal a, 'cba' - assert_equal 'abc'.reverse!, 'cba' + assert_equal 'cba', a + assert_equal 'cba', 'abc'.reverse! end assert('String#rindex', '15.2.10.5.31') do - assert_equal 'abc'.rindex('a'), 0 + assert_equal 0, 'abc'.rindex('a') assert_nil 'abc'.rindex('d') - assert_equal 'abcabc'.rindex('a', 1), 0 - assert_equal 'abcabc'.rindex('a', 4), 3 + assert_equal 0, 'abcabc'.rindex('a', 1) + assert_equal 3, 'abcabc'.rindex('a', 4) end # 'String#scan', '15.2.10.5.32' will be tested in mrbgems. assert('String#size', '15.2.10.5.33') do - assert_equal 'abc'.size, 3 + assert_equal 3, 'abc'.size end assert('String#slice', '15.2.10.5.34') do @@ -356,33 +356,33 @@ assert('String#slice', '15.2.10.5.34') do a3 = 'abc'.slice('bc') b3 = 'abc'.slice('XX') - assert_equal a, 'a' - assert_equal b, 'c' + assert_equal 'a', a + assert_equal 'c', b assert_nil c assert_nil d assert_nil a1 assert_nil b1 assert_nil c1 - assert_equal d1, '' - assert_equal e1, 'bc' - assert_equal e11, 'b' - assert_equal a3, 'bc' + assert_equal '', d1 + assert_equal 'bc', e1 + assert_equal 'b', e11 + assert_equal 'bc', a3 assert_nil b3 end # TODO Broken ATM assert('String#split', '15.2.10.5.35') do # without RegExp behavior is actually unspecified - assert_equal 'abc abc abc'.split, ['abc', 'abc', 'abc'] - assert_equal 'a,b,c,,d'.split(','), ["a", "b", "c", "", "d"] - assert_equal 'abc abc abc'.split(nil), ['abc', 'abc', 'abc'] - assert_equal 'abc'.split(""), ['a', 'b', 'c'] + assert_equal ['abc', 'abc', 'abc'], 'abc abc abc'.split + assert_equal ["a", "b", "c", "", "d"], 'a,b,c,,d'.split(',') + assert_equal ['abc', 'abc', 'abc'], 'abc abc abc'.split(nil) + assert_equal ['a', 'b', 'c'], 'abc'.split("") end assert('String#sub', '15.2.10.5.36') do - assert_equal 'abcabc'.sub('b', 'B'), 'aBcabc' - assert_equal 'abcabc'.sub('b') { |w| w.capitalize }, 'aBcabc' - assert_equal 'aa#'.sub('#', '$'), 'aa$' + assert_equal 'aBcabc', 'abcabc'.sub('b', 'B') + assert_equal 'aBcabc', 'abcabc'.sub('b') { |w| w.capitalize } + assert_equal 'aa$', 'aa#'.sub('#', '$') end assert('String#sub!', '15.2.10.5.37') do @@ -392,39 +392,38 @@ assert('String#sub!', '15.2.10.5.37') do b = 'abcabc' b.sub!('b') { |w| w.capitalize } - assert_equal a, 'aBcabc' - assert_equal b, 'aBcabc' + assert_equal 'aBcabc', a + assert_equal 'aBcabc', b end +assert('String#to_f', '15.2.10.5.38') do + a = ''.to_f + b = '123456789'.to_f + c = '12345.6789'.to_f -assert('String#to_i', '15.2.10.5.38') do + assert_float(0.0, a) + assert_float(123456789.0, b) + assert_float(12345.6789, c) +end + +assert('String#to_i', '15.2.10.5.39') do a = ''.to_i b = '32143'.to_i c = 'a'.to_i(16) d = '100'.to_i(2) - assert_equal a, 0 - assert_equal b, 32143 - assert_equal c, 10 - assert_equal d, 4 -end - -assert('String#to_f', '15.2.10.5.39') do - a = ''.to_f - b = '123456789'.to_f - c = '12345.6789'.to_f - - assert_float(a, 0.0) - assert_float(b, 123456789.0) - assert_float(c, 12345.6789) + assert_equal 0, a + assert_equal 32143, b + assert_equal 10, c + assert_equal 4, d end assert('String#to_s', '15.2.10.5.40') do - assert_equal 'abc'.to_s, 'abc' + assert_equal 'abc', 'abc'.to_s end assert('String#to_sym', '15.2.10.5.41') do - assert_equal 'abc'.to_sym, :abc + assert_equal :abc, 'abc'.to_sym end assert('String#upcase', '15.2.10.5.42') do @@ -433,8 +432,8 @@ assert('String#upcase', '15.2.10.5.42') do b.upcase - assert_equal a, 'ABC' - assert_equal b, 'abc' + assert_equal 'ABC', a + assert_equal 'abc', b end assert('String#upcase!', '15.2.10.5.43') do @@ -442,14 +441,14 @@ assert('String#upcase!', '15.2.10.5.43') do a.upcase! - assert_equal a, 'ABC' + assert_equal 'ABC', a end # Not ISO specified assert('String interpolation (mrb_str_concat for shared strings)') do a = "A" * 32 - assert_equal "#{a}:", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA:" + assert_equal "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA:", "#{a}:" end assert('Check the usage of a NUL character') do @@ -463,8 +462,8 @@ assert('String#bytes') do str2 = "\xFF" bytes2 = [0xFF] - assert_equal str1.bytes, bytes1 - assert_equal str2.bytes, bytes2 + assert_equal bytes1, str1.bytes + assert_equal bytes2, str2.bytes end assert('String#each_byte') do @@ -479,5 +478,5 @@ end assert('String#inspect') do ("\1" * 100).inspect # should not raise an exception - regress #1210 - assert_equal "\0".inspect, "\"\\000\"" + assert_equal "\"\\000\"", "\0".inspect end diff --git a/test/t/symbol.rb b/test/t/symbol.rb index e4ced61e0..f852dcd00 100644 --- a/test/t/symbol.rb +++ b/test/t/symbol.rb @@ -2,11 +2,11 @@ # Symbol ISO Test assert('Symbol', '15.2.11') do - assert_equal Symbol.class, Class + assert_equal Class, Symbol.class end assert('Symbol superclass', '15.2.11.2') do - assert_equal Symbol.superclass, Object + assert_equal Object, Symbol.superclass end assert('Symbol#===', '15.2.11.3.1') do @@ -15,13 +15,13 @@ assert('Symbol#===', '15.2.11.3.1') do end assert('Symbol#id2name', '15.2.11.3.2') do - assert_equal :abc.id2name, 'abc' + assert_equal 'abc', :abc.id2name end assert('Symbol#to_s', '15.2.11.3.3') do - assert_equal :abc.to_s, 'abc' + assert_equal 'abc', :abc.to_s end assert('Symbol#to_sym', '15.2.11.3.4') do - assert_equal :abc.to_sym, :abc + assert_equal :abc, :abc.to_sym end diff --git a/test/t/syntax.rb b/test/t/syntax.rb index 332cfcca8..c87a81e06 100644 --- a/test/t/syntax.rb +++ b/test/t/syntax.rb @@ -22,7 +22,7 @@ assert('super', '11.3.4') do bar = SuperBar.new assert_true bar.foo - assert_equal bar.bar(1,2,3), [1,2,3] + assert_equal [1,2,3], bar.bar(1,2,3) end assert('yield', '11.3.5') do @@ -37,9 +37,9 @@ assert('Abbreviated variable assignment', '11.4.2.3.2') do c = 1 c += 2 - assert_equal a, 1 + assert_equal 1, a assert_nil b - assert_equal c, 3 + assert_equal 3, c end assert('Nested const reference') do @@ -51,8 +51,8 @@ assert('Nested const reference') do end end end - assert_equal Syntax4Const::CONST1, "hello world" - assert_equal Syntax4Const::Const2.new.const1, "hello world" + assert_equal "hello world", Syntax4Const::CONST1 + assert_equal "hello world", Syntax4Const::Const2.new.const1 end assert('Abbreviated variable assignment as returns') do @@ -63,5 +63,14 @@ assert('Abbreviated variable assignment as returns') do end end end - assert_equal Syntax4AbbrVarAsgnAsReturns::A.new.b, 1 + assert_equal 1, Syntax4AbbrVarAsgnAsReturns::A.new.b +end + +assert('Splat and mass assignment') do + *a = *[1,2,3] + b, *c = *[7,8,9] + + assert_equal [1,2,3], a + assert_equal 7, b + assert_equal [8,9], c end diff --git a/test/t/true.rb b/test/t/true.rb index 9ea68f81b..3aebf43a1 100644 --- a/test/t/true.rb +++ b/test/t/true.rb @@ -2,11 +2,11 @@ # TrueClass ISO Test assert('TrueClass', '15.2.5') do - assert_equal TrueClass.class, Class + assert_equal Class, TrueClass.class end assert('TrueClass superclass', '15.2.5.2') do - assert_equal TrueClass.superclass, Object + assert_equal Object, TrueClass.superclass end assert('TrueClass true', '15.2.5.1') do @@ -24,7 +24,7 @@ assert('TrueClass#^', '15.2.5.3.2') do end assert('TrueClass#to_s', '15.2.5.3.3') do - assert_equal true.to_s, 'true' + assert_equal 'true', true.to_s end assert('TrueClass#|', '15.2.5.3.4') do diff --git a/test/t/typeerror.rb b/test/t/typeerror.rb index 59c3ea993..a91fb1be2 100644 --- a/test/t/typeerror.rb +++ b/test/t/typeerror.rb @@ -2,10 +2,10 @@ # TypeError ISO Test assert('TypeError', '15.2.29') do - assert_equal TypeError.class, Class + assert_equal Class, TypeError.class end assert('TypeError superclass', '15.2.29.2') do - assert_equal TypeError.superclass, StandardError + assert_equal StandardError, TypeError.superclass end diff --git a/tools/mrbc/mrbc.c b/tools/mrbc/mrbc.c index 0a165b78a..735b22b1d 100644 --- a/tools/mrbc/mrbc.c +++ b/tools/mrbc/mrbc.c @@ -51,15 +51,19 @@ usage(const char *name) static char * get_outfilename(mrb_state *mrb, char *infile, char *ext) { + size_t infilelen; + size_t extlen; char *outfile; char *p; - outfile = (char*)mrb_malloc(mrb, strlen(infile) + strlen(ext) + 1); - strcpy(outfile, infile); + infilelen = strlen(infile); + extlen = strlen(ext); + outfile = (char*)mrb_malloc(mrb, infilelen + extlen + 1); + memcpy(outfile, infile, infilelen + 1); if (*ext) { if ((p = strrchr(outfile, '.')) == NULL) - p = &outfile[strlen(outfile)]; - strcpy(p, ext); + p = outfile + infilelen; + memcpy(p, ext, extlen + 1); } return outfile; @@ -117,6 +121,8 @@ parse_args(mrb_state *mrb, int argc, char **argv, struct mrbc_args *args) case 'g': args->debug_info = 1; break; + case 'h': + return -1; case '-': if (argv[i][1] == '\n') { return i; diff --git a/travis_config.rb b/travis_config.rb index 040b1a255..bd2939f94 100644 --- a/travis_config.rb +++ b/travis_config.rb @@ -3,4 +3,5 @@ MRuby::Build.new do |conf| # include all core GEMs conf.gembox 'full-core' + conf.cc.defines = %w(MRB_DEBUG) end |
