--[[
Returns the current environment in use by the function. f can be a Lua function or a number that specifies the function at that stack level: Level 1 is the function calling getfenv.
If the given function is not a Lua function, or if f is 0, getfenv returns the global environment. The default for f is 1.
]]
f = 4
function a()
f = "a"
print( getfenv(b).f, getfenv(0).f, getfenv(1).f, getfenv(2).f, getfenv(3).f)
end
A = {}
setmetatable(A, {__index = _G})
setfenv(a, A)
--print(getfenv(a).f)
function b()
f = "b"
A.a()
end
B = {}
setmetatable(B, {__index = _G})
setfenv(b, B)
function c()
f = "c"
B.b()
end
C = {}
setmetatable(C, {__index = _G})
setfenv(c, C)
c()
print(f, A.f, B.f, C.f )
Returns the current environment in use by the function. f can be a Lua function or a number that specifies the function at that stack level: Level 1 is the function calling getfenv.
If the given function is not a Lua function, or if f is 0, getfenv returns the global environment. The default for f is 1.
]]
f = 4
function a()
f = "a"
print( getfenv(b).f, getfenv(0).f, getfenv(1).f, getfenv(2).f, getfenv(3).f)
end
A = {}
setmetatable(A, {__index = _G})
setfenv(a, A)
--print(getfenv(a).f)
function b()
f = "b"
A.a()
end
B = {}
setmetatable(B, {__index = _G})
setfenv(b, B)
function c()
f = "c"
B.b()
end
C = {}
setmetatable(C, {__index = _G})
setfenv(c, C)
c()
print(f, A.f, B.f, C.f )