6 – Standard Libraries

The standard Lua libraries provide useful functions that are implemented directly through the C API. Some of these functions provide essential services to the language (e.g., type and getmetatable); others provide access to "outside" services (e.g., I/O); and others could be implemented in Lua itself, but are quite useful or have critical performance requirements that deserve an implementation in C (e.g., table.sort).

All libraries are implemented through the official C API and are provided as separate C modules. Currently, Lua has the following standard libraries:

  • basic library (§6.1);
  • coroutine library (§6.2);
  • package library (§6.3);
  • string manipulation (§6.4);
  • basic UTF-8 support (§6.5);
  • table manipulation (§6.6);
  • mathematical functions (§6.7) (sin, log, etc.);
  • input and output (§6.8);
  • operating system facilities (§6.9);
  • debug facilities (§6.10).

Except for the basic and the package libraries, each library provides all its functions as fields of a global table or as methods of its objects.

To have access to these libraries, the C host program should call the luaL_openlibs function, which opens all standard libraries. Alternatively, the host program can open them individually by using luaL_requiref to call luaopen_base (for the basic library), luaopen_package (for the package library), luaopen_coroutine (for the coroutine library), luaopen_string (for the string library), luaopen_utf8 (for the UTF8 library), luaopen_table (for the table library), luaopen_math (for the mathematical library), luaopen_io (for the I/O library), luaopen_os (for the operating system library), and luaopen_debug (for the debug library). These functions are declared in lualib.h.

io.tmpfile()
  • References/Lua/Lua/Standard Libraries/Input and Output Facilities

io.tmpfile () In case of success, returns a handle for a temporary file. This file is opened in update mode and it is automatically removed when the program ends.

2025-01-10 15:47:30
xpcall()
  • References/Lua/Lua/Standard Libraries/Basic Functions

xpcall (f, msgh [, arg1, ···]) This function is similar to pcall, except

2025-01-10 15:47:30
setmetatable()
  • References/Lua/Lua/Standard Libraries/Basic Functions

setmetatable (table, metatable) Sets the metatable for the given table. (To change the metatable of other types from Lua code, you must use the debug library (

2025-01-10 15:47:30
debug.getupvalue()
  • References/Lua/Lua/Standard Libraries/The Debug Library

debug.getupvalue (f, up) This function returns the name and the value of the upvalue with index up of the function f. The function returns nil

2025-01-10 15:47:30
debug.getinfo()
  • References/Lua/Lua/Standard Libraries/The Debug Library

debug.getinfo ([thread,] f [, what]) Returns a table with information about a function. You can give the function directly or you can give a number as the value of f

2025-01-10 15:47:30
table.pack()
  • References/Lua/Lua/Standard Libraries/Table Manipulation

table.pack (···) Returns a new table with all parameters stored into keys 1, 2, etc. and with a field "n" with the total number of parameters. Note that the resulting

2025-01-10 15:47:30
ipairs()
  • References/Lua/Lua/Standard Libraries/Basic Functions

ipairs (t) Returns three values (an iterator function, the table t, and 0) so that the construction for i,v in ipairs(t)

2025-01-10 15:47:30
math.rad()
  • References/Lua/Lua/Standard Libraries/Mathematical Functions

math.rad (x) Converts the angle x from degrees to radians.

2025-01-10 15:47:30
string.len()
  • References/Lua/Lua/Standard Libraries/String Manipulation

string.len (s)"""a\000bc\000"

2025-01-10 15:47:30
io.open()
  • References/Lua/Lua/Standard Libraries/Input and Output Facilities

io.open (filename [, mode]) This function opens a file, in the mode specified in the string mode. In case of success, it returns a new file handle. The mode

2025-01-10 15:47:30