add helpers
This commit is contained in:
@@ -673,30 +673,28 @@ 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 )
|
||||||
{
|
{
|
||||||
lua_pushcfunction( L, GetLuaStack );
|
|
||||||
int iErrFunc = lua_gettop( L );
|
|
||||||
|
|
||||||
// load string
|
// load string
|
||||||
{
|
|
||||||
int ret = luaL_loadbuffer( L, sScript.data(), sScript.size(), sName );
|
int ret = luaL_loadbuffer( L, sScript.data(), sScript.size(), sName );
|
||||||
if( ret )
|
if( ret )
|
||||||
{
|
{
|
||||||
LuaHelpers::Pop( L, sError );
|
LuaHelpers::Pop( L, sError );
|
||||||
lua_pop( L, iArgs );
|
|
||||||
lua_remove( L, iErrFunc );
|
|
||||||
for( int i = 0; i < iReturnValues; ++i )
|
|
||||||
lua_pushnil( L );
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// move the function above the params
|
return true;
|
||||||
lua_insert( L, lua_gettop(L) - iArgs );
|
}
|
||||||
|
|
||||||
|
bool LuaHelpers::RunScriptOnStack( Lua *L, RString &sError, int iArgs, int iReturnValues )
|
||||||
|
{
|
||||||
|
lua_pushcfunction( L, GetLuaStack );
|
||||||
|
|
||||||
|
// move the error function above the function and params
|
||||||
|
int iErrFunc = lua_gettop(L) - iArgs - 1;
|
||||||
|
lua_insert( L, iErrFunc );
|
||||||
|
|
||||||
// evaluate
|
// evaluate
|
||||||
{
|
|
||||||
int ret = lua_pcall( L, iArgs, iReturnValues, iErrFunc );
|
int ret = lua_pcall( L, iArgs, iReturnValues, iErrFunc );
|
||||||
if( ret )
|
if( ret )
|
||||||
{
|
{
|
||||||
@@ -706,12 +704,27 @@ bool LuaHelpers::RunScript( Lua *L, const RString &sScript, const RString &sName
|
|||||||
lua_pushnil( L );
|
lua_pushnil( L );
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
lua_remove( L, iErrFunc );
|
lua_remove( L, iErrFunc );
|
||||||
return true;
|
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 );
|
||||||
|
|
||||||
|
return LuaHelpers::RunScriptOnStack( L, sError, iArgs, iReturnValues );
|
||||||
|
}
|
||||||
|
|
||||||
bool LuaHelpers::RunExpression( Lua *L, const RString &sExpression, const RString &sName )
|
bool LuaHelpers::RunExpression( Lua *L, const RString &sExpression, const RString &sName )
|
||||||
{
|
{
|
||||||
RString sError;
|
RString sError;
|
||||||
|
|||||||
@@ -49,9 +49,19 @@ private:
|
|||||||
|
|
||||||
namespace LuaHelpers
|
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
|
* the Lua stack. On error, nils are left on the stack, sError is set and
|
||||||
* false is returned. */
|
* 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 );
|
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
|
/* Run the given expression, returning a single value, and leave the return value on the
|
||||||
|
|||||||
Reference in New Issue
Block a user