lua-users home
lua-l archive

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


Sorry - __G was a typo.  Should be _G.

> Kevin Baca wrote:
> 
> >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
> >  
> >
> I must have been seeing things because now I am back home the code 
> doesn't work? However your code does, however I for me I used 
> a single 
> underscore _G but your code used a double __G. The double did 
> not work 
> for me.
> 
> #!/usr/local/bin/lua
> 
> function compilestring( s )
>         e = setmetatable( {}, { __index = _G } )
>         c = loadstring( s )
>         setfenv( c, e )
>         return c
> end
> 
> c1 = compilestring( "print(x)" )
> c2 = compilestring( "x = 42" )
> c3 = compilestring( "x = 19 print(x)" )
> 
> c1()
> c2()
> c1()
> c3()
> c1()
> 
> [Lua]$ ./x.lua
> nil
> nil
> 19
> nil
> [Lua]$
> 
> getting better, thanks for your help.
>