Well I wouldn't propose it as the default behaviour at all, but it would be nice to be able to create 'environment-less' functions in addition to Lua's functions with bound environments.
I didn't know about debug.sethook, but looking at it now, it seems that it would affect ALL function calls (and I suppose will be expensive too).
perhaps a wrapper function:
function envwrap(f)
setfenv(f, getfenv(1))
return f()
end
or more correctly
function envwrap(f)
local e = getfenv(f)
setfenv(f, getfenv(1))
args = {f()}
setfenv(f, e)
return unpack(args)
end
and in use:
fsub = function()
print("fsub", getfenv(0), getfenv(1))
b = 1
end
fmain = function()
print("fmain", getfenv(0), getfenv(1))
a = 1
envwrap(fsub())
end
But I feel like I'm doing a lot of expensive workaround, where a simpler 'environment-less' function would suffice.
On Mar 24, 2007, at 4:00 PM, Tomas Guisasola Gorham wrote:
Hi Graham
Is there any way to set the environment of a function such that all
functions it calls will also inherit this environment? In the
example below, I change the environment of fmain, which in turn calls
fsub, but the environment of fsub is unchanged.
I don't think so. Anyway I think it should became a nightmare
if this became the default behavior. Good luck!
By the way, did you try to set the call hook to do that change
of environments?
Again, good luck,
Tomás
Grrr Waaa
www.grahamwakefield.net