summaryrefslogtreecommitdiff
path: root/client/lua.cc
diff options
context:
space:
mode:
authordeva <deva>2008-06-04 14:28:03 +0000
committerdeva <deva>2008-06-04 14:28:03 +0000
commitc3a7893d9f2c67614257bc0dbfa802b9cc9ff056 (patch)
tree7c87c6905f8464f53f4f95c4d078a9c9939ce4e9 /client/lua.cc
parent0543325eb3f00c6e8ee1eb1b493708060439d54b (diff)
Added extensive error checking and handling in the LUA wrapper.
Diffstat (limited to 'client/lua.cc')
-rw-r--r--client/lua.cc69
1 files changed, 44 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());
+}