userdata)
在Lua中可以通过自定义类型的方式与C语言代码更高效、更灵活的交互。这里我们通过一个简单完整的示例来学习一下Lua中userdata的使用方式。需要说明的是,该示例完全来自于Programming in Lua。其功能是用C程序实现一个Lua的布尔数组,以提供程序的执行效率。见下面的代码和关键性注释。
1 #include <lua.hpp> 2 #include <lauxlib.h> 3 #include <lualib.h> 4 #include <limits.h> 5 6 #define BITS_PER_WORD (CHAR_BIT * sizeof(int)) 7 #define I_WORD(i) ((unsigned int)(i))/BITS_PER_WORD 8 #define I_BIT(i) (1 << ((unsigned int)(i)%BITS_PER_WORD)) 9 10 typedef struct NumArray { 11 int size; 12 unsigned int values[ 1 ]; 13 } NumArray; 14 15 extern " C " int newArray(lua_State* L) 16 { 17 // 1. 检查第一个参数是否为整型。以及该参数的值是否大于等于1. 18 int n = luaL_checkint(L, 1 ); 19 luaL_argcheck(L, n >= 1 , 1 , " invalid size. " ); 20 size_t nbytes = sizeof (NumArray) + I_WORD(n - 1 ) * sizeof ( int ); 21 // 2. 参数表示Lua为userdata分配的字节数。同时将分配后的userdata对象压入栈中。 22 NumArray* a = (NumArray* )lua_newuserdata(L,nbytes); 23 a->size = n; 24 for ( int i = 0 ; i < I_WORD(n - 1 ); ++ i) 25 a->values[i] = 0 ; 26 // 获取注册表变量myarray,该key的值为metatable。 27 luaL_getmetatable(L, " myarray " ); 28 // 将userdata的元表设置为和myarray关联的table。同时将栈顶元素弹出。 29 lua_setmetatable(L,- 2 ); 30 return 1 ; 31 } 32 33 extern " C " int setArray(lua_State* L) 34 { 35 // 1. Lua传给该函数的第一个参数必须是userdata,该对象的元表也必须是注册表中和myarray关联的table。 36 // 否则该函数报错并终止程序。 37 NumArray* a = (NumArray*)luaL_checkudata(L, 1 , " myarray " ); 38 int index = luaL_checkint(L, 2 ) - 1 ; 39 // 2. 由于任何类型的数据都可以成为布尔值,因此这里使用any只是为了确保有3个参数。 40 luaL_checkany(L, 3 ); 41 luaL_argcheck(L,a != NULL, 1 , " 'array' expected. " ); 42 luaL_argcheck(L, 0 <= index && index < a->size, 2 , " index out of range. " ); 43 if (lua_toboolean(L, 3 )) 44 a->values[I_WORD(index)] |= I_BIT(index); 45 else 46 a->values[I_WORD(index)] &= ~ I_BIT(index); 47 return 0 ; 48 } 49 50 extern " C " int getArray(lua_State* L) 51 { 52 NumArray* a = (NumArray*)luaL_checkudata(L, 1 , " myarray " ); 53 int index = luaL_checkint(L, 2 ) - 1 ; 54 luaL_argcheck(L, a != NULL, 1 , " 'array' expected. " ); 55 luaL_argcheck(L, 0 <= index && index < a->size, 2 , " index out of range " ); 56 lua_pushboolean(L,a->values[I_WORD(index)] & I_BIT(index)); 57 return 1 ; 58 } 59 60 extern " C " int getSize(lua_State* L) 61 { 62 NumArray* a = (NumArray*)luaL_checkudata(L, 1 , " myarray " ); 63 luaL_argcheck(L,a != NULL, 1 , " 'array' expected. " ); 64 lua_pushinteger(L,a-> size); 65 return 1 ; 66 } 67 68 extern " C " int array2string(lua_State* L) 69 { 70 NumArray* a = (NumArray*)luaL_checkudata(L, 1 , " myarray " ); 71 lua_pushfstring(L, " array(%d) " ,a-> size); 72 return 1 ; 73 } 74 75 static luaL_Reg arraylib_f [] = { 76 { " new " , newArray}, 77 {NULL, NULL} 78 }; 79 80 static luaL_Reg arraylib_m [] = { 81 { " set " , setArray}, 82 { " get " , getArray}, 83 { " size " , getSize}, 84 { " __tostring " , array2string}, // print(a)时Lua会调用该元方法。 85 {NULL, NULL} 86 }; 87 88 extern " C " __declspec(dllexport) 89 int luaopen_testuserdata(lua_State* L) 90 { 91 // 1. 创建元表,并将该元表指定给newArray函数新创建的userdata。在Lua中userdata也是以table的身份表现的。 92 // 这样在调用对象函数时,可以通过验证其metatable的名称来确定参数userdata是否合法。 93 luaL_newmetatable(L, " myarray " ); 94 lua_pushvalue(L,- 1 ); 95 // 2. 为了实现面对对象的调用方式,需要将元表的__index字段指向自身,同时再将arraylib_m数组中的函数注册到 96 // 元表中,之后基于这些注册函数的调用就可以以面向对象的形式调用了。 97 // lua_setfield在执行后会将栈顶的table弹出。 98 lua_setfield(L,- 2 , " __index " ); 99 // 将这些成员函数注册给元表,以保证Lua在寻找方法时可以定位。NULL参数表示将用栈顶的table代替第二个参数。 100 luaL_register(L,NULL,arraylib_m); 101 // 这里只注册的工厂方法。 102 luaL_register(L, " testuserdata " ,arraylib_f); 103 return 1 ; 104 }
轻量级userdata:
之前介绍的是full userdata,Lua还提供了另一种轻量级userdata(light userdata)。事实上,轻量级userdata仅仅表示的是C指针的值,即(void*)。由于它只是一个值,所以不用创建。如果需要将一个轻量级userdata放入栈中,调用lua_pushlightuserdata即可。full userdata和light userdata之间最大的区别来自于相等性判断,对于一个full userdata,它只是与自身相等,而light userdata则表示为一个C指针,因此,它与所有表示同一指针的light userdata相等。再有就是light userdata不会受到垃圾收集器的管理,使用时就像一个普通的整型数字一样。
分类: Lua编程
作者: Leo_wl
出处: http://www.cnblogs.com/Leo_wl/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
版权信息声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did48386