3.1 – States
The Lua library is fully reentrant: it has no global variables. The whole state of the Lua interpreter (global variables, stack, etc.) is stored in a dynamically allocated structure of type lua_State
. A pointer to this state must be passed as the first argument to every function in the library, except to lua_open
, which creates a Lua state from scratch.
Before calling any API function, you must create a state by calling lua_open
:
- lua_State *lua_open (void);
To release a state created with lua_open
, call lua_close
:
- void lua_close (lua_State *L);
This function destroys all objects in the given Lua state (calling the corresponding garbage-collection metamethods, if any) and frees all dynamic memory used by that state. On several platforms, you may not need to call this function, because all resources are naturally released when the host program ends. On the other hand, long-running programs, such as a daemon or a web server, might need to release states as soon as they are not needed, to avoid growing too large.