Script: Fixed that the value of the Script measure is not reset when an error occurs.

In the following case, for example, the value of the measure used to remain 10 even after an error. Now it will default to 0 on error.

function Initialize()
	i = 0
end

function Update()
	if i < 5 then
		i = i + 1
		return 10
	else
		i() -- error here, execution stops
		return 2
	end
end
This commit is contained in:
Birunthan Mohanathas 2011-07-25 17:59:43 +00:00
parent b23217d840
commit 23f4a31bf0
2 changed files with 11 additions and 25 deletions

View File

@ -93,24 +93,13 @@ bool CMeasureScript::Update()
return false;
}
if (m_HasUpdateFunction)
if (!(m_HasUpdateFunction && m_LuaScript->RunFunctionWithReturn(g_UpdateFunctionName, m_Value, m_StringValue)) &&
!(m_HasGetStringFunction && m_LuaScript->RunFunctionWithReturn(g_GetStringFunctionName, m_Value, m_StringValue)))
{
bool ret = m_LuaScript->RunFunctionWithReturn(g_UpdateFunctionName, m_Value, m_StringValue);
if (!ret)
if (!m_StringValue.empty())
{
// Update() didn't return anything. For backwards compatibility, check for GetStringValue() first
if (m_HasGetStringFunction)
{
m_LuaScript->RunFunctionWithReturn(g_GetStringFunctionName, m_Value, m_StringValue);
}
else
{
std::wstring error = L"Script: Update() in measure [";
error += m_Name;
error += L"] is not returning a valid number or string.";
Log(LOG_WARNING, error.c_str());
}
m_Value = 0;
m_StringValue.clear();
}
}
@ -184,9 +173,9 @@ void CMeasureScript::ReadConfig(CConfigParser& parser, const WCHAR* section)
tolua_pushusertype(L, m_MeterWindow, "CMeterWindow");
lua_settable(L, -3);
lua_pushstring(L, "RAINMETER");
tolua_pushusertype(L, m_MeterWindow->GetMainObject(), "CRainmeter");
lua_settable(L, -3);
//lua_pushstring(L, "RAINMETER");
//tolua_pushusertype(L, m_MeterWindow->GetMainObject(), "CRainmeter");
//lua_settable(L, -3);
// Look in the properties table for values to read from the section.
lua_getfield(L, -1, "PROPERTIES");
@ -197,7 +186,6 @@ void CMeasureScript::ReadConfig(CConfigParser& parser, const WCHAR* section)
while (lua_next(L, -2))
{
lua_pop(L, 1);
const char* strKey = lua_tostring(L, -1);
std::wstring wstrKey = ConvertToWide(strKey);

View File

@ -171,14 +171,12 @@ bool LuaScript::RunFunctionWithReturn(const char* funcName, double& numValue, st
{
if (lua_isstring(m_State, -1))
{
// Type is LUA_TSTRING or LUA_TNUMBER
const char* str = lua_tostring(m_State, -1);
strValue = ConvertToWide(str);
// A number is a string and numerical string (e.g. "10") is a number, so check for it here
if (lua_isnumber(m_State, -1))
{
numValue = lua_tonumber(m_State, -1);
}
// lua_tonumber() returns 0 when type is non-number
numValue = lua_tonumber(m_State, -1);
ret = true;
}