xref: /aosp_15_r20/external/lua/src/ldo.c (revision 088332b5b69e7ab13924864b272aabfc2509d2d5)
1*088332b5SXin Li /*
2*088332b5SXin Li ** $Id: ldo.c $
3*088332b5SXin Li ** Stack and Call structure of Lua
4*088332b5SXin Li ** See Copyright Notice in lua.h
5*088332b5SXin Li */
6*088332b5SXin Li 
7*088332b5SXin Li #define ldo_c
8*088332b5SXin Li #define LUA_CORE
9*088332b5SXin Li 
10*088332b5SXin Li #include "lprefix.h"
11*088332b5SXin Li 
12*088332b5SXin Li 
13*088332b5SXin Li #include <setjmp.h>
14*088332b5SXin Li #include <stdlib.h>
15*088332b5SXin Li #include <string.h>
16*088332b5SXin Li 
17*088332b5SXin Li #include "lua.h"
18*088332b5SXin Li 
19*088332b5SXin Li #include "lapi.h"
20*088332b5SXin Li #include "ldebug.h"
21*088332b5SXin Li #include "ldo.h"
22*088332b5SXin Li #include "lfunc.h"
23*088332b5SXin Li #include "lgc.h"
24*088332b5SXin Li #include "lmem.h"
25*088332b5SXin Li #include "lobject.h"
26*088332b5SXin Li #include "lopcodes.h"
27*088332b5SXin Li #include "lparser.h"
28*088332b5SXin Li #include "lstate.h"
29*088332b5SXin Li #include "lstring.h"
30*088332b5SXin Li #include "ltable.h"
31*088332b5SXin Li #include "ltm.h"
32*088332b5SXin Li #include "lundump.h"
33*088332b5SXin Li #include "lvm.h"
34*088332b5SXin Li #include "lzio.h"
35*088332b5SXin Li 
36*088332b5SXin Li 
37*088332b5SXin Li 
38*088332b5SXin Li #define errorstatus(s)	((s) > LUA_YIELD)
39*088332b5SXin Li 
40*088332b5SXin Li 
41*088332b5SXin Li /*
42*088332b5SXin Li ** {======================================================
43*088332b5SXin Li ** Error-recovery functions
44*088332b5SXin Li ** =======================================================
45*088332b5SXin Li */
46*088332b5SXin Li 
47*088332b5SXin Li /*
48*088332b5SXin Li ** LUAI_THROW/LUAI_TRY define how Lua does exception handling. By
49*088332b5SXin Li ** default, Lua handles errors with exceptions when compiling as
50*088332b5SXin Li ** C++ code, with _longjmp/_setjmp when asked to use them, and with
51*088332b5SXin Li ** longjmp/setjmp otherwise.
52*088332b5SXin Li */
53*088332b5SXin Li #if !defined(LUAI_THROW)				/* { */
54*088332b5SXin Li 
55*088332b5SXin Li #if defined(__cplusplus) && !defined(LUA_USE_LONGJMP)	/* { */
56*088332b5SXin Li 
57*088332b5SXin Li /* C++ exceptions */
58*088332b5SXin Li #define LUAI_THROW(L,c)		throw(c)
59*088332b5SXin Li #define LUAI_TRY(L,c,a) \
60*088332b5SXin Li 	try { a } catch(...) { if ((c)->status == 0) (c)->status = -1; }
61*088332b5SXin Li #define luai_jmpbuf		int  /* dummy variable */
62*088332b5SXin Li 
63*088332b5SXin Li #elif defined(LUA_USE_POSIX)				/* }{ */
64*088332b5SXin Li 
65*088332b5SXin Li /* in POSIX, try _longjmp/_setjmp (more efficient) */
66*088332b5SXin Li #define LUAI_THROW(L,c)		_longjmp((c)->b, 1)
67*088332b5SXin Li #define LUAI_TRY(L,c,a)		if (_setjmp((c)->b) == 0) { a }
68*088332b5SXin Li #define luai_jmpbuf		jmp_buf
69*088332b5SXin Li 
70*088332b5SXin Li #else							/* }{ */
71*088332b5SXin Li 
72*088332b5SXin Li /* ISO C handling with long jumps */
73*088332b5SXin Li #define LUAI_THROW(L,c)		longjmp((c)->b, 1)
74*088332b5SXin Li #define LUAI_TRY(L,c,a)		if (setjmp((c)->b) == 0) { a }
75*088332b5SXin Li #define luai_jmpbuf		jmp_buf
76*088332b5SXin Li 
77*088332b5SXin Li #endif							/* } */
78*088332b5SXin Li 
79*088332b5SXin Li #endif							/* } */
80*088332b5SXin Li 
81*088332b5SXin Li 
82*088332b5SXin Li 
83*088332b5SXin Li /* chain list of long jump buffers */
84*088332b5SXin Li struct lua_longjmp {
85*088332b5SXin Li   struct lua_longjmp *previous;
86*088332b5SXin Li   luai_jmpbuf b;
87*088332b5SXin Li   volatile int status;  /* error code */
88*088332b5SXin Li };
89*088332b5SXin Li 
90*088332b5SXin Li 
luaD_seterrorobj(lua_State * L,int errcode,StkId oldtop)91*088332b5SXin Li void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) {
92*088332b5SXin Li   switch (errcode) {
93*088332b5SXin Li     case LUA_ERRMEM: {  /* memory error? */
94*088332b5SXin Li       setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */
95*088332b5SXin Li       break;
96*088332b5SXin Li     }
97*088332b5SXin Li     case LUA_ERRERR: {
98*088332b5SXin Li       setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling"));
99*088332b5SXin Li       break;
100*088332b5SXin Li     }
101*088332b5SXin Li     case CLOSEPROTECT: {
102*088332b5SXin Li       setnilvalue(s2v(oldtop));  /* no error message */
103*088332b5SXin Li       break;
104*088332b5SXin Li     }
105*088332b5SXin Li     default: {
106*088332b5SXin Li       setobjs2s(L, oldtop, L->top - 1);  /* error message on current top */
107*088332b5SXin Li       break;
108*088332b5SXin Li     }
109*088332b5SXin Li   }
110*088332b5SXin Li   L->top = oldtop + 1;
111*088332b5SXin Li }
112*088332b5SXin Li 
113*088332b5SXin Li 
luaD_throw(lua_State * L,int errcode)114*088332b5SXin Li l_noret luaD_throw (lua_State *L, int errcode) {
115*088332b5SXin Li   if (L->errorJmp) {  /* thread has an error handler? */
116*088332b5SXin Li     L->errorJmp->status = errcode;  /* set status */
117*088332b5SXin Li     LUAI_THROW(L, L->errorJmp);  /* jump to it */
118*088332b5SXin Li   }
119*088332b5SXin Li   else {  /* thread has no error handler */
120*088332b5SXin Li     global_State *g = G(L);
121*088332b5SXin Li     errcode = luaF_close(L, L->stack, errcode);  /* close all upvalues */
122*088332b5SXin Li     L->status = cast_byte(errcode);  /* mark it as dead */
123*088332b5SXin Li     if (g->mainthread->errorJmp) {  /* main thread has a handler? */
124*088332b5SXin Li       setobjs2s(L, g->mainthread->top++, L->top - 1);  /* copy error obj. */
125*088332b5SXin Li       luaD_throw(g->mainthread, errcode);  /* re-throw in main thread */
126*088332b5SXin Li     }
127*088332b5SXin Li     else {  /* no handler at all; abort */
128*088332b5SXin Li       if (g->panic) {  /* panic function? */
129*088332b5SXin Li         luaD_seterrorobj(L, errcode, L->top);  /* assume EXTRA_STACK */
130*088332b5SXin Li         if (L->ci->top < L->top)
131*088332b5SXin Li           L->ci->top = L->top;  /* pushing msg. can break this invariant */
132*088332b5SXin Li         lua_unlock(L);
133*088332b5SXin Li         g->panic(L);  /* call panic function (last chance to jump out) */
134*088332b5SXin Li       }
135*088332b5SXin Li       abort();
136*088332b5SXin Li     }
137*088332b5SXin Li   }
138*088332b5SXin Li }
139*088332b5SXin Li 
140*088332b5SXin Li 
luaD_rawrunprotected(lua_State * L,Pfunc f,void * ud)141*088332b5SXin Li int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
142*088332b5SXin Li   global_State *g = G(L);
143*088332b5SXin Li   l_uint32 oldnCcalls = g->Cstacklimit - (L->nCcalls + L->nci);
144*088332b5SXin Li   struct lua_longjmp lj;
145*088332b5SXin Li   lj.status = LUA_OK;
146*088332b5SXin Li   lj.previous = L->errorJmp;  /* chain new error handler */
147*088332b5SXin Li   L->errorJmp = &lj;
148*088332b5SXin Li   LUAI_TRY(L, &lj,
149*088332b5SXin Li     (*f)(L, ud);
150*088332b5SXin Li   );
151*088332b5SXin Li   L->errorJmp = lj.previous;  /* restore old error handler */
152*088332b5SXin Li   L->nCcalls = g->Cstacklimit - oldnCcalls - L->nci;
153*088332b5SXin Li   return lj.status;
154*088332b5SXin Li }
155*088332b5SXin Li 
156*088332b5SXin Li /* }====================================================== */
157*088332b5SXin Li 
158*088332b5SXin Li 
159*088332b5SXin Li /*
160*088332b5SXin Li ** {==================================================================
161*088332b5SXin Li ** Stack reallocation
162*088332b5SXin Li ** ===================================================================
163*088332b5SXin Li */
correctstack(lua_State * L,StkId oldstack,StkId newstack)164*088332b5SXin Li static void correctstack (lua_State *L, StkId oldstack, StkId newstack) {
165*088332b5SXin Li   CallInfo *ci;
166*088332b5SXin Li   UpVal *up;
167*088332b5SXin Li   if (oldstack == newstack)
168*088332b5SXin Li     return;  /* stack address did not change */
169*088332b5SXin Li   L->top = (L->top - oldstack) + newstack;
170*088332b5SXin Li   for (up = L->openupval; up != NULL; up = up->u.open.next)
171*088332b5SXin Li     up->v = s2v((uplevel(up) - oldstack) + newstack);
172*088332b5SXin Li   for (ci = L->ci; ci != NULL; ci = ci->previous) {
173*088332b5SXin Li     ci->top = (ci->top - oldstack) + newstack;
174*088332b5SXin Li     ci->func = (ci->func - oldstack) + newstack;
175*088332b5SXin Li     if (isLua(ci))
176*088332b5SXin Li       ci->u.l.trap = 1;  /* signal to update 'trap' in 'luaV_execute' */
177*088332b5SXin Li   }
178*088332b5SXin Li }
179*088332b5SXin Li 
180*088332b5SXin Li 
181*088332b5SXin Li /* some space for error handling */
182*088332b5SXin Li #define ERRORSTACKSIZE	(LUAI_MAXSTACK + 200)
183*088332b5SXin Li 
184*088332b5SXin Li 
luaD_reallocstack(lua_State * L,int newsize,int raiseerror)185*088332b5SXin Li int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) {
186*088332b5SXin Li   int lim = L->stacksize;
187*088332b5SXin Li   StkId newstack = luaM_reallocvector(L, L->stack, lim, newsize, StackValue);
188*088332b5SXin Li   lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE);
189*088332b5SXin Li   lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK);
190*088332b5SXin Li   if (unlikely(newstack == NULL)) {  /* reallocation failed? */
191*088332b5SXin Li     if (raiseerror)
192*088332b5SXin Li       luaM_error(L);
193*088332b5SXin Li     else return 0;  /* do not raise an error */
194*088332b5SXin Li   }
195*088332b5SXin Li   for (; lim < newsize; lim++)
196*088332b5SXin Li     setnilvalue(s2v(newstack + lim)); /* erase new segment */
197*088332b5SXin Li   correctstack(L, L->stack, newstack);
198*088332b5SXin Li   L->stack = newstack;
199*088332b5SXin Li   L->stacksize = newsize;
200*088332b5SXin Li   L->stack_last = L->stack + newsize - EXTRA_STACK;
201*088332b5SXin Li   return 1;
202*088332b5SXin Li }
203*088332b5SXin Li 
204*088332b5SXin Li 
205*088332b5SXin Li /*
206*088332b5SXin Li ** Try to grow the stack by at least 'n' elements. when 'raiseerror'
207*088332b5SXin Li ** is true, raises any error; otherwise, return 0 in case of errors.
208*088332b5SXin Li */
luaD_growstack(lua_State * L,int n,int raiseerror)209*088332b5SXin Li int luaD_growstack (lua_State *L, int n, int raiseerror) {
210*088332b5SXin Li   int size = L->stacksize;
211*088332b5SXin Li   int newsize = 2 * size;  /* tentative new size */
212*088332b5SXin Li   if (unlikely(size > LUAI_MAXSTACK)) {  /* need more space after extra size? */
213*088332b5SXin Li     if (raiseerror)
214*088332b5SXin Li       luaD_throw(L, LUA_ERRERR);  /* error inside message handler */
215*088332b5SXin Li     else return 0;
216*088332b5SXin Li   }
217*088332b5SXin Li   else {
218*088332b5SXin Li     int needed = cast_int(L->top - L->stack) + n + EXTRA_STACK;
219*088332b5SXin Li     if (newsize > LUAI_MAXSTACK)  /* cannot cross the limit */
220*088332b5SXin Li       newsize = LUAI_MAXSTACK;
221*088332b5SXin Li     if (newsize < needed)  /* but must respect what was asked for */
222*088332b5SXin Li       newsize = needed;
223*088332b5SXin Li     if (unlikely(newsize > LUAI_MAXSTACK)) {  /* stack overflow? */
224*088332b5SXin Li       /* add extra size to be able to handle the error message */
225*088332b5SXin Li       luaD_reallocstack(L, ERRORSTACKSIZE, raiseerror);
226*088332b5SXin Li       if (raiseerror)
227*088332b5SXin Li         luaG_runerror(L, "stack overflow");
228*088332b5SXin Li       else return 0;
229*088332b5SXin Li     }
230*088332b5SXin Li   }  /* else no errors */
231*088332b5SXin Li   return luaD_reallocstack(L, newsize, raiseerror);
232*088332b5SXin Li }
233*088332b5SXin Li 
234*088332b5SXin Li 
stackinuse(lua_State * L)235*088332b5SXin Li static int stackinuse (lua_State *L) {
236*088332b5SXin Li   CallInfo *ci;
237*088332b5SXin Li   StkId lim = L->top;
238*088332b5SXin Li   for (ci = L->ci; ci != NULL; ci = ci->previous) {
239*088332b5SXin Li     if (lim < ci->top) lim = ci->top;
240*088332b5SXin Li   }
241*088332b5SXin Li   lua_assert(lim <= L->stack_last);
242*088332b5SXin Li   return cast_int(lim - L->stack) + 1;  /* part of stack in use */
243*088332b5SXin Li }
244*088332b5SXin Li 
245*088332b5SXin Li 
luaD_shrinkstack(lua_State * L)246*088332b5SXin Li void luaD_shrinkstack (lua_State *L) {
247*088332b5SXin Li   int inuse = stackinuse(L);
248*088332b5SXin Li   int goodsize = inuse + BASIC_STACK_SIZE;
249*088332b5SXin Li   if (goodsize > LUAI_MAXSTACK)
250*088332b5SXin Li     goodsize = LUAI_MAXSTACK;  /* respect stack limit */
251*088332b5SXin Li   /* if thread is currently not handling a stack overflow and its
252*088332b5SXin Li      good size is smaller than current size, shrink its stack */
253*088332b5SXin Li   if (inuse <= (LUAI_MAXSTACK - EXTRA_STACK) && goodsize < L->stacksize)
254*088332b5SXin Li     luaD_reallocstack(L, goodsize, 0);  /* ok if that fails */
255*088332b5SXin Li   else  /* don't change stack */
256*088332b5SXin Li     condmovestack(L,{},{});  /* (change only for debugging) */
257*088332b5SXin Li   luaE_shrinkCI(L);  /* shrink CI list */
258*088332b5SXin Li }
259*088332b5SXin Li 
260*088332b5SXin Li 
luaD_inctop(lua_State * L)261*088332b5SXin Li void luaD_inctop (lua_State *L) {
262*088332b5SXin Li   luaD_checkstack(L, 1);
263*088332b5SXin Li   L->top++;
264*088332b5SXin Li }
265*088332b5SXin Li 
266*088332b5SXin Li /* }================================================================== */
267*088332b5SXin Li 
268*088332b5SXin Li 
269*088332b5SXin Li /*
270*088332b5SXin Li ** Call a hook for the given event. Make sure there is a hook to be
271*088332b5SXin Li ** called. (Both 'L->hook' and 'L->hookmask', which trigger this
272*088332b5SXin Li ** function, can be changed asynchronously by signals.)
273*088332b5SXin Li */
luaD_hook(lua_State * L,int event,int line,int ftransfer,int ntransfer)274*088332b5SXin Li void luaD_hook (lua_State *L, int event, int line,
275*088332b5SXin Li                               int ftransfer, int ntransfer) {
276*088332b5SXin Li   lua_Hook hook = L->hook;
277*088332b5SXin Li   if (hook && L->allowhook) {  /* make sure there is a hook */
278*088332b5SXin Li     int mask = CIST_HOOKED;
279*088332b5SXin Li     CallInfo *ci = L->ci;
280*088332b5SXin Li     ptrdiff_t top = savestack(L, L->top);
281*088332b5SXin Li     ptrdiff_t ci_top = savestack(L, ci->top);
282*088332b5SXin Li     lua_Debug ar;
283*088332b5SXin Li     ar.event = event;
284*088332b5SXin Li     ar.currentline = line;
285*088332b5SXin Li     ar.i_ci = ci;
286*088332b5SXin Li     if (ntransfer != 0) {
287*088332b5SXin Li       mask |= CIST_TRAN;  /* 'ci' has transfer information */
288*088332b5SXin Li       ci->u2.transferinfo.ftransfer = ftransfer;
289*088332b5SXin Li       ci->u2.transferinfo.ntransfer = ntransfer;
290*088332b5SXin Li     }
291*088332b5SXin Li     luaD_checkstack(L, LUA_MINSTACK);  /* ensure minimum stack size */
292*088332b5SXin Li     if (L->top + LUA_MINSTACK > ci->top)
293*088332b5SXin Li       ci->top = L->top + LUA_MINSTACK;
294*088332b5SXin Li     L->allowhook = 0;  /* cannot call hooks inside a hook */
295*088332b5SXin Li     ci->callstatus |= mask;
296*088332b5SXin Li     lua_unlock(L);
297*088332b5SXin Li     (*hook)(L, &ar);
298*088332b5SXin Li     lua_lock(L);
299*088332b5SXin Li     lua_assert(!L->allowhook);
300*088332b5SXin Li     L->allowhook = 1;
301*088332b5SXin Li     ci->top = restorestack(L, ci_top);
302*088332b5SXin Li     L->top = restorestack(L, top);
303*088332b5SXin Li     ci->callstatus &= ~mask;
304*088332b5SXin Li   }
305*088332b5SXin Li }
306*088332b5SXin Li 
307*088332b5SXin Li 
308*088332b5SXin Li /*
309*088332b5SXin Li ** Executes a call hook for Lua functions. This function is called
310*088332b5SXin Li ** whenever 'hookmask' is not zero, so it checks whether call hooks are
311*088332b5SXin Li ** active.
312*088332b5SXin Li */
luaD_hookcall(lua_State * L,CallInfo * ci)313*088332b5SXin Li void luaD_hookcall (lua_State *L, CallInfo *ci) {
314*088332b5SXin Li   int hook = (ci->callstatus & CIST_TAIL) ? LUA_HOOKTAILCALL : LUA_HOOKCALL;
315*088332b5SXin Li   Proto *p;
316*088332b5SXin Li   if (!(L->hookmask & LUA_MASKCALL))  /* some other hook? */
317*088332b5SXin Li     return;  /* don't call hook */
318*088332b5SXin Li   p = clLvalue(s2v(ci->func))->p;
319*088332b5SXin Li   L->top = ci->top;  /* prepare top */
320*088332b5SXin Li   ci->u.l.savedpc++;  /* hooks assume 'pc' is already incremented */
321*088332b5SXin Li   luaD_hook(L, hook, -1, 1, p->numparams);
322*088332b5SXin Li   ci->u.l.savedpc--;  /* correct 'pc' */
323*088332b5SXin Li }
324*088332b5SXin Li 
325*088332b5SXin Li 
rethook(lua_State * L,CallInfo * ci,StkId firstres,int nres)326*088332b5SXin Li static StkId rethook (lua_State *L, CallInfo *ci, StkId firstres, int nres) {
327*088332b5SXin Li   ptrdiff_t oldtop = savestack(L, L->top);  /* hook may change top */
328*088332b5SXin Li   int delta = 0;
329*088332b5SXin Li   if (isLuacode(ci)) {
330*088332b5SXin Li     Proto *p = ci_func(ci)->p;
331*088332b5SXin Li     if (p->is_vararg)
332*088332b5SXin Li       delta = ci->u.l.nextraargs + p->numparams + 1;
333*088332b5SXin Li     if (L->top < ci->top)
334*088332b5SXin Li       L->top = ci->top;  /* correct top to run hook */
335*088332b5SXin Li   }
336*088332b5SXin Li   if (L->hookmask & LUA_MASKRET) {  /* is return hook on? */
337*088332b5SXin Li     int ftransfer;
338*088332b5SXin Li     ci->func += delta;  /* if vararg, back to virtual 'func' */
339*088332b5SXin Li     ftransfer = cast(unsigned short, firstres - ci->func);
340*088332b5SXin Li     luaD_hook(L, LUA_HOOKRET, -1, ftransfer, nres);  /* call it */
341*088332b5SXin Li     ci->func -= delta;
342*088332b5SXin Li   }
343*088332b5SXin Li   if (isLua(ci = ci->previous))
344*088332b5SXin Li     L->oldpc = pcRel(ci->u.l.savedpc, ci_func(ci)->p);  /* update 'oldpc' */
345*088332b5SXin Li   return restorestack(L, oldtop);
346*088332b5SXin Li }
347*088332b5SXin Li 
348*088332b5SXin Li 
349*088332b5SXin Li /*
350*088332b5SXin Li ** Check whether 'func' has a '__call' metafield. If so, put it in the
351*088332b5SXin Li ** stack, below original 'func', so that 'luaD_call' can call it. Raise
352*088332b5SXin Li ** an error if there is no '__call' metafield.
353*088332b5SXin Li */
luaD_tryfuncTM(lua_State * L,StkId func)354*088332b5SXin Li void luaD_tryfuncTM (lua_State *L, StkId func) {
355*088332b5SXin Li   const TValue *tm = luaT_gettmbyobj(L, s2v(func), TM_CALL);
356*088332b5SXin Li   StkId p;
357*088332b5SXin Li   if (unlikely(ttisnil(tm)))
358*088332b5SXin Li     luaG_typeerror(L, s2v(func), "call");  /* nothing to call */
359*088332b5SXin Li   for (p = L->top; p > func; p--)  /* open space for metamethod */
360*088332b5SXin Li     setobjs2s(L, p, p-1);
361*088332b5SXin Li   L->top++;  /* stack space pre-allocated by the caller */
362*088332b5SXin Li   setobj2s(L, func, tm);  /* metamethod is the new function to be called */
363*088332b5SXin Li }
364*088332b5SXin Li 
365*088332b5SXin Li 
366*088332b5SXin Li /*
367*088332b5SXin Li ** Given 'nres' results at 'firstResult', move 'wanted' of them to 'res'.
368*088332b5SXin Li ** Handle most typical cases (zero results for commands, one result for
369*088332b5SXin Li ** expressions, multiple results for tail calls/single parameters)
370*088332b5SXin Li ** separated.
371*088332b5SXin Li */
moveresults(lua_State * L,StkId res,int nres,int wanted)372*088332b5SXin Li static void moveresults (lua_State *L, StkId res, int nres, int wanted) {
373*088332b5SXin Li   StkId firstresult;
374*088332b5SXin Li   int i;
375*088332b5SXin Li   switch (wanted) {  /* handle typical cases separately */
376*088332b5SXin Li     case 0:  /* no values needed */
377*088332b5SXin Li       L->top = res;
378*088332b5SXin Li       return;
379*088332b5SXin Li     case 1:  /* one value needed */
380*088332b5SXin Li       if (nres == 0)   /* no results? */
381*088332b5SXin Li         setnilvalue(s2v(res));  /* adjust with nil */
382*088332b5SXin Li       else
383*088332b5SXin Li         setobjs2s(L, res, L->top - nres);  /* move it to proper place */
384*088332b5SXin Li       L->top = res + 1;
385*088332b5SXin Li       return;
386*088332b5SXin Li     case LUA_MULTRET:
387*088332b5SXin Li       wanted = nres;  /* we want all results */
388*088332b5SXin Li       break;
389*088332b5SXin Li     default:  /* multiple results (or to-be-closed variables) */
390*088332b5SXin Li       if (hastocloseCfunc(wanted)) {  /* to-be-closed variables? */
391*088332b5SXin Li         ptrdiff_t savedres = savestack(L, res);
392*088332b5SXin Li         luaF_close(L, res, LUA_OK);  /* may change the stack */
393*088332b5SXin Li         res = restorestack(L, savedres);
394*088332b5SXin Li         wanted = codeNresults(wanted);  /* correct value */
395*088332b5SXin Li         if (wanted == LUA_MULTRET)
396*088332b5SXin Li           wanted = nres;
397*088332b5SXin Li       }
398*088332b5SXin Li       break;
399*088332b5SXin Li   }
400*088332b5SXin Li   firstresult = L->top - nres;  /* index of first result */
401*088332b5SXin Li   /* move all results to correct place */
402*088332b5SXin Li   for (i = 0; i < nres && i < wanted; i++)
403*088332b5SXin Li     setobjs2s(L, res + i, firstresult + i);
404*088332b5SXin Li   for (; i < wanted; i++)  /* complete wanted number of results */
405*088332b5SXin Li     setnilvalue(s2v(res + i));
406*088332b5SXin Li   L->top = res + wanted;  /* top points after the last result */
407*088332b5SXin Li }
408*088332b5SXin Li 
409*088332b5SXin Li 
410*088332b5SXin Li /*
411*088332b5SXin Li ** Finishes a function call: calls hook if necessary, removes CallInfo,
412*088332b5SXin Li ** moves current number of results to proper place.
413*088332b5SXin Li */
luaD_poscall(lua_State * L,CallInfo * ci,int nres)414*088332b5SXin Li void luaD_poscall (lua_State *L, CallInfo *ci, int nres) {
415*088332b5SXin Li   if (L->hookmask)
416*088332b5SXin Li     L->top = rethook(L, ci, L->top - nres, nres);
417*088332b5SXin Li   L->ci = ci->previous;  /* back to caller */
418*088332b5SXin Li   /* move results to proper place */
419*088332b5SXin Li   moveresults(L, ci->func, nres, ci->nresults);
420*088332b5SXin Li }
421*088332b5SXin Li 
422*088332b5SXin Li 
423*088332b5SXin Li 
424*088332b5SXin Li #define next_ci(L)  (L->ci->next ? L->ci->next : luaE_extendCI(L))
425*088332b5SXin Li 
426*088332b5SXin Li 
427*088332b5SXin Li /*
428*088332b5SXin Li ** Prepare a function for a tail call, building its call info on top
429*088332b5SXin Li ** of the current call info. 'narg1' is the number of arguments plus 1
430*088332b5SXin Li ** (so that it includes the function itself).
431*088332b5SXin Li */
luaD_pretailcall(lua_State * L,CallInfo * ci,StkId func,int narg1)432*088332b5SXin Li void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) {
433*088332b5SXin Li   Proto *p = clLvalue(s2v(func))->p;
434*088332b5SXin Li   int fsize = p->maxstacksize;  /* frame size */
435*088332b5SXin Li   int nfixparams = p->numparams;
436*088332b5SXin Li   int i;
437*088332b5SXin Li   for (i = 0; i < narg1; i++)  /* move down function and arguments */
438*088332b5SXin Li     setobjs2s(L, ci->func + i, func + i);
439*088332b5SXin Li   checkstackGC(L, fsize);
440*088332b5SXin Li   func = ci->func;  /* moved-down function */
441*088332b5SXin Li   for (; narg1 <= nfixparams; narg1++)
442*088332b5SXin Li     setnilvalue(s2v(func + narg1));  /* complete missing arguments */
443*088332b5SXin Li   ci->top = func + 1 + fsize;  /* top for new function */
444*088332b5SXin Li   lua_assert(ci->top <= L->stack_last);
445*088332b5SXin Li   ci->u.l.savedpc = p->code;  /* starting point */
446*088332b5SXin Li   ci->callstatus |= CIST_TAIL;
447*088332b5SXin Li   L->top = func + narg1;  /* set top */
448*088332b5SXin Li }
449*088332b5SXin Li 
450*088332b5SXin Li 
451*088332b5SXin Li /*
452*088332b5SXin Li ** Call a function (C or Lua). The function to be called is at *func.
453*088332b5SXin Li ** The arguments are on the stack, right after the function.
454*088332b5SXin Li ** When returns, all the results are on the stack, starting at the original
455*088332b5SXin Li ** function position.
456*088332b5SXin Li */
luaD_call(lua_State * L,StkId func,int nresults)457*088332b5SXin Li void luaD_call (lua_State *L, StkId func, int nresults) {
458*088332b5SXin Li   lua_CFunction f;
459*088332b5SXin Li  retry:
460*088332b5SXin Li   switch (ttypetag(s2v(func))) {
461*088332b5SXin Li     case LUA_VCCL:  /* C closure */
462*088332b5SXin Li       f = clCvalue(s2v(func))->f;
463*088332b5SXin Li       goto Cfunc;
464*088332b5SXin Li     case LUA_VLCF:  /* light C function */
465*088332b5SXin Li       f = fvalue(s2v(func));
466*088332b5SXin Li      Cfunc: {
467*088332b5SXin Li       int n;  /* number of returns */
468*088332b5SXin Li       CallInfo *ci;
469*088332b5SXin Li       checkstackGCp(L, LUA_MINSTACK, func);  /* ensure minimum stack size */
470*088332b5SXin Li       L->ci = ci = next_ci(L);
471*088332b5SXin Li       ci->nresults = nresults;
472*088332b5SXin Li       ci->callstatus = CIST_C;
473*088332b5SXin Li       ci->top = L->top + LUA_MINSTACK;
474*088332b5SXin Li       ci->func = func;
475*088332b5SXin Li       lua_assert(ci->top <= L->stack_last);
476*088332b5SXin Li       if (L->hookmask & LUA_MASKCALL) {
477*088332b5SXin Li         int narg = cast_int(L->top - func) - 1;
478*088332b5SXin Li         luaD_hook(L, LUA_HOOKCALL, -1, 1, narg);
479*088332b5SXin Li       }
480*088332b5SXin Li       lua_unlock(L);
481*088332b5SXin Li       n = (*f)(L);  /* do the actual call */
482*088332b5SXin Li       lua_lock(L);
483*088332b5SXin Li       api_checknelems(L, n);
484*088332b5SXin Li       luaD_poscall(L, ci, n);
485*088332b5SXin Li       break;
486*088332b5SXin Li     }
487*088332b5SXin Li     case LUA_VLCL: {  /* Lua function */
488*088332b5SXin Li       CallInfo *ci;
489*088332b5SXin Li       Proto *p = clLvalue(s2v(func))->p;
490*088332b5SXin Li       int narg = cast_int(L->top - func) - 1;  /* number of real arguments */
491*088332b5SXin Li       int nfixparams = p->numparams;
492*088332b5SXin Li       int fsize = p->maxstacksize;  /* frame size */
493*088332b5SXin Li       checkstackGCp(L, fsize, func);
494*088332b5SXin Li       L->ci = ci = next_ci(L);
495*088332b5SXin Li       ci->nresults = nresults;
496*088332b5SXin Li       ci->u.l.savedpc = p->code;  /* starting point */
497*088332b5SXin Li       ci->callstatus = 0;
498*088332b5SXin Li       ci->top = func + 1 + fsize;
499*088332b5SXin Li       ci->func = func;
500*088332b5SXin Li       L->ci = ci;
501*088332b5SXin Li       for (; narg < nfixparams; narg++)
502*088332b5SXin Li         setnilvalue(s2v(L->top++));  /* complete missing arguments */
503*088332b5SXin Li       lua_assert(ci->top <= L->stack_last);
504*088332b5SXin Li       luaV_execute(L, ci);  /* run the function */
505*088332b5SXin Li       break;
506*088332b5SXin Li     }
507*088332b5SXin Li     default: {  /* not a function */
508*088332b5SXin Li       checkstackGCp(L, 1, func);  /* space for metamethod */
509*088332b5SXin Li       luaD_tryfuncTM(L, func);  /* try to get '__call' metamethod */
510*088332b5SXin Li       goto retry;  /* try again with metamethod */
511*088332b5SXin Li     }
512*088332b5SXin Li   }
513*088332b5SXin Li }
514*088332b5SXin Li 
515*088332b5SXin Li 
516*088332b5SXin Li /*
517*088332b5SXin Li ** Similar to 'luaD_call', but does not allow yields during the call.
518*088332b5SXin Li */
luaD_callnoyield(lua_State * L,StkId func,int nResults)519*088332b5SXin Li void luaD_callnoyield (lua_State *L, StkId func, int nResults) {
520*088332b5SXin Li   incXCcalls(L);
521*088332b5SXin Li   if (getCcalls(L) <= CSTACKERR) {  /* possible C stack overflow? */
522*088332b5SXin Li     luaE_exitCcall(L);  /* to compensate decrement in next call */
523*088332b5SXin Li     luaE_enterCcall(L);  /* check properly */
524*088332b5SXin Li   }
525*088332b5SXin Li   luaD_call(L, func, nResults);
526*088332b5SXin Li   decXCcalls(L);
527*088332b5SXin Li }
528*088332b5SXin Li 
529*088332b5SXin Li 
530*088332b5SXin Li /*
531*088332b5SXin Li ** Completes the execution of an interrupted C function, calling its
532*088332b5SXin Li ** continuation function.
533*088332b5SXin Li */
finishCcall(lua_State * L,int status)534*088332b5SXin Li static void finishCcall (lua_State *L, int status) {
535*088332b5SXin Li   CallInfo *ci = L->ci;
536*088332b5SXin Li   int n;
537*088332b5SXin Li   /* must have a continuation and must be able to call it */
538*088332b5SXin Li   lua_assert(ci->u.c.k != NULL && yieldable(L));
539*088332b5SXin Li   /* error status can only happen in a protected call */
540*088332b5SXin Li   lua_assert((ci->callstatus & CIST_YPCALL) || status == LUA_YIELD);
541*088332b5SXin Li   if (ci->callstatus & CIST_YPCALL) {  /* was inside a pcall? */
542*088332b5SXin Li     ci->callstatus &= ~CIST_YPCALL;  /* continuation is also inside it */
543*088332b5SXin Li     L->errfunc = ci->u.c.old_errfunc;  /* with the same error function */
544*088332b5SXin Li   }
545*088332b5SXin Li   /* finish 'lua_callk'/'lua_pcall'; CIST_YPCALL and 'errfunc' already
546*088332b5SXin Li      handled */
547*088332b5SXin Li   adjustresults(L, ci->nresults);
548*088332b5SXin Li   lua_unlock(L);
549*088332b5SXin Li   n = (*ci->u.c.k)(L, status, ci->u.c.ctx);  /* call continuation function */
550*088332b5SXin Li   lua_lock(L);
551*088332b5SXin Li   api_checknelems(L, n);
552*088332b5SXin Li   luaD_poscall(L, ci, n);  /* finish 'luaD_call' */
553*088332b5SXin Li }
554*088332b5SXin Li 
555*088332b5SXin Li 
556*088332b5SXin Li /*
557*088332b5SXin Li ** Executes "full continuation" (everything in the stack) of a
558*088332b5SXin Li ** previously interrupted coroutine until the stack is empty (or another
559*088332b5SXin Li ** interruption long-jumps out of the loop). If the coroutine is
560*088332b5SXin Li ** recovering from an error, 'ud' points to the error status, which must
561*088332b5SXin Li ** be passed to the first continuation function (otherwise the default
562*088332b5SXin Li ** status is LUA_YIELD).
563*088332b5SXin Li */
unroll(lua_State * L,void * ud)564*088332b5SXin Li static void unroll (lua_State *L, void *ud) {
565*088332b5SXin Li   CallInfo *ci;
566*088332b5SXin Li   if (ud != NULL)  /* error status? */
567*088332b5SXin Li     finishCcall(L, *(int *)ud);  /* finish 'lua_pcallk' callee */
568*088332b5SXin Li   while ((ci = L->ci) != &L->base_ci) {  /* something in the stack */
569*088332b5SXin Li     if (!isLua(ci))  /* C function? */
570*088332b5SXin Li       finishCcall(L, LUA_YIELD);  /* complete its execution */
571*088332b5SXin Li     else {  /* Lua function */
572*088332b5SXin Li       luaV_finishOp(L);  /* finish interrupted instruction */
573*088332b5SXin Li       luaV_execute(L, ci);  /* execute down to higher C 'boundary' */
574*088332b5SXin Li     }
575*088332b5SXin Li   }
576*088332b5SXin Li }
577*088332b5SXin Li 
578*088332b5SXin Li 
579*088332b5SXin Li /*
580*088332b5SXin Li ** Try to find a suspended protected call (a "recover point") for the
581*088332b5SXin Li ** given thread.
582*088332b5SXin Li */
findpcall(lua_State * L)583*088332b5SXin Li static CallInfo *findpcall (lua_State *L) {
584*088332b5SXin Li   CallInfo *ci;
585*088332b5SXin Li   for (ci = L->ci; ci != NULL; ci = ci->previous) {  /* search for a pcall */
586*088332b5SXin Li     if (ci->callstatus & CIST_YPCALL)
587*088332b5SXin Li       return ci;
588*088332b5SXin Li   }
589*088332b5SXin Li   return NULL;  /* no pending pcall */
590*088332b5SXin Li }
591*088332b5SXin Li 
592*088332b5SXin Li 
593*088332b5SXin Li /*
594*088332b5SXin Li ** Recovers from an error in a coroutine. Finds a recover point (if
595*088332b5SXin Li ** there is one) and completes the execution of the interrupted
596*088332b5SXin Li ** 'luaD_pcall'. If there is no recover point, returns zero.
597*088332b5SXin Li */
recover(lua_State * L,int status)598*088332b5SXin Li static int recover (lua_State *L, int status) {
599*088332b5SXin Li   StkId oldtop;
600*088332b5SXin Li   CallInfo *ci = findpcall(L);
601*088332b5SXin Li   if (ci == NULL) return 0;  /* no recovery point */
602*088332b5SXin Li   /* "finish" luaD_pcall */
603*088332b5SXin Li   oldtop = restorestack(L, ci->u2.funcidx);
604*088332b5SXin Li   luaF_close(L, oldtop, status);  /* may change the stack */
605*088332b5SXin Li   oldtop = restorestack(L, ci->u2.funcidx);
606*088332b5SXin Li   luaD_seterrorobj(L, status, oldtop);
607*088332b5SXin Li   L->ci = ci;
608*088332b5SXin Li   L->allowhook = getoah(ci->callstatus);  /* restore original 'allowhook' */
609*088332b5SXin Li   luaD_shrinkstack(L);
610*088332b5SXin Li   L->errfunc = ci->u.c.old_errfunc;
611*088332b5SXin Li   return 1;  /* continue running the coroutine */
612*088332b5SXin Li }
613*088332b5SXin Li 
614*088332b5SXin Li 
615*088332b5SXin Li /*
616*088332b5SXin Li ** Signal an error in the call to 'lua_resume', not in the execution
617*088332b5SXin Li ** of the coroutine itself. (Such errors should not be handled by any
618*088332b5SXin Li ** coroutine error handler and should not kill the coroutine.)
619*088332b5SXin Li */
resume_error(lua_State * L,const char * msg,int narg)620*088332b5SXin Li static int resume_error (lua_State *L, const char *msg, int narg) {
621*088332b5SXin Li   L->top -= narg;  /* remove args from the stack */
622*088332b5SXin Li   setsvalue2s(L, L->top, luaS_new(L, msg));  /* push error message */
623*088332b5SXin Li   api_incr_top(L);
624*088332b5SXin Li   lua_unlock(L);
625*088332b5SXin Li   return LUA_ERRRUN;
626*088332b5SXin Li }
627*088332b5SXin Li 
628*088332b5SXin Li 
629*088332b5SXin Li /*
630*088332b5SXin Li ** Do the work for 'lua_resume' in protected mode. Most of the work
631*088332b5SXin Li ** depends on the status of the coroutine: initial state, suspended
632*088332b5SXin Li ** inside a hook, or regularly suspended (optionally with a continuation
633*088332b5SXin Li ** function), plus erroneous cases: non-suspended coroutine or dead
634*088332b5SXin Li ** coroutine.
635*088332b5SXin Li */
resume(lua_State * L,void * ud)636*088332b5SXin Li static void resume (lua_State *L, void *ud) {
637*088332b5SXin Li   int n = *(cast(int*, ud));  /* number of arguments */
638*088332b5SXin Li   StkId firstArg = L->top - n;  /* first argument */
639*088332b5SXin Li   CallInfo *ci = L->ci;
640*088332b5SXin Li   if (L->status == LUA_OK) {  /* starting a coroutine? */
641*088332b5SXin Li     luaD_call(L, firstArg - 1, LUA_MULTRET);
642*088332b5SXin Li   }
643*088332b5SXin Li   else {  /* resuming from previous yield */
644*088332b5SXin Li     lua_assert(L->status == LUA_YIELD);
645*088332b5SXin Li     L->status = LUA_OK;  /* mark that it is running (again) */
646*088332b5SXin Li     if (isLua(ci))  /* yielded inside a hook? */
647*088332b5SXin Li       luaV_execute(L, ci);  /* just continue running Lua code */
648*088332b5SXin Li     else {  /* 'common' yield */
649*088332b5SXin Li       if (ci->u.c.k != NULL) {  /* does it have a continuation function? */
650*088332b5SXin Li         lua_unlock(L);
651*088332b5SXin Li         n = (*ci->u.c.k)(L, LUA_YIELD, ci->u.c.ctx); /* call continuation */
652*088332b5SXin Li         lua_lock(L);
653*088332b5SXin Li         api_checknelems(L, n);
654*088332b5SXin Li       }
655*088332b5SXin Li       luaD_poscall(L, ci, n);  /* finish 'luaD_call' */
656*088332b5SXin Li     }
657*088332b5SXin Li     unroll(L, NULL);  /* run continuation */
658*088332b5SXin Li   }
659*088332b5SXin Li }
660*088332b5SXin Li 
lua_resume(lua_State * L,lua_State * from,int nargs,int * nresults)661*088332b5SXin Li LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs,
662*088332b5SXin Li                                       int *nresults) {
663*088332b5SXin Li   int status;
664*088332b5SXin Li   lua_lock(L);
665*088332b5SXin Li   if (L->status == LUA_OK) {  /* may be starting a coroutine */
666*088332b5SXin Li     if (L->ci != &L->base_ci)  /* not in base level? */
667*088332b5SXin Li       return resume_error(L, "cannot resume non-suspended coroutine", nargs);
668*088332b5SXin Li     else if (L->top - (L->ci->func + 1) == nargs)  /* no function? */
669*088332b5SXin Li       return resume_error(L, "cannot resume dead coroutine", nargs);
670*088332b5SXin Li   }
671*088332b5SXin Li   else if (L->status != LUA_YIELD)  /* ended with errors? */
672*088332b5SXin Li     return resume_error(L, "cannot resume dead coroutine", nargs);
673*088332b5SXin Li   if (from == NULL)
674*088332b5SXin Li     L->nCcalls = CSTACKTHREAD;
675*088332b5SXin Li   else  /* correct 'nCcalls' for this thread */
676*088332b5SXin Li     L->nCcalls = getCcalls(from) - L->nci - CSTACKCF;
677*088332b5SXin Li   if (L->nCcalls <= CSTACKERR)
678*088332b5SXin Li     return resume_error(L, "C stack overflow", nargs);
679*088332b5SXin Li   luai_userstateresume(L, nargs);
680*088332b5SXin Li   api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs);
681*088332b5SXin Li   status = luaD_rawrunprotected(L, resume, &nargs);
682*088332b5SXin Li    /* continue running after recoverable errors */
683*088332b5SXin Li   while (errorstatus(status) && recover(L, status)) {
684*088332b5SXin Li     /* unroll continuation */
685*088332b5SXin Li     status = luaD_rawrunprotected(L, unroll, &status);
686*088332b5SXin Li   }
687*088332b5SXin Li   if (likely(!errorstatus(status)))
688*088332b5SXin Li     lua_assert(status == L->status);  /* normal end or yield */
689*088332b5SXin Li   else {  /* unrecoverable error */
690*088332b5SXin Li     L->status = cast_byte(status);  /* mark thread as 'dead' */
691*088332b5SXin Li     luaD_seterrorobj(L, status, L->top);  /* push error message */
692*088332b5SXin Li     L->ci->top = L->top;
693*088332b5SXin Li   }
694*088332b5SXin Li   *nresults = (status == LUA_YIELD) ? L->ci->u2.nyield
695*088332b5SXin Li                                     : cast_int(L->top - (L->ci->func + 1));
696*088332b5SXin Li   lua_unlock(L);
697*088332b5SXin Li   return status;
698*088332b5SXin Li }
699*088332b5SXin Li 
700*088332b5SXin Li 
lua_isyieldable(lua_State * L)701*088332b5SXin Li LUA_API int lua_isyieldable (lua_State *L) {
702*088332b5SXin Li   return yieldable(L);
703*088332b5SXin Li }
704*088332b5SXin Li 
705*088332b5SXin Li 
lua_yieldk(lua_State * L,int nresults,lua_KContext ctx,lua_KFunction k)706*088332b5SXin Li LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx,
707*088332b5SXin Li                         lua_KFunction k) {
708*088332b5SXin Li   CallInfo *ci;
709*088332b5SXin Li   luai_userstateyield(L, nresults);
710*088332b5SXin Li   lua_lock(L);
711*088332b5SXin Li   ci = L->ci;
712*088332b5SXin Li   api_checknelems(L, nresults);
713*088332b5SXin Li   if (unlikely(!yieldable(L))) {
714*088332b5SXin Li     if (L != G(L)->mainthread)
715*088332b5SXin Li       luaG_runerror(L, "attempt to yield across a C-call boundary");
716*088332b5SXin Li     else
717*088332b5SXin Li       luaG_runerror(L, "attempt to yield from outside a coroutine");
718*088332b5SXin Li   }
719*088332b5SXin Li   L->status = LUA_YIELD;
720*088332b5SXin Li   if (isLua(ci)) {  /* inside a hook? */
721*088332b5SXin Li     lua_assert(!isLuacode(ci));
722*088332b5SXin Li     api_check(L, k == NULL, "hooks cannot continue after yielding");
723*088332b5SXin Li     ci->u2.nyield = 0;  /* no results */
724*088332b5SXin Li   }
725*088332b5SXin Li   else {
726*088332b5SXin Li     if ((ci->u.c.k = k) != NULL)  /* is there a continuation? */
727*088332b5SXin Li       ci->u.c.ctx = ctx;  /* save context */
728*088332b5SXin Li     ci->u2.nyield = nresults;  /* save number of results */
729*088332b5SXin Li     luaD_throw(L, LUA_YIELD);
730*088332b5SXin Li   }
731*088332b5SXin Li   lua_assert(ci->callstatus & CIST_HOOKED);  /* must be inside a hook */
732*088332b5SXin Li   lua_unlock(L);
733*088332b5SXin Li   return 0;  /* return to 'luaD_hook' */
734*088332b5SXin Li }
735*088332b5SXin Li 
736*088332b5SXin Li 
737*088332b5SXin Li /*
738*088332b5SXin Li ** Call the C function 'func' in protected mode, restoring basic
739*088332b5SXin Li ** thread information ('allowhook', etc.) and in particular
740*088332b5SXin Li ** its stack level in case of errors.
741*088332b5SXin Li */
luaD_pcall(lua_State * L,Pfunc func,void * u,ptrdiff_t old_top,ptrdiff_t ef)742*088332b5SXin Li int luaD_pcall (lua_State *L, Pfunc func, void *u,
743*088332b5SXin Li                 ptrdiff_t old_top, ptrdiff_t ef) {
744*088332b5SXin Li   int status;
745*088332b5SXin Li   CallInfo *old_ci = L->ci;
746*088332b5SXin Li   lu_byte old_allowhooks = L->allowhook;
747*088332b5SXin Li   ptrdiff_t old_errfunc = L->errfunc;
748*088332b5SXin Li   L->errfunc = ef;
749*088332b5SXin Li   status = luaD_rawrunprotected(L, func, u);
750*088332b5SXin Li   if (unlikely(status != LUA_OK)) {  /* an error occurred? */
751*088332b5SXin Li     StkId oldtop = restorestack(L, old_top);
752*088332b5SXin Li     L->ci = old_ci;
753*088332b5SXin Li     L->allowhook = old_allowhooks;
754*088332b5SXin Li     status = luaF_close(L, oldtop, status);
755*088332b5SXin Li     oldtop = restorestack(L, old_top);  /* previous call may change stack */
756*088332b5SXin Li     luaD_seterrorobj(L, status, oldtop);
757*088332b5SXin Li     luaD_shrinkstack(L);
758*088332b5SXin Li   }
759*088332b5SXin Li   L->errfunc = old_errfunc;
760*088332b5SXin Li   return status;
761*088332b5SXin Li }
762*088332b5SXin Li 
763*088332b5SXin Li 
764*088332b5SXin Li 
765*088332b5SXin Li /*
766*088332b5SXin Li ** Execute a protected parser.
767*088332b5SXin Li */
768*088332b5SXin Li struct SParser {  /* data to 'f_parser' */
769*088332b5SXin Li   ZIO *z;
770*088332b5SXin Li   Mbuffer buff;  /* dynamic structure used by the scanner */
771*088332b5SXin Li   Dyndata dyd;  /* dynamic structures used by the parser */
772*088332b5SXin Li   const char *mode;
773*088332b5SXin Li   const char *name;
774*088332b5SXin Li };
775*088332b5SXin Li 
776*088332b5SXin Li 
checkmode(lua_State * L,const char * mode,const char * x)777*088332b5SXin Li static void checkmode (lua_State *L, const char *mode, const char *x) {
778*088332b5SXin Li   if (mode && strchr(mode, x[0]) == NULL) {
779*088332b5SXin Li     luaO_pushfstring(L,
780*088332b5SXin Li        "attempt to load a %s chunk (mode is '%s')", x, mode);
781*088332b5SXin Li     luaD_throw(L, LUA_ERRSYNTAX);
782*088332b5SXin Li   }
783*088332b5SXin Li }
784*088332b5SXin Li 
785*088332b5SXin Li 
f_parser(lua_State * L,void * ud)786*088332b5SXin Li static void f_parser (lua_State *L, void *ud) {
787*088332b5SXin Li   LClosure *cl;
788*088332b5SXin Li   struct SParser *p = cast(struct SParser *, ud);
789*088332b5SXin Li   int c = zgetc(p->z);  /* read first character */
790*088332b5SXin Li   if (c == LUA_SIGNATURE[0]) {
791*088332b5SXin Li     checkmode(L, p->mode, "binary");
792*088332b5SXin Li     cl = luaU_undump(L, p->z, p->name);
793*088332b5SXin Li   }
794*088332b5SXin Li   else {
795*088332b5SXin Li     checkmode(L, p->mode, "text");
796*088332b5SXin Li     cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c);
797*088332b5SXin Li   }
798*088332b5SXin Li   lua_assert(cl->nupvalues == cl->p->sizeupvalues);
799*088332b5SXin Li   luaF_initupvals(L, cl);
800*088332b5SXin Li }
801*088332b5SXin Li 
802*088332b5SXin Li 
luaD_protectedparser(lua_State * L,ZIO * z,const char * name,const char * mode)803*088332b5SXin Li int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
804*088332b5SXin Li                                         const char *mode) {
805*088332b5SXin Li   struct SParser p;
806*088332b5SXin Li   int status;
807*088332b5SXin Li   incnny(L);  /* cannot yield during parsing */
808*088332b5SXin Li   p.z = z; p.name = name; p.mode = mode;
809*088332b5SXin Li   p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0;
810*088332b5SXin Li   p.dyd.gt.arr = NULL; p.dyd.gt.size = 0;
811*088332b5SXin Li   p.dyd.label.arr = NULL; p.dyd.label.size = 0;
812*088332b5SXin Li   luaZ_initbuffer(L, &p.buff);
813*088332b5SXin Li   status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc);
814*088332b5SXin Li   luaZ_freebuffer(L, &p.buff);
815*088332b5SXin Li   luaM_freearray(L, p.dyd.actvar.arr, p.dyd.actvar.size);
816*088332b5SXin Li   luaM_freearray(L, p.dyd.gt.arr, p.dyd.gt.size);
817*088332b5SXin Li   luaM_freearray(L, p.dyd.label.arr, p.dyd.label.size);
818*088332b5SXin Li   decnny(L);
819*088332b5SXin Li   return status;
820*088332b5SXin Li }
821*088332b5SXin Li 
822*088332b5SXin Li 
823