#define ldo_c
#define LUA_CORE
#include "lprefix.h"
#include <setjmp.h>
#include <stdlib.h>
#include <string.h>
#include "lua.h"
#include "lapi.h"
#include "ldebug.h"
#include "ldo.h"
#include "lfunc.h"
#include "lgc.h"
#include "lmem.h"
#include "lobject.h"
#include "lopcodes.h"
#include "lparser.h"
#include "lstate.h"
#include "lstring.h"
#include "ltable.h"
#include "ltm.h"
#include "lundump.h"
#include "lvm.h"
#include "lzio.h"
#define errorstatus(s) ((s) > LUA_YIELD)
#if !defined(luai_userstateresume)
#define luai_userstateresume(L,n) ((void)L)
#endif
#if !defined(luai_userstateyield)
#define luai_userstateyield(L,n) ((void)L)
#endif
typedef struct lua_longjmp {
struct lua_longjmp *previous;
jmp_buf b;
volatile TStatus status;
} lua_longjmp;
#if !defined(LUAI_THROW)
#if defined(__cplusplus) && !defined(LUA_USE_LONGJMP)
#define LUAI_THROW(L,c) throw(c)
static void LUAI_TRY (lua_State *L, lua_longjmp *c, Pfunc f, void *ud) {
try {
f(L, ud);
}
catch (lua_longjmp *c1) {
if (c1 != c)
throw;
}
catch (...) {
c->status = -1;
}
}
#elif defined(LUA_USE_POSIX)
#define LUAI_THROW(L,c) _longjmp((c)->b, 1)
#define LUAI_TRY(L,c,f,ud) if (_setjmp((c)->b) == 0) ((f)(L, ud))
#else
#define LUAI_THROW(L,c) longjmp((c)->b, 1)
#define LUAI_TRY(L,c,f,ud) if (setjmp((c)->b) == 0) ((f)(L, ud))
#endif
#endif
void luaD_seterrorobj (lua_State *L, TStatus errcode, StkId oldtop) {
if (errcode == LUA_ERRMEM) {
setsvalue2s(L, oldtop, G(L)->memerrmsg);
}
else {
lua_assert(errorstatus(errcode));
lua_assert(!ttisnil(s2v(L->top.p - 1)));
setobjs2s(L, oldtop, L->top.p - 1);
}
L->top.p = oldtop + 1;
}
l_noret luaD_throw (lua_State *L, TStatus errcode) {
if (L->errorJmp) {
L->errorJmp->status = errcode;
LUAI_THROW(L, L->errorJmp);
}
else {
global_State *g = G(L);
lua_State *mainth = mainthread(g);
errcode = luaE_resetthread(L, errcode);
L->status = errcode;
if (mainth->errorJmp) {
setobjs2s(L, mainth->top.p++, L->top.p - 1);
luaD_throw(mainth, errcode);
}
else {
if (g->panic) {
lua_unlock(L);
g->panic(L);
}
abort();
}
}
}
l_noret luaD_throwbaselevel (lua_State *L, TStatus errcode) {
if (L->errorJmp) {
while (L->errorJmp->previous != NULL)
L->errorJmp = L->errorJmp->previous;
}
luaD_throw(L, errcode);
}
TStatus luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
l_uint32 oldnCcalls = L->nCcalls;
lua_longjmp lj;
lj.status = LUA_OK;
lj.previous = L->errorJmp;
L->errorJmp = &lj;
LUAI_TRY(L, &lj, f, ud);
L->errorJmp = lj.previous;
L->nCcalls = oldnCcalls;
return lj.status;
}
#define STACKERRSPACE 200
#if !defined(LUAI_MAXSTACK)
#if 1000000 < (INT_MAX / 2)
#define LUAI_MAXSTACK 1000000
#else
#define LUAI_MAXSTACK (INT_MAX / 2u)
#endif
#endif
#define MAXSTACK_BYSIZET ((MAX_SIZET / sizeof(StackValue)) - STACKERRSPACE)
#define MAXSTACK cast_int(LUAI_MAXSTACK < MAXSTACK_BYSIZET \
? LUAI_MAXSTACK : MAXSTACK_BYSIZET)
#define ERRORSTACKSIZE (MAXSTACK + STACKERRSPACE)
l_noret luaD_errerr (lua_State *L) {
TString *msg = luaS_newliteral(L, "error in error handling");
setsvalue2s(L, L->top.p, msg);
L->top.p++;
luaD_throw(L, LUA_ERRERR);
}
int luaD_checkminstack (lua_State *L) {
return ((stacksize(L) < MAXSTACK - BASIC_STACK_SIZE) &&
(getCcalls(L) < LUAI_MAXCCALLS - 2));
}
#if !defined(LUAI_STRICT_ADDRESS)
#define LUAI_STRICT_ADDRESS 1
#endif
#if LUAI_STRICT_ADDRESS
static void relstack (lua_State *L) {
CallInfo *ci;
UpVal *up;
L->top.offset = savestack(L, L->top.p);
L->tbclist.offset = savestack(L, L->tbclist.p);
for (up = L->openupval; up != NULL; up = up->u.open.next)
up->v.offset = savestack(L, uplevel(up));
for (ci = L->ci; ci != NULL; ci = ci->previous) {
ci->top.offset = savestack(L, ci->top.p);
ci->func.offset = savestack(L, ci->func.p);
}
}
static void correctstack (lua_State *L, StkId oldstack) {
CallInfo *ci;
UpVal *up;
UNUSED(oldstack);
L->top.p = restorestack(L, L->top.offset);
L->tbclist.p = restorestack(L, L->tbclist.offset);
for (up = L->openupval; up != NULL; up = up->u.open.next)
up->v.p = s2v(restorestack(L, up->v.offset));
for (ci = L->ci; ci != NULL; ci = ci->previous) {
ci->top.p = restorestack(L, ci->top.offset);
ci->func.p = restorestack(L, ci->func.offset);
if (isLua(ci))
ci->u.l.trap = 1;
}
}
#else
static void relstack (lua_State *L) { UNUSED(L); }
static void correctstack (lua_State *L, StkId oldstack) {
CallInfo *ci;
UpVal *up;
StkId newstack = L->stack.p;
if (oldstack == newstack)
return;
L->top.p = L->top.p - oldstack + newstack;
L->tbclist.p = L->tbclist.p - oldstack + newstack;
for (up = L->openupval; up != NULL; up = up->u.open.next)
up->v.p = s2v(uplevel(up) - oldstack + newstack);
for (ci = L->ci; ci != NULL; ci = ci->previous) {
ci->top.p = ci->top.p - oldstack + newstack;
ci->func.p = ci->func.p - oldstack + newstack;
if (isLua(ci))
ci->u.l.trap = 1;
}
}
#endif
int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) {
int oldsize = stacksize(L);
int i;
StkId newstack;
StkId oldstack = L->stack.p;
lu_byte oldgcstop = G(L)->gcstopem;
lua_assert(newsize <= MAXSTACK || newsize == ERRORSTACKSIZE);
relstack(L);
G(L)->gcstopem = 1;
newstack = luaM_reallocvector(L, oldstack, oldsize + EXTRA_STACK,
newsize + EXTRA_STACK, StackValue);
G(L)->gcstopem = oldgcstop;
if (l_unlikely(newstack == NULL)) {
correctstack(L, oldstack);
if (raiseerror)
luaM_error(L);
else return 0;
}
L->stack.p = newstack;
correctstack(L, oldstack);
L->stack_last.p = L->stack.p + newsize;
for (i = oldsize + EXTRA_STACK; i < newsize + EXTRA_STACK; i++)
setnilvalue(s2v(newstack + i));
return 1;
}
int luaD_growstack (lua_State *L, int n, int raiseerror) {
int size = stacksize(L);
if (l_unlikely(size > MAXSTACK)) {
lua_assert(stacksize(L) == ERRORSTACKSIZE);
if (raiseerror)
luaD_errerr(L);
return 0;
}
else if (n < MAXSTACK) {
int newsize = size + (size >> 1);
int needed = cast_int(L->top.p - L->stack.p) + n;
if (newsize > MAXSTACK)
newsize = MAXSTACK;
if (newsize < needed)
newsize = needed;
if (l_likely(newsize <= MAXSTACK))
return luaD_reallocstack(L, newsize, raiseerror);
}
luaD_reallocstack(L, ERRORSTACKSIZE, raiseerror);
if (raiseerror)
luaG_runerror(L, "stack overflow");
return 0;
}
static int stackinuse (lua_State *L) {
CallInfo *ci;
int res;
StkId lim = L->top.p;
for (ci = L->ci; ci != NULL; ci = ci->previous) {
if (lim < ci->top.p) lim = ci->top.p;
}
lua_assert(lim <= L->stack_last.p + EXTRA_STACK);
res = cast_int(lim - L->stack.p) + 1;
if (res < LUA_MINSTACK)
res = LUA_MINSTACK;
return res;
}
void luaD_shrinkstack (lua_State *L) {
int inuse = stackinuse(L);
int max = (inuse > MAXSTACK / 3) ? MAXSTACK : inuse * 3;
if (inuse <= MAXSTACK && stacksize(L) > max) {
int nsize = (inuse > MAXSTACK / 2) ? MAXSTACK : inuse * 2;
luaD_reallocstack(L, nsize, 0);
}
else
condmovestack(L,(void)0,(void)0);
luaE_shrinkCI(L);
}
void luaD_inctop (lua_State *L) {
L->top.p++;
luaD_checkstack(L, 1);
}
void luaD_hook (lua_State *L, int event, int line,
int ftransfer, int ntransfer) {
lua_Hook hook = L->hook;
if (hook && L->allowhook) {
CallInfo *ci = L->ci;
ptrdiff_t top = savestack(L, L->top.p);
ptrdiff_t ci_top = savestack(L, ci->top.p);
lua_Debug ar;
ar.event = event;
ar.currentline = line;
ar.i_ci = ci;
L->transferinfo.ftransfer = ftransfer;
L->transferinfo.ntransfer = ntransfer;
if (isLua(ci) && L->top.p < ci->top.p)
L->top.p = ci->top.p;
luaD_checkstack(L, LUA_MINSTACK);
if (ci->top.p < L->top.p + LUA_MINSTACK)
ci->top.p = L->top.p + LUA_MINSTACK;
L->allowhook = 0;
ci->callstatus |= CIST_HOOKED;
lua_unlock(L);
(*hook)(L, &ar);
lua_lock(L);
lua_assert(!L->allowhook);
L->allowhook = 1;
ci->top.p = restorestack(L, ci_top);
L->top.p = restorestack(L, top);
ci->callstatus &= ~CIST_HOOKED;
}
}
void luaD_hookcall (lua_State *L, CallInfo *ci) {
L->oldpc = 0;
if (L->hookmask & LUA_MASKCALL) {
int event = (ci->callstatus & CIST_TAIL) ? LUA_HOOKTAILCALL
: LUA_HOOKCALL;
Proto *p = ci_func(ci)->p;
ci->u.l.savedpc++;
luaD_hook(L, event, -1, 1, p->numparams);
ci->u.l.savedpc--;
}
}
static void rethook (lua_State *L, CallInfo *ci, int nres) {
if (L->hookmask & LUA_MASKRET) {
StkId firstres = L->top.p - nres;
int delta = 0;
int ftransfer;
if (isLua(ci)) {
Proto *p = ci_func(ci)->p;
if (p->flag & PF_VAHID)
delta = ci->u.l.nextraargs + p->numparams + 1;
}
ci->func.p += delta;
ftransfer = cast_int(firstres - ci->func.p);
luaD_hook(L, LUA_HOOKRET, -1, ftransfer, nres);
ci->func.p -= delta;
}
if (isLua(ci = ci->previous))
L->oldpc = pcRel(ci->u.l.savedpc, ci_func(ci)->p);
}
static unsigned tryfuncTM (lua_State *L, StkId func, unsigned status) {
const TValue *tm;
StkId p;
tm = luaT_gettmbyobj(L, s2v(func), TM_CALL);
if (l_unlikely(ttisnil(tm)))
luaG_callerror(L, s2v(func));
for (p = L->top.p; p > func; p--)
setobjs2s(L, p, p-1);
L->top.p++;
setobj2s(L, func, tm);
if ((status & MAX_CCMT) == MAX_CCMT)
luaG_runerror(L, "'__call' chain too long");
return status + (1u << CIST_CCMT);
}
l_sinline void genmoveresults (lua_State *L, StkId res, int nres,
int wanted) {
StkId firstresult = L->top.p - nres;
int i;
if (nres > wanted)
nres = wanted;
for (i = 0; i < nres; i++)
setobjs2s(L, res + i, firstresult + i);
for (; i < wanted; i++)
setnilvalue(s2v(res + i));
L->top.p = res + wanted;
}
l_sinline void moveresults (lua_State *L, StkId res, int nres,
l_uint32 fwanted) {
switch (fwanted) {
case 0 + 1:
L->top.p = res;
return;
case 1 + 1:
if (nres == 0)
setnilvalue(s2v(res));
else
setobjs2s(L, res, L->top.p - nres);
L->top.p = res + 1;
return;
case LUA_MULTRET + 1:
genmoveresults(L, res, nres, nres);
break;
default: {
int wanted = get_nresults(fwanted);
if (fwanted & CIST_TBC) {
L->ci->u2.nres = nres;
L->ci->callstatus |= CIST_CLSRET;
res = luaF_close(L, res, CLOSEKTOP, 1);
L->ci->callstatus &= ~CIST_CLSRET;
if (L->hookmask) {
ptrdiff_t savedres = savestack(L, res);
rethook(L, L->ci, nres);
res = restorestack(L, savedres);
}
if (wanted == LUA_MULTRET)
wanted = nres;
}
genmoveresults(L, res, nres, wanted);
break;
}
}
}
void luaD_poscall (lua_State *L, CallInfo *ci, int nres) {
l_uint32 fwanted = ci->callstatus & (CIST_TBC | CIST_NRESULTS);
if (l_unlikely(L->hookmask) && !(fwanted & CIST_TBC))
rethook(L, ci, nres);
moveresults(L, ci->func.p, nres, fwanted);
lua_assert(!(ci->callstatus &
(CIST_HOOKED | CIST_YPCALL | CIST_FIN | CIST_CLSRET)));
L->ci = ci->previous;
}
#define next_ci(L) (L->ci->next ? L->ci->next : luaE_extendCI(L))
l_sinline CallInfo *prepCallInfo (lua_State *L, StkId func, unsigned status,
StkId top) {
CallInfo *ci = L->ci = next_ci(L);
ci->func.p = func;
lua_assert((status & ~(CIST_NRESULTS | CIST_C | MAX_CCMT)) == 0);
ci->callstatus = status;
ci->top.p = top;
return ci;
}
l_sinline int precallC (lua_State *L, StkId func, unsigned status,
lua_CFunction f) {
int n;
CallInfo *ci;
checkstackp(L, LUA_MINSTACK, func);
L->ci = ci = prepCallInfo(L, func, status | CIST_C,
L->top.p + LUA_MINSTACK);
lua_assert(ci->top.p <= L->stack_last.p);
if (l_unlikely(L->hookmask & LUA_MASKCALL)) {
int narg = cast_int(L->top.p - func) - 1;
luaD_hook(L, LUA_HOOKCALL, -1, 1, narg);
}
lua_unlock(L);
n = (*f)(L);
lua_lock(L);
api_checknelems(L, n);
luaD_poscall(L, ci, n);
return n;
}
int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func,
int narg1, int delta) {
unsigned status = LUA_MULTRET + 1;
retry:
switch (ttypetag(s2v(func))) {
case LUA_VCCL:
return precallC(L, func, status, clCvalue(s2v(func))->f);
case LUA_VLCF:
return precallC(L, func, status, fvalue(s2v(func)));
case LUA_VLCL: {
Proto *p = clLvalue(s2v(func))->p;
int fsize = p->maxstacksize;
int nfixparams = p->numparams;
int i;
checkstackp(L, fsize - delta, func);
ci->func.p -= delta;
for (i = 0; i < narg1; i++)
setobjs2s(L, ci->func.p + i, func + i);
func = ci->func.p;
for (; narg1 <= nfixparams; narg1++)
setnilvalue(s2v(func + narg1));
ci->top.p = func + 1 + fsize;
lua_assert(ci->top.p <= L->stack_last.p);
ci->u.l.savedpc = p->code;
ci->callstatus |= CIST_TAIL;
L->top.p = func + narg1;
return -1;
}
default: {
checkstackp(L, 1, func);
status = tryfuncTM(L, func, status);
narg1++;
goto retry;
}
}
}
CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) {
unsigned status = cast_uint(nresults + 1);
lua_assert(status <= MAXRESULTS + 1);
retry:
switch (ttypetag(s2v(func))) {
case LUA_VCCL:
precallC(L, func, status, clCvalue(s2v(func))->f);
return NULL;
case LUA_VLCF:
precallC(L, func, status, fvalue(s2v(func)));
return NULL;
case LUA_VLCL: {
CallInfo *ci;
Proto *p = clLvalue(s2v(func))->p;
int narg = cast_int(L->top.p - func) - 1;
int nfixparams = p->numparams;
int fsize = p->maxstacksize;
checkstackp(L, fsize, func);
L->ci = ci = prepCallInfo(L, func, status, func + 1 + fsize);
ci->u.l.savedpc = p->code;
for (; narg < nfixparams; narg++)
setnilvalue(s2v(L->top.p++));
lua_assert(ci->top.p <= L->stack_last.p);
return ci;
}
default: {
checkstackp(L, 1, func);
status = tryfuncTM(L, func, status);
goto retry;
}
}
}
l_sinline void ccall (lua_State *L, StkId func, int nResults, l_uint32 inc) {
CallInfo *ci;
L->nCcalls += inc;
if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) {
checkstackp(L, 0, func);
luaE_checkcstack(L);
}
if ((ci = luaD_precall(L, func, nResults)) != NULL) {
ci->callstatus |= CIST_FRESH;
luaV_execute(L, ci);
}
L->nCcalls -= inc;
}
void luaD_call (lua_State *L, StkId func, int nResults) {
ccall(L, func, nResults, 1);
}
void luaD_callnoyield (lua_State *L, StkId func, int nResults) {
ccall(L, func, nResults, nyci);
}
static TStatus finishpcallk (lua_State *L, CallInfo *ci) {
TStatus status = getcistrecst(ci);
if (l_likely(status == LUA_OK))
status = LUA_YIELD;
else {
StkId func = restorestack(L, ci->u2.funcidx);
L->allowhook = getoah(ci);
func = luaF_close(L, func, status, 1);
luaD_seterrorobj(L, status, func);
luaD_shrinkstack(L);
setcistrecst(ci, LUA_OK);
}
ci->callstatus &= ~CIST_YPCALL;
L->errfunc = ci->u.c.old_errfunc;
return status;
}
static void finishCcall (lua_State *L, CallInfo *ci) {
int n;
if (ci->callstatus & CIST_CLSRET) {
lua_assert(ci->callstatus & CIST_TBC);
n = ci->u2.nres;
}
else {
TStatus status = LUA_YIELD;
lua_KFunction kf = ci->u.c.k;
lua_assert(kf != NULL && yieldable(L));
if (ci->callstatus & CIST_YPCALL)
status = finishpcallk(L, ci);
adjustresults(L, LUA_MULTRET);
lua_unlock(L);
n = (*kf)(L, APIstatus(status), ci->u.c.ctx);
lua_lock(L);
api_checknelems(L, n);
}
luaD_poscall(L, ci, n);
}
static void unroll (lua_State *L, void *ud) {
CallInfo *ci;
UNUSED(ud);
while ((ci = L->ci) != &L->base_ci) {
if (!isLua(ci))
finishCcall(L, ci);
else {
luaV_finishOp(L);
luaV_execute(L, ci);
}
}
}
static CallInfo *findpcall (lua_State *L) {
CallInfo *ci;
for (ci = L->ci; ci != NULL; ci = ci->previous) {
if (ci->callstatus & CIST_YPCALL)
return ci;
}
return NULL;
}
static int resume_error (lua_State *L, const char *msg, int narg) {
api_checkpop(L, narg);
L->top.p -= narg;
setsvalue2s(L, L->top.p, luaS_new(L, msg));
api_incr_top(L);
lua_unlock(L);
return LUA_ERRRUN;
}
static void resume (lua_State *L, void *ud) {
int n = *(cast(int*, ud));
StkId firstArg = L->top.p - n;
CallInfo *ci = L->ci;
if (L->status == LUA_OK)
ccall(L, firstArg - 1, LUA_MULTRET, 0);
else {
lua_assert(L->status == LUA_YIELD);
L->status = LUA_OK;
if (isLua(ci)) {
lua_assert(ci->callstatus & CIST_HOOKYIELD);
ci->u.l.savedpc--;
L->top.p = firstArg;
luaV_execute(L, ci);
}
else {
if (ci->u.c.k != NULL) {
lua_unlock(L);
n = (*ci->u.c.k)(L, LUA_YIELD, ci->u.c.ctx);
lua_lock(L);
api_checknelems(L, n);
}
luaD_poscall(L, ci, n);
}
unroll(L, NULL);
}
}
static TStatus precover (lua_State *L, TStatus status) {
CallInfo *ci;
while (errorstatus(status) && (ci = findpcall(L)) != NULL) {
L->ci = ci;
setcistrecst(ci, status);
status = luaD_rawrunprotected(L, unroll, NULL);
}
return status;
}
LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs,
int *nresults) {
TStatus status;
lua_lock(L);
if (L->status == LUA_OK) {
if (L->ci != &L->base_ci)
return resume_error(L, "cannot resume non-suspended coroutine", nargs);
else if (L->top.p - (L->ci->func.p + 1) == nargs)
return resume_error(L, "cannot resume dead coroutine", nargs);
}
else if (L->status != LUA_YIELD)
return resume_error(L, "cannot resume dead coroutine", nargs);
L->nCcalls = (from) ? getCcalls(from) : 0;
if (getCcalls(L) >= LUAI_MAXCCALLS)
return resume_error(L, "C stack overflow", nargs);
L->nCcalls++;
luai_userstateresume(L, nargs);
api_checkpop(L, (L->status == LUA_OK) ? nargs + 1 : nargs);
status = luaD_rawrunprotected(L, resume, &nargs);
status = precover(L, status);
if (l_likely(!errorstatus(status)))
lua_assert(status == L->status);
else {
L->status = status;
luaD_seterrorobj(L, status, L->top.p);
L->ci->top.p = L->top.p;
}
*nresults = (status == LUA_YIELD) ? L->ci->u2.nyield
: cast_int(L->top.p - (L->ci->func.p + 1));
lua_unlock(L);
return APIstatus(status);
}
LUA_API int lua_isyieldable (lua_State *L) {
return yieldable(L);
}
LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx,
lua_KFunction k) {
CallInfo *ci;
luai_userstateyield(L, nresults);
lua_lock(L);
ci = L->ci;
api_checkpop(L, nresults);
if (l_unlikely(!yieldable(L))) {
if (L != mainthread(G(L)))
luaG_runerror(L, "attempt to yield across a C-call boundary");
else
luaG_runerror(L, "attempt to yield from outside a coroutine");
}
L->status = LUA_YIELD;
ci->u2.nyield = nresults;
if (isLua(ci)) {
lua_assert(!isLuacode(ci));
api_check(L, nresults == 0, "hooks cannot yield values");
api_check(L, k == NULL, "hooks cannot continue after yielding");
}
else {
if ((ci->u.c.k = k) != NULL)
ci->u.c.ctx = ctx;
luaD_throw(L, LUA_YIELD);
}
lua_assert(ci->callstatus & CIST_HOOKED);
lua_unlock(L);
return 0;
}
struct CloseP {
StkId level;
TStatus status;
};
static void closepaux (lua_State *L, void *ud) {
struct CloseP *pcl = cast(struct CloseP *, ud);
luaF_close(L, pcl->level, pcl->status, 0);
}
TStatus luaD_closeprotected (lua_State *L, ptrdiff_t level, TStatus status) {
CallInfo *old_ci = L->ci;
lu_byte old_allowhooks = L->allowhook;
for (;;) {
struct CloseP pcl;
pcl.level = restorestack(L, level); pcl.status = status;
status = luaD_rawrunprotected(L, &closepaux, &pcl);
if (l_likely(status == LUA_OK))
return pcl.status;
else {
L->ci = old_ci;
L->allowhook = old_allowhooks;
}
}
}
TStatus luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t old_top,
ptrdiff_t ef) {
TStatus status;
CallInfo *old_ci = L->ci;
lu_byte old_allowhooks = L->allowhook;
ptrdiff_t old_errfunc = L->errfunc;
L->errfunc = ef;
status = luaD_rawrunprotected(L, func, u);
if (l_unlikely(status != LUA_OK)) {
L->ci = old_ci;
L->allowhook = old_allowhooks;
status = luaD_closeprotected(L, old_top, status);
luaD_seterrorobj(L, status, restorestack(L, old_top));
luaD_shrinkstack(L);
}
L->errfunc = old_errfunc;
return status;
}
struct SParser {
ZIO *z;
Mbuffer buff;
Dyndata dyd;
const char *mode;
const char *name;
};
static void checkmode (lua_State *L, const char *mode, const char *x) {
if (strchr(mode, x[0]) == NULL) {
luaO_pushfstring(L,
"attempt to load a %s chunk (mode is '%s')", x, mode);
luaD_throw(L, LUA_ERRSYNTAX);
}
}
static void f_parser (lua_State *L, void *ud) {
LClosure *cl;
struct SParser *p = cast(struct SParser *, ud);
const char *mode = p->mode ? p->mode : "bt";
int c = zgetc(p->z);
if (c == LUA_SIGNATURE[0]) {
int fixed = 0;
if (strchr(mode, 'B') != NULL)
fixed = 1;
else
checkmode(L, mode, "binary");
cl = luaU_undump(L, p->z, p->name, fixed);
}
else {
checkmode(L, mode, "text");
cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c);
}
lua_assert(cl->nupvalues == cl->p->sizeupvalues);
luaF_initupvals(L, cl);
}
TStatus luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
const char *mode) {
struct SParser p;
TStatus status;
incnny(L);
p.z = z; p.name = name; p.mode = mode;
p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0;
p.dyd.gt.arr = NULL; p.dyd.gt.size = 0;
p.dyd.label.arr = NULL; p.dyd.label.size = 0;
luaZ_initbuffer(L, &p.buff);
status = luaD_pcall(L, f_parser, &p, savestack(L, L->top.p), L->errfunc);
luaZ_freebuffer(L, &p.buff);
luaM_freearray(L, p.dyd.actvar.arr, cast_sizet(p.dyd.actvar.size));
luaM_freearray(L, p.dyd.gt.arr, cast_sizet(p.dyd.gt.size));
luaM_freearray(L, p.dyd.label.arr, cast_sizet(p.dyd.label.size));
decnny(L);
return status;
}