ldo.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /*
  2. ** $Id: ldo.c,v 2.72 2009/11/17 16:46:44 roberto Exp roberto $
  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. /* chain list of long jump buffers */
  34. struct lua_longjmp {
  35. struct lua_longjmp *previous;
  36. luai_jmpbuf b;
  37. volatile int status; /* error code */
  38. };
  39. void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) {
  40. switch (errcode) {
  41. case LUA_ERRMEM: {
  42. setsvalue2s(L, oldtop, luaS_newliteral(L, MEMERRMSG));
  43. break;
  44. }
  45. case LUA_ERRERR: {
  46. setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling"));
  47. break;
  48. }
  49. case LUA_ERRSYNTAX:
  50. case LUA_ERRGCMM:
  51. case LUA_ERRRUN: {
  52. setobjs2s(L, oldtop, L->top - 1); /* error message on current top */
  53. break;
  54. }
  55. }
  56. L->top = oldtop + 1;
  57. }
  58. void luaD_throw (lua_State *L, int errcode) {
  59. if (L->errorJmp) { /* thread has an error handler? */
  60. L->errorJmp->status = errcode; /* set status */
  61. LUAI_THROW(L, L->errorJmp); /* jump to it */
  62. }
  63. else { /* thread has no error handler */
  64. L->status = cast_byte(errcode); /* mark it as dead */
  65. if (G(L)->mainthread->errorJmp) { /* main thread has a handler? */
  66. setobjs2s(L, G(L)->mainthread->top++, L->top - 1); /* copy error obj. */
  67. luaD_throw(G(L)->mainthread, errcode); /* re-throw in main thread */
  68. }
  69. else { /* no handler at all; abort */
  70. if (G(L)->panic) { /* panic function? */
  71. lua_unlock(L);
  72. G(L)->panic(L); /* call it (last chance to jump out) */
  73. }
  74. abort();
  75. }
  76. }
  77. }
  78. int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
  79. unsigned short oldnCcalls = G(L)->nCcalls;
  80. struct lua_longjmp lj;
  81. lj.status = LUA_OK;
  82. lj.previous = L->errorJmp; /* chain new error handler */
  83. L->errorJmp = &lj;
  84. LUAI_TRY(L, &lj,
  85. (*f)(L, ud);
  86. );
  87. L->errorJmp = lj.previous; /* restore old error handler */
  88. G(L)->nCcalls = oldnCcalls;
  89. return lj.status;
  90. }
  91. /* }====================================================== */
  92. static void correctstack (lua_State *L, TValue *oldstack) {
  93. CallInfo *ci;
  94. GCObject *up;
  95. L->top = (L->top - oldstack) + L->stack;
  96. for (up = L->openupval; up != NULL; up = up->gch.next)
  97. gco2uv(up)->v = (gco2uv(up)->v - oldstack) + L->stack;
  98. for (ci = L->ci; ci != NULL; ci = ci->previous) {
  99. ci->top = (ci->top - oldstack) + L->stack;
  100. ci->func = (ci->func - oldstack) + L->stack;
  101. if (isLua(ci))
  102. ci->u.l.base = (ci->u.l.base - oldstack) + L->stack;
  103. }
  104. }
  105. /* some space for error handling */
  106. #define ERRORSTACKSIZE (LUAI_MAXSTACK + 200)
  107. void luaD_reallocstack (lua_State *L, int newsize) {
  108. TValue *oldstack = L->stack;
  109. int lim = L->stacksize;
  110. lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE);
  111. lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK);
  112. luaM_reallocvector(L, L->stack, L->stacksize, newsize, TValue);
  113. for (; lim < newsize; lim++)
  114. setnilvalue(L->stack + lim); /* erase new segment */
  115. L->stacksize = newsize;
  116. L->stack_last = L->stack + newsize - EXTRA_STACK;
  117. correctstack(L, oldstack);
  118. }
  119. void luaD_growstack (lua_State *L, int n) {
  120. int size = L->stacksize;
  121. if (size > LUAI_MAXSTACK) /* error after extra size? */
  122. luaD_throw(L, LUA_ERRERR);
  123. else {
  124. int needed = L->top - L->stack + n + EXTRA_STACK;
  125. int newsize = 2 * size;
  126. if (newsize > LUAI_MAXSTACK) newsize = LUAI_MAXSTACK;
  127. if (newsize < needed) newsize = needed;
  128. if (newsize > LUAI_MAXSTACK) { /* stack overflow? */
  129. luaD_reallocstack(L, ERRORSTACKSIZE);
  130. luaG_runerror(L, "stack overflow");
  131. }
  132. else
  133. luaD_reallocstack(L, newsize);
  134. }
  135. }
  136. static int stackinuse (lua_State *L) {
  137. CallInfo *ci;
  138. StkId lim = L->top;
  139. for (ci = L->ci; ci != NULL; ci = ci->previous) {
  140. lua_assert(ci->top <= L->stack_last);
  141. if (lim < ci->top) lim = ci->top;
  142. }
  143. return cast_int(lim - L->stack) + 1; /* part of stack in use */
  144. }
  145. void luaD_shrinkstack (lua_State *L) {
  146. int inuse = stackinuse(L);
  147. int goodsize = inuse + (inuse / 8) + 2*EXTRA_STACK;
  148. if (goodsize > LUAI_MAXSTACK) goodsize = LUAI_MAXSTACK;
  149. if (inuse > LUAI_MAXSTACK || /* handling stack overflow? */
  150. goodsize >= L->stacksize) /* would grow instead of shrink? */
  151. condmovestack(L); /* don't change stack (change only for debugging) */
  152. else
  153. luaD_reallocstack(L, goodsize); /* shrink it */
  154. }
  155. void luaD_hook (lua_State *L, int event, int line) {
  156. lua_Hook hook = L->hook;
  157. if (hook && L->allowhook) {
  158. CallInfo *ci = L->ci;
  159. ptrdiff_t top = savestack(L, L->top);
  160. ptrdiff_t ci_top = savestack(L, ci->top);
  161. lua_Debug ar;
  162. ar.event = event;
  163. ar.currentline = line;
  164. ar.i_ci = ci;
  165. luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
  166. ci->top = L->top + LUA_MINSTACK;
  167. lua_assert(ci->top <= L->stack_last);
  168. L->allowhook = 0; /* cannot call hooks inside a hook */
  169. ci->callstatus |= CIST_HOOKED;
  170. lua_unlock(L);
  171. (*hook)(L, &ar);
  172. lua_lock(L);
  173. lua_assert(!L->allowhook);
  174. L->allowhook = 1;
  175. ci->top = restorestack(L, ci_top);
  176. L->top = restorestack(L, top);
  177. ci->callstatus &= ~CIST_HOOKED;
  178. }
  179. }
  180. static void callhook (lua_State *L, CallInfo *ci) {
  181. int hook = LUA_HOOKCALL;
  182. ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */
  183. if (isLua(ci->previous) &&
  184. GET_OPCODE(*(ci->previous->u.l.savedpc - 1)) == OP_TAILCALL) {
  185. ci->callstatus |= CIST_TAIL;
  186. hook = LUA_HOOKTAILCALL;
  187. }
  188. luaD_hook(L, hook, -1);
  189. ci->u.l.savedpc--; /* correct 'pc' */
  190. }
  191. static StkId adjust_varargs (lua_State *L, Proto *p, int actual) {
  192. int i;
  193. int nfixargs = p->numparams;
  194. StkId base, fixed;
  195. lua_assert(actual >= nfixargs);
  196. /* move fixed parameters to final position */
  197. fixed = L->top - actual; /* first fixed argument */
  198. base = L->top; /* final position of first argument */
  199. for (i=0; i<nfixargs; i++) {
  200. setobjs2s(L, L->top++, fixed + i);
  201. setnilvalue(fixed + i);
  202. }
  203. return base;
  204. }
  205. static StkId tryfuncTM (lua_State *L, StkId func) {
  206. const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL);
  207. StkId p;
  208. ptrdiff_t funcr = savestack(L, func);
  209. if (!ttisfunction(tm))
  210. luaG_typeerror(L, func, "call");
  211. /* Open a hole inside the stack at `func' */
  212. for (p = L->top; p > func; p--) setobjs2s(L, p, p-1);
  213. incr_top(L);
  214. func = restorestack(L, funcr); /* previous call may change stack */
  215. setobj2s(L, func, tm); /* tag method is the new function to be called */
  216. return func;
  217. }
  218. #define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L)))
  219. /*
  220. ** returns true if function has been executed (C function)
  221. */
  222. int luaD_precall (lua_State *L, StkId func, int nresults) {
  223. LClosure *cl;
  224. ptrdiff_t funcr;
  225. if (!ttisfunction(func)) /* `func' is not a function? */
  226. func = tryfuncTM(L, func); /* check the `function' tag method */
  227. funcr = savestack(L, func);
  228. cl = &clvalue(func)->l;
  229. L->ci->nresults = nresults;
  230. if (!cl->isC) { /* Lua function? prepare its call */
  231. CallInfo *ci;
  232. int nparams, nargs;
  233. StkId base;
  234. Proto *p = cl->p;
  235. luaD_checkstack(L, p->maxstacksize);
  236. func = restorestack(L, funcr);
  237. nargs = cast_int(L->top - func) - 1; /* number of real arguments */
  238. nparams = p->numparams; /* number of expected parameters */
  239. for (; nargs < nparams; nargs++)
  240. setnilvalue(L->top++); /* complete missing arguments */
  241. if (!p->is_vararg) /* no varargs? */
  242. base = func + 1;
  243. else /* vararg function */
  244. base = adjust_varargs(L, p, nargs);
  245. ci = next_ci(L); /* now 'enter' new function */
  246. ci->func = func;
  247. ci->u.l.base = base;
  248. ci->top = base + p->maxstacksize;
  249. lua_assert(ci->top <= L->stack_last);
  250. ci->u.l.savedpc = p->code; /* starting point */
  251. ci->callstatus = CIST_LUA;
  252. L->top = ci->top;
  253. if (L->hookmask & LUA_MASKCALL)
  254. callhook(L, ci);
  255. return 0;
  256. }
  257. else { /* if is a C function, call it */
  258. CallInfo *ci;
  259. int n;
  260. luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
  261. ci = next_ci(L); /* now 'enter' new function */
  262. ci->func = restorestack(L, funcr);
  263. ci->top = L->top + LUA_MINSTACK;
  264. lua_assert(ci->top <= L->stack_last);
  265. ci->callstatus = 0;
  266. if (L->hookmask & LUA_MASKCALL)
  267. luaD_hook(L, LUA_HOOKCALL, -1);
  268. lua_unlock(L);
  269. n = (*curr_func(L)->c.f)(L); /* do the actual call */
  270. lua_lock(L);
  271. luaD_poscall(L, L->top - n);
  272. return 1;
  273. }
  274. }
  275. int luaD_poscall (lua_State *L, StkId firstResult) {
  276. StkId res;
  277. int wanted, i;
  278. CallInfo *ci = L->ci;
  279. if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) {
  280. if (L->hookmask & LUA_MASKRET) {
  281. ptrdiff_t fr = savestack(L, firstResult); /* hook may change stack */
  282. luaD_hook(L, LUA_HOOKRET, -1);
  283. firstResult = restorestack(L, fr);
  284. }
  285. L->oldpc = ci->previous->u.l.savedpc; /* 'oldpc' for caller function */
  286. }
  287. res = ci->func; /* res == final position of 1st result */
  288. L->ci = ci = ci->previous; /* back to caller */
  289. wanted = ci->nresults;
  290. /* move results to correct place */
  291. for (i = wanted; i != 0 && firstResult < L->top; i--)
  292. setobjs2s(L, res++, firstResult++);
  293. while (i-- > 0)
  294. setnilvalue(res++);
  295. L->top = res;
  296. return (wanted - LUA_MULTRET); /* 0 iff wanted == LUA_MULTRET */
  297. }
  298. /*
  299. ** Call a function (C or Lua). The function to be called is at *func.
  300. ** The arguments are on the stack, right after the function.
  301. ** When returns, all the results are on the stack, starting at the original
  302. ** function position.
  303. */
  304. void luaD_call (lua_State *L, StkId func, int nResults, int allowyield) {
  305. global_State *g = G(L);
  306. if (++g->nCcalls >= LUAI_MAXCCALLS) {
  307. if (g->nCcalls == LUAI_MAXCCALLS)
  308. luaG_runerror(L, "C stack overflow");
  309. else if (g->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))
  310. luaD_throw(L, LUA_ERRERR); /* error while handing stack error */
  311. }
  312. if (!allowyield) L->nny++;
  313. if (!luaD_precall(L, func, nResults)) /* is a Lua function? */
  314. luaV_execute(L); /* call it */
  315. if (!allowyield) L->nny--;
  316. g->nCcalls--;
  317. luaC_checkGC(L);
  318. }
  319. static void finishCcall (lua_State *L) {
  320. CallInfo *ci = L->ci;
  321. int n;
  322. lua_assert(ci->u.c.k != NULL); /* must have a continuation */
  323. lua_assert(L->nny == 0);
  324. /* finish 'luaD_call' */
  325. G(L)->nCcalls--;
  326. /* finish 'lua_callk' */
  327. adjustresults(L, ci->nresults);
  328. /* call continuation function */
  329. if (!(ci->callstatus & CIST_STAT)) /* no call status? */
  330. ci->u.c.status = LUA_YIELD; /* 'default' status */
  331. lua_assert(ci->u.c.status != LUA_OK);
  332. ci->callstatus = (ci->callstatus & ~(CIST_YPCALL | CIST_STAT)) | CIST_YIELDED;
  333. lua_unlock(L);
  334. n = (*ci->u.c.k)(L);
  335. lua_lock(L);
  336. /* finish 'luaD_precall' */
  337. luaD_poscall(L, L->top - n);
  338. }
  339. static void unroll (lua_State *L, void *ud) {
  340. UNUSED(ud);
  341. for (;;) {
  342. if (L->ci == &L->base_ci) /* stack is empty? */
  343. return; /* coroutine finished normally */
  344. if (!isLua(L->ci)) /* C function? */
  345. finishCcall(L);
  346. else { /* Lua function */
  347. luaV_finishOp(L); /* finish interrupted instruction */
  348. luaV_execute(L); /* execute down to higher C 'boundary' */
  349. }
  350. }
  351. }
  352. static void resume (lua_State *L, void *ud) {
  353. StkId firstArg = cast(StkId, ud);
  354. CallInfo *ci = L->ci;
  355. if (L->status == LUA_OK) { /* start coroutine? */
  356. lua_assert(ci == &L->base_ci);
  357. if (!luaD_precall(L, firstArg - 1, LUA_MULTRET)) /* Lua function? */
  358. luaV_execute(L); /* call it */
  359. }
  360. else { /* resuming from previous yield */
  361. lua_assert(L->status == LUA_YIELD);
  362. L->status = LUA_OK;
  363. if (isLua(ci)) /* yielded inside a hook? */
  364. luaV_execute(L);
  365. else { /* 'common' yield */
  366. if (ci->u.c.k != NULL) { /* does it have a continuation? */
  367. int n;
  368. ci->u.c.status = LUA_YIELD; /* 'default' status */
  369. ci->callstatus |= CIST_YIELDED;
  370. ci->func = restorestack(L, ci->u.c.oldtop);
  371. lua_unlock(L);
  372. n = (*ci->u.c.k)(L); /* call continuation */
  373. lua_lock(L);
  374. firstArg = L->top - n;
  375. }
  376. G(L)->nCcalls--; /* finish 'luaD_call' */
  377. luaD_poscall(L, firstArg); /* finish 'luaD_precall' */
  378. }
  379. unroll(L, NULL);
  380. }
  381. }
  382. static int resume_error (lua_State *L, const char *msg) {
  383. L->top = L->ci->func + 1;
  384. setsvalue2s(L, L->top, luaS_new(L, msg));
  385. incr_top(L);
  386. lua_unlock(L);
  387. return LUA_ERRRUN;
  388. }
  389. /*
  390. ** check whether thread has a suspended protected call
  391. */
  392. static CallInfo *findpcall (lua_State *L) {
  393. CallInfo *ci;
  394. for (ci = L->ci; ci != NULL; ci = ci->previous) { /* search for a pcall */
  395. if (ci->callstatus & CIST_YPCALL)
  396. return ci;
  397. }
  398. return NULL; /* no pending pcall */
  399. }
  400. static int recover (lua_State *L, int status) {
  401. StkId oldtop;
  402. CallInfo *ci = findpcall(L);
  403. if (ci == NULL) return 0; /* no recovery point */
  404. /* "finish" luaD_pcall */
  405. oldtop = restorestack(L, ci->u.c.oldtop);
  406. luaF_close(L, oldtop);
  407. luaD_seterrorobj(L, status, oldtop);
  408. L->ci = ci;
  409. L->allowhook = ci->u.c.old_allowhook;
  410. L->nny = 0; /* should be zero to be yieldable */
  411. luaD_shrinkstack(L);
  412. L->errfunc = ci->u.c.old_errfunc;
  413. ci->callstatus |= CIST_STAT; /* call has error status */
  414. ci->u.c.status = status; /* (here it is) */
  415. return 1; /* continue running the coroutine */
  416. }
  417. LUA_API int lua_resume (lua_State *L, int nargs) {
  418. int status;
  419. lua_lock(L);
  420. if (L->status != LUA_YIELD) {
  421. if (L->status != LUA_OK)
  422. return resume_error(L, "cannot resume dead coroutine");
  423. else if (L->ci != &L->base_ci)
  424. return resume_error(L, "cannot resume non-suspended coroutine");
  425. }
  426. luai_userstateresume(L, nargs);
  427. if (G(L)->nCcalls >= LUAI_MAXCCALLS)
  428. return resume_error(L, "C stack overflow");
  429. ++G(L)->nCcalls; /* count resume */
  430. L->nny = 0; /* allow yields */
  431. status = luaD_rawrunprotected(L, resume, L->top - nargs);
  432. while (status != LUA_OK && status != LUA_YIELD) { /* error? */
  433. if (recover(L, status)) /* recover point? */
  434. status = luaD_rawrunprotected(L, unroll, NULL); /* run continuation */
  435. else { /* unrecoverable error */
  436. L->status = cast_byte(status); /* mark thread as `dead' */
  437. luaD_seterrorobj(L, status, L->top);
  438. L->ci->top = L->top;
  439. break;
  440. }
  441. }
  442. lua_assert(status == L->status);
  443. L->nny = 1; /* do not allow yields */
  444. --G(L)->nCcalls;
  445. lua_unlock(L);
  446. return status;
  447. }
  448. LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k) {
  449. CallInfo *ci = L->ci;
  450. luai_userstateyield(L, nresults);
  451. lua_lock(L);
  452. if (L->nny > 0)
  453. luaG_runerror(L, "attempt to yield across metamethod/C-call boundary");
  454. L->status = LUA_YIELD;
  455. if (isLua(ci)) { /* inside a hook? */
  456. api_check(L, k == NULL, "hooks cannot continue after yielding");
  457. }
  458. else {
  459. if ((ci->u.c.k = k) != NULL) { /* is there a continuation? */
  460. ci->u.c.ctx = ctx; /* save context */
  461. ci->u.c.oldtop = savestack(L, ci->func); /* save current 'func' */
  462. }
  463. ci->func = L->top - nresults - 1; /* protect stack slots below */
  464. luaD_throw(L, LUA_YIELD);
  465. }
  466. lua_assert(ci->callstatus & CIST_HOOKED); /* must be inside a hook */
  467. lua_unlock(L);
  468. return 0; /* return to 'luaD_hook' */
  469. }
  470. int luaD_pcall (lua_State *L, Pfunc func, void *u,
  471. ptrdiff_t old_top, ptrdiff_t ef) {
  472. int status;
  473. CallInfo *old_ci = L->ci;
  474. lu_byte old_allowhooks = L->allowhook;
  475. unsigned short old_nny = L->nny;
  476. ptrdiff_t old_errfunc = L->errfunc;
  477. L->errfunc = ef;
  478. status = luaD_rawrunprotected(L, func, u);
  479. if (status != LUA_OK) { /* an error occurred? */
  480. StkId oldtop = restorestack(L, old_top);
  481. luaF_close(L, oldtop); /* close possible pending closures */
  482. luaD_seterrorobj(L, status, oldtop);
  483. L->ci = old_ci;
  484. L->allowhook = old_allowhooks;
  485. L->nny = old_nny;
  486. luaD_shrinkstack(L);
  487. }
  488. L->errfunc = old_errfunc;
  489. return status;
  490. }
  491. /*
  492. ** Execute a protected parser.
  493. */
  494. struct SParser { /* data to `f_parser' */
  495. ZIO *z;
  496. Mbuffer buff; /* buffer to be used by the scanner */
  497. Varlist varl; /* list of local variables (to be used by the parser) */
  498. const char *name;
  499. };
  500. static void f_parser (lua_State *L, void *ud) {
  501. int i;
  502. Proto *tf;
  503. Closure *cl;
  504. struct SParser *p = cast(struct SParser *, ud);
  505. int c = luaZ_lookahead(p->z);
  506. tf = (c == LUA_SIGNATURE[0])
  507. ? luaU_undump(L, p->z, &p->buff, p->name)
  508. : luaY_parser(L, p->z, &p->buff, &p->varl, p->name);
  509. setptvalue2s(L, L->top, tf);
  510. incr_top(L);
  511. cl = luaF_newLclosure(L, tf->sizeupvalues, hvalue(&G(L)->l_gt));
  512. cl->l.p = tf;
  513. setclvalue(L, L->top - 1, cl);
  514. for (i = 0; i < tf->sizeupvalues; i++) /* initialize upvalues */
  515. cl->l.upvals[i] = luaF_newupval(L);
  516. }
  517. int luaD_protectedparser (lua_State *L, ZIO *z, const char *name) {
  518. struct SParser p;
  519. int status;
  520. L->nny++; /* cannot yield during parsing */
  521. p.z = z; p.name = name;
  522. p.varl.actvar = NULL; p.varl.nactvar = p.varl.actvarsize = 0;
  523. luaZ_initbuffer(L, &p.buff);
  524. status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc);
  525. luaZ_freebuffer(L, &p.buff);
  526. luaM_freearray(L, p.varl.actvar, p.varl.actvarsize);
  527. L->nny--;
  528. return status;
  529. }