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_getfield
  • References/Lua/Lua/API/Functions and Types

lua_getfield[-0, +1, e] int lua_getfield (lua_State *L, int index, const char *k); Pushes onto

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

lua_newtable[-0, +1, m] void lua_newtable (lua_State *L); Creates a new empty table and pushes

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

lua_load[-0, +1, –] int lua_load (lua_State *L, lua_Reader reader, void *data,

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

lua_pushglobaltable[-0, +1, –] void lua_pushglobaltable (lua_State *L); Pushes the global environment onto

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

lua_isthread[-0, +0, –] int lua_isthread (lua_State *L, int index); Returns 1 if the value at the given

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

lua_isuserdata[-0, +0, –] int lua_isuserdata (lua_State *L, int index); Returns 1 if the value at the given

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

lua_callk[-(nargs + 1), +nresults, e] void lua_callk (lua_State *L, int nargs,

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

lua_tothread[-0, +0, –] lua_State *lua_tothread (lua_State *L, int index); Converts the value at the given

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

lua_Alloc typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize,

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

lua_rawset[-2, +0, m] void lua_rawset (lua_State *L, int index); Similar to

2025-01-10 15:47:30