Lua Functiebibliotheek

Referentie: Lua 5.4 Referentiehandleiding Bekijk details →

search

Lua C API

lua_absindex

C API
int lua_absindex (lua_State *L, int idx);

Converts the acceptable index into an equivalent absolute index.

lua_Alloc

C API
typedef void * (*lua_Alloc) (void *ud,
                             void *ptr,
                             size_t osize,
                             size_t nsize);

, a pointer to the block being allocated/reallocated/freed;, the original size of the block or some code about what is being allocated; and, the new size of the block. When is not, is the size of the block pointed by, that is, the size given when it was allocated or reallocated. When is, encodes the kind of object that Lua is allocating. is any of, , , , or when Lua is creating a new object of that type. When is some other value, Lua is allocating memory for something else. Lua assumes the following behavior from the allocator function: When is zero, the allocator must behave like and then return. When is not zero, the allocator must behave like. In particular, the allocator returns if and only if it cannot fulfill the request. Here is a simple implementation for the allocator function. It is used in the auxiliary library by. static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { ud; osize; /* not used */ if { free; return NULL; } else return realloc; } Note that ISO C ensures that has no effect and that is equivalent to.

lua_arith

C API
void lua_arith (lua_State *L, int op);

Performs an arithmetic or bitwise operation over the two values at the top of the stack, with the value on the top being the second operand, pops these values, and pushes the result of the operation. The function follows the semantics of the corresponding Lua operator. The value of must be one of the following constants: : performs addition : performs subtraction : performs multiplication : performs float division : performs floor division : performs modulo : performs exponentiation : performs mathematical negation : performs bitwise NOT : performs bitwise AND : performs bitwise OR : performs bitwise exclusive OR : performs left shift : performs right shift

lua_atpanic

C API
lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf);

Sets a new panic function and returns the old one.

lua_call

C API
void lua_call (lua_State *L, int nargs, int nresults);

Calls a function. Like regular Lua calls, respects the metamethod. So, here the word "function" means any callable value. To do a call you must use the following protocol: first, the function to be called is pushed onto the stack; then, the arguments to the call are pushed in direct order; that is, the first argument is pushed first. Finally you call ; is the number of arguments that you pushed onto the stack. When the function returns, all arguments and the function value are popped and the call results are pushed onto the stack. The number of results is adjusted to, unless is. In this case, all results from the function are pushed; Lua takes care that the returned values fit into the stack space, but it does not ensure any extra space in the stack. The function results are pushed onto the stack in direct order, so that after the call the last result is on the top of the stack. Any error while calling and running the function is propagated upwards. The following example shows how the host program can do the equivalent to this Lua code: a = f

Auxiliary Library

luaL_addchar

Hulpmiddel
void luaL_addchar (luaL_Buffer *B, char c);

Adds the byte to the buffer.

luaL_addgsub

Hulpmiddel
const void luaL_addgsub (luaL_Buffer *B, const char *s,
                         const char *p, const char *r);

Adds a copy of the string to the buffer, replacing any occurrence of the string with the string.

luaL_addlstring

Hulpmiddel
void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);

Adds the string pointed to by with length to the buffer. The string can contain embedded zeros.

luaL_addsize

Hulpmiddel
void luaL_addsize (luaL_Buffer *B, size_t n);

Adds to the buffer a string of length previously copied to the buffer area.

luaL_addstring

Hulpmiddel
void luaL_addstring (luaL_Buffer *B, const char *s);

Adds the zero-terminated string pointed to by to the buffer.

Basic Functions

lua.h

