User:Anomie/Lua reference manual

Introduction
This manual documents Lua as it is used in MediaWiki with the Scribunto extension. Some parts are derived from the Lua 5.1 reference manual, which is available under an MIT-style license.

This derivative manual may also be copied under the terms of the same license.

Getting started
On a MediaWiki wiki with Lua enabled, create a page with a title starting with "Module:", for example "Module:Bananas". Into this new page, copy the following text:

Save that, then on another (non-module) page, write:

Except that you should replace "Bananas" with whatever you called your module. This will call the "hello" function exported from that module. The will be replaced with the text that the function returned, in this case, "Hello, world!"

Tokens
Names (also called identifiers) in Lua can be any string of letters, digits, and underscores, not beginning with a digit. Names are case-sensitive; "foo", "Foo", and "FOO" are all different names.

The following keywords are reserved, and may not be used as names:  Names starting with an underscore followed by uppercase letters are reserved for internal Lua global variables.
 * and
 * break
 * do
 * else
 * elseif
 * end
 * false
 * for
 * function
 * if
 * in
 * local
 * nil
 * not
 * or
 * repeat
 * return
 * then
 * true
 * until
 * while

Other tokens are: 
 * &#x25;
 * &#3a;
 * &#3b;
 * ]
 * }
 * &#3a;
 * &#3b;
 * ]
 * }
 * &#3a;
 * &#3b;
 * ]
 * }
 * &#3b;
 * ]
 * }
 * ]
 * }
 * ]
 * }
 * ]
 * }
 * }
 * }
 * }

Comments
A comment starts with a  anywhere outside a string. If the  is immediately followed by an opening long bracket, the comment continues to the corresponding closing long bracket; otherwise the comment runs to the end of the current line.

Data types
Lua is a dynamically-typed language, which means that varibles and function arguments have no type, only the values assigned to them. All values carry a type.

Lua has eight basic data types, however only six are relevant to the Scribunto extension. The  function will return the type of a value.

The  function will convert a value to a string. The  function will convert a value to a number if possible, and otherwise will return nil. There are no explicit functions to convert a value to other data types.

Numbers are automatically converted to strings when used where a string is expected, e.g. when used with the concatenation operator. Strings recognized by  are automatically converted to numbers when used with arithmetic operators. When a boolean value is expected, all values other than nil and false are considered to be true.

nil
"Nil" is the data type of, which exists to represent the absence of a value. Nil is the only data type that may not be used as a key in a table, and there is no difference between an unassigned table key and a key assigned a nil value.

When converted to a string, the result is "nil". When converted to boolean, nil is considered false.

boolean
Boolean values are  and.

When converted to a string, the result is "true" or "false". Unlike many other languages, boolean values may not be directly converted to numbers. And unlike many other languages, only false and nil are considered false for boolean conversion; the number 0 and the empty string are both considered true.

string
Lua strings are considered a series of 8-bit bytes; it is up to the application to interpret them in any particular encoding.

String literals may be delimited by either single or double quotes ( or  ); like JavaScrpit and unlike PHP, there is no difference between the two. The following escape sequences are recognized: '\a' (bell), '\b' (backspace), '\f' (form feed), '\n' (newline), '\r' (carriage return), '\t' (horizontal tab), '\v' (vertical tab), '\\' (backslash), '\"' (double quote), and '\ (single quote). A literal newline may also be included in a string by preceeding it with a backslash. Bytes may also be specified using an escape sequence '\ddd', where ddd'' is the decimal value of the byte in the range 0–255. To include Unicode characters using escape sequences, the individual bytes for the UTF-8 encoding must be specified; in general, it will be more straightforward to enter the Unicode characters directly.

Literal strings can also be defined using long brackets. An opening long bracket consists of an opening square bracket followed by zero or more equal signs followed by another opening square bracket, e.g.,  , or. The opening long bracket must be matched by the corresponding closing long bracket, e.g.,  , or. Strings delimited by long brackets do not interpret escape sequences. As a special case, if an opening long bracket is immediately followed by a newline then the newline is not included in the string.

Note that all strings are considered true when converted to boolean. This is unlike most other languages, where the empty string is usually considered false.

