4 – The Application Program Interface

This section describes the C API for Lua, that is, the set of C functions available to the host program to communicate with Lua. All API functions and related types and constants are declared in the header file lua.h.

Even when we use the term "function", any facility in the API may be provided as a macro instead. Except where stated otherwise, all such macros use each of their arguments exactly once (except for the first argument, which is always a Lua state), and so do not generate any hidden side-effects.

As in most C libraries, the Lua API functions do not check their arguments for validity or consistency. However, you can change this behavior by compiling Lua with the macro LUA_USE_APICHECK defined.

The Lua library is fully reentrant: it has no global variables. It keeps all information it needs in a dynamic structure, called the Lua state.

Each Lua state has one or more threads, which correspond to independent, cooperative lines of execution. The type lua_State (despite its name) refers to a thread. (Indirectly, through the thread, it also refers to the Lua state associated to the thread.)

A pointer to a thread must be passed as the first argument to every function in the library, except to lua_newstate, which creates a Lua state from scratch and returns a pointer to the main thread in the new state.

lua_getextraspace
  • References/Lua/Lua/API/Functions and Types

lua_getextraspace[-0, +0, –] void *lua_getextraspace (lua_State *L); Returns a pointer to a raw memory

2025-01-10 15:47:30
lua_yieldk
  • References/Lua/Lua/API/Functions and Types

lua_yieldk[-?, +?, e] int lua_yieldk (lua_State *L, int nresults, lua_KContext

2025-01-10 15:47:30
lua_pushboolean
  • References/Lua/Lua/API/Functions and Types

lua_pushboolean[-0, +1, –] void lua_pushboolean (lua_State *L, int b); Pushes a boolean value with value

2025-01-10 15:47:30
lua_stringtonumber
  • References/Lua/Lua/API/Functions and Types

lua_stringtonumber[-0, +1, –] size_t lua_stringtonumber (lua_State *L, const char *s); Converts the zero-terminated

2025-01-10 15:47:30
lua_tolstring
  • References/Lua/Lua/API/Functions and Types

lua_tolstring[-0, +0, m] const char *lua_tolstring (lua_State *L, int index, size_t *len); Converts

2025-01-10 15:47:30
lua_rawlen
  • References/Lua/Lua/API/Functions and Types

lua_rawlen[-0, +0, –] size_t lua_rawlen (lua_State *L, int index); Returns the raw "length" of the value

2025-01-10 15:47:30
lua_tonumber
  • References/Lua/Lua/API/Functions and Types

lua_tonumber[-0, +0, –] lua_Number lua_tonumber (lua_State *L, int index); Equivalent to

2025-01-10 15:47:30
lua_type
  • References/Lua/Lua/API/Functions and Types

lua_type[-0, +0, –] int lua_type (lua_State *L, int index); Returns the type of the value in the given

2025-01-10 15:47:30
lua_len
  • References/Lua/Lua/API/Functions and Types

lua_len[-0, +1, e] void lua_len (lua_State *L, int index); Returns the length of the value at

2025-01-10 15:47:30
lua_tocfunction
  • References/Lua/Lua/API/Functions and Types

lua_tocfunction[-0, +0, –] lua_CFunction lua_tocfunction (lua_State *L, int index); Converts a value at

2025-01-10 15:47:30