5

When writing a C function that pushes a table onto the stack as its return value to the Lua caller, what should it return in the C context? I know you are supposed to return the number of values that you are passing back to the Lua caller, but in the case of a table, is it 1 for the table reference, or do you need to account for the contents of the table?

The method of passing back a table I am using is shown in "Pushing a Lua Table."

Community
  • 1
  • 1
Jesse Craig
  • 560
  • 5
  • 18
  • 1
    I am saying this without any prior experience with lua; however reading this: http://lua-users.org/wiki/TablesTutorial it seems that lua tables are passed by reference; so I guess that the answer would be 1 ; because you're just passing the reference around – Ahmed Masud May 30 '13 at 00:00

1 Answers1

6

You are only returning one lua value directly, so your C function should return 1.

Something like this:

int my_table( luaState * L) {
  lua_newtable(L);
  lua_pushstring(L, "a_key");
  lua_pushstring(L, "a_value");
  lua_settable(L, -3);
  return 1;
}
Michael Anderson
  • 70,661
  • 7
  • 134
  • 187