From 73c2c6426fe568681044f62cac72d5ab0562097a Mon Sep 17 00:00:00 2001 From: Dibyendu Majumdar Date: Sun, 5 Jul 2020 16:19:47 +0100 Subject: [PATCH] issue #163 Maintain optionality of defer statement --- include/lfunc.h | 4 ++-- include/lua.h | 3 ++- src/lcorolib.c | 2 ++ src/ldo.c | 4 ++++ src/lstate.c | 2 ++ tests/run_tests.sh | 4 ++-- 6 files changed, 14 insertions(+), 5 deletions(-) diff --git a/include/lfunc.h b/include/lfunc.h index 67931898..21f1aa25 100644 --- a/include/lfunc.h +++ b/include/lfunc.h @@ -32,7 +32,7 @@ #define upisopen(up) ((up)->v != &(up)->u.value) - +#ifdef RAVI_DEFER_STATEMENT /* ** Special "status" for 'luaF_close' */ @@ -42,7 +42,7 @@ /* close upvalues running all closing methods in protected mode */ #define CLOSEPROTECT (-2) - +#endif LUAI_FUNC Proto *luaF_newproto (lua_State *L); LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nelems); diff --git a/include/lua.h b/include/lua.h index 0f89bbaf..88657cfd 100644 --- a/include/lua.h +++ b/include/lua.h @@ -226,8 +226,9 @@ extern const char lua_ident[]; LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); LUA_API void (lua_close) (lua_State *L); LUA_API lua_State *(lua_newthread) (lua_State *L); +#ifdef RAVI_DEFER_STATEMENT LUA_API int (lua_resetthread) (lua_State *L); - +#endif LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); diff --git a/src/lcorolib.c b/src/lcorolib.c index c0ef009b..8e71d50b 100644 --- a/src/lcorolib.c +++ b/src/lcorolib.c @@ -75,9 +75,11 @@ static int luaB_auxwrap (lua_State *L) { lua_State *co = lua_tothread(L, lua_upvalueindex(1)); int r = auxresume(L, co, lua_gettop(L)); if (r < 0) { +#ifdef RAVI_DEFER_STATEMENT int stat = lua_status(co); if (stat != LUA_OK && stat != LUA_YIELD) lua_resetthread(co); /* close variables in case of errors */ +#endif if (lua_type(L, -1) == LUA_TSTRING) { /* error object is a string? */ luaL_where(L, 1); /* add extra info, if available */ lua_insert(L, -2); diff --git a/src/ldo.c b/src/ldo.c index c6bb83ec..4a4aea5a 100644 --- a/src/ldo.c +++ b/src/ldo.c @@ -104,10 +104,12 @@ void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) { setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling")); break; } +#ifdef RAVI_DEFER_STATEMENT case CLOSEPROTECT: { setnilvalue(oldtop); /* no error message */ break; } +#endif default: { setobjs2s(L, oldtop, L->top - 1); /* error message on current top */ break; @@ -124,7 +126,9 @@ l_noret luaD_throw (lua_State *L, int errcode) { } else { /* thread has no error handler */ global_State *g = G(L); +#ifdef RAVI_DEFER_STATEMENT errcode = luaF_close(L, L->stack, errcode); /* close all upvalues */ +#endif L->status = cast_byte(errcode); /* mark it as dead */ if (g->mainthread->errorJmp) { /* main thread has a handler? */ setobjs2s(L, g->mainthread->top++, L->top - 1); /* copy error obj. */ diff --git a/src/lstate.c b/src/lstate.c index 4fbe2faa..3061b276 100644 --- a/src/lstate.c +++ b/src/lstate.c @@ -320,6 +320,7 @@ void luaE_freethread (lua_State *L, lua_State *L1) { luaM_free(L, l); } +#ifdef RAVI_DEFER_STATEMENT int lua_resetthread (lua_State *L) { CallInfo *ci; int status; @@ -340,6 +341,7 @@ int lua_resetthread (lua_State *L) { lua_unlock(L); return status; } +#endif /* TODO following should probably not live here*/ diff --git a/tests/run_tests.sh b/tests/run_tests.sh index 74829950..d64d64d7 100644 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -44,8 +44,8 @@ run_lua53_tests "_port=true ravi.auto(true,1)" "Lua53 auto JIT all test failed" run_ravi_tests language ravi_tests1.ravi "ravi.jit(false)" run_ravi_tests language ravi_tests1.ravi "ravi.auto(true,1)" -#run_ravi_tests language defer_tests.ravi "ravi.jit(false)" -#run_ravi_tests language defer_tests.ravi "ravi.auto(true,1)" +run_ravi_tests language defer_tests.ravi "ravi.jit(false)" +run_ravi_tests language defer_tests.ravi "ravi.auto(true,1)" run_ravi_tests language ravi_tests3.ravi "ravi.auto(true,1)" run_ravi_tests language ravi_errors.ravi "ravi.auto(true,1)" run_ravi_tests language basics.lua "ravi.auto(true,1)"