[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Determining if _ENV is the first upvalue of a closure?
- From: Patrick Donnelly <batrick@...>
- Date: Fri, 13 Mar 2015 12:24:49 -0400
On Fri, Mar 13, 2015 at 8:30 AM, Roberto Ierusalimschy
<roberto@inf.puc-rio.br> wrote:
> I guess you could check whether the first (or actually, any) upvalue
> has the same table as the distinguished environment. If so, you send
> some special code that says "use your distinguished environment here".
> It is true that some functions have custom environments; but, in that
> case, it seems useless to know that it is the _ENV, since you cannot
> use the receiver's _ENV there anyway.
>
> In other words, you should worry about the value, not the name. If an
> upvalue carries the distinguished environment (despite its name), you
> probably want to use the receive's distinguished environment. If an
> upvalue carries any other value (despite its name), you need some
> other solution.
This is exactly what I had to do for lua-serialize:
https://github.com/batrick/lua-serialize/blob/master/serialize.lua#L182-200
Basically, if a serialized object references a distinguished function
from the global environment, that function is not serialized and
instead loaded when the object is deserialized.
--
Patrick Donnelly