ldo.c 22 KB

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