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.

string.reverse()
  • References/Lua/Lua/Standard Libraries/String Manipulation

string.reverse (s)s

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

debug.debug () Enters an interactive mode with the user, running each string that the user enters. Using simple commands and other debug facilities, the user can inspect global

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

math.random ([m [, n]]) When called without arguments, returns a pseudo-random float with uniform distribution in the range [0,1). When called with two integers m

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

debug.getlocal ([thread,] f, local) This function returns the name and the value of the local variable with index local of the function at level f of

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

debug.sethook ([thread,] hook, mask [, count]) Sets the given function as a hook. The string mask and the number count describe when the hook will be

2025-01-10 15:47:30
os.date()
  • References/Lua/Lua/Standard Libraries/Operating System Facilities

os.date ([format [, time]]) Returns a string or a table containing date and time, formatted according to the given string format. If the time

2025-01-10 15:47:30
os.difftime()
  • References/Lua/Lua/Standard Libraries/Operating System Facilities

os.difftime (t2, t1) Returns the difference, in seconds, from time t1 to time t2 (where the times are values returned by

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

string.char (···) Numeric codes are not necessarily portable across platforms.

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

file:setvbuf (mode [, size]) Sets the buffering mode for an output file. There are three available modes: "no": no buffering; the result of

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

math.randomseed (x) Sets x as the "seed" for the pseudo-random generator: equal seeds produce equal sequences of numbers.

2025-01-10 15:47:30