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