setfenv
Lua function

setfenv

Summary

Sets a function's environment

Prototype

f = setfenv (f)



Description

Sets the current environment to be used by f, which can be a function, userdata, thread or stack level. Level 1 is the current function. Level 0 is the global environment of the current thread.

The return value is the function whose environment was changed, unless the argument was 0.


function f (v)
  test = v  -- assign to global variable test
end -- function f

local myenv = {}  -- my local environment table
setfenv (f, myenv)  -- change environment for function f
f (42)  -- call f with new environment

print (test) --> nil  (global test was not changed)
print (myenv.test)  --> 42  (test inside myenv was changed)


This can be used as a form of "sandbox", so that functions can run in an environment where they do not have access to normal global variables. For example, in the above code, you could not call "print" from inside function f, as print is not in the environment. To make it accessible you might do this:


myenv.print = print -- copy print function into environment table


A nice use of the setfenv function is to limit the damage that can be done when reading an external file.
An example is to let the user of your script provide a configuration file, that you want to read in. An example might be:


a = 42
b = "nick"
c = { "the", "quick", "brown", "fox" }


A quick way of processing that file would be to: dofile ("config.txt")
However if the "config.txt" file contained malicious code (like: os.remove "myapplication.exe") then it could have undesired side-effects. Even a simple assignment like: 'print = nil' could cause problems. The solution is to use setfenv to limit the global environment for this file, like this:


config = {} -- empty environment table
-- load the file, get a function to execute  
local f = assert (loadfile ("config.txt"))
-- want to load file into the config table
setfenv (f, config)
-- load it
f ()
print (config.a)  --> 42 


What this does is change the global environment for the function returned from loadfile to be the empty config table. This means that all "global" variables will be relative to config, not _G. It also means that all the standard functions (like os.remove) are not visible.



See Also ...

Topics

DOC_lua_base Lua base functions
DOC_lua_bc Lua bc (big number) functions
DOC_lua_bit Lua bit manipulation functions
DOC_lua_coroutines Lua coroutine functions
DOC_lua_debug Lua debug functions
DOC_lua_io Lua io functions
DOC_lua_math Lua math functions
DOC_lua_os Lua os functions
DOC_lua_package Lua package functions
DOC_lua_rex Lua PCRE regular expression functions
DOC_lua Lua script extensions
DOC_lua_string Lua string functions
DOC_lua_tables Lua table functions
DOC_lua_utils Lua utilities
DOC_scripting Scripting

Lua functions

LUA_assert assert (Asserts that condition is not nil and not false)
LUA_collectgarbage collectgarbage (Collects garbage)
LUA_dofile dofile (Executes a Lua file)
LUA_error error (Raises an error message)
LUA_gcinfo gcinfo (Returns amount of dynamic memory in use)
LUA_getfenv getfenv (Returns the current environment table)
LUA_getmetatable getmetatable (Returns the metatable for the object)
LUA_ipairs ipairs (Iterates over a numerically keyed table)
LUA_load load (Loads a chunk by calling a function repeatedly)
LUA_loadfile loadfile (Loads a Lua file and parses it)
LUA_loadlib loadlib (Loads a DLL (obsolete in Lua 5.1))
LUA_loadstring loadstring (Compiles a string of Lua code)
LUA_module module (Creates a Lua module)
LUA_next next (Returns next key / value pair in a table)
LUA_pairs pairs (Traverse all items in a table)
LUA_pcall pcall (Calls a function in protected mode)
LUA_print print (Prints its arguments)
LUA_rawequal rawequal (Compares two values for equality without invoking metamethods)
LUA_rawget rawget (Gets the value of a table item without invoking metamethods)
LUA_rawset rawset (Sets the value of a table item without invoking metamethods)
LUA_require require (Loads a module)
LUA_select select (Returns items in a list)
LUA_setmetatable setmetatable (Sets the metatable for a table)
LUA_tonumber tonumber (Converts a string (of the given base) to a number)
LUA_tostring tostring (Converts its argument to a string)
LUA_type type (Returns the type of a variable)
LUA_unpack unpack (Unpacks a table into individual items)
LUA_xpcall xpcall (Calls a function with a custom error handler)

(Help topic: lua=setfenv)

DOC_contents Documentation contents page