3

So I have a Lua script in which I set the environment of a function and define other functions that I make available in the environment. The issue is that when I call the function I did setfenv on, any function it calls within the environment uses the global _G environment to lookup values as opposed to the environment I set when calling the initial function. I have replicated the issue below:

> function f() print(t) end
> t = 5
> f()
5
> env = {}
> env['print'] = print
> env['t'] = 7
> env['f'] = f
> setfenv(f, env)
> f()
7
> setfenv(f, _G)
> f()
5
> function g() f() end
> g()
5
> setfenv(g, env)
> g()
5

Is there a way to get the environment to propagate to all calls without having to do setfenv on every function I want to add to env (basically, so that the last call in the example to g() would return 7 instead of 5)?

Using Lua 5.1.

Colonel Thirty Two
26.9k9 gold badges52 silver badges94 bronze badges
asked Oct 13, 2015 at 15:39
2
  • Function environments in Lua 5.1 are not inherited by function invocation (you have to manually call setfenv before calling that function to solve your problem). New environment system (since 5.2, using _ENV) makes correct inheritance easily possible. Commented Oct 13, 2015 at 15:50
  • Is there no way to replicate this behaviour in 5.1? Commented Oct 13, 2015 at 16:03

1 Answer 1

1

You can write a function so that it automatically changes its own environment on every call

-- This code works on Lua 5.1, 5.2, 5.3
local getfenv = getfenv or function() end
local setfenv = setfenv or getfenv
a = 1
function f(env)
 local _ENV = env or getfenv(2) or _ENV; setfenv(1, _ENV)
 print(a)
end
env = { a = 2, print = print }
-- inherit environment for this call
f() --> 1
-- set special environment for this call
f(env) --> 2
f() --> 1

Or simply setfenv(1, getfenv(2)) if you are working only with Lua 5.1 and want to ALWAYS inherit environment.

answered Oct 14, 2015 at 10:17
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.