Skip to content

Lua 5.1 Reference Manual

The reference manual is the official definition of the Lua language. For a complete introduction to Lua programming, see the book Programming in Lua.

This manual is also available as a book:

Lua 5.1 Reference Manual by R. Ierusalimschy, L. H. de Figueiredo, W. Celes Lua.org, August 2006 ISBN 85-903798-3-3 Buy a copy of this book and help to support the Lua project.

start · contents · index · errata · other versions · português · español · deutsch Copyright © 2006–2012 Lua.org, PUC-Rio. Freely available under the terms of the Lua license.

Contents

1 – Introduction 2 – The Language 2.1 – Lexical Conventions 2.2 – Values and Types 2.2.1 – Coercion 2.3 – Variables 2.4 – Statements 2.4.1 – Chunks 2.4.2 – Blocks 2.4.3 – Assignment 2.4.4 – Control Structures 2.4.5 – For Statement 2.4.6 – Function Calls as Statements 2.4.7 – Local Declarations 2.5 – Expressions 2.5.1 – Arithmetic Operators 2.5.2 – Relational Operators 2.5.3 – Logical Operators 2.5.4 – Concatenation 2.5.5 – The Length Operator 2.5.6 – Precedence 2.5.7 – Table Constructors 2.5.8 – Function Calls 2.5.9 – Function Definitions 2.6 – Visibility Rules 2.7 – Error Handling 2.8 – Metatables 2.9 – Environments 2.10 – Garbage Collection 2.10.1 – Garbage-Collection Metamethods 2.10.2 – Weak Tables 2.11 – Coroutines 3 – The Application Program Interface 3.1 – The Stack 3.2 – Stack Size 3.3 – Pseudo-Indices 3.4 – C Closures 3.5 – Registry 3.6 – Error Handling in C 3.7 – Functions and Types 3.8 – The Debug Interface 4 – The Auxiliary Library 4.1 – Functions and Types 5 – Standard Libraries 5.1 – Basic Functions 5.2 – Coroutine Manipulation 5.3 – Modules 5.4 – String Manipulation 5.4.1 – Patterns 5.5 – Table Manipulation 5.6 – Mathematical Functions 5.7 – Input and Output Facilities 5.8 – Operating System Facilities 5.9 – The Debug Library 6 – Lua Stand-alone 7 – Incompatibilities with the Previous Version 7.1 – Changes in the Language 7.2 – Changes in the Libraries 7.3 – Changes in the API 8 – The Complete Syntax of Lua Index

Lua functions

basic _G _VERSION assert collectgarbage dofile error getfenv getmetatable ipairs load loadfile loadstring module next pairs pcall print rawequal rawget rawset require select setfenv setmetatable tonumber tostring type unpack xpcall

coroutine coroutine.create coroutine.resume coroutine.running coroutine.status coroutine.wrap coroutine.yield

debug debug.debug debug.getfenv debug.gethook debug.getinfo debug.getlocal debug.getmetatable debug.getregistry debug.getupvalue debug.setfenv debug.sethook debug.setlocal debug.setmetatable debug.setupvalue debug.traceback

io io.close io.flush io.input io.lines io.open io.output io.popen io.read io.stderr io.stdin io.stdout io.tmpfile io.type io.write file:close file:flush file:lines file:read file:seek file:setvbuf file:write

math math.abs math.acos math.asin math.atan math.atan2 math.ceil math.cos math.cosh math.deg math.exp math.floor math.fmod math.frexp math.huge math.ldexp math.log math.log10 math.max math.min math.modf math.pi math.pow math.rad math.random math.randomseed math.sin math.sinh math.sqrt math.tan math.tanh

os os.clock os.date os.difftime os.execute os.exit os.getenv os.remove os.rename os.setlocale os.time os.tmpname

package package.cpath package.loaded package.loaders package.loadlib package.path package.preload package.seeall

string string.byte string.char string.dump string.find string.format string.gmatch string.gsub string.len string.lower string.match string.rep string.reverse string.sub string.upper

table table.concat table.insert table.maxn table.remove table.sort

environment variables

LUA_CPATH LUA_INIT LUA_PATH C API

lua_Alloc lua_CFunction lua_Debug lua_Hook lua_Integer lua_Number lua_Reader lua_State lua_Writer

lua_atpanic lua_call lua_checkstack lua_close lua_concat lua_cpcall lua_createtable lua_dump lua_equal lua_error lua_gc lua_getallocf lua_getfenv lua_getfield lua_getglobal lua_gethook lua_gethookcount lua_gethookmask lua_getinfo lua_getlocal lua_getmetatable lua_getstack lua_gettable lua_gettop lua_getupvalue lua_insert lua_isboolean lua_iscfunction lua_isfunction lua_islightuserdata lua_isnil lua_isnone lua_isnoneornil lua_isnumber lua_isstring lua_istable lua_isthread lua_isuserdata lua_lessthan lua_load lua_newstate lua_newtable lua_newthread lua_newuserdata lua_next lua_objlen lua_pcall lua_pop lua_pushboolean lua_pushcclosure lua_pushcfunction lua_pushfstring lua_pushinteger lua_pushlightuserdata lua_pushliteral lua_pushlstring lua_pushnil lua_pushnumber lua_pushstring lua_pushthread lua_pushvalue lua_pushvfstring lua_rawequal lua_rawget lua_rawgeti lua_rawset lua_rawseti lua_register lua_remove lua_replace lua_resume lua_setallocf lua_setfenv lua_setfield lua_setglobal lua_sethook lua_setlocal lua_setmetatable lua_settable lua_settop lua_setupvalue lua_status lua_toboolean lua_tocfunction lua_tointeger lua_tolstring lua_tonumber lua_topointer lua_tostring lua_tothread lua_touserdata lua_type lua_typename lua_upvalueindex lua_xmove lua_yield auxiliary library

luaL_Buffer luaL_Reg

luaL_addchar luaL_addlstring luaL_addsize luaL_addstring luaL_addvalue luaL_argcheck luaL_argerror luaL_buffinit luaL_callmeta luaL_checkany luaL_checkint luaL_checkinteger luaL_checklong luaL_checklstring luaL_checknumber luaL_checkoption luaL_checkstack luaL_checkstring luaL_checktype luaL_checkudata luaL_dofile luaL_dostring luaL_error luaL_getmetafield luaL_getmetatable luaL_gsub luaL_loadbuffer luaL_loadfile luaL_loadstring luaL_newmetatable luaL_newstate luaL_openlibs luaL_optint luaL_optinteger luaL_optlong luaL_optlstring luaL_optnumber luaL_optstring luaL_prepbuffer luaL_pushresult luaL_ref luaL_register luaL_typename luaL_typerror luaL_unref luaL_where

constants

LUA_ENVIRONINDEX LUA_ERRERR LUA_ERRFILE LUA_ERRMEM LUA_ERRRUN LUA_ERRSYNTAX LUA_GLOBALSINDEX LUA_HOOKCOUNT LUA_HOOKLINE LUA_HOOKRET LUA_MASKCALL LUA_MASKCOUNT LUA_MASKLINE LUA_MASKRET LUA_MINSTACK LUA_MULTRET LUA_NOREF LUA_REFNIL LUA_REGISTRYINDEX LUA_YIELD