From 082420b94a74a6c4ac6bff79128569c65f4cf65f Mon Sep 17 00:00:00 2001 From: Mm2PL Date: Mon, 30 Jan 2023 22:21:09 +0100 Subject: [PATCH] Add more push/peek/pop functions to LuaUtilities --- src/controllers/plugins/LuaUtilities.cpp | 36 +++++++++++++++++++ src/controllers/plugins/LuaUtilities.hpp | 16 +++++++++ src/controllers/plugins/PluginController.cpp | 37 ++++++++++++-------- 3 files changed, 74 insertions(+), 15 deletions(-) diff --git a/src/controllers/plugins/LuaUtilities.cpp b/src/controllers/plugins/LuaUtilities.cpp index e54b1ef11..f230b847d 100644 --- a/src/controllers/plugins/LuaUtilities.cpp +++ b/src/controllers/plugins/LuaUtilities.cpp @@ -2,8 +2,12 @@ #include "common/Channel.hpp" #include "controllers/commands/CommandContext.hpp" +#include "lauxlib.h" #include "lua.h" +#include +#include + namespace chatterino::lua { int pushEmptyArray(lua_State *L, int countArray) { @@ -35,4 +39,36 @@ int push(lua_State *L, const CommandContext &ctx) return outIdx; } +int push(lua_State *L, const bool &b) +{ + lua_pushboolean(L, int(b)); + return lua_gettop(L); +} + +bool peek(lua_State *L, double *out, int idx) +{ + int ok{0}; + auto v = lua_tonumberx(L, idx, &ok); + if (ok != 0) + { + *out = v; + } + return ok != 0; +} + +bool peek(lua_State *L, QString *out, int idx) +{ + size_t len{0}; + const char *str = lua_tolstring(L, idx, &len); + if (str == nullptr) + { + return false; + } + if (len >= INT_MAX) + { + assert(false && "string longer than INT_MAX, shit's fucked, yo"); + } + *out = QString::fromUtf8(str, int(len)); + return true; +} } // namespace chatterino::lua diff --git a/src/controllers/plugins/LuaUtilities.hpp b/src/controllers/plugins/LuaUtilities.hpp index bf5cb47b8..c73c1f453 100644 --- a/src/controllers/plugins/LuaUtilities.hpp +++ b/src/controllers/plugins/LuaUtilities.hpp @@ -17,6 +17,11 @@ int pushEmptyTable(lua_State *L, int countProperties); int push(lua_State *L, const CommandContext &ctx); int push(lua_State *L, const QString &str); +int push(lua_State *L, const bool &b); + +// returns OK? +bool peek(lua_State *L, double *out, int idx = -1); +bool peek(lua_State *L, QString *out, int idx = -1); /// TEMPLATES @@ -48,4 +53,15 @@ int push(lua_State *L, QList vec) return out; } +template +bool pop(lua_State *L, T *out, int idx = -1) +{ + auto ok = peek(L, out, idx); + if (ok) + { + lua_pop(L, 1); + } + return ok; +} + } // namespace chatterino::lua diff --git a/src/controllers/plugins/PluginController.cpp b/src/controllers/plugins/PluginController.cpp index cdb155f82..6225af569 100644 --- a/src/controllers/plugins/PluginController.cpp +++ b/src/controllers/plugins/PluginController.cpp @@ -143,9 +143,6 @@ QString PluginController::tryExecPluginCommand(const QString &commandName, return ""; } -constexpr int C_FALSE = 0; -constexpr int C_TRUE = 1; - extern "C" { int luaC2SystemMsg(lua_State *L) @@ -154,22 +151,25 @@ int luaC2SystemMsg(lua_State *L) { qCDebug(chatterinoLua) << "system_msg: need 2 args"; luaL_error(L, "need exactly 2 arguments"); // NOLINT - lua_pushboolean(L, C_FALSE); + lua::push(L, false); return 1; } - const char *channel = luaL_optstring(L, 1, NULL); - const char *text = luaL_optstring(L, 2, NULL); - lua_pop(L, 2); + //const char *channel = luaL_optstring(L, 1, NULL); + QString channel; + QString text; + lua::pop(L, &text); + lua::pop(L, &channel); + //const char *text = luaL_optstring(L, 2, NULL); const auto chn = getApp()->twitch->getChannelOrEmpty(channel); if (chn->isEmpty()) { qCDebug(chatterinoLua) << "system_msg: no channel" << channel; - lua_pushboolean(L, C_FALSE); + lua::push(L, false); return 1; } qCDebug(chatterinoLua) << "system_msg: OK!"; chn->addMessage(makeSystemMessage(text)); - lua_pushboolean(L, C_TRUE); + lua::push(L, true); return 1; } @@ -182,7 +182,13 @@ int luaC2RegisterCommand(lua_State *L) return 0; } - const char *name = luaL_optstring(L, 1, NULL); + QString name; + if (!lua::peek(L, &name, 1)) + { + // NOLINTNEXTLINE + luaL_error(L, "cannot get string (1st arg of register_command)"); + return 0; + } if (lua_isnoneornil(L, 2)) { // NOLINTNEXTLINE @@ -202,22 +208,23 @@ int luaC2RegisterCommand(lua_State *L) } int luaC2SendMsg(lua_State *L) { - const char *channel = luaL_optstring(L, 1, NULL); - const char *text = luaL_optstring(L, 2, NULL); - lua_pop(L, 2); + QString text; + QString channel; + lua::pop(L, &text); + lua::pop(L, &channel); const auto chn = getApp()->twitch->getChannelOrEmpty(channel); if (chn->isEmpty()) { qCDebug(chatterinoLua) << "send_msg: no channel" << channel; - lua_pushboolean(L, C_FALSE); + lua::push(L, false); return 1; } QString message = text; message = message.replace('\n', ' '); QString outText = getApp()->commands->execCommand(message, chn, false); chn->sendMessage(outText); - lua_pushboolean(L, C_TRUE); + lua::push(L, true); return 1; }