aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1998-03-30 15:17:55 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1998-03-30 15:17:55 -0300
commit0a0c9593b846c33781f3adea84a8e9125a747131 (patch)
tree80d3830f4ef1f4db8f43981144c5ab8332ef88e1
parentd4707925176f94a29e69ff92f6618f36ca1928dd (diff)
downloadlua-0a0c9593b846c33781f3adea84a8e9125a747131.tar.gz
lua-0a0c9593b846c33781f3adea84a8e9125a747131.tar.bz2
lua-0a0c9593b846c33781f3adea84a8e9125a747131.zip
many small corrections; strings with '\0'
-rw-r--r--manual.tex413
1 files changed, 265 insertions, 148 deletions
diff --git a/manual.tex b/manual.tex
index 0084f04d..dde7d737 100644
--- a/manual.tex
+++ b/manual.tex
@@ -1,4 +1,4 @@
1% $Id: manual.tex,v 1.5 1998/01/13 15:42:43 roberto Exp roberto $ 1% $Id: manual.tex,v 1.6 1998/01/14 13:49:44 roberto Exp roberto $
2 2
3\documentstyle[fullpage,11pt,bnf]{article} 3\documentstyle[fullpage,11pt,bnf]{article}
4 4
@@ -38,7 +38,7 @@ Waldemar Celes
38\tecgraf\ --- Computer Science Department --- PUC-Rio 38\tecgraf\ --- Computer Science Department --- PUC-Rio
39} 39}
40 40
41\date{\small \verb$Date: 1998/01/13 15:42:43 $} 41\date{\small \verb$Date: 1998/01/14 13:49:44 $}
42 42
43\maketitle 43\maketitle
44 44
@@ -47,21 +47,54 @@ Waldemar Celes
47 47
48\begin{abstract} 48\begin{abstract}
49\noindent 49\noindent
50Lua is an extension programming language designed to be used 50Lua is a programming language originally designed for extending applications,
51as a configuration language for any program that needs one. 51but also frequently used as a general-purpose, stand-alone language.
52This document describes version \Version\ of the Lua programming language and 52Lua combines simple procedural syntax (similar to Pascal)
53the API that allows interaction between Lua programs and their host C programs. 53with powerful data description constructs based on associative
54arrays and extensible semantics.
55Lua is dynamically typed, interpreted from bytecodes,
56and has automatic memory management with garbage collection,
57making it ideal for configuration, scripting, and rapid prototyping.
58
59Lua is implemented as a small library of C functions,
60written in ANSI C, and compiles unmodified in all known platforms.
61The implementation goals are simplicity, efficiency, portability,
62and low embedding cost.
63
64Lua has been developed at TeCGraf,
65the Computer Graphics Technology Group of PUC-Rio
66(the Pontifical Catholic University of Rio de Janeiro in Brazil).
67TeCGraf is a laboratory of the Department of Computer Science.
68Dozens of industrial products developed by TeCGraf use Lua.
69
70This document describes version \Version\ of the Lua programming language
71and the API that allows interaction between Lua programs and their
72host C programs.
54\end{abstract} 73\end{abstract}
55
56\vspace{4ex} 74\vspace{4ex}
57\begin{quotation} 75\begin{quotation}
58\small 76\small
59\begin{center}{\bf Sum\'ario}\end{center} 77\begin{center}{\bf Sum\'ario}\end{center}
60\vspace{1ex} 78\vspace{1ex}
61\noindent 79\noindent
62Lua \'e uma linguagem de extens\~ao projetada para ser usada como 80Lua \'e uma linguagem de programa\c{c}\~ao originalmente projetada para
63linguagem de configura\c{c}\~ao em qualquer programa que precise de 81extens\~ao de aplica\c{c}\~oes,
64uma. 82e que \'e tamb\'em frequentemente usada como uma linguagem de
83prop\'osito geral.
84Lua combina uma sintaxe procedural simples (similar a Pascal)
85com poderosas facilidades para descri\c{c}\~ao de dados baseadas
86em tabelas associativas e uma sem\^antica estens\'{\i}vel.
87Lua tem tipagem din\^amica, \'e interpretada via bytecodes,
88e tem gerenciamento autom\'atico de mem\'oria com coleta de lixo,
89tornando-se ideal para configura\c{c}\~ao, scripting,
90e prototipagem r\'apida.
91
92Lua \'e implementada como uma pequena biblioteca de fun\c{c}\~oes C,
93escrita em ANSI C, e compila sem modifica\c{c}\~oes em todas as
94plataformas conhecidas.
95Os objetivos da implementa\c{c}\~ao s\~ao simplicidade, efici\^encia,
96portabilidade, e baixo custo.
97
65Este documento descreve a vers\~ao \Version\ da linguagem de 98Este documento descreve a vers\~ao \Version\ da linguagem de
66programa\c{c}\~ao Lua e a Interface de Programa\c{c}\~ao (API) que permite 99programa\c{c}\~ao Lua e a Interface de Programa\c{c}\~ao (API) que permite
67a intera\c{c}\~ao entre programas Lua e programas C hospedeiros. 100a intera\c{c}\~ao entre programas Lua e programas C hospedeiros.
@@ -74,7 +107,8 @@ a intera\c{c}\~ao entre programas Lua e programas C hospedeiros.
74\footnotesize 107\footnotesize
75Copyright \copyright\ 1994--1998 TeCGraf, PUC-Rio. 108Copyright \copyright\ 1994--1998 TeCGraf, PUC-Rio.
76Written by Waldemar Celes Filho, 109Written by Waldemar Celes Filho,
77Roberto Ierusalimschy, Luiz Henrique de Figueiredo. 110Roberto Ierusalimschy,
111and Luiz Henrique de Figueiredo.
78All rights reserved. 112All rights reserved.
79% 113%
80Permission is hereby granted, without written agreement and without license or 114Permission is hereby granted, without written agreement and without license or
@@ -170,6 +204,7 @@ A chunk is simply a sequence of statements:
170\begin{Produc} 204\begin{Produc}
171\produc{chunk}{\rep{stat} \opt{ret}} 205\produc{chunk}{\rep{stat} \opt{ret}}
172\end{Produc}% 206\end{Produc}%
207Statements are described in \See{stats}.
173(As usual, \rep{\emph{a}} means 0 or more \emph{a}'s, 208(As usual, \rep{\emph{a}} means 0 or more \emph{a}'s,
174\opt{\emph{a}} means an optional \emph{a} and \oneormore{\emph{a}} means 209\opt{\emph{a}} means an optional \emph{a} and \oneormore{\emph{a}} means
175one or more \emph{a}'s.) 210one or more \emph{a}'s.)
@@ -203,7 +238,8 @@ whose main property is to be different from any other value.
203\emph{Number} represents real (floating-point) numbers, 238\emph{Number} represents real (floating-point) numbers,
204while \emph{string} has the usual meaning; 239while \emph{string} has the usual meaning;
205notice that Lua is \Index{eight-bit clean}, 240notice that Lua is \Index{eight-bit clean},
206so strings can have ISO characters. 241and so strings can contain any ISO character,
242\emph{including} \verb|'\0'|.
207The function \verb|type| returns a string describing the type 243The function \verb|type| returns a string describing the type
208of a given value \see{pdf-type}. 244of a given value \see{pdf-type}.
209 245
@@ -222,7 +258,7 @@ arbitrary \Index{C pointers} to be stored in Lua variables.
222It corresponds to a \verb|void*| and has no pre-defined operations in Lua, 258It corresponds to a \verb|void*| and has no pre-defined operations in Lua,
223besides assignment and equality test. 259besides assignment and equality test.
224However, by using \emph{tag methods}, 260However, by using \emph{tag methods},
225the programmer may define operations for \emph{userdata} values 261the programmer can define operations for \emph{userdata} values
226\see{tag-method}. 262\see{tag-method}.
227 263
228The type \emph{table} implements \Index{associative arrays}, 264The type \emph{table} implements \Index{associative arrays},
@@ -230,6 +266,7 @@ that is, \Index{arrays} that can be indexed not only with numbers,
230but with any value (except \nil). 266but with any value (except \nil).
231Therefore, this type may be used not only to represent ordinary arrays, 267Therefore, this type may be used not only to represent ordinary arrays,
232but also symbol tables, sets, records, etc. 268but also symbol tables, sets, records, etc.
269Tables are the main data structuring mechanism in Lua.
233To represent \Index{records}, Lua uses the field name as an index. 270To represent \Index{records}, Lua uses the field name as an index.
234The language supports this representation by 271The language supports this representation by
235providing \verb|a.name| as syntactic sugar for \verb|a["name"]|. 272providing \verb|a.name| as syntactic sugar for \verb|a["name"]|.
@@ -248,13 +285,15 @@ Moreover, tables must be explicitly created before used
248\see{tableconstructor}. 285\see{tableconstructor}.
249 286
250Tags are mainly used to select tag methods when 287Tags are mainly used to select tag methods when
251some events occur \see{tag-method}. 288some events occur.
252Each of the types nil, number and string has a different tag. 289Tag methods are the main mechanism for extending the
290semantics of Lua \see{tag-method}.
291Each of the types \M{nil}, \M{number} and \M{string} has a different tag.
253All values of each of these types have this same pre-defined tag. 292All values of each of these types have this same pre-defined tag.
254Values of type function can have two different tags, 293Values of type \M{function} can have two different tags,
255depending on whether they are Lua or C functions. 294depending on whether they are Lua or C functions.
256Finally, 295Finally,
257values of type userdata and table can have 296values of type \M{userdata} and \M{table} can have
258as many different tags as needed \see{tag-method}. 297as many different tags as needed \see{tag-method}.
259Tags are created with the function \verb|newtag|, 298Tags are created with the function \verb|newtag|,
260and the function \verb|tag| returns the tag of a given value. 299and the function \verb|tag| returns the tag of a given value.
@@ -296,7 +335,20 @@ The following strings denote other \Index{tokens}:
296 335
297\Index{Literal strings} can be delimited by matching single or double quotes, 336\Index{Literal strings} can be delimited by matching single or double quotes,
298and can contain the C-like escape sequences 337and can contain the C-like escape sequences
299\verb|'\n'|, \verb|'\t'| and \verb|'\r'|. 338\verb|'\a'| (bell),
339\verb|'\b'| (back space),
340\verb|'\f'| (form feed),
341\verb|'\n'| (new line),
342\verb|'\r'| (carriage return),
343\verb|'\t'| (horizontal tab),
344\verb|'\v'| (vertical tab),
345\verb|'\\'|,
346\verb|'\"'|,
347and \verb|'\''|.
348A character in a string may also be specified by its numerical value,
349through the escape sequence \verb|'\ddd'|,
350where \verb|ddd| is a sequence of up to three \emph{decimal} digits.
351Strings in Lua may contain any 8-bit value, including 0.
300Literal strings can also be delimited by matching \verb|[[ ... ]]|. 352Literal strings can also be delimited by matching \verb|[[ ... ]]|.
301Literals in this bracketed form may run for several lines, 353Literals in this bracketed form may run for several lines,
302may contain nested \verb|[[ ... ]]| pairs, 354may contain nested \verb|[[ ... ]]| pairs,
@@ -304,6 +356,15 @@ and do not interpret escape sequences.
304This form is specially convenient for 356This form is specially convenient for
305writing strings that contain program pieces or 357writing strings that contain program pieces or
306other quoted strings. 358other quoted strings.
359As an example, in a system using ASCII,
360the following three literals are equivalent:
361\begin{verbatim}
3621) "alo\n123\""
3632) '\97lo\10\04923"'
3643) [[alo
365 123"]]
366\end{verbatim}
367
307 368
308\Index{Comments} start anywhere outside a string with a 369\Index{Comments} start anywhere outside a string with a
309double hyphen (\verb|--|) and run until the end of the line. 370double hyphen (\verb|--|) and run until the end of the line.
@@ -322,7 +383,7 @@ Examples of valid numerical constants are:
322\subsection{The \Index{Pre-processor}} \label{pre-processor} 383\subsection{The \Index{Pre-processor}} \label{pre-processor}
323 384
324All lines that start with a \verb|$| are handled by a pre-processor. 385All lines that start with a \verb|$| are handled by a pre-processor.
325The \verb|$| can be followed by any of the following directives: 386The \verb|$| must be immediately followed by one of the following directives:
326\begin{description} 387\begin{description}
327\item[\T{debug}] --- turn on some debugging facilities \see{pragma}. 388\item[\T{debug}] --- turn on some debugging facilities \see{pragma}.
328\item[\T{nodebug}] --- turn off some debugging facilities \see{pragma}. 389\item[\T{nodebug}] --- turn off some debugging facilities \see{pragma}.
@@ -332,11 +393,11 @@ If \M{cond} is false, then this part is skipped by the lexical analyzer.
332If \M{cond} is true, then this part is skipped by the lexical analyzer. 393If \M{cond} is true, then this part is skipped by the lexical analyzer.
333\item[\T{end}] --- ends a conditional part. 394\item[\T{end}] --- ends a conditional part.
334\item[\T{else}] --- starts an ``else'' conditional part, 395\item[\T{else}] --- starts an ``else'' conditional part,
335switching the ``skip'' status. 396flopping the ``skip'' status.
336\item[\T{endinput}] --- ends the lexical parse of the file. 397\item[\T{endinput}] --- ends the lexical parse of the file.
337\end{description} 398\end{description}
338 399
339Directives can be freely nested. 400Directives may be freely nested.
340Particularly, a \verb|$endinput| may occur inside a \verb|$if|; 401Particularly, a \verb|$endinput| may occur inside a \verb|$if|;
341in that case, even the matching \verb|$end| is not parsed. 402in that case, even the matching \verb|$end| is not parsed.
342 403
@@ -352,7 +413,7 @@ Therefore, actions in a chunk do not affect its own conditional directives.
352 413
353\subsection{\Index{Coercion}} \label{coercion} 414\subsection{\Index{Coercion}} \label{coercion}
354 415
355Lua provides some automatic conversions between values. 416Lua provides some automatic conversions between values at run time.
356Any arithmetic operation applied to a string tries to convert 417Any arithmetic operation applied to a string tries to convert
357that string to a number, following the usual rules. 418that string to a number, following the usual rules.
358Conversely, whenever a number is used when a string is expected, 419Conversely, whenever a number is used when a string is expected,
@@ -373,13 +434,15 @@ the system does not know how many values a function will return,
373or how many parameters it needs. 434or how many parameters it needs.
374Therefore, sometimes, a list of values must be \emph{adjusted}, at run time, 435Therefore, sometimes, a list of values must be \emph{adjusted}, at run time,
375to a given length. 436to a given length.
376If there are more values than are needed, then the last values are thrown away. 437If there are more values than are needed,
377If there are more needs than values, then the list is extended with as 438then the last values are thrown away.
378many \nil's as needed. 439If there are more needs than values,
379Adjustment occurs in multiple assignment and function calls. 440then the list is extended with as many \nil's as needed.
441Adjustment occurs in multiple assignment \see{assignment}
442and function calls \see{functioncall}.
380 443
381 444
382\subsection{Statements} 445\subsection{Statements}\label{stats}
383 446
384Lua supports an almost conventional set of \Index{statements}, 447Lua supports an almost conventional set of \Index{statements},
385similar to those in Pascal or C. 448similar to those in Pascal or C.
@@ -391,7 +454,7 @@ and local variable declarations \see{localvar}.
391 454
392\subsubsection{Blocks} 455\subsubsection{Blocks}
393A \Index{block} is a list of statements, which are executed sequentially. 456A \Index{block} is a list of statements, which are executed sequentially.
394Any statement can be optionally followed by a semicolon: 457A statement may be optionally followed by a semicolon:
395\begin{Produc} 458\begin{Produc}
396\produc{block}{\rep{stat sc} \opt{ret}} 459\produc{block}{\rep{stat sc} \opt{ret}}
397\produc{sc}{\opt{\ter{;}}} 460\produc{sc}{\opt{\ter{;}}}
@@ -404,7 +467,7 @@ A block may be explicitly delimited:
404\begin{Produc} 467\begin{Produc}
405\produc{stat}{\rwd{do} block \rwd{end}} 468\produc{stat}{\rwd{do} block \rwd{end}}
406\end{Produc}% 469\end{Produc}%
407This is useful to control the scope of local variables. 470This is useful to control the scope of local variables \see{localvar}.
408 471
409\subsubsection{\Index{Assignment}} \label{assignment} 472\subsubsection{\Index{Assignment}} \label{assignment}
410The language allows \Index{multiple assignment}. 473The language allows \Index{multiple assignment}.
@@ -436,8 +499,15 @@ Square brackets are used to index a table:
436\begin{Produc} 499\begin{Produc}
437\produc{var}{simpleexp \ter{[} exp1 \ter{]}} 500\produc{var}{simpleexp \ter{[} exp1 \ter{]}}
438\end{Produc}% 501\end{Produc}%
439The \verb|var| should result in a table value, 502The \M{simpleexp} should result in a table value,
440where the field indexed by the expression value gets the assigned value. 503from where the field indexed by the expression
504value gets the assigned value.
505
506The syntax \verb|var.NAME| is just syntactic sugar for
507\verb|var["NAME"]|:
508\begin{Produc}
509\produc{var}{simpleexp \ter{.} name}
510\end{Produc}%
441 511
442The meaning of assignments and evaluations of global variables and 512The meaning of assignments and evaluations of global variables and
443indexed variables can be changed by tag methods \see{tag-method}. 513indexed variables can be changed by tag methods \see{tag-method}.
@@ -446,16 +516,10 @@ an assignment \verb|x = val|, where \verb|x| is a global variable,
446is equivalent to a call \verb|setglobal('x', val)|; 516is equivalent to a call \verb|setglobal('x', val)|;
447an assignment \verb|t[i] = val| is equivalent to 517an assignment \verb|t[i] = val| is equivalent to
448\verb|settable_event(t, i, val)|. 518\verb|settable_event(t, i, val)|.
449See \See{tag-method} for a description of these functions. 519See \See{tag-method} for a complete description of these functions.
450(Function \verb|setglobal| is pre-defined in Lua. 520(Function \verb|setglobal| is pre-defined in Lua.
451Function \T{settable\_event} is used only for explanatory purposes.) 521Function \T{settable\_event} is used only for explanatory purposes.)
452 522
453The syntax \verb|var.NAME| is just syntactic sugar for
454\verb|var["NAME"]|:
455\begin{Produc}
456\produc{var}{simpleexp \ter{.} name}
457\end{Produc}%
458
459\subsubsection{Control Structures} 523\subsubsection{Control Structures}
460The \Index{condition expression} of a control structure may return any value. 524The \Index{condition expression} of a control structure may return any value.
461All values different from \nil\ are considered true; 525All values different from \nil\ are considered true;
@@ -471,7 +535,7 @@ only \nil\ is considered false.
471\produc{elseif}{\rwd{elseif} exp1 \rwd{then} block} 535\produc{elseif}{\rwd{elseif} exp1 \rwd{then} block}
472\end{Produc} 536\end{Produc}
473 537
474A \T{return} is used to return values from a function or a chunk. 538A \T{return} is used to return values from a function or from a chunk.
475\label{return} 539\label{return}
476Because they may return more than one value, 540Because they may return more than one value,
477the syntax for a \Index{return statement} is: 541the syntax for a \Index{return statement} is:
@@ -485,7 +549,7 @@ function calls can be executed as statements:
485\begin{Produc} 549\begin{Produc}
486\produc{stat}{functioncall} 550\produc{stat}{functioncall}
487\end{Produc}% 551\end{Produc}%
488In this case, returned values are thrown away. 552In this case, all returned values are thrown away.
489Function calls are explained in \See{functioncall}. 553Function calls are explained in \See{functioncall}.
490 554
491\subsubsection{Local Declarations} \label{localvar} 555\subsubsection{Local Declarations} \label{localvar}
@@ -549,7 +613,7 @@ the binary \verb|+| (addition),
549\verb|/| (division) and \verb|^| (exponentiation), 613\verb|/| (division) and \verb|^| (exponentiation),
550and unary \verb|-| (negation). 614and unary \verb|-| (negation).
551If the operands are numbers, or strings that can be converted to 615If the operands are numbers, or strings that can be converted to
552numbers, according to the rules given in \See{coercion}, 616numbers (according to the rules given in \See{coercion}),
553then all operations except exponentiation have the usual meaning. 617then all operations except exponentiation have the usual meaning.
554Otherwise, an appropriate tag method is called \see{tag-method}. 618Otherwise, an appropriate tag method is called \see{tag-method}.
555An exponentiation always calls a tag method. 619An exponentiation always calls a tag method.
@@ -564,7 +628,7 @@ Lua provides the following \Index{relational operators}:
564\end{verbatim} 628\end{verbatim}
565All these return \nil\ as false and a value different from \nil\ as true. 629All these return \nil\ as false and a value different from \nil\ as true.
566 630
567Equality first compares the types of its operands. 631Equality first compares the tags of its operands.
568If they are different, then the result is \nil. 632If they are different, then the result is \nil.
569Otherwise, their values are compared. 633Otherwise, their values are compared.
570Numbers and strings are compared in the usual way. 634Numbers and strings are compared in the usual way.
@@ -573,22 +637,24 @@ that is, two tables are considered equal only if they are the same table.
573The operator \verb|~=| is exactly the negation of equality (\verb|==|). 637The operator \verb|~=| is exactly the negation of equality (\verb|==|).
574Note that the conversion rules of \See{coercion} 638Note that the conversion rules of \See{coercion}
575\emph{do not} apply to equality comparisons. 639\emph{do not} apply to equality comparisons.
576Thus, \verb|"0"==0| evaluates to false. 640Thus, \verb|"0"==0| evaluates to false,
641and \verb|t[0]| and \verb|t["0"]| denote different
642entries in a table.
577 643
578The other operators work as follows. 644The other operators work as follows.
579If both arguments are numbers, then they are compared as such. 645If both arguments are numbers, then they are compared as such.
580Otherwise, if both arguments are strings, 646Otherwise, if both arguments are strings,
581their values are compared using lexicographical order. 647then their values are compared using lexicographical order.
582Otherwise, the ``order'' tag method is called \see{tag-method}. 648Otherwise, the ``order'' tag method is called \see{tag-method}.
583 649
584\subsubsection{Logical Operators} 650\subsubsection{Logical Operators}
585Like control structures, all logical operators
586consider \nil\ as false and anything else as true.
587The \Index{logical operators} are: 651The \Index{logical operators} are:
588\index{and}\index{or}\index{not} 652\index{and}\index{or}\index{not}
589\begin{verbatim} 653\begin{verbatim}
590 and or not 654 and or not
591\end{verbatim} 655\end{verbatim}
656Like control structures, all logical operators
657consider \nil\ as false and anything else as true.
592The operator \verb|and| returns \nil\ if its first argument is \nil; 658The operator \verb|and| returns \nil\ if its first argument is \nil;
593otherwise, it returns its second argument. 659otherwise, it returns its second argument.
594The operator \verb|or| returns its first argument 660The operator \verb|or| returns its first argument
@@ -646,13 +712,15 @@ For example:
646\begin{verbatim} 712\begin{verbatim}
647 a = {"v1", "v2", 34} 713 a = {"v1", "v2", 34}
648\end{verbatim} 714\end{verbatim}
649is essentially equivalent to: 715is equivalent to:
650\begin{verbatim} 716\begin{verbatim}
651 temp = {} 717 do
652 temp[1] = "v1" 718 local temp = {}
653 temp[2] = "v2" 719 temp[1] = "v1"
654 temp[3] = 34 720 temp[2] = "v2"
655 a = temp 721 temp[3] = 34
722 a = temp
723 end
656\end{verbatim} 724\end{verbatim}
657 725
658The form \emph{ffieldlist1} initializes other fields in a table: 726The form \emph{ffieldlist1} initializes other fields in a table:
@@ -664,14 +732,16 @@ For example:
664\begin{verbatim} 732\begin{verbatim}
665 a = {[f(k)] = g(y), x = 1, y = 3, [0] = b+c} 733 a = {[f(k)] = g(y), x = 1, y = 3, [0] = b+c}
666\end{verbatim} 734\end{verbatim}
667is essentially equivalent to: 735is equivalent to:
668\begin{verbatim} 736\begin{verbatim}
669 temp = {} 737 do
670 temp[f(k)] = g(y) 738 local temp = {}
671 temp.x = 1 -- or temp["x"] = 1 739 temp[f(k)] = g(y)
672 temp.y = 3 -- or temp["y"] = 3 740 temp.x = 1 -- or temp["x"] = 1
673 temp[0] = b+c 741 temp.y = 3 -- or temp["y"] = 3
674 a = temp 742 temp[0] = b+c
743 a = temp
744 end
675\end{verbatim} 745\end{verbatim}
676An expression like \verb|{x = 1, y = 4}| is 746An expression like \verb|{x = 1, y = 4}| is
677in fact syntactic sugar for \verb|{["x"] = 1, ["y"] = 4}|. 747in fact syntactic sugar for \verb|{["x"] = 1, ["y"] = 4}|.
@@ -681,25 +751,28 @@ and can be used in the same constructor separated by
681a semi-collon. 751a semi-collon.
682For example, all forms below are correct: 752For example, all forms below are correct:
683\begin{verbatim} 753\begin{verbatim}
684 x = {;}; x = {'a', 'b',}; x = {type='list'; 'a', 'b'} 754 x = {;}
755 x = {'a', 'b',}
756 x = {type='list'; 'a', 'b'}
685 x = {f(0), f(1), f(2),; n=3} 757 x = {f(0), f(1), f(2),; n=3}
686\end{verbatim} 758\end{verbatim}
687 759
688\subsubsection{Function Calls} \label{functioncall} 760\subsubsection{Function Calls} \label{functioncall}
689A \Index{function call} has the following syntax: 761A \Index{function call} has the following syntax:
690\begin{Produc} 762\begin{Produc}
691\produc{functioncall}{simpleexp realParams} 763\produc{functioncall}{simpleexp args}
692\end{Produc}% 764\end{Produc}%
693First, \M{simpleexp} is evaluated. 765First, \M{simpleexp} is evaluated.
694If its value has type \emph{function}, 766If its value has type \emph{function},
695then this function is called. 767then this function is called,
768with the given arguments.
696Otherwise, the ``function'' tag method is called, 769Otherwise, the ``function'' tag method is called,
697having as first parameter the value of \M{simpleexp}, 770having as first parameter the value of \M{simpleexp},
698and then the original call parameters. 771and then the original call parameters.
699 772
700The form: 773The form:
701\begin{Produc} 774\begin{Produc}
702\produc{functioncall}{simpleexp \ter{:} name realParams} 775\produc{functioncall}{simpleexp \ter{:} name args}
703\end{Produc}% 776\end{Produc}%
704can be used to call ``methods''. 777can be used to call ``methods''.
705A call \verb|simpleexp:name(...)| 778A call \verb|simpleexp:name(...)|
@@ -710,9 +783,9 @@ is syntactic sugar for
710except that \verb|simpleexp| is evaluated only once. 783except that \verb|simpleexp| is evaluated only once.
711 784
712\begin{Produc} 785\begin{Produc}
713\produc{realParams}{\ter{(} \opt{explist1} \ter{)}} 786\produc{args}{\ter{(} \opt{explist1} \ter{)}}
714\produc{realParams}{tableconstructor} 787\produc{args}{tableconstructor}
715\produc{realParams}{\ter{literal}} 788\produc{args}{\ter{literal}}
716\produc{explist1}{exp1 \rep{\ter{,} exp1}} 789\produc{explist1}{exp1 \rep{\ter{,} exp1}}
717\end{Produc}% 790\end{Produc}%
718All argument expressions are evaluated before the call. 791All argument expressions are evaluated before the call.
@@ -761,7 +834,7 @@ is just syntactic sugar for:
761 end 834 end
762\end{verbatim} 835\end{verbatim}
763 836
764A function definition is an executable expresion, 837A function definition is an executable expression,
765whose value has type \emph{function}. 838whose value has type \emph{function}.
766When Lua pre-compiles a chunk, 839When Lua pre-compiles a chunk,
767all its function bodies are pre-compiled, too. 840all its function bodies are pre-compiled, too.
@@ -774,7 +847,7 @@ Different instances of a same function
774may have different upvalues. 847may have different upvalues.
775 848
776Parameters act as local variables, 849Parameters act as local variables,
777initialized with the argument values. 850initialized with the argument values:
778\begin{Produc} 851\begin{Produc}
779\produc{parlist1}{\ter{\ldots}} 852\produc{parlist1}{\ter{\ldots}}
780\produc{parlist1}{name \rep{\ter{,} name} \opt{\ter{,} \ter{\ldots}}} 853\produc{parlist1}{name \rep{\ter{,} name} \opt{\ter{,} \ter{\ldots}}}
@@ -786,8 +859,8 @@ the length of the list of parameters \see{adjust},
786unless the function is a \Def{vararg} function, 859unless the function is a \Def{vararg} function,
787indicated by the dots (\ldots) at the end of its parameter list. 860indicated by the dots (\ldots) at the end of its parameter list.
788A vararg function does not adjust its argument list; 861A vararg function does not adjust its argument list;
789instead, it collects any extra arguments in an implicit parameter, 862instead, it collects any extra arguments into an implicit parameter,
790called \Def{arg}. 863called \IndexVerb{arg}.
791This parameter is always initialized as a table, 864This parameter is always initialized as a table,
792with a field \verb|n| with the number of extra arguments, 865with a field \verb|n| with the number of extra arguments,
793and the extra arguments at positions 1, 2, \ldots 866and the extra arguments at positions 1, 2, \ldots
@@ -815,7 +888,7 @@ If control reaches the end of a function without a return instruction,
815then the function returns with no results. 888then the function returns with no results.
816 889
817There is a special syntax for defining \Index{methods}, 890There is a special syntax for defining \Index{methods},
818that is, functions that have an extra parameter \Def{self}. 891that is, functions that have an extra parameter \IndexVerb{self}.
819\begin{Produc} 892\begin{Produc}
820\produc{function}{\rwd{function} name \ter{:} name \ter{(} \opt{parlist1} 893\produc{function}{\rwd{function} name \ter{:} name \ter{(} \opt{parlist1}
821 \ter{)} block \rwd{end}} 894 \ter{)} block \rwd{end}}
@@ -858,17 +931,17 @@ appears is instantiated.
858The name used in an upvalue may be the name of any variable visible 931The name used in an upvalue may be the name of any variable visible
859at the point where the function is defined. 932at the point where the function is defined.
860 933
861See some examples below: 934Here are some examples:
862\begin{verbatim} 935\begin{verbatim}
863a,b,c = 1 -- global variables 936a,b,c = 1 -- global variables
864function f () 937function f ()
865 local x,b; -- x and b are locals to f 938 local x,b -- x and b are local to f
866 function g () 939 function g ()
867 local a,y -- a and y are locals go g 940 local a,y -- a and y are local to g
868 p = a -- OK, access local 'a' 941 p = a -- OK, access local 'a'
869 p = c -- OK, access global 'c' 942 p = c -- OK, access global 'c'
870 p = b -- ERROR: cannot access a variable in outer scope 943 p = b -- ERROR: cannot access a variable in outer scope
871 p = %b -- OK, access frozen value of 'b' 944 p = %b -- OK, access frozen value of 'b' (local to 'f')
872 p = %c -- OK, access frozen value of global 'c' 945 p = %c -- OK, access frozen value of global 'c'
873 p = %y -- ERROR: 'y' is not visible where 'g' is defined 946 p = %y -- ERROR: 'y' is not visible where 'g' is defined
874 end 947 end
@@ -881,7 +954,7 @@ end
881Lua provides a powerful mechanism to extend its semantics, 954Lua provides a powerful mechanism to extend its semantics,
882called \Def{Tag Methods}. 955called \Def{Tag Methods}.
883A tag method (TM) is a programmer-defined function 956A tag method (TM) is a programmer-defined function
884that can be called at many key points of the evaluation of a program, 957that is called at specific key points during the evaluation of a program,
885allowing a programmer to change the standard Lua behavior at these points. 958allowing a programmer to change the standard Lua behavior at these points.
886Each of these points is called an \Def{event}. 959Each of these points is called an \Def{event}.
887 960
@@ -889,7 +962,7 @@ The tag method called for any specific event is selected
889according to the tag of the values involved 962according to the tag of the values involved
890in the event \see{TypesSec}. 963in the event \see{TypesSec}.
891The function \IndexVerb{settagmethod} changes the tag method 964The function \IndexVerb{settagmethod} changes the tag method
892associated with a given pair \M{<tag, event>}. 965associated with a given pair \M{(tag, event)}.
893Its first parameter is the tag, the second the event name 966Its first parameter is the tag, the second the event name
894(a string, see below), 967(a string, see below),
895and the third parameter is the new method (a function), 968and the third parameter is the new method (a function),
@@ -941,7 +1014,7 @@ If it also fails, then it gets a tag method from tag~0:
941 -- call the method with both operands and an extra 1014 -- call the method with both operands and an extra
942 -- argument with the event name 1015 -- argument with the event name
943 return tm(op1, op2, "add") 1016 return tm(op1, op2, "add")
944 else -- no tag method available: Default behavior 1017 else -- no tag method available: default behavior
945 error("unexpected type at arithmetic operation") 1018 error("unexpected type at arithmetic operation")
946 end 1019 end
947 end 1020 end
@@ -950,15 +1023,15 @@ If it also fails, then it gets a tag method from tag~0:
950 1023
951\item[``sub'':]\index{sub event} 1024\item[``sub'':]\index{sub event}
952called when a \verb|-| operation is applied to non numerical operands. 1025called when a \verb|-| operation is applied to non numerical operands.
953Behavior similar to \verb|"add"| event. 1026Behavior similar to the \verb|"add"| event.
954 1027
955\item[``mul'':]\index{mul event} 1028\item[``mul'':]\index{mul event}
956called when a \verb|*| operation is applied to non numerical operands. 1029called when a \verb|*| operation is applied to non numerical operands.
957Behavior similar to \verb|"add"| event. 1030Behavior similar to the \verb|"add"| event.
958 1031
959\item[``div'':]\index{div event} 1032\item[``div'':]\index{div event}
960called when a \verb|/| operation is applied to non numerical operands. 1033called when a \verb|/| operation is applied to non numerical operands.
961Behavior similar to \verb|"add"| event. 1034Behavior similar to the \verb|"add"| event.
962 1035
963\item[``pow'':]\index{pow event} 1036\item[``pow'':]\index{pow event}
964called when a \verb|^| operation is applied. 1037called when a \verb|^| operation is applied.
@@ -969,7 +1042,7 @@ called when a \verb|^| operation is applied.
969 -- call the method with both operands and an extra 1042 -- call the method with both operands and an extra
970 -- argument with the event name 1043 -- argument with the event name
971 return tm(op1, op2, "pow") 1044 return tm(op1, op2, "pow")
972 else -- no tag method available: Default behavior 1045 else -- no tag method available: default behavior
973 error("unexpected type at arithmetic operation") 1046 error("unexpected type at arithmetic operation")
974 end 1047 end
975 end 1048 end
@@ -991,7 +1064,7 @@ called when an unary \verb|-| operation is applied to a non numerical operand.
991 -- call the method with the operand, nil, and an extra 1064 -- call the method with the operand, nil, and an extra
992 -- argument with the event name 1065 -- argument with the event name
993 return tm(op, nil, "unm") 1066 return tm(op, nil, "unm")
994 else -- no tag method available: Default behavior 1067 else -- no tag method available: default behavior
995 error("unexpected type at arithmetic operation") 1068 error("unexpected type at arithmetic operation")
996 end 1069 end
997 end 1070 end
@@ -1021,17 +1094,17 @@ or non string operands.
1021\item[``gt'':]\index{gt event} 1094\item[``gt'':]\index{gt event}
1022called when a \verb|>| operation is applied to non numerical 1095called when a \verb|>| operation is applied to non numerical
1023or non string operands. 1096or non string operands.
1024Behavior similar to \verb|"lt"| event. 1097Behavior similar to the \verb|"lt"| event.
1025 1098
1026\item[``le'':]\index{le event} 1099\item[``le'':]\index{le event}
1027called when a \verb|<=| operation is applied to non numerical 1100called when a \verb|<=| operation is applied to non numerical
1028or non string operands. 1101or non string operands.
1029Behavior similar to \verb|"lt"| event. 1102Behavior similar to the \verb|"lt"| event.
1030 1103
1031\item[``ge'':]\index{ge event} 1104\item[``ge'':]\index{ge event}
1032called when a \verb|>=| operation is applied to non numerical 1105called when a \verb|>=| operation is applied to non numerical
1033or non string operands. 1106or non string operands.
1034Behavior similar to \verb|"lt"| event. 1107Behavior similar to the \verb|"lt"| event.
1035 1108
1036\item[``concat'':]\index{concatenation event} 1109\item[``concat'':]\index{concatenation event}
1037called when a concatenation is applied to non string operands. 1110called when a concatenation is applied to non string operands.
@@ -1057,7 +1130,7 @@ not present in a table.
1057See event \verb|"gettable"| for its semantics. 1130See event \verb|"gettable"| for its semantics.
1058 1131
1059\item[``getglobal'':]\index{getglobal event} 1132\item[``getglobal'':]\index{getglobal event}
1060called whenever Lua accesses a global variable. 1133called whenever Lua needs the value of a global variable.
1061This method can only be set for \nil\ and for tags 1134This method can only be set for \nil\ and for tags
1062created by \verb|newtag|. 1135created by \verb|newtag|.
1063\begin{verbatim} 1136\begin{verbatim}
@@ -1176,7 +1249,8 @@ Lua does the equivalent of the call \verb|gc_event(nil)|.
1176\subsection{Error Handling} \label{error} 1249\subsection{Error Handling} \label{error}
1177 1250
1178Because Lua is an extension language, 1251Because Lua is an extension language,
1179all Lua actions start from C code calling a function from the Lua library. 1252all Lua actions start from C code in the host program
1253calling a function from the Lua library.
1180Whenever an error occurs during Lua compilation or execution, 1254Whenever an error occurs during Lua compilation or execution,
1181the \Def{error method} is called, 1255the \Def{error method} is called,
1182and then the corresponding function from the library 1256and then the corresponding function from the library
@@ -1193,7 +1267,7 @@ which gets the new error handler as its only parameter
1193The standard I/O library uses this facility to redefine the error method, 1267The standard I/O library uses this facility to redefine the error method,
1194using the debug facilities \see{debugI}, 1268using the debug facilities \see{debugI},
1195in order to print some extra information, 1269in order to print some extra information,
1196like the call stack. 1270such as the call stack.
1197 1271
1198To provide more information about errors, 1272To provide more information about errors,
1199Lua programs should include the compilation pragma \verb|$debug|. 1273Lua programs should include the compilation pragma \verb|$debug|.
@@ -1236,6 +1310,7 @@ This function allocates and initializes some internal structures,
1236and defines all pre-defined functions of Lua. 1310and defines all pre-defined functions of Lua.
1237If the library is already opened, 1311If the library is already opened,
1238this function has no effect. 1312this function has no effect.
1313All standard libraries call \verb|lua_open| when they are opened.
1239 1314
1240If necessary, the library may be closed:\Deffunc{lua_close} 1315If necessary, the library may be closed:\Deffunc{lua_close}
1241\begin{verbatim} 1316\begin{verbatim}
@@ -1277,9 +1352,9 @@ and 0 otherwise.
1277The function \verb|lua_isnumber| accepts numbers and numerical strings, 1352The function \verb|lua_isnumber| accepts numbers and numerical strings,
1278whereas 1353whereas
1279\verb|lua_isstring| accepts strings and numbers \see{coercion}, 1354\verb|lua_isstring| accepts strings and numbers \see{coercion},
1280and \verb|lua_isfunction| accepts Lua and C functions. 1355and \verb|lua_isfunction| accepts Lua functions and C functions.
1281 1356
1282To check the tag of a \verb|lua_Object|, 1357To get the tag of a \verb|lua_Object|,
1283the following function is available: 1358the following function is available:
1284\Deffunc{lua_tag} 1359\Deffunc{lua_tag}
1285\begin{verbatim} 1360\begin{verbatim}
@@ -1288,11 +1363,12 @@ int lua_tag (lua_Object object);
1288 1363
1289To translate a value from type \verb|lua_Object| to a specific C type, 1364To translate a value from type \verb|lua_Object| to a specific C type,
1290the programmer can use: 1365the programmer can use:
1291\Deffunc{lua_getnumber}\Deffunc{lua_getstring} 1366\Deffunc{lua_getnumber}\Deffunc{lua_getstring}\Deffunc{lua_strlen}
1292\Deffunc{lua_getcfunction}\Deffunc{lua_getuserdata} 1367\Deffunc{lua_getcfunction}\Deffunc{lua_getuserdata}
1293\begin{verbatim} 1368\begin{verbatim}
1294float lua_getnumber (lua_Object object); 1369double lua_getnumber (lua_Object object);
1295char *lua_getstring (lua_Object object); 1370char *lua_getstring (lua_Object object);
1371long lua_strlen (lua_Object object);
1296lua_CFunction lua_getcfunction (lua_Object object); 1372lua_CFunction lua_getcfunction (lua_Object object);
1297void *lua_getuserdata (lua_Object object); 1373void *lua_getuserdata (lua_Object object);
1298\end{verbatim} 1374\end{verbatim}
@@ -1306,9 +1382,13 @@ This \verb|lua_Object| must be a string or a number;
1306otherwise, the function returns~0 (the \verb|NULL| pointer). 1382otherwise, the function returns~0 (the \verb|NULL| pointer).
1307This function does not create a new string, 1383This function does not create a new string,
1308but returns a pointer to a string inside the Lua environment. 1384but returns a pointer to a string inside the Lua environment.
1385Those strings always have a 0 after their last character (like in C),
1386but may contain other zeros in their body.
1387If you do not know whether a string may contain zeros,
1388you can use \verb|lua_strlen| to check the actual length.
1309Because Lua has garbage collection, 1389Because Lua has garbage collection,
1310there is no guarantee that such pointer will be valid after the block ends 1390there is no guarantee that such pointer will be valid after the block ends
1311(see below). 1391\see{GC}.
1312 1392
1313\verb|lua_getcfunction| converts a \verb|lua_Object| to a C function. 1393\verb|lua_getcfunction| converts a \verb|lua_Object| to a C function.
1314This \verb|lua_Object| must have type \emph{CFunction}; 1394This \verb|lua_Object| must have type \emph{CFunction};
@@ -1319,6 +1399,7 @@ The type \verb|lua_CFunction| is explained in \See{LuacallC}.
1319This \verb|lua_Object| must have type \emph{userdata}; 1399This \verb|lua_Object| must have type \emph{userdata};
1320otherwise, the function returns 0 (the \verb|NULL| pointer). 1400otherwise, the function returns 0 (the \verb|NULL| pointer).
1321 1401
1402\subsection{Garbage Collection}\label{GC}
1322Because Lua has automatic memory management and garbage collection, 1403Because Lua has automatic memory management and garbage collection,
1323a \verb|lua_Object| has a limited scope, 1404a \verb|lua_Object| has a limited scope,
1324and is only valid inside the \emph{block} where it has been created. 1405and is only valid inside the \emph{block} where it has been created.
@@ -1335,7 +1416,7 @@ long lua_collectgarbage (long limit);
1335\end{verbatim} 1416\end{verbatim}
1336This function returns the number of objects collected. 1417This function returns the number of objects collected.
1337The argument \verb|limit| makes the next cycle occur only 1418The argument \verb|limit| makes the next cycle occur only
1338when that number of new objects have been created. 1419after that number of new objects have been created.
1339If \verb|limit|=0, then Lua uses an adaptable heuristics to set this limit. 1420If \verb|limit|=0, then Lua uses an adaptable heuristics to set this limit.
1340 1421
1341 1422
@@ -1343,15 +1424,15 @@ All communication between Lua and C is done through two
1343abstract data types, called \Def{lua2C} and \Def{C2lua}. 1424abstract data types, called \Def{lua2C} and \Def{C2lua}.
1344The first one, as the name implies, is used to pass values 1425The first one, as the name implies, is used to pass values
1345from Lua to C: 1426from Lua to C:
1346Parameters when Lua calls C and results when C calls Lua. 1427parameters when Lua calls C and results when C calls Lua.
1347The structure C2lua is used in the reverse direction: 1428The structure C2lua is used in the reverse direction:
1348Parameters when C calls Lua and results when Lua calls C. 1429parameters when C calls Lua and results when Lua calls C.
1349 1430
1350The structure lua2C is an abstract array, 1431The structure lua2C is an abstract array,
1351which can be indexed with the function: 1432which can be indexed with the function:
1352\Deffunc{lua_lua2C} 1433\Deffunc{lua_lua2C}
1353\begin{verbatim} 1434\begin{verbatim}
1354lua_Object lua_lua2C (int number); 1435lua_Object lua_lua2C (int number);
1355\end{verbatim} 1436\end{verbatim}
1356where \verb|number| starts with 1. 1437where \verb|number| starts with 1.
1357When called with a number larger than the array size, 1438When called with a number larger than the array size,
@@ -1361,15 +1442,16 @@ a variable number of parameters,
1361and to call Lua functions that return a variable number of results. 1442and to call Lua functions that return a variable number of results.
1362Notice that the structure lua2C cannot be directly modified by C code. 1443Notice that the structure lua2C cannot be directly modified by C code.
1363 1444
1364The second structure, C2lua, is a stack. 1445The second structure, C2lua, is an abstract stack.
1365Pushing elements into this stack 1446Pushing elements into this stack
1366is done with the following functions and macros: 1447is done with the following functions and macros:
1367\Deffunc{lua_pushnumber}\Deffunc{lua_pushstring} 1448\Deffunc{lua_pushnumber}\Deffunc{lua_pushlstring}\Deffunc{lua_pushstring}
1368\Deffunc{lua_pushcfunction}\Deffunc{lua_pushusertag} 1449\Deffunc{lua_pushcfunction}\Deffunc{lua_pushusertag}
1369\Deffunc{lua_pushnil}\Deffunc{lua_pushobject} 1450\Deffunc{lua_pushnil}\Deffunc{lua_pushobject}
1370\Deffunc{lua_pushuserdata}\label{pushing} 1451\Deffunc{lua_pushuserdata}\label{pushing}
1371\begin{verbatim} 1452\begin{verbatim}
1372void lua_pushnumber (double n); 1453void lua_pushnumber (double n);
1454void lua_pushlstring (char *s, long len);
1373void lua_pushstring (char *s); 1455void lua_pushstring (char *s);
1374void lua_pushusertag (void *u, int tag); 1456void lua_pushusertag (void *u, int tag);
1375void lua_pushnil (void); 1457void lua_pushnil (void);
@@ -1379,6 +1461,11 @@ void lua_pushcfunction (lua_CFunction f); /* macro */
1379All of them receive a C value, 1461All of them receive a C value,
1380convert it to a corresponding \verb|lua_Object|, 1462convert it to a corresponding \verb|lua_Object|,
1381and leave the result on the top of C2lua. 1463and leave the result on the top of C2lua.
1464Particularly, functions \verb|lua_pushlstring| and \verb|lua_pushstring|
1465make an internal copy of the given string.
1466Function \verb|lua_pushstring| can only be used to push proper C strings
1467(that is, strings which do not contain zeros and end with a zero);
1468otherwise you can use the more generic \verb|lua_pushlstring|.
1382The function 1469The function
1383\Deffunc{lua_pop} 1470\Deffunc{lua_pop}
1384\begin{verbatim} 1471\begin{verbatim}
@@ -1398,9 +1485,9 @@ Otherwise, a new userdata is created, with the given value and tag.
1398If this function is called with 1485If this function is called with
1399\verb|tag|=\verb|LUA_ANYTAG|\Deffunc{LUA_ANYTAG}, 1486\verb|tag|=\verb|LUA_ANYTAG|\Deffunc{LUA_ANYTAG},
1400then Lua will try to find any userdata with the given value, 1487then Lua will try to find any userdata with the given value,
1401no matter its tag. 1488regardless of its tag.
1402If there is no userdata with that value, then a new one is created, 1489If there is no userdata with that value, then a new one is created,
1403with tag=0. 1490with tag equals to 0.
1404 1491
1405Userdata can have different tags, 1492Userdata can have different tags,
1406whose semantics are only known to the host program. 1493whose semantics are only known to the host program.
@@ -1533,7 +1620,6 @@ Functions defined in Lua by a chunk executed with
1533This is done using the following protocol: 1620This is done using the following protocol:
1534first, the arguments to the function are pushed onto C2lua 1621first, the arguments to the function are pushed onto C2lua
1535\see{pushing}, in direct order, i.e., the first argument is pushed first. 1622\see{pushing}, in direct order, i.e., the first argument is pushed first.
1536
1537Then, the function is called using 1623Then, the function is called using
1538\Deffunc{lua_callfunction} 1624\Deffunc{lua_callfunction}
1539\begin{verbatim} 1625\begin{verbatim}
@@ -1558,7 +1644,7 @@ equivalent to the Lua code:
1558 lua_pushstring("how"); /* 1st argument */ 1644 lua_pushstring("how"); /* 1st argument */
1559 lua_pushobject(lua_getglobal("t")); /* push value of global 't' */ 1645 lua_pushobject(lua_getglobal("t")); /* push value of global 't' */
1560 lua_pushstring("x"); /* push the string 'x' */ 1646 lua_pushstring("x"); /* push the string 'x' */
1561 lua_pushobject(lua_gettable()); /* push result of t.x (= t['x']) */ 1647 lua_pushobject(lua_gettable()); /* push result of t.x (2nd arg) */
1562 lua_pushnumber(4); /* 3th argument */ 1648 lua_pushnumber(4); /* 3th argument */
1563 lua_callfunction(lua_getglobal("f")); /* call Lua function */ 1649 lua_callfunction(lua_getglobal("f")); /* call Lua function */
1564 lua_pushobject(lua_getresult(1)); /* push first result of the call */ 1650 lua_pushobject(lua_getresult(1)); /* push first result of the call */
@@ -1639,7 +1725,7 @@ which defines the way parameters and results are passed.
1639 1725
1640A C function receives its arguments in structure lua2C; 1726A C function receives its arguments in structure lua2C;
1641to access them, it uses the macro \verb|lua_getparam|, \Deffunc{lua_getparam} 1727to access them, it uses the macro \verb|lua_getparam|, \Deffunc{lua_getparam}
1642again just another name to \verb|lua_lua2C|. 1728again just another name for \verb|lua_lua2C|.
1643To return values, a C function just pushes them onto the stack C2lua, 1729To return values, a C function just pushes them onto the stack C2lua,
1644in direct order \see{valuesCLua}. 1730in direct order \see{valuesCLua}.
1645Like a Lua function, a C function called by Lua can also return 1731Like a Lua function, a C function called by Lua can also return
@@ -1666,11 +1752,11 @@ these upvalues are inserted as the first arguments to the function,
1666before the actual arguments provided in the call. 1752before the actual arguments provided in the call.
1667 1753
1668For some examples of C functions, see files \verb|lstrlib.c|, 1754For some examples of C functions, see files \verb|lstrlib.c|,
1669\verb|liolib.c| and \verb|lmathlib.c| in Lua distribution. 1755\verb|liolib.c| and \verb|lmathlib.c| in the official Lua distribution.
1670 1756
1671\subsection{References to Lua Objects} 1757\subsection{References to Lua Objects}
1672 1758
1673As noted in \See{LuacallC}, \verb|lua_Object|s are volatile. 1759As noted in \See{GC}, \verb|lua_Object|s are volatile.
1674If the C code needs to keep a \verb|lua_Object| 1760If the C code needs to keep a \verb|lua_Object|
1675outside block boundaries, 1761outside block boundaries,
1676then it must create a \Def{reference} to the object. 1762then it must create a \Def{reference} to the object.
@@ -1695,7 +1781,7 @@ if the object has been collected,
1695\verb|lua_getref| returns \verb|LUA_NOOBJECT|. 1781\verb|lua_getref| returns \verb|LUA_NOOBJECT|.
1696 1782
1697When a reference is no longer needed, 1783When a reference is no longer needed,
1698it can be freed with a call to \verb|lua_unref|. 1784it can be released with a call to \verb|lua_unref|.
1699 1785
1700 1786
1701 1787
@@ -1740,7 +1826,7 @@ then Lua stops getting arguments at the first nil value.
1740 1826
1741By default, 1827By default,
1742all results from \verb|func| are just returned by the call. 1828all results from \verb|func| are just returned by the call.
1743If the string \verb|mode| contains \verb|p|, 1829If the string \verb|mode| contains \verb|"p"|,
1744the results are \emph{packed} in a single table.\index{packed results} 1830the results are \emph{packed} in a single table.\index{packed results}
1745That is, \verb|call| returns just one table; 1831That is, \verb|call| returns just one table;
1746at index \verb|n|, the table has the total number of results 1832at index \verb|n|, the table has the total number of results
@@ -1748,8 +1834,8 @@ from the call;
1748the first result is at index 1, etc. 1834the first result is at index 1, etc.
1749For instance, the following calls produce the following results: 1835For instance, the following calls produce the following results:
1750\begin{verbatim} 1836\begin{verbatim}
1751a = call(sin, {5}) --> a = 0.0871557 = sin(5) 1837a = call(sin, {5}) --> a = 0.0871557 = sin(5)
1752a = call(max, {1,4,5; n=2}) --> a = 4 (only 1 and 4 are arguments) 1838a = call(max, {1,4,5; n=2}) --> a = 4 (only 1 and 4 are arguments)
1753t = {x=1} 1839t = {x=1}
1754a = call(next, {t,nil;n=2}, "p") --> a={"x", 1; n=2} 1840a = call(next, {t,nil;n=2}, "p") --> a={"x", 1; n=2}
1755\end{verbatim} 1841\end{verbatim}
@@ -1757,7 +1843,7 @@ a = call(next, {t,nil;n=2}, "p") --> a={"x", 1; n=2}
1757By default, 1843By default,
1758if an error occurs during the function call, 1844if an error occurs during the function call,
1759the error is propagated. 1845the error is propagated.
1760If the string \verb|mode| contains \verb|x|, 1846If the string \verb|mode| contains \verb|"x"|,
1761then the call is \emph{protected}.\index{protected calls} 1847then the call is \emph{protected}.\index{protected calls}
1762In this mode, function \verb|call| does not generate an error, 1848In this mode, function \verb|call| does not generate an error,
1763whatever happens during the call. 1849whatever happens during the call.
@@ -1773,7 +1859,7 @@ no error messages will be issued during the execution of the called function.
1773Forces a garbage collection cycle. 1859Forces a garbage collection cycle.
1774Returns the number of objects collected. 1860Returns the number of objects collected.
1775An optional argument, \verb|limit|, is a number that 1861An optional argument, \verb|limit|, is a number that
1776makes the next cycle occur when that number of new 1862makes the next cycle occur only after that number of new
1777objects have been created. 1863objects have been created.
1778If absent, Lua uses an adaptable algorithm to set 1864If absent, Lua uses an adaptable algorithm to set
1779this limit. 1865this limit.
@@ -1782,10 +1868,10 @@ the API function \verb|lua_collectgarbage|.
1782 1868
1783\subsubsection*{\ff \T{dofile (filename)}}\Deffunc{dofile} 1869\subsubsection*{\ff \T{dofile (filename)}}\Deffunc{dofile}
1784This function receives a file name, 1870This function receives a file name,
1785opens it, and executes its contents as a Lua chunk, 1871opens the file, and executes the file contents as a Lua chunk,
1786or as pre-compiled chunks. 1872or as pre-compiled chunks.
1787When called without arguments, 1873When called without arguments,
1788it executes the contents of the standard input (\verb|stdin|). 1874\verb|dofile| executes the contents of the standard input (\verb|stdin|).
1789If there is any error executing the file, 1875If there is any error executing the file,
1790then \verb|dofile| returns \nil. 1876then \verb|dofile| returns \nil.
1791Otherwise, it returns the values returned by the chunk, 1877Otherwise, it returns the values returned by the chunk,
@@ -1843,12 +1929,12 @@ otherwise the semantics of \verb|nextvar| is undefined.
1843This function cannot be written with the standard API. 1929This function cannot be written with the standard API.
1844 1930
1845\subsubsection*{\ff \T{foreach (table, function)}}\Deffunc{foreach} 1931\subsubsection*{\ff \T{foreach (table, function)}}\Deffunc{foreach}
1846Executes \verb|function| over all elements of \verb|table|. 1932Executes the given \verb|function| over all elements of \verb|table|.
1847For each element, the function is called with the index and 1933For each element, the function is called with the index and
1848respective value as arguments. 1934respective value as arguments.
1849If the function returns any non-nil value, 1935If the function returns any non-nil value,
1850the loop is broken, and the value is returned 1936the loop is broken, and the value is returned
1851as the final value of |verb|foreach|. 1937as the final value of \verb|foreach|.
1852 1938
1853This function could be defined in Lua: 1939This function could be defined in Lua:
1854\begin{verbatim} 1940\begin{verbatim}
@@ -1868,7 +1954,7 @@ For each variable,
1868the function is called with its name and its value as arguments. 1954the function is called with its name and its value as arguments.
1869If the function returns any non-nil value, 1955If the function returns any non-nil value,
1870the loop is broken, and the value is returned 1956the loop is broken, and the value is returned
1871as the final value of |verb|foreachvar|. 1957as the final value of \verb|foreachvar|.
1872 1958
1873This function could be defined in Lua: 1959This function could be defined in Lua:
1874\begin{verbatim} 1960\begin{verbatim}
@@ -1908,7 +1994,7 @@ represents 10, `B' represents 11, and so forth, with `Z' representing 35.
1908 1994
1909In base 10 (the default), the number may have a decimal part, 1995In base 10 (the default), the number may have a decimal part,
1910as well as an optional exponent part \see{coercion}. 1996as well as an optional exponent part \see{coercion}.
1911In other bases only integer numbers are accepted. 1997In other bases only integers are accepted.
1912 1998
1913\subsubsection*{\ff \T{type (v)}}\Deffunc{type}\label{pdf-type} 1999\subsubsection*{\ff \T{type (v)}}\Deffunc{type}\label{pdf-type}
1914This function allows Lua to test the type of a value. 2000This function allows Lua to test the type of a value.
@@ -1930,14 +2016,15 @@ It receives one argument, and returns its tag (a number).
1930This function sets the tag of a given table \see{TypesSec}. 2016This function sets the tag of a given table \see{TypesSec}.
1931\verb|tag| must be a value created with \verb|newtag| 2017\verb|tag| must be a value created with \verb|newtag|
1932\see{pdf-newtag}. 2018\see{pdf-newtag}.
2019It returns the value of its first argument (the table).
1933For security reasons, 2020For security reasons,
1934it is impossible to change the tag of a userdata from Lua. 2021it is impossible to change the tag of a userdata from Lua.
1935 2022
2023
1936\subsubsection*{\ff \T{assert (v [, message])}}\Deffunc{assert} 2024\subsubsection*{\ff \T{assert (v [, message])}}\Deffunc{assert}
1937This function issues an \emph{``assertion failed!''} error 2025This function issues an \emph{``assertion failed!''} error
1938when its argument is \nil. 2026when its argument is \nil.
1939 2027This function is equivalent to the following Lua function:
1940This function could be defined in Lua:
1941\begin{verbatim} 2028\begin{verbatim}
1942function assert (v, m) 2029function assert (v, m)
1943 if not v then 2030 if not v then
@@ -1978,10 +2065,15 @@ this function can set global variables with strange names like
1978\verb|"m v 1"| or \verb|34|. 2065\verb|"m v 1"| or \verb|34|.
1979It returns the value of its second argument. 2066It returns the value of its second argument.
1980 2067
2068The string \verb|name| does not need to be a
2069syntactically valid variable name.
2070
1981\subsubsection*{\ff \T{setglobal (name, value)}}\Deffunc{setglobal} 2071\subsubsection*{\ff \T{setglobal (name, value)}}\Deffunc{setglobal}
1982This function assigns the given value to a global variable, 2072This function assigns the given value to a global variable,
1983or calls a tag method. 2073or calls a tag method.
1984Its full semantics is explained in \See{tag-method}. 2074Its full semantics is explained in \See{tag-method}.
2075The string \verb|name| does not need to be a
2076syntactically valid variable name.
1985 2077
1986\subsubsection*{\ff \T{rawgetglobal (name)}}\Deffunc{rawgetglobal} 2078\subsubsection*{\ff \T{rawgetglobal (name)}}\Deffunc{rawgetglobal}
1987This function retrieves the value of a global variable. 2079This function retrieves the value of a global variable.
@@ -1993,6 +2085,9 @@ This function retrieves the value of a global variable,
1993or calls a tag method. 2085or calls a tag method.
1994Its full semantics is explained in \See{tag-method}. 2086Its full semantics is explained in \See{tag-method}.
1995 2087
2088The string \verb|name| does not need to be a
2089syntactically valid variable name.
2090
1996\subsubsection*{\ff \T{seterrormethod (newmethod)}} 2091\subsubsection*{\ff \T{seterrormethod (newmethod)}}
1997\label{pdf-seterrormethod} 2092\label{pdf-seterrormethod}
1998Sets the error handler \see{error}. 2093Sets the error handler \see{error}.
@@ -2002,7 +2097,7 @@ Returns the old error handler.
2002 2097
2003\subsubsection*{\ff \T{settagmethod (tag, event, newmethod)}} 2098\subsubsection*{\ff \T{settagmethod (tag, event, newmethod)}}
2004\Deffunc{settagmethod} 2099\Deffunc{settagmethod}
2005This function sets a new tag method to the given pair \M{<tag, event>}. 2100This function sets a new tag method to the given pair \M{(tag, event)}.
2006It returns the old method. 2101It returns the old method.
2007If \verb|newmethod| is \nil, 2102If \verb|newmethod| is \nil,
2008it restores the default behavior for the given event. 2103it restores the default behavior for the given event.
@@ -2010,7 +2105,7 @@ it restores the default behavior for the given event.
2010\subsubsection*{\ff \T{gettagmethod (tag, event)}} 2105\subsubsection*{\ff \T{gettagmethod (tag, event)}}
2011\Deffunc{gettagmethod} 2106\Deffunc{gettagmethod}
2012This function returns the current tag method 2107This function returns the current tag method
2013for a given pair \M{<tag, event>}. 2108for a given pair \M{(tag, event)}.
2014 2109
2015\subsubsection*{\ff \T{copytagmethods (tagto, tagfrom)}} 2110\subsubsection*{\ff \T{copytagmethods (tagto, tagfrom)}}
2016\Deffunc{copytagmethods} 2111\Deffunc{copytagmethods}
@@ -2022,7 +2117,7 @@ it returns \verb|tagto|.
2022This library provides generic functions for string manipulation, 2117This library provides generic functions for string manipulation,
2023such as finding and extracting substrings and pattern matching. 2118such as finding and extracting substrings and pattern matching.
2024When indexing a string, the first character is at position~1 2119When indexing a string, the first character is at position~1
2025(not~0, as in C). 2120(not at~0, as in C).
2026 2121
2027\subsubsection*{\ff \T{strfind (str, pattern [, init [, plain]])}} 2122\subsubsection*{\ff \T{strfind (str, pattern [, init [, plain]])}}
2028\Deffunc{strfind} 2123\Deffunc{strfind}
@@ -2035,6 +2130,10 @@ If the pattern specifies captures,
2035the captured strings are returned as extra results. 2130the captured strings are returned as extra results.
2036A third optional numerical argument specifies where to start the search; 2131A third optional numerical argument specifies where to start the search;
2037its default value is 1. 2132its default value is 1.
2133If \verb|init| is negative,
2134it is replaced by the length of the string minus its
2135absolute value plus 1.
2136Therefore, \M{-1} points to the last character of \verb|str|.
2038A value of 1 as a fourth optional argument 2137A value of 1 as a fourth optional argument
2039turns off the pattern matching facilities, 2138turns off the pattern matching facilities,
2040so the function does a plain ``find substring'' operation, 2139so the function does a plain ``find substring'' operation,
@@ -2049,9 +2148,9 @@ starting at \verb|i| and running until \verb|j|.
2049If \verb|i| or \verb|j| are negative, 2148If \verb|i| or \verb|j| are negative,
2050they are replaced by the length of the string minus their 2149they are replaced by the length of the string minus their
2051absolute value plus 1. 2150absolute value plus 1.
2052Therefore, -1 points to the last character of \verb|s| 2151Therefore, \M{-1} points to the last character of \verb|s|
2053and -2 to the previous one. 2152and \M{-2} to the previous one.
2054If \verb|j| is absent, it is assumed to be equal to -1 2153If \verb|j| is absent, it is assumed to be equal to \M{-1}
2055(which is the same as the string length). 2154(which is the same as the string length).
2056In particular, 2155In particular,
2057the call \verb|strsub(s,1,j)| returns a prefix of \verb|s| 2156the call \verb|strsub(s,1,j)| returns a prefix of \verb|s|
@@ -2063,19 +2162,33 @@ with length \verb|i|.
2063Receives a string and returns a copy of that string with all 2162Receives a string and returns a copy of that string with all
2064upper case letters changed to lower case. 2163upper case letters changed to lower case.
2065All other characters are left unchanged. 2164All other characters are left unchanged.
2165The definition of what is an upper case
2166letter depends on the current locale.
2066 2167
2067\subsubsection*{\ff \T{strupper (s)}}\Deffunc{strupper} 2168\subsubsection*{\ff \T{strupper (s)}}\Deffunc{strupper}
2068Receives a string and returns a copy of that string with all 2169Receives a string and returns a copy of that string with all
2069lower case letters changed to upper case. 2170lower case letters changed to upper case.
2070All other characters are left unchanged. 2171All other characters are left unchanged.
2172The definition of what is a lower case
2173letter depends on the current locale.
2071 2174
2072\subsubsection*{\ff \T{strrep (s, n)}}\Deffunc{strrep} 2175\subsubsection*{\ff \T{strrep (s, n)}}\Deffunc{strrep}
2073Returns a string which is the concatenation of \verb|n| copies of 2176Returns a string that is the concatenation of \verb|n| copies of
2074the string \verb|s|. 2177the string \verb|s|.
2075 2178
2076\subsubsection*{\ff \T{ascii (s [, i])}}\Deffunc{ascii} 2179\subsubsection*{\ff \T{ascii (s [, i])}}\Deffunc{ascii}
2077Returns the ASCII code of the character \verb|s[i]|. 2180Returns the internal numerical code of the character \verb|s[i]|.
2078If \verb|i| is absent, then it is assumed to be 1. 2181If \verb|i| is absent, then it is assumed to be 1.
2182If \verb|i| is negative,
2183it is replaced by the length of the string minus its
2184absolute value plus 1.
2185Therefore, \M{-1} points to the last character of \verb|s|.
2186
2187\subsubsection*{\ff \T{int2str (i1, i2, \ldots)}}\Deffunc{int2str}
2188Receives 0 or more integers.
2189Returns a string with length equal to the number of arguments,
2190wherein each character has ascii value equal
2191to its correspondent argument.
2079 2192
2080\subsubsection*{\ff \T{format (formatstring, e1, e2, \ldots)}}\Deffunc{format} 2193\subsubsection*{\ff \T{format (formatstring, e1, e2, \ldots)}}\Deffunc{format}
2081\label{format} 2194\label{format}
@@ -2122,6 +2235,9 @@ the appropriate format string.
2122For example, \verb|"%*g"| can be simulated with 2235For example, \verb|"%*g"| can be simulated with
2123\verb|"%"..width.."g"|. 2236\verb|"%"..width.."g"|.
2124 2237
2238\emph{Function \verb|format| can only be used with strings that do not
2239contain zeros.}
2240
2125\subsubsection*{\ff \T{gsub (s, pat, repl [, n])}} 2241\subsubsection*{\ff \T{gsub (s, pat, repl [, n])}}
2126\Deffunc{gsub} 2242\Deffunc{gsub}
2127Returns a copy of \verb|s|, 2243Returns a copy of \verb|s|,
@@ -2137,7 +2253,7 @@ stands for the value of the n-th captured substring.
2137 2253
2138If \verb|repl| is a function, then this function is called every time a 2254If \verb|repl| is a function, then this function is called every time a
2139match occurs, with all captured substrings passed as arguments, 2255match occurs, with all captured substrings passed as arguments,
2140in order (see below); 2256in order (see below).
2141If the value returned by this function is a string, 2257If the value returned by this function is a string,
2142then it is used as the replacement string; 2258then it is used as the replacement string;
2143otherwise, the replacement string is the empty string. 2259otherwise, the replacement string is the empty string.
@@ -2213,7 +2329,7 @@ represents the complement of char-set,
2213where char-set is interpreted as above. 2329where char-set is interpreted as above.
2214\end{description} 2330\end{description}
2215 2331
2216The definitions of letter, space, etc depend on the current locale. 2332The definitions of letter, space, etc. depend on the current locale.
2217In particular, the class \verb|[a-z]| may not be equivalent to \verb|%l|. 2333In particular, the class \verb|[a-z]| may not be equivalent to \verb|%l|.
2218The second form should be preferred for more portable programs. 2334The second form should be preferred for more portable programs.
2219 2335
@@ -2283,8 +2399,8 @@ The library provides the following functions:
2283\Deffunc{mod}\Deffunc{sin}\Deffunc{sqrt}\Deffunc{tan} 2399\Deffunc{mod}\Deffunc{sin}\Deffunc{sqrt}\Deffunc{tan}
2284\Deffunc{random}\Deffunc{randomseed} 2400\Deffunc{random}\Deffunc{randomseed}
2285\begin{verbatim} 2401\begin{verbatim}
2286abs acos asin atan atan2 ceil cos floor log log10 2402abs acos asin atan atan2 ceil cos deg floor log log10
2287max min mod sin sqrt tan random randomseed 2403max min mod rad sin sqrt tan random randomseed
2288\end{verbatim} 2404\end{verbatim}
2289plus a global variable \IndexVerb{PI}. 2405plus a global variable \IndexVerb{PI}.
2290Most of them 2406Most of them
@@ -2297,7 +2413,7 @@ between radians and degrees.
2297The function \verb|max| returns the maximum 2413The function \verb|max| returns the maximum
2298value of its numeric arguments. 2414value of its numeric arguments.
2299Similarly, \verb|min| computes the minimum. 2415Similarly, \verb|min| computes the minimum.
2300Both can be used with an unlimited number of arguments. 2416Both can be used with 1, 2 or more arguments.
2301 2417
2302The functions \verb|random| and \verb|randomseed| are interfaces to 2418The functions \verb|random| and \verb|randomseed| are interfaces to
2303the simple random generator functions \verb|rand| and \verb|srand|, 2419the simple random generator functions \verb|rand| and \verb|srand|,
@@ -2305,7 +2421,7 @@ provided by ANSI C.
2305The function \verb|random|, when called without arguments, 2421The function \verb|random|, when called without arguments,
2306returns a pseudo-random real number in the range \Math{[0,1)}. 2422returns a pseudo-random real number in the range \Math{[0,1)}.
2307When called with a number \Math{n}, 2423When called with a number \Math{n},
2308returns a pseudo-random integer in the range \Math{[1,n]}. 2424\verb|random| returns a pseudo-random integer in the range \Math{[1,n]}.
2309 2425
2310 2426
2311\subsection{I/O Facilities} \label{libio} 2427\subsection{I/O Facilities} \label{libio}
@@ -2347,7 +2463,7 @@ plus a string describing the error.
2347\begin{quotation} 2463\begin{quotation}
2348\noindent 2464\noindent
2349\emph{System dependent}: if \verb|filename| starts with a \verb-|-, 2465\emph{System dependent}: if \verb|filename| starts with a \verb-|-,
2350then a \Index{piped input} is open, via function \IndexVerb{popen}. 2466then a \Index{piped input} is opened, via function \IndexVerb{popen}.
2351Not all systems implement pipes. 2467Not all systems implement pipes.
2352Moreover, 2468Moreover,
2353the number of files that can be open at the same time is 2469the number of files that can be open at the same time is
@@ -2375,7 +2491,7 @@ plus a string describing the error.
2375\begin{quotation} 2491\begin{quotation}
2376\noindent 2492\noindent
2377\emph{System dependent}: if \verb|filename| starts with a \verb-|-, 2493\emph{System dependent}: if \verb|filename| starts with a \verb-|-,
2378then a \Index{piped output} is open, via function \IndexVerb{popen}. 2494then a \Index{piped output} is opened, via function \IndexVerb{popen}.
2379Not all systems implement pipes. 2495Not all systems implement pipes.
2380Moreover, 2496Moreover,
2381the number of files that can be open at the same time is 2497the number of files that can be open at the same time is
@@ -2391,7 +2507,8 @@ this function does not erase any previous content of the file.
2391If this function fails, it returns \nil, 2507If this function fails, it returns \nil,
2392plus a string describing the error. 2508plus a string describing the error.
2393 2509
2394Notice that function \verb|writeto| is available to close an output file. 2510Notice that function \verb|writeto| is
2511available to close an output file opened by \verb|appendto|.
2395 2512
2396\subsubsection*{\ff \T{remove (filename)}}\Deffunc{remove} 2513\subsubsection*{\ff \T{remove (filename)}}\Deffunc{remove}
2397 2514
@@ -2557,13 +2674,13 @@ given function has been defined.
2557If the ``function'' is in fact the main code of a chunk, 2674If the ``function'' is in fact the main code of a chunk,
2558then \verb|linedefined| is 0. 2675then \verb|linedefined| is 0.
2559If the function is a C function, 2676If the function is a C function,
2560then \verb|linedefined| is -1, and \verb|filename| is \verb|"(C)"|. 2677then \verb|linedefined| is \M{-1}, and \verb|filename| is \verb|"(C)"|.
2561 2678
2562The function \verb|lua_currentline| gives the current line where 2679The function \verb|lua_currentline| gives the current line where
2563a given function is executing. 2680a given function is executing.
2564It only works if the function has been compiled with debug 2681It only works if the function has been compiled with debug
2565information \see{pragma}. 2682information \see{pragma}.
2566When no line information is available, it returns -1. 2683When no line information is available, it returns \M{-1}.
2567 2684
2568Function \verb|lua_getobjname| tries to find a reasonable name for 2685Function \verb|lua_getobjname| tries to find a reasonable name for
2569a given function. 2686a given function.
@@ -2661,12 +2778,12 @@ accepting commands from standard input until an \verb|EOF|.
2661Each line entered is immediately executed. 2778Each line entered is immediately executed.
2662\item[\T{-q}] same as \T{-i}, but without a prompt (quiet mode). 2779\item[\T{-q}] same as \T{-i}, but without a prompt (quiet mode).
2663\item[\T{-}] executes \verb|stdin| as a file. 2780\item[\T{-}] executes \verb|stdin| as a file.
2664\item[\T{var=value}] sets global \verb|var| with string \verb|value|. 2781\item[\T{var=value}] sets global \verb|var| with string \verb|"value"|.
2665\item[\T{filename}] executes file \verb|filename| as a Lua chunk. 2782\item[\T{filename}] executes file \verb|filename| as a Lua chunk.
2666\end{description} 2783\end{description}
2667When called without arguments, 2784When called without arguments,
2668Lua behaves as \verb|lua -v -i| when \verb|stdin| is a terminal, 2785Lua behaves as \verb|lua -v -i| when \verb|stdin| is a terminal,
2669and \verb|lua -| otherwise. 2786and as \verb|lua -| otherwise.
2670 2787
2671All arguments are handled in order. 2788All arguments are handled in order.
2672For instance, an invocation like 2789For instance, an invocation like
@@ -2675,13 +2792,13 @@ $ lua -i a=test prog.lua
2675\end{verbatim} 2792\end{verbatim}
2676will first interact with the user until an \verb|EOF|, 2793will first interact with the user until an \verb|EOF|,
2677then will set \verb|a| to \verb|"test"|, 2794then will set \verb|a| to \verb|"test"|,
2678and finally will run file \verb|prog.lua|. 2795and finally will run the file \verb|prog.lua|.
2679 2796
2680When in interactive mode, 2797When in interactive mode,
2681a multi-line statement can be written finishing intermediate 2798a multi-line statement can be written finishing intermediate
2682lines with a backslash (\verb|\|). 2799lines with a backslash (\verb|\|).
2683The prompt presented is the value of the global variable \verb|_PROMPT|. 2800The prompt presented is the value of the global variable \verb|_PROMPT|.
2684For instance, the prompt can be changed like below: 2801Therefore, the prompt can be changed like below:
2685\begin{verbatim} 2802\begin{verbatim}
2686$ lua _PROMPT='myprompt> ' -i 2803$ lua _PROMPT='myprompt> ' -i
2687\end{verbatim} 2804\end{verbatim}
@@ -2712,8 +2829,8 @@ Here is a list of all these incompatibilities.
2712\item To support multiple contexts, 2829\item To support multiple contexts,
2713Lua 3.1 must be explicitly opened before used. 2830Lua 3.1 must be explicitly opened before used.
2714However, all standard libraries check whether Lua is already opened, 2831However, all standard libraries check whether Lua is already opened,
2715so any program that opens at least one standard library before using 2832so any program that opens at least one standard library before calling
2716Lua API does not need to be modified. 2833Lua does not need to be modified.
2717 2834
2718\item Function \verb|dostring| no longer accepts an optional second argument, 2835\item Function \verb|dostring| no longer accepts an optional second argument,
2719with a temporary error method. 2836with a temporary error method.
@@ -2725,9 +2842,9 @@ Closures replace this feature with advantage.
2725 2842
2726\item The syntax for function declaration is now more restricted; 2843\item The syntax for function declaration is now more restricted;
2727for instance, the old syntax \verb|function f[exp] (x) ... end| is not 2844for instance, the old syntax \verb|function f[exp] (x) ... end| is not
2728accepted in 3.1. 2845accepted in Lua 3.1.
2729In these cases, 2846In these cases,
2730programs should use an explicit assignment instead, like 2847programs should use an explicit assignment instead, such as
2731\verb|f[exp] = function (x) ... end|. 2848\verb|f[exp] = function (x) ... end|.
2732 2849
2733\item Old pre-compiled code is obsolete, and must be re-compiled. 2850\item Old pre-compiled code is obsolete, and must be re-compiled.