diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-01-10 17:19:27 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-01-10 17:19:27 -0200 |
commit | ab0a851db438c4dd50b76bcf624c15623cc057e4 (patch) | |
tree | da55c14dffd959fc38351fc43dcb0fcb294077cb /ldo.c | |
parent | 728ff9459579894ef7fd861c90be6247831799be (diff) | |
download | lua-ab0a851db438c4dd50b76bcf624c15623cc057e4.tar.gz lua-ab0a851db438c4dd50b76bcf624c15623cc057e4.tar.bz2 lua-ab0a851db438c4dd50b76bcf624c15623cc057e4.zip |
'luaD_tryfuncTM' can ensure it does not change the stack
Diffstat (limited to 'ldo.c')
-rw-r--r-- | ldo.c | 11 |
1 files changed, 4 insertions, 7 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ldo.c,v 2.184 2017/12/28 14:17:09 roberto Exp roberto $ | 2 | ** $Id: ldo.c,v 2.185 2017/12/29 15:44:51 roberto Exp roberto $ |
3 | ** Stack and Call structure of Lua | 3 | ** Stack and Call structure of Lua |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -327,18 +327,15 @@ static void rethook (lua_State *L, CallInfo *ci) { | |||
327 | ** it in stack below original 'func' so that 'luaD_call' can call | 327 | ** it in stack below original 'func' so that 'luaD_call' can call |
328 | ** it. Raise an error if __call metafield is not a function. | 328 | ** it. Raise an error if __call metafield is not a function. |
329 | */ | 329 | */ |
330 | StkId luaD_tryfuncTM (lua_State *L, StkId func) { | 330 | void luaD_tryfuncTM (lua_State *L, StkId func) { |
331 | const TValue *tm = luaT_gettmbyobj(L, s2v(func), TM_CALL); | 331 | const TValue *tm = luaT_gettmbyobj(L, s2v(func), TM_CALL); |
332 | StkId p; | 332 | StkId p; |
333 | if (!ttisfunction(tm)) | 333 | if (!ttisfunction(tm)) |
334 | luaG_typeerror(L, s2v(func), "call"); | 334 | luaG_typeerror(L, s2v(func), "call"); |
335 | /* Open a hole inside the stack at 'func' */ | ||
336 | checkstackp(L, 1, func); /* ensure space for metamethod */ | ||
337 | for (p = L->top; p > func; p--) | 335 | for (p = L->top; p > func; p--) |
338 | setobjs2s(L, p, p-1); | 336 | setobjs2s(L, p, p-1); |
339 | L->top++; | 337 | L->top++; /* assume EXTRA_STACK */ |
340 | setobj2s(L, func, tm); /* metamethod is the new function to be called */ | 338 | setobj2s(L, func, tm); /* metamethod is the new function to be called */ |
341 | return func; | ||
342 | } | 339 | } |
343 | 340 | ||
344 | 341 | ||
@@ -489,7 +486,7 @@ void luaD_call (lua_State *L, StkId func, int nresults) { | |||
489 | break; | 486 | break; |
490 | } | 487 | } |
491 | default: { /* not a function */ | 488 | default: { /* not a function */ |
492 | func = luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */ | 489 | luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */ |
493 | luaD_call(L, func, nresults); /* now it must be a function */ | 490 | luaD_call(L, func, nresults); /* now it must be a function */ |
494 | break; | 491 | break; |
495 | } | 492 | } |