feat(plugins): add debug.traceback (#6652)

Reviewed-by: Mm2PL <mm2pl+gh@kotmisia.pl>
This commit is contained in:
nerix
2025-12-28 13:03:43 +01:00
committed by GitHub
parent a9ab584cbb
commit e2ba7dff03
7 changed files with 178 additions and 22 deletions
@@ -9,6 +9,7 @@
# include "controllers/commands/CommandController.hpp"
# include "controllers/plugins/api/Accounts.hpp"
# include "controllers/plugins/api/ChannelRef.hpp"
# include "controllers/plugins/api/DebugLibrary.hpp"
# include "controllers/plugins/api/HTTPRequest.hpp"
# include "controllers/plugins/api/HTTPResponse.hpp"
# include "controllers/plugins/api/IOWrapper.hpp"
@@ -207,6 +208,13 @@ void PluginController::openLibrariesFor(Plugin *plugin)
r["_IO_input"] = sol::nil;
r["_IO_output"] = sol::nil;
}
// set up debug lib
{
auto debuglib = lua.create_table();
g["debug"] = debuglib;
debuglib.set_function("traceback", lua::api::debugTraceback);
}
PluginController::initSol(lua, plugin);
}
@@ -0,0 +1,46 @@
#include "controllers/plugins/api/DebugLibrary.hpp"
#ifdef CHATTERINO_HAVE_PLUGINS
# include <sol/sol.hpp>
namespace chatterino::lua::api {
/// Signature in Lua: ([thread,] [message [, level]])
int debugTraceback(lua_State *L)
{
int argOffset = 1;
lua_State *targetThread = L;
// If the first argument is a thread, take that as our target
if (lua_isthread(L, argOffset))
{
targetThread = lua_tothread(L, argOffset);
argOffset += 1;
}
// The message is an optional string. If it's not a string/number/nil, we
// return the message as-is and don't create a traceback.
const char *msg = lua_tostring(L, argOffset);
if (!msg && !lua_isnoneornil(L, argOffset))
{
lua_pushvalue(L, argOffset);
return 1;
}
argOffset += 1;
int defaultLevel = 0;
if (L == targetThread)
{
defaultLevel = 1;
}
int level = static_cast<int>(luaL_optinteger(L, argOffset, defaultLevel));
// push a traceback with `msg` at the start
luaL_traceback(L, targetThread, msg, level);
return 1;
}
} // namespace chatterino::lua::api
#endif
@@ -0,0 +1,14 @@
#pragma once
#ifdef CHATTERINO_HAVE_PLUGINS
# include <sol/forward.hpp>
namespace chatterino::lua::api {
/// A reimplementation of Lua's debug.traceback (ldblib.c).
/// Creates a traceback of a thread.
/// It's intended to be used as a message handler in `xpcall`.
/// See https://www.lua.org/manual/5.4/manual.html#pdf-debug.traceback
int debugTraceback(lua_State *L);
} // namespace chatterino::lua::api
#endif