diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2014-06-03 15:46:16 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2014-06-03 15:46:16 +0900 |
| commit | b83ff9ff300b9efa57f481333cd4a2001f49266b (patch) | |
| tree | 608a06128b6085fcf3bf4f837a8ea61724bc2341 | |
| parent | 896c1312311a15518218d0c50f7c7921b7983e1a (diff) | |
| parent | acec9d1ff19e2dbea9a4ff7b0030f74b9d42aa17 (diff) | |
| download | mruby-b83ff9ff300b9efa57f481333cd4a2001f49266b.tar.gz mruby-b83ff9ff300b9efa57f481333cd4a2001f49266b.zip | |
Merge pull request #2347 from take-cheeze/nomethod_args
Implement `NoMethodError#args`.
| -rw-r--r-- | include/mruby/error.h | 1 | ||||
| -rw-r--r-- | mrblib/error.rb | 6 | ||||
| -rw-r--r-- | src/class.c | 3 | ||||
| -rw-r--r-- | src/error.c | 14 | ||||
| -rw-r--r-- | 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 |
