6

我正在尝试将 Lua 表传递给我的 C 程序,但我不知道该怎么做。

我的 Lua 代码:

local stages = {}
stages[1] = stage1
stages[2] = stage2
stages[3] = stage3

lstage.buildpollingtable(stages)

我的 C 代码:

static int lstage_build_polling_table (lua_State * L) {    
    luaL_checktype(L, 1, LUA_TTABLE);

    lua_getfield(L, 1, "stage1");
    lua_getfield(L, 1, "stage2");
    lua_getfield(L, 1, "stage3");

    stage_t s1 = lstage_tostage(L, -3);
    stage_t s2 = lstage_tostage(L, -2);
    stage_t s3 = lstage_tostage(L, -1);

    printf("%d\n",s1->priority);
    printf("%d\n",s2->priority);
    printf("%d\n",s3->priority);

    return 1;
}

我必须做什么才能在所有元素上运行?此代码生成如下错误:

'buildpollingtable' 的错误参数 #-3(lstage-Stage * 预期,得到表)

谁能解释我做错了什么?

4

1 回答 1

4

您的表没有名为stage1等的字段,只有字段1, 2, 3。所以试试

lua_rawgeti(L,1,1);
lua_rawgeti(L,1,2);
lua_rawgeti(L,1,3);

代替

lua_getfield(L, 1, "stage1");
lua_getfield(L, 1, "stage2");
lua_getfield(L, 1, "stage3");
于 2014-10-29T02:16:43.080 回答