From 49be22d8dfaa82da0b5f0538096ec1338e3c9a61 Mon Sep 17 00:00:00 2001 From: matz Date: Sat, 1 Sep 2007 12:56:29 +0000 Subject: * eval_jump.ci (rb_f_catch): generate new tag object if no argument is given. backported from MatzRuby. [ruby-dev:31609] * eval_jump.ci (rb_catch): call #catch without arguments if tag string is NULL. * eval_jump.ci (rb_f_throw): allow throwing non-symbol object. * eval.c (rb_catch_obj): new function to wait throw with arbitrary object. * eval.c (rb_throw_obj): new function to throw arbitrary object. * variable.c (check_autoload_table): prevent multiple calls from RSTRING_PTR(). git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13331 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 18 +++++++++++++++++ bootstraptest/test_jump.rb | 7 +++++++ bootstraptest/test_knownbug.rb | 8 -------- eval_jump.ci | 44 +++++++++++++++++++++++++++++++++--------- variable.c | 3 ++- 5 files changed, 62 insertions(+), 18 deletions(-) diff --git a/ChangeLog b/ChangeLog index b8803fef9b..2d3b742bf5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -46,6 +46,24 @@ Sat Sep 1 14:24:23 2007 Nobuyoshi Nakada * include/ruby/st.h (rb_index_t): use st_data_t for the platforms it is larger than int. +Sat Sep 1 10:43:30 2007 Yukihiro Matsumoto + + * eval_jump.ci (rb_f_catch): generate new tag object if no argument is + given. backported from MatzRuby. [ruby-dev:31609] + + * eval_jump.ci (rb_catch): call #catch without arguments if tag + string is NULL. + + * eval_jump.ci (rb_f_throw): allow throwing non-symbol object. + + * eval.c (rb_catch_obj): new function to wait throw with arbitrary + object. + + * eval.c (rb_throw_obj): new function to throw arbitrary object. + + * variable.c (check_autoload_table): prevent multiple calls from + RSTRING_PTR(). + Fri Aug 31 07:12:24 2007 NAKAMURA Usaku * numeric.c (SQRT_LONG_MAX): use SIZEOF_LONG instead of SIZEOF_VALUE diff --git a/bootstraptest/test_jump.rb b/bootstraptest/test_jump.rb index a83b6f0eab..9ada6829b1 100644 --- a/bootstraptest/test_jump.rb +++ b/bootstraptest/test_jump.rb @@ -230,3 +230,10 @@ assert_equal %q{1}, %q{ m } +assert_equal 'ok', %q{ + begin + catch {|t| throw t, :ok } + rescue ArgumentError + :ng + end +}, '[ruby-dev:31609]' diff --git a/bootstraptest/test_knownbug.rb b/bootstraptest/test_knownbug.rb index 382f027b34..b97a08d928 100644 --- a/bootstraptest/test_knownbug.rb +++ b/bootstraptest/test_knownbug.rb @@ -3,11 +3,3 @@ # So all tests will cause failure. # -# catch/throw -assert_equal 'ok', %q{ - begin - catch {|t| throw t, :ok } - rescue ArgumentError - :ng - end -}, '[ruby-dev:31609]' diff --git a/eval_jump.ci b/eval_jump.ci index ddb99c9271..a637f97227 100644 --- a/eval_jump.ci +++ b/eval_jump.ci @@ -27,8 +27,6 @@ rb_f_throw(int argc, VALUE *argv) struct rb_vm_tag *tt = th->tag; rb_scan_args(argc, argv, "11", &tag, &value); - tag = ID2SYM(rb_to_id(tag)); - while (tt) { if (tt->tag == tag) { tt->retval = value; @@ -37,8 +35,8 @@ rb_f_throw(int argc, VALUE *argv) tt = tt->prev; } if (!tt) { - rb_name_error(SYM2ID(tag), "uncaught throw `%s'", - rb_id2name(SYM2ID(tag))); + VALUE desc = rb_inspect(tag); + rb_raise(rb_eArgError, "uncaught throw %s", RSTRING_PTR(desc)); } rb_trap_restore_mask(); th->errinfo = tag; @@ -59,6 +57,16 @@ rb_throw(const char *tag, VALUE val) rb_f_throw(2, argv); } +void +rb_throw_obj(VALUE tag, VALUE val) +{ + VALUE argv[2]; + + argv[0] = tag; + argv[1] = val; + rb_f_throw(2, argv); +} + /* * call-seq: * catch(symbol) {| | block } > obj @@ -91,13 +99,17 @@ rb_throw(const char *tag, VALUE val) */ static VALUE -rb_f_catch(VALUE dmy, VALUE tag) +rb_f_catch(int argc, VALUE *argv) { + VALUE tag; int state; VALUE val = Qnil; /* OK */ rb_thread_t *th = GET_THREAD(); - tag = ID2SYM(rb_to_id(tag)); + rb_scan_args(argc, argv, "01", &tag); + if (argc == 0) { + tag = rb_obj_alloc(rb_cObject); + } PUSH_TAG(); th->tag->tag = tag; @@ -117,6 +129,12 @@ rb_f_catch(VALUE dmy, VALUE tag) return val; } +static VALUE +catch_null_i(VALUE dmy) +{ + return rb_funcall(Qnil, rb_intern("catch"), 0, 0); +} + static VALUE catch_i(VALUE tag) { @@ -126,8 +144,16 @@ catch_i(VALUE tag) VALUE rb_catch(const char *tag, VALUE (*func)(), VALUE data) { - return rb_iterate((VALUE (*)_((VALUE)))catch_i, ID2SYM(rb_intern(tag)), - func, data); + if (!tag) { + return rb_iterate(catch_null_i, 0, func, data); + } + return rb_iterate(catch_i, ID2SYM(rb_intern(tag)), func, data); +} + +VALUE +rb_catch_obj(VALUE tag, VALUE (*func)(), VALUE data) +{ + return rb_iterate((VALUE (*)_((VALUE)))catch_i, tag, func, data); } @@ -276,7 +302,7 @@ rb_exec_end_proc(void) void Init_jump(void) { - rb_define_global_function("catch", rb_f_catch, 1); + rb_define_global_function("catch", rb_f_catch, -1); rb_define_global_function("throw", rb_f_throw, -1); rb_define_global_function("at_exit", rb_f_at_exit, 0); } diff --git a/variable.c b/variable.c index 58fd17b78c..2ddfb1ac5b 100644 --- a/variable.c +++ b/variable.c @@ -1166,7 +1166,8 @@ check_autoload_table(VALUE av) Check_Type(av, T_DATA); if (RDATA(av)->dmark != (RUBY_DATA_FUNC)rb_mark_tbl || RDATA(av)->dfree != (RUBY_DATA_FUNC)st_free_table) { - rb_raise(rb_eTypeError, "wrong autoload table: %s", RSTRING_PTR(rb_inspect(av))); + VALUE desc = rb_inspect(av); + rb_raise(rb_eTypeError, "wrong autoload table: %s", RSTRING_PTR(desc)); } return (struct st_table *)DATA_PTR(av); } -- cgit v1.2.3