Skip to content

Lua Standard Libraries

Lua includes a set of standard libraries that provide essential functionalities. These libraries are implemented in C and are readily available in any Lua environment. They offer services ranging from basic operations and string manipulation to file I/O and debugging.

The standard libraries are organized into modules (tables). Some functions are global, while others are accessed through these module tables (e.g., string.format, math.sin).

  • Basic Library: Core functions, error handling, type checking, coroutine manipulation.
  • Package Library: For loading modules (require).
  • String Manipulation Library: For working with strings, including pattern matching.
  • Table Manipulation Library: Functions for working with tables.
  • Mathematical Library: Standard mathematical functions.
  • UTF-8 Library (Lua 5.3+): Basic support for UTF-8 encoded strings.
  • Input and Output (I/O) Library: For file operations and standard I/O.
  • Operating System Facilities Library: Functions for interacting with the OS (e.g., time, file system operations).
  • Debug Library: For debugging and introspection.

Basic Library (Global Functions and coroutine)

Section titled “Basic Library (Global Functions and coroutine)”

This library provides fundamental Lua functions. Many are globally accessible.

S.N.Library / Method & Purpose
1.Core Functions & Type System: assert, error, pcall, xpcall, type, tostring, tonumber, print. (Covered in Error Handling, Data Types, etc.)
2.Metatables: getmetatable, setmetatable, rawequal, rawget, rawset. (Covered in Metatables)
3.Iteration: pairs, ipairs, next. (Covered in Iterators, Loops)
4.Module Loading (deprecated way of loading): load, loadfile, loadstring. These compile Lua code from a string/file/function into a function. dofile executes a file. (Modern module loading uses require from the Package Library).
5.Environment: _G (the global environment table). _VERSION (string, e.g., "Lua 5.4"). Note: getfenv and setfenv are deprecated since Lua 5.2; use the _ENV upvalue for environment manipulation.
6.Coroutine Library: coroutine.create, coroutine.resume, coroutine.yield, coroutine.status, coroutine.wrap, coroutine.running, coroutine.isyieldable (Lua 5.2+). (Covered in Coroutines)
7.Garbage Collection: collectgarbage. (Covered in Garbage Collection)
8.Variable Arguments: select(index, ...). Returns arguments after index, or if index is "#", returns the total count of varargs. table.pack(...) (Lua 5.2+) is often preferred for collecting varargs into a table.

Manages modules in Lua. The primary function is require(modname), which loads and runs a module, caching its result. The package table contains path information (package.path, package.cpath), loaded modules (package.loaded), etc. (Covered in Modules).

Provides functions for string operations, such as string.len, string.sub, string.find, string.gsub, string.format, string.upper, string.lower, string.rep, string.char, string.byte, string.pack, string.unpack (Lua 5.3+). (Covered in Strings).

Functions for working with tables (Lua’s primary data structure), including table.insert, table.remove, table.concat, table.sort, table.pack (Lua 5.2+), table.unpack (Lua 5.2+, formerly global unpack), table.move (Lua 5.3+). (Covered in Tables, Arrays).

Includes standard mathematical functions like math.sin, math.cos, math.sqrt, math.log, math.exp, math.abs, math.pi, math.huge, math.random, math.randomseed, math.floor, math.ceil, number type functions like math.type (Lua 5.3+), math.tointeger (Lua 5.3+). (This will be detailed in a dedicated Math Library chapter, if available, or refer to official Lua documentation).

Provides basic support for UTF-8 encoded strings. Functions include utf8.char, utf8.codes (iterator), utf8.codepoint, utf8.len, utf8.offset. This library helps in correctly handling multi-byte characters.

For file operations and standard input/output streams. Functions include io.open, io.close, io.read, io.write, io.lines, io.input, io.output, io.tmpfile, file:read, file:write, file:lines, file:seek, file:close. (Covered in File I/O).

Provides functions to interact with the operating system, such as os.clock (CPU time), os.date (format date/time), os.time (current time), os.difftime, os.execute (run shell command), os.exit, os.getenv, os.remove, os.rename, os.setlocale, os.tmpname. (This will be detailed in a dedicated OS Facilities chapter, if available, or refer to official Lua documentation).

Offers introspection and debugging capabilities. Functions include debug.getinfo, debug.getlocal, debug.getupvalue, debug.sethook, debug.traceback. (Covered in Debugging).

Note: For detailed information on any specific function or library, the official Lua Reference Manual for your Lua version is the most authoritative resource.