Expand description
Module provides FFI bindings for the following constants, types and functions, realted to Lua C API:
- Plain lua C API
- lauxlib
- Lua utitlites, implemented in Tarantool
Structs
Constants
- Lua provides a registry, a pre-defined table that can be used by any C code to store whatever Lua value it needs to store. This table is always located at pseudo-index
LUA_REGISTRYINDEX
. Any C library can store data into this table, but it should take care to choose keys different from those used by other libraries, to avoid collisions. Typically, you should use as key a string containing your library name or a light userdata with the address of a C object in your code.
Functions
- Checks whether the function argument
idx
is a cdata - Return CTypeID (FFI) of given CDATA type
ctypename
is a C type name as string (e.g. “struct request”, “uint32_t”, etc.). See also:luaL_pushcdata
,luaL_checkcdata
- Raises an error. The error message format is given by
fmt
plus any extra arguments, following the same rules oflua_pushfstring
. It also adds at the beginning of the message the file name and the line number where the error occurred, if this information is available. [-0, +0, v] - Push the metafield
field
of the value at givenindex
onto the stack. Returns1
if the value was pushed,0
otherwise. [-0, +(1|0), -] - Check if value at given
index
has metafieldfield
. [-0, +0, -] - Check if value at
index
is a lua function, a lua value with a__call
metamethod or a callablecdata
. - Push cdata of given
ctypeid
onto the stack. CTypeID must be used from FFI at least once. Allocated memory returned uninitialized. Only numbers and pointers are supported. - Push
i64
onto the stack [-0, +1, -] - Push
u64
onto the stack [-0, +1, -] - Creates and returns a reference, in the table at index
t
, for the object at the top of the stack (and pops the object). [-1, +0, m] - Checks whether the argument
idx
is ai64
or a convertable string and returns this number. [-0, +0, -] - Checks whether the argument
idx
is au64
or a convertable string and returns this number. [-0, +0, -] - Releases reference
r
from the table at indext
(seeluaL_ref
). The entry is removed from the table, so that the referred object can be collected. The referencer
is also freed to be used again. [-0, +0, -] - Convert the value at
idx
to string using__tostring
metamethod if other measures didn’t work and return it. Sets thelen
if it’s notNULL
. The newly created string is left on top of the stack. [-0, +1, m] - Calls the C function
func
in protected mode.func
starts with only one element in its stack, a light userdata containingud
. In case of errors, lua_cpcall returns the same error codes as lua_pcall, plus the error object on the top of the stack; otherwise, it returns zero, and does not change the stack. All values returned byfunc
are discarded. [-0, +(0|1), -] - Creates a new empty table and pushes it onto the stack. The new table has space pre-allocated for
narr
array elements andnrec
non-array elements. This pre-allocation is useful when you know exactly how many elements the table will have. Otherwise you can use the functionlua_newtable
. [-0, +1, m] - Generates a Lua error. The error message (which can actually be a Lua value of any type) must be on the stack top. This function does a long jump, and therefore never returns. (see
luaL_error
). [-1, +0, v] - Pushes onto the stack the value
t[k]
, wheret
is the value at the given validindex
. As in Lua, this function may trigger a metamethod for the “index” event [-0, +1, e] - Pushes onto the stack the value of the global
name
. [-0, +1, e] - Pushes onto the stack the value
t[k]
, wheret
is the value at the given validindex
andk
is the value at the top of the stack. [-1, +1, e] - Returns the index of the top element in the stack. Because indices start at 1, this result is equal to the number of elements in the stack (and so 0 means an empty stack). [-0, +0, -]
- Moves the top element into the given valid
index
, shifting up the elements above thisindex
to open space. Cannot be called with a pseudo-index, because a pseudo-index is not an actual stack position. [-1, +1, -] - [-0, +1, -] Loads a Lua chunk. If there are no errors,
lua_load
pushes the compiled chunk as a Lua function on top of the stack. Otherwise, it pushes an error message. The return values oflua_load
are: - Creates a new empty table and pushes it onto the stack. It is equivalent to
lua_createtable
(L, 0, 0)
. [-0, +1, m] - Creates a new thread, pushes it on the stack, and returns a pointer to a
lua_State
that represents this new thread. The new state returned by this function shares with the original state all global objects (such as tables), but has an independent execution stack. [-0, +1, m] - This function allocates a new block of memory with the given size, pushes onto the stack a new full userdata with the block address, and returns this address. [-0, +1, m]
- Pops a key from the stack, and pushes a key-value pair from the table at the given
index
(the “next” pair after the given key). If there are no more elements in the table, thenlua_next
returns 0 (and pushes nothing). [-1, +(2|0), e] - Calls a function in protected mode. [-(nargs + 1), +(nresults|1), -]
- Pushes a new C closure onto the stack. [-n, +1, m]
- Pushes a C function onto the stack. This function receives a pointer to a C function and pushes onto the stack a Lua value of type function that, when called, invokes the corresponding C function. [-0, +1, m]
- Pushes a number with value
n
onto the stack. [-0, +1, -] - Pushes a light userdata onto the stack. [-0, +1, -]
- Pushes a number with value
n
onto the stack. [-0, +1, -] - Pushes the zero-terminated string pointed to by
s
onto the stack. Lua makes (or reuses) an internal copy of the given string, so the memory at s can be freed or reused immediately after the function returns. The string cannot contain embedded zeros; it is assumed to end at the first zero. [-0, +1, m] - Pushes a copy of the element at the given valid
index
onto the stack. [-0, +1, -] - Similar to
lua_gettable
, but does a raw access (i.e., without metamethods). [-1, +1, -] - Pushes onto the stack the value
t[n]
, wheret
is the value at the given validindex
. The access is raw; that is, it does not invoke metamethods. [-0, +1, -] - Similar to
lua_settable
, but does a raw assignment (i.e., without metamethods). [-2, +0, m] - Does the equivalent of
t[n] = v
, wheret
is the value at the given validindex
andv
is the value at the top of the stack. [-1, +0, m] - Removes the element at the given valid index, shifting down the elements above this index to fill the gap. Cannot be called with a pseudo-index, because a pseudo-index is not an actual stack position. [-1, +0, -]
- Does the equivalent to
t[k] = v
, wheret
is the value at the given valid index andv
is the value at the top of the stack. [-1, +0, e] - Pops a value from the stack and sets it as the new value of global
name
. [-1, +0, e] - Pops a table from the stack and sets it as the new metatable for the value at the given acceptable
index
. [-1, +0, -] - Does the equivalent to
t[k] = v
, wheret
is the value at the given validindex
,v
is the value at the top of the stack, andk
is the value just below the top. [-2, +0, e] - Converts the Lua value at the given acceptable
index
to a C string. Iflen
is not NULL, it also sets*len
with the string length. The Lua value must be a string or a number; otherwise, the function returns NULL. If the value is a number, thenlua_tolstring
also changes the actual value in the stack to a string. (This change confuseslua_next
whenlua_tolstring
is applied to keys during a table traversal.) [-0, +0, m] - If the value at the given acceptable
index
is a full userdata, returns its block address. If the value is a light userdata, returns its pointer. Otherwise, returnsNULL
. [-0, +0, -] - Returns the type of the value in the given acceptable
index
, orLUA_TNONE
for a non-valid index (that is, an index to an “empty” stack position). The types returned by lua_type are coded by the following constants:LUA_TNIL
,LUA_TNUMBER
,LUA_TBOOLEAN
,LUA_TSTRING
,LUA_TTABLE
,LUA_TFUNCTION
,LUA_TUSERDATA
,LUA_TTHREAD
, andLUA_TLIGHTUSERDATA
. [-0, +0, -] - Returns the name of the type encoded by the value
tp
, which must be one the values returned bylua_type
. [-0, +0, -] - When a C function is created, it is possible to associate some values with it, thus creating a C closure; these values are called upvalues and are accessible to the function whenever it is called (see
lua_pushcclosure
).
Type Aliases
- Type for C functions.
- The reader function used by
lua_load
. Every time it needs another piece of the chunk,lua_load
calls the reader, passing along itsdata
parameter. The reader must return a pointer to a block of memory with a new piece of the chunk and setsize
to the block size. The block must exist until the reader function is called again. To signal the end of the chunk, the reader must returnNULL
or setsize
to zero. The reader function may return pieces of any size greater than zero.