diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-11-07 14:42:05 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-11-07 14:42:05 -0200 |
commit | 7f6f70853c8a2730fca2e95d5968ad52cf470bda (patch) | |
tree | 948147a9cf6a5c5eb34232e7547c310eb06eadea /testes/locals.lua | |
parent | b8fed93215a23a3f443c5b0126f0de1725771b44 (diff) | |
download | lua-7f6f70853c8a2730fca2e95d5968ad52cf470bda.tar.gz lua-7f6f70853c8a2730fca2e95d5968ad52cf470bda.tar.bz2 lua-7f6f70853c8a2730fca2e95d5968ad52cf470bda.zip |
To-be-closed variable in 'for' loop separated from the state
The variable to be closed in a generic 'for' loop now is the
4th value produced in the loop initialization, instead of being
the loop state (the 2nd value produced). That allows a loop to
use a state with a '__toclose' metamethod but do not close it.
(As an example, 'f:lines()' might use the file 'f' as a state
for the loop, but it should not close the file when the loop ends.)
Diffstat (limited to 'testes/locals.lua')
-rw-r--r-- | testes/locals.lua | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/testes/locals.lua b/testes/locals.lua index 869ac1ff..28f88e54 100644 --- a/testes/locals.lua +++ b/testes/locals.lua | |||
@@ -371,6 +371,8 @@ do | |||
371 | x = x - 1 | 371 | x = x - 1 |
372 | if x > 0 then return x end | 372 | if x > 0 then return x end |
373 | end, | 373 | end, |
374 | nil, -- state | ||
375 | nil, -- control variable | ||
374 | function () -- closing function | 376 | function () -- closing function |
375 | numopen = numopen - 1 | 377 | numopen = numopen - 1 |
376 | end | 378 | end |
@@ -392,12 +394,16 @@ do | |||
392 | -- repeat test with '__open' metamethod instead of a function | 394 | -- repeat test with '__open' metamethod instead of a function |
393 | local function open (x) | 395 | local function open (x) |
394 | numopen = numopen + 1 | 396 | numopen = numopen + 1 |
397 | local state = setmetatable({x}, | ||
398 | {__close = function () numopen = numopen - 1 end}) | ||
395 | return | 399 | return |
396 | function (t) -- iteraction function | 400 | function (t) -- iteraction function |
397 | t[1] = t[1] - 1 | 401 | t[1] = t[1] - 1 |
398 | if t[1] > 0 then return t[1] end | 402 | if t[1] > 0 then return t[1] end |
399 | end, | 403 | end, |
400 | setmetatable({x}, {__close = function () numopen = numopen - 1 end}) | 404 | state, |
405 | nil, | ||
406 | state -- to-be-closed | ||
401 | end | 407 | end |
402 | 408 | ||
403 | local s = 0 | 409 | local s = 0 |