Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate a lua table in c with a custom pair function

Tags:

iterator

c

lua

I'd like to use the Ordered Table Simple example, I found at the lua-wiki site. Here's the link.

In Lua it iterates fine with this:

for i,v in t:opairs() do
   print( i,v )
end

Instead iterating in lua, I want pass t to a C method and iterate the table there. In the C API I found only lua_next for the original pairs iterator. How can I iterate this lua code in C ?

like image 431
user2626195 Avatar asked Aug 21 '13 08:08

user2626195


1 Answers

What you can do is write a custom next C function that mimics lua_next but works on that ordered table instead having opairs method.

int luaL_orderednext(luaState *L)
{
  luaL_checkany(L, -1);                 // previous key
  luaL_checktype(L, -2, LUA_TTABLE);    // self
  luaL_checktype(L, -3, LUA_TFUNCTION); // iterator
  lua_pop(L, 1);                        // pop the key since 
                                        // opair doesn't use it

  // iter(self)
  lua_pushvalue(L, -2);
  lua_pushvalue(L, -2);
  lua_call(L, 1, 2);

  if(lua_isnil(L, -2))
  {
    lua_pop(L, 2);
    return 0;
  }
  return 2;
}

You can then use it in C similar to lua_next:

int orderedtraverse(luaState *L)
{
  lua_settop(L, 1);
  luaL_checktype(L, 1, LUA_TTABLE);

  // t:opairs()
  lua_getfield(L, 1, "opairs");
  lua_pushvalue(L, -2);
  lua_call(L, 1, 2);

  // iter, self (t), nil
  for(lua_pushnil(L); luaL_orderednext(L); lua_pop(L, 1))
  {
    printf("%s - %s\n", 
           lua_typename(L, lua_type(L, -2)), 
           lua_typename(L, lua_type(L, -1)));
  }
  return 0;
}

Note, I didn't test this but it should work.

like image 122
greatwolf Avatar answered Nov 20 '22 17:11

greatwolf