diff options
Diffstat (limited to 'src/state.c')
| -rw-r--r-- | src/state.c | 45 |
1 files changed, 34 insertions, 11 deletions
diff --git a/src/state.c b/src/state.c index 3e5ebb483..533bdaa0b 100644 --- a/src/state.c +++ b/src/state.c @@ -19,11 +19,25 @@ void mrb_init_mrbgems(mrb_state*); void mrb_gc_init(mrb_state*, mrb_gc *gc); void mrb_gc_destroy(mrb_state*, mrb_gc *gc); +int mrb_core_init_protect(mrb_state *mrb, void (*body)(mrb_state *, void *), void *opaque); + +static void +init_gc_and_core(mrb_state *mrb, void *opaque) +{ + static const struct mrb_context mrb_context_zero = { 0 }; + + mrb_gc_init(mrb, &mrb->gc); + mrb->c = (struct mrb_context*)mrb_malloc(mrb, sizeof(struct mrb_context)); + *mrb->c = mrb_context_zero; + mrb->root_c = mrb->c; + + mrb_init_core(mrb); +} + MRB_API mrb_state* mrb_open_core(mrb_allocf f, void *ud) { static const mrb_state mrb_state_zero = { 0 }; - static const struct mrb_context mrb_context_zero = { 0 }; mrb_state *mrb; if (f == NULL) f = mrb_default_allocf; @@ -35,12 +49,10 @@ mrb_open_core(mrb_allocf f, void *ud) mrb->allocf = f; mrb->atexit_stack_len = 0; - mrb_gc_init(mrb, &mrb->gc); - mrb->c = (struct mrb_context*)mrb_malloc(mrb, sizeof(struct mrb_context)); - *mrb->c = mrb_context_zero; - mrb->root_c = mrb->c; - - mrb_init_core(mrb); + if (mrb_core_init_protect(mrb, init_gc_and_core, NULL)) { + mrb_close(mrb); + return NULL; + } return mrb; } @@ -65,6 +77,12 @@ mrb_open(void) return mrb; } +static void +init_mrbgems(mrb_state *mrb, void *opaque) +{ + mrb_init_mrbgems(mrb); +} + MRB_API mrb_state* mrb_open_allocf(mrb_allocf f, void *ud) { @@ -75,7 +93,10 @@ mrb_open_allocf(mrb_allocf f, void *ud) } #ifndef DISABLE_GEMS - mrb_init_mrbgems(mrb); + if (mrb_core_init_protect(mrb, init_mrbgems, NULL)) { + mrb_close(mrb); + return NULL; + } mrb_gc_arena_restore(mrb, 0); #endif return mrb; @@ -131,9 +152,11 @@ mrb_irep_free(mrb_state *mrb, mrb_irep *irep) } mrb_free(mrb, irep->pool); mrb_free(mrb, irep->syms); - for (i=0; i<irep->rlen; i++) { - if (irep->reps[i]) - mrb_irep_decref(mrb, irep->reps[i]); + if (irep->reps) { + for (i=0; i<irep->rlen; i++) { + if (irep->reps[i]) + mrb_irep_decref(mrb, irep->reps[i]); + } } mrb_free(mrb, irep->reps); mrb_free(mrb, irep->lv); |