number
Lua has only one numeric type, which is typically represented internally as a double-precision floating-point value. In this format, integers between -9007199254740992 and 9007199254740992 may be represented exactly, while higher and lower numbers will suffer from round-off error.

Numbers are specified using a period as a decimal separator and without grouping separators, e.g.  . Numbers may also be represented using E notation without spaces, e.g. ,  , or. Integers may also be specified in hexidecimal notation using a  prefix, e.g..

Although NaN and positive and negative infinities are correctly stored and handled, Lua does not provide corresponding literals. The constant  is positive infinity, as is a division such as , and a division such as   may be used to quickly generate a NaN.

Note that all numbers are considered true when converted to boolean. This is unlike most other languages, where the number 0 is usually considered false. When converted to a string, finite numbers are represented in decimal, possibly in E notation; NaN is "nan" or "-nan"; and infinities are "inf" or "-inf".

table
Lua tables are associative arrays, much like PHP arrays and JavaScript objects.

Tables are created using curly braces. The empty table is. To populate fields on creation, a comma- and/or semicolon-separated list of field specifiers may be included in the braces. These take any of several forms:
 * uses the (first) value of expression1 as the key and the (first) value of expression2 as the value.
 * is equivalent to
 * is roughly equivalent to, where i is an integer starting at 1 and incrementing with each field specification of this form. If this is the last field specifier and the expression has multiple values, all values are used; otherwise only the first is kept.

The fields in a table are accessed using bracket notation, e.g. . String keys that are also valid names may also be accessed using dot notation, e.g.   is equivalent to. Calling a function that is a value in the table may use colon notation, e.g., which is equivalent to.

A sequence is a table with non-nil values for all positive integers from 1 to N and no value (nil) for all positive integers greater than N. Many Lua functions operate only on sequences, and ignore non-positive-integer keys.

Unlike PHP or JavaScript, however, any value except nil and NaN may be used as a key. These are all valid and distinct:

Similarly, any value except nil may be stored as a value in a table. Storing nil is equivalent to deleting the key from the table, and accessing any key that has not been set will result in a nil value.

Note that tables are never implicitly copied in Lua; if a table is passed as an argument to the function and the function manipulates the keys or values in the table, those changes will be visible in the caller.

When converted to a string, the result is "table". Even the empty table is considered true as a boolean.

function
Functions in Lua are first-class values: they may be created anonymously, passed as arguments, assigned to variables, and so on.

Functions are created using the  keyword, and called using parentheses. Syntactic sugar is available for named functions, local functions, and functions that act like member functions to a table. See and  below for details.

Lua functions are closures, meaning that they maintain a reference to the scope in which they are declared and can access and manipulate variables in that scope.

Like tables, if a function is assigned to a different variable or passed as an argument to another function, it is still the same underlying "function object" that will be called.

When converted to a string, the result is "function".

Unsupported types
The userdata type is used to hold opaque values for extensions to Lua written in other languages; for example, a userdata might be used to hold a C pointer or struct. To allow for use of Scribunto in hosting environments where custom-compiled code is not allowed, no such extensions are used.

The thread type represents the handles for coroutines, which are not available in Scribunto's sandbox.

Metatables
Every table may have an associated table known as a metatable. The fields in the metatable are used by some operators and functions to specify different or fallback behavior for the table. The metatable for a table may be accessed using the getmetatable function, and set with the setmetatable function.

Metatable fields are accessed as if with rawget.

Metatable fields that affect the table itself are:
 * __index : This is used when a table access  would return nil. If the value of this field is a table, the access will be repeated in that table, i.e.  . If the value of this field is a function, the function will be called as  . The rawget function bypasses this metamethod.
 * __newindex : This is used when assigning a key to a table  where   would return nil. If the value of this field is a table, the assignment will be repeated in that table, i.e.  . If the value of this field is a function, the function will be called as  . The rawset function bypasses this metamethod.
 * __call : This is used when function call syntax is used on a table, . The value must be a function, which is called as something like.
 * __mode : This is used to make tables holding weak references. The value must be a string. By default, any value that is used as a key or as a value in a table will not be garbage collected. But if this field contains the letter 'k', keys may be garbage collected, and if it contains 'v' values may be; in either case, both the corresponding key and value are removed from the table. Note that behavior is undefined if this field is altered after the table is used as a metatable.

