- yaahhoo, now we properly detect invalid LUA functions etc for scenarios

This commit is contained in:
Mark Vejvoda 2010-10-09 10:43:23 +00:00
parent 6cf68531ea
commit 3963b99e37
2 changed files with 23 additions and 9 deletions

View File

@ -34,6 +34,8 @@ class LuaScript{
private: private:
LuaHandle *luaState; LuaHandle *luaState;
int argumentCount; int argumentCount;
string currentLuaFunction;
bool currentLuaFunctionIsValid;
public: public:
LuaScript(); LuaScript();

View File

@ -48,6 +48,8 @@ public:
LuaScript::LuaScript() { LuaScript::LuaScript() {
Lua_STREFLOP_Wrapper streflopWrapper; Lua_STREFLOP_Wrapper streflopWrapper;
currentLuaFunction = "";
currentLuaFunctionIsValid = false;
luaState= luaL_newstate(); luaState= luaL_newstate();
luaL_openlibs(luaState); luaL_openlibs(luaState);
@ -68,6 +70,8 @@ LuaScript::~LuaScript() {
void LuaScript::loadCode(const string &code, const string &name){ void LuaScript::loadCode(const string &code, const string &name){
Lua_STREFLOP_Wrapper streflopWrapper; Lua_STREFLOP_Wrapper streflopWrapper;
//printf("Code [%s]\nName [%s]\n",code.c_str(),name.c_str());
int errorCode= luaL_loadbuffer(luaState, code.c_str(), code.length(), name.c_str()); int errorCode= luaL_loadbuffer(luaState, code.c_str(), code.length(), name.c_str());
if(errorCode !=0 ) { if(errorCode !=0 ) {
throw runtime_error("Error loading lua code: " + errorToString(errorCode)); throw runtime_error("Error loading lua code: " + errorToString(errorCode));
@ -82,30 +86,38 @@ void LuaScript::loadCode(const string &code, const string &name){
throw runtime_error("Error initializing lua: " + errorToString(errorCode)); throw runtime_error("Error initializing lua: " + errorToString(errorCode));
} }
const char *errMsg = lua_tostring(luaState, -1); //const char *errMsg = lua_tostring(luaState, -1);
//printf("END of call to Name [%s]\n",name.c_str());
//SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] name [%s], errorCode = %d,\ncode [%s]\n",__FILE__,__FUNCTION__,__LINE__,name.c_str(),errorCode,code.c_str()); //SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] name [%s], errorCode = %d,\ncode [%s]\n",__FILE__,__FUNCTION__,__LINE__,name.c_str(),errorCode,code.c_str());
SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] name [%s], errorCode = %d, errMsg = %s\n",__FILE__,__FUNCTION__,__LINE__,name.c_str(),errorCode,(errMsg != NULL ? errMsg : "")); SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] name [%s], errorCode = %d\n",__FILE__,__FUNCTION__,__LINE__,name.c_str(),errorCode);
} }
void LuaScript::beginCall(const string& functionName){ void LuaScript::beginCall(const string& functionName) {
Lua_STREFLOP_Wrapper streflopWrapper; Lua_STREFLOP_Wrapper streflopWrapper;
currentLuaFunction = functionName;
SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] functionName [%s]\n",__FILE__,__FUNCTION__,__LINE__,functionName.c_str()); SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] functionName [%s]\n",__FILE__,__FUNCTION__,__LINE__,functionName.c_str());
SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] functionName [%s]\n",__FILE__,__FUNCTION__,__LINE__,functionName.c_str()); SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] functionName [%s]\n",__FILE__,__FUNCTION__,__LINE__,functionName.c_str());
lua_getglobal(luaState, functionName.c_str()); lua_getglobal(luaState, functionName.c_str());
//if( lua_isfunction(luaState,lua_gettop(luaState)) == false) { currentLuaFunctionIsValid = lua_isfunction(luaState,lua_gettop(luaState));
// throw runtime_error("Error unknown lua function called: [" + functionName + "]");
//}
argumentCount= 0; argumentCount= 0;
} }
void LuaScript::endCall(){ void LuaScript::endCall() {
Lua_STREFLOP_Wrapper streflopWrapper; Lua_STREFLOP_Wrapper streflopWrapper;
SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] currentLuaFunction [%s], currentLuaFunctionIsValid = %d\n",__FILE__,__FUNCTION__,__LINE__,currentLuaFunction.c_str(),currentLuaFunctionIsValid);
lua_pcall(luaState, argumentCount, 0, 0);
if(currentLuaFunctionIsValid == true) {
int errorCode= lua_pcall(luaState, argumentCount, 0, 0);
if(errorCode !=0 ) {
throw runtime_error("Error calling lua function [" + currentLuaFunction + "] error: " + errorToString(errorCode));
}
}
} }
void LuaScript::registerFunction(LuaFunction luaFunction, const string &functionName) { void LuaScript::registerFunction(LuaFunction luaFunction, const string &functionName) {