Move `MRB_TRY` part of `mrb_close` to `src/error.c`; ref
We don't want to increase number of files that should be compiled by C++
compiler when `enable_cxx_exception` is turned on.
MODIFY
src/error.c
src/error.c
@@ -591,6 +591,28 @@ mrb_core_init_abort(mrb_state *mrb)
exc_throw(mrb, mrb_nil_value());
}
+void
+mrb_protect_atexit(mrb_state *mrb)
+{
+ if (mrb->atexit_stack_len > 0) {
+ struct mrb_jmpbuf *prev_jmp = mrb->jmp;
+ struct mrb_jmpbuf c_jmp;
+ for (int i = mrb->atexit_stack_len; i > 0; --i) {
+ MRB_TRY(&c_jmp) {
+ mrb->jmp = &c_jmp;
+ mrb->atexit_stack[i - 1](mrb);
+ mrb->jmp = prev_jmp;
+ } MRB_CATCH(&c_jmp) {
+ /* ignore atexit errors */
+ } MRB_END_EXC(&c_jmp);
+ }
+#ifndef MRB_FIXED_STATE_ATEXIT_STACK
+ mrb_free(mrb, mrb->atexit_stack);
+#endif
+ mrb->jmp = prev_jmp;
+ }
+}
+
mrb_noreturn void
mrb_raise_nomemory(mrb_state *mrb)
{

MODIFY
src/state.c
src/state.c
#include <mruby/debug.h>
#include <mruby/string.h>
#include <mruby/class.h>
-#include <mruby/throw.h>
void mrb_init_core(mrb_state*);
void mrb_init_mrbgems(mrb_state*);
@@ -177,26 +176,13 @@ mrb_free_context(mrb_state *mrb, struct mrb_context *c)
mrb_free(mrb, c);
}
+int mrb_protect_atexit(mrb_state *mrb);
+
MRB_API void
mrb_close(mrb_state *mrb)
{
if (!mrb) return;
- if (mrb->atexit_stack_len > 0) {
- struct mrb_jmpbuf *prev_jmp = mrb->jmp;
- struct mrb_jmpbuf c_jmp;
- for (int i = mrb->atexit_stack_len; i > 0; --i) {
- MRB_TRY(&c_jmp) {
- mrb->jmp = &c_jmp;
- mrb->atexit_stack[i - 1](mrb);
- mrb->jmp = prev_jmp;
- } MRB_CATCH(&c_jmp) {
- /* ignore atexit errors */
- } MRB_END_EXC(&c_jmp);
- }
-#ifndef MRB_FIXED_STATE_ATEXIT_STACK
- mrb_free(mrb, mrb->atexit_stack);
-#endif
- }
+ mrb_protect_atexit(mrb);
/* free */
mrb_gc_destroy(mrb, &mrb->gc);

Move `MRB_TRY` part of `mrb_close` to `src/error.c`; ref 6cc52b3

We don't want to increase number of files that should be compiled by C++
compiler when `enable_cxx_exception` is turned on.