allow LuaManager::RunExpression to return a function
show the filename when a full script has an error use LuaManager::RunExpression in LuaExpression
This commit is contained in:
@@ -71,7 +71,7 @@ void ActorCommands::Register()
|
|||||||
|
|
||||||
|
|
||||||
CString s2 = s.str();
|
CString s2 = s.str();
|
||||||
LUA->RunScript( s2, 1 );
|
LUA->RunScript( s2, "in", 1 );
|
||||||
|
|
||||||
/* The function is now on the stack. */
|
/* The function is now on the stack. */
|
||||||
this->SetFromStack();
|
this->SetFromStack();
|
||||||
|
|||||||
@@ -261,16 +261,16 @@ bool LuaManager::RunScriptFile( const CString &sFile )
|
|||||||
return false;
|
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
|
// load string
|
||||||
{
|
{
|
||||||
ChunkReaderData data;
|
ChunkReaderData data;
|
||||||
data.buf = &sScript;
|
data.buf = &sScript;
|
||||||
int ret = lua_load( L, ChunkReaderString, &data, "in" );
|
int ret = lua_load( L, ChunkReaderString, &data, sName );
|
||||||
|
|
||||||
if( ret )
|
if( ret )
|
||||||
{
|
{
|
||||||
@@ -303,16 +303,9 @@ bool LuaManager::RunScript( const CString &sScript, int iReturnValues )
|
|||||||
|
|
||||||
bool LuaManager::RunExpression( const CString &sExpression )
|
bool LuaManager::RunExpression( const CString &sExpression )
|
||||||
{
|
{
|
||||||
if( !RunScript( "return " + sExpression, 1 ) )
|
if( !RunScript( "return " + sExpression, "in", 1 ) )
|
||||||
return false;
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -321,6 +314,10 @@ bool LuaManager::RunExpressionB( const CString &str )
|
|||||||
if( !RunExpression( str ) )
|
if( !RunExpression( str ) )
|
||||||
return false;
|
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 );
|
bool result = !!lua_toboolean( L, -1 );
|
||||||
lua_pop( L, -1 );
|
lua_pop( L, -1 );
|
||||||
|
|
||||||
@@ -332,6 +329,10 @@ float LuaManager::RunExpressionF( const CString &str )
|
|||||||
if( !RunExpression( str ) )
|
if( !RunExpression( str ) )
|
||||||
return 0;
|
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 );
|
float result = (float) lua_tonumber( L, -1 );
|
||||||
lua_pop( L, -1 );
|
lua_pop( L, -1 );
|
||||||
|
|
||||||
@@ -343,7 +344,9 @@ bool LuaManager::RunExpressionS( const CString &str, CString &sOut )
|
|||||||
if( !RunExpression( str ) )
|
if( !RunExpression( str ) )
|
||||||
return false;
|
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 );
|
sOut = lua_tostring( L, -1 );
|
||||||
lua_pop( L, -1 );
|
lua_pop( L, -1 );
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ public:
|
|||||||
void ResetState();
|
void ResetState();
|
||||||
|
|
||||||
/* Run a complete script in the global environment, which returns no value. */
|
/* 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. */
|
/* Run an expression in the global environment, returning the given type. */
|
||||||
bool RunExpressionB( const CString &str );
|
bool RunExpressionB( const CString &str );
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ void LuaExpression::SetFromExpression( const CString &sExpression )
|
|||||||
|
|
||||||
void LuaExpression::Register()
|
void LuaExpression::Register()
|
||||||
{
|
{
|
||||||
LUA->RunScript( "return " + m_sExpression, 1 );
|
LUA->RunExpression( m_sExpression );
|
||||||
|
|
||||||
/* Store the result. */
|
/* Store the result. */
|
||||||
this->SetFromStack();
|
this->SetFromStack();
|
||||||
|
|||||||
Reference in New Issue
Block a user