Created
December 4, 2025 22:05
-
-
Save artfwo/6a5e333a913783fae88d764f783e24c1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #include <cstdio> | |
| #include <lua.hpp> | |
| static void dump_table(lua_State *L, int index, int level); | |
| static void dump_value(lua_State *L, int index, int level) { | |
| int value_type = lua_type(L, index); | |
| switch (value_type) { | |
| case LUA_TNIL: | |
| printf("nil"); | |
| break; | |
| case LUA_TBOOLEAN: | |
| printf("%s", lua_toboolean(L, index) ? "true" : "false"); | |
| break; | |
| case LUA_TNUMBER: | |
| if (lua_isinteger(L, index)) { | |
| printf("%lld", lua_tointeger(L, index)); | |
| } else { | |
| printf("%f", lua_tonumber(L, index)); | |
| } | |
| break; | |
| case LUA_TSTRING: | |
| printf("\"%s\"", lua_tostring(L, index)); | |
| break; | |
| case LUA_TTABLE: | |
| dump_table(L, index, level); | |
| break; | |
| default: | |
| // LUA_TFUNCTION | |
| // LUA_TUSERDATA | |
| // LUA_TLIGHTUSERDATA | |
| // LUA_TTHREAD | |
| printf("%p", lua_topointer(L, index)); | |
| break; | |
| } | |
| } | |
| static bool is_empty(lua_State *L, int index) | |
| { | |
| int abs_index = lua_absindex(L, index); | |
| lua_pushnil(L); | |
| if (lua_next(L, abs_index) != 0) { | |
| lua_pop(L, 2); | |
| return false; | |
| } | |
| return true; | |
| } | |
| static void dump_indent(int level) { | |
| for (int i = 0; i < level + 1; ++i) { | |
| printf(" "); | |
| } | |
| } | |
| static void dump_table(lua_State *L, int index, int level) { | |
| int abs_index = lua_absindex(L, index); | |
| if (is_empty(L, abs_index)) { | |
| printf("{}"); | |
| return; | |
| } | |
| printf("{\n"); | |
| lua_pushnil(L); // first key | |
| while (lua_next(L, abs_index) != 0) { | |
| dump_indent(level); | |
| dump_value(L, -2, level + 1); | |
| printf(" = "); | |
| dump_value(L, -1, level + 1); | |
| printf(",\n"); | |
| lua_pop(L, 1); // remove value, keep key for next iteration | |
| } | |
| dump_indent(level - 1); | |
| printf("}"); | |
| } | |
| void dump_lua_stack(lua_State* L) { | |
| int top = lua_gettop(L); | |
| printf("==== Lua Stack (top = %d) ====\n", top); | |
| if (top == 0) { | |
| printf(" (empty)\n"); | |
| return; | |
| } | |
| for (int i = 1; i <= top; ++i) { | |
| int type = lua_type(L, i); | |
| const char* type_name = lua_typename(L, type); | |
| printf("[%d] [%d] %s ", i, i - top - 1, type_name); | |
| dump_value(L, i, 0); | |
| printf("\n"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment