Lua 与C/C++ 交互系列:Light userdata翻译

利用零碎的时间,先把以后用的知识点提前准备好。最近比较忙,正在准备一篇绑定C++对象到Lua中。但是,不想轻易下手,希望做足准备。这篇翻译来自于lua-users.org ,原文地址。Light User DataLight 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 behavior2、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.Light userdata 和full userdata 有些类似,是一种userdata. light userdata在Lua中是一种基本数据类型。Light userdata具有下列特性:1、Light userdata 和Full userdata 具有一样的名称. 两种type(x)都是"userdata",但是两种userdata的行为上完全不同。2、Light userdata 表示指向物理内存的指针。(Void*)类型指针. 指针长度根据不同的平台是32bit 或者64bit.Light userdata 主要目的是用来在Lua Code中存储C语言指针.(注:Lua Number 不适合在不同的平台来存储C Point)3、Full userdata表示一个由Lua 分配与Lua GC管理的可变长度内存地址。 这是可以通过C语言在Lua Code中允许分配内存的唯一途径。4、Light userdata是值类型,Full userdata是对象类型,每一个对象都是独一无二的。两个有相同数据的Full userdata是不同的。rawequal 将从内存地址来区别Full userdata.Light userdata是通过值比较而不是通过地址比较。5、Light userdata不能被GC垃圾回收。但是Full userdata可以被GC垃圾收集器收集。Lua 实现Light userdata通过拷贝来传递一个值,然而,Full userdata是一个分配在堆上面 ,通过指针(引用)来传递Full userdata.6、相等性:如果 x是Light userdata, 如果 x==y 为true时,只有x和y 是一种Light userdata同时指向同一个指针。Light userdata可以作为table的键, t[x]==t[y]意味着指向相同的值。7、Light userdata 是所有Light userdata共享一个metatable,通常默认为nil,而Full userdata是每一个值都有一个独立的metatable。8、tostring()方法通过16进制来展示Light userdata指向的地址。常见观点:1、通常映射C Points到Lua Code Value中是在注册表中把指向指针的Light userdata作为键。需要注意的是这种映射不会被GC 自动回收,可以使用weak table 弱引用Table来代替注册表。2、 一些人仅仅认为Light userdata仅仅能够表示指针。但是,也可以使用Light userdata表示其他类型值。3、 不要混合使用Light userdata和Full userdata,,可以容易引起混淆。4、在C API中通过lua_pushlightuserdata 创建Light userdata,但是在Lua Code中没有提供任何创建Light userdata的形式。

没有了爱的语言,所有的文字都是乏味的

Lua 与C/C++ 交互系列:Light userdata翻译

相关文章:

你感兴趣的文章:

标签云: