Passing Multiple Arrays (tables) From C to Lua
Posted: Tue Apr 12, 2016 12:52 pm
I have a C function that returns two lists to Lua like this:
lua_checkstack(L, m); // where m is the combined size of the two arrays
lua_newtable(L);
for (i=1; i <= idx1; i++) {
lua_pushnumber(L, List1[i - 1]);
lua_rawseti(L, -2, i);
}
lua_newtable(L);
for (j=1; j <= idx2; j++) {
lua_pushnumber(L, List2[j - 1]);
lua_rawseti(L, -2, j);
}
return 2;
It seems to work, but not always. Is something missing here? This is what the call looks like in Lua:
List1, List2 = Cfunction(source, period);
It seems like List2 always gets populated, but List1 doesn't. Is there a better way to do this?
lua_checkstack(L, m); // where m is the combined size of the two arrays
lua_newtable(L);
for (i=1; i <= idx1; i++) {
lua_pushnumber(L, List1[i - 1]);
lua_rawseti(L, -2, i);
}
lua_newtable(L);
for (j=1; j <= idx2; j++) {
lua_pushnumber(L, List2[j - 1]);
lua_rawseti(L, -2, j);
}
return 2;
It seems to work, but not always. Is something missing here? This is what the call looks like in Lua:
List1, List2 = Cfunction(source, period);
It seems like List2 always gets populated, but List1 doesn't. Is there a better way to do this?