From ca0bfb1900336397634b303ce18efca9e00d0566 Mon Sep 17 00:00:00 2001 From: Mm2PL Date: Mon, 30 Jan 2023 22:31:42 +0100 Subject: [PATCH] Add a StackIdx type alias --- src/controllers/plugins/LuaUtilities.cpp | 15 ++++++++------- src/controllers/plugins/LuaUtilities.hpp | 18 ++++++++++-------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/controllers/plugins/LuaUtilities.cpp b/src/controllers/plugins/LuaUtilities.cpp index f230b847d..1f493b95d 100644 --- a/src/controllers/plugins/LuaUtilities.cpp +++ b/src/controllers/plugins/LuaUtilities.cpp @@ -9,25 +9,26 @@ #include namespace chatterino::lua { -int pushEmptyArray(lua_State *L, int countArray) + +StackIdx pushEmptyArray(lua_State *L, int countArray) { lua_createtable(L, countArray, 0); return lua_gettop(L); } -int pushEmptyTable(lua_State *L, int countProperties) +StackIdx pushEmptyTable(lua_State *L, int countProperties) { lua_createtable(L, 0, countProperties); return lua_gettop(L); } -int push(lua_State *L, const QString &str) +StackIdx 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) +StackIdx push(lua_State *L, const CommandContext &ctx) { auto outIdx = pushEmptyTable(L, 2); @@ -39,13 +40,13 @@ int push(lua_State *L, const CommandContext &ctx) return outIdx; } -int push(lua_State *L, const bool &b) +StackIdx 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) +bool peek(lua_State *L, double *out, StackIdx idx) { int ok{0}; auto v = lua_tonumberx(L, idx, &ok); @@ -56,7 +57,7 @@ bool peek(lua_State *L, double *out, int idx) return ok != 0; } -bool peek(lua_State *L, QString *out, int idx) +bool peek(lua_State *L, QString *out, StackIdx idx) { size_t len{0}; const char *str = lua_tolstring(L, idx, &len); diff --git a/src/controllers/plugins/LuaUtilities.hpp b/src/controllers/plugins/LuaUtilities.hpp index c73c1f453..4dde4a432 100644 --- a/src/controllers/plugins/LuaUtilities.hpp +++ b/src/controllers/plugins/LuaUtilities.hpp @@ -12,16 +12,18 @@ class CommandContext; namespace chatterino::lua { -int pushEmptyArray(lua_State *L, int countArray); -int pushEmptyTable(lua_State *L, int countProperties); +using StackIdx = int; -int push(lua_State *L, const CommandContext &ctx); -int push(lua_State *L, const QString &str); -int push(lua_State *L, const bool &b); +StackIdx pushEmptyArray(lua_State *L, int countArray); +StackIdx pushEmptyTable(lua_State *L, int countProperties); + +StackIdx push(lua_State *L, const CommandContext &ctx); +StackIdx push(lua_State *L, const QString &str); +StackIdx 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); +bool peek(lua_State *L, double *out, StackIdx idx = -1); +bool peek(lua_State *L, QString *out, StackIdx idx = -1); /// TEMPLATES @@ -54,7 +56,7 @@ int push(lua_State *L, QList vec) } template -bool pop(lua_State *L, T *out, int idx = -1) +bool pop(lua_State *L, T *out, StackIdx idx = -1) { auto ok = peek(L, out, idx); if (ok)