利用零碎的时间,先把以后用的知识点提前准备好。最近比较忙,正在准备一篇绑定C++对象到Lua中。但是,不想轻易下手,希望做足准备。
这篇翻译来自于lua-users.org ,原文地址。
Light User Data
Light userdata, like heavy userdata, are a form of userdata, which is one of the basic data types in Lua .Light userdata are characterized by the following properties:
1、Light userdata and heavy userdata have deceptively similar names, and both have the property type(x) == ‘userdata‘, but these two forms of userdata are otherwise quite different in behavior
2、A light userdatum represents a single pointer to a physical memory address (void *), which is typically a 32- or 64-bit value depending on platform.
Light userdata are intended to store C pointers in Lua (note: Lua numbers may or may not be suitable for this purpose depending on the data types on the platform).
3、A heavy userdatum represents a region of mutable bytes allocated in Lua‘s memory and managed by Lua (garbage collected).
This is the only memory in Lua that you are permitted to read/write directly from C (via the userdata‘s pointer) without the C API.
4、Light userdata have the semantics of values, while heavy userdata have the semantics of objects.
Objects have a unique identity.Two heavy userdata constructed with the same data will always be distinguishable (e.g. rawequal will differentiate them by memory address);
two light userdata so constructed will never be distinguishable because they are compared by value not by address.
5、Light userdata (unlike heavy userdata) are not garbage collected.
Lua Implementations typically fit each light userdatum in a single register and copy them around by value, while heavy userdata is allocated on the heap and passed around by reference (pointer).
6、Equality: If x is a lightuserdatum, then x == y if-and-only-if y is a light userdatum representing the same pointer.
Light userdata can be used as table keys, in which case x == y implies t[x] and t[y] refer to the same table value (though not necessarily that t[x] == t[y] since NaN ~= NaN).
The __eq metamethod has no effect on light userdata (note: the manual isn‘t clear on this .
7、Light userdata (unlike heavy userdata) have no per-value metatables. All light userdata share the same metatable, which by default is not set (nil).
8、tostring(x) typically displays the pointer in hex notation, although this is specific to Lua Implementations.
Some interesting points:
1、A common technique for mapping C pointers to Lua values is to store a light userdata of that pointer as a key in the registry table.
Bear in mind that these mapping are not automatically garbage collected , and you might want to use a weak table rather than the registry (which by default is not weak, but a weak table can be stored in it).
2、Some people represent handles rather just pointers as lightuserdata.It‘s possible to represent other data in them as well .
3、Take care not to mix pointer and non-pointer userdata in a way that might conflict (e.g. storing both in the registry table).
4、Light userdata are created from the C API via [lua_pushlightuserdata]. Out-of-the-box, Lua doesn‘t provide a way to create lightuserdata from Lua.