From 294ed0c031072489f520c90e373b2f24aa16ed8c Mon Sep 17 00:00:00 2001 From: deva Date: Wed, 4 Jun 2008 08:16:12 +0000 Subject: Introduced the errorbox to communicate server errors to the client. --- server/src/luaquerymapper.cc | 22 ++++++++++++++++++++-- server/src/luaquerymapper.h | 1 + server/src/server.cc | 19 ++++++++----------- 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/server/src/luaquerymapper.cc b/server/src/luaquerymapper.cc index b698fe9..2de1d3c 100644 --- a/server/src/luaquerymapper.cc +++ b/server/src/luaquerymapper.cc @@ -78,6 +78,8 @@ LUAQueryMapper::LUAQueryMapper(QueryResult &res) // Run program (init) lua_pcall(L, 0, LUA_MULTRET, 0); + + clean_top = lua_gettop(L); } LUAQueryMapper::~LUAQueryMapper() @@ -87,6 +89,14 @@ LUAQueryMapper::~LUAQueryMapper() Value LUAQueryMapper::map(const std::string &mapper) { + Value v; + + if(mapper == "") { + printf("Empty LUA mapper detected!\n"); + v.timestamp = 0; + v.value = ""; + } + int s = luaL_loadbuffer(L, mapper.c_str(), mapper.size(), "mapper"); switch(s) { case 0: //no errors; @@ -104,8 +114,16 @@ Value LUAQueryMapper::map(const std::string &mapper) // Run the loaded code lua_pcall(L, 0, LUA_MULTRET, 0); - Value v; - + // Check if app messed up the stack. + if(lua_gettop(L) != clean_top) { + printf("LUA mapper messed up the stack (wrong number of return values)!\n"); + lua_pop(L, lua_gettop(L) - clean_top); + Value v; + v.timestamp = 0; + v.value = ""; + return v; + } + v.timestamp = lua_tointeger(L, lua_gettop(L)); lua_pop(L, 1); v.value = lua_tostring(L, lua_gettop(L)); diff --git a/server/src/luaquerymapper.h b/server/src/luaquerymapper.h index d5e03b0..83866a0 100644 --- a/server/src/luaquerymapper.h +++ b/server/src/luaquerymapper.h @@ -52,6 +52,7 @@ public: private: lua_State *L; + int clean_top; }; diff --git a/server/src/server.cc b/server/src/server.cc index a896102..9229f28 100644 --- a/server/src/server.cc +++ b/server/src/server.cc @@ -51,17 +51,14 @@ static std::string error_box(std::string message) { std::string errorbox; - errorbox += " \n"; - errorbox += " \n"; - errorbox += " \n"; - errorbox += " \n"; - errorbox += "