add helpers

This commit is contained in:
Glenn Maynard
2007-02-04 08:03:47 +00:00
parent 5dd54edec1
commit f45875adc7
2 changed files with 52 additions and 29 deletions
+41 -28
View File
@@ -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 )
+11 -1
View File
@@ -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