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_Hook
  • References/Lua/Lua/API/The Debug Interface

lua_Hook typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); Type for debugging hook functions. Whenever a hook is called

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

lua_KFunction typedef int (*lua_KFunction) (lua_State *L, int status, lua_KContext ctx); Type for continuation functions (see

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

lua_Integer typedef ... lua_Integer; The type of integers in Lua. By default this type is long long, (usually

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

lua_checkstack[-0, +0, –] int lua_checkstack (lua_State *L, int n); Ensures that the stack has space for

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

lua_pushlstring[-0, +1, m] const char *lua_pushlstring (lua_State *L, const char *s, size_t len);

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

lua_pushnil[-0, +1, –] void lua_pushnil (lua_State *L); Pushes a nil value onto the stack.

2025-01-10 15:47:30
lua_getstack
  • References/Lua/Lua/API/The Debug Interface

lua_getstack[-0, +0, –] int lua_getstack (lua_State *L, int level, lua_Debug *ar); Gets information about

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

lua_setallocf[-0, +0, –] void lua_setallocf (lua_State *L, lua_Alloc f, void *ud); Changes the allocator

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

lua_pushvalue[-0, +1, –] void lua_pushvalue (lua_State *L, int index); Pushes a copy of the element at

2025-01-10 15:47:30