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