object.c: improve handling of the broken inspect method.

When `inspect` method returns non-string value, it will return a string
given from `to_s` (and if `to_s` fails, will raise an exception).
This commit is contained in:
Yukihiro "Matz" Matsumoto
2022-02-26 22:01:52 +09:00
parent 5305c5c84d
commit 57540fbf6c
+5 -1
View File
@@ -606,7 +606,11 @@ mrb_check_hash_type(mrb_state *mrb, mrb_value hash)
MRB_API mrb_value
mrb_inspect(mrb_state *mrb, mrb_value obj)
{
return mrb_obj_as_string(mrb, mrb_funcall_id(mrb, obj, MRB_SYM(inspect), 0));
mrb_value v = mrb_funcall_id(mrb, obj, MRB_SYM(inspect), 0);
if (!mrb_string_p(v)) {
v = mrb_obj_as_string(mrb, obj);
}
return v;
}
MRB_API mrb_bool