This answer is a slightly edited version of the answer provided by @lhf in the comments.
It has the advantage that it does not modify any values in the stack, and does not require any additional space.
static void dumpstack (lua_State *L) {
int top=lua_gettop(L);
for (int i=1; i <= top; i++) {
printf("%d\t%s\t", i, luaL_typename(L,i));
switch (lua_type(L, i)) {
case LUA_TNUMBER:
printf("%g\n",lua_tonumber(L,i));
break;
case LUA_TSTRING:
printf("%s\n",lua_tostring(L,i));
break;
case LUA_TBOOLEAN:
printf("%s\n", (lua_toboolean(L, i) ? "true" : "false"));
break;
case LUA_TNIL:
printf("%s\n", "nil");
break;
default:
printf("%p\n",lua_topointer(L,i));
break;
}
}
}
If you want, you can also use lua_isinteger(L, i)
inside the LUA_TNUMBER case in order to distinguish between integers and floating-point numbers.
1
toget_top(L)
– Homeless