From c3a7893d9f2c67614257bc0dbfa802b9cc9ff056 Mon Sep 17 00:00:00 2001 From: deva Date: Wed, 4 Jun 2008 14:28:03 +0000 Subject: Added extensive error checking and handling in the LUA wrapper. --- client/lua.cc | 69 +++++++++++++++++++++++++++++++++++++---------------------- client/lua.h | 2 ++ 2 files changed, 46 insertions(+), 25 deletions(-) diff --git a/client/lua.cc b/client/lua.cc index 7902cd2..abd834d 100644 --- a/client/lua.cc +++ b/client/lua.cc @@ -36,7 +36,6 @@ static int _getValue(lua_State *L) if(n != 1) { char errstr[512]; sprintf(errstr, "Number of args expected 0, got %d", n); - fprintf(stderr, errstr); lua_pushstring(L, errstr); lua_error(L); return 1; @@ -48,7 +47,6 @@ static int _getValue(lua_State *L) LUA *lua = (LUA*)lua_touserdata(L, lua_gettop(L)); if(!lua) { - printf("No LUA pointer!\n"); lua_pushstring(L, "No LUA pointer!"); lua_error(L); return 1; @@ -66,7 +64,6 @@ static int _setValue(lua_State *L) if(n != 2) { char errstr[512]; sprintf(errstr, "Number of args expected 0, got %d", n); - fprintf(stderr, errstr); lua_pushstring(L, errstr); lua_error(L); return 0; @@ -81,7 +78,6 @@ static int _setValue(lua_State *L) LUA *lua = (LUA*)lua_touserdata(L, lua_gettop(L)); if(!lua) { - printf("No LUA pointer!\n"); lua_pushstring(L, "No LUA pointer!"); lua_error(L); return 1; @@ -98,7 +94,8 @@ LUA::LUA(MacroWindow *macrowindow) L = luaL_newstate(); if(L == NULL) { - // throw LUADataParserException("Could not create LUA state."); + error("Could not create LUA state."); + return; } luaL_openlibs(L); @@ -127,36 +124,58 @@ void LUA::setValue(QString name, QString value) bool LUA::run(QString program, QString name, QString value) { - printf("Running %s\n", program.toStdString().c_str()); + if(L == NULL) { + error("LUA state not initialized!"); + return false; + } + + printf("Running %s on %s with value %s\n", + program.toStdString().c_str(), + name.toStdString().c_str(), + value.toStdString().c_str() ); if(macrowindow->luaprograms.contains(program) == false) return false; - QString luacode = "value = " + value + "\nname = " + name + "\n"; + lua_pushstring(L, value.toStdString().c_str()); + lua_setglobal(L, "value"); + + lua_pushstring(L, name.toStdString().c_str()); + lua_setglobal(L, "name"); + + int top = lua_gettop(L); + QString luaprogram = macrowindow->luaprograms.value(program); - luacode += luaprogram; - - int s = luaL_loadbuffer(L, - luacode.toStdString().c_str(), - luacode.size(), - program.toStdString().c_str()); - switch(s) { - case 0: //no errors; - break; - case LUA_ERRSYNTAX: //syntax error during pre-compilation; - case LUA_ERRMEM: //memory allocation error. - case LUA_ERRFILE: //cannot open/read the file. - //throw LUADataParserException(lua_tostring(L, lua_gettop(L))); - break; - default: - //throw LUADataParserException("Unknown return value of luaL_loadfile."); - break; + if(luaL_loadbuffer(L, + luaprogram.toStdString().c_str(), + luaprogram.size(), + program.toStdString().c_str())) { + error(lua_tostring(L, lua_gettop(L))); + return false; } // Run the loaded code - lua_pcall(L, 0, LUA_MULTRET, 0); + if(lua_pcall(L, 0, LUA_MULTRET, 0)) { + error(lua_tostring(L, lua_gettop(L))); + return false; + } + + if(top != lua_gettop(L) - 1) { + error("Program did not return a single value.\n"); + return false; + } + + if(lua_isboolean(L, lua_gettop(L)) == false) { + error("Program did not return a boolean value.\n"); + return false; + } bool res = lua_toboolean(L, lua_gettop(L)); lua_pop(L, 1); return res; } + +void LUA::error(QString message) +{ + printf("LUA ERROR: %s\n", message.toStdString().c_str()); +} diff --git a/client/lua.h b/client/lua.h index b0737a0..1499279 100644 --- a/client/lua.h +++ b/client/lua.h @@ -44,6 +44,8 @@ public: QString getValue(QString name); void setValue(QString name, QString value); + void error(QString message); + private: lua_State *L; MacroWindow *macrowindow; -- cgit v1.2.3