lua userdata

lua userdata 就是用户自定义类型的数据

直接上例子

wss@localhost:~/lua/userdata/userdata2$ ls
array.so  arr_test.lua  lib.c  log.lua

 

wss@localhost:~/lua/userdata/userdata2$ vim lib.c

#include <stdio.h>
#include <string.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

typedef struct NumArray
{
        int size;
        double values[1];  /* variable part */
}NumArray;

static int newarray (lua_State *L)
{
        int n = luaL_checkint(L, 1);  //检查所给参数是否是一个数字,如果是返回该数字,否则报错
        size_t nbytes = sizeof(NumArray) + (n - 1)*sizeof(double);
        NumArray *a = (NumArray *)lua_newuserdata(L, nbytes);
        a->size = n;
        return 1;  /* new userdatum is already on the stack */
}

static int setarray (lua_State *L)
{
        NumArray *a = (NumArray *)lua_touserdata(L, 1);  //返回userdata的地址
        int index = luaL_checkint(L, 2);
        double value = luaL_checknumber(L, 3);

        luaL_argcheck(L, a != NULL, 1, "'array' expected");
        //检查a != NULL 是否等于true,如果不是,抛出错误,并给出指定的错误信息

        luaL_argcheck(L, 1 <= index && index <= a->size, 2, "index out of range");
        a->values[index-1] = value;

        return 0;

}

static int getarray (lua_State *L)
{
        NumArray *a = (NumArray *)lua_touserdata(L, 1);

int index = luaL_checkint(L, 2);
        luaL_argcheck(L, a != NULL, 1, "'array' expected");
        luaL_argcheck(L, 1 <= index && index <= a->size, 2, "index out of range");
        lua_pushnumber(L, a->values[index-1]);

        return 1;

}

static int getsize (lua_State *L)
{
        NumArray *a = (NumArray *)lua_touserdata(L, 1);
        luaL_argcheck(L, a != NULL, 1, "`array' expected");
        lua_pushnumber(L, a->size);

        return 1;

}

static luaL_Reg arraylib [] =
{

        {"new", newarray},
        {"set", setarray},
        {"get", getarray},
        {"size", getsize},
        {NULL, NULL}

};

int luaopen_array (lua_State *L)
{
        const char* libName = "array";
        luaL_register(L,libName,arraylib);
        return 1;

}

 

wss@localhost:~/lua/userdata/userdata2$ vim arr_test.lua

require "array"
local log = require "log"
a = array.new(1000)

print(a)                 --> userdata: 0x8064d48

print(array.size(a))     --> 1000

for i=1,1000 do

    array.set(a, i, 1/i)

end

print(array.get(a, 10))  --> 0.1
log.print_r(array.get(a, 10))

 

wss@localhost:~/lua/userdata/userdata2$ gcc -fpic -shared -o array.so lib.c

wss@localhost:~/lua/userdata/userdata2$ ls
array.so  arr_test.lua  lib.c  log.lua
wss@localhost:~/lua/userdata/userdata2$ lua arr_test.lua
userdata: 0x8aeb784
1000
0.1

 

log.lua是用来调试,打印信息的,在每个.lua文件开始添加一行local log = require "luci.log",在需要打印的函数中调用log.print()或者log.print_r(), 最后在/tmp/luci.output文件中查看添加的打印内容。

如:

wss@localhost:~/lua/userdata/userdata2$ echo "" > /tmp/luci.output
wss@localhost:~/lua/userdata/userdata2$ cat /tmp/luci.output       

wss@localhost:~/lua/userdata/userdata2$ lua arr_test.lua           
userdata: 0x82256ac
1000
0.1
wss@localhost:~/lua/userdata/userdata2$ cat /tmp/luci.output

[11:20:43]: 0.1 

log.print打印一个普通变量

log.print_r打印table

 

你可能感兴趣的:(lua userdata)