use pre-allocated RuntimeError for out-of-memory

parent b01bb33b
...@@ -167,6 +167,7 @@ typedef struct mrb_state { ...@@ -167,6 +167,7 @@ typedef struct mrb_state {
struct RClass *eException_class; struct RClass *eException_class;
struct RClass *eStandardError_class; struct RClass *eStandardError_class;
struct RObject *nomem_err; /* pre-allocated NoMemoryError */
void *ud; /* auxiliary data */ void *ud; /* auxiliary data */
} mrb_state; } mrb_state;
......
...@@ -227,7 +227,9 @@ mrb_noreturn void ...@@ -227,7 +227,9 @@ mrb_noreturn void
mrb_exc_raise(mrb_state *mrb, mrb_value exc) mrb_exc_raise(mrb_state *mrb, mrb_value exc)
{ {
mrb->exc = mrb_obj_ptr(exc); mrb->exc = mrb_obj_ptr(exc);
if (!mrb->out_of_memory) {
exc_debug_info(mrb, mrb->exc); exc_debug_info(mrb, mrb->exc);
}
if (!mrb->jmp) { if (!mrb->jmp) {
mrb_p(mrb, exc); mrb_p(mrb, exc);
abort(); abort();
...@@ -459,7 +461,7 @@ mrb_no_method_error(mrb_state *mrb, mrb_sym id, mrb_int argc, const mrb_value *a ...@@ -459,7 +461,7 @@ mrb_no_method_error(mrb_state *mrb, mrb_sym id, mrb_int argc, const mrb_value *a
void void
mrb_init_exception(mrb_state *mrb) mrb_init_exception(mrb_state *mrb)
{ {
struct RClass *exception, *script_error; struct RClass *exception, *runtime_error, *script_error;
mrb->eException_class = exception = mrb_define_class(mrb, "Exception", mrb->object_class); /* 15.2.22 */ mrb->eException_class = exception = mrb_define_class(mrb, "Exception", mrb->object_class); /* 15.2.22 */
mrb_define_class_method(mrb, exception, "exception", mrb_instance_new, MRB_ARGS_ANY()); mrb_define_class_method(mrb, exception, "exception", mrb_instance_new, MRB_ARGS_ANY());
...@@ -472,7 +474,8 @@ mrb_init_exception(mrb_state *mrb) ...@@ -472,7 +474,8 @@ mrb_init_exception(mrb_state *mrb)
mrb_define_method(mrb, exception, "backtrace", mrb_exc_backtrace, MRB_ARGS_NONE()); mrb_define_method(mrb, exception, "backtrace", mrb_exc_backtrace, MRB_ARGS_NONE());
mrb->eStandardError_class = mrb_define_class(mrb, "StandardError", mrb->eException_class); /* 15.2.23 */ mrb->eStandardError_class = mrb_define_class(mrb, "StandardError", mrb->eException_class); /* 15.2.23 */
mrb_define_class(mrb, "RuntimeError", mrb->eStandardError_class); /* 15.2.28 */ runtime_error = mrb_define_class(mrb, "RuntimeError", mrb->eStandardError_class); /* 15.2.28 */
mrb->nomem_err = mrb_obj_ptr(mrb_exc_new_str(mrb, runtime_error, mrb_str_new_lit(mrb, "Out of memory")));
script_error = mrb_define_class(mrb, "ScriptError", mrb->eException_class); /* 15.2.37 */ script_error = mrb_define_class(mrb, "ScriptError", mrb->eException_class); /* 15.2.37 */
mrb_define_class(mrb, "SyntaxError", script_error); /* 15.2.38 */ mrb_define_class(mrb, "SyntaxError", script_error); /* 15.2.38 */
} }
...@@ -189,7 +189,7 @@ mrb_realloc(mrb_state *mrb, void *p, size_t len) ...@@ -189,7 +189,7 @@ mrb_realloc(mrb_state *mrb, void *p, size_t len)
} }
else { else {
mrb->out_of_memory = TRUE; mrb->out_of_memory = TRUE;
mrb_raise(mrb, E_RUNTIME_ERROR, "Out of memory"); mrb_exc_raise(mrb, mrb_obj_value(mrb->nomem_err));
} }
} }
else { else {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment