Expand description
Reference for the Lua 5.5 standard library.
Lua is a dynamically typed language: values carry types, variables do not.
Every value belongs to one of the basic types documented here — nil,
boolean, number, string, table, function, userdata
and thread.
The standard library provides a set of global functions (listed below),
functions attached to the string and table types, and the
math, os, io, coroutine and utf8 libraries.
The behaviour of operators on values can be customised through metatables — see the Metatables section below.
print("hello, world")Lua and its logo are from Lua.org — logo © 1998 Lua.org, graphic design by Alexandre Nakonechnyj.
Modules§
- coroutine
- The coroutine library.
- io
- The input and output library.
- math
- The mathematical library.
- os
- The operating system library.
- utf8
- Basic support for UTF-8 encoding.
Structs§
- any
- Stands for a value of any type.
- boolean
- The type of the truth values true and false.
- function
- The type of Lua and C functions.
- nil
- The type of the absence of a value.
- number
- The type of numeric values.
- string
- The type of immutable sequences of bytes.
- table
- The type of associative arrays — the sole data-structuring mechanism in Lua.
- thread
- The type of independent threads of execution, used to implement coroutines.
- userdata
- The type of arbitrary C data.
Statics§
- _G
- A global variable that holds the global environment itself.
- _VERSION
- A global variable containing the running Lua version, as a string —
currently
"Lua 5.5".
Traits§
- arithmetic
- Metamethods controlling the arithmetic operators.
- bitwise
- Metamethods controlling the bitwise operators.
- callable
- Metamethod making a value callable.
- comparison
- Metamethods controlling the comparison operators.
- concatenation
- Metamethod controlling the concatenation operator.
- indexing
- Metamethods controlling indexed access.
- length
- Metamethod controlling the length operator.
- lifecycle
- Metamethods controlling the life cycle of a value.
- tostring
- Metamethods controlling string conversion.
Functions§
- assert
- Raises an error if the value of its argument
vis false (i.e.nilorfalse); otherwise, returns all its arguments. On error,messageis used as the error object; when absent, it defaults to"assertion failed!". - collectgarbage
- A generic interface to the garbage collector. Performs different jobs
according to
opt:"collect"runs a full collection cycle (the default),"stop"and"restart"pause and resume the collector,"count"returns the total memory in use by Lua in kilobytes,"step"performs an incremental step,"isrunning"tells whether the collector is stopped, and"incremental"/"generational"select the collector mode. - dofile
- Opens the named file and executes its contents as a Lua chunk. When
called without arguments, executes the contents of the standard input.
Returns all values returned by the chunk. In case of errors,
dofilepropagates the error to its caller: it does not run in protected mode. - error
- Raises an error with
messageas the error object. This function never returns. - getmetatable
- If
objectdoes not have a metatable, returnsnil. Otherwise, if the metatable has a__metatablefield, returns the associated value. Otherwise, returns the metatable of the given object. - inspect
extended - Renders any value as a human-readable string. Extended library —
feature
extended. In the spirit of the widely usedinspect.lua. - ipairs
- Returns three values (an iterator function,
t, and 0) so that the construction - load
- Loads a chunk. If
chunkis a string, the chunk is that string; if it is a function,loadcalls it repeatedly to get the chunk pieces. If there are no syntactic errors, returns the compiled chunk as a function; otherwise, returnsnilplus the error message. - next
- Allows a program to traverse all fields of a table. Its first argument
is a table and its second argument is an index in this table. A call
returns the next index of the table and its associated value; when
called with
nilas its second argument, returns an initial index and its associated value. When called with the last index, or withnilin an empty table, returnsnil. - pairs
- Returns three values (the
nextfunction, the tablet, andnil) so that the construction - pcall
- Calls the function
fwith the given arguments in protected mode: errors insidefare not propagated. Returns a status code (a boolean) followed, on success, by all results of the call, or, on failure, by the error object. - Receives any number of arguments and prints their values to the
standard output, converting each to a string following the same rules
as
tostring(), separated by tabs and followed by a newline. - rawequal
- Checks whether
v1is equal tov2, without invoking the__eqmetamethod. Returns a boolean. - rawget
- Gets the real value of
t[index], without using the__indexmetamethod.tmust be a table. - rawlen
- Returns the length of the object
v, which must be a table or a string, without invoking the__lenmetamethod. - rawset
- Sets the real value of
t[index]tovalue, without using the__newindexmetamethod.tmust be a table,indexany value different fromniland NaN. Returnst. - require
- Loads the given module. Starts by looking into the
package.loadedtable to determine whethermodnameis already loaded; if so, returns the stored value. Otherwise, finds a loader for the module by consulting the searchers inpackage.searchers, runs it, and caches its result inpackage.loaded[modname]. - select
- If
indexis a number, returns all arguments after argument numberindex; a negative number indexes from the end (−1 is the last argument). Otherwise,indexmust be the string"#", andselectreturns the total number of extra arguments it received. - setmetatable
- Sets the metatable for the given table. If
metatableisnil, removes the metatable of the table. If the original metatable has a__metatablefield, raises an error. - tonumber
- When called with no
base, tries to convert its argument to a number. If the argument is already a number or a string convertible to a number, returns that number; otherwise, returnsnil. - tostring
- Receives a value of any type and converts it to a string in a human-readable format.
- type
- Returns the type of its only argument, coded as a string. The possible
results are
"nil","boolean","number","string","table","function","userdata"and"thread". - xpcall
- Like
pcall, but setsmsghas a message handler: in case of error,msghis called with the error object before the stack unwinds, and its return value becomes the error object returned byxpcall.