From e893ac885837d1d5ae6c15b33657fdebf6a53eb8 Mon Sep 17 00:00:00 2001 From: "Nicolas \"Pixel\" Noble" Date: Thu, 25 Jul 2013 07:51:04 +0200 Subject: Cleaning up the Lua code a bit. --- includes/BLua.h | 72 ++++++-------------------------------------- src/BLua.cc | 93 ++++++++++++++++++++++++++++++++++++++------------------- 2 files changed, 72 insertions(+), 93 deletions(-) diff --git a/includes/BLua.h b/includes/BLua.h index ce49a01..bf7caae 100644 --- a/includes/BLua.h +++ b/includes/BLua.h @@ -139,12 +139,9 @@ class Lua { void load(const String &, bool docall = true) throw (GeneralException); void dumpvars(IO out, const String & prefix, int idx = -1); Lua thread(bool saveit = true); - Lua thread(const String &, int nargs = 0, bool saveit = true); - Lua thread(IO in, int nargs = 0, bool saveit = true); int yield(int nresults = 0) { return lua_yield(L, nresults); } + bool yielded() { return lua_status(L) == LUA_YIELD; } bool resume(int nargs = 0) throw (GeneralException); - bool resume(const String &, int nargs = 0); - bool resume(IO in, int nargs = 0); void showstack(int level = M_INFO); void showerror(); int getmetatable(int i = -1) { checkstack(); return lua_getmetatable(L, i); } @@ -273,76 +270,25 @@ struct lua_functypes_t { template T * LuaObjectFactory::getMe(Lua & L, int idx) { return L.recast(idx); } +class LuaHelpersBase { + protected: + static void validate(const lua_functypes_t & entry, bool method, int n, Lua & L, const char * className); +}; + template -class LuaHelpers { +class LuaHelpers : public LuaHelpersBase { public: static int method_multiplex(int caller, lua_State * __L, int (*proceed)(Lua & L, int n, T * obj, int caller), int (*proceed_static)(Lua & L, int n, int caller), lua_functypes_t * tab, bool method) { Lua L(__L); int add = method ? 1 : 0; int n = L.gettop() - add; T * obj = 0; - int i, j, mask; - bool invalid = false, arg_valid; + static ClassName cn = ClassName((T *) 0); if (method) obj = LuaObjectFactory::getMe(L); - if ((n < tab[caller].minargs) || (n > tab[caller].maxargs)) { - invalid = true; - } else { - for (i = 0; i < tab[caller].maxargs && !invalid; i++) { - if (n >= (i + 1)) { - arg_valid = false; - for (j = 0; j < MAX_TYPE && !arg_valid; j++) { - mask = 1 << j; - if (tab[caller].argtypes[i] & mask) { - switch(mask) { - case BLUA_OBJECT: - if (L.istable(i + 1 + add)) { - L.push("__obj"); - L.gettable(i + 1 + add); - arg_valid = L.isuserdata(); - L.pop(); - } else { - arg_valid = L.isnil(i + 1 + add); - } - break; - case BLUA_TABLE: - arg_valid = L.istable(i + 1 + add); - break; - case BLUA_BOOLEAN: - arg_valid = L.isboolean(i + 1 + add); - break; - case BLUA_NUMBER: - arg_valid = L.isnumber(i + 1 + add); - break; - case BLUA_STRING: - arg_valid = L.isstring(i + 1 + add); - break; - case BLUA_FUNCTION: - arg_valid = L.isfunction(i + 1 + add); - break; - case BLUA_NIL: - arg_valid = L.isnil(i + 1 + add); - break; - case BLUA_USERDATA: - arg_valid = L.isuserdata(i + 1 + add) || L.islightuserdata(i + 1 + add); - break; - } - } - } - invalid = !arg_valid; - } - } - } - - if (invalid) { - if (method) { - L.error(String("Invalid arguments to method `") + typeid(T).name() + "::" + tab[caller].name + "'"); - } else { - L.error(String("Invalid arguments to function `") + typeid(T).name() + " " + tab[caller].name + "'"); - } - } + validate(tab[caller], method, n, L, cn.c_str()); if (method) { return proceed(L, n, obj, caller); diff --git a/src/BLua.cc b/src/BLua.cc index 29b68c2..6005035 100644 --- a/src/BLua.cc +++ b/src/BLua.cc @@ -699,18 +699,6 @@ Balau::Lua Balau::Lua::thread(bool saveit) { return Lua(L1); } -Balau::Lua Balau::Lua::thread(const String & code, int nargs, bool saveit) { - Lua L1 = thread(saveit); - L1.resume(code, nargs); - return L1; -} - -Balau::Lua Balau::Lua::thread(IO h, int nargs, bool saveit) { - Lua L1 = thread(saveit); - L1.resume(h, nargs); - return L1; -} - bool Balau::Lua::resume(int nargs) throw (GeneralException) { int r; @@ -740,24 +728,6 @@ bool Balau::Lua::resume(int nargs) throw (GeneralException) { } } -bool Balau::Lua::resume(IO h, int nargs) { - bool r; - - load(h, false); - r = resume(nargs); - - return r; -} - -bool Balau::Lua::resume(const String & s, int nargs) { - bool r; - - load(s, false); - r = resume(nargs); - - return r; -} - void Balau::Lua::showstack(int level) { int n = lua_gettop(L); int i; @@ -874,3 +844,66 @@ void Balau::LuaObjectFactory::pushDestruct(Lua & L) { m_wantsDestruct = true; push(L); } + +void Balau::LuaHelpersBase::validate(const lua_functypes_t & entry, bool method, int n, Lua & L, const char * className) { + bool invalid = false, arg_valid; + int i, j, mask; + int add = method ? 1 : 0; + + if ((n < entry.minargs) || (n > entry.maxargs)) { + invalid = true; + } else { + for (i = 0; i < entry.maxargs && !invalid; i++) { + if (n >= (i + 1)) { + arg_valid = false; + for (j = 0; j < MAX_TYPE && !arg_valid; j++) { + mask = 1 << j; + if (entry.argtypes[i] & mask) { + switch(mask) { + case BLUA_OBJECT: + if (L.istable(i + 1 + add)) { + L.push("__obj"); + L.gettable(i + 1 + add); + arg_valid = L.isuserdata(); + L.pop(); + } else { + arg_valid = L.isnil(i + 1 + add); + } + break; + case BLUA_TABLE: + arg_valid = L.istable(i + 1 + add); + break; + case BLUA_BOOLEAN: + arg_valid = L.isboolean(i + 1 + add); + break; + case BLUA_NUMBER: + arg_valid = L.isnumber(i + 1 + add); + break; + case BLUA_STRING: + arg_valid = L.isstring(i + 1 + add); + break; + case BLUA_FUNCTION: + arg_valid = L.isfunction(i + 1 + add); + break; + case BLUA_NIL: + arg_valid = L.isnil(i + 1 + add); + break; + case BLUA_USERDATA: + arg_valid = L.isuserdata(i + 1 + add) || L.islightuserdata(i + 1 + add); + break; + } + } + } + invalid = !arg_valid; + } + } + } + + if (invalid) { + if (method) { + L.error(String("Invalid arguments to method `") + className + "::" + entry.name + "'"); + } else { + L.error(String("Invalid arguments to function `") + className + " " + entry.name + "'"); + } + } +} -- cgit v1.2.3