ldo.c 37 KB

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