|
@@ -1,5 +1,5 @@
|
|
/*
|
|
/*
|
|
-** $Id: ldo.c,v 2.164 2017/11/01 18:20:48 roberto Exp roberto $
|
|
|
|
|
|
+** $Id: ldo.c,v 2.165 2017/11/02 11:28:56 roberto Exp roberto $
|
|
** Stack and Call structure of Lua
|
|
** Stack and Call structure of Lua
|
|
** See Copyright Notice in lua.h
|
|
** See Copyright Notice in lua.h
|
|
*/
|
|
*/
|
|
@@ -123,8 +123,8 @@ l_noret luaD_throw (lua_State *L, int errcode) {
|
|
else { /* no handler at all; abort */
|
|
else { /* no handler at all; abort */
|
|
if (g->panic) { /* panic function? */
|
|
if (g->panic) { /* panic function? */
|
|
seterrorobj(L, errcode, L->top); /* assume EXTRA_STACK */
|
|
seterrorobj(L, errcode, L->top); /* assume EXTRA_STACK */
|
|
- if (L->ci->top < L->top)
|
|
|
|
- L->ci->top = L->top; /* pushing msg. can break this invariant */
|
|
|
|
|
|
+ if (functop(L->func) < L->top) /* check invariant */
|
|
|
|
+ setfunctop(L->func, L->top);
|
|
lua_unlock(L);
|
|
lua_unlock(L);
|
|
g->panic(L); /* call panic function (last chance to jump out) */
|
|
g->panic(L); /* call panic function (last chance to jump out) */
|
|
}
|
|
}
|
|
@@ -164,7 +164,6 @@ static void correctstack (lua_State *L, StkId oldstack) {
|
|
for (up = L->openupval; up != NULL; up = up->u.open.next)
|
|
for (up = L->openupval; up != NULL; up = up->u.open.next)
|
|
up->v = s2v((uplevel(up) - oldstack) + L->stack);
|
|
up->v = s2v((uplevel(up) - oldstack) + L->stack);
|
|
for (ci = L->ci; ci != NULL; ci = ci->previous) {
|
|
for (ci = L->ci; ci != NULL; ci = ci->previous) {
|
|
- ci->top = (ci->top - oldstack) + L->stack;
|
|
|
|
ci->func = (ci->func - oldstack) + L->stack;
|
|
ci->func = (ci->func - oldstack) + L->stack;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -208,10 +207,11 @@ void luaD_growstack (lua_State *L, int n) {
|
|
|
|
|
|
|
|
|
|
static int stackinuse (lua_State *L) {
|
|
static int stackinuse (lua_State *L) {
|
|
- CallInfo *ci;
|
|
|
|
StkId lim = L->top;
|
|
StkId lim = L->top;
|
|
- for (ci = L->ci; ci != NULL; ci = ci->previous) {
|
|
|
|
- if (lim < ci->top) lim = ci->top;
|
|
|
|
|
|
+ StkId func = L->func;
|
|
|
|
+ for (; func->stkci.previous != 0; func -= func->stkci.previous) {
|
|
|
|
+ if (lim < functop(func))
|
|
|
|
+ lim = functop(func);
|
|
}
|
|
}
|
|
lua_assert(lim <= L->stack_last);
|
|
lua_assert(lim <= L->stack_last);
|
|
return cast_int(lim - L->stack) + 1; /* part of stack in use */
|
|
return cast_int(lim - L->stack) + 1; /* part of stack in use */
|
|
@@ -255,34 +255,38 @@ void luaD_hook (lua_State *L, int event, int line) {
|
|
if (hook && L->allowhook) { /* make sure there is a hook */
|
|
if (hook && L->allowhook) { /* make sure there is a hook */
|
|
CallInfo *ci = L->ci;
|
|
CallInfo *ci = L->ci;
|
|
ptrdiff_t top = savestack(L, L->top);
|
|
ptrdiff_t top = savestack(L, L->top);
|
|
- ptrdiff_t ci_top = savestack(L, ci->top);
|
|
|
|
|
|
+ int origframesize = L->func->stkci.framesize;
|
|
|
|
+ int tmpframesize; /* frame size to run hook */
|
|
lua_Debug ar;
|
|
lua_Debug ar;
|
|
ar.event = event;
|
|
ar.event = event;
|
|
ar.currentline = line;
|
|
ar.currentline = line;
|
|
ar.i_ci = ci;
|
|
ar.i_ci = ci;
|
|
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
|
|
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
|
|
- ci->top = L->top + LUA_MINSTACK;
|
|
|
|
- lua_assert(ci->top <= L->stack_last);
|
|
|
|
|
|
+ tmpframesize = L->top - L->func + LUA_MINSTACK;
|
|
|
|
+ if (tmpframesize > origframesize) /* need to grow frame? */
|
|
|
|
+ L->func->stkci.framesize = tmpframesize;
|
|
|
|
+ lua_assert(functop(L->func) <= L->stack_last);
|
|
L->allowhook = 0; /* cannot call hooks inside a hook */
|
|
L->allowhook = 0; /* cannot call hooks inside a hook */
|
|
- ci->callstatus |= CIST_HOOKED;
|
|
|
|
|
|
+ callstatus(L->func) |= CIST_HOOKED;
|
|
lua_unlock(L);
|
|
lua_unlock(L);
|
|
(*hook)(L, &ar);
|
|
(*hook)(L, &ar);
|
|
lua_lock(L);
|
|
lua_lock(L);
|
|
lua_assert(!L->allowhook);
|
|
lua_assert(!L->allowhook);
|
|
L->allowhook = 1;
|
|
L->allowhook = 1;
|
|
- ci->top = restorestack(L, ci_top);
|
|
|
|
|
|
+ L->func->stkci.framesize = origframesize;
|
|
L->top = restorestack(L, top);
|
|
L->top = restorestack(L, top);
|
|
- ci->callstatus &= ~CIST_HOOKED;
|
|
|
|
|
|
+ callstatus(L->func) &= ~CIST_HOOKED;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
static void callhook (lua_State *L, CallInfo *ci) {
|
|
static void callhook (lua_State *L, CallInfo *ci) {
|
|
int hook = LUA_HOOKCALL;
|
|
int hook = LUA_HOOKCALL;
|
|
|
|
+ StkId previous = L->func - L->func->stkci.previous;
|
|
ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */
|
|
ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */
|
|
- if (isLua(ci->previous) &&
|
|
|
|
|
|
+ if (isLua(previous) &&
|
|
GET_OPCODE(*(ci->previous->u.l.savedpc - 1)) == OP_TAILCALL) {
|
|
GET_OPCODE(*(ci->previous->u.l.savedpc - 1)) == OP_TAILCALL) {
|
|
- ci->callstatus |= CIST_TAIL;
|
|
|
|
|
|
+ callstatus(L->func) |= CIST_TAIL;
|
|
hook = LUA_HOOKTAILCALL;
|
|
hook = LUA_HOOKTAILCALL;
|
|
}
|
|
}
|
|
luaD_hook(L, hook, -1);
|
|
luaD_hook(L, hook, -1);
|
|
@@ -358,8 +362,8 @@ static int moveresults (lua_State *L, StkId firstResult, StkId res,
|
|
** wanted multiple (variable number of) results.
|
|
** wanted multiple (variable number of) results.
|
|
*/
|
|
*/
|
|
int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) {
|
|
int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) {
|
|
- StkId res;
|
|
|
|
- int wanted = ci->nresults;
|
|
|
|
|
|
+ StkId res = L->func; /* res == final position of 1st result */
|
|
|
|
+ int wanted = res->stkci.nresults;
|
|
if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) {
|
|
if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) {
|
|
if (L->hookmask & LUA_MASKRET) {
|
|
if (L->hookmask & LUA_MASKRET) {
|
|
ptrdiff_t fr = savestack(L, firstResult); /* hook may change stack */
|
|
ptrdiff_t fr = savestack(L, firstResult); /* hook may change stack */
|
|
@@ -368,7 +372,6 @@ int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) {
|
|
}
|
|
}
|
|
L->oldpc = ci->previous->u.l.savedpc; /* 'oldpc' for caller function */
|
|
L->oldpc = ci->previous->u.l.savedpc; /* 'oldpc' for caller function */
|
|
}
|
|
}
|
|
- res = L->func; /* res == final position of 1st result */
|
|
|
|
L->ci = ci->previous; /* back to caller */
|
|
L->ci = ci->previous; /* back to caller */
|
|
L->func -= L->func->stkci.previous;
|
|
L->func -= L->func->stkci.previous;
|
|
lua_assert(L->func == L->ci->func);
|
|
lua_assert(L->func == L->ci->func);
|
|
@@ -402,12 +405,12 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
|
|
int n; /* number of returns */
|
|
int n; /* number of returns */
|
|
checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */
|
|
checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */
|
|
ci = next_ci(L); /* now 'enter' new function */
|
|
ci = next_ci(L); /* now 'enter' new function */
|
|
- ci->nresults = nresults;
|
|
|
|
|
|
+ func->stkci.nresults = nresults;
|
|
func->stkci.previous = func - L->func;
|
|
func->stkci.previous = func - L->func;
|
|
L->func = ci->func = func;
|
|
L->func = ci->func = func;
|
|
- ci->top = L->top + LUA_MINSTACK;
|
|
|
|
- lua_assert(ci->top <= L->stack_last);
|
|
|
|
- ci->callstatus = 0;
|
|
|
|
|
|
+ setfunctop(func, L->top + LUA_MINSTACK);
|
|
|
|
+ lua_assert(functop(func) <= L->stack_last);
|
|
|
|
+ callstatus(func) = 0;
|
|
if (L->hookmask & LUA_MASKCALL)
|
|
if (L->hookmask & LUA_MASKCALL)
|
|
luaD_hook(L, LUA_HOOKCALL, -1);
|
|
luaD_hook(L, LUA_HOOKCALL, -1);
|
|
lua_unlock(L);
|
|
lua_unlock(L);
|
|
@@ -427,13 +430,14 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
|
|
if (p->is_vararg)
|
|
if (p->is_vararg)
|
|
luaT_adjustvarargs(L, p, n);
|
|
luaT_adjustvarargs(L, p, n);
|
|
ci = next_ci(L); /* now 'enter' new function */
|
|
ci = next_ci(L); /* now 'enter' new function */
|
|
- ci->nresults = nresults;
|
|
|
|
|
|
+ func->stkci.nresults = nresults;
|
|
func->stkci.previous = func - L->func;
|
|
func->stkci.previous = func - L->func;
|
|
|
|
+ func->stkci.framesize = fsize + 1; /* size includes function itself */
|
|
L->func = ci->func = func;
|
|
L->func = ci->func = func;
|
|
- L->top = ci->top = func + 1 + fsize;
|
|
|
|
- lua_assert(ci->top <= L->stack_last);
|
|
|
|
|
|
+ L->top = func + 1 + fsize;
|
|
|
|
+ lua_assert(functop(func) <= L->stack_last);
|
|
ci->u.l.savedpc = p->code; /* starting point */
|
|
ci->u.l.savedpc = p->code; /* starting point */
|
|
- ci->callstatus = CIST_LUA;
|
|
|
|
|
|
+ callstatus(func) = 0;
|
|
if (L->hookmask & LUA_MASKCALL)
|
|
if (L->hookmask & LUA_MASKCALL)
|
|
callhook(L, ci);
|
|
callhook(L, ci);
|
|
return 0;
|
|
return 0;
|
|
@@ -493,18 +497,19 @@ void luaD_callnoyield (lua_State *L, StkId func, int nResults) {
|
|
*/
|
|
*/
|
|
static void finishCcall (lua_State *L, int status) {
|
|
static void finishCcall (lua_State *L, int status) {
|
|
CallInfo *ci = L->ci;
|
|
CallInfo *ci = L->ci;
|
|
|
|
+ StkId func = L->func;
|
|
int n;
|
|
int n;
|
|
/* must have a continuation and must be able to call it */
|
|
/* must have a continuation and must be able to call it */
|
|
lua_assert(ci->u.c.k != NULL && L->nny == 0);
|
|
lua_assert(ci->u.c.k != NULL && L->nny == 0);
|
|
/* error status can only happen in a protected call */
|
|
/* error status can only happen in a protected call */
|
|
- lua_assert((ci->callstatus & CIST_YPCALL) || status == LUA_YIELD);
|
|
|
|
- if (ci->callstatus & CIST_YPCALL) { /* was inside a pcall? */
|
|
|
|
- ci->callstatus &= ~CIST_YPCALL; /* continuation is also inside it */
|
|
|
|
|
|
+ lua_assert((callstatus(func) & CIST_YPCALL) || status == LUA_YIELD);
|
|
|
|
+ if (callstatus(func) & CIST_YPCALL) { /* was inside a pcall? */
|
|
|
|
+ callstatus(func) &= ~CIST_YPCALL; /* continuation is also inside it */
|
|
L->errfunc = ci->u.c.old_errfunc; /* with the same error function */
|
|
L->errfunc = ci->u.c.old_errfunc; /* with the same error function */
|
|
}
|
|
}
|
|
/* finish 'lua_callk'/'lua_pcall'; CIST_YPCALL and 'errfunc' already
|
|
/* finish 'lua_callk'/'lua_pcall'; CIST_YPCALL and 'errfunc' already
|
|
handled */
|
|
handled */
|
|
- adjustresults(L, ci->nresults);
|
|
|
|
|
|
+ adjustresults(L, func->stkci.nresults);
|
|
lua_unlock(L);
|
|
lua_unlock(L);
|
|
n = (*ci->u.c.k)(L, status, ci->u.c.ctx); /* call continuation function */
|
|
n = (*ci->u.c.k)(L, status, ci->u.c.ctx); /* call continuation function */
|
|
lua_lock(L);
|
|
lua_lock(L);
|
|
@@ -525,7 +530,7 @@ static void unroll (lua_State *L, void *ud) {
|
|
if (ud != NULL) /* error status? */
|
|
if (ud != NULL) /* error status? */
|
|
finishCcall(L, *(int *)ud); /* finish 'lua_pcallk' callee */
|
|
finishCcall(L, *(int *)ud); /* finish 'lua_pcallk' callee */
|
|
while (L->ci != &L->base_ci) { /* something in the stack */
|
|
while (L->ci != &L->base_ci) { /* something in the stack */
|
|
- if (!isLua(L->ci)) /* C function? */
|
|
|
|
|
|
+ if (!isLua(L->func)) /* C function? */
|
|
finishCcall(L, LUA_YIELD); /* complete its execution */
|
|
finishCcall(L, LUA_YIELD); /* complete its execution */
|
|
else { /* Lua function */
|
|
else { /* Lua function */
|
|
luaV_finishOp(L); /* finish interrupted instruction */
|
|
luaV_finishOp(L); /* finish interrupted instruction */
|
|
@@ -542,7 +547,7 @@ static void unroll (lua_State *L, void *ud) {
|
|
static CallInfo *findpcall (lua_State *L) {
|
|
static CallInfo *findpcall (lua_State *L) {
|
|
CallInfo *ci;
|
|
CallInfo *ci;
|
|
for (ci = L->ci; ci != NULL; ci = ci->previous) { /* search for a pcall */
|
|
for (ci = L->ci; ci != NULL; ci = ci->previous) { /* search for a pcall */
|
|
- if (ci->callstatus & CIST_YPCALL)
|
|
|
|
|
|
+ if (callstatus(ci->func) & CIST_YPCALL)
|
|
return ci;
|
|
return ci;
|
|
}
|
|
}
|
|
return NULL; /* no pending pcall */
|
|
return NULL; /* no pending pcall */
|
|
@@ -564,7 +569,7 @@ static int recover (lua_State *L, int status) {
|
|
seterrorobj(L, status, oldtop);
|
|
seterrorobj(L, status, oldtop);
|
|
L->ci = ci;
|
|
L->ci = ci;
|
|
L->func = ci->func;
|
|
L->func = ci->func;
|
|
- L->allowhook = getoah(ci->callstatus); /* restore original 'allowhook' */
|
|
|
|
|
|
+ L->allowhook = getoah(callstatus(L->func)); /* restore original 'allowhook' */
|
|
L->nny = 0; /* should be zero to be yieldable */
|
|
L->nny = 0; /* should be zero to be yieldable */
|
|
luaD_shrinkstack(L);
|
|
luaD_shrinkstack(L);
|
|
L->errfunc = ci->u.c.old_errfunc;
|
|
L->errfunc = ci->u.c.old_errfunc;
|
|
@@ -604,7 +609,7 @@ static void resume (lua_State *L, void *ud) {
|
|
else { /* resuming from previous yield */
|
|
else { /* resuming from previous yield */
|
|
lua_assert(L->status == LUA_YIELD);
|
|
lua_assert(L->status == LUA_YIELD);
|
|
L->status = LUA_OK; /* mark that it is running (again) */
|
|
L->status = LUA_OK; /* mark that it is running (again) */
|
|
- if (isLua(ci)) /* yielded inside a hook? */
|
|
|
|
|
|
+ if (isLua(L->func)) /* yielded inside a hook? */
|
|
luaV_execute(L); /* just continue running Lua code */
|
|
luaV_execute(L); /* just continue running Lua code */
|
|
else { /* 'common' yield */
|
|
else { /* 'common' yield */
|
|
if (ci->u.c.k != NULL) { /* does it have a continuation function? */
|
|
if (ci->u.c.k != NULL) { /* does it have a continuation function? */
|
|
@@ -649,7 +654,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs,
|
|
if (errorstatus(status)) { /* unrecoverable error? */
|
|
if (errorstatus(status)) { /* unrecoverable error? */
|
|
L->status = cast_byte(status); /* mark thread as 'dead' */
|
|
L->status = cast_byte(status); /* mark thread as 'dead' */
|
|
seterrorobj(L, status, L->top); /* push error message */
|
|
seterrorobj(L, status, L->top); /* push error message */
|
|
- L->ci->top = L->top;
|
|
|
|
|
|
+ L->func->stkci.framesize = L->top - L->func;
|
|
}
|
|
}
|
|
else lua_assert(status == L->status); /* normal end or yield */
|
|
else lua_assert(status == L->status); /* normal end or yield */
|
|
}
|
|
}
|
|
@@ -671,6 +676,7 @@ LUA_API int lua_isyieldable (lua_State *L) {
|
|
LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx,
|
|
LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx,
|
|
lua_KFunction k) {
|
|
lua_KFunction k) {
|
|
CallInfo *ci = L->ci;
|
|
CallInfo *ci = L->ci;
|
|
|
|
+ StkId func = L->func;
|
|
luai_userstateyield(L, nresults);
|
|
luai_userstateyield(L, nresults);
|
|
lua_lock(L);
|
|
lua_lock(L);
|
|
api_checknelems(L, nresults);
|
|
api_checknelems(L, nresults);
|
|
@@ -681,7 +687,7 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx,
|
|
luaG_runerror(L, "attempt to yield from outside a coroutine");
|
|
luaG_runerror(L, "attempt to yield from outside a coroutine");
|
|
}
|
|
}
|
|
L->status = LUA_YIELD;
|
|
L->status = LUA_YIELD;
|
|
- if (isLua(ci)) { /* inside a hook? */
|
|
|
|
|
|
+ if (isLua(func)) { /* inside a hook? */
|
|
api_check(L, k == NULL, "hooks cannot continue after yielding");
|
|
api_check(L, k == NULL, "hooks cannot continue after yielding");
|
|
ci->u2.nyield = 0; /* no results */
|
|
ci->u2.nyield = 0; /* no results */
|
|
}
|
|
}
|
|
@@ -691,7 +697,7 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx,
|
|
ci->u2.nyield = nresults; /* save number of results */
|
|
ci->u2.nyield = nresults; /* save number of results */
|
|
luaD_throw(L, LUA_YIELD);
|
|
luaD_throw(L, LUA_YIELD);
|
|
}
|
|
}
|
|
- lua_assert(ci->callstatus & CIST_HOOKED); /* must be inside a hook */
|
|
|
|
|
|
+ lua_assert(callstatus(func) & CIST_HOOKED); /* must be inside a hook */
|
|
lua_unlock(L);
|
|
lua_unlock(L);
|
|
return 0; /* return to 'luaD_hook' */
|
|
return 0; /* return to 'luaD_hook' */
|
|
}
|
|
}
|