Learning Lua: 7 - userdata

1. Userdata basic

"There are eight basic types in Lua: nil, boolean, number, string, userdata, function, thread, and table.

The type() function gives the type name of any given value. " Ref[1]

"The userdata type allows arbitrary C data to be stored in Lua variables. It has no predefined operations in Lua, except

assignment and equality test. Userdata are used to represent new types created by an application program or alibrary

written in C " Ref[1] - 2.7 Userdata and Threads p18

"Lua compares tables and userdata by reference, that is, two such values are considered equal only if they are the very

same object. " Ref[1] - 3.2 Relational Operations p22

2. Userdata and Metatable

"Tables and userdata have individual metatables; values of other types share one single metatable for all values of

that type. " Ref[1] - 13 Metatables and Metamethods p127

"The usual method to distinguish one type of userdata from other userdata is to create a unique metatable for that type.

Everytime we create a userdata, we mark it with the corresponding metatable; everytime we get a userdata, we check

whether it has the right metatable. " Ref[1] - 29.2 Metatables p296

3. Lua C API for userdata

Lua C API 中提供的针对userdata的接口有:

C API 接口说明
lua_isuserdata
int lua_isuserdata (lua_State *L, int index);

Returns 1 if the value at the given acceptable index is a userdata (either full or light), and 0 otherwise.

 LUA_API void *lua_touserdata (lua_State *L, int idx) {
StkId o = index2adr(L, idx);
switch (ttype(o)) {
case LUA_TUSERDATA: return (rawuvalue(o) + );
case LUA_TLIGHTUSERDATA: return pvalue(o);
default: return NULL;
}
}
lua_touserdata
void *lua_touserdata (lua_State *L, int index);

If the value at the given acceptable index is a full userdata, returns its block address. If the value is a light userdata,

returns its pointer. Otherwise, returns NULL.

lua_pushlightuserdata
void lua_pushlightuserdata (lua_State *L, void *p);

Pushes a light userdata onto the stack.

Userdata represent C values in Lua. A light userdata represents a pointer. It is a value (like a number): you do not create it,

it has no individual metatable, and it is not collected (as it was never created). A light userdata is equal to "any" light

userdata with the same C address.

lua_newuserdata
void *lua_newuserdata (lua_State *L, size_t size);

This function allocates a new block of memory with the given size, pushes onto the stack a new full userdata with the block address,

and returns this address.

Userdata represent C values in Lua. A full userdata represents a block of memory. It is an object (like a table): you must create it,

it can have its own metatable, and you can detect when it is being collected. A full userdata is only equal to itself (under raw equality).

When Lua collects a full userdata with a gc metamethod, Lua calls the metamethod and marks the userdata as finalized. When this userdata

is collected again then Lua frees its corresponding memory.

luaL_checkudata

void *luaL_checkudata (lua_State *L, int narg, const char *tname);

Checks whether the function argument narg is a userdata of the type tname

4. full userdata and light userdata

"A light userdatum is a value that represents a C pointer (that is, a value). A light userdata is a value, not an object;

we do not create them " Ref[1] 29.5 Light Userdata p301

"Light userdata are not buffers, but single pointers. They have no metatables. Like numbers, light userdata are not

managed by the garbage collector. " Ref[1] 29.5 Light Userdata p302

"The real use of light userdata comes from equality. As a full userdata is an object, it is only equal to itself.

A light userdata, on the other hand, represents a C pointer value. As such, it is equal to any userdata that represents

the same pointer. Therefore, we can use light userdata to find C objects inside Lua. "

"Another typical scenario is the need to retrieve a full userdata given its C address. "

Ref[1] 29.5 Light Userdata p303

"We can not store a Lua table inside a userdatum(or inside any C structure), but Lua allows each userdata to

have a user value, which can be any Lua table, associated to it." Ref[1] 30.2 An XML Parser p311

other: thread function table


Reference

1. <<Programming in Lua>> 3rd Edition

