diff options
Diffstat (limited to 'src/lua/ldo.c')
| -rw-r--r-- | src/lua/ldo.c | 963 |
1 files changed, 0 insertions, 963 deletions
diff --git a/src/lua/ldo.c b/src/lua/ldo.c deleted file mode 100644 index 7135079..0000000 --- a/src/lua/ldo.c +++ /dev/null | |||
| @@ -1,963 +0,0 @@ | |||
| 1 | /* | ||
| 2 | ** $Id: ldo.c $ | ||
| 3 | ** Stack and Call structure of Lua | ||
| 4 | ** See Copyright Notice in lua.h | ||
| 5 | */ | ||
| 6 | |||
| 7 | #define ldo_c | ||
| 8 | #define LUA_CORE | ||
| 9 | |||
| 10 | #include "lprefix.h" | ||
| 11 | |||
| 12 | |||
| 13 | #include <setjmp.h> | ||
| 14 | #include <stdlib.h> | ||
| 15 | #include <string.h> | ||
| 16 | |||
| 17 | #include "lua.h" | ||
| 18 | |||
| 19 | #include "lapi.h" | ||
| 20 | #include "ldebug.h" | ||
| 21 | #include "ldo.h" | ||
| 22 | #include "lfunc.h" | ||
| 23 | #include "lgc.h" | ||
| 24 | #include "lmem.h" | ||
| 25 | #include "lobject.h" | ||
| 26 | #include "lopcodes.h" | ||
| 27 | #include "lparser.h" | ||
| 28 | #include "lstate.h" | ||
| 29 | #include "lstring.h" | ||
| 30 | #include "ltable.h" | ||
| 31 | #include "ltm.h" | ||
| 32 | #include "lundump.h" | ||
| 33 | #include "lvm.h" | ||
| 34 | #include "lzio.h" | ||
| 35 | |||
| 36 | |||
| 37 | |||
| 38 | #define errorstatus(s) ((s) > LUA_YIELD) | ||
| 39 | |||
| 40 | |||
| 41 | /* | ||
| 42 | ** {====================================================== | ||
| 43 | ** Error-recovery functions | ||
| 44 | ** ======================================================= | ||
| 45 | */ | ||
| 46 | |||
| 47 | /* | ||
| 48 | ** LUAI_THROW/LUAI_TRY define how Lua does exception handling. By | ||
| 49 | ** default, Lua handles errors with exceptions when compiling as | ||
| 50 | ** C++ code, with _longjmp/_setjmp when asked to use them, and with | ||
| 51 | ** longjmp/setjmp otherwise. | ||
| 52 | */ | ||
| 53 | #if !defined(LUAI_THROW) /* { */ | ||
| 54 | |||
| 55 | #if defined(__cplusplus) && !defined(LUA_USE_LONGJMP) /* { */ | ||
| 56 | |||
| 57 | /* C++ exceptions */ | ||
| 58 | #define LUAI_THROW(L,c) throw(c) | ||
| 59 | #define LUAI_TRY(L,c,a) \ | ||
| 60 | try { a } catch(...) { if ((c)->status == 0) (c)->status = -1; } | ||
| 61 | #define luai_jmpbuf int /* dummy variable */ | ||
| 62 | |||
| 63 | #elif defined(LUA_USE_POSIX) /* }{ */ | ||
| 64 | |||
| 65 | /* in POSIX, try _longjmp/_setjmp (more efficient) */ | ||
| 66 | #define LUAI_THROW(L,c) _longjmp((c)->b, 1) | ||
| 67 | #define LUAI_TRY(L,c,a) if (_setjmp((c)->b) == 0) { a } | ||
| 68 | #define luai_jmpbuf jmp_buf | ||
| 69 | |||
| 70 | #else /* }{ */ | ||
| 71 | |||
| 72 | /* ISO C handling with long jumps */ | ||
| 73 | #define LUAI_THROW(L,c) longjmp((c)->b, 1) | ||
| 74 | #define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a } | ||
| 75 | #define luai_jmpbuf jmp_buf | ||
| 76 | |||
| 77 | #endif /* } */ | ||
| 78 | |||
| 79 | #endif /* } */ | ||
| 80 | |||
| 81 | |||
| 82 | |||
| 83 | /* chain list of long jump buffers */ | ||
| 84 | struct lua_longjmp { | ||
| 85 | struct lua_longjmp *previous; | ||
| 86 | luai_jmpbuf b; | ||
| 87 | volatile int status; /* error code */ | ||
| 88 | }; | ||
| 89 | |||
| 90 | |||
| 91 | void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) { | ||
| 92 | switch (errcode) { | ||
| 93 | case LUA_ERRMEM: { /* memory error? */ | ||
| 94 | setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */ | ||
| 95 | break; | ||
| 96 | } | ||
| 97 | case LUA_ERRERR: { | ||
| 98 | setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling")); | ||
| 99 | break; | ||
| 100 | } | ||
| 101 | case LUA_OK: { /* special case only for closing upvalues */ | ||
| 102 | setnilvalue(s2v(oldtop)); /* no error message */ | ||
| 103 | break; | ||
| 104 | } | ||
| 105 | default: { | ||
| 106 | lua_assert(errorstatus(errcode)); /* real error */ | ||
| 107 | setobjs2s(L, oldtop, L->top - 1); /* error message on current top */ | ||
| 108 | break; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | L->top = oldtop + 1; | ||
| 112 | } | ||
| 113 | |||
| 114 | |||
| 115 | l_noret luaD_throw (lua_State *L, int errcode) { | ||
| 116 | if (L->errorJmp) { /* thread has an error handler? */ | ||
| 117 | L->errorJmp->status = errcode; /* set status */ | ||
| 118 | LUAI_THROW(L, L->errorJmp); /* jump to it */ | ||
| 119 | } | ||
| 120 | else { /* thread has no error handler */ | ||
| 121 | global_State *g = G(L); | ||
| 122 | errcode = luaE_resetthread(L, errcode); /* close all upvalues */ | ||
| 123 | if (g->mainthread->errorJmp) { /* main thread has a handler? */ | ||
| 124 | setobjs2s(L, g->mainthread->top++, L->top - 1); /* copy error obj. */ | ||
| 125 | luaD_throw(g->mainthread, errcode); /* re-throw in main thread */ | ||
| 126 | } | ||
| 127 | else { /* no handler at all; abort */ | ||
| 128 | if (g->panic) { /* panic function? */ | ||
| 129 | lua_unlock(L); | ||
| 130 | g->panic(L); /* call panic function (last chance to jump out) */ | ||
| 131 | } | ||
| 132 | abort(); | ||
| 133 | } | ||
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 | |||
| 138 | int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { | ||
| 139 | l_uint32 oldnCcalls = L->nCcalls; | ||
| 140 | struct lua_longjmp lj; | ||
| 141 | lj.status = LUA_OK; | ||
| 142 | lj.previous = L->errorJmp; /* chain new error handler */ | ||
| 143 | L->errorJmp = &lj; | ||
| 144 | LUAI_TRY(L, &lj, | ||
| 145 | (*f)(L, ud); | ||
| 146 | ); | ||
| 147 | L->errorJmp = lj.previous; /* restore old error handler */ | ||
| 148 | L->nCcalls = oldnCcalls; | ||
| 149 | return lj.status; | ||
| 150 | } | ||
| 151 | |||
| 152 | /* }====================================================== */ | ||
| 153 | |||
| 154 | |||
| 155 | /* | ||
| 156 | ** {================================================================== | ||
| 157 | ** Stack reallocation | ||
| 158 | ** =================================================================== | ||
| 159 | */ | ||
| 160 | static void correctstack (lua_State *L, StkId oldstack, StkId newstack) { | ||
| 161 | CallInfo *ci; | ||
| 162 | UpVal *up; | ||
| 163 | L->top = (L->top - oldstack) + newstack; | ||
| 164 | L->tbclist = (L->tbclist - oldstack) + newstack; | ||
| 165 | for (up = L->openupval; up != NULL; up = up->u.open.next) | ||
| 166 | up->v = s2v((uplevel(up) - oldstack) + newstack); | ||
| 167 | for (ci = L->ci; ci != NULL; ci = ci->previous) { | ||
| 168 | ci->top = (ci->top - oldstack) + newstack; | ||
| 169 | ci->func = (ci->func - oldstack) + newstack; | ||
| 170 | if (isLua(ci)) | ||
| 171 | ci->u.l.trap = 1; /* signal to update 'trap' in 'luaV_execute' */ | ||
| 172 | } | ||
| 173 | } | ||
| 174 | |||
| 175 | |||
| 176 | /* some space for error handling */ | ||
| 177 | #define ERRORSTACKSIZE (LUAI_MAXSTACK + 200) | ||
| 178 | |||
| 179 | |||
| 180 | /* | ||
| 181 | ** Reallocate the stack to a new size, correcting all pointers into | ||
| 182 | ** it. (There are pointers to a stack from its upvalues, from its list | ||
| 183 | ** of call infos, plus a few individual pointers.) The reallocation is | ||
| 184 | ** done in two steps (allocation + free) because the correction must be | ||
| 185 | ** done while both addresses (the old stack and the new one) are valid. | ||
| 186 | ** (In ISO C, any pointer use after the pointer has been deallocated is | ||
| 187 | ** undefined behavior.) | ||
| 188 | ** In case of allocation error, raise an error or return false according | ||
| 189 | ** to 'raiseerror'. | ||
| 190 | */ | ||
| 191 | int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { | ||
| 192 | int oldsize = stacksize(L); | ||
| 193 | int i; | ||
| 194 | StkId newstack = luaM_reallocvector(L, NULL, 0, | ||
| 195 | newsize + EXTRA_STACK, StackValue); | ||
| 196 | lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE); | ||
| 197 | if (l_unlikely(newstack == NULL)) { /* reallocation failed? */ | ||
| 198 | if (raiseerror) | ||
| 199 | luaM_error(L); | ||
| 200 | else return 0; /* do not raise an error */ | ||
| 201 | } | ||
| 202 | /* number of elements to be copied to the new stack */ | ||
| 203 | i = ((oldsize <= newsize) ? oldsize : newsize) + EXTRA_STACK; | ||
| 204 | memcpy(newstack, L->stack, i * sizeof(StackValue)); | ||
| 205 | for (; i < newsize + EXTRA_STACK; i++) | ||
| 206 | setnilvalue(s2v(newstack + i)); /* erase new segment */ | ||
| 207 | correctstack(L, L->stack, newstack); | ||
| 208 | luaM_freearray(L, L->stack, oldsize + EXTRA_STACK); | ||
| 209 | L->stack = newstack; | ||
| 210 | L->stack_last = L->stack + newsize; | ||
| 211 | return 1; | ||
| 212 | } | ||
| 213 | |||
| 214 | |||
| 215 | /* | ||
| 216 | ** Try to grow the stack by at least 'n' elements. when 'raiseerror' | ||
| 217 | ** is true, raises any error; otherwise, return 0 in case of errors. | ||
| 218 | */ | ||
| 219 | int luaD_growstack (lua_State *L, int n, int raiseerror) { | ||
| 220 | int size = stacksize(L); | ||
| 221 | if (l_unlikely(size > LUAI_MAXSTACK)) { | ||
| 222 | /* if stack is larger than maximum, thread is already using the | ||
| 223 | extra space reserved for errors, that is, thread is handling | ||
| 224 | a stack error; cannot grow further than that. */ | ||
| 225 | lua_assert(stacksize(L) == ERRORSTACKSIZE); | ||
| 226 | if (raiseerror) | ||
| 227 | luaD_throw(L, LUA_ERRERR); /* error inside message handler */ | ||
| 228 | return 0; /* if not 'raiseerror', just signal it */ | ||
| 229 | } | ||
| 230 | else { | ||
| 231 | int newsize = 2 * size; /* tentative new size */ | ||
| 232 | int needed = cast_int(L->top - L->stack) + n; | ||
| 233 | if (newsize > LUAI_MAXSTACK) /* cannot cross the limit */ | ||
| 234 | newsize = LUAI_MAXSTACK; | ||
| 235 | if (newsize < needed) /* but must respect what was asked for */ | ||
| 236 | newsize = needed; | ||
| 237 | if (l_likely(newsize <= LUAI_MAXSTACK)) | ||
| 238 | return luaD_reallocstack(L, newsize, raiseerror); | ||
| 239 | else { /* stack overflow */ | ||
| 240 | /* add extra size to be able to handle the error message */ | ||
| 241 | luaD_reallocstack(L, ERRORSTACKSIZE, raiseerror); | ||
| 242 | if (raiseerror) | ||
| 243 | luaG_runerror(L, "stack overflow"); | ||
| 244 | return 0; | ||
| 245 | } | ||
| 246 | } | ||
| 247 | } | ||
| 248 | |||
| 249 | |||
| 250 | static int stackinuse (lua_State *L) { | ||
| 251 | CallInfo *ci; | ||
| 252 | int res; | ||
| 253 | StkId lim = L->top; | ||
| 254 | for (ci = L->ci; ci != NULL; ci = ci->previous) { | ||
| 255 | if (lim < ci->top) lim = ci->top; | ||
| 256 | } | ||
| 257 | lua_assert(lim <= L->stack_last); | ||
| 258 | res = cast_int(lim - L->stack) + 1; /* part of stack in use */ | ||
| 259 | if (res < LUA_MINSTACK) | ||
| 260 | res = LUA_MINSTACK; /* ensure a minimum size */ | ||
| 261 | return res; | ||
| 262 | } | ||
| 263 | |||
| 264 | |||
| 265 | /* | ||
| 266 | ** If stack size is more than 3 times the current use, reduce that size | ||
| 267 | ** to twice the current use. (So, the final stack size is at most 2/3 the | ||
| 268 | ** previous size, and half of its entries are empty.) | ||
| 269 | ** As a particular case, if stack was handling a stack overflow and now | ||
| 270 | ** it is not, 'max' (limited by LUAI_MAXSTACK) will be smaller than | ||
| 271 | ** stacksize (equal to ERRORSTACKSIZE in this case), and so the stack | ||
| 272 | ** will be reduced to a "regular" size. | ||
| 273 | */ | ||
| 274 | void luaD_shrinkstack (lua_State *L) { | ||
| 275 | int inuse = stackinuse(L); | ||
| 276 | int nsize = inuse * 2; /* proposed new size */ | ||
| 277 | int max = inuse * 3; /* maximum "reasonable" size */ | ||
| 278 | if (max > LUAI_MAXSTACK) { | ||
| 279 | max = LUAI_MAXSTACK; /* respect stack limit */ | ||
| 280 | if (nsize > LUAI_MAXSTACK) | ||
| 281 | nsize = LUAI_MAXSTACK; | ||
| 282 | } | ||
| 283 | /* if thread is currently not handling a stack overflow and its | ||
| 284 | size is larger than maximum "reasonable" size, shrink it */ | ||
| 285 | if (inuse <= LUAI_MAXSTACK && stacksize(L) > max) | ||
| 286 | luaD_reallocstack(L, nsize, 0); /* ok if that fails */ | ||
| 287 | else /* don't change stack */ | ||
| 288 | condmovestack(L,{},{}); /* (change only for debugging) */ | ||
| 289 | luaE_shrinkCI(L); /* shrink CI list */ | ||
| 290 | } | ||
| 291 | |||
| 292 | |||
| 293 | void luaD_inctop (lua_State *L) { | ||
| 294 | luaD_checkstack(L, 1); | ||
| 295 | L->top++; | ||
| 296 | } | ||
| 297 | |||
| 298 | /* }================================================================== */ | ||
| 299 | |||
| 300 | |||
| 301 | /* | ||
| 302 | ** Call a hook for the given event. Make sure there is a hook to be | ||
| 303 | ** called. (Both 'L->hook' and 'L->hookmask', which trigger this | ||
| 304 | ** function, can be changed asynchronously by signals.) | ||
| 305 | */ | ||
| 306 | void luaD_hook (lua_State *L, int event, int line, | ||
| 307 | int ftransfer, int ntransfer) { | ||
| 308 | lua_Hook hook = L->hook; | ||
| 309 | if (hook && L->allowhook) { /* make sure there is a hook */ | ||
| 310 | int mask = CIST_HOOKED; | ||
| 311 | CallInfo *ci = L->ci; | ||
| 312 | ptrdiff_t top = savestack(L, L->top); /* preserve original 'top' */ | ||
| 313 | ptrdiff_t ci_top = savestack(L, ci->top); /* idem for 'ci->top' */ | ||
| 314 | lua_Debug ar; | ||
| 315 | ar.event = event; | ||
| 316 | ar.currentline = line; | ||
| 317 | ar.i_ci = ci; | ||
| 318 | if (ntransfer != 0) { | ||
| 319 | mask |= CIST_TRAN; /* 'ci' has transfer information */ | ||
| 320 | ci->u2.transferinfo.ftransfer = ftransfer; | ||
| 321 | ci->u2.transferinfo.ntransfer = ntransfer; | ||
| 322 | } | ||
| 323 | if (isLua(ci) && L->top < ci->top) | ||
| 324 | L->top = ci->top; /* protect entire activation register */ | ||
| 325 | luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ | ||
| 326 | if (ci->top < L->top + LUA_MINSTACK) | ||
| 327 | ci->top = L->top + LUA_MINSTACK; | ||
| 328 | L->allowhook = 0; /* cannot call hooks inside a hook */ | ||
| 329 | ci->callstatus |= mask; | ||
| 330 | lua_unlock(L); | ||
| 331 | (*hook)(L, &ar); | ||
| 332 | lua_lock(L); | ||
| 333 | lua_assert(!L->allowhook); | ||
| 334 | L->allowhook = 1; | ||
| 335 | ci->top = restorestack(L, ci_top); | ||
| 336 | L->top = restorestack(L, top); | ||
| 337 | ci->callstatus &= ~mask; | ||
| 338 | } | ||
| 339 | } | ||
| 340 | |||
| 341 | |||
| 342 | /* | ||
| 343 | ** Executes a call hook for Lua functions. This function is called | ||
| 344 | ** whenever 'hookmask' is not zero, so it checks whether call hooks are | ||
| 345 | ** active. | ||
| 346 | */ | ||
| 347 | void luaD_hookcall (lua_State *L, CallInfo *ci) { | ||
| 348 | L->oldpc = 0; /* set 'oldpc' for new function */ | ||
| 349 | if (L->hookmask & LUA_MASKCALL) { /* is call hook on? */ | ||
| 350 | int event = (ci->callstatus & CIST_TAIL) ? LUA_HOOKTAILCALL | ||
| 351 | : LUA_HOOKCALL; | ||
| 352 | Proto *p = ci_func(ci)->p; | ||
| 353 | ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ | ||
| 354 | luaD_hook(L, event, -1, 1, p->numparams); | ||
| 355 | ci->u.l.savedpc--; /* correct 'pc' */ | ||
| 356 | } | ||
| 357 | } | ||
| 358 | |||
| 359 | |||
| 360 | /* | ||
| 361 | ** Executes a return hook for Lua and C functions and sets/corrects | ||
| 362 | ** 'oldpc'. (Note that this correction is needed by the line hook, so it | ||
| 363 | ** is done even when return hooks are off.) | ||
| 364 | */ | ||
| 365 | static void rethook (lua_State *L, CallInfo *ci, int nres) { | ||
| 366 | if (L->hookmask & LUA_MASKRET) { /* is return hook on? */ | ||
| 367 | StkId firstres = L->top - nres; /* index of first result */ | ||
| 368 | int delta = 0; /* correction for vararg functions */ | ||
| 369 | int ftransfer; | ||
| 370 | if (isLua(ci)) { | ||
| 371 | Proto *p = ci_func(ci)->p; | ||
| 372 | if (p->is_vararg) | ||
| 373 | delta = ci->u.l.nextraargs + p->numparams + 1; | ||
| 374 | } | ||
| 375 | ci->func += delta; /* if vararg, back to virtual 'func' */ | ||
| 376 | ftransfer = cast(unsigned short, firstres - ci->func); | ||
| 377 | luaD_hook(L, LUA_HOOKRET, -1, ftransfer, nres); /* call it */ | ||
| 378 | ci->func -= delta; | ||
| 379 | } | ||
| 380 | if (isLua(ci = ci->previous)) | ||
| 381 | L->oldpc = pcRel(ci->u.l.savedpc, ci_func(ci)->p); /* set 'oldpc' */ | ||
| 382 | } | ||
| 383 | |||
| 384 | |||
| 385 | /* | ||
| 386 | ** Check whether 'func' has a '__call' metafield. If so, put it in the | ||
| 387 | ** stack, below original 'func', so that 'luaD_precall' can call it. Raise | ||
| 388 | ** an error if there is no '__call' metafield. | ||
| 389 | */ | ||
| 390 | void luaD_tryfuncTM (lua_State *L, StkId func) { | ||
| 391 | const TValue *tm = luaT_gettmbyobj(L, s2v(func), TM_CALL); | ||
| 392 | StkId p; | ||
| 393 | if (l_unlikely(ttisnil(tm))) | ||
| 394 | luaG_callerror(L, s2v(func)); /* nothing to call */ | ||
| 395 | for (p = L->top; p > func; p--) /* open space for metamethod */ | ||
| 396 | setobjs2s(L, p, p-1); | ||
| 397 | L->top++; /* stack space pre-allocated by the caller */ | ||
| 398 | setobj2s(L, func, tm); /* metamethod is the new function to be called */ | ||
| 399 | } | ||
| 400 | |||
| 401 | |||
| 402 | /* | ||
| 403 | ** Given 'nres' results at 'firstResult', move 'wanted' of them to 'res'. | ||
| 404 | ** Handle most typical cases (zero results for commands, one result for | ||
| 405 | ** expressions, multiple results for tail calls/single parameters) | ||
| 406 | ** separated. | ||
| 407 | */ | ||
| 408 | static void moveresults (lua_State *L, StkId res, int nres, int wanted) { | ||
| 409 | StkId firstresult; | ||
| 410 | int i; | ||
| 411 | switch (wanted) { /* handle typical cases separately */ | ||
| 412 | case 0: /* no values needed */ | ||
| 413 | L->top = res; | ||
| 414 | return; | ||
| 415 | case 1: /* one value needed */ | ||
| 416 | if (nres == 0) /* no results? */ | ||
| 417 | setnilvalue(s2v(res)); /* adjust with nil */ | ||
| 418 | else /* at least one result */ | ||
| 419 | setobjs2s(L, res, L->top - nres); /* move it to proper place */ | ||
| 420 | L->top = res + 1; | ||
| 421 | return; | ||
| 422 | case LUA_MULTRET: | ||
| 423 | wanted = nres; /* we want all results */ | ||
| 424 | break; | ||
| 425 | default: /* two/more results and/or to-be-closed variables */ | ||
| 426 | if (hastocloseCfunc(wanted)) { /* to-be-closed variables? */ | ||
| 427 | ptrdiff_t savedres = savestack(L, res); | ||
| 428 | L->ci->callstatus |= CIST_CLSRET; /* in case of yields */ | ||
| 429 | L->ci->u2.nres = nres; | ||
| 430 | luaF_close(L, res, CLOSEKTOP, 1); | ||
| 431 | L->ci->callstatus &= ~CIST_CLSRET; | ||
| 432 | if (L->hookmask) /* if needed, call hook after '__close's */ | ||
| 433 | rethook(L, L->ci, nres); | ||
| 434 | res = restorestack(L, savedres); /* close and hook can move stack */ | ||
| 435 | wanted = decodeNresults(wanted); | ||
| 436 | if (wanted == LUA_MULTRET) | ||
| 437 | wanted = nres; /* we want all results */ | ||
| 438 | } | ||
| 439 | break; | ||
| 440 | } | ||
| 441 | /* generic case */ | ||
| 442 | firstresult = L->top - nres; /* index of first result */ | ||
| 443 | if (nres > wanted) /* extra results? */ | ||
| 444 | nres = wanted; /* don't need them */ | ||
| 445 | for (i = 0; i < nres; i++) /* move all results to correct place */ | ||
| 446 | setobjs2s(L, res + i, firstresult + i); | ||
| 447 | for (; i < wanted; i++) /* complete wanted number of results */ | ||
| 448 | setnilvalue(s2v(res + i)); | ||
| 449 | L->top = res + wanted; /* top points after the last result */ | ||
| 450 | } | ||
| 451 | |||
| 452 | |||
| 453 | /* | ||
| 454 | ** Finishes a function call: calls hook if necessary, moves current | ||
| 455 | ** number of results to proper place, and returns to previous call | ||
| 456 | ** info. If function has to close variables, hook must be called after | ||
| 457 | ** that. | ||
| 458 | */ | ||
| 459 | void luaD_poscall (lua_State *L, CallInfo *ci, int nres) { | ||
| 460 | int wanted = ci->nresults; | ||
| 461 | if (l_unlikely(L->hookmask && !hastocloseCfunc(wanted))) | ||
| 462 | rethook(L, ci, nres); | ||
| 463 | /* move results to proper place */ | ||
| 464 | moveresults(L, ci->func, nres, wanted); | ||
| 465 | /* function cannot be in any of these cases when returning */ | ||
| 466 | lua_assert(!(ci->callstatus & | ||
| 467 | (CIST_HOOKED | CIST_YPCALL | CIST_FIN | CIST_TRAN | CIST_CLSRET))); | ||
| 468 | L->ci = ci->previous; /* back to caller (after closing variables) */ | ||
| 469 | } | ||
| 470 | |||
| 471 | |||
| 472 | |||
| 473 | #define next_ci(L) (L->ci->next ? L->ci->next : luaE_extendCI(L)) | ||
| 474 | |||
| 475 | |||
| 476 | /* | ||
| 477 | ** Prepare a function for a tail call, building its call info on top | ||
| 478 | ** of the current call info. 'narg1' is the number of arguments plus 1 | ||
| 479 | ** (so that it includes the function itself). | ||
| 480 | */ | ||
| 481 | void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) { | ||
| 482 | Proto *p = clLvalue(s2v(func))->p; | ||
| 483 | int fsize = p->maxstacksize; /* frame size */ | ||
| 484 | int nfixparams = p->numparams; | ||
| 485 | int i; | ||
| 486 | for (i = 0; i < narg1; i++) /* move down function and arguments */ | ||
| 487 | setobjs2s(L, ci->func + i, func + i); | ||
| 488 | checkstackGC(L, fsize); | ||
| 489 | func = ci->func; /* moved-down function */ | ||
| 490 | for (; narg1 <= nfixparams; narg1++) | ||
| 491 | setnilvalue(s2v(func + narg1)); /* complete missing arguments */ | ||
| 492 | ci->top = func + 1 + fsize; /* top for new function */ | ||
| 493 | lua_assert(ci->top <= L->stack_last); | ||
| 494 | ci->u.l.savedpc = p->code; /* starting point */ | ||
| 495 | ci->callstatus |= CIST_TAIL; | ||
| 496 | L->top = func + narg1; /* set top */ | ||
| 497 | } | ||
| 498 | |||
| 499 | |||
| 500 | /* | ||
| 501 | ** Prepares the call to a function (C or Lua). For C functions, also do | ||
| 502 | ** the call. The function to be called is at '*func'. The arguments | ||
| 503 | ** are on the stack, right after the function. Returns the CallInfo | ||
| 504 | ** to be executed, if it was a Lua function. Otherwise (a C function) | ||
| 505 | ** returns NULL, with all the results on the stack, starting at the | ||
| 506 | ** original function position. | ||
| 507 | */ | ||
| 508 | CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { | ||
| 509 | lua_CFunction f; | ||
| 510 | retry: | ||
| 511 | switch (ttypetag(s2v(func))) { | ||
| 512 | case LUA_VCCL: /* C closure */ | ||
| 513 | f = clCvalue(s2v(func))->f; | ||
| 514 | goto Cfunc; | ||
| 515 | case LUA_VLCF: /* light C function */ | ||
| 516 | f = fvalue(s2v(func)); | ||
| 517 | Cfunc: { | ||
| 518 | int n; /* number of returns */ | ||
| 519 | CallInfo *ci; | ||
| 520 | checkstackGCp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ | ||
| 521 | L->ci = ci = next_ci(L); | ||
| 522 | ci->nresults = nresults; | ||
| 523 | ci->callstatus = CIST_C; | ||
| 524 | ci->top = L->top + LUA_MINSTACK; | ||
| 525 | ci->func = func; | ||
| 526 | lua_assert(ci->top <= L->stack_last); | ||
| 527 | if (l_unlikely(L->hookmask & LUA_MASKCALL)) { | ||
| 528 | int narg = cast_int(L->top - func) - 1; | ||
| 529 | luaD_hook(L, LUA_HOOKCALL, -1, 1, narg); | ||
| 530 | } | ||
| 531 | lua_unlock(L); | ||
| 532 | n = (*f)(L); /* do the actual call */ | ||
| 533 | lua_lock(L); | ||
| 534 | api_checknelems(L, n); | ||
| 535 | luaD_poscall(L, ci, n); | ||
| 536 | return NULL; | ||
| 537 | } | ||
| 538 | case LUA_VLCL: { /* Lua function */ | ||
| 539 | CallInfo *ci; | ||
| 540 | Proto *p = clLvalue(s2v(func))->p; | ||
| 541 | int narg = cast_int(L->top - func) - 1; /* number of real arguments */ | ||
| 542 | int nfixparams = p->numparams; | ||
| 543 | int fsize = p->maxstacksize; /* frame size */ | ||
| 544 | checkstackGCp(L, fsize, func); | ||
| 545 | L->ci = ci = next_ci(L); | ||
| 546 | ci->nresults = nresults; | ||
| 547 | ci->u.l.savedpc = p->code; /* starting point */ | ||
| 548 | ci->top = func + 1 + fsize; | ||
| 549 | ci->func = func; | ||
| 550 | L->ci = ci; | ||
| 551 | for (; narg < nfixparams; narg++) | ||
| 552 | setnilvalue(s2v(L->top++)); /* complete missing arguments */ | ||
| 553 | lua_assert(ci->top <= L->stack_last); | ||
| 554 | return ci; | ||
| 555 | } | ||
| 556 | default: { /* not a function */ | ||
| 557 | checkstackGCp(L, 1, func); /* space for metamethod */ | ||
| 558 | luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */ | ||
| 559 | goto retry; /* try again with metamethod */ | ||
| 560 | } | ||
| 561 | } | ||
| 562 | } | ||
| 563 | |||
| 564 | |||
| 565 | /* | ||
| 566 | ** Call a function (C or Lua) through C. 'inc' can be 1 (increment | ||
| 567 | ** number of recursive invocations in the C stack) or nyci (the same | ||
| 568 | ** plus increment number of non-yieldable calls). | ||
| 569 | */ | ||
| 570 | static void ccall (lua_State *L, StkId func, int nResults, int inc) { | ||
| 571 | CallInfo *ci; | ||
| 572 | L->nCcalls += inc; | ||
| 573 | if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) | ||
| 574 | luaE_checkcstack(L); | ||
| 575 | if ((ci = luaD_precall(L, func, nResults)) != NULL) { /* Lua function? */ | ||
| 576 | ci->callstatus = CIST_FRESH; /* mark that it is a "fresh" execute */ | ||
| 577 | luaV_execute(L, ci); /* call it */ | ||
| 578 | } | ||
| 579 | L->nCcalls -= inc; | ||
| 580 | } | ||
| 581 | |||
| 582 | |||
| 583 | /* | ||
| 584 | ** External interface for 'ccall' | ||
| 585 | */ | ||
| 586 | void luaD_call (lua_State *L, StkId func, int nResults) { | ||
| 587 | ccall(L, func, nResults, 1); | ||
| 588 | } | ||
| 589 | |||
| 590 | |||
| 591 | /* | ||
| 592 | ** Similar to 'luaD_call', but does not allow yields during the call. | ||
| 593 | */ | ||
| 594 | void luaD_callnoyield (lua_State *L, StkId func, int nResults) { | ||
| 595 | ccall(L, func, nResults, nyci); | ||
| 596 | } | ||
| 597 | |||
| 598 | |||
| 599 | /* | ||
| 600 | ** Finish the job of 'lua_pcallk' after it was interrupted by an yield. | ||
| 601 | ** (The caller, 'finishCcall', does the final call to 'adjustresults'.) | ||
| 602 | ** The main job is to complete the 'luaD_pcall' called by 'lua_pcallk'. | ||
| 603 | ** If a '__close' method yields here, eventually control will be back | ||
| 604 | ** to 'finishCcall' (when that '__close' method finally returns) and | ||
| 605 | ** 'finishpcallk' will run again and close any still pending '__close' | ||
| 606 | ** methods. Similarly, if a '__close' method errs, 'precover' calls | ||
| 607 | ** 'unroll' which calls ''finishCcall' and we are back here again, to | ||
| 608 | ** close any pending '__close' methods. | ||
| 609 | ** Note that, up to the call to 'luaF_close', the corresponding | ||
| 610 | ** 'CallInfo' is not modified, so that this repeated run works like the | ||
| 611 | ** first one (except that it has at least one less '__close' to do). In | ||
| 612 | ** particular, field CIST_RECST preserves the error status across these | ||
| 613 | ** multiple runs, changing only if there is a new error. | ||
| 614 | */ | ||
| 615 | static int finishpcallk (lua_State *L, CallInfo *ci) { | ||
| 616 | int status = getcistrecst(ci); /* get original status */ | ||
| 617 | if (l_likely(status == LUA_OK)) /* no error? */ | ||
| 618 | status = LUA_YIELD; /* was interrupted by an yield */ | ||
| 619 | else { /* error */ | ||
| 620 | StkId func = restorestack(L, ci->u2.funcidx); | ||
| 621 | L->allowhook = getoah(ci->callstatus); /* restore 'allowhook' */ | ||
| 622 | luaF_close(L, func, status, 1); /* can yield or raise an error */ | ||
| 623 | func = restorestack(L, ci->u2.funcidx); /* stack may be moved */ | ||
| 624 | luaD_seterrorobj(L, status, func); | ||
| 625 | luaD_shrinkstack(L); /* restore stack size in case of overflow */ | ||
| 626 | setcistrecst(ci, LUA_OK); /* clear original status */ | ||
| 627 | } | ||
| 628 | ci->callstatus &= ~CIST_YPCALL; | ||
| 629 | L->errfunc = ci->u.c.old_errfunc; | ||
| 630 | /* if it is here, there were errors or yields; unlike 'lua_pcallk', | ||
| 631 | do not change status */ | ||
| 632 | return status; | ||
| 633 | } | ||
| 634 | |||
| 635 | |||
| 636 | /* | ||
| 637 | ** Completes the execution of a C function interrupted by an yield. | ||
| 638 | ** The interruption must have happened while the function was either | ||
| 639 | ** closing its tbc variables in 'moveresults' or executing | ||
| 640 | ** 'lua_callk'/'lua_pcallk'. In the first case, it just redoes | ||
| 641 | ** 'luaD_poscall'. In the second case, the call to 'finishpcallk' | ||
| 642 | ** finishes the interrupted execution of 'lua_pcallk'. After that, it | ||
| 643 | ** calls the continuation of the interrupted function and finally it | ||
| 644 | ** completes the job of the 'luaD_call' that called the function. In | ||
| 645 | ** the call to 'adjustresults', we do not know the number of results | ||
| 646 | ** of the function called by 'lua_callk'/'lua_pcallk', so we are | ||
| 647 | ** conservative and use LUA_MULTRET (always adjust). | ||
| 648 | */ | ||
| 649 | static void finishCcall (lua_State *L, CallInfo *ci) { | ||
| 650 | int n; /* actual number of results from C function */ | ||
| 651 | if (ci->callstatus & CIST_CLSRET) { /* was returning? */ | ||
| 652 | lua_assert(hastocloseCfunc(ci->nresults)); | ||
| 653 | n = ci->u2.nres; /* just redo 'luaD_poscall' */ | ||
| 654 | /* don't need to reset CIST_CLSRET, as it will be set again anyway */ | ||
| 655 | } | ||
| 656 | else { | ||
| 657 | int status = LUA_YIELD; /* default if there were no errors */ | ||
| 658 | /* must have a continuation and must be able to call it */ | ||
| 659 | lua_assert(ci->u.c.k != NULL && yieldable(L)); | ||
| 660 | if (ci->callstatus & CIST_YPCALL) /* was inside a 'lua_pcallk'? */ | ||
| 661 | status = finishpcallk(L, ci); /* finish it */ | ||
| 662 | adjustresults(L, LUA_MULTRET); /* finish 'lua_callk' */ | ||
| 663 | lua_unlock(L); | ||
| 664 | n = (*ci->u.c.k)(L, status, ci->u.c.ctx); /* call continuation */ | ||
| 665 | lua_lock(L); | ||
| 666 | api_checknelems(L, n); | ||
| 667 | } | ||
| 668 | luaD_poscall(L, ci, n); /* finish 'luaD_call' */ | ||
| 669 | } | ||
| 670 | |||
| 671 | |||
| 672 | /* | ||
| 673 | ** Executes "full continuation" (everything in the stack) of a | ||
| 674 | ** previously interrupted coroutine until the stack is empty (or another | ||
| 675 | ** interruption long-jumps out of the loop). | ||
| 676 | */ | ||
| 677 | static void unroll (lua_State *L, void *ud) { | ||
| 678 | CallInfo *ci; | ||
| 679 | UNUSED(ud); | ||
| 680 | while ((ci = L->ci) != &L->base_ci) { /* something in the stack */ | ||
| 681 | if (!isLua(ci)) /* C function? */ | ||
| 682 | finishCcall(L, ci); /* complete its execution */ | ||
| 683 | else { /* Lua function */ | ||
| 684 | luaV_finishOp(L); /* finish interrupted instruction */ | ||
| 685 | luaV_execute(L, ci); /* execute down to higher C 'boundary' */ | ||
| 686 | } | ||
| 687 | } | ||
| 688 | } | ||
| 689 | |||
| 690 | |||
| 691 | /* | ||
| 692 | ** Try to find a suspended protected call (a "recover point") for the | ||
| 693 | ** given thread. | ||
| 694 | */ | ||
| 695 | static CallInfo *findpcall (lua_State *L) { | ||
| 696 | CallInfo *ci; | ||
| 697 | for (ci = L->ci; ci != NULL; ci = ci->previous) { /* search for a pcall */ | ||
| 698 | if (ci->callstatus & CIST_YPCALL) | ||
| 699 | return ci; | ||
| 700 | } | ||
| 701 | return NULL; /* no pending pcall */ | ||
| 702 | } | ||
| 703 | |||
| 704 | |||
| 705 | /* | ||
| 706 | ** Signal an error in the call to 'lua_resume', not in the execution | ||
| 707 | ** of the coroutine itself. (Such errors should not be handled by any | ||
| 708 | ** coroutine error handler and should not kill the coroutine.) | ||
| 709 | */ | ||
| 710 | static int resume_error (lua_State *L, const char *msg, int narg) { | ||
| 711 | L->top -= narg; /* remove args from the stack */ | ||
| 712 | setsvalue2s(L, L->top, luaS_new(L, msg)); /* push error message */ | ||
| 713 | api_incr_top(L); | ||
| 714 | lua_unlock(L); | ||
| 715 | return LUA_ERRRUN; | ||
| 716 | } | ||
| 717 | |||
| 718 | |||
| 719 | /* | ||
| 720 | ** Do the work for 'lua_resume' in protected mode. Most of the work | ||
| 721 | ** depends on the status of the coroutine: initial state, suspended | ||
| 722 | ** inside a hook, or regularly suspended (optionally with a continuation | ||
| 723 | ** function), plus erroneous cases: non-suspended coroutine or dead | ||
| 724 | ** coroutine. | ||
| 725 | */ | ||
| 726 | static void resume (lua_State *L, void *ud) { | ||
| 727 | int n = *(cast(int*, ud)); /* number of arguments */ | ||
| 728 | StkId firstArg = L->top - n; /* first argument */ | ||
| 729 | CallInfo *ci = L->ci; | ||
| 730 | if (L->status == LUA_OK) /* starting a coroutine? */ | ||
| 731 | ccall(L, firstArg - 1, LUA_MULTRET, 1); /* just call its body */ | ||
| 732 | else { /* resuming from previous yield */ | ||
| 733 | lua_assert(L->status == LUA_YIELD); | ||
| 734 | L->status = LUA_OK; /* mark that it is running (again) */ | ||
| 735 | luaE_incCstack(L); /* control the C stack */ | ||
| 736 | if (isLua(ci)) { /* yielded inside a hook? */ | ||
| 737 | L->top = firstArg; /* discard arguments */ | ||
| 738 | luaV_execute(L, ci); /* just continue running Lua code */ | ||
| 739 | } | ||
| 740 | else { /* 'common' yield */ | ||
| 741 | if (ci->u.c.k != NULL) { /* does it have a continuation function? */ | ||
| 742 | lua_unlock(L); | ||
| 743 | n = (*ci->u.c.k)(L, LUA_YIELD, ci->u.c.ctx); /* call continuation */ | ||
| 744 | lua_lock(L); | ||
| 745 | api_checknelems(L, n); | ||
| 746 | } | ||
| 747 | luaD_poscall(L, ci, n); /* finish 'luaD_call' */ | ||
| 748 | } | ||
| 749 | unroll(L, NULL); /* run continuation */ | ||
| 750 | } | ||
| 751 | } | ||
| 752 | |||
| 753 | |||
| 754 | /* | ||
| 755 | ** Unrolls a coroutine in protected mode while there are recoverable | ||
| 756 | ** errors, that is, errors inside a protected call. (Any error | ||
| 757 | ** interrupts 'unroll', and this loop protects it again so it can | ||
| 758 | ** continue.) Stops with a normal end (status == LUA_OK), an yield | ||
| 759 | ** (status == LUA_YIELD), or an unprotected error ('findpcall' doesn't | ||
| 760 | ** find a recover point). | ||
| 761 | */ | ||
| 762 | static int precover (lua_State *L, int status) { | ||
| 763 | CallInfo *ci; | ||
| 764 | while (errorstatus(status) && (ci = findpcall(L)) != NULL) { | ||
| 765 | L->ci = ci; /* go down to recovery functions */ | ||
| 766 | setcistrecst(ci, status); /* status to finish 'pcall' */ | ||
| 767 | status = luaD_rawrunprotected(L, unroll, NULL); | ||
| 768 | } | ||
| 769 | return status; | ||
| 770 | } | ||
| 771 | |||
| 772 | |||
| 773 | LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, | ||
| 774 | int *nresults) { | ||
| 775 | int status; | ||
| 776 | lua_lock(L); | ||
| 777 | if (L->status == LUA_OK) { /* may be starting a coroutine */ | ||
| 778 | if (L->ci != &L->base_ci) /* not in base level? */ | ||
| 779 | return resume_error(L, "cannot resume non-suspended coroutine", nargs); | ||
| 780 | else if (L->top - (L->ci->func + 1) == nargs) /* no function? */ | ||
| 781 | return resume_error(L, "cannot resume dead coroutine", nargs); | ||
| 782 | } | ||
| 783 | else if (L->status != LUA_YIELD) /* ended with errors? */ | ||
| 784 | return resume_error(L, "cannot resume dead coroutine", nargs); | ||
| 785 | L->nCcalls = (from) ? getCcalls(from) : 0; | ||
| 786 | luai_userstateresume(L, nargs); | ||
| 787 | api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs); | ||
| 788 | status = luaD_rawrunprotected(L, resume, &nargs); | ||
| 789 | /* continue running after recoverable errors */ | ||
| 790 | status = precover(L, status); | ||
| 791 | if (l_likely(!errorstatus(status))) | ||
| 792 | lua_assert(status == L->status); /* normal end or yield */ | ||
| 793 | else { /* unrecoverable error */ | ||
| 794 | L->status = cast_byte(status); /* mark thread as 'dead' */ | ||
| 795 | luaD_seterrorobj(L, status, L->top); /* push error message */ | ||
| 796 | L->ci->top = L->top; | ||
| 797 | } | ||
| 798 | *nresults = (status == LUA_YIELD) ? L->ci->u2.nyield | ||
| 799 | : cast_int(L->top - (L->ci->func + 1)); | ||
| 800 | lua_unlock(L); | ||
| 801 | return status; | ||
| 802 | } | ||
| 803 | |||
| 804 | |||
| 805 | LUA_API int lua_isyieldable (lua_State *L) { | ||
| 806 | return yieldable(L); | ||
| 807 | } | ||
| 808 | |||
| 809 | |||
| 810 | LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, | ||
| 811 | lua_KFunction k) { | ||
| 812 | CallInfo *ci; | ||
| 813 | luai_userstateyield(L, nresults); | ||
| 814 | lua_lock(L); | ||
| 815 | ci = L->ci; | ||
| 816 | api_checknelems(L, nresults); | ||
| 817 | if (l_unlikely(!yieldable(L))) { | ||
| 818 | if (L != G(L)->mainthread) | ||
| 819 | luaG_runerror(L, "attempt to yield across a C-call boundary"); | ||
| 820 | else | ||
| 821 | luaG_runerror(L, "attempt to yield from outside a coroutine"); | ||
| 822 | } | ||
| 823 | L->status = LUA_YIELD; | ||
| 824 | ci->u2.nyield = nresults; /* save number of results */ | ||
| 825 | if (isLua(ci)) { /* inside a hook? */ | ||
| 826 | lua_assert(!isLuacode(ci)); | ||
| 827 | api_check(L, nresults == 0, "hooks cannot yield values"); | ||
| 828 | api_check(L, k == NULL, "hooks cannot continue after yielding"); | ||
| 829 | } | ||
| 830 | else { | ||
| 831 | if ((ci->u.c.k = k) != NULL) /* is there a continuation? */ | ||
| 832 | ci->u.c.ctx = ctx; /* save context */ | ||
| 833 | luaD_throw(L, LUA_YIELD); | ||
| 834 | } | ||
| 835 | lua_assert(ci->callstatus & CIST_HOOKED); /* must be inside a hook */ | ||
| 836 | lua_unlock(L); | ||
| 837 | return 0; /* return to 'luaD_hook' */ | ||
| 838 | } | ||
| 839 | |||
| 840 | |||
| 841 | /* | ||
| 842 | ** Auxiliary structure to call 'luaF_close' in protected mode. | ||
| 843 | */ | ||
| 844 | struct CloseP { | ||
| 845 | StkId level; | ||
| 846 | int status; | ||
| 847 | }; | ||
| 848 | |||
| 849 | |||
| 850 | /* | ||
| 851 | ** Auxiliary function to call 'luaF_close' in protected mode. | ||
| 852 | */ | ||
| 853 | static void closepaux (lua_State *L, void *ud) { | ||
| 854 | struct CloseP *pcl = cast(struct CloseP *, ud); | ||
| 855 | luaF_close(L, pcl->level, pcl->status, 0); | ||
| 856 | } | ||
| 857 | |||
| 858 | |||
| 859 | /* | ||
| 860 | ** Calls 'luaF_close' in protected mode. Return the original status | ||
| 861 | ** or, in case of errors, the new status. | ||
| 862 | */ | ||
| 863 | int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status) { | ||
| 864 | CallInfo *old_ci = L->ci; | ||
| 865 | lu_byte old_allowhooks = L->allowhook; | ||
| 866 | for (;;) { /* keep closing upvalues until no more errors */ | ||
| 867 | struct CloseP pcl; | ||
| 868 | pcl.level = restorestack(L, level); pcl.status = status; | ||
| 869 | status = luaD_rawrunprotected(L, &closepaux, &pcl); | ||
| 870 | if (l_likely(status == LUA_OK)) /* no more errors? */ | ||
| 871 | return pcl.status; | ||
| 872 | else { /* an error occurred; restore saved state and repeat */ | ||
| 873 | L->ci = old_ci; | ||
| 874 | L->allowhook = old_allowhooks; | ||
| 875 | } | ||
| 876 | } | ||
| 877 | } | ||
| 878 | |||
| 879 | |||
| 880 | /* | ||
| 881 | ** Call the C function 'func' in protected mode, restoring basic | ||
| 882 | ** thread information ('allowhook', etc.) and in particular | ||
| 883 | ** its stack level in case of errors. | ||
| 884 | */ | ||
| 885 | int luaD_pcall (lua_State *L, Pfunc func, void *u, | ||
| 886 | ptrdiff_t old_top, ptrdiff_t ef) { | ||
| 887 | int status; | ||
| 888 | CallInfo *old_ci = L->ci; | ||
| 889 | lu_byte old_allowhooks = L->allowhook; | ||
| 890 | ptrdiff_t old_errfunc = L->errfunc; | ||
| 891 | L->errfunc = ef; | ||
| 892 | status = luaD_rawrunprotected(L, func, u); | ||
| 893 | if (l_unlikely(status != LUA_OK)) { /* an error occurred? */ | ||
| 894 | L->ci = old_ci; | ||
| 895 | L->allowhook = old_allowhooks; | ||
| 896 | status = luaD_closeprotected(L, old_top, status); | ||
| 897 | luaD_seterrorobj(L, status, restorestack(L, old_top)); | ||
| 898 | luaD_shrinkstack(L); /* restore stack size in case of overflow */ | ||
| 899 | } | ||
| 900 | L->errfunc = old_errfunc; | ||
| 901 | return status; | ||
| 902 | } | ||
| 903 | |||
| 904 | |||
| 905 | |||
| 906 | /* | ||
| 907 | ** Execute a protected parser. | ||
| 908 | */ | ||
| 909 | struct SParser { /* data to 'f_parser' */ | ||
| 910 | ZIO *z; | ||
| 911 | Mbuffer buff; /* dynamic structure used by the scanner */ | ||
| 912 | Dyndata dyd; /* dynamic structures used by the parser */ | ||
| 913 | const char *mode; | ||
| 914 | const char *name; | ||
| 915 | }; | ||
| 916 | |||
| 917 | |||
| 918 | static void checkmode (lua_State *L, const char *mode, const char *x) { | ||
| 919 | if (mode && strchr(mode, x[0]) == NULL) { | ||
| 920 | luaO_pushfstring(L, | ||
| 921 | "attempt to load a %s chunk (mode is '%s')", x, mode); | ||
| 922 | luaD_throw(L, LUA_ERRSYNTAX); | ||
| 923 | } | ||
| 924 | } | ||
| 925 | |||
| 926 | |||
| 927 | static void f_parser (lua_State *L, void *ud) { | ||
| 928 | LClosure *cl; | ||
| 929 | struct SParser *p = cast(struct SParser *, ud); | ||
| 930 | int c = zgetc(p->z); /* read first character */ | ||
| 931 | if (c == LUA_SIGNATURE[0]) { | ||
| 932 | checkmode(L, p->mode, "binary"); | ||
| 933 | cl = luaU_undump(L, p->z, p->name); | ||
| 934 | } | ||
| 935 | else { | ||
| 936 | checkmode(L, p->mode, "text"); | ||
| 937 | cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c); | ||
| 938 | } | ||
| 939 | lua_assert(cl->nupvalues == cl->p->sizeupvalues); | ||
| 940 | luaF_initupvals(L, cl); | ||
| 941 | } | ||
| 942 | |||
| 943 | |||
| 944 | int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, | ||
| 945 | const char *mode) { | ||
| 946 | struct SParser p; | ||
| 947 | int status; | ||
| 948 | incnny(L); /* cannot yield during parsing */ | ||
| 949 | p.z = z; p.name = name; p.mode = mode; | ||
| 950 | p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0; | ||
| 951 | p.dyd.gt.arr = NULL; p.dyd.gt.size = 0; | ||
| 952 | p.dyd.label.arr = NULL; p.dyd.label.size = 0; | ||
| 953 | luaZ_initbuffer(L, &p.buff); | ||
| 954 | status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc); | ||
| 955 | luaZ_freebuffer(L, &p.buff); | ||
| 956 | luaM_freearray(L, p.dyd.actvar.arr, p.dyd.actvar.size); | ||
| 957 | luaM_freearray(L, p.dyd.gt.arr, p.dyd.gt.size); | ||
| 958 | luaM_freearray(L, p.dyd.label.arr, p.dyd.label.size); | ||
| 959 | decnny(L); | ||
| 960 | return status; | ||
| 961 | } | ||
| 962 | |||
| 963 | |||
