Storing state in Lua
There are two ways to store state in Lua for lua_CFunction
: upvalues and the registry. Let’s recap them and dig deeper into upvalues.
Upvalues
To introduce the complete definition for upvalues, we need to introduce Lua C closures at the same time. To quote the Lua reference manual:
To put it simply, the closure is still our old friend lua_CFunction
. When you associate some values with it, it becomes a closure, and the values become upvalues.
It is important to note that Lua C closures and upvalues are inseparable.
To create a closure, use the following library function:
void lua_pushcclosure( lua_State *L, lua_CFunction fn, int n);
This creates a closure from lua_CFunction
and associates n
values with it.
To see...