[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua 5.2 environments and changing a loaded file environment
- From: Roberto Ierusalimschy <roberto@...>
- Date: Mon, 9 Aug 2010 09:40:22 -0300
> [...] (One could use debug.upvaluejoin to give it a new upvalue,
> but this is an even worse hack in my opinion.)
>
> I would rather avoid changing the file any to accomplish this (it
> messes with debug output from errors and whatnot) -- that is, I don't
> want to add a function to each script that lets me change the
> environment dynamically. Does anyone see a better way to do this?
Basically you want to break a function abstraction, interfering from the
outside in what the function does inside. This is a hack, no matter how
you implement it. This kind of thing should not be a regular operation
in the language (as it was with setfenv).
As lhf mentioned, adding a "local _ENV = ...;" when reading your
chunk solves your problem. (It will not mess with debug output
from errors if it does not have an end of line.)
-- Roberto