From da05115506440d1a0dfb0955bde2d933a7aa0020 Mon Sep 17 00:00:00 2001 From: Mm2PL Date: Wed, 8 Feb 2023 23:57:32 +0100 Subject: [PATCH] Add lua::toString which converts value to a string --- src/controllers/plugins/LuaUtilities.cpp | 7 +++++++ src/controllers/plugins/LuaUtilities.hpp | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/src/controllers/plugins/LuaUtilities.cpp b/src/controllers/plugins/LuaUtilities.cpp index e02170e99..3726f7d63 100644 --- a/src/controllers/plugins/LuaUtilities.cpp +++ b/src/controllers/plugins/LuaUtilities.cpp @@ -146,5 +146,12 @@ bool peek(lua_State *L, std::string *out, StackIdx idx) *out = std::string(str, len); return true; } + +QString toString(lua_State *L, StackIdx idx) +{ + size_t len{}; + const auto *ptr = luaL_tolstring(L, idx, &len); + return QString::fromUtf8(ptr, int(len)); +} } // namespace chatterino::lua #endif diff --git a/src/controllers/plugins/LuaUtilities.hpp b/src/controllers/plugins/LuaUtilities.hpp index 132a1e26e..eb0a8d661 100644 --- a/src/controllers/plugins/LuaUtilities.hpp +++ b/src/controllers/plugins/LuaUtilities.hpp @@ -5,6 +5,7 @@ # include +# include # include struct lua_State; class QJsonObject; @@ -35,6 +36,9 @@ bool peek(lua_State *L, QString *out, StackIdx idx = -1); bool peek(lua_State *L, QByteArray *out, StackIdx idx = -1); bool peek(lua_State *L, std::string *out, StackIdx idx = -1); +// forces conversion of value at idx to a string +QString toString(lua_State *L, StackIdx idx = -1); + /// TEMPLATES template