aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1998-06-18 14:36:27 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1998-06-18 14:36:27 -0300
commitbec9bc4154e54fbc19b134874f6301f1e4654d5d (patch)
treeda7ca157669975b942d0edf4ddf2a310733e30c9
parent955a811aa1ab50cc4165d88cdf5a259b7b77582a (diff)
downloadlua-bec9bc4154e54fbc19b134874f6301f1e4654d5d.tar.gz
lua-bec9bc4154e54fbc19b134874f6301f1e4654d5d.tar.bz2
lua-bec9bc4154e54fbc19b134874f6301f1e4654d5d.zip
sugestions by lhf
-rw-r--r--manual.tex440
1 files changed, 232 insertions, 208 deletions
diff --git a/manual.tex b/manual.tex
index 5bc36b80..7b64e2a3 100644
--- a/manual.tex
+++ b/manual.tex
@@ -1,4 +1,4 @@
1% $Id: manual.tex,v 1.13 1998/06/06 21:05:52 roberto Exp roberto $ 1% $Id: manual.tex,v 1.14 1998/06/15 21:34:14 roberto Exp roberto $
2 2
3\documentclass[11pt]{article} 3\documentclass[11pt]{article}
4\usepackage{fullpage,bnf} 4\usepackage{fullpage,bnf}
@@ -39,7 +39,7 @@ Waldemar Celes
39\tecgraf\ --- Computer Science Department --- PUC-Rio 39\tecgraf\ --- Computer Science Department --- PUC-Rio
40} 40}
41 41
42%\date{\small \verb$Date: 1998/06/06 21:05:52 $} 42%\date{\small \verb$Date: 1998/06/15 21:34:14 $}
43 43
44\maketitle 44\maketitle
45 45
@@ -57,17 +57,6 @@ Lua is dynamically typed, interpreted from bytecodes,
57and has automatic memory management with garbage collection, 57and has automatic memory management with garbage collection,
58making it ideal for configuration, scripting, and rapid prototyping. 58making it ideal for configuration, scripting, and rapid prototyping.
59 59
60Lua is implemented as a small library of C functions,
61written in ANSI C, and compiles unmodified in all known platforms.
62The implementation goals are simplicity, efficiency, portability,
63and low embedding cost.
64
65Lua has been developed at TeCGraf,
66the Computer Graphics Technology Group of PUC-Rio
67(the Pontifical Catholic University of Rio de Janeiro in Brazil).
68TeCGraf is a laboratory of the Department of Computer Science.
69Dozens of industrial products developed by TeCGraf use Lua.
70
71This document describes version \Version\ of the Lua programming language 60This document describes version \Version\ of the Lua programming language
72and the API that allows interaction between Lua programs and their 61and the API that allows interaction between Lua programs and their
73host C programs. 62host C programs.
@@ -90,12 +79,6 @@ e tem gerenciamento autom\'atico de mem\'oria com coleta de lixo,
90tornando-se ideal para configura\c{c}\~ao, scripting, 79tornando-se ideal para configura\c{c}\~ao, scripting,
91e prototipagem r\'apida. 80e prototipagem r\'apida.
92 81
93Lua \'e implementada como uma pequena biblioteca de fun\c{c}\~oes C,
94escrita em ANSI C, e compila sem modifica\c{c}\~oes em todas as
95plataformas conhecidas.
96Os objetivos da implementa\c{c}\~ao s\~ao simplicidade, efici\^encia,
97portabilidade, e baixo custo.
98
99Este documento descreve a vers\~ao \Version\ da linguagem de 82Este documento descreve a vers\~ao \Version\ da linguagem de
100programa\c{c}\~ao Lua e a Interface de Programa\c{c}\~ao (API) que permite 83programa\c{c}\~ao Lua e a Interface de Programa\c{c}\~ao (API) que permite
101a intera\c{c}\~ao entre programas Lua e programas C hospedeiros. 84a intera\c{c}\~ao entre programas Lua e programas C hospedeiros.
@@ -108,6 +91,7 @@ a intera\c{c}\~ao entre programas Lua e programas C hospedeiros.
108\footnotesize 91\footnotesize
109Copyright \copyright\ 1994--1998 TeCGraf, PUC-Rio. All rights reserved. 92Copyright \copyright\ 1994--1998 TeCGraf, PUC-Rio. All rights reserved.
110 93
94\noindent
111Permission is hereby granted, without written agreement and without license 95Permission is hereby granted, without written agreement and without license
112or royalty fees, to use, copy, modify, and distribute this software and its 96or royalty fees, to use, copy, modify, and distribute this software and its
113documentation for any purpose, including commercial applications, subject to 97documentation for any purpose, including commercial applications, subject to
@@ -126,18 +110,19 @@ the following conditions:
126\end{itemize} 110\end{itemize}
127The authors specifically disclaim any warranties, including, but not limited 111The authors specifically disclaim any warranties, including, but not limited
128to, the implied warranties of merchantability and fitness for a particular 112to, the implied warranties of merchantability and fitness for a particular
129purpose. The software provided hereunder is on an "as is" basis, and the 113purpose. The software provided hereunder is on an ``as is'' basis, and the
130authors have no obligation to provide maintenance, support, updates, 114authors have no obligation to provide maintenance, support, updates,
131enhancements, or modifications. In no event shall TeCGraf, PUC-Rio, or the 115enhancements, or modifications. In no event shall TeCGraf, PUC-Rio, or the
132authors be held liable to any party for direct, indirect, special, 116authors be held liable to any party for direct, indirect, special,
133incidental, or consequential damages arising out of the use of this software 117incidental, or consequential damages arising out of the use of this software
134and its documentation. 118and its documentation.
135 119
136The Lua language and this implementation have been entirely designed and 120\noindent
137written by Waldemar Celes Filho, Roberto Ierusalimschy and 121The Lua language and this implementation have been entirely designed and
138Luiz Henrique de Figueiredo at TeCGraf, PUC-Rio. 122written by Waldemar Celes, Roberto Ierusalimschy and Luiz Henrique de
139This implementation contains no third-party code. 123Figueiredo at TeCGraf, PUC-Rio. This implementation contains no third-party
140\end{quotation} 124code.
125\end{quotation}
141 126
142\newpage 127\newpage
143 128
@@ -153,7 +138,7 @@ This implementation contains no third-party code.
153Lua is an extension programming language designed to support 138Lua is an extension programming language designed to support
154general procedural programming with data description 139general procedural programming with data description
155facilities. 140facilities.
156It is intended to be used as a light-weight, but powerful, 141Lua is intended to be used as a light-weight, but powerful,
157configuration language for any program that needs one. 142configuration language for any program that needs one.
158Lua has been designed and implemented by 143Lua has been designed and implemented by
159W.~Celes, 144W.~Celes,
@@ -173,7 +158,7 @@ thus creating customized programming languages sharing a syntactical framework.
173 158
174Lua is free-distribution software, 159Lua is free-distribution software,
175and provided as usual with no guarantees, 160and provided as usual with no guarantees,
176as stated in the copyright notice in the front page of this manual. 161as stated in the copyright notice.
177The implementation described in this manual is available 162The implementation described in this manual is available
178at the following URL's: 163at the following URL's:
179\begin{verbatim} 164\begin{verbatim}
@@ -188,6 +173,8 @@ All statements in Lua are executed in a \Def{global environment}.
188This environment, which keeps all global variables, 173This environment, which keeps all global variables,
189is initialized at the beginning of the embedding program and 174is initialized at the beginning of the embedding program and
190persists until its end. 175persists until its end.
176Optionally, a user can create multiple independent global
177environments \see{mangstate}.
191 178
192The global environment can be manipulated by Lua code or 179The global environment can be manipulated by Lua code or
193by the embedding program, 180by the embedding program,
@@ -236,23 +223,23 @@ There are six \Index{basic types} in Lua: \Def{nil}, \Def{number},
236\Def{string}, \Def{function}, \Def{userdata}, and \Def{table}. 223\Def{string}, \Def{function}, \Def{userdata}, and \Def{table}.
237\emph{Nil} is the type of the value \nil, 224\emph{Nil} is the type of the value \nil,
238whose main property is to be different from any other value. 225whose main property is to be different from any other value.
239\emph{Number} represents real (floating-point with double precision) numbers, 226\emph{Number} represents real (double precision floating point) numbers,
240while \emph{string} has the usual meaning; 227while \emph{string} has the usual meaning.
241notice that Lua is \Index{eight-bit clean}, 228Lua is \Index{eight-bit clean},
242and so strings can contain any ISO character, 229and so strings may contain any 8-bit character,
243\emph{including} \verb|'\0'|. 230\emph{including} embedded zeros (\verb|'\0'|).
244The function \verb|type| returns a string describing the type 231The function \verb|type| returns a string describing the type
245of a given value \see{pdf-type}. 232of a given value \see{pdf-type}.
246 233
247Functions are considered first-class values in Lua. 234Functions are considered first-class values in Lua.
248This means that functions can be stored in variables, 235This means that functions can be stored in variables,
249passed as arguments to other functions and returned as results. 236passed as arguments to other functions, and returned as results.
250Lua can call (and manipulate) functions written in Lua and 237Lua can call (and manipulate) functions written in Lua and
251functions written in C. 238functions written in C.
252They can be distinguished by their tags: 239They can be distinguished by their tags:
253all Lua functions have the same tag, 240all Lua functions have the same tag,
254and all C functions have the same tag, 241and all C functions have the same tag,
255which is different from the tag of a Lua function. 242which is different from the tag of Lua functions.
256 243
257The type \emph{userdata} is provided to allow 244The type \emph{userdata} is provided to allow
258arbitrary \Index{C pointers} to be stored in Lua variables. 245arbitrary \Index{C pointers} to be stored in Lua variables.
@@ -278,9 +265,9 @@ The form \verb|t:f(x)| is syntactic sugar for \verb|t.f(t,x)|,
278which calls the method \verb|f| from the table \verb|t| passing 265which calls the method \verb|f| from the table \verb|t| passing
279itself as the first parameter \see{func-def}. 266itself as the first parameter \see{func-def}.
280 267
281It is important to notice that tables are \emph{objects}, and not values. 268Note that tables are \emph{objects}, and not values.
282Variables cannot contain tables, only \emph{references} to them. 269Variables cannot contain tables, only \emph{references} to them.
283Assignment, parameter passing and returns always manipulate references 270Assignment, parameter passing, and returns always manipulate references
284to tables, and do not imply any kind of copy. 271to tables, and do not imply any kind of copy.
285Moreover, tables must be explicitly created before used 272Moreover, tables must be explicitly created before used
286\see{tableconstructor}. 273\see{tableconstructor}.
@@ -309,7 +296,8 @@ This section describes the lexis, the syntax and the semantics of Lua.
309 296
310\subsection{Lexical Conventions} \label{lexical} 297\subsection{Lexical Conventions} \label{lexical}
311 298
312\Index{Identifiers} can be any string of letters, digits, and underscores, 299\Index{Identifiers} in Lua can be any string of letters,
300digits, and underscores,
313not beginning with a digit. 301not beginning with a digit.
314The definition of letter depends on the current locale: 302The definition of letter depends on the current locale:
315Any character considered alphabetic by the current locale 303Any character considered alphabetic by the current locale
@@ -330,8 +318,8 @@ uppercase letters should not be used in regular programs.
330 318
331The following strings denote other \Index{tokens}: 319The following strings denote other \Index{tokens}:
332\begin{verbatim} 320\begin{verbatim}
333 ~= <= >= < > == = .. + - * / 321 ~= <= >= < > == = + - * / %
334 % ( ) { } [ ] ; , . ... 322 ( ) { } [ ] ; , . .. ...
335\end{verbatim} 323\end{verbatim}
336 324
337\Index{Literal strings} can be delimited by matching single or double quotes, 325\Index{Literal strings} can be delimited by matching single or double quotes,
@@ -343,13 +331,13 @@ and can contain the C-like escape sequences
343\verb|'\r'| (carriage return), 331\verb|'\r'| (carriage return),
344\verb|'\t'| (horizontal tab), 332\verb|'\t'| (horizontal tab),
345\verb|'\v'| (vertical tab), 333\verb|'\v'| (vertical tab),
346\verb|'\\'|, 334\verb|'\\'|, (backslash),
347\verb|'\"'|, 335\verb|'\"'|, (double quote),
348and \verb|'\''|. 336and \verb|'\''| (single quote).
349A character in a string may also be specified by its numerical value, 337A character in a string may also be specified by its numerical value,
350through the escape sequence \verb|'\ddd'|, 338through the escape sequence \verb|'\ddd'|,
351where \verb|ddd| is a sequence of up to three \emph{decimal} digits. 339where \verb|ddd| is a sequence of up to three \emph{decimal} digits.
352Strings in Lua may contain any 8-bit value, including 0. 340Strings in Lua may contain any 8-bit value, including embedded 0.
353 341
354Literal strings can also be delimited by matching \verb|[[ ... ]]|. 342Literal strings can also be delimited by matching \verb|[[ ... ]]|.
355Literals in this bracketed form may run for several lines, 343Literals in this bracketed form may run for several lines,
@@ -371,7 +359,7 @@ the following three literals are equivalent:
371\Index{Comments} start anywhere outside a string with a 359\Index{Comments} start anywhere outside a string with a
372double hyphen (\verb|--|) and run until the end of the line. 360double hyphen (\verb|--|) and run until the end of the line.
373Moreover, 361Moreover,
374the first line of a chunk file is skipped if it starts with \verb|#|. 362the first line of a chunk is skipped if it starts with \verb|#|.
375This facility allows the use of Lua as a script interpreter 363This facility allows the use of Lua as a script interpreter
376in Unix systems \see{lua-sa}. 364in Unix systems \see{lua-sa}.
377 365
@@ -384,18 +372,19 @@ Examples of valid numerical constants are:
384 372
385\subsection{The \Index{Pre-processor}} \label{pre-processor} 373\subsection{The \Index{Pre-processor}} \label{pre-processor}
386 374
387All lines that start with a \verb|$| are handled by a pre-processor. 375All lines that start with a \verb|$| sign are handled by a pre-processor.
388The \verb|$| must be immediately followed by one of the following directives: 376The \verb|$| sign must be immediately
377followed by one of the following directives:
389\begin{description} 378\begin{description}
390\item[\T{debug}] --- turn on some debugging facilities \see{pragma}. 379\item[\T{debug}] --- turn on debugging facilities \see{pragma}.
391\item[\T{nodebug}] --- turn off some debugging facilities \see{pragma}. 380\item[\T{nodebug}] --- turn off debugging facilities \see{pragma}.
392\item[\T{if \M{cond}}] --- starts a conditional part. 381\item[\T{if \M{cond}}] --- starts a conditional part.
393If \M{cond} is false, then this part is skipped by the lexical analyzer. 382If \M{cond} is false, then this part is skipped by the lexical analyzer.
394\item[\T{ifnot \M{cond}}] --- starts a conditional part. 383\item[\T{ifnot \M{cond}}] --- starts a conditional part.
395If \M{cond} is true, then this part is skipped by the lexical analyzer. 384If \M{cond} is true, then this part is skipped by the lexical analyzer.
396\item[\T{end}] --- ends a conditional part. 385\item[\T{end}] --- ends a conditional part.
397\item[\T{else}] --- starts an ``else'' conditional part, 386\item[\T{else}] --- starts an ``else'' conditional part,
398flopping the ``skip'' status. 387flipping the ``skip'' status.
399\item[\T{endinput}] --- ends the lexical parse of the file. 388\item[\T{endinput}] --- ends the lexical parse of the file.
400\end{description} 389\end{description}
401 390
@@ -409,7 +398,7 @@ A \M{cond} part may be:
409\item[\T{1}] --- always true. 398\item[\T{1}] --- always true.
410\item[\M{name}] --- true if the value of the 399\item[\M{name}] --- true if the value of the
411global variable \M{name} is different from \nil. 400global variable \M{name} is different from \nil.
412Notice that \M{name} is evaluated before the chunk starts its execution. 401Note that \M{name} is evaluated \emph{before} the chunk starts its execution.
413Therefore, actions in a chunk do not affect its own conditional directives. 402Therefore, actions in a chunk do not affect its own conditional directives.
414\end{description} 403\end{description}
415 404
@@ -666,10 +655,18 @@ Both \verb|and| and \verb|or| use \Index{short-cut evaluation},
666that is, 655that is,
667the second operand is evaluated only when necessary. 656the second operand is evaluated only when necessary.
668 657
658A useful Lua idiom is \verb|x = x or v|,
659which is equivalent to
660\begin{verbatim}
661 if x == nil then x = v end
662\end{verbatim}
663i.e., it sets \verb|x| to a default value \verb|v| when
664\verb|x| is not set.
665
669\subsubsection{Concatenation} 666\subsubsection{Concatenation}
670Lua offers a string \Index{concatenation} operator, 667The string \Index{concatenation} operator in Lua is
671denoted by ``\IndexVerb{..}''. 668denoted by ``\IndexVerb{..}''.
672If operands are strings or numbers, then they are converted to 669If both operands are strings or numbers, they are converted to
673strings according to the rules in \See{coercion}. 670strings according to the rules in \See{coercion}.
674Otherwise, the ``concat'' tag method is called \see{tag-method}. 671Otherwise, the ``concat'' tag method is called \see{tag-method}.
675 672
@@ -748,7 +745,7 @@ is equivalent to:
748An expression like \verb|{x = 1, y = 4}| is 745An expression like \verb|{x = 1, y = 4}| is
749in fact syntactic sugar for \verb|{["x"] = 1, ["y"] = 4}|. 746in fact syntactic sugar for \verb|{["x"] = 1, ["y"] = 4}|.
750 747
751Both forms may have an optional ending comma, 748Both forms may have an optional trailing comma,
752and can be used in the same constructor separated by 749and can be used in the same constructor separated by
753a semi-collon. 750a semi-collon.
754For example, all forms below are correct: 751For example, all forms below are correct:
@@ -812,6 +809,17 @@ thus discarding all returned values but the first one.
812If the function is called in a place that can hold many values 809If the function is called in a place that can hold many values
813(syntactically denoted by the non-terminal \M{exp}), 810(syntactically denoted by the non-terminal \M{exp}),
814then no adjustment is made. 811then no adjustment is made.
812Note that the only place that can hold many values
813is the last expression (or the only one) in an assignment
814or in a return statement; see examples below.
815\begin{verbatim}
816 f(); -- adjusted to 0
817 g(x, f()); -- f() is adjusted to 1
818 a,b,c = f(), x; -- f() is adjusted to 1 result (and c gets nil)
819 a,b,c = x, f(); -- f() is adjusted to 2
820 a,b,c = f(); -- f() is adjusted to 3
821 return f(); -- returns all values returned by f()
822\end{verbatim}
815 823
816\subsubsection{\Index{Function Definitions}} \label{func-def} 824\subsubsection{\Index{Function Definitions}} \label{func-def}
817 825
@@ -890,7 +898,7 @@ If control reaches the end of a function without a return instruction,
890then the function returns with no results. 898then the function returns with no results.
891 899
892There is a special syntax for defining \Index{methods}, 900There is a special syntax for defining \Index{methods},
893that is, functions that have an extra parameter \IndexVerb{self}. 901that is, functions that have an implicit extra parameter \IndexVerb{self}.
894\begin{Produc} 902\begin{Produc}
895\produc{function}{\rwd{function} name \ter{:} name \ter{(} \opt{parlist1} 903\produc{function}{\rwd{function} name \ter{:} name \ter{(} \opt{parlist1}
896 \ter{)} block \rwd{end}} 904 \ter{)} block \rwd{end}}
@@ -908,7 +916,7 @@ v.f = function (self, ...)
908end 916end
909\end{verbatim} 917\end{verbatim}
910that is, the function gets an extra formal parameter called \verb|self|. 918that is, the function gets an extra formal parameter called \verb|self|.
911Notice that the variable \verb|v| must have been 919Note that the variable \verb|v| must have been
912previously initialized with a table value. 920previously initialized with a table value.
913 921
914 922
@@ -936,19 +944,19 @@ at the point where the function is defined.
936 944
937Here are some examples: 945Here are some examples:
938\begin{verbatim} 946\begin{verbatim}
939a,b,c = 1 -- global variables 947a,b,c = 1,2,3 -- global variables
940function f () 948function f (x)
941 local x,b -- x and b are local to f 949 local b -- x and b are local to f
942 function g () 950 local g = function (a)
943 local a,y -- a and y are local to g 951 local y -- a and y are local to g
944 p = a -- OK, access local 'a' 952 p = a -- OK, access local 'a'
945 p = c -- OK, access global 'c' 953 p = c -- OK, access global 'c'
946 p = b -- ERROR: cannot access a variable in outer scope 954 p = b -- ERROR: cannot access a variable in outer scope
947 p = %b -- OK, access frozen value of 'b' (local to 'f') 955 p = %b -- OK, access frozen value of 'b' (local to 'f')
948 p = %c -- OK, access frozen value of global 'c' 956 p = %c -- OK, access frozen value of global 'c'
949 p = %y -- ERROR: 'y' is not visible where 'g' is defined 957 p = %y -- ERROR: 'y' is not visible where 'g' is defined
950 end 958 end -- g
951end 959end -- f
952\end{verbatim} 960\end{verbatim}
953 961
954 962
@@ -956,9 +964,9 @@ end
956 964
957Lua provides a powerful mechanism to extend its semantics, 965Lua provides a powerful mechanism to extend its semantics,
958called \Def{Tag Methods}. 966called \Def{Tag Methods}.
959A tag method (TM) is a programmer-defined function 967A tag method is a programmer-defined function
960that is called at specific key points during the evaluation of a program, 968that is called at specific key points during the evaluation of a program,
961allowing a programmer to change the standard Lua behavior at these points. 969allowing the programmer to change the standard Lua behavior at these points.
962Each of these points is called an \Def{event}. 970Each of these points is called an \Def{event}.
963 971
964The tag method called for any specific event is selected 972The tag method called for any specific event is selected
@@ -966,11 +974,11 @@ according to the tag of the values involved
966in the event \see{TypesSec}. 974in the event \see{TypesSec}.
967The function \IndexVerb{settagmethod} changes the tag method 975The function \IndexVerb{settagmethod} changes the tag method
968associated with a given pair \M{(tag, event)}. 976associated with a given pair \M{(tag, event)}.
969Its first parameter is the tag, the second the event name 977Its first parameter is the tag, the second is the event name
970(a string, see below), 978(a string, see below),
971and the third parameter is the new method (a function), 979and the third parameter is the new method (a function),
972or \nil\ to restore the default behavior. 980or \nil\ to restore the default behavior.
973The function returns the previous tag method. 981The function returns the previous tag method for that pair.
974Another function, \IndexVerb{gettagmethod}, 982Another function, \IndexVerb{gettagmethod},
975receives a tag and an event name and returns the 983receives a tag and an event name and returns the
976current method associated with the pair. 984current method associated with the pair.
@@ -995,7 +1003,7 @@ called when a \verb|+| operation is applied to non numerical operands.
995 1003
996The function \verb|getbinmethod| defines how Lua chooses a tag method 1004The function \verb|getbinmethod| defines how Lua chooses a tag method
997for a binary operation. 1005for a binary operation.
998First Lua tries the first operand. 1006First, Lua tries the first operand.
999If its tag does not define a tag method for the operation, 1007If its tag does not define a tag method for the operation,
1000then Lua tries the second operand. 1008then Lua tries the second operand.
1001If it also fails, then it gets a tag method from tag~0: 1009If it also fails, then it gets a tag method from tag~0:
@@ -1011,7 +1019,7 @@ If it also fails, then it gets a tag method from tag~0:
1011 local o1, o2 = tonumber(op1), tonumber(op2) 1019 local o1, o2 = tonumber(op1), tonumber(op2)
1012 if o1 and o2 then -- both operands are numeric 1020 if o1 and o2 then -- both operands are numeric
1013 return o1+o2 -- '+' here is the primitive 'add' 1021 return o1+o2 -- '+' here is the primitive 'add'
1014 else -- at least one of the operands is not numeric. 1022 else -- at least one of the operands is not numeric
1015 local tm = getbinmethod(op1, op2, "add") 1023 local tm = getbinmethod(op1, op2, "add")
1016 if tm then 1024 if tm then
1017 -- call the method with both operands and an extra 1025 -- call the method with both operands and an extra
@@ -1147,7 +1155,7 @@ created by \verb|newtag|.
1147 end 1155 end
1148 end 1156 end
1149\end{verbatim} 1157\end{verbatim}
1150Notice: the function \verb|getglobal| is pre-defined in Lua \see{predefined}. 1158The function \verb|getglobal| is pre-defined in Lua \see{predefined}.
1151 1159
1152\item[``setglobal'':]\index{setglobal event} 1160\item[``setglobal'':]\index{setglobal event}
1153called whenever Lua assigns to a global variable. 1161called whenever Lua assigns to a global variable.
@@ -1179,7 +1187,7 @@ This method cannot be set for tables with default tag.
1179 else 1187 else
1180 local v = rawgettable(table, index) 1188 local v = rawgettable(table, index)
1181 tm = gettagmethod(tag(table), "index") 1189 tm = gettagmethod(tag(table), "index")
1182 if (v == nil) and tm then 1190 if v == nil and tm then
1183 return tm(table, index) 1191 return tm(table, index)
1184 else 1192 else
1185 return v 1193 return v
@@ -1257,12 +1265,13 @@ calling a function from the Lua library.
1257Whenever an error occurs during Lua compilation or execution, 1265Whenever an error occurs during Lua compilation or execution,
1258the \Def{error method} is called, 1266the \Def{error method} is called,
1259and then the corresponding function from the library 1267and then the corresponding function from the library
1260(\verb|lua_dofile|, \verb|lua_dostring|, or \verb|lua_callfunction|) 1268(\verb|lua_dofile|, \verb|lua_dostring|,
1261is terminated returning an error condition. 1269\verb|lua_dobuffer|, or \verb|lua_callfunction|)
1270is terminated, returning an error condition.
1262 1271
1263The only argument to the error method is a string 1272The only argument to the error method is a string
1264describing the error. 1273describing the error.
1265The default method prints this message in \verb|stderr|. 1274The default method prints this message to \verb|stderr|.
1266If needed, it is possible to change the error method with the 1275If needed, it is possible to change the error method with the
1267function \verb|seterrormethod|, 1276function \verb|seterrormethod|,
1268which gets the new error handler as its only parameter 1277which gets the new error handler as its only parameter
@@ -1304,7 +1313,7 @@ The API functions can be classified in the following categories:
1304All API functions and related types and constants 1313All API functions and related types and constants
1305are declared in the header file \verb|lua.h|. 1314are declared in the header file \verb|lua.h|.
1306 1315
1307\subsection{Managing States} 1316\subsection{Managing States} \label{mangstate}
1308The whole state of the Lua interpreter 1317The whole state of the Lua interpreter
1309(global variables, stack, tag methods, etc) 1318(global variables, stack, tag methods, etc)
1310is stored in a dynamic structure pointed by\Deffunc{lua_state} 1319is stored in a dynamic structure pointed by\Deffunc{lua_state}
@@ -1317,23 +1326,23 @@ Before calling any API function,
1317this state must be initialized. 1326this state must be initialized.
1318This is done by calling\Deffunc{lua_open} 1327This is done by calling\Deffunc{lua_open}
1319\begin{verbatim} 1328\begin{verbatim}
1320void lua_open (void); 1329void lua_open (void);
1321\end{verbatim} 1330\end{verbatim}
1322This function allocates and initializes some internal structures, 1331This function allocates and initializes some internal structures,
1323and defines all pre-defined functions of Lua. 1332and defines all pre-defined functions of Lua.
1324If \verb|lua_state| is already different from \verb|NULL|, 1333If \verb|lua_state| is already different from \verb|NULL|,
1325this function has no effect; 1334\verb|lua_open| has no effect;
1326therefore, it is safe to call this function multiple times. 1335therefore, it is safe to call this function multiple times.
1327All standard libraries call \verb|lua_open| when they are opened. 1336All standard libraries call \verb|lua_open| when they are opened.
1328 1337
1329Function \verb|lua_setstate| is used to change the current state 1338Function \verb|lua_setstate| is used to change the current state
1330of Lua:\Deffunc{lua_setstate} 1339of Lua:\Deffunc{lua_setstate}
1331\begin{verbatim} 1340\begin{verbatim}
1332lua_State *lua_setstate (lua_State *st); 1341lua_State *lua_setstate (lua_State *st);
1333\end{verbatim} 1342\end{verbatim}
1334It sets \verb|lua_state| to \verb|st| and returns the old state. 1343It sets \verb|lua_state| to \verb|st| and returns the old state.
1335 1344
1336Multiple, independent, states may be created. 1345Multiple, independent states may be created.
1337For that, you must set \verb|lua_state| back to \verb|NULL| before 1346For that, you must set \verb|lua_state| back to \verb|NULL| before
1338calling \verb|lua_open|. 1347calling \verb|lua_open|.
1339An easy way to do that is defining an auxiliary function: 1348An easy way to do that is defining an auxiliary function:
@@ -1346,13 +1355,13 @@ lua_State *lua_newstate (void) {
1346\end{verbatim} 1355\end{verbatim}
1347This function creates a new state without changing the current state 1356This function creates a new state without changing the current state
1348of the interpreter. 1357of the interpreter.
1349Notice that any new state is built with all predefined functions; 1358Note that any new state is built with all predefined functions,
1350any additional library (such as the standard libraries) must be 1359but any additional library (such as the standard libraries) must be
1351explicitly open in the new state, if needed. 1360explicitly open in the new state, if needed.
1352 1361
1353If necessary, a state may be released:\Deffunc{lua_close} 1362If necessary, a state may be released:\Deffunc{lua_close}
1354\begin{verbatim} 1363\begin{verbatim}
1355void lua_close (void); 1364void lua_close (void);
1356\end{verbatim} 1365\end{verbatim}
1357This function destroys all objects in the current Lua environment 1366This function destroys all objects in the current Lua environment
1358(calling the correspondent garbage collector tag methods), 1367(calling the correspondent garbage collector tag methods),
@@ -1361,7 +1370,7 @@ and then sets \verb|lua_state| to \verb|NULL|.
1361Usually, there is no need to call this function, 1370Usually, there is no need to call this function,
1362since these resources are naturally released when the program ends. 1371since these resources are naturally released when the program ends.
1363If \verb|lua_state| is already \verb|NULL|, 1372If \verb|lua_state| is already \verb|NULL|,
1364this function has no effect. 1373\verb|lua_close| has no effect.
1365 1374
1366If you are using multiple states, 1375If you are using multiple states,
1367you may find useful the following function, 1376you may find useful the following function,
@@ -1389,13 +1398,13 @@ the following functions are available:
1389\Deffunc{lua_istable}\Deffunc{lua_iscfunction}\Deffunc{lua_isuserdata} 1398\Deffunc{lua_istable}\Deffunc{lua_iscfunction}\Deffunc{lua_isuserdata}
1390\Deffunc{lua_isfunction} 1399\Deffunc{lua_isfunction}
1391\begin{verbatim} 1400\begin{verbatim}
1392int lua_isnil (lua_Object object); 1401int lua_isnil (lua_Object object);
1393int lua_isnumber (lua_Object object); 1402int lua_isnumber (lua_Object object);
1394int lua_isstring (lua_Object object); 1403int lua_isstring (lua_Object object);
1395int lua_istable (lua_Object object); 1404int lua_istable (lua_Object object);
1396int lua_isfunction (lua_Object object); 1405int lua_isfunction (lua_Object object);
1397int lua_iscfunction (lua_Object object); 1406int lua_iscfunction (lua_Object object);
1398int lua_isuserdata (lua_Object object); 1407int lua_isuserdata (lua_Object object);
1399\end{verbatim} 1408\end{verbatim}
1400All macros return 1 if the object is compatible with the given type, 1409All macros return 1 if the object is compatible with the given type,
1401and 0 otherwise. 1410and 0 otherwise.
@@ -1408,7 +1417,7 @@ To get the tag of a \verb|lua_Object|,
1408the following function is available: 1417the following function is available:
1409\Deffunc{lua_tag} 1418\Deffunc{lua_tag}
1410\begin{verbatim} 1419\begin{verbatim}
1411int lua_tag (lua_Object object); 1420int lua_tag (lua_Object object);
1412\end{verbatim} 1421\end{verbatim}
1413 1422
1414To translate a value from type \verb|lua_Object| to a specific C type, 1423To translate a value from type \verb|lua_Object| to a specific C type,
@@ -1416,16 +1425,16 @@ the programmer can use:
1416\Deffunc{lua_getnumber}\Deffunc{lua_getstring}\Deffunc{lua_strlen} 1425\Deffunc{lua_getnumber}\Deffunc{lua_getstring}\Deffunc{lua_strlen}
1417\Deffunc{lua_getcfunction}\Deffunc{lua_getuserdata} 1426\Deffunc{lua_getcfunction}\Deffunc{lua_getuserdata}
1418\begin{verbatim} 1427\begin{verbatim}
1419double lua_getnumber (lua_Object object); 1428double lua_getnumber (lua_Object object);
1420char *lua_getstring (lua_Object object); 1429char *lua_getstring (lua_Object object);
1421long lua_strlen (lua_Object object); 1430long lua_strlen (lua_Object object);
1422lua_CFunction lua_getcfunction (lua_Object object); 1431lua_CFunction lua_getcfunction (lua_Object object);
1423void *lua_getuserdata (lua_Object object); 1432void *lua_getuserdata (lua_Object object);
1424\end{verbatim} 1433\end{verbatim}
1425 1434
1426\verb|lua_getnumber| converts a \verb|lua_Object| to a floating-point number. 1435\verb|lua_getnumber| converts a \verb|lua_Object| to a floating-point number.
1427This \verb|lua_Object| must be a number or a string convertible to number 1436This \verb|lua_Object| must be a number or a string convertible to number
1428\see{coercion}; otherwise, the function returns~0. 1437\see{coercion}; otherwise, \verb|lua_getnumber| returns~0.
1429 1438
1430\verb|lua_getstring| converts a \verb|lua_Object| to a string (\verb|char*|). 1439\verb|lua_getstring| converts a \verb|lua_Object| to a string (\verb|char*|).
1431This \verb|lua_Object| must be a string or a number; 1440This \verb|lua_Object| must be a string or a number;
@@ -1435,19 +1444,19 @@ but returns a pointer to a string inside the Lua environment.
1435Those strings always have a 0 after their last character (like in C), 1444Those strings always have a 0 after their last character (like in C),
1436but may contain other zeros in their body. 1445but may contain other zeros in their body.
1437If you do not know whether a string may contain zeros, 1446If you do not know whether a string may contain zeros,
1438you can use \verb|lua_strlen| to check the actual length. 1447you can use \verb|lua_strlen| to get the actual length.
1439Because Lua has garbage collection, 1448Because Lua has garbage collection,
1440there is no guarantee that such pointer will be valid after the block ends 1449there is no guarantee that such pointer will be valid after the block ends
1441\see{GC}. 1450\see{GC}.
1442 1451
1443\verb|lua_getcfunction| converts a \verb|lua_Object| to a C function. 1452\verb|lua_getcfunction| converts a \verb|lua_Object| to a C function.
1444This \verb|lua_Object| must have type \emph{CFunction}; 1453This \verb|lua_Object| must have type \emph{CFunction};
1445otherwise, the function returns 0 (the \verb|NULL| pointer). 1454otherwise, \verb|lua_getcfunction| returns 0 (the \verb|NULL| pointer).
1446The type \verb|lua_CFunction| is explained in \See{LuacallC}. 1455The type \verb|lua_CFunction| is explained in \See{LuacallC}.
1447 1456
1448\verb|lua_getuserdata| converts a \verb|lua_Object| to \verb|void*|. 1457\verb|lua_getuserdata| converts a \verb|lua_Object| to \verb|void*|.
1449This \verb|lua_Object| must have type \emph{userdata}; 1458This \verb|lua_Object| must have type \emph{userdata};
1450otherwise, the function returns 0 (the \verb|NULL| pointer). 1459otherwise, \verb|lua_getuserdata| returns 0 (the \verb|NULL| pointer).
1451 1460
1452\subsection{Garbage Collection}\label{GC} 1461\subsection{Garbage Collection}\label{GC}
1453Because Lua has automatic memory management and garbage collection, 1462Because Lua has automatic memory management and garbage collection,
@@ -1462,12 +1471,12 @@ and never to store \verb|lua_Object|s in C global variables.
1462A garbage collection cycle can be forced by: 1471A garbage collection cycle can be forced by:
1463\Deffunc{lua_collectgarbage} 1472\Deffunc{lua_collectgarbage}
1464\begin{verbatim} 1473\begin{verbatim}
1465long lua_collectgarbage (long limit); 1474long lua_collectgarbage (long limit);
1466\end{verbatim} 1475\end{verbatim}
1467This function returns the number of objects collected. 1476This function returns the number of objects collected.
1468The argument \verb|limit| makes the next cycle occur only 1477The argument \verb|limit| makes the next cycle occur only
1469after that number of new objects have been created. 1478after that number of new objects have been created.
1470If \verb|limit|=0, then Lua uses an adaptable heuristics to set this limit. 1479If \verb|limit|=0, then Lua uses an adaptive heuristics to set this limit.
1471 1480
1472 1481
1473All communication between Lua and C is done through two 1482All communication between Lua and C is done through two
@@ -1482,7 +1491,7 @@ The structure lua2C is an abstract array,
1482which can be indexed with the function: 1491which can be indexed with the function:
1483\Deffunc{lua_lua2C} 1492\Deffunc{lua_lua2C}
1484\begin{verbatim} 1493\begin{verbatim}
1485lua_Object lua_lua2C (int number); 1494lua_Object lua_lua2C (int number);
1486\end{verbatim} 1495\end{verbatim}
1487where \verb|number| starts with 1. 1496where \verb|number| starts with 1.
1488When called with a number larger than the array size, 1497When called with a number larger than the array size,
@@ -1490,7 +1499,7 @@ this function returns \verb|LUA_NOOBJECT|\Deffunc{LUA_NOOBJECT}.
1490In this way, it is possible to write C functions that receive 1499In this way, it is possible to write C functions that receive
1491a variable number of parameters, 1500a variable number of parameters,
1492and to call Lua functions that return a variable number of results. 1501and to call Lua functions that return a variable number of results.
1493Notice that the structure lua2C cannot be directly modified by C code. 1502Note that the structure lua2C cannot be directly modified by C code.
1494 1503
1495The second structure, C2lua, is an abstract stack. 1504The second structure, C2lua, is an abstract stack.
1496Pushing elements into this stack 1505Pushing elements into this stack
@@ -1500,13 +1509,13 @@ is done with the following functions and macros:
1500\Deffunc{lua_pushnil}\Deffunc{lua_pushobject} 1509\Deffunc{lua_pushnil}\Deffunc{lua_pushobject}
1501\Deffunc{lua_pushuserdata}\label{pushing} 1510\Deffunc{lua_pushuserdata}\label{pushing}
1502\begin{verbatim} 1511\begin{verbatim}
1503void lua_pushnumber (double n); 1512void lua_pushnumber (double n);
1504void lua_pushlstring (char *s, long len); 1513void lua_pushlstring (char *s, long len);
1505void lua_pushstring (char *s); 1514void lua_pushstring (char *s);
1506void lua_pushusertag (void *u, int tag); 1515void lua_pushusertag (void *u, int tag);
1507void lua_pushnil (void); 1516void lua_pushnil (void);
1508void lua_pushobject (lua_Object object); 1517void lua_pushobject (lua_Object object);
1509void lua_pushcfunction (lua_CFunction f); /* macro */ 1518void lua_pushcfunction (lua_CFunction f); /* macro */
1510\end{verbatim} 1519\end{verbatim}
1511All of them receive a C value, 1520All of them receive a C value,
1512convert it to a corresponding \verb|lua_Object|, 1521convert it to a corresponding \verb|lua_Object|,
@@ -1514,12 +1523,12 @@ and leave the result on the top of C2lua.
1514Particularly, functions \verb|lua_pushlstring| and \verb|lua_pushstring| 1523Particularly, functions \verb|lua_pushlstring| and \verb|lua_pushstring|
1515make an internal copy of the given string. 1524make an internal copy of the given string.
1516Function \verb|lua_pushstring| can only be used to push proper C strings 1525Function \verb|lua_pushstring| can only be used to push proper C strings
1517(that is, strings which do not contain zeros and end with a zero); 1526(that is, strings that do not contain zeros and end with a zero);
1518otherwise you can use the more generic \verb|lua_pushlstring|. 1527otherwise you should use the more generic \verb|lua_pushlstring|.
1519The function 1528The function
1520\Deffunc{lua_pop} 1529\Deffunc{lua_pop}
1521\begin{verbatim} 1530\begin{verbatim}
1522lua_Object lua_pop (void); 1531lua_Object lua_pop (void);
1523\end{verbatim} 1532\end{verbatim}
1524returns a reference to the object at the top of the C2lua stack, 1533returns a reference to the object at the top of the C2lua stack,
1525and pops it. 1534and pops it.
@@ -1533,25 +1542,25 @@ If Lua has a userdata with the given value (\verb|void*|) and tag,
1533that userdata is pushed. 1542that userdata is pushed.
1534Otherwise, a new userdata is created, with the given value and tag. 1543Otherwise, a new userdata is created, with the given value and tag.
1535If this function is called with 1544If this function is called with
1536\verb|tag|=\verb|LUA_ANYTAG|\Deffunc{LUA_ANYTAG}, 1545\verb|tag| equal to \verb|LUA_ANYTAG|\Deffunc{LUA_ANYTAG},
1537then Lua will try to find any userdata with the given value, 1546then Lua will try to find any userdata with the given value,
1538regardless of its tag. 1547regardless of its tag.
1539If there is no userdata with that value, then a new one is created, 1548If there is no userdata with that value, then a new one is created,
1540with tag equals to 0. 1549with tag equal to 0.
1541 1550
1542Userdata can have different tags, 1551Userdata can have different tags,
1543whose semantics are only known to the host program. 1552whose semantics are only known to the host program.
1544Tags are created with the function: 1553Tags are created with the function:
1545\Deffunc{lua_newtag} 1554\Deffunc{lua_newtag}
1546\begin{verbatim} 1555\begin{verbatim}
1547int lua_newtag (void); 1556int lua_newtag (void);
1548\end{verbatim} 1557\end{verbatim}
1549The function \verb|lua_settag| changes the tag of 1558The function \verb|lua_settag| changes the tag of
1550the object on the top of C2lua (and pops it); 1559the object on the top of C2lua (and pops it);
1551the object must be a userdata or a table. 1560the object must be a userdata or a table.
1552\Deffunc{lua_settag} 1561\Deffunc{lua_settag}
1553\begin{verbatim} 1562\begin{verbatim}
1554void lua_settag (int tag); 1563void lua_settag (int tag);
1555\end{verbatim} 1564\end{verbatim}
1556\verb|tag| must be a value created with \verb|lua_newtag|. 1565\verb|tag| must be a value created with \verb|lua_newtag|.
1557 1566
@@ -1561,16 +1570,17 @@ and may cause a stack overflow.
1561To avoid this, 1570To avoid this,
1562nested blocks can be defined with the functions: 1571nested blocks can be defined with the functions:
1563\begin{verbatim} 1572\begin{verbatim}
1564void lua_beginblock (void); 1573void lua_beginblock (void);
1565void lua_endblock (void); 1574void lua_endblock (void);
1566\end{verbatim} 1575\end{verbatim}
1567After the end of the block, 1576After the end of the block,
1568all \verb|lua_Object|'s created inside it are released. 1577all \verb|lua_Object|'s created inside it are released.
1569The use of explicit nested blocks is strongly encouraged. 1578The use of explicit nested blocks is good progamming practice
1579and is strongly encouraged.
1570 1580
1571\subsection{Executing Lua Code} 1581\subsection{Executing Lua Code}
1572A host program can execute Lua chunks written in a file or in a string 1582A host program can execute Lua chunks written in a file or in a string
1573using the following functions: 1583using the following functions:%
1574\Deffunc{lua_dofile}\Deffunc{lua_dostring}\Deffunc{lua_dobuffer} 1584\Deffunc{lua_dofile}\Deffunc{lua_dostring}\Deffunc{lua_dobuffer}
1575\begin{verbatim} 1585\begin{verbatim}
1576int lua_dofile (char *filename); 1586int lua_dofile (char *filename);
@@ -1581,8 +1591,8 @@ All these functions return an error code:
15810, in case of success; non zero, in case of errors. 15910, in case of success; non zero, in case of errors.
1582More specifically, \verb|lua_dofile| returns 2 if for any reason 1592More specifically, \verb|lua_dofile| returns 2 if for any reason
1583it could not open the file. 1593it could not open the file.
1584The function \verb|lua_dofile|, if called with argument \verb|NULL|, 1594When called with argument \verb|NULL|,
1585executes the \verb|stdin| stream. 1595\verb|lua_dofile| executes the \verb|stdin| stream.
1586Functions \verb|lua_dofile| and \verb|lua_dobuffer| 1596Functions \verb|lua_dofile| and \verb|lua_dobuffer|
1587are both able to execute pre-compiled chunks. 1597are both able to execute pre-compiled chunks.
1588They automatically detect whether the chunk is text or binary, 1598They automatically detect whether the chunk is text or binary,
@@ -1606,47 +1616,47 @@ To read the value of any global Lua variable,
1606one uses the function: 1616one uses the function:
1607\Deffunc{lua_getglobal} 1617\Deffunc{lua_getglobal}
1608\begin{verbatim} 1618\begin{verbatim}
1609lua_Object lua_getglobal (char *varname); 1619lua_Object lua_getglobal (char *varname);
1610\end{verbatim} 1620\end{verbatim}
1611As in Lua, this function may trigger a tag method. 1621As in Lua, this function may trigger a tag method.
1612To read the real value of any global variable, 1622To read the real value of any global variable,
1613without invoking any tag method, 1623without invoking any tag method,
1614this function has a \emph{raw} version: 1624use the \emph{raw} version:
1615\Deffunc{lua_rawgetglobal} 1625\Deffunc{lua_rawgetglobal}
1616\begin{verbatim} 1626\begin{verbatim}
1617lua_Object lua_rawgetglobal (char *varname); 1627lua_Object lua_rawgetglobal (char *varname);
1618\end{verbatim} 1628\end{verbatim}
1619 1629
1620To store a value previously pushed onto C2lua in a global variable, 1630To store a value previously pushed onto C2lua in a global variable,
1621there is the function: 1631there is the function:
1622\Deffunc{lua_setglobal} 1632\Deffunc{lua_setglobal}
1623\begin{verbatim} 1633\begin{verbatim}
1624void lua_setglobal (char *varname); 1634void lua_setglobal (char *varname);
1625\end{verbatim} 1635\end{verbatim}
1626As in Lua, this function may trigger a tag method. 1636As in Lua, this function may trigger a tag method.
1627To set the real value of any global variable, 1637To set the real value of any global variable,
1628without invoking any tag method, 1638without invoking any tag method,
1629this function has a \emph{raw} version: 1639use the \emph{raw} version:
1630\Deffunc{lua_rawgetglobal} 1640\Deffunc{lua_rawgetglobal}
1631\begin{verbatim} 1641\begin{verbatim}
1632void lua_rawsetglobal (char *varname); 1642void lua_rawsetglobal (char *varname);
1633\end{verbatim} 1643\end{verbatim}
1634 1644
1635Tables can also be manipulated via the API. 1645Tables can also be manipulated via the API.
1636The function 1646The function
1637\Deffunc{lua_gettable} 1647\Deffunc{lua_gettable}
1638\begin{verbatim} 1648\begin{verbatim}
1639lua_Object lua_gettable (void); 1649lua_Object lua_gettable (void);
1640\end{verbatim} 1650\end{verbatim}
1641pops from the stack C2lua a table and an index, 1651pops from the stack C2lua a table and an index,
1642and returns the contents of the table at that index. 1652and returns the contents of the table at that index.
1643As in Lua, this operation may trigger a tag method. 1653As in Lua, this operation may trigger a tag method.
1644To get the real value of any table index, 1654To get the real value of any table index,
1645without invoking any tag method, 1655without invoking any tag method,
1646this function has a \emph{raw} version: 1656use the \emph{raw} version:
1647\Deffunc{lua_rawgetglobal} 1657\Deffunc{lua_rawgetglobal}
1648\begin{verbatim} 1658\begin{verbatim}
1649lua_Object lua_rawgettable (void); 1659lua_Object lua_rawgettable (void);
1650\end{verbatim} 1660\end{verbatim}
1651 1661
1652To store a value in an index, 1662To store a value in an index,
@@ -1655,35 +1665,35 @@ and the value onto C2lua,
1655and then call the function: 1665and then call the function:
1656\Deffunc{lua_settable} 1666\Deffunc{lua_settable}
1657\begin{verbatim} 1667\begin{verbatim}
1658void lua_settable (void); 1668void lua_settable (void);
1659\end{verbatim} 1669\end{verbatim}
1660Again, the tag method for ``settable'' may be called. 1670Again, the tag method for ``settable'' may be called.
1661To set the real value of any table index, 1671To set the real value of any table index,
1662without invoking any tag method, 1672without invoking any tag method,
1663this function has a \emph{raw} version: 1673use the \emph{raw} version:
1664\Deffunc{lua_rawsettable} 1674\Deffunc{lua_rawsettable}
1665\begin{verbatim} 1675\begin{verbatim}
1666void lua_rawsettable (void); 1676void lua_rawsettable (void);
1667\end{verbatim} 1677\end{verbatim}
1668 1678
1669Finally, the function 1679Finally, the function
1670\Deffunc{lua_createtable} 1680\Deffunc{lua_createtable}
1671\begin{verbatim} 1681\begin{verbatim}
1672lua_Object lua_createtable (void); 1682lua_Object lua_createtable (void);
1673\end{verbatim} 1683\end{verbatim}
1674creates and returns a new, empty table. 1684creates and returns a new, empty table.
1675 1685
1676 1686
1677\subsection{Calling Lua Functions} 1687\subsection{Calling Lua Functions}
1678Functions defined in Lua by a chunk executed with 1688Functions defined in Lua by a chunk
1679\verb|dofile| or \verb|dostring| can be called from the host program. 1689can be called from the host program.
1680This is done using the following protocol: 1690This is done using the following protocol:
1681first, the arguments to the function are pushed onto C2lua 1691first, the arguments to the function are pushed onto C2lua
1682\see{pushing}, in direct order, i.e., the first argument is pushed first. 1692\see{pushing}, in direct order, i.e., the first argument is pushed first.
1683Then, the function is called using 1693Then, the function is called using
1684\Deffunc{lua_callfunction} 1694\Deffunc{lua_callfunction}
1685\begin{verbatim} 1695\begin{verbatim}
1686int lua_callfunction (lua_Object function); 1696int lua_callfunction (lua_Object function);
1687\end{verbatim} 1697\end{verbatim}
1688This function returns an error code: 1698This function returns an error code:
16890, in case of success; non zero, in case of errors. 16990, in case of success; non zero, in case of errors.
@@ -1691,24 +1701,26 @@ Finally, the results (a Lua function may return many values)
1691are returned in structure lua2C, 1701are returned in structure lua2C,
1692and can be retrieved with the macro \verb|lua_getresult|, 1702and can be retrieved with the macro \verb|lua_getresult|,
1693\Deffunc{lua_getresult} 1703\Deffunc{lua_getresult}
1694which is just another name to the function \verb|lua_lua2C|. 1704which is just another name to function \verb|lua_lua2C|.
1695Notice that the function \verb|lua_callfunction| 1705Note that function \verb|lua_callfunction|
1696pops all elements from the C2lua stack. 1706pops all elements from the C2lua stack.
1697 1707
1698The following example shows how a C program may do the 1708The following example shows how a C program may do the
1699equivalent to the Lua code: 1709equivalent to the Lua code:
1700\begin{verbatim} 1710\begin{verbatim}
1701 a = f("how", t.x, 4) 1711 a,b = f("how", t.x, 4)
1702\end{verbatim} 1712\end{verbatim}
1703\begin{verbatim} 1713\begin{verbatim}
1704 lua_pushstring("how"); /* 1st argument */ 1714 lua_pushstring("how"); /* 1st argument */
1705 lua_pushobject(lua_getglobal("t")); /* push value of global 't' */ 1715 lua_pushobject(lua_getglobal("t")); /* push value of global 't' */
1706 lua_pushstring("x"); /* push the string 'x' */ 1716 lua_pushstring("x"); /* push the string 'x' */
1707 lua_pushobject(lua_gettable()); /* push result of t.x (2nd arg) */ 1717 lua_pushobject(lua_gettable()); /* push result of t.x (2nd arg) */
1708 lua_pushnumber(4); /* 3th argument */ 1718 lua_pushnumber(4); /* 3rd argument */
1709 lua_callfunction(lua_getglobal("f")); /* call Lua function */ 1719 lua_callfunction(lua_getglobal("f")); /* call Lua function */
1710 lua_pushobject(lua_getresult(1)); /* push first result of the call */ 1720 lua_pushobject(lua_getresult(1)); /* push first result of the call */
1711 lua_setglobal("a"); /* sets global variable 'a' */ 1721 lua_setglobal("a"); /* sets global variable 'a' */
1722 lua_pushobject(lua_getresult(2)); /* push second result of the call */
1723 lua_setglobal("b"); /* sets global variable 'b' */
1712\end{verbatim} 1724\end{verbatim}
1713 1725
1714Some special Lua functions have exclusive interfaces. 1726Some special Lua functions have exclusive interfaces.
@@ -1726,36 +1738,34 @@ The \verb|message| is passed to the error handler method.
1726If \verb|message| is \verb|NULL|, 1738If \verb|message| is \verb|NULL|,
1727the error handler method is not called. 1739the error handler method is not called.
1728 1740
1729The error handler method \see{error} can be changed with: 1741The error handler method \see{error} can be
1730\Deffunc{lua_seterrormethod} 1742changed with: \Deffunc{lua_seterrormethod}
1731\begin{verbatim} 1743\begin{verbatim}
1732lua_Object lua_seterrormethod (void); 1744lua_Object lua_seterrormethod (void);
1733\end{verbatim} 1745\end{verbatim}
1734This function sets the object at the top of C2lua 1746This function sets the object at the top of C2lua
1735as the new error method, 1747as the new error method,
1736and returns the old error method value. 1748and returns the old error method value.
1737 1749
1738Tag methods can be changed with: 1750Tag methods can be changed with: \Deffunc{lua_settagmethod}
1739\Deffunc{lua_settagmethod}
1740\begin{verbatim} 1751\begin{verbatim}
1741lua_Object lua_settagmethod (int tag, char *event); 1752lua_Object lua_settagmethod (int tag, char *event);
1742\end{verbatim} 1753\end{verbatim}
1743The first parameter is the tag, 1754The first parameter is the tag,
1744the second is the event name \see{tag-method}; 1755and the second is the event name \see{tag-method};
1745the new method is pushed from C2lua. 1756the new method is pushed from C2lua.
1746This function returns a \verb|lua_Object|, 1757This function returns a \verb|lua_Object|,
1747which is the old tag method value. 1758which is the old tag method value.
1748To get just the current value of a tag method, 1759To get just the current value of a tag method,
1749there is the function 1760use the function \Deffunc{lua_gettagmethod}
1750\Deffunc{lua_gettagmethod}
1751\begin{verbatim} 1761\begin{verbatim}
1752lua_Object lua_gettagmethod (int tag, char *event); 1762lua_Object lua_gettagmethod (int tag, char *event);
1753\end{verbatim} 1763\end{verbatim}
1754 1764
1755It is also possible to copy all tag methods from one tag to another: 1765It is also possible to copy all tag methods from one tag
1756\Deffunc{lua_copytagmethods} 1766to another: \Deffunc{lua_copytagmethods}
1757\begin{verbatim} 1767\begin{verbatim}
1758int lua_copytagmethods (int tagto, int tagfrom); 1768int lua_copytagmethods (int tagto, int tagfrom);
1759\end{verbatim} 1769\end{verbatim}
1760This function returns \verb|tagto|. 1770This function returns \verb|tagto|.
1761 1771
@@ -1798,15 +1808,15 @@ as common arguments.
1798To associate upvalues to a function, 1808To associate upvalues to a function,
1799first these values must be pushed on C2lua. 1809first these values must be pushed on C2lua.
1800Then the function: 1810Then the function:
1801\Deffunc{lua_pushCclosure} 1811\Deffunc{lua_pushcclosure}
1802\begin{verbatim} 1812\begin{verbatim}
1803void lua_pushCclosure (lua_CFunction fn, int n); 1813void lua_pushcclosure (lua_CFunction fn, int n);
1804\end{verbatim} 1814\end{verbatim}
1805is used to put the C function on C2lua, 1815is used to put the C function on C2lua,
1806with the argument \verb|n| telling how many upvalues must be 1816with the argument \verb|n| telling how many upvalues must be
1807associated with the function; 1817associated with the function;
1808in fact, the macro \verb|lua_pushcfunction| is defined as 1818in fact, the macro \verb|lua_pushcfunction| is defined as
1809\verb|lua_pushCclosure| with \verb|n| set to 0. 1819\verb|lua_pushcclosure| with \verb|n| set to 0.
1810Then, any time the function is called, 1820Then, any time the function is called,
1811these upvalues are inserted as the first arguments to the function, 1821these upvalues are inserted as the first arguments to the function,
1812before the actual arguments provided in the call. 1822before the actual arguments provided in the call.
@@ -1824,16 +1834,16 @@ The routines to manipulate references are the following:
1824\Deffunc{lua_ref}\Deffunc{lua_getref} 1834\Deffunc{lua_ref}\Deffunc{lua_getref}
1825\Deffunc{lua_unref} 1835\Deffunc{lua_unref}
1826\begin{verbatim} 1836\begin{verbatim}
1827int lua_ref (int lock); 1837int lua_ref (int lock);
1828lua_Object lua_getref (int ref); 1838lua_Object lua_getref (int ref);
1829void lua_unref (int ref); 1839void lua_unref (int ref);
1830\end{verbatim} 1840\end{verbatim}
1831The function \verb|lua_ref| creates a reference 1841The function \verb|lua_ref| creates a reference
1832to the object that is on the top of the stack, 1842to the object that is on the top of the stack,
1833and returns this reference. 1843and returns this reference.
1834If \verb|lock| is true, the object is \emph{locked}: 1844If \verb|lock| is true, the object is \emph{locked}:
1835this means the object will not be garbage collected. 1845this means the object will not be garbage collected.
1836Notice that an unlocked reference may be garbage collected. 1846Note that an unlocked reference may be garbage collected.
1837Whenever the referenced object is needed, 1847Whenever the referenced object is needed,
1838a call to \verb|lua_getref| 1848a call to \verb|lua_getref|
1839returns a handle to it; 1849returns a handle to it;
@@ -1868,7 +1878,7 @@ To have access to these libraries,
1868the C host program must call the functions 1878the C host program must call the functions
1869\verb|lua_strlibopen|, \verb|lua_mathlibopen|, 1879\verb|lua_strlibopen|, \verb|lua_mathlibopen|,
1870and \verb|lua_iolibopen|, declared in \verb|lualib.h|. 1880and \verb|lua_iolibopen|, declared in \verb|lualib.h|.
1871\Deffunc{lua_strlibopen}\Deffunc{lua_mathlibopen}\Deffunc{lua_iolibopen}. 1881\Deffunc{lua_strlibopen}\Deffunc{lua_mathlibopen}\Deffunc{lua_iolibopen}
1872 1882
1873 1883
1874\subsection{Predefined Functions} \label{predefined} 1884\subsection{Predefined Functions} \label{predefined}
@@ -1882,7 +1892,7 @@ The call is equivalent to
1882 func(arg[1], arg[2], ..., arg[arg.n]) 1892 func(arg[1], arg[2], ..., arg[arg.n])
1883\end{verbatim} 1893\end{verbatim}
1884If \verb|arg.n| is not defined, 1894If \verb|arg.n| is not defined,
1885then Lua stops getting arguments at the first nil value. 1895then Lua stops getting arguments at the first \nil\ value.
1886 1896
1887By default, 1897By default,
1888all results from \verb|func| are just returned by the call. 1898all results from \verb|func| are just returned by the call.
@@ -1921,7 +1931,7 @@ Returns the number of objects collected.
1921An optional argument, \verb|limit|, is a number that 1931An optional argument, \verb|limit|, is a number that
1922makes the next cycle occur only after that number of new 1932makes the next cycle occur only after that number of new
1923objects have been created. 1933objects have been created.
1924If absent, Lua uses an adaptable algorithm to set 1934If absent, Lua uses an adaptive algorithm to set
1925this limit. 1935this limit.
1926\verb|collectgarbage| is equivalent to 1936\verb|collectgarbage| is equivalent to
1927the API function \verb|lua_collectgarbage|. 1937the API function \verb|lua_collectgarbage|.
@@ -1941,7 +1951,8 @@ It issues an error when called with a non string argument.
1941 1951
1942\subsubsection*{\ff \T{dostring (string)}}\Deffunc{dostring} 1952\subsubsection*{\ff \T{dostring (string)}}\Deffunc{dostring}
1943This function executes a given string as a Lua chunk. 1953This function executes a given string as a Lua chunk.
1944If there is any error executing the string, it returns \nil. 1954If there is any error executing the string,
1955\verb|dostring| returns \nil.
1945Otherwise, it returns the values returned by the chunk, 1956Otherwise, it returns the values returned by the chunk,
1946or a non \nil\ value if the chunk returns no values. 1957or a non \nil\ value if the chunk returns no values.
1947\verb|dostring| is equivalent to the API function \verb|lua_dostring|. 1958\verb|dostring| is equivalent to the API function \verb|lua_dostring|.
@@ -1977,7 +1988,7 @@ This function cannot be written with the standard API.
1977 1988
1978\subsubsection*{\ff \T{nextvar (name)}}\Deffunc{nextvar} 1989\subsubsection*{\ff \T{nextvar (name)}}\Deffunc{nextvar}
1979This function is similar to the function \verb|next|, 1990This function is similar to the function \verb|next|,
1980but iterates over the global variables. 1991but iterates instead over the global variables.
1981Its single argument is the name of a global variable, 1992Its single argument is the name of a global variable,
1982or \nil\ to get a first name. 1993or \nil\ to get a first name.
1983Similarly to \verb|next|, it returns the name of another variable 1994Similarly to \verb|next|, it returns the name of another variable
@@ -1992,7 +2003,7 @@ This function cannot be written with the standard API.
1992Executes the given \verb|function| over all elements of \verb|table|. 2003Executes the given \verb|function| over all elements of \verb|table|.
1993For each element, the function is called with the index and 2004For each element, the function is called with the index and
1994respective value as arguments. 2005respective value as arguments.
1995If the function returns any non-nil value, 2006If the function returns any non-\nil\ value,
1996the loop is broken, and the value is returned 2007the loop is broken, and the value is returned
1997as the final value of \verb|foreach|. 2008as the final value of \verb|foreach|.
1998 2009
@@ -2031,12 +2042,14 @@ end
2031\subsubsection*{\ff \T{tostring (e)}}\Deffunc{tostring} 2042\subsubsection*{\ff \T{tostring (e)}}\Deffunc{tostring}
2032This function receives an argument of any type and 2043This function receives an argument of any type and
2033converts it to a string in a reasonable format. 2044converts it to a string in a reasonable format.
2045For complete control on how numbers are converted,
2046use function \verb|format|.
2034 2047
2035\subsubsection*{\ff \T{print (e1, e2, ...)}}\Deffunc{print} 2048\subsubsection*{\ff \T{print (e1, e2, ...)}}\Deffunc{print}
2036This function receives any number of arguments, 2049This function receives any number of arguments,
2037and prints their values using the strings returned by \verb|tostring|. 2050and prints their values using the strings returned by \verb|tostring|.
2038This function is not intended for formatted output, 2051This function is not intended for formatted output,
2039but as a quick way to show a value, 2052but only as a quick way to show a value,
2040for instance for error messages or debugging. 2053for instance for error messages or debugging.
2041See \See{libio} for functions for formatted output. 2054See \See{libio} for functions for formatted output.
2042 2055
@@ -2044,7 +2057,7 @@ See \See{libio} for functions for formatted output.
2044This function receives one argument, 2057This function receives one argument,
2045and tries to convert it to a number. 2058and tries to convert it to a number.
2046If the argument is already a number or a string convertible 2059If the argument is already a number or a string convertible
2047to a number, then it returns that number; 2060to a number, then \verb|tonumber| returns that number;
2048otherwise, it returns \nil. 2061otherwise, it returns \nil.
2049 2062
2050An optional argument specifies the base to interpret the numeral. 2063An optional argument specifies the base to interpret the numeral.
@@ -2054,7 +2067,7 @@ represents 10, `B' represents 11, and so forth, with `Z' representing 35.
2054 2067
2055In base 10 (the default), the number may have a decimal part, 2068In base 10 (the default), the number may have a decimal part,
2056as well as an optional exponent part \see{coercion}. 2069as well as an optional exponent part \see{coercion}.
2057In other bases only integers are accepted. 2070In other bases, only integers are accepted.
2058 2071
2059\subsubsection*{\ff \T{type (v)}}\Deffunc{type}\label{pdf-type} 2072\subsubsection*{\ff \T{type (v)}}\Deffunc{type}\label{pdf-type}
2060This function allows Lua to test the type of a value. 2073This function allows Lua to test the type of a value.
@@ -2097,10 +2110,11 @@ end
2097\subsubsection*{\ff \T{error (message)}}\Deffunc{error}\label{pdf-error} 2110\subsubsection*{\ff \T{error (message)}}\Deffunc{error}\label{pdf-error}
2098This function calls the error handler and then terminates 2111This function calls the error handler and then terminates
2099the last protected function called 2112the last protected function called
2100(in C: \verb|lua_dofile|, \verb|lua_dostring|, or \verb|lua_callfunction|; 2113(in~C: \verb|lua_dofile|, \verb|lua_dostring|,
2114\verb|lua_dobuffer|, or \verb|lua_callfunction|;
2101in Lua: \verb|dofile|, \verb|dostring|, or \verb|call| in protected mode). 2115in Lua: \verb|dofile|, \verb|dostring|, or \verb|call| in protected mode).
2102If \verb|message| is \nil, the error handler is not called. 2116If \verb|message| is \nil, the error handler is not called.
2103It never returns. 2117Function \verb|error| never returns.
2104\verb|error| is equivalent to the API function \verb|lua_error|. 2118\verb|error| is equivalent to the API function \verb|lua_error|.
2105 2119
2106\subsubsection*{\ff \T{rawgettable (table, index)}}\Deffunc{rawgettable} 2120\subsubsection*{\ff \T{rawgettable (table, index)}}\Deffunc{rawgettable}
@@ -2110,7 +2124,7 @@ without invoking any tag method.
2110and \verb|index| is any value different from \nil. 2124and \verb|index| is any value different from \nil.
2111 2125
2112\subsubsection*{\ff \T{rawsettable (table, index, value)}}\Deffunc{rawsettable} 2126\subsubsection*{\ff \T{rawsettable (table, index, value)}}\Deffunc{rawsettable}
2113Sets the real value \verb|table[index]=value|, 2127Sets the real value of \verb|table[index]| to \verb|value|,
2114without invoking any tag method. 2128without invoking any tag method.
2115\verb|table| must be a table, 2129\verb|table| must be a table,
2116\verb|index| is any value different from \nil, 2130\verb|index| is any value different from \nil,
@@ -2123,10 +2137,7 @@ syntactically valid variable name.
2123Therefore, 2137Therefore,
2124this function can set global variables with strange names like 2138this function can set global variables with strange names like
2125\verb|"m v 1"| or \verb|34|. 2139\verb|"m v 1"| or \verb|34|.
2126It returns the value of its second argument. 2140Function \verb|rawsetglobal| returns the value of its second argument.
2127
2128The string \verb|name| does not need to be a
2129syntactically valid variable name.
2130 2141
2131\subsubsection*{\ff \T{setglobal (name, value)}}\Deffunc{setglobal} 2142\subsubsection*{\ff \T{setglobal (name, value)}}\Deffunc{setglobal}
2132This function assigns the given value to a global variable, 2143This function assigns the given value to a global variable,
@@ -2134,6 +2145,7 @@ or calls a tag method.
2134Its full semantics is explained in \See{tag-method}. 2145Its full semantics is explained in \See{tag-method}.
2135The string \verb|name| does not need to be a 2146The string \verb|name| does not need to be a
2136syntactically valid variable name. 2147syntactically valid variable name.
2148Function \verb|setglobal| returns the value of its second argument.
2137 2149
2138\subsubsection*{\ff \T{rawgetglobal (name)}}\Deffunc{rawgetglobal} 2150\subsubsection*{\ff \T{rawgetglobal (name)}}\Deffunc{rawgetglobal}
2139This function retrieves the value of a global variable. 2151This function retrieves the value of a global variable.
@@ -2144,7 +2156,6 @@ syntactically valid variable name.
2144This function retrieves the value of a global variable, 2156This function retrieves the value of a global variable,
2145or calls a tag method. 2157or calls a tag method.
2146Its full semantics is explained in \See{tag-method}. 2158Its full semantics is explained in \See{tag-method}.
2147
2148The string \verb|name| does not need to be a 2159The string \verb|name| does not need to be a
2149syntactically valid variable name. 2160syntactically valid variable name.
2150 2161
@@ -2160,7 +2171,7 @@ Returns the old error handler.
2160This function sets a new tag method to the given pair \M{(tag, event)}. 2171This function sets a new tag method to the given pair \M{(tag, event)}.
2161It returns the old method. 2172It returns the old method.
2162If \verb|newmethod| is \nil, 2173If \verb|newmethod| is \nil,
2163it restores the default behavior for the given event. 2174\verb|settagmethod| restores the default behavior for the given event.
2164 2175
2165\subsubsection*{\ff \T{gettagmethod (tag, event)}} 2176\subsubsection*{\ff \T{gettagmethod (tag, event)}}
2166\Deffunc{gettagmethod} 2177\Deffunc{gettagmethod}
@@ -2193,7 +2204,7 @@ its default value is 1.
2193If \verb|init| is negative, 2204If \verb|init| is negative,
2194it is replaced by the length of the string minus its 2205it is replaced by the length of the string minus its
2195absolute value plus 1. 2206absolute value plus 1.
2196Therefore, \M{-1} points to the last character of \verb|str|. 2207Therefore, \Math{-1} points to the last character of \verb|str|.
2197A value of 1 as a fourth optional argument 2208A value of 1 as a fourth optional argument
2198turns off the pattern matching facilities, 2209turns off the pattern matching facilities,
2199so the function does a plain ``find substring'' operation, 2210so the function does a plain ``find substring'' operation,
@@ -2208,9 +2219,9 @@ starting at \verb|i| and running until \verb|j|.
2208If \verb|i| or \verb|j| are negative, 2219If \verb|i| or \verb|j| are negative,
2209they are replaced by the length of the string minus their 2220they are replaced by the length of the string minus their
2210absolute value plus 1. 2221absolute value plus 1.
2211Therefore, \M{-1} points to the last character of \verb|s| 2222Therefore, \Math{-1} points to the last character of \verb|s|
2212and \M{-2} to the previous one. 2223and \Math{-2} to the previous one.
2213If \verb|j| is absent, it is assumed to be equal to \M{-1} 2224If \verb|j| is absent, it is assumed to be equal to \Math{-1}
2214(which is the same as the string length). 2225(which is the same as the string length).
2215In particular, 2226In particular,
2216the call \verb|strsub(s,1,j)| returns a prefix of \verb|s| 2227the call \verb|strsub(s,1,j)| returns a prefix of \verb|s|
@@ -2242,7 +2253,9 @@ If \verb|i| is absent, then it is assumed to be 1.
2242If \verb|i| is negative, 2253If \verb|i| is negative,
2243it is replaced by the length of the string minus its 2254it is replaced by the length of the string minus its
2244absolute value plus 1. 2255absolute value plus 1.
2245Therefore, \M{-1} points to the last character of \verb|s|. 2256Therefore, \Math{-1} points to the last character of \verb|s|.
2257
2258Note that numerical codes are not necessarily portable across platforms.
2246 2259
2247\subsubsection*{\ff \T{strchar (i1, i2, \ldots)}}\Deffunc{strchar} 2260\subsubsection*{\ff \T{strchar (i1, i2, \ldots)}}\Deffunc{strchar}
2248Receives 0 or more integers. 2261Receives 0 or more integers.
@@ -2250,6 +2263,8 @@ Returns a string with length equal to the number of arguments,
2250wherein each character has the internal numerical code equal 2263wherein each character has the internal numerical code equal
2251to its correspondent argument. 2264to its correspondent argument.
2252 2265
2266Note that numerical codes are not necessarily portable across platforms.
2267
2253\subsubsection*{\ff \T{format (formatstring, e1, e2, \ldots)}}\Deffunc{format} 2268\subsubsection*{\ff \T{format (formatstring, e1, e2, \ldots)}}\Deffunc{format}
2254\label{format} 2269\label{format}
2255This function returns a formatted version of its variable number of arguments 2270This function returns a formatted version of its variable number of arguments
@@ -2295,7 +2310,7 @@ the appropriate format string.
2295For example, \verb|"%*g"| can be simulated with 2310For example, \verb|"%*g"| can be simulated with
2296\verb|"%"..width.."g"|. 2311\verb|"%"..width.."g"|.
2297 2312
2298\emph{Function \T{format} can only be used with strings that do not 2313\emph{Note: function \T{format} can only be used with strings that do not
2299contain zeros.} 2314contain zeros.}
2300 2315
2301\subsubsection*{\ff \T{gsub (s, pat, repl [, n])}} 2316\subsubsection*{\ff \T{gsub (s, pat, repl [, n])}}
@@ -2331,15 +2346,18 @@ See some examples below:
2331 x = gsub("hello world", "(%w%w*)", "%1 %1", 1) 2346 x = gsub("hello world", "(%w%w*)", "%1 %1", 1)
2332 --> x="hello hello world" 2347 --> x="hello hello world"
2333 2348
2349 x = gsub("hello world from Lua", "(%w%w*)%s*(%w%w*)", "%2 %1")
2350 --> x="world hello Lua from"
2351
2334 x = gsub("home = $HOME, user = $USER", "$(%w%w*)", getenv) 2352 x = gsub("home = $HOME, user = $USER", "$(%w%w*)", getenv)
2335 --> x="home = /home/roberto, user = roberto" (for instance) 2353 --> x="home = /home/roberto, user = roberto" (for instance)
2336 2354
2337 x = gsub("4+5 = $return 4+5$", "$(.-)%$", dostring) 2355 x = gsub("4+5 = $return 4+5$", "$(.-)%$", dostring)
2338 --> x="4+5 = 9" 2356 --> x="4+5 = 9"
2339 2357
2340 local t = {name="lua", version="3.0"} 2358 local t = {name="lua", version="3.1"}
2341 x = gsub("$name - $version", "$(%w%w*)", function (v) return %t[v] end) 2359 x = gsub("$name - $version", "$(%w%w*)", function (v) return %t[v] end)
2342 --> x="lua - 3.0" 2360 --> x="lua - 3.1"
2343 2361
2344 t = {n=0} 2362 t = {n=0}
2345 gsub("first second word", "(%w%w*)", 2363 gsub("first second word", "(%w%w*)",
@@ -2356,7 +2374,7 @@ The following combinations are allowed in describing a character class:
2356\begin{description} 2374\begin{description}
2357\item[\emph{x}] (where \emph{x} is any character not in the list \verb|()%.[*-?|) 2375\item[\emph{x}] (where \emph{x} is any character not in the list \verb|()%.[*-?|)
2358--- represents the character \emph{x} itself. 2376--- represents the character \emph{x} itself.
2359\item[\T{.}] --- represents all characters. 2377\item[\T{.}] --- (a dot) represents all characters.
2360\item[\T{\%a}] --- represents all letters. 2378\item[\T{\%a}] --- represents all letters.
2361\item[\T{\%A}] --- represents all non letter characters. 2379\item[\T{\%A}] --- represents all non letter characters.
2362\item[\T{\%d}] --- represents all digits. 2380\item[\T{\%d}] --- represents all digits.
@@ -2377,13 +2395,14 @@ Represents the class which is the union of all
2377characters in char-set. 2395characters in char-set.
2378To include a \verb|]| in char-set, it must be the first character. 2396To include a \verb|]| in char-set, it must be the first character.
2379A range of characters may be specified by 2397A range of characters may be specified by
2380separating the end characters of the range with a \verb|-|; 2398separating the end characters of the range with a \verb|-|.
2381e.g., \verb|A-Z| specifies the English upper case characters.
2382If \verb|-| appears as the first or last character of char-set, 2399If \verb|-| appears as the first or last character of char-set,
2383then it represents itself. 2400then it represents itself.
2384All classes \verb|%|\emph{x} described above can also be used as 2401All classes \verb|%|\emph{x} described above can also be used as
2385components in a char-set. 2402components in a char-set.
2386All other characters in char-set represent themselves. 2403All other characters in char-set represent themselves.
2404E.g., assuming an \emph{ascii} character set,
2405\verb|[%dA-Fa-f]| specifies the hexa-decimal digits.
2387\item[\T{[\^{ }char-set]}] --- 2406\item[\T{[\^{ }char-set]}] ---
2388represents the complement of char-set, 2407represents the complement of char-set,
2389where char-set is interpreted as above. 2408where char-set is interpreted as above.
@@ -2441,9 +2460,9 @@ that match captures are stored (\emph{captured}) for future use.
2441Captures are numbered according to their left parentheses. 2460Captures are numbered according to their left parentheses.
2442For instance, in the pattern \verb|"(a*(.)%w(%s*))"|, 2461For instance, in the pattern \verb|"(a*(.)%w(%s*))"|,
2443the part of the string matching \verb|"a*(.)%w(%s*)"| is 2462the part of the string matching \verb|"a*(.)%w(%s*)"| is
2444stored as the first capture (and therefore has number 1); 2463stored as the first capture (and therefore has number~1);
2445the character matching \verb|.| is captured with number 2, 2464the character matching \verb|.| is captured with number~2,
2446and the part matching \verb|%s*| has number 3. 2465and the part matching \verb|%s*| has number~3.
2447 2466
2448 2467
2449\subsection{Mathematical Functions} \label{mathlib} 2468\subsection{Mathematical Functions} \label{mathlib}
@@ -2540,7 +2559,7 @@ it opens the named file,
2540sets its handle as the value of \verb|_OUTPUT|, 2559sets its handle as the value of \verb|_OUTPUT|,
2541and returns this value. 2560and returns this value.
2542It does not close the current output file. 2561It does not close the current output file.
2543Notice that, if the file already exists, 2562Note that, if the file already exists,
2544then it will be \emph{completely erased} with this operation. 2563then it will be \emph{completely erased} with this operation.
2545When called without parameters, 2564When called without parameters,
2546this function closes the \verb|_OUTPUT| file, 2565this function closes the \verb|_OUTPUT| file,
@@ -2569,7 +2588,7 @@ this function does not erase any previous content of the file.
2569If this function fails, it returns \nil, 2588If this function fails, it returns \nil,
2570plus a string describing the error. 2589plus a string describing the error.
2571 2590
2572Notice that function \verb|writeto| is 2591Note that function \verb|writeto| is
2573available to close an output file opened by \verb|appendto|. 2592available to close an output file opened by \verb|appendto|.
2574 2593
2575\subsubsection*{\ff \T{remove (filename)}}\Deffunc{remove} 2594\subsubsection*{\ff \T{remove (filename)}}\Deffunc{remove}
@@ -2594,7 +2613,7 @@ The file must be explicitly removed when no longer needed.
2594 2613
2595This function reads the file \verb|_INPUT|, 2614This function reads the file \verb|_INPUT|,
2596or from \verb|filehandle| if this argument is given, 2615or from \verb|filehandle| if this argument is given,
2597according to a read pattern, that specifies how much to read; 2616according to a read pattern, which specifies how much to read;
2598characters are read from the input file until 2617characters are read from the input file until
2599the read pattern fails or ends. 2618the read pattern fails or ends.
2600The function \verb|read| returns a string with the characters read, 2619The function \verb|read| returns a string with the characters read,
@@ -2616,7 +2635,7 @@ it never fails.
2616A character class followed by \verb|*| reads until a character that 2635A character class followed by \verb|*| reads until a character that
2617does not belong to the class, or end of file; 2636does not belong to the class, or end of file;
2618since it can match a sequence of zero characters, it never fails. 2637since it can match a sequence of zero characters, it never fails.
2619Notice that the behavior of read patterns is slightly different from 2638Note that the behavior of read patterns is slightly different from
2620the regular pattern matching behavior, 2639the regular pattern matching behavior,
2621where a \verb|*| expands to the maximum length \emph{such that} 2640where a \verb|*| expands to the maximum length \emph{such that}
2622the rest of the pattern does not fail. 2641the rest of the pattern does not fail.
@@ -2677,7 +2696,7 @@ The default value for \verb|code| is 1.
2677 2696
2678\subsubsection*{\ff \T{getenv (varname)}}\Deffunc{getenv} 2697\subsubsection*{\ff \T{getenv (varname)}}\Deffunc{getenv}
2679 2698
2680Returns the value of the environment variable \verb|varname|, 2699Returns the value of the process environment variable \verb|varname|,
2681or \nil\ if the variable is not defined. 2700or \nil\ if the variable is not defined.
2682 2701
2683\subsubsection*{\ff \T{execute (command)}}\Deffunc{execute} 2702\subsubsection*{\ff \T{execute (command)}}\Deffunc{execute}
@@ -2690,10 +2709,10 @@ It returns an error code, which is system-dependent.
2690 2709
2691This function is an interface to the ANSI C function \verb|setlocale|. 2710This function is an interface to the ANSI C function \verb|setlocale|.
2692\verb|locale| is a string specifying a locale; 2711\verb|locale| is a string specifying a locale;
2693\verb|category| is a number describing which category to change: 2712\verb|category| is an optional string describing which category to change:
26940 is \verb|LC_ALL|, 1 is \verb|LC_COLLATE|, 2 is \verb|LC_CTYPE|, 2713\verb|"all"|, \verb|"collate"|, \verb|"ctype"|,
26953 is \verb|LC_MONETARY|, 4 is \verb|LC_NUMERIC|, and 5 is \verb|LC_TIME|; 2714\verb|"monetary"|, \verb|"numeric"|, or \verb|"time"|;
2696the default category is \verb|LC_ALL|. 2715the default category is \verb|"all"|.
2697The function returns the name of the new locale, 2716The function returns the name of the new locale,
2698or \nil\ if the request cannot be honored. 2717or \nil\ if the request cannot be honored.
2699 2718
@@ -2741,13 +2760,14 @@ given function has been defined.
2741If the ``function'' is in fact the main code of a chunk, 2760If the ``function'' is in fact the main code of a chunk,
2742then \verb|linedefined| is 0. 2761then \verb|linedefined| is 0.
2743If the function is a C function, 2762If the function is a C function,
2744then \verb|linedefined| is \M{-1}, and \verb|filename| is \verb|"(C)"|. 2763then \verb|linedefined| is \Math{-1}, and \verb|filename| is \verb|"(C)"|.
2745 2764
2746The function \verb|lua_currentline| gives the current line where 2765The function \verb|lua_currentline| gives the current line where
2747a given function is executing. 2766a given function is executing.
2748It only works if the function has been compiled with debug 2767It only works if the function has been compiled with debug
2749information \see{pragma}. 2768information \see{pragma}.
2750When no line information is available, it returns \M{-1}. 2769When no line information is available,
2770\verb|lua_currentline| returns \Math{-1}.
2751 2771
2752Function \verb|lua_getobjname| tries to find a reasonable name for 2772Function \verb|lua_getobjname| tries to find a reasonable name for
2753a given function. 2773a given function.
@@ -2870,6 +2890,10 @@ Therefore, the prompt can be changed like below:
2870$ lua _PROMPT='myprompt> ' -i 2890$ lua _PROMPT='myprompt> ' -i
2871\end{verbatim} 2891\end{verbatim}
2872 2892
2893In Unix systems, Lua scripts can be made into executable programs
2894by using the \verb|#!| form,
2895as in \verb|#!/usr/local/bin/lua|.
2896
2873\section*{Acknowledgments} 2897\section*{Acknowledgments}
2874 2898
2875The authors would like to thank CENPES/PETROBRAS which, 2899The authors would like to thank CENPES/PETROBRAS which,
@@ -2897,8 +2921,8 @@ Here is a list of all these incompatibilities.
2897Lua 3.1 must be explicitly opened before used, 2921Lua 3.1 must be explicitly opened before used,
2898with function \verb|lua_open|. 2922with function \verb|lua_open|.
2899However, all standard libraries check whether Lua is already opened, 2923However, all standard libraries check whether Lua is already opened,
2900so any program that opens at least one standard library before calling 2924so any existing program that opens at least one standard
2901Lua does not need to be modified. 2925library before calling Lua does not need to be modified.
2902 2926
2903\item Function \verb|dostring| no longer accepts an optional second argument, 2927\item Function \verb|dostring| no longer accepts an optional second argument,
2904with a temporary error method. 2928with a temporary error method.