API getfenv
Jump to navigation
Jump to search
Returns the current environment in use by the function.
env = getfenv(f)
Arguments
- f
- function or number - A function to retrieve the environment of, or a numeric level of a function on the call stack starting from 1.
Returns
- env
- table? - The environment assigned to the function.
Details
- If the environment of a function has a metatable applied with the
__environment
field set, this function will instead return the value assigned to that key. - This function always returns nil if called inside a
__gc
finalizer.
Patch changes
Patch 10.1.0 (2023-05-02): Now returns nil if called inside a __gc
finalizer.
Patch 1.1.0 (2004-11-07): Added.