[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Lua 5.2 environments and changing a loaded file environment
- From: Patrick Donnelly <batrick@...>
- Date: Mon, 9 Aug 2010 07:43:35 -0400
Hello List,
I'm trying to decide how best to change the environment of a loaded
"script" file such that it can be given a brand new environment and
re-executed multiple times without recompiling the script (loadin).
Obviously the most straightforward method would be to do something
like:
local f = assert(loadfile(file))
while true do
-- ...
debug.setupvalue(f, 1, {})
f()
-- ...
end
However, this actually won't work because the f closure will "share"
the upvalue across invocations. Closures created by the first
invocation will use the new table set for the second invocation as
their _ENV. (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?
--
- Patrick Donnelly