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