Simplify `print_backtrace()`
MODIFY
src/backtrace.c
src/backtrace.c
@@ -25,8 +25,7 @@ typedef void (*each_backtrace_func)(mrb_state*, const struct backtrace_location*
static const mrb_data_type bt_type = { "Backtrace", mrb_free };
-mrb_value mrb_exc_to_s(mrb_state *mrb, mrb_value exc);
-mrb_value mrb_mod_to_s(mrb_state *mrb, mrb_value klass);
+mrb_value mrb_exc_inspect(mrb_state *mrb, mrb_value exc);
mrb_value mrb_unpack_backtrace(mrb_state *mrb, mrb_value backtrace);
static void
@@ -82,7 +81,7 @@ print_backtrace(mrb_state *mrb, struct RObject *exc, mrb_value backtrace)
{
int i;
mrb_int n = RARRAY_LEN(backtrace);
- mrb_value *loc, mesg, cname;
+ mrb_value *loc, mesg;
FILE *stream = stderr;
if (n != 0) {
@@ -93,21 +92,12 @@ print_backtrace(mrb_state *mrb, struct RObject *exc, mrb_value backtrace)
i, (int)RSTRING_LEN(*loc), RSTRING_PTR(*loc));
}
}
- }
-
- if (n != 0) {
+ if (mrb_string_p(*loc)) {
fprintf(stream, "%.*s: ", (int)RSTRING_LEN(*loc), RSTRING_PTR(*loc));
}
- mesg = mrb_exc_to_s(mrb, mrb_obj_value(exc));
- cname = mrb_mod_to_s(mrb, mrb_obj_value(exc->c));
- if (RSTRING_LEN(mesg) == 0) {
- fprintf(stream, "%.*s \n", (int)RSTRING_LEN(cname), RSTRING_PTR(cname));
- }
- else {
- fprintf(stream, "%.*s (%.*s)\n",
- (int)RSTRING_LEN(mesg), RSTRING_PTR(mesg),
- (int)RSTRING_LEN(cname), RSTRING_PTR(cname));
}
+ mesg = mrb_exc_inspect(mrb, mrb_obj_value(exc));
+ fprintf(stream, "%.*s\n", (int)RSTRING_LEN(mesg), RSTRING_PTR(mesg));
}
/* mrb_print_backtrace

MODIFY
src/error.c
src/error.c
@@ -88,7 +88,7 @@ exc_exception(mrb_state *mrb, mrb_value self)
*/
mrb_value
-mrb_exc_to_s(mrb_state *mrb, mrb_value exc)
+exc_to_s(mrb_state *mrb, mrb_value exc)
{
mrb_value mesg = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "mesg"));
struct RObject *p;
@@ -127,8 +127,8 @@ exc_message(mrb_state *mrb, mrb_value exc)
* returns message and class name.
*/
-static mrb_value
-exc_inspect(mrb_state *mrb, mrb_value exc)
+mrb_value
+mrb_exc_inspect(mrb_state *mrb, mrb_value exc)
{
mrb_value mesg = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "mesg"));
mrb_value cname = mrb_mod_to_s(mrb, mrb_obj_value(mrb_obj_class(mrb, exc)));
@@ -543,9 +543,9 @@ mrb_init_exception(mrb_state *mrb)
mrb_define_class_method(mrb, exception, "exception", mrb_instance_new, MRB_ARGS_OPT(1));
mrb_define_method(mrb, exception, "exception", exc_exception, MRB_ARGS_OPT(1));
mrb_define_method(mrb, exception, "initialize", exc_initialize, MRB_ARGS_OPT(1));
- mrb_define_method(mrb, exception, "to_s", mrb_exc_to_s, MRB_ARGS_NONE());
+ mrb_define_method(mrb, exception, "to_s", exc_to_s, MRB_ARGS_NONE());
mrb_define_method(mrb, exception, "message", exc_message, MRB_ARGS_NONE());
- mrb_define_method(mrb, exception, "inspect", exc_inspect, MRB_ARGS_NONE());
+ mrb_define_method(mrb, exception, "inspect", mrb_exc_inspect, MRB_ARGS_NONE());
mrb_define_method(mrb, exception, "backtrace", mrb_exc_backtrace, MRB_ARGS_NONE());
mrb_define_method(mrb, exception, "set_backtrace", exc_set_backtrace, MRB_ARGS_REQ(1));

Simplify `print_backtrace()`