From db479f112597ec6805dc33dbeb249a1bf3f2e542 Mon Sep 17 00:00:00 2001 From: Mm2PL Date: Mon, 7 Oct 2024 23:44:45 +0200 Subject: [PATCH] Move c2.later to sol --- src/controllers/plugins/LuaAPI.cpp | 42 +++++--------------- src/controllers/plugins/LuaAPI.hpp | 2 +- src/controllers/plugins/PluginController.cpp | 8 +--- 3 files changed, 13 insertions(+), 39 deletions(-) diff --git a/src/controllers/plugins/LuaAPI.cpp b/src/controllers/plugins/LuaAPI.cpp index e6acf0393..d2603750b 100644 --- a/src/controllers/plugins/LuaAPI.cpp +++ b/src/controllers/plugins/LuaAPI.cpp @@ -16,6 +16,8 @@ # include # include # include +# include +# include # include # include # include @@ -102,58 +104,36 @@ void c2_log(sol::this_state L, Plugin *pl, LogLevel lvl, } } -int c2_later(lua_State *L) +int c2_later(sol::this_state L, sol::protected_function callback, int time) { auto *pl = getApp()->getPlugins()->getPluginByStatePtr(L); if (pl == nullptr) { return luaL_error(L, "c2.later: internal error: no plugin?"); } - if (lua_gettop(L) != 2) - { - return luaL_error( - L, "c2.later expects two arguments (a callback that takes no " - "arguments and returns nothing and a number the time in " - "milliseconds to wait)\n"); - } - int time{}; - if (!lua::pop(L, &time)) - { - return luaL_error(L, "cannot get time (2nd arg of c2.later, " - "expected a number)"); - } - - if (!lua_isfunction(L, lua_gettop(L))) - { - return luaL_error(L, "cannot get callback (1st arg of c2.later, " - "expected a function)"); - } + sol::state_view lua(L); auto *timer = new QTimer(); timer->setInterval(time); auto id = pl->addTimeout(timer); auto name = QString("timeout_%1").arg(id); - auto *coro = lua_newthread(L); + //auto *coro = lua_newthread(L); - QObject::connect(timer, &QTimer::timeout, [pl, coro, name, timer]() { + QObject::connect(timer, &QTimer::timeout, [pl, name, timer, callback]() { timer->deleteLater(); pl->removeTimeout(timer); - int nres{}; - lua_resume(coro, nullptr, 0, &nres); + sol::state_view lua(callback.lua_state()); + sol::protected_function_result res = callback(); - lua_pushnil(coro); - lua_setfield(coro, LUA_REGISTRYINDEX, name.toStdString().c_str()); - if (lua_gettop(coro) != 0) + if (res.return_count() != 0) { - stackDump(coro, + stackDump(lua.lua_state(), pl->id + ": timer returned a value, this shouldn't happen " "and is probably a plugin bug"); } + lua.registry()[name.toStdString()] = sol::nil; }); - stackDump(L, "before setfield"); - lua_setfield(L, LUA_REGISTRYINDEX, name.toStdString().c_str()); - lua_xmove(L, coro, 1); // move function to thread timer->start(); return 0; diff --git a/src/controllers/plugins/LuaAPI.hpp b/src/controllers/plugins/LuaAPI.hpp index 75f2af0fa..99143e43d 100644 --- a/src/controllers/plugins/LuaAPI.hpp +++ b/src/controllers/plugins/LuaAPI.hpp @@ -123,7 +123,7 @@ void c2_log(sol::this_state L, Plugin *pl, LogLevel lvl, * @lua@param msec number How long to wait. * @exposed c2.later */ -int c2_later(lua_State *L); +int c2_later(sol::this_state L, sol::protected_function callback, int time); // These ones are global sol::variadic_results g_load(sol::this_state s, sol::object data); diff --git a/src/controllers/plugins/PluginController.cpp b/src/controllers/plugins/PluginController.cpp index 1ea05b7a8..b4dde0ed5 100644 --- a/src/controllers/plugins/PluginController.cpp +++ b/src/controllers/plugins/PluginController.cpp @@ -151,19 +151,12 @@ void PluginController::openLibrariesFor(Plugin *plugin, const QDir &pluginDir) luaL_requiref(L, LUA_IOLIBNAME, luaopen_io, int(false)); lua_setfield(L, LUA_REGISTRYINDEX, lua::api::REG_REAL_IO_NAME); - // NOLINTNEXTLINE(*-avoid-c-arrays) - static const luaL_Reg c2Lib[] = { - {"later", lua::api::c2_later}, - {nullptr, nullptr}, - }; lua_pushglobaltable(L); auto gtable = lua_gettop(L); // count of elements in C2LIB + LogLevel + EventType auto c2libIdx = lua::pushEmptyTable(L, 8); - luaL_setfuncs(L, c2Lib, 0); - lua_setfield(L, gtable, "c2"); // ban functions @@ -275,6 +268,7 @@ void PluginController::initSol(sol::state_view &lua, Plugin *plugin) sol::variadic_args args) { lua::api::c2_log(s, plugin, lvl, args); }); + c2.set_function("later", &lua::api::c2_later); lua::api::ChannelRef::createUserType(c2); lua::api::HTTPResponse::createUserType(c2);