diff options
author | aunsane <aunsane@gmail.com> | 2017-09-13 23:17:50 +0300 |
---|---|---|
committer | aunsane <aunsane@gmail.com> | 2017-09-13 23:17:50 +0300 |
commit | 3dd3aed5c8d4efac30217cb308b9b94ff4b811f6 (patch) | |
tree | 6b872f8ced5e052dbbe3444c6b13c903ae0b0318 /plugins | |
parent | 2879e5200db8fafe4ec76e229d096df5f4ab385c (diff) |
MirLua: ability to create MT from both new and __call
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/MirLua/src/mlua_metatable.h | 39 |
1 files changed, 27 insertions, 12 deletions
diff --git a/plugins/MirLua/src/mlua_metatable.h b/plugins/MirLua/src/mlua_metatable.h index 1cd3f04a56..58dfede077 100644 --- a/plugins/MirLua/src/mlua_metatable.h +++ b/plugins/MirLua/src/mlua_metatable.h @@ -197,14 +197,6 @@ private: return 1;
}
- static int lua__gc(lua_State *L)
- {
- T **obj = (T**)luaL_checkudata(L, 1, MT::name);
- MT::Free(L, obj);
-
- return 0;
- }
-
static int lua__tostring(lua_State *L)
{
T *obj = *(T**)luaL_checkudata(L, 1, MT::name);
@@ -252,6 +244,26 @@ private: return 1;
}
+ static int lua__gc(lua_State *L)
+ {
+ T **obj = (T**)luaL_checkudata(L, 1, MT::name);
+ MT::Free(L, obj);
+
+ return 0;
+ }
+
+ static int lua__call(lua_State *L)
+ {
+ luaL_checktype(L, 1, LUA_TTABLE);
+ luaL_checkany(L, 2);
+
+ lua_getfield(L, 1, "new");
+ lua_pushvalue(L, 2);
+ luaM_pcall(L, 1, 1);
+
+ return 1;
+ }
+
public:
MT(lua_State *L, const char *tname) : L(L)
{
@@ -262,18 +274,21 @@ public: lua_setfield(L, -2, "__index");
lua_pushcfunction(L, lua__bnot);
lua_setfield(L, -2, "__bnot");
- lua_pushcfunction(L, lua__gc);
- lua_setfield(L, -2, "__gc");
lua_pushcfunction(L, lua__tostring);
lua_setfield(L, -2, "__tostring");
+ lua_pushcfunction(L, lua__gc);
+ lua_setfield(L, -2, "__gc");
lua_pop(L, 1);
lua_createtable(L, 0, 1);
lua_pushcfunction(L, lua__new);
lua_setfield(L, -2, "new");
- lua_pushvalue(L, -1);
+
+ lua_createtable(L, 0, 2);
+ lua_pushcfunction(L, lua__call);
+ lua_setfield(L, -2, "__call");
+ lua_setmetatable(L, -2);
lua_setglobal(L, MT::name);
- lua_pop(L, 1);
}
template<typename R>
|