From 55ca0ecfa502004ebb473cb5997d9985c0547d33 Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Thu, 14 Jan 2016 18:32:22 +0000 Subject: MirLua: - added Version in m_core - reworked m_schedule, pt.2 git-svn-id: http://svn.miranda-ng.org/main/trunk@16090 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/MirLua/src/m_core.cpp | 8 + plugins/MirLua/src/m_schedule.cpp | 365 +++++++++++++++++++++++++------------- 2 files changed, 253 insertions(+), 120 deletions(-) (limited to 'plugins/MirLua/src') diff --git a/plugins/MirLua/src/m_core.cpp b/plugins/MirLua/src/m_core.cpp index 1c39729333..d75f0435a0 100644 --- a/plugins/MirLua/src/m_core.cpp +++ b/plugins/MirLua/src/m_core.cpp @@ -283,6 +283,8 @@ luaL_Reg coreApi[] = { "GetFullPath", core_GetFullPath }, + { "Version", NULL }, + { "NULL", NULL }, { "INVALID_HANDLE_VALUE", NULL }, { "CALLSERVICE_NOTFOUND", NULL }, @@ -299,6 +301,12 @@ LUAMOD_API int luaopen_m_core(lua_State *L) lua_setfield(L, -2, "INVALID_HANDLE_VALUE"); lua_pushinteger(L, CALLSERVICE_NOTFOUND); lua_setfield(L, -2, "CALLSERVICE_NOTFOUND"); + + char version[128]; + CallService(MS_SYSTEM_GETVERSIONTEXT, (WPARAM)_countof(version), (LPARAM)version); + lua_pushstring(L, ptrA(mir_utf8encode(version))); + lua_setfield(L, -2, "Version"); + // set copy to global variable m lua_pushvalue(L, -1); lua_setglobal(L, "m"); diff --git a/plugins/MirLua/src/m_schedule.cpp b/plugins/MirLua/src/m_schedule.cpp index 1546d122e3..3ee8b70663 100644 --- a/plugins/MirLua/src/m_schedule.cpp +++ b/plugins/MirLua/src/m_schedule.cpp @@ -4,10 +4,11 @@ static mir_cs threadLock; static HANDLE hScheduleEvent = NULL; static HANDLE hScheduleThread = NULL; +#define STOP ((void *) -1) + struct ScheduleTask { - time_t startTime; - time_t endTime; + time_t timestamp; time_t interval; lua_State *L; @@ -17,7 +18,7 @@ struct ScheduleTask static int TaskCompare(const ScheduleTask *p1, const ScheduleTask *p2) { - return p1->startTime - p2->startTime; + return p1->timestamp - p2->timestamp; } static LIST tasks(1, TaskCompare); @@ -34,9 +35,11 @@ void ExecuteTaskThread(void *arg) ScheduleTask *task = (ScheduleTask*)arg; lua_rawgeti(task->L, LUA_REGISTRYINDEX, task->callbackRef); - luaM_pcall(task->L); + luaM_pcall(task->L, 0, 1); + + void* res = lua_touserdata(task->L, -1); - if (task->interval == 0) + if (res == STOP || task->interval == 0) { DestroyTask(task); return; @@ -46,10 +49,10 @@ void ExecuteTaskThread(void *arg) mir_cslock lock(threadLock); time_t timestamp = time(NULL); - if(task->startTime + task->interval >= timestamp) - task->startTime += task->interval; + if(task->timestamp + task->interval >= timestamp) + task->timestamp += task->interval; else - task->startTime = timestamp + task->interval; + task->timestamp = timestamp + task->interval; tasks.insert(task); } SetEvent(hScheduleEvent); @@ -71,20 +74,14 @@ wait: WaitForSingleObject(hScheduleEvent, waitTime); mir_cslock lock(threadLock); time_t timestamp = time(NULL); - if (task->startTime > timestamp) + if (task->timestamp > timestamp) { - waitTime = (task->startTime - timestamp - 1) * 1000; + waitTime = (task->timestamp - timestamp - 1) * 1000; goto wait; } tasks.remove(task); - if (task->endTime > 0 && task->endTime < timestamp) - { - DestroyTask(task); - continue; - } - mir_forkthread(ExecuteTaskThread, task); } @@ -117,12 +114,14 @@ static time_t luaM_opttimestamp(lua_State *L, int idx, time_t def = 0) 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); + if (sscanf_s(strtime, "%02d:%02d:%02d", &hour, &min, &sec) >= 2) + { + struct tm *ti = localtime(&def); + ti->tm_hour = hour; + ti->tm_min = min; + ti->tm_sec = sec; + return mktime(ti); + } } } return def; @@ -130,6 +129,83 @@ static time_t luaM_opttimestamp(lua_State *L, int idx, time_t def = 0) /***********************************************/ +static int fluent_Do(lua_State *L) +{ + luaL_checktype(L, 1, LUA_TFUNCTION); + + lua_getfield(L, lua_upvalueindex(1), "Interval"); + int interval = luaL_optinteger(L, -1, 0); + lua_pop(L, 1); + + lua_getfield(L, lua_upvalueindex(1), "Timestamp"); + time_t timestamp = lua_tointeger(L, -1); + lua_pop(L, 1); + + time_t now = time(NULL); + if (timestamp < now) + { + if (interval == 0) + return 0; + timestamp += (((now - timestamp) / interval) + 1) * interval; + } + + lua_pushnil(L); + lua_replace(L, lua_upvalueindex(1)); + + ScheduleTask *task = new ScheduleTask(); + task->timestamp = timestamp; + task->interval = interval; + task->L = lua_newthread(L); + task->threadRef = luaL_ref(L, LUA_REGISTRYINDEX); + lua_pushvalue(L, 1); + task->callbackRef = luaL_ref(L, LUA_REGISTRYINDEX); + { + mir_cslock lock(threadLock); + tasks.insert(task); + } + SetEvent(hScheduleEvent); + + return 0; +} + +static int fluent_At(lua_State *L) +{ + time_t timestamp = luaM_opttimestamp(L, 1, 0); + + lua_pushvalue(L, lua_upvalueindex(1)); + + time_t now = time(NULL); + if (timestamp < now) + { + lua_getfield(L, -1, "Interval"); + int interval = luaL_optinteger(L, -1, 0); + lua_pop(L, 1); + if (interval > 0) + timestamp += (((now - timestamp) / interval) + 1) * interval; + } + + lua_pushinteger(L, timestamp); + lua_setfield(L, -2, "Timestamp"); + + lua_newtable(L); + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); + lua_pushvalue(L, -2); + lua_pushcclosure(L, fluent_Do, 1); + lua_setfield(L, -2, "Do"); + lua_setmetatable(L, -2); + + return 1; +} + +static const luaL_Reg scheduleEvery3Api[] = +{ + { "At", fluent_At }, + { "Do", fluent_Do }, + + { NULL, NULL } +}; + static int fluent_Second(lua_State *L) { lua_pushvalue(L, lua_upvalueindex(1)); @@ -137,6 +213,13 @@ static int fluent_Second(lua_State *L) lua_pushinteger(L, 1); lua_setfield(L, -2, "Interval"); + lua_newtable(L); + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); + lua_pushvalue(L, -2); + luaL_setfuncs(L, scheduleEvery3Api, 1); + lua_setmetatable(L, -2); + return 1; } @@ -150,6 +233,13 @@ static int fluent_Seconds(lua_State *L) lua_pushinteger(L, seconds); lua_setfield(L, -2, "Interval"); + lua_newtable(L); + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); + lua_pushvalue(L, -2); + luaL_setfuncs(L, scheduleEvery3Api, 1); + lua_setmetatable(L, -2); + return 1; } @@ -160,6 +250,13 @@ static int fluent_Minute(lua_State *L) lua_pushinteger(L, 60); lua_setfield(L, -2, "Interval"); + lua_newtable(L); + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); + lua_pushvalue(L, -2); + luaL_setfuncs(L, scheduleEvery3Api, 1); + lua_setmetatable(L, -2); + return 1; } @@ -173,6 +270,13 @@ static int fluent_Minutes(lua_State *L) lua_pushinteger(L, interval * 60); lua_setfield(L, -2, "Interval"); + lua_newtable(L); + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); + lua_pushvalue(L, -2); + luaL_setfuncs(L, scheduleEvery3Api, 1); + lua_setmetatable(L, -2); + return 1; } @@ -183,6 +287,13 @@ static int fluent_Hour(lua_State *L) lua_pushinteger(L, 60 * 60); lua_setfield(L, -2, "Interval"); + lua_newtable(L); + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); + lua_pushvalue(L, -2); + luaL_setfuncs(L, scheduleEvery3Api, 1); + lua_setmetatable(L, -2); + return 1; } @@ -196,6 +307,13 @@ static int fluent_Hours(lua_State *L) lua_pushinteger(L, interval * 60 * 60); lua_setfield(L, -2, "Interval"); + lua_newtable(L); + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); + lua_pushvalue(L, -2); + luaL_setfuncs(L, scheduleEvery3Api, 1); + lua_setmetatable(L, -2); + return 1; } @@ -206,6 +324,13 @@ static int fluent_Day(lua_State *L) lua_pushinteger(L, 60 * 60 * 24); lua_setfield(L, -2, "Interval"); + lua_newtable(L); + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); + lua_pushvalue(L, -2); + luaL_setfuncs(L, scheduleEvery3Api, 1); + lua_setmetatable(L, -2); + return 1; } @@ -219,6 +344,13 @@ static int fluent_Days(lua_State *L) lua_pushinteger(L, interval * 60 * 60 * 24); lua_setfield(L, -2, "Interval"); + lua_newtable(L); + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); + lua_pushvalue(L, -2); + luaL_setfuncs(L, scheduleEvery3Api, 1); + lua_setmetatable(L, -2); + return 1; } @@ -229,6 +361,13 @@ static int fluent_Week(lua_State *L) lua_pushinteger(L, 60 * 60 * 24 * 7); lua_setfield(L, -2, "Interval"); + lua_newtable(L); + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); + lua_pushvalue(L, -2); + luaL_setfuncs(L, scheduleEvery3Api, 1); + lua_setmetatable(L, -2); + return 1; } @@ -240,10 +379,17 @@ static int fluent_Monday(lua_State *L) struct tm *ti = localtime(×tamp); ti->tm_mday += abs(1 - ti->tm_wday); lua_pushinteger(L, mktime(ti)); - lua_setfield(L, -2, "StartTime"); + lua_setfield(L, -2, "Timestamp"); lua_pushinteger(L, 60 * 60 * 24 * 7); lua_setfield(L, -2, "Interval"); + lua_newtable(L); + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); + lua_pushvalue(L, -2); + luaL_setfuncs(L, scheduleEvery3Api, 1); + lua_setmetatable(L, -2); + return 1; } @@ -255,10 +401,17 @@ static int fluent_Tuesday(lua_State *L) struct tm *ti = localtime(×tamp); ti->tm_mday += abs(2 - ti->tm_wday); lua_pushinteger(L, mktime(ti)); - lua_setfield(L, -2, "StartTime"); + lua_setfield(L, -2, "Timestamp"); lua_pushinteger(L, 60 * 60 * 24 * 7); lua_setfield(L, -2, "Interval"); + lua_newtable(L); + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); + lua_pushvalue(L, -2); + luaL_setfuncs(L, scheduleEvery3Api, 1); + lua_setmetatable(L, -2); + return 1; } @@ -270,10 +423,17 @@ static int fluent_Wednesday(lua_State *L) struct tm *ti = localtime(×tamp); ti->tm_mday += abs(3 - ti->tm_wday); lua_pushinteger(L, mktime(ti)); - lua_setfield(L, -2, "StartTime"); + lua_setfield(L, -2, "Timestamp"); lua_pushinteger(L, 60 * 60 * 24 * 7); lua_setfield(L, -2, "Interval"); + lua_newtable(L); + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); + lua_pushvalue(L, -2); + luaL_setfuncs(L, scheduleEvery3Api, 1); + lua_setmetatable(L, -2); + return 1; } @@ -285,10 +445,17 @@ static int fluent_Thursday(lua_State *L) struct tm *ti = localtime(×tamp); ti->tm_mday += abs(4 - ti->tm_wday); lua_pushinteger(L, mktime(ti)); - lua_setfield(L, -2, "StartTime"); + lua_setfield(L, -2, "Timestamp"); lua_pushinteger(L, 60 * 60 * 24 * 7); lua_setfield(L, -2, "Interval"); + lua_newtable(L); + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); + lua_pushvalue(L, -2); + luaL_setfuncs(L, scheduleEvery3Api, 1); + lua_setmetatable(L, -2); + return 1; } @@ -300,10 +467,17 @@ static int fluent_Friday(lua_State *L) struct tm *ti = localtime(×tamp); ti->tm_mday += abs(5 - ti->tm_wday); lua_pushinteger(L, mktime(ti)); - lua_setfield(L, -2, "StartTime"); + lua_setfield(L, -2, "Timestamp"); lua_pushinteger(L, 60 * 60 * 24 * 7); lua_setfield(L, -2, "Interval"); + lua_newtable(L); + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); + lua_pushvalue(L, -2); + luaL_setfuncs(L, scheduleEvery3Api, 1); + lua_setmetatable(L, -2); + return 1; } @@ -315,10 +489,17 @@ static int fluent_Saturday(lua_State *L) struct tm *ti = localtime(×tamp); ti->tm_mday += abs(6 - ti->tm_wday); lua_pushinteger(L, mktime(ti)); - lua_setfield(L, -2, "StartTime"); + lua_setfield(L, -2, "Timestamp"); lua_pushinteger(L, 60 * 60 * 24 * 7); lua_setfield(L, -2, "Interval"); + lua_newtable(L); + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); + lua_pushvalue(L, -2); + luaL_setfuncs(L, scheduleEvery3Api, 1); + lua_setmetatable(L, -2); + return 1; } @@ -330,104 +511,36 @@ static int fluent_Sunday(lua_State *L) struct tm *ti = localtime(×tamp); ti->tm_mday += abs(-ti->tm_wday); lua_pushinteger(L, mktime(ti)); - lua_setfield(L, -2, "StartTime"); + lua_setfield(L, -2, "Timestamp"); lua_pushinteger(L, 60 * 60 * 24 * 7); lua_setfield(L, -2, "Interval"); - return 1; -} - -static int fluent_From(lua_State *L) -{ - time_t timestamp = time(NULL); - time_t startTime = luaM_opttimestamp(L, 1, timestamp); - - lua_pushvalue(L, lua_upvalueindex(1)); - - if (startTime < timestamp) - { - lua_getfield(L, -1, "Interval"); - int interval = luaL_optinteger(L, -1, 1); - lua_pop(L, 1); - if (interval > 0) - startTime += (((timestamp - startTime) / interval) + 1) * interval; - } - - lua_pushinteger(L, startTime); - lua_setfield(L, -2, "StartTime"); - - return 1; -} - -static int fluent_To(lua_State *L) -{ - time_t endTime = luaM_opttimestamp(L, 1); - - lua_pushvalue(L, lua_upvalueindex(1)); - lua_pushinteger(L, endTime); - lua_setfield(L, -2, "EndTime"); + lua_newtable(L); + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); + lua_pushvalue(L, -2); + luaL_setfuncs(L, scheduleEvery3Api, 1); + lua_setmetatable(L, -2); return 1; } -static int fluent_Do(lua_State *L) +static const luaL_Reg scheduleEvery2Api[] = { - luaL_checktype(L, 1, LUA_TFUNCTION); - - lua_getfield(L, lua_upvalueindex(1), "Interval"); - int interval = luaL_optinteger(L, -1, 0); - lua_pop(L, 1); - - time_t timestamp = time(NULL); - - lua_getfield(L, lua_upvalueindex(1), "EndTime"); - time_t endTime = luaL_optinteger(L, -1, 0); - lua_pop(L, 1); - - if (endTime > 0 && endTime < timestamp) - return 0; - - lua_getfield(L, lua_upvalueindex(1), "StartTime"); - time_t startTime = luaL_optinteger(L, -1, timestamp); - lua_pop(L, 1); - - if (startTime < timestamp) - { - if (interval == 0) - return 0; - startTime += (((timestamp - startTime) / interval) + 1) * interval; - } - - lua_pushnil(L); - lua_replace(L, lua_upvalueindex(1)); - - ScheduleTask *task = new ScheduleTask(); - task->startTime = startTime; - task->endTime = endTime; - task->interval = interval; - task->L = lua_newthread(L); - task->threadRef = luaL_ref(L, LUA_REGISTRYINDEX); - lua_pushvalue(L, 1); - task->callbackRef = luaL_ref(L, LUA_REGISTRYINDEX); - { - mir_cslock lock(threadLock); - tasks.insert(task); - } - SetEvent(hScheduleEvent); + { "Seconds", fluent_Seconds }, + { "Minutes", fluent_Minutes }, + { "Hours", fluent_Hours }, + { "Days", fluent_Days }, - return 0; -} + { NULL, NULL } +}; -static const luaL_Reg scheduleFluentApi[] = +static const luaL_Reg scheduleEvery1Api[] = { { "Second", fluent_Second }, - { "Seconds", fluent_Seconds }, { "Minute", fluent_Minute }, - { "Minutes", fluent_Minutes }, { "Hour", fluent_Hour }, - { "Hours", fluent_Hours }, { "Day", fluent_Day }, - { "Days", fluent_Days }, { "Week", fluent_Week }, { "Monday", fluent_Monday }, { "Tuesday", fluent_Tuesday }, @@ -436,9 +549,6 @@ static const luaL_Reg scheduleFluentApi[] = { "Friday", fluent_Friday }, { "Saturday", fluent_Saturday }, { "Sunday", fluent_Sunday }, - { "From", fluent_From }, - { "To", fluent_To }, - { "Do", fluent_Do }, { NULL, NULL } }; @@ -452,14 +562,15 @@ static int schedule_At(lua_State *L) lua_newtable(L); lua_pushinteger(L, startTime); - lua_setfield(L, -2, "StartTime"); + lua_setfield(L, -2, "Timestamp"); + lua_newtable(L); lua_pushvalue(L, -1); - lua_pushcclosure(L, fluent_To, 1); - lua_setfield(L, -2, "To"); - lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); + lua_pushvalue(L, -2); lua_pushcclosure(L, fluent_Do, 1); lua_setfield(L, -2, "Do"); + lua_setmetatable(L, -2); return 1; } @@ -469,10 +580,18 @@ static int schedule_Every(lua_State *L) int interval = luaL_optinteger(L, 1, 0); lua_newtable(L); - lua_pushvalue(L, -1); lua_pushinteger(L, interval); lua_setfield(L, -2, "Interval"); - luaL_setfuncs(L, scheduleFluentApi, 1); + + lua_newtable(L); + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); + lua_pushvalue(L, -2); + const luaL_Reg *funcs = lua_isnoneornil(L, 1) + ? scheduleEvery1Api + : scheduleEvery2Api; + luaL_setfuncs(L, funcs, 1); + lua_setmetatable(L, -2); return 1; } @@ -496,12 +615,18 @@ static const luaL_Reg scheduleApi[] = { "Every", schedule_Every }, { "Do", schedule_Do }, + { "STOP", NULL }, + { NULL, NULL } }; +/***********************************************/ + LUAMOD_API int luaopen_m_schedule(lua_State *L) { luaL_newlib(L, scheduleApi); + lua_pushlightuserdata(L, STOP); + lua_setfield(L, -2, "STOP"); if (hScheduleEvent == NULL) hScheduleEvent = CreateEvent(NULL, FALSE, FALSE, NULL); -- cgit v1.2.3