ldo.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. /*
  2. ** $Id: ldo.c $
  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. ** these macros allow user-specific actions when a thread is
  32. ** resumed/yielded.
  33. */
  34. #if !defined(luai_userstateresume)
  35. #define luai_userstateresume(L,n) ((void)L)
  36. #endif
  37. #if !defined(luai_userstateyield)
  38. #define luai_userstateyield(L,n) ((void)L)
  39. #endif
  40. /*
  41. ** {======================================================
  42. ** Error-recovery functions
  43. ** =======================================================
  44. */
  45. /*
  46. ** LUAI_THROW/LUAI_TRY define how Lua does exception handling. By
  47. ** default, Lua handles errors with exceptions when compiling as
  48. ** C++ code, with _longjmp/_setjmp when asked to use them, and with
  49. ** longjmp/setjmp otherwise.
  50. */
  51. #if !defined(LUAI_THROW) /* { */
  52. #if defined(__cplusplus) && !defined(LUA_USE_LONGJMP) /* { */
  53. /* C++ exceptions */
  54. #define LUAI_THROW(L,c) throw(c)
  55. #define LUAI_TRY(L,c,f,ud) \
  56. try { (f)(L, ud); } catch(...) { if ((c)->status == 0) (c)->status = -1; }
  57. #define luai_jmpbuf int /* dummy field */
  58. #elif defined(LUA_USE_POSIX) /* }{ */
  59. /* in POSIX, try _longjmp/_setjmp (more efficient) */
  60. #define LUAI_THROW(L,c) _longjmp((c)->b, 1)
  61. #define LUAI_TRY(L,c,f,ud) if (_setjmp((c)->b) == 0) ((f)(L, ud))
  62. #define luai_jmpbuf jmp_buf
  63. #else /* }{ */
  64. /* ISO C handling with long jumps */
  65. #define LUAI_THROW(L,c) longjmp((c)->b, 1)
  66. #define LUAI_TRY(L,c,f,ud) if (setjmp((c)->b) == 0) ((f)(L, ud))
  67. #define luai_jmpbuf jmp_buf
  68. #endif /* } */
  69. #endif /* } */
  70. /* chain list of long jump buffers */
  71. struct lua_longjmp {
  72. struct lua_longjmp *previous;
  73. luai_jmpbuf b;
  74. volatile TStatus status; /* error code */
  75. };
  76. void luaD_seterrorobj (lua_State *L, TStatus errcode, StkId oldtop) {
  77. switch (errcode) {
  78. case LUA_ERRMEM: { /* memory error? */
  79. setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */
  80. break;
  81. }
  82. case LUA_ERRERR: {
  83. setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling"));
  84. break;
  85. }
  86. case LUA_OK: { /* special case only for closing upvalues */
  87. setnilvalue(s2v(oldtop)); /* no error message */
  88. break;
  89. }
  90. default: {
  91. lua_assert(errorstatus(errcode)); /* real error */
  92. setobjs2s(L, oldtop, L->top.p - 1); /* error message on current top */
  93. break;
  94. }
  95. }
  96. L->top.p = oldtop + 1;
  97. }
  98. l_noret luaD_throw (lua_State *L, TStatus errcode) {
  99. if (L->errorJmp) { /* thread has an error handler? */
  100. L->errorJmp->status = errcode; /* set status */
  101. LUAI_THROW(L, L->errorJmp); /* jump to it */
  102. }
  103. else { /* thread has no error handler */
  104. global_State *g = G(L);
  105. lua_State *mainth = mainthread(g);
  106. errcode = luaE_resetthread(L, errcode); /* close all upvalues */
  107. L->status = errcode;
  108. if (mainth->errorJmp) { /* main thread has a handler? */
  109. setobjs2s(L, mainth->top.p++, L->top.p - 1); /* copy error obj. */
  110. luaD_throw(mainth, errcode); /* re-throw in main thread */
  111. }
  112. else { /* no handler at all; abort */
  113. if (g->panic) { /* panic function? */
  114. lua_unlock(L);
  115. g->panic(L); /* call panic function (last chance to jump out) */
  116. }
  117. abort();
  118. }
  119. }
  120. }
  121. TStatus luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
  122. l_uint32 oldnCcalls = L->nCcalls;
  123. struct lua_longjmp lj;
  124. lj.status = LUA_OK;
  125. lj.previous = L->errorJmp; /* chain new error handler */
  126. L->errorJmp = &lj;
  127. LUAI_TRY(L, &lj, f, ud); /* call 'f' catching errors */
  128. L->errorJmp = lj.previous; /* restore old error handler */
  129. L->nCcalls = oldnCcalls;
  130. return lj.status;
  131. }
  132. /* }====================================================== */
  133. /*
  134. ** {==================================================================
  135. ** Stack reallocation
  136. ** ===================================================================
  137. */
  138. /* some stack space for error handling */
  139. #define STACKERRSPACE 200
  140. /* maximum stack size that respects size_t */
  141. #define MAXSTACK_BYSIZET ((MAX_SIZET / sizeof(StackValue)) - STACKERRSPACE)
  142. /*
  143. ** Minimum between LUAI_MAXSTACK and MAXSTACK_BYSIZET
  144. ** (Maximum size for the stack must respect size_t.)
  145. */
  146. #define MAXSTACK cast_int(LUAI_MAXSTACK < MAXSTACK_BYSIZET \
  147. ? LUAI_MAXSTACK : MAXSTACK_BYSIZET)
  148. /* stack size with extra space for error handling */
  149. #define ERRORSTACKSIZE (MAXSTACK + STACKERRSPACE)
  150. /*
  151. ** In ISO C, any pointer use after the pointer has been deallocated is
  152. ** undefined behavior. So, before a stack reallocation, all pointers
  153. ** should be changed to offsets, and after the reallocation they should
  154. ** be changed back to pointers. As during the reallocation the pointers
  155. ** are invalid, the reallocation cannot run emergency collections.
  156. ** Alternatively, we can use the old address after the deallocation.
  157. ** That is not strict ISO C, but seems to work fine everywhere.
  158. ** The following macro chooses how strict is the code.
  159. */
  160. #if !defined(LUAI_STRICT_ADDRESS)
  161. #define LUAI_STRICT_ADDRESS 0
  162. #endif
  163. #if LUAI_STRICT_ADDRESS
  164. /*
  165. ** Change all pointers to the stack into offsets.
  166. */
  167. static void relstack (lua_State *L) {
  168. CallInfo *ci;
  169. UpVal *up;
  170. L->top.offset = savestack(L, L->top.p);
  171. L->tbclist.offset = savestack(L, L->tbclist.p);
  172. for (up = L->openupval; up != NULL; up = up->u.open.next)
  173. up->v.offset = savestack(L, uplevel(up));
  174. for (ci = L->ci; ci != NULL; ci = ci->previous) {
  175. ci->top.offset = savestack(L, ci->top.p);
  176. ci->func.offset = savestack(L, ci->func.p);
  177. }
  178. }
  179. /*
  180. ** Change back all offsets into pointers.
  181. */
  182. static void correctstack (lua_State *L, StkId oldstack) {
  183. CallInfo *ci;
  184. UpVal *up;
  185. UNUSED(oldstack);
  186. L->top.p = restorestack(L, L->top.offset);
  187. L->tbclist.p = restorestack(L, L->tbclist.offset);
  188. for (up = L->openupval; up != NULL; up = up->u.open.next)
  189. up->v.p = s2v(restorestack(L, up->v.offset));
  190. for (ci = L->ci; ci != NULL; ci = ci->previous) {
  191. ci->top.p = restorestack(L, ci->top.offset);
  192. ci->func.p = restorestack(L, ci->func.offset);
  193. if (isLua(ci))
  194. ci->u.l.trap = 1; /* signal to update 'trap' in 'luaV_execute' */
  195. }
  196. }
  197. #else
  198. /*
  199. ** Assume that it is fine to use an address after its deallocation,
  200. ** as long as we do not dereference it.
  201. */
  202. static void relstack (lua_State *L) { UNUSED(L); } /* do nothing */
  203. /*
  204. ** Correct pointers into 'oldstack' to point into 'L->stack'.
  205. */
  206. static void correctstack (lua_State *L, StkId oldstack) {
  207. CallInfo *ci;
  208. UpVal *up;
  209. StkId newstack = L->stack.p;
  210. if (oldstack == newstack)
  211. return;
  212. L->top.p = L->top.p - oldstack + newstack;
  213. L->tbclist.p = L->tbclist.p - oldstack + newstack;
  214. for (up = L->openupval; up != NULL; up = up->u.open.next)
  215. up->v.p = s2v(uplevel(up) - oldstack + newstack);
  216. for (ci = L->ci; ci != NULL; ci = ci->previous) {
  217. ci->top.p = ci->top.p - oldstack + newstack;
  218. ci->func.p = ci->func.p - oldstack + newstack;
  219. if (isLua(ci))
  220. ci->u.l.trap = 1; /* signal to update 'trap' in 'luaV_execute' */
  221. }
  222. }
  223. #endif
  224. /*
  225. ** Reallocate the stack to a new size, correcting all pointers into it.
  226. ** In case of allocation error, raise an error or return false according
  227. ** to 'raiseerror'.
  228. */
  229. int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) {
  230. int oldsize = stacksize(L);
  231. int i;
  232. StkId newstack;
  233. StkId oldstack = L->stack.p;
  234. lu_byte oldgcstop = G(L)->gcstopem;
  235. lua_assert(newsize <= MAXSTACK || newsize == ERRORSTACKSIZE);
  236. relstack(L); /* change pointers to offsets */
  237. G(L)->gcstopem = 1; /* stop emergency collection */
  238. newstack = luaM_reallocvector(L, oldstack, oldsize + EXTRA_STACK,
  239. newsize + EXTRA_STACK, StackValue);
  240. G(L)->gcstopem = oldgcstop; /* restore emergency collection */
  241. if (l_unlikely(newstack == NULL)) { /* reallocation failed? */
  242. correctstack(L, oldstack); /* change offsets back to pointers */
  243. if (raiseerror)
  244. luaM_error(L);
  245. else return 0; /* do not raise an error */
  246. }
  247. L->stack.p = newstack;
  248. correctstack(L, oldstack); /* change offsets back to pointers */
  249. L->stack_last.p = L->stack.p + newsize;
  250. for (i = oldsize + EXTRA_STACK; i < newsize + EXTRA_STACK; i++)
  251. setnilvalue(s2v(newstack + i)); /* erase new segment */
  252. return 1;
  253. }
  254. /*
  255. ** Try to grow the stack by at least 'n' elements. When 'raiseerror'
  256. ** is true, raises any error; otherwise, return 0 in case of errors.
  257. */
  258. int luaD_growstack (lua_State *L, int n, int raiseerror) {
  259. int size = stacksize(L);
  260. if (l_unlikely(size > MAXSTACK)) {
  261. /* if stack is larger than maximum, thread is already using the
  262. extra space reserved for errors, that is, thread is handling
  263. a stack error; cannot grow further than that. */
  264. lua_assert(stacksize(L) == ERRORSTACKSIZE);
  265. if (raiseerror)
  266. luaD_throw(L, LUA_ERRERR); /* error inside message handler */
  267. return 0; /* if not 'raiseerror', just signal it */
  268. }
  269. else if (n < MAXSTACK) { /* avoids arithmetic overflows */
  270. int newsize = 2 * size; /* tentative new size */
  271. int needed = cast_int(L->top.p - L->stack.p) + n;
  272. if (newsize > MAXSTACK) /* cannot cross the limit */
  273. newsize = MAXSTACK;
  274. if (newsize < needed) /* but must respect what was asked for */
  275. newsize = needed;
  276. if (l_likely(newsize <= MAXSTACK))
  277. return luaD_reallocstack(L, newsize, raiseerror);
  278. }
  279. /* else stack overflow */
  280. /* add extra size to be able to handle the error message */
  281. luaD_reallocstack(L, ERRORSTACKSIZE, raiseerror);
  282. if (raiseerror)
  283. luaG_runerror(L, "stack overflow");
  284. return 0;
  285. }
  286. /*
  287. ** Compute how much of the stack is being used, by computing the
  288. ** maximum top of all call frames in the stack and the current top.
  289. */
  290. static int stackinuse (lua_State *L) {
  291. CallInfo *ci;
  292. int res;
  293. StkId lim = L->top.p;
  294. for (ci = L->ci; ci != NULL; ci = ci->previous) {
  295. if (lim < ci->top.p) lim = ci->top.p;
  296. }
  297. lua_assert(lim <= L->stack_last.p + EXTRA_STACK);
  298. res = cast_int(lim - L->stack.p) + 1; /* part of stack in use */
  299. if (res < LUA_MINSTACK)
  300. res = LUA_MINSTACK; /* ensure a minimum size */
  301. return res;
  302. }
  303. /*
  304. ** If stack size is more than 3 times the current use, reduce that size
  305. ** to twice the current use. (So, the final stack size is at most 2/3 the
  306. ** previous size, and half of its entries are empty.)
  307. ** As a particular case, if stack was handling a stack overflow and now
  308. ** it is not, 'max' (limited by MAXSTACK) will be smaller than
  309. ** stacksize (equal to ERRORSTACKSIZE in this case), and so the stack
  310. ** will be reduced to a "regular" size.
  311. */
  312. void luaD_shrinkstack (lua_State *L) {
  313. int inuse = stackinuse(L);
  314. int max = (inuse > MAXSTACK / 3) ? MAXSTACK : inuse * 3;
  315. /* if thread is currently not handling a stack overflow and its
  316. size is larger than maximum "reasonable" size, shrink it */
  317. if (inuse <= MAXSTACK && stacksize(L) > max) {
  318. int nsize = (inuse > MAXSTACK / 2) ? MAXSTACK : inuse * 2;
  319. luaD_reallocstack(L, nsize, 0); /* ok if that fails */
  320. }
  321. else /* don't change stack */
  322. condmovestack(L,(void)0,(void)0); /* (change only for debugging) */
  323. luaE_shrinkCI(L); /* shrink CI list */
  324. }
  325. void luaD_inctop (lua_State *L) {
  326. L->top.p++;
  327. luaD_checkstack(L, 1);
  328. }
  329. /* }================================================================== */
  330. /*
  331. ** Call a hook for the given event. Make sure there is a hook to be
  332. ** called. (Both 'L->hook' and 'L->hookmask', which trigger this
  333. ** function, can be changed asynchronously by signals.)
  334. */
  335. void luaD_hook (lua_State *L, int event, int line,
  336. int ftransfer, int ntransfer) {
  337. lua_Hook hook = L->hook;
  338. if (hook && L->allowhook) { /* make sure there is a hook */
  339. CallInfo *ci = L->ci;
  340. ptrdiff_t top = savestack(L, L->top.p); /* preserve original 'top' */
  341. ptrdiff_t ci_top = savestack(L, ci->top.p); /* idem for 'ci->top' */
  342. lua_Debug ar;
  343. ar.event = event;
  344. ar.currentline = line;
  345. ar.i_ci = ci;
  346. L->transferinfo.ftransfer = ftransfer;
  347. L->transferinfo.ntransfer = ntransfer;
  348. if (isLua(ci) && L->top.p < ci->top.p)
  349. L->top.p = ci->top.p; /* protect entire activation register */
  350. luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
  351. if (ci->top.p < L->top.p + LUA_MINSTACK)
  352. ci->top.p = L->top.p + LUA_MINSTACK;
  353. L->allowhook = 0; /* cannot call hooks inside a hook */
  354. ci->callstatus |= CIST_HOOKED;
  355. lua_unlock(L);
  356. (*hook)(L, &ar);
  357. lua_lock(L);
  358. lua_assert(!L->allowhook);
  359. L->allowhook = 1;
  360. ci->top.p = restorestack(L, ci_top);
  361. L->top.p = restorestack(L, top);
  362. ci->callstatus &= ~CIST_HOOKED;
  363. }
  364. }
  365. /*
  366. ** Executes a call hook for Lua functions. This function is called
  367. ** whenever 'hookmask' is not zero, so it checks whether call hooks are
  368. ** active.
  369. */
  370. void luaD_hookcall (lua_State *L, CallInfo *ci) {
  371. L->oldpc = 0; /* set 'oldpc' for new function */
  372. if (L->hookmask & LUA_MASKCALL) { /* is call hook on? */
  373. int event = (ci->callstatus & CIST_TAIL) ? LUA_HOOKTAILCALL
  374. : LUA_HOOKCALL;
  375. Proto *p = ci_func(ci)->p;
  376. ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */
  377. luaD_hook(L, event, -1, 1, p->numparams);
  378. ci->u.l.savedpc--; /* correct 'pc' */
  379. }
  380. }
  381. /*
  382. ** Executes a return hook for Lua and C functions and sets/corrects
  383. ** 'oldpc'. (Note that this correction is needed by the line hook, so it
  384. ** is done even when return hooks are off.)
  385. */
  386. static void rethook (lua_State *L, CallInfo *ci, int nres) {
  387. if (L->hookmask & LUA_MASKRET) { /* is return hook on? */
  388. StkId firstres = L->top.p - nres; /* index of first result */
  389. int delta = 0; /* correction for vararg functions */
  390. int ftransfer;
  391. if (isLua(ci)) {
  392. Proto *p = ci_func(ci)->p;
  393. if (p->flag & PF_ISVARARG)
  394. delta = ci->u.l.nextraargs + p->numparams + 1;
  395. }
  396. ci->func.p += delta; /* if vararg, back to virtual 'func' */
  397. ftransfer = cast_int(firstres - ci->func.p);
  398. luaD_hook(L, LUA_HOOKRET, -1, ftransfer, nres); /* call it */
  399. ci->func.p -= delta;
  400. }
  401. if (isLua(ci = ci->previous))
  402. L->oldpc = pcRel(ci->u.l.savedpc, ci_func(ci)->p); /* set 'oldpc' */
  403. }
  404. /*
  405. ** Check whether 'func' has a '__call' metafield. If so, put it in the
  406. ** stack, below original 'func', so that 'luaD_precall' can call it.
  407. ** Raise an error if there is no '__call' metafield.
  408. ** Bits CIST_CCMT in status count how many _call metamethods were
  409. ** invoked and how many corresponding extra arguments were pushed.
  410. ** (This count will be saved in the 'callstatus' of the call).
  411. ** Raise an error if this counter overflows.
  412. */
  413. static unsigned tryfuncTM (lua_State *L, StkId func, unsigned status) {
  414. const TValue *tm;
  415. StkId p;
  416. tm = luaT_gettmbyobj(L, s2v(func), TM_CALL);
  417. if (l_unlikely(ttisnil(tm))) /* no metamethod? */
  418. luaG_callerror(L, s2v(func));
  419. for (p = L->top.p; p > func; p--) /* open space for metamethod */
  420. setobjs2s(L, p, p-1);
  421. L->top.p++; /* stack space pre-allocated by the caller */
  422. setobj2s(L, func, tm); /* metamethod is the new function to be called */
  423. if ((status & MAX_CCMT) == MAX_CCMT) /* is counter full? */
  424. luaG_runerror(L, "'__call' chain too long");
  425. return status + (1u << CIST_CCMT); /* increment counter */
  426. }
  427. /* Generic case for 'moveresult' */
  428. l_sinline void genmoveresults (lua_State *L, StkId res, int nres,
  429. int wanted) {
  430. StkId firstresult = L->top.p - nres; /* index of first result */
  431. int i;
  432. if (nres > wanted) /* extra results? */
  433. nres = wanted; /* don't need them */
  434. for (i = 0; i < nres; i++) /* move all results to correct place */
  435. setobjs2s(L, res + i, firstresult + i);
  436. for (; i < wanted; i++) /* complete wanted number of results */
  437. setnilvalue(s2v(res + i));
  438. L->top.p = res + wanted; /* top points after the last result */
  439. }
  440. /*
  441. ** Given 'nres' results at 'firstResult', move 'fwanted-1' of them
  442. ** to 'res'. Handle most typical cases (zero results for commands,
  443. ** one result for expressions, multiple results for tail calls/single
  444. ** parameters) separated. The flag CIST_TBC in 'fwanted', if set,
  445. ** forces the swicth to go to the default case.
  446. */
  447. l_sinline void moveresults (lua_State *L, StkId res, int nres,
  448. l_uint32 fwanted) {
  449. switch (fwanted) { /* handle typical cases separately */
  450. case 0 + 1: /* no values needed */
  451. L->top.p = res;
  452. return;
  453. case 1 + 1: /* one value needed */
  454. if (nres == 0) /* no results? */
  455. setnilvalue(s2v(res)); /* adjust with nil */
  456. else /* at least one result */
  457. setobjs2s(L, res, L->top.p - nres); /* move it to proper place */
  458. L->top.p = res + 1;
  459. return;
  460. case LUA_MULTRET + 1:
  461. genmoveresults(L, res, nres, nres); /* we want all results */
  462. break;
  463. default: { /* two/more results and/or to-be-closed variables */
  464. int wanted = get_nresults(fwanted);
  465. if (fwanted & CIST_TBC) { /* to-be-closed variables? */
  466. L->ci->u2.nres = nres;
  467. L->ci->callstatus |= CIST_CLSRET; /* in case of yields */
  468. res = luaF_close(L, res, CLOSEKTOP, 1);
  469. L->ci->callstatus &= ~CIST_CLSRET;
  470. if (L->hookmask) { /* if needed, call hook after '__close's */
  471. ptrdiff_t savedres = savestack(L, res);
  472. rethook(L, L->ci, nres);
  473. res = restorestack(L, savedres); /* hook can move stack */
  474. }
  475. if (wanted == LUA_MULTRET)
  476. wanted = nres; /* we want all results */
  477. }
  478. genmoveresults(L, res, nres, wanted);
  479. break;
  480. }
  481. }
  482. }
  483. /*
  484. ** Finishes a function call: calls hook if necessary, moves current
  485. ** number of results to proper place, and returns to previous call
  486. ** info. If function has to close variables, hook must be called after
  487. ** that.
  488. */
  489. void luaD_poscall (lua_State *L, CallInfo *ci, int nres) {
  490. l_uint32 fwanted = ci->callstatus & (CIST_TBC | CIST_NRESULTS);
  491. if (l_unlikely(L->hookmask) && !(fwanted & CIST_TBC))
  492. rethook(L, ci, nres);
  493. /* move results to proper place */
  494. moveresults(L, ci->func.p, nres, fwanted);
  495. /* function cannot be in any of these cases when returning */
  496. lua_assert(!(ci->callstatus &
  497. (CIST_HOOKED | CIST_YPCALL | CIST_FIN | CIST_CLSRET)));
  498. L->ci = ci->previous; /* back to caller (after closing variables) */
  499. }
  500. #define next_ci(L) (L->ci->next ? L->ci->next : luaE_extendCI(L))
  501. /*
  502. ** Allocate and initialize CallInfo structure. At this point, the
  503. ** only valid fields in the call status are number of results,
  504. ** CIST_C (if it's a C function), and number of extra arguments.
  505. ** (All these bit-fields fit in 16-bit values.)
  506. */
  507. l_sinline CallInfo *prepCallInfo (lua_State *L, StkId func, unsigned status,
  508. StkId top) {
  509. CallInfo *ci = L->ci = next_ci(L); /* new frame */
  510. ci->func.p = func;
  511. lua_assert((status & ~(CIST_NRESULTS | CIST_C | MAX_CCMT)) == 0);
  512. ci->callstatus = status;
  513. ci->top.p = top;
  514. return ci;
  515. }
  516. /*
  517. ** precall for C functions
  518. */
  519. l_sinline int precallC (lua_State *L, StkId func, unsigned status,
  520. lua_CFunction f) {
  521. int n; /* number of returns */
  522. CallInfo *ci;
  523. checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */
  524. L->ci = ci = prepCallInfo(L, func, status | CIST_C,
  525. L->top.p + LUA_MINSTACK);
  526. lua_assert(ci->top.p <= L->stack_last.p);
  527. if (l_unlikely(L->hookmask & LUA_MASKCALL)) {
  528. int narg = cast_int(L->top.p - func) - 1;
  529. luaD_hook(L, LUA_HOOKCALL, -1, 1, narg);
  530. }
  531. lua_unlock(L);
  532. n = (*f)(L); /* do the actual call */
  533. lua_lock(L);
  534. api_checknelems(L, n);
  535. luaD_poscall(L, ci, n);
  536. return n;
  537. }
  538. /*
  539. ** Prepare a function for a tail call, building its call info on top
  540. ** of the current call info. 'narg1' is the number of arguments plus 1
  541. ** (so that it includes the function itself). Return the number of
  542. ** results, if it was a C function, or -1 for a Lua function.
  543. */
  544. int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func,
  545. int narg1, int delta) {
  546. unsigned status = LUA_MULTRET + 1;
  547. retry:
  548. switch (ttypetag(s2v(func))) {
  549. case LUA_VCCL: /* C closure */
  550. return precallC(L, func, status, clCvalue(s2v(func))->f);
  551. case LUA_VLCF: /* light C function */
  552. return precallC(L, func, status, fvalue(s2v(func)));
  553. case LUA_VLCL: { /* Lua function */
  554. Proto *p = clLvalue(s2v(func))->p;
  555. int fsize = p->maxstacksize; /* frame size */
  556. int nfixparams = p->numparams;
  557. int i;
  558. checkstackp(L, fsize - delta, func);
  559. ci->func.p -= delta; /* restore 'func' (if vararg) */
  560. for (i = 0; i < narg1; i++) /* move down function and arguments */
  561. setobjs2s(L, ci->func.p + i, func + i);
  562. func = ci->func.p; /* moved-down function */
  563. for (; narg1 <= nfixparams; narg1++)
  564. setnilvalue(s2v(func + narg1)); /* complete missing arguments */
  565. ci->top.p = func + 1 + fsize; /* top for new function */
  566. lua_assert(ci->top.p <= L->stack_last.p);
  567. ci->u.l.savedpc = p->code; /* starting point */
  568. ci->callstatus |= CIST_TAIL;
  569. L->top.p = func + narg1; /* set top */
  570. return -1;
  571. }
  572. default: { /* not a function */
  573. checkstackp(L, 1, func); /* space for metamethod */
  574. status = tryfuncTM(L, func, status); /* try '__call' metamethod */
  575. narg1++;
  576. goto retry; /* try again */
  577. }
  578. }
  579. }
  580. /*
  581. ** Prepares the call to a function (C or Lua). For C functions, also do
  582. ** the call. The function to be called is at '*func'. The arguments
  583. ** are on the stack, right after the function. Returns the CallInfo
  584. ** to be executed, if it was a Lua function. Otherwise (a C function)
  585. ** returns NULL, with all the results on the stack, starting at the
  586. ** original function position.
  587. */
  588. CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) {
  589. unsigned status = cast_uint(nresults + 1);
  590. lua_assert(status <= MAXRESULTS + 1);
  591. retry:
  592. switch (ttypetag(s2v(func))) {
  593. case LUA_VCCL: /* C closure */
  594. precallC(L, func, status, clCvalue(s2v(func))->f);
  595. return NULL;
  596. case LUA_VLCF: /* light C function */
  597. precallC(L, func, status, fvalue(s2v(func)));
  598. return NULL;
  599. case LUA_VLCL: { /* Lua function */
  600. CallInfo *ci;
  601. Proto *p = clLvalue(s2v(func))->p;
  602. int narg = cast_int(L->top.p - func) - 1; /* number of real arguments */
  603. int nfixparams = p->numparams;
  604. int fsize = p->maxstacksize; /* frame size */
  605. checkstackp(L, fsize, func);
  606. L->ci = ci = prepCallInfo(L, func, status, func + 1 + fsize);
  607. ci->u.l.savedpc = p->code; /* starting point */
  608. for (; narg < nfixparams; narg++)
  609. setnilvalue(s2v(L->top.p++)); /* complete missing arguments */
  610. lua_assert(ci->top.p <= L->stack_last.p);
  611. return ci;
  612. }
  613. default: { /* not a function */
  614. checkstackp(L, 1, func); /* space for metamethod */
  615. status = tryfuncTM(L, func, status); /* try '__call' metamethod */
  616. goto retry; /* try again with metamethod */
  617. }
  618. }
  619. }
  620. /*
  621. ** Call a function (C or Lua) through C. 'inc' can be 1 (increment
  622. ** number of recursive invocations in the C stack) or nyci (the same
  623. ** plus increment number of non-yieldable calls).
  624. ** This function can be called with some use of EXTRA_STACK, so it should
  625. ** check the stack before doing anything else. 'luaD_precall' already
  626. ** does that.
  627. */
  628. l_sinline void ccall (lua_State *L, StkId func, int nResults, l_uint32 inc) {
  629. CallInfo *ci;
  630. L->nCcalls += inc;
  631. if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) {
  632. checkstackp(L, 0, func); /* free any use of EXTRA_STACK */
  633. luaE_checkcstack(L);
  634. }
  635. if ((ci = luaD_precall(L, func, nResults)) != NULL) { /* Lua function? */
  636. ci->callstatus |= CIST_FRESH; /* mark that it is a "fresh" execute */
  637. luaV_execute(L, ci); /* call it */
  638. }
  639. L->nCcalls -= inc;
  640. }
  641. /*
  642. ** External interface for 'ccall'
  643. */
  644. void luaD_call (lua_State *L, StkId func, int nResults) {
  645. ccall(L, func, nResults, 1);
  646. }
  647. /*
  648. ** Similar to 'luaD_call', but does not allow yields during the call.
  649. */
  650. void luaD_callnoyield (lua_State *L, StkId func, int nResults) {
  651. ccall(L, func, nResults, nyci);
  652. }
  653. /*
  654. ** Finish the job of 'lua_pcallk' after it was interrupted by an yield.
  655. ** (The caller, 'finishCcall', does the final call to 'adjustresults'.)
  656. ** The main job is to complete the 'luaD_pcall' called by 'lua_pcallk'.
  657. ** If a '__close' method yields here, eventually control will be back
  658. ** to 'finishCcall' (when that '__close' method finally returns) and
  659. ** 'finishpcallk' will run again and close any still pending '__close'
  660. ** methods. Similarly, if a '__close' method errs, 'precover' calls
  661. ** 'unroll' which calls ''finishCcall' and we are back here again, to
  662. ** close any pending '__close' methods.
  663. ** Note that, up to the call to 'luaF_close', the corresponding
  664. ** 'CallInfo' is not modified, so that this repeated run works like the
  665. ** first one (except that it has at least one less '__close' to do). In
  666. ** particular, field CIST_RECST preserves the error status across these
  667. ** multiple runs, changing only if there is a new error.
  668. */
  669. static TStatus finishpcallk (lua_State *L, CallInfo *ci) {
  670. TStatus status = getcistrecst(ci); /* get original status */
  671. if (l_likely(status == LUA_OK)) /* no error? */
  672. status = LUA_YIELD; /* was interrupted by an yield */
  673. else { /* error */
  674. StkId func = restorestack(L, ci->u2.funcidx);
  675. L->allowhook = getoah(ci); /* restore 'allowhook' */
  676. func = luaF_close(L, func, status, 1); /* can yield or raise an error */
  677. luaD_seterrorobj(L, status, func);
  678. luaD_shrinkstack(L); /* restore stack size in case of overflow */
  679. setcistrecst(ci, LUA_OK); /* clear original status */
  680. }
  681. ci->callstatus &= ~CIST_YPCALL;
  682. L->errfunc = ci->u.c.old_errfunc;
  683. /* if it is here, there were errors or yields; unlike 'lua_pcallk',
  684. do not change status */
  685. return status;
  686. }
  687. /*
  688. ** Completes the execution of a C function interrupted by an yield.
  689. ** The interruption must have happened while the function was either
  690. ** closing its tbc variables in 'moveresults' or executing
  691. ** 'lua_callk'/'lua_pcallk'. In the first case, it just redoes
  692. ** 'luaD_poscall'. In the second case, the call to 'finishpcallk'
  693. ** finishes the interrupted execution of 'lua_pcallk'. After that, it
  694. ** calls the continuation of the interrupted function and finally it
  695. ** completes the job of the 'luaD_call' that called the function. In
  696. ** the call to 'adjustresults', we do not know the number of results
  697. ** of the function called by 'lua_callk'/'lua_pcallk', so we are
  698. ** conservative and use LUA_MULTRET (always adjust).
  699. */
  700. static void finishCcall (lua_State *L, CallInfo *ci) {
  701. int n; /* actual number of results from C function */
  702. if (ci->callstatus & CIST_CLSRET) { /* was closing TBC variable? */
  703. lua_assert(ci->callstatus & CIST_TBC);
  704. n = ci->u2.nres; /* just redo 'luaD_poscall' */
  705. /* don't need to reset CIST_CLSRET, as it will be set again anyway */
  706. }
  707. else {
  708. TStatus status = LUA_YIELD; /* default if there were no errors */
  709. lua_KFunction kf = ci->u.c.k; /* continuation function */
  710. /* must have a continuation and must be able to call it */
  711. lua_assert(kf != NULL && yieldable(L));
  712. if (ci->callstatus & CIST_YPCALL) /* was inside a 'lua_pcallk'? */
  713. status = finishpcallk(L, ci); /* finish it */
  714. adjustresults(L, LUA_MULTRET); /* finish 'lua_callk' */
  715. lua_unlock(L);
  716. n = (*kf)(L, APIstatus(status), ci->u.c.ctx); /* call continuation */
  717. lua_lock(L);
  718. api_checknelems(L, n);
  719. }
  720. luaD_poscall(L, ci, n); /* finish 'luaD_call' */
  721. }
  722. /*
  723. ** Executes "full continuation" (everything in the stack) of a
  724. ** previously interrupted coroutine until the stack is empty (or another
  725. ** interruption long-jumps out of the loop).
  726. */
  727. static void unroll (lua_State *L, void *ud) {
  728. CallInfo *ci;
  729. UNUSED(ud);
  730. while ((ci = L->ci) != &L->base_ci) { /* something in the stack */
  731. if (!isLua(ci)) /* C function? */
  732. finishCcall(L, ci); /* complete its execution */
  733. else { /* Lua function */
  734. luaV_finishOp(L); /* finish interrupted instruction */
  735. luaV_execute(L, ci); /* execute down to higher C 'boundary' */
  736. }
  737. }
  738. }
  739. /*
  740. ** Try to find a suspended protected call (a "recover point") for the
  741. ** given thread.
  742. */
  743. static CallInfo *findpcall (lua_State *L) {
  744. CallInfo *ci;
  745. for (ci = L->ci; ci != NULL; ci = ci->previous) { /* search for a pcall */
  746. if (ci->callstatus & CIST_YPCALL)
  747. return ci;
  748. }
  749. return NULL; /* no pending pcall */
  750. }
  751. /*
  752. ** Signal an error in the call to 'lua_resume', not in the execution
  753. ** of the coroutine itself. (Such errors should not be handled by any
  754. ** coroutine error handler and should not kill the coroutine.)
  755. */
  756. static int resume_error (lua_State *L, const char *msg, int narg) {
  757. api_checkpop(L, narg);
  758. L->top.p -= narg; /* remove args from the stack */
  759. setsvalue2s(L, L->top.p, luaS_new(L, msg)); /* push error message */
  760. api_incr_top(L);
  761. lua_unlock(L);
  762. return LUA_ERRRUN;
  763. }
  764. /*
  765. ** Do the work for 'lua_resume' in protected mode. Most of the work
  766. ** depends on the status of the coroutine: initial state, suspended
  767. ** inside a hook, or regularly suspended (optionally with a continuation
  768. ** function), plus erroneous cases: non-suspended coroutine or dead
  769. ** coroutine.
  770. */
  771. static void resume (lua_State *L, void *ud) {
  772. int n = *(cast(int*, ud)); /* number of arguments */
  773. StkId firstArg = L->top.p - n; /* first argument */
  774. CallInfo *ci = L->ci;
  775. if (L->status == LUA_OK) /* starting a coroutine? */
  776. ccall(L, firstArg - 1, LUA_MULTRET, 0); /* just call its body */
  777. else { /* resuming from previous yield */
  778. lua_assert(L->status == LUA_YIELD);
  779. L->status = LUA_OK; /* mark that it is running (again) */
  780. if (isLua(ci)) { /* yielded inside a hook? */
  781. /* undo increment made by 'luaG_traceexec': instruction was not
  782. executed yet */
  783. lua_assert(ci->callstatus & CIST_HOOKYIELD);
  784. ci->u.l.savedpc--;
  785. L->top.p = firstArg; /* discard arguments */
  786. luaV_execute(L, ci); /* just continue running Lua code */
  787. }
  788. else { /* 'common' yield */
  789. if (ci->u.c.k != NULL) { /* does it have a continuation function? */
  790. lua_unlock(L);
  791. n = (*ci->u.c.k)(L, LUA_YIELD, ci->u.c.ctx); /* call continuation */
  792. lua_lock(L);
  793. api_checknelems(L, n);
  794. }
  795. luaD_poscall(L, ci, n); /* finish 'luaD_call' */
  796. }
  797. unroll(L, NULL); /* run continuation */
  798. }
  799. }
  800. /*
  801. ** Unrolls a coroutine in protected mode while there are recoverable
  802. ** errors, that is, errors inside a protected call. (Any error
  803. ** interrupts 'unroll', and this loop protects it again so it can
  804. ** continue.) Stops with a normal end (status == LUA_OK), an yield
  805. ** (status == LUA_YIELD), or an unprotected error ('findpcall' doesn't
  806. ** find a recover point).
  807. */
  808. static TStatus precover (lua_State *L, TStatus status) {
  809. CallInfo *ci;
  810. while (errorstatus(status) && (ci = findpcall(L)) != NULL) {
  811. L->ci = ci; /* go down to recovery functions */
  812. setcistrecst(ci, status); /* status to finish 'pcall' */
  813. status = luaD_rawrunprotected(L, unroll, NULL);
  814. }
  815. return status;
  816. }
  817. LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs,
  818. int *nresults) {
  819. TStatus status;
  820. lua_lock(L);
  821. if (L->status == LUA_OK) { /* may be starting a coroutine */
  822. if (L->ci != &L->base_ci) /* not in base level? */
  823. return resume_error(L, "cannot resume non-suspended coroutine", nargs);
  824. else if (L->top.p - (L->ci->func.p + 1) == nargs) /* no function? */
  825. return resume_error(L, "cannot resume dead coroutine", nargs);
  826. }
  827. else if (L->status != LUA_YIELD) /* ended with errors? */
  828. return resume_error(L, "cannot resume dead coroutine", nargs);
  829. L->nCcalls = (from) ? getCcalls(from) : 0;
  830. if (getCcalls(L) >= LUAI_MAXCCALLS)
  831. return resume_error(L, "C stack overflow", nargs);
  832. L->nCcalls++;
  833. luai_userstateresume(L, nargs);
  834. api_checkpop(L, (L->status == LUA_OK) ? nargs + 1 : nargs);
  835. status = luaD_rawrunprotected(L, resume, &nargs);
  836. /* continue running after recoverable errors */
  837. status = precover(L, status);
  838. if (l_likely(!errorstatus(status)))
  839. lua_assert(status == L->status); /* normal end or yield */
  840. else { /* unrecoverable error */
  841. L->status = status; /* mark thread as 'dead' */
  842. luaD_seterrorobj(L, status, L->top.p); /* push error message */
  843. L->ci->top.p = L->top.p;
  844. }
  845. *nresults = (status == LUA_YIELD) ? L->ci->u2.nyield
  846. : cast_int(L->top.p - (L->ci->func.p + 1));
  847. lua_unlock(L);
  848. return APIstatus(status);
  849. }
  850. LUA_API int lua_isyieldable (lua_State *L) {
  851. return yieldable(L);
  852. }
  853. LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx,
  854. lua_KFunction k) {
  855. CallInfo *ci;
  856. luai_userstateyield(L, nresults);
  857. lua_lock(L);
  858. ci = L->ci;
  859. api_checkpop(L, nresults);
  860. if (l_unlikely(!yieldable(L))) {
  861. if (L != mainthread(G(L)))
  862. luaG_runerror(L, "attempt to yield across a C-call boundary");
  863. else
  864. luaG_runerror(L, "attempt to yield from outside a coroutine");
  865. }
  866. L->status = LUA_YIELD;
  867. ci->u2.nyield = nresults; /* save number of results */
  868. if (isLua(ci)) { /* inside a hook? */
  869. lua_assert(!isLuacode(ci));
  870. api_check(L, nresults == 0, "hooks cannot yield values");
  871. api_check(L, k == NULL, "hooks cannot continue after yielding");
  872. }
  873. else {
  874. if ((ci->u.c.k = k) != NULL) /* is there a continuation? */
  875. ci->u.c.ctx = ctx; /* save context */
  876. luaD_throw(L, LUA_YIELD);
  877. }
  878. lua_assert(ci->callstatus & CIST_HOOKED); /* must be inside a hook */
  879. lua_unlock(L);
  880. return 0; /* return to 'luaD_hook' */
  881. }
  882. /*
  883. ** Auxiliary structure to call 'luaF_close' in protected mode.
  884. */
  885. struct CloseP {
  886. StkId level;
  887. TStatus status;
  888. };
  889. /*
  890. ** Auxiliary function to call 'luaF_close' in protected mode.
  891. */
  892. static void closepaux (lua_State *L, void *ud) {
  893. struct CloseP *pcl = cast(struct CloseP *, ud);
  894. luaF_close(L, pcl->level, pcl->status, 0);
  895. }
  896. /*
  897. ** Calls 'luaF_close' in protected mode. Return the original status
  898. ** or, in case of errors, the new status.
  899. */
  900. TStatus luaD_closeprotected (lua_State *L, ptrdiff_t level, TStatus status) {
  901. CallInfo *old_ci = L->ci;
  902. lu_byte old_allowhooks = L->allowhook;
  903. for (;;) { /* keep closing upvalues until no more errors */
  904. struct CloseP pcl;
  905. pcl.level = restorestack(L, level); pcl.status = status;
  906. status = luaD_rawrunprotected(L, &closepaux, &pcl);
  907. if (l_likely(status == LUA_OK)) /* no more errors? */
  908. return pcl.status;
  909. else { /* an error occurred; restore saved state and repeat */
  910. L->ci = old_ci;
  911. L->allowhook = old_allowhooks;
  912. }
  913. }
  914. }
  915. /*
  916. ** Call the C function 'func' in protected mode, restoring basic
  917. ** thread information ('allowhook', etc.) and in particular
  918. ** its stack level in case of errors.
  919. */
  920. TStatus luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t old_top,
  921. ptrdiff_t ef) {
  922. TStatus status;
  923. CallInfo *old_ci = L->ci;
  924. lu_byte old_allowhooks = L->allowhook;
  925. ptrdiff_t old_errfunc = L->errfunc;
  926. L->errfunc = ef;
  927. status = luaD_rawrunprotected(L, func, u);
  928. if (l_unlikely(status != LUA_OK)) { /* an error occurred? */
  929. L->ci = old_ci;
  930. L->allowhook = old_allowhooks;
  931. status = luaD_closeprotected(L, old_top, status);
  932. luaD_seterrorobj(L, status, restorestack(L, old_top));
  933. luaD_shrinkstack(L); /* restore stack size in case of overflow */
  934. }
  935. L->errfunc = old_errfunc;
  936. return status;
  937. }
  938. /*
  939. ** Execute a protected parser.
  940. */
  941. struct SParser { /* data to 'f_parser' */
  942. ZIO *z;
  943. Mbuffer buff; /* dynamic structure used by the scanner */
  944. Dyndata dyd; /* dynamic structures used by the parser */
  945. const char *mode;
  946. const char *name;
  947. };
  948. static void checkmode (lua_State *L, const char *mode, const char *x) {
  949. if (strchr(mode, x[0]) == NULL) {
  950. luaO_pushfstring(L,
  951. "attempt to load a %s chunk (mode is '%s')", x, mode);
  952. luaD_throw(L, LUA_ERRSYNTAX);
  953. }
  954. }
  955. static void f_parser (lua_State *L, void *ud) {
  956. LClosure *cl;
  957. struct SParser *p = cast(struct SParser *, ud);
  958. const char *mode = p->mode ? p->mode : "bt";
  959. int c = zgetc(p->z); /* read first character */
  960. if (c == LUA_SIGNATURE[0]) {
  961. int fixed = 0;
  962. if (strchr(mode, 'B') != NULL)
  963. fixed = 1;
  964. else
  965. checkmode(L, mode, "binary");
  966. cl = luaU_undump(L, p->z, p->name, fixed);
  967. }
  968. else {
  969. checkmode(L, mode, "text");
  970. cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c);
  971. }
  972. lua_assert(cl->nupvalues == cl->p->sizeupvalues);
  973. luaF_initupvals(L, cl);
  974. }
  975. TStatus luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
  976. const char *mode) {
  977. struct SParser p;
  978. TStatus status;
  979. incnny(L); /* cannot yield during parsing */
  980. p.z = z; p.name = name; p.mode = mode;
  981. p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0;
  982. p.dyd.gt.arr = NULL; p.dyd.gt.size = 0;
  983. p.dyd.label.arr = NULL; p.dyd.label.size = 0;
  984. luaZ_initbuffer(L, &p.buff);
  985. status = luaD_pcall(L, f_parser, &p, savestack(L, L->top.p), L->errfunc);
  986. luaZ_freebuffer(L, &p.buff);
  987. luaM_freearray(L, p.dyd.actvar.arr, cast_sizet(p.dyd.actvar.size));
  988. luaM_freearray(L, p.dyd.gt.arr, cast_sizet(p.dyd.gt.size));
  989. luaM_freearray(L, p.dyd.label.arr, cast_sizet(p.dyd.label.size));
  990. decnny(L);
  991. return status;
  992. }