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