lua 和 C 语言进行交互 —— 如何传递table

方法1:

采用 lua_pushstring/lua_pushnumber 传递key、value,然后通过 lua_settable 设置 table 中的kv对,like this:
        lua_pushnumber(L, 1);
        lua_pushstring(L, "value1");
        lua_settable(L, -3);
其中“lua_settable(L, -3)”表示将前压栈中的前两个参数作为一个kv对放到table中。
1. 首先编写 lua 文件 lua_test.lua:
-- file: lua_test.lua
function domain(i)

    -- call C function.
    local tab = gettab() 

    -- show key and value
    for k, v in pairs(tab) do
        print("key: ".. k)
        print("val: ".. v)
	print()
    end

end
2. 其中 gettab 是 C 中注册的函数,下面是对应的 C 代码 c_test.c :
/*
 * file: c_test.c
 */
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>

int get_tab(lua_State *L)
{
    /* create table. */
    lua_newtable(L);

    /* push (key, value). */
    int i;
    char value[10] = {0};
    for(i=0; i<5; ++i)
    {
        sprintf(value, "value%d", i+1);
        lua_pushnumber(L, i+1);    //key
        lua_pushstring(L, value);  //value
        lua_settable(L, -3);       //push key,value
    }

    /* deal return. */
    return 1;
}

int main()
{
    /* create a state and load standard library. */
    lua_State* L = luaL_newstate();

    /* opens all standard Lua libraries into the given state. */
    luaL_openlibs(L);

    /* register function be called by lua. */
    lua_register(L, "gettab", get_tab);

    /* load and exec the specified lua file. */
    int error = luaL_dofile(L, "lua_test.lua");
    if(error) {
        perror("luaL_dofile error");
        exit(1);
    }

    /* get the domain function from lua file. */ 
    lua_getglobal(L,"domain");

    /* exec the domain function. */
    error = lua_pcall(L, 0, 0, 0);
    if (error) {
        fprintf(stderr,"%s\n",lua_tostring(L,-1));
        lua_pop(L,1);
    }

    /* close lua state. */
    lua_close(L);

    return 0;
}
使用一下命令编译:
gcc c_test.c -I/usr/local/include/luajit-2.0 -L/usr/local/lib -lluajit-5.1 -o exe
note:环境需要装luajit

3. 下面执行编译出来的可执行文件 exe:
key: 1
val: value1

key: 2
val: value2

key: 3
val: value3

key: 4
val: value4

key: 5
val: value5


方法2:

上面对 table 的 kv 对是这样处理的:
        lua_pushnumber(L, i+1);    //key
        lua_pushstring(L, value);  //value
        lua_settable(L, -3);       //push key,value
其实有一种效率高的方法,like this:
        lua_pushstring(L, value);
        lua_rawseti(L, -2, i++);

最后再写一种 lua 调用函数库的方法:
1. 首先编写 C 文件 so_test.c:
/* 
 * file:so_test.c
 */
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <stdio.h>

int get_tab(lua_State *L)
{
    /* get the input. */
    int num = lua_tonumber(L, 1);

    /* create table. */
    lua_newtable(L);

    /* push (key, value). */
    int i;
    char value[10] = {0};
    for(i=0; i<num; ++i)
    {
        sprintf(value, "value%d", i+1);
        lua_pushnumber(L, i+1);
        lua_pushstring(L, value);
        lua_settable(L, -3);
    }

    /* deal return. */
    return 1;
}

static const luaL_Reg mylib[] = {
    {"get_tab", get_tab},
    {NULL, NULL}
};

int luaopen_lua_test(lua_State *L) {
    luaL_register(L, "lua_test", mylib);
    return 0;
}
用以下命令编译出动态库 lua_test.so:
gcc so_test.c -shared -fPIC -I/usr/local/include/luajit-2.0/ -I/usr/include/ -lluajit-5.1 -o lua_test.so

2. 然后编写 lua 测试代码 test.lua:
-- file: test.lua
local test = require('lua_test')

function domain(num)

    -- call C function.
    local tab = test.get_tab(num) 

    -- show key and value
    for k, v in pairs(tab) do
        print("key: ".. k)
        print("val: ".. v)
	print()
    end

end

domain(5)

3. 执行 lua 代码:
# lua test.lua 
key: 1
val: value1

key: 2
val: value2

key: 3
val: value3

key: 4
val: value4

key: 5
val: value5

以上代码的编译执行环境:

os:CentOS release 6.4 (Final)

gcc:gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-3)

lua:Lua 5.1.4

luajit:LuaJIT 2.0.2

参考书: Programming in Lua
详细描述Lua和C之间相互传递Table类型数据 /* ====================================================== */ // 遍历Lua传入的Table类型参数, 获取它的Key/Value, 其关键操作lua_next() // lua_next() 返回1示读取成功,返回0示已经没有数据可读了 // lua_next() 会使用栈顶元素作为Key去定位本次需要取出Table里面的那个值对 // 如果Key=nil, 那就示本次取出的是第一个元素 // 它会先将当前的这个Key弹出,然后将本次取出的Key/Value压入栈, Value在栈顶 // 一个比较隐晦的处理就是, 我们不应直接使用lua_tostring(L, -2)来读取Key // 因为lua_tostring()在Key类型不是字符串时, 它会修改栈上的Key数据 // 这样, 下次调用lua_next()时, 就会因为Key被修改了而导致错误 // 为此,先调用lua_pushvalue(L, -2),将它Copy一份到栈顶,对这个Copy进行lua_tostring() // 读取Key,Value到C变量里面后,将Value和Copy弹出,留着Key在栈顶,给下次lua_next()用 // // 指令及栈图变化如下: (假如Table的栈下标是Index) // 0. 刚进入函数时 ...Table, ... <--- 这里栈顶 // 1. lua_pushnil(L) ...Table, ..., nil <--- 这里栈顶 // 2. lua_next(L, Index) ...Table, ..., Key, Value <--- 这里栈顶 // 3. lua_pushvalue(L, -2) ...Table, ..., Key, Value, KeyCopy <--- 这里栈顶 // 4. lua_pop(L, 2), ...Table, ..., Key <--- 这里栈顶 // ... 如此重复2,3,4 // N. lua_next(L, Index)返回0 ...Table, ... <--- 这里栈顶 /* ====================================================== */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值