luaL_ref在表上出错?

时间:2014-08-28 17:54:20

标签: lua lua-api

现在,我有两个模块,CModule和CModule2。在CModule中,我的代码如下:

static int RegisterTable(lua_State *L)
{
    luaL_checktype(L, 1, LUA_TTABLE);
    int iRef = luaL_ref(L, LUA_REGISTRYINDEX);
    lua_pushinteger(L, iRef);
    return 1;
}

我将在CModule2中获取注册表。在CModule2中,我的代码如下:

int iRef = luaL_checkinteger(L, 1);
lua_rawgeti(L, LUA_REGISTRYINDEX, iRef);
size_t iLen = lua_objlen(L, -1);
printf("iLen:%d", iLen);
for (size_t i = 1; i <= iLen; ++iLen)
{
    lua_rawgeti(L, -1, i);
    int iValue = lua_tointeger(L, -1);
    lua_pop(L, 1);
    printf("%d", iValue);
}
    printf("\n");

但是现在。当我打电话给lua_objlen时,我一无所获。有什么错误吗?如果我注册一个函数或一个字符串值,它就可以正常工作。

我像这样在Lua中调用CModule。

require "CModule"
require "CModule2"
local tbTest = {2, 4, 6, 8, 10}
local iRef1 = CModule.RegisterTable(tbTest)
CModule2.GetRegisteredTable(iRef1)

为什么?

0 个答案:

没有答案