xref: /freebsd-src/sys/contrib/openzfs/module/lua/ldo.c (revision dbd5678dca91abcefe8d046aa2f9b66497a95ffb)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy ** $Id: ldo.c,v 2.108.1.3 2013/11/08 18:22:50 roberto Exp $
3eda14cbcSMatt Macy ** Stack and Call structure of Lua
4eda14cbcSMatt Macy ** See Copyright Notice in lua.h
5eda14cbcSMatt Macy */
6eda14cbcSMatt Macy 
7eda14cbcSMatt Macy 
8eda14cbcSMatt Macy #define ldo_c
9eda14cbcSMatt Macy #define LUA_CORE
10eda14cbcSMatt Macy 
11eda14cbcSMatt Macy #include <sys/lua/lua.h>
12eda14cbcSMatt Macy 
13eda14cbcSMatt Macy #include "lapi.h"
14eda14cbcSMatt Macy #include "ldebug.h"
15eda14cbcSMatt Macy #include "ldo.h"
16eda14cbcSMatt Macy #include "lfunc.h"
17eda14cbcSMatt Macy #include "lgc.h"
18eda14cbcSMatt Macy #include "lmem.h"
19eda14cbcSMatt Macy #include "lobject.h"
20eda14cbcSMatt Macy #include "lopcodes.h"
21eda14cbcSMatt Macy #include "lparser.h"
22eda14cbcSMatt Macy #include "lstate.h"
23eda14cbcSMatt Macy #include "lstring.h"
24eda14cbcSMatt Macy #include "ltable.h"
25eda14cbcSMatt Macy #include "ltm.h"
26eda14cbcSMatt Macy #include "lvm.h"
27eda14cbcSMatt Macy #include "lzio.h"
28eda14cbcSMatt Macy 
29eda14cbcSMatt Macy 
30eda14cbcSMatt Macy 
31eda14cbcSMatt Macy /* Return the number of bytes available on the stack. */
32eda14cbcSMatt Macy #if defined (_KERNEL) && defined(__linux__)
33eda14cbcSMatt Macy #include <asm/current.h>
34eda14cbcSMatt Macy static intptr_t stack_remaining(void) {
35f2d48b5eSRyan Libby   intptr_t local;
36f2d48b5eSRyan Libby   local = (intptr_t)&local - (intptr_t)current->stack;
37f2d48b5eSRyan Libby   return local;
38eda14cbcSMatt Macy }
39eda14cbcSMatt Macy #elif defined (_KERNEL) && defined(__FreeBSD__)
40eda14cbcSMatt Macy #include <sys/pcpu.h>
41eda14cbcSMatt Macy static intptr_t stack_remaining(void) {
42f2d48b5eSRyan Libby   intptr_t local;
43f2d48b5eSRyan Libby   local = (intptr_t)&local - (intptr_t)curthread->td_kstack;
44f2d48b5eSRyan Libby   return local;
45eda14cbcSMatt Macy }
46eda14cbcSMatt Macy #else
47eda14cbcSMatt Macy static intptr_t stack_remaining(void) {
48eda14cbcSMatt Macy   return INTPTR_MAX;
49eda14cbcSMatt Macy }
50eda14cbcSMatt Macy #endif
51eda14cbcSMatt Macy 
52eda14cbcSMatt Macy /*
53eda14cbcSMatt Macy ** {======================================================
54eda14cbcSMatt Macy ** Error-recovery functions
55eda14cbcSMatt Macy ** =======================================================
56eda14cbcSMatt Macy */
57eda14cbcSMatt Macy 
58eda14cbcSMatt Macy /*
59eda14cbcSMatt Macy ** LUAI_THROW/LUAI_TRY define how Lua does exception handling. By
60eda14cbcSMatt Macy ** default, Lua handles errors with exceptions when compiling as
61eda14cbcSMatt Macy ** C++ code, with _longjmp/_setjmp when asked to use them, and with
62eda14cbcSMatt Macy ** longjmp/setjmp otherwise.
63eda14cbcSMatt Macy */
64eda14cbcSMatt Macy #if !defined(LUAI_THROW)
65eda14cbcSMatt Macy 
66eda14cbcSMatt Macy #ifdef _KERNEL
67eda14cbcSMatt Macy 
68eda14cbcSMatt Macy #ifdef __linux__
69eda14cbcSMatt Macy #if defined(__i386__)
70eda14cbcSMatt Macy #define	JMP_BUF_CNT	6
71eda14cbcSMatt Macy #elif defined(__x86_64__)
72eda14cbcSMatt Macy #define	JMP_BUF_CNT	8
73eda14cbcSMatt Macy #elif defined(__sparc__) && defined(__arch64__)
74eda14cbcSMatt Macy #define	JMP_BUF_CNT	6
75eda14cbcSMatt Macy #elif defined(__powerpc__)
76eda14cbcSMatt Macy #define	JMP_BUF_CNT	26
77eda14cbcSMatt Macy #elif defined(__aarch64__)
78eda14cbcSMatt Macy #define	JMP_BUF_CNT	64
79eda14cbcSMatt Macy #elif defined(__arm__)
80eda14cbcSMatt Macy #define	JMP_BUF_CNT	65
81eda14cbcSMatt Macy #elif defined(__mips__)
82eda14cbcSMatt Macy #define JMP_BUF_CNT	12
83eda14cbcSMatt Macy #elif defined(__s390x__)
84eda14cbcSMatt Macy #define JMP_BUF_CNT	18
85eda14cbcSMatt Macy #elif defined(__riscv)
86eda14cbcSMatt Macy #define JMP_BUF_CNT     64
87eda14cbcSMatt Macy #else
88eda14cbcSMatt Macy #define	JMP_BUF_CNT	1
89eda14cbcSMatt Macy #endif
90eda14cbcSMatt Macy 
91eda14cbcSMatt Macy typedef	struct _label_t { long long unsigned val[JMP_BUF_CNT]; } label_t;
92eda14cbcSMatt Macy 
93eda14cbcSMatt Macy int setjmp(label_t *) __attribute__ ((__nothrow__));
94da5137abSMartin Matuska extern __attribute__((noreturn)) void longjmp(label_t *);
95eda14cbcSMatt Macy 
96eda14cbcSMatt Macy #define LUAI_THROW(L,c)		longjmp(&(c)->b)
97eda14cbcSMatt Macy #define LUAI_TRY(L,c,a)		if (setjmp(&(c)->b) == 0) { a }
98eda14cbcSMatt Macy #define luai_jmpbuf		label_t
99eda14cbcSMatt Macy 
100eda14cbcSMatt Macy /* unsupported arches will build but not be able to run lua programs */
101eda14cbcSMatt Macy #if JMP_BUF_CNT == 1
102eda14cbcSMatt Macy int setjmp (label_t *buf) {
103eda14cbcSMatt Macy 	return 1;
104eda14cbcSMatt Macy }
105eda14cbcSMatt Macy 
106eda14cbcSMatt Macy void longjmp (label_t * buf) {
107eda14cbcSMatt Macy 	for (;;);
108eda14cbcSMatt Macy }
109eda14cbcSMatt Macy #endif
110eda14cbcSMatt Macy #else
111eda14cbcSMatt Macy #define LUAI_THROW(L,c)		longjmp((c)->b, 1)
112eda14cbcSMatt Macy #define LUAI_TRY(L,c,a)		if (setjmp((c)->b) == 0) { a }
113eda14cbcSMatt Macy #define luai_jmpbuf		jmp_buf
114eda14cbcSMatt Macy #endif
115eda14cbcSMatt Macy 
116eda14cbcSMatt Macy #else /* _KERNEL */
117eda14cbcSMatt Macy 
118eda14cbcSMatt Macy #if defined(__cplusplus) && !defined(LUA_USE_LONGJMP)
119eda14cbcSMatt Macy /* C++ exceptions */
120eda14cbcSMatt Macy #define LUAI_THROW(L,c)		throw(c)
121eda14cbcSMatt Macy #define LUAI_TRY(L,c,a) \
122eda14cbcSMatt Macy 	try { a } catch(...) { if ((c)->status == 0) (c)->status = -1; }
123eda14cbcSMatt Macy #define luai_jmpbuf		int  /* dummy variable */
124eda14cbcSMatt Macy 
125eda14cbcSMatt Macy #elif defined(LUA_USE_ULONGJMP)
126eda14cbcSMatt Macy /* in Unix, try _longjmp/_setjmp (more efficient) */
127eda14cbcSMatt Macy #define LUAI_THROW(L,c)		_longjmp((c)->b, 1)
128eda14cbcSMatt Macy #define LUAI_TRY(L,c,a)		if (_setjmp((c)->b) == 0) { a }
129eda14cbcSMatt Macy #define luai_jmpbuf		jmp_buf
130eda14cbcSMatt Macy 
131eda14cbcSMatt Macy #else
132eda14cbcSMatt Macy /* default handling with long jumps */
133eda14cbcSMatt Macy #define LUAI_THROW(L,c)		longjmp((c)->b, 1)
134eda14cbcSMatt Macy #define LUAI_TRY(L,c,a)		if (setjmp((c)->b) == 0) { a }
135eda14cbcSMatt Macy #define luai_jmpbuf		jmp_buf
136eda14cbcSMatt Macy 
137eda14cbcSMatt Macy #endif
138eda14cbcSMatt Macy 
139eda14cbcSMatt Macy #endif /* _KERNEL */
140eda14cbcSMatt Macy 
141eda14cbcSMatt Macy #endif /* LUAI_THROW */
142eda14cbcSMatt Macy 
143eda14cbcSMatt Macy 
144eda14cbcSMatt Macy /* chain list of long jump buffers */
145eda14cbcSMatt Macy struct lua_longjmp {
146eda14cbcSMatt Macy   struct lua_longjmp *previous;
147eda14cbcSMatt Macy   luai_jmpbuf b;
148eda14cbcSMatt Macy   volatile int status;  /* error code */
149eda14cbcSMatt Macy };
150eda14cbcSMatt Macy 
151eda14cbcSMatt Macy 
152eda14cbcSMatt Macy static void seterrorobj (lua_State *L, int errcode, StkId oldtop) {
153eda14cbcSMatt Macy   switch (errcode) {
154eda14cbcSMatt Macy     case LUA_ERRMEM: {  /* memory error? */
155eda14cbcSMatt Macy       setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */
156eda14cbcSMatt Macy       break;
157eda14cbcSMatt Macy     }
158eda14cbcSMatt Macy     case LUA_ERRERR: {
159eda14cbcSMatt Macy       setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling"));
160eda14cbcSMatt Macy       break;
161eda14cbcSMatt Macy     }
162eda14cbcSMatt Macy     default: {
163eda14cbcSMatt Macy       setobjs2s(L, oldtop, L->top - 1);  /* error message on current top */
164eda14cbcSMatt Macy       break;
165eda14cbcSMatt Macy     }
166eda14cbcSMatt Macy   }
167eda14cbcSMatt Macy   L->top = oldtop + 1;
168eda14cbcSMatt Macy }
169eda14cbcSMatt Macy 
170a0b956f5SMartin Matuska /*
171a0b956f5SMartin Matuska  * Silence infinite recursion warning which was added to -Wall in gcc 12.1
172a0b956f5SMartin Matuska  */
173a0b956f5SMartin Matuska #if defined(HAVE_INFINITE_RECURSION)
174a0b956f5SMartin Matuska #pragma GCC diagnostic push
175a0b956f5SMartin Matuska #pragma GCC diagnostic ignored "-Winfinite-recursion"
176a0b956f5SMartin Matuska #endif
177eda14cbcSMatt Macy 
178eda14cbcSMatt Macy l_noret luaD_throw (lua_State *L, int errcode) {
179eda14cbcSMatt Macy   if (L->errorJmp) {  /* thread has an error handler? */
180eda14cbcSMatt Macy     L->errorJmp->status = errcode;  /* set status */
181eda14cbcSMatt Macy     LUAI_THROW(L, L->errorJmp);  /* jump to it */
182eda14cbcSMatt Macy   }
183eda14cbcSMatt Macy   else {  /* thread has no error handler */
184eda14cbcSMatt Macy     L->status = cast_byte(errcode);  /* mark it as dead */
185eda14cbcSMatt Macy     if (G(L)->mainthread->errorJmp) {  /* main thread has a handler? */
186eda14cbcSMatt Macy       setobjs2s(L, G(L)->mainthread->top++, L->top - 1);  /* copy error obj. */
187eda14cbcSMatt Macy       luaD_throw(G(L)->mainthread, errcode);  /* re-throw in main thread */
188eda14cbcSMatt Macy     }
189eda14cbcSMatt Macy     else {  /* no handler at all; abort */
190eda14cbcSMatt Macy       if (G(L)->panic) {  /* panic function? */
191eda14cbcSMatt Macy         lua_unlock(L);
192eda14cbcSMatt Macy         G(L)->panic(L);  /* call it (last chance to jump out) */
193eda14cbcSMatt Macy       }
194eda14cbcSMatt Macy       panic("no error handler");
195eda14cbcSMatt Macy     }
196eda14cbcSMatt Macy   }
197eda14cbcSMatt Macy }
198eda14cbcSMatt Macy 
199a0b956f5SMartin Matuska #if defined(HAVE_INFINITE_RECURSION)
200a0b956f5SMartin Matuska #pragma GCC diagnostic pop
201a0b956f5SMartin Matuska #endif
202a0b956f5SMartin Matuska 
203eda14cbcSMatt Macy 
204eda14cbcSMatt Macy int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
205eda14cbcSMatt Macy   unsigned short oldnCcalls = L->nCcalls;
206eda14cbcSMatt Macy   struct lua_longjmp lj;
207eda14cbcSMatt Macy   lj.status = LUA_OK;
208eda14cbcSMatt Macy   lj.previous = L->errorJmp;  /* chain new error handler */
209eda14cbcSMatt Macy   L->errorJmp = &lj;
210eda14cbcSMatt Macy   LUAI_TRY(L, &lj,
211eda14cbcSMatt Macy     (*f)(L, ud);
212eda14cbcSMatt Macy   );
213eda14cbcSMatt Macy   L->errorJmp = lj.previous;  /* restore old error handler */
214eda14cbcSMatt Macy   L->nCcalls = oldnCcalls;
215eda14cbcSMatt Macy   return lj.status;
216eda14cbcSMatt Macy }
217eda14cbcSMatt Macy 
218eda14cbcSMatt Macy /* }====================================================== */
219eda14cbcSMatt Macy 
220eda14cbcSMatt Macy 
221eda14cbcSMatt Macy static void correctstack (lua_State *L, TValue *oldstack) {
222eda14cbcSMatt Macy   CallInfo *ci;
223eda14cbcSMatt Macy   GCObject *up;
224eda14cbcSMatt Macy   L->top = (L->top - oldstack) + L->stack;
225eda14cbcSMatt Macy   for (up = L->openupval; up != NULL; up = up->gch.next)
226eda14cbcSMatt Macy     gco2uv(up)->v = (gco2uv(up)->v - oldstack) + L->stack;
227eda14cbcSMatt Macy   for (ci = L->ci; ci != NULL; ci = ci->previous) {
228eda14cbcSMatt Macy     ci->top = (ci->top - oldstack) + L->stack;
229eda14cbcSMatt Macy     ci->func = (ci->func - oldstack) + L->stack;
230eda14cbcSMatt Macy     if (isLua(ci))
231eda14cbcSMatt Macy       ci->u.l.base = (ci->u.l.base - oldstack) + L->stack;
232eda14cbcSMatt Macy   }
233eda14cbcSMatt Macy }
234eda14cbcSMatt Macy 
235eda14cbcSMatt Macy 
236eda14cbcSMatt Macy /* some space for error handling */
237eda14cbcSMatt Macy #define ERRORSTACKSIZE	(LUAI_MAXSTACK + 200)
238eda14cbcSMatt Macy 
239eda14cbcSMatt Macy 
240eda14cbcSMatt Macy void luaD_reallocstack (lua_State *L, int newsize) {
241eda14cbcSMatt Macy   TValue *oldstack = L->stack;
242eda14cbcSMatt Macy   int lim = L->stacksize;
243eda14cbcSMatt Macy   lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE);
244eda14cbcSMatt Macy   lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK);
245eda14cbcSMatt Macy   luaM_reallocvector(L, L->stack, L->stacksize, newsize, TValue);
246eda14cbcSMatt Macy   for (; lim < newsize; lim++)
247eda14cbcSMatt Macy     setnilvalue(L->stack + lim); /* erase new segment */
248eda14cbcSMatt Macy   L->stacksize = newsize;
249eda14cbcSMatt Macy   L->stack_last = L->stack + newsize - EXTRA_STACK;
250eda14cbcSMatt Macy   correctstack(L, oldstack);
251eda14cbcSMatt Macy }
252eda14cbcSMatt Macy 
253eda14cbcSMatt Macy 
254eda14cbcSMatt Macy void luaD_growstack (lua_State *L, int n) {
255eda14cbcSMatt Macy   int size = L->stacksize;
256eda14cbcSMatt Macy   if (size > LUAI_MAXSTACK)  /* error after extra size? */
257eda14cbcSMatt Macy     luaD_throw(L, LUA_ERRERR);
258eda14cbcSMatt Macy   else {
259eda14cbcSMatt Macy     int needed = cast_int(L->top - L->stack) + n + EXTRA_STACK;
260eda14cbcSMatt Macy     int newsize = 2 * size;
261eda14cbcSMatt Macy     if (newsize > LUAI_MAXSTACK) newsize = LUAI_MAXSTACK;
262eda14cbcSMatt Macy     if (newsize < needed) newsize = needed;
263eda14cbcSMatt Macy     if (newsize > LUAI_MAXSTACK) {  /* stack overflow? */
264eda14cbcSMatt Macy       luaD_reallocstack(L, ERRORSTACKSIZE);
265eda14cbcSMatt Macy       luaG_runerror(L, "stack overflow");
266eda14cbcSMatt Macy     }
267eda14cbcSMatt Macy     else
268eda14cbcSMatt Macy       luaD_reallocstack(L, newsize);
269eda14cbcSMatt Macy   }
270eda14cbcSMatt Macy }
271eda14cbcSMatt Macy 
272eda14cbcSMatt Macy 
273eda14cbcSMatt Macy static int stackinuse (lua_State *L) {
274eda14cbcSMatt Macy   CallInfo *ci;
275eda14cbcSMatt Macy   StkId lim = L->top;
276eda14cbcSMatt Macy   for (ci = L->ci; ci != NULL; ci = ci->previous) {
277eda14cbcSMatt Macy     lua_assert(ci->top <= L->stack_last);
278eda14cbcSMatt Macy     if (lim < ci->top) lim = ci->top;
279eda14cbcSMatt Macy   }
280eda14cbcSMatt Macy   return cast_int(lim - L->stack) + 1;  /* part of stack in use */
281eda14cbcSMatt Macy }
282eda14cbcSMatt Macy 
283eda14cbcSMatt Macy 
284eda14cbcSMatt Macy void luaD_shrinkstack (lua_State *L) {
285eda14cbcSMatt Macy   int inuse = stackinuse(L);
286eda14cbcSMatt Macy   int goodsize = inuse + (inuse / 8) + 2*EXTRA_STACK;
287eda14cbcSMatt Macy   if (goodsize > LUAI_MAXSTACK) goodsize = LUAI_MAXSTACK;
288eda14cbcSMatt Macy   if (inuse > LUAI_MAXSTACK ||  /* handling stack overflow? */
289eda14cbcSMatt Macy       goodsize >= L->stacksize)  /* would grow instead of shrink? */
290eda14cbcSMatt Macy     condmovestack(L);  /* don't change stack (change only for debugging) */
291eda14cbcSMatt Macy   else
292eda14cbcSMatt Macy     luaD_reallocstack(L, goodsize);  /* shrink it */
293eda14cbcSMatt Macy }
294eda14cbcSMatt Macy 
295eda14cbcSMatt Macy 
296eda14cbcSMatt Macy void luaD_hook (lua_State *L, int event, int line) {
297eda14cbcSMatt Macy   lua_Hook hook = L->hook;
298eda14cbcSMatt Macy   if (hook && L->allowhook) {
299eda14cbcSMatt Macy     CallInfo *ci = L->ci;
300eda14cbcSMatt Macy     ptrdiff_t top = savestack(L, L->top);
301eda14cbcSMatt Macy     ptrdiff_t ci_top = savestack(L, ci->top);
302eda14cbcSMatt Macy     lua_Debug ar;
303eda14cbcSMatt Macy     ar.event = event;
304eda14cbcSMatt Macy     ar.currentline = line;
305eda14cbcSMatt Macy     ar.i_ci = ci;
306eda14cbcSMatt Macy     luaD_checkstack(L, LUA_MINSTACK);  /* ensure minimum stack size */
307eda14cbcSMatt Macy     ci->top = L->top + LUA_MINSTACK;
308eda14cbcSMatt Macy     lua_assert(ci->top <= L->stack_last);
309eda14cbcSMatt Macy     L->allowhook = 0;  /* cannot call hooks inside a hook */
310eda14cbcSMatt Macy     ci->callstatus |= CIST_HOOKED;
311eda14cbcSMatt Macy     lua_unlock(L);
312eda14cbcSMatt Macy     (*hook)(L, &ar);
313eda14cbcSMatt Macy     lua_lock(L);
314eda14cbcSMatt Macy     lua_assert(!L->allowhook);
315eda14cbcSMatt Macy     L->allowhook = 1;
316eda14cbcSMatt Macy     ci->top = restorestack(L, ci_top);
317eda14cbcSMatt Macy     L->top = restorestack(L, top);
318eda14cbcSMatt Macy     ci->callstatus &= ~CIST_HOOKED;
319eda14cbcSMatt Macy   }
320eda14cbcSMatt Macy }
321eda14cbcSMatt Macy 
322eda14cbcSMatt Macy 
323eda14cbcSMatt Macy static void callhook (lua_State *L, CallInfo *ci) {
324eda14cbcSMatt Macy   int hook = LUA_HOOKCALL;
325eda14cbcSMatt Macy   ci->u.l.savedpc++;  /* hooks assume 'pc' is already incremented */
326eda14cbcSMatt Macy   if (isLua(ci->previous) &&
327eda14cbcSMatt Macy       GET_OPCODE(*(ci->previous->u.l.savedpc - 1)) == OP_TAILCALL) {
328eda14cbcSMatt Macy     ci->callstatus |= CIST_TAIL;
329eda14cbcSMatt Macy     hook = LUA_HOOKTAILCALL;
330eda14cbcSMatt Macy   }
331eda14cbcSMatt Macy   luaD_hook(L, hook, -1);
332eda14cbcSMatt Macy   ci->u.l.savedpc--;  /* correct 'pc' */
333eda14cbcSMatt Macy }
334eda14cbcSMatt Macy 
335eda14cbcSMatt Macy 
336eda14cbcSMatt Macy static StkId adjust_varargs (lua_State *L, Proto *p, int actual) {
337eda14cbcSMatt Macy   int i;
338eda14cbcSMatt Macy   int nfixargs = p->numparams;
339eda14cbcSMatt Macy   StkId base, fixed;
340eda14cbcSMatt Macy   lua_assert(actual >= nfixargs);
341eda14cbcSMatt Macy   /* move fixed parameters to final position */
342eda14cbcSMatt Macy   luaD_checkstack(L, p->maxstacksize);  /* check again for new 'base' */
343eda14cbcSMatt Macy   fixed = L->top - actual;  /* first fixed argument */
344eda14cbcSMatt Macy   base = L->top;  /* final position of first argument */
345eda14cbcSMatt Macy   for (i=0; i<nfixargs; i++) {
346eda14cbcSMatt Macy     setobjs2s(L, L->top++, fixed + i);
347eda14cbcSMatt Macy     setnilvalue(fixed + i);
348eda14cbcSMatt Macy   }
349eda14cbcSMatt Macy   return base;
350eda14cbcSMatt Macy }
351eda14cbcSMatt Macy 
352eda14cbcSMatt Macy 
353eda14cbcSMatt Macy static StkId tryfuncTM (lua_State *L, StkId func) {
354eda14cbcSMatt Macy   const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL);
355eda14cbcSMatt Macy   StkId p;
356eda14cbcSMatt Macy   ptrdiff_t funcr = savestack(L, func);
357eda14cbcSMatt Macy   if (!ttisfunction(tm))
358eda14cbcSMatt Macy     luaG_typeerror(L, func, "call");
359eda14cbcSMatt Macy   /* Open a hole inside the stack at `func' */
360eda14cbcSMatt Macy   for (p = L->top; p > func; p--) setobjs2s(L, p, p-1);
361eda14cbcSMatt Macy   incr_top(L);
362eda14cbcSMatt Macy   func = restorestack(L, funcr);  /* previous call may change stack */
363eda14cbcSMatt Macy   setobj2s(L, func, tm);  /* tag method is the new function to be called */
364eda14cbcSMatt Macy   return func;
365eda14cbcSMatt Macy }
366eda14cbcSMatt Macy 
367eda14cbcSMatt Macy 
368eda14cbcSMatt Macy 
369eda14cbcSMatt Macy #define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L)))
370eda14cbcSMatt Macy 
371eda14cbcSMatt Macy 
372eda14cbcSMatt Macy /*
373eda14cbcSMatt Macy ** returns true if function has been executed (C function)
374eda14cbcSMatt Macy */
375eda14cbcSMatt Macy int luaD_precall (lua_State *L, StkId func, int nresults) {
376eda14cbcSMatt Macy   lua_CFunction f;
377eda14cbcSMatt Macy   CallInfo *ci;
378eda14cbcSMatt Macy   int n;  /* number of arguments (Lua) or returns (C) */
379eda14cbcSMatt Macy   ptrdiff_t funcr = savestack(L, func);
380eda14cbcSMatt Macy   switch (ttype(func)) {
381eda14cbcSMatt Macy     case LUA_TLCF:  /* light C function */
382eda14cbcSMatt Macy       f = fvalue(func);
383eda14cbcSMatt Macy       goto Cfunc;
384eda14cbcSMatt Macy     case LUA_TCCL: {  /* C closure */
385eda14cbcSMatt Macy       f = clCvalue(func)->f;
386eda14cbcSMatt Macy      Cfunc:
387eda14cbcSMatt Macy       luaD_checkstack(L, LUA_MINSTACK);  /* ensure minimum stack size */
388eda14cbcSMatt Macy       ci = next_ci(L);  /* now 'enter' new function */
389eda14cbcSMatt Macy       ci->nresults = nresults;
390eda14cbcSMatt Macy       ci->func = restorestack(L, funcr);
391eda14cbcSMatt Macy       ci->top = L->top + LUA_MINSTACK;
392eda14cbcSMatt Macy       lua_assert(ci->top <= L->stack_last);
393eda14cbcSMatt Macy       ci->callstatus = 0;
394eda14cbcSMatt Macy       luaC_checkGC(L);  /* stack grow uses memory */
395eda14cbcSMatt Macy       if (L->hookmask & LUA_MASKCALL)
396eda14cbcSMatt Macy         luaD_hook(L, LUA_HOOKCALL, -1);
397eda14cbcSMatt Macy       lua_unlock(L);
398eda14cbcSMatt Macy       n = (*f)(L);  /* do the actual call */
399eda14cbcSMatt Macy       lua_lock(L);
400eda14cbcSMatt Macy       api_checknelems(L, n);
401eda14cbcSMatt Macy       luaD_poscall(L, L->top - n);
402eda14cbcSMatt Macy       return 1;
403eda14cbcSMatt Macy     }
404eda14cbcSMatt Macy     case LUA_TLCL: {  /* Lua function: prepare its call */
405eda14cbcSMatt Macy       StkId base;
406eda14cbcSMatt Macy       Proto *p = clLvalue(func)->p;
407eda14cbcSMatt Macy       n = cast_int(L->top - func) - 1;  /* number of real arguments */
408be181ee2SMartin Matuska       luaD_checkstack(L, p->maxstacksize + p->numparams);
409eda14cbcSMatt Macy       for (; n < p->numparams; n++)
410eda14cbcSMatt Macy         setnilvalue(L->top++);  /* complete missing arguments */
411eda14cbcSMatt Macy       if (!p->is_vararg) {
412eda14cbcSMatt Macy         func = restorestack(L, funcr);
413eda14cbcSMatt Macy         base = func + 1;
414eda14cbcSMatt Macy       }
415eda14cbcSMatt Macy       else {
416eda14cbcSMatt Macy         base = adjust_varargs(L, p, n);
417eda14cbcSMatt Macy         func = restorestack(L, funcr);  /* previous call can change stack */
418eda14cbcSMatt Macy       }
419eda14cbcSMatt Macy       ci = next_ci(L);  /* now 'enter' new function */
420eda14cbcSMatt Macy       ci->nresults = nresults;
421eda14cbcSMatt Macy       ci->func = func;
422eda14cbcSMatt Macy       ci->u.l.base = base;
423eda14cbcSMatt Macy       ci->top = base + p->maxstacksize;
424eda14cbcSMatt Macy       lua_assert(ci->top <= L->stack_last);
425eda14cbcSMatt Macy       ci->u.l.savedpc = p->code;  /* starting point */
426eda14cbcSMatt Macy       ci->callstatus = CIST_LUA;
427eda14cbcSMatt Macy       L->top = ci->top;
428eda14cbcSMatt Macy       luaC_checkGC(L);  /* stack grow uses memory */
429eda14cbcSMatt Macy       if (L->hookmask & LUA_MASKCALL)
430eda14cbcSMatt Macy         callhook(L, ci);
431eda14cbcSMatt Macy       return 0;
432eda14cbcSMatt Macy     }
433eda14cbcSMatt Macy     default: {  /* not a function */
434eda14cbcSMatt Macy       func = tryfuncTM(L, func);  /* retry with 'function' tag method */
435eda14cbcSMatt Macy       return luaD_precall(L, func, nresults);  /* now it must be a function */
436eda14cbcSMatt Macy     }
437eda14cbcSMatt Macy   }
438eda14cbcSMatt Macy }
439eda14cbcSMatt Macy 
440eda14cbcSMatt Macy 
441eda14cbcSMatt Macy int luaD_poscall (lua_State *L, StkId firstResult) {
442eda14cbcSMatt Macy   StkId res;
443eda14cbcSMatt Macy   int wanted, i;
444eda14cbcSMatt Macy   CallInfo *ci = L->ci;
445eda14cbcSMatt Macy   if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) {
446eda14cbcSMatt Macy     if (L->hookmask & LUA_MASKRET) {
447eda14cbcSMatt Macy       ptrdiff_t fr = savestack(L, firstResult);  /* hook may change stack */
448eda14cbcSMatt Macy       luaD_hook(L, LUA_HOOKRET, -1);
449eda14cbcSMatt Macy       firstResult = restorestack(L, fr);
450eda14cbcSMatt Macy     }
451eda14cbcSMatt Macy     L->oldpc = ci->previous->u.l.savedpc;  /* 'oldpc' for caller function */
452eda14cbcSMatt Macy   }
453eda14cbcSMatt Macy   res = ci->func;  /* res == final position of 1st result */
454eda14cbcSMatt Macy   wanted = ci->nresults;
455*dbd5678dSMartin Matuska   L->ci = ci->previous;  /* back to caller */
456eda14cbcSMatt Macy   /* move results to correct place */
457eda14cbcSMatt Macy   for (i = wanted; i != 0 && firstResult < L->top; i--)
458eda14cbcSMatt Macy     setobjs2s(L, res++, firstResult++);
459eda14cbcSMatt Macy   while (i-- > 0)
460eda14cbcSMatt Macy     setnilvalue(res++);
461eda14cbcSMatt Macy   L->top = res;
462eda14cbcSMatt Macy   return (wanted - LUA_MULTRET);  /* 0 iff wanted == LUA_MULTRET */
463eda14cbcSMatt Macy }
464eda14cbcSMatt Macy 
465eda14cbcSMatt Macy 
466eda14cbcSMatt Macy /*
467eda14cbcSMatt Macy ** Call a function (C or Lua). The function to be called is at *func.
468eda14cbcSMatt Macy ** The arguments are on the stack, right after the function.
469eda14cbcSMatt Macy ** When returns, all the results are on the stack, starting at the original
470eda14cbcSMatt Macy ** function position.
471eda14cbcSMatt Macy */
472eda14cbcSMatt Macy void luaD_call (lua_State *L, StkId func, int nResults, int allowyield) {
473eda14cbcSMatt Macy   if (++L->nCcalls >= LUAI_MAXCCALLS) {
474eda14cbcSMatt Macy     if (L->nCcalls == LUAI_MAXCCALLS)
475eda14cbcSMatt Macy       luaG_runerror(L, "C stack overflow");
476eda14cbcSMatt Macy     else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))
477eda14cbcSMatt Macy       luaD_throw(L, LUA_ERRERR);  /* error while handling stack error */
478eda14cbcSMatt Macy   }
479eda14cbcSMatt Macy   intptr_t remaining = stack_remaining();
480eda14cbcSMatt Macy   if (L->runerror == 0 && remaining < LUAI_MINCSTACK)
481eda14cbcSMatt Macy     luaG_runerror(L, "C stack overflow");
482eda14cbcSMatt Macy   if (L->runerror != 0 && remaining < LUAI_MINCSTACK / 2)
483eda14cbcSMatt Macy     luaD_throw(L, LUA_ERRERR);  /* error while handling stack error */
484eda14cbcSMatt Macy   if (!allowyield) L->nny++;
485eda14cbcSMatt Macy   if (!luaD_precall(L, func, nResults))  /* is a Lua function? */
486eda14cbcSMatt Macy     luaV_execute(L);  /* call it */
487eda14cbcSMatt Macy   if (!allowyield) L->nny--;
488eda14cbcSMatt Macy   L->nCcalls--;
489eda14cbcSMatt Macy }
490eda14cbcSMatt Macy 
491eda14cbcSMatt Macy 
492eda14cbcSMatt Macy static void finishCcall (lua_State *L) {
493eda14cbcSMatt Macy   CallInfo *ci = L->ci;
494eda14cbcSMatt Macy   int n;
495eda14cbcSMatt Macy   lua_assert(ci->u.c.k != NULL);  /* must have a continuation */
496eda14cbcSMatt Macy   lua_assert(L->nny == 0);
497eda14cbcSMatt Macy   if (ci->callstatus & CIST_YPCALL) {  /* was inside a pcall? */
498eda14cbcSMatt Macy     ci->callstatus &= ~CIST_YPCALL;  /* finish 'lua_pcall' */
499eda14cbcSMatt Macy     L->errfunc = ci->u.c.old_errfunc;
500eda14cbcSMatt Macy   }
501eda14cbcSMatt Macy   /* finish 'lua_callk'/'lua_pcall' */
502eda14cbcSMatt Macy   adjustresults(L, ci->nresults);
503eda14cbcSMatt Macy   /* call continuation function */
504eda14cbcSMatt Macy   if (!(ci->callstatus & CIST_STAT))  /* no call status? */
505eda14cbcSMatt Macy     ci->u.c.status = LUA_YIELD;  /* 'default' status */
506eda14cbcSMatt Macy   lua_assert(ci->u.c.status != LUA_OK);
507eda14cbcSMatt Macy   ci->callstatus = (ci->callstatus & ~(CIST_YPCALL | CIST_STAT)) | CIST_YIELDED;
508eda14cbcSMatt Macy   lua_unlock(L);
509eda14cbcSMatt Macy   n = (*ci->u.c.k)(L);
510eda14cbcSMatt Macy   lua_lock(L);
511eda14cbcSMatt Macy   api_checknelems(L, n);
512eda14cbcSMatt Macy   /* finish 'luaD_precall' */
513eda14cbcSMatt Macy   luaD_poscall(L, L->top - n);
514eda14cbcSMatt Macy }
515eda14cbcSMatt Macy 
516eda14cbcSMatt Macy 
517eda14cbcSMatt Macy static void unroll (lua_State *L, void *ud) {
518eda14cbcSMatt Macy   UNUSED(ud);
519eda14cbcSMatt Macy   for (;;) {
520eda14cbcSMatt Macy     if (L->ci == &L->base_ci)  /* stack is empty? */
521eda14cbcSMatt Macy       return;  /* coroutine finished normally */
522eda14cbcSMatt Macy     if (!isLua(L->ci))  /* C function? */
523eda14cbcSMatt Macy       finishCcall(L);
524eda14cbcSMatt Macy     else {  /* Lua function */
525eda14cbcSMatt Macy       luaV_finishOp(L);  /* finish interrupted instruction */
526eda14cbcSMatt Macy       luaV_execute(L);  /* execute down to higher C 'boundary' */
527eda14cbcSMatt Macy     }
528eda14cbcSMatt Macy   }
529eda14cbcSMatt Macy }
530eda14cbcSMatt Macy 
531eda14cbcSMatt Macy 
532eda14cbcSMatt Macy /*
533eda14cbcSMatt Macy ** check whether thread has a suspended protected call
534eda14cbcSMatt Macy */
535eda14cbcSMatt Macy static CallInfo *findpcall (lua_State *L) {
536eda14cbcSMatt Macy   CallInfo *ci;
537eda14cbcSMatt Macy   for (ci = L->ci; ci != NULL; ci = ci->previous) {  /* search for a pcall */
538eda14cbcSMatt Macy     if (ci->callstatus & CIST_YPCALL)
539eda14cbcSMatt Macy       return ci;
540eda14cbcSMatt Macy   }
541eda14cbcSMatt Macy   return NULL;  /* no pending pcall */
542eda14cbcSMatt Macy }
543eda14cbcSMatt Macy 
544eda14cbcSMatt Macy 
545eda14cbcSMatt Macy static int recover (lua_State *L, int status) {
546eda14cbcSMatt Macy   StkId oldtop;
547eda14cbcSMatt Macy   CallInfo *ci = findpcall(L);
548eda14cbcSMatt Macy   if (ci == NULL) return 0;  /* no recovery point */
549eda14cbcSMatt Macy   /* "finish" luaD_pcall */
550eda14cbcSMatt Macy   oldtop = restorestack(L, ci->extra);
551eda14cbcSMatt Macy   luaF_close(L, oldtop);
552eda14cbcSMatt Macy   seterrorobj(L, status, oldtop);
553eda14cbcSMatt Macy   L->ci = ci;
554eda14cbcSMatt Macy   L->allowhook = ci->u.c.old_allowhook;
555eda14cbcSMatt Macy   L->nny = 0;  /* should be zero to be yieldable */
556eda14cbcSMatt Macy   luaD_shrinkstack(L);
557eda14cbcSMatt Macy   L->errfunc = ci->u.c.old_errfunc;
558eda14cbcSMatt Macy   ci->callstatus |= CIST_STAT;  /* call has error status */
559eda14cbcSMatt Macy   ci->u.c.status = status;  /* (here it is) */
560eda14cbcSMatt Macy   return 1;  /* continue running the coroutine */
561eda14cbcSMatt Macy }
562eda14cbcSMatt Macy 
563eda14cbcSMatt Macy 
564eda14cbcSMatt Macy /*
565eda14cbcSMatt Macy ** signal an error in the call to 'resume', not in the execution of the
566eda14cbcSMatt Macy ** coroutine itself. (Such errors should not be handled by any coroutine
567eda14cbcSMatt Macy ** error handler and should not kill the coroutine.)
568eda14cbcSMatt Macy */
569eda14cbcSMatt Macy static l_noret resume_error (lua_State *L, const char *msg, StkId firstArg) {
570eda14cbcSMatt Macy   L->top = firstArg;  /* remove args from the stack */
571eda14cbcSMatt Macy   setsvalue2s(L, L->top, luaS_new(L, msg));  /* push error message */
572eda14cbcSMatt Macy   api_incr_top(L);
573eda14cbcSMatt Macy   luaD_throw(L, -1);  /* jump back to 'lua_resume' */
574eda14cbcSMatt Macy }
575eda14cbcSMatt Macy 
576eda14cbcSMatt Macy 
577eda14cbcSMatt Macy /*
578eda14cbcSMatt Macy ** do the work for 'lua_resume' in protected mode
579eda14cbcSMatt Macy */
580eda14cbcSMatt Macy static void resume_cb (lua_State *L, void *ud) {
581eda14cbcSMatt Macy   int nCcalls = L->nCcalls;
582eda14cbcSMatt Macy   StkId firstArg = cast(StkId, ud);
583eda14cbcSMatt Macy   CallInfo *ci = L->ci;
584eda14cbcSMatt Macy   if (nCcalls >= LUAI_MAXCCALLS)
585eda14cbcSMatt Macy     resume_error(L, "C stack overflow", firstArg);
586eda14cbcSMatt Macy   if (L->status == LUA_OK) {  /* may be starting a coroutine */
587eda14cbcSMatt Macy     if (ci != &L->base_ci)  /* not in base level? */
588eda14cbcSMatt Macy       resume_error(L, "cannot resume non-suspended coroutine", firstArg);
589eda14cbcSMatt Macy     /* coroutine is in base level; start running it */
590eda14cbcSMatt Macy     if (!luaD_precall(L, firstArg - 1, LUA_MULTRET))  /* Lua function? */
591eda14cbcSMatt Macy       luaV_execute(L);  /* call it */
592eda14cbcSMatt Macy   }
593eda14cbcSMatt Macy   else if (L->status != LUA_YIELD)
594eda14cbcSMatt Macy     resume_error(L, "cannot resume dead coroutine", firstArg);
595eda14cbcSMatt Macy   else {  /* resuming from previous yield */
596eda14cbcSMatt Macy     L->status = LUA_OK;
597eda14cbcSMatt Macy     ci->func = restorestack(L, ci->extra);
598eda14cbcSMatt Macy     if (isLua(ci))  /* yielded inside a hook? */
599eda14cbcSMatt Macy       luaV_execute(L);  /* just continue running Lua code */
600eda14cbcSMatt Macy     else {  /* 'common' yield */
601eda14cbcSMatt Macy       if (ci->u.c.k != NULL) {  /* does it have a continuation? */
602eda14cbcSMatt Macy         int n;
603eda14cbcSMatt Macy         ci->u.c.status = LUA_YIELD;  /* 'default' status */
604eda14cbcSMatt Macy         ci->callstatus |= CIST_YIELDED;
605eda14cbcSMatt Macy         lua_unlock(L);
606eda14cbcSMatt Macy         n = (*ci->u.c.k)(L);  /* call continuation */
607eda14cbcSMatt Macy         lua_lock(L);
608eda14cbcSMatt Macy         api_checknelems(L, n);
609eda14cbcSMatt Macy         firstArg = L->top - n;  /* yield results come from continuation */
610eda14cbcSMatt Macy       }
611eda14cbcSMatt Macy       luaD_poscall(L, firstArg);  /* finish 'luaD_precall' */
612eda14cbcSMatt Macy     }
613eda14cbcSMatt Macy     unroll(L, NULL);
614eda14cbcSMatt Macy   }
615eda14cbcSMatt Macy   lua_assert(nCcalls == L->nCcalls);
616eda14cbcSMatt Macy }
617eda14cbcSMatt Macy 
618eda14cbcSMatt Macy 
619eda14cbcSMatt Macy LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) {
620eda14cbcSMatt Macy   int status;
621eda14cbcSMatt Macy   int oldnny = L->nny;  /* save 'nny' */
622eda14cbcSMatt Macy   lua_lock(L);
623eda14cbcSMatt Macy   luai_userstateresume(L, nargs);
624eda14cbcSMatt Macy   L->nCcalls = (from) ? from->nCcalls + 1 : 1;
625eda14cbcSMatt Macy   L->nny = 0;  /* allow yields */
626eda14cbcSMatt Macy   api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs);
627eda14cbcSMatt Macy   status = luaD_rawrunprotected(L, resume_cb, L->top - nargs);
628eda14cbcSMatt Macy   if (status == -1)  /* error calling 'lua_resume'? */
629eda14cbcSMatt Macy     status = LUA_ERRRUN;
630eda14cbcSMatt Macy   else {  /* yield or regular error */
631eda14cbcSMatt Macy     while (status != LUA_OK && status != LUA_YIELD) {  /* error? */
632eda14cbcSMatt Macy       if (recover(L, status))  /* recover point? */
633eda14cbcSMatt Macy         status = luaD_rawrunprotected(L, unroll, NULL);  /* run continuation */
634eda14cbcSMatt Macy       else {  /* unrecoverable error */
635eda14cbcSMatt Macy         L->status = cast_byte(status);  /* mark thread as `dead' */
636eda14cbcSMatt Macy         seterrorobj(L, status, L->top);
637eda14cbcSMatt Macy         L->ci->top = L->top;
638eda14cbcSMatt Macy         break;
639eda14cbcSMatt Macy       }
640eda14cbcSMatt Macy     }
641eda14cbcSMatt Macy     lua_assert(status == L->status);
642eda14cbcSMatt Macy   }
643eda14cbcSMatt Macy   L->nny = oldnny;  /* restore 'nny' */
644eda14cbcSMatt Macy   L->nCcalls--;
645eda14cbcSMatt Macy   lua_assert(L->nCcalls == ((from) ? from->nCcalls : 0));
646eda14cbcSMatt Macy   lua_unlock(L);
647eda14cbcSMatt Macy   return status;
648eda14cbcSMatt Macy }
649eda14cbcSMatt Macy 
650eda14cbcSMatt Macy 
651eda14cbcSMatt Macy LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k) {
652eda14cbcSMatt Macy   CallInfo *ci = L->ci;
653eda14cbcSMatt Macy   luai_userstateyield(L, nresults);
654eda14cbcSMatt Macy   lua_lock(L);
655eda14cbcSMatt Macy   api_checknelems(L, nresults);
656eda14cbcSMatt Macy   if (L->nny > 0) {
657eda14cbcSMatt Macy     if (L != G(L)->mainthread)
658eda14cbcSMatt Macy       luaG_runerror(L, "attempt to yield across a C-call boundary");
659eda14cbcSMatt Macy     else
660eda14cbcSMatt Macy       luaG_runerror(L, "attempt to yield from outside a coroutine");
661eda14cbcSMatt Macy   }
662eda14cbcSMatt Macy   L->status = LUA_YIELD;
663eda14cbcSMatt Macy   ci->extra = savestack(L, ci->func);  /* save current 'func' */
664eda14cbcSMatt Macy   if (isLua(ci)) {  /* inside a hook? */
665eda14cbcSMatt Macy     api_check(L, k == NULL, "hooks cannot continue after yielding");
666eda14cbcSMatt Macy   }
667eda14cbcSMatt Macy   else {
668eda14cbcSMatt Macy     if ((ci->u.c.k = k) != NULL)  /* is there a continuation? */
669eda14cbcSMatt Macy       ci->u.c.ctx = ctx;  /* save context */
670eda14cbcSMatt Macy     ci->func = L->top - nresults - 1;  /* protect stack below results */
671eda14cbcSMatt Macy     luaD_throw(L, LUA_YIELD);
672eda14cbcSMatt Macy   }
673eda14cbcSMatt Macy   lua_assert(ci->callstatus & CIST_HOOKED);  /* must be inside a hook */
674eda14cbcSMatt Macy   lua_unlock(L);
675eda14cbcSMatt Macy   return 0;  /* return to 'luaD_hook' */
676eda14cbcSMatt Macy }
677eda14cbcSMatt Macy 
678eda14cbcSMatt Macy 
679eda14cbcSMatt Macy int luaD_pcall (lua_State *L, Pfunc func, void *u,
680eda14cbcSMatt Macy                 ptrdiff_t old_top, ptrdiff_t ef) {
681eda14cbcSMatt Macy   int status;
682eda14cbcSMatt Macy   CallInfo *old_ci = L->ci;
683eda14cbcSMatt Macy   lu_byte old_allowhooks = L->allowhook;
684eda14cbcSMatt Macy   unsigned short old_nny = L->nny;
685eda14cbcSMatt Macy   ptrdiff_t old_errfunc = L->errfunc;
686eda14cbcSMatt Macy   L->errfunc = ef;
687eda14cbcSMatt Macy   status = luaD_rawrunprotected(L, func, u);
688eda14cbcSMatt Macy   if (status != LUA_OK) {  /* an error occurred? */
689eda14cbcSMatt Macy     StkId oldtop = restorestack(L, old_top);
690eda14cbcSMatt Macy     luaF_close(L, oldtop);  /* close possible pending closures */
691eda14cbcSMatt Macy     seterrorobj(L, status, oldtop);
692eda14cbcSMatt Macy     L->ci = old_ci;
693eda14cbcSMatt Macy     L->allowhook = old_allowhooks;
694eda14cbcSMatt Macy     L->nny = old_nny;
695eda14cbcSMatt Macy     luaD_shrinkstack(L);
696eda14cbcSMatt Macy   }
697eda14cbcSMatt Macy   L->errfunc = old_errfunc;
698eda14cbcSMatt Macy   return status;
699eda14cbcSMatt Macy }
700eda14cbcSMatt Macy 
701eda14cbcSMatt Macy 
702eda14cbcSMatt Macy 
703eda14cbcSMatt Macy /*
704eda14cbcSMatt Macy ** Execute a protected parser.
705eda14cbcSMatt Macy */
706eda14cbcSMatt Macy struct SParser {  /* data to `f_parser' */
707eda14cbcSMatt Macy   ZIO *z;
708eda14cbcSMatt Macy   Mbuffer buff;  /* dynamic structure used by the scanner */
709eda14cbcSMatt Macy   Dyndata dyd;  /* dynamic structures used by the parser */
710eda14cbcSMatt Macy   const char *mode;
711eda14cbcSMatt Macy   const char *name;
712eda14cbcSMatt Macy };
713eda14cbcSMatt Macy 
714eda14cbcSMatt Macy 
715eda14cbcSMatt Macy static void checkmode (lua_State *L, const char *mode, const char *x) {
716eda14cbcSMatt Macy   if (mode && strchr(mode, x[0]) == NULL) {
717eda14cbcSMatt Macy     luaO_pushfstring(L,
718eda14cbcSMatt Macy        "attempt to load a %s chunk (mode is " LUA_QS ")", x, mode);
719eda14cbcSMatt Macy     luaD_throw(L, LUA_ERRSYNTAX);
720eda14cbcSMatt Macy   }
721eda14cbcSMatt Macy }
722eda14cbcSMatt Macy 
723eda14cbcSMatt Macy 
724eda14cbcSMatt Macy static void f_parser (lua_State *L, void *ud) {
725eda14cbcSMatt Macy   int i;
726eda14cbcSMatt Macy   Closure *cl;
727eda14cbcSMatt Macy   struct SParser *p = cast(struct SParser *, ud);
728eda14cbcSMatt Macy   int c = zgetc(p->z);  /* read first character */
729eda14cbcSMatt Macy   lua_assert(c != LUA_SIGNATURE[0]);	/* binary not supported */
730eda14cbcSMatt Macy   checkmode(L, p->mode, "text");
731eda14cbcSMatt Macy   cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c);
732eda14cbcSMatt Macy   lua_assert(cl->l.nupvalues == cl->l.p->sizeupvalues);
733eda14cbcSMatt Macy   for (i = 0; i < cl->l.nupvalues; i++) {  /* initialize upvalues */
734eda14cbcSMatt Macy     UpVal *up = luaF_newupval(L);
735eda14cbcSMatt Macy     cl->l.upvals[i] = up;
736eda14cbcSMatt Macy     luaC_objbarrier(L, cl, up);
737eda14cbcSMatt Macy   }
738eda14cbcSMatt Macy }
739eda14cbcSMatt Macy 
740eda14cbcSMatt Macy 
741eda14cbcSMatt Macy int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
742eda14cbcSMatt Macy                                         const char *mode) {
743eda14cbcSMatt Macy   struct SParser p;
744eda14cbcSMatt Macy   int status;
745eda14cbcSMatt Macy   L->nny++;  /* cannot yield during parsing */
746eda14cbcSMatt Macy   p.z = z; p.name = name; p.mode = mode;
747eda14cbcSMatt Macy   p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0;
748eda14cbcSMatt Macy   p.dyd.gt.arr = NULL; p.dyd.gt.size = 0;
749eda14cbcSMatt Macy   p.dyd.label.arr = NULL; p.dyd.label.size = 0;
750eda14cbcSMatt Macy   luaZ_initbuffer(L, &p.buff);
751eda14cbcSMatt Macy   status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc);
752eda14cbcSMatt Macy   luaZ_freebuffer(L, &p.buff);
753eda14cbcSMatt Macy   luaM_freearray(L, p.dyd.actvar.arr, p.dyd.actvar.size);
754eda14cbcSMatt Macy   luaM_freearray(L, p.dyd.gt.arr, p.dyd.gt.size);
755eda14cbcSMatt Macy   luaM_freearray(L, p.dyd.label.arr, p.dyd.label.size);
756eda14cbcSMatt Macy   L->nny--;
757eda14cbcSMatt Macy   return status;
758eda14cbcSMatt Macy }
759