Understanding the Lua stack
The Lua stack is used between C/C++ code and Lua code so that they can communicate with each other. Both C++ code and Lua code can operate on this stack either explicitly or implicitly.
We have seen some Lua library functions reading from and writing to the stack. For example, luaL_loadstring
can push a compiled chuck onto the stack, and lua_pcall
pops the chunk from the stack. Let us learn some explicit ways to operate on the stack.
Pushing elements
The Lua library provides functions to push different types of values onto the stack:
void lua_pushnil (lua_State *L); void lua_pushboolean (lua_State *L, int bool); void lua_pushnumber (lua_State *L, lua_Number n); void lua_pushinteger (lua_State *L, lua_Integer n); void lua_pushstring (lua_State *L, const char *s);
There are more lua_pushX
functions but the ones shown above are the basic ones. lua_Number
is a type alias most likely for either...