[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Meta function tables for LUA_FILEHANDLE: why two separate tables?
- From: Sean Conner <sean@...>
- Date: Fri, 18 Nov 2022 18:29:39 -0500
It was thus said that the Great Viacheslav Usov once stated:
> On Fri, Nov 18, 2022 at 1:25 AM Sean Conner <sean@conman.org> wrote:
>
> Less than three years later, Lua 5.0 was released. Judging from its
> manual, it was possible to change __gc in a metatable once the
> metatable is obtained, which was also not restricted by default. This
> is in sharp contrast with the previous version.
>
> Why the change? I have looked for an answer in "The Evolution of Lua".
> It does describe the reasons why tags and tag methods were traded for
> metatables, and also touches upon the changes in userdata and garbage
> collection between 4.0 and 5.0, but, I think, it says nothing that
> would justify the ability of the user code to change the __gc method
> of userdata. I believe it was a mistake, possibly guided by the
> conceptual generality or elegance of the approach.
>
> That it was a mistake was recognized no later than in 5.2 (some 8
> years after). Before that, not only was __gc method freely changeable
> by the user, but the changes were also taking effect at the
> finalization time. 5.2 made those changes take no effect on EXISTING
> data, but still let them have effect on FUTURE data (with the same
> metatable). So a prudent library author can proactively restore the
> __gc method in the metatable before it is associated with a new
> userdatum, or bite the bullet and roll out the whole nine yards of
> __metatable machinery, but it looks like few library authors are aware
> of the issue, and some of those who are, such as the authors of the
> built-in IO library, just look the other way.
I am wondering just how much of an issue this is. Have you encountered
Lua code that wantonly changed or called __gc() diretly? Have you contacted
the authors of Lua modules you use to report the error? I personally
haven't encountered any Lua code that manipulates __gc of userdata in such a
manner, and I've been using Lua since 2009 and the majority of modules I've
used (written by others) don't seem to set __gc each time it sets a
metatable [1].
-spc
[1] The only one I've seen is luafilesystem, but only because its
metatables only have two methods, __index and __gc, and I'm thinking
the author felt it was easier to just create and set the table than
to use luaL_Reg[] and the asociated functions.