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

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

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

lua_getuservalue[-0, +1, –] int lua_getuservalue (lua_State *L, int index); Pushes onto the stack the Lua

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

lua_gethookmask[-0, +0, –] int lua_gethookmask (lua_State *L); Returns the current hook mask.

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

lua_getglobal[-0, +1, e] int lua_getglobal (lua_State *L, const char *name); Pushes onto the stack

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

lua_atpanic[-0, +0, –] lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf); Sets a new panic

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

lua_tostring[-0, +0, m] const char *lua_tostring (lua_State *L, int index); Equivalent to

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

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

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

lua_pushstring[-0, +1, m] const char *lua_pushstring (lua_State *L, const char *s); Pushes the

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

lua_gettop[-0, +0, –] int lua_gettop (lua_State *L); Returns the index of the top element in the stack

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

lua_setlocal[-(0|1), +0, –] const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n); Sets the

2025-01-10 15:47:30