1 /* BEGIN CSTYLED */ 2 /* 3 ** $Id: ldo.c,v 2.108.1.3 2013/11/08 18:22:50 roberto Exp $ 4 ** Stack and Call structure of Lua 5 ** See Copyright Notice in lua.h 6 */ 7 8 9 #define ldo_c 10 #define LUA_CORE 11 12 #include <sys/lua/lua.h> 13 14 #include "lapi.h" 15 #include "ldebug.h" 16 #include "ldo.h" 17 #include "lfunc.h" 18 #include "lgc.h" 19 #include "lmem.h" 20 #include "lobject.h" 21 #include "lopcodes.h" 22 #include "lparser.h" 23 #include "lstate.h" 24 #include "lstring.h" 25 #include "ltable.h" 26 #include "ltm.h" 27 #include "lvm.h" 28 #include "lzio.h" 29 30 31 32 /* Return the number of bytes available on the stack. */ 33 #if defined (_KERNEL) && defined(__linux__) 34 #include <asm/current.h> 35 static intptr_t stack_remaining(void) { 36 intptr_t local; 37 local = (intptr_t)&local - (intptr_t)current->stack; 38 return local; 39 } 40 #elif defined (_KERNEL) && defined(__FreeBSD__) 41 #include <sys/pcpu.h> 42 static intptr_t stack_remaining(void) { 43 intptr_t local; 44 local = (intptr_t)&local - (intptr_t)curthread->td_kstack; 45 return local; 46 } 47 #else 48 static intptr_t stack_remaining(void) { 49 return INTPTR_MAX; 50 } 51 #endif 52 53 /* 54 ** {====================================================== 55 ** Error-recovery functions 56 ** ======================================================= 57 */ 58 59 /* 60 ** LUAI_THROW/LUAI_TRY define how Lua does exception handling. By 61 ** default, Lua handles errors with exceptions when compiling as 62 ** C++ code, with _longjmp/_setjmp when asked to use them, and with 63 ** longjmp/setjmp otherwise. 64 */ 65 #if !defined(LUAI_THROW) 66 67 #ifdef _KERNEL 68 69 #ifdef __linux__ 70 #if defined(__i386__) 71 #define JMP_BUF_CNT 6 72 #elif defined(__x86_64__) 73 #define JMP_BUF_CNT 8 74 #elif defined(__sparc__) && defined(__arch64__) 75 #define JMP_BUF_CNT 6 76 #elif defined(__powerpc__) 77 #define JMP_BUF_CNT 26 78 #elif defined(__aarch64__) 79 #define JMP_BUF_CNT 64 80 #elif defined(__arm__) 81 #define JMP_BUF_CNT 65 82 #elif defined(__mips__) 83 #define JMP_BUF_CNT 12 84 #elif defined(__s390x__) 85 #define JMP_BUF_CNT 18 86 #elif defined(__riscv) 87 #define JMP_BUF_CNT 64 88 #else 89 #define JMP_BUF_CNT 1 90 #endif 91 92 typedef struct _label_t { long long unsigned val[JMP_BUF_CNT]; } label_t; 93 94 int setjmp(label_t *) __attribute__ ((__nothrow__)); 95 extern void longjmp(label_t *) __attribute__((__noreturn__)); 96 97 #define LUAI_THROW(L,c) longjmp(&(c)->b) 98 #define LUAI_TRY(L,c,a) if (setjmp(&(c)->b) == 0) { a } 99 #define luai_jmpbuf label_t 100 101 /* unsupported arches will build but not be able to run lua programs */ 102 #if JMP_BUF_CNT == 1 103 int setjmp (label_t *buf) { 104 return 1; 105 } 106 107 void longjmp (label_t * buf) { 108 for (;;); 109 } 110 #endif 111 #else 112 #define LUAI_THROW(L,c) longjmp((c)->b, 1) 113 #define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a } 114 #define luai_jmpbuf jmp_buf 115 #endif 116 117 #else /* _KERNEL */ 118 119 #if defined(__cplusplus) && !defined(LUA_USE_LONGJMP) 120 /* C++ exceptions */ 121 #define LUAI_THROW(L,c) throw(c) 122 #define LUAI_TRY(L,c,a) \ 123 try { a } catch(...) { if ((c)->status == 0) (c)->status = -1; } 124 #define luai_jmpbuf int /* dummy variable */ 125 126 #elif defined(LUA_USE_ULONGJMP) 127 /* in Unix, try _longjmp/_setjmp (more efficient) */ 128 #define LUAI_THROW(L,c) _longjmp((c)->b, 1) 129 #define LUAI_TRY(L,c,a) if (_setjmp((c)->b) == 0) { a } 130 #define luai_jmpbuf jmp_buf 131 132 #else 133 /* default handling with long jumps */ 134 #define LUAI_THROW(L,c) longjmp((c)->b, 1) 135 #define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a } 136 #define luai_jmpbuf jmp_buf 137 138 #endif 139 140 #endif /* _KERNEL */ 141 142 #endif /* LUAI_THROW */ 143 144 145 /* chain list of long jump buffers */ 146 struct lua_longjmp { 147 struct lua_longjmp *previous; 148 luai_jmpbuf b; 149 volatile int status; /* error code */ 150 }; 151 152 153 static void seterrorobj (lua_State *L, int errcode, StkId oldtop) { 154 switch (errcode) { 155 case LUA_ERRMEM: { /* memory error? */ 156 setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */ 157 break; 158 } 159 case LUA_ERRERR: { 160 setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling")); 161 break; 162 } 163 default: { 164 setobjs2s(L, oldtop, L->top - 1); /* error message on current top */ 165 break; 166 } 167 } 168 L->top = oldtop + 1; 169 } 170 171 172 l_noret luaD_throw (lua_State *L, int errcode) { 173 if (L->errorJmp) { /* thread has an error handler? */ 174 L->errorJmp->status = errcode; /* set status */ 175 LUAI_THROW(L, L->errorJmp); /* jump to it */ 176 } 177 else { /* thread has no error handler */ 178 L->status = cast_byte(errcode); /* mark it as dead */ 179 if (G(L)->mainthread->errorJmp) { /* main thread has a handler? */ 180 setobjs2s(L, G(L)->mainthread->top++, L->top - 1); /* copy error obj. */ 181 luaD_throw(G(L)->mainthread, errcode); /* re-throw in main thread */ 182 } 183 else { /* no handler at all; abort */ 184 if (G(L)->panic) { /* panic function? */ 185 lua_unlock(L); 186 G(L)->panic(L); /* call it (last chance to jump out) */ 187 } 188 panic("no error handler"); 189 } 190 } 191 } 192 193 194 int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { 195 unsigned short oldnCcalls = L->nCcalls; 196 struct lua_longjmp lj; 197 lj.status = LUA_OK; 198 lj.previous = L->errorJmp; /* chain new error handler */ 199 L->errorJmp = &lj; 200 LUAI_TRY(L, &lj, 201 (*f)(L, ud); 202 ); 203 L->errorJmp = lj.previous; /* restore old error handler */ 204 L->nCcalls = oldnCcalls; 205 return lj.status; 206 } 207 208 /* }====================================================== */ 209 210 211 static void correctstack (lua_State *L, TValue *oldstack) { 212 CallInfo *ci; 213 GCObject *up; 214 L->top = (L->top - oldstack) + L->stack; 215 for (up = L->openupval; up != NULL; up = up->gch.next) 216 gco2uv(up)->v = (gco2uv(up)->v - oldstack) + L->stack; 217 for (ci = L->ci; ci != NULL; ci = ci->previous) { 218 ci->top = (ci->top - oldstack) + L->stack; 219 ci->func = (ci->func - oldstack) + L->stack; 220 if (isLua(ci)) 221 ci->u.l.base = (ci->u.l.base - oldstack) + L->stack; 222 } 223 } 224 225 226 /* some space for error handling */ 227 #define ERRORSTACKSIZE (LUAI_MAXSTACK + 200) 228 229 230 void luaD_reallocstack (lua_State *L, int newsize) { 231 TValue *oldstack = L->stack; 232 int lim = L->stacksize; 233 lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE); 234 lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK); 235 luaM_reallocvector(L, L->stack, L->stacksize, newsize, TValue); 236 for (; lim < newsize; lim++) 237 setnilvalue(L->stack + lim); /* erase new segment */ 238 L->stacksize = newsize; 239 L->stack_last = L->stack + newsize - EXTRA_STACK; 240 correctstack(L, oldstack); 241 } 242 243 244 void luaD_growstack (lua_State *L, int n) { 245 int size = L->stacksize; 246 if (size > LUAI_MAXSTACK) /* error after extra size? */ 247 luaD_throw(L, LUA_ERRERR); 248 else { 249 int needed = cast_int(L->top - L->stack) + n + EXTRA_STACK; 250 int newsize = 2 * size; 251 if (newsize > LUAI_MAXSTACK) newsize = LUAI_MAXSTACK; 252 if (newsize < needed) newsize = needed; 253 if (newsize > LUAI_MAXSTACK) { /* stack overflow? */ 254 luaD_reallocstack(L, ERRORSTACKSIZE); 255 luaG_runerror(L, "stack overflow"); 256 } 257 else 258 luaD_reallocstack(L, newsize); 259 } 260 } 261 262 263 static int stackinuse (lua_State *L) { 264 CallInfo *ci; 265 StkId lim = L->top; 266 for (ci = L->ci; ci != NULL; ci = ci->previous) { 267 lua_assert(ci->top <= L->stack_last); 268 if (lim < ci->top) lim = ci->top; 269 } 270 return cast_int(lim - L->stack) + 1; /* part of stack in use */ 271 } 272 273 274 void luaD_shrinkstack (lua_State *L) { 275 int inuse = stackinuse(L); 276 int goodsize = inuse + (inuse / 8) + 2*EXTRA_STACK; 277 if (goodsize > LUAI_MAXSTACK) goodsize = LUAI_MAXSTACK; 278 if (inuse > LUAI_MAXSTACK || /* handling stack overflow? */ 279 goodsize >= L->stacksize) /* would grow instead of shrink? */ 280 condmovestack(L); /* don't change stack (change only for debugging) */ 281 else 282 luaD_reallocstack(L, goodsize); /* shrink it */ 283 } 284 285 286 void luaD_hook (lua_State *L, int event, int line) { 287 lua_Hook hook = L->hook; 288 if (hook && L->allowhook) { 289 CallInfo *ci = L->ci; 290 ptrdiff_t top = savestack(L, L->top); 291 ptrdiff_t ci_top = savestack(L, ci->top); 292 lua_Debug ar; 293 ar.event = event; 294 ar.currentline = line; 295 ar.i_ci = ci; 296 luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ 297 ci->top = L->top + LUA_MINSTACK; 298 lua_assert(ci->top <= L->stack_last); 299 L->allowhook = 0; /* cannot call hooks inside a hook */ 300 ci->callstatus |= CIST_HOOKED; 301 lua_unlock(L); 302 (*hook)(L, &ar); 303 lua_lock(L); 304 lua_assert(!L->allowhook); 305 L->allowhook = 1; 306 ci->top = restorestack(L, ci_top); 307 L->top = restorestack(L, top); 308 ci->callstatus &= ~CIST_HOOKED; 309 } 310 } 311 312 313 static void callhook (lua_State *L, CallInfo *ci) { 314 int hook = LUA_HOOKCALL; 315 ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ 316 if (isLua(ci->previous) && 317 GET_OPCODE(*(ci->previous->u.l.savedpc - 1)) == OP_TAILCALL) { 318 ci->callstatus |= CIST_TAIL; 319 hook = LUA_HOOKTAILCALL; 320 } 321 luaD_hook(L, hook, -1); 322 ci->u.l.savedpc--; /* correct 'pc' */ 323 } 324 325 326 static StkId adjust_varargs (lua_State *L, Proto *p, int actual) { 327 int i; 328 int nfixargs = p->numparams; 329 StkId base, fixed; 330 lua_assert(actual >= nfixargs); 331 /* move fixed parameters to final position */ 332 luaD_checkstack(L, p->maxstacksize); /* check again for new 'base' */ 333 fixed = L->top - actual; /* first fixed argument */ 334 base = L->top; /* final position of first argument */ 335 for (i=0; i<nfixargs; i++) { 336 setobjs2s(L, L->top++, fixed + i); 337 setnilvalue(fixed + i); 338 } 339 return base; 340 } 341 342 343 static StkId tryfuncTM (lua_State *L, StkId func) { 344 const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL); 345 StkId p; 346 ptrdiff_t funcr = savestack(L, func); 347 if (!ttisfunction(tm)) 348 luaG_typeerror(L, func, "call"); 349 /* Open a hole inside the stack at `func' */ 350 for (p = L->top; p > func; p--) setobjs2s(L, p, p-1); 351 incr_top(L); 352 func = restorestack(L, funcr); /* previous call may change stack */ 353 setobj2s(L, func, tm); /* tag method is the new function to be called */ 354 return func; 355 } 356 357 358 359 #define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L))) 360 361 362 /* 363 ** returns true if function has been executed (C function) 364 */ 365 int luaD_precall (lua_State *L, StkId func, int nresults) { 366 lua_CFunction f; 367 CallInfo *ci; 368 int n; /* number of arguments (Lua) or returns (C) */ 369 ptrdiff_t funcr = savestack(L, func); 370 switch (ttype(func)) { 371 case LUA_TLCF: /* light C function */ 372 f = fvalue(func); 373 goto Cfunc; 374 case LUA_TCCL: { /* C closure */ 375 f = clCvalue(func)->f; 376 Cfunc: 377 luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ 378 ci = next_ci(L); /* now 'enter' new function */ 379 ci->nresults = nresults; 380 ci->func = restorestack(L, funcr); 381 ci->top = L->top + LUA_MINSTACK; 382 lua_assert(ci->top <= L->stack_last); 383 ci->callstatus = 0; 384 luaC_checkGC(L); /* stack grow uses memory */ 385 if (L->hookmask & LUA_MASKCALL) 386 luaD_hook(L, LUA_HOOKCALL, -1); 387 lua_unlock(L); 388 n = (*f)(L); /* do the actual call */ 389 lua_lock(L); 390 api_checknelems(L, n); 391 luaD_poscall(L, L->top - n); 392 return 1; 393 } 394 case LUA_TLCL: { /* Lua function: prepare its call */ 395 StkId base; 396 Proto *p = clLvalue(func)->p; 397 n = cast_int(L->top - func) - 1; /* number of real arguments */ 398 luaD_checkstack(L, p->maxstacksize); 399 for (; n < p->numparams; n++) 400 setnilvalue(L->top++); /* complete missing arguments */ 401 if (!p->is_vararg) { 402 func = restorestack(L, funcr); 403 base = func + 1; 404 } 405 else { 406 base = adjust_varargs(L, p, n); 407 func = restorestack(L, funcr); /* previous call can change stack */ 408 } 409 ci = next_ci(L); /* now 'enter' new function */ 410 ci->nresults = nresults; 411 ci->func = func; 412 ci->u.l.base = base; 413 ci->top = base + p->maxstacksize; 414 lua_assert(ci->top <= L->stack_last); 415 ci->u.l.savedpc = p->code; /* starting point */ 416 ci->callstatus = CIST_LUA; 417 L->top = ci->top; 418 luaC_checkGC(L); /* stack grow uses memory */ 419 if (L->hookmask & LUA_MASKCALL) 420 callhook(L, ci); 421 return 0; 422 } 423 default: { /* not a function */ 424 func = tryfuncTM(L, func); /* retry with 'function' tag method */ 425 return luaD_precall(L, func, nresults); /* now it must be a function */ 426 } 427 } 428 } 429 430 431 int luaD_poscall (lua_State *L, StkId firstResult) { 432 StkId res; 433 int wanted, i; 434 CallInfo *ci = L->ci; 435 if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) { 436 if (L->hookmask & LUA_MASKRET) { 437 ptrdiff_t fr = savestack(L, firstResult); /* hook may change stack */ 438 luaD_hook(L, LUA_HOOKRET, -1); 439 firstResult = restorestack(L, fr); 440 } 441 L->oldpc = ci->previous->u.l.savedpc; /* 'oldpc' for caller function */ 442 } 443 res = ci->func; /* res == final position of 1st result */ 444 wanted = ci->nresults; 445 L->ci = ci = ci->previous; /* back to caller */ 446 /* move results to correct place */ 447 for (i = wanted; i != 0 && firstResult < L->top; i--) 448 setobjs2s(L, res++, firstResult++); 449 while (i-- > 0) 450 setnilvalue(res++); 451 L->top = res; 452 return (wanted - LUA_MULTRET); /* 0 iff wanted == LUA_MULTRET */ 453 } 454 455 456 /* 457 ** Call a function (C or Lua). The function to be called is at *func. 458 ** The arguments are on the stack, right after the function. 459 ** When returns, all the results are on the stack, starting at the original 460 ** function position. 461 */ 462 void luaD_call (lua_State *L, StkId func, int nResults, int allowyield) { 463 if (++L->nCcalls >= LUAI_MAXCCALLS) { 464 if (L->nCcalls == LUAI_MAXCCALLS) 465 luaG_runerror(L, "C stack overflow"); 466 else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3))) 467 luaD_throw(L, LUA_ERRERR); /* error while handling stack error */ 468 } 469 intptr_t remaining = stack_remaining(); 470 if (L->runerror == 0 && remaining < LUAI_MINCSTACK) 471 luaG_runerror(L, "C stack overflow"); 472 if (L->runerror != 0 && remaining < LUAI_MINCSTACK / 2) 473 luaD_throw(L, LUA_ERRERR); /* error while handling stack error */ 474 if (!allowyield) L->nny++; 475 if (!luaD_precall(L, func, nResults)) /* is a Lua function? */ 476 luaV_execute(L); /* call it */ 477 if (!allowyield) L->nny--; 478 L->nCcalls--; 479 } 480 481 482 static void finishCcall (lua_State *L) { 483 CallInfo *ci = L->ci; 484 int n; 485 lua_assert(ci->u.c.k != NULL); /* must have a continuation */ 486 lua_assert(L->nny == 0); 487 if (ci->callstatus & CIST_YPCALL) { /* was inside a pcall? */ 488 ci->callstatus &= ~CIST_YPCALL; /* finish 'lua_pcall' */ 489 L->errfunc = ci->u.c.old_errfunc; 490 } 491 /* finish 'lua_callk'/'lua_pcall' */ 492 adjustresults(L, ci->nresults); 493 /* call continuation function */ 494 if (!(ci->callstatus & CIST_STAT)) /* no call status? */ 495 ci->u.c.status = LUA_YIELD; /* 'default' status */ 496 lua_assert(ci->u.c.status != LUA_OK); 497 ci->callstatus = (ci->callstatus & ~(CIST_YPCALL | CIST_STAT)) | CIST_YIELDED; 498 lua_unlock(L); 499 n = (*ci->u.c.k)(L); 500 lua_lock(L); 501 api_checknelems(L, n); 502 /* finish 'luaD_precall' */ 503 luaD_poscall(L, L->top - n); 504 } 505 506 507 static void unroll (lua_State *L, void *ud) { 508 UNUSED(ud); 509 for (;;) { 510 if (L->ci == &L->base_ci) /* stack is empty? */ 511 return; /* coroutine finished normally */ 512 if (!isLua(L->ci)) /* C function? */ 513 finishCcall(L); 514 else { /* Lua function */ 515 luaV_finishOp(L); /* finish interrupted instruction */ 516 luaV_execute(L); /* execute down to higher C 'boundary' */ 517 } 518 } 519 } 520 521 522 /* 523 ** check whether thread has a suspended protected call 524 */ 525 static CallInfo *findpcall (lua_State *L) { 526 CallInfo *ci; 527 for (ci = L->ci; ci != NULL; ci = ci->previous) { /* search for a pcall */ 528 if (ci->callstatus & CIST_YPCALL) 529 return ci; 530 } 531 return NULL; /* no pending pcall */ 532 } 533 534 535 static int recover (lua_State *L, int status) { 536 StkId oldtop; 537 CallInfo *ci = findpcall(L); 538 if (ci == NULL) return 0; /* no recovery point */ 539 /* "finish" luaD_pcall */ 540 oldtop = restorestack(L, ci->extra); 541 luaF_close(L, oldtop); 542 seterrorobj(L, status, oldtop); 543 L->ci = ci; 544 L->allowhook = ci->u.c.old_allowhook; 545 L->nny = 0; /* should be zero to be yieldable */ 546 luaD_shrinkstack(L); 547 L->errfunc = ci->u.c.old_errfunc; 548 ci->callstatus |= CIST_STAT; /* call has error status */ 549 ci->u.c.status = status; /* (here it is) */ 550 return 1; /* continue running the coroutine */ 551 } 552 553 554 /* 555 ** signal an error in the call to 'resume', not in the execution of the 556 ** coroutine itself. (Such errors should not be handled by any coroutine 557 ** error handler and should not kill the coroutine.) 558 */ 559 static l_noret resume_error (lua_State *L, const char *msg, StkId firstArg) { 560 L->top = firstArg; /* remove args from the stack */ 561 setsvalue2s(L, L->top, luaS_new(L, msg)); /* push error message */ 562 api_incr_top(L); 563 luaD_throw(L, -1); /* jump back to 'lua_resume' */ 564 } 565 566 567 /* 568 ** do the work for 'lua_resume' in protected mode 569 */ 570 static void resume_cb (lua_State *L, void *ud) { 571 int nCcalls = L->nCcalls; 572 StkId firstArg = cast(StkId, ud); 573 CallInfo *ci = L->ci; 574 if (nCcalls >= LUAI_MAXCCALLS) 575 resume_error(L, "C stack overflow", firstArg); 576 if (L->status == LUA_OK) { /* may be starting a coroutine */ 577 if (ci != &L->base_ci) /* not in base level? */ 578 resume_error(L, "cannot resume non-suspended coroutine", firstArg); 579 /* coroutine is in base level; start running it */ 580 if (!luaD_precall(L, firstArg - 1, LUA_MULTRET)) /* Lua function? */ 581 luaV_execute(L); /* call it */ 582 } 583 else if (L->status != LUA_YIELD) 584 resume_error(L, "cannot resume dead coroutine", firstArg); 585 else { /* resuming from previous yield */ 586 L->status = LUA_OK; 587 ci->func = restorestack(L, ci->extra); 588 if (isLua(ci)) /* yielded inside a hook? */ 589 luaV_execute(L); /* just continue running Lua code */ 590 else { /* 'common' yield */ 591 if (ci->u.c.k != NULL) { /* does it have a continuation? */ 592 int n; 593 ci->u.c.status = LUA_YIELD; /* 'default' status */ 594 ci->callstatus |= CIST_YIELDED; 595 lua_unlock(L); 596 n = (*ci->u.c.k)(L); /* call continuation */ 597 lua_lock(L); 598 api_checknelems(L, n); 599 firstArg = L->top - n; /* yield results come from continuation */ 600 } 601 luaD_poscall(L, firstArg); /* finish 'luaD_precall' */ 602 } 603 unroll(L, NULL); 604 } 605 lua_assert(nCcalls == L->nCcalls); 606 } 607 608 609 LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) { 610 int status; 611 int oldnny = L->nny; /* save 'nny' */ 612 lua_lock(L); 613 luai_userstateresume(L, nargs); 614 L->nCcalls = (from) ? from->nCcalls + 1 : 1; 615 L->nny = 0; /* allow yields */ 616 api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs); 617 status = luaD_rawrunprotected(L, resume_cb, L->top - nargs); 618 if (status == -1) /* error calling 'lua_resume'? */ 619 status = LUA_ERRRUN; 620 else { /* yield or regular error */ 621 while (status != LUA_OK && status != LUA_YIELD) { /* error? */ 622 if (recover(L, status)) /* recover point? */ 623 status = luaD_rawrunprotected(L, unroll, NULL); /* run continuation */ 624 else { /* unrecoverable error */ 625 L->status = cast_byte(status); /* mark thread as `dead' */ 626 seterrorobj(L, status, L->top); 627 L->ci->top = L->top; 628 break; 629 } 630 } 631 lua_assert(status == L->status); 632 } 633 L->nny = oldnny; /* restore 'nny' */ 634 L->nCcalls--; 635 lua_assert(L->nCcalls == ((from) ? from->nCcalls : 0)); 636 lua_unlock(L); 637 return status; 638 } 639 640 641 LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k) { 642 CallInfo *ci = L->ci; 643 luai_userstateyield(L, nresults); 644 lua_lock(L); 645 api_checknelems(L, nresults); 646 if (L->nny > 0) { 647 if (L != G(L)->mainthread) 648 luaG_runerror(L, "attempt to yield across a C-call boundary"); 649 else 650 luaG_runerror(L, "attempt to yield from outside a coroutine"); 651 } 652 L->status = LUA_YIELD; 653 ci->extra = savestack(L, ci->func); /* save current 'func' */ 654 if (isLua(ci)) { /* inside a hook? */ 655 api_check(L, k == NULL, "hooks cannot continue after yielding"); 656 } 657 else { 658 if ((ci->u.c.k = k) != NULL) /* is there a continuation? */ 659 ci->u.c.ctx = ctx; /* save context */ 660 ci->func = L->top - nresults - 1; /* protect stack below results */ 661 luaD_throw(L, LUA_YIELD); 662 } 663 lua_assert(ci->callstatus & CIST_HOOKED); /* must be inside a hook */ 664 lua_unlock(L); 665 return 0; /* return to 'luaD_hook' */ 666 } 667 668 669 int luaD_pcall (lua_State *L, Pfunc func, void *u, 670 ptrdiff_t old_top, ptrdiff_t ef) { 671 int status; 672 CallInfo *old_ci = L->ci; 673 lu_byte old_allowhooks = L->allowhook; 674 unsigned short old_nny = L->nny; 675 ptrdiff_t old_errfunc = L->errfunc; 676 L->errfunc = ef; 677 status = luaD_rawrunprotected(L, func, u); 678 if (status != LUA_OK) { /* an error occurred? */ 679 StkId oldtop = restorestack(L, old_top); 680 luaF_close(L, oldtop); /* close possible pending closures */ 681 seterrorobj(L, status, oldtop); 682 L->ci = old_ci; 683 L->allowhook = old_allowhooks; 684 L->nny = old_nny; 685 luaD_shrinkstack(L); 686 } 687 L->errfunc = old_errfunc; 688 return status; 689 } 690 691 692 693 /* 694 ** Execute a protected parser. 695 */ 696 struct SParser { /* data to `f_parser' */ 697 ZIO *z; 698 Mbuffer buff; /* dynamic structure used by the scanner */ 699 Dyndata dyd; /* dynamic structures used by the parser */ 700 const char *mode; 701 const char *name; 702 }; 703 704 705 static void checkmode (lua_State *L, const char *mode, const char *x) { 706 if (mode && strchr(mode, x[0]) == NULL) { 707 luaO_pushfstring(L, 708 "attempt to load a %s chunk (mode is " LUA_QS ")", x, mode); 709 luaD_throw(L, LUA_ERRSYNTAX); 710 } 711 } 712 713 714 static void f_parser (lua_State *L, void *ud) { 715 int i; 716 Closure *cl; 717 struct SParser *p = cast(struct SParser *, ud); 718 int c = zgetc(p->z); /* read first character */ 719 lua_assert(c != LUA_SIGNATURE[0]); /* binary not supported */ 720 checkmode(L, p->mode, "text"); 721 cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c); 722 lua_assert(cl->l.nupvalues == cl->l.p->sizeupvalues); 723 for (i = 0; i < cl->l.nupvalues; i++) { /* initialize upvalues */ 724 UpVal *up = luaF_newupval(L); 725 cl->l.upvals[i] = up; 726 luaC_objbarrier(L, cl, up); 727 } 728 } 729 730 731 int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, 732 const char *mode) { 733 struct SParser p; 734 int status; 735 L->nny++; /* cannot yield during parsing */ 736 p.z = z; p.name = name; p.mode = mode; 737 p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0; 738 p.dyd.gt.arr = NULL; p.dyd.gt.size = 0; 739 p.dyd.label.arr = NULL; p.dyd.label.size = 0; 740 luaZ_initbuffer(L, &p.buff); 741 status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc); 742 luaZ_freebuffer(L, &p.buff); 743 luaM_freearray(L, p.dyd.actvar.arr, p.dyd.actvar.size); 744 luaM_freearray(L, p.dyd.gt.arr, p.dyd.gt.size); 745 luaM_freearray(L, p.dyd.label.arr, p.dyd.label.size); 746 L->nny--; 747 return status; 748 } 749 /* END CSTYLED */ 750