Lua 與C/C++ 互動系列:Light userdata翻譯

來源:互聯網
上載者:User

標籤:c++   lua   註冊c函數   調用c函數   light userdata   

利用零碎的時間,先把以後用的知識點提前準備好。最近比較忙,正在準備一篇綁定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.
 
Light userdata 和full userdata 有些類似,是一種userdata. light userdata在Lua中是一種基礎資料型別 (Elementary Data Type)。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翻譯

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.