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

The Length Operator

3.4.7 – The Length Operator The length operator is denoted by the unary prefix operator #. The length of a string is its number of bytes (that is, the usual meaning of string length when each character is one byte). The length operator applied on a table returns a border in that table. A border in a table t is any natural number that satisfies the following condition: (border == 0 or t[border] ~= nil) and t[border + 1] == nil In words, a border is any (natural) index in a table where a n

debug.getlocal()

debug.getlocal ([thread,] f, local) This function returns the name and the value of the local variable with index local of the function at level f of the stack. This function accesses not only explicit local variables, but also parameters, temporaries, etc. The first parameter or local variable has index 1, and so on, following the order that they are declared in the code, counting only the variables that are active in the current scope of the function. Negative indices refer to vararg para

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

lua_Hook

lua_Hook typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); Type for debugging hook functions. Whenever a hook is called, its ar argument has its field event set to the specific event that triggered the hook. Lua identifies these events with the following constants: LUA_HOOKCALL, LUA_HOOKRET, LUA_HOOKTAILCALL, LUA_HOOKLINE, and LUA_HOOKCOUNT. Moreover, for line events, the field currentline is also set. To get the value of any other field in ar, the hook must call lua_getinfo. For ca

lua_getupvalue

lua_getupvalue[-0, +(0|1), –] const char *lua_getupvalue (lua_State *L, int funcindex, int n); Gets information about the n-th upvalue of the closure at index funcindex. It pushes the upvalue's value onto the stack and returns its name. Returns NULL (and pushes nothing) when the index n is greater than the number of upvalues. For C functions, this function uses the empty string "" as a name for all upvalues. (For Lua functions, upvalues are the external local variables that the function us

lua_pushfstring

lua_pushfstring[-0, +1, e] const char *lua_pushfstring (lua_State *L, const char *fmt, ...); Pushes onto the stack a formatted string and returns a pointer to this string. It is similar to the ISO C function sprintf, but has some important differences: You do not have to allocate space for the result: the result is a Lua string and Lua takes care of memory allocation (and deallocation, through garbage collection). The conversion specifiers are quite restricted. There are no flags, widths

luaL_addsize

luaL_addsize[-?, +?, –] void luaL_addsize (luaL_Buffer *B, size_t n); Adds to the buffer B (see luaL_Buffer) a string of length n previously copied to the buffer area (see luaL_prepbuffer).

table.unpack()

table.unpack (list [, i [, j]]) Returns the elements from the given list. This function is equivalent to return list[i], list[i+1], ···, list[j] By default, i is 1 and j is #list.

lua_getfield

lua_getfield[-0, +1, e] int lua_getfield (lua_State *L, int index, const char *k); Pushes onto the stack the value t[k], where t is the value at the given index. As in Lua, this function may trigger a metamethod for the "index" event (see §2.4). Returns the type of the pushed value.