Created
November 18, 2022 12:58
-
-
Save MikuAuahDark/d6643dca160564da5c8df32edcb3238b to your computer and use it in GitHub Desktop.
Lua Stack Inspector with Relative Indices
This file contains 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
extern "C" | |
{ | |
#include <lua.h> | |
#include <lauxlib.h> | |
} | |
#include <cstdio> | |
#include <sstream> | |
static std::string luax_checkstring(lua_State *L, int i) | |
{ | |
size_t size; | |
const char *str = luaL_checklstring(L, i, &size); | |
return std::string(str, size); | |
} | |
void inspect(lua_State *L) | |
{ | |
std::stringstream ss; | |
int top = lua_gettop(L); | |
for (int i = 1; i <= top; i++) | |
{ | |
int type = lua_type(L, i); | |
ss << (i - top - 1) << ". "; | |
switch (type) | |
{ | |
case LUA_TNONE: | |
case LUA_TNIL: | |
ss << "LUA_TNIL"; | |
break; | |
case LUA_TBOOLEAN: | |
ss << "LUA_TBOOLEAN " << bool(lua_toboolean(L, i)); | |
break; | |
case LUA_TLIGHTUSERDATA: | |
ss << "LUA_TLIGHTUSERDATA " << lua_topointer(L, i); | |
break; | |
case LUA_TNUMBER: | |
ss << "LUA_TNUMBER " << lua_tonumber(L, i); | |
break; | |
case LUA_TSTRING: | |
ss << "LUA_TSTRING " << luax_checkstring(L, i); | |
break; | |
case LUA_TTABLE: | |
ss << "LUA_TTABLE " << lua_topointer(L, i); | |
break; | |
case LUA_TFUNCTION: | |
ss << "LUA_TFUNCTION " << lua_topointer(L, i); | |
break; | |
case LUA_TUSERDATA: | |
ss << "LUA_TUSERDATA " << lua_topointer(L, i); | |
break; | |
case LUA_TTHREAD: | |
ss << "LUA_TTHREAD " << lua_topointer(L, i); | |
break; | |
default: | |
ss << "LUA_TUNKNOWN(" << type << ")"; | |
break; | |
} | |
ss << "\n"; | |
} | |
std::string result = ss.str(); | |
fwrite(result.data(), 1, result.length(), stdout); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment