Userdata
The Lua userdata can be categorized into light userdata and full userdata.
It is important to note that they are different things. In the Lua library, conventionally, light userdata is named lightuserdata, while full userdata is named userdata.
Light userdata
Light userdata represents a C/C++ pointer. It is a value type and the value is passed around. You push a pointer in C/C++ code onto the stack with lua_pushlightuserdata
. You cannot create light userdata with the Lua library.
Full userdata
Full userdata is a raw memory area allocated by the Lua library with a call to lua_newuserdatauv
. It is an object type and only its reference is passed around.
Because full userdata is created by Lua in the heap, Lua garbage collection comes into the picture. On the C++ side, you can provide a finalizer by providing the __gc
metamethod.
For a complete example of how to utilize full userdata to access C++ objects in Lua, check LuaModuleExporter
.