summaryrefslogtreecommitdiff
path: root/ltablib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2009-03-10 14:14:37 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2009-03-10 14:14:37 -0300
commitba484b9eb16dd62a7c3a5400a66eb952b654d3f0 (patch)
tree31692ddf85265e5a46eab0cd55a0b0757a225e68 /ltablib.c
parentf9d015523ef48266cea37e13717c223c16941b23 (diff)
downloadlua-github-ba484b9eb16dd62a7c3a5400a66eb952b654d3f0.tar.gz
yielding across lua_call (first version)
Diffstat (limited to 'ltablib.c')
-rw-r--r--ltablib.c30
1 files changed, 19 insertions, 11 deletions
diff --git a/ltablib.c b/ltablib.c
index ab83c0c8..7296fb38 100644
--- a/ltablib.c
+++ b/ltablib.c
@@ -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);
}