lua-users home
lua-l archive

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


On Thu, Aug 5, 2010 at 5:39 PM, King, Mike <MKing@klmicrowave.com> wrote:
> How would one write the following Lua code using the Lua C interface?  I have written a function that behaves like ipairs and pairs.  I would like to use it from the C interface but I'm not sure how to accomplish it.

Here's code generalized for a generic for loop:

/* f, s, var on stack
 */
for (;;) {
  int top = lua_gettop(L);
  lua_pushvalue(L, -3);
  lua_pushvalue(L, -3);
  lua_pushvalue(L, -3);
  lua_call(L, 2, LUA_MULTRET);
  lua_remove(L, top);
  if (lua_isnoneornil(L, top)) {
    lua_settop(L, top);
    break;
  }
  /* do stuff, var_1, ..., var_n --> top, ..., lua_gettop(L) */
  lua_settop(L, top);
}

Haven't tested it, but I'm pretty sure it is correct.

-- 
- Patrick Donnelly