Просмотр исходного кода

Refactor with LUA_OK.

Contributed by François Perrad.
Mike Pall 8 лет назад
Родитель
Сommit
9c685f7003
6 измененных файлов с 24 добавлено и 23 удалено
  1. 4 4
      src/lib_base.c
  2. 4 4
      src/lj_api.c
  3. 1 1
      src/lj_err.c
  4. 4 4
      src/lj_state.c
  5. 2 1
      src/lua.h
  6. 9 9
      src/luajit.c

+ 4 - 4
src/lib_base.c

@@ -346,7 +346,7 @@ LJLIB_ASM_(xpcall)		LJLIB_REC(.)
 
 
 static int load_aux(lua_State *L, int status, int envarg)
 static int load_aux(lua_State *L, int status, int envarg)
 {
 {
-  if (status == 0) {
+  if (status == LUA_OK) {
     if (tvistab(L->base+envarg-1)) {
     if (tvistab(L->base+envarg-1)) {
       GCfunc *fn = funcV(L->top-1);
       GCfunc *fn = funcV(L->top-1);
       GCtab *t = tabV(L->base+envarg-1);
       GCtab *t = tabV(L->base+envarg-1);
@@ -419,7 +419,7 @@ LJLIB_CF(dofile)
   GCstr *fname = lj_lib_optstr(L, 1);
   GCstr *fname = lj_lib_optstr(L, 1);
   setnilV(L->top);
   setnilV(L->top);
   L->top = L->base+1;
   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_error(L);
   lua_call(L, 0, LUA_MULTRET);
   lua_call(L, 0, LUA_MULTRET);
   return (int)(L->top - L->base) - 1;
   return (int)(L->top - L->base) - 1;
@@ -537,7 +537,7 @@ LJLIB_CF(coroutine_status)
   co = threadV(L->base);
   co = threadV(L->base);
   if (co == L) s = "running";
   if (co == L) s = "running";
   else if (co->status == LUA_YIELD) s = "suspended";
   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->base > tvref(co->stack)+1+LJ_FR2) s = "normal";
   else if (co->top == co->base) s = "dead";
   else if (co->top == co->base) s = "dead";
   else s = "suspended";
   else s = "suspended";
@@ -583,7 +583,7 @@ LJLIB_ASM(coroutine_yield)
 static int ffh_resume(lua_State *L, lua_State *co, int wrap)
 static int ffh_resume(lua_State *L, lua_State *co, int wrap)
 {
 {
   if (co->cframe != NULL || co->status > LUA_YIELD ||
   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;
     ErrMsg em = co->cframe ? LJ_ERR_CORUN : LJ_ERR_CODEAD;
     if (wrap) lj_err_caller(L, em);
     if (wrap) lj_err_caller(L, em);
     setboolV(L->base-1-LJ_FR2, 0);
     setboolV(L->base-1-LJ_FR2, 0);

+ 4 - 4
src/lj_api.c

@@ -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)
 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);
   api_checknelems(L, nargs+1);
   lj_vm_call(L, api_call_base(L, nargs), nresults+1);
   lj_vm_call(L, api_call_base(L, nargs), nresults+1);
 }
 }
@@ -1043,7 +1043,7 @@ LUA_API int lua_pcall(lua_State *L, int nargs, int nresults, int errfunc)
   uint8_t oldh = hook_save(g);
   uint8_t oldh = hook_save(g);
   ptrdiff_t ef;
   ptrdiff_t ef;
   int status;
   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);
   api_checknelems(L, nargs+1);
   if (errfunc == 0) {
   if (errfunc == 0) {
     ef = 0;
     ef = 0;
@@ -1075,7 +1075,7 @@ LUA_API int lua_cpcall(lua_State *L, lua_CFunction func, void *ud)
   global_State *g = G(L);
   global_State *g = G(L);
   uint8_t oldh = hook_save(g);
   uint8_t oldh = hook_save(g);
   int status;
   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);
   status = lj_vm_cpcall(L, func, ud, cpcall);
   if (status) hook_restore(g, oldh);
   if (status) hook_restore(g, oldh);
   return status;
   return status;
@@ -1140,7 +1140,7 @@ LUA_API int lua_resume(lua_State *L, int nargs)
 {
 {
   if (L->cframe == NULL && L->status <= LUA_YIELD)
   if (L->cframe == NULL && L->status <= LUA_YIELD)
     return lj_vm_resume(L,
     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);
       0, 0);
   L->top = L->base;
   L->top = L->base;
   setstrV(L, L->top, lj_err_str(L, LJ_ERR_COSUSP));
   setstrV(L, L->top, lj_err_str(L, LJ_ERR_COSUSP));

+ 1 - 1
src/lj_err.c

@@ -509,7 +509,7 @@ LJ_NOINLINE void LJ_FASTCALL lj_err_throw(lua_State *L, int errcode)
   global_State *g = G(L);
   global_State *g = G(L);
   lj_trace_abort(g);
   lj_trace_abort(g);
   setmref(g->jit_base, NULL);
   setmref(g->jit_base, NULL);
-  L->status = 0;
+  L->status = LUA_OK;
 #if LJ_UNWIND_EXT
 #if LJ_UNWIND_EXT
   err_raise_ext(errcode);
   err_raise_ext(errcode);
   /*
   /*

+ 4 - 4
src/lj_state.c

@@ -224,7 +224,7 @@ LUA_API lua_State *lua_newstate(lua_Alloc f, void *ud)
     close_state(L);
     close_state(L);
     return NULL;
     return NULL;
   }
   }
-  L->status = 0;
+  L->status = LUA_OK;
   return L;
   return L;
 }
 }
 
 
@@ -256,10 +256,10 @@ LUA_API void lua_close(lua_State *L)
 #endif
 #endif
   for (i = 0;;) {
   for (i = 0;;) {
     hook_enter(g);
     hook_enter(g);
-    L->status = 0;
+    L->status = LUA_OK;
     L->base = L->top = tvref(L->stack) + 1 + LJ_FR2;
     L->base = L->top = tvref(L->stack) + 1 + LJ_FR2;
     L->cframe = NULL;
     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;
       if (++i >= 10) break;
       lj_gc_separateudata(g, 1);  /* Separate udata again. */
       lj_gc_separateudata(g, 1);  /* Separate udata again. */
       if (gcref(g->gc.mmudata) == NULL)  /* Until nothing is left to do. */
       if (gcref(g->gc.mmudata) == NULL)  /* Until nothing is left to do. */
@@ -274,7 +274,7 @@ lua_State *lj_state_new(lua_State *L)
   lua_State *L1 = lj_mem_newobj(L, lua_State);
   lua_State *L1 = lj_mem_newobj(L, lua_State);
   L1->gct = ~LJ_TTHREAD;
   L1->gct = ~LJ_TTHREAD;
   L1->dummy_ffid = FF_C;
   L1->dummy_ffid = FF_C;
-  L1->status = 0;
+  L1->status = LUA_OK;
   L1->stacksize = 0;
   L1->stacksize = 0;
   setmref(L1->stack, NULL);
   setmref(L1->stack, NULL);
   L1->cframe = NULL;
   L1->cframe = NULL;

+ 2 - 1
src/lua.h

@@ -39,7 +39,8 @@
 #define lua_upvalueindex(i)	(LUA_GLOBALSINDEX-(i))
 #define lua_upvalueindex(i)	(LUA_GLOBALSINDEX-(i))
 
 
 
 
-/* thread status; 0 is OK */
+/* thread status */
+#define LUA_OK		0
 #define LUA_YIELD	1
 #define LUA_YIELD	1
 #define LUA_ERRRUN	2
 #define LUA_ERRRUN	2
 #define LUA_ERRSYNTAX	3
 #define LUA_ERRSYNTAX	3

+ 9 - 9
src/luajit.c

@@ -124,7 +124,7 @@ static int docall(lua_State *L, int narg, int clear)
 #endif
 #endif
   lua_remove(L, base);  /* remove traceback function */
   lua_remove(L, base);  /* remove traceback function */
   /* force a complete garbage collection in case of errors */
   /* 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;
   return status;
 }
 }
 
 
@@ -249,9 +249,9 @@ static void dotty(lua_State *L)
   const char *oldprogname = progname;
   const char *oldprogname = progname;
   progname = NULL;
   progname = NULL;
   while ((status = loadline(L)) != -1) {
   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);
     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_getglobal(L, "print");
       lua_insert(L, 1);
       lua_insert(L, 1);
       if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
       if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
@@ -273,7 +273,7 @@ static int handle_script(lua_State *L, char **argx)
   if (strcmp(fname, "-") == 0 && strcmp(argx[-1], "--") != 0)
   if (strcmp(fname, "-") == 0 && strcmp(argx[-1], "--") != 0)
     fname = NULL;  /* stdin */
     fname = NULL;  /* stdin */
   status = luaL_loadfile(L, fname);
   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. */
     /* Fetch args from arg table. LUA_INIT or -e might have changed them. */
     int narg = 0;
     int narg = 0;
     lua_getglobal(L, "arg");
     lua_getglobal(L, "arg");
@@ -483,7 +483,7 @@ static int runargs(lua_State *L, char **argv, int argn)
     default: break;
     default: break;
     }
     }
   }
   }
-  return 0;
+  return LUA_OK;
 }
 }
 
 
 static int handle_luainit(lua_State *L)
 static int handle_luainit(lua_State *L)
