From c3e4ee0bf292d9e48eaa5646410b1d424efc557b Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Fri, 6 May 2022 16:21:39 +0900 Subject: [PATCH] error.c: store error message converted as a string. This is controversial since this change alters the `raise` behavior. Previously, when an object that cannot be converted to a string, it just ignore message when it's printed (using `Exception#inspect`). But after this change, `raise` (more precisly `Exception.new`) raises TypeError exception. I think the new behavior is clearer, more intuitive. --- src/error.c | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/error.c b/src/error.c index 916109cb1..0fb951390 100644 --- a/src/error.c +++ b/src/error.c @@ -21,26 +21,18 @@ void mrb_exc_mesg_set(mrb_state *mrb, struct RException *exc, mrb_value mesg) { - if (!mrb_immediate_p(mesg)) { - exc->mesg = mrb_obj_ptr(mesg); - mrb_field_write_barrier_value(mrb, (struct RBasic*)exc, mesg); - mrb_iv_remove(mrb, mrb_obj_value(exc), MRB_SYM(mesg)); - } - else { - exc->mesg = NULL; - mrb_obj_iv_set(mrb, (struct RObject*)exc, MRB_SYM(mesg), mesg); + if (!mrb_string_p(mesg)) { + mesg = mrb_obj_as_string(mrb, mesg); } + exc->mesg = mrb_obj_ptr(mesg); + mrb_field_write_barrier_value(mrb, (struct RBasic*)exc, mesg); } mrb_value mrb_exc_mesg_get(mrb_state *mrb, struct RException *exc) { - if (exc->mesg) { - return mrb_obj_value(exc->mesg); - } - else { - return mrb_obj_iv_get(mrb, (struct RObject*)exc, MRB_SYM(mesg)); - } + if (exc->mesg == NULL) return mrb_nil_value(); + return mrb_obj_value(exc->mesg); } MRB_API mrb_value