diff --git a/extlib/benz/error.c b/extlib/benz/error.c index a9390af2..b4964172 100644 --- a/extlib/benz/error.c +++ b/extlib/benz/error.c @@ -63,8 +63,8 @@ pic_errmsg(pic_state *pic) return pic_str_cstr(pic, str); } -static pic_value -native_exception_handler(pic_state *pic) +pic_value +pic_native_exception_handler(pic_state *pic) { pic_value err; struct pic_proc *cont; @@ -81,16 +81,11 @@ native_exception_handler(pic_state *pic) } void -pic_push_try(pic_state *pic, struct pic_proc *cont) +pic_push_handler(pic_state *pic, struct pic_proc *handler) { - struct pic_proc *handler; size_t xp_len; ptrdiff_t xp_offset; - handler = pic_make_proc(pic, native_exception_handler, "(native-exception-handler)"); - - pic_proc_env_set(pic, handler, "cont", pic_obj_value(cont)); - if (pic->xp >= pic->xpend) { xp_len = (size_t)(pic->xpend - pic->xpbase) * 2; xp_offset = pic->xp - pic->xpbase; @@ -102,10 +97,14 @@ pic_push_try(pic_state *pic, struct pic_proc *cont) *pic->xp++ = handler; } -void -pic_pop_try(pic_state *pic) +struct pic_proc * +pic_pop_handler(pic_state *pic) { - --pic->xp; + if (pic->xp == pic->xpbase) { + pic_panic(pic, "no exception handler registered"); + } + + return *--pic->xp; } struct pic_error * @@ -131,17 +130,13 @@ pic_raise_continuable(pic_state *pic, pic_value err) struct pic_proc *handler; pic_value v; - if (pic->xp == pic->xpbase) { - pic_panic(pic, "no exception handler registered"); - } - - handler = *--pic->xp; + handler = pic_pop_handler(pic); pic_gc_protect(pic, pic_obj_value(handler)); v = pic_apply1(pic, handler, err); - *pic->xp++ = handler; + pic_push_handler(pic, handler); return v; } @@ -153,7 +148,7 @@ pic_raise(pic_state *pic, pic_value err) val = pic_raise_continuable(pic, err); - --pic->xp; + pic_pop_handler(pic); pic_errorf(pic, "error handler returned with ~s on error ~s", val, err); } @@ -179,24 +174,14 @@ pic_error_with_exception_handler(pic_state *pic) { struct pic_proc *handler, *thunk; pic_value val; - size_t xp_len; - ptrdiff_t xp_offset; pic_get_args(pic, "ll", &handler, &thunk); - if (pic->xp >= pic->xpend) { - xp_len = (size_t)(pic->xpend - pic->xpbase) * 2; - xp_offset = pic->xp - pic->xpbase; - pic->xpbase = pic_realloc(pic, pic->xpbase, sizeof(struct pic_proc *) * xp_len); - pic->xp = pic->xpbase + xp_offset; - pic->xpend = pic->xpbase + xp_len; - } - - *pic->xp++ = handler; + pic_push_handler(pic, handler); val = pic_apply0(pic, thunk); - --pic->xp; + pic_pop_handler(pic); return val; } diff --git a/extlib/benz/include/picrin/error.h b/extlib/benz/include/picrin/error.h index ce20eb69..7200ddf9 100644 --- a/extlib/benz/include/picrin/error.h +++ b/extlib/benz/include/picrin/error.h @@ -25,18 +25,22 @@ struct pic_error *pic_make_error(pic_state *, pic_sym *, const char *, pic_list) /* do not return from try block! */ #define pic_try \ - pic_try_(PIC_GENSYM(escape)) + pic_try_(PIC_GENSYM(cont), PIC_GENSYM(handler)) #define pic_catch \ pic_catch_(PIC_GENSYM(label)) -#define pic_try_(cont) \ +#define pic_try_(cont, handler) \ do { \ struct pic_cont *cont = pic_malloc(pic, sizeof(struct pic_cont)); \ pic_save_point(pic, cont); \ if (PIC_SETJMP(pic, cont->jmp.buf) == 0) { \ + extern pic_value pic_native_exception_handler(pic_state *); \ + struct pic_proc *handler; \ + handler = pic_make_proc(pic, pic_native_exception_handler, "(native-exception-handler)"); \ + pic_proc_env_set(pic, handler, "cont", pic_obj_value(pic_make_cont(pic, cont))); \ do { \ - pic_push_try(pic, pic_make_cont(pic, cont)); + pic_push_handler(pic, handler); #define pic_catch_(label) \ - pic_pop_try(pic); \ + pic_pop_handler(pic); \ } while (0); \ pic->jmp = pic->jmp->prev; \ } else { \ @@ -47,8 +51,8 @@ struct pic_error *pic_make_error(pic_state *, pic_sym *, const char *, pic_list) if (0) \ label: -void pic_push_try(pic_state *, struct pic_proc *); -void pic_pop_try(pic_state *); +void pic_push_handler(pic_state *, struct pic_proc *); +struct pic_proc *pic_pop_handler(pic_state *); pic_value pic_raise_continuable(pic_state *, pic_value); PIC_NORETURN void pic_raise(pic_state *, pic_value);