Skip to content

Commit

Permalink
[lua] added lua::state::collect_stack() which scrapes the lua stack i…
Browse files Browse the repository at this point in the history
…nto a pretty-printable string. when a lua assert failure happens, the stack is now printed out to help diagnosis
  • Loading branch information
harrand committed Aug 5, 2023
1 parent 663dd4a commit 60c69b8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/tz/lua/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,40 @@ namespace tz::lua
return ret;
}

std::string state::collect_stack() const
{
if(!this->impl_check_stack(3))
{
return "<cant collect - oom>";
}
std::string ret;

auto* s = static_cast<lua_State*>(this->lstate);
int top = lua_gettop(s);
int bottom = 1;
ret = "=== stack (size: " + std::to_string(top - bottom + 1) + ") ===\n";
lua_getglobal(s, "tostring");
for(int i = top; i >= bottom; i--)
{
lua_pushvalue(s, -1);
lua_pushvalue(s, i);
lua_pcall(s, 1, 1, 0);
const char* str = lua_tostring(s, -1);
ret += std::string(">") + std::to_string(i) + ": ";
if(str == nullptr)
{
ret += luaL_typename(s, i) + std::string("\n");
}
else
{
ret += str + std::string("\n");
}
lua_pop(s, 1);
}
lua_pop(s, 1);
return ret + "=== end ===";
}

bool state::impl_check_stack(std::size_t sz) const
{
auto* s = static_cast<lua_State*>(this->lstate);
Expand All @@ -117,10 +151,11 @@ namespace tz::lua
int tz_lua_assert(lua_State* state)
{
bool b = lua_toboolean(state, 1);
std::string stack = lua::state{state}.collect_stack();
lua_Debug ar;
lua_getstack(state, 1, &ar);
lua_getinfo(state, "nSl", &ar);
tz::assert(b, "Lua Assertion: ```lua\n\n%s\n\n```\nOn line %d", ar.source, ar.currentline);
tz::assert(b, "Lua Assertion Failure: ```lua\n\n%s\n\n```\nOn line %d\nStack:\n%s", ar.source, ar.currentline, stack.c_str());
return 0;
}

Expand Down
1 change: 1 addition & 0 deletions src/tz/lua/state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ namespace tz::lua
bool define_int(const char* varname, std::int64_t i) const;
bool define_uint(const char* varname, std::uint64_t u) const;
bool define_func(const char* varname, void* func_ptr) const;
std::string collect_stack() const;
private:
bool impl_check_stack(std::size_t sz) const;
void* lstate = nullptr;
Expand Down

0 comments on commit 60c69b8

Please sign in to comment.