[前][次][番号順一覧][スレッド一覧]

ruby-changes:26740

From: mrkn <ko1@a...>
Date: Sat, 12 Jan 2013 17:47:34 +0900 (JST)
Subject: [ruby-changes:26740] mrkn:r38792 (trunk): * numeric.c (do_coerce): fix for the exceptions which the coerce

mrkn	2013-01-12 17:47:18 +0900 (Sat, 12 Jan 2013)

  New Revision: 38792

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=38792

  Log:
    * numeric.c (do_coerce): fix for the exceptions which the coerce
      method raises.  The optimization done by r38756 is preserved.
      [Bug #7645] [ruby-core:51213]

  Modified files:
    trunk/ChangeLog
    trunk/numeric.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 38791)
+++ ChangeLog	(revision 38792)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Jan 12 17:42:00 2013  Kenta Murata  <mrkn@c...>
+
+	* numeric.c (do_coerce): fix for the exceptions which the coerce
+	  method raises.  The optimization done by r38756 is preserved.
+	  [Bug #7645] [ruby-core:51213]
+
 Sat Jan 12 16:12:46 2013  Nobuyoshi Nakada  <nobu@r...>
 
 	* win32/setup.mak (-runtime-): see msvcrt from link header on mswin
Index: numeric.c
===================================================================
--- numeric.c	(revision 38791)
+++ numeric.c	(revision 38792)
@@ -211,24 +211,42 @@ num_coerce(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L211
     return rb_assoc_new(y, x);
 }
 
+static VALUE
+coerce_body(VALUE *x)
+{
+    return rb_funcall(x[1], id_coerce, 1, x[0]);
+}
+
+static VALUE
+coerce_rescue(VALUE *x)
+{
+    volatile VALUE v = rb_inspect(x[1]);
+
+    rb_raise(rb_eTypeError, "%s can't be coerced into %s",
+	     rb_special_const_p(x[1])?
+	     RSTRING_PTR(v):
+	     rb_obj_classname(x[1]),
+	     rb_obj_classname(x[0]));
+
+    return Qnil;		/* dummy */
+}
+
 static int
 do_coerce(VALUE *x, VALUE *y, int err)
 {
     VALUE ary;
+    VALUE a[2];
+
+    a[0] = *x; a[1] = *y;
 
-    ary = rb_check_funcall(*y, id_coerce, 1, x);
-    if (ary == Qundef) {
+    if (!rb_respond_to(*y, id_coerce)) {
 	if (err) {
-	    volatile VALUE v = rb_inspect(*y);
-	    rb_raise(rb_eTypeError, "%s can't be coerced into %s",
-		     rb_special_const_p(*y)?
-		     RSTRING_PTR(v):
-		     rb_obj_classname(*y),
-		     rb_obj_classname(*x));
+	    coerce_rescue(a);
 	}
 	return FALSE;
     }
 
+    ary = rb_rescue(coerce_body, (VALUE)a, err ? coerce_rescue : 0, (VALUE)a);
     if (!RB_TYPE_P(ary, T_ARRAY) || RARRAY_LEN(ary) != 2) {
 	if (err) {
 	    rb_raise(rb_eTypeError, "coerce must return [x, y]");

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]