The stack
The Lua stack can serve two purposes:
- Exchange data between C++ and Lua. Passing function arguments and retrieving function return values fit into this usage.
- Keep intermediate results. For example, we can keep a table reference in the stack until we are done with the table; we can push some values onto the stack and then pop and use them as upvalues.
The Lua stack comes in two forms:
- The public stack that comes with the Lua state. Once a Lua state is created via
luaL_newstate
orlua_newstate
, you can pass the state around and the same Lua stack is accessible to all functions that can access the Lua state. - The private stack for each
lua_CFunction
call. The stack is only accessible to a function call. Calling the samelua_CFunction
multiple times will not share the same stack. So, the stack that is passed to alua_CFunction
call is private to the function call.
Pushing onto the stack
You can use lua_pushXXX
functions to push a value...