aboutsummaryrefslogtreecommitdiff
path: root/ldo.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2025-01-30 11:41:39 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2025-01-30 11:41:39 -0300
commitd1e677c52be3b107a7a29fdc482158f6d9251e79 (patch)
tree6b201f229a9dc1f0752f6544b71498a4d528941d /ldo.c
parentf7439112a5469078ac4f444106242cf1c1d3fe8a (diff)
downloadlua-d1e677c52be3b107a7a29fdc482158f6d9251e79.tar.gz
lua-d1e677c52be3b107a7a29fdc482158f6d9251e79.tar.bz2
lua-d1e677c52be3b107a7a29fdc482158f6d9251e79.zip
New type 'TStatus' for thread status/error codes
Diffstat (limited to 'ldo.c')
-rw-r--r--ldo.c45
1 files changed, 23 insertions, 22 deletions
diff --git a/ldo.c b/ldo.c
index 31c00a21..3f9c8b7d 100644
--- a/ldo.c
+++ b/ldo.c
@@ -97,11 +97,11 @@
97struct lua_longjmp { 97struct lua_longjmp {
98 struct lua_longjmp *previous; 98 struct lua_longjmp *previous;
99 luai_jmpbuf b; 99 luai_jmpbuf b;
100 volatile int status; /* error code */ 100 volatile TStatus status; /* error code */
101}; 101};
102 102
103 103
104void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) { 104void luaD_seterrorobj (lua_State *L, TStatus errcode, StkId oldtop) {
105 switch (errcode) { 105 switch (errcode) {
106 case LUA_ERRMEM: { /* memory error? */ 106 case LUA_ERRMEM: { /* memory error? */
107 setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */ 107 setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */
@@ -125,7 +125,7 @@ void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) {
125} 125}
126 126
127 127
128l_noret luaD_throw (lua_State *L, int errcode) { 128l_noret luaD_throw (lua_State *L, TStatus errcode) {
129 if (L->errorJmp) { /* thread has an error handler? */ 129 if (L->errorJmp) { /* thread has an error handler? */
130 L->errorJmp->status = errcode; /* set status */ 130 L->errorJmp->status = errcode; /* set status */
131 LUAI_THROW(L, L->errorJmp); /* jump to it */ 131 LUAI_THROW(L, L->errorJmp); /* jump to it */
@@ -133,7 +133,7 @@ l_noret luaD_throw (lua_State *L, int errcode) {
133 else { /* thread has no error handler */ 133 else { /* thread has no error handler */
134 global_State *g = G(L); 134 global_State *g = G(L);
135 errcode = luaE_resetthread(L, errcode); /* close all upvalues */ 135 errcode = luaE_resetthread(L, errcode); /* close all upvalues */
136 L->status = cast_byte(errcode); 136 L->status = errcode;
137 if (g->mainthread->errorJmp) { /* main thread has a handler? */ 137 if (g->mainthread->errorJmp) { /* main thread has a handler? */
138 setobjs2s(L, g->mainthread->top.p++, L->top.p - 1); /* copy error obj. */ 138 setobjs2s(L, g->mainthread->top.p++, L->top.p - 1); /* copy error obj. */
139 luaD_throw(g->mainthread, errcode); /* re-throw in main thread */ 139 luaD_throw(g->mainthread, errcode); /* re-throw in main thread */
@@ -149,7 +149,7 @@ l_noret luaD_throw (lua_State *L, int errcode) {
149} 149}
150 150
151 151
152int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { 152TStatus luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
153 l_uint32 oldnCcalls = L->nCcalls; 153 l_uint32 oldnCcalls = L->nCcalls;
154 struct lua_longjmp lj; 154 struct lua_longjmp lj;
155 lj.status = LUA_OK; 155 lj.status = LUA_OK;
@@ -751,8 +751,8 @@ void luaD_callnoyield (lua_State *L, StkId func, int nResults) {
751** particular, field CIST_RECST preserves the error status across these 751** particular, field CIST_RECST preserves the error status across these
752** multiple runs, changing only if there is a new error. 752** multiple runs, changing only if there is a new error.
753*/ 753*/
754static int finishpcallk (lua_State *L, CallInfo *ci) { 754static TStatus finishpcallk (lua_State *L, CallInfo *ci) {
755 int status = getcistrecst(ci); /* get original status */ 755 TStatus status = getcistrecst(ci); /* get original status */
756 if (l_likely(status == LUA_OK)) /* no error? */ 756 if (l_likely(status == LUA_OK)) /* no error? */
757 status = LUA_YIELD; /* was interrupted by an yield */ 757 status = LUA_YIELD; /* was interrupted by an yield */
758 else { /* error */ 758 else { /* error */
@@ -792,14 +792,15 @@ static void finishCcall (lua_State *L, CallInfo *ci) {
792 /* don't need to reset CIST_CLSRET, as it will be set again anyway */ 792 /* don't need to reset CIST_CLSRET, as it will be set again anyway */
793 } 793 }
794 else { 794 else {
795 int status = LUA_YIELD; /* default if there were no errors */ 795 TStatus status = LUA_YIELD; /* default if there were no errors */
796 lua_KFunction kf = ci->u.c.k; /* continuation function */
796 /* must have a continuation and must be able to call it */ 797 /* must have a continuation and must be able to call it */
797 lua_assert(ci->u.c.k != NULL && yieldable(L)); 798 lua_assert(kf != NULL && yieldable(L));
798 if (ci->callstatus & CIST_YPCALL) /* was inside a 'lua_pcallk'? */ 799 if (ci->callstatus & CIST_YPCALL) /* was inside a 'lua_pcallk'? */
799 status = finishpcallk(L, ci); /* finish it */ 800 status = finishpcallk(L, ci); /* finish it */
800 adjustresults(L, LUA_MULTRET); /* finish 'lua_callk' */ 801 adjustresults(L, LUA_MULTRET); /* finish 'lua_callk' */
801 lua_unlock(L); 802 lua_unlock(L);
802 n = (*ci->u.c.k)(L, status, ci->u.c.ctx); /* call continuation */ 803 n = (*kf)(L, APIstatus(status), ci->u.c.ctx); /* call continuation */
803 lua_lock(L); 804 lua_lock(L);
804 api_checknelems(L, n); 805 api_checknelems(L, n);
805 } 806 }
@@ -901,7 +902,7 @@ static void resume (lua_State *L, void *ud) {
901** (status == LUA_YIELD), or an unprotected error ('findpcall' doesn't 902** (status == LUA_YIELD), or an unprotected error ('findpcall' doesn't
902** find a recover point). 903** find a recover point).
903*/ 904*/
904static int precover (lua_State *L, int status) { 905static TStatus precover (lua_State *L, TStatus status) {
905 CallInfo *ci; 906 CallInfo *ci;
906 while (errorstatus(status) && (ci = findpcall(L)) != NULL) { 907 while (errorstatus(status) && (ci = findpcall(L)) != NULL) {
907 L->ci = ci; /* go down to recovery functions */ 908 L->ci = ci; /* go down to recovery functions */
@@ -914,7 +915,7 @@ static int precover (lua_State *L, int status) {
914 915
915LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, 916LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs,
916 int *nresults) { 917 int *nresults) {
917 int status; 918 TStatus status;
918 lua_lock(L); 919 lua_lock(L);
919 if (L->status == LUA_OK) { /* may be starting a coroutine */ 920 if (L->status == LUA_OK) { /* may be starting a coroutine */
920 if (L->ci != &L->base_ci) /* not in base level? */ 921 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,
936 if (l_likely(!errorstatus(status))) 937 if (l_likely(!errorstatus(status)))
937 lua_assert(status == L->status); /* normal end or yield */ 938 lua_assert(status == L->status); /* normal end or yield */
938 else { /* unrecoverable error */ 939 else { /* unrecoverable error */
939 L->status = cast_byte(status); /* mark thread as 'dead' */ 940 L->status = status; /* mark thread as 'dead' */
940 luaD_seterrorobj(L, status, L->top.p); /* push error message */ 941 luaD_seterrorobj(L, status, L->top.p); /* push error message */
941 L->ci->top.p = L->top.p; 942 L->ci->top.p = L->top.p;
942 } 943 }
943 *nresults = (status == LUA_YIELD) ? L->ci->u2.nyield 944 *nresults = (status == LUA_YIELD) ? L->ci->u2.nyield
944 : cast_int(L->top.p - (L->ci->func.p + 1)); 945 : cast_int(L->top.p - (L->ci->func.p + 1));
945 lua_unlock(L); 946 lua_unlock(L);
946 return status; 947 return APIstatus(status);
947} 948}
948 949
949 950
@@ -988,7 +989,7 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx,
988*/ 989*/
989struct CloseP { 990struct CloseP {
990 StkId level; 991 StkId level;
991 int status; 992 TStatus status;
992}; 993};
993 994
994 995
@@ -1005,7 +1006,7 @@ static void closepaux (lua_State *L, void *ud) {
1005** Calls 'luaF_close' in protected mode. Return the original status 1006** Calls 'luaF_close' in protected mode. Return the original status
1006** or, in case of errors, the new status. 1007** or, in case of errors, the new status.
1007*/ 1008*/
1008int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status) { 1009TStatus luaD_closeprotected (lua_State *L, ptrdiff_t level, TStatus status) {
1009 CallInfo *old_ci = L->ci; 1010 CallInfo *old_ci = L->ci;
1010 lu_byte old_allowhooks = L->allowhook; 1011 lu_byte old_allowhooks = L->allowhook;
1011 for (;;) { /* keep closing upvalues until no more errors */ 1012 for (;;) { /* keep closing upvalues until no more errors */
@@ -1027,9 +1028,9 @@ int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status) {
1027** thread information ('allowhook', etc.) and in particular 1028** thread information ('allowhook', etc.) and in particular
1028** its stack level in case of errors. 1029** its stack level in case of errors.
1029*/ 1030*/
1030int luaD_pcall (lua_State *L, Pfunc func, void *u, 1031TStatus luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t old_top,
1031 ptrdiff_t old_top, ptrdiff_t ef) { 1032 ptrdiff_t ef) {
1032 int status; 1033 TStatus status;
1033 CallInfo *old_ci = L->ci; 1034 CallInfo *old_ci = L->ci;
1034 lu_byte old_allowhooks = L->allowhook; 1035 lu_byte old_allowhooks = L->allowhook;
1035 ptrdiff_t old_errfunc = L->errfunc; 1036 ptrdiff_t old_errfunc = L->errfunc;
@@ -1091,10 +1092,10 @@ static void f_parser (lua_State *L, void *ud) {
1091} 1092}
1092 1093
1093 1094
1094int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, 1095TStatus luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
1095 const char *mode) { 1096 const char *mode) {
1096 struct SParser p; 1097 struct SParser p;
1097 int status; 1098 TStatus status;
1098 incnny(L); /* cannot yield during parsing */ 1099 incnny(L); /* cannot yield during parsing */
1099 p.z = z; p.name = name; p.mode = mode; 1100 p.z = z; p.name = name; p.mode = mode;
1100 p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0; 1101 p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0;