diff options
Diffstat (limited to 'ltablib.c')
| -rw-r--r-- | ltablib.c | 30 |
1 files changed, 19 insertions, 11 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltablib.c,v 1.43 2008/02/14 16:03:27 roberto Exp roberto $ | 2 | ** $Id: ltablib.c,v 1.44 2008/04/07 18:43:00 roberto Exp roberto $ |
| 3 | ** Library for Table Manipulation | 3 | ** Library for Table Manipulation |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -36,20 +36,28 @@ static int foreachi (lua_State *L) { | |||
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | 38 | ||
| 39 | static int foreach (lua_State *L) { | 39 | static int foreachcont (lua_State *L) { |
| 40 | luaL_checktype(L, 1, LUA_TTABLE); | 40 | for (;;) { |
| 41 | luaL_checktype(L, 2, LUA_TFUNCTION); | ||
| 42 | lua_pushnil(L); /* first key */ | ||
| 43 | while (lua_next(L, 1)) { | ||
| 44 | lua_pushvalue(L, 2); /* function */ | ||
| 45 | lua_pushvalue(L, -3); /* key */ | ||
| 46 | lua_pushvalue(L, -3); /* value */ | ||
| 47 | lua_call(L, 2, 1); | ||
| 48 | if (!lua_isnil(L, -1)) | 41 | if (!lua_isnil(L, -1)) |
| 49 | return 1; | 42 | return 1; |
| 50 | lua_pop(L, 2); /* remove value and result */ | 43 | lua_pop(L, 2); /* remove value and result */ |
| 44 | if (lua_next(L, 1) == 0) /* no more elements? */ | ||
| 45 | return 0; | ||
| 46 | lua_pushvalue(L, 2); /* function */ | ||
| 47 | lua_pushvalue(L, -3); /* key */ | ||
| 48 | lua_pushvalue(L, -3); /* value */ | ||
| 49 | lua_callcont(L, 2, 1, &foreachcont); | ||
| 51 | } | 50 | } |
| 52 | return 0; | 51 | } |
| 52 | |||
| 53 | |||
| 54 | static int foreach (lua_State *L) { | ||
| 55 | luaL_checktype(L, 1, LUA_TTABLE); | ||
| 56 | luaL_checktype(L, 2, LUA_TFUNCTION); | ||
| 57 | lua_pushnil(L); /* first key */ | ||
| 58 | lua_pushnil(L); /* first value */ | ||
| 59 | lua_pushnil(L); /* first "return" */ | ||
| 60 | return foreachcont(L); | ||
| 53 | } | 61 | } |
| 54 | 62 | ||
| 55 | 63 | ||
