Skip to content

Instantly share code, notes, and snippets.

@nagolove
Created February 24, 2025 15:31
Show Gist options
  • Save nagolove/ff87082d020c1e5620b15c9c44cb0c72 to your computer and use it in GitHub Desktop.
Save nagolove/ff87082d020c1e5620b15c9c44cb0c72 to your computer and use it in GitHub Desktop.
const char *L_stack_dump(lua_State *lua) {
if (!lua)
return NULL;
// TODO: Проверка на вместимость во внутреннего буфера
static char ret[1024] = {0, };
memset(ret, 0, sizeof(ret));
char *ptr = ret;
int top = lua_gettop(lua);
for (int i = 1; i <= top; i++) {
int t = lua_type(lua, i);
int max_chars = sizeof(ret) - (ptr - ret) - 1; // -1 is random value
switch (t) {
case LUA_TUSERDATA:
ptr += snprintf(
ptr, max_chars, "userdata %p", lua_topointer(lua, i)
);
break;
case LUA_TSTRING:
ptr += snprintf(ptr, max_chars, "’%s’", lua_tostring(lua, i));
break;
case LUA_TBOOLEAN:
ptr += snprintf(ptr, max_chars, lua_toboolean(lua, i) ? "true" : "false");
break;
case LUA_TNUMBER:
ptr += snprintf(ptr, max_chars, "%g", lua_tonumber(lua, i));
break;
default:
ptr += snprintf(ptr, max_chars, "%s", lua_typename(lua, t));
break;
}
ptr += snprintf(ptr, max_chars, " ");
}
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment