From c131f69d21eebaf49df288326fdd09aac51b709b Mon Sep 17 00:00:00 2001 From: yui-knk Date: Sat, 31 May 2014 14:10:30 +0900 Subject: Remove space. --- src/parse.y | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parse.y b/src/parse.y index c6795fb65..719e78218 100644 --- a/src/parse.y +++ b/src/parse.y @@ -5856,7 +5856,7 @@ mrb_parser_dump(mrb_state *mrb, node *tree, int offset) { node *n2 = tree->car; - if (n2 && (n2->car || n2->cdr)) { + if (n2 && (n2->car || n2->cdr)) { dump_prefix(offset+1); printf("local variables:\n"); dump_prefix(offset+2); -- cgit v1.2.3 From 36ab17d8ce7ff0b481d8217bd3a389b1eab28f22 Mon Sep 17 00:00:00 2001 From: katmutua Date: Sat, 31 May 2014 10:23:29 -0300 Subject: Fix typos in gc --- src/gc.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/gc.c b/src/gc.c index 4478b71f3..171f0858d 100644 --- a/src/gc.c +++ b/src/gc.c @@ -34,7 +34,7 @@ == Two White Types - There're two white color types in a flip-flop fassion: White-A and White-B, + There're two white color types in a flip-flop fashion: 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). @@ -43,8 +43,8 @@ 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. + painting them as White-B, just switch the meaning of White-A and White-B as + this will 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 @@ -73,8 +73,8 @@ 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: + sweep phase, instead of painting them White. The key ideas are still the same + as 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 @@ -82,7 +82,7 @@ * Major GC - same as a full regular GC cycle. - The difference to a "traditional" generational GC is, that the major GC + The difference from "traditional" generational GC is, that the major GC in mruby is triggered incrementally in a tri-color manner. -- cgit v1.2.3 From e413294488f28e05557061b751e9114e84b656c9 Mon Sep 17 00:00:00 2001 From: yui-knk Date: Sun, 1 Jun 2014 22:04:51 +0900 Subject: Remove space. --- src/vm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vm.c b/src/vm.c index d604e5d9e..41b5ca57f 100644 --- a/src/vm.c +++ b/src/vm.c @@ -811,7 +811,7 @@ RETRY_TRY_BLOCK: CASE(OP_LOADL) { /* A Bx R(A) := Pool(Bx) */ - regs[GETARG_A(i)] = pool[GETARG_Bx(i)]; + regs[GETARG_A(i)] = pool[GETARG_Bx(i)]; NEXT; } -- cgit v1.2.3 From 15ab1b7ab07a9c3634e6c79a43ceefc6bea4b22b Mon Sep 17 00:00:00 2001 From: yui-knk Date: Sun, 1 Jun 2014 22:07:28 +0900 Subject: Fix indent. --- src/parse.y | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/parse.y b/src/parse.y index 719e78218..93923aa71 100644 --- a/src/parse.y +++ b/src/parse.y @@ -2875,15 +2875,15 @@ var_ref : variable | keyword_self { $$ = new_self(p); - } + } | keyword_true { $$ = new_true(p); - } + } | keyword_false { $$ = new_false(p); - } + } | keyword__FILE__ { if (!p->filename) { -- cgit v1.2.3 From de8916b1f2a3835f06c8a1b10ba3a5b40bc4441e Mon Sep 17 00:00:00 2001 From: Carson McDonald Date: Sun, 1 Jun 2014 09:51:21 -0400 Subject: Fix mgem test args passing --- tasks/mrbgem_spec.rake | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tasks/mrbgem_spec.rake b/tasks/mrbgem_spec.rake index 67edffbc5..36dd8f9c1 100644 --- a/tasks/mrbgem_spec.rake +++ b/tasks/mrbgem_spec.rake @@ -44,7 +44,7 @@ module MRuby end def run_test_in_other_mrb_state? - not test_preload.nil? or not test_objs.empty? + not test_preload.nil? or not test_objs.empty? or not test_args.empty? end def setup @@ -182,6 +182,7 @@ module MRuby f.puts %Q[#include "mruby/irep.h"] f.puts %Q[#include "mruby/string.h"] f.puts %Q[#include "mruby/variable.h"] + f.puts %Q[#include "mruby/hash.h"] unless test_args.empty? end def version_ok?(req_versions) -- cgit v1.2.3 From cbe73e93f7623e4f93ad0085a616cb7764043ef0 Mon Sep 17 00:00:00 2001 From: Jun Hiroe Date: Mon, 2 Jun 2014 22:00:05 +0900 Subject: Add comments in String#lstrip, rstrip, strip, lstring_bang, rstrip_bang and strip_bang --- mrbgems/mruby-string-ext/mrblib/string.rb | 58 +++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/mrbgems/mruby-string-ext/mrblib/string.rb b/mrbgems/mruby-string-ext/mrblib/string.rb index 45c631b94..18a7a62d3 100644 --- a/mrbgems/mruby-string-ext/mrblib/string.rb +++ b/mrbgems/mruby-string-ext/mrblib/string.rb @@ -1,4 +1,14 @@ class String + ## + # call-seq: + # str.lstrip -> new_str + # + # Returns a copy of str with leading whitespace removed. See also + # String#rstrip and String#strip. + # + # " hello ".lstrip #=> "hello " + # "hello".lstrip #=> "hello" + # def lstrip a = 0 z = self.size - 1 @@ -6,6 +16,16 @@ class String (z >= 0) ? self[a..z] : "" end + ## + # call-seq: + # str.rstrip -> new_str + # + # Returns a copy of str with trailing whitespace removed. See also + # String#lstrip and String#strip. + # + # " hello ".rstrip #=> " hello" + # "hello".rstrip #=> "hello" + # def rstrip a = 0 z = self.size - 1 @@ -13,6 +33,15 @@ class String (z >= 0) ? self[a..z] : "" end + ## + # call-seq: + # str.strip -> new_str + # + # Returns a copy of str with leading and trailing whitespace removed. + # + # " hello ".strip #=> "hello" + # "\tgoodbye\r\n".strip #=> "goodbye" + # def strip a = 0 z = self.size - 1 @@ -21,16 +50,45 @@ class String (z >= 0) ? self[a..z] : "" end + ## + # call-seq: + # str.lstrip! -> self or nil + # + # Removes leading whitespace from str, returning nil if no + # change was made. See also String#rstrip! and + # String#strip!. + # + # " hello ".lstrip #=> "hello " + # "hello".lstrip! #=> nil + # def lstrip! s = self.lstrip (s == self) ? nil : self.replace(s) end + ## + # call-seq: + # str.rstrip! -> self or nil + # + # Removes trailing whitespace from str, returning nil if + # no change was made. See also String#lstrip! and + # String#strip!. + # + # " hello ".rstrip #=> " hello" + # "hello".rstrip! #=> nil + # def rstrip! s = self.rstrip (s == self) ? nil : self.replace(s) end + ## + # call-seq: + # str.strip! -> str or nil + # + # Removes leading and trailing whitespace from str. Returns + # nil if str was not altered. + # def strip! s = self.strip (s == self) ? nil : self.replace(s) -- cgit v1.2.3 From 85ed19ca4da194d48f27bb06d1eacccf9f877d58 Mon Sep 17 00:00:00 2001 From: Jun Hiroe Date: Mon, 2 Jun 2014 22:03:29 +0900 Subject: Fix indent in String#casecmp comments --- mrbgems/mruby-string-ext/mrblib/string.rb | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/mrbgems/mruby-string-ext/mrblib/string.rb b/mrbgems/mruby-string-ext/mrblib/string.rb index 45c631b94..3066ea698 100644 --- a/mrbgems/mruby-string-ext/mrblib/string.rb +++ b/mrbgems/mruby-string-ext/mrblib/string.rb @@ -36,16 +36,17 @@ class String (s == self) ? nil : self.replace(s) end -# call-seq: -# str.casecmp(other_str) -> -1, 0, +1 or nil -# -# Case-insensitive version of String#<=>. -# -# "abcdef".casecmp("abcde") #=> 1 -# "aBcDeF".casecmp("abcdef") #=> 0 -# "abcdef".casecmp("abcdefg") #=> -1 -# "abcdef".casecmp("ABCDEF") #=> 0 -# + ## + # call-seq: + # str.casecmp(other_str) -> -1, 0, +1 or nil + # + # Case-insensitive version of String#<=>. + # + # "abcdef".casecmp("abcde") #=> 1 + # "aBcDeF".casecmp("abcdef") #=> 0 + # "abcdef".casecmp("abcdefg") #=> -1 + # "abcdef".casecmp("ABCDEF") #=> 0 + # def casecmp(str) self.downcase <=> str.to_str.downcase rescue NoMethodError -- cgit v1.2.3 From acec9d1ff19e2dbea9a4ff7b0030f74b9d42aa17 Mon Sep 17 00:00:00 2001 From: take_cheeze Date: Mon, 2 Jun 2014 22:38:02 +0900 Subject: Implement `NoMethodError#args`. --- include/mruby/error.h | 1 + mrblib/error.rb | 6 ++++++ src/class.c | 3 +-- src/error.c | 14 ++++++++++++++ test/t/nomethoderror.rb | 13 +++++++++++++ 5 files changed, 35 insertions(+), 2 deletions(-) diff --git a/include/mruby/error.h b/include/mruby/error.h index 7ae2d4348..4d37f1701 100644 --- a/include/mruby/error.h +++ b/include/mruby/error.h @@ -19,6 +19,7 @@ void mrb_exc_print(mrb_state *mrb, struct RObject *exc); void mrb_print_backtrace(mrb_state *mrb); mrb_value mrb_exc_backtrace(mrb_state *mrb, mrb_value exc); mrb_value mrb_get_backtrace(mrb_state *mrb); +mrb_noreturn void mrb_no_method_error(mrb_state *mrb, mrb_sym id, mrb_int argc, const mrb_value *argv, const char *fmt, ...); /* declaration for fail method */ mrb_value mrb_f_raise(mrb_state*, mrb_value); diff --git a/mrblib/error.rb b/mrblib/error.rb index 6e8181e9d..a5b6b3223 100644 --- a/mrblib/error.rb +++ b/mrblib/error.rb @@ -48,6 +48,12 @@ end # ISO 15.2.32 class NoMethodError < NameError + attr_reader :args + + def initialize(message=nil, name=nil, args=nil) + @args = args + super message, name + end end # ISO 15.2.33 diff --git a/src/class.c b/src/class.c index db9a8ac26..ba078911b 100644 --- a/src/class.c +++ b/src/class.c @@ -1216,8 +1216,7 @@ mrb_bob_missing(mrb_state *mrb, mrb_value mod) repr = mrb_any_to_s(mrb, mod); } - mrb_raisef(mrb, E_NOMETHOD_ERROR, "undefined method '%S' for %S", - mrb_sym2str(mrb, name), repr); + mrb_no_method_error(mrb, name, alen, a, "undefined method '%S' for %S", mrb_sym2str(mrb, name), repr); /* not reached */ return mrb_nil_value(); } diff --git a/src/error.c b/src/error.c index 360df8f2e..c60fff10d 100644 --- a/src/error.c +++ b/src/error.c @@ -442,6 +442,20 @@ mrb_sys_fail(mrb_state *mrb, const char *mesg) } } +mrb_noreturn void +mrb_no_method_error(mrb_state *mrb, mrb_sym id, mrb_int argc, const mrb_value *argv, char const* fmt, ...) +{ + mrb_value exc; + va_list ap; + + va_start(ap, fmt); + exc = mrb_funcall(mrb, mrb_obj_value(E_NOMETHOD_ERROR), "new", 3, + mrb_vformat(mrb, fmt, ap), mrb_symbol_value(id), + mrb_ary_new_from_values(mrb, argc, argv)); + va_end(ap); + mrb_exc_raise(mrb, exc); +} + void mrb_init_exception(mrb_state *mrb) { diff --git a/test/t/nomethoderror.rb b/test/t/nomethoderror.rb index 561e545f9..709d31165 100644 --- a/test/t/nomethoderror.rb +++ b/test/t/nomethoderror.rb @@ -11,3 +11,16 @@ end assert('NoMethodError superclass', '15.2.32.2') do assert_equal NameError, NoMethodError.superclass end + +assert('NoMethodError#args', '15.2.32.2.1') do + a = NoMethodError.new 'test', :test, [1, 2] + assert_equal [1, 2], a.args + + assert_nothing_raised do + begin + doesNotExistAsAMethodNameForVerySure 3, 1, 4 + rescue NoMethodError => e + assert_equal [3, 1, 4], e.args + end + end +end -- cgit v1.2.3 From a0b12bf6b4ed5e148d33562142d8e89271669953 Mon Sep 17 00:00:00 2001 From: Mitsutaka Mimura Date: Mon, 2 Jun 2014 23:06:02 +0900 Subject: Add Array#to_h --- mrbgems/mruby-array-ext/src/array.c | 44 +++++++++++++++++++++++++++++++++++ mrbgems/mruby-array-ext/test/array.rb | 8 +++++++ 2 files changed, 52 insertions(+) diff --git a/mrbgems/mruby-array-ext/src/array.c b/mrbgems/mruby-array-ext/src/array.c index 014137e99..d69f0ac44 100644 --- a/mrbgems/mruby-array-ext/src/array.c +++ b/mrbgems/mruby-array-ext/src/array.c @@ -2,6 +2,7 @@ #include "mruby/value.h" #include "mruby/array.h" #include "mruby/range.h" +#include "mruby/hash.h" /* * call-seq: @@ -105,6 +106,48 @@ mrb_ary_values_at(mrb_state *mrb, mrb_value self) return mrb_get_values_at(mrb, self, RARRAY_LEN(self), argc, argv, mrb_ary_ref); } +/* + * call-seq: + * ary.to_h -> Hash + * + * Returns the result of interpreting aray as an array of + * [key, value] paris. + * + * [[:foo, :bar], [1, 2]].to_h + * # => {:foo => :bar, 1 => 2} + */ + +static mrb_value +mrb_ary_to_h(mrb_state *mrb, mrb_value ary) +{ + mrb_int i; + mrb_value v, hash; + + hash = mrb_hash_new_capa(mrb, 0); + + for (i = 0; i < RARRAY_LEN(ary); ++i) { + v = mrb_check_array_type(mrb, RARRAY_PTR(ary)[i]); + + if (mrb_nil_p(v)) { + mrb_raisef(mrb, E_TYPE_ERROR, "wrong element type %S at %S (expected array)", + mrb_str_new_cstr(mrb, mrb_obj_classname(mrb, RARRAY_PTR(ary)[i])), + mrb_fixnum_value(i) + ); + } + + if (RARRAY_LEN(v) != 2) { + mrb_raisef(mrb, E_ARGUMENT_ERROR, "wrong array length at %S (expected 2, was %S)", + mrb_fixnum_value(i), + mrb_fixnum_value(RARRAY_LEN(v)) + ); + } + + mrb_hash_set(mrb, hash, RARRAY_PTR(v)[0], RARRAY_PTR(v)[1]); + } + + return hash; +} + void mrb_mruby_array_ext_gem_init(mrb_state* mrb) { @@ -114,6 +157,7 @@ mrb_mruby_array_ext_gem_init(mrb_state* mrb) mrb_define_method(mrb, a, "at", mrb_ary_at, MRB_ARGS_REQ(1)); mrb_define_method(mrb, a, "rassoc", mrb_ary_rassoc, MRB_ARGS_REQ(1)); mrb_define_method(mrb, a, "values_at", mrb_ary_values_at, MRB_ARGS_ANY()); + mrb_define_method(mrb, a, "to_h", mrb_ary_to_h, MRB_ARGS_REQ(0)); } void diff --git a/mrbgems/mruby-array-ext/test/array.rb b/mrbgems/mruby-array-ext/test/array.rb index cb5652dde..8c919f7e0 100644 --- a/mrbgems/mruby-array-ext/test/array.rb +++ b/mrbgems/mruby-array-ext/test/array.rb @@ -285,3 +285,11 @@ assert('Array#values_at') do assert_equal ['none', nil, nil, 'red', 'green', 'purple'], a.values_at(4..6, 0...3) assert_raise(TypeError) { a.values_at 'tt' } end + +assert('Array#to_h') do + assert_equal({}, [].to_h) + assert_equal({a: 1, b:2}, [[:a, 1], [:b, 2]].to_h) + + assert_raise(TypeError) { [1].to_h } + assert_raise(ArgumentError) { [[1]].to_h } +end -- cgit v1.2.3