Move pushing stuff to lua stack to LuaUtilities

This commit is contained in:
Mm2PL 2023-01-30 21:19:29 +01:00
parent 7312af1bec
commit b4a20e15c0
No known key found for this signature in database
GPG key ID: 94AC9B80EFA15ED9
4 changed files with 93 additions and 19 deletions

View file

@ -138,6 +138,8 @@ set(SOURCE_FILES
controllers/plugins/PluginController.hpp controllers/plugins/PluginController.hpp
controllers/plugins/PluginController.cpp controllers/plugins/PluginController.cpp
controllers/plugins/LuaUtilities.cpp
controllers/plugins/LuaUtilities.hpp
controllers/userdata/UserDataController.cpp controllers/userdata/UserDataController.cpp
controllers/userdata/UserDataController.hpp controllers/userdata/UserDataController.hpp

View file

@ -0,0 +1,38 @@
#include "LuaUtilities.hpp"
#include "common/Channel.hpp"
#include "controllers/commands/CommandContext.hpp"
#include "lua.h"
namespace chatterino::lua {
int pushEmptyArray(lua_State *L, int countArray)
{
lua_createtable(L, countArray, 0);
return lua_gettop(L);
}
int pushEmptyTable(lua_State *L, int countProperties)
{
lua_createtable(L, 0, countProperties);
return lua_gettop(L);
}
int push(lua_State *L, const QString &str)
{
lua_pushstring(L, str.toStdString().c_str());
return lua_gettop(L);
}
int push(lua_State *L, const CommandContext &ctx)
{
auto outIdx = pushEmptyTable(L, 2);
push(L, ctx.words);
lua_setfield(L, outIdx, "words");
push(L, ctx.channel->getName());
lua_setfield(L, outIdx, "channelName");
return outIdx;
}
} // namespace chatterino::lua

View file

@ -0,0 +1,51 @@
#pragma once
#include "lua.h"
#include <qlist.h>
#include <vector>
struct lua_State;
namespace chatterino {
class CommandContext;
} // namespace chatterino
namespace chatterino::lua {
int pushEmptyArray(lua_State *L, int countArray);
int pushEmptyTable(lua_State *L, int countProperties);
int push(lua_State *L, const CommandContext &ctx);
int push(lua_State *L, const QString &str);
/// TEMPLATES
template <typename T>
int push(lua_State *L, std::vector<T> vec)
{
auto out = pushEmptyArray(L, vec.size());
int i = 1;
for (const auto &el : vec)
{
push(L, el);
lua_seti(L, out, i);
i += 1;
}
return out;
}
template <typename T>
int push(lua_State *L, QList<T> vec)
{
auto out = pushEmptyArray(L, vec.size());
int i = 1;
for (const auto &el : vec)
{
push(L, el);
lua_seti(L, out, i);
i += 1;
}
return out;
}
} // namespace chatterino::lua

View file

@ -3,6 +3,7 @@
#include "Application.hpp" #include "Application.hpp"
#include "common/QLogging.hpp" #include "common/QLogging.hpp"
#include "controllers/commands/CommandContext.hpp" #include "controllers/commands/CommandContext.hpp"
#include "controllers/plugins/LuaUtilities.hpp"
#include "messages/MessageBuilder.hpp" #include "messages/MessageBuilder.hpp"
#include "providers/twitch/TwitchIrcServer.hpp" #include "providers/twitch/TwitchIrcServer.hpp"
#include "singletons/WindowManager.hpp" #include "singletons/WindowManager.hpp"
@ -98,25 +99,7 @@ QString PluginController::tryExecPluginCommand(const QString &commandName,
auto *L = plugin->state_; // NOLINT auto *L = plugin->state_; // NOLINT
lua_getfield(L, LUA_REGISTRYINDEX, funcName.toStdString().c_str()); lua_getfield(L, LUA_REGISTRYINDEX, funcName.toStdString().c_str());
// put args on stack lua::push(L, ctx);
lua_createtable(L, 0, 2);
auto outIdx = lua_gettop(L);
lua_createtable(L, ctx.words.count(), 0);
auto wordsIdx = lua_gettop(L);
int i = 1;
for (const auto &w : ctx.words)
{
lua_pushstring(L, w.toStdString().c_str());
lua_seti(L, wordsIdx, i);
i += 1;
}
lua_setfield(L, outIdx, "words");
lua_pushstring(L, ctx.channel->getName().toStdString().c_str());
lua_setfield(L, outIdx, "channelName");
auto res = lua_pcall(L, 1, 0, 0); auto res = lua_pcall(L, 1, 0, 0);
if (res != LUA_OK) if (res != LUA_OK)