diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2009-03-10 14:14:37 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2009-03-10 14:14:37 -0300 |
commit | ba484b9eb16dd62a7c3a5400a66eb952b654d3f0 (patch) | |
tree | 31692ddf85265e5a46eab0cd55a0b0757a225e68 /ltablib.c | |
parent | f9d015523ef48266cea37e13717c223c16941b23 (diff) | |
download | lua-github-ba484b9eb16dd62a7c3a5400a66eb952b654d3f0.tar.gz |
yielding across lua_call (first version)
Diffstat (limited to 'ltablib.c')
-rw-r--r-- | ltablib.c | 30 |
1 files changed, 19 insertions, 11 deletions
@@ -1,5 +1,5 @@ /* -** $Id: ltablib.c,v 1.43 2008/02/14 16:03:27 roberto Exp roberto $ +** $Id: ltablib.c,v 1.44 2008/04/07 18:43:00 roberto Exp roberto $ ** Library for Table Manipulation ** See Copyright Notice in lua.h */ @@ -36,20 +36,28 @@ static int foreachi (lua_State *L) { } -static int foreach (lua_State *L) { - luaL_checktype(L, 1, LUA_TTABLE); - luaL_checktype(L, 2, LUA_TFUNCTION); - lua_pushnil(L); /* first key */ - while (lua_next(L, 1)) { - lua_pushvalue(L, 2); /* function */ - lua_pushvalue(L, -3); /* key */ - lua_pushvalue(L, -3); /* value */ - lua_call(L, 2, 1); +static int foreachcont (lua_State *L) { + for (;;) { if (!lua_isnil(L, -1)) return 1; lua_pop(L, 2); /* remove value and result */ + if (lua_next(L, 1) == 0) /* no more elements? */ + return 0; + lua_pushvalue(L, 2); /* function */ + lua_pushvalue(L, -3); /* key */ + lua_pushvalue(L, -3); /* value */ + lua_callcont(L, 2, 1, &foreachcont); } - return 0; +} + + +static int foreach (lua_State *L) { + luaL_checktype(L, 1, LUA_TTABLE); + luaL_checktype(L, 2, LUA_TFUNCTION); + lua_pushnil(L); /* first key */ + lua_pushnil(L); /* first value */ + lua_pushnil(L); /* first "return" */ + return foreachcont(L); } |