when running Lua code, there is no need to keep 'L->top' "correct";

set it only when needed.
This commit is contained in:
Roberto Ierusalimschy
2017-12-20 12:58:05 -02:00
parent 4dc0be950a
commit 1d5b885437
5 changed files with 53 additions and 41 deletions

26
ltm.c
View File

@@ -1,5 +1,5 @@
/*
** $Id: ltm.c,v 2.53 2017/12/15 13:07:10 roberto Exp roberto $
** $Id: ltm.c,v 2.54 2017/12/19 16:40:17 roberto Exp roberto $
** Tag methods
** See Copyright Notice in lua.h
*/
@@ -101,12 +101,12 @@ const char *luaT_objtypename (lua_State *L, const TValue *o) {
void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
const TValue *p2, const TValue *p3) {
StkId func = L->top;
StkId func = (isLuacode(L->ci)) ? L->ci->top : L->top;
setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */
setobj2s(L, func + 1, p1); /* 1st argument */
setobj2s(L, func + 2, p2); /* 2nd argument */
setobj2s(L, func + 3, p3); /* 3rd argument */
L->top += 4;
L->top = func + 4;
/* metamethod may yield only when called from Lua code */
if (isLuacode(L->ci))
luaD_call(L, func, 0);
@@ -115,8 +115,8 @@ void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
}
void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1,
const TValue *p2, StkId res) {
static void reallycallTMres (lua_State *L, const TValue *f, const TValue *p1,
const TValue *p2, StkId res) {
ptrdiff_t result = savestack(L, res);
StkId func = L->top;
setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */
@@ -129,7 +129,15 @@ void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1,
else
luaD_callnoyield(L, func, 1);
res = restorestack(L, result);
setobjs2s(L, res, --L->top); /* more result to its place */
setobjs2s(L, res, --L->top); /* move result to its place */
}
void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1,
const TValue *p2, StkId res) {
if (isLuacode(L->ci))
L->top = L->ci->top; /* prepare top */
reallycallTMres(L, f, p1, p2, res);
}
@@ -139,13 +147,15 @@ static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
if (ttisnil(tm))
tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
if (ttisnil(tm)) return 0;
luaT_callTMres(L, tm, p1, p2, res);
reallycallTMres(L, tm, p1, p2, res);
return 1;
}
void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
StkId res, TMS event) {
if (event != TM_CONCAT && isLuacode(L->ci))
L->top = L->ci->top; /* prepare top */
if (!callbinTM(L, p1, p2, res, event)) {
switch (event) {
case TM_CONCAT:
@@ -185,6 +195,8 @@ void luaT_trybiniTM (lua_State *L, const TValue *p1, int i2,
int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
TMS event) {
if (isLuacode(L->ci))
L->top = L->ci->top; /* prepare top */
if (callbinTM(L, p1, p2, L->top, event)) /* try original event */
return !l_isfalse(s2v(L->top));
else if (event == TM_LE) {