lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Thu, Feb 11, 2010 at 12:17 AM, Nick Gammon <nick@gammon.com.au> wrote:
> This proposed solution assumes you want io.open to be available. If I want to replicate:
>
> func = loadfile(path)
> setfenv(func, env)
> func()
>
> ... but have sandboxed out io.open, is there another solution?

If you're the one implementing the sandbox, then make available to the
sandbox your own implementation of loadfile which allows for custom
environments. My personal solution is the loadfile_envcall function
defined in http://code.google.com/p/corsix-th/source/browse/trunk/CorsixTH/Lua/utility.lua#85
which is a Lua 5.1 and 5.2 compatible function which acts like
loadfile, but the returned function takes it's environment as the
first parameter. You could easily modify this to have a separate
function for setting the environment on the result of a loadfile
rather than giving the environment every time.