Ver código fonte

New type 'TStatus' for thread status/error codes

Roberto Ierusalimschy 6 meses atrás
pai
commit
d1e677c52b
10 arquivos alterados com 56 adições e 46 exclusões
  1. 5 5
      lapi.c
  2. 23 22
      ldo.c
  3. 8 6
      ldo.h
  4. 3 2
      lfunc.c
  5. 2 2
      lfunc.h
  6. 1 1
      lgc.c
  7. 6 0
      llimits.h
  8. 3 3
      lstate.c
  9. 4 4
      lstate.h
  10. 1 1
      lstring.c

+ 5 - 5
lapi.c

@@ -1070,7 +1070,7 @@ static void f_call (lua_State *L, void *ud) {
 LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
                         lua_KContext ctx, lua_KFunction k) {
   struct CallS c;
-  int status;
+  TStatus status;
   ptrdiff_t func;
   lua_lock(L);
   api_check(L, k == NULL || !isLua(L->ci),
@@ -1107,14 +1107,14 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
   }
   adjustresults(L, nresults);
   lua_unlock(L);
-  return status;
+  return APIstatus(status);
 }
 
 
 LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
                       const char *chunkname, const char *mode) {
   ZIO z;
-  int status;
+  TStatus status;
   lua_lock(L);
   if (!chunkname) chunkname = "?";
   luaZ_init(L, &z, reader, data);
@@ -1131,7 +1131,7 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
     }
   }
   lua_unlock(L);
-  return status;
+  return APIstatus(status);
 }
 
 
