|
Not quite. The compiler generates the top-level (aka “main”) function for a chunk as if it had one upvalue, _ENV. It’s the bytecode *loader* (and only the Lua one, not the C API one) that wires this to the supplied (or distinguished) environment. Since, at the bytecode level, the “main” function of a chunk is indistinguishable from an arbitrary function dumped with string.dump(), the various behaviors observed by Sean are a result. In fact, this behavior makes it HARDER to sandbox untrusted functions, though Roberto would no doubt point out that you cannot trust bytecode anyway, and should only load Lua text, which will have a correctly wired “main” chunk function. —TIm |