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

lua_setfield[-1, +0, e] void lua_setfield (lua_State *L, int index, const char *k); Does the equivalent

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

lua_insert[-1, +1, –] void lua_insert (lua_State *L, int index); Moves the top element into the given valid

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

lua_createtable[-0, +1, m] void lua_createtable (lua_State *L, int narr, int nrec); Creates a

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

lua_rawequal[-0, +0, –] int lua_rawequal (lua_State *L, int index1, int index2); Returns 1 if the two values

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

lua_isnone[-0, +0, –] int lua_isnone (lua_State *L, int index); Returns 1 if the given index is not valid

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

lua_getmetatable[-0, +(0|1), –] int lua_getmetatable (lua_State *L, int index); If the value at the given

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

lua_numbertointeger int lua_numbertointeger (lua_Number n, lua_Integer *p); Converts a Lua float to a Lua integer. This macro assumes

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

lua_seti[-1, +0, e] void lua_seti (lua_State *L, int index, lua_Integer n); Does the equivalent

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

lua_rawsetp[-1, +0, m] void lua_rawsetp (lua_State *L, int index, const void *p); Does the equivalent

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

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

2025-01-10 15:47:30