From 6f88bf18c038c483aab610f163925c7aaa306272 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Thu, 17 Feb 2005 05:46:31 +0000 Subject: [PATCH] Cleanup: change RunExpression to a RunScript helper. Never tack on "return "; the caller can do that if it wants. Instead, just act as a helper to display a generic dialog on error. --- stepmania/src/LuaManager.cpp | 12 ++++++------ stepmania/src/LuaManager.h | 9 ++++++--- stepmania/src/LuaReference.cpp | 8 ++++++-- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/stepmania/src/LuaManager.cpp b/stepmania/src/LuaManager.cpp index 915fb9b5f1..6003ef50ec 100644 --- a/stepmania/src/LuaManager.cpp +++ b/stepmania/src/LuaManager.cpp @@ -313,12 +313,12 @@ bool LuaManager::RunScript( const CString &sScript, const CString &sName, CStrin } -bool LuaManager::RunExpression( const CString &sExpression ) +bool LuaManager::RunScript( const CString &sExpression, const CString &sName ) { CString sError; - if( !RunScript( "return " + sExpression, "in", sError, 1 ) ) + if( !RunScript( sExpression, sName.size()? sName:"in", sError, 1 ) ) { - sError = ssprintf( "Lua runtime error parsing \"%s\": %s", sExpression.c_str(), sError.c_str() ); + sError = ssprintf( "Lua runtime error parsing \"%s\": %s", sName.size()? sName.c_str():sExpression.c_str(), sError.c_str() ); Dialog::OK( sError, "LUA_ERROR" ); return false; } @@ -328,7 +328,7 @@ bool LuaManager::RunExpression( const CString &sExpression ) bool LuaManager::RunExpressionB( const CString &str ) { - if( !RunExpression( str ) ) + if( !RunScript( "return " + str ) ) return false; /* Don't accept a function as a return value. */ @@ -343,7 +343,7 @@ bool LuaManager::RunExpressionB( const CString &str ) float LuaManager::RunExpressionF( const CString &str ) { - if( !RunExpression( str ) ) + if( !RunScript( "return " + str ) ) return 0; /* Don't accept a function as a return value. */ @@ -363,7 +363,7 @@ int LuaManager::RunExpressionI( const CString &str ) bool LuaManager::RunExpressionS( const CString &str, CString &sOut ) { - if( !RunExpression( str ) ) + if( !RunScript( "return " + str ) ) return false; /* Don't accept a function as a return value. */ diff --git a/stepmania/src/LuaManager.h b/stepmania/src/LuaManager.h index 04bd8a0d33..bc69bf0a17 100644 --- a/stepmania/src/LuaManager.h +++ b/stepmania/src/LuaManager.h @@ -24,9 +24,14 @@ public: /* Reset the environment, freeing any globals left over by previously executed scripts. */ void ResetState(); - /* Run a complete script in the global environment, which returns no value. */ + /* Run a script with the given name. Return values are left on the Lua stack. + * Returns false on error, with sError set*/ bool RunScript( const CString &sScript, const CString &sName, CString &sError, int iReturnValues = 0 ); + /* Convenience: run a script with one return value, displaying an error on failure. + * The return value is left on the Lua stack. */ + bool RunScript( const CString &sExpression, const CString &sName = "" ); + /* Run an expression in the global environment, returning the given type. */ bool RunExpressionB( const CString &str ); float RunExpressionF( const CString &str ); @@ -60,8 +65,6 @@ public: /* Read the table at the top of the stack back into a vector. */ static void ReadArrayFromTable( vector &aOut, lua_State *L = NULL ); - /* Run an expression. The result is left on the Lua stack. */ - bool RunExpression( const CString &sExpression ); lua_State *L; }; diff --git a/stepmania/src/LuaReference.cpp b/stepmania/src/LuaReference.cpp index 4418dc1f4c..898c718dba 100644 --- a/stepmania/src/LuaReference.cpp +++ b/stepmania/src/LuaReference.cpp @@ -113,13 +113,17 @@ void LuaReference::ReRegister() void LuaExpression::SetFromExpression( const CString &sExpression ) { - m_sExpression = sExpression; + m_sExpression = "return " + sExpression; Register(); } void LuaExpression::Register() { - LUA->RunExpression( m_sExpression ); + if( !LUA->RunScript( m_sExpression ) ) + { + this->SetFromNil(); + return; + } /* Store the result. */ this->SetFromStack();