You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

672 lines
20 KiB

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