Add stackDump lua utility, for debugging or tests

This commit is contained in:
Mm2PL 2023-02-11 22:48:49 +01:00
parent 5b9f3e95ee
commit 3625f5706b
2 changed files with 45 additions and 0 deletions

View file

@ -2,6 +2,7 @@
#ifdef CHATTERINO_HAVE_PLUGINS
# include "common/Channel.hpp"
# include "common/QLogging.hpp"
# include "controllers/commands/CommandContext.hpp"
# include "lauxlib.h"
# include "lua.h"
@ -11,6 +12,43 @@
namespace chatterino::lua {
void stackDump(lua_State *L, const QString &tag)
{
qCDebug(chatterinoLua) << "--------------------";
auto count = lua_gettop(L);
if (!tag.isEmpty())
{
qCDebug(chatterinoLua) << "Tag: " << tag;
}
qCDebug(chatterinoLua) << "Count elems: " << count;
for (int i = 1; i <= count; i++)
{
auto typeint = lua_type(L, i);
if (typeint == LUA_TSTRING)
{
QString str;
lua::peek(L, &str, i);
qCDebug(chatterinoLua)
<< "At" << i << "is a" << lua_typename(L, typeint) << "("
<< typeint << "): " << str;
}
else if (typeint == LUA_TTABLE)
{
qCDebug(chatterinoLua)
<< "At" << i << "is a" << lua_typename(L, typeint) << "("
<< typeint << ")"
<< "its length is " << lua_rawlen(L, i);
}
else
{
qCDebug(chatterinoLua)
<< "At" << i << "is a" << lua_typename(L, typeint) << "("
<< typeint << ")";
}
}
qCDebug(chatterinoLua) << "--------------------";
}
QString humanErrorText(lua_State *L, int errCode)
{
QString errName;

View file

@ -19,6 +19,13 @@ struct CommandContext;
namespace chatterino::lua {
/**
* @brief Dumps the Lua stack into qCDebug(chatterinoLua)
*
* @param tag is a string to let you know which dump is which when browsing logs
*/
void stackDump(lua_State *L, const QString &tag);
/**
* @brief Converts a lua error code and potentially string on top of the stack into a human readable message
*/