Lua.LearningLua.7-userdata的更多相关文章

  1. Lua中的userdata

    [话从这里说起] 在我发表<Lua中的类型与值>这篇文章时,就有读者给我留言了,说:你应该好好总结一下Lua中的function和userdata类型.现在是时候总结了.对于functio ...

  2. lua笔记之userdata

    1.一直使用框架里封装好的c库,想着自己一点一点的写些例子,学习下,以后需要c库,可以自己写了. 下边是一个简单的userdata的例子--数组操作. newarray.c #include &quo ...

  3. Lua的Full UserData、Light UserData和metatable

    http://lua.2524044.n2.nabble.com/LightUserData-and-metatables-td3807698.html https://www.lua.org/man ...

  4. Lua.LearningLua.5-document-for-luaL_findtable-function

    Learning Lua: 5 - Document for luaL_findtable() function 文档中没有找到luaL_findtable()函数的说明,这里尝试补充. LUALIB ...

  5. Lua 与 C 交互之UserData(4)

    lua作为脚本于要能够使用宿主语言的类型,不管是宿主基本的或者扩展的类型结构,所以Lua提供的UserData来满足扩展的需求.在Lua中使用宿主语言的类型至少要考虑到几个方面: 数据内存 生命周期 ...

  6. lua 基础 2 类型和值

    -- 类型 和 值--[[ 8中类型 滚动类nil.boolean. number.string.userdata.function.thread 和 table.]] print (type(&qu ...

  7. 《The Evolution of Lua》读书笔记 1

    lua的优点: 可移植性 容易嵌入 体积小 高效率 这些优点都来自于lua的设计目标:简洁.从Scheme获得了很多灵感,包括匿名函数,合理的语义域概念   lua前身: 巴西被禁运,引入计算机软件和 ...

  8. lua中的table、stack和registery

    ok,前面准备给一个dll写wrapper,写了篇日志,看似写的比较明白了,但是其实有很多米有弄明白的.比如PIL中使用的element,key,tname,field这些,还是比较容易混淆的.今天正 ...

  9. C++混合编程之idlcpp教程Lua篇(3)

    上一篇 C++混合编程之idlcpp教程Lua篇(2) 是一个 hello world 的例子,仅仅涉及了静态函数的调用.这一篇会有新的内容. 与LuaTutorial0相似,工程LuaTutoria ...

随机推荐

  1. angular+bootstrap+MVC 之二,模态窗

    本例实现一个bootstrap的模态窗 1.HTML代码 <!doctype html> <!--suppress ALL --> <html ng-app=" ...

  2. Linxu IO测试软件

    fio 安装 apt-get install fio fdisk -l Device Boot Start End Blocks Id System/dev/sda1 * 2048 968390655 ...

  3. ZooKeeper程序员指南(转)

    译自http://zookeeper.apache.org/doc/trunk/zookeeperProgrammers.html 1 简介 本文是为想要创建使用ZooKeeper协调服务优势的分布式 ...

  4. 【javascript基础】5、创建对象

    前言 今天从家里回到了学校,在家呆了十天,胖了几斤的重量,又折腾回学校了,春节回家真是艰辛的路途.随便扯扯我的往返行程:为了省钱我没有选择直飞到长春往返都是到北京转的,这样我和女朋友可以节省4000块 ...

  5. 在IIS8添加WCF服务支持

    最近在做Silverlight,Windows Phone应用移植到Windows 8平台,在IIS8中测试一些传统WCF服务应用,发现IIS8不支持WCF服务svc请求,后来发现IIS8缺少对WCF ...

  6. 新书发布《大数据时代的IT架构设计》

    <大数据时代的IT架构设计>以大数据时代为背景,邀请著名企业中的一线架构师,结合工作中的实际案例展开与架构相关的讨论.<大数据时代的IT架构设计>作者来自互联网.教育.传统行业 ...

  7. Linux:远程到linux的图形界面

    一般linux都没有安装图形界面,可以通过VNC服务来实现步骤如下: 一.安装vnc server1.查看是否安装vncrpm -q vnc-serverpackage vnc is not inst ...

  8. centos7.2上实践cgoup

    基本介绍 CGroups 是一种对进程资源管理和控制的统一框架,它提供的是一种机制,而具体的策略(Policy)是通过子系统(subsystem)来完成的.子系统是CGroups对进程组进行资源控制的 ...

  9. jquery表格增加删除后改变序号

    有个小bug,懒得修了. 目的:增加一行的时候,td第一列排序. 删除一行的时候,td第一列排序 <!DOCTYPE HTML> <html> <head> < ...

  10. Python全栈开发day3

    1.Pycharm使用介绍 1.1 新建py文件自动添加python和编码 1.2 更改pycharm默认字体和风格 点击左上角“file”-->“Settings”(或者用“Ctrl+Alt+ ...