From cc1069734cfcb28eee378858456064915c8ceb5d Mon Sep 17 00:00:00 2001 From: Celtic Minstrel Date: Tue, 17 Sep 2024 21:03:24 -0400 Subject: [PATCH] Add a debugging function that prints out the contents of the Lua stack --- src/scripting/lua_common.cpp | 19 ++++++++++++++++++- src/scripting/lua_common.hpp | 4 ++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/scripting/lua_common.cpp b/src/scripting/lua_common.cpp index e83d86c50a9..b5e71bcc279 100644 --- a/src/scripting/lua_common.cpp +++ b/src/scripting/lua_common.cpp @@ -840,8 +840,25 @@ void luaW_pushconfig(lua_State *L, const config& cfg) luaW_filltable(L, cfg); } +luaW_PrintStack luaW_debugstack(lua_State* L) { + return {L}; +} - +std::ostream& operator<<(std::ostream& os, const luaW_PrintStack& s) { + int top = lua_gettop(s.L); + os << "Lua Stack\n"; + for(int i = 1; i <= top; i++) { + luaW_getglobal(s.L, "wesnoth", "as_text"); + lua_pushvalue(s.L, i); + lua_call(s.L, 1, 1); + auto value = luaL_checkstring(s.L, -1); + lua_pop(s.L, 1); + os << '[' << i << ']' << value << '\n'; + } + if(top == 0) os << "(empty)\n"; + os << std::flush; + return os; +} #define return_misformed() \ do { lua_settop(L, initial_top); return false; } while (0) diff --git a/src/scripting/lua_common.hpp b/src/scripting/lua_common.hpp index d70044381c3..cb22dfbd526 100644 --- a/src/scripting/lua_common.hpp +++ b/src/scripting/lua_common.hpp @@ -225,6 +225,10 @@ int luaW_pcall_internal(lua_State *L, int nArgs, int nRets); int luaW_type_error(lua_State *L, int narg, const char *tname); int luaW_type_error(lua_State *L, int narg, const char* kpath, const char *tname); +struct luaW_PrintStack { lua_State* L; }; +luaW_PrintStack luaW_debugstack(lua_State* L); +std::ostream& operator<<(std::ostream& os, const luaW_PrintStack&); + #define deprecate_attrib(name, prefix, level, version, msg) deprecated_message(prefix "." name, DEP_LEVEL::level, version, msg) #define return_deprecated_attrib(type_macro, name, accessor, prefix, level, version, msg) \