local e={print=print, load=load, a='2'}
e._G=e
load(
[[
print(a)
load("print(a)")()
]]
, '', '', e)()
--expected result
2
2
--but in reality
2
1
Why does "load" not get the environment for the compiled chunk from the environment?
local e={print=print, load=load, a='2'}
e._G=e
load(
[[
print(a)
load("print(a)")()
]]
, '', '', e)()
--expected result
2
2
--but in reality
2
1
Why does "load" not get the environment for the compiled chunk from the environment?
http://www.lua.org/manual/5.2/manual.html#pdf-load
I.e. no matter where you are now in terms of
_ENV
, missingenv
argument toload
will connect loaded chunk to very global environment.If you want the loaded source in 5.2 to inherit new environment by default, substitute
load
/loadfile
functions: