Garbage-Collection Metamethods

2.5.1 – Garbage-Collection Metamethods You can set garbage-collector metamethods for tables and, using the C API, for full userdata (see §2.4). These metamethods are also called finalizers. Finalizers allow you to coordinate Lua's garbage collection with external resource management (such as closing files, network or database connections, or freeing your own memory). For an object (table or userdata) to be finalized when collected, you must mark it for finalization. You mark an object for fi

Function Definitions

3.4.11 – Function Definitions The syntax for function definition is functiondef ::= function funcbody funcbody ::= ‘(’ [parlist] ‘)’ block end The following syntactic sugar simplifies function definitions: stat ::= function funcname funcbody stat ::= local function Name funcbody funcname ::= Name {‘.’ Name} [‘:’ Name] The statement function f () body end translates to f = function () body end The statement function t.a.b.c.f () body end translates to t.a.b.c.f = function () body end

Function Calls as Statements

3.3.6 – Function Calls as Statements To allow possible side-effects, function calls can be executed as statements: stat ::= functioncall In this case, all returned values are thrown away. Function calls are explained in §3.4.10.

Function Calls

3.4.10 – Function Calls A function call in Lua has the following syntax: functioncall ::= prefixexp args In a function call, first prefixexp and args are evaluated. If the value of prefixexp has type function, then this function is called with the given arguments. Otherwise, the prefixexp "call" metamethod is called, having as first parameter the value of prefixexp, followed by the original call arguments (see §2.4). The form functioncall ::= prefixexp ‘:’ Name args can be used to call "

Format Strings for Pack and Unpack

6.4.2 – Format Strings for Pack and Unpack The first argument to string.pack, string.packsize, and string.unpack is a format string, which describes the layout of the structure being created or read. A format string is a sequence of conversion options. The conversion options are as follows: <: sets little endian >: sets big endian =: sets native endian ![n]: sets maximum alignment to n (default is native alignment) b: a signed byte (char) B: an unsigned byte (char) h: a signed

For Statement

3.3.5 – For Statement The for statement has two forms: one numerical and one generic. The numerical for loop repeats a block of code while a control variable runs through an arithmetic progression. It has the following syntax: stat ::= for Name ‘=’ exp ‘,’ exp [‘,’ exp] do block end The block is repeated for name starting at the value of the first exp, until it passes the second exp by steps of the third exp. More precisely, a for statement like for v = e1, e2, e3 do block end is equival

file:write()

file:write (···) Writes the value of each of its arguments to file. The arguments must be strings or numbers. In case of success, this function returns file. Otherwise it returns nil plus a string describing the error.

file:setvbuf()

file:setvbuf (mode [, size]) Sets the buffering mode for an output file. There are three available modes: "no": no buffering; the result of any output operation appears immediately. "full": full buffering; output operation is performed only when the buffer is full or when you explicitly flush the file (see io.flush). "line": line buffering; output is buffered until a newline is output or there is any input from some special files (such as a terminal device). For the last two case

file:seek()

file:seek ([whence [, offset]]) Sets and gets the file position, measured from the beginning of the file, to the position given by offset plus a base specified by the string whence, as follows: "set": base is position 0 (beginning of the file); "cur": base is current position; "end": base is end of file; In case of success, seek returns the final file position, measured in bytes from the beginning of the file. If seek fails, it returns nil, plus a string describing the error. The

file:read()

file:read (···) Reads the file file, according to the given formats, which specify what to read. For each format, the function returns a string or a number with the characters read, or nil if it cannot read data with the specified format. (In this latter case, the function does not read subsequent formats.) When called without formats, it uses a default format that reads the next line (see below). The available formats are "n": reads a numeral and returns it as a float or an integer, fol