Skip to content

Instantly share code, notes, and snippets.

@Qix-
Created September 28, 2021 18:37
Show Gist options
  • Save Qix-/88c5d450fee0192ca69a4c787e01cb75 to your computer and use it in GitHub Desktop.
Save Qix-/88c5d450fee0192ca69a4c787e01cb75 to your computer and use it in GitHub Desktop.
Useful little Lua stack debugger
static const char * typecolor(int type) {
switch(type) {
case LUA_TBOOLEAN: return "\x1b[32m";
case LUA_TSTRING: return "\x1b[31m";
case LUA_TFUNCTION: return "\x1b[33m";
case LUA_TNUMBER: return "\x1b[94m";
case LUA_TLIGHTUSERDATA: return "\x1b[95m";
case LUA_TUSERDATA: return "\x1b[35m";
case LUA_TTABLE: return "\x1b[36m";
case LUA_TTHREAD: return "\x1b[37m";
case LUA_TNIL: return "\x1b[90m";
default: return "\x1b[m";
}
}
static void dump(lua_State *L, const char *msg) {
fprintf(
stderr,
"@@ %s\x1b[20G\x1b[K %s%s\x1b[24G\x1b[K %s%s\x1b[28G\x1b[K %s%s\x1b[32G\x1b[K %s%s\x1b[36G\x1b[K %s%s\x1b[40G\x1b[K\x1b[m\n",
msg,
typecolor(lua_type(L, -1)), lua_typename(L, lua_type(L, -1)),
typecolor(lua_type(L, -2)), lua_typename(L, lua_type(L, -2)),
typecolor(lua_type(L, -3)), lua_typename(L, lua_type(L, -3)),
typecolor(lua_type(L, -4)), lua_typename(L, lua_type(L, -4)),
typecolor(lua_type(L, -5)), lua_typename(L, lua_type(L, -5))
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment