[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: C implementation of Generic For?
- From: Patrick Donnelly <batrick@...>
- Date: Thu, 5 Aug 2010 18:40:11 -0400
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