From 533a0aa0576ed31f1840c099267a34330952c4e8 Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Mon, 28 Dec 2015 19:08:23 +0000 Subject: MirLua: minor changes git-svn-id: http://svn.miranda-ng.org/main/trunk@15960 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/MirLua/src/m_chat.cpp | 3 +- plugins/MirLua/src/m_core.cpp | 6 +- plugins/MirLua/src/m_database.cpp | 6 +- plugins/MirLua/src/m_message.cpp | 3 +- plugins/MirLua/src/m_msg_buttonsbar.cpp | 3 +- plugins/MirLua/src/m_protocols.cpp | 30 ++++--- plugins/MirLua/src/m_schedule.cpp | 130 +++++++++++------------------- plugins/MirLua/src/mlua.cpp | 3 +- plugins/MirLua/src/mlua_module_loader.cpp | 6 +- plugins/MirLua/src/mlua_script.cpp | 14 +--- plugins/MirLua/src/mlua_utils.cpp | 42 +++++++--- plugins/MirLua/src/stdafx.h | 4 +- 12 files changed, 109 insertions(+), 141 deletions(-) (limited to 'plugins/MirLua') diff --git a/plugins/MirLua/src/m_chat.cpp b/plugins/MirLua/src/m_chat.cpp index 7a2428c67f..78aa88c810 100644 --- a/plugins/MirLua/src/m_chat.cpp +++ b/plugins/MirLua/src/m_chat.cpp @@ -47,8 +47,7 @@ int GCHookEventObjParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param) lua_pushstring(L, ptrA(mir_utf8encodeT(gce->pDest->ptszID))); lua_settable(L, -3); - if (lua_pcall(L, 2, 1, 0)) - printf("%s\n", lua_tostring(L, -1)); + luaM_pcall(L, 2, 1); int res = (int)lua_tointeger(L, 1); diff --git a/plugins/MirLua/src/m_core.cpp b/plugins/MirLua/src/m_core.cpp index 7498e84ba4..fb8211dda8 100644 --- a/plugins/MirLua/src/m_core.cpp +++ b/plugins/MirLua/src/m_core.cpp @@ -33,8 +33,7 @@ int HookEventObjParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param) else lua_pushnil(L); - if (lua_pcall(L, 2, 1, 0)) - CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)lua_tostring(L, -1)); + luaM_pcall(L, 2, 1); return lua_tointeger(L, 1); } @@ -119,8 +118,7 @@ INT_PTR CreateServiceFunctionObjParam(void *obj, WPARAM wParam, LPARAM lParam, L lua_pushlightuserdata(L, (void*)wParam); lua_pushlightuserdata(L, (void*)lParam); - if (lua_pcall(L, 2, 1, 0)) - CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)lua_tostring(L, -1)); + luaM_pcall(L, 2, 1); INT_PTR res = lua_tointeger(L, 1); lua_pushinteger(L, res); diff --git a/plugins/MirLua/src/m_database.cpp b/plugins/MirLua/src/m_database.cpp index 74ad79af0f..c631715c49 100644 --- a/plugins/MirLua/src/m_database.cpp +++ b/plugins/MirLua/src/m_database.cpp @@ -360,8 +360,7 @@ static int lua_EnumSettings(lua_State *L) { lua_rawgeti(L, LUA_REGISTRYINDEX, ref); lua_pushstring(L, mir_utf8encode(param.pszSettingName[i])); - if (lua_pcall(L, 1, 0, 0)) - CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)lua_tostring(L, -1)); + luaM_pcall(L, 1, 0); mir_free(param.pszSettingName[i]); } @@ -474,8 +473,7 @@ static int SettingsChangedHookEventObjParam(void *obj, WPARAM wParam, LPARAM lPa } lua_settable(L, -3); - if (lua_pcall(L, 2, 1, 0)) - printf("%s\n", lua_tostring(L, -1)); + luaM_pcall(L, 2, 1); int res = (int)lua_tointeger(L, 1); diff --git a/plugins/MirLua/src/m_message.cpp b/plugins/MirLua/src/m_message.cpp index 0db063f684..933160dea1 100644 --- a/plugins/MirLua/src/m_message.cpp +++ b/plugins/MirLua/src/m_message.cpp @@ -93,8 +93,7 @@ int MsgWindowEventHookEventObjParam(void *obj, WPARAM wParam, LPARAM lParam, LPA lua_pushinteger(L, ev->uFlags); lua_settable(L, -3); - if (lua_pcall(L, 2, 1, 0)) - printf("%s\n", lua_tostring(L, -1)); + luaM_pcall(L, 2, 1); int res = (int)lua_tointeger(L, 1); diff --git a/plugins/MirLua/src/m_msg_buttonsbar.cpp b/plugins/MirLua/src/m_msg_buttonsbar.cpp index 25245b7321..b4b4e42800 100644 --- a/plugins/MirLua/src/m_msg_buttonsbar.cpp +++ b/plugins/MirLua/src/m_msg_buttonsbar.cpp @@ -149,8 +149,7 @@ int ButtonPressedHookEventObjParam(void *obj, WPARAM wParam, LPARAM lParam, LPAR lua_pushinteger(L, bcd->flags); lua_settable(L, -3); - if (lua_pcall(L, 2, 1, 0)) - printf("%s\n", lua_tostring(L, -1)); + luaM_pcall(L, 2, 1); int res = (int)lua_tointeger(L, 1); diff --git a/plugins/MirLua/src/m_protocols.cpp b/plugins/MirLua/src/m_protocols.cpp index 8ca1c38460..530f4343a4 100644 --- a/plugins/MirLua/src/m_protocols.cpp +++ b/plugins/MirLua/src/m_protocols.cpp @@ -13,11 +13,11 @@ static void MapToTable(lua_State *L, const PROTOCOLDESCRIPTOR* pd) lua_settable(L, -3); } -static int lua_GetProto(lua_State *L) +static int lua_GetProtocol(lua_State *L) { const char *name = luaL_checkstring(L, 1); - PROTOCOLDESCRIPTOR* pd = ::Proto_IsProtocolLoaded(ptrA(mir_utf8decodeA(name))); + PROTOCOLDESCRIPTOR *pd = ::Proto_IsProtocolLoaded(ptrA(mir_utf8decodeA(name))); if (pd) MT::Set(L, pd); else @@ -26,11 +26,11 @@ static int lua_GetProto(lua_State *L) return 1; } -static int lua_ProtoIterator(lua_State *L) +static int lua_ProtocolIterator(lua_State *L) { int i = lua_tointeger(L, lua_upvalueindex(1)); int count = lua_tointeger(L, lua_upvalueindex(2)); - PROTOCOLDESCRIPTOR** protos = (PROTOCOLDESCRIPTOR**)lua_touserdata(L, lua_upvalueindex(3)); + PROTOCOLDESCRIPTOR **protos = (PROTOCOLDESCRIPTOR**)lua_touserdata(L, lua_upvalueindex(3)); if (i < count) { @@ -44,7 +44,7 @@ static int lua_ProtoIterator(lua_State *L) return 1; } -static int lua_AllProtos(lua_State *L) +static int lua_AllProtocols(lua_State *L) { int count; PROTOCOLDESCRIPTOR** protos; @@ -53,7 +53,7 @@ static int lua_AllProtos(lua_State *L) lua_pushinteger(L, 0); lua_pushinteger(L, count); lua_pushlightuserdata(L, protos); - lua_pushcclosure(L, lua_ProtoIterator, 3); + lua_pushcclosure(L, lua_ProtocolIterator, 3); return 1; } @@ -79,8 +79,7 @@ static int lua_EnumProtos(lua_State *L) { lua_rawgeti(L, LUA_REGISTRYINDEX, ref); MapToTable(L, protos[i]); - if (lua_pcall(L, 1, 0, 0)) - CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)lua_tostring(L, -1)); + luaM_pcall(L, 1, 0); } luaL_unref(L, LUA_REGISTRYINDEX, ref); @@ -181,8 +180,7 @@ static int lua_EnumAccounts(lua_State *L) { lua_rawgeti(L, LUA_REGISTRYINDEX, ref); MapToTable(L, accounts[i]); - if (lua_pcall(L, 1, 0, 0)) - CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)lua_tostring(L, -1)); + luaM_pcall(L, 1, 0); } luaL_unref(L, LUA_REGISTRYINDEX, ref); @@ -222,8 +220,7 @@ int ProtoAckHookEventObjParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM pa lua_pushnumber(L, ack->lParam); lua_settable(L, -3); - if (lua_pcall(L, 2, 1, 0)) - printf("%s\n", lua_tostring(L, -1)); + luaM_pcall(L, 2, 1); int res = (int)lua_tointeger(L, 1); @@ -271,8 +268,7 @@ int RecvMessageHookEventObjParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM lua_pushstring(L, pre->szMessage); lua_settable(L, -3); - if (lua_pcall(L, 2, 1, 0)) - printf("%s\n", lua_tostring(L, -1)); + luaM_pcall(L, 2, 1); int res = (int)lua_tointeger(L, 1); @@ -309,8 +305,10 @@ INT_PTR FilterRecvMessage(WPARAM wParam, LPARAM lParam) static luaL_Reg protocolsApi[] = { - { "GetProto", lua_GetProto }, - { "AllProtos", lua_AllProtos }, + { "GetProto", lua_GetProtocol }, + { "GetProtocol", lua_GetProtocol }, + { "AllProtos", lua_AllProtocols }, + { "AllProtocols", lua_AllProtocols }, { "EnumProtos", lua_EnumProtos }, { "GetAccount", lua_GetAccount }, diff --git a/plugins/MirLua/src/m_schedule.cpp b/plugins/MirLua/src/m_schedule.cpp index ce4e2ea8f9..05563c1cc8 100644 --- a/plugins/MirLua/src/m_schedule.cpp +++ b/plugins/MirLua/src/m_schedule.cpp @@ -11,8 +11,8 @@ struct ScheduleTask time_t interval; lua_State *L; - lua_State *T; - int threadRef; + //lua_State *T; + //int threadRef; int callbackRef; }; @@ -25,7 +25,7 @@ static LIST tasks(1, TaskCompare); void DestroyTask(ScheduleTask *task) { - luaL_unref(task->L, LUA_REGISTRYINDEX, task->threadRef); + //luaL_unref(task->L, LUA_REGISTRYINDEX, task->threadRef); delete task; } @@ -33,9 +33,9 @@ void ExecuteTaskThread(void *arg) { ScheduleTask *task = (ScheduleTask*)arg; - lua_rawgeti(task->T, LUA_REGISTRYINDEX, task->callbackRef); - if (lua_pcall(task->T, 0, 2, 0)) - CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)lua_tostring(task->T, -1)); + lua_State *L = lua_newthread(task->L); + lua_rawgeti(L, LUA_REGISTRYINDEX, task->callbackRef); + luaM_pcall(L, 0, 2); if (task->interval == 0) { @@ -66,6 +66,9 @@ wait: WaitForSingleObject(hScheduleEvent, waitTime); while (ScheduleTask *task = tasks[0]) { + if (Miranda_Terminated()) + return; + mir_cslock lock(threadLock); time_t timestamp = time(NULL); @@ -103,6 +106,31 @@ void KillModuleScheduleTasks() /***********************************************/ +static time_t luaM_opttimestamp(lua_State *L, int idx, time_t def = 0) +{ + switch (lua_type(L, idx)) + { + case LUA_TNUMBER: + return luaL_optinteger(L, idx, def); + + case LUA_TSTRING: + { + const char *strtime = luaL_optstring(L, idx, "00:00:00"); + + int hour = 0, min = 0, sec = 0; + sscanf_s(strtime, "%02d:%02d:%02d", &hour, &min, &sec); + struct tm *ti = localtime(&def); + ti->tm_hour = hour; + ti->tm_min = min; + ti->tm_sec = sec; + return mktime(ti); + } + } + return def; +} + +/***********************************************/ + enum DayOfWeek { None = -1, @@ -345,31 +373,8 @@ static int lua__From(lua_State *L) { luaL_checktype(L, 1, LUA_TTABLE); - time_t startTime, timestamp = time(NULL); - switch (lua_type(L, 2)) - { - case LUA_TNUMBER: - startTime = luaL_optinteger(L, 2, timestamp); - break; - - case LUA_TSTRING: - { - const char *strtime = luaL_optstring(L, 2, "00:00:00"); - - int hour = 0, min = 0, sec = 0; - sscanf_s(strtime, "%02d:%02d:%02d", &hour, &min, &sec); - struct tm *ti = localtime(×tamp); - ti->tm_hour = hour; - ti->tm_min = min; - ti->tm_sec = sec; - startTime = mktime(ti); - break; - } - - default: - startTime = timestamp; - break; - } + time_t timestamp = time(NULL); + time_t startTime = luaM_opttimestamp(L, 2, timestamp); if (startTime < timestamp) { @@ -388,32 +393,11 @@ static int lua__From(lua_State *L) return 1; } -static int lua__Untill(lua_State *L) +static int lua__Until(lua_State *L) { luaL_checktype(L, 1, LUA_TTABLE); - time_t endTime = 0; - switch (lua_type(L, 2)) - { - case LUA_TNUMBER: - endTime = luaL_optinteger(L, 2, 0); - break; - - case LUA_TSTRING: - { - const char *strtime = luaL_optstring(L, 2, "00:00:00"); - - int hour = 0, min = 0, sec = 0; - sscanf_s(strtime, "%02d:%02d:%02d", &hour, &min, &sec); - time_t timestamp = time(NULL); - struct tm *ti = localtime(×tamp); - ti->tm_hour = hour; - ti->tm_min = min; - ti->tm_sec = sec; - endTime = mktime(ti); - break; - } - } + time_t endTime = luaM_opttimestamp(L, 2); lua_pushvalue(L, 1); lua_pushinteger(L, endTime); @@ -469,8 +453,8 @@ static int lua__Do(lua_State *L) task->L = L; lua_pushvalue(L, 2); task->callbackRef = luaL_ref(L, LUA_REGISTRYINDEX); - task->T = lua_newthread(L); - task->threadRef = luaL_ref(L, LUA_REGISTRYINDEX); + //task->T = lua_newthread(L); + //task->threadRef = luaL_ref(L, LUA_REGISTRYINDEX); { mir_cslock lock(threadLock); tasks.insert(task); @@ -500,7 +484,7 @@ static const luaL_Reg schedule[] = { "Saturday", lua__Saturday }, { "Sunday", lua__Sunday }, { "From", lua__From }, - { "Untill", lua__Untill }, + { "Until", lua__Until }, { "Do", lua__Do }, { NULL, NULL } @@ -510,35 +494,12 @@ static const luaL_Reg schedule[] = static int lua__At(lua_State *L) { - time_t startTime, timestamp = time(NULL); - switch (lua_type(L, 1)) - { - case LUA_TNUMBER: - startTime = luaL_optinteger(L, 1, timestamp); - break; - - case LUA_TSTRING: - { - const char *strtime = luaL_optstring(L, 1, "00:00:00"); - - int hour = 0, min = 0, sec = 0; - sscanf_s(strtime, "%02d:%02d:%02d", &hour, &min, &sec); - struct tm *ti = localtime(×tamp); - ti->tm_hour = hour; - ti->tm_min = min; - ti->tm_sec = sec; - startTime = mktime(ti); - break; - } - - default: - startTime = timestamp; - break; - } + time_t timestamp = time(NULL); + time_t startTime = luaM_opttimestamp(L, 1, timestamp); lua_newtable(L); - lua_pushcclosure(L, lua__Untill, 0); - lua_setfield(L, -2, "Untill"); + lua_pushcclosure(L, lua__Until, 0); + lua_setfield(L, -2, "Until"); lua_pushcclosure(L, lua__Do, 0); lua_setfield(L, -2, "Do"); lua_pushinteger(L, startTime); @@ -562,6 +523,7 @@ static const luaL_Reg scheduleApi[] = { { "At", lua__At }, { "Every", lua__Every }, + { "Do", lua__Do }, { NULL, NULL } }; diff --git a/plugins/MirLua/src/mlua.cpp b/plugins/MirLua/src/mlua.cpp index 2b9ad5978e..81dbc6f612 100644 --- a/plugins/MirLua/src/mlua.cpp +++ b/plugins/MirLua/src/mlua.cpp @@ -110,8 +110,7 @@ int CMLua::HookEventObjParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM par lua_pushlightuserdata(L, (void*)wParam); lua_pushlightuserdata(L, (void*)lParam); - if (lua_pcall(L, 2, 1, 0)) - CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)lua_tostring(L, -1)); + luaM_pcall(L, 2, 1); int res = (int)lua_tointeger(L, 1); diff --git a/plugins/MirLua/src/mlua_module_loader.cpp b/plugins/MirLua/src/mlua_module_loader.cpp index a4cce4f418..bd5074a592 100644 --- a/plugins/MirLua/src/mlua_module_loader.cpp +++ b/plugins/MirLua/src/mlua_module_loader.cpp @@ -35,10 +35,8 @@ void CLuaModuleLoader::LoadModules() lua_pushglobaltable(L); lua_getfield(L, -1, "require"); lua_pushstring(L, "m_core"); - if (lua_pcall(L, 1, 1, 0)) - CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)lua_tostring(L, -1)); - lua_pop(L, 1); - lua_pop(L, 1); + luaM_pcall(L, 1, 1); + lua_pop(L, 2); } void CLuaModuleLoader::Load(lua_State *L) diff --git a/plugins/MirLua/src/mlua_script.cpp b/plugins/MirLua/src/mlua_script.cpp index f2deb2ff27..3ddbbd8a15 100644 --- a/plugins/MirLua/src/mlua_script.cpp +++ b/plugins/MirLua/src/mlua_script.cpp @@ -42,11 +42,8 @@ bool CMLuaScript::Load() return false; } - if (lua_pcall(L, 0, 1, 0)) - { - CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)lua_tostring(L, -1)); + if (luaM_pcall(L)) return false; - } isLoaded = true; @@ -56,10 +53,7 @@ bool CMLuaScript::Load() lua_pushliteral(L, "Load"); lua_gettable(L, -2); if (lua_isfunction(L, -1)) - { - if (lua_pcall(L, 0, 0, 0)) - CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)lua_tostring(L, -1)); - } + luaM_pcall(L); else lua_pop(L, 1); @@ -80,8 +74,8 @@ void CMLuaScript::Unload() if (isLoaded && unloadRef) { lua_rawgeti(L, LUA_REGISTRYINDEX, unloadRef); - if (lua_isfunction(L, -1) && lua_pcall(L, 0, 0, 0)) - CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)lua_tostring(L, -1)); + if (lua_isfunction(L, -1)) + luaM_pcall(L); luaL_unref(L, LUA_REGISTRYINDEX, unloadRef); isLoaded = false; } diff --git a/plugins/MirLua/src/mlua_utils.cpp b/plugins/MirLua/src/mlua_utils.cpp index 7d8d158555..f8fbae33b8 100644 --- a/plugins/MirLua/src/mlua_utils.cpp +++ b/plugins/MirLua/src/mlua_utils.cpp @@ -1,5 +1,35 @@ #include "stdafx.h" +int luaM_atpanic(lua_State *L) +{ + CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)lua_tostring(L, -1)); + + 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) +{ + /*lua_pushcfunction(L, luaM_trace); + lua_insert(L, f); + const int f = -(n + 2);*/ + const int f = 0; + int res = lua_pcall(L, n, r, f); + if (res != LUA_OK) + CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)lua_tostring(L, -1)); + return res; +} + int luaM_print(lua_State *L) { CMStringA data; @@ -34,13 +64,6 @@ int luaM_print(lua_State *L) return 0; } -int luaM_atpanic(lua_State *L) -{ - CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)lua_tostring(L, -1)); - - return 0; -} - int luaM_toansi(lua_State *L) { const char* value = luaL_checkstring(L, 1); @@ -70,7 +93,7 @@ int luaM_toucs2(lua_State *L) bool luaM_toboolean(lua_State *L, int idx) { if (lua_type(L, idx) == LUA_TNUMBER) - return lua_tonumber(L, idx) != 0; + return lua_tonumber(L, idx) > 0; return lua_toboolean(L, idx) > 0; } @@ -119,8 +142,7 @@ int luaM_totable(lua_State *L) //lua_getfield(L, -1, "__init"); lua_getglobal(L, tname); lua_pushvalue(L, 1); - if (lua_pcall(L, 1, 1, 0)) - CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)lua_tostring(L, -1)); + luaM_pcall(L, 1, 1); return 1; } diff --git a/plugins/MirLua/src/stdafx.h b/plugins/MirLua/src/stdafx.h index 2192f19872..ad4652ec1e 100644 --- a/plugins/MirLua/src/stdafx.h +++ b/plugins/MirLua/src/stdafx.h @@ -110,8 +110,10 @@ LUAMOD_API int (luaopen_m_variables)(lua_State *L); #define MLUA_WINDOWS "m_windows" LUAMOD_API int (luaopen_m_windows)(lua_State *L); -int luaM_print(lua_State *L); int luaM_atpanic(lua_State *L); +int luaM_pcall(lua_State *L, int n = 0, int r = 0); + +int luaM_print(lua_State *L); int luaM_toansi(lua_State *L); int luaM_toucs2(lua_State *L); -- cgit v1.2.3