From 7540dae59505b5c5d38507a05116c493cce628cb Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Sat, 2 Jan 2016 20:06:32 +0000 Subject: MirLua: metatable fixes git-svn-id: http://svn.miranda-ng.org/main/trunk@15997 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/MirLua/src/m_database.cpp | 92 ++++++++++++------------------------- plugins/MirLua/src/m_protocols.cpp | 28 +---------- plugins/MirLua/src/m_schedule.cpp | 37 ++++++++++++++- plugins/MirLua/src/m_windows.cpp | 10 ++++ plugins/MirLua/src/mlua.cpp | 4 +- plugins/MirLua/src/mlua_metatable.h | 50 ++++++++++++-------- plugins/MirLua/src/mlua_utils.cpp | 22 +-------- 7 files changed, 112 insertions(+), 131 deletions(-) (limited to 'plugins') diff --git a/plugins/MirLua/src/m_database.cpp b/plugins/MirLua/src/m_database.cpp index 192997920c..85561e8845 100644 --- a/plugins/MirLua/src/m_database.cpp +++ b/plugins/MirLua/src/m_database.cpp @@ -428,23 +428,6 @@ static luaL_Reg databaseApi[] = #define MT_DBCONTACTWRITESETTING "DBCONTACTWRITESETTING" -static int dbcw__init(lua_State *L) -{ - DBCONTACTWRITESETTING *udata = (DBCONTACTWRITESETTING*)lua_touserdata(L, 1); - if (udata == NULL) - { - lua_pushnil(L); - return 1; - } - - DBCONTACTWRITESETTING **dbcw = (DBCONTACTWRITESETTING**)lua_newuserdata(L, sizeof(DBCONTACTWRITESETTING*)); - *dbcw = udata; - - luaL_setmetatable(L, MT_DBCONTACTWRITESETTING); - - return 1; -} - static int dbcw__index(lua_State *L) { DBCONTACTWRITESETTING *dbcw = (DBCONTACTWRITESETTING*)luaL_checkudata(L, 1, MT_DBCONTACTWRITESETTING); @@ -486,68 +469,52 @@ static int dbcw__index(lua_State *L) return 1; } -static const luaL_Reg dbcwMeta[] = -{ - { "__init", dbcw__init }, - { "__index", dbcw__index }, - { NULL, NULL } -}; - /***********************************************/ -DBEVENTINFO* MT::Load(lua_State *L) +#define MT_DBEVENTINFO "DBEVENTINFO" + +void MT::Init(lua_State *L, DBEVENTINFO **dbei) { MEVENT hDbEvent = luaL_checkinteger(L, 1); - DBEVENTINFO *dbei = (DBEVENTINFO*)mir_calloc(sizeof(DBEVENTINFO)); - dbei->cbSize = sizeof(DBEVENTINFO); - dbei->cbBlob = db_event_getBlobSize((MEVENT)hDbEvent); - dbei->pBlob = (PBYTE)mir_calloc(dbei->cbBlob); - db_event_get((MEVENT)hDbEvent, dbei); - return dbei; + (*dbei)->cbSize = sizeof(DBEVENTINFO); + (*dbei)->cbBlob = db_event_getBlobSize((MEVENT)hDbEvent); + (*dbei)->pBlob = (PBYTE)mir_calloc((*dbei)->cbBlob); + db_event_get((MEVENT)hDbEvent, (*dbei)); } void MT::Free(DBEVENTINFO **dbei) { mir_free((*dbei)->pBlob); - mir_free(*dbei); } /***********************************************/ #define MT_CONTACTINFO "CONTACTINFO" -static int ci__init(lua_State *L) +void MT::Init(lua_State *L, CONTACTINFO **ci) { - MCONTACT udata = 0; - switch(lua_type(L, 1)) + MCONTACT hContact = 0; + switch (lua_type(L, 1)) { case LUA_TNUMBER: - udata = lua_tointeger(L, 1); + hContact = lua_tointeger(L, 1); break; case LUA_TLIGHTUSERDATA: - udata = (MCONTACT)lua_touserdata(L, 1); + hContact = (MCONTACT)lua_touserdata(L, 1); break; default: const char *msg = lua_pushfstring(L, "hContact expected, got %s", lua_typename(L, lua_type(L, 1))); luaL_argerror(L, 1, msg); } - MCONTACT *hContact = (MCONTACT*)lua_newuserdata(L, sizeof(MCONTACT)); - *hContact = udata; - - luaL_setmetatable(L, MT_CONTACTINFO); - - return 1; + (*ci)->cbSize = sizeof(CONTACTINFO); + (*ci)->hContact = hContact; } static int ci__index(lua_State *L) { - MCONTACT *hContact = (MCONTACT*)luaL_checkudata(L, 1, MT_CONTACTINFO); - - mir_ptr ci((CONTACTINFO*)mir_calloc(sizeof(CONTACTINFO))); - ci->cbSize = sizeof(CONTACTINFO); - ci->hContact = *hContact; + CONTACTINFO *ci = (CONTACTINFO*)luaL_checkudata(L, 1, MT_CONTACTINFO); if (lua_type(L, 2) == LUA_TNUMBER) ci->dwFlag = lua_tointeger(L, 2); @@ -557,7 +524,7 @@ static int ci__index(lua_State *L) if (mir_strcmpi(key, "Handle") == 0) { - lua_pushinteger(L, *hContact); + lua_pushinteger(L, ci->hContact); return 1; } @@ -631,20 +598,24 @@ static int ci__index(lua_State *L) return 1; } -static const luaL_Reg ciMeta[] = +/***********************************************/ + +static int dbei__test(lua_State *L) { - { "__init", ci__init }, - { "__index", ci__index }, - { NULL, NULL } -}; + int type = lua_type(L, 1); -/***********************************************/ + return 0; +} LUAMOD_API int luaopen_m_database(lua_State *L) { luaL_newlib(L, databaseApi); - MT(L, "DBEVENTINFO") + MT(L, MT_DBCONTACTWRITESETTING) + .Method(dbcw__index, "__index"); + lua_pop(L, 1); + + MT(L, MT_DBEVENTINFO) .Field(&DBEVENTINFO::szModule, "Module", LUA_TSTRINGA) .Field(&DBEVENTINFO::timestamp, "Timestamp", LUA_TINTEGER) .Field(&DBEVENTINFO::eventType, "Type", LUA_TINTEGER) @@ -653,12 +624,9 @@ LUAMOD_API int luaopen_m_database(lua_State *L) .Field(&DBEVENTINFO::pBlob, "Blob", LUA_TLIGHTUSERDATA); lua_pop(L, 1); - luaL_newmetatable(L, MT_DBCONTACTWRITESETTING); - luaL_setfuncs(L, dbcwMeta, 0); - lua_pop(L, 1); - - luaL_newmetatable(L, MT_CONTACTINFO); - luaL_setfuncs(L, ciMeta, 0); + MT(L, "CONTACTINFO") + .Field(&CONTACTINFO::hContact, "hContact", LUA_TINTEGER) + .Method(ci__index, "__index"); lua_pop(L, 1); return 1; diff --git a/plugins/MirLua/src/m_protocols.cpp b/plugins/MirLua/src/m_protocols.cpp index 0a687fe14a..4492c23232 100644 --- a/plugins/MirLua/src/m_protocols.cpp +++ b/plugins/MirLua/src/m_protocols.cpp @@ -158,23 +158,6 @@ static luaL_Reg protocolsApi[] = #define MT_CCSDATA "CCSDATA" -static int ccs__init(lua_State *L) -{ - CCSDATA *udata = (CCSDATA*)lua_touserdata(L, 1); - if (udata == NULL) - { - lua_pushnil(L); - return 1; - } - - CCSDATA **ccs = (CCSDATA**)lua_newuserdata(L, sizeof(CCSDATA*)); - *ccs = udata; - - luaL_setmetatable(L, MT_CCSDATA); - - return 1; -} - static int ccs__index(lua_State *L) { CCSDATA *ccs = *(CCSDATA**)luaL_checkudata(L, 1, MT_CCSDATA); @@ -193,13 +176,6 @@ static int ccs__index(lua_State *L) return 1; } -static luaL_Reg ccsMeta[] = -{ - { "__init", ccs__init }, - { "__index", ccs__index }, - { NULL, NULL } -}; - /***********************************************/ LUAMOD_API int luaopen_m_protocols(lua_State *L) @@ -230,8 +206,8 @@ LUAMOD_API int luaopen_m_protocols(lua_State *L) .Field(&ACKDATA::lParam, "lParam", LUA_TLIGHTUSERDATA); lua_pop(L, 1); - luaL_newmetatable(L, MT_CCSDATA); - luaL_setfuncs(L, ccsMeta, 0); + MT(L, MT_CCSDATA) + .Method(ccs__index, "__index"); lua_pop(L, 1); return 1; diff --git a/plugins/MirLua/src/m_schedule.cpp b/plugins/MirLua/src/m_schedule.cpp index 05563c1cc8..e79d0932c1 100644 --- a/plugins/MirLua/src/m_schedule.cpp +++ b/plugins/MirLua/src/m_schedule.cpp @@ -464,7 +464,18 @@ static int lua__Do(lua_State *L) return 0; } -static const luaL_Reg schedule[] = +static int lua__index(lua_State *L) +{ + int t1 = lua_type(L, 1); + int t2 = lua_type(L, 2); + + //lua_pushvalue(L, 1); + lua_getmetatable(L, 1); + + return 1; +} + +static const luaL_Reg scheduleMeta[] = { { "Second", lua__Second }, { "Seconds", lua__Seconds }, @@ -487,11 +498,15 @@ static const luaL_Reg schedule[] = { "Until", lua__Until }, { "Do", lua__Do }, + //{ "__index", lua__index }, + { NULL, NULL } }; /***********************************************/ +#define MT_SCHEDULETASK "SCHEDULETASK" + static int lua__At(lua_State *L) { time_t timestamp = time(NULL); @@ -512,9 +527,21 @@ static int lua__Every(lua_State *L) { int interval = luaL_optinteger(L, 1, 0); - luaL_newlib(L, schedule); + int top = lua_gettop(L); + luaL_newlib(L, scheduleMeta); + //lua_newtable(L); lua_pushinteger(L, interval); lua_setfield(L, -2, "Interval"); + top = lua_gettop(L); + + lua_createtable(L, 0, 1); + //lua_pushcfunction(L, lua__index); + lua_pushvalue(L, -2); + lua_setfield(L, -2, "__index"); + lua_setmetatable(L, -2); + top = lua_gettop(L); + + //luaL_setmetatable(L, MT_SCHEDULETASK); return 1; } @@ -532,6 +559,12 @@ LUAMOD_API int luaopen_m_schedule(lua_State *L) { luaL_newlib(L, scheduleApi); + //luaL_newmetatable(L, MT_SCHEDULETASK); + /*lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index");*/ + //luaL_setfuncs(L, scheduleMeta, 0); + //lua_pop(L, 1); + if (hScheduleEvent == NULL) hScheduleEvent = CreateEvent(NULL, FALSE, FALSE, NULL); diff --git a/plugins/MirLua/src/m_windows.cpp b/plugins/MirLua/src/m_windows.cpp index 160f632974..b90b1d808f 100644 --- a/plugins/MirLua/src/m_windows.cpp +++ b/plugins/MirLua/src/m_windows.cpp @@ -72,6 +72,8 @@ static int lua_MessageBox(lua_State *L) return 1; } +/***********************************************/ + static int lua_ShellExecute(lua_State *L) { ptrT command(mir_utf8decodeT(lua_tostring(L, 1))); @@ -84,6 +86,8 @@ static int lua_ShellExecute(lua_State *L) return 0; } +/***********************************************/ + static int lua_FindIterator(lua_State *L) { HANDLE hFind = lua_touserdata(L, lua_upvalueindex(1)); @@ -166,6 +170,8 @@ static int lua_GetKeyState(lua_State *L) return 1; } +/***********************************************/ + static int lua_GetIniValue(lua_State *L) { ptrT path(mir_utf8decodeT(luaL_checkstring(L, 1))); @@ -221,6 +227,8 @@ static int lua_DeleteIniValue(lua_State *L) return 1; } +/***********************************************/ + static int lua_GetRegValue(lua_State *L) { HKEY hRootKey = (HKEY)lua_touserdata(L, 1); @@ -360,6 +368,8 @@ static int lua_DeleteRegValue(lua_State *L) return 1; } +/***********************************************/ + static luaL_Reg winApi[] = { { "MessageBox", lua_MessageBox }, diff --git a/plugins/MirLua/src/mlua.cpp b/plugins/MirLua/src/mlua.cpp index 50ea5070f8..7256c2b6df 100644 --- a/plugins/MirLua/src/mlua.cpp +++ b/plugins/MirLua/src/mlua.cpp @@ -24,11 +24,11 @@ void CMLua::SetPaths() lua_getglobal(L, "package"); FoldersGetCustomPathT(g_hScriptsFolder, path, _countof(path), VARST(MIRLUA_CPATHT)); - lua_pushfstring(L, "%s\\?.dll", ptrA(mir_utf8encodeT(path))); + lua_pushstring(L, ptrA(mir_utf8encodeT(path))); lua_setfield(L, -2, "cpath"); FoldersGetCustomPathT(g_hScriptsFolder, path, _countof(path), VARST(MIRLUA_PATHT)); - lua_pushfstring(L, "%s\\?.lua", ptrA(mir_utf8encodeT(path))); + lua_pushstring(L, ptrA(mir_utf8encodeT(path))); lua_setfield(L, -2, "path"); lua_pop(L, 1); diff --git a/plugins/MirLua/src/mlua_metatable.h b/plugins/MirLua/src/mlua_metatable.h index df42f2dca9..048c4f425c 100644 --- a/plugins/MirLua/src/mlua_metatable.h +++ b/plugins/MirLua/src/mlua_metatable.h @@ -19,6 +19,8 @@ template class MT { private: + lua_State *L; + static const char *name; static std::map fields; @@ -30,25 +32,29 @@ private: return res; } - static int lua__new(lua_State *L) + static void Init(lua_State *L, T **obj) + { + luaL_checktype(L, 1, LUA_TLIGHTUSERDATA); + T *udata = (T*)lua_touserdata(L, 1); + memcpy(*obj, udata, sizeof(T)); + } + + static void Free(T **obj) { - T *obj = NULL; - T *udata = NULL; + *obj = NULL; + } - int type = lua_type(L, 1); - switch (type) + static int lua__new(lua_State *L) + { + T *udata = (T*)lua_newuserdata(L, sizeof(T)); + memset(udata, 0, sizeof(T)); + Init(L, &udata); + if (udata == NULL) { - case LUA_TLIGHTUSERDATA: - obj = (T*)MT::Load(L); - if (obj == NULL) break; - //case LUA_TNONE: - udata = (T*)lua_newuserdata(L, sizeof(T)); - memcpy(udata, obj, sizeof(T)); - case LUA_TUSERDATA: - luaL_setmetatable(L, MT::name); + lua_pushnil(L); return 1; } - lua_pushnil(L); + luaL_setmetatable(L, MT::name); return 1; } @@ -106,11 +112,8 @@ private: return 0; } - static T* Load(lua_State *L) { return (T*)lua_touserdata(L, 1); } - static void Free(T **obj) { *obj = NULL; } - public: - MT(lua_State *L, const char *tname) + MT(lua_State *L, const char *tname) : L(L) { MT::name = tname; @@ -133,7 +136,16 @@ public: return *this; } - static void Set(lua_State *L, T* obj) + 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) { diff --git a/plugins/MirLua/src/mlua_utils.cpp b/plugins/MirLua/src/mlua_utils.cpp index c253ad244c..7ff2a32127 100644 --- a/plugins/MirLua/src/mlua_utils.cpp +++ b/plugins/MirLua/src/mlua_utils.cpp @@ -16,7 +16,7 @@ void ShowNotification(const char *caption, const char *message, int flags, MCONT return; } - ::MessageBoxA(NULL, message, caption, MB_OK | flags); + MessageBoxA(NULL, message, caption, MB_OK | flags); } void ReportError(const char *message) @@ -33,27 +33,9 @@ int luaM_atpanic(lua_State *L) return 0; } -int luaM_trace(lua_State *L) -{ - lua_getglobal(L, "debug"); - lua_getfield(L, -1, "traceback"); - lua_pushvalue(L, 1); - lua_pushinteger(L, 2); - lua_call(L, 2, 1); - - return 1; -} - int luaM_pcall(lua_State *L, int n, int r) { - int f = 0; - /*if (db_get_b(NULL, MODULE, "AddTaraceback", 0)) - { - f = -(n + 2); - lua_pushcfunction(L, luaM_trace); - lua_insert(L, f); - }*/ - int res = lua_pcall(L, n, r, f); + int res = lua_pcall(L, n, r, 0); if (res != LUA_OK) ReportError(lua_tostring(L, -1)); return res; -- cgit v1.2.3