From 0fd82b60cff02f1fe2a74d820902bd1fc4667d7b Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Mon, 18 Jan 2016 13:33:03 +0100 Subject: [PATCH] Make js_try, js_savetry and js_endtry public functions/macros. --- jsi.h | 10 ++-------- jsrun.c | 23 ++++++++++++++++++++++- mujs.h | 9 +++++++++ 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/jsi.h b/jsi.h index 40821e9..08ee5fd 100644 --- a/jsi.h +++ b/jsi.h @@ -117,16 +117,10 @@ struct js_Jumpbuf js_Instruction *pc; }; -void js_savetry(js_State *J, js_Instruction *pc); +void *js_savetrypc(js_State *J, js_Instruction *pc); #define js_trypc(J, PC) \ - setjmp((js_savetry(J, PC), J->trybuf[J->trytop++].buf)) - -#define js_try(J) \ - setjmp((js_savetry(J, NULL), J->trybuf[J->trytop++].buf)) - -#define js_endtry(J) \ - (--J->trytop) + setjmp(js_savetrypc(J, PC)) /* State struct */ diff --git a/jsrun.c b/jsrun.c index abacf11..c44cf6d 100644 --- a/jsrun.c +++ b/jsrun.c @@ -1144,7 +1144,7 @@ int js_pcall(js_State *J, int n) /* Exceptions */ -void js_savetry(js_State *J, js_Instruction *pc) +void *js_savetrypc(js_State *J, js_Instruction *pc) { if (J->trytop == JS_TRYLIMIT) js_error(J, "try: exception stack overflow"); @@ -1154,6 +1154,27 @@ void js_savetry(js_State *J, js_Instruction *pc) J->trybuf[J->trytop].top = J->top; J->trybuf[J->trytop].bot = J->bot; J->trybuf[J->trytop].pc = pc; + return J->trybuf[J->trytop++].buf; +} + +void *js_savetry(js_State *J) +{ + if (J->trytop == JS_TRYLIMIT) + js_error(J, "try: exception stack overflow"); + J->trybuf[J->trytop].E = J->E; + J->trybuf[J->trytop].envtop = J->envtop; + J->trybuf[J->trytop].tracetop = J->tracetop; + J->trybuf[J->trytop].top = J->top; + J->trybuf[J->trytop].bot = J->bot; + J->trybuf[J->trytop].pc = NULL; + return J->trybuf[J->trytop++].buf; +} + +void js_endtry(js_State *J) +{ + if (J->trytop == 0) + js_error(J, "endtry: exception stack underflow"); + --J->trytop; } void js_throw(js_State *J) diff --git a/mujs.h b/mujs.h index c1d66c8..3b8169c 100644 --- a/mujs.h +++ b/mujs.h @@ -48,6 +48,15 @@ int js_ploadfile(js_State *J, const char *filename); int js_pcall(js_State *J, int n); int js_pconstruct(js_State *J, int n); +/* Exception handling */ + +void *js_savetry(js_State *J); /* returns a jmp_buf */ + +#define js_try(J) \ + setjmp(js_savetry(J)) + +void js_endtry(js_State *J); + /* State constructor flags */ enum { JS_STRICT = 1,