load
Loads a chunk by calling a function repeatedly
Prototype
f = load (loader, debugname)
Description
Loads a chunk using function loader to get its pieces. Each call to loader must return a string that concatenates with previous results. A return of nil (or no value) signals the end of the chunk.
If there are no errors, returns the compiled chunk as a function; otherwise, returns nil plus the error message. The environment of the returned function is the global environment.
This could be used to gradually build up a chunk (for example from a network), or to write a preprocessor that reads in a file, preprocess it (like the C preprocessor) and returns the altered code to the Lua interpreter.
The example below shows generating code from entries in a table.
If there are no errors, returns the compiled chunk as a function; otherwise, returns nil plus the error message. The environment of the returned function is the global environment.
This could be used to gradually build up a chunk (for example from a network), or to write a preprocessor that reads in a file, preprocess it (like the C preprocessor) and returns the altered code to the Lua interpreter.
The example below shows generating code from entries in a table.
function getcode (t)
local pos = 0
local function iterator (s)
pos = pos + 1
return t [pos]
end -- iterator
return iterator
end -- getcode
code = {
"local a = 1 ",
"print ('a =', a) ",
"print 'done.'"
}
assert (load (getcode (code))) ()
Lua functions
- assert - Asserts that condition is not nil and not false
- collectgarbage - Collects garbage
- dofile - Executes a Lua file
- error - Raises an error message
- gcinfo - Returns amount of dynamic memory in use
- getfenv - Returns the current environment table
- getmetatable - Returns the metatable for the object
- ipairs - Iterates over a numerically keyed table
- loadfile - Loads a Lua file and parses it
- loadlib - Loads a DLL (obsolete in Lua 5.1)
- loadstring - Compiles a string of Lua code
- module - Creates a Lua module
- next - Returns next key / value pair in a table
- pairs - Traverse all items in a table
- pcall - Calls a function in protected mode
- print - Prints its arguments
- rawequal - Compares two values for equality without invoking metamethods
- rawget - Gets the value of a table item without invoking metamethods
- rawset - Sets the value of a table item without invoking metamethods
- require - Loads a module
- select - Returns items in a list
- setfenv - Sets a function's environment
- setmetatable - Sets the metatable for a table
- tonumber - Converts a string (of the given base) to a number
- tostring - Converts its argument to a string
- type - Returns the type of a variable
- unpack - Unpacks a table into individual items
- xpcall - Calls a function with a custom error handler
Topics
- Lua PCRE regular expression functions
- Lua base functions
- Lua bc (big number) functions
- Lua bit manipulation functions
- Lua coroutine functions
- Lua debug functions
- Lua io functions
- Lua math functions
- Lua os functions
- Lua package functions
- Lua script extensions
- Lua string functions
- Lua syntax
- Lua table functions
- Lua utilities
- Scripting callbacks - plugins
- Scripting