From ee37779cc2bdb12315b962af3df2501fdcc68089 Mon Sep 17 00:00:00 2001 From: Vadim Dashevskiy Date: Fri, 24 Jun 2016 17:43:57 +0000 Subject: temporary revert git-svn-id: http://svn.miranda-ng.org/main/trunk@17032 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- .../MirLua/Modules/m_msg_buttonsbar/src/main.cpp | 23 +++--- plugins/MirLua/src/m_core.cpp | 94 +++++++++++++++++++--- plugins/MirLua/src/mlua.cpp | 34 +++++++- plugins/MirLua/src/mlua.h | 12 +-- plugins/MirLua/src/mlua_options.cpp | 4 +- plugins/MirLua/src/mlua_script.cpp | 8 +- plugins/MirLua/src/mlua_script.h | 8 +- plugins/MirLua/src/mlua_script_loader.cpp | 4 +- 8 files changed, 146 insertions(+), 41 deletions(-) (limited to 'plugins') diff --git a/plugins/MirLua/Modules/m_msg_buttonsbar/src/main.cpp b/plugins/MirLua/Modules/m_msg_buttonsbar/src/main.cpp index 004eee6b87..de2fbdc021 100644 --- a/plugins/MirLua/Modules/m_msg_buttonsbar/src/main.cpp +++ b/plugins/MirLua/Modules/m_msg_buttonsbar/src/main.cpp @@ -36,14 +36,14 @@ static int lua_AddButton(lua_State *L) { if (lua_type(L, 1) != LUA_TTABLE) { - lua_pushboolean(L, false); + lua_pushlightuserdata(L, 0); return 1; } - BBButton *bbb = MakeBBButton(L); + BBButton* bbb = MakeBBButton(L); INT_PTR res = CallService(MS_BB_ADDBUTTON, 0, (LPARAM)bbb); - lua_pushboolean(L, !res); + lua_pushinteger(L, res); return 1; } @@ -52,14 +52,14 @@ static int lua_ModifyButton(lua_State *L) { if (lua_type(L, 1) != LUA_TTABLE) { - lua_pushboolean(L, false); + lua_pushlightuserdata(L, 0); return 1; } - BBButton *bbb = MakeBBButton(L); + BBButton* bbb = MakeBBButton(L); INT_PTR res = CallService(MS_BB_MODIFYBUTTON, 0, (LPARAM)bbb); - lua_pushboolean(L, !res); + lua_pushinteger(L, res); mir_free(bbb->pszModuleName); mir_free(bbb->ptszTooltip); @@ -70,15 +70,14 @@ static int lua_ModifyButton(lua_State *L) static int lua_RemoveButton(lua_State *L) { - ptrA module(mir_utf8decodeA(luaL_checkstring(L, 1))); - int buttonId = luaL_checkinteger(L, 2); + ptrA szModuleName(mir_utf8decodeA(luaL_checkstring(L, 1))); BBButton mbb = { sizeof(BBButton) }; - mbb.pszModuleName = module; - mbb.dwButtonID = buttonId; + mbb.pszModuleName = szModuleName; + mbb.dwButtonID = luaL_checkinteger(L, 2); - INT_PTR res = CallService(MS_BB_REMOVEBUTTON, 0, (LPARAM)&mbb); - lua_pushboolean(L, !res); + INT_PTR res = ::CallService(MS_BB_REMOVEBUTTON, 0, (LPARAM)&mbb); + lua_pushinteger(L, res); return 1; } diff --git a/plugins/MirLua/src/m_core.cpp b/plugins/MirLua/src/m_core.cpp index 0d2d07eb4d..4c2b6e7a38 100644 --- a/plugins/MirLua/src/m_core.cpp +++ b/plugins/MirLua/src/m_core.cpp @@ -16,9 +16,9 @@ static int core_CreateHookableEvent(lua_State *L) return 1; } -int HookEventObjParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param) +int HookEventLuaStateParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param) { - lua_State *L = lua_newthread(*(CMLua*)obj); + lua_State *L = (lua_State*)obj; int ref = param; lua_rawgeti(L, LUA_REGISTRYINDEX, ref); @@ -38,6 +38,28 @@ int HookEventObjParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param) return lua_tointeger(L, -1); } +int HookEventScriptParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param) +{ + CMLuaScript *script = (CMLuaScript*)obj; + + int ref = param; + lua_rawgeti(script->L, LUA_REGISTRYINDEX, ref); + + if (wParam) + lua_pushlightuserdata(script->L, (void*)wParam); + else + lua_pushnil(script->L); + + if (lParam) + lua_pushlightuserdata(script->L, (void*)lParam); + else + lua_pushnil(script->L); + + luaM_pcall(script->L, 2, 1); + + return lua_tointeger(script->L, -1); +} + static int core_HookEvent(lua_State *L) { const char *name = luaL_checkstring(L, 1); @@ -46,15 +68,24 @@ static int core_HookEvent(lua_State *L) lua_pushvalue(L, 2); int ref = luaL_ref(L, LUA_REGISTRYINDEX); - HANDLE hEvent = HookEventObjParam(name, HookEventObjParam, g_mLua, ref); - if (hEvent == NULL) + HANDLE res = NULL; + CMLuaScript *script = CMLuaScript::GetScriptFromEnviroment(L); + if (script) + res = HookEventObjParam(name, HookEventScriptParam, script, ref); + else + res = HookEventObjParam(name, HookEventLuaStateParam, L, ref); + if (res == NULL) { luaL_unref(L, LUA_REGISTRYINDEX, ref); lua_pushnil(L); return 1; } - lua_pushlightuserdata(L, hEvent); + + CMLua::HookRefs.insert(new HandleRefParam(L, res, ref)); + + lua_pushlightuserdata(L, res); + return 1; } @@ -64,6 +95,16 @@ static int core_UnhookEvent(lua_State *L) HANDLE hEvent = lua_touserdata(L, 1); int res = UnhookEvent(hEvent); + if (!res) + { + HandleRefParam *param = (HandleRefParam*)CMLua::HookRefs.find(&hEvent); + if (param != NULL) + { + luaL_unref(param->L, LUA_REGISTRYINDEX, param->ref); + CMLua::HookRefs.remove(param); + delete param; + } + } lua_pushboolean(L, !res); return 1; @@ -95,9 +136,9 @@ static int core_DestroyHookableEvent(lua_State *L) /***********************************************/ -INT_PTR CreateServiceFunctionObjParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param) +INT_PTR CreateServiceFunctionLuaStateParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param) { - lua_State *L = lua_newthread(*(CMLua*)obj); + lua_State *L = (lua_State*)obj; int ref = param; lua_rawgeti(L, LUA_REGISTRYINDEX, ref); @@ -112,6 +153,23 @@ INT_PTR CreateServiceFunctionObjParam(void *obj, WPARAM wParam, LPARAM lParam, L return res; } +INT_PTR CreateServiceFunctionScriptParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param) +{ + CMLuaScript *script = (CMLuaScript*)obj; + + int ref = param; + lua_rawgeti(script->L, LUA_REGISTRYINDEX, ref); + + lua_pushlightuserdata(script->L, (void*)wParam); + lua_pushlightuserdata(script->L, (void*)lParam); + luaM_pcall(script->L, 2, 1); + + INT_PTR res = lua_tointeger(script->L, 1); + lua_pushinteger(script->L, res); + + return res; +} + static int core_CreateServiceFunction(lua_State *L) { const char *name = luaL_checkstring(L, 1); @@ -120,14 +178,22 @@ static int core_CreateServiceFunction(lua_State *L) lua_pushvalue(L, 2); int ref = luaL_ref(L, LUA_REGISTRYINDEX); - HANDLE hService = CreateServiceFunctionObjParam(name, CreateServiceFunctionObjParam, g_mLua, ref); - if (!hService) + HANDLE res = NULL; + CMLuaScript *script = CMLuaScript::GetScriptFromEnviroment(L); + if (script) + res = CreateServiceFunctionObjParam(name, CreateServiceFunctionScriptParam, script, ref); + else + res = CreateServiceFunctionObjParam(name, CreateServiceFunctionLuaStateParam, L, ref); + if (!res) { luaL_unref(L, LUA_REGISTRYINDEX, ref); lua_pushnil(L); return 1; } - lua_pushlightuserdata(L, hService); + + CMLua::ServiceRefs.insert(new HandleRefParam(L, res, ref)); + + lua_pushlightuserdata(L, res); return 1; } @@ -159,6 +225,14 @@ static int core_DestroyServiceFunction(lua_State *L) luaL_checktype(L, 1, LUA_TLIGHTUSERDATA); HANDLE hService = lua_touserdata(L, 1); + HandleRefParam *param = (HandleRefParam*)CMLua::ServiceRefs.find(&hService); + if (param != NULL) + { + luaL_unref(param->L, LUA_REGISTRYINDEX, param->ref); + CMLua::ServiceRefs.remove(param); + delete param; + } + DestroyServiceFunction(hService); return 0; diff --git a/plugins/MirLua/src/mlua.cpp b/plugins/MirLua/src/mlua.cpp index bea7645bbf..2c67f32f3e 100644 --- a/plugins/MirLua/src/mlua.cpp +++ b/plugins/MirLua/src/mlua.cpp @@ -2,6 +2,9 @@ int hMLuaLangpack; +LIST CMLua::HookRefs(1, HandleKeySortT); +LIST CMLua::ServiceRefs(1, HandleKeySortT); + static int CompareScripts(const CMLuaScript* p1, const CMLuaScript* p2) { return mir_strcmpi(p1->GetModuleName(), p2->GetModuleName()); @@ -80,7 +83,7 @@ void CMLua::Unload() { CMLuaScript *script = g_mLua->Scripts[last - 1]; Scripts.remove(script); - script->Unload(L); + script->Unload(); delete script; } @@ -89,8 +92,33 @@ void CMLua::Unload() KillModuleMenus(hMLuaLangpack); KillModuleHotkeys(hMLuaLangpack); - KillObjectEventHooks(this); - KillObjectServices(this); + KillObjectEventHooks(L); + KillObjectServices(L); lua_close(L); } + +void CMLua::KillLuaRefs() +{ + while (HookRefs.getCount()) + { + HandleRefParam *param = (HandleRefParam*)HookRefs[0]; + if (param != NULL) + { + luaL_unref(param->L, LUA_REGISTRYINDEX, param->ref); + HookRefs.remove(0); + delete param; + } + } + + while (ServiceRefs.getCount()) + { + HandleRefParam *param = (HandleRefParam*)ServiceRefs[0]; + if (param != NULL) + { + luaL_unref(param->L, LUA_REGISTRYINDEX, param->ref); + ServiceRefs.remove(0); + delete param; + } + } +} diff --git a/plugins/MirLua/src/mlua.h b/plugins/MirLua/src/mlua.h index 3c5e3dd769..c732d2c8a4 100644 --- a/plugins/MirLua/src/mlua.h +++ b/plugins/MirLua/src/mlua.h @@ -6,6 +6,8 @@ struct HandleRefParam HANDLE h; int ref; lua_State *L; + HandleRefParam(HANDLE h) : L(NULL), h(h), ref(0) { } + HandleRefParam(lua_State *L, HANDLE h, int ref = 0) : L(L), h(h), ref(ref) { } }; class CMLua @@ -15,17 +17,17 @@ private: void SetPaths(); + static void KillLuaRefs(); + public: + static LIST HookRefs; + static LIST ServiceRefs; + LIST Scripts; CMLua(); ~CMLua(); - operator lua_State*() const - { - return L; - } - void Load(); void Unload(); }; diff --git a/plugins/MirLua/src/mlua_options.cpp b/plugins/MirLua/src/mlua_options.cpp index e1c5d4fbeb..72bde7761f 100644 --- a/plugins/MirLua/src/mlua_options.cpp +++ b/plugins/MirLua/src/mlua_options.cpp @@ -131,8 +131,8 @@ void CMLuaOptions::OnScriptListClick(CCtrlListView::TEventInfo *evt) case 2: //m_scripts.DeleteItem(evt->nmlvia->iItem); - script->Unload((lua_State*)g_mLua); - script->Load((lua_State*)g_mLua); + script->Unload(); + script->Load(); lvi.mask = LVIF_IMAGE; lvi.iSubItem = 0; lvi.iImage = script->GetStatus() - 1; diff --git a/plugins/MirLua/src/mlua_script.cpp b/plugins/MirLua/src/mlua_script.cpp index 1d4a2938b0..d36f93b7a8 100644 --- a/plugins/MirLua/src/mlua_script.cpp +++ b/plugins/MirLua/src/mlua_script.cpp @@ -2,8 +2,8 @@ #define SCRIPT "Script" -CMLuaScript::CMLuaScript(const TCHAR *path) - : status(None), unloadRef(LUA_NOREF) +CMLuaScript::CMLuaScript(lua_State *L, const TCHAR *path) + : L(L), status(None), unloadRef(LUA_NOREF) { mir_tstrcpy(filePath, path); @@ -91,7 +91,7 @@ const CMLuaScript::Status CMLuaScript::GetStatus() const return status; } -bool CMLuaScript::Load(lua_State *L) +bool CMLuaScript::Load() { status = Failed; @@ -154,7 +154,7 @@ bool CMLuaScript::Load(lua_State *L) return true; } -void CMLuaScript::Unload(lua_State *L) +void CMLuaScript::Unload() { if (status == Loaded) { diff --git a/plugins/MirLua/src/mlua_script.h b/plugins/MirLua/src/mlua_script.h index 8cf7fc1e29..c466e16c20 100644 --- a/plugins/MirLua/src/mlua_script.h +++ b/plugins/MirLua/src/mlua_script.h @@ -4,6 +4,8 @@ class CMLuaScript { public: + lua_State *L; + enum Status { None, @@ -21,7 +23,7 @@ private: public: - CMLuaScript(const TCHAR *path); + CMLuaScript(lua_State *L, const TCHAR *path); ~CMLuaScript(); static CMLuaScript* GetScriptFromEnviroment(lua_State *L); @@ -37,8 +39,8 @@ public: const Status GetStatus() const; - bool Load(lua_State *L); - void Unload(lua_State *L); + bool Load(); + void Unload(); }; #endif //_LUA_SCRIPT_H_ diff --git a/plugins/MirLua/src/mlua_script_loader.cpp b/plugins/MirLua/src/mlua_script_loader.cpp index f68f00096a..ddecbad547 100644 --- a/plugins/MirLua/src/mlua_script_loader.cpp +++ b/plugins/MirLua/src/mlua_script_loader.cpp @@ -10,7 +10,7 @@ void CMLuaScriptLoader::LoadScript(const TCHAR *scriptDir, const TCHAR *file) mir_sntprintf(fullPath, _T("%s\\%s"), scriptDir, file); PathToRelativeT(fullPath, path); - CMLuaScript *script = new CMLuaScript(path); + CMLuaScript *script = new CMLuaScript(L, path); g_mLua->Scripts.insert(script); if (db_get_b(NULL, MODULE, _T2A(file), 1) == FALSE) @@ -19,7 +19,7 @@ void CMLuaScriptLoader::LoadScript(const TCHAR *scriptDir, const TCHAR *file) return; } - if (script->Load(L)) + if (script->Load()) Log(_T("%s:OK"), path); } -- cgit v1.2.3