C functions can be tail called, too

A tail call to a C function can have the behavior of a "real" tail
call, reusing the stack frame of the caller.
This commit is contained in:
Roberto Ierusalimschy
2021-06-14 13:28:21 -03:00
parent 901d760093
commit 04e19712a5
4 changed files with 29 additions and 29 deletions

9
lvm.c
View File

@@ -1636,7 +1636,6 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
updatetrap(ci); /* C call; nothing else to be done */
else { /* Lua call: run function in this same C frame */
ci = newci;
ci->callstatus = 0;
goto startfunc;
}
vmbreak;
@@ -1655,16 +1654,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
lua_assert(L->tbclist < base); /* no pending tbc variables */
lua_assert(base == ci->func + 1);
}
if (luaD_precall(L, ra, LUA_MULTRET, delta + 1)) { /* Lua function? */
ci->callstatus |= CIST_TAIL;
if (luaD_precall(L, ra, LUA_MULTRET, delta + 1)) /* Lua function? */
goto startfunc; /* execute the callee */
}
else { /* C function */
updatetrap(ci);
updatestack(ci); /* stack may have been relocated */
ci->func -= delta; /* restore 'func' (if vararg) */
luaD_poscall(L, ci, cast_int(L->top - ra)); /* finish caller */
updatetrap(ci); /* 'luaD_poscall' can change hooks */
goto ret; /* caller returns after the tail call */
}
}