summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-11-07 10:44:44 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-11-07 10:44:44 -0200
commit523c5d8e1c5c62f16586e93baceb6fcddd0a3ded (patch)
tree7a2d415fe72859f994b34bb9f86beb1a419f8381
parent40a4c767739f70645ea71bbf6330f4ad5e48ca55 (diff)
downloadlua-4.0.tar.gz
lua-4.0.tar.bz2
lua-4.0.zip
last changes by lhf.v4.0
-rw-r--r--lundump.c4
-rw-r--r--lundump.h10
-rw-r--r--manual.tex1026
3 files changed, 518 insertions, 522 deletions
diff --git a/lundump.c b/lundump.c
index 022b25ba..a8d06106 100644
--- a/lundump.c
+++ b/lundump.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lundump.c,v 1.32 2000/09/21 03:15:36 lhf Exp lhf $ 2** $Id: lundump.c,v 1.33 2000/10/31 16:57:23 lhf Exp $
3** load bytecodes from files 3** load bytecodes from files
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -178,7 +178,7 @@ static void TestSize (lua_State* L, int s, const char* what, ZIO* Z)
178 int r=ezgetc(L,Z); 178 int r=ezgetc(L,Z);
179 if (r!=s) 179 if (r!=s)
180 luaO_verror(L,"virtual machine mismatch in `%.99s':\n" 180 luaO_verror(L,"virtual machine mismatch in `%.99s':\n"
181 " %s is %d but read %d",ZNAME(Z),what,s,r); 181 " %.20s is %d but read %d",ZNAME(Z),what,s,r);
182} 182}
183 183
184#define TESTSIZE(s) TestSize(L,s,#s,Z) 184#define TESTSIZE(s) TestSize(L,s,#s,Z)
diff --git a/lundump.h b/lundump.h
index 703a3e34..446d2de9 100644
--- a/lundump.h
+++ b/lundump.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lundump.h,v 1.20 2000/09/18 20:03:46 lhf Exp lhf $ 2** $Id: lundump.h,v 1.21 2000/10/31 16:57:23 lhf Exp $
3** load pre-compiled Lua chunks 3** load pre-compiled Lua chunks
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -23,10 +23,10 @@ int luaU_endianess (void);
23#define SIGNATURE "Lua" /* ...followed by this signature */ 23#define SIGNATURE "Lua" /* ...followed by this signature */
24 24
25/* formats for error messages */ 25/* formats for error messages */
26#define xSOURCE "<%d:%.255s>" 26#define SOURCE_FMT "<%d:%.99s>"
27#define SOURCE "<%.255s:%d>" 27#define SOURCE tf->lineDefined,tf->source->str
28#define IN " in %p " SOURCE 28#define IN_FMT " in %p " SOURCE_FMT
29#define INLOC tf,tf->source->str,tf->lineDefined 29#define IN tf,SOURCE
30 30
31/* a multiple of PI for testing native format */ 31/* a multiple of PI for testing native format */
32/* multiplying by 1E8 gives non-trivial integer values */ 32/* multiplying by 1E8 gives non-trivial integer values */
diff --git a/manual.tex b/manual.tex
index 817f17ad..cac5d19c 100644
--- a/manual.tex
+++ b/manual.tex
@@ -1,25 +1,31 @@
1% $Id: manual.tex,v 1.44 2000/09/20 17:21:20 roberto Exp roberto $ 1% $Id: manual.tex,v 1.45 2000/10/31 18:20:01 roberto Exp roberto $
2 2
3\documentclass[11pt]{article} 3\documentclass[11pt]{article}
4\usepackage{fullpage,bnf} 4\usepackage{fullpage}
5\usepackage{bnf}
5\usepackage{graphicx} 6\usepackage{graphicx}
6%\usepackage{times} 7%\usepackage{times}
7 8
8\catcode`\_=12 9\catcode`\_=12
9 10
10\newcommand{\See}[1]{Section~\ref{#1}} 11%\newcommand{\See}[1]{Section~\ref{#1}}
11\newcommand{\see}[1]{(see \See{#1})} 12\newcommand{\See}[1]{\S\ref{#1}}
12\newcommand{\M}[1]{\rm\emph{#1}} 13\newcommand{\see}[1]{(see~\See{#1})}
14\newcommand{\M}[1]{{\rm\emph{#1}}}
13\newcommand{\T}[1]{{\tt #1}} 15\newcommand{\T}[1]{{\tt #1}}
14\newcommand{\Math}[1]{$#1$} 16\newcommand{\Math}[1]{$#1$}
15\newcommand{\nil}{{\bf nil}} 17\newcommand{\nil}{{\bf nil}}
16\def\tecgraf{{\sf TeC\kern-.21em\lower.7ex\hbox{Graf}}} 18\def\tecgraf{{\sf TeC\kern-.21em\lower.7ex\hbox{Graf}}}
17 19
18\newcommand{\Index}[1]{#1\index{#1}} 20\newcommand{\Index}[1]{#1\index{#1@{\lowercase{#1}}}}
19\newcommand{\IndexVerb}[1]{\T{#1}\index{#1}} 21\newcommand{\IndexVerb}[1]{\T{#1}\index{#1@{\tt #1}}}
20\newcommand{\IndexEmph}[1]{\emph{#1}\index{#1}} 22\newcommand{\IndexEmph}[1]{\emph{#1}\index{#1@{\lowercase{#1}}}}
23\newcommand{\IndexTM}[1]{\index{#1 event@{``#1'' event}}\index{tag method!#1}}
21\newcommand{\Def}[1]{\emph{#1}\index{#1}} 24\newcommand{\Def}[1]{\emph{#1}\index{#1}}
22\newcommand{\Deffunc}[1]{\index{#1}} 25\newcommand{\IndexAPI}[1]{\T{#1}\DefAPI{#1}}
26\newcommand{\IndexLIB}[1]{\T{#1}\DefLIB{#1}}
27\newcommand{\DefLIB}[1]{\index{#1@{\tt #1}}}
28\newcommand{\DefAPI}[1]{\index{C API!#1@{\tt #1}}}
23 29
24\newcommand{\ff}{$\bullet$\ } 30\newcommand{\ff}{$\bullet$\ }
25 31
@@ -27,7 +33,6 @@
27 33
28% LHF 34% LHF
29\renewcommand{\ter}[1]{{\rm`{\tt#1}'}} 35\renewcommand{\ter}[1]{{\rm`{\tt#1}'}}
30\newcommand{\Nter}[1]{{\rm{\tt#1}}}
31\newcommand{\NOTE}{\par\medskip\noindent\emph{NOTE}: } 36\newcommand{\NOTE}{\par\medskip\noindent\emph{NOTE}: }
32 37
33\makeindex 38\makeindex
@@ -97,8 +102,8 @@ and its documentation.
97 102
98\noindent 103\noindent
99The Lua language and this implementation have been entirely designed and 104The Lua language and this implementation have been entirely designed and
100written by Waldemar Celes, Roberto Ierusalimschy and Luiz Henrique de 105written by Waldemar Celes, Roberto Ierusalimschy, and Luiz Henrique de
101Figueiredo at TeCGraf, PUC-Rio. 106Figueiredo at TeCGraf, PUC-Rio in Brazil.
102 107
103\noindent 108\noindent
104This implementation contains no third-party code. 109This implementation contains no third-party code.
@@ -129,16 +134,13 @@ Waldemar Celes
129\tecgraf\ --- Computer Science Department --- PUC-Rio 134\tecgraf\ --- Computer Science Department --- PUC-Rio
130} 135}
131 136
132\date{{\small \tt\$Date: 2000/09/20 17:21:20 $ $}} 137\date{{\small \tt\$Date: 2000/10/31 18:20:01 $ $}}
133 138
134\maketitle 139\maketitle
135 140
136\pagestyle{plain} 141\pagestyle{plain}
137\pagenumbering{roman} 142\pagenumbering{roman}
138 143
139%\thispagestyle{empty}
140%\pagestyle{empty}
141
142\begin{abstract} 144\begin{abstract}
143\noindent 145\noindent
144Lua is a powerful, light-weight programming language 146Lua is a powerful, light-weight programming language
@@ -197,8 +199,8 @@ a intera\c{c}\~ao entre programas Lua e programas C~hospedeiros.
197 199
198\newpage 200\newpage
199\setcounter{page}{1} 201\setcounter{page}{1}
200\pagenumbering{arabic}
201\pagestyle{plain} 202\pagestyle{plain}
203\pagenumbering{arabic}
202 204
203 205
204\section{Introduction} 206\section{Introduction}
@@ -269,8 +271,8 @@ using API functions from the library that implements Lua.
269\Index{Global variables} in Lua do not need to be declared. 271\Index{Global variables} in Lua do not need to be declared.
270Any variable is assumed to be global unless explicitly declared local 272Any variable is assumed to be global unless explicitly declared local
271\see{localvar}. 273\see{localvar}.
272Before the first assignment, the value of a global variable is \nil; 274Before the first assignment, the value of a global variable is \nil\ %
273this default can be changed \see{tag-method}. 275(this default can be changed; see \See{tag-method}).
274A table is used to keep all global names and values 276A table is used to keep all global names and values
275(tables are explained in \See{TypesSec}). 277(tables are explained in \See{TypesSec}).
276 278
@@ -291,11 +293,12 @@ The complete syntax of Lua is given on page~\pageref{BNF}.)
291 293
292A chunk may be stored in a file or in a string inside the host program. 294A chunk may be stored in a file or in a string inside the host program.
293When a chunk is executed, first it is pre-compiled into bytecodes for 295When a chunk is executed, first it is pre-compiled into bytecodes for
294a virtual machine, and then the statements are executed in sequential order. 296a virtual machine, and then the statements are executed in sequential order,
297by simulating the virtual machine.
295All modifications a chunk effects on the global environment persist 298All modifications a chunk effects on the global environment persist
296after the chunk ends. 299after the chunk ends.
297 300
298Chunks may also be pre-compiled into binary form; 301Chunks may also be pre-compiled into binary form and stored in files;
299see program \IndexVerb{luac} for details. 302see program \IndexVerb{luac} for details.
300Text files with chunks and their binary pre-compiled forms 303Text files with chunks and their binary pre-compiled forms
301are interchangeable. 304are interchangeable.
@@ -320,7 +323,7 @@ while \emph{string} has the usual meaning.
320\index{eight-bit clean} 323\index{eight-bit clean}
321Lua is 8-bit clean, 324Lua is 8-bit clean,
322and so strings may contain any 8-bit character, 325and so strings may contain any 8-bit character,
323\emph{including} embedded zeros (\verb|'\0'|) \see{lexical}. 326including embedded zeros (\verb|'\0'|) \see{lexical}.
324The \verb|type| function returns a string describing the type 327The \verb|type| function returns a string describing the type
325of a given value \see{pdf-type}. 328of a given value \see{pdf-type}.
326 329
@@ -329,7 +332,7 @@ This means that functions can be stored in variables,
329passed as arguments to other functions, and returned as results. 332passed as arguments to other functions, and returned as results.
330Lua can call (and manipulate) functions written in Lua and 333Lua can call (and manipulate) functions written in Lua and
331functions written in C. 334functions written in C.
332The kinds of functions can be distinguished by their tags: 335The two kinds of functions can be distinguished by their tags:
333all Lua functions have the same tag, 336all Lua functions have the same tag,
334and all C~functions have the same tag, 337and all C~functions have the same tag,
335which is different from the tag of Lua functions. 338which is different from the tag of Lua functions.
@@ -337,10 +340,10 @@ The \verb|tag| function returns the tag
337of a given value \see{pdf-tag}. 340of a given value \see{pdf-tag}.
338 341
339The type \emph{userdata} is provided to allow 342The type \emph{userdata} is provided to allow
340arbitrary \Index{C pointers} to be stored in Lua variables. 343arbitrary \Index{C~pointers} to be stored in Lua variables.
341This type corresponds to a \verb|void*| 344This type corresponds to a \verb|void*|
342and has no pre-defined operations in Lua, 345and has no pre-defined operations in Lua,
343except for assignment and equality test. 346except assignment and equality test.
344However, by using \emph{tag methods}, 347However, by using \emph{tag methods},
345the programmer can define operations for \emph{userdata} values 348the programmer can define operations for \emph{userdata} values
346\see{tag-method}. 349\see{tag-method}.
@@ -349,7 +352,7 @@ The type \emph{table} implements \Index{associative arrays},
349that is, \Index{arrays} that can be indexed not only with numbers, 352that is, \Index{arrays} that can be indexed not only with numbers,
350but with any value (except \nil). 353but with any value (except \nil).
351Therefore, this type may be used not only to represent ordinary arrays, 354Therefore, this type may be used not only to represent ordinary arrays,
352but also symbol tables, sets, records, etc. 355but also symbol tables, sets, records, graphs, trees, etc.
353Tables are the main data structuring mechanism in Lua. 356Tables are the main data structuring mechanism in Lua.
354To represent \Index{records}, Lua uses the field name as an index. 357To represent \Index{records}, Lua uses the field name as an index.
355The language supports this representation by 358The language supports this representation by
@@ -370,17 +373,17 @@ Moreover, tables must be explicitly created before used
370 373
371Each of the types \M{nil}, \M{number}, and \M{string} has a different tag. 374Each of the types \M{nil}, \M{number}, and \M{string} has a different tag.
372All values of each of these types have the same pre-defined tag. 375All values of each of these types have the same pre-defined tag.
373Values of type \M{function} can have two different tags, 376As explained above,
377values of type \M{function} can have two different tags,
374depending on whether they are Lua functions or C~functions. 378depending on whether they are Lua functions or C~functions.
375Finally, 379Finally,
376values of type \M{userdata} and \M{table} can have variable tags, 380values of type \M{userdata} and \M{table} can have variable tags,
377assigned by the programmer \see{tag-method}. 381assigned by the programmer \see{tag-method}.
378The function \verb|tag| returns the tag of a given value. 382The \verb|tag| function returns the tag of a given value.
379User tags are created with the function \verb|newtag|, 383User tags are created with the function \verb|newtag|.
380The function \verb|settag| \see{pdf-newtag} 384The \verb|settag| function
381is used to change the tag of a table. 385is used to change the tag of a table \see{pdf-newtag}.
382The data of userdata can only be set from~C. 386The tag of userdata values can only be set from~C \see{C-tags}.
383
384Tags are mainly used to select \emph{tag methods} when 387Tags are mainly used to select \emph{tag methods} when
385some events occur. 388some events occur.
386Tag methods are the main mechanism for extending the 389Tag methods are the main mechanism for extending the
@@ -528,12 +531,11 @@ A block may be explicitly delimited:
528\begin{Produc} 531\begin{Produc}
529\produc{stat}{\rwd{do} block \rwd{end}} 532\produc{stat}{\rwd{do} block \rwd{end}}
530\end{Produc}% 533\end{Produc}%
531This is useful to control the scope of local variables \see{localvar}, 534Explicit blocks are useful
532and to add a \rwd{return} or \rwd{break} statement in the middle 535to control the scope of local variables \see{localvar}.
533of another block; for instance, 536Explicit blocks are also sometimes used to
534\begin{verbatim} 537add a \rwd{return} or \rwd{break} statement in the middle
535 do return end -- return is the last statement in this block 538of another block \see{control}.
536\end{verbatim}
537 539
538\subsubsection{\Index{Assignment}} \label{assignment} 540\subsubsection{\Index{Assignment}} \label{assignment}
539Lua allows \Index{multiple assignment}. 541Lua allows \Index{multiple assignment}.
@@ -548,7 +550,7 @@ The elements in both lists are separated by commas:
548This statement first evaluates all values on the right side 550This statement first evaluates all values on the right side
549and eventual indices on the left side, 551and eventual indices on the left side,
550and then makes the assignments. 552and then makes the assignments.
551So 553So, the code
552\begin{verbatim} 554\begin{verbatim}
553 i = 3 555 i = 3
554 i, a[i] = 4, 20 556 i, a[i] = 4, 20
@@ -571,6 +573,7 @@ or a formal parameter:
571\begin{Produc} 573\begin{Produc}
572\produc{var}{name} 574\produc{var}{name}
573\end{Produc}% 575\end{Produc}%
576
574Square brackets are used to index a table: 577Square brackets are used to index a table:
575\begin{Produc} 578\begin{Produc}
576\produc{var}{varorfunc \ter{[} exp1 \ter{]}} 579\produc{var}{varorfunc \ter{[} exp1 \ter{]}}
@@ -590,18 +593,21 @@ The meaning of assignments and evaluations of global variables and
590indexed variables can be changed by tag methods \see{tag-method}. 593indexed variables can be changed by tag methods \see{tag-method}.
591Actually, 594Actually,
592an assignment \verb|x = val|, where \verb|x| is a global variable, 595an assignment \verb|x = val|, where \verb|x| is a global variable,
593is equivalent to a call \verb|setglobal("x", val)|; 596is equivalent to a call \verb|setglobal("x", val)| and
594an assignment \verb|t[i] = val| is equivalent to 597an assignment \verb|t[i] = val| is equivalent to
595\verb|settable_event(t,i,val)|. 598\verb|settable_event(t,i,val)|.
596See \See{tag-method} for a complete description of these functions. 599See \See{tag-method} for a complete description of these functions
597(The function \verb|setglobal| is defined in the basic library. 600(\verb|setglobal| is in the basic library;
598The function \T{settable\_event} is used only for explanatory purposes.) 601\T{settable\_event} is used for explanatory purposes only).
599 602
600\subsubsection{Control Structures} 603\subsubsection{Control Structures}\label{control}
601The control structures 604The control structures
602\index{while-do}\index{repeat-until}\index{if-then-else}% 605\rwd{if}, \rwd{while}, and \rwd{repeat} have the usual meaning and
603\T{if}, \T{while}, and \T{repeat} have the usual meaning and 606familiar syntax
604familiar syntax: 607%(there is also a \rwd{for} statement; see \See{for}):
608\index{while-do statement}
609\index{repeat-until statement}
610\index{if-then-else statement}
605\begin{Produc} 611\begin{Produc}
606\produc{stat}{\rwd{while} exp1 \rwd{do} block \rwd{end}} 612\produc{stat}{\rwd{while} exp1 \rwd{do} block \rwd{end}}
607\produc{stat}{\rwd{repeat} block \rwd{until} exp1} 613\produc{stat}{\rwd{repeat} block \rwd{until} exp1}
@@ -613,34 +619,39 @@ The \Index{condition expression} \M{exp1} of a control structure may return any
613All values different from \nil\ are considered true; 619All values different from \nil\ are considered true;
614only \nil\ is considered false. 620only \nil\ is considered false.
615 621
616\index{return}
617The \rwd{return} statement is used to return values 622The \rwd{return} statement is used to return values
618from a function or from a chunk. 623from a function or from a chunk.
619\label{return} 624\label{return}%
625\index{return statement}%
620Because functions or chunks may return more than one value, 626Because functions or chunks may return more than one value,
621the syntax for a \Index{return statement} is 627the syntax for the \rwd{return} statement is
622\begin{Produc} 628\begin{Produc}
623\produc{stat}{\rwd{return} \opt{explist1}} 629\produc{stat}{\rwd{return} \opt{explist1}}
624\end{Produc}% 630\end{Produc}%
625 631
626\index{break}
627The \rwd{break} statement can be used to terminate the execution of a loop, 632The \rwd{break} statement can be used to terminate the execution of a loop,
628skipping to the next statement after the loop: 633skipping to the next statement after the loop:
634\index{break statement}
629\begin{Produc} 635\begin{Produc}
630\produc{stat}{\rwd{break}} 636\produc{stat}{\rwd{break}}
631\end{Produc}% 637\end{Produc}%
632A \rwd{break} ends the innermost enclosing loop 638A \rwd{break} ends the innermost enclosing loop
633(while, repeat, or for). 639(\rwd{while}, \rwd{repeat}, or \rwd{for}).
634 640
635\NOTE 641\NOTE
636For syntactic reasons, \rwd{return} and \rwd{break} 642For syntactic reasons, \rwd{return} and \rwd{break}
637statements can only be written as the last statements of a block. 643statements can only be written as the \emph{last} statements of a block.
644If it is really necessary to \rwd{return} or \rwd{break} in the
645middle of a block,
646an explicit inner block can used,
647as in the idiom `\verb|do return end|',
648because now \rwd{return} is last statement in the inner block.
638 649
639\subsubsection{For Statement} \label{for}\index{for} 650\subsubsection{For Statement} \label{for}\index{for statement}
640 651
641The \rwd{for} statement has two forms, 652The \rwd{for} statement has two forms,
642one for numbers and one for tables. 653one for numbers and one for tables.
643 654\newpage
644The numerical \rwd{for} loop has the following syntax: 655The numerical \rwd{for} loop has the following syntax:
645\begin{Produc} 656\begin{Produc}
646\produc{stat}{\rwd{for} name \ter{=} exp1 \ter{,} exp1 \opt{\ter{,} exp1} 657\produc{stat}{\rwd{for} name \ter{=} exp1 \ter{,} exp1 \opt{\ter{,} exp1}
@@ -667,7 +678,7 @@ Note the following:
667The names are here for explanatory purposes only. 678The names are here for explanatory purposes only.
668\item The behavior is \emph{undefined} if you assign to \verb|var| inside 679\item The behavior is \emph{undefined} if you assign to \verb|var| inside
669the block. 680the block.
670\item If the third expression (the step) is absent, then a step of 1 is used. 681\item If the third expression (the step) is absent, then a step of~1 is used.
671\item Both the limit and the step are evaluated only once, 682\item Both the limit and the step are evaluated only once,
672before the loop starts. 683before the loop starts.
673\item The variable \verb|var| is local to the statement; 684\item The variable \verb|var| is local to the statement;
@@ -741,7 +752,7 @@ of a multiple assignment.
741Otherwise, all variables are initialized with \nil. 752Otherwise, all variables are initialized with \nil.
742 753
743A chunk is also a block, 754A chunk is also a block,
744so local variables can be declared outside any explicit block. 755and so local variables can be declared outside any explicit block.
745 756
746The scope of local variables begins \emph{after} 757The scope of local variables begins \emph{after}
747the declaration and lasts until the end of the block. 758the declaration and lasts until the end of the block.
@@ -760,9 +771,9 @@ The basic expressions in Lua are
760\produc{exp}{\rwd{nil}} 771\produc{exp}{\rwd{nil}}
761\produc{exp}{number} 772\produc{exp}{number}
762\produc{exp}{literal} 773\produc{exp}{literal}
763\produc{exp}{function}
764\produc{exp}{var} 774\produc{exp}{var}
765\produc{exp}{upvalue} 775\produc{exp}{upvalue}
776\produc{exp}{function}
766\produc{exp}{functioncall} 777\produc{exp}{functioncall}
767\produc{exp}{tableconstructor} 778\produc{exp}{tableconstructor}
768\end{Produc}% 779\end{Produc}%
@@ -771,17 +782,17 @@ Numbers (numerical constants) and
771literal strings are explained in \See{lexical}; 782literal strings are explained in \See{lexical};
772variables are explained in \See{assignment}; 783variables are explained in \See{assignment};
773upvalues are explained in \See{upvalue}; 784upvalues are explained in \See{upvalue};
774function definitions (\M{function}) are explained in \See{func-def}; 785function definitions are explained in \See{func-def};
775function calls are explained in \See{functioncall}. 786function calls are explained in \See{functioncall}.
776Table constructors are explained in \See{tableconstructor}. 787Table constructors are explained in \See{tableconstructor}.
777 788
778An access to a global variable \verb|x| is equivalent to a 789An access to a global variable \verb|x| is equivalent to a
779call \verb|getglobal("x")|; 790call \verb|getglobal("x")| and
780an access to an indexed variable \verb|t[i]| is equivalent to 791an access to an indexed variable \verb|t[i]| is equivalent to
781a call \verb|gettable_event(t,i)|. 792a call \verb|gettable_event(t,i)|.
782See \See{tag-method} for a description of these functions. 793See \See{tag-method} for a description of these functions
783(Function \verb|getglobal| is defined in the basic library. 794(\verb|getglobal| is in the basic library;
784Function \T{gettable\_event} is used only for explanatory purposes.) 795\T{gettable\_event} is used for explanatory purposes only).
785 796
786The non-terminal \M{exp1} is used to indicate that the values 797The non-terminal \M{exp1} is used to indicate that the values
787returned by an expression must be adjusted to one single value: 798returned by an expression must be adjusted to one single value:
@@ -849,7 +860,7 @@ if it is different from \nil;
849otherwise, it returns its second argument. 860otherwise, it returns its second argument.
850Both \verb|and| and \verb|or| use \Index{short-cut evaluation}, 861Both \verb|and| and \verb|or| use \Index{short-cut evaluation},
851that is, 862that is,
852the second operand is evaluated only when necessary. 863the second operand is evaluated only if necessary.
853 864
854There are two useful Lua idioms that use logical operators. 865There are two useful Lua idioms that use logical operators.
855The first idiom is 866The first idiom is
@@ -907,7 +918,7 @@ tag methods for these operators.
907Table \Index{constructors} are expressions that create tables; 918Table \Index{constructors} are expressions that create tables;
908every time a constructor is evaluated, a new table is created. 919every time a constructor is evaluated, a new table is created.
909Constructors can be used to create empty tables, 920Constructors can be used to create empty tables,
910or to create a table and initialize some fields. 921or to create a table and initialize some of its fields.
911The general syntax for constructors is 922The general syntax for constructors is
912\begin{Produc} 923\begin{Produc}
913\produc{tableconstructor}{\ter{\{} fieldlist \ter{\}}} 924\produc{tableconstructor}{\ter{\{} fieldlist \ter{\}}}
@@ -922,7 +933,7 @@ The form \emph{lfieldlist1} is used to initialize lists:
922\produc{lfieldlist1}{exp \rep{\ter{,} exp} \opt{\ter{,}}} 933\produc{lfieldlist1}{exp \rep{\ter{,} exp} \opt{\ter{,}}}
923\end{Produc}% 934\end{Produc}%
924The expressions in the list are assigned to consecutive numerical indices, 935The expressions in the list are assigned to consecutive numerical indices,
925starting with 1. 936starting with~1.
926For example, 937For example,
927\begin{verbatim} 938\begin{verbatim}
928 a = {"v1", "v2", 34} 939 a = {"v1", "v2", 34}
@@ -983,7 +994,8 @@ then this function is called,
983with the given arguments. 994with the given arguments.
984Otherwise, the ``function'' tag method is called, 995Otherwise, the ``function'' tag method is called,
985having as first parameter the value of \M{varorfunc}, 996having as first parameter the value of \M{varorfunc},
986and then the original call arguments. 997and then the original call arguments
998\see{tag-method}.
987 999
988The form 1000The form
989\begin{Produc} 1001\begin{Produc}
@@ -998,21 +1010,21 @@ Arguments have the following syntax:
998\begin{Produc} 1010\begin{Produc}
999\produc{args}{\ter{(} \opt{explist1} \ter{)}} 1011\produc{args}{\ter{(} \opt{explist1} \ter{)}}
1000\produc{args}{tableconstructor} 1012\produc{args}{tableconstructor}
1001\produc{args}{\Nter{literal}} 1013\produc{args}{literal}
1002\produc{explist1}{\rep{exp1 \ter{,}} exp} 1014\produc{explist1}{\rep{exp1 \ter{,}} exp}
1003\end{Produc}% 1015\end{Produc}%
1004All argument expressions are evaluated before the call. 1016All argument expressions are evaluated before the call.
1005A call of the form \verb|f{...}| is syntactic sugar for 1017A call of the form \verb|f{...}| is syntactic sugar for
1006\verb|f({...})|, that is, 1018\verb|f({...})|, that is,
1007the parameter list is a single new table. 1019the argument list is a single new table.
1008A call of the form \verb|f'...'| 1020A call of the form \verb|f'...'|
1009(or \verb|f"..."| or \verb|f[[...]]|) is syntactic sugar for 1021(or \verb|f"..."| or \verb|f[[...]]|) is syntactic sugar for
1010\verb|f('...')|, that is, 1022\verb|f('...')|, that is,
1011the parameter list is a single literal string. 1023the argument list is a single literal string.
1012 1024
1013Because a function can return any number of results 1025Because a function can return any number of results
1014\see{return}, 1026\see{return},
1015the number of results must be adjusted before they are used. 1027the number of results must be adjusted before they are used \see{adjust}.
1016If the function is called as a statement \see{funcstat}, 1028If the function is called as a statement \see{funcstat},
1017then its return list is adjusted to~0, 1029then its return list is adjusted to~0,
1018thus discarding all returned values. 1030thus discarding all returned values.
@@ -1025,17 +1037,17 @@ If the function is called in a place that can hold many values
1025then no adjustment is made. 1037then no adjustment is made.
1026The only places that can hold many values 1038The only places that can hold many values
1027is the last (or the only) expression in an assignment, 1039is the last (or the only) expression in an assignment,
1028in an argument list, or in a return statement. 1040in an argument list, or in the \rwd{return} statement.
1029Here are some examples: 1041Here are some examples:
1030\begin{verbatim} 1042\begin{verbatim}
1031 f(); -- adjusted to 0 results 1043 f() -- adjusted to 0 results
1032 g(f(), x); -- f() is adjusted to 1 result 1044 g(f(), x) -- f() is adjusted to 1 result
1033 g(x, f()); -- g gets x plus all values returned by f() 1045 g(x, f()) -- g gets x plus all values returned by f()
1034 a,b,c = f(), x; -- f() is adjusted to 1 result (and c gets nil) 1046 a,b,c = f(), x -- f() is adjusted to 1 result (and c gets nil)
1035 a,b,c = x, f(); -- f() is adjusted to 2 1047 a,b,c = x, f() -- f() is adjusted to 2
1036 a,b,c = f(); -- f() is adjusted to 3 1048 a,b,c = f() -- f() is adjusted to 3
1037 return f(); -- returns all values returned by f() 1049 return f() -- returns all values returned by f()
1038 return x,y,f(); -- returns a, b, and all values returned by f() 1050 return x,y,f() -- returns a, b, and all values returned by f()
1039\end{verbatim} 1051\end{verbatim}
1040 1052
1041\subsubsection{\Index{Function Definitions}} \label{func-def} 1053\subsubsection{\Index{Function Definitions}} \label{func-def}
@@ -1050,19 +1062,19 @@ The syntax for function definition is
1050\end{Produc}% 1062\end{Produc}%
1051The statement 1063The statement
1052\begin{verbatim} 1064\begin{verbatim}
1053 function f () ... end 1065 function f () ... end
1054\end{verbatim} 1066\end{verbatim}
1055is just syntactic sugar for 1067is just syntactic sugar for
1056\begin{verbatim} 1068\begin{verbatim}
1057 f = function () ... end 1069 f = function () ... end
1058\end{verbatim} 1070\end{verbatim}
1059and the statement 1071and the statement
1060\begin{verbatim} 1072\begin{verbatim}
1061 function v.f () ... end 1073 function v.f () ... end
1062\end{verbatim} 1074\end{verbatim}
1063is syntactic sugar for 1075is syntactic sugar for
1064\begin{verbatim} 1076\begin{verbatim}
1065 v.f = function () ... end 1077 v.f = function () ... end
1066\end{verbatim} 1078\end{verbatim}
1067 1079
1068A function definition is an executable expression, 1080A function definition is an executable expression,
@@ -1083,19 +1095,19 @@ initialized with the argument values:
1083\produc{parlist1}{\ter{\ldots}} 1095\produc{parlist1}{\ter{\ldots}}
1084\produc{parlist1}{name \rep{\ter{,} name} \opt{\ter{,} \ter{\ldots}}} 1096\produc{parlist1}{name \rep{\ter{,} name} \opt{\ter{,} \ter{\ldots}}}
1085\end{Produc}% 1097\end{Produc}%
1086\label{vararg} 1098\label{vararg}%
1087When a function is called, 1099When a function is called,
1088the list of \Index{arguments} is adjusted to 1100the list of \Index{arguments} is adjusted to
1089the length of the list of parameters \see{adjust}, 1101the length of the list of parameters \see{adjust},
1090unless the function is a \Def{vararg} function, 1102unless the function is a \Def{vararg function},
1091which is 1103which is
1092indicated by three dots (`\verb|...|') at the end of its parameter list. 1104indicated by three dots (`\verb|...|') at the end of its parameter list.
1093A vararg function does not adjust its argument list; 1105A vararg function does not adjust its argument list;
1094instead, it collects all extra arguments into an implicit parameter, 1106instead, it collects all extra arguments into an implicit parameter,
1095called \IndexVerb{arg}. 1107called \IndexLIB{arg}.
1096The value of \verb|arg| is a table, 1108The value of \verb|arg| is a table,
1097with a field~\verb|n| whose value is the number of extra arguments, 1109with a field~\verb|n| whose value is the number of extra arguments,
1098and the extra arguments at positions 1,~2,~\ldots,\M{n}. 1110and the extra arguments at positions 1,~2,~\ldots,~\verb|n|.
1099 1111
1100As an example, consider the following definitions: 1112As an example, consider the following definitions:
1101\begin{verbatim} 1113\begin{verbatim}
@@ -1129,27 +1141,26 @@ The syntax
1129\produc{funcname}{name \ter{:} name} 1141\produc{funcname}{name \ter{:} name}
1130\end{Produc}% 1142\end{Produc}%
1131is used for defining \IndexEmph{methods}, 1143is used for defining \IndexEmph{methods},
1132that is, functions that have an implicit extra parameter \IndexVerb{self}: 1144that is, functions that have an implicit extra parameter \IndexVerb{self}.
1133Thus, the statement 1145
1146The statement
1134\begin{verbatim} 1147\begin{verbatim}
1135 function v:f (...) ... end 1148 function v:f (...) ... end
1136\end{verbatim} 1149\end{verbatim}
1137is equivalent to 1150is just syntactic sugar for
1138\begin{verbatim} 1151\begin{verbatim}
1139 v.f = function (self, ...) ... end 1152 v.f = function (self, ...) ... end
1140\end{verbatim} 1153\end{verbatim}
1141Note that the function gets an extra formal parameter called \verb|self|. 1154Note that the function gets an extra formal parameter called \verb|self|.
1142Note also that \verb|v| must have been
1143previously initialized with a table value.
1144 1155
1145 1156
1146\subsection{Visibility and Upvalues} \label{upvalue} 1157\subsection{Visibility and Upvalues} \label{upvalue}
1147\index{Visibility} \index{Upvalues} 1158\index{visibility}\index{upvalues}
1148 1159
1149A function body may refer to its own local variables 1160A function body may refer to its own local variables
1150(which include its parameters) and to global variables, 1161(which include its parameters) and to global variables,
1151as long as they are not \emph{shadowed} by local 1162as long as they are not \emph{shadowed} by local
1152variables from enclosing functions. 1163variables with the same name from enclosing functions.
1153A function \emph{cannot} access a local 1164A function \emph{cannot} access a local
1154variable from an enclosing function, 1165variable from an enclosing function,
1155since such variables may no longer exist when the function is called. 1166since such variables may no longer exist when the function is called.
@@ -1159,13 +1170,15 @@ whose syntax is
1159\begin{Produc} 1170\begin{Produc}
1160\produc{upvalue}{\ter{\%} name} 1171\produc{upvalue}{\ter{\%} name}
1161\end{Produc}% 1172\end{Produc}%
1173
1162An upvalue is somewhat similar to a variable expression, 1174An upvalue is somewhat similar to a variable expression,
1163but whose value is \emph{frozen} when the function wherein it 1175but whose value is \emph{frozen} when the function wherein it
1164appears is instantiated. 1176appears is instantiated.
1165The name used in an upvalue may be the name of any variable visible 1177The name used in an upvalue may be the name of any variable visible
1166at the point where the function is defined, 1178at the point where the function is defined,
1167that is, 1179that is,
1168global variables and local variables from the immediately enclosing function. 1180global variables and local variables
1181from the \emph{immediately enclosing} function.
1169Note that when the upvalue is a table, 1182Note that when the upvalue is a table,
1170only the \emph{reference} to that table 1183only the \emph{reference} to that table
1171(which is the value of the upvalue) is frozen; 1184(which is the value of the upvalue) is frozen;
@@ -1178,7 +1191,7 @@ Here are some examples:
1178 a,b,c = 1,2,3 -- global variables 1191 a,b,c = 1,2,3 -- global variables
1179 local d 1192 local d
1180 function f (x) 1193 function f (x)
1181 local b = {} -- x and b are local to f; b shadows the global b 1194 local b = {} -- x and b are local to f; b shadows the global b
1182 local g = function (a) 1195 local g = function (a)
1183 local y -- a and y are local to g 1196 local y -- a and y are local to g
1184 p = a -- OK, access local `a' 1197 p = a -- OK, access local `a'
@@ -1201,11 +1214,11 @@ Because Lua is an extension language,
1201all Lua actions start from C~code in the host program 1214all Lua actions start from C~code in the host program
1202calling a function from the Lua library. 1215calling a function from the Lua library.
1203Whenever an error occurs during Lua compilation or execution, 1216Whenever an error occurs during Lua compilation or execution,
1204the function \verb|_ERRORMESSAGE| is called \Deffunc{_ERRORMESSAGE} 1217the function \verb|_ERRORMESSAGE| is called \DefLIB{_ERRORMESSAGE}
1205(provided it is different from \nil), 1218(provided it is different from \nil),
1206and then the corresponding function from the library 1219and then the corresponding function from the library
1207(\verb|lua_dofile|, \verb|lua_dostring|, 1220(\verb|lua_dofile|, \verb|lua_dostring|,
1208\verb|lua_dobuffer|, or \verb|lua_callfunction|) 1221\verb|lua_dobuffer|, or \verb|lua_call|)
1209is terminated, returning an error condition. 1222is terminated, returning an error condition.
1210 1223
1211Memory allocation errors are an exception to the previous rule. 1224Memory allocation errors are an exception to the previous rule.
@@ -1215,12 +1228,13 @@ So, for this kind of error, Lua does not call
1215the \verb|_ERRORMESSAGE| function; 1228the \verb|_ERRORMESSAGE| function;
1216instead, the corresponding function from the library 1229instead, the corresponding function from the library
1217returns immediately with a special error code (\verb|LUA_ERRMEM|). 1230returns immediately with a special error code (\verb|LUA_ERRMEM|).
1218This and other error codes are defined in \verb|lua.h|. 1231This and other error codes are defined in \verb|lua.h|;
1232\See{luado}.
1219 1233
1220The only argument to \verb|_ERRORMESSAGE| is a string 1234The only argument to \verb|_ERRORMESSAGE| is a string
1221describing the error. 1235describing the error.
1222The default definition for 1236The default definition for
1223this function calls \verb|_ALERT|, \Deffunc{_ALERT} 1237this function calls \verb|_ALERT|, \DefLIB{_ALERT}
1224which prints the message to \verb|stderr| \see{alert}. 1238which prints the message to \verb|stderr| \see{alert}.
1225The standard I/O library redefines \verb|_ERRORMESSAGE| 1239The standard I/O library redefines \verb|_ERRORMESSAGE|
1226and uses the debug facilities \see{debugI} 1240and uses the debug facilities \see{debugI}
@@ -1233,26 +1247,26 @@ Lua code can ``catch'' an error using the function
1233\verb|call| \see{pdf-call}. 1247\verb|call| \see{pdf-call}.
1234 1248
1235 1249
1236\subsection{Tag Methods} \label{tag-method} 1250\subsection{Tag Methods} \label{tag-method}\index{tag method}
1237 1251
1238Lua provides a powerful mechanism to extend its semantics, 1252Lua provides a powerful mechanism to extend its semantics,
1239called \Def{tag methods}. 1253called \emph{tag methods}.
1240A tag method is a programmer-defined function 1254A tag method is a programmer-defined function
1241that is called at specific key points during the evaluation of a program, 1255that is called at specific key points during the execution of a Lua program,
1242allowing the programmer to change the standard Lua behavior at these points. 1256allowing the programmer to change the standard Lua behavior at these points.
1243Each of these points is called an \Def{event}. 1257Each of these points is called an \Def{event}.
1244 1258
1245The tag method called for any specific event is selected 1259The tag method called for any specific event is selected
1246according to the tag of the values involved 1260according to the tag of the values involved
1247in the event \see{TypesSec}. 1261in the event \see{TypesSec}.
1248The function \IndexVerb{settagmethod} changes the tag method 1262The function \IndexLIB{settagmethod} changes the tag method
1249associated with a given pair \M{(tag, event)}. 1263associated with a given pair \M{(tag, event)}.
1250Its first parameter is the tag, the second parameter is the event name 1264Its first parameter is the tag, the second parameter is the event name
1251(a string; see below), 1265(a string; see below),
1252and the third parameter is the new method (a function), 1266and the third parameter is the new method (a function),
1253or \nil\ to restore the default behavior for the pair. 1267or \nil\ to restore the default behavior for the pair.
1254The \verb|settagmethod| function returns the previous tag method for that pair. 1268The \verb|settagmethod| function returns the previous tag method for that pair.
1255A companion function \IndexVerb{gettagmethod} 1269A companion function \IndexLIB{gettagmethod}
1256receives a tag and an event name and returns the 1270receives a tag and an event name and returns the
1257current method associated with the pair. 1271current method associated with the pair.
1258 1272
@@ -1271,7 +1285,7 @@ are described in \See{predefined}.
1271 1285
1272\begin{description} 1286\begin{description}
1273 1287
1274\item[``add'':]\index{add event} 1288\item[``add'':]\IndexTM{add}
1275called when a \verb|+| operation is applied to non-numerical operands. 1289called when a \verb|+| operation is applied to non-numerical operands.
1276 1290
1277The function \verb|getbinmethod| below defines how Lua chooses a tag method 1291The function \verb|getbinmethod| below defines how Lua chooses a tag method
@@ -1307,19 +1321,19 @@ the tag method for the ``add'' event is
1307 end 1321 end
1308\end{verbatim} 1322\end{verbatim}
1309 1323
1310\item[``sub'':]\index{sub event} 1324\item[``sub'':]\IndexTM{sub}
1311called when a \verb|-| operation is applied to non-numerical operands. 1325called when a \verb|-| operation is applied to non-numerical operands.
1312Behavior similar to the ``add'' event. 1326Behavior similar to the ``add'' event.
1313 1327
1314\item[``mul'':]\index{mul event} 1328\item[``mul'':]\IndexTM{mul}
1315called when a \verb|*| operation is applied to non-numerical operands. 1329called when a \verb|*| operation is applied to non-numerical operands.
1316Behavior similar to the ``add'' event. 1330Behavior similar to the ``add'' event.
1317 1331
1318\item[``div'':]\index{div event} 1332\item[``div'':]\IndexTM{div}
1319called when a \verb|/| operation is applied to non-numerical operands. 1333called when a \verb|/| operation is applied to non-numerical operands.
1320Behavior similar to the ``add'' event. 1334Behavior similar to the ``add'' event.
1321 1335
1322\item[``pow'':]\index{pow event} 1336\item[``pow'':]\IndexTM{pow}
1323called when a \verb|^| operation (exponentiation) is applied, 1337called when a \verb|^| operation (exponentiation) is applied,
1324even for numerical operands. 1338even for numerical operands.
1325\begin{verbatim} 1339\begin{verbatim}
@@ -1335,7 +1349,7 @@ even for numerical operands.
1335 end 1349 end
1336\end{verbatim} 1350\end{verbatim}
1337 1351
1338\item[``unm'':]\index{unm event} 1352\item[``unm'':]\IndexTM{unm}
1339called when a unary \verb|-| operation is applied to a non-numerical operand. 1353called when a unary \verb|-| operation is applied to a non-numerical operand.
1340\begin{verbatim} 1354\begin{verbatim}
1341 function unm_event (op) 1355 function unm_event (op)
@@ -1358,7 +1372,7 @@ called when a unary \verb|-| operation is applied to a non-numerical operand.
1358 end 1372 end
1359\end{verbatim} 1373\end{verbatim}
1360 1374
1361\item[``lt'':]\index{lt event} 1375\item[``lt'':]\IndexTM{lt}
1362called when an order operation is applied to non-numerical 1376called when an order operation is applied to non-numerical
1363or non-string operands. 1377or non-string operands.
1364It corresponds to the \verb|<| operator. 1378It corresponds to the \verb|<| operator.
@@ -1386,7 +1400,7 @@ usual equivalences:
1386 a>=b <=> not (a<b) 1400 a>=b <=> not (a<b)
1387\end{verbatim} 1401\end{verbatim}
1388 1402
1389\item[``concat'':]\index{concatenation event} 1403\item[``concat'':]\IndexTM{concatenation}
1390called when a concatenation is applied to non-string operands. 1404called when a concatenation is applied to non-string operands.
1391\begin{verbatim} 1405\begin{verbatim}
1392 function concat_event (op1, op2) 1406 function concat_event (op1, op2)
@@ -1404,12 +1418,12 @@ called when a concatenation is applied to non-string operands.
1404 end 1418 end
1405\end{verbatim} 1419\end{verbatim}
1406 1420
1407\item[``index'':]\index{index event} 1421\item[``index'':]\IndexTM{index}
1408called when Lua tries to retrieve the value of an index 1422called when Lua tries to retrieve the value of an index
1409not present in a table. 1423not present in a table.
1410See the ``gettable'' event for its semantics. 1424See the ``gettable'' event for its semantics.
1411 1425
1412\item[``getglobal'':]\index{getglobal event} 1426\item[``getglobal'':]\IndexTM{getglobal}
1413called whenever Lua needs the value of a global variable. 1427called whenever Lua needs the value of a global variable.
1414This method can only be set for \nil\ and for tags 1428This method can only be set for \nil\ and for tags
1415created by \verb|newtag|. 1429created by \verb|newtag|.
@@ -1429,7 +1443,7 @@ the tag is that of the \emph{current value} of the global variable.
1429\end{verbatim} 1443\end{verbatim}
1430The function \verb|getglobal| is defined in the basic library~\see{predefined}. 1444The function \verb|getglobal| is defined in the basic library~\see{predefined}.
1431 1445
1432\item[``setglobal'':]\index{setglobal event} 1446\item[``setglobal'':]\IndexTM{setglobal}
1433called whenever Lua assigns to a global variable. 1447called whenever Lua assigns to a global variable.
1434This method cannot be set for numbers, strings, and tables and 1448This method cannot be set for numbers, strings, and tables and
1435userdata with the default tag. 1449userdata with the default tag.
@@ -1446,7 +1460,7 @@ userdata with the default tag.
1446\end{verbatim} 1460\end{verbatim}
1447The function \verb|setglobal| is defined in the basic library~\see{predefined}. 1461The function \verb|setglobal| is defined in the basic library~\see{predefined}.
1448 1462
1449\item[``gettable'':]\index{gettable event} 1463\item[``gettable'':]\IndexTM{gettable}
1450called whenever Lua accesses an indexed variable. 1464called whenever Lua accesses an indexed variable.
1451This method cannot be set for tables with the default tag. 1465This method cannot be set for tables with the default tag.
1452\begin{verbatim} 1466\begin{verbatim}
@@ -1468,7 +1482,7 @@ This method cannot be set for tables with the default tag.
1468 end 1482 end
1469\end{verbatim} 1483\end{verbatim}
1470 1484
1471\item[``settable'':]\index{settable event} 1485\item[``settable'':]\IndexTM{settable}
1472called when Lua assigns to an indexed variable. 1486called when Lua assigns to an indexed variable.
1473This method cannot be set for tables with the default tag. 1487This method cannot be set for tables with the default tag.
1474\begin{verbatim} 1488\begin{verbatim}
@@ -1484,7 +1498,7 @@ This method cannot be set for tables with the default tag.
1484 end 1498 end
1485\end{verbatim} 1499\end{verbatim}
1486 1500
1487\item[``function'':]\index{function event} 1501\item[``function'':]\IndexTM{function}
1488called when Lua tries to call a non-function value. 1502called when Lua tries to call a non-function value.
1489\begin{verbatim} 1503\begin{verbatim}
1490 function function_event (func, ...) 1504 function function_event (func, ...)
@@ -1506,7 +1520,7 @@ called when Lua tries to call a non-function value.
1506 end 1520 end
1507\end{verbatim} 1521\end{verbatim}
1508 1522
1509\item[``gc'':]\index{gc event} 1523\item[``gc'':]\IndexTM{gc}
1510called when Lua is ``garbage collecting'' a userdata. 1524called when Lua is ``garbage collecting'' a userdata.
1511This tag method can be set only from~C, 1525This tag method can be set only from~C,
1512and cannot be set for a userdata with the default tag. 1526and cannot be set for a userdata with the default tag.
@@ -1520,10 +1534,9 @@ Lua does the equivalent of the following function:
1520 end 1534 end
1521 end 1535 end
1522\end{verbatim} 1536\end{verbatim}
1523In a garbage-collection cicle, 1537In a garbage-collection cycle,
1524the tag methods for userdata are called in reverse 1538the tag methods for userdata are called in \emph{reverse} order of tag creation,
1525order of tag creation: 1539that is, the first tag methods to be called are those associated
1526That is, the first tag methods to be called are those associated
1527with the last tag created in the program. 1540with the last tag created in the program.
1528Moreover, at the end of the cycle, 1541Moreover, at the end of the cycle,
1529Lua does the equivalent of the call \verb|gc_event(nil)|. 1542Lua does the equivalent of the call \verb|gc_event(nil)|.
@@ -1534,7 +1547,7 @@ Lua does the equivalent of the call \verb|gc_event(nil)|.
1534 1547
1535 1548
1536\section{The Application Program Interface} 1549\section{The Application Program Interface}
1537 1550\index{C API}
1538This section describes the API for Lua, that is, 1551This section describes the API for Lua, that is,
1539the set of C~functions available to the host program to communicate 1552the set of C~functions available to the host program to communicate
1540with Lua. 1553with Lua.
@@ -1552,33 +1565,35 @@ and so do not generate hidden side-effects.
1552 1565
1553The Lua library is fully reentrant: 1566The Lua library is fully reentrant:
1554it does not have any global variables. 1567it does not have any global variables.
1568\index{state}
1555The whole state of the Lua interpreter 1569The whole state of the Lua interpreter
1556(global variables, stack, tag methods, etc.) 1570(global variables, stack, tag methods, etc.)
1557is stored in a dynamically allocated structure; \Deffunc{lua_State} 1571is stored in a dynamically allocated structure of type \verb|lua_State|; \DefAPI{lua_State}
1558this state must be passed as the first argument to 1572this state must be passed as the first argument to
1559every function in the library (except \verb|lua_open| below). 1573every function in the library (except \verb|lua_open| below).
1560 1574
1561Before calling any API function, 1575Before calling any API function,
1562you must create a state. 1576you must create a state by calling
1563This is done by calling\Deffunc{lua_open} 1577\DefAPI{lua_open}
1564\begin{verbatim} 1578\begin{verbatim}
1565 lua_State *lua_open (int stacksize); 1579 lua_State *lua_open (int stacksize);
1566\end{verbatim} 1580\end{verbatim}
1567The sole argument to this function is the stack size for the interpreter. 1581The sole argument to this function is the stack size for the interpreter.
1568(Each function call needs one stack position for each argument, local variable 1582(Each function call needs one stack position for each argument, local variable,
1569and temporary values, plus one position for book-keeping. 1583and temporary value, plus one position for book-keeping.
1570The stack must also have some 20 extra positions available. 1584The stack must also have some 20 extra positions available.
1571For very small implementations, without recursive functions, 1585For very small implementations, without recursive functions,
1572a stack size of 100 should be enough.) 1586a stack size of~100 should be enough.)
1573If \verb|stacksize| is zero, 1587If \verb|stacksize| is zero,
1574then a default size is used (the default is 1024). 1588then a default size of~1024 is used.
1575 1589
1576To release a state created with \verb|lua_open|, call 1590To release a state created with \verb|lua_open|, call
1591\DefAPI{lua_close}
1577\begin{verbatim} 1592\begin{verbatim}
1578 void lua_close (lua_State *L); 1593 void lua_close (lua_State *L);
1579\end{verbatim} 1594\end{verbatim}
1580This function destroys all objects in the given Lua environment 1595This function destroys all objects in the given Lua environment
1581(calling the corresponding garbage-collection tag methods) 1596(calling the corresponding garbage-collection tag methods, if any)
1582and frees all dynamic memory used by that state. 1597and frees all dynamic memory used by that state.
1583Usually, you do not need to call this function, 1598Usually, you do not need to call this function,
1584because all resources are naturally released when your program ends. 1599because all resources are naturally released when your program ends.
@@ -1589,7 +1604,7 @@ might need to release states as soon as they are not needed,
1589to avoid growing too big. 1604to avoid growing too big.
1590 1605
1591With the exception of \verb|lua_open|, 1606With the exception of \verb|lua_open|,
1592all functions in the API need a state as their first argument. 1607all functions in the Lua API need a state as their first argument.
1593 1608
1594 1609
1595\subsection{The Stack and Indices} 1610\subsection{The Stack and Indices}
@@ -1602,37 +1617,38 @@ For convenience,
1602most query operations in the API do not follow a strict stack discipline. 1617most query operations in the API do not follow a strict stack discipline.
1603Instead, they can refer to any element in the stack by using an \emph{index}: 1618Instead, they can refer to any element in the stack by using an \emph{index}:
1604A positive index represents an \emph{absolute} stack position 1619A positive index represents an \emph{absolute} stack position
1605(starting at 1, not 0 as in C); 1620(starting at~1, not 0 as in C);
1606a negative index represents an \emph{offset} from the top of the stack. 1621a negative index represents an \emph{offset} from the top of the stack.
1607More specifically, if the stack has \M{n} elements, 1622More specifically, if the stack has \M{n} elements,
1608index 1 represents the first element 1623index~1 represents the first element
1609(that is, the first element pushed onto the stack), 1624(that is, the first element pushed onto the stack),
1610index \M{n} represents the last element; 1625and
1611index \Math{-1} also represents the last element 1626index~\M{n} represents the last element;
1627index~\Math{-1} also represents the last element
1612(that is, the element at the top), 1628(that is, the element at the top),
1613and index \Math{-n} represents the first element. 1629and index \Math{-n} represents the first element.
1614We say that an index is \emph{valid} 1630We say that an index is \emph{valid}
1615if it lays between 1 and the stack top 1631if it lays between~1 and the stack top
1616(that is, \verb|(1 <= abs(index) <= top)|). 1632(that is, if \verb|1 <= abs(index) <= top|).
1617\index{stack index} \index{valid index} 1633\index{stack index} \index{valid index}
1618 1634
1619At any time, you can get the index of the top element by calling 1635At any time, you can get the index of the top element by calling
1620\Deffunc{lua_gettop} 1636\DefAPI{lua_gettop}
1621\begin{verbatim} 1637\begin{verbatim}
1622 int lua_gettop (lua_State *L); 1638 int lua_gettop (lua_State *L);
1623\end{verbatim} 1639\end{verbatim}
1624Because indices start at 1, 1640Because indices start at~1,
1625the result of \verb|lua_gettop| is equal to the number of elements in the stack 1641the result of \verb|lua_gettop| is equal to the number of elements in the stack
1626(0 means an empty stack). 1642(and so 0~means an empty stack).
1627 1643
1628When you interact with Lua API, 1644When you interact with Lua API,
1629\emph{you are responsible for controlling stack overflow}. 1645\emph{you are responsible for controlling stack overflow}.
1630The function \Deffunc{lua_stackspace} 1646The function \DefAPI{lua_stackspace}
1631\begin{verbatim} 1647\begin{verbatim}
1632 int lua_stackspace (lua_State *L); 1648 int lua_stackspace (lua_State *L);
1633\end{verbatim} 1649\end{verbatim}
1634returns the number of stack positions still available. 1650returns the number of stack positions still available.
1635Whenever Lua calls C, \Deffunc{LUA_MINSTACK} 1651Whenever Lua calls C, \DefAPI{LUA_MINSTACK}
1636it ensures that 1652it ensures that
1637at least \verb|LUA_MINSTACK| positions are still available. 1653at least \verb|LUA_MINSTACK| positions are still available.
1638\verb|LUA_MINSTACK| is defined in \verb|lua.h| and is at least~16, 1654\verb|LUA_MINSTACK| is defined in \verb|lua.h| and is at least~16,
@@ -1647,12 +1663,12 @@ as
1647\begin{verbatim} 1663\begin{verbatim}
1648 (index < 0 && abs(index) <= top) || (index > 0 && index <= top + stackspace) 1664 (index < 0 && abs(index) <= top) || (index > 0 && index <= top + stackspace)
1649\end{verbatim} 1665\end{verbatim}
1650(Note that 0 is not an acceptable index.) 1666Note that 0 is not an acceptable index.
1651 1667
1652\subsection{Stack Manipulation} 1668\subsection{Stack Manipulation}
1653The API offers the following functions for basic stack manipulation: 1669The API offers the following functions for basic stack manipulation:
1654\Deffunc{lua_settop}\Deffunc{lua_pushvalue} 1670\DefAPI{lua_settop}\DefAPI{lua_pushvalue}
1655\Deffunc{lua_remove}\Deffunc{lua_insert} 1671\DefAPI{lua_remove}\DefAPI{lua_insert}
1656\begin{verbatim} 1672\begin{verbatim}
1657 void lua_settop (lua_State *L, int index); 1673 void lua_settop (lua_State *L, int index);
1658 void lua_pushvalue (lua_State *L, int index); 1674 void lua_pushvalue (lua_State *L, int index);
@@ -1672,7 +1688,7 @@ A useful macro defined in the API is
1672\end{verbatim} 1688\end{verbatim}
1673which pops \verb|n| elements from the stack. 1689which pops \verb|n| elements from the stack.
1674 1690
1675\verb|lua_pushvalue| pushes onto the stack a copy of the element 1691\verb|lua_pushvalue| pushes onto the stack a \emph{copy} of the element
1676at the given index. 1692at the given index.
1677\verb|lua_remove| removes the element at the given position, 1693\verb|lua_remove| removes the element at the given position,
1678shifting down the elements on top of that position to fill in the gap. 1694shifting down the elements on top of that position to fill in the gap.
@@ -1698,11 +1714,12 @@ then
1698 1714
1699To check the type of a stack element, 1715To check the type of a stack element,
1700the following functions are available: 1716the following functions are available:
1701\Deffunc{lua_isnil}\Deffunc{lua_isnumber}\Deffunc{lua_isstring} 1717\DefAPI{lua_type}\DefAPI{lua_tag}
1702\Deffunc{lua_istable}\Deffunc{lua_iscfunction}\Deffunc{lua_isuserdata} 1718\DefAPI{lua_isnil}\DefAPI{lua_isnumber}\DefAPI{lua_isstring}
1703\Deffunc{lua_isfunction}\Deffunc{lua_type} 1719\DefAPI{lua_istable}
1720\DefAPI{lua_isfunction}\DefAPI{lua_iscfunction}\DefAPI{lua_isuserdata}
1704\begin{verbatim} 1721\begin{verbatim}
1705 const char *lua_type (lua_State *L, int index); 1722 int lua_type (lua_State *L, int index);
1706 int lua_tag (lua_State *L, int index); 1723 int lua_tag (lua_State *L, int index);
1707 int lua_isnil (lua_State *L, int index); 1724 int lua_isnil (lua_State *L, int index);
1708 int lua_isnumber (lua_State *L, int index); 1725 int lua_isnumber (lua_State *L, int index);
@@ -1714,20 +1731,31 @@ the following functions are available:
1714\end{verbatim} 1731\end{verbatim}
1715These functions can be called with any acceptable index. 1732These functions can be called with any acceptable index.
1716 1733
1717\verb|lua_type| returns one of the following strings, 1734\verb|lua_type| returns one of the following constants,
1718describing the type of the given object: 1735according to the type of the given object:
1736\verb|LUA_TNIL|,
1737\verb|LUA_TNUMBER|,
1738\verb|LUA_TSTRING|,
1739\verb|LUA_TTABLE|,
1740\verb|LUA_TFUNCTION|,
1741\verb|LUA_TUSERDATA|.
1742If the index is non-valid
1743(that is, if that stack position is ``empty''),
1744then \verb|lua_type| returns \verb|LUA_TNONE|.
1745These constants can be converted to strings with
1746\DefAPI{lua_typename}
1747\begin{verbatim}
1748 const char *lua_typename (lua_State *L, int t);
1749\end{verbatim}
1750where \verb|t| is a type returned by \verb|lua_type|.
1751The strings returned by \verb|lua_typename| are
1719\verb|"nil"|, \verb|"number"|, \verb|"string"|, \verb|"table"|, 1752\verb|"nil"|, \verb|"number"|, \verb|"string"|, \verb|"table"|,
1720\verb|"function"|, \verb|"userdata"|; or \verb|"NO VALUE"|, 1753\verb|"function"|, \verb|"userdata"|, and \verb|"no value"|,
1721if the index is non-valid (that is, if that stack position is ``empty'').
1722 1754
1723\verb|lua_tag| returns the tag of a value, 1755\verb|lua_tag| returns the tag of a value,
1724or \verb|LUA_NOTAG| for a non-valid index. 1756or \verb|LUA_NOTAG| for a non-valid index.
1725(Very dirty trick: some type names start with the same letter (Number-Nil);
1726others have the second letter in common (nUmber-fUnction).
1727However, you can use the emph{third} letter as a unique scalar
1728identification for each type.)
1729 1757
1730The \verb|lua_is*| functions return 1 if the object is compatible 1758The \verb|lua_is*| functions return~1 if the object is compatible
1731with the given type, and 0 otherwise. 1759with the given type, and 0 otherwise.
1732They always return 0 for a non-valid index. 1760They always return 0 for a non-valid index.
1733\verb|lua_isnumber| accepts numbers and numerical strings, 1761\verb|lua_isnumber| accepts numbers and numerical strings,
@@ -1739,19 +1767,21 @@ To distinguish between numbers and numerical strings,
1739you can use \verb|lua_type|. 1767you can use \verb|lua_type|.
1740 1768
1741The API also has functions to compare two values in the stack: 1769The API also has functions to compare two values in the stack:
1770\DefAPI{lua_equal}
1771\DefAPI{lua_lessthan}
1742\begin{verbatim} 1772\begin{verbatim}
1743 int lua_equal (lua_State *L, int index1, int index2); 1773 int lua_equal (lua_State *L, int index1, int index2);
1744 int lua_lessthan (lua_State *L, int index1, int index2); 1774 int lua_lessthan (lua_State *L, int index1, int index2);
1745\end{verbatim} 1775\end{verbatim}
1746These functions are equivalent to their counterparts in Lua. 1776These functions are equivalent to their counterparts in Lua.
1747Specifically, \verb|lua_lessthan| is equivalent to the \verb|lt_event| 1777Specifically, \verb|lua_lessthan| is equivalent to the \verb|lt_event|
1748described in~\ref{tag-method}. 1778described in \See{tag-method}.
1749Both functions return 0 if any of the indices are non-valid. 1779Both functions return 0 if any of the indices are non-valid.
1750 1780
1751To translate a value in the stack to a specific C~type, 1781To translate a value in the stack to a specific C~type,
1752you can use the following conversion functions: 1782you can use the following conversion functions:
1753\Deffunc{lua_tonumber}\Deffunc{lua_tostring}\Deffunc{lua_strlen} 1783\DefAPI{lua_tonumber}\DefAPI{lua_tostring}\DefAPI{lua_strlen}
1754\Deffunc{lua_tocfunction}\Deffunc{lua_touserdata} 1784\DefAPI{lua_tocfunction}\DefAPI{lua_touserdata}
1755\begin{verbatim} 1785\begin{verbatim}
1756 double lua_tonumber (lua_State *L, int index); 1786 double lua_tonumber (lua_State *L, int index);
1757 const char *lua_tostring (lua_State *L, int index); 1787 const char *lua_tostring (lua_State *L, int index);
@@ -1773,7 +1803,7 @@ This value must be a number or a string convertible to number
1773This value must be a string or a number; 1803This value must be a string or a number;
1774otherwise, the function returns \verb|NULL|. 1804otherwise, the function returns \verb|NULL|.
1775This function returns a pointer to a string inside the Lua environment. 1805This function returns a pointer to a string inside the Lua environment.
1776Those strings always have a 0 after their last character (like in C), 1806Those strings always have a zero (\verb|'\0'|) after their last character (as in C),
1777but may contain other zeros in their body. 1807but may contain other zeros in their body.
1778If you do not know whether a string may contain zeros, 1808If you do not know whether a string may contain zeros,
1779you should use \verb|lua_strlen| to get its actual length. 1809you should use \verb|lua_strlen| to get its actual length.
@@ -1796,9 +1826,9 @@ otherwise, \verb|lua_touserdata| returns \verb|NULL|.
1796 1826
1797The API has the following functions to 1827The API has the following functions to
1798push C~values onto the stack: 1828push C~values onto the stack:
1799\Deffunc{lua_pushnumber}\Deffunc{lua_pushlstring}\Deffunc{lua_pushstring} 1829\DefAPI{lua_pushnumber}\DefAPI{lua_pushlstring}\DefAPI{lua_pushstring}
1800\Deffunc{lua_pushcfunction}\Deffunc{lua_pushusertag} 1830\DefAPI{lua_pushcfunction}\DefAPI{lua_pushusertag}
1801\Deffunc{lua_pushnil}\Deffunc{lua_pushuserdata}\label{pushing} 1831\DefAPI{lua_pushnil}\DefAPI{lua_pushuserdata}\label{pushing}
1802\begin{verbatim} 1832\begin{verbatim}
1803 void lua_pushnumber (lua_State *L, double n); 1833 void lua_pushnumber (lua_State *L, double n);
1804 void lua_pushlstring (lua_State *L, const char *s, size_t len); 1834 void lua_pushlstring (lua_State *L, const char *s, size_t len);
@@ -1820,42 +1850,43 @@ which accepts an explicit size.
1820 1850
1821\subsection{Garbage Collection}\label{GC} 1851\subsection{Garbage Collection}\label{GC}
1822 1852
1823Lua keeps two numbers to control its garbage collection. 1853Lua uses two numbers to control its garbage collection.
1824One number counts how many bytes of dynamic memory Lua is using, 1854One number counts how many bytes of dynamic memory Lua is using,
1825and the other keeps a threshold. 1855and the other is a threshold.
1826(This internal byte counter kept by Lua is not completely acurate: 1856(This internal byte counter kept by Lua is not completely acurate;
1827Instead, it is a lower bound, usually within 10\% of the correct value.) 1857it is just a lower bound, usually within~10\% of the correct value.)
1828When the number of bytes crosses the threshold, 1858When the number of bytes crosses the threshold,
1829Lua runs a garbage-collection cycle, 1859Lua runs a garbage-collection cycle,
1830that reclaims the memory of all ``dead'' objects 1860which reclaims the memory of all ``dead'' objects
1831(that is, objects no longer accessible from Lua). 1861(that is, objects no longer accessible from Lua).
1832The byte counter is corrected, 1862The byte counter is corrected,
1833and then the threshold is reset to twice the value of the byte counter. 1863and then the threshold is reset to twice the value of the byte counter.
1834 1864
1835You can access the current values of these two numbers through the 1865You can access the current values of these two numbers through the
1836following functions: 1866following functions:
1837\Deffunc{lua_getgcthreshold} \Deffunc{lua_getgccount} 1867\DefAPI{lua_getgcthreshold} \DefAPI{lua_getgccount}
1838\begin{verbatim} 1868\begin{verbatim}
1839 int lua_getgccount (lua_State *L); 1869 int lua_getgccount (lua_State *L);
1840 int lua_getgcthreshold (lua_State *L); 1870 int lua_getgcthreshold (lua_State *L);
1841\end{verbatim} 1871\end{verbatim}
1842Both return their respective values in Kbytes. 1872Both return their respective values in Kbytes.
1843You can change the threshold value with 1873You can change the threshold value with
1844\Deffunc{lua_setgcthreshold} 1874\DefAPI{lua_setgcthreshold}
1845\begin{verbatim} 1875\begin{verbatim}
1846 void lua_setgcthreshold (lua_State *L, int newthreshold); 1876 void lua_setgcthreshold (lua_State *L, int newthreshold);
1847\end{verbatim} 1877\end{verbatim}
1848Again, the \verb|newthreshold| value is given in Kbytes. 1878Again, the \verb|newthreshold| value is given in Kbytes.
1849When you call this function, 1879When you call this function,
1850Lua sets the new threshold and checks it against the byte counter; 1880Lua sets the new threshold and checks it against the byte counter.
1851if the new threshold is smaller than the byte counter, 1881If the new threshold is smaller than the byte counter,
1852Lua runs immediately the garbage collector 1882then Lua immediately runs the garbage collector;
1853(and, after it, sets a new threshold according to the previous rule). 1883after the collection,
1884a new threshold is set according to the previous rule.
1854 1885
1855If you want to change the adaptative behavior of the garbage collector, 1886If you want to change the adaptative behavior of the garbage collector,
1856you can use the garbage-collection tag method for the tag \nil 1887you can use the garbage-collection tag method for \nil\ %
1857to set your own threshold 1888to set your own threshold
1858(the tag method is called after Lua resets the threshold). 1889(the tag method is called after Lua resets the threshold).
1859 1890
1860 1891
1861\subsection{Userdata and Tags}\label{C-tags} 1892\subsection{Userdata and Tags}\label{C-tags}
@@ -1866,7 +1897,7 @@ If Lua has a userdata with the given value (\verb|void*|) and tag,
1866then that userdata is pushed. 1897then that userdata is pushed.
1867Otherwise, a new userdata is created, with the given value and tag. 1898Otherwise, a new userdata is created, with the given value and tag.
1868If this function is called with 1899If this function is called with
1869\verb|tag| equal to \verb|LUA_ANYTAG|\Deffunc{LUA_ANYTAG}, 1900\verb|tag| equal to \verb|LUA_ANYTAG|\DefAPI{LUA_ANYTAG},
1870then Lua will try to find any userdata with the given value, 1901then Lua will try to find any userdata with the given value,
1871regardless of its tag. 1902regardless of its tag.
1872If there is no userdata with that value, then a new one is created, 1903If there is no userdata with that value, then a new one is created,
@@ -1875,23 +1906,23 @@ with tag equal to 0.
1875Userdata can have different tags, 1906Userdata can have different tags,
1876whose semantics are only known to the host program. 1907whose semantics are only known to the host program.
1877Tags are created with the function 1908Tags are created with the function
1878\Deffunc{lua_newtag} 1909\DefAPI{lua_newtag}
1879\begin{verbatim} 1910\begin{verbatim}
1880 int lua_newtag (lua_State *L); 1911 int lua_newtag (lua_State *L);
1881\end{verbatim} 1912\end{verbatim}
1882The function \verb|lua_settag| changes the tag of 1913The function \verb|lua_settag| changes the tag of
1883the object on top of the stack (without popping it): 1914the object on top of the stack (without popping it):
1884\Deffunc{lua_settag} 1915\DefAPI{lua_settag}
1885\begin{verbatim} 1916\begin{verbatim}
1886 void lua_settag (lua_State *L, int tag); 1917 void lua_settag (lua_State *L, int tag);
1887\end{verbatim} 1918\end{verbatim}
1888The object must be a userdata or a table; 1919The object must be a userdata or a table;
1889the given \verb|tag| must be a value created with \verb|lua_newtag|. 1920the given \verb|tag| must be a value created with \verb|lua_newtag|.
1890 1921
1891\subsection{Executing Lua Code} 1922\subsection{Executing Lua Code}\label{luado}
1892A host program can execute Lua chunks written in a file or in a string 1923A host program can execute Lua chunks written in a file or in a string
1893by using the following functions:% 1924by using the following functions:%
1894\Deffunc{lua_dofile}\Deffunc{lua_dostring}\Deffunc{lua_dobuffer}% 1925\DefAPI{lua_dofile}\DefAPI{lua_dostring}\DefAPI{lua_dobuffer}%
1895\begin{verbatim} 1926\begin{verbatim}
1896 int lua_dofile (lua_State *L, const char *filename); 1927 int lua_dofile (lua_State *L, const char *filename);
1897 int lua_dostring (lua_State *L, const char *string); 1928 int lua_dostring (lua_State *L, const char *string);
@@ -1899,21 +1930,19 @@ by using the following functions:%
1899 size_t size, const char *name); 1930 size_t size, const char *name);
1900\end{verbatim} 1931\end{verbatim}
1901These functions return 1932These functions return
19020 in case of success, or one of the following error codes if they fail 19330 in case of success, or one of the following error codes if they fail:
1903(these constants are defined in \verb|lua.h|.):
1904\begin{itemize} 1934\begin{itemize}
1905\item \IndexVerb{LUA_ERRRUN} --- 1935\item \IndexAPI{LUA_ERRRUN} ---
1906error while running the chunk. 1936error while running the chunk.
1907\item \IndexVerb{LUA_ERRSYNTAX} --- 1937\item \IndexAPI{LUA_ERRSYNTAX} ---
1908syntax error during pre-compilation. 1938syntax error during pre-compilation.
1909\item \IndexVerb{LUA_ERRMEM} --- 1939\item \IndexAPI{LUA_ERRMEM} ---
1910memory allocation error. 1940memory allocation error.
1911For such errors, Lua does not call the \verb|_ERRORMESSAGE| function 1941For such errors, Lua does not call \verb|_ERRORMESSAGE| \see{error}.
1912\see{error}. 1942\item \IndexAPI{LUA_ERRERR} ---
1913\item \IndexVerb{LUA_ERRERR} --- 1943error while running \verb|_ERRORMESSAGE|.
1914error while running the \verb|_ERRORMESSAGE| function. 1944For such errors, Lua does not call \verb|_ERRORMESSAGE| again, to avoid loops.
1915For such errors, Lua does not call the function again, to avoid loops. 1945\item \IndexAPI{LUA_ERRFILE} ---
1916\item \IndexVerb{LUA_ERRFILE} ---
1917error opening the file (only for \verb|lua_dofile|). 1946error opening the file (only for \verb|lua_dofile|).
1918In this case, 1947In this case,
1919you may want to 1948you may want to
@@ -1921,6 +1950,7 @@ check \verb|errno|,
1921call \verb|strerror|, 1950call \verb|strerror|,
1922or call \verb|perror| to tell the user what went wrong. 1951or call \verb|perror| to tell the user what went wrong.
1923\end{itemize} 1952\end{itemize}
1953These constants are defined in \verb|lua.h|.
1924 1954
1925When called with argument \verb|NULL|, 1955When called with argument \verb|NULL|,
1926\verb|lua_dofile| executes the \verb|stdin| stream. 1956\verb|lua_dofile| executes the \verb|stdin| stream.
@@ -1933,18 +1963,20 @@ given in textual form.
1933 1963
1934The third parameter to \verb|lua_dobuffer| 1964The third parameter to \verb|lua_dobuffer|
1935is the ``name of the chunk'', 1965is the ``name of the chunk'',
1936used in error messages and debug information. 1966which is used in error messages and debug information.
1937If \verb|name| is \verb|NULL|, 1967If \verb|name| is \verb|NULL|,
1938then Lua gives a default name to the chunk. 1968then Lua gives a default name to the chunk.
1939 1969
1940These functions push onto the stack 1970These functions push onto the stack
1941any values eventually returned by the chunks. 1971any values eventually returned by the chunk.
1942A chunk may return any number of values; 1972A chunk may return any number of values;
1943Lua takes care that these values fit into the stack space, 1973Lua takes care that these values fit into the stack space,
1944but after the call the responsibility is back with you. 1974but after the call the responsibility is back to you.
1945If you need to push other elements after calling any of these functions, 1975If you need to push other elements after calling any of these functions,
1946and you want to play safe, 1976and you want to ``play safe'',
1947you must either check the stack space or remove the returned elements 1977you must either check the stack space
1978with \verb|lua_stackspace|
1979or remove the returned elements
1948from the stack (if you do not need them). 1980from the stack (if you do not need them).
1949For instance, the following code 1981For instance, the following code
1950loads a chunk in a file and discards all results returned by this chunk, 1982loads a chunk in a file and discards all results returned by this chunk,
@@ -1962,41 +1994,42 @@ leaving the stack as it was before the call:
1962 1994
1963To read the value of a global Lua variable, 1995To read the value of a global Lua variable,
1964you call 1996you call
1965\Deffunc{lua_getglobal} 1997\DefAPI{lua_getglobal}
1966\begin{verbatim} 1998\begin{verbatim}
1967 void lua_getglobal (lua_State *L, const char *varname); 1999 void lua_getglobal (lua_State *L, const char *varname);
1968\end{verbatim} 2000\end{verbatim}
1969which pushes onto the stack the value of the given variable. 2001which pushes onto the stack the value of the given variable.
1970As in Lua, this function may trigger a tag method 2002As in Lua, this function may trigger a tag method
1971for the ``getglobal'' event. 2003for the ``getglobal'' event \see{tag-method}.
1972To read the real value of a global variable, 2004To read the real value of a global variable,
1973without invoking any tag method, 2005without invoking any tag method,
1974use the \verb|lua_rawget| function over the table of globals 2006use \verb|lua_rawget| over the table of globals
1975(see below). 2007(see below).
1976 2008
1977To store a value in a global variable, 2009To store a value in a global variable,
1978you call 2010you call
1979\Deffunc{lua_setglobal} 2011\DefAPI{lua_setglobal}
1980\begin{verbatim} 2012\begin{verbatim}
1981 void lua_setglobal (lua_State *L, const char *varname); 2013 void lua_setglobal (lua_State *L, const char *varname);
1982\end{verbatim} 2014\end{verbatim}
1983which pops from the stack the value to be stored in the given variable. 2015which pops from the stack the value to be stored in the given variable.
1984As in Lua, this function may trigger a tag method 2016As in Lua, this function may trigger a tag method
1985for the ``setglobal'' event. 2017for the ``setglobal'' event \see{tag-method}.
1986To set the real value of a global variable, 2018To set the real value of a global variable,
1987without invoking any tag method, 2019without invoking any tag method,
1988use the \verb|lua_rawset| function over the table of globals. 2020use \verb|lua_rawset| over the table of globals
2021(see below).
1989 2022
1990All global variables are kept in an ordinary Lua table. 2023All global variables are kept in an ordinary Lua table.
1991You can get this table calling 2024You can get this table calling
1992\Deffunc{lua_getglobals} 2025\DefAPI{lua_getglobals}
1993\begin{verbatim} 2026\begin{verbatim}
1994 void lua_getglobals (lua_State *L); 2027 void lua_getglobals (lua_State *L);
1995\end{verbatim} 2028\end{verbatim}
1996which pushes the current table of globals onto the stack. 2029which pushes the current table of globals onto the stack.
1997To set another table as the table of globals, 2030To set another table as the table of globals,
1998you call 2031you call
1999\Deffunc{lua_setglobals} 2032\DefAPI{lua_setglobals}
2000\begin{verbatim} 2033\begin{verbatim}
2001 void lua_setglobals (lua_State *L); 2034 void lua_setglobals (lua_State *L);
2002\end{verbatim} 2035\end{verbatim}
@@ -2009,7 +2042,7 @@ To read the value of in a table,
2009the table must reside somewhere in the stack. 2042the table must reside somewhere in the stack.
2010With this set, 2043With this set,
2011you call 2044you call
2012\Deffunc{lua_gettable} 2045\DefAPI{lua_gettable}
2013\begin{verbatim} 2046\begin{verbatim}
2014 void lua_gettable (lua_State *L, int index); 2047 void lua_gettable (lua_State *L, int index);
2015\end{verbatim} 2048\end{verbatim}
@@ -2021,7 +2054,7 @@ for the ``gettable'' event.
2021To get the real value of any table key, 2054To get the real value of any table key,
2022without invoking any tag method, 2055without invoking any tag method,
2023use the \emph{raw} version: 2056use the \emph{raw} version:
2024\Deffunc{lua_rawget} 2057\DefAPI{lua_rawget}
2025\begin{verbatim} 2058\begin{verbatim}
2026 void lua_rawget (lua_State *L, int index); 2059 void lua_rawget (lua_State *L, int index);
2027\end{verbatim} 2060\end{verbatim}
@@ -2030,7 +2063,7 @@ To store a value into a table that resides somewhere in the stack,
2030you push the key and the value onto the stack 2063you push the key and the value onto the stack
2031(in this order), 2064(in this order),
2032and then call 2065and then call
2033\Deffunc{lua_settable} 2066\DefAPI{lua_settable}
2034\begin{verbatim} 2067\begin{verbatim}
2035 void lua_settable (lua_State *L, int index); 2068 void lua_settable (lua_State *L, int index);
2036\end{verbatim} 2069\end{verbatim}
@@ -2041,13 +2074,13 @@ for the ``settable'' event.
2041To set the real value of any table index, 2074To set the real value of any table index,
2042without invoking any tag method, 2075without invoking any tag method,
2043use the \emph{raw} version: 2076use the \emph{raw} version:
2044\Deffunc{lua_rawset} 2077\DefAPI{lua_rawset}
2045\begin{verbatim} 2078\begin{verbatim}
2046 void lua_rawset (lua_State *L, int index); 2079 void lua_rawset (lua_State *L, int index);
2047\end{verbatim} 2080\end{verbatim}
2048 2081
2049Finally, the function 2082Finally, the function
2050\Deffunc{lua_newtable} 2083\DefAPI{lua_newtable}
2051\begin{verbatim} 2084\begin{verbatim}
2052 void lua_newtable (lua_State *L); 2085 void lua_newtable (lua_State *L);
2053\end{verbatim} 2086\end{verbatim}
@@ -2057,19 +2090,19 @@ creates a new, empty table and pushes it onto the stack.
2057The API has functions that help to use Lua tables as arrays, 2090The API has functions that help to use Lua tables as arrays,
2058that is, 2091that is,
2059tables indexed by numbers only: 2092tables indexed by numbers only:
2060\Deffunc{lua_rawgeti} 2093\DefAPI{lua_rawgeti}
2061\Deffunc{lua_rawseti} 2094\DefAPI{lua_rawseti}
2062\Deffunc{lua_getn} 2095\DefAPI{lua_getn}
2063\begin{verbatim} 2096\begin{verbatim}
2064 void lua_rawgeti (lua_State *L, int index, int n); 2097 void lua_rawgeti (lua_State *L, int index, int n);
2065 void lua_rawseti (lua_State *L, int index, int n); 2098 void lua_rawseti (lua_State *L, int index, int n);
2066 int lua_getn (lua_State *L, int index); 2099 int lua_getn (lua_State *L, int index);
2067\end{verbatim} 2100\end{verbatim}
2068 2101
2069\verb|lua_rawgeti| gets the value of the \verb|n|-th element of the table 2102\verb|lua_rawgeti| gets the value of the \M{n}-th element of the table
2070at stack position \verb|index|. 2103at stack position \verb|index|.
2071 2104
2072\verb|lua_rawseti| sets the value of the \verb|n|-th element of the table 2105\verb|lua_rawseti| sets the value of the \M{n}-th element of the table
2073at stack position \verb|index| to the value at the top of the stack. 2106at stack position \verb|index| to the value at the top of the stack.
2074 2107
2075\verb|lua_getn| returns the number of elements in the table 2108\verb|lua_getn| returns the number of elements in the table
@@ -2082,23 +2115,33 @@ the largest numerical index with a non-nil value in the table.
2082\subsection{Calling Lua Functions} 2115\subsection{Calling Lua Functions}
2083 2116
2084Functions defined in Lua 2117Functions defined in Lua
2118(and C~functions registered in Lua)
2085can be called from the host program. 2119can be called from the host program.
2086This is done using the following protocol: 2120This is done using the following protocol:
2087First, the function to be called is pushed onto the stack; 2121First, the function to be called is pushed onto the stack;
2088then, the arguments to the function are pushed 2122then, the arguments to the function are pushed
2089\see{pushing} in direct order, i.e., the first argument is pushed first. 2123\see{pushing} in \emph{direct order}, that is, the first argument is pushed first.
2090Finally, the function is called using 2124Finally, the function is called using
2091\Deffunc{lua_call} 2125\DefAPI{lua_call}
2092\begin{verbatim} 2126\begin{verbatim}
2093 int lua_call (lua_State *L, int nargs, int nresults); 2127 int lua_call (lua_State *L, int nargs, int nresults);
2094\end{verbatim} 2128\end{verbatim}
2095This function returns the same error codes as \verb|lua_dostring|. 2129This function returns the same error codes as \verb|lua_dostring| and
2096Here, 2130friends \see{luado}.
2131If you want to propagate the error,
2132instead of returning an error code,
2133use
2134\DefAPI{lua_rawcall}
2135\begin{verbatim}
2136 void lua_rawcall (lua_State *L, int nargs, int nresults);
2137\end{verbatim}
2138
2139In both functions,
2097\verb|nargs| is the number of arguments that you pushed onto the stack. 2140\verb|nargs| is the number of arguments that you pushed onto the stack.
2098All arguments and the function value are popped from the stack, 2141All arguments and the function value are popped from the stack,
2099and the function results are pushed. 2142and the function results are pushed.
2100The number of results are adjusted \see{adjust} to \verb|nresults|, 2143The number of results are adjusted \see{adjust} to \verb|nresults|,
2101unless \verb|nresults| is \IndexVerb{LUA_MULTRET}. 2144unless \verb|nresults| is \IndexAPI{LUA_MULTRET}.
2102In that case, \emph{all} results from the function are pushed. 2145In that case, \emph{all} results from the function are pushed.
2103The function results are pushed in direct order 2146The function results are pushed in direct order
2104(the first result is pushed first), 2147(the first result is pushed first),
@@ -2123,12 +2166,15 @@ Here it is in~C:
2123 lua_pop(L, 1); /* remove `t' from the stack */ 2166 lua_pop(L, 1); /* remove `t' from the stack */
2124\end{verbatim} 2167\end{verbatim}
2125Notice that the code above is ``balanced'': 2168Notice that the code above is ``balanced'':
2126at its end the stack is back to its original configuration. 2169at its end ,the stack is back to its original configuration.
2127This is considered good programming practice. 2170This is considered good programming practice.
2128 2171
2172\medskip
2173
2174
2129Some special Lua functions have their own C~interfaces. 2175Some special Lua functions have their own C~interfaces.
2130The host program can generate a Lua error calling the function 2176The host program can generate a Lua error calling the function
2131\Deffunc{lua_error} 2177\DefAPI{lua_error}
2132\begin{verbatim} 2178\begin{verbatim}
2133 void lua_error (lua_State *L, const char *message); 2179 void lua_error (lua_State *L, const char *message);
2134\end{verbatim} 2180\end{verbatim}
@@ -2144,31 +2190,35 @@ the \verb|message| is passed to the error handler function,
2144If \verb|message| is \verb|NULL|, 2190If \verb|message| is \verb|NULL|,
2145then \verb|_ERRORMESSAGE| is not called. 2191then \verb|_ERRORMESSAGE| is not called.
2146 2192
2147Tag methods can be changed with \Deffunc{lua_settagmethod} 2193\medskip
2194
2195Tag methods can be changed with \DefAPI{lua_settagmethod}
2148\begin{verbatim} 2196\begin{verbatim}
2149 void lua_settagmethod (lua_State *L, int tag, const char *event); 2197 void lua_settagmethod (lua_State *L, int tag, const char *event);
2150\end{verbatim} 2198\end{verbatim}
2151The second parameter is the tag, 2199The second parameter is the tag,
2152and the third is the event name \see{tag-method}; 2200and the third is the event name \see{tag-method};
2153the new method is popped from the stack. 2201the new method is popped from the stack.
2154To just get the current value of a tag method, 2202To get the current value of a tag method,
2155use the function \Deffunc{lua_gettagmethod} 2203use the function \DefAPI{lua_gettagmethod}
2156\begin{verbatim} 2204\begin{verbatim}
2157 void lua_gettagmethod (lua_State *L, int tag, const char *event); 2205 void lua_gettagmethod (lua_State *L, int tag, const char *event);
2158\end{verbatim} 2206\end{verbatim}
2159 2207
2160It is also possible to copy all tag methods from one tag 2208It is also possible to copy all tag methods from one tag
2161to another: \Deffunc{lua_copytagmethods} 2209to another: \DefAPI{lua_copytagmethods}
2162\begin{verbatim} 2210\begin{verbatim}
2163 int lua_copytagmethods (lua_State *L, int tagto, int tagfrom); 2211 int lua_copytagmethods (lua_State *L, int tagto, int tagfrom);
2164\end{verbatim} 2212\end{verbatim}
2165This function returns \verb|tagto|. 2213This function returns \verb|tagto|.
2166 2214
2167You can traverse a table with the function \Deffunc{lua_next} 2215\medskip
2216
2217You can traverse a table with the function \DefAPI{lua_next}
2168\begin{verbatim} 2218\begin{verbatim}
2169 int lua_next (lua_State *L, int index); 2219 int lua_next (lua_State *L, int index);
2170\end{verbatim} 2220\end{verbatim}
2171\verb|index| refers to the table to be traversed. 2221where \verb|index| refers to the table to be traversed.
2172The function pops a key from the stack, 2222The function pops a key from the stack,
2173and pushes a key-value pair from the table 2223and pushes a key-value pair from the table
2174(the ``next'' pair after the given key). 2224(the ``next'' pair after the given key).
@@ -2180,17 +2230,18 @@ A typical traversal looks like this:
2180 lua_pushnil(L); /* first key */ 2230 lua_pushnil(L); /* first key */
2181 while (lua_next(L, t) != 0) { 2231 while (lua_next(L, t) != 0) {
2182 /* `key' is at index -2 and `value' at index -1 */ 2232 /* `key' is at index -2 and `value' at index -1 */
2183 printf("%s - %s\n", lua_type(L, -2), lua_type(L, -1)); 2233 printf("%s - %s\n",
2234 lua_typename(L, lua_type(L, -2)), lua_typename(L, lua_type(L, -1)));
2184 lua_pop(L, 1); /* removes `value'; keeps `index' for next iteration */ 2235 lua_pop(L, 1); /* removes `value'; keeps `index' for next iteration */
2185 } 2236 }
2186\end{verbatim} 2237\end{verbatim}
2187 2238
2188The function \Deffunc{lua_concat} 2239The function \DefAPI{lua_concat}
2189\begin{verbatim} 2240\begin{verbatim}
2190 void lua_concat (lua_State *L, int n); 2241 void lua_concat (lua_State *L, int n);
2191\end{verbatim} 2242\end{verbatim}
2192concatenates the \verb|n| values at the top of the stack, 2243concatenates the \verb|n| values at the top of the stack,
2193pops them, and leaves the result at the top. 2244pops them, and leaves the result at the top;
2194\verb|n|~must be at least 2. 2245\verb|n|~must be at least 2.
2195Concatenation is done following the usual semantics of Lua 2246Concatenation is done following the usual semantics of Lua
2196\see{concat}. 2247\see{concat}.
@@ -2199,7 +2250,7 @@ Concatenation is done following the usual semantics of Lua
2199\subsection{Defining C Functions} \label{LuacallC} 2250\subsection{Defining C Functions} \label{LuacallC}
2200To register a C~function to Lua, 2251To register a C~function to Lua,
2201there is the following convenience macro: 2252there is the following convenience macro:
2202\Deffunc{lua_register} 2253\DefAPI{lua_register}
2203\begin{verbatim} 2254\begin{verbatim}
2204 #define lua_register(L, n, f) (lua_pushcfunction(L, f), lua_setglobal(L, n)) 2255 #define lua_register(L, n, f) (lua_pushcfunction(L, f), lua_setglobal(L, n))
2205 /* const char *n; */ 2256 /* const char *n; */
@@ -2209,7 +2260,7 @@ which receives the name the function will have in Lua,
2209and a pointer to the function. 2260and a pointer to the function.
2210This pointer must have type \verb|lua_CFunction|, 2261This pointer must have type \verb|lua_CFunction|,
2211which is defined as 2262which is defined as
2212\Deffunc{lua_CFunction} 2263\DefAPI{lua_CFunction}
2213\begin{verbatim} 2264\begin{verbatim}
2214 typedef int (*lua_CFunction) (lua_State *L); 2265 typedef int (*lua_CFunction) (lua_State *L);
2215\end{verbatim} 2266\end{verbatim}
@@ -2220,10 +2271,9 @@ In order to communicate properly with Lua,
2220a C~function must follow the following protocol, 2271a C~function must follow the following protocol,
2221which defines the way parameters and results are passed: 2272which defines the way parameters and results are passed:
2222A C~function receives its arguments from Lua in the stack, 2273A C~function receives its arguments from Lua in the stack,
2223in direct order (first argument is pushed first). 2274in direct order (the first argument is pushed first).
2224To return values to Lua, a C~function just pushes them onto the stack, 2275To return values to Lua, a C~function just pushes them onto the stack,
2225in direct order, 2276in direct order (the first result is pushed first),
2226%\see{valuesCLua},
2227and returns the number of results. 2277and returns the number of results.
2228Like a Lua function, a C~function called by Lua can also return 2278Like a Lua function, a C~function called by Lua can also return
2229many results. 2279many results.
@@ -2237,7 +2287,7 @@ of numerical arguments and returns their average and sum:
2237 int i; 2287 int i;
2238 for (i = 1; i <= n; i++) { 2288 for (i = 1; i <= n; i++) {
2239 if (!lua_isnumber(L, i)) 2289 if (!lua_isnumber(L, i))
2240 lua_error(L, "incorrect argument to function `foo'"); 2290 lua_error(L, "incorrect argument to function `average'");
2241 sum += lua_tonumber(L, i); 2291 sum += lua_tonumber(L, i);
2242 } 2292 }
2243 lua_pushnumber(L, sum/n); /* first result */ 2293 lua_pushnumber(L, sum/n); /* first result */
@@ -2245,7 +2295,7 @@ of numerical arguments and returns their average and sum:
2245 return 2; /* number of results */ 2295 return 2; /* number of results */
2246 } 2296 }
2247\end{verbatim} 2297\end{verbatim}
2248This function may be registered in Lua as \verb|average| by calling 2298This function may be registered in Lua as `\verb|average|' by calling
2249\begin{verbatim} 2299\begin{verbatim}
2250 lua_register(L, "average", foo); 2300 lua_register(L, "average", foo);
2251\end{verbatim} 2301\end{verbatim}
@@ -2256,10 +2306,10 @@ it is possible to associate some \emph{upvalues} to it
2256\see{upvalue}, 2306\see{upvalue},
2257thus creating a \IndexEmph{C~closure}; 2307thus creating a \IndexEmph{C~closure};
2258these values are passed to the function whenever it is called, 2308these values are passed to the function whenever it is called,
2259as common arguments. 2309as ordinary arguments.
2260To associate upvalues to a C~function, 2310To associate upvalues to a C~function,
2261first these values should be pushed onto the stack. 2311first these values should be pushed onto the stack.
2262Then the function \Deffunc{lua_pushcclosure} 2312Then the function \DefAPI{lua_pushcclosure}
2263\begin{verbatim} 2313\begin{verbatim}
2264 void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n); 2314 void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);
2265\end{verbatim} 2315\end{verbatim}
@@ -2286,9 +2336,9 @@ in the official Lua distribution.
2286If the C~code needs to keep a Lua value 2336If the C~code needs to keep a Lua value
2287outside the life span of a C~function, 2337outside the life span of a C~function,
2288then it must create a \Def{reference} to the value. 2338then it must create a \Def{reference} to the value.
2289The routines to manipulate references are the following: 2339The functions to manipulate references are the following:
2290\Deffunc{lua_ref}\Deffunc{lua_getref} 2340\DefAPI{lua_ref}\DefAPI{lua_getref}
2291\Deffunc{lua_unref} 2341\DefAPI{lua_unref}
2292\begin{verbatim} 2342\begin{verbatim}
2293 int lua_ref (lua_State *L, int lock); 2343 int lua_ref (lua_State *L, int lock);
2294 int lua_getref (lua_State *L, int ref); 2344 int lua_getref (lua_State *L, int ref);
@@ -2299,10 +2349,11 @@ The routines to manipulate references are the following:
2299the stack, creates a reference to it, 2349the stack, creates a reference to it,
2300and returns this reference. 2350and returns this reference.
2301For a \nil\ value, 2351For a \nil\ value,
2302the reference is always \verb|LUA_REFNIL|.\Deffunc{LUA_REFNIL} 2352the reference is always \verb|LUA_REFNIL|.\DefAPI{LUA_REFNIL}
2303The constant \verb|LUA_NOREF| \Deffunc{LUA_NOREF} 2353(\verb|lua.h| also defines a constant \verb|LUA_NOREF| \DefAPI{LUA_NOREF}
2304is different from any valid reference. 2354that
2305If \verb|lock| is true, then the object is \emph{locked}: 2355is different from any valid reference.)
2356If \verb|lock| is not zero, then the object is \emph{locked}:
2306this means the object will not be garbage collected. 2357this means the object will not be garbage collected.
2307\emph{Unlocked references may be garbage collected}. 2358\emph{Unlocked references may be garbage collected}.
2308 2359
@@ -2319,19 +2370,19 @@ it should be released with a call to \verb|lua_unref|.
2319\subsubsection*{Registry} 2370\subsubsection*{Registry}
2320 2371
2321When Lua starts, it registers a table at position 2372When Lua starts, it registers a table at position
2322\IndexVerb{LUA_REFREGISTRY}. 2373\IndexAPI{LUA_REFREGISTRY}.
2323It can be accessed through the macro\Deffunc{lua_getregistry} 2374It can be accessed through the macro\DefAPI{lua_getregistry}
2324\begin{verbatim} 2375\begin{verbatim}
2325 #define lua_getregistry(L) lua_getref(L, LUA_REFREGISTRY) 2376 #define lua_getregistry(L) lua_getref(L, LUA_REFREGISTRY)
2326\end{verbatim} 2377\end{verbatim}
2327This table can be used by C libraries as a general registry mechanism. 2378This table can be used by C~libraries as a general registry mechanism.
2328Any C library can store data into this table, 2379Any C~library can store data into this table,
2329as long as it chooses a key different from other libraries. 2380as long as it chooses a key different from other libraries.
2330 2381
2331 2382
2332\section{Standard Libraries} 2383\section{Standard Libraries}
2333 2384
2334The standard libraries provide useful routines 2385The standard libraries provide useful functions
2335that are implemented directly through the standard API. 2386that are implemented directly through the standard API.
2336Therefore, they are not necessary to the language, 2387Therefore, they are not necessary to the language,
2337and are provided as separate C~modules. 2388and are provided as separate C~modules.
@@ -2346,20 +2397,23 @@ To have access to these libraries,
2346the C~host program must call the functions 2397the C~host program must call the functions
2347\verb|lua_baselibopen|, 2398\verb|lua_baselibopen|,
2348\verb|lua_strlibopen|, \verb|lua_mathlibopen|, 2399\verb|lua_strlibopen|, \verb|lua_mathlibopen|,
2349and \verb|lua_iolibopen|, declared in \verb|lualib.h|. 2400and \verb|lua_iolibopen|, which are declared in \verb|lualib.h|.
2350\Deffunc{lua_strlibopen}\Deffunc{lua_mathlibopen}\Deffunc{lua_iolibopen} 2401\DefAPI{lua_baselibopen}
2402\DefAPI{lua_strlibopen}
2403\DefAPI{lua_mathlibopen}
2404\DefAPI{lua_iolibopen}
2405
2406\subsection{Basic Functions} \label{predefined}
2351 2407
2352The basic library provides some core functions to Lua. 2408The basic library provides some core functions to Lua.
2353Therefore, if you do not include this library in your application, 2409Therefore, if you do not include this library in your application,
2354you should check carefully whether you need to provide some alternative 2410you should check carefully whether you need to provide some alternative
2355implementation for some facilities. 2411implementation for some facilities.
2356(For instance, without function \verb|_ERRORMESSAGE|, 2412(For instance,
2357defined in the basic library, Lua is unable to show error messages.) 2413without function \verb|_ERRORMESSAGE|,
2414Lua is unable to show error messages.)
2358 2415
2359 2416\subsubsection*{\ff \T{_ALERT (message)}}\DefLIB{alert}\label{alert}
2360\subsection{Basic Functions} \label{predefined}
2361
2362\subsubsection*{\ff \T{_ALERT (message)}}\Deffunc{alert}\label{alert}
2363Prints its only string argument to \IndexVerb{stderr}. 2417Prints its only string argument to \IndexVerb{stderr}.
2364All error messages in Lua are printed through the function stored 2418All error messages in Lua are printed through the function stored
2365in the \verb|_ALERT| global variable 2419in the \verb|_ALERT| global variable
@@ -2368,7 +2422,7 @@ Therefore, a program may assign another function to this variable
2368to change the way such messages are shown 2422to change the way such messages are shown
2369(for instance, for systems without \verb|stderr|). 2423(for instance, for systems without \verb|stderr|).
2370 2424
2371\subsubsection*{\ff \T{assert (v [, message])}}\Deffunc{assert} 2425\subsubsection*{\ff \T{assert (v [, message])}}\DefLIB{assert}
2372Issues an \emph{``assertion failed!''} error 2426Issues an \emph{``assertion failed!''} error
2373when its argument \verb|v| is \nil. 2427when its argument \verb|v| is \nil.
2374This function is equivalent to the following Lua function: 2428This function is equivalent to the following Lua function:
@@ -2381,7 +2435,7 @@ This function is equivalent to the following Lua function:
2381 end 2435 end
2382\end{verbatim} 2436\end{verbatim}
2383 2437
2384\subsubsection*{\ff \T{call (func, arg [, mode [, errhandler]])}}\Deffunc{call} 2438\subsubsection*{\ff \T{call (func, arg [, mode [, errhandler]])}}\DefLIB{call}
2385\label{pdf-call} 2439\label{pdf-call}
2386Calls function \verb|func| with 2440Calls function \verb|func| with
2387the arguments given by the table \verb|arg|. 2441the arguments given by the table \verb|arg|.
@@ -2408,25 +2462,23 @@ while \verb|func| runs.
2408In particular, if \verb|errhandler| is \nil, 2462In particular, if \verb|errhandler| is \nil,
2409no error messages will be issued during the execution of the called function. 2463no error messages will be issued during the execution of the called function.
2410 2464
2411\subsubsection*{\ff \T{collectgarbage ([limit])}}\Deffunc{collectgarbage} 2465\subsubsection*{\ff \T{collectgarbage ([limit])}}\DefLIB{collectgarbage}
2412 2466
2413Sets the garbage-collection threshold for the given limit 2467Sets the garbage-collection threshold for the given limit
2414(in Kbytes), and checks it against the byte counter; 2468(in Kbytes), and checks it against the byte counter.
2415if the new threshold is smaller than the byte counter, 2469If the new threshold is smaller than the byte counter,
2416Lua runs immediately the garbage collector \see{GC}. 2470then Lua immediately runs the garbage collector \see{GC}.
2417
2418If \verb|limit| is absent, it defaults to zero 2471If \verb|limit| is absent, it defaults to zero
2419(thus forcing a garbage-collection cycle). 2472(thus forcing a garbage-collection cycle).
2420 2473%\verb|collectgarbage| is equivalent to
2421\verb|collectgarbage| is equivalent to 2474%the API function \verb|lua_setgcthreshold|.
2422the API function \verb|lua_setgcthreshold|.
2423 2475
2424\subsubsection*{\ff \T{copytagmethods (tagto, tagfrom)}} 2476\subsubsection*{\ff \T{copytagmethods (tagto, tagfrom)}}
2425\Deffunc{copytagmethods} 2477\DefLIB{copytagmethods}
2426Copies all tag methods from one tag to another; 2478Copies all tag methods from one tag to another;
2427it returns \verb|tagto|. 2479returns \verb|tagto|.
2428 2480
2429\subsubsection*{\ff \T{dofile (filename)}}\Deffunc{dofile} 2481\subsubsection*{\ff \T{dofile (filename)}}\DefLIB{dofile}
2430Receives a file name, 2482Receives a file name,
2431opens the named file, and executes its contents as a Lua chunk, 2483opens the named file, and executes its contents as a Lua chunk,
2432or as pre-compiled chunks. 2484or as pre-compiled chunks.
@@ -2437,9 +2489,9 @@ then \verb|dofile| returns \nil.
2437Otherwise, it returns the values returned by the chunk, 2489Otherwise, it returns the values returned by the chunk,
2438or a non-\nil\ value if the chunk returns no values. 2490or a non-\nil\ value if the chunk returns no values.
2439It issues an error when called with a non-string argument. 2491It issues an error when called with a non-string argument.
2440\verb|dofile| is equivalent to the API function \verb|lua_dofile|. 2492%\verb|dofile| is equivalent to the API function \verb|lua_dofile|.
2441 2493
2442\subsubsection*{\ff \T{dostring (string [, chunkname])}}\Deffunc{dostring} 2494\subsubsection*{\ff \T{dostring (string [, chunkname])}}\DefLIB{dostring}
2443Executes a given string as a Lua chunk. 2495Executes a given string as a Lua chunk.
2444If there is any error executing the string, 2496If there is any error executing the string,
2445then \verb|dostring| returns \nil. 2497then \verb|dostring| returns \nil.
@@ -2448,9 +2500,9 @@ or a non-\nil\ value if the chunk returns no values.
2448The optional parameter \verb|chunkname| 2500The optional parameter \verb|chunkname|
2449is the ``name of the chunk'', 2501is the ``name of the chunk'',
2450used in error messages and debug information. 2502used in error messages and debug information.
2451\verb|dostring| is equivalent to the API function \verb|lua_dostring|. 2503%\verb|dostring| is equivalent to the API function \verb|lua_dostring|.
2452 2504
2453\subsubsection*{\ff \T{error (message)}}\Deffunc{error}\label{pdf-error} 2505\subsubsection*{\ff \T{error (message)}}\DefLIB{error}\label{pdf-error}
2454Calls the error handler \see{error} and then terminates 2506Calls the error handler \see{error} and then terminates
2455the last protected function called 2507the last protected function called
2456(in~C: \verb|lua_dofile|, \verb|lua_dostring|, 2508(in~C: \verb|lua_dofile|, \verb|lua_dostring|,
@@ -2458,16 +2510,15 @@ the last protected function called
2458in Lua: \verb|dofile|, \verb|dostring|, or \verb|call| in protected mode). 2510in Lua: \verb|dofile|, \verb|dostring|, or \verb|call| in protected mode).
2459If \verb|message| is \nil, then the error handler is not called. 2511If \verb|message| is \nil, then the error handler is not called.
2460Function \verb|error| never returns. 2512Function \verb|error| never returns.
2461\verb|error| is equivalent to the API function \verb|lua_error|. 2513%\verb|error| is equivalent to the API function \verb|lua_error|.
2462 2514
2463\subsubsection*{\ff \T{foreach (table, func)}}\Deffunc{foreach} 2515\subsubsection*{\ff \T{foreach (table, func)}}\DefLIB{foreach}
2464Executes the given \verb|func| over all elements of \verb|table|. 2516Executes the given \verb|func| over all elements of \verb|table|.
2465For each element, the function is called with the index and 2517For each element, the function is called with the index and
2466respective value as arguments. 2518respective value as arguments.
2467If the function returns any non-\nil\ value, 2519If the function returns any non-\nil\ value,
2468then the loop is broken, and this value is returned 2520then the loop is broken, and this value is returned
2469as the final value of \verb|foreach|. 2521as the final value of \verb|foreach|.
2470
2471This function could be defined in Lua: 2522This function could be defined in Lua:
2472\begin{verbatim} 2523\begin{verbatim}
2473 function foreach (t, f) 2524 function foreach (t, f)
@@ -2479,21 +2530,20 @@ This function could be defined in Lua:
2479\end{verbatim} 2530\end{verbatim}
2480 2531
2481The behavior of \verb|foreach| is \emph{undefined} if you change 2532The behavior of \verb|foreach| is \emph{undefined} if you change
2482the table \verb|_t| during the traversal. 2533the table \verb|t| during the traversal.
2483 2534
2484 2535
2485\subsubsection*{\ff \T{foreachi (table, func)}}\Deffunc{foreachi} 2536\subsubsection*{\ff \T{foreachi (table, func)}}\DefLIB{foreachi}
2486Executes the given \verb|func| over the 2537Executes the given \verb|func| over the
2487numerical indices of \verb|table|. 2538numerical indices of \verb|table|.
2488For each index, the function is called with the index and 2539For each index, the function is called with the index and
2489respective value as arguments. 2540respective value as arguments.
2490Indices are visited in sequential order, 2541Indices are visited in sequential order,
2491from 1 to \verb|n|, 2542from~1 to \verb|n|,
2492where \verb|n| is the result of \verb|getn(table)| \see{getn}. 2543where \verb|n| is the result of \verb|getn(table)| \see{getn}.
2493If the function returns any non-\nil\ value, 2544If the function returns any non-\nil\ value,
2494then the loop is broken, and this value is returned 2545then the loop is broken, and this value is returned
2495as the final value of \verb|foreachi|. 2546as the final value of \verb|foreachi|.
2496
2497This function could be defined in Lua: 2547This function could be defined in Lua:
2498\begin{verbatim} 2548\begin{verbatim}
2499 function foreachi (t, f) 2549 function foreachi (t, f)
@@ -2505,58 +2555,52 @@ This function could be defined in Lua:
2505\end{verbatim} 2555\end{verbatim}
2506 2556
2507 2557
2508\subsubsection*{\ff \T{foreachvar (function)}}\Deffunc{foreachvar} 2558\subsubsection*{\ff \T{getglobal (name)}}\DefLIB{getglobal}
2509This function is obsolete.
2510Use \verb|foreach(globals(), function)| instead.
2511
2512
2513\subsubsection*{\ff \T{getglobal (name)}}\Deffunc{getglobal}
2514Gets the value of a global variable, 2559Gets the value of a global variable,
2515or calls a tag method for ``getglobal''. 2560or calls a tag method for ``getglobal''.
2516Its full semantics is explained in \See{tag-method}. 2561Its full semantics is explained in \See{tag-method}.
2517The string \verb|name| does not need to be a 2562The string \verb|name| does not need to be a
2518syntactically valid variable name. 2563syntactically valid variable name.
2519 2564
2520\subsubsection*{\ff \T{getn (table)}}\Deffunc{getn}\label{getn} 2565\subsubsection*{\ff \T{getn (table)}}\DefLIB{getn}\label{getn}
2521Returns the ``size'' of a table, when seen as a list. 2566Returns the ``size'' of a table, when seen as a list.
2522If the table has an \verb|n| field with a numeric value, 2567If the table has an \verb|n| field with a numeric value,
2523this value is its ``size''. 2568this value is the ``size'' of the table.
2524Otherwise, the size is the largest numerical index with a non-nil 2569Otherwise, the ``size'' is the largest numerical index with a non-nil
2525value in the table. 2570value in the table.
2526This function could be defined in Lua: 2571This function could be defined in Lua:
2527\begin{verbatim} 2572\begin{verbatim}
2528 function getn (t) 2573 function getn (t)
2529 if type(t.n) == 'number' then return t.n end 2574 if type(t.n) == "number" then return t.n end
2530 local max = 0 2575 local max = 0
2531 for i, _ in t do 2576 for i, _ in t do
2532 if type(i) == 'number' and i>max then max=i end 2577 if type(i) == "number" and i>max then max=i end
2533 end 2578 end
2534 return max 2579 return max
2535 end 2580 end
2536\end{verbatim} 2581\end{verbatim}
2537 2582
2538\subsubsection*{\ff \T{gettagmethod (tag, event)}} 2583\subsubsection*{\ff \T{gettagmethod (tag, event)}}
2539\Deffunc{gettagmethod} 2584\DefLIB{gettagmethod}
2540Returns the current tag method 2585Returns the current tag method
2541for a given pair \M{(tag, event)}. 2586for a given pair \M{(tag, event)}.
2542
2543This function cannot be used to get a tag method for the ``gc'' event. 2587This function cannot be used to get a tag method for the ``gc'' event.
2544(Such tag methods can only be manipulated by C code.) 2588(Such tag methods can only be manipulated by C~code.)
2545 2589
2546\subsubsection*{\ff \T{globals ([table])}}\Deffunc{globals} 2590\subsubsection*{\ff \T{globals ([table])}}\DefLIB{globals}
2547Returns the current table of globals. 2591Returns the current table of globals.
2548If the argument \verb|table| is given, 2592If the argument \verb|table| is given,
2549then it sets this table as the table of globals. 2593then it also sets this table as the table of globals.
2550 2594
2551\subsubsection*{\ff \T{newtag ()}}\Deffunc{newtag}\label{pdf-newtag} 2595\subsubsection*{\ff \T{newtag ()}}\DefLIB{newtag}\label{pdf-newtag}
2552Returns a new tag. 2596Returns a new tag.
2553\verb|newtag| is equivalent to the API function \verb|lua_newtag|. 2597%\verb|newtag| is equivalent to the API function \verb|lua_newtag|.
2554 2598
2555\subsubsection*{\ff \T{next (table, [index])}}\Deffunc{next} 2599\subsubsection*{\ff \T{next (table, [index])}}\DefLIB{next}
2556Allows a program to traverse all fields of a table. 2600Allows a program to traverse all fields of a table.
2557Its first argument is a table and its second argument 2601Its first argument is a table and its second argument
2558is an index in this table. 2602is an index in this table.
2559It returns the next index of the table and the 2603\verb|next| returns the next index of the table and the
2560value associated with the index. 2604value associated with the index.
2561When called with \nil\ as its second argument, 2605When called with \nil\ as its second argument,
2562\verb|next| returns the first index 2606\verb|next| returns the first index
@@ -2573,16 +2617,12 @@ Therefore, \verb|next| only considers fields with non-\nil\ values.
2573The order in which the indices are enumerated is not specified, 2617The order in which the indices are enumerated is not specified,
2574\emph{even for numeric indices} 2618\emph{even for numeric indices}
2575(to traverse a table in numeric order, 2619(to traverse a table in numeric order,
2576use a counter or the function \verb|foreachi|). 2620use a numerical \rwd{for} or the function \verb|foreachi|).
2577 2621
2578The behavior of \verb|next| is \emph{undefined} if you change 2622The behavior of \verb|next| is \emph{undefined} if you change
2579the table during the traversal. 2623the table during the traversal.
2580 2624
2581\subsubsection*{\ff \T{nextvar (name)}}\Deffunc{nextvar} 2625\subsubsection*{\ff \T{print (e1, e2, ...)}}\DefLIB{print}
2582This function is obsolete.
2583Use \verb|next(globals(), name)| instead.
2584
2585\subsubsection*{\ff \T{print (e1, e2, ...)}}\Deffunc{print}
2586Receives any number of arguments, 2626Receives any number of arguments,
2587and prints their values using the strings returned by \verb|tostring|. 2627and prints their values using the strings returned by \verb|tostring|.
2588This function is not intended for formatted output, 2628This function is not intended for formatted output,
@@ -2590,81 +2630,66 @@ but only as a quick way to show a value,
2590for instance for debugging. 2630for instance for debugging.
2591See \See{libio} for functions for formatted output. 2631See \See{libio} for functions for formatted output.
2592 2632
2593\subsubsection*{\ff \T{rawget (table, index)}}\Deffunc{rawget} 2633\subsubsection*{\ff \T{rawget (table, index)}}\DefLIB{rawget}
2594Gets the real value of \verb|table[index]|, 2634Gets the real value of \verb|table[index]|,
2595without invoking any tag method. 2635without invoking any tag method.
2596\verb|table| must be a table, 2636\verb|table| must be a table,
2597and \verb|index| is any value different from \nil. 2637and \verb|index| is any value different from \nil.
2598 2638
2599\subsubsection*{\ff \T{rawgetglobal (name)}}\Deffunc{rawgetglobal} 2639\subsubsection*{\ff \T{rawset (table, index, value)}}\DefLIB{rawset}
2600This function is obsolete.
2601Use \verb|rawget(globals(), name)| instead.
2602
2603\subsubsection*{\ff \T{rawgettable (table, index)}}\Deffunc{rawgettable}
2604This function has been renamed to \verb|rawget|.
2605
2606\subsubsection*{\ff \T{rawset (table, index, value)}}\Deffunc{rawset}
2607Sets the real value of \verb|table[index]| to \verb|value|, 2640Sets the real value of \verb|table[index]| to \verb|value|,
2608without invoking any tag method. 2641without invoking any tag method.
2609\verb|table| must be a table, 2642\verb|table| must be a table,
2610\verb|index| is any value different from \nil, 2643\verb|index| is any value different from \nil,
2611and \verb|value| is any Lua value. 2644and \verb|value| is any Lua value.
2612 2645
2613\subsubsection*{\ff \T{rawsetglobal (name, value)}}\Deffunc{rawsetglobal} 2646\subsubsection*{\ff \T{setglobal (name, value)}}\DefLIB{setglobal}
2614This function is obsolete.
2615Use \verb|rawset(globals(), name, value)| instead.
2616
2617\subsubsection*{\ff \T{rawsettable (table, index, value)}}\Deffunc{rawsettable}
2618This function has been renamed to \verb|rawset|.
2619
2620\subsubsection*{\ff \T{setglobal (name, value)}}\Deffunc{setglobal}
2621Sets the named global variable to the given value, 2647Sets the named global variable to the given value,
2622or calls a tag method for ``setglobal''. 2648or calls a tag method for ``setglobal''.
2623Its full semantics is explained in \See{tag-method}. 2649Its full semantics is explained in \See{tag-method}.
2624The string \verb|name| does not need to be a 2650The string \verb|name| does not need to be a
2625syntactically valid variable name. 2651syntactically valid variable name.
2626 2652
2627\subsubsection*{\ff \T{settag (t, tag)}}\Deffunc{settag} 2653\subsubsection*{\ff \T{settag (t, tag)}}\DefLIB{settag}
2628Sets the tag of a given table \see{TypesSec}. 2654Sets the tag of a given table \see{TypesSec}.
2629\verb|tag| must be a value created with \verb|newtag| 2655\verb|tag| must be a value created with \verb|newtag|
2630\see{pdf-newtag}. 2656\see{pdf-newtag}.
2631It returns the value of its first argument (the table). 2657\verb|settag| returns the value of its first argument (the table).
2632For the safety of host programs, 2658For the safety of host programs,
2633it is impossible to change the tag of a userdata from Lua. 2659it is impossible to change the tag of a userdata from Lua.
2634 2660
2635\subsubsection*{\ff \T{settagmethod (tag, event, newmethod)}} 2661\subsubsection*{\ff \T{settagmethod (tag, event, newmethod)}}
2636\Deffunc{settagmethod} 2662\DefLIB{settagmethod}
2637Sets a new tag method to the given pair \M{(tag, event)}. 2663Sets a new tag method to the given pair \M{(tag, event)} and
2638It returns the old method. 2664returns the old method.
2639If \verb|newmethod| is \nil, 2665If \verb|newmethod| is \nil,
2640then \verb|settagmethod| restores the default behavior for the given event. 2666then \verb|settagmethod| restores the default behavior for the given event.
2641
2642This function cannot be used to set a tag method for the ``gc'' event. 2667This function cannot be used to set a tag method for the ``gc'' event.
2643(Such tag methods can only be manipulated by C code.) 2668(Such tag methods can only be manipulated by C~code.)
2644 2669
2645\subsubsection*{\ff \T{sort (table [, comp])}}\Deffunc{sort} 2670\subsubsection*{\ff \T{sort (table [, comp])}}\DefLIB{sort}
2646Sorts table elements in a given order, \emph{in-place}, 2671Sorts table elements in a given order, \emph{in-place},
2647from \verb|table[1]| to \verb|table[n]|, 2672from \verb|table[1]| to \verb|table[n]|,
2648where \verb|n| is the result of \verb|getn(table)| \see{getn}. 2673where \verb|n| is the result of \verb|getn(table)| \see{getn}.
2649If \verb|comp| is given, 2674If \verb|comp| is given,
2650it must be a function that receives two table elements, 2675then it must be a function that receives two table elements,
2651and returns true when the first is less than the second 2676and returns true (that is, a value different from \nil)
2677when the first is less than the second
2652(so that \verb|not comp(a[i+1], a[i])| will be true after the sort). 2678(so that \verb|not comp(a[i+1], a[i])| will be true after the sort).
2653If \verb|comp| is not given, 2679If \verb|comp| is not given,
2654the standard Lua operator \verb|<| is used instead. 2680then the standard Lua operator \verb|<| is used instead.
2655 2681
2656The sort algorithm is not stable 2682The sort algorithm is \emph{not} stable
2657(that is, elements considered equal by the given order 2683(that is, elements considered equal by the given order
2658may have their relative positions changed by the sort). 2684may have their relative positions changed by the sort).
2659 2685
2660\subsubsection*{\ff \T{tag (v)}}\Deffunc{tag}\label{pdf-tag} 2686\subsubsection*{\ff \T{tag (v)}}\DefLIB{tag}\label{pdf-tag}
2661Allows Lua programs to test the tag of a value \see{TypesSec}. 2687Allows Lua programs to test the tag of a value \see{TypesSec}.
2662It receives one argument, and returns its tag (a number). 2688It receives one argument, and returns its tag (a number).
2663\verb|tag| is equivalent to the API function \verb|lua_tag|. 2689%\verb|tag| is equivalent to the API function \verb|lua_tag|.
2664 2690
2665\subsubsection*{\ff \T{tonumber (e [, base])}}\Deffunc{tonumber} 2691\subsubsection*{\ff \T{tonumber (e [, base])}}\DefLIB{tonumber}
2666Receives one argument, 2692Tries to convert its argument to a number.
2667and tries to convert it to a number.
2668If the argument is already a number or a string convertible 2693If the argument is already a number or a string convertible
2669to a number, then \verb|tonumber| returns that number; 2694to a number, then \verb|tonumber| returns that number;
2670otherwise, it returns \nil. 2695otherwise, it returns \nil.
@@ -2673,12 +2698,11 @@ An optional argument specifies the base to interpret the numeral.
2673The base may be any integer between 2 and 36, inclusive. 2698The base may be any integer between 2 and 36, inclusive.
2674In bases above~10, the letter `A' (either upper or lower case) 2699In bases above~10, the letter `A' (either upper or lower case)
2675represents~10, `B' represents~11, and so forth, with `Z' representing 35. 2700represents~10, `B' represents~11, and so forth, with `Z' representing 35.
2676
2677In base 10 (the default), the number may have a decimal part, 2701In base 10 (the default), the number may have a decimal part,
2678as well as an optional exponent part \see{coercion}. 2702as well as an optional exponent part \see{coercion}.
2679In other bases, only unsigned integers are accepted. 2703In other bases, only unsigned integers are accepted.
2680 2704
2681\subsubsection*{\ff \T{tostring (e)}}\Deffunc{tostring} 2705\subsubsection*{\ff \T{tostring (e)}}\DefLIB{tostring}
2682Receives an argument of any type and 2706Receives an argument of any type and
2683converts it to a string in a reasonable format. 2707converts it to a string in a reasonable format.
2684For complete control of how numbers are converted, 2708For complete control of how numbers are converted,
@@ -2686,7 +2710,7 @@ use function \verb|format|.
2686 2710
2687 2711
2688 2712
2689\subsubsection*{\ff \T{tinsert (table [, pos] , value)}}\Deffunc{tinsert} 2713\subsubsection*{\ff \T{tinsert (table [, pos] , value)}}\DefLIB{tinsert}
2690 2714
2691Inserts element \verb|value| at table position \verb|pos|, 2715Inserts element \verb|value| at table position \verb|pos|,
2692shifting other elements to open space, if necessary. 2716shifting other elements to open space, if necessary.
@@ -2696,7 +2720,6 @@ so that a call \verb|tinsert(t,x)| inserts \verb|x| at the end
2696of table \verb|t|. 2720of table \verb|t|.
2697This function also sets or increments the field \verb|n| of the table 2721This function also sets or increments the field \verb|n| of the table
2698to \verb|n+1|. 2722to \verb|n+1|.
2699
2700This function is equivalent to the following Lua function, 2723This function is equivalent to the following Lua function,
2701except that the table accesses are all \emph{raw} 2724except that the table accesses are all \emph{raw}
2702(that is, without tag methods): 2725(that is, without tag methods):
@@ -2717,7 +2740,7 @@ except that the table accesses are all \emph{raw}
2717 end 2740 end
2718\end{verbatim} 2741\end{verbatim}
2719 2742
2720\subsubsection*{\ff \T{tremove (table [, pos])}}\Deffunc{tremove} 2743\subsubsection*{\ff \T{tremove (table [, pos])}}\DefLIB{tremove}
2721 2744
2722Removes from \verb|table| the element at position \verb|pos|, 2745Removes from \verb|table| the element at position \verb|pos|,
2723shifting other elements to close the space, if necessary. 2746shifting other elements to close the space, if necessary.
@@ -2747,7 +2770,7 @@ except that the table accesses are all \emph{raw}
2747 end 2770 end
2748\end{verbatim} 2771\end{verbatim}
2749 2772
2750\subsubsection*{\ff \T{type (v)}}\Deffunc{type}\label{pdf-type} 2773\subsubsection*{\ff \T{type (v)}}\DefLIB{type}\label{pdf-type}
2751Allows Lua programs to test the type of a value. 2774Allows Lua programs to test the type of a value.
2752It receives one argument, and returns its type, coded as a string. 2775It receives one argument, and returns its type, coded as a string.
2753The possible results of this function are 2776The possible results of this function are
@@ -2764,19 +2787,20 @@ This library provides generic functions for string manipulation,
2764such as finding and extracting substrings and pattern matching. 2787such as finding and extracting substrings and pattern matching.
2765When indexing a string in Lua, the first character is at position~1 2788When indexing a string in Lua, the first character is at position~1
2766(not at~0, as in C). 2789(not at~0, as in C).
2790Also,
2791indices are allowed to be negative and are intepreted as indexing backwards,
2792from the end of the string. Thus, the last character is at position \Math{-1},
2793and so on.
2767 2794
2768\subsubsection*{\ff \T{strbyte (s [, i])}}\Deffunc{strbyte} 2795\subsubsection*{\ff \T{strbyte (s [, i])}}\DefLIB{strbyte}
2769Returns the internal numerical code of the character \verb|s[i]|. 2796Returns the internal numerical code of the \M{i}-th character of \verb|s|.
2770If \verb|i| is absent, then it is assumed to be 1. 2797If \verb|i| is absent, then it is assumed to be~1.
2771If \verb|i| is negative, 2798\verb|i| may be negative.
2772it is replaced by the length of the string minus its
2773absolute value plus 1.
2774Therefore, \Math{-1} points to the last character of \verb|s|.
2775 2799
2776\NOTE 2800\NOTE
2777Numerical codes are not necessarily portable across platforms. 2801Numerical codes are not necessarily portable across platforms.
2778 2802
2779\subsubsection*{\ff \T{strchar (i1, i2, \ldots)}}\Deffunc{strchar} 2803\subsubsection*{\ff \T{strchar (i1, i2, \ldots)}}\DefLIB{strchar}
2780Receives 0 or more integers. 2804Receives 0 or more integers.
2781Returns a string with length equal to the number of arguments, 2805Returns a string with length equal to the number of arguments,
2782wherein each character has the internal numerical code equal 2806wherein each character has the internal numerical code equal
@@ -2785,52 +2809,46 @@ to its correspondent argument.
2785\NOTE 2809\NOTE
2786Numerical codes are not necessarily portable across platforms. 2810Numerical codes are not necessarily portable across platforms.
2787 2811
2788\subsubsection*{\ff \T{strfind (str, pattern [, init [, plain]])}} 2812\subsubsection*{\ff \T{strfind (s, pattern [, init [, plain]])}}
2789\Deffunc{strfind} 2813\DefLIB{strfind}
2790Looks for the first \emph{match} of 2814Looks for the first \emph{match} of
2791\verb|pattern| in \verb|str|. 2815\verb|pattern| in \verb|s|.
2792If it finds one, then it returns the indices of \verb|str| 2816If it finds one, then \verb|strfind| returns the indices of \verb|s|
2793where this occurrence starts and ends; 2817where this occurrence starts and ends;
2794otherwise, it returns \nil. 2818otherwise, it returns \nil.
2795If the pattern specifies captures (see \verb|gsub| below), 2819If the pattern specifies captures (see \verb|gsub| below),
2796the captured strings are returned as extra results. 2820the captured strings are returned as extra results.
2797A third optional numerical argument specifies where to start the search; 2821A third, optional numerical argument \verb|init| specifies
2798its default value is 1. 2822where to start the search;
2799If \verb|init| is negative, 2823its default value is~1, and may be negative.
2800it is replaced by the length of the string minus its 2824A value of~1 as a fourth, optional argument \verb|plain|
2801absolute value plus 1.
2802Therefore, \Math{-1} points to the last character of \verb|str|.
2803A value of 1 as a fourth optional argument
2804turns off the pattern matching facilities, 2825turns off the pattern matching facilities,
2805so the function does a plain ``find substring'' operation, 2826so the function does a plain ``find substring'' operation,
2806with no characters in \verb|pattern| being considered ``magic''. 2827with no characters in \verb|pattern| being considered ``magic''.
2828Note that if \verb|plain| is given, then \verb|init| must be given too.
2807 2829
2808\subsubsection*{\ff \T{strlen (s)}}\Deffunc{strlen} 2830\subsubsection*{\ff \T{strlen (s)}}\DefLIB{strlen}
2809Receives a string and returns its length. 2831Receives a string and returns its length.
2810The empty string \verb|""| has length 0. 2832The empty string \verb|""| has length 0.
2811Embedded zeros are counted, 2833Embedded zeros are counted,
2812and so \verb|"a\000b\000c"| has length 5. 2834and so \verb|"a\000b\000c"| has length 5.
2813 2835
2814\subsubsection*{\ff \T{strlower (s)}}\Deffunc{strlower} 2836\subsubsection*{\ff \T{strlower (s)}}\DefLIB{strlower}
2815Receives a string and returns a copy of that string with all 2837Receives a string and returns a copy of that string with all
2816upper case letters changed to lower case. 2838upper case letters changed to lower case.
2817All other characters are left unchanged. 2839All other characters are left unchanged.
2818The definition of what is an upper-case 2840The definition of what is an upper-case
2819letter depends on the current locale. 2841letter depends on the current locale.
2820 2842
2821\subsubsection*{\ff \T{strrep (s, n)}}\Deffunc{strrep} 2843\subsubsection*{\ff \T{strrep (s, n)}}\DefLIB{strrep}
2822Returns a string that is the concatenation of \verb|n| copies of 2844Returns a string that is the concatenation of \verb|n| copies of
2823the string \verb|s|. 2845the string \verb|s|.
2824 2846
2825\subsubsection*{\ff \T{strsub (s, i [, j])}}\Deffunc{strsub} 2847\subsubsection*{\ff \T{strsub (s, i [, j])}}\DefLIB{strsub}
2826Returns another string, which is a substring of \verb|s|, 2848Returns another string, which is a substring of \verb|s|,
2827starting at \verb|i| and running until \verb|j|. 2849starting at \verb|i| and running until \verb|j|;
2828If \verb|i| or \verb|j| are negative, 2850\verb|i| and \verb|j| may be negative,
2829they are replaced by the length of the string minus their 2851If \verb|j| is absent, then it is assumed to be equal to \Math{-1}
2830absolute value plus 1.
2831Therefore, \Math{-1} points to the last character of \verb|s|
2832and \Math{-2} to the previous one.
2833If \verb|j| is absent, it is assumed to be equal to \Math{-1}
2834(which is the same as the string length). 2852(which is the same as the string length).
2835In particular, 2853In particular,
2836the call \verb|strsub(s,1,j)| returns a prefix of \verb|s| 2854the call \verb|strsub(s,1,j)| returns a prefix of \verb|s|
@@ -2838,14 +2856,14 @@ with length \verb|j|,
2838and the call \verb|strsub(s, -i)| returns a suffix of \verb|s| 2856and the call \verb|strsub(s, -i)| returns a suffix of \verb|s|
2839with length \verb|i|. 2857with length \verb|i|.
2840 2858
2841\subsubsection*{\ff \T{strupper (s)}}\Deffunc{strupper} 2859\subsubsection*{\ff \T{strupper (s)}}\DefLIB{strupper}
2842Receives a string and returns a copy of that string with all 2860Receives a string and returns a copy of that string with all
2843lower case letters changed to upper case. 2861lower case letters changed to upper case.
2844All other characters are left unchanged. 2862All other characters are left unchanged.
2845The definition of what is a lower case 2863The definition of what is a lower case
2846letter depends on the current locale. 2864letter depends on the current locale.
2847 2865
2848\subsubsection*{\ff \T{format (formatstring, e1, e2, \ldots)}}\Deffunc{format} 2866\subsubsection*{\ff \T{format (formatstring, e1, e2, \ldots)}}\DefLIB{format}
2849\label{format} 2867\label{format}
2850Returns a formatted version of its variable number of arguments 2868Returns a formatted version of its variable number of arguments
2851following the description given in its first argument (which must be a string). 2869following the description given in its first argument (which must be a string).
@@ -2891,14 +2909,15 @@ For example, \verb|"%*g"| can be simulated with
2891 2909
2892\NOTE 2910\NOTE
2893Neither the format string nor the string values to be formatted with 2911Neither the format string nor the string values to be formatted with
2894\T{format} can contain embedded zeros. 2912\verb|%s| can contain embedded zeros.
2913\verb|%q| handles string values with embedded zeros.
2895 2914
2896\subsubsection*{\ff \T{gsub (s, pat, repl [, n])}} 2915\subsubsection*{\ff \T{gsub (s, pat, repl [, n])}}
2897\Deffunc{gsub} 2916\DefLIB{gsub}
2898Returns a copy of \verb|s|, 2917Returns a copy of \verb|s|
2899in which all occurrences of the pattern \verb|pat| have been 2918in which all occurrences of the pattern \verb|pat| have been
2900replaced by a replacement string specified by \verb|repl|. 2919replaced by a replacement string specified by \verb|repl|.
2901This function also returns, as a second value, 2920\verb|gsub| also returns, as a second value,
2902the total number of substitutions made. 2921the total number of substitutions made.
2903 2922
2904If \verb|repl| is a string, then its value is used for replacement. 2923If \verb|repl| is a string, then its value is used for replacement.
@@ -2951,7 +2970,7 @@ Here are some examples:
2951a \Def{character class} is used to represent a set of characters. 2970a \Def{character class} is used to represent a set of characters.
2952The following combinations are allowed in describing a character class: 2971The following combinations are allowed in describing a character class:
2953\begin{description} 2972\begin{description}
2954\item[\emph{x}] (where \emph{x} is any character not in the list 2973\item[\emph{x}] (where \emph{x} is any magic characters
2955\verb|^$()%.[]*+-?|) 2974\verb|^$()%.[]*+-?|)
2956--- represents the character \emph{x} itself. 2975--- represents the character \emph{x} itself.
2957\item[\T{.}] --- (a dot) represents all characters. 2976\item[\T{.}] --- (a dot) represents all characters.
@@ -2967,14 +2986,14 @@ The following combinations are allowed in describing a character class:
2967\item[\T{\%z}] --- represents the character with representation 0. 2986\item[\T{\%z}] --- represents the character with representation 0.
2968\item[\T{\%\M{x}}] (where \M{x} is any non-alphanumeric character) --- 2987\item[\T{\%\M{x}}] (where \M{x} is any non-alphanumeric character) ---
2969represents the character \M{x}. 2988represents the character \M{x}.
2970This is the standard way to escape the magic characters \verb|()%.[]*+-?|. 2989This is the standard way to escape the magic characters.
2971We recommend that any ``punct'' character (even the non magic) 2990We recommend that any punctuation character (even the non magic)
2972should be preceded by a \verb|%| 2991should be preceded by a \verb|%|
2973when used to represent itself in a pattern. 2992when used to represent itself in a pattern.
2974 2993
2975\item[\T{[char-set]}] --- 2994\item[\T{[char-set]}] ---
2976represents the class which is the union of all 2995represents the class which is the union of all
2977characters in char-set. 2996characters in \verb|char-set|.
2978A range of characters may be specified by 2997A range of characters may be specified by
2979separating the end characters of the range with a \verb|-|. 2998separating the end characters of the range with a \verb|-|.
2980All classes \verb|%|\emph{x} described above may also be used as 2999All classes \verb|%|\emph{x} described above may also be used as
@@ -2990,9 +3009,9 @@ The interaction between ranges and classes is not defined.
2990Therefore, patterns like \verb|[%a-z]| or \verb|[a-%%]| 3009Therefore, patterns like \verb|[%a-z]| or \verb|[a-%%]|
2991have no meaning. 3010have no meaning.
2992 3011
2993\item[\T{[\^{ }char-set]}] --- 3012\item[\T{[\^\null char-set]}] ---
2994represents the complement of char-set, 3013represents the complement of \verb|char-set|,
2995where char-set is interpreted as above. 3014where \verb|char-set| is interpreted as above.
2996\end{description} 3015\end{description}
2997For all classes represented by single letters (\verb|%a|, \verb|%c|, \ldots), 3016For all classes represented by single letters (\verb|%a|, \verb|%c|, \ldots),
2998the corresponding upper-case letter represents the complement of the class. 3017the corresponding upper-case letter represents the complement of the class.
@@ -3034,7 +3053,7 @@ such item matches strings that start with~\M{x}, end with~\M{y},
3034and where the \M{x} and \M{y} are \emph{balanced}. 3053and where the \M{x} and \M{y} are \emph{balanced}.
3035This means that, if one reads the string from left to right, 3054This means that, if one reads the string from left to right,
3036counting \Math{+1} for an \M{x} and \Math{-1} for a \M{y}, 3055counting \Math{+1} for an \M{x} and \Math{-1} for a \M{y},
3037the ending \M{y} is the first where the count reaches 0. 3056the ending \M{y} is the first \M{y} where the count reaches 0.
3038For instance, the item \verb|%b()| matches expressions with 3057For instance, the item \verb|%b()| matches expressions with
3039balanced parentheses. 3058balanced parentheses.
3040\end{itemize} 3059\end{itemize}
@@ -3050,7 +3069,7 @@ At other positions,
3050 3069
3051\paragraph{Captures:} 3070\paragraph{Captures:}
3052A pattern may contain sub-patterns enclosed in parentheses, 3071A pattern may contain sub-patterns enclosed in parentheses,
3053that describe \Def{captures}. 3072they describe \Def{captures}.
3054When a match succeeds, the sub-strings of the subject string 3073When a match succeeds, the sub-strings of the subject string
3055that match captures are stored (\emph{captured}) for future use. 3074that match captures are stored (\emph{captured}) for future use.
3056Captures are numbered according to their left parentheses. 3075Captures are numbered according to their left parentheses.
@@ -3061,8 +3080,7 @@ the character matching \verb|.| is captured with number~2,
3061and the part matching \verb|%s*| has number~3. 3080and the part matching \verb|%s*| has number~3.
3062 3081
3063\NOTE 3082\NOTE
3064{\em A pattern cannot contain embedded zeros. 3083A pattern cannot contain embedded zeros. Use \verb|%z| instead.
3065Use \verb|%z| instead.}
3066 3084
3067 3085
3068\subsection{Mathematical Functions} \label{mathlib} 3086\subsection{Mathematical Functions} \label{mathlib}
@@ -3072,22 +3090,21 @@ In addition, it registers a tag method for the binary operator \verb|^| that
3072returns \Math{x^y} when applied to numbers \verb|x^y|. 3090returns \Math{x^y} when applied to numbers \verb|x^y|.
3073 3091
3074The library provides the following functions: 3092The library provides the following functions:
3075\Deffunc{abs}\Deffunc{acos}\Deffunc{asin}\Deffunc{atan} 3093\DefLIB{abs}\DefLIB{acos}\DefLIB{asin}\DefLIB{atan}
3076\Deffunc{atan2}\Deffunc{ceil}\Deffunc{cos}\Deffunc{floor} 3094\DefLIB{atan2}\DefLIB{ceil}\DefLIB{cos}\DefLIB{def}\DefLIB{exp}
3077\Deffunc{log}\Deffunc{log10}\Deffunc{max}\Deffunc{min} 3095\DefLIB{floor}\DefLIB{log}\DefLIB{log10}\DefLIB{max}\DefLIB{min}
3078\Deffunc{mod}\Deffunc{sin}\Deffunc{sqrt}\Deffunc{tan} 3096\DefLIB{mod}\DefLIB{rad}\DefLIB{sin}\DefLIB{sqrt}\DefLIB{tan}
3079\Deffunc{frexp}\Deffunc{ldexp} 3097\DefLIB{frexp}\DefLIB{ldexp}\DefLIB{random}\DefLIB{randomseed}
3080\Deffunc{random}\Deffunc{randomseed} 3098\begin{verbatim}
3081\begin{verbatim} 3099 abs acos asin atan atan2 ceil cos deg exp floor log log10
3082 abs acos asin atan atan2 ceil cos deg floor log log10
3083 max min mod rad sin sqrt tan frexp ldexp random randomseed 3100 max min mod rad sin sqrt tan frexp ldexp random randomseed
3084\end{verbatim} 3101\end{verbatim}
3085plus a global variable \IndexVerb{PI}. 3102plus a global variable \IndexLIB{PI}.
3086Most of them 3103Most of them
3087are only interfaces to the homonymous functions in the C~library, 3104are only interfaces to the homonymous functions in the C~library,
3088except that, for the trigonometric functions, 3105except that, for the trigonometric functions,
3089all angles are expressed in \emph{degrees}, not radians. 3106all angles are expressed in \emph{degrees}, not radians.
3090Functions \IndexVerb{deg} and \IndexVerb{rad} can be used to convert 3107The functions \verb|deg| and \verb|rad| can be used to convert
3091between radians and degrees. 3108between radians and degrees.
3092 3109
3093The function \verb|max| returns the maximum 3110The function \verb|max| returns the maximum
@@ -3116,19 +3133,19 @@ called \verb|_INPUT| and \verb|_OUTPUT|.
3116The global variables 3133The global variables
3117\verb|_STDIN|, \verb|_STDOUT|, and \verb|_STDERR| 3134\verb|_STDIN|, \verb|_STDOUT|, and \verb|_STDERR|
3118are initialized with file descriptors for 3135are initialized with file descriptors for
3119\verb|stdin|, \verb|stdout| and \verb|stderr|. 3136\verb|stdin|, \verb|stdout|, and \verb|stderr|.
3120Initially, \verb|_INPUT=_STDIN| and \verb|_OUTPUT=_STDOUT|. 3137Initially, \verb|_INPUT=_STDIN| and \verb|_OUTPUT=_STDOUT|.
3121\Deffunc{_INPUT}\Deffunc{_OUTPUT} 3138\DefLIB{_INPUT}\DefLIB{_OUTPUT}
3122\Deffunc{_STDIN}\Deffunc{_STDOUT}\Deffunc{_STDERR} 3139\DefLIB{_STDIN}\DefLIB{_STDOUT}\DefLIB{_STDERR}
3123 3140
3124A file handle is a userdata containing the file stream \verb|FILE*|, 3141A file handle is a userdata containing the file stream (\verb|FILE*|),
3125and with a distinctive tag created by the I/O library. 3142and with a distinctive tag created by the I/O library.
3126 3143
3127Unless otherwise stated, 3144Unless otherwise stated,
3128all I/O functions return \nil\ on failure and 3145all I/O functions return \nil\ on failure and
3129some value different from \nil\ on success. 3146some value different from \nil\ on success.
3130 3147
3131\subsubsection*{\ff \T{openfile (filename, mode)}}\Deffunc{openfile} 3148\subsubsection*{\ff \T{openfile (filename, mode)}}\DefLIB{openfile}
3132 3149
3133This function opens a file, 3150This function opens a file,
3134in the mode specified in the string \verb|mode|. 3151in the mode specified in the string \verb|mode|.
@@ -3150,12 +3167,12 @@ The \verb|mode| string may also have a \verb|b| at the end,
3150which is needed in some systems to open the file in binary mode. 3167which is needed in some systems to open the file in binary mode.
3151This string is exactlty what is used in the standard~C function \verb|fopen|. 3168This string is exactlty what is used in the standard~C function \verb|fopen|.
3152 3169
3153\subsubsection*{\ff \T{closefile (handle)}}\Deffunc{closefile} 3170\subsubsection*{\ff \T{closefile (handle)}}\DefLIB{closefile}
3154 3171
3155This function closes the given file. 3172This function closes the given file.
3156It does not modify either \verb|_INPUT| or \verb|_OUTPUT|. 3173It does not modify either \verb|_INPUT| or \verb|_OUTPUT|.
3157 3174
3158\subsubsection*{\ff \T{readfrom (filename)}}\Deffunc{readfrom} 3175\subsubsection*{\ff \T{readfrom (filename)}}\DefLIB{readfrom}
3159 3176
3160This function may be called in two ways. 3177This function may be called in two ways.
3161When called with a file name, it opens the named file, 3178When called with a file name, it opens the named file,
@@ -3165,20 +3182,18 @@ It does not close the current input file.
3165When called without parameters, 3182When called without parameters,
3166it closes the \verb|_INPUT| file, 3183it closes the \verb|_INPUT| file,
3167and restores \verb|stdin| as the value of \verb|_INPUT|. 3184and restores \verb|stdin| as the value of \verb|_INPUT|.
3168
3169If this function fails, it returns \nil, 3185If this function fails, it returns \nil,
3170plus a string describing the error. 3186plus a string describing the error.
3171 3187
3172\NOTE 3188\NOTE
3173(system dependency) 3189If \verb|filename| starts with a \verb-|-,
3174if \verb|filename| starts with a \verb-|-,
3175then a \Index{piped input} is opened, via function \IndexVerb{popen}. 3190then a \Index{piped input} is opened, via function \IndexVerb{popen}.
3176Not all systems implement pipes. 3191Not all systems implement pipes.
3177Moreover, 3192Moreover,
3178the number of files that can be open at the same time is 3193the number of files that can be open at the same time is
3179usually limited and depends on the system. 3194usually limited and depends on the system.
3180 3195
3181\subsubsection*{\ff \T{writeto (filename)}}\Deffunc{writeto} 3196\subsubsection*{\ff \T{writeto (filename)}}\DefLIB{writeto}
3182 3197
3183This function may be called in two ways. 3198This function may be called in two ways.
3184When called with a file name, 3199When called with a file name,
@@ -3192,20 +3207,18 @@ When called without parameters,
3192this function closes the \verb|_OUTPUT| file, 3207this function closes the \verb|_OUTPUT| file,
3193and restores \verb|stdout| as the value of \verb|_OUTPUT|. 3208and restores \verb|stdout| as the value of \verb|_OUTPUT|.
3194\index{closing a file} 3209\index{closing a file}
3195
3196If this function fails, it returns \nil, 3210If this function fails, it returns \nil,
3197plus a string describing the error. 3211plus a string describing the error.
3198 3212
3199\NOTE 3213\NOTE
3200(system dependency) 3214If \verb|filename| starts with a \verb-|-,
3201if \verb|filename| starts with a \verb-|-,
3202then a \Index{piped input} is opened, via function \IndexVerb{popen}. 3215then a \Index{piped input} is opened, via function \IndexVerb{popen}.
3203Not all systems implement pipes. 3216Not all systems implement pipes.
3204Moreover, 3217Moreover,
3205the number of files that can be open at the same time is 3218the number of files that can be open at the same time is
3206usually limited and depends on the system. 3219usually limited and depends on the system.
3207 3220
3208\subsubsection*{\ff \T{appendto (filename)}}\Deffunc{appendto} 3221\subsubsection*{\ff \T{appendto (filename)}}\DefLIB{appendto}
3209 3222
3210Opens a file named \verb|filename| and sets it as the 3223Opens a file named \verb|filename| and sets it as the
3211value of \verb|_OUTPUT|. 3224value of \verb|_OUTPUT|.
@@ -3215,19 +3228,19 @@ instead, anything written to the file is appended to its end.
3215If this function fails, it returns \nil, 3228If this function fails, it returns \nil,
3216plus a string describing the error. 3229plus a string describing the error.
3217 3230
3218\subsubsection*{\ff \T{remove (filename)}}\Deffunc{remove} 3231\subsubsection*{\ff \T{remove (filename)}}\DefLIB{remove}
3219 3232
3220Deletes the file with the given name. 3233Deletes the file with the given name.
3221If this function fails, it returns \nil, 3234If this function fails, it returns \nil,
3222plus a string describing the error. 3235plus a string describing the error.
3223 3236
3224\subsubsection*{\ff \T{rename (name1, name2)}}\Deffunc{rename} 3237\subsubsection*{\ff \T{rename (name1, name2)}}\DefLIB{rename}
3225 3238
3226Renames file named \verb|name1| to \verb|name2|. 3239Renames file named \verb|name1| to \verb|name2|.
3227If this function fails, it returns \nil, 3240If this function fails, it returns \nil,
3228plus a string describing the error. 3241plus a string describing the error.
3229 3242
3230\subsubsection*{\ff \T{flush ([filehandle])}}\Deffunc{flush} 3243\subsubsection*{\ff \T{flush ([filehandle])}}\DefLIB{flush}
3231 3244
3232Saves any written data to the given file. 3245Saves any written data to the given file.
3233If \verb|filehandle| is not specified, 3246If \verb|filehandle| is not specified,
@@ -3235,7 +3248,7 @@ then \verb|flush| flushes all open files.
3235If this function fails, it returns \nil, 3248If this function fails, it returns \nil,
3236plus a string describing the error. 3249plus a string describing the error.
3237 3250
3238\subsubsection*{\ff \T{seek (filehandle [, whence] [, offset])}}\Deffunc{seek} 3251\subsubsection*{\ff \T{seek (filehandle [, whence] [, offset])}}\DefLIB{seek}
3239 3252
3240Sets and gets the file position, 3253Sets and gets the file position,
3241measured in bytes from the beginning of the file, 3254measured in bytes from the beginning of the file,
@@ -3260,14 +3273,14 @@ beginning of the file (and returns 0);
3260and the call \verb|seek(file, "end")| sets the position to the 3273and the call \verb|seek(file, "end")| sets the position to the
3261end of the file, and returns its size. 3274end of the file, and returns its size.
3262 3275
3263\subsubsection*{\ff \T{tmpname ()}}\Deffunc{tmpname} 3276\subsubsection*{\ff \T{tmpname ()}}\DefLIB{tmpname}
3264 3277
3265Returns a string with a file name that can safely 3278Returns a string with a file name that can safely
3266be used for a temporary file. 3279be used for a temporary file.
3267The file must be explicitly opened before its use 3280The file must be explicitly opened before its use
3268and removed when no longer needed. 3281and removed when no longer needed.
3269 3282
3270\subsubsection*{\ff \T{read ([filehandle,] format1, ...)}}\Deffunc{read} 3283\subsubsection*{\ff \T{read ([filehandle,] format1, ...)}}\DefLIB{read}
3271 3284
3272Reads file \verb|_INPUT|, 3285Reads file \verb|_INPUT|,
3273or \verb|filehandle| if this argument is given, 3286or \verb|filehandle| if this argument is given,
@@ -3295,7 +3308,7 @@ skipping spaces if necessary, or \nil\ on end of file.
3295or \nil\ on end of file. 3308or \nil\ on end of file.
3296\end{description} 3309\end{description}
3297 3310
3298\subsubsection*{\ff \T{write ([filehandle, ] value1, ...)}}\Deffunc{write} 3311\subsubsection*{\ff \T{write ([filehandle, ] value1, ...)}}\DefLIB{write}
3299 3312
3300Writes the value of each of its arguments to 3313Writes the value of each of its arguments to
3301file \verb|_OUTPUT|, 3314file \verb|_OUTPUT|,
@@ -3308,12 +3321,12 @@ plus a string describing the error.
3308 3321
3309\subsection{System Facilities} \label{libiosys} 3322\subsection{System Facilities} \label{libiosys}
3310 3323
3311\subsubsection*{\ff \T{clock ()}}\Deffunc{clock} 3324\subsubsection*{\ff \T{clock ()}}\DefLIB{clock}
3312 3325
3313Returns an approximation of the amount of CPU time 3326Returns an approximation of the amount of CPU time
3314used by the program, in seconds. 3327used by the program, in seconds.
3315 3328
3316\subsubsection*{\ff \T{date ([format])}}\Deffunc{date} 3329\subsubsection*{\ff \T{date ([format])}}\DefLIB{date}
3317 3330
3318Returns a string containing date and time 3331Returns a string containing date and time
3319formatted according to the given string \verb|format|, 3332formatted according to the given string \verb|format|,
@@ -3322,25 +3335,25 @@ When called without arguments,
3322it returns a reasonable date and time representation that depends on 3335it returns a reasonable date and time representation that depends on
3323the host system and on the current locale. 3336the host system and on the current locale.
3324 3337
3325\subsubsection*{\ff \T{execute (command)}}\Deffunc{execute} 3338\subsubsection*{\ff \T{execute (command)}}\DefLIB{execute}
3326 3339
3327This function is equivalent to the C~function \verb|system|. 3340This function is equivalent to the C~function \verb|system|.
3328It passes \verb|command| to be executed by an operating system shell. 3341It passes \verb|command| to be executed by an operating system shell.
3329It returns a status code, which is system-dependent. 3342It returns a status code, which is system-dependent.
3330 3343
3331\subsubsection*{\ff \T{exit ([code])}}\Deffunc{exit} 3344\subsubsection*{\ff \T{exit ([code])}}\DefLIB{exit}
3332 3345
3333Calls the C~function \verb|exit|, 3346Calls the C~function \verb|exit|,
3334with an optional \verb|code|, 3347with an optional \verb|code|,
3335to terminate the program. 3348to terminate the program.
3336The default value for \verb|code| is the success code. 3349The default value for \verb|code| is the success code.
3337 3350
3338\subsubsection*{\ff \T{getenv (varname)}}\Deffunc{getenv} 3351\subsubsection*{\ff \T{getenv (varname)}}\DefLIB{getenv}
3339 3352
3340Returns the value of the process environment variable \verb|varname|, 3353Returns the value of the process environment variable \verb|varname|,
3341or \nil\ if the variable is not defined. 3354or \nil\ if the variable is not defined.
3342 3355
3343\subsubsection*{\ff \T{setlocale (locale [, category])}}\Deffunc{setlocale} 3356\subsubsection*{\ff \T{setlocale (locale [, category])}}\DefLIB{setlocale}
3344 3357
3345This function is an interface to the ANSI~C function \verb|setlocale|. 3358This function is an interface to the ANSI~C function \verb|setlocale|.
3346\verb|locale| is a string specifying a locale; 3359\verb|locale| is a string specifying a locale;
@@ -3364,7 +3377,7 @@ This interface is declared in \verb|luadebug.h|.
3364 3377
3365\subsection{Stack and Function Information} 3378\subsection{Stack and Function Information}
3366 3379
3367\Deffunc{lua_getstack} 3380\DefAPI{lua_getstack}
3368The main function to get information about the interpreter stack is 3381The main function to get information about the interpreter stack is
3369\begin{verbatim} 3382\begin{verbatim}
3370 int lua_getstack (lua_State *L, int level, lua_Debug *ar); 3383 int lua_getstack (lua_State *L, int level, lua_Debug *ar);
@@ -3378,7 +3391,7 @@ Usually, \verb|lua_getstack| returns 1;
3378when called with a level greater than the stack depth, 3391when called with a level greater than the stack depth,
3379it returns 0. 3392it returns 0.
3380 3393
3381\Deffunc{lua_Debug} 3394\DefAPI{lua_Debug}
3382The structure \verb|lua_Debug| is used to carry different pieces of 3395The structure \verb|lua_Debug| is used to carry different pieces of
3383information about an active function: 3396information about an active function:
3384\begin{verbatim} 3397\begin{verbatim}
@@ -3400,7 +3413,7 @@ information about an active function:
3400\verb|lua_getstack| fills only the private part 3413\verb|lua_getstack| fills only the private part
3401of this structure, for future use. 3414of this structure, for future use.
3402To fill in the other fields of \verb|lua_Debug| with useful information, 3415To fill in the other fields of \verb|lua_Debug| with useful information,
3403call \Deffunc{lua_getinfo} 3416call \DefAPI{lua_getinfo}
3404\begin{verbatim} 3417\begin{verbatim}
3405 int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar); 3418 int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);
3406\end{verbatim} 3419\end{verbatim}
@@ -3409,10 +3422,10 @@ This function returns 0 on error
3409Each character in the string \verb|what| 3422Each character in the string \verb|what|
3410selects some fields of \verb|ar| to be filled, 3423selects some fields of \verb|ar| to be filled,
3411as indicated by the letter in parentheses in the definition of \verb|lua_Debug|: 3424as indicated by the letter in parentheses in the definition of \verb|lua_Debug|:
3412An \verb|S| fills in the fields \verb|source|, \verb|linedefined|, 3425`\verb|S|' fills in the fields \verb|source|, \verb|linedefined|,
3413and \verb|what|; 3426and \verb|what|;
3414\verb|l| fills in the field \verb|currentline|, etc. 3427`\verb|l|' fills in the field \verb|currentline|, etc.
3415Moreover, character \verb|f| pushes onto the stack the function that is 3428Moreover, `\verb|f|' pushes onto the stack the function that is
3416running at the given level. 3429running at the given level.
3417 3430
3418To get information about a function that is not active (that is, 3431To get information about a function that is not active (that is,
@@ -3427,7 +3440,6 @@ you can write
3427 lua_getinfo(L, ">S", &ar); 3440 lua_getinfo(L, ">S", &ar);
3428 printf("%d\n", ar.linedefined); 3441 printf("%d\n", ar.linedefined);
3429\end{verbatim} 3442\end{verbatim}
3430
3431The fields of \verb|lua_Debug| have the following meaning: 3443The fields of \verb|lua_Debug| have the following meaning:
3432\begin{description} 3444\begin{description}
3433 3445
@@ -3485,10 +3497,10 @@ Number of upvalues of a function.
3485 3497
3486For the manipulation of local variables, 3498For the manipulation of local variables,
3487\verb|luadebug.h| uses indices: 3499\verb|luadebug.h| uses indices:
3488The first parameter has index 1, and so on, 3500The first parameter or local variable has index~1, and so on,
3489until the last active local variable. 3501until the last active local variable.
3490 3502
3491\Deffunc{lua_getlocal}\Deffunc{lua_setlocal} 3503\DefAPI{lua_getlocal}\DefAPI{lua_setlocal}
3492The following functions allow the manipulation of the 3504The following functions allow the manipulation of the
3493local variables of a given activation record. 3505local variables of a given activation record.
3494\begin{verbatim} 3506\begin{verbatim}
@@ -3504,12 +3516,12 @@ and returns its name.
3504For \verb|lua_setlocal|, 3516For \verb|lua_setlocal|,
3505you push the new value onto the stack, 3517you push the new value onto the stack,
3506and the function assigns that value to the variable and returns its name. 3518and the function assigns that value to the variable and returns its name.
3507Both functions return NULL on failure; 3519Both functions return \verb|NULL| on failure;
3508that happens if the index is greater than 3520that happens if the index is greater than
3509the number of active local variables. 3521the number of active local variables.
3510 3522
3511As an example, the following function lists the names of all 3523As an example, the following function lists the names of all
3512local variables for a function in a given level of the stack: 3524local variables for a function at a given level of the stack:
3513\begin{verbatim} 3525\begin{verbatim}
3514 int listvars (lua_State *L, int level) { 3526 int listvars (lua_State *L, int level) {
3515 lua_Debug ar; 3527 lua_Debug ar;
@@ -3531,11 +3543,12 @@ local variables for a function in a given level of the stack:
3531The Lua interpreter offers two hooks for debugging purposes: 3543The Lua interpreter offers two hooks for debugging purposes:
3532a \emph{call} hook and a \emph{line} hook. 3544a \emph{call} hook and a \emph{line} hook.
3533Both have the same type, 3545Both have the same type,
3546\DefAPI{lua_Hook}
3534\begin{verbatim} 3547\begin{verbatim}
3535 typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); 3548 typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
3536\end{verbatim} 3549\end{verbatim}
3537and you can set them with the following functions: 3550and you can set them with the following functions:
3538\Deffunc{lua_Hook}\Deffunc{lua_setcallhook}\Deffunc{lua_setlinehook} 3551\DefAPI{lua_setcallhook}\DefAPI{lua_setlinehook}
3539\begin{verbatim} 3552\begin{verbatim}
3540 lua_Hook lua_setcallhook (lua_State *L, lua_Hook func); 3553 lua_Hook lua_setcallhook (lua_State *L, lua_Hook func);
3541 lua_Hook lua_setlinehook (lua_State *L, lua_Hook func); 3554 lua_Hook lua_setlinehook (lua_State *L, lua_Hook func);
@@ -3550,7 +3563,7 @@ interpreter enters or leaves a function.
3550The \verb|event| field of \verb|ar| has the strings \verb|"call"| 3563The \verb|event| field of \verb|ar| has the strings \verb|"call"|
3551or \verb|"return"|. 3564or \verb|"return"|.
3552This \verb|ar| can then be used in calls to \verb|lua_getinfo|, 3565This \verb|ar| can then be used in calls to \verb|lua_getinfo|,
3553\verb|lua_getlocal|, and \verb|lua_setlocal|, 3566\verb|lua_getlocal|, and \verb|lua_setlocal|
3554to get more information about the function and to manipulate its 3567to get more information about the function and to manipulate its
3555local variables. 3568local variables.
3556 3569
@@ -3564,12 +3577,6 @@ While Lua is running a hook, it disables other calls to hooks.
3564Therefore, if a hook calls Lua to execute a function or a chunk, 3577Therefore, if a hook calls Lua to execute a function or a chunk,
3565this execution ocurrs without any calls to hooks. 3578this execution ocurrs without any calls to hooks.
3566 3579
3567A hook cannot call \T{lua_error}.
3568It must return to Lua through a regular return.
3569(There is no problem if the error is inside a chunk or a Lua function
3570called by the hook, because those errors are protected;
3571the control returns to the hook anyway.)
3572
3573 3580
3574\subsection{The Reflexive Debug Interface} 3581\subsection{The Reflexive Debug Interface}
3575 3582
@@ -3578,6 +3585,7 @@ the functionality of the debug interface to Lua programs.
3578If you want to use this library, 3585If you want to use this library,
3579your host application must open it, 3586your host application must open it,
3580by calling \verb|lua_dblibopen|. 3587by calling \verb|lua_dblibopen|.
3588\DefAPI{lua_dblibopen}
3581 3589
3582You should exert great care when using this library. 3590You should exert great care when using this library.
3583The functions provided here should be used exclusively for debugging 3591The functions provided here should be used exclusively for debugging
@@ -3590,7 +3598,7 @@ As a general rule, if your program does not need this library,
3590do not open it. 3598do not open it.
3591 3599
3592 3600
3593\subsubsection*{\ff \T{getinfo (function, [what])}}\Deffunc{getinfo} 3601\subsubsection*{\ff \T{getinfo (function, [what])}}\DefLIB{getinfo}
3594 3602
3595This function returns a table with information about a function. 3603This function returns a table with information about a function.
3596You can give the function directly, 3604You can give the function directly,
@@ -3600,7 +3608,7 @@ Level 0 is the current function (\verb|getinfo| itself);
3600level 1 is the function that called \verb|getinfo|; 3608level 1 is the function that called \verb|getinfo|;
3601and so on. 3609and so on.
3602If \verb|function| is a number larger than the number of active functions, 3610If \verb|function| is a number larger than the number of active functions,
3603\verb|getinfo| returns \nil. 3611then \verb|getinfo| returns \nil.
3604 3612
3605The returned table contains all the fields returned by \verb|lua_getinfo|, 3613The returned table contains all the fields returned by \verb|lua_getinfo|,
3606with the string \verb|what| describing what to get. 3614with the string \verb|what| describing what to get.
@@ -3612,18 +3620,18 @@ and \verb|getinfo(print)| returns a table with all available information
3612about the \verb|print| function. 3620about the \verb|print| function.
3613 3621
3614 3622
3615\subsubsection*{\ff \T{getlocal (level, local)}}\Deffunc{getlocal} 3623\subsubsection*{\ff \T{getlocal (level, local)}}\DefLIB{getlocal}
3616 3624
3617This function returns the name and the value of the local variable 3625This function returns the name and the value of the local variable
3618with index \verb|local| of the function at level \verb|level| of the stack. 3626with index \verb|local| of the function at level \verb|level| of the stack.
3619(The first parameter has index 1, and so on, 3627(The first parameter or local variable has index~1, and so on,
3620until the last active local variable.) 3628until the last active local variable.)
3621The function returns \nil\ if there is no local 3629The function returns \nil\ if there is no local
3622variable with the given index, 3630variable with the given index,
3623and raises an error when called with a \verb|level| out of range. 3631and raises an error when called with a \verb|level| out of range.
3624(You can call \verb|getstack| to check whether the level is valid.) 3632(You can call \verb|getinfo| to check whether the level is valid.)
3625 3633
3626\subsubsection*{\ff \T{setlocal (level, local, value)}}\Deffunc{setlocal} 3634\subsubsection*{\ff \T{setlocal (level, local, value)}}\DefLIB{setlocal}
3627 3635
3628This function assigns the value \verb|value| to the local variable 3636This function assigns the value \verb|value| to the local variable
3629with index \verb|local| of the function at level \verb|level| of the stack. 3637with index \verb|local| of the function at level \verb|level| of the stack.
@@ -3631,41 +3639,41 @@ The function returns \nil\ if there is no local
3631variable with the given index, 3639variable with the given index,
3632and raises an error when called with a \verb|level| out of range. 3640and raises an error when called with a \verb|level| out of range.
3633 3641
3634\subsubsection*{\ff \T{setcallhook (hook)}}\Deffunc{setcallhook} 3642\subsubsection*{\ff \T{setcallhook (hook)}}\DefLIB{setcallhook}
3635 3643
3636Sets the function \verb|hook| as the call hook; 3644Sets the function \verb|hook| as the call hook;
3637this hook will be called every time the interpreter starts and 3645this hook will be called every time the interpreter starts and
3638exits the execution of a function. 3646exits the execution of a function.
3639The only argument to this hook is the event name (\verb|"call"| or 3647The only argument to the call hook is the event name (\verb|"call"| or
3640\verb|"return"|). 3648\verb|"return"|).
3641You can call \verb|getstack| with level 2 to get more information about 3649You can call \verb|getinfo| with level 2 to get more information about
3642the function being called or returning 3650the function being called or returning
3643(level 0 is the \verb|getstack| function, 3651(level~0 is the \verb|getinfo| function,
3644and level 1 is the hook function). 3652and level~1 is the hook function).
3645
3646When called without arguments, 3653When called without arguments,
3647this function turns off call hooks. 3654this function turns off call hooks.
3655\verb|setcallhook| returns the old hook.
3648 3656
3649\subsubsection*{\ff \T{setlinehook (hook)}}\Deffunc{setlinehook} 3657\subsubsection*{\ff \T{setlinehook (hook)}}\DefLIB{setlinehook}
3650 3658
3651Sets the function \verb|hook| as the line hook; 3659Sets the function \verb|hook| as the line hook;
3652this hook will be called every time the interpreter changes 3660this hook will be called every time the interpreter changes
3653the line of code it is executing. 3661the line of code it is executing.
3654The only argument to the hook is the line number the interpreter 3662The only argument to the line hook is the line number the interpreter
3655is about to execute. 3663is about to execute.
3656
3657When called without arguments, 3664When called without arguments,
3658this function turns off line hooks. 3665this function turns off line hooks.
3666\verb|setlinehook| returns the old hook.
3659 3667
3660 3668
3661\section{\Index{Lua Stand-alone}} \label{lua-sa} 3669\section{\Index{Lua Stand-alone}} \label{lua-sa}
3662 3670
3663Although Lua has been designed as an extension language, 3671Although Lua has been designed as an extension language,
3672to be embedded in a host C~program,
3664it is frequently used as a stand-alone language. 3673it is frequently used as a stand-alone language.
3665An interpreter for Lua as a stand-alone language, 3674An interpreter for Lua as a stand-alone language,
3666called simply \verb|lua|, 3675called simply \verb|lua|,
3667is provided with the standard distribution. 3676is provided with the standard distribution.
3668
3669This program can be called with any sequence of the following arguments: 3677This program can be called with any sequence of the following arguments:
3670\begin{description}\leftskip=20pt 3678\begin{description}\leftskip=20pt
3671\item[\T{-sNUM}] sets the stack size to \T{NUM} 3679\item[\T{-sNUM}] sets the stack size to \T{NUM}
@@ -3682,7 +3690,7 @@ remaining arguments in table \verb|arg|;
3682\item[\T{filename}] executes file \verb|filename|. 3690\item[\T{filename}] executes file \verb|filename|.
3683\end{description} 3691\end{description}
3684When called without arguments, 3692When called without arguments,
3685Lua behaves as \verb|lua -v -i| when \verb|stdin| is a terminal, 3693\verb|lua| behaves as \verb|lua -v -i| when \verb|stdin| is a terminal,
3686and as \verb|lua -| otherwise. 3694and as \verb|lua -| otherwise.
3687 3695
3688All arguments are handled in order, except \verb|-c|. 3696All arguments are handled in order, except \verb|-c|.
@@ -3698,29 +3706,28 @@ and finally will run the file \verb|prog.lua|.
3698 3706
3699When the option \T{-f filename} is used, 3707When the option \T{-f filename} is used,
3700all remaining arguments in the command line 3708all remaining arguments in the command line
3701are passed to the Lua program in a table called \verb|arg|. 3709are passed to the Lua program \verb|filename| in a table called \verb|arg|.
3702In this table, 3710In this table,
3703the field \verb|n| gets the index of the last argument, 3711the field \verb|n| gets the index of the last argument,
3704and the field 0 gets the \T{filename}. 3712and the field 0 gets \verb|"filename"|.
3705For instance, in the call 3713For instance, in the call
3706\begin{verbatim} 3714\begin{verbatim}
3707 $ lua a.lua -f b.lua t1 t3 3715 $ lua a.lua -f b.lua t1 t3
3708\end{verbatim} 3716\end{verbatim}
3709the interpreter first runs the file \T{a.lua}, 3717the interpreter first runs the file \T{a.lua},
3710then creates a table \T{arg}, 3718then creates a table
3711\begin{verbatim} 3719\begin{verbatim}
3712 arg = {"t1", "t3"; n = 2, [0] = "b.lua"} 3720 arg = {"t1", "t3"; n = 2, [0] = "b.lua"}
3713\end{verbatim} 3721\end{verbatim}
3714and then runs the file \T{b.lua}. 3722and finally runs the file \T{b.lua}.
3715\Deffunc{getargs} 3723\DefLIB{getargs}
3716The stand-alone interpreter also provides a \verb|getargs| function that 3724The stand-alone interpreter also provides a \verb|getargs| function that
3717can be used to access \emph{all} command line arguments. 3725can be used to access \emph{all} command line arguments.
3718For instance, if you call Lua with the line 3726For instance, if you call Lua with the line
3719\begin{verbatim} 3727\begin{verbatim}
3720 $ lua -c a b 3728 $ lua -c a b
3721\end{verbatim} 3729\end{verbatim}
3722and the file \verb|a| (or \verb|b|) calls \verb|getargs|, 3730then a call to \verb|getargs| in \verb|a| or \verb|b| will return the table
3723the call will return the table
3724\begin{verbatim} 3731\begin{verbatim}
3725 {[0] = "lua", [1] = "-c", [2] = "a", [3] = "b", n = 3} 3732 {[0] = "lua", [1] = "-c", [2] = "a", [3] = "b", n = 3}
3726\end{verbatim} 3733\end{verbatim}
@@ -3728,12 +3735,13 @@ the call will return the table
3728In interactive mode, 3735In interactive mode,
3729a multi-line statement can be written finishing intermediate 3736a multi-line statement can be written finishing intermediate
3730lines with a backslash (`\verb|\|'). 3737lines with a backslash (`\verb|\|').
3731If the global variable \verb|_PROMPT| is defined as a string, 3738If the global variable \IndexVerb{_PROMPT} is defined as a string,
3732then its value is used as the prompt. \index{_PROMPT} 3739then its value is used as the prompt.
3733Therefore, the prompt can be changed directly on the command line: 3740Therefore, the prompt can be changed directly on the command line:
3734\begin{verbatim} 3741\begin{verbatim}
3735 $ lua _PROMPT='myprompt> ' -i 3742 $ lua _PROMPT='myprompt> ' -i
3736\end{verbatim} 3743\end{verbatim}
3744or in any Lua programs by assigning to \verb|_PROMPT|.
3737 3745
3738In Unix systems, Lua scripts can be made into executable programs 3746In Unix systems, Lua scripts can be made into executable programs
3739by using \verb|chmod +x| and the~\verb|#!| form, 3747by using \verb|chmod +x| and the~\verb|#!| form,
@@ -3759,7 +3767,7 @@ Lua means ``moon'' in Portuguese.
3759Lua 4.0 is a major revision of the language. 3767Lua 4.0 is a major revision of the language.
3760We took a great care to avoid incompatibilities with 3768We took a great care to avoid incompatibilities with
3761the previous public versions of Lua, 3769the previous public versions of Lua,
3762some differences had to be introduced. 3770but some differences had to be introduced.
3763Here is a list of all these incompatibilities. 3771Here is a list of all these incompatibilities.
3764 3772
3765 3773
@@ -3772,16 +3780,16 @@ Here is a list of all these incompatibilities.
3772All pragmas (\verb|$debug|, \verb|$if|, \ldots) have been removed. 3780All pragmas (\verb|$debug|, \verb|$if|, \ldots) have been removed.
3773 3781
3774\item 3782\item
3775\rwd{for}, \rwd{break}, and \rwd{in} now are reserved words. 3783\rwd{for}, \rwd{break}, and \rwd{in} are now reserved words.
3776 3784
3777\item 3785\item
3778Garbage-collection tag methods for tables is now deprecated. 3786Garbage-collection tag methods for tables is now obsolete.
3779 3787
3780\item 3788\item
3781There is now only one tag method for order operators. 3789There is now only one tag method for order operators.
3782 3790
3783\item 3791\item
3784In nested function calls like \verb|f(g(x))| 3792In nested function calls like \verb|f(g(x))|,
3785\emph{all} return values from \verb|g| are passed as arguments to \verb|f|. 3793\emph{all} return values from \verb|g| are passed as arguments to \verb|f|.
3786This only happens when \verb|g| is the last 3794This only happens when \verb|g| is the last
3787or the only argument to \verb|f|. 3795or the only argument to \verb|f|.
@@ -3805,7 +3813,7 @@ When traversing a table with \verb|next| or \verb|foreach|,
3805the table cannot be modified in any way. 3813the table cannot be modified in any way.
3806 3814
3807\item 3815\item
3808General read patterns are now deprecated. 3816General read patterns are now obsolete.
3809 3817
3810\item 3818\item
3811The functions \verb|rawgettable| and \verb|rawsettable| 3819The functions \verb|rawgettable| and \verb|rawsettable|
@@ -3813,7 +3821,7 @@ have been renamed to \verb|rawget| and \verb|rawset|.
3813 3821
3814\item 3822\item
3815The functions \verb|foreachvar|, \verb|nextvar|, 3823The functions \verb|foreachvar|, \verb|nextvar|,
3816\verb|rawsetglobal|, and \verb|rawgetglobal| are deprecated. 3824\verb|rawsetglobal|, and \verb|rawgetglobal| are obsolete.
3817You can get their functionality using table operations 3825You can get their functionality using table operations
3818over the table of globals, 3826over the table of globals,
3819which is returned by \verb|globals|. 3827which is returned by \verb|globals|.
@@ -3823,7 +3831,7 @@ which is returned by \verb|globals|.
3823\verb|type| no longer returns a second value. 3831\verb|type| no longer returns a second value.
3824 3832
3825\item 3833\item
3826The \verb|p| option in function \verb|call| is deprecated. 3834The \verb|p| option in function \verb|call| is now obsolete.
3827 3835
3828\end{itemize} 3836\end{itemize}
3829 3837
@@ -3838,23 +3846,6 @@ It is now fully reentrant and much clearer.
3838\item 3846\item
3839The debug API has been completely rewritten. 3847The debug API has been completely rewritten.
3840 3848
3841%\item
3842%A \verb|const| qualifier has been added to \verb|char*|
3843%in all API functions that handle C~strings.
3844%
3845%\item
3846%Some variables of type \verb|long| were changed to \verb|size_t|.
3847%
3848%\item
3849%\verb|luaL_openlib| no longer automatically calls \verb|lua_open|.
3850%So,
3851%you must now explicitly call \verb|lua_open| before opening
3852%the standard libraries.
3853%
3854%\item
3855%\verb|lua_type| now returns a string describing the type,
3856%and is no longer a synonym for \verb|lua_tag|.
3857
3858\end{itemize} 3849\end{itemize}
3859 3850
3860%{=============================================================== 3851%{===============================================================
@@ -3865,6 +3856,12 @@ The debug API has been completely rewritten.
3865\renewenvironment{Produc}{\vspace{0.8ex}\par\noindent\hspace{3ex}\it\begin{tabular}{rrl}}{\end{tabular}\vspace{0.8ex}\par\noindent} 3856\renewenvironment{Produc}{\vspace{0.8ex}\par\noindent\hspace{3ex}\it\begin{tabular}{rrl}}{\end{tabular}\vspace{0.8ex}\par\noindent}
3866 3857
3867\renewcommand{\OrNL}{\\ & \Or & } 3858\renewcommand{\OrNL}{\\ & \Or & }
3859%\newcommand{\Nter}[1]{{\rm{\tt#1}}}
3860\newcommand{\Nter}[1]{#1}
3861
3862\index{grammar}
3863
3864
3868 3865
3869\begin{Produc} 3866\begin{Produc}
3870 3867
@@ -3981,5 +3978,4 @@ The debug API has been completely rewritten.
3981\addcontentsline{toc}{section}{Index} 3978\addcontentsline{toc}{section}{Index}
3982\input{manual.id} 3979\input{manual.id}
3983 3980
3984
3985\end{document} 3981\end{document}