@@ -1154,7 +1154,7 @@ LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) {
 
 
 LUA_API int lua_status (lua_State *L) {
-  return L->status;
+  return APIstatus(L->status);
 }
 
 

+ 23 - 22
ldo.c

@@ -97,11 +97,11 @@
 struct lua_longjmp {
   struct lua_longjmp *previous;
   luai_jmpbuf b;
-  volatile int status;  /* error code */
+  volatile TStatus status;  /* error code */
 };
 
 
-void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) {
+void luaD_seterrorobj (lua_State *L, TStatus errcode, StkId oldtop) {
   switch (errcode) {
     case LUA_ERRMEM: {  /* memory error? */
       setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */
@@ -125,7 +125,7 @@ void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) {
 }
 
 
-l_noret luaD_throw (lua_State *L, int errcode) {
+l_noret luaD_throw (lua_State *L, TStatus errcode) {
   if (L->errorJmp) {  /* thread has an error handler? */
     L->errorJmp->status = errcode;  /* set status */
     LUAI_THROW(L, L->errorJmp);  /* jump to it */
@@ -133,7 +133,7 @@ l_noret luaD_throw (lua_State *L, int errcode) {
   else {  /* thread has no error handler */
     global_State *g = G(L);
     errcode = luaE_resetthread(L, errcode);  /* close all upvalues */
-    L->status = cast_byte(errcode);
+    L->status = errcode;
     if (g->mainthread->errorJmp) {  /* main thread has a handler? */
       setobjs2s(L, g->mainthread->top.p++, L->top.p - 1);  /* copy error obj. */
       luaD_throw(g->mainthread, errcode);  /* re-throw in main thread */
@@ -149,7 +149,7 @@ l_noret luaD_throw (lua_State *L, int errcode) {
 }
 
 
-int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
+TStatus luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
   l_uint32 oldnCcalls = L->nCcalls;
   struct lua_longjmp lj;
   lj.status = LUA_OK;
@@ -751,8 +751,8 @@ void luaD_callnoyield (lua_State *L, StkId func, int nResults) {
 ** particular, field CIST_RECST preserves the error status across these
 ** multiple runs, changing only if there is a new error.
 */
-static int finishpcallk (lua_State *L,  CallInfo *ci) {
-  int status = getcistrecst(ci);  /* get original status */
+static TStatus finishpcallk (lua_State *L,  CallInfo *ci) {
+  TStatus status = getcistrecst(ci);  /* get original status */
   if (l_likely(status == LUA_OK))  /* no error? */
     status = LUA_YIELD;  /* was interrupted by an yield */
   else {  /* error */
@@ -792,14 +792,15 @@ static void finishCcall (lua_State *L, CallInfo *ci) {
     /* don't need to reset CIST_CLSRET, as it will be set again anyway */
   }
   else {
-    int status = LUA_YIELD;  /* default if there were no errors */
+    TStatus status = LUA_YIELD;  /* default if there were no errors */
+    lua_KFunction kf = ci->u.c.k;  /* continuation function */
     /* must have a continuation and must be able to call it */
-    lua_assert(ci->u.c.k != NULL && yieldable(L));
+    lua_assert(kf != NULL && yieldable(L));
     if (ci->callstatus & CIST_YPCALL)   /* was inside a 'lua_pcallk'? */
       status = finishpcallk(L, ci);  /* finish it */
     adjustresults(L, LUA_MULTRET);  /* finish 'lua_callk' */
     lua_unlock(L);
-    n = (*ci->u.c.k)(L, status, ci->u.c.ctx);  /* call continuation */
+    n = (*kf)(L, APIstatus(status), ci->u.c.ctx);  /* call continuation */
     lua_lock(L);
     api_checknelems(L, n);
   }
@@ -901,7 +902,7 @@ static void resume (lua_State *L, void *ud) {
 ** (status == LUA_YIELD), or an unprotected error ('findpcall' doesn't
 ** find a recover point).
 */
-static int precover (lua_State *L, int status) {
+static TStatus precover (lua_State *L, TStatus status) {
   CallInfo *ci;
   while (errorstatus(status) && (ci = findpcall(L)) != NULL) {
     L->ci = ci;  /* go down to recovery functions */
@@ -914,7 +915,7 @@ static int precover (lua_State *L, int status) {
 
 LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs,
                                       int *nresults) {
-  int status;
+  TStatus status;
   lua_lock(L);
   if (L->status == LUA_OK) {  /* may be starting a coroutine */
     if (L->ci != &L->base_ci)  /* not in base level? */
@@ -936,14 +937,14 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs,
   if (l_likely(!errorstatus(status)))
     lua_assert(status == L->status);  /* normal end or yield */
   else {  /* unrecoverable error */
-    L->status = cast_byte(status);  /* mark thread as 'dead' */
+    L->status = status;  /* mark thread as 'dead' */
     luaD_seterrorobj(L, status, L->top.p);  /* push error message */
     L->ci->top.p = L->top.p;
   }
   *nresults = (status == LUA_YIELD) ? L->ci->u2.nyield
                                     : cast_int(L->top.p - (L->ci->func.p + 1));
   lua_unlock(L);
-  return status;
+  return APIstatus(status);
 }
 
 
@@ -988,7 +989,7 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx,
 */
 struct CloseP {
   StkId level;
-  int status;
+  TStatus status;
 };
 
 
@@ -1005,7 +1006,7 @@ static void closepaux (lua_State *L, void *ud) {
 ** Calls 'luaF_close' in protected mode. Return the original status
 ** or, in case of errors, the new status.
 */
-int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status) {
+TStatus luaD_closeprotected (lua_State *L, ptrdiff_t level, TStatus status) {
   CallInfo *old_ci = L->ci;
   lu_byte old_allowhooks = L->allowhook;
   for (;;) {  /* keep closing upvalues until no more errors */
@@ -1027,9 +1028,9 @@ int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status) {
 ** thread information ('allowhook', etc.) and in particular
 ** its stack level in case of errors.
 */
-int luaD_pcall (lua_State *L, Pfunc func, void *u,
-                ptrdiff_t old_top, ptrdiff_t ef) {
-  int status;
+TStatus luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t old_top,
+                                  ptrdiff_t ef) {
+  TStatus status;
   CallInfo *old_ci = L->ci;
   lu_byte old_allowhooks = L->allowhook;
   ptrdiff_t old_errfunc = L->errfunc;
@@ -1091,10 +1092,10 @@ static void f_parser (lua_State *L, void *ud) {
 }
 
 
-int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
-                                        const char *mode) {
+TStatus luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
+                                            const char *mode) {
   struct SParser p;
-  int status;
+  TStatus status;
   incnny(L);  /* cannot yield during parsing */
   p.z = z; p.name = name; p.mode = mode;
   p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0;

+ 8 - 6
ldo.h

@@ -67,8 +67,9 @@
 /* type of protected functions, to be ran by 'runprotected' */
 typedef void (*Pfunc) (lua_State *L, void *ud);
 
-LUAI_FUNC void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop);
-LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
+LUAI_FUNC void luaD_seterrorobj (lua_State *L, TStatus errcode, StkId oldtop);
+LUAI_FUNC TStatus luaD_protectedparser (lua_State *L, ZIO *z,
+                                                  const char *name,
                                                   const char *mode);
 LUAI_FUNC void luaD_hook (lua_State *L, int event, int line,
                                         int fTransfer, int nTransfer);
@@ -78,8 +79,9 @@ LUAI_FUNC int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func,
 LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nResults);
 LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults);
 LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults);
-LUAI_FUNC int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status);
-LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u,
+LUAI_FUNC TStatus luaD_closeprotected (lua_State *L, ptrdiff_t level,
+                                                     TStatus status);
+LUAI_FUNC TStatus luaD_pcall (lua_State *L, Pfunc func, void *u,
                                         ptrdiff_t oldtop, ptrdiff_t ef);
 LUAI_FUNC void luaD_poscall (lua_State *L, CallInfo *ci, int nres);
 LUAI_FUNC int luaD_reallocstack (lua_State *L, int newsize, int raiseerror);
@@ -87,8 +89,8 @@ LUAI_FUNC int luaD_growstack (lua_State *L, int n, int raiseerror);
 LUAI_FUNC void luaD_shrinkstack (lua_State *L);
 LUAI_FUNC void luaD_inctop (lua_State *L);
 
-LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode);
-LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud);
+LUAI_FUNC l_noret luaD_throw (lua_State *L, TStatus errcode);
+LUAI_FUNC TStatus luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud);
 
 #endif
 

+ 3 - 2
lfunc.c

@@ -140,7 +140,8 @@ static void checkclosemth (lua_State *L, StkId level) {
 ** the 'level' of the upvalue being closed, as everything after that
 ** won't be used again.
 */
-static void prepcallclosemth (lua_State *L, StkId level, int status, int yy) {
+static void prepcallclosemth (lua_State *L, StkId level, TStatus status,
+                                            int yy) {
   TValue *uv = s2v(level);  /* value being closed */
   TValue *errobj;
   if (status == CLOSEKTOP)
@@ -224,7 +225,7 @@ static void poptbclist (lua_State *L) {
 ** Close all upvalues and to-be-closed variables up to the given stack
 ** level. Return restored 'level'.
 */
-StkId luaF_close (lua_State *L, StkId level, int status, int yy) {
+StkId luaF_close (lua_State *L, StkId level, TStatus status, int yy) {
   ptrdiff_t levelrel = savestack(L, level);
   luaF_closeupval(L, level);  /* first, close the upvalues */
   while (L->tbclist.p >= level) {  /* traverse tbc's down to that level */

+ 2 - 2
lfunc.h

@@ -44,7 +44,7 @@
 
 
 /* special status to close upvalues preserving the top of the stack */
-#define CLOSEKTOP	(-1)
+#define CLOSEKTOP	(LUA_ERRERR + 1)
 
 
 LUAI_FUNC Proto *luaF_newproto (lua_State *L);
@@ -54,7 +54,7 @@ LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl);
 LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level);
 LUAI_FUNC void luaF_newtbcupval (lua_State *L, StkId level);
 LUAI_FUNC void luaF_closeupval (lua_State *L, StkId level);
-LUAI_FUNC StkId luaF_close (lua_State *L, StkId level, int status, int yy);
+LUAI_FUNC StkId luaF_close (lua_State *L, StkId level, TStatus status, int yy);
 LUAI_FUNC void luaF_unlinkupval (UpVal *uv);
 LUAI_FUNC lu_mem luaF_protosize (Proto *p);
 LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f);

+ 1 - 1
lgc.c

@@ -953,7 +953,7 @@ static void GCTM (lua_State *L) {
   setgcovalue(L, &v, udata2finalize(g));
   tm = luaT_gettmbyobj(L, &v, TM_GC);
   if (!notm(tm)) {  /* is there a finalizer? */
-    int status;
+    TStatus status;
     lu_byte oldah = L->allowhook;
     lu_byte oldgcstp  = g->gcstp;
     g->gcstp |= GCSTPGC;  /* avoid GC steps */

+ 6 - 0
llimits.h

@@ -41,6 +41,12 @@ typedef unsigned char lu_byte;
 typedef signed char ls_byte;
 
 
+/* Type for thread status/error codes */
+typedef lu_byte TStatus;
+
+/* The C API still uses 'int' for status/error codes */
+#define APIstatus(st)	cast_int(st)
+
 /* maximum value for size_t */
 #define MAX_SIZET	((size_t)(~(size_t)0))
 

+ 3 - 3
lstate.c

@@ -320,7 +320,7 @@ void luaE_freethread (lua_State *L, lua_State *L1) {
 }
 
 
-int luaE_resetthread (lua_State *L, int status) {
+TStatus luaE_resetthread (lua_State *L, TStatus status) {
   CallInfo *ci = L->ci = &L->base_ci;  /* unwind CallInfo list */
   setnilvalue(s2v(L->stack.p));  /* 'function' entry for basic 'ci' */
   ci->func.p = L->stack.p;
@@ -340,12 +340,12 @@ int luaE_resetthread (lua_State *L, int status) {
 
 
 LUA_API int lua_closethread (lua_State *L, lua_State *from) {
-  int status;
+  TStatus status;
   lua_lock(L);
   L->nCcalls = (from) ? getCcalls(from) : 0;
   status = luaE_resetthread(L, L->status);
   lua_unlock(L);
-  return status;
+  return APIstatus(status);
 }
 
 

+ 4 - 4
lstate.h

@@ -339,8 +339,8 @@ typedef struct global_State {
 */
 struct lua_State {
   CommonHeader;
-  lu_byte status;
   lu_byte allowhook;
+  TStatus status;
   unsigned short nci;  /* number of items in 'ci' list */
   StkIdRel top;  /* first free slot in the stack */
   global_State *l_G;
@@ -352,10 +352,10 @@ struct lua_State {
   GCObject *gclist;
   struct lua_State *twups;  /* list of threads with open upvalues */
   struct lua_longjmp *errorJmp;  /* current error recover point */
-  CallInfo base_ci;  /* CallInfo for first level (C calling Lua) */
+  CallInfo base_ci;  /* CallInfo for first level (C host) */
   volatile lua_Hook hook;
   ptrdiff_t errfunc;  /* current error handling function (stack index) */
-  l_uint32 nCcalls;  /* number of nested (non-yieldable | C)  calls */
+  l_uint32 nCcalls;  /* number of nested non-yieldable or C calls */
   int oldpc;  /* last pc traced */
   int basehookcount;
   int hookcount;
@@ -438,7 +438,7 @@ LUAI_FUNC void luaE_checkcstack (lua_State *L);
 LUAI_FUNC void luaE_incCstack (lua_State *L);
 LUAI_FUNC void luaE_warning (lua_State *L, const char *msg, int tocont);
 LUAI_FUNC void luaE_warnerror (lua_State *L, const char *where);
-LUAI_FUNC int luaE_resetthread (lua_State *L, int status);
+LUAI_FUNC TStatus luaE_resetthread (lua_State *L, TStatus status);
 
 
 #endif

+ 1 - 1
lstring.c

@@ -329,7 +329,7 @@ TString *luaS_newextlstr (lua_State *L,
     if (!falloc)
       f_pintern(L, &ne);  /* just internalize string */
     else {
-      int status = luaD_rawrunprotected(L, f_pintern, &ne);
+      TStatus status = luaD_rawrunprotected(L, f_pintern, &ne);
       (*falloc)(ud, cast_voidp(s), len + 1, 0);  /* free external string */
       if (status != LUA_OK)  /* memory error? */
         luaM_error(L);  /* re-raise memory error */