Skip to content

Commit

Permalink
issue #163 Maintain optionality of defer statement
Browse files Browse the repository at this point in the history
  • Loading branch information
dibyendumajumdar committed Jul 5, 2020
1 parent b63005f commit 73c2c64
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 5 deletions.
4 changes: 2 additions & 2 deletions include/lfunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

#define upisopen(up) ((up)->v != &(up)->u.value)


#ifdef RAVI_DEFER_STATEMENT
/*
** Special "status" for 'luaF_close'
*/
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion include/lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);


Expand Down
2 changes: 2 additions & 0 deletions src/lcorolib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/ldo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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. */
Expand Down
2 changes: 2 additions & 0 deletions src/lstate.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -340,6 +341,7 @@ int lua_resetthread (lua_State *L) {
lua_unlock(L);
return status;
}
#endif

/* TODO following should probably not live here*/

Expand Down
4 changes: 2 additions & 2 deletions tests/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down

0 comments on commit 73c2c64

Please sign in to comment.