summaryrefslogtreecommitdiff
path: root/numeric.c
diff options
context:
space:
mode:
authormrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-01-12 08:47:18 +0000
committermrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-01-12 08:47:18 +0000
commit350c448da83c61cebbe0c679678af18cd1c1a858 (patch)
tree3f0194ab15806febd62a8baa1056cbafd0e411a6 /numeric.c
parent8d816aef2e8eecd805193398ad00b604ce424d6c (diff)
* 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] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38792 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c34
1 files changed, 26 insertions, 8 deletions
diff --git a/numeric.c b/numeric.c
index 6535cb59f4..ca0936ca8a 100644
--- a/numeric.c
+++ b/numeric.c
@@ -211,24 +211,42 @@ num_coerce(VALUE x, VALUE y)
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]");