From 50b6a5ca2c5652fdf4e5fab69d9918c0f633cfb5 Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Thu, 23 Jun 2016 13:01:49 +0000 Subject: MirLua: reworked m_core and CMLuaScript git-svn-id: http://svn.miranda-ng.org/main/trunk@17023 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- .../MirLua/Modules/m_msg_buttonsbar/src/main.cpp | 23 +++--- plugins/MirLua/src/m_core.cpp | 95 +++++----------------- plugins/MirLua/src/mlua.cpp | 30 +------ 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, 48 insertions(+), 136 deletions(-) (limited to 'plugins/MirLua') diff --git a/plugins/MirLua/Modules/m_msg_buttonsbar/src/main.cpp b/plugins/MirLua/Modules/m_msg_buttonsbar/src/main.cpp index de2fbdc021..004eee6b87 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_pushlightuserdata(L, 0); + lua_pushboolean(L, false); return 1; } - BBButton* bbb = MakeBBButton(L); + BBButton *bbb = MakeBBButton(L); INT_PTR res = CallService(MS_BB_ADDBUTTON, 0, (LPARAM)bbb); - lua_pushinteger(L, res); + lua_pushboolean(L, !res); return 1; } @@ -52,14 +52,14 @@ static int lua_ModifyButton(lua_State *L) { if (lua_type(L, 1) != LUA_TTABLE) { - lua_pushlightuserdata(L, 0); + lua_pushboolean(L, false); return 1; } - BBButton* bbb = MakeBBButton(L); + BBButton *bbb = MakeBBButton(L); INT_PTR res = CallService(MS_BB_MODIFYBUTTON, 0, (LPARAM)bbb); - lua_pushinteger(L, res); + lua_pushboolean(L, !res); mir_free(bbb->pszModuleName); mir_free(bbb->ptszTooltip); @@ -70,14 +70,15 @@ static int lua_ModifyButton(lua_State *L) static int lua_RemoveButton(lua_State *L) { - ptrA szModuleName(mir_utf8decodeA(luaL_checkstring(L, 1))); + ptrA module(mir_utf8decodeA(luaL_checkstring(L, 1))); + int buttonId = luaL_checkinteger(L, 2); BBButton mbb = { sizeof(BBButton) }; - mbb.pszModuleName = szModuleName; - mbb.dwButtonID = luaL_checkinteger(L, 2); + mbb.pszModuleName = module; + mbb.dwButtonID = buttonId; - INT_PTR res = ::CallService(MS_BB_REMOVEBUTTON, 0, (LPARAM)&mbb); - lua_pushinteger(L, res); + INT_PTR res = CallService(MS_BB_REMOVEBUTTON, 0, (LPARAM)&mbb); + lua_pushboolean(L, !res); return 1; } diff --git a/plugins/MirLua/src/m_core.cpp b/plugins/MirLua/src/m_core.cpp index 4c2b6e7a38..8d388bf8c7 100644 --- a/plugins/MirLua/src/m_core.cpp +++ b/plugins/MirLua/src/m_core.cpp @@ -16,7 +16,7 @@ static int core_CreateHookableEvent(lua_State *L) return 1; } -int HookEventLuaStateParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param) +int HookEventObjParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param) { lua_State *L = (lua_State*)obj; @@ -38,28 +38,6 @@ int HookEventLuaStateParam(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); @@ -68,13 +46,10 @@ static int core_HookEvent(lua_State *L) lua_pushvalue(L, 2); int ref = luaL_ref(L, LUA_REGISTRYINDEX); - 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) + lua_State *T = lua_newthread(L); + + HANDLE hEvent = HookEventObjParam(name, HookEventObjParam, T, ref); + if (hEvent == NULL) { luaL_unref(L, LUA_REGISTRYINDEX, ref); lua_pushnil(L); @@ -82,9 +57,8 @@ static int core_HookEvent(lua_State *L) return 1; } - CMLua::HookRefs.insert(new HandleRefParam(L, res, ref)); - - lua_pushlightuserdata(L, res); + lua_rawsetp(L, LUA_REGISTRYINDEX, hEvent); + lua_pushlightuserdata(L, hEvent); return 1; } @@ -97,13 +71,8 @@ static int core_UnhookEvent(lua_State *L) 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_pushnil(L); + lua_rawsetp(L, LUA_REGISTRYINDEX, hEvent); } lua_pushboolean(L, !res); @@ -136,7 +105,7 @@ static int core_DestroyHookableEvent(lua_State *L) /***********************************************/ -INT_PTR CreateServiceFunctionLuaStateParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param) +INT_PTR CreateServiceFunctionObjParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param) { lua_State *L = (lua_State*)obj; @@ -153,23 +122,6 @@ INT_PTR CreateServiceFunctionLuaStateParam(void *obj, WPARAM wParam, LPARAM lPar 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); @@ -178,22 +130,18 @@ static int core_CreateServiceFunction(lua_State *L) lua_pushvalue(L, 2); int ref = luaL_ref(L, LUA_REGISTRYINDEX); - 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) + lua_State *T = lua_newthread(L); + + HANDLE hService = CreateServiceFunctionObjParam(name, CreateServiceFunctionObjParam, T, ref); + if (!hService) { luaL_unref(L, LUA_REGISTRYINDEX, ref); lua_pushnil(L); return 1; } - CMLua::ServiceRefs.insert(new HandleRefParam(L, res, ref)); - - lua_pushlightuserdata(L, res); + lua_rawsetp(L, LUA_REGISTRYINDEX, hService); + lua_pushlightuserdata(L, hService); return 1; } @@ -225,16 +173,11 @@ 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); + lua_pushnil(L); + lua_rawsetp(L, LUA_REGISTRYINDEX, hService); + return 0; } diff --git a/plugins/MirLua/src/mlua.cpp b/plugins/MirLua/src/mlua.cpp index 2c67f32f3e..afe49d0f31 100644 --- a/plugins/MirLua/src/mlua.cpp +++ b/plugins/MirLua/src/mlua.cpp @@ -2,9 +2,6 @@ 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()); @@ -83,7 +80,7 @@ void CMLua::Unload() { CMLuaScript *script = g_mLua->Scripts[last - 1]; Scripts.remove(script); - script->Unload(); + script->Unload(L); delete script; } @@ -97,28 +94,3 @@ void CMLua::Unload() 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 c732d2c8a4..3c5e3dd769 100644 --- a/plugins/MirLua/src/mlua.h +++ b/plugins/MirLua/src/mlua.h @@ -6,8 +6,6 @@ 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 @@ -17,17 +15,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 72bde7761f..e1c5d4fbeb 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(); - script->Load(); + script->Unload((lua_State*)g_mLua); + script->Load((lua_State*)g_mLua); 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 d36f93b7a8..1d4a2938b0 100644 --- a/plugins/MirLua/src/mlua_script.cpp +++ b/plugins/MirLua/src/mlua_script.cpp @@ -2,8 +2,8 @@ #define SCRIPT "Script" -CMLuaScript::CMLuaScript(lua_State *L, const TCHAR *path) - : L(L), status(None), unloadRef(LUA_NOREF) +CMLuaScript::CMLuaScript(const TCHAR *path) + : status(None), unloadRef(LUA_NOREF) { mir_tstrcpy(filePath, path); @@ -91,7 +91,7 @@ const CMLuaScript::Status CMLuaScript::GetStatus() const return status; } -bool CMLuaScript::Load() +bool CMLuaScript::Load(lua_State *L) { status = Failed; @@ -154,7 +154,7 @@ bool CMLuaScript::Load() return true; } -void CMLuaScript::Unload() +void CMLuaScript::Unload(lua_State *L) { if (status == Loaded) { diff --git a/plugins/MirLua/src/mlua_script.h b/plugins/MirLua/src/mlua_script.h index c466e16c20..8cf7fc1e29 100644 --- a/plugins/MirLua/src/mlua_script.h +++ b/plugins/MirLua/src/mlua_script.h @@ -4,8 +4,6 @@ class CMLuaScript { public: - lua_State *L; - enum Status { None, @@ -23,7 +21,7 @@ private: public: - CMLuaScript(lua_State *L, const TCHAR *path); + CMLuaScript(const TCHAR *path); ~CMLuaScript(); static CMLuaScript* GetScriptFromEnviroment(lua_State *L); @@ -39,8 +37,8 @@ public: const Status GetStatus() const; - bool Load(); - void Unload(); + bool Load(lua_State *L); + void Unload(lua_State *L); }; #endif //_LUA_SCRIPT_H_ diff --git a/plugins/MirLua/src/mlua_script_loader.cpp b/plugins/MirLua/src/mlua_script_loader.cpp index ddecbad547..f68f00096a 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(L, path); + CMLuaScript *script = new CMLuaScript(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()) + if (script->Load(L)) Log(_T("%s:OK"), path); } -- cgit v1.2.3