API:setfenv

__NOTOC__
setfenv
You can change the environment of a Lua function or the running thread by calling setfenv.
envfunc = setfenv (func, _env)
== Parameters ==
=== Arguments ===
:;func
::function|number - Sets the environment to be used by the given function. func 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.
:;_env
::table - The environment to set the function to. As a special case, when func is 0, setfenv changes the environment of the running thread, and returns no values.
=== Returns ===
:;func
::function - The function in its new environment.
== Example ==
local myScopedFunc = setenv(myFunc, myEnv);
== Notes ==
*Lua docs seem to have 2 contradictory sentiments concerning C functions:
::1) You can only manipulate the environment of C functions through the debug library.
::2) To manipulate the environment of other objects (userdata, C functions, other threads) you must use the C API.
* http://www.lua.org/manual/5.1/manual.html#pdf-setfenv
* http://www.lua.org/manual/5.1/manual.html#2.3
* http://www.lua.org/manual/5.1/manual.html#2.9
* http://www.lua.org/manual/5.1/manual.html#pdf-_G
* http://www.lua.org/manual/5.1/manual.html#pdf-debug.setfenv
* http://www.lua.org/manual/5.1/manual.html#lua_setfenv
== Related Functions ==
* getfenv