diff options
Diffstat (limited to '')
-rw-r--r-- | ldo.c | 66 |
1 files changed, 44 insertions, 22 deletions
@@ -102,24 +102,13 @@ struct lua_longjmp { | |||
102 | 102 | ||
103 | 103 | ||
104 | void luaD_seterrorobj (lua_State *L, TStatus errcode, StkId oldtop) { | 104 | void luaD_seterrorobj (lua_State *L, TStatus errcode, StkId oldtop) { |
105 | switch (errcode) { | 105 | if (errcode == LUA_ERRMEM) { /* memory error? */ |
106 | case LUA_ERRMEM: { /* memory error? */ | 106 | setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */ |
107 | setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */ | 107 | } |
108 | break; | 108 | else { |
109 | } | 109 | lua_assert(errorstatus(errcode)); /* must be a real error */ |
110 | case LUA_ERRERR: { | 110 | lua_assert(!ttisnil(s2v(L->top.p - 1))); /* with a non-nil object */ |
111 | setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling")); | 111 | setobjs2s(L, oldtop, L->top.p - 1); /* move it to 'oldtop' */ |
112 | break; | ||
113 | } | ||
114 | default: { | ||
115 | lua_assert(errorstatus(errcode)); /* must be a real error */ | ||
116 | if (!ttisnil(s2v(L->top.p - 1))) { /* error object is not nil? */ | ||
117 | setobjs2s(L, oldtop, L->top.p - 1); /* move it to 'oldtop' */ | ||
118 | } | ||
119 | else /* change it to a proper message */ | ||
120 | setsvalue2s(L, oldtop, luaS_newliteral(L, "<error object is nil>")); | ||
121 | break; | ||
122 | } | ||
123 | } | 112 | } |
124 | L->top.p = oldtop + 1; /* top goes back to old top plus error object */ | 113 | L->top.p = oldtop + 1; /* top goes back to old top plus error object */ |
125 | } | 114 | } |
@@ -150,6 +139,16 @@ l_noret luaD_throw (lua_State *L, TStatus errcode) { | |||
150 | } | 139 | } |
151 | 140 | ||
152 | 141 | ||
142 | l_noret luaD_throwbaselevel (lua_State *L, TStatus errcode) { | ||
143 | if (L->errorJmp) { | ||
144 | /* unroll error entries up to the first level */ | ||
145 | while (L->errorJmp->previous != NULL) | ||
146 | L->errorJmp = L->errorJmp->previous; | ||
147 | } | ||
148 | luaD_throw(L, errcode); | ||
149 | } | ||
150 | |||
151 | |||
153 | TStatus luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { | 152 | TStatus luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { |
154 | l_uint32 oldnCcalls = L->nCcalls; | 153 | l_uint32 oldnCcalls = L->nCcalls; |
155 | struct lua_longjmp lj; | 154 | struct lua_longjmp lj; |
@@ -175,6 +174,20 @@ TStatus luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { | |||
175 | #define STACKERRSPACE 200 | 174 | #define STACKERRSPACE 200 |
176 | 175 | ||
177 | 176 | ||
177 | /* | ||
178 | ** LUAI_MAXSTACK limits the size of the Lua stack. | ||
179 | ** It must fit into INT_MAX/2. | ||
180 | */ | ||
181 | |||
182 | #if !defined(LUAI_MAXSTACK) | ||
183 | #if 1000000 < (INT_MAX / 2) | ||
184 | #define LUAI_MAXSTACK 1000000 | ||
185 | #else | ||
186 | #define LUAI_MAXSTACK (INT_MAX / 2u) | ||
187 | #endif | ||
188 | #endif | ||
189 | |||
190 | |||
178 | /* maximum stack size that respects size_t */ | 191 | /* maximum stack size that respects size_t */ |
179 | #define MAXSTACK_BYSIZET ((MAX_SIZET / sizeof(StackValue)) - STACKERRSPACE) | 192 | #define MAXSTACK_BYSIZET ((MAX_SIZET / sizeof(StackValue)) - STACKERRSPACE) |
180 | 193 | ||
@@ -190,6 +203,15 @@ TStatus luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { | |||
190 | #define ERRORSTACKSIZE (MAXSTACK + STACKERRSPACE) | 203 | #define ERRORSTACKSIZE (MAXSTACK + STACKERRSPACE) |
191 | 204 | ||
192 | 205 | ||
206 | /* raise an error while running the message handler */ | ||
207 | l_noret luaD_errerr (lua_State *L) { | ||
208 | TString *msg = luaS_newliteral(L, "error in error handling"); | ||
209 | setsvalue2s(L, L->top.p, msg); | ||
210 | L->top.p++; /* assume EXTRA_STACK */ | ||
211 | luaD_throw(L, LUA_ERRERR); | ||
212 | } | ||
213 | |||
214 | |||
193 | /* | 215 | /* |
194 | ** In ISO C, any pointer use after the pointer has been deallocated is | 216 | ** In ISO C, any pointer use after the pointer has been deallocated is |
195 | ** undefined behavior. So, before a stack reallocation, all pointers | 217 | ** undefined behavior. So, before a stack reallocation, all pointers |
@@ -201,7 +223,7 @@ TStatus luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { | |||
201 | ** The following macro chooses how strict is the code. | 223 | ** The following macro chooses how strict is the code. |
202 | */ | 224 | */ |
203 | #if !defined(LUAI_STRICT_ADDRESS) | 225 | #if !defined(LUAI_STRICT_ADDRESS) |
204 | #define LUAI_STRICT_ADDRESS 0 | 226 | #define LUAI_STRICT_ADDRESS 1 |
205 | #endif | 227 | #endif |
206 | 228 | ||
207 | #if LUAI_STRICT_ADDRESS | 229 | #if LUAI_STRICT_ADDRESS |
@@ -317,11 +339,11 @@ int luaD_growstack (lua_State *L, int n, int raiseerror) { | |||
317 | a stack error; cannot grow further than that. */ | 339 | a stack error; cannot grow further than that. */ |
318 | lua_assert(stacksize(L) == ERRORSTACKSIZE); | 340 | lua_assert(stacksize(L) == ERRORSTACKSIZE); |
319 | if (raiseerror) | 341 | if (raiseerror) |
320 | luaD_throw(L, LUA_ERRERR); /* error inside message handler */ | 342 | luaD_errerr(L); /* error inside message handler */ |
321 | return 0; /* if not 'raiseerror', just signal it */ | 343 | return 0; /* if not 'raiseerror', just signal it */ |
322 | } | 344 | } |
323 | else if (n < MAXSTACK) { /* avoids arithmetic overflows */ | 345 | else if (n < MAXSTACK) { /* avoids arithmetic overflows */ |
324 | int newsize = 2 * size; /* tentative new size */ | 346 | int newsize = size + (size >> 1); /* tentative new size (size * 1.5) */ |
325 | int needed = cast_int(L->top.p - L->stack.p) + n; | 347 | int needed = cast_int(L->top.p - L->stack.p) + n; |
326 | if (newsize > MAXSTACK) /* cannot cross the limit */ | 348 | if (newsize > MAXSTACK) /* cannot cross the limit */ |
327 | newsize = MAXSTACK; | 349 | newsize = MAXSTACK; |
@@ -515,7 +537,7 @@ l_sinline void genmoveresults (lua_State *L, StkId res, int nres, | |||
515 | ** to 'res'. Handle most typical cases (zero results for commands, | 537 | ** to 'res'. Handle most typical cases (zero results for commands, |
516 | ** one result for expressions, multiple results for tail calls/single | 538 | ** one result for expressions, multiple results for tail calls/single |
517 | ** parameters) separated. The flag CIST_TBC in 'fwanted', if set, | 539 | ** parameters) separated. The flag CIST_TBC in 'fwanted', if set, |
518 | ** forces the swicth to go to the default case. | 540 | ** forces the switch to go to the default case. |
519 | */ | 541 | */ |
520 | l_sinline void moveresults (lua_State *L, StkId res, int nres, | 542 | l_sinline void moveresults (lua_State *L, StkId res, int nres, |
521 | l_uint32 fwanted) { | 543 | l_uint32 fwanted) { |