From b4a20e15c0fe37e4585974fce072d8baba6a14bc Mon Sep 17 00:00:00 2001 From: Mm2PL Date: Mon, 30 Jan 2023 21:19:29 +0100 Subject: [PATCH] Move pushing stuff to lua stack to LuaUtilities --- src/CMakeLists.txt | 2 + src/controllers/plugins/LuaUtilities.cpp | 38 +++++++++++++++ src/controllers/plugins/LuaUtilities.hpp | 51 ++++++++++++++++++++ src/controllers/plugins/PluginController.cpp | 21 +------- 4 files changed, 93 insertions(+), 19 deletions(-) create mode 100644 src/controllers/plugins/LuaUtilities.cpp create mode 100644 src/controllers/plugins/LuaUtilities.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 828fa235f..28200168d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -138,6 +138,8 @@ set(SOURCE_FILES controllers/plugins/PluginController.hpp controllers/plugins/PluginController.cpp + controllers/plugins/LuaUtilities.cpp + controllers/plugins/LuaUtilities.hpp controllers/userdata/UserDataController.cpp controllers/userdata/UserDataController.hpp diff --git a/src/controllers/plugins/LuaUtilities.cpp b/src/controllers/plugins/LuaUtilities.cpp new file mode 100644 index 000000000..e54b1ef11 --- /dev/null +++ b/src/controllers/plugins/LuaUtilities.cpp @@ -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 diff --git a/src/controllers/plugins/LuaUtilities.hpp b/src/controllers/plugins/LuaUtilities.hpp new file mode 100644 index 000000000..bf5cb47b8 --- /dev/null +++ b/src/controllers/plugins/LuaUtilities.hpp @@ -0,0 +1,51 @@ +#pragma once + +#include "lua.h" + +#include + +#include +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 +int push(lua_State *L, std::vector 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 +int push(lua_State *L, QList 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 diff --git a/src/controllers/plugins/PluginController.cpp b/src/controllers/plugins/PluginController.cpp index 98f180191..cdb155f82 100644 --- a/src/controllers/plugins/PluginController.cpp +++ b/src/controllers/plugins/PluginController.cpp @@ -3,6 +3,7 @@ #include "Application.hpp" #include "common/QLogging.hpp" #include "controllers/commands/CommandContext.hpp" +#include "controllers/plugins/LuaUtilities.hpp" #include "messages/MessageBuilder.hpp" #include "providers/twitch/TwitchIrcServer.hpp" #include "singletons/WindowManager.hpp" @@ -98,25 +99,7 @@ QString PluginController::tryExecPluginCommand(const QString &commandName, auto *L = plugin->state_; // NOLINT lua_getfield(L, LUA_REGISTRYINDEX, funcName.toStdString().c_str()); - // put args on stack - 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"); + lua::push(L, ctx); auto res = lua_pcall(L, 1, 0, 0); if (res != LUA_OK)