Other metatable fields include: † For binary operators, Lua looks first at the left argument's metatable (if any) then the right's when looking for a metamethod to use. ‡ For relational operators, the metamethod is only used if the same function is specified in both arguments' metatables. Different anonymous functions, even with identical body and closure, may not be considered the same. * __metatable affects both getmetatable and setmetatable
 * __add†
 * __sub†
 * __mul†
 * __div†
 * __mod†
 * __pow†
 * __unm
 * __concat†
 * __eq‡
 * __lt‡
 * __le‡
 * __pairs
 * __ipairs
 * __metatable*
 * __tostring

Note: In Lua, all strings also share a single metatable, in which __index refers to the  table. This metatable is not accessible in Scribunto, nor is the referenced  table; the string table available to modules is a copy.

Variables
Variables are places that store values. There are three kinds of variables in Lua: global variables, local variables, and table fields.

A name represents a global or local variable (or a function argument, which is just a kind of local variable). Variables are assumed to be global unless explicitly declared as local using the  keyword. Any variable that has not been assigned a value is considered to have a nil value.

Global variables are stored in a standard Lua table called an environment; this table is often available as the global variable. It is possible to set a metatable for this global variable table; the __index and __newindex metamethods will be called for accesses of and assignments to global variables just as they would for accesses of and assignments to fields in any other table.

The environment for a function may be accessed using the getfenv function and changed using the setfenv function; in Scribunto, these functions are severely restricted if they are available at all.

Expressions
An expression is something that has values: literals (numbers, strings, true, false, nil), anonymous function declarations, table constructors, variable references, function calls, the vararg expression, expressions wrapped in parentheses, and unary operators applied to expressions, and expressions combined with binary operators.

Most expressions have one value; function calls and the vararg expression can have any number. Note that wrapping a function call or vararg expression in parentheses will lose all except the first value.

Expression lists are comma-separated lists of expressions. All except the last expression are forced to one value (dropping additional values, or using nil if the expression has no values); all values from the last expression are included in the values of the expression list.

Arithmetic operators
Lua supports the usual binary arithmetic operators: addition, subtraction, multiplication, division, modulo, exponentiation, and negation.

When all operands are numbers or strings for which tonumber returns non-nil, the operations have their usual meaning. Note that exponentiation will work correctly with non-integer exponents, and that modulo is defined as.

If either operand is a table with an appropriate metamethod, the metamethod will be called.

Relational operators
The relational operators in Lua are,  ,  ,  ,  , and. The result of a relational operator is always a boolean.

Equality first compares the types of its operands; if they are different, the result is false. Then it compares the values: nil, boolean, number, and string are compared in the expected manner. Functions are equal if they refer to the exact same function;  will return false, as it is comparing two different anonymous functions. Tables are by default compared in the same manner, but this may be changed using the __eq metamethod.

Inequality is the exact negation of equality.

For the ordering operators, if both are numbers or both are strings, they are compared directly. Next, metamethods are checked:  uses __lt;   uses __le if available, or if __lt is available than   is considered equivalent to  ;   is considered equivalent to  ; and   is considered equivalent to. If the necessary metamethods are not available, an error is raised.

Logical operators
The logical operators are,  , and. All use the standard interpretation where nil and false are considered false and anything else is considered true.

For, if the left operand is considered false then it is returned and the second operand is not evaluated; othewise the second operand is returned.

For, if the left operand is considered true then it is returned and the second operand is not evaluated; othewise the second operand is returned.

For, the result is always true or false.

Note that  and   short circuit. For example,  will only call   if   returns false.

Concatenation operator
The concatenation operator is two dots, used as. If both operands are numbers or strings, they are converted to strings and concatenated. Otherwise if a __concat metamethod is availabe, it is used. Otherwise, an error is raised.

Note that Lua strings are immutable and Lua does not provide any sort of "string builder", so a loop that repeatedly does  will have to create a new string for each iteration and eventually garbage-collect the old strings. If many strings need concatenating, it may be faster to use string.format or to insert all the strings into a and use table.concat at the end.

Length operator
The length operator is, used as. If  is a string, it returns the length in bytes. If  is a, it returns the length of the sequence.

