Lua.LearningLua.7-userdata

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.

1 LUA_API void *lua_touserdata (lua_State *L, int idx) {
2   StkId o = index2adr(L, idx);
3   switch (ttype(o)) {
4     case LUA_TUSERDATA: return (rawuvalue(o) + 1);
5     case LUA_TLIGHTUSERDATA: return pvalue(o);
6     default: return NULL;
7   }
8 }

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

时间: 2024-11-14 12:20:15

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

快速掌握Lua 5.3 —— userdata (1)

Q:什么是"userdata"? A:"userdata"分为两类,"full userdata"和"light userdata".Lua使用他们来表示C中一些特殊的类型.前面的章节中,我们看到了如何通过C编写新的函数来扩展Lua:使用"userdata",我们将可以通过C编写新的类新来扩展Lua. Q:两种"userdata"的区别? A: \ "full userdata

Lua中的userdata

[话从这里说起] 在我发表<Lua中的类型与值>这篇文章时,就有读者给我留言了,说:你应该好好总结一下Lua中的function和userdata类型.现在是时候总结了.对于function,我在<Lua中的函数>这篇文章中进行了总结,而这篇文章将会对Lua中的userdata进行仔细的总结.对于文章,大家如果有任何疑议,都可以在文章的下方给我留言,也可以关注我的新浪微博与我互动.学习,就要分享,我期待你的加入. [userdata是啥?] userdata是啥?简单直译就是用户数

lua笔记之userdata

1.一直使用框架里封装好的c库,想着自己一点一点的写些例子,学习下,以后需要c库,可以自己写了. 下边是一个简单的userdata的例子--数组操作. newarray.c #include "lua.h" #include "lauxlib.h" #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <string.h> typedef

快速掌握Lua 5.3 —— userdata (2)

Q:如何使用"userdata"的"metamethods"? A:我们继续来修改上一节中的例子,这次我们的目标是使用面向对象的方式调用"userdata"的方法.这个目标既可以在Lua中实现,也可以在C库中实现,我们先来看一个比较简单的方式,在Lua中实现."mylib.c"中代码无需更改,只需要修改"a.lua"中的代码, local array = require "mylib"

Lua的Full UserData、Light UserData和metatable

http://lua.2524044.n2.nabble.com/LightUserData-and-metatables-td3807698.html "... do you realize that by setting the metatable of a light userdatayou are actually setting the metatable of all light userdata at once ?"I did not realise this. oops

lua userdata

userdata类型是为了方便C/C++对Lua进行扩展,因为在用C/C++扩展时,我们经常会自定义数据类型,如: typedef struct epoll_fd {      int epfd;     size_t size;     struct epoll_event *events; }epoll_fd_t; 我们要想在Lua中使用此类型的对象,就必须使用userdata类型来表示. 对于Lua而言,所有C/C++自定义的类型都是userdata类型,无法区分,那么在实际的应用中use

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

Lua中Userdata类型源码实现

1.概述 Lua中userdata分两种,一种是轻量级userdata(light userdata),轻量级userdata是一种表示C指针的值,对Lua虚拟机来说,这种数据类型不需要GC(垃圾回收),其指向的内存由用户分配和释放,其实现就是一个void *p指针:后一种userdata类型完全userdata(full userdata),内存是由Lua虚拟机分配,并有GC机制负责处理.下面将通过Lua 5.2.1的源码来看后一种userdata的实现. 2.源码实现 userdata内存存

Lua入门系列

当初工作中需要使用Lua,然后就顺便把Lua的基础知识都总结了一遍,希望对大家有帮助. Lua中的类型与值 Lua中的表达式 Lua中的语句 Lua中的函数 Lua中的闭包 Lua中的迭代器与泛型for Lua中的协同程序 Lua中的元表与元方法 Lua中__index和__newindex实践 Lua中的环境概念 Lua中的模块与包 Lua中的面向对象编程 Lua中的一些库(1) Lua中的一些库(2) Lua中字符串库中的几个重点函数 Lua与C C“控制”Lua Lua“控制”C 再说C模