|
Besides metatables,
objects of types thread, function, and userdata
have another table associated with them,
called their environment.
Like metatables, environments are regular tables and
multiple objects can share the same environment.
Environments associated with userdata have no meaning for Lua.
It is only a convenience feature for programmers to associate a table to
a userdata.
Environments associated with threads are called
global environments.
They are used as the default environment for their threads and
non-nested functions created by the thread
(through loadfile, loadstring or load)
and can be directly accessed by C code (see §3.3).
Environments associated with C functions can be directly
accessed by C code (see §3.3).
They are used as the default environment for other C functions
created by the function.
Environments associated with Lua functions are used to resolve
all accesses to global variables within the function (see §2.3).
They are used as the default environment for other Lua functions
created by the function.
You can change the environment of a Lua function or the
running thread by calling setfenv.
You can get the environment of a Lua function or the running thread
by calling getfenv.
To manipulate the environment of other objects
(userdata, C functions, other threads) you must
use the C API. |