lua-users home
lua-l archive

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


Hello,

On Tue, 20 Mar 2001, Jean Claude Morlier wrote:
>
> What's the usage of globals([table]) ? 
> In the example above I try to assign my own globals but this don't work.
> I need to set for a time myGlobals and after restore initial globals.
> May be I don't do this in a fine way.

  Your example didn't work because you didn't save enough
information to lua work properly. It isn't enough to save
'print', 'dostring' and 'globals'. For example, how could
lua traverse a table without function 'next'? (anybody, 
if I'm wrong, please correct me).

  In order to this work properly, you should save the hole
context to your table 'myGlobals', and then do whatever you
have to. (see the corrections in the code below).

> a = 1
> b = 2
> 
> function x(l)
>   print('x Start')
>   local c = 8
>   l.f = function(v) 	 print('function f () v = ' .. v)  end
>   l.c = c

-- you won't need to copy these 3 funtions
>   l.print = print
>   l.dostring = dostring
>   l.globals = globals

>   l.print("before globals(l)")
>   l.savglobals = globals()
>   globals(l)
>   f(a)
> 
>   print("after  globals(l)")
>   print('before dostring');
>   dostring('print("in dostring"); f(a); f(b); f(c); ')	 
>   globals(savglobals)
>   print("x end")
> end
> 
> myGlobals = {}

-- copy hole context to myGlobals here
myGlobals = globals()

> myGlobals.a = a
> myGlobals.b = b
> x(myGlobals)

  Hope I helped somehow...

  Cheers,
  -- Diogo.