From f45875adc7301e1f0d6a43fc0d8e1832d9032faa Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Sun, 4 Feb 2007 08:03:47 +0000 Subject: [PATCH] add helpers --- stepmania/src/LuaManager.cpp | 69 +++++++++++++++++++++--------------- stepmania/src/LuaManager.h | 12 ++++++- 2 files changed, 52 insertions(+), 29 deletions(-) diff --git a/stepmania/src/LuaManager.cpp b/stepmania/src/LuaManager.cpp index f57be91b79..98b7a8fcb3 100644 --- a/stepmania/src/LuaManager.cpp +++ b/stepmania/src/LuaManager.cpp @@ -673,43 +673,56 @@ bool LuaHelpers::RunScriptFile( const RString &sFile ) } -bool LuaHelpers::RunScript( Lua *L, const RString &sScript, const RString &sName, RString &sError, int iArgs, int iReturnValues ) +bool LuaHelpers::LoadScript( Lua *L, const RString &sScript, const RString &sName, RString &sError ) +{ + // load string + int ret = luaL_loadbuffer( L, sScript.data(), sScript.size(), sName ); + if( ret ) + { + LuaHelpers::Pop( L, sError ); + return false; + } + + return true; +} + +bool LuaHelpers::RunScriptOnStack( Lua *L, RString &sError, int iArgs, int iReturnValues ) { lua_pushcfunction( L, GetLuaStack ); - int iErrFunc = lua_gettop( L ); - // load string + // move the error function above the function and params + int iErrFunc = lua_gettop(L) - iArgs - 1; + lua_insert( L, iErrFunc ); + + // evaluate + int ret = lua_pcall( L, iArgs, iReturnValues, iErrFunc ); + if( ret ) { - int ret = luaL_loadbuffer( L, sScript.data(), sScript.size(), sName ); - if( ret ) - { - LuaHelpers::Pop( L, sError ); - lua_pop( L, iArgs ); - lua_remove( L, iErrFunc ); - for( int i = 0; i < iReturnValues; ++i ) - lua_pushnil( L ); - return false; - } + LuaHelpers::Pop( L, sError ); + lua_remove( L, iErrFunc ); + for( int i = 0; i < iReturnValues; ++i ) + lua_pushnil( L ); + return false; + } + + lua_remove( L, iErrFunc ); + return true; +} + +bool LuaHelpers::RunScript( Lua *L, const RString &sScript, const RString &sName, RString &sError, int iArgs, int iReturnValues ) +{ + if( !LoadScript(L, sScript, sName, sError) ) + { + lua_pop( L, iArgs ); + for( int i = 0; i < iReturnValues; ++i ) + lua_pushnil( L ); + return false; } // move the function above the params lua_insert( L, lua_gettop(L) - iArgs ); - // evaluate - { - int ret = lua_pcall( L, iArgs, iReturnValues, iErrFunc ); - if( ret ) - { - LuaHelpers::Pop( L, sError ); - lua_remove( L, iErrFunc ); - for( int i = 0; i < iReturnValues; ++i ) - lua_pushnil( L ); - return false; - } - } - - lua_remove( L, iErrFunc ); - return true; + return LuaHelpers::RunScriptOnStack( L, sError, iArgs, iReturnValues ); } bool LuaHelpers::RunExpression( Lua *L, const RString &sExpression, const RString &sName ) diff --git a/stepmania/src/LuaManager.h b/stepmania/src/LuaManager.h index a558a1525c..bf1bb15117 100644 --- a/stepmania/src/LuaManager.h +++ b/stepmania/src/LuaManager.h @@ -49,9 +49,19 @@ private: namespace LuaHelpers { - /* Run a script with the given name. The given number of return values are left on + /* Load the given script with the given name. On success, the resulting + * chunk will be on the stack. On error, the error is stored in sError + * and the stack is unchanged. */ + bool LoadScript( Lua *L, const RString &sScript, const RString &sName, RString &sError ); + + /* Run the function with arguments at the top of the stack, with the given + * number of arguments. The specified number of return values are left on * the Lua stack. On error, nils are left on the stack, sError is set and * false is returned. */ + bool RunScriptOnStack( Lua *L, RString &sError, int iArgs = 0, int iReturnValues = 0 ); + + /* LoadScript the given script, and RunScriptOnStack it. iArgs arguments are + * at the top of the stack. */ bool RunScript( Lua *L, const RString &sScript, const RString &sName, RString &sError, int iArgs = 0, int iReturnValues = 0 ); /* Run the given expression, returning a single value, and leave the return value on the