Przeglądaj źródła

'luaD_tryfuncTM' can ensure it does not change the stack

Roberto Ierusalimschy 7 lat temu
rodzic
commit
ab0a851db4
3 zmienionych plików z 8 dodań i 13 usunięć
  1. 4 7
      ldo.c
  2. 2 2
      ldo.h
  3. 2 4
      lvm.c

+ 4 - 7
ldo.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ldo.c,v 2.184 2017/12/28 14:17:09 roberto Exp roberto $
+** $Id: ldo.c,v 2.185 2017/12/29 15:44:51 roberto Exp roberto $
 ** Stack and Call structure of Lua
 ** See Copyright Notice in lua.h
 */
@@ -327,18 +327,15 @@ static void rethook (lua_State *L, CallInfo *ci) {
 ** it in stack below original 'func' so that 'luaD_call' can call
 ** it. Raise an error if __call metafield is not a function.
 */
-StkId luaD_tryfuncTM (lua_State *L, StkId func) {
+void luaD_tryfuncTM (lua_State *L, StkId func) {
   const TValue *tm = luaT_gettmbyobj(L, s2v(func), TM_CALL);
   StkId p;
   if (!ttisfunction(tm))
     luaG_typeerror(L, s2v(func), "call");
-  /* Open a hole inside the stack at 'func' */
-  checkstackp(L, 1, func);  /* ensure space for metamethod */
   for (p = L->top; p > func; p--)
     setobjs2s(L, p, p-1);
-  L->top++;
+  L->top++;  /* assume EXTRA_STACK */
   setobj2s(L, func, tm);  /* metamethod is the new function to be called */
-  return func;
 }
 
 
@@ -489,7 +486,7 @@ void luaD_call (lua_State *L, StkId func, int nresults) {
       break;
     }
     default: {  /* not a function */
-      func = luaD_tryfuncTM(L, func);  /* try to get '__call' metamethod */
+      luaD_tryfuncTM(L, func);  /* try to get '__call' metamethod */
       luaD_call(L, func, nresults);  /* now it must be a function */
       break;
     }

+ 2 - 2
ldo.h

@@ -1,5 +1,5 @@
 /*
-** $Id: ldo.h,v 2.37 2017/12/08 17:28:25 roberto Exp roberto $
+** $Id: ldo.h,v 2.38 2017/12/11 12:43:40 roberto Exp roberto $
 ** Stack and Call structure of Lua
 ** See Copyright Notice in lua.h
 */
@@ -51,7 +51,7 @@ LUAI_FUNC void luaD_hook (lua_State *L, int event, int line);
 LUAI_FUNC void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n);
 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 StkId luaD_tryfuncTM (lua_State *L, StkId func);
+LUAI_FUNC void luaD_tryfuncTM (lua_State *L, StkId func);
 LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u,
                                         ptrdiff_t oldtop, ptrdiff_t ef);
 LUAI_FUNC void luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult,

+ 2 - 4
lvm.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lvm.c,v 2.331 2017/12/30 20:46:18 roberto Exp roberto $
+** $Id: lvm.c,v 2.332 2018/01/09 14:23:40 roberto Exp roberto $
 ** Lua virtual machine
 ** See Copyright Notice in lua.h
 */
@@ -1501,9 +1501,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
           b = L->top - ra;
         lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
         if (!ttisfunction(vra)) {  /* not a function? */
-          /* try to get '__call' metamethod */
-          ProtectNT(ra = luaD_tryfuncTM(L, ra));
-          vra = s2v(ra);
+          ProtectNT(luaD_tryfuncTM(L, ra));  /* try '__call' metamethod */
           b++;  /* there is now one extra argument */
         }
         if (!ttisLclosure(vra)) {  /* C function? */