_ENV implemented in Lua 5.1

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

_ENV implemented in Lua 5.1

David Manura
-- foo.lua
local function g(_ENV) print(x) end
g {print=print, x=math.sqrt(4)}
_ENV = {print=print, math=math, x=5}
print(x)

$ lua51 -v -e 'require"compat_envvar":install_searcher()' -lfoo
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
2
5

-- This is what internally gets passed to loadstring():
local _ENV = ...; return function(...) local function g(_ENV) _ENV.print(_ENV.x) end
g {print=_ENV.print, x=_ENV.math.sqrt(4)}
_ENV = {print=_ENV.print, math=_ENV.math, x=5}
_ENV.print(_ENV.x)
end

:)  This is presently implemented in [1], though might be more integrated into [2] for furthering [3].