From 1c6b8b1f06d26f3ac4db17d6eb203538ba358057 Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Mon, 13 Jun 2016 18:22:20 +0000 Subject: MirLua: break backward compatibility - removed BLOB metatable - m_database's blob now used simple lua table - removed deprecated functions from m_core - added PROTORECVEVENT metatable - added GCDEST metatable - version bump git-svn-id: http://svn.miranda-ng.org/main/trunk@16968 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/MirLua/src/m_chat.cpp | 20 ++- plugins/MirLua/src/m_core.cpp | 19 +-- plugins/MirLua/src/m_database.cpp | 262 ++++++++++++++++++------------------ plugins/MirLua/src/m_protocols.cpp | 9 +- plugins/MirLua/src/mlua.cpp | 4 +- plugins/MirLua/src/mlua_metatable.h | 2 - plugins/MirLua/src/mlua_utils.cpp | 112 +-------------- plugins/MirLua/src/stdafx.h | 3 - plugins/MirLua/src/version.h | 4 +- 9 files changed, 174 insertions(+), 261 deletions(-) diff --git a/plugins/MirLua/src/m_chat.cpp b/plugins/MirLua/src/m_chat.cpp index da45fd93e4..dfe8b3c54d 100644 --- a/plugins/MirLua/src/m_chat.cpp +++ b/plugins/MirLua/src/m_chat.cpp @@ -5,14 +5,28 @@ static luaL_Reg chatApi[] = { NULL, NULL } }; +int MT::Index(lua_State *L, GCEVENT *gce) +{ + const char *key = lua_tostring(L, 2); + + if (mir_strcmp(key, "Destination") == 0) + MT::Set(L, gce->pDest); + else + lua_pushnil(L); + + return 1; +} + LUAMOD_API int luaopen_m_chat(lua_State *L) { luaL_newlib(L, chatApi); + MT(L, "GCDEST") + .Field(&GCDEST::pszModule, "Module", LUA_TSTRINGA) + .Field(&GCDEST::ptszID, "Id", LUA_TSTRINGW) + .Field(&GCDEST::iType, "Type", LUA_TINTEGER); + MT(L, "GCEVENT") - .Field([](GCEVENT *gce) { return (void*)gce->pDest->pszModule; }, "Module", LUA_TSTRINGA) - .Field([](GCEVENT *gce) { return (void*)gce->pDest->ptszID; }, "Id", LUA_TSTRINGW) - .Field([](GCEVENT *gce) { return (void*)gce->pDest->iType; }, "Type", LUA_TINTEGER) .Field(&GCEVENT::time, "Timestamp", LUA_TINTEGER) .Field(&GCEVENT::time, "IsMe", LUA_TINTEGER) .Field(&GCEVENT::time, "Flags", LUA_TINTEGER) diff --git a/plugins/MirLua/src/m_core.cpp b/plugins/MirLua/src/m_core.cpp index 286365d9b0..3e62d2f032 100644 --- a/plugins/MirLua/src/m_core.cpp +++ b/plugins/MirLua/src/m_core.cpp @@ -304,6 +304,8 @@ static int core_GetFullPath(lua_State *L) return 1; } +/***********************************************/ + struct core_ForkThreadParam { lua_State *L; @@ -364,18 +366,12 @@ static int core_TerminateThread(lua_State *L) return 1; } -int core_ptr2number(lua_State *L) -{ - ObsoleteMethod(L, "Use tonumber(x) instead"); - - luaL_checktype(L, 1, LUA_TLIGHTUSERDATA); - lua_pushnumber(L, (intptr_t)lua_touserdata(L, 1)); - return 1; -} +/***********************************************/ luaL_Reg coreApi[] = { { "CreateHookableEvent", core_CreateHookableEvent }, + // potentially unsefe for use in scripts //{ "DestroyHookableEvent", core_DestroyHookableEvent }, { "NotifyEventHooks", core_NotifyEventHooks }, @@ -399,15 +395,12 @@ luaL_Reg coreApi[] = { "Translate", core_Translate }, { "Parse", core_Parse }, - { "ReplaceVariables", core_Parse }, + + { "GetFullPath", core_GetFullPath }, { "ForkThread", core_ForkThread }, { "TerminateThread", core_TerminateThread }, - { "PointerToNumber", core_ptr2number }, - - { "GetFullPath", core_GetFullPath }, - { "Version", NULL }, { "NULL", NULL }, diff --git a/plugins/MirLua/src/m_database.cpp b/plugins/MirLua/src/m_database.cpp index b9dc4cb148..f74c869ae7 100644 --- a/plugins/MirLua/src/m_database.cpp +++ b/plugins/MirLua/src/m_database.cpp @@ -237,7 +237,6 @@ void MakeDbEvent(lua_State *L, DBEVENTINFO &dbei) { dbei.cbSize = sizeof(dbei); - lua_getfield(L, -1, "Module"); dbei.szModule = mir_strdup(lua_tostring(L, -1)); lua_pop(L, 1); @@ -254,12 +253,18 @@ void MakeDbEvent(lua_State *L, DBEVENTINFO &dbei) dbei.flags = lua_tointeger(L, -1); lua_pop(L, 1); - lua_getfield(L, -1, "Length"); - dbei.cbBlob = lua_tonumber(L, -1); - lua_pop(L, 1); - lua_getfield(L, -1, "Blob"); - dbei.pBlob = ((BLOB*)lua_touserdata(L, -1))->pBlobData; + if (lua_istable(L, -1)) + { + dbei.cbBlob = lua_rawlen(L, 4); + dbei.pBlob = (BYTE*)mir_calloc(dbei.cbBlob); + for (DWORD i = 0; i < dbei.cbBlob; i++) + { + lua_geti(L, 4, i + 1); + dbei.pBlob[i] = lua_tointeger(L, -1); + lua_pop(L, 1); + } + } lua_pop(L, 1); } @@ -269,81 +274,69 @@ static int db_AddEvent(lua_State *L) DBEVENTINFO dbei; MakeDbEvent(L, dbei); - lua_pushnumber(L, db_event_add(hContact, &dbei)); + MEVENT hDbEvent = db_event_add(hContact, &dbei); + + if (hDbEvent) + lua_pushnumber(L, hDbEvent); + else + lua_pushnil(L); + return 1; } - /***********************************************/ -static int db_GetSetting(lua_State *L) +static int ModulesEnumProc(const char *szModuleName, DWORD, LPARAM lParam) { - MCONTACT hContact = lua_tointeger(L, 1); - LPCSTR szModule = luaL_checkstring(L, 2); - LPCSTR szSetting = luaL_checkstring(L, 3); - - DBVARIANT dbv; - if (db_get(hContact, szModule, szSetting, &dbv)) + if (szModuleName) { - lua_pushvalue(L, 4); - return 1; + LIST* p = (LIST*)lParam; + p->insert(mir_strdup(szModuleName)); } - switch (dbv.type) + return 0; +} + +static int db_ModulesIterator(lua_State *L) +{ + int i = lua_tointeger(L, lua_upvalueindex(1)); + LIST ¶m = *(LIST*)lua_touserdata(L, lua_upvalueindex(2)); + + if (i < param.getCount()) { - case DBVT_BYTE: - lua_pushinteger(L, dbv.bVal); - break; - case DBVT_WORD: - lua_pushinteger(L, dbv.wVal); - break; - case DBVT_DWORD: - lua_pushnumber(L, dbv.dVal); - break; - case DBVT_ASCIIZ: - lua_pushstring(L, ptrA(mir_utf8encode(dbv.pszVal))); - break; - case DBVT_UTF8: - lua_pushstring(L, dbv.pszVal); - break; - case DBVT_WCHAR: - lua_pushstring(L, ptrA(mir_utf8encodeW(dbv.pwszVal))); - break; - case DBVT_BLOB: - { - lua_getglobal(L, MT_BLOB); - lua_pushlightuserdata(L, dbv.pbVal); - lua_pushnumber(L, dbv.cpbVal); - luaM_pcall(L, 2, 1); - } - break; - default: - db_free(&dbv); - lua_pushvalue(L, 4); - return 1; + lua_pushinteger(L, (i + 1)); + lua_replace(L, lua_upvalueindex(1)); + lua_pushstring(L, ptrA(mir_utf8encode(param[i]))); + mir_free(param[i]); + } + else + { + lua_pushnil(L); + delete ¶m; } - - db_free(&dbv); return 1; } -typedef struct +static int db_Modules(lua_State *L) { - int count; - char **pszSettingName; + LIST *param = new LIST(5, PtrKeySortT); + + CallService(MS_DB_MODULES_ENUM, (WPARAM)param, (LPARAM)ModulesEnumProc); + + lua_pushinteger(L, 0); + lua_pushlightuserdata(L, param); + lua_pushcclosure(L, db_ModulesIterator, 2); + + return 1; } -enumDBSettingsParam; static int SettingsEnumProc(const char* szSetting, LPARAM lParam) { if (szSetting) { - enumDBSettingsParam* p = (enumDBSettingsParam*)lParam; - - p->count++; - p->pszSettingName = (char**)mir_realloc(p->pszSettingName, p->count * sizeof(char*)); - p->pszSettingName[p->count - 1] = mir_strdup(szSetting); + LIST* p = (LIST*)lParam; + p->insert(mir_strdup(szSetting)); } return 0; } @@ -351,33 +344,41 @@ static int SettingsEnumProc(const char* szSetting, LPARAM lParam) static int db_SettingIterator(lua_State *L) { int i = lua_tointeger(L, lua_upvalueindex(1)); - enumDBSettingsParam* param = (enumDBSettingsParam*)lua_touserdata(L, lua_upvalueindex(2)); + LIST ¶m = *(LIST*)lua_touserdata(L, lua_upvalueindex(2)); - if (i < param->count) + if (i < param.getCount()) { lua_pushinteger(L, (i + 1)); lua_replace(L, lua_upvalueindex(1)); - lua_pushstring(L, ptrA(mir_utf8encode(param->pszSettingName[i]))); - mir_free(param->pszSettingName[i]); + lua_pushstring(L, ptrA(mir_utf8encode(param[i]))); + mir_free(param[i]); } else { lua_pushnil(L); - mir_free(param->pszSettingName); - mir_free(param); + delete ¶m; } return 1; } +static int db_DeleteModule(lua_State *L) +{ + MCONTACT hContact = lua_tointeger(L, 1); + LPCSTR szModule = luaL_checkstring(L, 2); + + INT_PTR res = CallService(MS_DB_MODULE_DELETE, hContact, (LPARAM)szModule); + lua_pushboolean(L, !res); + + return 1; +} + static int db_Settings(lua_State *L) { MCONTACT hContact = lua_tointeger(L, 1); const char* szModule = luaL_checkstring(L, 2); - enumDBSettingsParam* param = (enumDBSettingsParam*)mir_alloc(sizeof(enumDBSettingsParam)); - param->count = 0; - param->pszSettingName = NULL; + LIST *param = new LIST(5, PtrKeySortT); DBCONTACTENUMSETTINGS dbces = { 0 }; dbces.pfnEnumProc = SettingsEnumProc; @@ -393,45 +394,56 @@ static int db_Settings(lua_State *L) return 1; } -static int ModulesEnumProc(const char *szModuleName, DWORD, LPARAM lParam) +static int db_GetSetting(lua_State *L) { - if (szModuleName) + MCONTACT hContact = lua_tointeger(L, 1); + LPCSTR szModule = luaL_checkstring(L, 2); + LPCSTR szSetting = luaL_checkstring(L, 3); + + DBVARIANT dbv; + if (db_get(hContact, szModule, szSetting, &dbv)) { - LIST* p = (LIST*)lParam; - p->insert(mir_strdup(szModuleName)); + lua_pushvalue(L, 4); + return 1; } - return 0; -} - -static int db_ModulesIterator(lua_State *L) -{ - int i = lua_tointeger(L, lua_upvalueindex(1)); - LIST ¶m = *(LIST*)lua_touserdata(L, lua_upvalueindex(2)); - if (i < param.getCount()) + switch (dbv.type) { - lua_pushinteger(L, (i + 1)); - lua_replace(L, lua_upvalueindex(1)); - lua_pushstring(L, ptrA(mir_utf8encode(param[i]))); - mir_free(param[i]); - } - else + case DBVT_BYTE: + lua_pushinteger(L, dbv.bVal); + break; + case DBVT_WORD: + lua_pushinteger(L, dbv.wVal); + break; + case DBVT_DWORD: + lua_pushnumber(L, dbv.dVal); + break; + case DBVT_ASCIIZ: + lua_pushstring(L, ptrA(mir_utf8encode(dbv.pszVal))); + break; + case DBVT_UTF8: + lua_pushstring(L, dbv.pszVal); + break; + case DBVT_WCHAR: + lua_pushstring(L, ptrA(mir_utf8encodeW(dbv.pwszVal))); + break; + case DBVT_BLOB: { - lua_pushnil(L); - delete ¶m; + lua_createtable(L, dbv.cpbVal, 0); + for (int i = 0; i < dbv.cpbVal; i++) + { + lua_pushinteger(L, dbv.pbVal[i]); + lua_rawseti(L, -2, i + 1); + } + } + break; + default: + db_free(&dbv); + lua_pushvalue(L, 4); + return 1; } - return 1; -} - -static int db_Modules(lua_State *L) -{ - LIST *param = new LIST(5, PtrKeySortT); - - CallService(MS_DB_MODULES_ENUM, (WPARAM)param, (LPARAM)ModulesEnumProc); - lua_pushinteger(L, 0); - lua_pushlightuserdata(L, param); - lua_pushcclosure(L, db_ModulesIterator, 2); + db_free(&dbv); return 1; } @@ -457,7 +469,7 @@ static int db_WriteSetting(lua_State *L) case LUA_TSTRING: dbv.type = DBVT_UTF8; break; - case LUA_TUSERDATA: + case LUA_TTABLE: dbv.type = DBVT_BLOB; break; default: @@ -492,9 +504,14 @@ static int db_WriteSetting(lua_State *L) break; case DBVT_BLOB: { - BLOB *blob = (BLOB*)luaL_checkudata(L, 4, MT_BLOB); - dbv.cpbVal = blob->cbSize; - dbv.pbVal = blob->pBlobData; + dbv.cpbVal = (WORD)lua_rawlen(L, 4); + dbv.pbVal = (BYTE*)mir_calloc(dbv.cpbVal); + for (int i = 0; i < dbv.cpbVal; i++) + { + lua_geti(L, 4, i + 1); + dbv.pbVal[i] = lua_tointeger(L, -1); + lua_pop(L, 1); + } } break; default: @@ -520,17 +537,6 @@ static int db_DeleteSetting(lua_State *L) return 1; } -static int db_DeleteModule(lua_State *L) -{ - MCONTACT hContact = lua_tointeger(L, 1); - LPCSTR szModule = luaL_checkstring(L, 2); - - INT_PTR res = ::CallService(MS_DB_MODULE_DELETE, hContact, (LPARAM)szModule); - lua_pushboolean(L, !res); - - return 1; -} - /***********************************************/ static luaL_Reg databaseApi[] = @@ -550,15 +556,15 @@ static luaL_Reg databaseApi[] = { "EventsFromEnd", db_EventsFromEnd }, { "AddEvent", db_AddEvent }, - { "WriteSetting", db_WriteSetting }, - { "SetSetting", db_WriteSetting }, - - { "GetSetting", db_GetSetting }, { "Settings", db_Settings }, + { "Modules", db_Modules }, - { "DeleteSetting", db_DeleteSetting }, { "DeleteModule", db_DeleteModule }, - { "Modules", db_Modules }, + + { "GetSetting", db_GetSetting }, + { "WriteSetting", db_WriteSetting }, + { "SetSetting", db_WriteSetting }, + { "DeleteSetting", db_DeleteSetting }, { "DBVT_BYTE", NULL }, { "DBVT_WORD", NULL }, @@ -578,11 +584,7 @@ int MT::Index(lua_State *L, DBCONTACTWRITESETTING *dbcw) { const char *key = luaL_checkstring(L, 2); - if (mir_strcmpi(key, "Module") == 0) - lua_pushstring(L, dbcw->szModule); - else if (mir_strcmpi(key, "Setting") == 0) - lua_pushstring(L, dbcw->szSetting); - else if (mir_strcmpi(key, "Value") == 0) + if (mir_strcmpi(key, "Value") == 0) { switch (dbcw->value.type) { @@ -606,10 +608,12 @@ int MT::Index(lua_State *L, DBCONTACTWRITESETTING *dbcw) break; case DBVT_BLOB: { - lua_getglobal(L, MT_BLOB); - lua_pushlightuserdata(L, dbcw->value.pbVal); - lua_pushnumber(L, dbcw->value.cpbVal); - luaM_pcall(L, 2, 1); + lua_createtable(L, dbcw->value.cpbVal, 0); + for (int i = 0; i < dbcw->value.cpbVal; i++) + { + lua_pushinteger(L, dbcw->value.pbVal[i]); + lua_rawseti(L, -2, i + 1); + } } break; default: diff --git a/plugins/MirLua/src/m_protocols.cpp b/plugins/MirLua/src/m_protocols.cpp index 493978283c..21ac0683a5 100644 --- a/plugins/MirLua/src/m_protocols.cpp +++ b/plugins/MirLua/src/m_protocols.cpp @@ -180,7 +180,14 @@ LUAMOD_API int luaopen_m_protocols(lua_State *L) MT(L, "CCSDATA") .Field(&CCSDATA::hContact, "hContact", LUA_TINTEGER) - .Field([](CCSDATA *ccs) { return ((PROTORECVEVENT*)ccs->lParam)->szMessage; }, "Message", LUA_TSTRING); + .Field(&CCSDATA::szProtoService, "Service", LUA_TSTRINGA) + .Field([](CCSDATA *ccd) { return (void*)ccd->wParam; }, "wParam", LUA_TLIGHTUSERDATA) + .Field([](CCSDATA *ccd) { return (void*)ccd->lParam; }, "lParam", LUA_TLIGHTUSERDATA); + + MT(L, "PROTORECVEVENT") + .Field(&PROTORECVEVENT::timestamp, "Timestamp", LUA_TINTEGER) + .Field(&PROTORECVEVENT::flags, "Flags", LUA_TINTEGER) + .Field(&PROTORECVEVENT::szMessage, "Message", LUA_TSTRING); return 1; } diff --git a/plugins/MirLua/src/mlua.cpp b/plugins/MirLua/src/mlua.cpp index d1f7811bdc..2c67f32f3e 100644 --- a/plugins/MirLua/src/mlua.cpp +++ b/plugins/MirLua/src/mlua.cpp @@ -67,8 +67,6 @@ 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"); @@ -83,7 +81,7 @@ void CMLua::Unload() while (int last = Scripts.getCount()) { - CMLuaScript* script = g_mLua->Scripts[last - 1]; + CMLuaScript *script = g_mLua->Scripts[last - 1]; Scripts.remove(script); script->Unload(); delete script; diff --git a/plugins/MirLua/src/mlua_metatable.h b/plugins/MirLua/src/mlua_metatable.h index fe1e2eba10..1b28285e96 100644 --- a/plugins/MirLua/src/mlua_metatable.h +++ b/plugins/MirLua/src/mlua_metatable.h @@ -3,8 +3,6 @@ #include -#define LFUNC(T, L) std::function(L) - #define LUA_TINTEGER LUA_NUMTAGS + 1 #define LUA_TSTRINGA LUA_NUMTAGS + 2 #define LUA_TSTRINGW LUA_NUMTAGS + 3 diff --git a/plugins/MirLua/src/mlua_utils.cpp b/plugins/MirLua/src/mlua_utils.cpp index d3002cb04c..f02ca6e147 100644 --- a/plugins/MirLua/src/mlua_utils.cpp +++ b/plugins/MirLua/src/mlua_utils.cpp @@ -166,20 +166,15 @@ int luaM_tonumber(lua_State *L) { lua_Integer value = (lua_Integer)lua_touserdata(L, 1); lua_pushinteger(L, value); + return 1; } - else if (lua_gettop(L) == 2) - { - lua_getglobal(L, "_tonumber"); - lua_pushvalue(L, 1); + + int n = lua_gettop(L); + lua_getglobal(L, "_tonumber"); + lua_pushvalue(L, 1); + if (n == 2) lua_pushvalue(L, 2); - luaM_pcall(L, 2, 1); - } - else - { - lua_getglobal(L, "_tonumber"); - lua_pushvalue(L, 1); - luaM_pcall(L, 1, 1); - } + luaM_pcall(L, n, 1); return 1; } @@ -251,97 +246,4 @@ bool luaM_toboolean(lua_State *L, int idx) if (lua_isnumber(L, idx)) return lua_tonumber(L, idx) > 0; return lua_toboolean(L, idx) > 0; -} - -/***********************************************/ - -static int blob_new(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 blob_call(lua_State *L) -{ - int nargs = lua_gettop(L); - lua_pushcfunction(L, blob_new); - for (int i = 2; i <= nargs; i++) - lua_pushvalue(L, i); - luaM_pcall(L, nargs - 1, 1); - - 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[] = -{ - { "__call", blob_call }, - { "__index", blob__index }, - { "__newindex", blob__newindex }, - { "__len", blob__len }, - { "__gc", blob__gc }, - - (NULL, NULL) -}; - -int luaopen_m_utils(lua_State *L) -{ - luaL_newmetatable(L, MT_BLOB); - luaL_setfuncs(L, blobApi, 0); - lua_pop(L, 1); - - lua_createtable(L, 0, 1); - lua_pushcfunction(L, blob_new); - lua_setfield(L, -2, "new"); - lua_pushvalue(L, -1); - lua_setglobal(L, MT_BLOB); - luaL_setmetatable(L, MT_BLOB); - 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 461dbdfc7a..3ced6ea10a 100644 --- a/plugins/MirLua/src/stdafx.h +++ b/plugins/MirLua/src/stdafx.h @@ -96,9 +96,6 @@ 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, ...); diff --git a/plugins/MirLua/src/version.h b/plugins/MirLua/src/version.h index 0204e9a8e9..6c296d6709 100644 --- a/plugins/MirLua/src/version.h +++ b/plugins/MirLua/src/version.h @@ -1,7 +1,7 @@ #define __MAJOR_VERSION 0 #define __MINOR_VERSION 11 -#define __RELEASE_NUM 7 -#define __BUILD_NUM 2 +#define __RELEASE_NUM 8 +#define __BUILD_NUM 0 #include -- cgit v1.2.3