I'm surprised this is an issue - my knowledge of the environments is not complete, and I had always thought subfunctions would be effected. In other words, that it is easy to make a "jail" in Lua.
Lua Lanes would give you one solution, since it allows defining which standard libraries a given lane (OS thread + Lua state) are initialized with. In addition, you'd gain speed when run on multicore processors.
Usage:
require "lanes"
local lane= lanes.prepare( fsub )( ...parameters... ) ...
print( lane[1] ) -- return values
Interested?
To give the substate some standard libs, one can: lanes.prepare( fsub, "os+math" ) lanes.prepare( fsub, "*" )
Graham Wakefield kirjoitti 25.3.2007 kello 1:09: 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
|