@@ -969,6 +969,15 @@ static int L_CallPlugin (lua_State *L)
969
969
970
970
const char * sPluginID = my_checkstring (L, 1 );
971
971
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
+
972
981
int i; // for iterating through arguments / return values
973
982
974
983
// preliminary checks ...
@@ -1022,7 +1031,7 @@ static int L_CallPlugin (lua_State *L)
1022
1031
1023
1032
if (pPlugin->m_ScriptEngine ->IsLua ())
1024
1033
{
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)
1026
1035
1027
1036
lua_State *pL = pPlugin->m_ScriptEngine ->L ; // plugin's Lua state
1028
1037
@@ -1044,10 +1053,7 @@ static int L_CallPlugin (lua_State *L)
1044
1053
1045
1054
// if we are calling ourselves, don't make a copy of everything
1046
1055
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
1051
1057
else
1052
1058
{ // calling a different plugin
1053
1059
@@ -1056,11 +1062,10 @@ static int L_CallPlugin (lua_State *L)
1056
1062
// but NOT: table, function, userdata, thread
1057
1063
1058
1064
// 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 );
1062
1067
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++)
1064
1069
{
1065
1070
1066
1071
switch (lua_type (L, i))
@@ -1090,7 +1095,7 @@ static int L_CallPlugin (lua_State *L)
1090
1095
lua_settop (pL, 0 ); // clear target plugin's stack to remove whatever we pushed onto it
1091
1096
lua_pushnumber (L, eBadParameter);
1092
1097
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
1094
1099
luaL_typename (L, i));
1095
1100
lua_pushstring (L, strError);
1096
1101
return 2 ; // eBadParameter, explanation
@@ -1114,11 +1119,9 @@ static int L_CallPlugin (lua_State *L)
1114
1119
CPlugin * pSavedPlugin = pDoc->m_CurrentPlugin ;
1115
1120
pDoc->m_CurrentPlugin = pPlugin;
1116
1121
1117
-
1118
-
1119
1122
// now call the routine in the plugin
1120
1123
1121
- if (CallLuaWithTraceBack (pL, n - 2 , LUA_MULTRET)) // true on error
1124
+ if (CallLuaWithTraceBack (pL, n, LUA_MULTRET)) // true on error
1122
1125
{
1123
1126
1124
1127
// here for execution error in plugin function ...
@@ -1170,8 +1173,8 @@ static int L_CallPlugin (lua_State *L)
1170
1173
// if we are calling ourselves, don't make a copy of everything
1171
1174
if (pL == L)
1172
1175
{
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
1175
1178
}
1176
1179
1177
1180
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)
1229
1232
1230
1233
// old fashioned way ...
1231
1234
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)
1235
1238
));
1236
1239
return 1 ; // number of result fields
1237
1240
} // end of L_CallPlugin
0 commit comments