ldo.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /*
  2. ** $Id: ldo.c,v 2.69 2009/10/11 20:02:19 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_callhook (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. if (event == LUA_HOOKTAILRET)
  165. ar.i_ci = NULL; /* tail call; no debug information about it */
  166. else
  167. ar.i_ci = ci;
  168. luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
  169. ci->top = L->top + LUA_MINSTACK;
  170. lua_assert(ci->top <= L->stack_last);
  171. L->allowhook = 0; /* cannot call hooks inside a hook */
  172. ci->callstatus |= CIST_HOOKED;
  173. lua_unlock(L);
  174. (*hook)(L, &ar);
  175. lua_lock(L);
  176. lua_assert(!L->allowhook);
  177. L->allowhook = 1;
  178. ci->top = restorestack(L, ci_top);
  179. L->top = restorestack(L, top);
  180. ci->callstatus &= ~CIST_HOOKED;
  181. }
  182. }
  183. static StkId adjust_varargs (lua_State *L, Proto *p, int actual) {
  184. int i;
  185. int nfixargs = p->numparams;
  186. StkId base, fixed;
  187. lua_assert(actual >= nfixargs);
  188. /* move fixed parameters to final position */
  189. fixed = L->top - actual; /* first fixed argument */
  190. base = L->top; /* final position of first argument */
  191. for (i=0; i<nfixargs; i++) {
  192. setobjs2s(L, L->top++, fixed + i);
  193. setnilvalue(fixed + i);
  194. }
  195. return base;
  196. }
  197. static StkId tryfuncTM (lua_State *L, StkId func) {
  198. const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL);
  199. StkId p;
  200. ptrdiff_t funcr = savestack(L, func);
  201. if (!ttisfunction(tm))
  202. luaG_typeerror(L, func, "call");
  203. /* Open a hole inside the stack at `func' */
  204. for (p = L->top; p > func; p--) setobjs2s(L, p, p-1);
  205. incr_top(L);
  206. func = restorestack(L, funcr); /* previous call may change stack */
  207. setobj2s(L, func, tm); /* tag method is the new function to be called */
  208. return func;
  209. }
  210. #define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L)))
  211. /*
  212. ** returns true if function has been executed (C function)
  213. */
  214. int luaD_precall (lua_State *L, StkId func, int nresults) {
  215. LClosure *cl;
  216. ptrdiff_t funcr;
  217. if (!ttisfunction(func)) /* `func' is not a function? */
  218. func = tryfuncTM(L, func); /* check the `function' tag method */
  219. funcr = savestack(L, func);
  220. cl = &clvalue(func)->l;
  221. L->ci->nresults = nresults;
  222. if (!cl->isC) { /* Lua function? prepare its call */
  223. CallInfo *ci;
  224. int nparams, nargs;
  225. StkId base;
  226. Proto *p = cl->p;
  227. luaD_checkstack(L, p->maxstacksize);
  228. func = restorestack(L, funcr);
  229. nargs = cast_int(L->top - func) - 1; /* number of real arguments */
  230. nparams = p->numparams; /* number of expected parameters */
  231. for (; nargs < nparams; nargs++)
  232. setnilvalue(L->top++); /* complete missing arguments */
  233. if (!p->is_vararg) /* no varargs? */
  234. base = func + 1;
  235. else /* vararg function */
  236. base = adjust_varargs(L, p, nargs);
  237. ci = next_ci(L); /* now 'enter' new function */
  238. ci->func = func;
  239. ci->u.l.base = base;
  240. ci->top = base + p->maxstacksize;
  241. lua_assert(ci->top <= L->stack_last);
  242. ci->u.l.savedpc = p->code; /* starting point */
  243. ci->u.l.tailcalls = 0;
  244. ci->callstatus = CIST_LUA;
  245. L->top = ci->top;
  246. if (L->hookmask & LUA_MASKCALL) {
  247. ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */
  248. luaD_callhook(L, LUA_HOOKCALL, -1);
  249. ci->u.l.savedpc--; /* correct 'pc' */
  250. }
  251. return 0;
  252. }
  253. else { /* if is a C function, call it */
  254. CallInfo *ci;
  255. int n;
  256. luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
  257. ci = next_ci(L); /* now 'enter' new function */
  258. ci->func = restorestack(L, funcr);
  259. ci->top = L->top + LUA_MINSTACK;
  260. lua_assert(ci->top <= L->stack_last);
  261. ci->callstatus = 0;
  262. if (L->hookmask & LUA_MASKCALL)
  263. luaD_callhook(L, LUA_HOOKCALL, -1);
  264. lua_unlock(L);
  265. n = (*curr_func(L)->c.f)(L); /* do the actual call */
  266. lua_lock(L);
  267. luaD_poscall(L, L->top - n);
  268. return 1;
  269. }
  270. }
  271. static StkId callrethooks (lua_State *L, StkId firstResult) {
  272. ptrdiff_t fr = savestack(L, firstResult); /* next call may change stack */
  273. luaD_callhook(L, LUA_HOOKRET, -1);
  274. if (isLua(L->ci)) { /* Lua function? */
  275. while ((L->hookmask & LUA_MASKRET) && L->ci->u.l.tailcalls--)
  276. luaD_callhook(L, LUA_HOOKTAILRET, -1); /* ret. hooks for tail calls */
  277. }
  278. return restorestack(L, fr);
  279. }
  280. int luaD_poscall (lua_State *L, StkId firstResult) {
  281. StkId res;
  282. int wanted, i;
  283. CallInfo *ci = L->ci;
  284. if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) {
  285. if (L->hookmask & LUA_MASKRET)
  286. firstResult = callrethooks(L, firstResult);
  287. L->oldpc = ci->previous->u.l.savedpc; /* 'oldpc' for returning function */
  288. }
  289. res = ci->func; /* res == final position of 1st result */
  290. L->ci = ci = ci->previous; /* back to caller */
  291. wanted = ci->nresults;
  292. /* move results to correct place */
  293. for (i = wanted; i != 0 && firstResult < L->top; i--)
  294. setobjs2s(L, res++, firstResult++);
  295. while (i-- > 0)
  296. setnilvalue(res++);
  297. L->top = res;
  298. return (wanted - LUA_MULTRET); /* 0 iff wanted == LUA_MULTRET */
  299. }
  300. /*
  301. ** Call a function (C or Lua). The function to be called is at *func.
  302. ** The arguments are on the stack, right after the function.
  303. ** When returns, all the results are on the stack, starting at the original
  304. ** function position.
  305. */
  306. void luaD_call (lua_State *L, StkId func, int nResults, int allowyield) {
  307. global_State *g = G(L);
  308. if (++g->nCcalls >= LUAI_MAXCCALLS) {
  309. if (g->nCcalls == LUAI_MAXCCALLS)
  310. luaG_runerror(L, "C stack overflow");
  311. else if (g->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))
  312. luaD_throw(L, LUA_ERRERR); /* error while handing stack error */
  313. }
  314. if (!allowyield) L->nny++;
  315. if (!luaD_precall(L, func, nResults)) /* is a Lua function? */
  316. luaV_execute(L); /* call it */
  317. if (!allowyield) L->nny--;
  318. g->nCcalls--;
  319. luaC_checkGC(L);
  320. }
  321. static void finishCcall (lua_State *L) {
  322. CallInfo *ci = L->ci;
  323. int n;
  324. lua_assert(ci->u.c.k != NULL); /* must have a continuation */
  325. lua_assert(L->nny == 0);
  326. /* finish 'luaD_call' */
  327. G(L)->nCcalls--;
  328. /* finish 'lua_callk' */
  329. adjustresults(L, ci->nresults);
  330. /* call continuation function */
  331. if (!(ci->callstatus & CIST_STAT)) /* no call status? */
  332. ci->u.c.status = LUA_YIELD; /* 'default' status */
  333. lua_assert(ci->u.c.status != LUA_OK);
  334. ci->callstatus = (ci->callstatus & ~(CIST_YPCALL | CIST_STAT)) | CIST_YIELDED;
  335. lua_unlock(L);
  336. n = (*ci->u.c.k)(L);
  337. lua_lock(L);
  338. /* finish 'luaD_precall' */
  339. luaD_poscall(L, L->top - n);
  340. }
  341. static void unroll (lua_State *L, void *ud) {
  342. UNUSED(ud);
  343. for (;;) {
  344. if (L->ci == &L->base_ci) /* stack is empty? */
  345. return; /* coroutine finished normally */
  346. if (!isLua(L->ci)) /* C function? */
  347. finishCcall(L);
  348. else { /* Lua function */
  349. luaV_finishOp(L); /* finish interrupted instruction */
  350. luaV_execute(L); /* execute down to higher C 'boundary' */
  351. }
  352. }
  353. }
  354. static void resume (lua_State *L, void *ud) {
  355. StkId firstArg = cast(StkId, ud);
  356. CallInfo *ci = L->ci;
  357. if (L->status == LUA_OK) { /* start coroutine? */
  358. lua_assert(ci == &L->base_ci);
  359. if (!luaD_precall(L, firstArg - 1, LUA_MULTRET)) /* Lua function? */
  360. luaV_execute(L); /* call it */
  361. }
  362. else { /* resuming from previous yield */
  363. lua_assert(L->status == LUA_YIELD);
  364. L->status = LUA_OK;
  365. if (isLua(ci)) /* yielded inside a hook? */
  366. luaV_execute(L);
  367. else { /* 'common' yield */
  368. if (ci->u.c.k != NULL) { /* does it have a continuation? */
  369. int n;
  370. ci->u.c.status = LUA_YIELD; /* 'default' status */
  371. ci->callstatus |= CIST_YIELDED;
  372. ci->func = restorestack(L, ci->u.c.oldtop);
  373. lua_unlock(L);
  374. n = (*ci->u.c.k)(L); /* call continuation */
  375. lua_lock(L);
  376. firstArg = L->top - n;
  377. }
  378. G(L)->nCcalls--; /* finish 'luaD_call' */
  379. luaD_poscall(L, firstArg); /* finish 'luaD_precall' */
  380. }
  381. unroll(L, NULL);
  382. }
  383. }
  384. static int resume_error (lua_State *L, const char *msg) {
  385. L->top = L->ci->func + 1;
  386. setsvalue2s(L, L->top, luaS_new(L, msg));
  387. incr_top(L);
  388. lua_unlock(L);
  389. return LUA_ERRRUN;
  390. }
  391. /*
  392. ** check whether thread has a suspended protected call
  393. */
  394. static CallInfo *findpcall (lua_State *L) {
  395. CallInfo *ci;
  396. for (ci = L->ci; ci != NULL; ci = ci->previous) { /* search for a pcall */
  397. if (ci->callstatus & CIST_YPCALL)
  398. return ci;
  399. }
  400. return NULL; /* no pending pcall */
  401. }
  402. static int recover (lua_State *L, int status) {
  403. StkId oldtop;
  404. CallInfo *ci = findpcall(L);
  405. if (ci == NULL) return 0; /* no recovery point */
  406. /* "finish" luaD_pcall */
  407. oldtop = restorestack(L, ci->u.c.oldtop);
  408. luaF_close(L, oldtop);
  409. luaD_seterrorobj(L, status, oldtop);
  410. L->ci = ci;
  411. L->allowhook = ci->u.c.old_allowhook;
  412. L->nny = 0; /* should be zero to be yieldable */
  413. luaD_shrinkstack(L);
  414. L->errfunc = ci->u.c.old_errfunc;
  415. ci->callstatus |= CIST_STAT; /* call has error status */
  416. ci->u.c.status = status; /* (here it is) */
  417. return 1; /* continue running the coroutine */
  418. }
  419. LUA_API int lua_resume (lua_State *L, int nargs) {
  420. int status;
  421. lua_lock(L);
  422. if (L->status != LUA_YIELD) {
  423. if (L->status != LUA_OK)
  424. return resume_error(L, "cannot resume dead coroutine");
  425. else if (L->ci != &L->base_ci)
  426. return resume_error(L, "cannot resume non-suspended coroutine");
  427. }
  428. luai_userstateresume(L, nargs);
  429. if (G(L)->nCcalls >= LUAI_MAXCCALLS)
  430. return resume_error(L, "C stack overflow");
  431. ++G(L)->nCcalls; /* count resume */
  432. L->nny = 0; /* allow yields */
  433. status = luaD_rawrunprotected(L, resume, L->top - nargs);
  434. while (status != LUA_OK && status != LUA_YIELD) { /* error? */
  435. if (recover(L, status)) /* recover point? */
  436. status = luaD_rawrunprotected(L, unroll, NULL); /* run continuation */
  437. else { /* unrecoverable error */
  438. L->status = cast_byte(status); /* mark thread as `dead' */
  439. luaD_seterrorobj(L, status, L->top);
  440. L->ci->top = L->top;
  441. break;
  442. }
  443. }
  444. lua_assert(status == L->status);
  445. L->nny = 1; /* do not allow yields */
  446. --G(L)->nCcalls;
  447. lua_unlock(L);
  448. return status;
  449. }
  450. LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k) {
  451. CallInfo *ci = L->ci;
  452. luai_userstateyield(L, nresults);
  453. lua_lock(L);
  454. if (L->nny > 0)
  455. luaG_runerror(L, "attempt to yield across metamethod/C-call boundary");
  456. L->status = LUA_YIELD;
  457. if (isLua(ci)) { /* inside a hook? */
  458. api_check(L, k == NULL, "hooks cannot continue after yielding");
  459. }
  460. else {
  461. if ((ci->u.c.k = k) != NULL) { /* is there a continuation? */
  462. ci->u.c.ctx = ctx; /* save context */
  463. ci->u.c.oldtop = savestack(L, ci->func); /* save current 'func' */
  464. }
  465. ci->func = L->top - nresults - 1; /* protect stack slots below */
  466. luaD_throw(L, LUA_YIELD);
  467. }
  468. lua_assert(ci->callstatus & CIST_HOOKED); /* must be inside a hook */
  469. lua_unlock(L);
  470. return 0; /* otherwise, return to 'luaD_callhook' */
  471. }
  472. int luaD_pcall (lua_State *L, Pfunc func, void *u,
  473. ptrdiff_t old_top, ptrdiff_t ef) {
  474. int status;
  475. CallInfo *old_ci = L->ci;
  476. lu_byte old_allowhooks = L->allowhook;
  477. unsigned short old_nny = L->nny;
  478. ptrdiff_t old_errfunc = L->errfunc;
  479. L->errfunc = ef;
  480. status = luaD_rawrunprotected(L, func, u);
  481. if (status != LUA_OK) { /* an error occurred? */
  482. StkId oldtop = restorestack(L, old_top);
  483. luaF_close(L, oldtop); /* close possible pending closures */
  484. luaD_seterrorobj(L, status, oldtop);
  485. L->ci = old_ci;
  486. L->allowhook = old_allowhooks;
  487. L->nny = old_nny;
  488. luaD_shrinkstack(L);
  489. }
  490. L->errfunc = old_errfunc;
  491. return status;
  492. }
  493. /*
  494. ** Execute a protected parser.
  495. */
  496. struct SParser { /* data to `f_parser' */
  497. ZIO *z;
  498. Mbuffer buff; /* buffer to be used by the scanner */
  499. Varlist varl; /* list of local variables (to be used by the parser) */
  500. const char *name;
  501. };
  502. static void f_parser (lua_State *L, void *ud) {
  503. int i;
  504. Proto *tf;
  505. Closure *cl;
  506. struct SParser *p = cast(struct SParser *, ud);
  507. int c = luaZ_lookahead(p->z);
  508. luaC_checkGC(L);
  509. tf = (c == LUA_SIGNATURE[0])
  510. ? luaU_undump(L, p->z, &p->buff, p->name)
  511. : luaY_parser(L, p->z, &p->buff, &p->varl, p->name);
  512. setptvalue2s(L, L->top, tf);
  513. incr_top(L);
  514. cl = luaF_newLclosure(L, tf->sizeupvalues, hvalue(&G(L)->l_gt));
  515. cl->l.p = tf;
  516. setclvalue(L, L->top - 1, cl);
  517. for (i = 0; i < tf->sizeupvalues; i++) /* initialize upvalues */
  518. cl->l.upvals[i] = luaF_newupval(L);
  519. }
  520. int luaD_protectedparser (lua_State *L, ZIO *z, const char *name) {
  521. struct SParser p;
  522. int status;
  523. p.z = z; p.name = name;
  524. p.varl.actvar = NULL; p.varl.nactvar = p.varl.actvarsize = 0;
  525. luaZ_initbuffer(L, &p.buff);
  526. status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc);
  527. luaZ_freebuffer(L, &p.buff);
  528. luaM_freearray(L, p.varl.actvar, p.varl.actvarsize);
  529. return status;
  530. }