lua-users home
lua-l archive

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


Luiz Henrique de Figueiredo wrote:

> Max Ischenko wrote:

> >function xxx()
> >	SandBox['DATE'] = 1
> >	DATE = 1
> >	SandBox['Date'] = Date
> >	local outcome = sandbox(SandBox, f)
> >	return outcome
> >
> >Function f, called by xxx, refers to global variable DATE.
> >The problem is that SandBox.DATEE is not used (and __index is not
> >called) when f is evaluated so I have to assign to global DATEE before
> >calling f. 

> Do you mean DATE instead of DATEE?
Yes, sorry, s/DATEE/DATE/.

> Could you please show f? Better
> still, a complete (but short) program that fails? The program below
> works as expected.

[ ... skip ... ]

OK, here it is. It prints nil when line 17 is uncommented and prints 1
when line 19 is uncommented (and line 17 is commented out).
If I change line 23 into f = equals() then it prints 20, as expected.

     1   
     2  
     3  function sandbox(env, f, ...)
     4   local g = getfenv(f)
     5   setfenv(f, env)
     6   local retval = f(unpack(arg))
     7   setfenv(f, g)
     8   return retval
     9  end
    10  
    11  function equals()
    12          return DATE
    13  end
    14  
    15  function xxx(f)
    16   SandBox={}
    17   SandBox['DATE'] = 20
    18   SandBox['equals'] = equals
    19   --DATE = 1
    20   print(sandbox(SandBox, f))
    21  end
    22  
    23  f = loadstring("return equals()")
    24  xxx(f)




-- 
Regards, max.