lunarmodules / luafilesystem

LuaFileSystem is a Lua library developed to complement the set of functions related to file systems offered by the standard Lua distribution.
https://lunarmodules.github.io/luafilesystem/
MIT License
900 stars 291 forks source link

Lua 5.4 and dir_iter_factory question #140

Closed asmagill closed 3 years ago

asmagill commented 3 years ago

I'm trying to understand the changes for Lua 5.4 and am curious about https://github.com/keplerproject/luafilesystem/blob/master/src/lfs.c#L728-L734 in dir_iter_factory, i.e. these lines:

#if LUA_VERSION_NUM >= 504
  lua_pushnil(L);
  lua_pushvalue(L, -2);
  return 4;
#else
  return 2;
#endif

Is there a reason an extra nil and copy of the directory object are added to the returned values? Has there been a change for generators that I missed in the release notes or docs for Lua 5.4 (an entirely likely possibility as it has happened before)?

Just curious if I need to be modifying generators in our own code as well (haven't had an issue yet, but better safe than sorry!)

asmagill commented 3 years ago

Thinking about it a little more, I can understand it returning 3 items (the function, the object, and the argument for the first object (i.e. nil)), but doesn't that also apply from 5.2 forward?

But the additional copy of the directory object at the end still eludes me.

moteus commented 3 years ago

This is new to-be-closed variable

asmagill commented 3 years ago

So you are saying that for now takes a fourth argument for it's iterators/generators which it treats with <close> when used like this:

for file in lfs.dir("/home/me") do ... end

If so, that's what I missed in the docs.

asmagill commented 3 years ago

Update, just found it in the docs:

The loop starts by evaluating explist to produce four values: an iterator function, a state, an initial value for the control variable, and a closing value.

Then, at each iteration, Lua calls the iterator function with two arguments: the state and the control variable. The results from this call are then assigned to the loop variables, following the rules of multiple assignments (see §3.3.3). If the control variable becomes nil, the loop terminates. Otherwise, the body is executed and the loop goes to the next iteration.

The closing value behaves like a to-be-closed variable (see §3.3.8), which can be used to release resources when the loop ends. Otherwise, it does not interfere with the loop.

Ok, that's good enough for me. Thanks!

hishamhm commented 3 years ago

@asmagill glad you were able to figure it out :)