ldo.c 20 KB

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