[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: setfenv/getfenv
- From: Enrico Colombini <erix@...>
- Date: Tue, 12 Jan 2010 17:52:15 +0100
Roberto Ierusalimschy wrote:
If I undestood you correctly, this is precisely the goal of the new
function 'loadin'.
I'm probably saying something silly, but could it be possible to avoid
the loadin() problems by setting the environment when the loaded chunk
is executed, as opposed as when it is loaded?
-- wish I could do this
local t = {}
local s = "function f() a = 3 print(a) end"
local chunk = assert(loadstring(s))
-- chunk is currently executed in the global env,
-- I'd like to execute it in 't'
in t do chunk() end -- no effect in 't'
Currently this sets 'f' and 'a' in the global environment (not in t), as
does this variant:
-- wish I could do this
local t = { loadstring = loadstring, assert = assert }
in t do
local s = "function f() a = 3 print(a) end"
-- loadstring loads in the global env anyway --
local chunk = assert(loadstring(s))
loadstring = nil; assert = nil -- remove from environment
-- chunk is currently executed in the global env --
-- I'd like to execute it in 't'
chunk() -- no effect in 't'
end
In other terms, would it be problematic to allow setting the environment
of a loaded chunk when it is executed, rather than at load time?
Enrico