diff options
-rw-r--r-- | include/BLua.h | 133 |
1 files changed, 72 insertions, 61 deletions
diff --git a/include/BLua.h b/include/BLua.h index 4f8acc2..bee8836 100644 --- a/include/BLua.h +++ b/include/BLua.h @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: BLua.h,v 1.14 2004-05-01 11:48:57 pixel Exp $ */ +/* $Id: BLua.h,v 1.15 2004-05-02 00:41:39 pixel Exp $ */ #ifndef __BLUA_H__ #define __BLUA_H__ @@ -107,15 +107,18 @@ class LuaException : public GeneralException { }; enum Lua_types_t { - LUA_ANY = 0, - LUA_OBJECT, - LUA_TABLE, - LUA_BOOLEAN, - LUA_NUMBER, - LUA_STRING, - LUA_FUNCTION, + LUA_OBJECT = 0x01, + LUA_TABLE = 0x02, + LUA_BOOLEAN = 0x04, + LUA_NUMBER = 0x08, + LUA_STRING = 0x10, + LUA_FUNCTION = 0x20, + LUA_NIL = 0x40, + LUA_ANY = 0x7f, }; +#define MAX_TYPE 7 + #define MAXARGS 32 struct lua_functypes_t { @@ -186,68 +189,76 @@ template <class T> class LuaHelpers : public Base { 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 = Lua::find(_L); - int add = method ? 1 : 0; - int n = L->gettop() - add; - T * obj = 0; - int i; - bool invalid = false; - - if (method) - obj = (T *) LuaObject::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)) { - switch(tab[caller].argtypes[i]) { - case LUA_ANY: - break; - case LUA_OBJECT: - if (L->istable(i + 1 + add)) { - L->push("__obj"); - L->gettable(i + 1 + add); - invalid = !L->isuserdata(); - L->pop(); - } else { - invalid = !L->isnil(i + 1 + add); + Lua * L = Lua::find(_L); + int add = method ? 1 : 0; + int n = L->gettop() - add; + T * obj = 0; + int i, j, mask; + bool invalid = false, arg_valid; + + if (method) + obj = (T *) LuaObject::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 LUA_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 LUA_TABLE: + arg_valid = L->istable(i + 1 + add); + break; + case LUA_BOOLEAN: + arg_valid = L->isboolean(i + 1 + add); + break; + case LUA_NUMBER: + arg_valid = L->isnumber(i + 1 + add); + break; + case LUA_STRING: + arg_valid = L->isstring(i + 1 + add); + break; + case LUA_FUNCTION: + arg_valid = L->isfunction(i + 1 + add); + break; + case LUA_NIL: + arg_valid = L->isnil(i + 1 + add); + break; + } + } } - break; - case LUA_TABLE: - invalid = !L->istable(i + 1 + add); - break; - case LUA_BOOLEAN: - invalid = !L->isboolean(i + 1 + add); - break; - case LUA_NUMBER: - invalid = !L->isnumber(i + 1 + add); - break; - case LUA_STRING: - invalid = !L->isstring(i + 1 + add); - break; - case LUA_FUNCTION: - invalid = !L->isfunction(i + 1 + add); - break; + invalid = !arg_valid; } } } - } - if (invalid) { + 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 + "'"); + } + } + if (method) { - L->error(String("Invalid arguments to method `") + typeid(T).name() + "::" + tab[caller].name + "'"); + return proceed(L, n, obj, caller); } else { - L->error(String("Invalid arguments to function `") + typeid(T).name() + "::" + tab[caller].name + "'"); + return proceed_static(L, n, caller); } } - - if (method) { - return proceed(L, n, obj, caller); - } else { - return proceed_static(L, n, caller); - } - } }; |