Skip to content

Commit

Permalink
Refactor with LUA_OK.
Browse files Browse the repository at this point in the history
Contributed by François Perrad.
  • Loading branch information
Mike Pall committed Apr 7, 2017
1 parent 22dfa63 commit 9c685f7
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 23 deletions.
8 changes: 4 additions & 4 deletions src/lib_base.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ LJLIB_ASM_(xpcall) LJLIB_REC(.)

static int load_aux(lua_State *L, int status, int envarg)
{
if (status == 0) {
if (status == LUA_OK) {
if (tvistab(L->base+envarg-1)) {
GCfunc *fn = funcV(L->top-1);
GCtab *t = tabV(L->base+envarg-1);
Expand Down Expand Up @@ -419,7 +419,7 @@ LJLIB_CF(dofile)
GCstr *fname = lj_lib_optstr(L, 1);
setnilV(L->top);
L->top = L->base+1;
if (luaL_loadfile(L, fname ? strdata(fname) : NULL) != 0)
if (luaL_loadfile(L, fname ? strdata(fname) : NULL) != LUA_OK)
lua_error(L);
lua_call(L, 0, LUA_MULTRET);
return (int)(L->top - L->base) - 1;
Expand Down Expand Up @@ -537,7 +537,7 @@ LJLIB_CF(coroutine_status)
co = threadV(L->base);
if (co == L) s = "running";
else if (co->status == LUA_YIELD) s = "suspended";
else if (co->status != 0) s = "dead";
else if (co->status != LUA_OK) s = "dead";
else if (co->base > tvref(co->stack)+1+LJ_FR2) s = "normal";
else if (co->top == co->base) s = "dead";
else s = "suspended";
Expand Down Expand Up @@ -583,7 +583,7 @@ LJLIB_ASM(coroutine_yield)
static int ffh_resume(lua_State *L, lua_State *co, int wrap)
{
if (co->cframe != NULL || co->status > LUA_YIELD ||
(co->status == 0 && co->top == co->base)) {
(co->status == LUA_OK && co->top == co->base)) {
ErrMsg em = co->cframe ? LJ_ERR_CORUN : LJ_ERR_CODEAD;
if (wrap) lj_err_caller(L, em);
setboolV(L->base-1-LJ_FR2, 0);
Expand Down
8 changes: 4 additions & 4 deletions src/lj_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,7 @@ static TValue *api_call_base(lua_State *L, int nargs)

LUA_API void lua_call(lua_State *L, int nargs, int nresults)
{
api_check(L, L->status == 0 || L->status == LUA_ERRERR);
api_check(L, L->status == LUA_OK || L->status == LUA_ERRERR);
api_checknelems(L, nargs+1);
lj_vm_call(L, api_call_base(L, nargs), nresults+1);
}
Expand All @@ -1043,7 +1043,7 @@ LUA_API int lua_pcall(lua_State *L, int nargs, int nresults, int errfunc)
uint8_t oldh = hook_save(g);
ptrdiff_t ef;
int status;
api_check(L, L->status == 0 || L->status == LUA_ERRERR);
api_check(L, L->status == LUA_OK || L->status == LUA_ERRERR);
api_checknelems(L, nargs+1);
if (errfunc == 0) {
ef = 0;
Expand Down Expand Up @@ -1075,7 +1075,7 @@ LUA_API int lua_cpcall(lua_State *L, lua_CFunction func, void *ud)
global_State *g = G(L);
uint8_t oldh = hook_save(g);
int status;
api_check(L, L->status == 0 || L->status == LUA_ERRERR);
api_check(L, L->status == LUA_OK || L->status == LUA_ERRERR);
status = lj_vm_cpcall(L, func, ud, cpcall);
if (status) hook_restore(g, oldh);
return status;
Expand Down Expand Up @@ -1140,7 +1140,7 @@ LUA_API int lua_resume(lua_State *L, int nargs)
{
if (L->cframe == NULL && L->status <= LUA_YIELD)
return lj_vm_resume(L,
L->status == 0 ? api_call_base(L, nargs) : L->top - nargs,
L->status == LUA_OK ? api_call_base(L, nargs) : L->top - nargs,
0, 0);
L->top = L->base;
setstrV(L, L->top, lj_err_str(L, LJ_ERR_COSUSP));
Expand Down
2 changes: 1 addition & 1 deletion src/lj_err.c
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ LJ_NOINLINE void LJ_FASTCALL lj_err_throw(lua_State *L, int errcode)
global_State *g = G(L);
lj_trace_abort(g);
setmref(g->jit_base, NULL);
L->status = 0;
L->status = LUA_OK;
#if LJ_UNWIND_EXT
err_raise_ext(errcode);
/*
Expand Down
8 changes: 4 additions & 4 deletions src/lj_state.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ LUA_API lua_State *lua_newstate(lua_Alloc f, void *ud)
close_state(L);
return NULL;
}
L->status = 0;
L->status = LUA_OK;
return L;
}

Expand Down Expand Up @@ -256,10 +256,10 @@ LUA_API void lua_close(lua_State *L)
#endif
for (i = 0;;) {
hook_enter(g);
L->status = 0;
L->status = LUA_OK;
L->base = L->top = tvref(L->stack) + 1 + LJ_FR2;
L->cframe = NULL;
if (lj_vm_cpcall(L, NULL, NULL, cpfinalize) == 0) {
if (lj_vm_cpcall(L, NULL, NULL, cpfinalize) == LUA_OK) {
if (++i >= 10) break;
lj_gc_separateudata(g, 1); /* Separate udata again. */
if (gcref(g->gc.mmudata) == NULL) /* Until nothing is left to do. */
Expand All @@ -274,7 +274,7 @@ lua_State *lj_state_new(lua_State *L)
lua_State *L1 = lj_mem_newobj(L, lua_State);
L1->gct = ~LJ_TTHREAD;
L1->dummy_ffid = FF_C;
L1->status = 0;
L1->status = LUA_OK;
L1->stacksize = 0;
setmref(L1->stack, NULL);
L1->cframe = NULL;
Expand Down
3 changes: 2 additions & 1 deletion src/lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
#define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i))


/* thread status; 0 is OK */
/* thread status */
#define LUA_OK 0
#define LUA_YIELD 1
#define LUA_ERRRUN 2
#define LUA_ERRSYNTAX 3
Expand Down
18 changes: 9 additions & 9 deletions src/luajit.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ static int docall(lua_State *L, int narg, int clear)
#endif
lua_remove(L, base); /* remove traceback function */
/* force a complete garbage collection in case of errors */
if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0);
if (status != LUA_OK) lua_gc(L, LUA_GCCOLLECT, 0);
return status;
}

Expand Down Expand Up @@ -249,9 +249,9 @@ static void dotty(lua_State *L)
const char *oldprogname = progname;
progname = NULL;
while ((status = loadline(L)) != -1) {
if (status == 0) status = docall(L, 0, 0);
if (status == LUA_OK) status = docall(L, 0, 0);
report(L, status);
if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */
if (status == LUA_OK && lua_gettop(L) > 0) { /* any result to print? */
lua_getglobal(L, "print");
lua_insert(L, 1);
if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
Expand All @@ -273,7 +273,7 @@ static int handle_script(lua_State *L, char **argx)
if (strcmp(fname, "-") == 0 && strcmp(argx[-1], "--") != 0)
fname = NULL; /* stdin */
status = luaL_loadfile(L, fname);
if (status == 0) {
if (status == LUA_OK) {
/* Fetch args from arg table. LUA_INIT or -e might have changed them. */
int narg = 0;
lua_getglobal(L, "arg");
Expand Down Expand Up @@ -483,7 +483,7 @@ static int runargs(lua_State *L, char **argv, int argn)
default: break;
}
}
return 0;
return LUA_OK;
}

static int handle_luainit(lua_State *L)
Expand All @@ -494,7 +494,7 @@ static int handle_luainit(lua_State *L)
const char *init = getenv(LUA_INIT);
#endif
if (init == NULL)
return 0; /* status OK */
return LUA_OK;
else if (init[0] == '@')
return dofile(L, init+1);
else
Expand Down Expand Up @@ -539,17 +539,17 @@ static int pmain(lua_State *L)

if (!(flags & FLAGS_NOENV)) {
s->status = handle_luainit(L);
if (s->status != 0) return 0;
if (s->status != LUA_OK) return 0;
}

if ((flags & FLAGS_VERSION)) print_version();

s->status = runargs(L, argv, argn);
if (s->status != 0) return 0;
if (s->status != LUA_OK) return 0;

if (s->argc > argn) {
s->status = handle_script(L, argv + argn);
if (s->status != 0) return 0;
if (s->status != LUA_OK) return 0;
}

if ((flags & FLAGS_INTERACTIVE)) {
Expand Down

0 comments on commit 9c685f7

Please sign in to comment.