summaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
Diffstat (limited to 'doc')
-rw-r--r--doc/idx.html115
-rw-r--r--doc/index.html51
-rw-r--r--doc/luac.html246
-rw-r--r--doc/luac.man165
-rw-r--r--doc/manual.html1733
5 files changed, 1465 insertions, 845 deletions
diff --git a/doc/idx.html b/doc/idx.html
index 26fb74b2..534ce43f 100644
--- a/doc/idx.html
+++ b/doc/idx.html
@@ -1,6 +1,6 @@
<HTML>
<HEAD>
-<TITLE>Lua 3.0 Reference Manual - Word Index</TITLE>
+<TITLE>Lua 3.1 Reference Manual - Word Index</TITLE>
</HEAD>
<BODY>
@@ -8,58 +8,85 @@
<H1>Index</H1>
<A HREF="manual.html#..">..</A><BR>
+<A HREF="manual.html#Adjustment">Adjustment</A><BR>
+<A HREF="manual.html#Assignment">Assignment</A><BR>
+<A HREF="manual.html#Basic Expressions">Basic Expressions</A><BR>
+<A HREF="manual.html#C pointers">C pointers</A><BR>
+<A HREF="manual.html#C2lua">C2lua</A><BR>
+<A HREF="manual.html#Coercion">Coercion</A><BR>
+<A HREF="manual.html#Comments">Comments</A><BR>
+<A HREF="manual.html#Expressions">Expressions</A><BR>
+<A HREF="manual.html#Function Definitions">Function Definitions</A><BR>
+<A HREF="manual.html#Global variables">Global variables</A><BR>
+<A HREF="manual.html#Identifiers">Identifiers</A><BR>
+<A HREF="manual.html#LUA_ANYTAG">LUA_ANYTAG</A><BR>
+<A HREF="manual.html#LUA_NOOBJECT">LUA_NOOBJECT</A><BR>
+<A HREF="manual.html#Literal strings">Literal strings</A><BR>
+<A HREF="manual.html#Local variables">Local variables</A><BR>
+<A HREF="manual.html#Lua Stand-alone">Lua Stand-alone</A><BR>
+<A HREF="manual.html#Numerical constants">Numerical constants</A><BR>
+<A HREF="manual.html#Operator precedence">Operator precedence</A><BR>
+<A HREF="manual.html#PI">PI</A><BR>
+<A HREF="manual.html#Pre-processor">Pre-processor</A><BR>
+<A HREF="manual.html#Tag Methods">Tag Methods</A><BR>
+<A HREF="manual.html#Types and Tags">Types and Tags</A><BR>
+<A HREF="manual.html#Upvalues">Upvalues</A><BR>
+<A HREF="manual.html#Visibility">Visibility</A><BR>
+<A HREF="manual.html#_INPUT">_INPUT</A><BR>
+<A HREF="manual.html#_OUTPUT">_OUTPUT</A><BR>
+<A HREF="manual.html#_STDERR">_STDERR</A><BR>
+<A HREF="manual.html#_STDIN">_STDIN</A><BR>
+<A HREF="manual.html#_STDOUT">_STDOUT</A><BR>
<A HREF="manual.html#abs">abs</A><BR>
<A HREF="manual.html#acos">acos</A><BR>
<A HREF="manual.html#add event">add event</A><BR>
-<A HREF="manual.html#Adjustment">Adjustment</A><BR>
<A HREF="manual.html#and">and</A><BR>
<A HREF="manual.html#appendto">appendto</A><BR>
-<A HREF="manual.html#arg">arg</A><BR>
<A HREF="manual.html#arguments">arguments</A><BR>
+<A HREF="manual.html#arg">arg</A><BR>
<A HREF="manual.html#arithmetic operators">arithmetic operators</A><BR>
<A HREF="manual.html#arrays">arrays</A><BR>
-<A HREF="manual.html#ascii">ascii</A><BR>
<A HREF="manual.html#asin">asin</A><BR>
<A HREF="manual.html#assert">assert</A><BR>
-<A HREF="manual.html#Assignment">Assignment</A><BR>
<A HREF="manual.html#associative arrays">associative arrays</A><BR>
<A HREF="manual.html#atan2">atan2</A><BR>
<A HREF="manual.html#atan">atan</A><BR>
<A HREF="manual.html#basic types">basic types</A><BR>
<A HREF="manual.html#block">block</A><BR>
-<A HREF="manual.html#C pointers">C pointers</A><BR>
-<A HREF="manual.html#C2lua">C2lua</A><BR>
<A HREF="manual.html#call">call</A><BR>
<A HREF="manual.html#captures">captures</A><BR>
<A HREF="manual.html#ceil">ceil</A><BR>
<A HREF="manual.html#character class">character class</A><BR>
<A HREF="manual.html#chunk">chunk</A><BR>
+<A HREF="manual.html#clock">clock</A><BR>
<A HREF="manual.html#closing a file">closing a file</A><BR>
-<A HREF="manual.html#Coercion">Coercion</A><BR>
<A HREF="manual.html#collectgarbage">collectgarbage</A><BR>
-<A HREF="manual.html#Comments">Comments</A><BR>
<A HREF="manual.html#concatenation event">concatenation event</A><BR>
<A HREF="manual.html#concatenation">concatenation</A><BR>
<A HREF="manual.html#condition expression">condition expression</A><BR>
<A HREF="manual.html#constructors">constructors</A><BR>
+<A HREF="manual.html#copytagmethods">copytagmethods</A><BR>
<A HREF="manual.html#cos">cos</A><BR>
<A HREF="manual.html#date">date</A><BR>
<A HREF="manual.html#debug pragma">debug pragma</A><BR>
+<A HREF="manual.html#deg">deg</A><BR>
<A HREF="manual.html#div event">div event</A><BR>
<A HREF="manual.html#dofile">dofile</A><BR>
<A HREF="manual.html#dostring">dostring</A><BR>
+<A HREF="manual.html#eight-bit clean">eight-bit clean</A><BR>
<A HREF="manual.html#error method">error method</A><BR>
<A HREF="manual.html#error">error</A><BR>
<A HREF="manual.html#event">event</A><BR>
<A HREF="manual.html#execute">execute</A><BR>
<A HREF="manual.html#exit">exit</A><BR>
<A HREF="manual.html#exponentiation">exponentiation</A><BR>
-<A HREF="manual.html#Expressions">Expressions</A><BR>
<A HREF="manual.html#file handles">file handles</A><BR>
<A HREF="manual.html#floor">floor</A><BR>
+<A HREF="manual.html#foreachvar">foreachvar</A><BR>
+<A HREF="manual.html#foreach">foreach</A><BR>
<A HREF="manual.html#format">format</A><BR>
+<A HREF="manual.html#frexp">frexp</A><BR>
<A HREF="manual.html#function call">function call</A><BR>
-<A HREF="manual.html#Function Definitions">Function Definitions</A><BR>
<A HREF="manual.html#function event">function event</A><BR>
<A HREF="manual.html#function">function</A><BR>
<A HREF="manual.html#gc event">gc event</A><BR>
@@ -71,29 +98,25 @@
<A HREF="manual.html#gettagmethod">gettagmethod</A><BR>
<A HREF="manual.html#gettagmethod">gettagmethod</A><BR>
<A HREF="manual.html#global environment">global environment</A><BR>
-<A HREF="manual.html#Global variables">Global variables</A><BR>
<A HREF="manual.html#gsub">gsub</A><BR>
<A HREF="manual.html#gt event">gt event</A><BR>
-<A HREF="manual.html#Identifiers">Identifiers</A><BR>
<A HREF="manual.html#if-then-else">if-then-else</A><BR>
<A HREF="manual.html#index event">index event</A><BR>
-<A HREF="manual.html#_INPUT">_INPUT</A><BR>
+<A HREF="manual.html#ldexp">ldexp</A><BR>
<A HREF="manual.html#le event">le event</A><BR>
-<A HREF="manual.html#Literal strings">Literal strings</A><BR>
-<A HREF="manual.html#Local variables">Local variables</A><BR>
<A HREF="manual.html#log10">log10</A><BR>
-<A HREF="manual.html#log">log</A><BR>
<A HREF="manual.html#logical operators">logical operators</A><BR>
+<A HREF="manual.html#log">log</A><BR>
<A HREF="manual.html#lt event">lt event</A><BR>
-<A HREF="manual.html#Lua Stand-alone">Lua Stand-alone</A><BR>
<A HREF="manual.html#lua2C">lua2C</A><BR>
-<A HREF="manual.html#LUA_ANYTAG">LUA_ANYTAG</A><BR>
-<A HREF="manual.html#luac">luac</A><BR>
-<A HREF="manual.html#luac">luac</A><BR>
-<A HREF="manual.html#lua_callfunction">lua_callfunction</A><BR>
<A HREF="manual.html#lua_CFunction">lua_CFunction</A><BR>
+<A HREF="manual.html#lua_Object">lua_Object</A><BR>
+<A HREF="manual.html#lua_callfunction">lua_callfunction</A><BR>
+<A HREF="manual.html#lua_close">lua_close</A><BR>
<A HREF="manual.html#lua_collectgarbage">lua_collectgarbage</A><BR>
+<A HREF="manual.html#lua_copytagmethods">lua_copytagmethods</A><BR>
<A HREF="manual.html#lua_createtable">lua_createtable</A><BR>
+<A HREF="manual.html#lua_dobuffer">lua_dobuffer</A><BR>
<A HREF="manual.html#lua_dofile">lua_dofile</A><BR>
<A HREF="manual.html#lua_dostring">lua_dostring</A><BR>
<A HREF="manual.html#lua_error">lua_error</A><BR>
@@ -107,6 +130,7 @@
<A HREF="manual.html#lua_gettable">lua_gettable</A><BR>
<A HREF="manual.html#lua_gettagmethod">lua_gettagmethod</A><BR>
<A HREF="manual.html#lua_getuserdata">lua_getuserdata</A><BR>
+<A HREF="manual.html#lua_iolibopen">lua_iolibopen</A><BR>
<A HREF="manual.html#lua_iscfunction">lua_iscfunction</A><BR>
<A HREF="manual.html#lua_isfunction">lua_isfunction</A><BR>
<A HREF="manual.html#lua_isnil">lua_isnil</A><BR>
@@ -115,11 +139,13 @@
<A HREF="manual.html#lua_istable">lua_istable</A><BR>
<A HREF="manual.html#lua_isuserdata">lua_isuserdata</A><BR>
<A HREF="manual.html#lua_lua2C">lua_lua2C</A><BR>
+<A HREF="manual.html#lua_mathlibopen">lua_mathlibopen</A><BR>
<A HREF="manual.html#lua_newtag">lua_newtag</A><BR>
-<A HREF="manual.html#LUA_NOOBJECT">LUA_NOOBJECT</A><BR>
-<A HREF="manual.html#lua_Object">lua_Object</A><BR>
+<A HREF="manual.html#lua_open">lua_open</A><BR>
<A HREF="manual.html#lua_pop">lua_pop</A><BR>
+<A HREF="manual.html#lua_pushcclosure">lua_pushcclosure</A><BR>
<A HREF="manual.html#lua_pushcfunction">lua_pushcfunction</A><BR>
+<A HREF="manual.html#lua_pushlstring">lua_pushlstring</A><BR>
<A HREF="manual.html#lua_pushnil">lua_pushnil</A><BR>
<A HREF="manual.html#lua_pushnumber">lua_pushnumber</A><BR>
<A HREF="manual.html#lua_pushobject">lua_pushobject</A><BR>
@@ -134,11 +160,17 @@
<A HREF="manual.html#lua_register">lua_register</A><BR>
<A HREF="manual.html#lua_seterrormethod">lua_seterrormethod</A><BR>
<A HREF="manual.html#lua_setglobal">lua_setglobal</A><BR>
+<A HREF="manual.html#lua_setstate">lua_setstate</A><BR>
<A HREF="manual.html#lua_settable">lua_settable</A><BR>
-<A HREF="manual.html#lua_settag">lua_settag</A><BR>
<A HREF="manual.html#lua_settagmethod">lua_settagmethod</A><BR>
+<A HREF="manual.html#lua_settag">lua_settag</A><BR>
+<A HREF="manual.html#lua_state">lua_state</A><BR>
+<A HREF="manual.html#lua_strlen">lua_strlen</A><BR>
+<A HREF="manual.html#lua_strlibopen">lua_strlibopen</A><BR>
<A HREF="manual.html#lua_tag">lua_tag</A><BR>
<A HREF="manual.html#lua_unref">lua_unref</A><BR>
+<A HREF="manual.html#luac">luac</A><BR>
+<A HREF="manual.html#luac">luac</A><BR>
<A HREF="manual.html#max">max</A><BR>
<A HREF="manual.html#methods">methods</A><BR>
<A HREF="manual.html#min">min</A><BR>
@@ -146,15 +178,12 @@
<A HREF="manual.html#mul event">mul event</A><BR>
<A HREF="manual.html#multiple assignment">multiple assignment</A><BR>
<A HREF="manual.html#newtag">newtag</A><BR>
-<A HREF="manual.html#next">next</A><BR>
<A HREF="manual.html#nextvar">nextvar</A><BR>
+<A HREF="manual.html#next">next</A><BR>
<A HREF="manual.html#nil">nil</A><BR>
<A HREF="manual.html#not">not</A><BR>
<A HREF="manual.html#number">number</A><BR>
-<A HREF="manual.html#Numerical constants">Numerical constants</A><BR>
-<A HREF="manual.html#Operator precedence">Operator precedence</A><BR>
<A HREF="manual.html#or">or</A><BR>
-<A HREF="manual.html#_OUTPUT">_OUTPUT</A><BR>
<A HREF="manual.html#packed results">packed results</A><BR>
<A HREF="manual.html#pattern item">pattern item</A><BR>
<A HREF="manual.html#pattern">pattern</A><BR>
@@ -165,17 +194,18 @@
<A HREF="manual.html#pow event">pow event</A><BR>
<A HREF="manual.html#pre-compilation">pre-compilation</A><BR>
<A HREF="manual.html#predefined functions">predefined functions</A><BR>
-<A HREF="manual.html#Pre-processor">Pre-processor</A><BR>
<A HREF="manual.html#print">print</A><BR>
-<A HREF="manual.html#random">random</A><BR>
+<A HREF="manual.html#protected calls">protected calls</A><BR>
+<A HREF="manual.html#rad">rad</A><BR>
<A HREF="manual.html#randomseed">randomseed</A><BR>
+<A HREF="manual.html#random">random</A><BR>
<A HREF="manual.html#rawgetglobal">rawgetglobal</A><BR>
<A HREF="manual.html#rawgettable">rawgettable</A><BR>
<A HREF="manual.html#rawsetglobal">rawsetglobal</A><BR>
<A HREF="manual.html#rawsettable">rawsettable</A><BR>
<A HREF="manual.html#read pattern">read pattern</A><BR>
-<A HREF="manual.html#read">read</A><BR>
<A HREF="manual.html#readfrom">readfrom</A><BR>
+<A HREF="manual.html#read">read</A><BR>
<A HREF="manual.html#records">records</A><BR>
<A HREF="manual.html#reference">reference</A><BR>
<A HREF="manual.html#reflexivity">reflexivity</A><BR>
@@ -189,19 +219,18 @@
<A HREF="manual.html#self">self</A><BR>
<A HREF="manual.html#setglobal event">setglobal event</A><BR>
<A HREF="manual.html#setglobal">setglobal</A><BR>
+<A HREF="manual.html#setlocale">setlocale</A><BR>
<A HREF="manual.html#settable event">settable event</A><BR>
-<A HREF="manual.html#settag">settag</A><BR>
<A HREF="manual.html#settagmethod">settagmethod</A><BR>
<A HREF="manual.html#settagmethod">settagmethod</A><BR>
+<A HREF="manual.html#settag">settag</A><BR>
<A HREF="manual.html#short-cut evaluation">short-cut evaluation</A><BR>
-<A HREF="manual.html#Simple Expressions">Simple Expressions</A><BR>
<A HREF="manual.html#sin">sin</A><BR>
<A HREF="manual.html#skips">skips</A><BR>
<A HREF="manual.html#sqrt">sqrt</A><BR>
<A HREF="manual.html#statements">statements</A><BR>
-<A HREF="manual.html#_STDERR">_STDERR</A><BR>
-<A HREF="manual.html#_STDIN">_STDIN</A><BR>
-<A HREF="manual.html#_STDOUT">_STDOUT</A><BR>
+<A HREF="manual.html#strbyte">strbyte</A><BR>
+<A HREF="manual.html#strchar">strchar</A><BR>
<A HREF="manual.html#strfind">strfind</A><BR>
<A HREF="manual.html#string">string</A><BR>
<A HREF="manual.html#strlen">strlen</A><BR>
@@ -211,7 +240,6 @@
<A HREF="manual.html#strupper">strupper</A><BR>
<A HREF="manual.html#sub event">sub event</A><BR>
<A HREF="manual.html#table">table</A><BR>
-<A HREF="manual.html#Tag Methods">Tag Methods</A><BR>
<A HREF="manual.html#tag">tag</A><BR>
<A HREF="manual.html#tag">tag</A><BR>
<A HREF="manual.html#tan">tan</A><BR>
@@ -220,22 +248,17 @@
<A HREF="manual.html#tonumber">tonumber</A><BR>
<A HREF="manual.html#tostring">tostring</A><BR>
<A HREF="manual.html#type">type</A><BR>
-<A HREF="manual.html#Types and Tags">Types and Tags</A><BR>
<A HREF="manual.html#unm event">unm event</A><BR>
<A HREF="manual.html#userdata">userdata</A><BR>
<A HREF="manual.html#vararg">vararg</A><BR>
-<A HREF="manual.html#version 1.1">version 1.1</A><BR>
-<A HREF="manual.html#version 2.1">version 2.1</A><BR>
-<A HREF="manual.html#version 2.2">version 2.2</A><BR>
-<A HREF="manual.html#version 2.4">version 2.4</A><BR>
-<A HREF="manual.html#version 2.5">version 2.5</A><BR>
+<A HREF="manual.html#version 3.0">version 3.0</A><BR>
<A HREF="manual.html#while-do">while-do</A><BR>
-<A HREF="manual.html#write">write</A><BR>
<A HREF="manual.html#writeto">writeto</A><BR>
+<A HREF="manual.html#write">write</A><BR>
<HR>
Last update:
-Tue Jul 1 07:55:45 EST 1997
+Fri Jul 10 15:10:14 EST 1998
by <A HREF="http://www.tecgraf.puc-rio.br/~lhf/">lhf</A>.
</BODY>
</HTML>
diff --git a/doc/index.html b/doc/index.html
index 5ce16c44..ca4a2812 100644
--- a/doc/index.html
+++ b/doc/index.html
@@ -1,15 +1,17 @@
<HTML>
<HEAD>
-<TITLE>Lua 3.0 Reference Manual - Index</TITLE>
+<TITLE>Lua 3.1 Reference Manual - Contents</TITLE>
</HEAD>
<BODY>
-<H1>Reference Manual of the Programming Language Lua 3.0</H1>
+<H1>Reference Manual of the Programming Language
+<A HREF="http://www.tecgraf.puc-rio.br/lua/">Lua</A>
+3.1</H1>
<P>
<A HREF="http://www.inf.puc-rio.br/~roberto/">Roberto Ierusalimschy</A>,
<A HREF="http://www2.lncc.br/~lhf/">Luiz Henrique de Figueiredo</A>,
-<A HREF="http://www.graphics.cornell.edu/~celes/">Waldemar Celes</A>
+<A HREF="http://www.tecgraf.puc-rio.br/~celes/">Waldemar Celes</A>
<BR>
<A HREF="mailto:lua@tecgraf.puc-rio.br">lua@tecgraf.puc-rio.br</A><BR>
<A HREF="http://www.tecgraf.puc-rio.br">TeCGraf</A>,
@@ -17,22 +19,17 @@
<A HREF="http://www.puc-rio.br">PUC-Rio</A>
<P>
+[
+<A HREF="manual.html">top</A>
+|
+<A HREF="#contents">contents</A>
+|
+<A HREF="idx.html">index</A>
+|
+<A HREF="ftp://ftp.tecgraf.puc-rio.br/pub/lua/lua.ps.gz">ps</A>
+]
<HR>
-<B>Abstract.</B>
-Lua is an extension programming language designed to be used
-as a configuration language for any program that needs one.
-This document describes version 3.0 of the Lua programming language and
-the API that allows interaction between Lua programs and their host C programs.
-<HR>
-
-<B>Sumário.</B>
-Lua é uma linguagem de extensão projetada para ser usada como
-linguagem de configuração em qualquer programa que precise de uma.
-Este documento descreve a versão 3.0 da linguagem de
-programação Lua e a Interface de Programação (API) que permite
-a interação entre programas Lua e programas C hospedeiros.
-<HR>
-<A NAME="index"></A>
+<A NAME="contents"></A>
<UL>
<LI><A HREF="manual.html#1.">1 - Introduction</A>
<LI><A HREF="manual.html#2.">2 - Environment and Chunks</A>
@@ -45,18 +42,20 @@ a interação entre programas Lua e programas C hospedeiros.
<LI><A HREF="manual.html#4.4">4.4 - Adjustment</A>
<LI><A HREF="manual.html#4.5">4.5 - Statements</A>
<LI><A HREF="manual.html#4.6">4.6 - Expressions</A>
- <LI><A HREF="manual.html#4.7">4.7 - Function Definitions</A>
+ <LI><A HREF="manual.html#4.7">4.7 - Visibility and Upvalues</A>
<LI><A HREF="manual.html#4.8">4.8 - Tag Methods</A>
<LI><A HREF="manual.html#4.9">4.9 - Error Handling</A>
</UL>
<LI><A HREF="manual.html#5.">5 - The Application Program Interface</A>
<UL>
- <LI><A HREF="manual.html#5.1">5.1 - Exchanging Values between C and Lua</A>
- <LI><A HREF="manual.html#5.2">5.2 - Executing Lua Code</A>
- <LI><A HREF="manual.html#5.3">5.3 - Manipulating Lua Objects</A>
- <LI><A HREF="manual.html#5.4">5.4 - Calling Lua Functions</A>
- <LI><A HREF="manual.html#5.5">5.5 - C Functions</A>
- <LI><A HREF="manual.html#5.6">5.6 - References to Lua Objects</A>
+ <LI><A HREF="manual.html#5.1">5.1 - Managing States</A>
+ <LI><A HREF="manual.html#5.2">5.2 - Exchanging Values between C and Lua</A>
+ <LI><A HREF="manual.html#5.3">5.3 - Garbage Collection</A>
+ <LI><A HREF="manual.html#5.4">5.4 - Executing Lua Code</A>
+ <LI><A HREF="manual.html#5.5">5.5 - Manipulating Lua Objects</A>
+ <LI><A HREF="manual.html#5.6">5.6 - Calling Lua Functions</A>
+ <LI><A HREF="manual.html#5.7">5.7 - C Functions</A>
+ <LI><A HREF="manual.html#5.8">5.8 - References to Lua Objects</A>
</UL>
<LI><A HREF="manual.html#6.">6 - Predefined Functions and Libraries</A>
<UL>
@@ -79,7 +78,7 @@ a interação entre programas Lua e programas C hospedeiros.
<HR>
Last update:
-Tue Jul 1 07:55:45 EST 1997
+Fri Jul 10 15:10:14 EST 1998
by <A HREF="http://www.tecgraf.puc-rio.br/~lhf/">lhf</A>.
</BODY>
</HTML>
diff --git a/doc/luac.html b/doc/luac.html
index 7d43ff1d..1948e7ab 100644
--- a/doc/luac.html
+++ b/doc/luac.html
@@ -1,88 +1,158 @@
-<!-- manual page source format generated by RosettaMan, -->
-<!-- available via anonymous ftp from ftp.cs.berkeley.edu:/ucb/people/phelps/tcltk/rman.tar.Z -->
-<HTML>
-<HEADER>
-<TITLE>luac manual page</TITLE>
-</HEADER>
-<BODY>
-<A HREF="#toc">Table of Contents</A><P>
-
-<A NAME="sect0" HREF="#toc0"><H2>NAME</H2></A>
-luac - Lua compiler<P>
-
-<A NAME="sect1" HREF="#toc1"><H2>SYNOPSIS</H2></A>
-luac [ -c | -u ] [ -d ] [ -l ] [ -p ] [ -q ] [ -v ] [ -o <I>outputfile</I> ] <I>sourcefile</I> ...<P>
-
-<A NAME="sect2" HREF="#toc2"><H2>DESCRIPTION</H2></A>
-luac is the Lua compiler. It translates programs written in the Lua programming language into binary files that can be loaded and executed with lua_dofile in C or dofile in Lua.<P>
-luac produces a single output file containing the bytecode for all named source files. By default, the output file is named luac.out, but you can change this with the -o option.<P>
-You can use &quot;-&quot; to indicate stdin as a source file.<P>
-luac can also load and list binary files.<P>
-
-<A NAME="sect3" HREF="#toc3"><H2>OPTIONS</H2></A>
-<dl>
-<dt>-c <dd> compile (this is the default).<P>
-</dd>
-</dl>
-<dl>
-<dt>-u <dd> undump.<P>
-</dd>
-</dl>
-<dl>
-<dt>-d <dd> turns debugging on.<P>
-</dd>
-</dl>
-<dl>
-<dt>-l <dd> produces a listing of the compiled bytecode for Lua's virtual machine. This is the default when undumping.<P>
-</dd>
-</dl>
-<dl>
-<dt>-p <dd> parses sources files but does not generate any output file.<P>
-</dd>
-</dl>
-<dl>
-<dt>-q <dd> quiet; procudes no listing. This is the default when compiling.<P>
-</dd>
-</dl>
-<dl>
-<dt>-v <dd> prints version information.<P>
-</dd>
-</dl>
-<dl>
-<dt>-o <I>outputfile</I><dd> Name the output file <I>output</I>, instead of the default luac.out. The output file cannot be a source file.<P>
-</dd>
-</dl>
-
-<A NAME="sect4" HREF="#toc4"><H2>FILES</H2></A>
-<dl>
-<dt>luac.out <dd> default output file<P>
-</dd>
-</dl>
-
-<A NAME="sect5" HREF="#toc5"><H2>SEE ALSO</H2></A>
-<A HREF="http://localhost/cgi-bin/man2html?lua?1">lua(1)</A><P>
-<I>Reference</I> <I>Manual</I> <I>of</I> <I>the</I> <I>Programming</I> <I>Language</I> <I>Lua</I><P>
-
-<A NAME="sect6" HREF="#toc6"><H2>DIAGNOSTICS</H2></A>
-Error messages should be self explanatory.<P>
-
-<A NAME="sect7" HREF="#toc7"><H2>BUGS</H2></A>
-Inherits any bugs from Lua, but Lua has no bugs...<P>
-
-<A NAME="sect8" HREF="#toc8"><H2>AUTHORS</H2></A>
-W. Celes, R. Ierusalimschy &amp; L. H. de Figueiredo (lua@tecgraf.puc-rio.br)<P>
-
-<HR><P>
-<A NAME="toc"><B>Table of Contents</B></A><P>
-<UL>
-<LI><A NAME="toc0" HREF="#sect0">NAME</A></LI>
-<LI><A NAME="toc1" HREF="#sect1">SYNOPSIS</A></LI>
-<LI><A NAME="toc2" HREF="#sect2">DESCRIPTION</A></LI>
-<LI><A NAME="toc3" HREF="#sect3">OPTIONS</A></LI>
-<LI><A NAME="toc4" HREF="#sect4">FILES</A></LI>
-<LI><A NAME="toc5" HREF="#sect5">SEE ALSO</A></LI>
-<LI><A NAME="toc6" HREF="#sect6">DIAGNOSTICS</A></LI>
-<LI><A NAME="toc7" HREF="#sect7">BUGS</A></LI>
-<LI><A NAME="toc8" HREF="#sect8">AUTHORS</A></LI>
-</UL>
-</BODY></HTML>
+<!-- $Id: luac.man,v 1.8 1998/06/13 16:54:15 lhf Exp $ -->
+<TITLE>LUAC 1 "06 February 1998"</TITLE>
+<H1>NAME</H1>
+luac - Lua compiler
+<H1>SYNOPSIS</H1>
+<B>luac</B>
+[
+<B>-c</B>
+|
+<B>-u</B>
+] [
+<B>-D</B>
+<I>name</I>
+] [
+<B>-d</B>
+] [
+<B>-l</B>
+] [
+<B>-O</B>
+] [
+<B>-o</B>
+<I>filename</I>
+] [
+<B>-p</B>
+] [
+<B>-q</B>
+] [
+<B>-v</B>
+] [
+<B>-V</B>
+]
+<I>sourcefile </I>...
+<H1>DESCRIPTION</H1>
+<B>luac</B>
+is the Lua compiler.
+It translates programs written in the Lua programming language
+into binary files that can be loaded and executed with
+<B>lua_dofile</B>
+in C or with
+<B>dofile</B>
+in Lua.
+<P>
+The main advantages of pre-compiling chunks are:
+faster loading,
+protecting source code from user changes,
+off-line syntax error detection.
+The binary files created by
+<B>luac</B>
+are portable to all known architectures.
+<P>
+<B>luac</B>
+produces a single output file containing the bytecodes
+for all source files given.
+By default,
+the output file is named
+<B>luac.out</B>,
+but you can change this with the
+<B>-o</B>
+option.
+<P>
+You can use
+<B>"-"</B>
+to indicate
+<I>stdin</I>
+as a source file.
+<P>
+<B>luac</B>
+can also load and list binary files with the
+<B>-u</B>
+option.
+<P>
+Binary files produced by differents runs of
+<B>luac</B>
+can be combined into one large file,
+using
+<B>cat</B>(1).
+The result is still a valid binary file,
+and can be loaded with a single call to
+<B>lua_dofile</B>
+or
+<B>dofile</B>.
+<P>
+<H1>OPTIONS</H1>
+<P>
+<B>-c</B>
+compile (this is the default).
+<P>
+<B>-u</B>
+undump, i.e., load and list the given binary files.
+If no files are given, then luac undumps
+<B>luac.out</B>.
+<P>
+<B>-D "</B><I>name"</I>
+predefine symbol
+<I>name</I>
+for conditional compilation.
+<P>
+<B>-d</B>
+turn debugging on.
+Individual chunks may
+still control the generation of debug information with
+$debug and $nodebug.
+<P>
+<B>-l</B>
+produce a listing of the compiled bytecode for Lua's virtual machine.
+This is the default when undumping.
+<P>
+<B>-O</B>
+optimize code.
+Debug information is removed,
+duplicate constants are coalesced.
+<P>
+<B>-o "</B><I>filename"</I>
+output to
+<I>filename</I>,
+instead of the default
+<B>luac.out</B>.
+The output file cannot be a source file.
+<P>
+<B>-p</B>
+parse sources files but does not generate any output file.
+Used mainly for syntax checking.
+<P>
+<B>-q</B>
+quiet; produces no listing.
+This is the default when compiling.
+<P>
+<B>-v</B>
+print version information.
+<P>
+<B>-V</B>
+verbose;
+print the names of the source files as they are processed.
+<H1>FILES</H1>
+<P>
+<B>luac.out</B>
+default output file
+<H1>"SEE ALSO"</H1>
+<B>lua</B>(1)
+<BR>
+<I>"Reference Manual of the Programming Language Lua"</I>
+<BR>
+<A HREF="http://www.tecgraf.puc-rio.br/lua/">http://www.tecgraf.puc-rio.br/lua/</A>
+<BR>
+"Lua: an extensible extension language",
+<I>Software: Practice &amp; Experience</I>
+<B>26</B>
+#6 (1996) 635-652.
+<H1>DIAGNOSTICS</H1>
+Error messages should be self explanatory.
+<H1>BUGS</H1>
+Inherits any bugs from Lua,
+but Lua has no bugs...
+<H1>AUTHORS</H1>
+L. H. de Figueiredo,
+R. Ierusalimschy and
+W. Celes
+<I>(<A HREF="mailto:lua@tecgraf.puc-rio.br">lua@tecgraf.puc-rio.br</A>)</I>
diff --git a/doc/luac.man b/doc/luac.man
new file mode 100644
index 00000000..e5c90738
--- /dev/null
+++ b/doc/luac.man
@@ -0,0 +1,165 @@
+.\" $Id: luac.man,v 1.11 1998/07/01 14:51:45 lhf Exp $
+.TH LUAC 1 "01 July 1998"
+.SH NAME
+luac \- Lua compiler
+.SH SYNOPSIS
+.B luac
+[
+.B \-c
+|
+.B \-u
+] [
+.B \-d
+] [
+.B \-D
+.I name
+] [
+.B \-l
+] [
+.B \-o
+.I filename
+] [
+.B \-O
+] [
+.B \-p
+] [
+.B \-q
+] [
+.B \-v
+] [
+.B \-V
+]
+.IR sourcefile " ..."
+.SH DESCRIPTION
+.B luac
+is the Lua compiler.
+It translates programs written in the Lua programming language
+into binary files that can be loaded and executed with
+.B lua_dofile
+in C or with
+.B dofile
+in Lua.
+.LP
+The main advantages of pre-compiling chunks are:
+faster loading,
+protecting source code from user changes,
+off-line syntax error detection.
+The binary files created by
+.B luac
+are portable to all known architectures.
+.LP
+.B luac
+produces a single output file containing the bytecodes
+for all source files given.
+By default,
+the output file is named
+.BR luac.out ,
+but you can change this with the
+.B \-o
+option.
+.LP
+You can use
+.B "\-"
+to indicate
+.I stdin
+as a source file.
+.LP
+.B luac
+can also load and list binary files with the
+.B \-u
+option.
+.LP
+Binary files produced by differents runs of
+.B luac
+can be combined into one large file,
+using
+.BR cat (1).
+The result is still a valid binary file,
+and can be loaded with a single call to
+.B lua_dofile
+or
+.BR dofile .
+.LP
+.SH OPTIONS
+.TP
+.B \-c
+compile (this is the default).
+.TP
+.B \-u
+undump, i.e., load and list the given binary files.
+If no files are given, then luac undumps
+.BR luac.out .
+.TP
+.BI \-D " name"
+predefine symbol
+.I name
+for conditional compilation.
+By default,
+.B luac
+does
+.I not
+predefine any symbols,
+not even the built-in functions.
+.TP
+.B \-d
+turn debugging on.
+Individual chunks may
+still control the generation of debug information with
+$debug and $nodebug.
+If debugging is on, then listings show the names of the local variables.
+.TP
+.B \-l
+produce a listing of the compiled bytecode for Lua's virtual machine.
+This is the default when undumping.
+.TP
+.B \-O
+optimize code.
+Debug information is removed,
+duplicate constants are coalesced.
+.TP
+.BI \-o " filename"
+output to
+.IR filename ,
+instead of the default
+.BR luac.out .
+The output file cannot be a source file.
+.TP
+.B \-p
+parse sources files but does not generate any output file.
+Used mainly for syntax checking.
+.TP
+.B \-q
+quiet; produces no listing.
+This is the default when compiling.
+.TP
+.B \-v
+print version information.
+.TP
+.B \-V
+verbose;
+print the names of the source files as they are processed.
+.SH FILES
+.TP 15
+.B luac.out
+default output file
+.SH "SEE ALSO"
+.BR lua (1)
+.br
+.I "Reference Manual of the Programming Language Lua"
+.br
+http://www.tecgraf.puc-rio.br/lua/
+.br
+"Lua: an extensible extension language",
+.I Software: Practice & Experience
+.B 26
+#6 (1996) 635-652.
+.SH DIAGNOSTICS
+Error messages should be self explanatory.
+.SH BUGS
+Inherits any bugs from Lua,
+but Lua has no bugs...
+.SH AUTHORS
+L. H. de Figueiredo,
+R. Ierusalimschy and
+W. Celes
+.I (lua@tecgraf.puc-rio.br)
diff --git a/doc/manual.html b/doc/manual.html
index 0c3f531c..8707ec20 100644
--- a/doc/manual.html
+++ b/doc/manual.html
@@ -1,8 +1,8 @@
<HEAD>
-<TITLE>Lua 3.0 Reference Manual</TITLE>
+<TITLE>Lua 3.1 Reference Manual</TITLE>
</HEAD>
<BODY>
-<h1>Lua 3.0 Reference Manual</h1>
+<h1>Lua 3.1 Reference Manual</h1>
<!-- ====================================================================== -->
<HR>
@@ -12,7 +12,7 @@
Lua is an extension programming language designed to support
general procedural programming with data description
facilities.
-It is intended to be used as a light-weight, but powerful,
+Lua is intended to be used as a light-weight, but powerful,
configuration language for any program that needs one.
Lua has been designed and implemented by
W.&nbsp;Celes,
@@ -32,13 +32,13 @@ thus creating customized programming languages sharing a syntactical framework.
<P>
Lua is free-distribution software,
and provided as usual with no guarantees,
-as stated in the copyright notice in the front page of this manual.
+as stated in the copyright notice.
The implementation described in this manual is available
at the following URL's:
-<LISTING>
+<PRE>
<A HREF="http://www.tecgraf.puc-rio.br/lua/">http://www.tecgraf.puc-rio.br/lua/</A>
<A HREF="ftp://ftp.tecgraf.puc-rio.br/pub/lua/lua.tar.gz">ftp://ftp.tecgraf.puc-rio.br/pub/lua/lua.tar.gz</A>
-</LISTING>
+</PRE>
<P>
<P>
<!-- ====================================================================== -->
@@ -47,14 +47,16 @@ at the following URL's:
<H1>2 - Environment and Chunks</H1>
<P>
All statements in Lua are executed in a <A NAME="global environment"><EM>global environment</EM></A>.
-This environment, which keeps all global variables and functions,
+This environment, which keeps all global variables,
is initialized at the beginning of the embedding program and
persists until its end.
+Optionally, a user can create multiple independent global
+environments (see Section&nbsp;<A HREF="#mangstate">5.1</A>).
<P>
The global environment can be manipulated by Lua code or
by the embedding program,
which can read and write global variables
-using functions in the library that implements Lua.
+using functions from the API library that implements Lua.
<P>
<A NAME="Global variables">Global variables</A> do not need declaration.
Any variable is assumed to be global unless explicitly declared local
@@ -63,28 +65,24 @@ Before the first assignment, the value of a global variable is <B>nil</B>;
this default can be changed (see Section&nbsp;<A HREF="#tag-method">4.8</A>).
<P>
The unit of execution of Lua is called a <A NAME="chunk"><EM>chunk</EM></A>.
-The syntax
-\footnote{As usual, {<EM>a</EM>} means 0 or more <EM>a</EM>'s,
+A chunk is simply a sequence of statements:
+<PRE>
+chunk ::= {stat} [ret]
+</PRE>
+Statements are described in Section&nbsp;<A HREF="#stats">4.5</A>.
+(As usual, {<EM>a</EM>} means 0 or more <EM>a</EM>'s,
[<EM>a</EM>] means an optional <EM>a</EM> and ('<EM>a</EM>)+ means
-one or more <EM>a</EM>'s.}
-for chunks is:
-<LISTING>
-chunk ::= {stat | function} [ret]
-</LISTING>
-A chunk may contain statements and function definitions,
-and may be in a file or in a string inside the host program.
+one or more <EM>a</EM>'s.)
+<P>
+A chunk may be in a file or in a string inside the host program.
A chunk may optionally end with a <CODE>return</CODE> statement (see Section&nbsp;<A HREF="#return">4.5.3</A>).
-When a chunk is executed, first all its functions and statements are compiled,
+When a chunk is executed, first all its code is pre-compiled,
then the statements are executed in sequential order.
All modifications a chunk effects on the global environment persist
after its end.
-Those include modifications to global variables and definitions
-of new functions
-\footnote{Actually, a function definition is an
-assignment to a global variable (see Section&nbsp;<A HREF="#TypesSec">3</A>).}.
<P>
-Chunks may be pre-compiled into binary form;
-see program <A NAME="luac"><TT>luac</TT></A> for details.
+Chunks may also be pre-compiled into binary form;
+see program <A NAME="luac"><TT><A HREF="luac.html">luac</A></TT></A> for details.
Text files with chunks and their binary pre-compiled forms
are interchangeable.
Lua automatically detects the file type and acts accordingly.
@@ -106,29 +104,30 @@ There are six <A NAME="basic types">basic types</A> in Lua: <A NAME="nil"><EM>ni
<A NAME="string"><EM>string</EM></A>, <A NAME="function"><EM>function</EM></A>, <A NAME="userdata"><EM>userdata</EM></A>, and <A NAME="table"><EM>table</EM></A>.
<EM>Nil</EM> is the type of the value <B>nil</B>,
whose main property is to be different from any other value.
-<EM>Number</EM> represents real (floating-point) numbers,
+<EM>Number</EM> represents real (double precision floating point) numbers,
while <EM>string</EM> has the usual meaning.
+Lua is <A NAME="eight-bit clean">eight-bit clean</A>,
+and so strings may contain any 8-bit character,
+<EM>including</EM> embedded zeros (<CODE>'\0'</CODE>).
The function <CODE>type</CODE> returns a string describing the type
of a given value (see Section&nbsp;<A HREF="#pdf-type">6.1</A>).
<P>
Functions are considered first-class values in Lua.
This means that functions can be stored in variables,
-passed as arguments to other functions and returned as results.
-When a function is defined in Lua, its body is compiled and stored
-in a given variable.
+passed as arguments to other functions, and returned as results.
Lua can call (and manipulate) functions written in Lua and
functions written in C.
They can be distinguished by their tags:
all Lua functions have the same tag,
and all C functions have the same tag,
-which is different from the tag of a Lua function.
+which is different from the tag of Lua functions.
<P>
The type <EM>userdata</EM> is provided to allow
arbitrary <A NAME="C pointers">C pointers</A> to be stored in Lua variables.
It corresponds to a <CODE>void*</CODE> and has no pre-defined operations in Lua,
besides assignment and equality test.
However, by using <EM>tag methods</EM>,
-the programmer may define operations for <EM>userdata</EM> values
+the programmer can define operations for <EM>userdata</EM> values
(see Section&nbsp;<A HREF="#tag-method">4.8</A>).
<P>
The type <EM>table</EM> implements <A NAME="associative arrays">associative arrays</A>,
@@ -136,6 +135,7 @@ that is, <A NAME="arrays">arrays</A> that can be indexed not only with numbers,
but with any value (except <B>nil</B>).
Therefore, this type may be used not only to represent ordinary arrays,
but also symbol tables, sets, records, etc.
+Tables are the main data structuring mechanism in Lua.
To represent <A NAME="records">records</A>, Lua uses the field name as an index.
The language supports this representation by
providing <CODE>a.name</CODE> as syntactic sugar for <CODE>a["name"]</CODE>.
@@ -144,23 +144,25 @@ Because functions are first class values,
table fields may contain functions.
The form <CODE>t:f(x)</CODE> is syntactic sugar for <CODE>t.f(t,x)</CODE>,
which calls the method <CODE>f</CODE> from the table <CODE>t</CODE> passing
-itself as the first parameter (see Section&nbsp;<A HREF="#func-def">4.7</A>).
+itself as the first parameter (see Section&nbsp;<A HREF="#func-def">4.6.9</A>).
<P>
-It is important to notice that tables are <EM>objects</EM>, and not values.
+Note that tables are <EM>objects</EM>, and not values.
Variables cannot contain tables, only <EM>references</EM> to them.
-Assignment, parameter passing and returns always manipulate references
+Assignment, parameter passing, and returns always manipulate references
to tables, and do not imply any kind of copy.
Moreover, tables must be explicitly created before used
(see Section&nbsp;<A HREF="#tableconstructor">4.6.7</A>).
<P>
Tags are mainly used to select tag methods when
-some events occur (see Section&nbsp;<A HREF="#tag-method">4.8</A>).
-Each of the types nil, number and string has a different tag.
+some events occur.
+Tag methods are the main mechanism for extending the
+semantics of Lua (see Section&nbsp;<A HREF="#tag-method">4.8</A>).
+Each of the types <EM>nil</EM>, <EM>number</EM> and <EM>string</EM> has a different tag.
All values of each of these types have this same pre-defined tag.
-Values of type function can have two different tags,
+Values of type <EM>function</EM> can have two different tags,
depending on whether they are Lua or C functions.
Finally,
-values of type userdata and table can have
+values of type <EM>userdata</EM> and <EM>table</EM> can have
as many different tags as needed (see Section&nbsp;<A HREF="#tag-method">4.8</A>).
Tags are created with the function <CODE>newtag</CODE>,
and the function <CODE>tag</CODE> returns the tag of a given value.
@@ -180,69 +182,101 @@ This section describes the lexis, the syntax and the semantics of Lua.
<A NAME="4.1"></A>
<H2>4.1 - Lexical Conventions</H2>
<P>
-Lua is a case-sensitive language.
-<A NAME="Identifiers">Identifiers</A> can be any string of letters, digits, and underscores,
+<A NAME="Identifiers">Identifiers</A> in Lua can be any string of letters,
+digits, and underscores,
not beginning with a digit.
+The definition of letter depends on the current locale:
+Any character considered alphabetic by the current locale
+can be used in an identifier.
The following words are reserved, and cannot be used as identifiers:
<A NAME="reserved words"></A>
-<LISTING>
+<PRE>
and do else elseif
end function if local
nil not or repeat
return then until while
-</LISTING>
+</PRE>
+Lua is a case-sensitive language:
+<TT>and</TT> is a reserved word, but <TT>And</TT> and <TT>\'and</TT>
+(if the locale permits) are two other different identifiers.
+As a convention, identifiers starting with underscore followed by
+uppercase letters should not be used in regular programs.
<P>
The following strings denote other <A NAME="tokens">tokens</A>:
-<LISTING>
- ~= &lt;= &gt;= &lt; &gt; == = .. + - * /
- % ( ) { } [ ] ; , . ...
-</LISTING>
+<PRE>
+ ~= &lt;= &gt;= &lt; &gt; == = + - * /
+ ( ) { } [ ] ; , . .. ...
+</PRE>
<P>
<A NAME="Literal strings">Literal strings</A> can be delimited by matching single or double quotes,
and can contain the C-like escape sequences
-<CODE>'\n'</CODE>, <CODE>'\t'</CODE> and <CODE>'\r'</CODE>.
+<CODE>'\a'</CODE> (bell),
+<CODE>'\b'</CODE> (back space),
+<CODE>'\f'</CODE> (form feed),
+<CODE>'\n'</CODE> (new line),
+<CODE>'\r'</CODE> (carriage return),
+<CODE>'\t'</CODE> (horizontal tab),
+<CODE>'\v'</CODE> (vertical tab),
+<CODE>'\\'</CODE>, (backslash),
+<CODE>'\"'</CODE>, (double quote),
+and <CODE>'\''</CODE> (single quote).
+A character in a string may also be specified by its numerical value,
+through the escape sequence <CODE>'\ddd'</CODE>,
+where <CODE>ddd</CODE> is a sequence of up to three <EM>decimal</EM> digits.
+Strings in Lua may contain any 8-bit value, including embedded 0.
+<P>
Literal strings can also be delimited by matching <CODE>[[ ... ]]</CODE>.
Literals in this bracketed form may run for several lines,
may contain nested <CODE>[[ ... ]]</CODE> pairs,
and do not interpret escape sequences.
This form is specially convenient for
-handling strings that contain program pieces or
+writing strings that contain program pieces or
other quoted strings.
+As an example, in a system using ASCII,
+the following three literals are equivalent:
+<PRE>
+1) "alo\n123\""
+2) '\97lo\10\04923"'
+3) [[alo
+ 123"]]
+</PRE>
+<P>
<P>
<A NAME="Comments">Comments</A> start anywhere outside a string with a
double hyphen (<CODE>--</CODE>) and run until the end of the line.
Moreover,
-the first line of a chunk file is skipped if it starts with <CODE>#</CODE>
-\footnote{This facility allows the use of Lua as a script interpreter
-in Unix systems (see Section&nbsp;<A HREF="#lua-sa">8</A>).}.
+the first line of a chunk is skipped if it starts with <CODE>#</CODE>.
+This facility allows the use of Lua as a script interpreter
+in Unix systems (see Section&nbsp;<A HREF="#lua-sa">8</A>).
<P>
<A NAME="Numerical constants">Numerical constants</A> may be written with an optional decimal part,
and an optional decimal exponent.
Examples of valid numerical constants are:
-<LISTING>
+<PRE>
4 4.0 0.4 4.57e-3 0.3e12
-</LISTING>
+</PRE>
<P>
<A NAME="pre-processor"></A>
<A NAME="4.2"></A>
<H2>4.2 - The Pre-processor</H2>
<P>
-All lines that start with a <CODE>$</CODE> are handled by a pre-processor.
-The <CODE>$</CODE> can be followed by any of the following directives:
+All lines that start with a <CODE>$</CODE> sign are handled by a pre-processor.
+The <CODE>$</CODE> sign must be immediately
+followed by one of the following directives:
<DL>
-<DT><B><TT>debug</TT></B><DD> - turn on some debugging facilities (see Section&nbsp;<A HREF="#pragma">4.9</A>).
-<DT><B><TT>nodebug</TT></B><DD> - turn off some debugging facilities (see Section&nbsp;<A HREF="#pragma">4.9</A>).
+<DT><B><TT>debug</TT></B><DD> - turn on debugging facilities (see Section&nbsp;<A HREF="#pragma">4.9</A>).
+<DT><B><TT>nodebug</TT></B><DD> - turn off debugging facilities (see Section&nbsp;<A HREF="#pragma">4.9</A>).
<DT><B><TT>if <EM>cond</TT></EM></B><DD> - starts a conditional part.
If <EM>cond</EM> is false, then this part is skipped by the lexical analyzer.
<DT><B><TT>ifnot <EM>cond</TT></EM></B><DD> - starts a conditional part.
If <EM>cond</EM> is true, then this part is skipped by the lexical analyzer.
<DT><B><TT>end</TT></B><DD> - ends a conditional part.
<DT><B><TT>else</TT></B><DD> - starts an ``else'' conditional part,
-switching the ``skip'' status.
+flipping the ``skip'' status.
<DT><B><TT>endinput</TT></B><DD> - ends the lexical parse of the file.
</DL>
<P>
-Directives can be freely nested.
+Directives may be freely nested.
Particularly, a <CODE>$endinput</CODE> may occur inside a <CODE>$if</CODE>;
in that case, even the matching <CODE>$end</CODE> is not parsed.
<P>
@@ -252,7 +286,7 @@ A <EM>cond</EM> part may be:
<DT><B><TT>1</TT></B><DD> - always true.
<DT><B><EM>name</EM></B><DD> - true if the value of the
global variable <EM>name</EM> is different from <B>nil</B>.
-Notice that <EM>name</EM> is evaluated before the chunk starts its execution.
+Note that <EM>name</EM> is evaluated <EM>before</EM> the chunk starts its execution.
Therefore, actions in a chunk do not affect its own conditional directives.
</DL>
<P>
@@ -260,7 +294,7 @@ Therefore, actions in a chunk do not affect its own conditional directives.
<A NAME="4.3"></A>
<H2>4.3 - Coercion</H2>
<P>
-Lua provides some automatic conversions between values.
+Lua provides some automatic conversions between values at run time.
Any arithmetic operation applied to a string tries to convert
that string to a number, following the usual rules.
Conversely, whenever a number is used when a string is expected,
@@ -283,12 +317,15 @@ the system does not know how many values a function will return,
or how many parameters it needs.
Therefore, sometimes, a list of values must be <EM>adjusted</EM>, at run time,
to a given length.
-If there are more values than are needed, then the last values are thrown away.
-If there are more needs than values, then the list is extended with as
-many <B>nil</B>'s as needed.
-Adjustment occurs in multiple assignment and function calls.
+If there are more values than are needed,
+then the last values are thrown away.
+If there are more needs than values,
+then the list is extended with as many <B>nil</B>'s as needed.
+Adjustment occurs in multiple assignment (see Section&nbsp;<A HREF="#assignment">4.5.2</A>)
+and function calls (see Section&nbsp;<A HREF="#functioncall">4.6.8</A>).
<P>
<P>
+<A NAME="stats"></A>
<A NAME="4.5"></A>
<H2>4.5 - Statements</H2>
<P>
@@ -302,15 +339,21 @@ and local variable declarations (see Section&nbsp;<A HREF="#localvar">4.5.5</A>)
<P>
<H3>4.5.1 - Blocks</H3>
A <A NAME="block">block</A> is a list of statements, which are executed sequentially.
-Any statement can be optionally followed by a semicolon:
-<LISTING>
+A statement may be optionally followed by a semicolon:
+<PRE>
block ::= {stat sc} [ret]
sc ::= ['<B>;</B>']
-</LISTING>
+</PRE>
For syntactic reasons, a <A NAME="return"><TT>return</TT></A> statement can only be written
as the last statement of a block.
This restriction also avoids some ``statement not reached'' conditions.
<P>
+A block may be explicitly delimited:
+<PRE>
+stat ::= <B>do</B> block <B>end</B>
+</PRE>
+This is useful to control the scope of local variables (see Section&nbsp;<A HREF="#localvar">4.5.5</A>).
+<P>
<A NAME="assignment"></A>
<H3>4.5.2 - <A NAME="Assignment</H3>">Assignment</H3></A>
The language allows <A NAME="multiple assignment">multiple assignment</A>.
@@ -318,32 +361,39 @@ Therefore, the syntax for assignment
defines a list of variables on the left side,
and a list of expressions on the right side.
Both lists have their elements separated by commas:
-<LISTING>
+<PRE>
stat ::= varlist1 '<B>=</B>' explist1
varlist1 ::= var {'<B>,</B>' var}
-</LISTING>
+</PRE>
This statement first evaluates all values on the right side
and eventual indices on the left side,
and then makes the assignments.
Therefore, it can be used to exchange two values, as in
-<LISTING>
+<PRE>
x, y = y, x
-</LISTING>
+</PRE>
The two lists may have different lengths.
Before the assignment, the list of values is <EM>adjusted</EM> to
the length of the list of variables (see Section&nbsp;<A HREF="#adjust">4.4</A>).
<P>
A single name can denote a global or a local variable,
or a formal parameter:
-<LISTING>
+<PRE>
var ::= name
-</LISTING>
+</PRE>
Square brackets are used to index a table:
-<LISTING>
-var ::= var '<B>[</B>' exp1 '<B>]</B>'
-</LISTING>
-The <CODE>var</CODE> should result in a table value,
-where the field indexed by the expression value gets the assigned value.
+<PRE>
+var ::= simpleexp '<B>[</B>' exp1 '<B>]</B>'
+</PRE>
+The <EM>simpleexp</EM> should result in a table value,
+from where the field indexed by the expression
+value gets the assigned value.
+<P>
+The syntax <CODE>var.NAME</CODE> is just syntactic sugar for
+<CODE>var["NAME"]</CODE>:
+<PRE>
+var ::= simpleexp '<B>.</B>' name
+</PRE>
<P>
The meaning of assignments and evaluations of global variables and
indexed variables can be changed by tag methods (see Section&nbsp;<A HREF="#tag-method">4.8</A>).
@@ -352,15 +402,9 @@ an assignment <CODE>x = val</CODE>, where <CODE>x</CODE> is a global variable,
is equivalent to a call <CODE>setglobal('x', val)</CODE>;
an assignment <CODE>t[i] = val</CODE> is equivalent to
<CODE>settable_event(t, i, val)</CODE>.
-See Section&nbsp;<A HREF="#tag-method">4.8</A> for a description of these functions
-\footnote{Function <CODE>setglobal</CODE> is pre-defined in Lua.
-Function <TT>settable_event</TT> is used only for explanation purposes.}.
-<P>
-The syntax <CODE>var.NAME</CODE> is just syntactic sugar for
-<CODE>var["NAME"]</CODE>:
-<LISTING>
-var ::= var '<B>.</B>' name
-</LISTING>
+See Section&nbsp;<A HREF="#tag-method">4.8</A> for a complete description of these functions.
+(Function <CODE>setglobal</CODE> is pre-defined in Lua.
+Function <TT>settable_event</TT> is used only for explanatory purposes.)
<P>
<H3>4.5.3 - Control Structures</H3>
The <A NAME="condition expression">condition expression</A> of a control structure may return any value.
@@ -369,28 +413,28 @@ only <B>nil</B> is considered false.
<TT>if</TT>'s, <TT>while</TT>'s and <TT>repeat</TT>'s have the usual meaning.
<P>
<A NAME="while-do"></A><A NAME="repeat-until"></A><A NAME="if-then-else"></A>
-<LISTING>
+<PRE>
stat ::= <B>while</B> exp1 <B>do</B> block <B>end</B> <BR> | <B>repeat</B> block <B>until</B> exp1 <BR> | <B>if</B> exp1 <B>then</B> block {elseif} [<B>else</B> block] <B>end</B>
elseif ::= <B>elseif</B> exp1 <B>then</B> block
-</LISTING>
+</PRE>
<P>
-A <TT>return</TT> is used to return values from a function or a chunk.
+A <TT>return</TT> is used to return values from a function or from a chunk.
<A NAME="return"></A>
Because they may return more than one value,
the syntax for a <A NAME="return statement">return statement</A> is:
-<LISTING>
+<PRE>
ret ::= <B>return</B> [explist1] [sc]
-</LISTING>
+</PRE>
<P>
<A NAME="funcstat"></A>
<H3>4.5.4 - Function Calls as Statements</H3>
Because of possible side-effects,
function calls can be executed as statements:
-<LISTING>
+<PRE>
stat ::= functioncall
-</LISTING>
-In this case, returned values are thrown away.
+</PRE>
+In this case, all returned values are thrown away.
Function calls are explained in Section&nbsp;<A HREF="#functioncall">4.6.8</A>.
<P>
<A NAME="localvar"></A>
@@ -399,11 +443,11 @@ Function calls are explained in Section&nbsp;<A HREF="#functioncall">4.6.8</A>.
Their scope begins after the declaration and lasts until the
end of the block.
The declaration may include an initial assignment:
-<LISTING>
+<PRE>
stat ::= <B>local</B> declist [init]
declist ::= name {'<B>,</B>' name}
init ::= '<B>=</B>' explist1
-</LISTING>
+</PRE>
If present, an initial assignment has the same semantics
of a multiple assignment.
Otherwise, all variables are initialized with <B>nil</B>.
@@ -412,32 +456,42 @@ Otherwise, all variables are initialized with <B>nil</B>.
<A NAME="4.6"></A>
<H2>4.6 - Expressions</H2>
<P>
-<H3>4.6.1 - <A NAME="Simple Expressions</H3>">Simple Expressions</H3></A>
-Simple expressions are:
-<LISTING>
+<H3>4.6.1 - <A NAME="Basic Expressions</H3>">Basic Expressions</H3></A>
+Basic expressions are:
+<PRE>
exp ::= '<B>(</B>' exp '<B>)</B>'
exp ::= <B>nil</B>
exp ::= '<B>number</B>'
exp ::= '<B>literal</B>'
-exp ::= var
-</LISTING>
+exp ::= function
+exp ::= simpleexp
+</PRE>
+<PRE>
+simpleexp ::= var
+simpleexp ::= upvalue
+simpleexp ::= functioncall
+</PRE>
+<P>
Numbers (numerical constants) and
-string literals are explained in Section&nbsp;<A HREF="#lexical">4.1</A>.
-Variables are explained in Section&nbsp;<A HREF="#assignment">4.5.2</A>.
+string literals are explained in Section&nbsp;<A HREF="#lexical">4.1</A>;
+variables are explained in Section&nbsp;<A HREF="#assignment">4.5.2</A>;
+upvalues are explained in Section&nbsp;<A HREF="#upvalue">4.7</A>;
+function definitions (<EM>function</EM>) are explained in Section&nbsp;<A HREF="#func-def">4.6.9</A>;
+function call are explained in Section&nbsp;<A HREF="#functioncall">4.6.8</A>.
<P>
An access to a global variable <CODE>x</CODE> is equivalent to a
call <CODE>getglobal('x')</CODE>;
an access to an indexed variable <CODE>t[i]</CODE> is equivalent to
a call <CODE>gettable_event(t, i)</CODE>.
-See Section&nbsp;<A HREF="#tag-method">4.8</A> for a description of these functions
-\footnote{Function <CODE>getglobal</CODE> is pre-defined in Lua.
-Function <TT>gettable_event</TT> is used only for explanation purposes.}.
+See Section&nbsp;<A HREF="#tag-method">4.8</A> for a description of these functions.
+(Function <CODE>getglobal</CODE> is pre-defined in Lua.
+Function <TT>gettable_event</TT> is used only for explanatory purposes.)
<P>
The non-terminal <EM>exp1</EM> is used to indicate that the values
returned by an expression must be adjusted to one single value:
-<LISTING>
+<PRE>
exp1 ::= exp
-</LISTING>
+</PRE>
<P>
<H3>4.6.2 - Arithmetic Operators</H3>
Lua supports the usual <A NAME="arithmetic operators">arithmetic operators</A>:
@@ -446,7 +500,7 @@ the binary <CODE>+</CODE> (addition),
<CODE>/</CODE> (division) and <CODE>^</CODE> (exponentiation),
and unary <CODE>-</CODE> (negation).
If the operands are numbers, or strings that can be converted to
-numbers, according to the rules given in Section&nbsp;<A HREF="#coercion">4.3</A>,
+numbers (according to the rules given in Section&nbsp;<A HREF="#coercion">4.3</A>),
then all operations except exponentiation have the usual meaning.
Otherwise, an appropriate tag method is called (see Section&nbsp;<A HREF="#tag-method">4.8</A>).
An exponentiation always calls a tag method.
@@ -456,12 +510,12 @@ giving the expected meaning to <A NAME="exponentiation">exponentiation</A>
<P>
<H3>4.6.3 - Relational Operators</H3>
Lua provides the following <A NAME="relational operators">relational operators</A>:
-<LISTING>
+<PRE>
&lt; &gt; &lt;= &gt;= ~= ==
-</LISTING>
+</PRE>
All these return <B>nil</B> as false and a value different from <B>nil</B> as true.
<P>
-Equality first compares the types of its operands.
+Equality first compares the tags of its operands.
If they are different, then the result is <B>nil</B>.
Otherwise, their values are compared.
Numbers and strings are compared in the usual way.
@@ -470,22 +524,24 @@ that is, two tables are considered equal only if they are the same table.
The operator <CODE>~=</CODE> is exactly the negation of equality (<CODE>==</CODE>).
Note that the conversion rules of Section&nbsp;<A HREF="#coercion">4.3</A>
<EM>do not</EM> apply to equality comparisons.
-Thus, <CODE>"0"==0</CODE> evaluates to false.
+Thus, <CODE>"0"==0</CODE> evaluates to false,
+and <CODE>t[0]</CODE> and <CODE>t["0"]</CODE> denote different
+entries in a table.
<P>
The other operators work as follows.
If both arguments are numbers, then they are compared as such.
Otherwise, if both arguments are strings,
-their values are compared using lexicographical order.
+then their values are compared using lexicographical order.
Otherwise, the ``order'' tag method is called (see Section&nbsp;<A HREF="#tag-method">4.8</A>).
<P>
<H3>4.6.4 - Logical Operators</H3>
-Like control structures, all logical operators
-consider <B>nil</B> as false and anything else as true.
The <A NAME="logical operators">logical operators</A> are:
<A NAME="and"></A><A NAME="or"></A><A NAME="not"></A>
-<LISTING>
+<PRE>
and or not
-</LISTING>
+</PRE>
+Like control structures, all logical operators
+consider <B>nil</B> as false and anything else as true.
The operator <CODE>and</CODE> returns <B>nil</B> if its first argument is <B>nil</B>;
otherwise, it returns its second argument.
The operator <CODE>or</CODE> returns its first argument
@@ -495,17 +551,25 @@ Both <CODE>and</CODE> and <CODE>or</CODE> use <A NAME="short-cut evaluation">sho
that is,
the second operand is evaluated only when necessary.
<P>
+A useful Lua idiom is <CODE>x = x or v</CODE>,
+which is equivalent to
+<PRE>
+ if x == nil then x = v end
+</PRE>
+i.e., it sets <CODE>x</CODE> to a default value <CODE>v</CODE> when
+<CODE>x</CODE> is not set.
+<P>
<H3>4.6.5 - Concatenation</H3>
-Lua offers a string <A NAME="concatenation">concatenation</A> operator,
+The string <A NAME="concatenation">concatenation</A> operator in Lua is
denoted by ``<A NAME=".."><TT>..</TT></A>''.
-If operands are strings or numbers, then they are converted to
+If both operands are strings or numbers, they are converted to
strings according to the rules in Section&nbsp;<A HREF="#coercion">4.3</A>.
Otherwise, the ``concat'' tag method is called (see Section&nbsp;<A HREF="#tag-method">4.8</A>).
<P>
<H3>4.6.6 - Precedence</H3>
<A NAME="Operator precedence">Operator precedence</A> follows the table below,
from the lower to the higher priority:
-<LISTING>
+<PRE>
and or
&lt; &gt; &lt;= &gt;= ~= ==
..
@@ -513,7 +577,7 @@ from the lower to the higher priority:
* /
not - (unary)
^
-</LISTING>
+</PRE>
All binary operators are left associative,
except for <CODE>^</CODE> (exponentiation),
which is right associative.
@@ -526,87 +590,108 @@ Constructors can be used to create empty tables,
or to create a table and initialize some fields.
<P>
The general syntax for constructors is:
-<LISTING>
+<PRE>
tableconstructor ::= '<B>{</B>' fieldlist '<B>}</B>'
-fieldlist ::= lfieldlist | ffieldlist | lfieldlist '<B>;</B>' ffieldlist
+fieldlist ::= lfieldlist | ffieldlist | lfieldlist '<B>;</B>' ffieldlist | ffieldlist '<B>;</B>' lfieldlist
lfieldlist ::= [lfieldlist1]
ffieldlist ::= [ffieldlist1]
-</LISTING>
+</PRE>
<P>
The form <EM>lfieldlist1</EM> is used to initialize lists.
-<LISTING>
+<PRE>
lfieldlist1 ::= exp {'<B>,</B>' exp} ['<B>,</B>']
-</LISTING>
+</PRE>
The expressions in the list are assigned to consecutive numerical indices,
starting with 1.
For example:
-<LISTING>
+<PRE>
a = {"v1", "v2", 34}
-</LISTING>
-is essentially equivalent to:
-<LISTING>
- temp = {}
- temp[1] = "v1"
- temp[2] = "v2"
- temp[3] = 34
- a = temp
-</LISTING>
+</PRE>
+is equivalent to:
+<PRE>
+ do
+ local temp = {}
+ temp[1] = "v1"
+ temp[2] = "v2"
+ temp[3] = 34
+ a = temp
+ end
+</PRE>
<P>
The form <EM>ffieldlist1</EM> initializes other fields in a table:
-<LISTING>
+<PRE>
ffieldlist1 ::= ffield {'<B>,</B>' ffield} ['<B>,</B>']
-ffield ::= '<B>[</B>' exp '<B>]</B>' \ter {= exp | name '<B>=</B>' exp}
-</LISTING>
+ffield ::= '<B>[</B>' exp '<B>]</B>' '<B>=</B>' exp | name '<B>=</B>' exp
+</PRE>
For example:
-<LISTING>
+<PRE>
a = {[f(k)] = g(y), x = 1, y = 3, [0] = b+c}
-</LISTING>
-is essentially equivalent to:
-<LISTING>
- temp = {}
- temp[f(k)] = g(y)
- temp.x = 1 -- or temp["x"] = 1
- temp.y = 3 -- or temp["y"] = 3
- temp[0] = b+c
- a = temp
-</LISTING>
+</PRE>
+is equivalent to:
+<PRE>
+ do
+ local temp = {}
+ temp[f(k)] = g(y)
+ temp.x = 1 -- or temp["x"] = 1
+ temp.y = 3 -- or temp["y"] = 3
+ temp[0] = b+c
+ a = temp
+ end
+</PRE>
An expression like <CODE>{x = 1, y = 4}</CODE> is
in fact syntactic sugar for <CODE>{["x"] = 1, ["y"] = 4}</CODE>.
<P>
+Both forms may have an optional trailing comma,
+and can be used in the same constructor separated by
+a semi-collon.
+For example, all forms below are correct:
+<PRE>
+ x = {;}
+ x = {'a', 'b',}
+ x = {type='list'; 'a', 'b'}
+ x = {f(0), f(1), f(2),; n=3}
+</PRE>
+<P>
<A NAME="functioncall"></A>
<H3>4.6.8 - Function Calls</H3>
A <A NAME="function call">function call</A> has the following syntax:
-<LISTING>
-functioncall ::= var realParams
-</LISTING>
-Here, <EM>var</EM> can be any variable (global, local, indexed, etc).
+<PRE>
+functioncall ::= simpleexp args
+</PRE>
+First, <EM>simpleexp</EM> is evaluated.
If its value has type <EM>function</EM>,
-then this function is called.
+then this function is called,
+with the given arguments.
Otherwise, the ``function'' tag method is called,
-having as first parameter the value of <EM>var</EM>,
+having as first parameter the value of <EM>simpleexp</EM>,
and then the original call parameters.
<P>
The form:
-<LISTING>
-functioncall ::= var '<B>:</B>' name realParams
-</LISTING>
+<PRE>
+functioncall ::= simpleexp '<B>:</B>' name args
+</PRE>
can be used to call ``methods''.
-A call <CODE>var:name(...)</CODE>
+A call <CODE>simpleexp:name(...)</CODE>
is syntactic sugar for
-<LISTING>
- var.name(var, ...)
-</LISTING>
-except that <CODE>var</CODE> is evaluated only once.
-<P>
-<LISTING>
-realParams ::= '<B>(</B>' [explist1] '<B>)</B>'
-realParams ::= tableconstructor
+<PRE>
+ simpleexp.name(simpleexp, ...)
+</PRE>
+except that <CODE>simpleexp</CODE> is evaluated only once.
+<P>
+<PRE>
+args ::= '<B>(</B>' [explist1] '<B>)</B>'
+args ::= tableconstructor
+args ::= '<B>literal</B>'
explist1 ::= exp1 {'<B>,</B>' exp1}
-</LISTING>
+</PRE>
All argument expressions are evaluated before the call.
A call of the form <CODE>f{...}</CODE> is syntactic sugar for
<CODE>f({...})</CODE>, that is,
the parameter list is a single new table.
+A call of the form <CODE>f'...'</CODE>
+(or <CODE>f"..."</CODE> or <CODE>f[[...]]</CODE>) is syntactic sugar for
+<CODE>f('...')</CODE>, that is,
+the parameter list is a single literal string.
<P>
Because a function can return any number of results
(see Section&nbsp;<A HREF="#return">4.5.3</A>),
@@ -621,32 +706,58 @@ thus discarding all returned values but the first one.
If the function is called in a place that can hold many values
(syntactically denoted by the non-terminal <EM>exp</EM>),
then no adjustment is made.
-<P>
+Note that the only place that can hold many values
+is the last expression (or the only one) in an assignment
+or in a return statement; see examples below.
+<PRE>
+ f(); -- adjusted to 0
+ g(x, f()); -- f() is adjusted to 1
+ a,b,c = f(), x; -- f() is adjusted to 1 result (and c gets nil)
+ a,b,c = x, f(); -- f() is adjusted to 2
+ a,b,c = f(); -- f() is adjusted to 3
+ return f(); -- returns all values returned by f()
+</PRE>
<P>
<A NAME="func-def"></A>
-<A NAME="4.7"></A>
-<H2>4.7 - Function Definitions</H2>
+<H3>4.6.9 - <A NAME="Function Definitions</H3>">Function Definitions</H3></A>
<P>
-Functions in Lua can be defined anywhere in the global level of a chunk.
The syntax for function definition is:
-<LISTING>
-function ::= <B>function</B> var '<B>(</B>' [parlist1] '<B>)</B>' block <B>end</B>
-</LISTING>
+<PRE>
+function ::= <B>function</B> '<B>(</B>' [parlist1] '<B>)</B>' block <B>end</B>
+stat ::= <B>function</B> funcname '<B>(</B>' [parlist1] '<B>)</B>' block <B>end</B>
+funcname ::= name | name '<B>.</B>' name
+</PRE>
+The statement:
+<PRE>
+ function f (...)
+ ...
+ end
+</PRE>
+is just syntactic sugar for:
+<PRE>
+ f = function (...)
+ ...
+ end
+</PRE>
<P>
+A function definition is an executable expression,
+whose value has type <EM>function</EM>.
When Lua pre-compiles a chunk,
all its function bodies are pre-compiled, too.
-Then, when Lua ``executes'' the function definition,
-its body is stored, with type <EM>function</EM>,
-into the variable <CODE>var</CODE>.
-It is in this sense that
-a function definition is an assignment to a global variable.
+Then, whenever Lua executes the function definition,
+its upvalues are fixed (see Section&nbsp;<A HREF="#upvalue">4.7</A>),
+and the function is <EM>instantiated</EM> (or ``closed'').
+This function instance (or ``closure'')
+is the final value of the expression.
+Different instances of a same function
+may have different upvalues.
<P>
Parameters act as local variables,
-initialized with the argument values.
-<LISTING>
+initialized with the argument values:
+<PRE>
parlist1 ::= '<B>...</B>'
parlist1 ::= name {'<B>,</B>' name} ['<B>,</B>' '<B>...</B>']
-</LISTING>
+</PRE>
<A NAME="vararg"></A>
When a function is called,
@@ -655,19 +766,19 @@ the length of the list of parameters (see Section&nbsp;<A HREF="#adjust">4.4</A>
unless the function is a <A NAME="vararg"><EM>vararg</EM></A> function,
indicated by the dots (...) at the end of its parameter list.
A vararg function does not adjust its argument list;
-instead, it collects any extra arguments in an implicit parameter,
-called <A NAME="arg"><EM>arg</EM></A>.
+instead, it collects any extra arguments into an implicit parameter,
+called <A NAME="arg"><TT>arg</TT></A>.
This parameter is always initialized as a table,
with a field <CODE>n</CODE> with the number of extra arguments,
and the extra arguments at positions 1, 2, ...
<P>
As an example, suppose definitions like:
-<LISTING>
+<PRE>
function f(a, b) end
function g(a, b, ...) end
-</LISTING>
+</PRE>
Then, we have the following mapping from arguments to parameters:
-<LISTING>
+<PRE>
CALL PARAMETERS
<P>
f(3) a=3, b=nil
@@ -677,56 +788,97 @@ Then, we have the following mapping from arguments to parameters:
g(3) a=3, b=nil, arg={n=0}
g(3, 4) a=3, b=4, arg={n=0}
g(3, 4, 5, 8) a=3, b=4, arg={5, 8; n=2}
-</LISTING>
+</PRE>
<P>
Results are returned using the <CODE>return</CODE> statement (see Section&nbsp;<A HREF="#return">4.5.3</A>).
If control reaches the end of a function without a return instruction,
then the function returns with no results.
<P>
There is a special syntax for defining <A NAME="methods">methods</A>,
-that is, functions that have an extra parameter <A NAME="self"><EM>self</EM></A>.
-<LISTING>
-function ::= <B>function</B> var '<B>:</B>' name '<B>(</B>' [parlist1] '<B>)</B>' block <B>end</B>
-</LISTING>
+that is, functions that have an implicit extra parameter <A NAME="self"><TT>self</TT></A>.
+<PRE>
+function ::= <B>function</B> name '<B>:</B>' name '<B>(</B>' [parlist1] '<B>)</B>' block <B>end</B>
+</PRE>
Thus, a declaration like
-<LISTING>
+<PRE>
function v:f (...)
...
end
-</LISTING>
+</PRE>
is equivalent to
-<LISTING>
-function v.f (self, ...)
+<PRE>
+v.f = function (self, ...)
...
end
-</LISTING>
+</PRE>
that is, the function gets an extra formal parameter called <CODE>self</CODE>.
-Notice that
-the variable <CODE>v</CODE> must have been
+Note that the variable <CODE>v</CODE> must have been
previously initialized with a table value.
<P>
<P>
+<A NAME="upvalue"></A>
+<A NAME="4.7"></A>
+<H2>4.7 - Visibility and Upvalues</H2>
+<A NAME="Visibility"></A> <A NAME="Upvalues"></A>
+<P>
+A function body may refer to its own local variables
+(which includes its parameters) and to global variables,
+as long as they are not shadowed by local
+variables from enclosing functions.
+A function <EM>cannot</EM> access a local
+variable from an enclosing function,
+since such variables may no longer exist when the function is called.
+However, a function may access the <EM>value</EM> of a local variable
+from an enclosing function, using <EM>upvalues</EM>.
+<P>
+<PRE>
+upvalue ::= '<B>%</B>' name
+</PRE>
+An upvalue is somewhat similar to a variable expression,
+but whose value is frozen when the function wherein it
+appears is instantiated.
+The name used in an upvalue may be the name of any variable visible
+at the point where the function is defined.
+<P>
+Here are some examples:
+<PRE>
+a,b,c = 1,2,3 -- global variables
+function f (x)
+ local b -- x and b are local to f
+ local g = function (a)
+ local y -- a and y are local to g
+ p = a -- OK, access local 'a'
+ p = c -- OK, access global 'c'
+ p = b -- ERROR: cannot access a variable in outer scope
+ p = %b -- OK, access frozen value of 'b' (local to 'f')
+ p = %c -- OK, access frozen value of global 'c'
+ p = %y -- ERROR: 'y' is not visible where 'g' is defined
+ end -- g
+end -- f
+</PRE>
+<P>
+<P>
<A NAME="tag-method"></A>
<A NAME="4.8"></A>
<H2>4.8 - Tag Methods</H2>
<P>
Lua provides a powerful mechanism to extend its semantics,
called <A NAME="Tag Methods"><EM>Tag Methods</EM></A>.
-A tag method (TM) is a programmer-defined function
-that can be called at many key points of the evaluation of a program,
-allowing a programmer to change the standard Lua behavior at these points.
+A tag method is a programmer-defined function
+that is called at specific key points during the evaluation of a program,
+allowing the programmer to change the standard Lua behavior at these points.
Each of these points is called an <A NAME="event"><EM>event</EM></A>.
<P>
The tag method called for any specific event is selected
according to the tag of the values involved
in the event (see Section&nbsp;<A HREF="#TypesSec">3</A>).
The function <A NAME="settagmethod"><TT>settagmethod</TT></A> changes the tag method
-associated with a given pair <EM>&lt;tag, event&gt;</EM>.
-Its first parameter is the tag, the second the event name
+associated with a given pair <EM>(tag, event)</EM>.
+Its first parameter is the tag, the second is the event name
(a string, see below),
and the third parameter is the new method (a function),
or <B>nil</B> to restore the default behavior.
-The function returns the previous tag method.
+The function returns the previous tag method for that pair.
Another function, <A NAME="gettagmethod"><TT>gettagmethod</TT></A>,
receives a tag and an event name and returns the
current method associated with the pair.
@@ -739,8 +891,8 @@ The function not only shows when a tag method is called,
but also its arguments, its results and the default behavior.
Please notice that the code shown here is only illustrative;
the real behavior is hard coded in the interpreter,
-and it is much more efficient than this simulation.
-All functions used in these descriptions
+and it is much more efficient than this simulation.
+All functions used in these descriptions
(<CODE>rawgetglobal</CODE>, <CODE>tonumber</CODE>, <CODE>call</CODE>, etc)
are described in Section&nbsp;<A HREF="#predefined">6.1</A>.
<P>
@@ -751,65 +903,65 @@ called when a <CODE>+</CODE> operation is applied to non numerical operands.
<P>
The function <CODE>getbinmethod</CODE> defines how Lua chooses a tag method
for a binary operation.
-First Lua tries the first operand.
+First, Lua tries the first operand.
If its tag does not define a tag method for the operation,
then Lua tries the second operand.
If it also fails, then it gets a tag method from tag&nbsp;0:
-<LISTING>
+<PRE>
function getbinmethod (op1, op2, event)
return gettagmethod(tag(op1), event) or
gettagmethod(tag(op2), event) or
gettagmethod(0, event)
end
-</LISTING>
-<LISTING>
+</PRE>
+<PRE>
function add_event (op1, op2)
local o1, o2 = tonumber(op1), tonumber(op2)
if o1 and o2 then -- both operands are numeric
return o1+o2 -- '+' here is the primitive 'add'
- else -- at least one of the operands is not numeric.
+ else -- at least one of the operands is not numeric
local tm = getbinmethod(op1, op2, "add")
if tm then
-- call the method with both operands and an extra
-- argument with the event name
return tm(op1, op2, "add")
- else -- no tag method available: Default behavior
+ else -- no tag method available: default behavior
error("unexpected type at arithmetic operation")
end
end
end
-</LISTING>
+</PRE>
<P>
<DT><B>``sub'':</B><DD><A NAME="sub event"></A>
called when a <CODE>-</CODE> operation is applied to non numerical operands.
-Behavior similar to <CODE>"add"</CODE> event.
+Behavior similar to the <CODE>"add"</CODE> event.
<P>
<DT><B>``mul'':</B><DD><A NAME="mul event"></A>
called when a <CODE>*</CODE> operation is applied to non numerical operands.
-Behavior similar to <CODE>"add"</CODE> event.
+Behavior similar to the <CODE>"add"</CODE> event.
<P>
<DT><B>``div'':</B><DD><A NAME="div event"></A>
called when a <CODE>/</CODE> operation is applied to non numerical operands.
-Behavior similar to <CODE>"add"</CODE> event.
+Behavior similar to the <CODE>"add"</CODE> event.
<P>
<DT><B>``pow'':</B><DD><A NAME="pow event"></A>
called when a <CODE>^</CODE> operation is applied.
-<LISTING>
+<PRE>
function pow_event (op1, op2)
local tm = getbinmethod(op1, op2, "pow")
if tm then
-- call the method with both operands and an extra
-- argument with the event name
return tm(op1, op2, "pow")
- else -- no tag method available: Default behavior
+ else -- no tag method available: default behavior
error("unexpected type at arithmetic operation")
end
end
-</LISTING>
+</PRE>
<P>
<DT><B>``unm'':</B><DD><A NAME="unm event"></A>
called when an unary <CODE>-</CODE> operation is applied to a non numerical operand.
-<LISTING>
+<PRE>
function unm_event (op)
local o = tonumber(op)
if o then -- operand is numeric
@@ -823,17 +975,17 @@ called when an unary <CODE>-</CODE> operation is applied to a non numerical oper
-- call the method with the operand, nil, and an extra
-- argument with the event name
return tm(op, nil, "unm")
- else -- no tag method available: Default behavior
+ else -- no tag method available: default behavior
error("unexpected type at arithmetic operation")
end
end
end
-</LISTING>
+</PRE>
<P>
<DT><B>``lt'':</B><DD><A NAME="lt event"></A>
called when a <CODE>&lt;</CODE> operation is applied to non numerical
or non string operands.
-<LISTING>
+<PRE>
function lt_event (op1, op2)
if type(op1) == "number" and type(op2) == "number" then
return op1 &lt; op2 -- numeric comparison
@@ -848,26 +1000,26 @@ or non string operands.
end
end
end
-</LISTING>
+</PRE>
<P>
<DT><B>``gt'':</B><DD><A NAME="gt event"></A>
called when a <CODE>&gt;</CODE> operation is applied to non numerical
or non string operands.
-Behavior similar to <CODE>"lt"</CODE> event.
+Behavior similar to the <CODE>"lt"</CODE> event.
<P>
<DT><B>``le'':</B><DD><A NAME="le event"></A>
called when a <CODE>&lt;=</CODE> operation is applied to non numerical
or non string operands.
-Behavior similar to <CODE>"lt"</CODE> event.
+Behavior similar to the <CODE>"lt"</CODE> event.
<P>
<DT><B>``ge'':</B><DD><A NAME="ge event"></A>
called when a <CODE>&gt;=</CODE> operation is applied to non numerical
or non string operands.
-Behavior similar to <CODE>"lt"</CODE> event.
+Behavior similar to the <CODE>"lt"</CODE> event.
<P>
<DT><B>``concat'':</B><DD><A NAME="concatenation event"></A>
called when a concatenation is applied to non string operands.
-<LISTING>
+<PRE>
function concat_event (op1, op2)
if (type(op1) == "string" or type(op1) == "number") and
(type(op2) == "string" or type(op2) == "number") then
@@ -881,7 +1033,7 @@ called when a concatenation is applied to non string operands.
end
end
end
-</LISTING>
+</PRE>
<P>
<DT><B>``index'':</B><DD><A NAME="index event"></A>
called when Lua tries to retrieve the value of an index
@@ -889,10 +1041,10 @@ not present in a table.
See event <CODE>"gettable"</CODE> for its semantics.
<P>
<DT><B>``getglobal'':</B><DD><A NAME="getglobal event"></A>
-called whenever Lua accesses a global variable.
+called whenever Lua needs the value of a global variable.
This method can only be set for <B>nil</B> and for tags
created by <CODE>newtag</CODE>.
-<LISTING>
+<PRE>
function getglobal (varname)
local value = rawgetglobal(varname)
local tm = gettagmethod(tag(value), "getglobal")
@@ -902,14 +1054,14 @@ created by <CODE>newtag</CODE>.
return tm(varname, value)
end
end
-</LISTING>
-Notice: the function <CODE>getglobal</CODE> is pre-defined in Lua (see Section&nbsp;<A HREF="#predefined">6.1</A>).
+</PRE>
+The function <CODE>getglobal</CODE> is pre-defined in Lua (see Section&nbsp;<A HREF="#predefined">6.1</A>).
<P>
<DT><B>``setglobal'':</B><DD><A NAME="setglobal event"></A>
called whenever Lua assigns to a global variable.
-This method cannot be set for numbers, strings, and tables and
+This method cannot be set for numbers, strings, and tables and
userdata with default tags.
-<LISTING>
+<PRE>
function setglobal (varname, newvalue)
local oldvalue = rawgetglobal(varname)
local tm = gettagmethod(tag(oldvalue), "setglobal")
@@ -919,13 +1071,13 @@ userdata with default tags.
return tm(varname, oldvalue, newvalue)
end
end
-</LISTING>
+</PRE>
Notice: the function <CODE>setglobal</CODE> is pre-defined in Lua (see Section&nbsp;<A HREF="#predefined">6.1</A>).
<P>
<DT><B>``gettable'':</B><DD><A NAME="gettable event"></A>
called whenever Lua accesses an indexed variable.
This method cannot be set for tables with default tag.
-<LISTING>
+<PRE>
function gettable_event (table, index)
local tm = gettagmethod(tag(table), "gettable")
if tm then
@@ -935,19 +1087,19 @@ This method cannot be set for tables with default tag.
else
local v = rawgettable(table, index)
tm = gettagmethod(tag(table), "index")
- if (v == nil) and tm then
+ if v == nil and tm then
return tm(table, index)
else
return v
end
end
end
-</LISTING>
+</PRE>
<P>
<DT><B>``settable'':</B><DD><A NAME="settable event"></A>
called when Lua assigns to an indexed variable.
This method cannot be set for tables with default tag.
-<LISTING>
+<PRE>
function settable_event (table, index, value)
local tm = gettagmethod(tag(table), "settable")
if tm then
@@ -958,11 +1110,11 @@ This method cannot be set for tables with default tag.
rawsettable(table, index, value)
end
end
-</LISTING>
+</PRE>
<P>
<DT><B>``function'':</B><DD><A NAME="function event"></A>
called when Lua tries to call a non function value.
-<LISTING>
+<PRE>
function function_event (func, ...)
if type(func) == "function" then
return call(func, arg)
@@ -982,7 +1134,7 @@ called when Lua tries to call a non function value.
end
end
end
-</LISTING>
+</PRE>
<P>
<DT><B>``gc'':</B><DD><A NAME="gc event"></A>
called when Lua is garbage collecting an object.
@@ -990,14 +1142,14 @@ This method cannot be set for strings, numbers, functions,
and userdata with default tag.
For each object to be collected,
Lua does the equivalent of the following function:
-<LISTING>
+<PRE>
function gc_event (obj)
local tm = gettagmethod(tag(obj), "gc")
if tm then
tm(obj)
end
end
-</LISTING>
+</PRE>
Moreover, at the end of a garbage collection cycle,
Lua does the equivalent of the call <CODE>gc_event(nil)</CODE>.
<P>
@@ -1010,16 +1162,18 @@ Lua does the equivalent of the call <CODE>gc_event(nil)</CODE>.
<H2>4.9 - Error Handling</H2>
<P>
Because Lua is an extension language,
-all Lua actions start from C code calling a function from the Lua library.
+all Lua actions start from C code in the host program
+calling a function from the Lua library.
Whenever an error occurs during Lua compilation or execution,
the <A NAME="error method"><EM>error method</EM></A> is called,
and then the corresponding function from the library
-(<CODE>lua_dofile</CODE>, <CODE>lua_dostring</CODE>, or <CODE>lua_callfunction</CODE>)
-is terminated returning an error condition.
+(<CODE>lua_dofile</CODE>, <CODE>lua_dostring</CODE>,
+<CODE>lua_dobuffer</CODE>, or <CODE>lua_callfunction</CODE>)
+is terminated, returning an error condition.
<P>
The only argument to the error method is a string
describing the error.
-The default method prints this message in <CODE>stderr</CODE>.
+The default method prints this message to <CODE>stderr</CODE>.
If needed, it is possible to change the error method with the
function <CODE>seterrormethod</CODE>,
which gets the new error handler as its only parameter
@@ -1027,7 +1181,7 @@ which gets the new error handler as its only parameter
The standard I/O library uses this facility to redefine the error method,
using the debug facilities (see Section&nbsp;<A HREF="#debugI">7</A>),
in order to print some extra information,
-like the call stack.
+such as the call stack.
<P>
To provide more information about errors,
Lua programs should include the compilation pragma <CODE>$debug</CODE>.
@@ -1039,6 +1193,9 @@ lines where the calls (and the error) were made.
<P>
Lua code can explicitly generate an error by calling the built-in
function <CODE>error</CODE> (see Section&nbsp;<A HREF="#pdf-error">6.1</A>).
+Lua code can ``catch'' an error using the built-in function
+<CODE>call</CODE> (see Section&nbsp;<A HREF="#pdf-call">6.1</A>).
+<P>
<P>
<P>
<!-- ====================================================================== -->
@@ -1051,6 +1208,7 @@ the set of C functions available to the host program to communicate
with the Lua library.
The API functions can be classified in the following categories:
<OL>
+<LI>managing states;
<LI>exchanging values between C and Lua;
<LI>executing Lua code;
<LI>manipulating (reading and writing) Lua objects;
@@ -1061,9 +1219,81 @@ The API functions can be classified in the following categories:
All API functions and related types and constants
are declared in the header file <CODE>lua.h</CODE>.
<P>
-<A NAME="valuesCLua"></A>
+<A NAME="mangstate"></A>
<A NAME="5.1"></A>
-<H2>5.1 - Exchanging Values between C and Lua</H2>
+<H2>5.1 - Managing States</H2>
+The whole state of the Lua interpreter
+(global variables, stack, tag methods, etc)
+is stored in a dynamic structure pointed by<A NAME="lua_state"></A>
+<PRE>
+typedef struct lua_State lua_State;
+extern lua_State *lua_state;
+</PRE>
+<P>
+Before calling any API function,
+this state must be initialized.
+This is done by calling<A NAME="lua_open"></A>
+<PRE>
+void lua_open (void);
+</PRE>
+This function allocates and initializes some internal structures,
+and defines all pre-defined functions of Lua.
+If <CODE>lua_state</CODE> is already different from <CODE>NULL</CODE>,
+<CODE>lua_open</CODE> has no effect;
+therefore, it is safe to call this function multiple times.
+All standard libraries call <CODE>lua_open</CODE> when they are opened.
+<P>
+Function <CODE>lua_setstate</CODE> is used to change the current state
+of Lua:<A NAME="lua_setstate"></A>
+<PRE>
+lua_State *lua_setstate (lua_State *st);
+</PRE>
+It sets <CODE>lua_state</CODE> to <CODE>st</CODE> and returns the old state.
+<P>
+Multiple, independent states may be created.
+For that, you must set <CODE>lua_state</CODE> back to <CODE>NULL</CODE> before
+calling <CODE>lua_open</CODE>.
+An easy way to do that is defining an auxiliary function:
+<PRE>
+lua_State *lua_newstate (void) {
+ lua_State *old = lua_setstate(NULL);
+ lua_open();
+ return lua_setstate(old);
+}
+</PRE>
+This function creates a new state without changing the current state
+of the interpreter.
+Note that any new state is built with all predefined functions,
+but any additional library (such as the standard libraries) must be
+explicitly open in the new state, if needed.
+<P>
+If necessary, a state may be released:<A NAME="lua_close"></A>
+<PRE>
+void lua_close (void);
+</PRE>
+This function destroys all objects in the current Lua environment
+(calling the correspondent garbage collector tag methods),
+frees all dynamic memory used by the state,
+and then sets <CODE>lua_state</CODE> to <CODE>NULL</CODE>.
+Usually, there is no need to call this function,
+since these resources are naturally released when the program ends.
+If <CODE>lua_state</CODE> is already <CODE>NULL</CODE>,
+<CODE>lua_close</CODE> has no effect.
+<P>
+If you are using multiple states,
+you may find useful the following function,
+which releases a given state:
+<PRE>
+void lua_freestate (lua_State *st) {
+ lua_State *old = lua_setstate(st);
+ lua_close();
+ if (old != st) lua_setstate(old);
+}
+</PRE>
+<P>
+<A NAME="valuesCLua"></A>
+<A NAME="5.2"></A>
+<H2>5.2 - Exchanging Values between C and Lua</H2>
Because Lua has no static type system,
all values passed between Lua and C have type
<CODE>lua_Object</CODE><A NAME="lua_Object"></A>,
@@ -1077,65 +1307,73 @@ the following functions are available:
<A NAME="lua_isnil"></A><A NAME="lua_isnumber"></A><A NAME="lua_isstring"></A>
<A NAME="lua_istable"></A><A NAME="lua_iscfunction"></A><A NAME="lua_isuserdata"></A>
<A NAME="lua_isfunction"></A>
-<LISTING>
-int lua_isnil (lua_Object object);
-int lua_isnumber (lua_Object object);
-int lua_isstring (lua_Object object);
-int lua_istable (lua_Object object);
-int lua_isfunction (lua_Object object);
-int lua_iscfunction (lua_Object object);
-int lua_isuserdata (lua_Object object);
-</LISTING>
+<PRE>
+int lua_isnil (lua_Object object);
+int lua_isnumber (lua_Object object);
+int lua_isstring (lua_Object object);
+int lua_istable (lua_Object object);
+int lua_isfunction (lua_Object object);
+int lua_iscfunction (lua_Object object);
+int lua_isuserdata (lua_Object object);
+</PRE>
All macros return 1 if the object is compatible with the given type,
and 0 otherwise.
The function <CODE>lua_isnumber</CODE> accepts numbers and numerical strings,
whereas
<CODE>lua_isstring</CODE> accepts strings and numbers (see Section&nbsp;<A HREF="#coercion">4.3</A>),
-and <CODE>lua_isfunction</CODE> accepts Lua and C functions.
+and <CODE>lua_isfunction</CODE> accepts Lua functions and C functions.
<P>
-To check the tag of a <CODE>lua_Object</CODE>,
+To get the tag of a <CODE>lua_Object</CODE>,
the following function is available:
<A NAME="lua_tag"></A>
-<LISTING>
-int lua_tag (lua_Object object);
-</LISTING>
+<PRE>
+int lua_tag (lua_Object object);
+</PRE>
<P>
To translate a value from type <CODE>lua_Object</CODE> to a specific C type,
the programmer can use:
-<A NAME="lua_getnumber"></A><A NAME="lua_getstring"></A>
+<A NAME="lua_getnumber"></A><A NAME="lua_getstring"></A><A NAME="lua_strlen"></A>
<A NAME="lua_getcfunction"></A><A NAME="lua_getuserdata"></A>
-<LISTING>
-float lua_getnumber (lua_Object object);
-char *lua_getstring (lua_Object object);
-lua_CFunction lua_getcfunction (lua_Object object);
-void *lua_getuserdata (lua_Object object);
-</LISTING>
+<PRE>
+double lua_getnumber (lua_Object object);
+char *lua_getstring (lua_Object object);
+long lua_strlen (lua_Object object);
+lua_CFunction lua_getcfunction (lua_Object object);
+void *lua_getuserdata (lua_Object object);
+</PRE>
<P>
<CODE>lua_getnumber</CODE> converts a <CODE>lua_Object</CODE> to a floating-point number.
This <CODE>lua_Object</CODE> must be a number or a string convertible to number
-(see Section&nbsp;<A HREF="#coercion">4.3</A>); otherwise, the function returns&nbsp;0.
+(see Section&nbsp;<A HREF="#coercion">4.3</A>); otherwise, <CODE>lua_getnumber</CODE> returns&nbsp;0.
<P>
<CODE>lua_getstring</CODE> converts a <CODE>lua_Object</CODE> to a string (<CODE>char*</CODE>).
This <CODE>lua_Object</CODE> must be a string or a number;
otherwise, the function returns&nbsp;0 (the <CODE>NULL</CODE> pointer).
This function does not create a new string,
but returns a pointer to a string inside the Lua environment.
+Those strings always have a 0 after their last character (like in C),
+but may contain other zeros in their body.
+If you do not know whether a string may contain zeros,
+you can use <CODE>lua_strlen</CODE> to get the actual length.
Because Lua has garbage collection,
there is no guarantee that such pointer will be valid after the block ends
-(see below).
+(see Section&nbsp;<A HREF="#GC">5.3</A>).
<P>
<CODE>lua_getcfunction</CODE> converts a <CODE>lua_Object</CODE> to a C function.
This <CODE>lua_Object</CODE> must have type <EM>CFunction</EM>;
-otherwise, the function returns 0 (the <CODE>NULL</CODE> pointer).
-The type <CODE>lua_CFunction</CODE> is explained in Section&nbsp;<A HREF="#LuacallC">5.5</A>.
+otherwise, <CODE>lua_getcfunction</CODE> returns 0 (the <CODE>NULL</CODE> pointer).
+The type <CODE>lua_CFunction</CODE> is explained in Section&nbsp;<A HREF="#LuacallC">5.7</A>.
<P>
<CODE>lua_getuserdata</CODE> converts a <CODE>lua_Object</CODE> to <CODE>void*</CODE>.
This <CODE>lua_Object</CODE> must have type <EM>userdata</EM>;
-otherwise, the function returns 0 (the <CODE>NULL</CODE> pointer).
+otherwise, <CODE>lua_getuserdata</CODE> returns 0 (the <CODE>NULL</CODE> pointer).
<P>
+<A NAME="GC"></A>
+<A NAME="5.3"></A>
+<H2>5.3 - Garbage Collection</H2>
Because Lua has automatic memory management and garbage collection,
a <CODE>lua_Object</CODE> has a limited scope,
-and is only valid inside the <EM>block</EM> where it was created.
+and is only valid inside the <EM>block</EM> where it has been created.
A C function called from Lua is a block,
and its parameters are valid only until its end.
It is good programming practice to convert Lua objects to C values
@@ -1144,65 +1382,72 @@ and never to store <CODE>lua_Object</CODE>s in C global variables.
<P>
A garbage collection cycle can be forced by:
<A NAME="lua_collectgarbage"></A>
-<LISTING>
-long lua_collectgarbage (long limit);
-</LISTING>
+<PRE>
+long lua_collectgarbage (long limit);
+</PRE>
This function returns the number of objects collected.
The argument <CODE>limit</CODE> makes the next cycle occur only
-when that number of new objects have been created.
-If <CODE>limit</CODE>=0, then Lua uses an adaptable heuristics to set this limit.
+after that number of new objects have been created.
+If <CODE>limit</CODE>=0, then Lua uses an adaptive heuristics to set this limit.
<P>
<P>
All communication between Lua and C is done through two
abstract data types, called <A NAME="lua2C"><EM>lua2C</EM></A> and <A NAME="C2lua"><EM>C2lua</EM></A>.
The first one, as the name implies, is used to pass values
-from Lua to C: parameters when Lua calls C and results when C calls Lua.
+from Lua to C:
+parameters when Lua calls C and results when C calls Lua.
The structure C2lua is used in the reverse direction:
parameters when C calls Lua and results when Lua calls C.
<P>
The structure lua2C is an abstract array,
which can be indexed with the function:
<A NAME="lua_lua2C"></A>
-<LISTING>
+<PRE>
lua_Object lua_lua2C (int number);
-</LISTING>
+</PRE>
where <CODE>number</CODE> starts with 1.
When called with a number larger than the array size,
this function returns <CODE>LUA_NOOBJECT</CODE><A NAME="LUA_NOOBJECT"></A>.
In this way, it is possible to write C functions that receive
a variable number of parameters,
and to call Lua functions that return a variable number of results.
-Notice that the structure lua2C cannot be directly modified by C code.
+Note that the structure lua2C cannot be directly modified by C code.
<P>
-The second structure, C2lua, is a stack.
+The second structure, C2lua, is an abstract stack.
Pushing elements into this stack
-is done with the following functions:
-<A NAME="lua_pushnumber"></A><A NAME="lua_pushstring"></A>
+is done with the following functions and macros:
+<A NAME="lua_pushnumber"></A><A NAME="lua_pushlstring"></A><A NAME="lua_pushstring"></A>
<A NAME="lua_pushcfunction"></A><A NAME="lua_pushusertag"></A>
<A NAME="lua_pushnil"></A><A NAME="lua_pushobject"></A>
<A NAME="pushing"></A>
<A NAME="lua_pushuserdata"></A>
-<LISTING>
-void lua_pushnumber (double n);
-void lua_pushstring (char *s);
-void lua_pushcfunction (lua_CFunction f);
-void lua_pushusertag (void *u, int tag);
-void lua_pushnil (void);
-void lua_pushobject (lua_Object object);
-</LISTING>
+<PRE>
+void lua_pushnumber (double n);
+void lua_pushlstring (char *s, long len);
+void lua_pushstring (char *s);
+void lua_pushusertag (void *u, int tag);
+void lua_pushnil (void);
+void lua_pushobject (lua_Object object);
+void lua_pushcfunction (lua_CFunction f); /* macro */
+</PRE>
All of them receive a C value,
convert it to a corresponding <CODE>lua_Object</CODE>,
and leave the result on the top of C2lua.
+Particularly, functions <CODE>lua_pushlstring</CODE> and <CODE>lua_pushstring</CODE>
+make an internal copy of the given string.
+Function <CODE>lua_pushstring</CODE> can only be used to push proper C strings
+(that is, strings that do not contain zeros and end with a zero);
+otherwise you should use the more generic <CODE>lua_pushlstring</CODE>.
The function
<A NAME="lua_pop"></A>
-<LISTING>
-lua_Object lua_pop (void);
-</LISTING>
+<PRE>
+lua_Object lua_pop (void);
+</PRE>
returns a reference to the object at the top of the C2lua stack,
and pops it.
<P>
As a general rule, all API functions pop from the stack
-all elements that they use.
+all elements they use.
<P>
Because userdata are objects,
the function <CODE>lua_pushusertag</CODE> may create a new userdata.
@@ -1210,26 +1455,26 @@ If Lua has a userdata with the given value (<CODE>void*</CODE>) and tag,
that userdata is pushed.
Otherwise, a new userdata is created, with the given value and tag.
If this function is called with
-<CODE>tag</CODE>=<CODE>LUA_ANYTAG</CODE><A NAME="LUA_ANYTAG"></A>,
+<CODE>tag</CODE> equal to <CODE>LUA_ANYTAG</CODE><A NAME="LUA_ANYTAG"></A>,
then Lua will try to find any userdata with the given value,
-no matter its tag.
+regardless of its tag.
If there is no userdata with that value, then a new one is created,
-with tag=0.
+with tag equal to 0.
<P>
Userdata can have different tags,
whose semantics are only known to the host program.
Tags are created with the function:
<A NAME="lua_newtag"></A>
-<LISTING>
-int lua_newtag (void);
-</LISTING>
+<PRE>
+int lua_newtag (void);
+</PRE>
The function <CODE>lua_settag</CODE> changes the tag of
the object on the top of C2lua (and pops it);
the object must be a userdata or a table.
<A NAME="lua_settag"></A>
-<LISTING>
-void lua_settag (int tag);
-</LISTING>
+<PRE>
+void lua_settag (int tag);
+</PRE>
<CODE>tag</CODE> must be a value created with <CODE>lua_newtag</CODE>.
<P>
When C code calls Lua repeatedly, as in a loop,
@@ -1237,209 +1482,231 @@ objects returned by these calls can accumulate,
and may cause a stack overflow.
To avoid this,
nested blocks can be defined with the functions:
-<LISTING>
-void lua_beginblock (void);
-void lua_endblock (void);
-</LISTING>
+<PRE>
+void lua_beginblock (void);
+void lua_endblock (void);
+</PRE>
After the end of the block,
all <CODE>lua_Object</CODE>'s created inside it are released.
-The use of explicit nested blocks is strongly encouraged.
+The use of explicit nested blocks is good programming practice
+and is strongly encouraged.
<P>
-<A NAME="5.2"></A>
-<H2>5.2 - Executing Lua Code</H2>
+<A NAME="5.4"></A>
+<H2>5.4 - Executing Lua Code</H2>
A host program can execute Lua chunks written in a file or in a string
using the following functions:
-<A NAME="lua_dofile"></A><A NAME="lua_dostring"></A>
-<LISTING>
+<A NAME="lua_dofile"></A><A NAME="lua_dostring"></A><A NAME="lua_dobuffer"></A>
+<PRE>
int lua_dofile (char *filename);
int lua_dostring (char *string);
-</LISTING>
-Both functions return an error code:
+int lua_dobuffer (char *buff, int size, char *name);
+</PRE>
+All these functions return an error code:
0, in case of success; non zero, in case of errors.
More specifically, <CODE>lua_dofile</CODE> returns 2 if for any reason
it could not open the file.
-The function <CODE>lua_dofile</CODE>, if called with argument <CODE>NULL</CODE>,
-executes the <CODE>stdin</CODE> stream.
-Function <CODE>lua_dofile</CODE> is also able to execute pre-compiled chunks.
-It automatically detects whether the file is text or binary,
-and loads it accordingly (see program <A NAME="luac"><TT>luac</TT></A>).
+When called with argument <CODE>NULL</CODE>,
+<CODE>lua_dofile</CODE> executes the <CODE>stdin</CODE> stream.
+Functions <CODE>lua_dofile</CODE> and <CODE>lua_dobuffer</CODE>
+are both able to execute pre-compiled chunks.
+They automatically detect whether the chunk is text or binary,
+and load it accordingly (see program <A NAME="luac"><TT><A HREF="luac.html">luac</A></TT></A>).
+Function <CODE>lua_dostring</CODE> executes only source code.
+<P>
+The third parameter to <CODE>lua_dobuffer</CODE> (<CODE>name</CODE>)
+is the ``name of the chunk'',
+used in error messages and debug information.
+If <CODE>name</CODE> is <CODE>NULL</CODE>,
+Lua gives a default name to the chunk.
+In files this name is the file name,
+and <CODE>lua_dostring</CODE> uses a small prefix
+of the string as the chunk name.
<P>
These functions return, in structure lua2C,
any values eventually returned by the chunks.
They also empty the stack C2lua.
<P>
<P>
-<A NAME="5.3"></A>
-<H2>5.3 - Manipulating Lua Objects</H2>
+<A NAME="5.5"></A>
+<H2>5.5 - Manipulating Lua Objects</H2>
To read the value of any global Lua variable,
one uses the function:
<A NAME="lua_getglobal"></A>
-<LISTING>
-lua_Object lua_getglobal (char *varname);
-</LISTING>
+<PRE>
+lua_Object lua_getglobal (char *varname);
+</PRE>
As in Lua, this function may trigger a tag method.
To read the real value of any global variable,
without invoking any tag method,
-this function has a <EM>raw</EM> version:
+use the <EM>raw</EM> version:
<A NAME="lua_rawgetglobal"></A>
-<LISTING>
-lua_Object lua_rawgetglobal (char *varname);
-</LISTING>
+<PRE>
+lua_Object lua_rawgetglobal (char *varname);
+</PRE>
<P>
To store a value previously pushed onto C2lua in a global variable,
there is the function:
<A NAME="lua_setglobal"></A>
-<LISTING>
-void lua_setglobal (char *varname);
-</LISTING>
+<PRE>
+void lua_setglobal (char *varname);
+</PRE>
As in Lua, this function may trigger a tag method.
To set the real value of any global variable,
without invoking any tag method,
-this function has a <EM>raw</EM> version:
+use the <EM>raw</EM> version:
<A NAME="lua_rawgetglobal"></A>
-<LISTING>
-void lua_rawsetglobal (char *varname);
-</LISTING>
+<PRE>
+void lua_rawsetglobal (char *varname);
+</PRE>
<P>
Tables can also be manipulated via the API.
The function
<A NAME="lua_gettable"></A>
-<LISTING>
-lua_Object lua_gettable (void);
-</LISTING>
+<PRE>
+lua_Object lua_gettable (void);
+</PRE>
pops from the stack C2lua a table and an index,
and returns the contents of the table at that index.
As in Lua, this operation may trigger a tag method.
To get the real value of any table index,
without invoking any tag method,
-this function has a <EM>raw</EM> version:
+use the <EM>raw</EM> version:
<A NAME="lua_rawgetglobal"></A>
-<LISTING>
-lua_Object lua_rawgettable (void);
-</LISTING>
+<PRE>
+lua_Object lua_rawgettable (void);
+</PRE>
<P>
To store a value in an index,
the program must push the table, the index,
and the value onto C2lua,
and then call the function:
<A NAME="lua_settable"></A>
-<LISTING>
-void lua_settable (void);
-</LISTING>
+<PRE>
+void lua_settable (void);
+</PRE>
Again, the tag method for ``settable'' may be called.
To set the real value of any table index,
without invoking any tag method,
-this function has a <EM>raw</EM> version:
+use the <EM>raw</EM> version:
<A NAME="lua_rawsettable"></A>
-<LISTING>
-void lua_rawsettable (void);
-</LISTING>
+<PRE>
+void lua_rawsettable (void);
+</PRE>
<P>
Finally, the function
<A NAME="lua_createtable"></A>
-<LISTING>
-lua_Object lua_createtable (void);
-</LISTING>
+<PRE>
+lua_Object lua_createtable (void);
+</PRE>
creates and returns a new, empty table.
<P>
<P>
-<A NAME="5.4"></A>
-<H2>5.4 - Calling Lua Functions</H2>
-Functions defined in Lua by a chunk executed with
-<CODE>dofile</CODE> or <CODE>dostring</CODE> can be called from the host program.
+<A NAME="5.6"></A>
+<H2>5.6 - Calling Lua Functions</H2>
+Functions defined in Lua by a chunk
+can be called from the host program.
This is done using the following protocol:
first, the arguments to the function are pushed onto C2lua
-(see Section&nbsp;<A HREF="#pushing">5.1</A>), in direct order, i.e., the first argument is pushed first.
-<P>
+(see Section&nbsp;<A HREF="#pushing">5.3</A>), in direct order, i.e., the first argument is pushed first.
Then, the function is called using
<A NAME="lua_callfunction"></A>
-<LISTING>
-int lua_callfunction (lua_Object function);
-</LISTING>
+<PRE>
+int lua_callfunction (lua_Object function);
+</PRE>
This function returns an error code:
0, in case of success; non zero, in case of errors.
Finally, the results (a Lua function may return many values)
are returned in structure lua2C,
and can be retrieved with the macro <CODE>lua_getresult</CODE>,
<A NAME="lua_getresult"></A>
-which is just another name to the function <CODE>lua_lua2C</CODE>.
-Notice that the function <CODE>lua_callfunction</CODE>
+which is just another name to function <CODE>lua_lua2C</CODE>.
+Note that function <CODE>lua_callfunction</CODE>
pops all elements from the C2lua stack.
<P>
The following example shows how a C program may do the
equivalent to the Lua code:
-<LISTING>
- a = f("how", t.x, 4)
-</LISTING>
-<LISTING>
+<PRE>
+ a,b = f("how", t.x, 4)
+</PRE>
+<PRE>
lua_pushstring("how"); /* 1st argument */
lua_pushobject(lua_getglobal("t")); /* push value of global 't' */
lua_pushstring("x"); /* push the string 'x' */
- lua_pushobject(lua_gettable()); /* push result of t.x (= t['x']) */
- lua_pushnumber(4); /* 3th argument */
+ lua_pushobject(lua_gettable()); /* push result of t.x (2nd arg) */
+ lua_pushnumber(4); /* 3rd argument */
lua_callfunction(lua_getglobal("f")); /* call Lua function */
lua_pushobject(lua_getresult(1)); /* push first result of the call */
lua_setglobal("a"); /* sets global variable 'a' */
-</LISTING>
+ lua_pushobject(lua_getresult(2)); /* push second result of the call */
+ lua_setglobal("b"); /* sets global variable 'b' */
+</PRE>
<P>
Some special Lua functions have exclusive interfaces.
A C function can generate a Lua error calling the function
<A NAME="lua_error"></A>
-<LISTING>
+<PRE>
void lua_error (char *message);
-</LISTING>
+</PRE>
This function never returns.
If the C function has been called from Lua,
then the corresponding Lua execution terminates,
as if an error had occurred inside Lua code.
Otherwise, the whole program terminates with a call to <CODE>exit(1)</CODE>.
-<P>
-The error handler method (see Section&nbsp;<A HREF="#error">4.9</A>) can be changed with:
-<A NAME="lua_seterrormethod"></A>
-<LISTING>
-lua_Object lua_seterrormethod (void);
-</LISTING>
+The <CODE>message</CODE> is passed to the error handler method.
+If <CODE>message</CODE> is <CODE>NULL</CODE>,
+the error handler method is not called.
+<P>
+The error handler method (see Section&nbsp;<A HREF="#error">4.9</A>) can be
+changed with: <A NAME="lua_seterrormethod"></A>
+<PRE>
+lua_Object lua_seterrormethod (void);
+</PRE>
This function sets the object at the top of C2lua
as the new error method,
and returns the old error method value.
<P>
-Tag methods can be changed with:
-<A NAME="lua_settagmethod"></A>
-<LISTING>
-lua_Object lua_settagmethod (int tag, char *event);
-</LISTING>
+Tag methods can be changed with: <A NAME="lua_settagmethod"></A>
+<PRE>
+lua_Object lua_settagmethod (int tag, char *event);
+</PRE>
The first parameter is the tag,
-the second is the event name (see Section&nbsp;<A HREF="#tag-method">4.8</A>);
+and the second is the event name (see Section&nbsp;<A HREF="#tag-method">4.8</A>);
the new method is pushed from C2lua.
This function returns a <CODE>lua_Object</CODE>,
which is the old tag method value.
To get just the current value of a tag method,
-there is the function
-<A NAME="lua_gettagmethod"></A>
-<LISTING>
-lua_Object lua_gettagmethod (int tag, char *event);
-</LISTING>
+use the function <A NAME="lua_gettagmethod"></A>
+<PRE>
+lua_Object lua_gettagmethod (int tag, char *event);
+</PRE>
+<P>
+It is also possible to copy all tag methods from one tag
+to another: <A NAME="lua_copytagmethods"></A>
+<PRE>
+int lua_copytagmethods (int tagto, int tagfrom);
+</PRE>
+This function returns <CODE>tagto</CODE>.
<P>
<P>
<A NAME="LuacallC"></A>
-<A NAME="5.5"></A>
-<H2>5.5 - C Functions</H2>
+<A NAME="5.7"></A>
+<H2>5.7 - C Functions</H2>
To register a C function to Lua,
there is the following macro:
<A NAME="lua_register"></A>
-<LISTING>
+<PRE>
#define lua_register(n,f) (lua_pushcfunction(f), lua_setglobal(n))
/* char *n; */
/* lua_CFunction f; */
-</LISTING>
+</PRE>
which receives the name the function will have in Lua,
and a pointer to the function.
This pointer must have type <CODE>lua_CFunction</CODE>,
which is defined as
<A NAME="lua_CFunction"></A>
-<LISTING>
+<PRE>
typedef void (*lua_CFunction) (void);
-</LISTING>
+</PRE>
that is, a pointer to a function with no parameters and no results.
<P>
In order to communicate properly with Lua,
@@ -1448,36 +1715,56 @@ which defines the way parameters and results are passed.
<P>
A C function receives its arguments in structure lua2C;
to access them, it uses the macro <CODE>lua_getparam</CODE>, <A NAME="lua_getparam"></A>
-again just another name to <CODE>lua_lua2C</CODE>.
+again just another name for <CODE>lua_lua2C</CODE>.
To return values, a C function just pushes them onto the stack C2lua,
-in direct order (see Section&nbsp;<A HREF="#valuesCLua">5.1</A>).
+in direct order (see Section&nbsp;<A HREF="#valuesCLua">5.2</A>).
Like a Lua function, a C function called by Lua can also return
many results.
<P>
-For some examples, see files <CODE>strlib.c</CODE>,
-<CODE>iolib.c</CODE> and <CODE>mathlib.c</CODE> in Lua distribution.
-<P>
-<A NAME="5.6"></A>
-<H2>5.6 - References to Lua Objects</H2>
-<P>
-As noted in Section&nbsp;<A HREF="#LuacallC">5.5</A>, <CODE>lua_Object</CODE>s are volatile.
+When a C function is created,
+it is possible to associate some <EM>upvalues</EM> to it;
+then these values are passed to the function whenever it is called,
+as common arguments.
+To associate upvalues to a function,
+first these values must be pushed on C2lua.
+Then the function:
+<A NAME="lua_pushcclosure"></A>
+<PRE>
+void lua_pushcclosure (lua_CFunction fn, int n);
+</PRE>
+is used to put the C function on C2lua,
+with the argument <CODE>n</CODE> telling how many upvalues must be
+associated with the function;
+in fact, the macro <CODE>lua_pushcfunction</CODE> is defined as
+<CODE>lua_pushcclosure</CODE> with <CODE>n</CODE> set to 0.
+Then, any time the function is called,
+these upvalues are inserted as the first arguments to the function,
+before the actual arguments provided in the call.
+<P>
+For some examples of C functions, see files <CODE>lstrlib.c</CODE>,
+<CODE>liolib.c</CODE> and <CODE>lmathlib.c</CODE> in the official Lua distribution.
+<P>
+<A NAME="5.8"></A>
+<H2>5.8 - References to Lua Objects</H2>
+<P>
+As noted in Section&nbsp;<A HREF="#GC">5.3</A>, <CODE>lua_Object</CODE>s are volatile.
If the C code needs to keep a <CODE>lua_Object</CODE>
outside block boundaries,
then it must create a <A NAME="reference"><EM>reference</EM></A> to the object.
The routines to manipulate references are the following:
<A NAME="lua_ref"></A><A NAME="lua_getref"></A>
<A NAME="lua_unref"></A>
-<LISTING>
-int lua_ref (int lock);
-lua_Object lua_getref (int ref);
-void lua_unref (int ref);
-</LISTING>
+<PRE>
+int lua_ref (int lock);
+lua_Object lua_getref (int ref);
+void lua_unref (int ref);
+</PRE>
The function <CODE>lua_ref</CODE> creates a reference
to the object that is on the top of the stack,
and returns this reference.
If <CODE>lock</CODE> is true, the object is <EM>locked</EM>:
this means the object will not be garbage collected.
-Notice that an unlocked reference may be garbage collected.
+Note that an unlocked reference may be garbage collected.
Whenever the referenced object is needed,
a call to <CODE>lua_getref</CODE>
returns a handle to it;
@@ -1485,7 +1772,7 @@ if the object has been collected,
<CODE>lua_getref</CODE> returns <CODE>LUA_NOOBJECT</CODE>.
<P>
When a reference is no longer needed,
-it can be freed with a call to <CODE>lua_unref</CODE>.
+it can be released with a call to <CODE>lua_unref</CODE>.
<P>
<P>
<P>
@@ -1511,59 +1798,77 @@ Currently there are three standard libraries:
<LI>mathematical functions (sin, log, etc);
<LI>input and output (plus some system facilities).
</UL>
-In order to have access to these libraries,
-the host program must call the functions
-<CODE>strlib_open</CODE>, <CODE>mathlib_open</CODE>, and <CODE>iolib_open</CODE>,
-declared in <CODE>lualib.h</CODE>.
+To have access to these libraries,
+the C host program must call the functions
+<CODE>lua_strlibopen</CODE>, <CODE>lua_mathlibopen</CODE>,
+and <CODE>lua_iolibopen</CODE>, declared in <CODE>lualib.h</CODE>.
+<A NAME="lua_strlibopen"></A><A NAME="lua_mathlibopen"></A><A NAME="lua_iolibopen"></A>
<P>
<P>
<A NAME="predefined"></A>
<A NAME="6.1"></A>
<H2>6.1 - Predefined Functions</H2>
<P>
-<h3> <TT>call (func, arg, [retmode])</TT></h3><A NAME="call"></A>
+<h3> <TT>call (func, arg [, mode [, errmethod]])</TT></h3><A NAME="call"></A>
+<A NAME="pdf-call"></A>
+
This function calls function <CODE>func</CODE> with
the arguments given by the table <CODE>arg</CODE>.
The call is equivalent to
-<LISTING>
+<PRE>
func(arg[1], arg[2], ..., arg[arg.n])
-</LISTING>
+</PRE>
If <CODE>arg.n</CODE> is not defined,
-then Lua stops getting arguments at the first nil value.
+then Lua stops getting arguments at the first <B>nil</B> value.
<P>
-If <CODE>retmode</CODE> is absent,
+By default,
all results from <CODE>func</CODE> are just returned by the call.
-If <CODE>retmode</CODE> is equal to <CODE>"pack"</CODE>,
+If the string <CODE>mode</CODE> contains <CODE>"p"</CODE>,
the results are <EM>packed</EM> in a single table.<A NAME="packed results"></A>
That is, <CODE>call</CODE> returns just one table;
at index <CODE>n</CODE>, the table has the total number of results
from the call;
the first result is at index 1, etc.
For instance, the following calls produce the following results:
-<LISTING>
-a = call(sin, {5}) --&gt; a = 0.0871557 = sin(5)
-a = call(max, {1,4,5; n=2}) --&gt; a = 4 (only 1 and 4 are arguments)
+<PRE>
+a = call(sin, {5}) --&gt; a = 0.0871557 = sin(5)
+a = call(max, {1,4,5; n=2}) --&gt; a = 4 (only 1 and 4 are arguments)
t = {x=1}
-a = call(next, {t,nil;n=2}, "pack") --&gt; a={"x", 1; n=2}
-</LISTING>
+a = call(next, {t,nil;n=2}, "p") --&gt; a={"x", 1; n=2}
+</PRE>
+<P>
+By default,
+if an error occurs during the function call,
+the error is propagated.
+If the string <CODE>mode</CODE> contains <CODE>"x"</CODE>,
+then the call is <EM>protected</EM>.<A NAME="protected calls"></A>
+In this mode, function <CODE>call</CODE> does not generate an error,
+whatever happens during the call.
+Instead, it returns <B>nil</B> to signal the error
+(besides calling the appropriated error method).
+<P>
+If provided, <CODE>errmethod</CODE> is temporarily set as the error method,
+while <CODE>func</CODE> runs.
+As a particular case, if <CODE>errmethod</CODE> is <B>nil</B>,
+no error messages will be issued during the execution of the called function.
<P>
<h3> <TT>collectgarbage ([limit])</TT></h3><A NAME="collectgarbage"></A>
Forces a garbage collection cycle.
Returns the number of objects collected.
An optional argument, <CODE>limit</CODE>, is a number that
-makes the next cycle occur when that number of new
+makes the next cycle occur only after that number of new
objects have been created.
-If absent, Lua uses an adaptable algorithm to set
+If absent, Lua uses an adaptive algorithm to set
this limit.
<CODE>collectgarbage</CODE> is equivalent to
the API function <CODE>lua_collectgarbage</CODE>.
<P>
<h3> <TT>dofile (filename)</TT></h3><A NAME="dofile"></A>
This function receives a file name,
-opens it, and executes its contents as a Lua chunk,
+opens the file, and executes the file contents as a Lua chunk,
or as pre-compiled chunks.
When called without arguments,
-it executes the contents of the standard input (<CODE>stdin</CODE>).
+<CODE>dofile</CODE> executes the contents of the standard input (<CODE>stdin</CODE>).
If there is any error executing the file,
then <CODE>dofile</CODE> returns <B>nil</B>.
Otherwise, it returns the values returned by the chunk,
@@ -1571,15 +1876,16 @@ or a non <B>nil</B> value if the chunk returns no values.
It issues an error when called with a non string argument.
<CODE>dofile</CODE> is equivalent to the API function <CODE>lua_dofile</CODE>.
<P>
-<h3> <TT>dostring (string [, errmethod])</TT></h3><A NAME="dostring"></A>
+<h3> <TT>dostring (string [, chunkname])</TT></h3><A NAME="dostring"></A>
This function executes a given string as a Lua chunk.
-If there is any error executing the string, it returns <B>nil</B>.
+If there is any error executing the string,
+<CODE>dostring</CODE> returns <B>nil</B>.
Otherwise, it returns the values returned by the chunk,
or a non <B>nil</B> value if the chunk returns no values.
-If provided, <CODE>errmethod</CODE> is temporarily set as the error method,
-while <CODE>string</CODE> runs.
-As a particular case, if <CODE>errmethod</CODE> is <B>nil</B>,
-no error messages will be issued during the execution of the string.
+An optional second parameter (<CODE>chunkname</CODE>)
+is the ``name of the chunk'',
+used in error messages and debug information.
+<CODE>dostring</CODE> is equivalent to the API function <CODE>lua_dostring</CODE>.
<P>
<A NAME="pdf-newtag"></A>
<h3> <TT>newtag ()</TT></h3><A NAME="newtag"></A>
@@ -1595,17 +1901,17 @@ value associated with the index.
When called with <B>nil</B> as its second argument,
the function returns the first index
of the table (and its associated value).
-When called with the last index, or with <B>nil</B> in an empty table,
+When called with the last index,
+or with <B>nil</B> in an empty table,
it returns <B>nil</B>.
<P>
-In Lua there is no declaration of fields;
+Lua has no declaration of fields;
semantically, there is no difference between a
field not present in a table or a field with value <B>nil</B>.
Therefore, the function only considers fields with non <B>nil</B> values.
The order in which the indices are enumerated is not specified,
-<EM>not even for numeric indices</EM>
-(to traverse a table in numeric order,
-use a counter).
+<EM>even for numeric indices</EM>
+(to traverse a table in numeric order, use a counter).
If the table is modified in any way during a traversal,
the semantics of <CODE>next</CODE> is undefined.
<P>
@@ -1613,7 +1919,7 @@ This function cannot be written with the standard API.
<P>
<h3> <TT>nextvar (name)</TT></h3><A NAME="nextvar"></A>
This function is similar to the function <CODE>next</CODE>,
-but iterates over the global variables.
+but iterates instead over the global variables.
Its single argument is the name of a global variable,
or <B>nil</B> to get a first name.
Similarly to <CODE>next</CODE>, it returns the name of another variable
@@ -1624,26 +1930,76 @@ otherwise the semantics of <CODE>nextvar</CODE> is undefined.
<P>
This function cannot be written with the standard API.
<P>
+<h3> <TT>foreach (table, function)</TT></h3><A NAME="foreach"></A>
+Executes the given <CODE>function</CODE> over all elements of <CODE>table</CODE>.
+For each element, the function is called with the index and
+respective value as arguments.
+If the function returns any non-<B>nil</B> value,
+the loop is broken, and the value is returned
+as the final value of <CODE>foreach</CODE>.
+<P>
+This function could be defined in Lua:
+<PRE>
+function foreach (t, f)
+ local i, v = next(t, nil)
+ while i do
+ local res = f(i, v)
+ if res then return res end
+ i, v = next(t, i)
+ end
+end
+</PRE>
+<P>
+<h3> <TT>foreachvar (function)</TT></h3><A NAME="foreachvar"></A>
+Executes <CODE>function</CODE> over all global variables.
+For each variable,
+the function is called with its name and its value as arguments.
+If the function returns any non-nil value,
+the loop is broken, and the value is returned
+as the final value of <CODE>foreachvar</CODE>.
+<P>
+This function could be defined in Lua:
+<PRE>
+function foreachvar (f)
+ local n, v = nextvar(nil)
+ while n do
+ local res = f(n, v)
+ if res then return res end
+ n, v = nextvar(n)
+ end
+end
+</PRE>
+<P>
<h3> <TT>tostring (e)</TT></h3><A NAME="tostring"></A>
This function receives an argument of any type and
converts it to a string in a reasonable format.
+For complete control on how numbers are converted,
+use function <CODE>format</CODE>.
<P>
<h3> <TT>print (e1, e2, ...)</TT></h3><A NAME="print"></A>
This function receives any number of arguments,
-and prints their values in a reasonable format.
-Each value is printed in a new line.
+and prints their values using the strings returned by <CODE>tostring</CODE>.
This function is not intended for formatted output,
-but as a quick way to show a value,
+but only as a quick way to show a value,
for instance for error messages or debugging.
See Section&nbsp;<A HREF="#libio">6.4</A> for functions for formatted output.
<P>
-<h3> <TT>tonumber (e)</TT></h3><A NAME="tonumber"></A>
+<h3> <TT>tonumber (e [, base])</TT></h3><A NAME="tonumber"></A>
This function receives one argument,
and tries to convert it to a number.
If the argument is already a number or a string convertible
-to a number (see Section&nbsp;<A HREF="#coercion">4.3</A>), then it returns that number;
+to a number, then <CODE>tonumber</CODE> returns that number;
otherwise, it returns <B>nil</B>.
<P>
+An optional argument specifies the base to interpret the numeral.
+The base may be any integer between 2 and 36 inclusive.
+In bases above 10, the letter `A' (either upper or lower case)
+represents 10, `B' represents 11, and so forth, with `Z' representing 35.
+<P>
+In base 10 (the default), the number may have a decimal part,
+as well as an optional exponent part (see Section&nbsp;<A HREF="#coercion">4.3</A>).
+In other bases, only integers are accepted.
+<P>
<A NAME="pdf-type"></A>
<h3> <TT>type (v)</TT></h3><A NAME="type"></A>
This function allows Lua to test the type of a value.
@@ -1655,7 +2011,6 @@ The possible results of this function are
<CODE>"table"</CODE>,
<CODE>"function"</CODE>,
and <CODE>"userdata"</CODE>.
-<CODE>type</CODE> is equivalent to the API function <CODE>lua_type</CODE>.
<P>
<h3> <TT>tag (v)</TT></h3><A NAME="tag"></A>
This function allows Lua to test the tag of a value (see Section&nbsp;<A HREF="#TypesSec">3</A>).
@@ -1666,19 +2021,33 @@ It receives one argument, and returns its tag (a number).
This function sets the tag of a given table (see Section&nbsp;<A HREF="#TypesSec">3</A>).
<CODE>tag</CODE> must be a value created with <CODE>newtag</CODE>
(see Section&nbsp;<A HREF="#pdf-newtag">6.1</A>).
+It returns the value of its first argument (the table).
For security reasons,
it is impossible to change the tag of a userdata from Lua.
<P>
-<h3> <TT>assert (v)</TT></h3><A NAME="assert"></A>
+<P>
+<h3> <TT>assert (v [, message])</TT></h3><A NAME="assert"></A>
This function issues an <EM>``assertion failed!''</EM> error
when its argument is <B>nil</B>.
+This function is equivalent to the following Lua function:
+<PRE>
+function assert (v, m)
+ if not v then
+ m = m or ""
+ error("assertion failed! " .. m)
+ end
+end
+</PRE>
<P>
<A NAME="pdf-error"></A>
<h3> <TT>error (message)</TT></h3><A NAME="error"></A>
-This function issues an error message and terminates
-the last called function from the library
-(<CODE>lua_dofile</CODE>, <CODE>lua_dostring</CODE>, or <CODE>lua_callfunction</CODE>).
-It never returns.
+This function calls the error handler and then terminates
+the last protected function called
+(in&nbsp;C: <CODE>lua_dofile</CODE>, <CODE>lua_dostring</CODE>,
+<CODE>lua_dobuffer</CODE>, or <CODE>lua_callfunction</CODE>;
+in Lua: <CODE>dofile</CODE>, <CODE>dostring</CODE>, or <CODE>call</CODE> in protected mode).
+If <CODE>message</CODE> is <B>nil</B>, the error handler is not called.
+Function <CODE>error</CODE> never returns.
<CODE>error</CODE> is equivalent to the API function <CODE>lua_error</CODE>.
<P>
<h3> <TT>rawgettable (table, index)</TT></h3><A NAME="rawgettable"></A>
@@ -1688,7 +2057,7 @@ without invoking any tag method.
and <CODE>index</CODE> is any value different from <B>nil</B>.
<P>
<h3> <TT>rawsettable (table, index, value)</TT></h3><A NAME="rawsettable"></A>
-Sets the real value <CODE>table[index]=value</CODE>,
+Sets the real value of <CODE>table[index]</CODE> to <CODE>value</CODE>,
without invoking any tag method.
<CODE>table</CODE> must be a table,
<CODE>index</CODE> is any value different from <B>nil</B>,
@@ -1696,15 +2065,20 @@ and <CODE>value</CODE> is any Lua value.
<P>
<h3> <TT>rawsetglobal (name, value)</TT></h3><A NAME="rawsetglobal"></A>
This function assigns the given value to a global variable.
-The string <CODE>name</CODE> does not need to be a syntactically valid variable name.
-Therefore, this function can set global variables with strange names like
+The string <CODE>name</CODE> does not need to be a
+syntactically valid variable name.
+Therefore,
+this function can set global variables with strange names like
<CODE>"m v 1"</CODE> or <CODE>34</CODE>.
-It returns the value of its second argument.
+Function <CODE>rawsetglobal</CODE> returns the value of its second argument.
<P>
<h3> <TT>setglobal (name, value)</TT></h3><A NAME="setglobal"></A>
This function assigns the given value to a global variable,
or calls a tag method.
Its full semantics is explained in Section&nbsp;<A HREF="#tag-method">4.8</A>.
+The string <CODE>name</CODE> does not need to be a
+syntactically valid variable name.
+Function <CODE>setglobal</CODE> returns the value of its second argument.
<P>
<h3> <TT>rawgetglobal (name)</TT></h3><A NAME="rawgetglobal"></A>
This function retrieves the value of a global variable.
@@ -1715,6 +2089,8 @@ syntactically valid variable name.
This function retrieves the value of a global variable,
or calls a tag method.
Its full semantics is explained in Section&nbsp;<A HREF="#tag-method">4.8</A>.
+The string <CODE>name</CODE> does not need to be a
+syntactically valid variable name.
<P>
<h3> <TT>seterrormethod (newmethod)</TT></h3>
<A NAME="pdf-seterrormethod"></A>
@@ -1726,23 +2102,28 @@ Returns the old error handler.
<P>
<h3> <TT>settagmethod (tag, event, newmethod)</TT></h3>
<A NAME="settagmethod"></A>
-This function sets a new tag method to the given pair <EM>&lt;tag, event&gt;</EM>.
+This function sets a new tag method to the given pair <EM>(tag, event)</EM>.
It returns the old method.
If <CODE>newmethod</CODE> is <B>nil</B>,
-it restores the default behavior for the given event.
+<CODE>settagmethod</CODE> restores the default behavior for the given event.
<P>
<h3> <TT>gettagmethod (tag, event)</TT></h3>
<A NAME="gettagmethod"></A>
This function returns the current tag method
-for a given pair <EM>&lt;tag, event&gt;</EM>.
+for a given pair <EM>(tag, event)</EM>.
+<P>
+<h3> <TT>copytagmethods (tagto, tagfrom)</TT></h3>
+<A NAME="copytagmethods"></A>
+This function copies all tag methods from one tag to another;
+it returns <CODE>tagto</CODE>.
<P>
<P>
<A NAME="6.2"></A>
<H2>6.2 - String Manipulation</H2>
This library provides generic functions for string manipulation,
such as finding and extracting substrings and pattern matching.
-When indexing a string, the first character is at position&nbsp;1,
-not&nbsp;0, as in C.
+When indexing a string, the first character is at position&nbsp;1
+(not at&nbsp;0, as in C).
<P>
<h3> <TT>strfind (str, pattern [, init [, plain]])</TT></h3>
<A NAME="strfind"></A>
@@ -1755,6 +2136,10 @@ If the pattern specifies captures,
the captured strings are returned as extra results.
A third optional numerical argument specifies where to start the search;
its default value is 1.
+If <CODE>init</CODE> is negative,
+it is replaced by the length of the string minus its
+absolute value plus 1.
+Therefore, <I>-1</I> points to the last character of <CODE>str</CODE>.
A value of 1 as a fourth optional argument
turns off the pattern matching facilities,
so the function does a plain ``find substring'' operation,
@@ -1769,9 +2154,9 @@ starting at <CODE>i</CODE> and running until <CODE>j</CODE>.
If <CODE>i</CODE> or <CODE>j</CODE> are negative,
they are replaced by the length of the string minus their
absolute value plus 1.
-Therefore, -1 points to the last character of <CODE>s</CODE>
-and -2 to the previous one.
-If <CODE>j</CODE> is absent, it is assumed to be equal to -1
+Therefore, <I>-1</I> points to the last character of <CODE>s</CODE>
+and <I>-2</I> to the previous one.
+If <CODE>j</CODE> is absent, it is assumed to be equal to <I>-1</I>
(which is the same as the string length).
In particular,
the call <CODE>strsub(s,1,j)</CODE> returns a prefix of <CODE>s</CODE>
@@ -1783,24 +2168,42 @@ with length <CODE>i</CODE>.
Receives a string and returns a copy of that string with all
upper case letters changed to lower case.
All other characters are left unchanged.
+The definition of what is an upper case
+letter depends on the current locale.
<P>
<h3> <TT>strupper (s)</TT></h3><A NAME="strupper"></A>
Receives a string and returns a copy of that string with all
lower case letters changed to upper case.
All other characters are left unchanged.
+The definition of what is a lower case
+letter depends on the current locale.
<P>
<h3> <TT>strrep (s, n)</TT></h3><A NAME="strrep"></A>
-Returns a string which is the concatenation of <CODE>n</CODE> copies of
+Returns a string that is the concatenation of <CODE>n</CODE> copies of
the string <CODE>s</CODE>.
<P>
-<h3> <TT>ascii (s [, i])</TT></h3><A NAME="ascii"></A>
-Returns the ASCII code of the character <CODE>s[i]</CODE>.
+<h3> <TT>strbyte (s [, i])</TT></h3><A NAME="strbyte"></A>
+Returns the internal numerical code of the character <CODE>s[i]</CODE>.
If <CODE>i</CODE> is absent, then it is assumed to be 1.
+If <CODE>i</CODE> is negative,
+it is replaced by the length of the string minus its
+absolute value plus 1.
+Therefore, <I>-1</I> points to the last character of <CODE>s</CODE>.
+<P>
+Note that numerical codes are not necessarily portable across platforms.
+<P>
+<h3> <TT>strchar (i1, i2, ...)</TT></h3><A NAME="strchar"></A>
+Receives 0 or more integers.
+Returns a string with length equal to the number of arguments,
+wherein each character has the internal numerical code equal
+to its correspondent argument.
+<P>
+Note that numerical codes are not necessarily portable across platforms.
<P>
<h3> <TT>format (formatstring, e1, e2, ...)</TT></h3><A NAME="format"></A>
<A NAME="format"></A>
-This function returns a formated version of its variable number of arguments
+This function returns a formatted version of its variable number of arguments
following the description given in its first argument (which must be a string).
The format string follows the same rules as the <CODE>printf</CODE> family of
standard C functions.
@@ -1815,17 +2218,27 @@ the string is written between double quotes,
and all double quotes, returns and backslashes in the string
are correctly escaped when written.
For instance, the call
-<LISTING>
+<PRE>
format('%q', 'a string with "quotes" and \n new line')
-</LISTING>
+</PRE>
will produce the string:
-<LISTING>
+<PRE>
"a string with \"quotes\" and \
new line"
-</LISTING>
+</PRE>
+<P>
+Conversions can be applied to the n-th argument in the argument list,
+rather than the next unused argument.
+In this case, the conversion character <CODE>%</CODE> is replaced
+by the sequence <CODE>%d$</CODE>, where <CODE>d</CODE> is a
+decimal digit in the range [1,9],
+giving the position of the argument in the argument list.
+For instance, the call <CODE>format("%2$d -&gt; %1$03d", 1, 34)</CODE> will
+result in <CODE>"34 -&gt; 001"</CODE>.
+The same argument can be used in more than one conversion.
<P>
The options <CODE>c</CODE>, <CODE>d</CODE>, <CODE>E</CODE>, <CODE>e</CODE>, <CODE>f</CODE>,
-<CODE>g</CODE> <CODE>i</CODE>, <CODE>o</CODE>, <CODE>u</CODE>, <CODE>X</CODE>, and <CODE>x</CODE> all
+<CODE>g</CODE>, <CODE>G</CODE>, <CODE>i</CODE>, <CODE>o</CODE>, <CODE>u</CODE>, <CODE>X</CODE>, and <CODE>x</CODE> all
expect a number as argument,
whereas <CODE>q</CODE> and <CODE>s</CODE> expect a string.
Note that the <CODE>*</CODE> modifier can be simulated by building
@@ -1833,7 +2246,9 @@ the appropriate format string.
For example, <CODE>"%*g"</CODE> can be simulated with
<CODE>"%"..width.."g"</CODE>.
<P>
-<h3> <TT>gsub (s, pat, repl [, table] [, n])</TT></h3>
+<EM>Note: function <TT>format</TT> can only be used with strings that do not contain zeros.</EM>
+<P>
+<h3> <TT>gsub (s, pat, repl [, n])</TT></h3>
<A NAME="gsub"></A>
Returns a copy of <CODE>s</CODE>,
where all occurrences of the pattern <CODE>pat</CODE> have been
@@ -1847,12 +2262,8 @@ with <CODE>n</CODE> between 1 and 9
stands for the value of the n-th captured substring.
<P>
If <CODE>repl</CODE> is a function, then this function is called every time a
-match occurs, with the following arguments:
-If <CODE>table</CODE> is present, then the first argument is this table
-and the second one is a match counter (1 for the first call).
-Independently of these two optional arguments,
-all captured substrings are passed as arguments,
-in order (see below);
+match occurs, with all captured substrings passed as arguments,
+in order (see below).
If the value returned by this function is a string,
then it is used as the replacement string;
otherwise, the replacement string is the empty string.
@@ -1863,29 +2274,31 @@ For instance, when <CODE>n</CODE> is 1 only the first occurrence of
<CODE>pat</CODE> is replaced.
<P>
See some examples below:
-<LISTING>
+<PRE>
+ x = gsub("hello world", "(%w%w*)", "%1 %1")
+ --&gt; x="hello hello world world"
+<P>
x = gsub("hello world", "(%w%w*)", "%1 %1", 1)
--&gt; x="hello hello world"
<P>
+ x = gsub("hello world from Lua", "(%w%w*)%s*(%w%w*)", "%2 %1")
+ --&gt; x="world hello Lua from"
+<P>
x = gsub("home = $HOME, user = $USER", "$(%w%w*)", getenv)
--&gt; x="home = /home/roberto, user = roberto" (for instance)
<P>
x = gsub("4+5 = $return 4+5$", "$(.-)%$", dostring)
--&gt; x="4+5 = 9"
<P>
- function f(t, i, v) return t[v] end
- t = {name="lua", version="3.0"}
- x = gsub("$name - $version", "$(%w%w*)", f, t)
- --&gt; x="lua - 3.0"
-<P>
- t = {"apple", "orange", "lime"}
- x = gsub("x and x and x", "x", rawgettable, t)
- --&gt; x="apple and orange and lime"
+ local t = {name="lua", version="3.1"}
+ x = gsub("$name - $version", "$(%w%w*)", function (v) return %t[v] end)
+ --&gt; x="lua - 3.1"
<P>
- t = {}
- dummy, t.n = gsub("first second word", "(%w%w*)", rawsettable, t)
+ t = {n=0}
+ gsub("first second word", "(%w%w*)",
+ function (w) %t.n = %t.n+1; %t[%t.n] = w end)
--&gt; t={"first", "second", "word"; n=3}
-</LISTING>
+</PRE>
<P>
<P>
<A NAME="pm"></A>
@@ -1897,7 +2310,7 @@ The following combinations are allowed in describing a character class:
<DL>
<DT><B><EM>x</EM></B><DD> (where <EM>x</EM> is any character not in the list <CODE>()%.[*-?</CODE>)
- represents the character <EM>x</EM> itself.
-<DT><B><TT>.</TT></B><DD> - represents all characters.
+<DT><B><TT>.</TT></B><DD> - (a dot) represents all characters.
<DT><B><TT>%a</TT></B><DD> - represents all letters.
<DT><B><TT>%A</TT></B><DD> - represents all non letter characters.
<DT><B><TT>%d</TT></B><DD> - represents all digits.
@@ -1910,26 +2323,31 @@ The following combinations are allowed in describing a character class:
<DT><B><TT>%U</TT></B><DD> - represents all non upper case letter characters.
<DT><B><TT>%w</TT></B><DD> - represents all alphanumeric characters.
<DT><B><TT>%W</TT></B><DD> - represents all non alphanumeric characters.
-<DT><B><TT>%<EM>x</TT></EM></B><DD> (where <EM>x</EM> is any non alphanumeric character) -
+<DT><B><TT>%</TT><EM>x</EM></B><DD> (where <EM>x</EM> is any non alphanumeric character) -
represents the character <EM>x</EM>.
This is the standard way to escape the magic characters <CODE>()%.[*-?</CODE>.
-<DT><B><TT>[char-set]</TT></B><DD> -
+<DT><B><TT>[char-set</TT></B>]<DD> -
Represents the class which is the union of all
characters in char-set.
To include a <CODE>]</CODE> in char-set, it must be the first character.
A range of characters may be specified by
-separating the end characters of the range with a <CODE>-</CODE>;
-e.g., <CODE>A-Z</CODE> specifies the upper case characters.
+separating the end characters of the range with a <CODE>-</CODE>.
If <CODE>-</CODE> appears as the first or last character of char-set,
then it represents itself.
All classes <CODE>%</CODE><EM>x</EM> described above can also be used as
components in a char-set.
All other characters in char-set represent themselves.
-<DT><B><TT>[^char-set]</TT></B><DD> -
+E.g., assuming an <EM>ascii</EM> character set,
+<CODE>[%dA-Fa-f]</CODE> specifies the hexa-decimal digits.
+<DT><B><TT>[^char-set</TT></B>]<DD> -
represents the complement of char-set,
where char-set is interpreted as above.
</DL>
<P>
+The definitions of letter, space, etc. depend on the current locale.
+In particular, the class <CODE>[a-z]</CODE> may not be equivalent to <CODE>%l</CODE>.
+The second form should be preferred for more portable programs.
+<P>
<H4>Pattern Item:</H4>
a <A NAME="pattern item"><EM>pattern item</EM></A> may be:
<UL>
@@ -1978,9 +2396,10 @@ that match captures are stored (<EM>captured</EM>) for future use.
Captures are numbered according to their left parentheses.
For instance, in the pattern <CODE>"(a*(.)%w(%s*))"</CODE>,
the part of the string matching <CODE>"a*(.)%w(%s*)"</CODE> is
-stored as the first capture (and therefore has number 1);
-the character matching <CODE>.</CODE> is captured with number 2,
-and the part matching <CODE>%s*</CODE> has number 3.
+stored as the first capture (and therefore has number&nbsp;1);
+the character matching <CODE>.</CODE> is captured with number&nbsp;2,
+and the part matching <CODE>%s*</CODE> has number&nbsp;3.
+<P>
<P>
<A NAME="mathlib"></A>
<A NAME="6.3"></A>
@@ -1995,26 +2414,33 @@ The library provides the following functions:
<A NAME="atan2"></A><A NAME="ceil"></A><A NAME="cos"></A><A NAME="floor"></A>
<A NAME="log"></A><A NAME="log10"></A><A NAME="max"></A><A NAME="min"></A>
<A NAME="mod"></A><A NAME="sin"></A><A NAME="sqrt"></A><A NAME="tan"></A>
+<A NAME="frexp"></A><A NAME="ldexp"></A>
<A NAME="random"></A><A NAME="randomseed"></A>
-<LISTING>
-abs acos asin atan atan2 ceil cos floor log log10
-max min mod sin sqrt tan random randomseed
-</LISTING>
+<PRE>
+abs acos asin atan atan2 ceil cos deg floor log log10
+max min mod rad sin sqrt tan frexp ldexp
+random randomseed
+</PRE>
+plus a global variable <A NAME="PI"><TT>PI</TT></A>.
Most of them
are only interfaces to the homonymous functions in the C library,
except that, for the trigonometric functions,
all angles are expressed in <EM>degrees</EM>, not radians.
+Functions <A NAME="deg"><TT>deg</TT></A> and <A NAME="rad"><TT>rad</TT></A> can be used to convert
+between radians and degrees.
<P>
The function <CODE>max</CODE> returns the maximum
value of its numeric arguments.
Similarly, <CODE>min</CODE> computes the minimum.
-Both can be used with an unlimited number of arguments.
+Both can be used with 1, 2 or more arguments.
<P>
The functions <CODE>random</CODE> and <CODE>randomseed</CODE> are interfaces to
the simple random generator functions <CODE>rand</CODE> and <CODE>srand</CODE>,
provided by ANSI C.
-The function <CODE>random</CODE> returns pseudo-random numbers in the
-range <I>[0,1)</I>.
+The function <CODE>random</CODE>, when called without arguments,
+returns a pseudo-random real number in the range <I>[0,1)</I>.
+When called with a number <I>n</I>,
+<CODE>random</CODE> returns a pseudo-random integer in the range <I>[1,n]</I>.
<P>
<P>
<A NAME="libio"></A>
@@ -2057,7 +2483,7 @@ plus a string describing the error.
<P>
<CITE>
<EM>System dependent</EM>: if <CODE>filename</CODE> starts with a <CODE>|</CODE>,
-then a <A NAME="piped input">piped input</A> is open, via function <A NAME="popen"><TT>popen</TT></A>.
+then a <A NAME="piped input">piped input</A> is opened, via function <A NAME="popen"><TT>popen</TT></A>.
Not all systems implement pipes.
Moreover,
the number of files that can be open at the same time is
@@ -2072,7 +2498,7 @@ it opens the named file,
sets its handle as the value of <CODE>_OUTPUT</CODE>,
and returns this value.
It does not close the current output file.
-Notice that, if the file already exists,
+Note that, if the file already exists,
then it will be <EM>completely erased</EM> with this operation.
When called without parameters,
this function closes the <CODE>_OUTPUT</CODE> file,
@@ -2084,7 +2510,7 @@ plus a string describing the error.
<P>
<CITE>
<EM>System dependent</EM>: if <CODE>filename</CODE> starts with a <CODE>|</CODE>,
-then a <A NAME="piped output">piped output</A> is open, via function <A NAME="popen"><TT>popen</TT></A>.
+then a <A NAME="piped output">piped output</A> is opened, via function <A NAME="popen"><TT>popen</TT></A>.
Not all systems implement pipes.
Moreover,
the number of files that can be open at the same time is
@@ -2100,7 +2526,8 @@ this function does not erase any previous content of the file.
If this function fails, it returns <B>nil</B>,
plus a string describing the error.
<P>
-Notice that function <CODE>writeto</CODE> is available to close an output file.
+Note that function <CODE>writeto</CODE> is
+available to close an output file opened by <CODE>appendto</CODE>.
<P>
<h3> <TT>remove (filename)</TT></h3><A NAME="remove"></A>
<P>
@@ -2120,11 +2547,12 @@ This function returns a string with a file name that can safely
be used for a temporary file.
The file must be explicitly removed when no longer needed.
<P>
-<h3> <TT>read ([readpattern])</TT></h3><A NAME="read"></A>
+<h3> <TT>read ([filehandle] [readpattern])</TT></h3><A NAME="read"></A>
<P>
-This function reads the file <CODE>_INPUT</CODE>
-according to a read pattern, that specifies how much to read;
-characters are read from the current input file until
+This function reads the file <CODE>_INPUT</CODE>,
+or from <CODE>filehandle</CODE> if this argument is given,
+according to a read pattern, which specifies how much to read;
+characters are read from the input file until
the read pattern fails or ends.
The function <CODE>read</CODE> returns a string with the characters read,
even if the pattern succeeds only partially,
@@ -2145,14 +2573,12 @@ it never fails.
A character class followed by <CODE>*</CODE> reads until a character that
does not belong to the class, or end of file;
since it can match a sequence of zero characters, it never fails.
-\footnote{
-Notice that the behavior of read patterns is different from
+Note that the behavior of read patterns is slightly different from
the regular pattern matching behavior,
where a <CODE>*</CODE> expands to the maximum length <EM>such that</EM>
the rest of the pattern does not fail.
With the read pattern behavior
there is no need for backtracking the reading.
-}
<P>
A pattern item may contain sub-patterns enclosed in curly brackets,
that describe <A NAME="skips"><EM>skips</EM></A>.
@@ -2174,10 +2600,11 @@ or <B>nil</B> on end of file.
or <B>nil</B> if the next characters do not conform to an integer format.
</UL>
<P>
-<h3> <TT>write (value1, ...)</TT></h3><A NAME="write"></A>
+<h3> <TT>write ([filehandle, ] value1, ...)</TT></h3><A NAME="write"></A>
<P>
This function writes the value of each of its arguments to the
-file <CODE>_OUTPUT</CODE>.
+file <CODE>_OUTPUT</CODE>,
+or to <CODE>filehandle</CODE> if this argument is given,
The arguments must be strings or numbers.
To write other values,
use <CODE>tostring</CODE> or <CODE>format</CODE> before <CODE>write</CODE>.
@@ -2191,7 +2618,12 @@ formatted according to the given string <CODE>format</CODE>,
following the same rules of the ANSI C function <CODE>strftime</CODE>.
When called without arguments,
it returns a reasonable date and time representation that depends on
-the host system.
+the host system and the locale.
+<P>
+<h3> <TT>clock ()</TT></h3><A NAME="clock"></A>
+<P>
+This function returns an approximation of the amount of CPU time
+used by the program, in seconds.
<P>
<h3> <TT>exit ([code])</TT></h3><A NAME="exit"></A>
<P>
@@ -2202,7 +2634,7 @@ The default value for <CODE>code</CODE> is 1.
<P>
<h3> <TT>getenv (varname)</TT></h3><A NAME="getenv"></A>
<P>
-Returns the value of the environment variable <CODE>varname</CODE>,
+Returns the value of the process environment variable <CODE>varname</CODE>,
or <B>nil</B> if the variable is not defined.
<P>
<h3> <TT>execute (command)</TT></h3><A NAME="execute"></A>
@@ -2211,6 +2643,17 @@ This function is equivalent to the C function <CODE>system</CODE>.
It passes <CODE>command</CODE> to be executed by an operating system shell.
It returns an error code, which is system-dependent.
<P>
+<h3> <TT>setlocale (locale [, category])</TT></h3><A NAME="setlocale"></A>
+<P>
+This function is an interface to the ANSI C function <CODE>setlocale</CODE>.
+<CODE>locale</CODE> is a string specifying a locale;
+<CODE>category</CODE> is an optional string describing which category to change:
+<CODE>"all"</CODE>, <CODE>"collate"</CODE>, <CODE>"ctype"</CODE>,
+<CODE>"monetary"</CODE>, <CODE>"numeric"</CODE>, or <CODE>"time"</CODE>;
+the default category is <CODE>"all"</CODE>.
+The function returns the name of the new locale,
+or <B>nil</B> if the request cannot be honored.
+<P>
<P>
<A NAME="debugI"></A>
<!-- ====================================================================== -->
@@ -2231,9 +2674,9 @@ This interface is declared in the header file <CODE>luadebug.h</CODE>.
<P>
The main function to get information about the interpreter stack
is
-<LISTING>
+<PRE>
lua_Function lua_stackedfunction (int level);
-</LISTING>
+</PRE>
It returns a handle (<CODE>lua_Function</CODE>) to the <EM>activation record</EM>
of the function executing at a given level.
Level&nbsp;0 is the current running function,
@@ -2250,23 +2693,24 @@ it accepts only a handle returned by
<CODE>lua_stackedfunction</CODE>.
<P>
Three other functions produce extra information about a function:
-<LISTING>
+<PRE>
void lua_funcinfo (lua_Object func, char **filename, int *linedefined);
int lua_currentline (lua_Function func);
char *lua_getobjname (lua_Object o, char **name);
-</LISTING>
+</PRE>
<CODE>lua_funcinfo</CODE> gives the file name and the line where the
given function has been defined.
If the ``function'' is in fact the main code of a chunk,
then <CODE>linedefined</CODE> is 0.
If the function is a C function,
-then <CODE>linedefined</CODE> is -1, and <CODE>filename</CODE> is <CODE>"(C)"</CODE>.
+then <CODE>linedefined</CODE> is <I>-1</I>, and <CODE>filename</CODE> is <CODE>"(C)"</CODE>.
<P>
The function <CODE>lua_currentline</CODE> gives the current line where
a given function is executing.
It only works if the function has been compiled with debug
information (see Section&nbsp;<A HREF="#pragma">4.9</A>).
-When no line information is available, it returns -1.
+When no line information is available,
+<CODE>lua_currentline</CODE> returns <I>-1</I>.
<P>
Function <CODE>lua_getobjname</CODE> tries to find a reasonable name for
a given function.
@@ -2292,10 +2736,10 @@ The following functions allow the manipulation of the
local variables of a given activation record.
They only work if the function has been compiled with debug
information (see Section&nbsp;<A HREF="#pragma">4.9</A>).
-<LISTING>
+<PRE>
lua_Object lua_getlocal (lua_Function func, int local_number, char **name);
int lua_setlocal (lua_Function func, int local_number);
-</LISTING>
+</PRE>
<CODE>lua_getlocal</CODE> returns the value of a local variable,
and sets <CODE>name</CODE> to point to the variable name.
<CODE>local_number</CODE> is an index for local variables.
@@ -2309,7 +2753,7 @@ Formal parameters are the first local variables.
<P>
The function <CODE>lua_setlocal</CODE> sets the local variable
<CODE>local_number</CODE> to the value previously pushed on the stack
-(see Section&nbsp;<A HREF="#valuesCLua">5.1</A>).
+(see Section&nbsp;<A HREF="#valuesCLua">5.2</A>).
If the function succeeds, then it returns 1.
If <CODE>local_number</CODE> is greater than the number
of active local variables,
@@ -2320,13 +2764,13 @@ then this function fails and returns 0.
<H2>7.3 - Hooks</H2>
<P>
The Lua interpreter offers two hooks for debugging purposes:
-<LISTING>
+<PRE>
typedef void (*lua_CHFunction) (lua_Function func, char *file, int line);
extern lua_CHFunction lua_callhook;
<P>
typedef void (*lua_LHFunction) (int line);
extern lua_LHFunction lua_linehook;
-</LISTING>
+</PRE>
The first one is called whenever the interpreter enters or leaves a
function.
When entering a function,
@@ -2363,36 +2807,41 @@ is provided with the standard distribution.
This program can be called with any sequence of the following arguments:
<DL>
<DT><B><TT>-v</TT></B><DD> prints version information.
-<DT><B><TT>-</TT></B><DD> runs interactively, accepting commands from standard input
-until an <CODE>EOF</CODE>.
+<DT><B><TT>-d</TT></B><DD> turns on debug information.
<DT><B><TT>-e stat</TT></B><DD> executes <CODE>stat</CODE> as a Lua chunk.
-<DT><B><TT>var=exp</TT></B><DD> executes <CODE>var=exp</CODE> as a Lua chunk.
+<DT><B><TT>-i</TT></B><DD> runs interactively,
+accepting commands from standard input until an <CODE>EOF</CODE>.
+Each line entered is immediately executed.
+<DT><B><TT>-q</TT></B><DD> same as <TT>-i</TT>, but without a prompt (quiet mode).
+<DT><B><TT>-</TT></B><DD> executes <CODE>stdin</CODE> as a file.
+<DT><B><TT>var=value</TT></B><DD> sets global <CODE>var</CODE> with string <CODE>"value"</CODE>.
<DT><B><TT>filename</TT></B><DD> executes file <CODE>filename</CODE> as a Lua chunk.
</DL>
+When called without arguments,
+Lua behaves as <CODE>lua -v -i</CODE> when <CODE>stdin</CODE> is a terminal,
+and as <CODE>lua -</CODE> otherwise.
+<P>
All arguments are handled in order.
For instance, an invocation like
-<LISTING>
-$ lua - a=1 prog.lua
-</LISTING>
+<PRE>
+$ lua -i a=test prog.lua
+</PRE>
will first interact with the user until an <CODE>EOF</CODE>,
-then will set <CODE>a</CODE> to 1,
-and finally will run file <CODE>prog.lua</CODE>.
-<P>
-Please notice that the interaction with the shell may lead to
-unintended results.
-For instance, a call like
-<LISTING>
-$ lua a="name" prog.lua
-</LISTING>
-will <EM>not</EM> set <CODE>a</CODE> to the string <CODE>"name"</CODE>.
-Instead, the quotes will be handled by the shell,
-lua will get only <CODE>a=name</CODE> to run,
-and <CODE>a</CODE> will finish with <B>nil</B>,
-because the global variable <CODE>name</CODE> has not been initialized.
-Instead, one should write
-<LISTING>
-$ lua 'a="name"' prog.lua
-</LISTING>
+then will set <CODE>a</CODE> to <CODE>"test"</CODE>,
+and finally will run the file <CODE>prog.lua</CODE>.
+<P>
+When in interactive mode,
+a multi-line statement can be written finishing intermediate
+lines with a backslash (<CODE>\</CODE>).
+The prompt presented is the value of the global variable <CODE>_PROMPT</CODE>.
+Therefore, the prompt can be changed like below:
+<PRE>
+$ lua _PROMPT='myprompt&gt; ' -i
+</PRE>
+<P>
+In Unix systems, Lua scripts can be made into executable programs
+by using the <CODE>#!</CODE> form,
+as in <CODE>#!/usr/local/bin/lua</CODE>.
<P>
<HR>
<A NAME="Acknowledgments"></A>
@@ -2417,129 +2866,43 @@ the previous public versions of Lua,
some differences had to be introduced.
Here is a list of all these incompatibilities.
<P>
-<h2>Incompatibilities with <A NAME="version 2.5</h2>">version 2.5</h2></A>
+<h2>Incompatibilities with <A NAME="version 3.0</h2>">version 3.0</h2></A>
<UL>
-<LI>
-The whole fallback mechanism has been replaced by tag methods.
-Nevertheless, the function <CODE>setfallback</CODE> has been rewritten in
-a way that uses tag methods to fully emulate the old behavior
-of fallbacks.
-<LI>
-Tags now must be created with the function <CODE>newtag</CODE>.
-Nevertheless, old user defined tags are still accepted
-(user defined tags must be positive;
-<CODE>newtag</CODE> uses negative numbers).
-Tag methods cannot be set for such user defined tags,
-and fallbacks do not affect tags created by <CODE>newtag</CODE>.
-<LI>
-Lua 2.5 accepts mixed comparisons of strings and numbers,
-like <CODE>2&lt;"12"</CODE>, giving weird results.
-Now this is an error.
-<LI>
-Character <CODE>"-"</CODE> (hyphen) now is ``magic'' in pattern matching.
-<LI>
-Some API functions have been rewritten as macros.
-</UL>
<P>
-<h2>Incompatibilities with <A NAME="version 2.4</h2>">version 2.4</h2></A>
-The whole I/O facilities have been rewritten.
-We strongly encourage programmers to adapt their code
-to this new version.
-The incompatibilities between the new and the old libraries are:
-<UL>
-<LI>The format facility of function <CODE>write</CODE> has been supersed by
-function <CODE>format</CODE>;
-therefore this facility has been dropped.
-<LI>Function <CODE>read</CODE> now uses <EM>read patterns</EM> to specify
-what to read;
-this is incompatible with the old format options.
-<LI>Function <CODE>strfind</CODE> now accepts patterns,
-so it may have a different behavior when the pattern includes
-special characters.
-</UL>
+<LI>To support multiple contexts,
+Lua 3.1 must be explicitly opened before used,
+with function <CODE>lua_open</CODE>.
+However, all standard libraries check whether Lua is already opened,
+so any existing program that opens at least one standard
+library before calling Lua does not need to be modified.
<P>
-<h2>Incompatibilities with <A NAME="version 2.2</h2>">version 2.2</h2></A>
-<UL>
-<LI>
-Functions <CODE>date</CODE> and <CODE>time</CODE> (from <CODE>iolib</CODE>)
-have been superseded by the new, more powerful version of function <CODE>date</CODE>.
-<LI>
-Function <CODE>append</CODE> (from <CODE>iolib</CODE>) now returns 1 whenever it succeeds,
-whether the file is new or not.
-<LI>
-Function <CODE>int2str</CODE> (from <CODE>strlib</CODE>) has been superseded by new
-function <CODE>format</CODE>, with parameter <CODE>"%c"</CODE>.
-<LI>
-The API lock mechanism has been superseded by the reference mechanism.
-However, <CODE>lua.h</CODE> provides compatibility macros,
-so there is no need to change programs.
-<LI>
-The API function <CODE>lua_pushliteral</CODE> now is just a macro to
-<CODE>lua_pushstring</CODE>.
-</UL>
+<LI>Function <CODE>dostring</CODE> no longer accepts an optional second argument,
+with a temporary error method.
+This facility is now provided by function <CODE>call</CODE>.
<P>
-<h2>Incompatibilities with <A NAME="version 2.1</h2>">version 2.1</h2></A>
-<UL>
-<LI>
-The function <CODE>type</CODE> now returns the string <CODE>"function"</CODE>
-both for C and Lua functions.
-Because Lua functions and C functions are compatible,
-this behavior is usually more useful.
-When needed, the second result of function <TT>type</TT> may be used
-to distinguish between Lua and C functions.
-<LI>
-A function definition only assigns the function value to the
-given variable at execution time.
-</UL>
+<LI>Function <CODE>gsub</CODE> no longer accepts an optional fourth argument
+(a callback data, a table).
+Closures replace this feature with advantage.
+<P>
+<LI>The syntax for function declaration is now more restricted;
+for instance, the old syntax <CODE>function f[exp] (x) ... end</CODE> is not
+accepted in Lua 3.1.
+In these cases,
+programs should use an explicit assignment instead, such as
+<CODE>f[exp] = function (x) ... end</CODE>.
+<P>
+<LI>Old pre-compiled code is obsolete, and must be re-compiled.
+<P>
+<LI>The option <CODE>a=b</CODE> in Lua stand-alone now sets <CODE>a</CODE> to the
+<EM>string</EM> <CODE>b</CODE>, and not to the value of <CODE>b</CODE>.
<P>
-<h2>Incompatibilities with <A NAME="version 1.1</h2>">version 1.1</h2></A>
-<UL>
-<LI>
-The equality test operator now is denoted by <CODE>==</CODE>,
-instead of <CODE>=</CODE>.
-<LI>
-The syntax for table construction has been greatly simplified.
-The old <CODE>@(size)</CODE> has been substituted by <CODE>{}</CODE>.
-The list constructor (formerly <CODE>@[...]</CODE>) and the record
-constructor (formerly <CODE>@{...}</CODE>) now are both coded like
-<CODE>{...}</CODE>.
-When the construction involves a function call,
-like in <CODE>@func{...}</CODE>,
-the new syntax does not use the <CODE>@</CODE>.
-More important, {\em a construction function must now
-explicitly return the constructed table}.
-<LI>
-The function <CODE>lua_call</CODE> no longer has the parameter <CODE>nparam</CODE>.
-<LI>
-The function <CODE>lua_pop</CODE> is no longer available,
-since it could lead to strange behavior.
-In particular,
-to access results returned from a Lua function,
-the new macro <CODE>lua_getresult</CODE> should be used.
-<LI>
-The old functions <CODE>lua_storefield</CODE> and <CODE>lua_storeindexed</CODE>
-have been replaced by
-<LISTING>
-int lua_storesubscript (void);
-</LISTING>
-with the parameters explicitly pushed on the stack.
-<LI>
-The functionality of the function <CODE>lua_errorfunction</CODE> has been
-replaced by the <EM>fallback</EM> mechanism (see Section&nbsp;<A HREF="#error">4.9</A>).
-<LI>
-When calling a function from the Lua library,
-parameters passed through the stack
-must be pushed just before the corresponding call,
-with no intermediate calls to Lua.
-Special care should be taken with macros like
-<CODE>lua_getindexed</CODE> and <CODE>lua_getfield</CODE>.
</UL>
<P>
<P>
<HR>
Last update:
-Tue Jul 1 07:55:45 EST 1997
+Fri Jul 10 15:10:14 EST 1998
by <A HREF="http://www.tecgraf.puc-rio.br/~lhf/">lhf</A>.
</BODY>
</HTML>