diff options
Diffstat (limited to 'plugins/MirLua/src/mlua_metatable.h')
-rw-r--r-- | plugins/MirLua/src/mlua_metatable.h | 48 |
1 files changed, 29 insertions, 19 deletions
diff --git a/plugins/MirLua/src/mlua_metatable.h b/plugins/MirLua/src/mlua_metatable.h index b918971077..ddd67090d0 100644 --- a/plugins/MirLua/src/mlua_metatable.h +++ b/plugins/MirLua/src/mlua_metatable.h @@ -1,6 +1,7 @@ #ifndef _LUA_METATABLE_H_
#define _LUA_METATABLE_H_
+#include <map>
#include <cstddef>
#include <functional>
@@ -17,6 +18,7 @@ union MTFieldVal const char *string;
const char *stringA;
const wchar_t *stringW;
+ lua_CFunction function;
};
struct MTField
@@ -25,7 +27,6 @@ struct MTField MTFieldVal val;
};
-
class CMTField
{
public:
@@ -50,6 +51,21 @@ public: }
};
+class CMTFieldFunction : public CMTField
+{
+ lua_CFunction func;
+public:
+
+ CMTFieldFunction(lua_CFunction f) : func(f) {}
+
+ virtual MTField GetValue(void *obj)
+ {
+ MTField tmp = { LUA_TFUNCTION };
+ tmp.val.function = func;
+ return tmp;
+ }
+};
+
template <typename Obj>
class CMTFieldLambda : public CMTField
{
@@ -67,7 +83,6 @@ public: }
};
-
template<typename T>
class MT
{
@@ -77,10 +92,10 @@ private: static const char *name;
static std::map<std::string, CMTField*> fields;
- static void Init(lua_State *L, T **obj)
+ static T* Init(lua_State *L)
{
luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
- *obj = (T*)lua_touserdata(L, 1);
+ return (T*)lua_touserdata(L, 1);
}
static int Index(lua_State *L, T* /*obj*/)
@@ -97,7 +112,7 @@ private: static int lua__new(lua_State *L)
{
T **udata = (T**)lua_newuserdata(L, sizeof(T*));
- Init(L, udata);
+ *udata = Init(L);
if (*udata == NULL)
{
lua_pushnil(L);
@@ -108,17 +123,6 @@ private: return 1;
}
- static int lua__call(lua_State *L)
- {
- int nargs = lua_gettop(L);
- lua_pushcfunction(L, lua__new);
- for (int i = 2; i <= nargs; i++)
- lua_pushvalue(L, i);
- luaM_pcall(L, nargs - 1, 1);
-
- return 1;
- }
-
static int lua__bnot(lua_State *L)
{
T *obj = *(T**)luaL_checkudata(L, 1, MT::name);
@@ -163,6 +167,9 @@ private: case LUA_TLIGHTUSERDATA:
lua_pushlightuserdata(L, fieldVal.val.userdata);
break;
+ case LUA_TFUNCTION:
+ lua_pushcfunction(L, fieldVal.val.function);
+ break;
default:
lua_pushnil(L);
}
@@ -230,8 +237,6 @@ public: MT::name = tname;
luaL_newmetatable(L, MT::name);
- lua_pushcfunction(L, lua__call);
- lua_setfield(L, -2, "__call");
lua_pushcfunction(L, lua__index);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, lua__bnot);
@@ -247,7 +252,6 @@ public: lua_setfield(L, -2, "new");
lua_pushvalue(L, -1);
lua_setglobal(L, MT::name);
- luaL_setmetatable(L, MT::name);
lua_pop(L, 1);
}
@@ -270,6 +274,12 @@ public: return *this;
}
+ MT& Field(const lua_CFunction f, const char *name)
+ {
+ fields[name] = new CMTFieldFunction(f);
+ return *this;
+ }
+
static void Set(lua_State *L, T *obj)
{
if (obj == NULL)
|