[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: Tim Hill <drtimhill@...>
- Date: Fri, 13 Mar 2015 11:03:05 -0700
> On Mar 13, 2015, at 5:30 AM, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
>
>> The problem then, is determining which functions have _ENV as an upvalue and which do not. I’m unable to find a deterministic way to do this. I can’t look at the type of the first upvalue, since some of our upvalues are also tables. I can’t look at the name of the first upvalue, since this can be stripped out with debug information. I *can* look to see if the first upvalue is the same table as the distinguished environment, but (argh) some functions have custom environments and hence a different environment. I can look to see if the table has _G (and if it is a self-reference to the table), but while this is a good hint it is of course not deterministic as a regular table could have this (though it would be odd).
>
> 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.
>
> -- Roberto
>
Yes, this is pretty much where I ended up, thanks for the feedback.
—Tim