If  is a table that is not a sequence, the   may return any value N such that a[N] is not nil and a[N+1] is nil, even if there are non-nil values at higher indexes. For example,

Operator precedence
Lua's operator precedence, from highest to lowest:


 * not # - (negation)
 * + - (subtraction)
 * and
 * or
 * and
 * or
 * and
 * or

Within a precedence level, most binary operators are left-associative, i.e.  is interpreted as. Exponentiation and concatenation are right-associative, i.e.  is interpreted as.

Function calls
FILL IN

Function declarations
FILL IN

Statements
A statement is the basic unit of execution: one assignment, control structure, function call, variable delcaration, etc.

A chunk is a sequence of statements, optionally separated by semicolons. A chunk is basically considered the body of an anonymous function, so it can declare local variables, receive arguments, and return values.

A block is also a sequence of statements, just like a chunk. A block can be delimited to create a single statement:. These may be used to limit the scope of local variables, or to add a  or   in the middle of another block.

Assignments
The variable-list is a comma-separated list of variables; the expression-list is a comma-separated list of one or more expressions. All expressions are evaluated before any assignments are performed, so  will swap the values of a and b.

Local variable declarations
Local variables may be declared anywhere within a block. The first form, without an expression list, declares the variables but does not assign a value so all variables have nil as a value. The second form assigns values to the local variables, as described in above.

Note that visibility of the local variable begins with the statement after the local variable declaration. So a declaration like  declares a local variable x and assigns it the value of x from the outer scope. The local variable remains in scope until the end of the innermost block containing the local variable declaration.

Control structures
The while statement repeats a block as long as an expression evaluates to a true value.

The repeat statement repeats a block until an expression evaluates to a true value. Local variables declared inside the block may be accessed in the expression.

The first form of the for loop will declare a local variable, and repeat the block for values from exp1 to exp2 adding exp3 on each iteration. exp3 may be omitted, in which case 1 is used. All expressions are evaluated once before the loop is started.

This form of the for loop is roughly equivalent to except that the variables var, limit, and step are not accessible anywhere else. Note that the variable name is local to the block; to use the value after the loop, it must be copied to a variable declared outside the loop.

The second form of the for loop works with iterator functions. As in the first form, the exp-list is evaluated only once before beginning the loop.

This form of the for loop is roughly equivalent to except that again the variables var, limit, and step are not accessible anywhere else. Note that the variables in var-list are local to the block; to use them after the loop, they must be copied to variables declared outside the loop.

Often the exp-list is a single function call that returns the three values. If the iterator function can be written so it only depends on the parameters passed into it, that would be the most efficient. If not, Programming in Lua suggests that a closure be preferred to returning a table as the static variable and updating its members on each iteration.

Executes block1 if exp1 returns true, otherwise executes block2 if exp2 returns true, and block3 otherwise. The  portion may be omitted, and the   portion may be repeated or omitted as necessary.

The return statement is used to return values from a function or a chunk (which is just a function). The expression-list is a comma-separated list of zero or more expressions. All expressions in the list are forced to one value except the last.

The return statement must be the last statement in the block. If for some reason a return is needed in the middle of a block, an explicit block  may be used.

The break statement is used to terminate the execution of a while, repeat, or for loop, skipping to the next statement after the loop.

The break statement must be the last statement in the block. If for some reason a break is needed in the middle of a block, an explicit block  may be used.

Function calls as statements
A function call may be used as a statement; in this case, the function is being called only for any side effects it may have (e.g. mw.log logs values) and any return values are discarded.

Function declaration statements
FILL IN

Error handling
Errors may be "thrown" using the error function. To "catch" errors, use pcall or xpcall. Note that certain internal Scribunto errors cannot be caught in Lua code.

Garbage collection
Lua performs automatic memory management. This means that you have to worry neither about allocating memory for new objects nor about freeing it when the objects are no longer needed. Lua manages memory automatically by running a garbage collector from time to time to collect all dead objects (that is, objects that are no longer accessible from Lua). All memory used by Lua is subject to automatic management: tables, functions, strings, etc.

Garbage collection happens automatically, and cannot be configured from within Scribunto.

Standard libraries
FILL IN

Scribunto libraries
FILL IN

Differences from standard Lua
FILL IN