From 0a0ad8714c7c7f4c9cb3b8979f1d431e6e322b26 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Tue, 15 Feb 2005 02:15:26 +0000 Subject: [PATCH] allow LuaManager::RunExpression to return a function show the filename when a full script has an error use LuaManager::RunExpression in LuaExpression --- stepmania/src/ActorCommands.cpp | 2 +- stepmania/src/LuaManager.cpp | 27 +++++++++++++++------------ stepmania/src/LuaManager.h | 2 +- stepmania/src/LuaReference.cpp | 2 +- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/stepmania/src/ActorCommands.cpp b/stepmania/src/ActorCommands.cpp index ecaf415376..b9565d207c 100644 --- a/stepmania/src/ActorCommands.cpp +++ b/stepmania/src/ActorCommands.cpp @@ -71,7 +71,7 @@ void ActorCommands::Register() CString s2 = s.str(); - LUA->RunScript( s2, 1 ); + LUA->RunScript( s2, "in", 1 ); /* The function is now on the stack. */ this->SetFromStack(); diff --git a/stepmania/src/LuaManager.cpp b/stepmania/src/LuaManager.cpp index eea66ce1cc..90a078b29f 100644 --- a/stepmania/src/LuaManager.cpp +++ b/stepmania/src/LuaManager.cpp @@ -261,16 +261,16 @@ bool LuaManager::RunScriptFile( const CString &sFile ) return false; } - return RunScript( sScript ); + return RunScript( sScript, sFile ); } -bool LuaManager::RunScript( const CString &sScript, int iReturnValues ) +bool LuaManager::RunScript( const CString &sScript, const CString &sName, int iReturnValues ) { // load string { ChunkReaderData data; data.buf = &sScript; - int ret = lua_load( L, ChunkReaderString, &data, "in" ); + int ret = lua_load( L, ChunkReaderString, &data, sName ); if( ret ) { @@ -303,16 +303,9 @@ bool LuaManager::RunScript( const CString &sScript, int iReturnValues ) bool LuaManager::RunExpression( const CString &sExpression ) { - if( !RunScript( "return " + sExpression, 1 ) ) + if( !RunScript( "return " + sExpression, "in", 1 ) ) return false; - ASSERT_M( lua_gettop(L) == 1, ssprintf("%i", lua_gettop(L)) ); - - /* Don't accept a function as a return value; if you really want to use a function - * as a boolean, convert it before returning. */ - if( lua_isfunction( L, -1 ) ) - RageException::Throw( "result is a function; did you forget \"()\"?" ); - return true; } @@ -321,6 +314,10 @@ bool LuaManager::RunExpressionB( const CString &str ) if( !RunExpression( str ) ) return false; + /* Don't accept a function as a return value. */ + if( lua_isfunction( L, -1 ) ) + RageException::Throw( "result is a function; did you forget \"()\"?" ); + bool result = !!lua_toboolean( L, -1 ); lua_pop( L, -1 ); @@ -332,6 +329,10 @@ float LuaManager::RunExpressionF( const CString &str ) if( !RunExpression( str ) ) return 0; + /* Don't accept a function as a return value. */ + if( lua_isfunction( L, -1 ) ) + RageException::Throw( "result is a function; did you forget \"()\"?" ); + float result = (float) lua_tonumber( L, -1 ); lua_pop( L, -1 ); @@ -343,7 +344,9 @@ bool LuaManager::RunExpressionS( const CString &str, CString &sOut ) if( !RunExpression( str ) ) return false; - ASSERT( lua_gettop(L) > 0 ); + /* Don't accept a function as a return value. */ + if( lua_isfunction( L, -1 ) ) + RageException::Throw( "result is a function; did you forget \"()\"?" ); sOut = lua_tostring( L, -1 ); lua_pop( L, -1 ); diff --git a/stepmania/src/LuaManager.h b/stepmania/src/LuaManager.h index 281df3b8d7..608d74a31d 100644 --- a/stepmania/src/LuaManager.h +++ b/stepmania/src/LuaManager.h @@ -22,7 +22,7 @@ public: void ResetState(); /* Run a complete script in the global environment, which returns no value. */ - bool RunScript( const CString &sScript, int iReturnValues = 0 ); + bool RunScript( const CString &sScript, const CString &sName, int iReturnValues = 0 ); /* Run an expression in the global environment, returning the given type. */ bool RunExpressionB( const CString &str ); diff --git a/stepmania/src/LuaReference.cpp b/stepmania/src/LuaReference.cpp index c307de7801..31e094ca9d 100644 --- a/stepmania/src/LuaReference.cpp +++ b/stepmania/src/LuaReference.cpp @@ -114,7 +114,7 @@ void LuaExpression::SetFromExpression( const CString &sExpression ) void LuaExpression::Register() { - LUA->RunScript( "return " + m_sExpression, 1 ); + LUA->RunExpression( m_sExpression ); /* Store the result. */ this->SetFromStack();