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.
This commit is contained in:
@@ -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. */
|
||||
|
||||
@@ -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<bool> &aOut, lua_State *L = NULL );
|
||||
|
||||
/* Run an expression. The result is left on the Lua stack. */
|
||||
bool RunExpression( const CString &sExpression );
|
||||
lua_State *L;
|
||||
};
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user