Created
March 28, 2014 16:32
-
-
Save jagt/9837070 to your computer and use it in GitHub Desktop.
lua5.1 setfenv test.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- lua51 setfenv tests | |
function setenv1() | |
-- 1 is the calling function | |
setfenv(1, {foo='1', print=_G.print}) | |
print(foo) -- prints 1 | |
end | |
setenv1() | |
local print = _G.print | |
local setfenv = _G.setfenv | |
local _G = _G | |
local tostring=_G.tostring | |
function setenv2() | |
-- 2 is the direct outer function | |
setfenv(2, {bar='2'}) | |
end | |
setenv2() | |
print(bar) -- prints 2 | |
-- 0 means *thread* environment, and top level function will | |
-- pick up by default. but somehow baz isn't available | |
setfenv(0, {tostring=tostring, print=print, baz=0}) | |
function setenv0() | |
print(baz) -- prints nil? | |
end | |
setenv0() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment