Add lua::{peek,push} support for std::string

This commit is contained in:
Mm2PL 2023-02-08 23:56:27 +01:00
parent 0a9fa2fe09
commit d945663162
No known key found for this signature in database
GPG key ID: 94AC9B80EFA15ED9
2 changed files with 24 additions and 1 deletions

View file

@ -61,7 +61,12 @@ StackIdx pushEmptyTable(lua_State *L, int countProperties)
StackIdx push(lua_State *L, const QString &str) StackIdx push(lua_State *L, const QString &str)
{ {
lua_pushstring(L, str.toStdString().c_str()); return lua::push(L, str.toStdString());
}
StackIdx push(lua_State *L, const std::string &str)
{
lua_pushstring(L, str.c_str());
return lua_gettop(L); return lua_gettop(L);
} }
@ -125,5 +130,21 @@ bool peek(lua_State *L, QByteArray *out, StackIdx idx)
*out = QByteArray(str, int(len)); *out = QByteArray(str, int(len));
return true; return true;
} }
bool peek(lua_State *L, std::string *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 = std::string(str, len);
return true;
}
} // namespace chatterino::lua } // namespace chatterino::lua
#endif #endif

View file

@ -26,12 +26,14 @@ StackIdx pushEmptyTable(lua_State *L, int countProperties);
StackIdx push(lua_State *L, const CommandContext &ctx); StackIdx push(lua_State *L, const CommandContext &ctx);
StackIdx push(lua_State *L, const QString &str); StackIdx push(lua_State *L, const QString &str);
StackIdx push(lua_State *L, const std::string &str);
StackIdx push(lua_State *L, const bool &b); StackIdx push(lua_State *L, const bool &b);
// returns OK? // returns OK?
bool peek(lua_State *L, double *out, StackIdx idx = -1); 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, QString *out, StackIdx idx = -1);
bool peek(lua_State *L, QByteArray *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);
/// TEMPLATES /// TEMPLATES