1

Now, I have two modules, CModule and CModule2. In CModule, My code like below:

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;
}

I will get the registered table in the CModule2. In CModule2, My code like below:

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");

But now. When I call the lua_objlen, I got nothing. Is there any error? If I register a function or a string value, it works well.

I call the CModule in Lua like this.

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

Why?

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
vipygd
  • 78
  • 7
  • That second function is presumably `GetRegisteredTable`? Have you printed out the value of `iRef` in both C functions and in lua to make sure it is being passed through correctly? Are you getting a table back from the `lua_rawgeti` call correctly? – Etan Reisner Aug 28 '14 at 18:27
  • @EtanReisner Thanks for your comment. The second function is GetRegisteredTable whick defined in the CModule2. >> Have you printed out the value of iRef in both C functions and in lua to make sure it is being passed through correctly? Yes. I got the value. It's 1. I think the value is correct. >> Are you getting a table back from the lua_rawgeti call correctly? I call the lua_istable to make sure the retured value on the top stack is a table. Yes, It's table. But later, Everything goes wrong. – vipygd Aug 29 '14 at 01:41
  • Oh. It puzzled me. But now, it works well. I never edit the code.Oh my God. This is Lua. It surprised me. – vipygd Aug 29 '14 at 04:01
  • You got it working? What was the issue? – Etan Reisner Aug 29 '14 at 04:24
  • @EtanReisner Oh, I did nothing but change a computer. Is it a joke? Maybe, so I will try it later on the earlier one. – vipygd Aug 29 '14 at 10:57

0 Answers0