'__pairs' can also return a to-be-closed object

This commit is contained in:
Roberto I
2025-11-11 15:11:06 -03:00
parent 5b7d998764
commit 4cf498210e
3 changed files with 15 additions and 9 deletions

View File

@@ -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;
}