lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On 14/01/2010 20:50, Luiz Henrique de Figueiredo wrote:
I think we should have lua_pairs() to match lua_next()

__pairs is not a core metamethod, just like __tostring: it is provided
by the base library. So, it cannot influence the C API.

I didn't know there were a distintion among them (some being core, some not). Ok, so now we should think of lua_next in terms of "raw access to a table", and a combination of luaL_callmeta, lua_call and some stack fiddling as the way to traverse a table honoring its __pairs field, right?

Something like:

luaL_callmeta(L, table, "__pairs"); /* nil or { iterator, second value, third value */
int iterator = lua_gettop(L) - 2;
while(true) {
  lua_call(L, iterator, 2, LUA_MULTRET);
  if(lua_isnil(L, -1)) break;
  // etc