-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Integrate Lua with C++
By :

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.
To introduce the complete definition for upvalues, we need to introduce Lua C closures at the same time. To quote the Lua reference manual:
When a C function is created, it is possible to associate some values with it, thus creating a C closure; these values are called upvalues and are accessible to the function whenever it is called.
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...