Print errors by calling `mrb_print_backtrace()` without backtrace
In case of `NoMemoryError` exceptions, the error message is now printed directly.
Replaced `mrb_p()` used by with `mrb_print_error()`.
ref. squashed commit
ref. subcommit
ref. subcommit
MODIFY
src/backtrace.c
src/backtrace.c
@@ -100,7 +100,7 @@ static void
print_backtrace(mrb_state *mrb, struct RObject *exc, struct RArray *backtrace)
{
mrb_int i;
- mrb_int n = ARY_LEN(backtrace);
+ mrb_int n = (backtrace ? ARY_LEN(backtrace) : 0);
mrb_value *loc, mesg;
if (n != 0) {
@@ -117,10 +117,20 @@ print_backtrace(mrb_state *mrb, struct RObject *exc, struct RArray *backtrace)
fprintf(stderr, "%.*s: ", (int)RSTRING_LEN(*loc), RSTRING_PTR(*loc));
}
}
+ else {
+ fprintf(stderr, "%s:0: ", "(unknown)");
+ }
+
+ if (exc == mrb->nomem_err) {
+ static const char nomem[] = "Out of memory (NoMemoryError)\n";
+ fwrite(nomem, sizeof(nomem) - 1, 1, stderr);
+ }
+ else {
mesg = mrb_exc_inspect(mrb, mrb_obj_value(exc));
fwrite(RSTRING_PTR(mesg), RSTRING_LEN(mesg), 1, stderr);
fputc('\n', stderr);
}
+}
/* mrb_print_backtrace
@@ -130,13 +140,12 @@ print_backtrace(mrb_state *mrb, struct RObject *exc, struct RArray *backtrace)
MRB_API void
mrb_print_backtrace(mrb_state *mrb)
{
- if (!mrb->exc) {
+ if (!mrb->exc || mrb->exc->tt != MRB_TT_EXCEPTION) {
return;
}
struct RObject *backtrace = ((struct RException*)mrb->exc)->backtrace;
- if (!backtrace) return;
- if (backtrace->tt != MRB_TT_ARRAY) backtrace = mrb_unpack_backtrace(mrb, backtrace);
+ if (backtrace && backtrace->tt != MRB_TT_ARRAY) backtrace = mrb_unpack_backtrace(mrb, backtrace);
print_backtrace(mrb, mrb->exc, (struct RArray*)backtrace);
}
#else

MODIFY
src/error.c
src/error.c
@@ -195,7 +195,7 @@ static mrb_noreturn void
exc_throw(mrb_state *mrb, mrb_value exc)
{
if (!mrb->jmp) {
- mrb_p(mrb, exc);
+ mrb_print_error(mrb);
abort();
}
MRB_THROW(mrb->jmp);
@@ -576,7 +576,7 @@ mrb_core_init_protect(mrb_state *mrb, void (*body)(mrb_state *, void *), void *o
err = 0;
} MRB_CATCH(&c_jmp) {
if (mrb->exc) {
- mrb_p(mrb, mrb_obj_value(mrb->exc));
+ mrb_print_error(mrb);
mrb->exc = NULL;
}
else {

Print errors by calling `mrb_print_backtrace()` without backtrace

- In case of `NoMemoryError` exceptions, the error message is now printed directly.
- Replaced `mrb_p()` used by #4250 with `mrb_print_error()`.

  ref. squashed commit f1523d24042ca3416dc5b9be7b3fc220ddaed896
  ref. subcommit da7d7f881bbbad9988a3a2b7bad8f2b72ff06bc6
  ref. subcommit d9c7b6be6eb54630b64eea5c35be241e551676e5