1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
#include "luarunner.h"
#include "luastate.h"
#include "../ascstring.h"
#include "../basestrm.h"
LuaRunner::LuaRunner( LuaState& luaState ) : state ( luaState )
{
}
void LuaRunner::runFile( const ASCString& filename )
{
try {
tnfilestream stream ( filename, tnstream::reading );
ASCString line = stream.readString(true);
while ( line.size() ) {
int errorCode = luaL_loadbuffer(state.get(), line.c_str(), line.size(), stream.getLocation().c_str());
if ( !errorCode )
errorCode = lua_pcall(state.get(), 0, 0, 0);
if (errorCode) {
errors += lua_tostring(state.get(), -1);
errors += "\n";
lua_pop(state.get(), 1); /* pop error message from the stack */
}
line = stream.readString(true);
}
} catch ( treadafterend err ) {
}
}
void LuaRunner::runCommand( const ASCString& command )
{
int errorCode = luaL_loadbuffer(state.get(), command.c_str(), command.size(), command.c_str());
if ( !errorCode )
errorCode = lua_pcall(state.get(), 0, 0, 0);
if (errorCode) {
errors += lua_tostring(state.get(), -1);
errors += "\n";
lua_pop(state.get(), 1); /* pop error message from the stack */
}
}
const ASCString& LuaRunner::getErrors()
{
return errors;
}
|