'__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

@@ -905,13 +905,18 @@ local function foo1 (e,i)
if i <= e.n then return i,a[i] end
end
setmetatable(a, {__pairs = function (x) return foo, x, 0 end})
local closed = false
setmetatable(a, {__pairs = function (x)
local tbc = setmetatable({}, {__close = function () closed = true end})
return foo, x, 0, tbc
end})
local i = 0
for k,v in pairs(a) do
i = i + 1
assert(k == i and v == k+1)
end
assert(closed) -- 'tbc' has been closed
a.n = 5
a[3] = 30