Add a StackIdx type alias

This commit is contained in:
Mm2PL 2023-01-30 22:31:42 +01:00
parent 082420b94a
commit ca0bfb1900
No known key found for this signature in database
GPG key ID: 94AC9B80EFA15ED9
2 changed files with 18 additions and 15 deletions

View file

@ -9,25 +9,26 @@
#include <cstdlib> #include <cstdlib>
namespace chatterino::lua { namespace chatterino::lua {
int pushEmptyArray(lua_State *L, int countArray)
StackIdx pushEmptyArray(lua_State *L, int countArray)
{ {
lua_createtable(L, countArray, 0); lua_createtable(L, countArray, 0);
return lua_gettop(L); return lua_gettop(L);
} }
int pushEmptyTable(lua_State *L, int countProperties) StackIdx pushEmptyTable(lua_State *L, int countProperties)
{ {
lua_createtable(L, 0, countProperties); lua_createtable(L, 0, countProperties);
return lua_gettop(L); 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()); lua_pushstring(L, str.toStdString().c_str());
return lua_gettop(L); 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); auto outIdx = pushEmptyTable(L, 2);
@ -39,13 +40,13 @@ int push(lua_State *L, const CommandContext &ctx)
return outIdx; return outIdx;
} }
int push(lua_State *L, const bool &b) StackIdx push(lua_State *L, const bool &b)
{ {
lua_pushboolean(L, int(b)); lua_pushboolean(L, int(b));
return lua_gettop(L); 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}; int ok{0};
auto v = lua_tonumberx(L, idx, &ok); auto v = lua_tonumberx(L, idx, &ok);
@ -56,7 +57,7 @@ bool peek(lua_State *L, double *out, int idx)
return ok != 0; 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}; size_t len{0};
const char *str = lua_tolstring(L, idx, &len); const char *str = lua_tolstring(L, idx, &len);

View file

@ -12,16 +12,18 @@ class CommandContext;
namespace chatterino::lua { namespace chatterino::lua {
int pushEmptyArray(lua_State *L, int countArray); using StackIdx = int;
int pushEmptyTable(lua_State *L, int countProperties);
int push(lua_State *L, const CommandContext &ctx); StackIdx pushEmptyArray(lua_State *L, int countArray);
int push(lua_State *L, const QString &str); StackIdx pushEmptyTable(lua_State *L, int countProperties);
int push(lua_State *L, const bool &b);
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? // returns OK?
bool peek(lua_State *L, double *out, int idx = -1); bool peek(lua_State *L, double *out, StackIdx idx = -1);
bool peek(lua_State *L, QString *out, int idx = -1); bool peek(lua_State *L, QString *out, StackIdx idx = -1);
/// TEMPLATES /// TEMPLATES
@ -54,7 +56,7 @@ int push(lua_State *L, QList<T> vec)
} }
template <typename T> template <typename T>
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); auto ok = peek(L, out, idx);
if (ok) if (ok)