From 1fce5bea817de50e055a84c153a975f25bfcf493 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 29 Oct 2021 13:41:24 -0300 Subject: More uniform implementation for tail calls 'luaD_pretailcall' mimics 'luaD_precall', handling call metamethods and calling C functions directly. That makes the code in the interpreter loop simpler. This commit also goes back to emulating the tail call in 'luaD_precall' with a goto, as C compilers may not do proper tail calls and the C stack can overflow much sooner than the Lua stack (which grows as the metamethod is added to it). --- lvm.c | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) (limited to 'lvm.c') diff --git a/lvm.c b/lvm.c index 49ed3ddf..2ec34400 100644 --- a/lvm.c +++ b/lvm.c @@ -1643,6 +1643,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_TAILCALL) { int b = GETARG_B(i); /* number of arguments + 1 (function) */ + int n; /* number of results when calling a C function */ int nparams1 = GETARG_C(i); /* delta is virtual 'func' - real 'func' (vararg functions) */ int delta = (nparams1) ? ci->u.l.nextraargs + nparams1 : 0; @@ -1656,24 +1657,14 @@ void luaV_execute (lua_State *L, CallInfo *ci) { lua_assert(L->tbclist < base); /* no pending tbc variables */ lua_assert(base == ci->func + 1); } - while (!ttisfunction(s2v(ra))) { /* not a function? */ - ra = luaD_tryfuncTM(L, ra); /* try '__call' metamethod */ - b++; /* there is now one extra argument */ - } - if (!ttisLclosure(s2v(ra))) { /* C function? */ - luaD_precall(L, ra, LUA_MULTRET); /* call it */ - updatetrap(ci); - updatestack(ci); /* stack may have been relocated */ + if ((n = luaD_pretailcall(L, ci, ra, b, delta)) < 0) /* Lua function? */ + goto startfunc; /* execute the callee */ + else { /* C function? */ ci->func -= delta; /* restore 'func' (if vararg) */ - luaD_poscall(L, ci, cast_int(L->top - ra)); /* finish caller */ + luaD_poscall(L, ci, n); /* finish caller */ updatetrap(ci); /* 'luaD_poscall' can change hooks */ goto ret; /* caller returns after the tail call */ } - else { /* Lua function */ - ci->func -= delta; /* restore 'func' (if vararg) */ - luaD_pretailcall(L, ci, ra, b); /* prepare call frame */ - goto startfunc; /* execute the callee */ - } } vmcase(OP_RETURN) { int n = GETARG_B(i) - 1; /* number of results */ -- cgit v1.2.3-55-g6feb