00001
00002
00003 extern "C"
00004 {
00005
00006 #include <lua.h>
00007 #include <lualib.h>
00008 #include <lauxlib.h>
00009
00010 }
00011
00012
00013 #include "luarunner.h"
00014 #include "luastate.h"
00015 #include "../ascstring.h"
00016 #include "../basestrm.h"
00017
00018
00019 LuaRunner::LuaRunner( LuaState& luaState ) : state ( luaState )
00020 {
00021
00022 }
00023
00024
00025 void LuaRunner::runFile( const ASCString& filename )
00026 {
00027 try {
00028 tnfilestream stream ( filename, tnstream::reading );
00029 ASCString line = stream.readString(true);
00030 while ( line.size() ) {
00031
00032 int errorCode = luaL_loadbuffer(state.get(), line.c_str(), line.size(), stream.getLocation().c_str());
00033 if ( !errorCode )
00034 errorCode = lua_pcall(state.get(), 0, 0, 0);
00035
00036 if (errorCode) {
00037 errors += lua_tostring(state.get(), -1);
00038 errors += "\n";
00039 lua_pop(state.get(), 1);
00040 }
00041
00042 line = stream.readString(true);
00043 }
00044 } catch ( treadafterend err ) {
00045 }
00046 }
00047
00048 void LuaRunner::runCommand( const ASCString& command )
00049 {
00050 int errorCode = luaL_loadbuffer(state.get(), command.c_str(), command.size(), command.c_str());
00051 if ( !errorCode )
00052 errorCode = lua_pcall(state.get(), 0, 0, 0);
00053
00054 if (errorCode) {
00055 errors += lua_tostring(state.get(), -1);
00056 errors += "\n";
00057 lua_pop(state.get(), 1);
00058 }
00059 }
00060
00061 const ASCString& LuaRunner::getErrors()
00062 {
00063 return errors;
00064 }
00065