lua-users home
lua-l archive

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


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? Could you please show f? Better still,
a complete (but short) program that fails? The program below works as expected.
--lhf

function sandbox(env, f, ...)
 local g = getfenv(f)
 setfenv(f, env)
 local retval = f(unpack(arg))
 setfenv(f, g)
 return retval
end

function f()
 return DATE
end

function xxx()
 SandBox={}
 SandBox['DATE'] = 20
 DATE = 1
 print(sandbox(SandBox, f))
end

xxx()