-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex29.5.c
35 lines (35 loc) · 885 Bytes
/
ex29.5.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
static int finish_foreach(lua_State *L, int status, intptr_t ctx) {
if (status != LUA_YIELD) {
/* error */
return 0;
}
lua_pop(L, 1);
while (lua_next(L, 1)) {
lua_pushvalue(L, 2); /* f */
lua_pushvalue(L, 3); /* key */
lua_pushvalue(L, 4); /* value */
status = lua_pcallk(L, 2, 0, 0, finish_foreach);
if (status != LUA_OK && status != LUA_YIELD) {
/* error */
return 1;
}
lua_pop(L, 1); /* pop value */
}
return 0;
}
static int l_foreach_can_yield(lua_State *L) {
int status;
lua_pushnil(L);
while (lua_next(L, 1)) {
lua_pushvalue(L, 2); /* f */
lua_pushvalue(L, 3); /* key */
lua_pushvalue(L, 4); /* value */
status = lua_pcallk(L, 2, 0, 0, finish_foreach);
if (status != LUA_OK && status != LUA_YIELD) {
/* error */
return 1;
}
lua_pop(L, 1); /* pop value */
}
return 0;
}