简体   繁体   中英

Iterate a lua table in c with a custom pair function

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM