Skip to content

Commit 275bee6

Browse files
committed
Corrected CallPlugin to allow for bounced calls
1 parent ad6fe50 commit 275bee6

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

scripting/lua_methods.cpp

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,15 @@ static int L_CallPlugin (lua_State *L)
969969

970970
const char * sPluginID = my_checkstring (L, 1);
971971
const char * sRoutine = my_checkstring (L, 2);
972+
973+
// remove plugin ID and function name
974+
// this is so, after the lua_pcall the stack should be empty
975+
// so, if the called function does a CallPlugin back to us, it won't matter
976+
// if we do a lua_settop (pL, 0) (below) to clear the stack
977+
978+
lua_remove (L, 1); // remove plugin ID
979+
lua_remove (L, 1); // remove function name
980+
972981
int i; // for iterating through arguments / return values
973982

974983
// preliminary checks ...
@@ -1022,7 +1031,7 @@ static int L_CallPlugin (lua_State *L)
10221031

10231032
if (pPlugin->m_ScriptEngine->IsLua ())
10241033
{
1025-
int n = lua_gettop(L); // number of arguments in calling script
1034+
int n = lua_gettop(L); // number of arguments in calling script (we removed plugin ID and function name already)
10261035

10271036
lua_State *pL = pPlugin->m_ScriptEngine->L; // plugin's Lua state
10281037

@@ -1044,10 +1053,7 @@ static int L_CallPlugin (lua_State *L)
10441053

10451054
// if we are calling ourselves, don't make a copy of everything
10461055
if (pL == L)
1047-
{
1048-
lua_insert (L, 3); // move function to be called as third item
1049-
// (after plugin ID and function name), pushing others up
1050-
} // end of calling ourselves
1056+
lua_insert (pL, 1); // move function to be called as first item
10511057
else
10521058
{ // calling a different plugin
10531059

@@ -1056,11 +1062,10 @@ static int L_CallPlugin (lua_State *L)
10561062
// but NOT: table, function, userdata, thread
10571063

10581064
// check we can push our arguments.
1059-
// we have (n - 2) arguments (first two are the plugin ID and the function name)
1060-
// however we need room for the function itself and at least room for the return value
1061-
lua_checkstack (pL, n);
1065+
// we need room for the function itself and at least room for the return value
1066+
lua_checkstack (pL, n + 2);
10621067

1063-
for (i = 3; i <= n; i++) // arg 1 is plugin ID, arg 2 is function name, so start at 3
1068+
for (i = 1; i <= n; i++)
10641069
{
10651070

10661071
switch (lua_type (L, i))
@@ -1090,7 +1095,7 @@ static int L_CallPlugin (lua_State *L)
10901095
lua_settop (pL, 0); // clear target plugin's stack to remove whatever we pushed onto it
10911096
lua_pushnumber (L, eBadParameter);
10921097
CString strError = TFormat ("Cannot pass argument #%i (%s type) to CallPlugin",
1093-
i,
1098+
i + 2, // add two because we deleted plugin ID and function name
10941099
luaL_typename (L, i));
10951100
lua_pushstring (L, strError);
10961101
return 2; // eBadParameter, explanation
@@ -1114,11 +1119,9 @@ static int L_CallPlugin (lua_State *L)
11141119
CPlugin * pSavedPlugin = pDoc->m_CurrentPlugin;
11151120
pDoc->m_CurrentPlugin = pPlugin;
11161121

1117-
1118-
11191122
// now call the routine in the plugin
11201123

1121-
if (CallLuaWithTraceBack (pL, n - 2, LUA_MULTRET)) // true on error
1124+
if (CallLuaWithTraceBack (pL, n, LUA_MULTRET)) // true on error
11221125
{
11231126

11241127
// here for execution error in plugin function ...
@@ -1170,8 +1173,8 @@ static int L_CallPlugin (lua_State *L)
11701173
// if we are calling ourselves, don't make a copy of everything
11711174
if (pL == L)
11721175
{
1173-
lua_insert (L, 3); // put return code as third item (after plugin ID, function name), pushing others up
1174-
return 1 + ret_n - 2; // eOK plus all returned values, minus plugin ID and function name
1176+
lua_insert (L, 1); // put return code as first item pushing others up
1177+
return 1 + ret_n; // eOK plus all returned values
11751178
}
11761179

11771180
lua_checkstack (L, ret_n + 1); // check we can push eOK plus all the return results
@@ -1229,9 +1232,9 @@ static int L_CallPlugin (lua_State *L)
12291232

12301233
// old fashioned way ...
12311234
lua_pushnumber (L, pDoc->CallPlugin (
1232-
sPluginID, // PluginID
1233-
sRoutine, // Routine
1234-
my_optstring (L, 3, "") // Argument - optional
1235+
sPluginID, // PluginID - was argument 1 earlier on
1236+
sRoutine, // Routine - was argument 2 earlier on
1237+
my_optstring (L, 1, "") // Argument - optional (originally argument 3)
12351238
));
12361239
return 1; // number of result fields
12371240
} // end of L_CallPlugin

0 commit comments

Comments
 (0)