Introduction Lua is a powerful, efficient, lightweight, embeddable scripting language. It supports procedural programming, object-oriented programming, functional programming, data-driven programming, and data description. Lua combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics. Lua is dynamically typed, runs by interpreting bytecode with a register-based virtual machine, and has automatic memory management with a generational garbage collection, making it ideal for configuration, scripting, and rapid prototyping. Lua is implemented as a library, written in clean C, the common subset of standard C and C++. The Lua distribution includes a host program called, which uses the Lua library to offer a complete, standalone Lua interpreter, for interactive or batch use. Lua is intended to be used both as a powerful, lightweight, embeddable scripting language for any program that needs one, and as a powerful but lightweight and efficient stand-alone language. As an extension language, Lua has no notion of a "main" program: it works embedded in a host client, called the embedding program or simply the host. The host program can invoke functions to execute a piece of Lua code, can write and read Lua variables, and can register C functions to be called by Lua code. Through the use of C functions, Lua can be augmented to cope with a wide range of different domains, thus creating customized programming languages sharing a syntactical framework. Lua is free software, and is provided as usual with no guarantees, as stated in its license. The implementation described in this manual is available at Lua's official web site, . Like any other reference manual, this document is dry in places. For a discussion of the decisions behind the design of Lua, see the technical papers available at Lua's web site. For a detailed introduction to programming in Lua, see Roberto's book, Programming in Lua. 2 – Basic Concepts This section describes the basic concepts of the language. 2. 1 – Values and Types Lua is a dynamically typed language. This means that variables do not have types; only values do. There are no type definitions in the language. All values carry their own type. All values in Lua are first-class values. This means that all values can be stored in variables, passed as arguments to other functions, and returned as results. There are eight basic types in Lua: nil, boolean, number, string, function, userdata, thread, and table. The type nil has one single value, nil, whose main property is to be different from any other value; it often represents the absence of a useful value. The type boolean has two values, false and true. Both nil and false make a condition false; they are collectively called false values. Any other value makes a condition true. Despite its name, false is frequently used as an alternative to nil, with the key difference that false behaves like a regular value in a table, while a nil in a table represents an absent key. The type number represents both integer numbers and real numbers, using two subtypes: integer and float. Standard Lua uses 64-bit integers and double-precision floats, but you can also compile Lua so that it uses 32-bit integers and/or single-precision floats. The option with 32 bits for both integers and floats is particularly attractive for small machines and embedded systems. Unless stated otherwise, any overflow when manipulating integer values wrap around, according to the usual rules of two-complement arithmetic. (In other words, the actual result is the unique representable integer that is equal modulo 2n to the mathematical result, where n is the number of bits of the integer type. ) Lua has explicit rules about when each subtype is used, but it also converts between them automatically as needed. Therefore, the programmer may choose to mostly ignore the difference between integers and floats or to assume complete control over the representation of each number. The type string represents immutable sequences of bytes. Lua is 8-bit clean: strings can contain any 8-bit value, including embedded zeros. Lua is also encoding-agnostic; it makes no assumptions about the contents of a string. The length of any string in Lua must fit in a Lua integer. Lua can call functions written in Lua and functions written in C. Both are represented by the type function. The type userdata is provided to allow arbitrary C data to be stored in Lua variables. A userdata value represents a block of raw memory. There are two kinds of userdata: full userdata, which is an object with a block of memory managed by Lua, and light userdata, which is simply a C pointer value. Userdata has no predefined operations in Lua, except assignment and identity test. By using metatables, the programmer can define operations for full userdata values. Userdata values cannot be created or modified in Lua, only through the C API. This guarantees the integrity of data owned by the host program and C libraries. The type thread represents independent threads of execution and it is used to implement coroutines. Lua threads are not related to operating-system threads. Lua supports coroutines on all systems, even those that do not support threads natively. The type table implements associative arrays, that is, arrays that can have as indices not only numbers, but any Lua value except nil and NaN. (Not a Number is a special floating-point value used by the IEEE 754 standard to represent undefined numerical results, such as. ) Tables can be heterogeneous; that is, they can contain values of all types. Any key associated to the value nil is not considered part of the table. Conversely, any key that is not part of a table has an associated value nil. Tables are the sole data-structuring mechanism in Lua; they can be used to represent ordinary arrays, lists, symbol tables, sets, records, graphs, trees, etc. To represent records, Lua uses the field name as an index. The language supports this representation by providing as syntactic sugar for. There are several convenient ways to create tables in Lua. Like indices, the values of table fields can be of any type. In particular, because functions are first-class values, table fields can contain functions. Thus tables can also carry methods. The indexing of tables follows the definition of raw equality in the language. The expressions and denote the same table element if and only if and are raw equal. In particular, floats with integral values are equal to their respective integers. To avoid ambiguities, any float used as a key that is equal to an integer is converted to that integer.

assert (v [, message])

otherwise, returns all its arguments. In case of error, is the error object; when absent, it defaults to ""

collectgarbage ([opt [, arg]])

2. 5 for more details about garbage collection and some of these options. This function should not be called by a finalizer.

error (message [, level])

and so on. Passing a level 0 avoids the addition of error position information to the message.

getmetatable (object)

If does not have a metatable, returns nil. Otherwise, if the object's metatable has a field, returns the associated value. Otherwise, returns the metatable of the given object.