Lua 5.4: use __close to close dir if you break the iterator

This commit is contained in:
Hisham Muhammad
2020-04-21 19:38:05 -03:00
parent 3e879ffba1
commit 842505b6a3

View File

@@ -725,7 +725,13 @@ static int dir_iter_factory(lua_State * L)
if (d->dir == NULL)
luaL_error(L, "cannot open %s: %s", path, strerror(errno));
#endif
#if LUA_VERSION_NUM >= 504
lua_pushnil(L);
lua_pushvalue(L, -2);
return 4;
#else
return 2;
#endif
}
@@ -747,6 +753,11 @@ static int dir_create_meta(lua_State * L)
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, dir_close);
lua_setfield(L, -2, "__gc");
#if LUA_VERSION_NUM >= 504
lua_pushcfunction(L, dir_close);
lua_setfield(L, -2, "__close");
#endif
return 1;
}