How to loop over a Lua array/table in C/C++

Read time: 3 minutes

So I'm making a cross platform OpenGL editor where you write Lua and you get an OpenGL ES 2 surface to do your drawing on. I'm manually writing each of these APIs and helpers and in my travels found that I wanted to send a table of vertices to the GPU (for drawing, obviously).

Once this reached C land... how do you iterate over a Lua table in C?

Well, after some faffing around with some implementations I found in mailing lists and in StackOverflow none of those worked so I hit the Lua source and manual to see what exactly what each function was doing.

They weren't doing what we wanted, or at least not in the language we expect it to.

To loop over a table in Lua you need to write your loop like this.

1static int SendVertices(lua_State *L) {
2  if (!lua_istable(L, -1)) {
3    LOGE("Expected a table of vertices, got %s", lua_typename(L, lua_type(L, -1)));
4    return 1;
5  }
7  // Get the number of vertices we received.
8  size_t len = lua_rawlen(L, -1);
9  double vertices[len];
11  for (int index = 0; index <= len; index++) {
12    // Our actual index will be +1 because Lua 1 indexes tables.
13    int actualIndex = index + 1; 
14    // Push our target index to the stack.
15    lua_pushinteger(L, actualIndex);
16    // Get the table data at this index (and not get the table, which is what I thought this did.)
17    lua_gettable(L, -2); 
18    // Check for the sentinel nil element.
19    if (lua_type(L, -1) == LUA_TNIL) break; 
20    // Get it's value.
21    vertices[index] = luaL_checknumber(L, -1);
22    // Pop it off again.
23    lua_pop(L, 1);
24  }
26  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vertices);
27  glEnableVertexAttribArray(0);
29  return 0;

That's it, push the index you want to read and then read and pop the value as you require.