From 7e78bc7d9b3530c999898dc56de668ac8a466102 Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Thu, 26 Nov 2015 20:43:36 +0000 Subject: MirLua: more MT functionality git-svn-id: http://svn.miranda-ng.org/main/trunk@15780 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/MirLua/src/m_protocols.cpp | 8 ++++---- plugins/MirLua/src/m_windows.cpp | 6 +++++- plugins/MirLua/src/mlua_metatable.h | 28 ++++++++++++++++++++-------- 3 files changed, 29 insertions(+), 13 deletions(-) (limited to 'plugins') diff --git a/plugins/MirLua/src/m_protocols.cpp b/plugins/MirLua/src/m_protocols.cpp index 222a766129..8ca1c38460 100644 --- a/plugins/MirLua/src/m_protocols.cpp +++ b/plugins/MirLua/src/m_protocols.cpp @@ -19,7 +19,7 @@ static int lua_GetProto(lua_State *L) PROTOCOLDESCRIPTOR* pd = ::Proto_IsProtocolLoaded(ptrA(mir_utf8decodeA(name))); if (pd) - lua_pushlightuserdata(L, pd); + MT::Set(L, pd); else lua_pushnil(L); @@ -36,7 +36,7 @@ static int lua_ProtoIterator(lua_State *L) { lua_pushinteger(L, (i + 1)); lua_replace(L, lua_upvalueindex(1)); - lua_pushlightuserdata(L, protos[i]); + MT::Set(L, protos[i]); } else lua_pushnil(L); @@ -121,7 +121,7 @@ static int lua_GetAccount(lua_State *L) PROTOACCOUNT* pa = ::Proto_GetAccount(moduleName); if (pa) - lua_pushlightuserdata(L, pa); + MT::Set(L, pa); else lua_pushnil(L); @@ -138,7 +138,7 @@ static int lua_AccountIterator(lua_State *L) { lua_pushinteger(L, (i + 1)); lua_replace(L, lua_upvalueindex(1)); - lua_pushlightuserdata(L, accounts[i]); + MT::Set(L, accounts[i]); } else lua_pushnil(L); diff --git a/plugins/MirLua/src/m_windows.cpp b/plugins/MirLua/src/m_windows.cpp index c42be30335..b78f9f7278 100644 --- a/plugins/MirLua/src/m_windows.cpp +++ b/plugins/MirLua/src/m_windows.cpp @@ -121,12 +121,16 @@ static int lua_FindIterator(lua_State *L) return lua_FindIterator(L); } + LARGE_INTEGER size; + size.HighPart = ffd.nFileSizeHigh; + size.LowPart = ffd.nFileSizeLow; + lua_newtable(L); lua_pushliteral(L, "Name"); lua_pushstring(L, T2Utf(ffd.cFileName)); lua_settable(L, -3); lua_pushliteral(L, "Size"); - lua_pushinteger(L, (ffd.nFileSizeHigh * (MAXDWORD + 1)) + ffd.nFileSizeLow); + lua_pushinteger(L, size.QuadPart); lua_settable(L, -3); lua_pushliteral(L, "IsFile"); lua_pushboolean(L, !(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)); diff --git a/plugins/MirLua/src/mlua_metatable.h b/plugins/MirLua/src/mlua_metatable.h index 0d60cb278e..b0d31cf707 100644 --- a/plugins/MirLua/src/mlua_metatable.h +++ b/plugins/MirLua/src/mlua_metatable.h @@ -36,7 +36,7 @@ private: static int lua__new(lua_State *L) { T *obj = NULL; - T **udata = NULL; + T *udata = NULL; int type = lua_type(L, 1); switch (type) @@ -45,9 +45,9 @@ private: obj = (T*)MT::Load(L); if (obj == NULL) break; //case LUA_TNONE: - udata = (T**)lua_newuserdata(L, sizeof(T*)); - *udata = MT::Init(obj); - //case LUA_TUSERDATA: + udata = (T*)lua_newuserdata(L, sizeof(T)); + memcpy(udata, obj, sizeof(T)); + case LUA_TUSERDATA: luaL_setmetatable(L, MT::name); return 1; } @@ -58,7 +58,7 @@ private: static int lua__index(lua_State *L) { - T *obj = *(T**)luaL_checkudata(L, 1, MT::name); + T *obj = (T*)luaL_checkudata(L, 1, MT::name); const char *key = lua_tostring(L, 2); auto it = fields.find(key); @@ -104,13 +104,12 @@ private: static int lua__gc(lua_State *L) { - T** obj = (T**)luaL_checkudata(L, 1, MT::name); - MT::Free(obj); + T* obj = (T*)luaL_checkudata(L, 1, MT::name); + MT::Free(&obj); return 0; } static T* Load(lua_State *L) { return (T*)lua_touserdata(L, 1); } - static T* Init(T *val) { return val; } static void Free(T **obj) { *obj = NULL; } public: @@ -136,6 +135,19 @@ public: fields[name] = new MTField(offset, size, type); return *this; } + + static void Set(lua_State *L, T* obj) + { + if (obj == NULL) + { + lua_pushnil(L); + return; + } + + T *udata = (T*)lua_newuserdata(L, sizeof(T)); + memcpy(udata, obj, sizeof(T)); + luaL_setmetatable(L, MT::name); + } }; template -- cgit v1.2.3