ldo.c 38 KB

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