So any code loaded via load() (or similar) will hvae _ENV as the first upvalue, you can use setupvalue() to switch out the _ENV value currently set. Arbitrary functions might be a bit harder to locate the proper _ENV to swap (if not outright impossible in some cases).
Sadly that’s not true. Any chunk that is COMPILED will honor this, but code that is obtained via string.dump() followed by load() often will not. Such a dumped function may or may not have _ENV as one of its upvalues, and even if it does, it may not be the first.
—Tim
|