From bf6d081600bcf53068cedaac745187b1ebf20029 Mon Sep 17 00:00:00 2001 From: spx Date: Wed, 9 Feb 2011 06:10:25 +0000 Subject: [PATCH] Lua: Fixed an issue that unneeded stack elements aren't popped. --- Library/MeasureScript.cpp | 17 +++++++++-------- Library/lua/LuaScript.cpp | 31 ++++++++++++++++--------------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/Library/MeasureScript.cpp b/Library/MeasureScript.cpp index 5e5fcb29..12635e5b 100644 --- a/Library/MeasureScript.cpp +++ b/Library/MeasureScript.cpp @@ -207,16 +207,14 @@ void CMeasureScript::ReadConfig(CConfigParser& parser, const WCHAR* section) std::wstring wstrKey = ConvertToWide(strKey); std::wstring wstrValue = parser.ReadString(section, wstrKey.c_str(), L""); - + if (!wstrValue.empty()) { std::string strStrVal = ConvertToAscii(wstrValue.c_str()); const char* strValue = strStrVal.c_str(); - - lua_pushstring(L, strValue); - lua_settable(L, -3); - lua_pushstring(L, strKey); + lua_pushstring(L, strValue); + lua_setfield(L, -3, strKey); } } } @@ -266,10 +264,13 @@ void CMeasureScript::RunFunctionWithMeter(const char* p_strFunction, CMeter* p_p { LuaManager::ReportErrors(L); } - } - // Clean up - lua_pop(L, 1); + lua_pop(L, 1); + } + else + { + lua_pop(L, 2); + } } void CMeasureScript::MeterMouseEvent(CMeter* p_pMeter, MOUSE p_eMouse) diff --git a/Library/lua/LuaScript.cpp b/Library/lua/LuaScript.cpp index eafb4dd6..82a54cdc 100644 --- a/Library/lua/LuaScript.cpp +++ b/Library/lua/LuaScript.cpp @@ -7,7 +7,7 @@ LuaScript::LuaScript(lua_State* p_pState, const char* p_strFile, const char* p_s m_bInitialized(true) { int result = luaL_loadfile(m_pState, p_strFile); - + // If the file loaded okay. if (result == 0) { @@ -42,12 +42,14 @@ LuaScript::LuaScript(lua_State* p_pState, const char* p_strFile, const char* p_s { m_bInitialized = false; LuaManager::LuaLog(LOG_ERROR, "Script: Could not run file: %s", lua_tostring(m_pState, -1)); + lua_pop(m_pState, 1); } } else { m_bInitialized = false; LuaManager::LuaLog(LOG_ERROR, "Script: Could not run file: %s", lua_tostring(m_pState, -1)); + lua_pop(m_pState, 1); } } @@ -85,6 +87,8 @@ void LuaScript::BindVariable(const char* p_strName, void* p_pValue, const char* double LuaScript::RunFunctionDouble(const char* p_strFuncName) { + double result = -1; + if (m_bInitialized && p_strFuncName) { // Push our table onto the stack @@ -104,21 +108,20 @@ double LuaScript::RunFunctionDouble(const char* p_strFuncName) LuaManager::LuaLog(LOG_ERROR, "Script: Function '%s:%s' must return a number", m_strTableName, p_strFuncName); } - double d = lua_tonumber(m_pState, -1); - - lua_pop(m_pState, 1); - - return d; + result = lua_tonumber(m_pState, -1); } - lua_pop(m_pState, 1); + // Pop both the table and the return value off the stack. + lua_pop(m_pState, 2); } - return -1; + return result; } std::wstring LuaScript::RunFunctionString(const char* p_strFuncName) { + std::wstring result; + if (m_bInitialized && p_strFuncName) { // Push our table onto the stack @@ -139,16 +142,14 @@ std::wstring LuaScript::RunFunctionString(const char* p_strFuncName) } const char* str = lua_tostring(m_pState, -1); - - lua_pop(m_pState, 1); - - return ConvertToWide(str); + result = ConvertToWide(str); } - lua_pop(m_pState, 1); + // Pop both the table and the return value off the stack. + lua_pop(m_pState, 2); } - return L""; + return result; } void LuaScript::RunFunction(const char* p_strFuncName) @@ -182,7 +183,7 @@ bool LuaScript::FunctionExists(const char* p_strFuncName) // Push the function onto the stack lua_getfield(m_pState, -1, p_strFuncName); - if (lua_isfunction( m_pState, lua_gettop(m_pState))) + if (lua_isfunction( m_pState, -1)) { bExists = true; }