lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> I don't understand what you mean... :-/
> Could you point me to the relevant context?

Lua 5.1:
local new_env = {}
local foo = loadfile("foo.lua")
setfenv(foo, new_env)

Lua 5.2's 'loadin' only works on chunks, not filenames containing
chunks, as far as I know. So I'd have to use the io library to get the
contents, then loadin to load that chunk with the right environment.

dofile() should probably refer to _ENV rather than the original global
environment as well, because it loads and runs a file immediately. I
don't know, it just seems like if code loads other files (or other
code period), the code would expect those files to be in the same
environment unless otherwise specified.

>
> Couldn't you do
> sandbox.load = function(chunk) return loadin(sandbox,chunk) end
> ?

Not really, because the sandboxed code might want to include unrelated
modules that use loadstring or loadfile, which is entirely possible.
You might want those modules in an entirely new environment
(setfenv(foo, {}), but your 'sandbox.load' would load them into the
sandbox environment instead.