lua-users home
lua-l archive

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


I'm not sure why that's working for you.

What you want is called a "sandbox".  Search the archives for "sandbox"
for some interesting discussions.

Here is a simple example:

> s1 = "x = 42"
> c1 = loadstring( s1 )
> e = setmetatable( {}, { __index = __G } )
> setfenv( c1, e )
> c1()
> =x
nil
> =e.x
42

-Kevin

> -----Original Message-----
> From: lua-bounces@bazar2.conectiva.com.br 
> [mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of 
> Peter Hickman
> Sent: Tuesday, March 30, 2004 12:37 AM
> To: Lua list
> Subject: Re: Controlling scoping
> 
> 
> Kevin Baca wrote:
> 
> >See setfenv() in the lua docs
> >
> >http://www.lua.org/manual/5.0/manual.html#5.1
> >  
> >
> Thanks for the pointer, I now have
> 
> [Lua]$ cat example.lua
> #!/usr/local/bin/lua
> 
> function mycall( f )
>         local _g = getfenv(1)
>         f()
>         setfenv(1, _g)
> end
> 
> s1 = "x = 42"
> s2 = "print(x)"
> 
> c1 = loadstring(s1)
> c2 = loadstring(s2)
> 
> mycall( c2 )
> mycall( c1 )
> mycall( c2 )
> [Lua]$
> 
> Which seems to work, at least for the example.
> 
> Onward and upward
>