This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Konlab's profile picture

Environments

Started by Konlab, 01 September 2015 - 06:22 PM
Konlab #1
Posted 01 September 2015 - 08:22 PM
I have some questions about environments:
1. ik how to set an environment globally but how do u set it to one function?
2. What the getfenv's first argument does?
3. What the setfenv's number argument does, and do I have to use it?
4. How can I set/get environments if get/setfenv were removed?
Thanks!
And sry 4 grammatical errors i dont have a lot of time now …
Exerro #2
Posted 01 September 2015 - 08:36 PM
I'll talk about Lua 5.1 for now, since you're referring to (get/set)fenv() which aren't in any Lua past 5.1.

To set the environment of a function, you use

setfenv( f, env )
Where f is the function and env is the table environment.

Setfenv also works with levels, as well as functions, similar to error() and getfenv().
Spoiler

Level 1 is the current function, level 2 is the caller, level 3 is the caller's caller, and so on. Getfenv works like setfenv(). You can pass it a function (like I did in that screenshot) and it will return that function's environment, or you can pass in a level to get (something being called)'s environment, for example getfenv(2) being the caller's environment.

To set and get environments when the two functions are removed… well, depends. If you are the one loading the code, it's relatively easy. Otherwise, it's impossible. If there is a function object loaded in some environment you have no control over, there is no way to get/modify its environment without (get/set)fenv. However, if you are the one that loads it, you can give it a custom environment.

local environments = {}
local function newloadstring( str, src, bin, env )
    local penv = setmetatable( {}, { __index = env } )
    local f, err = load( str, src, bin, penv )
    if not f then return nil, err end
    environments[f] = penv
    return f
end
local function newsetfenv( f, env )
    if not environments[f] then error "that function was not loaded with newloadstring()" end
    getmetatable( environments[f] ).__index = env
end
local function newgetfenv( f )
    if not environments[f] then error "that function was not loaded with newloadstring()" end
    return getmetatable( environments[f] ).__index
end

That code will allow you to set/get the environment of a function you've loaded with newloadstring(). Hopefully you can see how it works. The cool thing about this is that you can actually set a function environment to a function :o/>.
KingofGamesYami #3
Posted 01 September 2015 - 08:48 PM
1. setfenv( func, tEnv )
2. getfenv( func ) returns the supplied functions environment.
3.
Reference Manual said:
setfenv (f, table)

Sets the environment to be used by the given function. f can be a Lua function or a number that specifies the function at that stack level: Level 1 is the function calling setfenv. setfenv returns the given function.

4. You can't, not as easily… Don't have time to elaborate now, sorry.
Konlab #4
Posted 02 September 2015 - 05:49 AM
Thanks for the fast reply!
I understand them now.

What are str/src/bin vars?
Edited on 02 September 2015 - 04:00 AM
Bomb Bloke #5
Posted 02 September 2015 - 07:05 AM
According to this, they're tied in with debugging functions. I'm uncertain whether they're be entirely usable under whatever setup Dan has planned.
Konlab #6
Posted 02 September 2015 - 08:30 AM
Thanks for helping!