From 0580ba06110f7998fdaead724907a4c8d6540107 Mon Sep 17 00:00:00 2001 From: matz Date: Thu, 29 Oct 2009 04:55:10 +0000 Subject: [PATCH] * array.c (rb_ary_to_ary): do not use #respond_to? to detect to_ary. Just call. [ruby-core:23738] * eval.c (rb_check_funcall): new function with method existence check. returns Qundef when the method does not exist. * enumerator.c (enumerator_rewind): just call method, using rb_check_funcall(). [ruby-core:23738] * error.c (exc_equal): ditto. * object.c (convert_type): ditto. * error.c (rb_name_err_mesg_new): export function. * eval.c (make_exception): ditto. * io.c (pop_last_hash): return early when the last argument is nil. * io.c (rb_io_puts): treat T_STRING specially for small optimization. * vm_eval.c (raise_method_missing): skip method call if possible using rb_method_basic_definition_p(). * vm_eval.c (method_missing): ditto. * test/ruby/test_rubyoptions.rb (TestRubyOptions#test_debug): test suites changed to ignore exceptions caused by just-call policy. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25556 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 32 +++++++++++++++++++ array.c | 9 ++---- enum.c | 7 ++-- enumerator.c | 3 +- error.c | 17 ++++------ eval.c | 60 +++++++++++++++++++++++++++++------ include/ruby/intern.h | 1 + io.c | 6 ++++ object.c | 15 ++++++--- test/ruby/test_rubyoptions.rb | 4 +-- vm_eval.c | 14 ++++++-- 11 files changed, 129 insertions(+), 39 deletions(-) diff --git a/ChangeLog b/ChangeLog index 33ca79a267..0a8b33bf6e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,35 @@ +Thu Oct 29 13:53:18 2009 Yukihiro Matsumoto + + * array.c (rb_ary_to_ary): do not use #respond_to? to detect + to_ary. Just call. [ruby-core:23738] + + * eval.c (rb_check_funcall): new function with method existence + check. returns Qundef when the method does not exist. + + * enumerator.c (enumerator_rewind): just call method, using + rb_check_funcall(). [ruby-core:23738] + + * error.c (exc_equal): ditto. + + * object.c (convert_type): ditto. + + * error.c (rb_name_err_mesg_new): export function. + + * eval.c (make_exception): ditto. + + * io.c (pop_last_hash): return early when the last argument is nil. + + * io.c (rb_io_puts): treat T_STRING specially for small + optimization. + + * vm_eval.c (raise_method_missing): skip method call if possible + using rb_method_basic_definition_p(). + + * vm_eval.c (method_missing): ditto. + + * test/ruby/test_rubyoptions.rb (TestRubyOptions#test_debug): test + suites changed to ignore exceptions caused by just-call policy. + Thu Oct 29 04:41:44 2009 NARUSE, Yui * ruby.c (process_options): call rb_filesystem_encoding(). diff --git a/array.c b/array.c index 1deda03482..c4cd30cd63 100644 --- a/array.c +++ b/array.c @@ -1223,12 +1223,9 @@ rb_ary_rindex(int argc, VALUE *argv, VALUE ary) VALUE rb_ary_to_ary(VALUE obj) { - if (TYPE(obj) == T_ARRAY) { - return obj; - } - if (rb_respond_to(obj, rb_intern("to_ary"))) { - return to_ary(obj); - } + VALUE tmp = rb_check_array_type(obj); + + if (!NIL_P(tmp)) return tmp; return rb_ary_new3(1, obj); } diff --git a/enum.c b/enum.c index 875beb9eaf..c1efb1b5c2 100644 --- a/enum.c +++ b/enum.c @@ -149,9 +149,10 @@ enum_count(int argc, VALUE *argv, VALUE obj) func = count_iter_i; } else { - if (rb_respond_to(obj, id_size)) { - return rb_funcall(obj, id_size, 0, 0); - } + VALUE tmp; + + tmp = rb_check_funcall(obj, id_size, 0, 0); + if (tmp != Qundef) return tmp; func = count_all_i; } } diff --git a/enumerator.c b/enumerator.c index 7b2ef3491e..0340338b50 100644 --- a/enumerator.c +++ b/enumerator.c @@ -856,8 +856,7 @@ enumerator_rewind(VALUE obj) { struct enumerator *e = enumerator_ptr(obj); - if (rb_respond_to(e->obj, id_rewind)) - rb_funcall(e->obj, id_rewind, 0); + rb_check_funcall(e->obj, id_rewind, 0, 0); e->fib = 0; e->dst = Qnil; diff --git a/error.c b/error.c index a7342de4e5..6297584ecd 100644 --- a/error.c +++ b/error.c @@ -608,13 +608,10 @@ exc_equal(VALUE exc, VALUE obj) CONST_ID(id_message, "message"); CONST_ID(id_backtrace, "backtrace"); - if (rb_respond_to(obj, id_message) && rb_respond_to(obj, id_backtrace)) { - mesg = rb_funcall(obj, id_message, 0, 0); - backtrace = rb_funcall(obj, id_backtrace, 0, 0); - } - else { - return Qfalse; - } + mesg = rb_check_funcall(obj, id_message, 0, 0); + if (mesg == Qundef) return Qfalse; + backtrace = rb_check_funcall(obj, id_backtrace, 0, 0); + if (backtrace == Qundef) return Qfalse; } else { mesg = rb_attr_get(obj, id_mesg); @@ -794,8 +791,8 @@ static const rb_data_type_t name_err_mesg_data_type = { }; /* :nodoc: */ -static VALUE -name_err_mesg_new(VALUE obj, VALUE mesg, VALUE recv, VALUE method) +VALUE +rb_name_err_mesg_new(VALUE obj, VALUE mesg, VALUE recv, VALUE method) { VALUE *ptr = ALLOC_N(VALUE, NAME_ERR_MESG_COUNT); @@ -1112,7 +1109,7 @@ Init_Exception(void) rb_define_method(rb_eNameError, "name", name_err_name, 0); rb_define_method(rb_eNameError, "to_s", name_err_to_s, 0); rb_cNameErrorMesg = rb_define_class_under(rb_eNameError, "message", rb_cData); - rb_define_singleton_method(rb_cNameErrorMesg, "!", name_err_mesg_new, NAME_ERR_MESG_COUNT); + rb_define_singleton_method(rb_cNameErrorMesg, "!", rb_name_err_mesg_new, NAME_ERR_MESG_COUNT); rb_define_method(rb_cNameErrorMesg, "==", name_err_mesg_equal, 1); rb_define_method(rb_cNameErrorMesg, "to_str", name_err_mesg_to_str, 0); rb_define_method(rb_cNameErrorMesg, "_dump", name_err_mesg_to_str, 1); diff --git a/eval.c b/eval.c index 5133181c4b..dec628204a 100644 --- a/eval.c +++ b/eval.c @@ -425,11 +425,13 @@ rb_longjmp(int tag, volatile VALUE mesg) JUMP_TAG(tag); } +static VALUE make_exception(int argc, VALUE *argv, int isstr); + void rb_exc_raise(VALUE mesg) { if (!NIL_P(mesg)) { - mesg = rb_make_exception(1, &mesg); + mesg = make_exception(1, &mesg, Qfalse); } rb_longjmp(TAG_RAISE, mesg); } @@ -438,7 +440,7 @@ void rb_exc_fatal(VALUE mesg) { if (!NIL_P(mesg)) { - mesg = rb_make_exception(1, &mesg); + mesg = make_exception(1, &mesg, Qfalse); } rb_longjmp(TAG_FATAL, mesg); } @@ -490,8 +492,40 @@ rb_f_raise(int argc, VALUE *argv) return Qnil; /* not reached */ } +struct rescue_funcall_args { + VALUE obj; + ID id; + int argc; + VALUE *argv; +}; + +static VALUE +check_funcall(struct rescue_funcall_args *args) +{ + return rb_funcall2(args->obj, args->id, args->argc, args->argv); +} + +static VALUE +check_failed(VALUE data) +{ + return data; +} + VALUE -rb_make_exception(int argc, VALUE *argv) +rb_check_funcall(VALUE obj, ID id, int argc, VALUE *argv) +{ + struct rescue_funcall_args args; + + args.obj = obj; + args.id = id; + args.argc = argc; + args.argv = argv; + return rb_rescue2(check_funcall, (VALUE)&args, check_failed, Qundef, + rb_eNoMethodError, (VALUE)0); +} + +static VALUE +make_exception(int argc, VALUE *argv, int isstr) { VALUE mesg; ID exception; @@ -504,10 +538,12 @@ rb_make_exception(int argc, VALUE *argv) case 1: if (NIL_P(argv[0])) break; - mesg = rb_check_string_type(argv[0]); - if (!NIL_P(mesg)) { - mesg = rb_exc_new3(rb_eRuntimeError, mesg); - break; + if (isstr) { + mesg = rb_check_string_type(argv[0]); + if (!NIL_P(mesg)) { + mesg = rb_exc_new3(rb_eRuntimeError, mesg); + break; + } } n = 0; goto exception_call; @@ -517,10 +553,10 @@ rb_make_exception(int argc, VALUE *argv) n = 1; exception_call: CONST_ID(exception, "exception"); - if (!rb_respond_to(argv[0], exception)) { + mesg = rb_check_funcall(argv[0], exception, n, argv+1); + if (mesg == Qundef) { rb_raise(rb_eTypeError, "exception class/object expected"); } - mesg = rb_funcall(argv[0], exception, n, argv[1]); break; default: rb_raise(rb_eArgError, "wrong number of arguments"); @@ -536,6 +572,12 @@ rb_make_exception(int argc, VALUE *argv) return mesg; } +VALUE +rb_make_exception(int argc, VALUE *argv) +{ + return make_exception(argc, argv, Qtrue); +} + void rb_raise_jump(VALUE mesg) { diff --git a/include/ruby/intern.h b/include/ruby/intern.h index cece6424fe..fd37ecab2a 100644 --- a/include/ruby/intern.h +++ b/include/ruby/intern.h @@ -203,6 +203,7 @@ void rb_check_frozen(VALUE); /* eval.c */ int rb_sourceline(void); const char *rb_sourcefile(void); +VALUE rb_check_funcall(VALUE, ID, int, VALUE*); #if defined(NFDBITS) && defined(HAVE_RB_FD_INIT) typedef struct { diff --git a/io.c b/io.c index f04540417e..f150fb7617 100644 --- a/io.c +++ b/io.c @@ -5157,6 +5157,7 @@ pop_last_hash(int *argc_p, VALUE *argv) if (*argc_p == 0) return Qnil; last = argv[*argc_p-1]; + if (NIL_P(last)) return Qnil; tmp = rb_check_convert_type(last, T_HASH, "Hash", "to_hash"); if (NIL_P(tmp)) return Qnil; @@ -6066,12 +6067,17 @@ rb_io_puts(int argc, VALUE *argv, VALUE out) return Qnil; } for (i=0; i