From 24c60030434abdecb22f4f69e6af1f418b8bea96 Mon Sep 17 00:00:00 2001 From: Mm2PL Date: Sat, 4 Feb 2023 01:20:41 +0100 Subject: [PATCH] Add lua::peek for QByteArray --- src/controllers/plugins/LuaUtilities.cpp | 16 ++++++++++++++++ src/controllers/plugins/LuaUtilities.hpp | 1 + 2 files changed, 17 insertions(+) diff --git a/src/controllers/plugins/LuaUtilities.cpp b/src/controllers/plugins/LuaUtilities.cpp index 0440ffe9a..de5bb75af 100644 --- a/src/controllers/plugins/LuaUtilities.cpp +++ b/src/controllers/plugins/LuaUtilities.cpp @@ -109,5 +109,21 @@ bool peek(lua_State *L, QString *out, StackIdx idx) *out = QString::fromUtf8(str, int(len)); return true; } + +bool peek(lua_State *L, QByteArray *out, StackIdx 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 = QByteArray(str, int(len)); + return true; +} } // namespace chatterino::lua #endif diff --git a/src/controllers/plugins/LuaUtilities.hpp b/src/controllers/plugins/LuaUtilities.hpp index 5530a7f59..fb8ceb17b 100644 --- a/src/controllers/plugins/LuaUtilities.hpp +++ b/src/controllers/plugins/LuaUtilities.hpp @@ -31,6 +31,7 @@ StackIdx push(lua_State *L, const bool &b); // returns OK? bool peek(lua_State *L, double *out, StackIdx idx = -1); bool peek(lua_State *L, QString *out, StackIdx idx = -1); +bool peek(lua_State *L, QByteArray *out, StackIdx idx = -1); /// TEMPLATES