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.
Threads are created sharing the environment of the creating thread.
Userdata and C functions are created sharing the environment
of the creating C function.
Non-nested Lua functions
(created by loadfile , loadstring or load )
are created sharing the environment of the creating thread.
Nested Lua functions are created sharing the environment of
the creating Lua function.
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 threads and
non-nested Lua functions created by the thread
and can be directly accessed by C code (see §3.3).
The environment associated with a C function can be directly
accessed by C code (see §3.3).
It is used as the default environment for other C functions
and userdata 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 nested 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. |