Skip to content

Commit

Permalink
Move c2.later to sol
Browse files Browse the repository at this point in the history
  • Loading branch information
Mm2PL committed Oct 7, 2024
1 parent f324965 commit d2a86e4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 39 deletions.
42 changes: 11 additions & 31 deletions src/controllers/plugins/LuaAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
# include <QTextCodec>
# include <QUrl>
# include <sol/forward.hpp>
# include <sol/protected_function_result.hpp>
# include <sol/stack.hpp>
# include <sol/state_view.hpp>
# include <sol/types.hpp>
# include <sol/variadic_args.hpp>
Expand Down Expand Up @@ -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, int time, sol::protected_function callback)
{
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;
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/plugins/LuaAPI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, int time, sol::protected_function callback);

// These ones are global
sol::variadic_results g_load(sol::this_state s, sol::object data);
Expand Down
8 changes: 1 addition & 7 deletions src/controllers/plugins/PluginController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit d2a86e4

Please sign in to comment.