Lua: Fixed an issue that unneeded stack elements aren't popped.

This commit is contained in:
spx 2011-02-09 06:10:25 +00:00
parent cde1e517ac
commit bf6d081600
2 changed files with 25 additions and 23 deletions

View File

@ -214,9 +214,7 @@ void CMeasureScript::ReadConfig(CConfigParser& parser, const WCHAR* section)
const char* strValue = strStrVal.c_str(); const char* strValue = strStrVal.c_str();
lua_pushstring(L, strValue); lua_pushstring(L, strValue);
lua_settable(L, -3); lua_setfield(L, -3, strKey);
lua_pushstring(L, strKey);
} }
} }
} }
@ -266,11 +264,14 @@ void CMeasureScript::RunFunctionWithMeter(const char* p_strFunction, CMeter* p_p
{ {
LuaManager::ReportErrors(L); 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) void CMeasureScript::MeterMouseEvent(CMeter* p_pMeter, MOUSE p_eMouse)
{ {

View File

@ -42,12 +42,14 @@ LuaScript::LuaScript(lua_State* p_pState, const char* p_strFile, const char* p_s
{ {
m_bInitialized = false; m_bInitialized = false;
LuaManager::LuaLog(LOG_ERROR, "Script: Could not run file: %s", lua_tostring(m_pState, -1)); LuaManager::LuaLog(LOG_ERROR, "Script: Could not run file: %s", lua_tostring(m_pState, -1));
lua_pop(m_pState, 1);
} }
} }
else else
{ {
m_bInitialized = false; m_bInitialized = false;
LuaManager::LuaLog(LOG_ERROR, "Script: Could not run file: %s", lua_tostring(m_pState, -1)); 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 LuaScript::RunFunctionDouble(const char* p_strFuncName)
{ {
double result = -1;
if (m_bInitialized && p_strFuncName) if (m_bInitialized && p_strFuncName)
{ {
// Push our table onto the stack // 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); LuaManager::LuaLog(LOG_ERROR, "Script: Function '%s:%s' must return a number", m_strTableName, p_strFuncName);
} }
double d = lua_tonumber(m_pState, -1); result = lua_tonumber(m_pState, -1);
lua_pop(m_pState, 1);
return d;
} }
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 LuaScript::RunFunctionString(const char* p_strFuncName)
{ {
std::wstring result;
if (m_bInitialized && p_strFuncName) if (m_bInitialized && p_strFuncName)
{ {
// Push our table onto the stack // 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); const char* str = lua_tostring(m_pState, -1);
result = ConvertToWide(str);
lua_pop(m_pState, 1);
return 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) void LuaScript::RunFunction(const char* p_strFuncName)
@ -182,7 +183,7 @@ bool LuaScript::FunctionExists(const char* p_strFuncName)
// Push the function onto the stack // Push the function onto the stack
lua_getfield(m_pState, -1, p_strFuncName); lua_getfield(m_pState, -1, p_strFuncName);
if (lua_isfunction( m_pState, lua_gettop(m_pState))) if (lua_isfunction( m_pState, -1))
{ {
bExists = true; bExists = true;
} }