'__pairs' can also return a to-be-closed object
This commit is contained in:
13
lbaselib.c
13
lbaselib.c
@@ -279,21 +279,22 @@ static int luaB_next (lua_State *L) {
|
||||
|
||||
static int pairscont (lua_State *L, int status, lua_KContext k) {
|
||||
(void)L; (void)status; (void)k; /* unused */
|
||||
return 3;
|
||||
return 4; /* __pairs did all the work, just return its results */
|
||||
}
|
||||
|
||||
static int luaB_pairs (lua_State *L) {
|
||||
luaL_checkany(L, 1);
|
||||
if (luaL_getmetafield(L, 1, "__pairs") == LUA_TNIL) { /* no metamethod? */
|
||||
lua_pushcfunction(L, luaB_next); /* will return generator, */
|
||||
lua_pushvalue(L, 1); /* state, */
|
||||
lua_pushnil(L); /* and initial value */
|
||||
lua_pushcfunction(L, luaB_next); /* will return generator and */
|
||||
lua_pushvalue(L, 1); /* state */
|
||||
lua_pushnil(L); /* initial value */
|
||||
lua_pushnil(L); /* to-be-closed object */
|
||||
}
|
||||
else {
|
||||
lua_pushvalue(L, 1); /* argument 'self' to metamethod */
|
||||
lua_callk(L, 1, 3, 0, pairscont); /* get 3 values from metamethod */
|
||||
lua_callk(L, 1, 4, 0, pairscont); /* get 4 values from metamethod */
|
||||
}
|
||||
return 3;
|
||||
return 4;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user