@@ -494,7 +494,7 @@ static int handle_luainit(lua_State *L)
   const char *init = getenv(LUA_INIT);
   const char *init = getenv(LUA_INIT);
 #endif
 #endif
   if (init == NULL)
   if (init == NULL)
-    return 0;  /* status OK */
+    return LUA_OK;
   else if (init[0] == '@')
   else if (init[0] == '@')
     return dofile(L, init+1);
     return dofile(L, init+1);
   else
   else
@@ -539,17 +539,17 @@ static int pmain(lua_State *L)
 
 
   if (!(flags & FLAGS_NOENV)) {
   if (!(flags & FLAGS_NOENV)) {
     s->status = handle_luainit(L);
     s->status = handle_luainit(L);
-    if (s->status != 0) return 0;
+    if (s->status != LUA_OK) return 0;
   }
   }
 
 
   if ((flags & FLAGS_VERSION)) print_version();
   if ((flags & FLAGS_VERSION)) print_version();
 
 
   s->status = runargs(L, argv, argn);
   s->status = runargs(L, argv, argn);
-  if (s->status != 0) return 0;
+  if (s->status != LUA_OK) return 0;
 
 
   if (s->argc > argn) {
   if (s->argc > argn) {
     s->status = handle_script(L, argv + 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)) {
   if ((flags & FLAGS_INTERACTIVE)) {