From 7532094f8593a3a0760a7cb453d960b009a6484c Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Mon, 4 Apr 2016 10:31:38 +0000 Subject: MirLua: added MT::Index as fallback on missed key in metatable git-svn-id: http://svn.miranda-ng.org/main/trunk@16585 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/MirLua/src/m_database.cpp | 17 ++++++----------- plugins/MirLua/src/mlua_metatable.h | 23 ++++++++++------------- 2 files changed, 16 insertions(+), 24 deletions(-) (limited to 'plugins/MirLua') diff --git a/plugins/MirLua/src/m_database.cpp b/plugins/MirLua/src/m_database.cpp index 246d96e9f6..a493970bfc 100644 --- a/plugins/MirLua/src/m_database.cpp +++ b/plugins/MirLua/src/m_database.cpp @@ -504,9 +504,8 @@ static luaL_Reg databaseApi[] = #define MT_DBCONTACTWRITESETTING "DBCONTACTWRITESETTING" -static int dbcw__index(lua_State *L) +int MT::Index(lua_State *L, DBCONTACTWRITESETTING *dbcw) { - DBCONTACTWRITESETTING *dbcw = (DBCONTACTWRITESETTING*)luaL_checkudata(L, 1, MT_DBCONTACTWRITESETTING); const char *key = luaL_checkstring(L, 2); if (mir_strcmpi(key, "Module") == 0) @@ -542,7 +541,7 @@ static int dbcw__index(lua_State *L) lua_pushnumber(L, dbcw->value.cpbVal); luaM_pcall(L, 2, 1); } - break; + break; default: lua_pushnil(L); } @@ -567,7 +566,7 @@ void MT::Init(lua_State *L, DBEVENTINFO **dbei) db_event_get((MEVENT)hDbEvent, (*dbei)); } -void MT::Free(DBEVENTINFO **dbei) +void MT::Free(lua_State*, DBEVENTINFO **dbei) { mir_free((*dbei)->pBlob); } @@ -596,10 +595,8 @@ void MT::Init(lua_State *L, CONTACTINFO **ci) (*ci)->hContact = hContact; } -static int ci__index(lua_State *L) +int MT::Index(lua_State *L, CONTACTINFO *ci) { - CONTACTINFO *ci = (CONTACTINFO*)luaL_checkudata(L, 1, MT_CONTACTINFO); - if (lua_isnumber(L, 2)) ci->dwFlag = lua_tointeger(L, 2); else if (lua_isstring(L, 2)) @@ -709,8 +706,7 @@ LUAMOD_API int luaopen_m_database(lua_State *L) MT(L, MT_DBCONTACTWRITESETTING) .Field(&DBCONTACTWRITESETTING::szModule, "Module", LUA_TSTRINGA) - .Field(&DBCONTACTWRITESETTING::szSetting, "Setting", LUA_TSTRINGA) - .Method(dbcw__index, "__index"); + .Field(&DBCONTACTWRITESETTING::szSetting, "Setting", LUA_TSTRINGA); lua_pop(L, 1); MT(L, MT_DBEVENTINFO) @@ -723,8 +719,7 @@ LUAMOD_API int luaopen_m_database(lua_State *L) lua_pop(L, 1); MT(L, "CONTACTINFO") - .Field(&CONTACTINFO::hContact, "hContact", LUA_TINTEGER) - .Method(ci__index, "__index"); + .Field(&CONTACTINFO::hContact, "hContact", LUA_TINTEGER); lua_pop(L, 1); return 1; diff --git a/plugins/MirLua/src/mlua_metatable.h b/plugins/MirLua/src/mlua_metatable.h index b8fcdec22b..5048c39288 100644 --- a/plugins/MirLua/src/mlua_metatable.h +++ b/plugins/MirLua/src/mlua_metatable.h @@ -63,8 +63,13 @@ private: //memcpy(*obj, udata, sizeof(T)); *obj = (T*)lua_touserdata(L, 1); } - - static void Free(T **obj) + + static int Index(lua_State *L, T */*obj*/) + { + lua_pushnil(L); + } + + static void Free(lua_State */*L*/, T **obj) { *obj = NULL; } @@ -130,7 +135,7 @@ private: lua_pushlightuserdata(L, field->GetValue(obj)); break; default: - lua_pushnil(L); + return Index(L, obj); } return 1; @@ -139,7 +144,8 @@ private: static int lua__gc(lua_State *L) { T **obj = (T**)luaL_checkudata(L, 1, MT::name); - MT::Free(obj); + MT::Free(L, obj); + return 0; } @@ -176,15 +182,6 @@ public: return *this; } - MT& Method(lua_CFunction func, const char *name) - { - luaL_getmetatable(L, MT::name); - lua_pushcfunction(L, func); - lua_setfield(L, -2, name); - lua_pop(L, 1); - return *this; - } - static void Set(lua_State *L, T *obj) { if (obj == NULL) -- cgit v1.2.3