From 1c96560f8bc136fa2930dd67f538f65c02a1c350 Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Sat, 4 Jun 2016 18:01:23 +0000 Subject: MirLua: - fixed DBEVENTINFO metatable - BLOB metatable temporary moved to utils - fixed tonumber function git-svn-id: http://svn.miranda-ng.org/main/trunk@16913 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/MirLua/src/m_database.cpp | 91 ++--------------------------------- plugins/MirLua/src/m_protocols.cpp | 4 +- plugins/MirLua/src/main.cpp | 7 ++- plugins/MirLua/src/mlua.cpp | 2 + plugins/MirLua/src/mlua_metatable.h | 5 +- plugins/MirLua/src/mlua_utils.cpp | 94 +++++++++++++++++++++++++++++++++---- plugins/MirLua/src/stdafx.h | 7 ++- 7 files changed, 102 insertions(+), 108 deletions(-) diff --git a/plugins/MirLua/src/m_database.cpp b/plugins/MirLua/src/m_database.cpp index 09e90633e5..b9482ed7f8 100644 --- a/plugins/MirLua/src/m_database.cpp +++ b/plugins/MirLua/src/m_database.cpp @@ -236,85 +236,6 @@ static int db_EventsFromEnd(lua_State *L) /***********************************************/ -#define MT_BLOB "BLOB" - -static int array_create(lua_State *L) -{ - BYTE *data = (BYTE*)lua_touserdata(L, 1); - size_t size = luaL_checkinteger(L, 2); - - BLOB *blob = (BLOB*)lua_newuserdata(L, sizeof(BLOB)); - blob->cbSize = size; - blob->pBlobData = (BYTE*)mir_calloc(size); - memcpy(blob->pBlobData, data, size); - luaL_setmetatable(L, MT_BLOB); - - return 1; -} - -static int array__index(lua_State *L) -{ - BLOB *blob = (BLOB*)luaL_checkudata(L, 1, MT_BLOB); - int i = luaL_checkinteger(L, 2); - - lua_pushinteger(L, (uint8_t)blob->pBlobData[i - 1]); - - return 1; -} - -static int array__newindex(lua_State *L) -{ - BLOB *blob = (BLOB*)luaL_checkudata(L, 1, MT_BLOB); - int i = luaL_checkinteger(L, 2); - - blob->pBlobData[i - 1] = (BYTE)luaL_checkinteger(L, 3); - - return 0; -} - -static int array__len(lua_State *L) -{ - BLOB *blob = (BLOB*)luaL_checkudata(L, 1, MT_BLOB); - - lua_pushinteger(L, blob->cbSize); - - return 1; -} - -static int array__tostring(lua_State *L) -{ - BLOB *blob = (BLOB*)luaL_checkudata(L, 1, MT_BLOB); - - char *res = (char*)alloca(blob->cbSize * 2 + 1); - bin2hex(blob->pBlobData, blob->cbSize, res); - - lua_pushstring(L, res); - - return 1; -} - -static int array__gc(lua_State *L) -{ - BLOB *blob = (BLOB*)luaL_checkudata(L, 1, MT_BLOB); - - mir_free(blob->pBlobData); - - return 0; -} - -static const struct luaL_Reg blobApi[] = -{ - { "__index", array__index }, - { "__newindex", array__newindex }, - { "__len", array__len }, - { "__tostring", array__tostring }, - { "__gc", array__gc }, - - (NULL, NULL) -}; - -/***********************************************/ - static int db_GetSetting(lua_State *L) { MCONTACT hContact = lua_tointeger(L, 1); @@ -350,7 +271,7 @@ static int db_GetSetting(lua_State *L) break; case DBVT_BLOB: { - lua_pushcfunction(L, array_create); + lua_getglobal(L, MT_BLOB); lua_pushlightuserdata(L, dbv.pbVal); lua_pushnumber(L, dbv.cpbVal); luaM_pcall(L, 2, 1); @@ -423,7 +344,7 @@ static int db_Settings(lua_State *L) dbces.szModule = szModule; dbces.ofsSettings = 0; dbces.lParam = (LPARAM)param; - ::CallService(MS_DB_CONTACT_ENUMSETTINGS, hContact, (LPARAM)&dbces); + CallService(MS_DB_CONTACT_ENUMSETTINGS, hContact, (LPARAM)&dbces); lua_pushinteger(L, 0); lua_pushlightuserdata(L, param); @@ -600,7 +521,7 @@ int MT::Index(lua_State *L, DBCONTACTWRITESETTING *dbcw) break; case DBVT_BLOB: { - lua_pushcfunction(L, array_create); + lua_getglobal(L, MT_BLOB); lua_pushlightuserdata(L, dbcw->value.pbVal); lua_pushnumber(L, dbcw->value.cpbVal); luaM_pcall(L, 2, 1); @@ -624,6 +545,7 @@ void MT::Init(lua_State *L, DBEVENTINFO **dbei) { MEVENT hDbEvent = luaL_checkinteger(L, 1); + *dbei = (DBEVENTINFO*)mir_calloc(sizeof(DBEVENTINFO)); (*dbei)->cbSize = sizeof(DBEVENTINFO); (*dbei)->cbBlob = db_event_getBlobSize((MEVENT)hDbEvent); (*dbei)->pBlob = (PBYTE)mir_calloc((*dbei)->cbBlob); @@ -633,6 +555,7 @@ void MT::Init(lua_State *L, DBEVENTINFO **dbei) void MT::Free(lua_State*, DBEVENTINFO **dbei) { mir_free((*dbei)->pBlob); + mir_free(*dbei); } /***********************************************/ @@ -654,10 +577,6 @@ LUAMOD_API int luaopen_m_database(lua_State *L) lua_pushnumber(L, DBVT_WCHAR); lua_setfield(L, -2, "DBVT_WCHAR"); - luaL_newmetatable(L, MT_BLOB); - luaL_setfuncs(L, blobApi, 0); - lua_pop(L, 1); - MT(L, MT_DBCONTACTWRITESETTING) .Field(&DBCONTACTWRITESETTING::szModule, "Module", LUA_TSTRINGA) .Field(&DBCONTACTWRITESETTING::szSetting, "Setting", LUA_TSTRINGA); diff --git a/plugins/MirLua/src/m_protocols.cpp b/plugins/MirLua/src/m_protocols.cpp index dd3db59833..9ea8f4b833 100644 --- a/plugins/MirLua/src/m_protocols.cpp +++ b/plugins/MirLua/src/m_protocols.cpp @@ -46,7 +46,7 @@ static int lua_ProtocolIterator(lua_State *L) static int lua_Protocols(lua_State *L) { int count; - PROTOCOLDESCRIPTOR** protos; + PROTOCOLDESCRIPTOR **protos; Proto_EnumProtocols(&count, &protos); lua_pushinteger(L, 0); @@ -116,7 +116,7 @@ static int lua_AccountIterator(lua_State *L) static int lua_Accounts(lua_State *L) { int count; - PROTOACCOUNT** accounts; + PROTOACCOUNT **accounts; Proto_EnumAccounts(&count, &accounts); lua_pushinteger(L, 0); diff --git a/plugins/MirLua/src/main.cpp b/plugins/MirLua/src/main.cpp index 973a681a15..49b50190fd 100644 --- a/plugins/MirLua/src/main.cpp +++ b/plugins/MirLua/src/main.cpp @@ -46,9 +46,6 @@ int OnModulesLoaded(WPARAM, LPARAM) HookEvent(ME_OPT_INITIALISE, CMLuaOptions::OnOptionsInit); - hRecvMessage = CreateHookableEvent(MODULE PSR_MESSAGE); - CreateProtoServiceFunction(MODULE, PSR_MESSAGE, FilterRecvMessage); - InitIcons(); g_mLua = new CMLua(); @@ -65,7 +62,7 @@ extern "C" int __declspec(dllexport) Load(void) NETLIBUSER nlu = { 0 }; nlu.cbSize = sizeof(nlu); - nlu.flags = NUF_OUTGOING | NUF_INCOMING | NUF_HTTPCONNS | NUF_UNICODE; + nlu.flags = NUF_NOOPTIONS | NUF_UNICODE; nlu.ptszDescriptiveName = _T(MODULE); nlu.szSettingsModule = MODULE; hNetlib = (HANDLE)CallService(MS_NETLIB_REGISTERUSER, 0, (LPARAM)&nlu); @@ -76,7 +73,9 @@ extern "C" int __declspec(dllexport) Load(void) pd.type = PROTOTYPE_FILTER; Proto_RegisterModule(&pd); + hRecvMessage = CreateHookableEvent(MODULE PSR_MESSAGE); CreateProtoServiceFunction(MODULE, PSR_MESSAGE, FilterRecvMessage); + /*CreateProtoServiceFunction(MODULE, PSR_AUTH, FilterRecvAuth); CreateProtoServiceFunction(MODULE, PSR_FILE, FilterRecvFile); CreateProtoServiceFunction(MODULE, PSR_URL, FilterRecvUrl); diff --git a/plugins/MirLua/src/mlua.cpp b/plugins/MirLua/src/mlua.cpp index 5f5c2080ce..d1f7811bdc 100644 --- a/plugins/MirLua/src/mlua.cpp +++ b/plugins/MirLua/src/mlua.cpp @@ -67,6 +67,8 @@ void CMLua::Load() lua_setfield(L, -2, "interpolate"); lua_pop(L, 3); + luaopen_m_utils(L); + lua_atpanic(L, luaM_atpanic); Log("Loading miranda modules"); diff --git a/plugins/MirLua/src/mlua_metatable.h b/plugins/MirLua/src/mlua_metatable.h index deb4c66cef..e67f54ee6d 100644 --- a/plugins/MirLua/src/mlua_metatable.h +++ b/plugins/MirLua/src/mlua_metatable.h @@ -60,8 +60,6 @@ private: 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)); *obj = (T*)lua_touserdata(L, 1); } @@ -71,7 +69,7 @@ private: return 1; } - static void Free(lua_State * /*L*/, T **obj) + static void Free(lua_State* /*L*/, T **obj) { *obj = NULL; } @@ -79,7 +77,6 @@ private: 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) { diff --git a/plugins/MirLua/src/mlua_utils.cpp b/plugins/MirLua/src/mlua_utils.cpp index 94e5e41434..e4ea3efd11 100644 --- a/plugins/MirLua/src/mlua_utils.cpp +++ b/plugins/MirLua/src/mlua_utils.cpp @@ -35,6 +35,19 @@ void ShowNotification(const char *caption, const char *message, int flags, MCONT MessageBoxA(NULL, message, caption, MB_OK | flags); } +void ObsoleteMethod(lua_State *L, const char *message) +{ + lua_Debug ar; + if (lua_getstack(L, 0, &ar) == 0 || lua_getinfo(L, "n", &ar) == 0) + return; + + char text[512]; + mir_snprintf(text, "%s is obsolete. %s", ar.name, message); + Log(text); + if (db_get_b(NULL, MODULE, "PopupOnObsolete", 0)) + ShowNotification(MODULE, text, MB_OK | MB_ICONWARNING, NULL); +} + void ReportError(const char *message) { Log(message); @@ -158,7 +171,7 @@ int luaM_tonumber(lua_State *L) { lua_getglobal(L, "_tonumber"); lua_pushvalue(L, 1); - lua_pushnumber(L, 2); + lua_pushvalue(L, 2); luaM_pcall(L, 2, 1); } @@ -234,15 +247,76 @@ bool luaM_toboolean(lua_State *L, int idx) return lua_toboolean(L, idx) > 0; } -void ObsoleteMethod(lua_State *L, const char *message) +/***********************************************/ + +static int blob_create(lua_State *L) { - lua_Debug ar; - if (lua_getstack(L, 0, &ar) == 0 || lua_getinfo(L, "n", &ar) == 0) - return; + BYTE *data = (BYTE*)lua_touserdata(L, 1); + size_t size = luaL_checkinteger(L, 2); - char text[512]; - mir_snprintf(text, "%s is obsolete. %s", ar.name, message); - Log(text); - if (db_get_b(NULL, MODULE, "PopupOnObsolete", 0)) - ShowNotification(MODULE, text, MB_OK | MB_ICONWARNING, NULL); + BLOB *blob = (BLOB*)lua_newuserdata(L, sizeof(BLOB)); + blob->cbSize = size; + blob->pBlobData = (BYTE*)mir_calloc(size); + memcpy(blob->pBlobData, data, size); + luaL_setmetatable(L, MT_BLOB); + + return 1; +} + +static int blob__index(lua_State *L) +{ + BLOB *blob = (BLOB*)luaL_checkudata(L, 1, MT_BLOB); + int i = luaL_checkinteger(L, 2); + + lua_pushinteger(L, (uint8_t)blob->pBlobData[i - 1]); + + return 1; +} + +static int blob__newindex(lua_State *L) +{ + BLOB *blob = (BLOB*)luaL_checkudata(L, 1, MT_BLOB); + int i = luaL_checkinteger(L, 2); + + blob->pBlobData[i - 1] = (BYTE)luaL_checkinteger(L, 3); + + return 0; +} + +static int blob__len(lua_State *L) +{ + BLOB *blob = (BLOB*)luaL_checkudata(L, 1, MT_BLOB); + + lua_pushinteger(L, blob->cbSize); + + return 1; +} + +static int blob__gc(lua_State *L) +{ + BLOB *blob = (BLOB*)luaL_checkudata(L, 1, MT_BLOB); + + mir_free(blob->pBlobData); + + return 0; +} + +static const struct luaL_Reg blobApi[] = +{ + { "__index", blob__index }, + { "__newindex", blob__newindex }, + { "__len", blob__len }, + { "__gc", blob__gc }, + + (NULL, NULL) +}; + +int luaopen_m_utils(lua_State *L) +{ + lua_register(L, MT_BLOB, blob_create); + luaL_newmetatable(L, MT_BLOB); + luaL_setfuncs(L, blobApi, 0); + lua_pop(L, 1); + + return 0; } \ No newline at end of file diff --git a/plugins/MirLua/src/stdafx.h b/plugins/MirLua/src/stdafx.h index d14e5bd116..461dbdfc7a 100644 --- a/plugins/MirLua/src/stdafx.h +++ b/plugins/MirLua/src/stdafx.h @@ -96,12 +96,17 @@ LUAMOD_API int (luaopen_m_sounds)(lua_State *L); /* utils */ +#define MT_BLOB "BLOB" +int (luaopen_m_utils)(lua_State *L); + extern HANDLE hNetlib; void Log(const char *format, ...); void Log(const wchar_t *format, ...); void ShowNotification(const char *caption, const char *message, int flags = 0, MCONTACT hContact = NULL); +void ObsoleteMethod(lua_State *L, const char *message); + int luaM_atpanic(lua_State *L); int luaM_pcall(lua_State *L, int n = 0, int r = 0); @@ -123,6 +128,4 @@ void InitIcons(); HICON GetIcon(int iconId); HANDLE GetIconHandle(int iconId); -void ObsoleteMethod(lua_State *L, const char *message); - #endif //_COMMON_H_ -- cgit v1.2.3