[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: loadwithprefix and Lua 5.4
- From: Roberto Ierusalimschy <roberto@...>
- Date: Sat, 13 Feb 2021 12:46:46 -0300
> I have been struggling with the "multiload" exercise in chapter 16
> of Programming in Lua. I managed to cook up a "loadwithprefix"
> that worked satisfactorily but when I extended it in the obvious
> way with a postfix as well I got "file already closed" errors.
> The io.lines iterator must have changed for Lua 5.4 to keep
> track of when the file is to-be-closed, and so presumably an
> extra upvalue of the iterator function is being used for this.
> But I am getting a headache trying to work out from liolib.c
> exactly what is going on. Can any kind expert give me some hints?
IIRC, the change to Lua 5.4 only affects incomplete loops (interrupted
with breaks). In previous versions, when the loop ends naturally, the
iterator function itself closes the file. When the loop is interrupted
by a break, the iterator function is no longer called and, therefore,
cannot close the loop. Lua 5.4 changed the semantics of for loops, which
now has an extra, fourth value (besides the iterator function, the
index, and the state) which is a to-be-closed variable: whenever the
loop ends, no matter how, it closes the file.
But it seems that a multiload function should iterate each file
until its end, so I do not see how that change in 5.4 could affect
anything in an implementation.