[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: setfenv/getfenv
- From: Nick Gammon <nick@...>
- Date: Thu, 11 Feb 2010 14:36:05 +1100
On 11/02/2010, at 1:19 PM, Peter Cawley wrote:
> If you're the one implementing the sandbox, then make available to the
> sandbox your own implementation of loadfile which allows for custom
> environments.
Thanks for the suggestion.
I want to point out to the Lua developers however that any solution involving the io library (as yours does) means that to do something simple like load a configuration file you now need to move from "core" Lua (loadfile and setfenv were in the base library) into incorporating the "io" library as well, something you may not want to do.
Here's an example of what I am talking about, using Lua to load up a program configuration, which might look like this:
--------
width = 100
height = 50
players = 2
print = false
--------
-- load configuration
local config = {}
local f = assert (loadfile ("config.lua"))
setfenv (f, config) ()
-- use it
print (config.width, config.height, config.print)
Now that simple usefulness seems to be gone.
If I may suggest, a new function like "loadfilein" which lets you specify an environment? Or modify loadfile to accept an environment as well as a file name.
- Nick