From a5797f62629d14e4651efec571eb7bac7c488cb9 Mon Sep 17 00:00:00 2001 From: jacob1 Date: Sat, 10 Jan 2015 20:10:42 -0500 Subject: [PATCH] reset length when running step functions in case a step was unregistered, fixes first half of #210 also cleanup / make it easier to read --- src/lua/LegacyLuaAPI.cpp | 60 +++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/src/lua/LegacyLuaAPI.cpp b/src/lua/LegacyLuaAPI.cpp index 179387fa9..0c61523cf 100644 --- a/src/lua/LegacyLuaAPI.cpp +++ b/src/lua/LegacyLuaAPI.cpp @@ -436,7 +436,7 @@ int luacon_elementwrite(lua_State* l) bool shortcuts = true; int luacon_keyevent(int key, int modifier, int event) { - int kycontinue = 1, i, j, callret; + int kycontinue = 1; lua_State* l=luacon_ci->l; lua_pushstring(l, "keyfunctions"); lua_rawget(l, LUA_REGISTRYINDEX); @@ -448,28 +448,27 @@ int luacon_keyevent(int key, int modifier, int event) lua_pushvalue(l, -2); lua_rawset(l, LUA_REGISTRYINDEX); } - int c=lua_objlen(l, -1); - for(i=1;i<=c && kycontinue;i++) + int len = lua_objlen(l, -1); + for (int i = 1; i <= len && kycontinue; i++) { lua_rawgeti(l, -1, i); lua_pushlstring(l, (const char*)&key, 1); lua_pushinteger(l, key); lua_pushinteger(l, modifier); lua_pushinteger(l, event); - callret = lua_pcall(l, 4, 1, 0); + int callret = lua_pcall(l, 4, 1, 0); if (callret) { if (!strcmp(luacon_geterror(), "Error: Script not responding")) { ui::Engine::Ref().LastTick(gettime()); - for(j=i;j<=c-1;j++) + for (int j = i; j <= len-1; j++) { lua_rawgeti(l, -2, j+1); lua_rawseti(l, -3, j); } lua_pushnil(l); - lua_rawseti(l, -3, c); - c--; + lua_rawseti(l, -3, len); i--; } luacon_ci->Log(CommandInterface::LogError, luacon_geterror()); @@ -481,6 +480,7 @@ int luacon_keyevent(int key, int modifier, int event) kycontinue = lua_toboolean(l, -1); lua_pop(l, 1); } + len = lua_objlen(l, -1); } lua_pop(l, 1); return kycontinue && shortcuts; @@ -488,7 +488,7 @@ int luacon_keyevent(int key, int modifier, int event) int luacon_mouseevent(int mx, int my, int mb, int event, int mouse_wheel) { - int mpcontinue = 1, i, j, callret; + int mpcontinue = 1; lua_State* l=luacon_ci->l; lua_pushstring(l, "mousefunctions"); lua_rawget(l, LUA_REGISTRYINDEX); @@ -500,8 +500,8 @@ int luacon_mouseevent(int mx, int my, int mb, int event, int mouse_wheel) lua_pushvalue(l, -2); lua_rawset(l, LUA_REGISTRYINDEX); } - int c=lua_objlen(l, -1); - for(i=1;i<=c && mpcontinue;i++) + int len = lua_objlen(l, -1); + for (int i = 1; i <= len && mpcontinue; i++) { lua_rawgeti(l, -1, i); lua_pushinteger(l, mx); @@ -509,20 +509,19 @@ int luacon_mouseevent(int mx, int my, int mb, int event, int mouse_wheel) lua_pushinteger(l, mb); lua_pushinteger(l, event); lua_pushinteger(l, mouse_wheel); - callret = lua_pcall(l, 5, 1, 0); + int callret = lua_pcall(l, 5, 1, 0); if (callret) { if (!strcmp(luacon_geterror(), "Error: Script not responding")) { ui::Engine::Ref().LastTick(gettime()); - for(j=i;j<=c-1;j++) + for (int j = i; j <= len-1; j++) { lua_rawgeti(l, -2, j+1); lua_rawseti(l, -3, j); } lua_pushnil(l); - lua_rawseti(l, -3, c); - c--; + lua_rawseti(l, -3, len); i--; } luacon_ci->Log(CommandInterface::LogError, luacon_geterror()); @@ -534,6 +533,7 @@ int luacon_mouseevent(int mx, int my, int mb, int event, int mouse_wheel) mpcontinue = lua_toboolean(l, -1); lua_pop(l, 1); } + len = lua_objlen(l, -1); } lua_pop(l, 1); return mpcontinue; @@ -541,15 +541,14 @@ int luacon_mouseevent(int mx, int my, int mb, int event, int mouse_wheel) int luacon_step(int mx, int my) { - int i, j, callret; - lua_State* l=luacon_ci->l; + lua_State* l = luacon_ci->l; lua_pushinteger(l, my); lua_pushinteger(l, mx); lua_setfield(l, tptProperties, "mousex"); lua_setfield(l, tptProperties, "mousey"); lua_pushstring(l, "stepfunctions"); lua_rawget(l, LUA_REGISTRYINDEX); - if(!lua_istable(l, -1)) + if (!lua_istable(l, -1)) { lua_pop(l, 1); lua_newtable(l); @@ -557,29 +556,29 @@ int luacon_step(int mx, int my) lua_pushvalue(l, -2); lua_rawset(l, LUA_REGISTRYINDEX); } - int c=lua_objlen(l, -1); - for(i=1;i<=c;i++) + int len = lua_objlen(l, -1); + for (int i = 1; i <= len; i++) { lua_rawgeti(l, -1, i); - callret = lua_pcall(l, 0, 0, 0); + int callret = lua_pcall(l, 0, 0, 0); if (callret) { if (!strcmp(luacon_geterror(), "Error: Script not responding")) { ui::Engine::Ref().LastTick(gettime()); - for(j=i;j<=c-1;j++) + for (int j = i; j <= len-1; j++) { lua_rawgeti(l, -2, j+1); lua_rawseti(l, -3, j); } lua_pushnil(l); - lua_rawseti(l, -3, c); - c--; + lua_rawseti(l, -3, len); i--; } luacon_ci->Log(CommandInterface::LogError, luacon_geterror()); lua_pop(l, 1); } + len = lua_objlen(l, -1); } lua_pop(l, 1); return 0; @@ -1538,20 +1537,23 @@ int luatpt_unregister_step(lua_State* l) lua_pushvalue(l, -2); lua_rawset(l, LUA_REGISTRYINDEX); } - int c = lua_objlen(l, -1); - int d = 0; - int i = 0; - for (i=1;i<=c;i++) + int len = lua_objlen(l, -1); + int adjust = 0; + for (int i = 1; i <= len; i++) { - lua_rawgeti(l, -1, i+d); + lua_rawgeti(l, -1, i+adjust); + //unregister the function if (lua_equal(l, 1, -1)) { lua_pop(l, 1); - d++; + adjust++; i--; } + //else, move everything down if we removed something earlier else + { lua_rawseti(l, -2, i); + } } } return 0;