mirror of
https://github.com/lua/lua
synced 2024-11-22 12:51:30 +03:00
Correct handling of 'luaV_execute' invocations
The previous stackless implementations marked all 'luaV_execute' invocations as fresh. However, re-entering 'luaV_execute' when resuming a coroutine should not be a fresh invocation. (It works because 'unroll' called 'luaV_execute' for each call entry, but it was slower than letting 'luaV_execute' finish all non-fresh invocations.)
This commit is contained in:
parent
287b302acb
commit
490d42b5f8
25
ldo.c
25
ldo.c
@ -449,12 +449,13 @@ void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** Prepares the call to a function (C or Lua). For C functions, also do
|
** Prepares the call to a function (C or Lua). For C functions, also do
|
||||||
** the call. The function to be called is at '*func'. The arguments are
|
** the call. The function to be called is at '*func'. The arguments
|
||||||
** on the stack, right after the function. Returns true if the call was
|
** are on the stack, right after the function. Returns the CallInfo
|
||||||
** made (it was a C function). When returns true, all the results are
|
** to be executed, if it was a Lua function. Otherwise (a C function)
|
||||||
** on the stack, starting at the original function position.
|
** returns NULL, with all the results on the stack, starting at the
|
||||||
|
** original function position.
|
||||||
*/
|
*/
|
||||||
int luaD_precall (lua_State *L, StkId func, int nresults) {
|
CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) {
|
||||||
lua_CFunction f;
|
lua_CFunction f;
|
||||||
retry:
|
retry:
|
||||||
switch (ttypetag(s2v(func))) {
|
switch (ttypetag(s2v(func))) {
|
||||||
@ -482,7 +483,7 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
|
|||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_checknelems(L, n);
|
api_checknelems(L, n);
|
||||||
luaD_poscall(L, ci, n);
|
luaD_poscall(L, ci, n);
|
||||||
return 1;
|
return NULL;
|
||||||
}
|
}
|
||||||
case LUA_VLCL: { /* Lua function */
|
case LUA_VLCL: { /* Lua function */
|
||||||
CallInfo *ci;
|
CallInfo *ci;
|
||||||
@ -494,14 +495,13 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
|
|||||||
L->ci = ci = next_ci(L);
|
L->ci = ci = next_ci(L);
|
||||||
ci->nresults = nresults;
|
ci->nresults = nresults;
|
||||||
ci->u.l.savedpc = p->code; /* starting point */
|
ci->u.l.savedpc = p->code; /* starting point */
|
||||||
ci->callstatus = 0;
|
|
||||||
ci->top = func + 1 + fsize;
|
ci->top = func + 1 + fsize;
|
||||||
ci->func = func;
|
ci->func = func;
|
||||||
L->ci = ci;
|
L->ci = ci;
|
||||||
for (; narg < nfixparams; narg++)
|
for (; narg < nfixparams; narg++)
|
||||||
setnilvalue(s2v(L->top++)); /* complete missing arguments */
|
setnilvalue(s2v(L->top++)); /* complete missing arguments */
|
||||||
lua_assert(ci->top <= L->stack_last);
|
lua_assert(ci->top <= L->stack_last);
|
||||||
return 0;
|
return ci;
|
||||||
}
|
}
|
||||||
default: { /* not a function */
|
default: { /* not a function */
|
||||||
checkstackGCp(L, 1, func); /* space for metamethod */
|
checkstackGCp(L, 1, func); /* space for metamethod */
|
||||||
@ -518,11 +518,14 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
|
|||||||
** increment number of non-yieldable calls).
|
** increment number of non-yieldable calls).
|
||||||
*/
|
*/
|
||||||
static void docall (lua_State *L, StkId func, int nResults, int inc) {
|
static void docall (lua_State *L, StkId func, int nResults, int inc) {
|
||||||
|
CallInfo *ci;
|
||||||
L->nCcalls += inc;
|
L->nCcalls += inc;
|
||||||
if (getCcalls(L) >= LUAI_MAXCCALLS)
|
if (unlikely(getCcalls(L) >= LUAI_MAXCCALLS))
|
||||||
luaE_checkcstack(L);
|
luaE_checkcstack(L);
|
||||||
if (!luaD_precall(L, func, nResults)) /* is a Lua function? */
|
if ((ci = luaD_precall(L, func, nResults)) != NULL) { /* Lua function? */
|
||||||
luaV_execute(L, L->ci); /* call it */
|
ci->callstatus = CIST_FRESH; /* mark that it is a "fresh" execute */
|
||||||
|
luaV_execute(L, ci); /* call it */
|
||||||
|
}
|
||||||
L->nCcalls -= inc;
|
L->nCcalls -= inc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
2
ldo.h
2
ldo.h
@ -59,7 +59,7 @@ LUAI_FUNC void luaD_hook (lua_State *L, int event, int line,
|
|||||||
int fTransfer, int nTransfer);
|
int fTransfer, int nTransfer);
|
||||||
LUAI_FUNC void luaD_hookcall (lua_State *L, CallInfo *ci);
|
LUAI_FUNC void luaD_hookcall (lua_State *L, CallInfo *ci);
|
||||||
LUAI_FUNC void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n);
|
LUAI_FUNC void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n);
|
||||||
LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nResults);
|
LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nResults);
|
||||||
LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults);
|
LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults);
|
||||||
LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults);
|
LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults);
|
||||||
LUAI_FUNC void luaD_tryfuncTM (lua_State *L, StkId func);
|
LUAI_FUNC void luaD_tryfuncTM (lua_State *L, StkId func);
|
||||||
|
2
lstate.c
2
lstate.c
@ -172,7 +172,7 @@ void luaE_checkcstack (lua_State *L) {
|
|||||||
|
|
||||||
LUAI_FUNC void luaE_incCstack (lua_State *L) {
|
LUAI_FUNC void luaE_incCstack (lua_State *L) {
|
||||||
L->nCcalls++;
|
L->nCcalls++;
|
||||||
if (getCcalls(L) >= LUAI_MAXCCALLS)
|
if (unlikely(getCcalls(L) >= LUAI_MAXCCALLS))
|
||||||
luaE_checkcstack(L);
|
luaE_checkcstack(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
15
lstate.h
15
lstate.h
@ -183,14 +183,15 @@ typedef struct CallInfo {
|
|||||||
*/
|
*/
|
||||||
#define CIST_OAH (1<<0) /* original value of 'allowhook' */
|
#define CIST_OAH (1<<0) /* original value of 'allowhook' */
|
||||||
#define CIST_C (1<<1) /* call is running a C function */
|
#define CIST_C (1<<1) /* call is running a C function */
|
||||||
#define CIST_HOOKED (1<<2) /* call is running a debug hook */
|
#define CIST_FRESH (1<<2) /* call is on a fresh "luaV_execute" frame */
|
||||||
#define CIST_YPCALL (1<<3) /* call is a yieldable protected call */
|
#define CIST_HOOKED (1<<3) /* call is running a debug hook */
|
||||||
#define CIST_TAIL (1<<4) /* call was tail called */
|
#define CIST_YPCALL (1<<4) /* call is a yieldable protected call */
|
||||||
#define CIST_HOOKYIELD (1<<5) /* last hook called yielded */
|
#define CIST_TAIL (1<<5) /* call was tail called */
|
||||||
#define CIST_FIN (1<<6) /* call is running a finalizer */
|
#define CIST_HOOKYIELD (1<<6) /* last hook called yielded */
|
||||||
#define CIST_TRAN (1<<7) /* 'ci' has transfer information */
|
#define CIST_FIN (1<<7) /* call is running a finalizer */
|
||||||
|
#define CIST_TRAN (1<<8) /* 'ci' has transfer information */
|
||||||
#if defined(LUA_COMPAT_LT_LE)
|
#if defined(LUA_COMPAT_LT_LE)
|
||||||
#define CIST_LEQ (1<<8) /* using __lt for __le */
|
#define CIST_LEQ (1<<9) /* using __lt for __le */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* active function is a Lua function */
|
/* active function is a Lua function */
|
||||||
|
25
lvm.c
25
lvm.c
@ -1124,7 +1124,6 @@ void luaV_finishOp (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
void luaV_execute (lua_State *L, CallInfo *ci) {
|
void luaV_execute (lua_State *L, CallInfo *ci) {
|
||||||
CallInfo * const origci = ci;
|
|
||||||
LClosure *cl;
|
LClosure *cl;
|
||||||
TValue *k;
|
TValue *k;
|
||||||
StkId base;
|
StkId base;
|
||||||
@ -1133,7 +1132,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
|
|||||||
#if LUA_USE_JUMPTABLE
|
#if LUA_USE_JUMPTABLE
|
||||||
#include "ljumptab.h"
|
#include "ljumptab.h"
|
||||||
#endif
|
#endif
|
||||||
tailcall:
|
execute:
|
||||||
trap = L->hookmask;
|
trap = L->hookmask;
|
||||||
cl = clLvalue(s2v(ci->func));
|
cl = clLvalue(s2v(ci->func));
|
||||||
k = cl->p->k;
|
k = cl->p->k;
|
||||||
@ -1607,17 +1606,19 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
|
|||||||
vmbreak;
|
vmbreak;
|
||||||
}
|
}
|
||||||
vmcase(OP_CALL) {
|
vmcase(OP_CALL) {
|
||||||
|
CallInfo *newci;
|
||||||
int b = GETARG_B(i);
|
int b = GETARG_B(i);
|
||||||
int nresults = GETARG_C(i) - 1;
|
int nresults = GETARG_C(i) - 1;
|
||||||
if (b != 0) /* fixed number of arguments? */
|
if (b != 0) /* fixed number of arguments? */
|
||||||
L->top = ra + b; /* top signals number of arguments */
|
L->top = ra + b; /* top signals number of arguments */
|
||||||
/* else previous instruction set top */
|
/* else previous instruction set top */
|
||||||
savepc(L); /* in case of errors */
|
savepc(L); /* in case of errors */
|
||||||
if (luaD_precall(L, ra, nresults))
|
if ((newci = luaD_precall(L, ra, nresults)) == NULL)
|
||||||
updatetrap(ci); /* C call; nothing else to be done */
|
updatetrap(ci); /* C call; nothing else to be done */
|
||||||
else { /* Lua call: run function in this same invocation */
|
else { /* Lua call: run function in this same invocation */
|
||||||
ci = L->ci;
|
ci = newci;
|
||||||
goto tailcall;
|
ci->callstatus = 0; /* call re-uses 'luaV_execute' */
|
||||||
|
goto execute;
|
||||||
}
|
}
|
||||||
vmbreak;
|
vmbreak;
|
||||||
}
|
}
|
||||||
@ -1647,13 +1648,13 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
|
|||||||
luaD_precall(L, ra, LUA_MULTRET); /* call it */
|
luaD_precall(L, ra, LUA_MULTRET); /* call it */
|
||||||
updatetrap(ci);
|
updatetrap(ci);
|
||||||
updatestack(ci); /* stack may have been relocated */
|
updatestack(ci); /* stack may have been relocated */
|
||||||
ci->func -= delta;
|
ci->func -= delta; /* restore 'func' (if vararg) */
|
||||||
luaD_poscall(L, ci, cast_int(L->top - ra)); /* finish caller */
|
luaD_poscall(L, ci, cast_int(L->top - ra)); /* finish caller */
|
||||||
goto ret;
|
goto ret; /* caller returns after the tail call */
|
||||||
}
|
}
|
||||||
ci->func -= delta;
|
ci->func -= delta; /* restore 'func' (if vararg) */
|
||||||
luaD_pretailcall(L, ci, ra, b); /* prepare call frame */
|
luaD_pretailcall(L, ci, ra, b); /* prepare call frame */
|
||||||
goto tailcall;
|
goto execute; /* execute the callee */
|
||||||
}
|
}
|
||||||
vmcase(OP_RETURN) {
|
vmcase(OP_RETURN) {
|
||||||
int n = GETARG_B(i) - 1; /* number of results */
|
int n = GETARG_B(i) - 1; /* number of results */
|
||||||
@ -1706,11 +1707,11 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
ret:
|
ret:
|
||||||
if (ci == origci)
|
if (ci->callstatus & CIST_FRESH)
|
||||||
return;
|
return; /* end this frame */
|
||||||
else {
|
else {
|
||||||
ci = ci->previous;
|
ci = ci->previous;
|
||||||
goto tailcall;
|
goto execute; /* continue running caller in this frame */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
vmcase(OP_FORLOOP) {
|
vmcase(OP_FORLOOP) {
|
||||||
|
Loading…
Reference in New Issue
Block a user