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

lua_gc[-0, +0, m] int lua_gc (lua_State *L, int what, int data); Controls the garbage collector

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

lua_rotate[-0, +0, –] void lua_rotate (lua_State *L, int idx, int n); Rotates the stack elements between

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

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

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

lua_getlocal[-0, +(0|1), –] const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n); Gets information

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

lua_error[-1, +0, v] int lua_error (lua_State *L); Generates a Lua error, using the value at the

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

lua_tonumberx[-0, +0, –] lua_Number lua_tonumberx (lua_State *L, int index, int *isnum); Converts the Lua

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

lua_gethook[-0, +0, –] lua_Hook lua_gethook (lua_State *L); Returns the current hook function.

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

lua_absindex[-0, +0, –] int lua_absindex (lua_State *L, int idx); Converts the acceptable index idx

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

lua_compare[-0, +0, e] int lua_compare (lua_State *L, int index1, int index2, int op); Compares

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