From 23f4a31bf05ff0ae986223b8dd6d544f815881d4 Mon Sep 17 00:00:00 2001 From: Birunthan Mohanathas Date: Mon, 25 Jul 2011 17:59:43 +0000 Subject: [PATCH] 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 --- Library/MeasureScript.cpp | 28 ++++++++-------------------- Library/lua/LuaScript.cpp | 8 +++----- 2 files changed, 11 insertions(+), 25 deletions(-) diff --git a/Library/MeasureScript.cpp b/Library/MeasureScript.cpp index f4853c6b..52dca406 100644 --- a/Library/MeasureScript.cpp +++ b/Library/MeasureScript.cpp @@ -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); diff --git a/Library/lua/LuaScript.cpp b/Library/lua/LuaScript.cpp index fd628822..2747ff41 100644 --- a/Library/lua/LuaScript.cpp +++ b/Library/lua/LuaScript.cpp @@ -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; }