mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
Move c2.later to sol
This commit is contained in:
parent
e1dcf28dac
commit
db479f1125
3 changed files with 13 additions and 39 deletions
|
@ -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>
|
||||
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue