I have a problem using Lua C API. When pcall (C API function) fail, the error is pushed on the stack.
lua_tostring shows an error on the stack but lua_gettop shows says the stack is empty.
#include <lua5.2/lauxlib.h>
#include <lua5.2/lua.h>
#include <lua5.2/lualib.h>
int main()
{
lua_State *L = luaL_newstate();
lua_pcall(L, 0, 0, 0);
printf("%d\n", lua_gettop(L)); // outputs 0, indicating empty stack
printf("%s\n", lua_tostring(L, -1)); // outputs "attempt to call a nil value", indicating non-empty stack
}
Compile with: gcc main.c `pkg-config --cflags lua5.2` `pkg-config --libs lua5.2`
This program display: 0 attempt to call a nil value
lua_gettop(L) return the stack size. Here I get 0. How can I get a string from an empty stack ?
The behavior is the same with the 5.1 version.