feat(plugins): add debug.traceback (#6652)
Reviewed-by: Mm2PL <mm2pl+gh@kotmisia.pl>
This commit is contained in:
@@ -254,6 +254,8 @@ set(SOURCE_FILES
|
||||
controllers/plugins/api/Accounts.hpp
|
||||
controllers/plugins/api/ChannelRef.cpp
|
||||
controllers/plugins/api/ChannelRef.hpp
|
||||
controllers/plugins/api/DebugLibrary.cpp
|
||||
controllers/plugins/api/DebugLibrary.hpp
|
||||
controllers/plugins/api/EventType.hpp
|
||||
controllers/plugins/api/HTTPRequest.cpp
|
||||
controllers/plugins/api/HTTPRequest.hpp
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user