summaryrefslogtreecommitdiff
path: root/manual.tex
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-04-07 13:40:04 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-04-07 13:40:04 -0300
commit4eb67aa7107e495bc756f37b7dcc936ee113afd7 (patch)
tree584b84c6b1d829d2dd5bb35149fc1a1239ad10dc /manual.tex
parent0133610315f42f9ec35acacd028f31e4a95a16b5 (diff)
downloadlua-4eb67aa7107e495bc756f37b7dcc936ee113afd7.tar.gz
lua-4eb67aa7107e495bc756f37b7dcc936ee113afd7.tar.bz2
lua-4eb67aa7107e495bc756f37b7dcc936ee113afd7.zip
changes from lhf.
Diffstat (limited to 'manual.tex')
-rw-r--r--manual.tex236
1 files changed, 120 insertions, 116 deletions
diff --git a/manual.tex b/manual.tex
index 90fa12a2..a291ea3e 100644
--- a/manual.tex
+++ b/manual.tex
@@ -1,4 +1,4 @@
1% $Id: manual.tex,v 1.27 1999/03/11 19:00:12 roberto Exp roberto $ 1% $Id: manual.tex,v 1.28 1999/03/29 14:21:37 roberto Exp roberto $
2 2
3\documentclass[11pt]{article} 3\documentclass[11pt]{article}
4\usepackage{fullpage,bnf} 4\usepackage{fullpage,bnf}
@@ -21,7 +21,7 @@
21 21
22\newcommand{\ff}{$\bullet$\ } 22\newcommand{\ff}{$\bullet$\ }
23 23
24\newcommand{\Version}{3.2 (alpha)} 24\newcommand{\Version}{3.2 (beta)}
25 25
26\makeindex 26\makeindex
27 27
@@ -41,7 +41,7 @@ Waldemar Celes
41\tecgraf\ --- Computer Science Department --- PUC-Rio 41\tecgraf\ --- Computer Science Department --- PUC-Rio
42} 42}
43 43
44%\date{\small \verb$Date: 1999/03/11 19:00:12 $} 44\date{{\small \tt\$Date: 1999/03/29 14:21:37 $ $}}
45 45
46\maketitle 46\maketitle
47 47
@@ -89,6 +89,7 @@ a intera\c{c}\~ao entre programas Lua e programas C hospedeiros.
89 89
90\newpage 90\newpage
91\begin{quotation} 91\begin{quotation}
92\parskip=10pt
92\noindent 93\noindent
93\footnotesize 94\footnotesize
94Copyright \copyright\ 1994--1999 TeCGraf, PUC-Rio. All rights reserved. 95Copyright \copyright\ 1994--1999 TeCGraf, PUC-Rio. All rights reserved.
@@ -144,10 +145,6 @@ general procedural programming with data description
144facilities. 145facilities.
145Lua is intended to be used as a light-weight, but powerful, 146Lua is intended to be used as a light-weight, but powerful,
146configuration language for any program that needs one. 147configuration language for any program that needs one.
147Lua has been designed and implemented by
148W.~Celes,
149R.~Ierusalimschy and
150L.~H.~de Figueiredo.
151 148
152Lua is implemented as a library, written in C. 149Lua is implemented as a library, written in C.
153Being an extension language, Lua has no notion of a ``main'' program: 150Being an extension language, Lua has no notion of a ``main'' program:
@@ -175,15 +172,17 @@ at the following URL's:
175 172
176All statements in Lua are executed in a \Def{global environment}. 173All statements in Lua are executed in a \Def{global environment}.
177This environment, which keeps all global variables, 174This environment, which keeps all global variables,
178is initialized at the beginning of the embedding program and 175is initialized with a call from the embedding program to
179persists until its end. 176\verb|lua_open| and
177persists until a call to \verb|lua_close|,
178or the end of the embedding program.
180Optionally, a user can create multiple independent global 179Optionally, a user can create multiple independent global
181environments \see{mangstate}. 180environments \see{mangstate}.
182 181
183The global environment can be manipulated by Lua code or 182The global environment can be manipulated by Lua code or
184by the embedding program, 183by the embedding program,
185which can read and write global variables 184which can read and write global variables
186using functions from the API library that implements Lua. 185using API functions from the library that implements Lua.
187 186
188\Index{Global variables} do not need declaration. 187\Index{Global variables} do not need declaration.
189Any variable is assumed to be global unless explicitly declared local 188Any variable is assumed to be global unless explicitly declared local
@@ -206,7 +205,7 @@ A chunk may optionally end with a \verb|return| statement \see{return}.
206When a chunk is executed, first all its code is pre-compiled, 205When a chunk is executed, first all its code is pre-compiled,
207then the statements are executed in sequential order. 206then the statements are executed in sequential order.
208All modifications a chunk effects on the global environment persist 207All modifications a chunk effects on the global environment persist
209after its end. 208after the chunk end.
210 209
211Chunks may also be pre-compiled into binary form; 210Chunks may also be pre-compiled into binary form;
212see program \IndexVerb{luac} for details. 211see program \IndexVerb{luac} for details.
@@ -227,7 +226,7 @@ There are six \Index{basic types} in Lua: \Def{nil}, \Def{number},
227\Def{string}, \Def{function}, \Def{userdata}, and \Def{table}. 226\Def{string}, \Def{function}, \Def{userdata}, and \Def{table}.
228\emph{Nil} is the type of the value \nil, 227\emph{Nil} is the type of the value \nil,
229whose main property is to be different from any other value. 228whose main property is to be different from any other value.
230\emph{Number} represents real (double precision floating point) numbers, 229\emph{Number} represents real (double-precision floating-point) numbers,
231while \emph{string} has the usual meaning. 230while \emph{string} has the usual meaning.
232Lua is \Index{eight-bit clean}, 231Lua is \Index{eight-bit clean},
233and so strings may contain any 8-bit character, 232and so strings may contain any 8-bit character,
@@ -283,7 +282,7 @@ semantics of Lua \see{tag-method}.
283Each of the types \M{nil}, \M{number} and \M{string} has a different tag. 282Each of the types \M{nil}, \M{number} and \M{string} has a different tag.
284All values of each of these types have this same pre-defined tag. 283All values of each of these types have this same pre-defined tag.
285Values of type \M{function} can have two different tags, 284Values of type \M{function} can have two different tags,
286depending on whether they are Lua or C functions. 285depending on whether they are Lua functions or C functions.
287Finally, 286Finally,
288values of type \M{userdata} and \M{table} can have 287values of type \M{userdata} and \M{table} can have
289as many different tags as needed \see{tag-method}. 288as many different tags as needed \see{tag-method}.
@@ -318,7 +317,7 @@ Lua is a case-sensitive language:
318\T{and} is a reserved word, but \T{And} and \T{\'and} 317\T{and} is a reserved word, but \T{And} and \T{\'and}
319(if the locale permits) are two other different identifiers. 318(if the locale permits) are two other different identifiers.
320As a convention, identifiers starting with underscore followed by 319As a convention, identifiers starting with underscore followed by
321uppercase letters should not be used in regular programs. 320uppercase letters are reserved for internal variables.
322 321
323The following strings denote other \Index{tokens}: 322The following strings denote other \Index{tokens}:
324\begin{verbatim} 323\begin{verbatim}
@@ -329,7 +328,7 @@ The following strings denote other \Index{tokens}:
329\Index{Literal strings} can be delimited by matching single or double quotes, 328\Index{Literal strings} can be delimited by matching single or double quotes,
330and can contain the C-like escape sequences 329and can contain the C-like escape sequences
331\verb|'\a'| (bell), 330\verb|'\a'| (bell),
332\verb|'\b'| (back space), 331\verb|'\b'| (backspace),
333\verb|'\f'| (form feed), 332\verb|'\f'| (form feed),
334\verb|'\n'| (new line), 333\verb|'\n'| (new line),
335\verb|'\r'| (carriage return), 334\verb|'\r'| (carriage return),
@@ -369,9 +368,9 @@ in Unix systems \see{lua-sa}.
369 368
370\Index{Numerical constants} may be written with an optional decimal part, 369\Index{Numerical constants} may be written with an optional decimal part,
371and an optional decimal exponent. 370and an optional decimal exponent.
372Examples of valid numerical constants are: 371Examples of valid numerical constants are
373\begin{verbatim} 372\begin{verbatim}
374 4 4.0 0.4 4.57e-3 0.3e12 373 3 3.0 3.1416 314.16e-2 0.31416E1
375\end{verbatim} 374\end{verbatim}
376 375
377\subsection{The \Index{Pre-processor}} \label{pre-processor} 376\subsection{The \Index{Pre-processor}} \label{pre-processor}
@@ -396,7 +395,7 @@ Directives may be freely nested.
396Particularly, a \verb|$endinput| may occur inside a \verb|$if|; 395Particularly, a \verb|$endinput| may occur inside a \verb|$if|;
397in that case, even the matching \verb|$end| is not parsed. 396in that case, even the matching \verb|$end| is not parsed.
398 397
399A \M{cond} part may be: 398A \M{cond} part may be
400\begin{description} 399\begin{description}
401\item[\T{nil}] --- always false. 400\item[\T{nil}] --- always false.
402\item[\T{1}] --- always true. 401\item[\T{1}] --- always true.
@@ -421,12 +420,13 @@ use the \verb|format| function \see{format}.
421 420
422Functions in Lua can return many values. 421Functions in Lua can return many values.
423Because there are no type declarations, 422Because there are no type declarations,
423when a function is called
424the system does not know how many values a function will return, 424the system does not know how many values a function will return,
425or how many parameters it needs. 425or how many parameters it needs.
426Therefore, sometimes, a list of values must be \emph{adjusted}, at run time, 426Therefore, sometimes, a list of values must be \emph{adjusted}, at run time,
427to a given length. 427to a given length.
428If there are more values than are needed, 428If there are more values than are needed,
429then the last values are thrown away. 429then the excess values are thrown away.
430If there are more needs than values, 430If there are more needs than values,
431then the list is extended with as many \nil's as needed. 431then the list is extended with as many \nil's as needed.
432Adjustment occurs in multiple assignment \see{assignment} 432Adjustment occurs in multiple assignment \see{assignment}
@@ -481,7 +481,7 @@ The two lists may have different lengths.
481Before the assignment, the list of values is \emph{adjusted} to 481Before the assignment, the list of values is \emph{adjusted} to
482the length of the list of variables \see{adjust}. 482the length of the list of variables \see{adjust}.
483 483
484A single name can denote a global or a local variable, 484A single name can denote a global variable, a local variable,
485or a formal parameter: 485or a formal parameter:
486\begin{Produc} 486\begin{Produc}
487\produc{var}{name} 487\produc{var}{name}
@@ -529,7 +529,7 @@ only \nil\ is considered false.
529A \T{return} is used to return values from a function or from a chunk. 529A \T{return} is used to return values from a function or from a chunk.
530\label{return} 530\label{return}
531Because they may return more than one value, 531Because they may return more than one value,
532the syntax for a \Index{return statement} is: 532the syntax for a \Index{return statement} is
533\begin{Produc} 533\begin{Produc}
534\produc{ret}{\rwd{return} \opt{explist1} \opt{sc}} 534\produc{ret}{\rwd{return} \opt{explist1} \opt{sc}}
535\end{Produc} 535\end{Produc}
@@ -561,7 +561,7 @@ Otherwise, all variables are initialized with \nil.
561\subsection{\Index{Expressions}} 561\subsection{\Index{Expressions}}
562 562
563\subsubsection{\Index{Basic Expressions}} 563\subsubsection{\Index{Basic Expressions}}
564Basic expressions are: 564Basic expressions are
565\begin{Produc} 565\begin{Produc}
566\produc{exp}{\ter{(} exp \ter{)}} 566\produc{exp}{\ter{(} exp \ter{)}}
567\produc{exp}{\rwd{nil}} 567\produc{exp}{\rwd{nil}}
@@ -581,7 +581,7 @@ string literals are explained in \See{lexical};
581variables are explained in \See{assignment}; 581variables are explained in \See{assignment};
582upvalues are explained in \See{upvalue}; 582upvalues are explained in \See{upvalue};
583function definitions (\M{function}) are explained in \See{func-def}; 583function definitions (\M{function}) are explained in \See{func-def};
584function call are explained in \See{functioncall}. 584function calls are explained in \See{functioncall}.
585 585
586An access to a global variable \verb|x| is equivalent to a 586An access to a global variable \verb|x| is equivalent to a
587call \verb|getglobal('x')|; 587call \verb|getglobal('x')|;
@@ -639,7 +639,7 @@ then their values are compared using lexicographical order.
639Otherwise, the ``order'' tag method is called \see{tag-method}. 639Otherwise, the ``order'' tag method is called \see{tag-method}.
640 640
641\subsubsection{Logical Operators} 641\subsubsection{Logical Operators}
642The \Index{logical operators} are: 642The \Index{logical operators} are
643\index{and}\index{or}\index{not} 643\index{and}\index{or}\index{not}
644\begin{verbatim} 644\begin{verbatim}
645 and or not 645 and or not
@@ -692,7 +692,7 @@ every time a constructor is evaluated, a new table is created.
692Constructors can be used to create empty tables, 692Constructors can be used to create empty tables,
693or to create a table and initialize some fields. 693or to create a table and initialize some fields.
694 694
695The general syntax for constructors is: 695The general syntax for constructors is
696\begin{Produc} 696\begin{Produc}
697\produc{tableconstructor}{\ter{\{} fieldlist \ter{\}}} 697\produc{tableconstructor}{\ter{\{} fieldlist \ter{\}}}
698\produc{fieldlist}{lfieldlist \Or ffieldlist \Or lfieldlist \ter{;} ffieldlist 698\produc{fieldlist}{lfieldlist \Or ffieldlist \Or lfieldlist \ter{;} ffieldlist
@@ -701,17 +701,17 @@ The general syntax for constructors is:
701\produc{ffieldlist}{\opt{ffieldlist1}} 701\produc{ffieldlist}{\opt{ffieldlist1}}
702\end{Produc} 702\end{Produc}
703 703
704The form \emph{lfieldlist1} is used to initialize lists. 704The form \emph{lfieldlist1} is used to initialize lists:
705\begin{Produc} 705\begin{Produc}
706\produc{lfieldlist1}{exp \rep{\ter{,} exp} \opt{\ter{,}}} 706\produc{lfieldlist1}{exp \rep{\ter{,} exp} \opt{\ter{,}}}
707\end{Produc}% 707\end{Produc}%
708The expressions in the list are assigned to consecutive numerical indices, 708The expressions in the list are assigned to consecutive numerical indices,
709starting with 1. 709starting with 1.
710For example: 710For example,
711\begin{verbatim} 711\begin{verbatim}
712 a = {"v1", "v2", 34} 712 a = {"v1", "v2", 34}
713\end{verbatim} 713\end{verbatim}
714is equivalent to: 714is equivalent to
715\begin{verbatim} 715\begin{verbatim}
716 do 716 do
717 local temp = {} 717 local temp = {}
@@ -727,11 +727,11 @@ The form \emph{ffieldlist1} initializes other fields in a table:
727\produc{ffieldlist1}{ffield \rep{\ter{,} ffield} \opt{\ter{,}}} 727\produc{ffieldlist1}{ffield \rep{\ter{,} ffield} \opt{\ter{,}}}
728\produc{ffield}{\ter{[} exp \ter{]} \ter{=} exp \Or name \ter{=} exp} 728\produc{ffield}{\ter{[} exp \ter{]} \ter{=} exp \Or name \ter{=} exp}
729\end{Produc}% 729\end{Produc}%
730For example: 730For example,
731\begin{verbatim} 731\begin{verbatim}
732 a = {[f(k)] = g(y), x = 1, y = 3, [0] = b+c} 732 a = {[f(k)] = g(y), x = 1, y = 3, [0] = b+c}
733\end{verbatim} 733\end{verbatim}
734is equivalent to: 734is equivalent to
735\begin{verbatim} 735\begin{verbatim}
736 do 736 do
737 local temp = {} 737 local temp = {}
@@ -753,7 +753,7 @@ For example, all forms below are correct:
753 x = {;} 753 x = {;}
754 x = {'a', 'b',} 754 x = {'a', 'b',}
755 x = {type='list'; 'a', 'b'} 755 x = {type='list'; 'a', 'b'}
756 x = {f(0), f(1), f(2),; n=3} 756 x = {f(0), f(1), f(2),; n=3,}
757\end{verbatim} 757\end{verbatim}
758 758
759\subsubsection{Function Calls} \label{functioncall} 759\subsubsection{Function Calls} \label{functioncall}
@@ -781,6 +781,7 @@ is syntactic sugar for
781\end{verbatim} 781\end{verbatim}
782except that \verb|simpleexp| is evaluated only once. 782except that \verb|simpleexp| is evaluated only once.
783 783
784Arguments have the following syntax:
784\begin{Produc} 785\begin{Produc}
785\produc{args}{\ter{(} \opt{explist1} \ter{)}} 786\produc{args}{\ter{(} \opt{explist1} \ter{)}}
786\produc{args}{tableconstructor} 787\produc{args}{tableconstructor}
@@ -813,17 +814,17 @@ Note that the only place that can hold many values
813is the last (or the only) expression in an assignment 814is the last (or the only) expression in an assignment
814or in a return statement; see examples below. 815or in a return statement; see examples below.
815\begin{verbatim} 816\begin{verbatim}
816 f(); -- adjusted to 0 817 f(); -- adjusted to 0
817 g(x, f()); -- f() is adjusted to 1 818 g(x, f()); -- f() is adjusted to 1
818 a,b,c = f(), x; -- f() is adjusted to 1 result (and c gets nil) 819 a,b,c = f(), x; -- f() is adjusted to 1 result (and c gets nil)
819 a,b,c = x, f(); -- f() is adjusted to 2 820 a,b,c = x, f(); -- f() is adjusted to 2
820 a,b,c = f(); -- f() is adjusted to 3 821 a,b,c = f(); -- f() is adjusted to 3
821 return f(); -- returns all values returned by f() 822 return f(); -- returns all values returned by f()
822\end{verbatim} 823\end{verbatim}
823 824
824\subsubsection{\Index{Function Definitions}} \label{func-def} 825\subsubsection{\Index{Function Definitions}} \label{func-def}
825 826
826The syntax for function definition is: 827The syntax for function definition is
827\begin{Produc} 828\begin{Produc}
828\produc{function}{\rwd{function} \ter{(} \opt{parlist1} \ter{)} 829\produc{function}{\rwd{function} \ter{(} \opt{parlist1} \ter{)}
829 block \rwd{end}} 830 block \rwd{end}}
@@ -831,13 +832,13 @@ The syntax for function definition is:
831 block \rwd{end}} 832 block \rwd{end}}
832\produc{funcname}{name \Or name \ter{.} name} 833\produc{funcname}{name \Or name \ter{.} name}
833\end{Produc} 834\end{Produc}
834The statement: 835The statement
835\begin{verbatim} 836\begin{verbatim}
836 function f (...) 837 function f (...)
837 ... 838 ...
838 end 839 end
839\end{verbatim} 840\end{verbatim}
840is just syntactic sugar for: 841is just syntactic sugar for
841\begin{verbatim} 842\begin{verbatim}
842 f = function (...) 843 f = function (...)
843 ... 844 ...
@@ -872,8 +873,8 @@ A vararg function does not adjust its argument list;
872instead, it collects any extra arguments into an implicit parameter, 873instead, it collects any extra arguments into an implicit parameter,
873called \IndexVerb{arg}. 874called \IndexVerb{arg}.
874This parameter is always initialized as a table, 875This parameter is always initialized as a table,
875with a field \verb|n| with the number of extra arguments, 876with a field \verb|n| whose value is the number of extra arguments,
876and the extra arguments at positions 1, 2, \ldots 877and the extra arguments at positions 1,~2,~\ldots
877 878
878As an example, suppose definitions like: 879As an example, suppose definitions like:
879\begin{verbatim} 880\begin{verbatim}
@@ -898,22 +899,22 @@ If control reaches the end of a function without a return instruction,
898then the function returns with no results. 899then the function returns with no results.
899 900
900There is a special syntax for defining \Index{methods}, 901There is a special syntax for defining \Index{methods},
901that is, functions that have an implicit extra parameter \IndexVerb{self}. 902that is, functions that have an implicit extra parameter \IndexVerb{self}:
902\begin{Produc} 903\begin{Produc}
903\produc{function}{\rwd{function} name \ter{:} name \ter{(} \opt{parlist1} 904\produc{function}{\rwd{function} name \ter{:} name \ter{(} \opt{parlist1}
904 \ter{)} block \rwd{end}} 905 \ter{)} block \rwd{end}}
905\end{Produc}% 906\end{Produc}%
906Thus, a declaration like 907Thus, a declaration like
907\begin{verbatim} 908\begin{verbatim}
908function v:f (...) 909 function v:f (...)
909 ... 910 ...
910end 911 end
911\end{verbatim} 912\end{verbatim}
912is equivalent to 913is equivalent to
913\begin{verbatim} 914\begin{verbatim}
914v.f = function (self, ...) 915 v.f = function (self, ...)
915 ... 916 ...
916end 917 end
917\end{verbatim} 918\end{verbatim}
918that is, the function gets an extra formal parameter called \verb|self|. 919that is, the function gets an extra formal parameter called \verb|self|.
919Note that the variable \verb|v| must have been 920Note that the variable \verb|v| must have been
@@ -944,26 +945,26 @@ at the point where the function is defined.
944 945
945Here are some examples: 946Here are some examples:
946\begin{verbatim} 947\begin{verbatim}
947a,b,c = 1,2,3 -- global variables 948 a,b,c = 1,2,3 -- global variables
948function f (x) 949 function f (x)
949 local b -- x and b are local to f 950 local b -- x and b are local to f
950 local g = function (a) 951 local g = function (a)
951 local y -- a and y are local to g 952 local y -- a and y are local to g
952 p = a -- OK, access local 'a' 953 p = a -- OK, access local 'a'
953 p = c -- OK, access global 'c' 954 p = c -- OK, access global 'c'
954 p = b -- ERROR: cannot access a variable in outer scope 955 p = b -- ERROR: cannot access a variable in outer scope
955 p = %b -- OK, access frozen value of 'b' (local to 'f') 956 p = %b -- OK, access frozen value of 'b' (local to 'f')
956 p = %c -- OK, access frozen value of global 'c' 957 p = %c -- OK, access frozen value of global 'c'
957 p = %y -- ERROR: 'y' is not visible where 'g' is defined 958 p = %y -- ERROR: 'y' is not visible where 'g' is defined
958 end -- g 959 end -- g
959end -- f 960 end -- f
960\end{verbatim} 961\end{verbatim}
961 962
962 963
963\subsection{Tag Methods} \label{tag-method} 964\subsection{Tag Methods} \label{tag-method}
964 965
965Lua provides a powerful mechanism to extend its semantics, 966Lua provides a powerful mechanism to extend its semantics,
966called \Def{Tag Methods}. 967called \Def{tag methods}.
967A tag method is a programmer-defined function 968A tag method is a programmer-defined function
968that is called at specific key points during the evaluation of a program, 969that is called at specific key points during the evaluation of a program,
969allowing the programmer to change the standard Lua behavior at these points. 970allowing the programmer to change the standard Lua behavior at these points.
@@ -974,10 +975,10 @@ according to the tag of the values involved
974in the event \see{TypesSec}. 975in the event \see{TypesSec}.
975The function \IndexVerb{settagmethod} changes the tag method 976The function \IndexVerb{settagmethod} changes the tag method
976associated with a given pair \M{(tag, event)}. 977associated with a given pair \M{(tag, event)}.
977Its first parameter is the tag, the second is the event name 978Its first parameter is the tag, the second parameter is the event name
978(a string, see below), 979(a string; see below),
979and the third parameter is the new method (a function), 980and the third parameter is the new method (a function),
980or \nil\ to restore the default behavior. 981or \nil\ to restore the default behavior for the pair.
981The function returns the previous tag method for that pair. 982The function returns the previous tag method for that pair.
982Another function, \IndexVerb{gettagmethod}, 983Another function, \IndexVerb{gettagmethod},
983receives a tag and an event name and returns the 984receives a tag and an event name and returns the
@@ -993,7 +994,7 @@ Please notice that the code shown here is only illustrative;
993the real behavior is hard coded in the interpreter, 994the real behavior is hard coded in the interpreter,
994and it is much more efficient than this simulation. 995and it is much more efficient than this simulation.
995All functions used in these descriptions 996All functions used in these descriptions
996(\verb|rawgetglobal|, \verb|tonumber|, \verb|call|, etc) 997(\verb|rawgetglobal|, \verb|tonumber|, \verb|call|, etc.)
997are described in \See{predefined}. 998are described in \See{predefined}.
998 999
999\begin{description} 1000\begin{description}
@@ -1237,7 +1238,7 @@ called when Lua tries to call a non function value.
1237\end{verbatim} 1238\end{verbatim}
1238 1239
1239\item[``gc'':]\index{gc event} 1240\item[``gc'':]\index{gc event}
1240called when Lua is garbage collecting an object. 1241called when Lua is ``garbage collecting'' an object.
1241This method cannot be set for strings, numbers, functions, 1242This method cannot be set for strings, numbers, functions,
1242and userdata with default tag. 1243and userdata with default tag.
1243For each object to be collected, 1244For each object to be collected,
@@ -1282,7 +1283,7 @@ such as the call stack.
1282To provide more information about errors, 1283To provide more information about errors,
1283Lua programs should include the compilation pragma \verb|$debug|. 1284Lua programs should include the compilation pragma \verb|$debug|.
1284\index{debug pragma}\label{pragma} 1285\index{debug pragma}\label{pragma}
1285When an error occurs in a program compiled with this option, 1286When an error occurs in a chunk compiled with this option,
1286the I/O error routine is able to print the number of the 1287the I/O error routine is able to print the number of the
1287lines where the calls (and the error) were made. 1288lines where the calls (and the error) were made.
1288 1289
@@ -1319,6 +1320,8 @@ is stored in a dynamic structure pointed by\Deffunc{lua_state}
1319typedef struct lua_State lua_State; 1320typedef struct lua_State lua_State;
1320extern lua_State *lua_state; 1321extern lua_State *lua_state;
1321\end{verbatim} 1322\end{verbatim}
1323The variable \verb|lua_state| is the only C global variable in
1324the Lua library.
1322 1325
1323Before calling any API function, 1326Before calling any API function,
1324this state must be initialized. 1327this state must be initialized.
@@ -1353,11 +1356,11 @@ An easy way to do that is defining an auxiliary function:
1353\end{verbatim} 1356\end{verbatim}
1354This function creates a new state without changing the current state 1357This function creates a new state without changing the current state
1355of the interpreter. 1358of the interpreter.
1356Note that any new state is built with all predefined functions, 1359Note that any new state is created with all predefined functions,
1357but any additional library (such as the standard libraries) must be 1360but any additional library (such as the standard libraries) must be
1358explicitly open in the new state, if needed. 1361explicitly open in the new state, if needed.
1359 1362
1360If necessary, a state may be released:\Deffunc{lua_close} 1363If necessary, a state may be released by calling\Deffunc{lua_close}
1361\begin{verbatim} 1364\begin{verbatim}
1362void lua_close (void); 1365void lua_close (void);
1363\end{verbatim} 1366\end{verbatim}
@@ -1404,7 +1407,7 @@ int lua_isfunction (lua_Object object);
1404int lua_iscfunction (lua_Object object); 1407int lua_iscfunction (lua_Object object);
1405int lua_isuserdata (lua_Object object); 1408int lua_isuserdata (lua_Object object);
1406\end{verbatim} 1409\end{verbatim}
1407All macros return 1 if the object is compatible with the given type, 1410These functions return 1 if the object is compatible with the given type,
1408and 0 otherwise. 1411and 0 otherwise.
1409The function \verb|lua_isnumber| accepts numbers and numerical strings, 1412The function \verb|lua_isnumber| accepts numbers and numerical strings,
1410whereas 1413whereas
@@ -1444,7 +1447,8 @@ but may contain other zeros in their body.
1444If you do not know whether a string may contain zeros, 1447If you do not know whether a string may contain zeros,
1445you can use \verb|lua_strlen| to get the actual length. 1448you can use \verb|lua_strlen| to get the actual length.
1446Because Lua has garbage collection, 1449Because Lua has garbage collection,
1447there is no guarantee that such pointer will be valid after the block ends 1450there is no guarantee that the pointer returned by \verb|lua_getstring|
1451will be valid after the block ends
1448\see{GC}. 1452\see{GC}.
1449 1453
1450\verb|lua_getcfunction| converts a \verb|lua_Object| to a C function. 1454\verb|lua_getcfunction| converts a \verb|lua_Object| to a C function.
@@ -1501,7 +1505,7 @@ Note that the structure lua2C cannot be directly modified by C code.
1501 1505
1502The second structure, C2lua, is an abstract stack. 1506The second structure, C2lua, is an abstract stack.
1503Pushing elements into this stack 1507Pushing elements into this stack
1504is done with the following functions and macros: 1508is done with the following functions:
1505\Deffunc{lua_pushnumber}\Deffunc{lua_pushlstring}\Deffunc{lua_pushstring} 1509\Deffunc{lua_pushnumber}\Deffunc{lua_pushlstring}\Deffunc{lua_pushstring}
1506\Deffunc{lua_pushcfunction}\Deffunc{lua_pushusertag} 1510\Deffunc{lua_pushcfunction}\Deffunc{lua_pushusertag}
1507\Deffunc{lua_pushnil}\Deffunc{lua_pushobject} 1511\Deffunc{lua_pushnil}\Deffunc{lua_pushobject}
@@ -1518,7 +1522,7 @@ void lua_pushcfunction (lua_CFunction f); /* macro */
1518All of them receive a C value, 1522All of them receive a C value,
1519convert it to a corresponding \verb|lua_Object|, 1523convert it to a corresponding \verb|lua_Object|,
1520and leave the result on the top of C2lua. 1524and leave the result on the top of C2lua.
1521Particularly, functions \verb|lua_pushlstring| and \verb|lua_pushstring| 1525In particular, functions \verb|lua_pushlstring| and \verb|lua_pushstring|
1522make an internal copy of the given string. 1526make an internal copy of the given string.
1523Function \verb|lua_pushstring| can only be used to push proper C strings 1527Function \verb|lua_pushstring| can only be used to push proper C strings
1524(that is, strings that do not contain zeros and end with a zero); 1528(that is, strings that do not contain zeros and end with a zero);
@@ -1602,9 +1606,6 @@ is the ``name of the chunk'',
1602used in error messages and debug information. 1606used in error messages and debug information.
1603If \verb|name| is \verb|NULL|, 1607If \verb|name| is \verb|NULL|,
1604Lua gives a default name to the chunk. 1608Lua gives a default name to the chunk.
1605In files this name is the file name,
1606and \verb|lua_dostring| uses a small prefix
1607of the string as the chunk name.
1608 1609
1609These functions return, in structure lua2C, 1610These functions return, in structure lua2C,
1610any values eventually returned by the chunks. 1611any values eventually returned by the chunks.
@@ -1648,7 +1649,7 @@ The function
1648\begin{verbatim} 1649\begin{verbatim}
1649lua_Object lua_gettable (void); 1650lua_Object lua_gettable (void);
1650\end{verbatim} 1651\end{verbatim}
1651pops from the stack C2lua a table and an index, 1652pops a table and an index from the stack C2lua,
1652and returns the contents of the table at that index. 1653and returns the contents of the table at that index.
1653As in Lua, this operation may trigger a tag method. 1654As in Lua, this operation may trigger a tag method.
1654To get the real value of any table index, 1655To get the real value of any table index,
@@ -1662,7 +1663,7 @@ lua_Object lua_rawgettable (void);
1662To store a value in an index, 1663To store a value in an index,
1663the program must push the table, the index, 1664the program must push the table, the index,
1664and the value onto C2lua, 1665and the value onto C2lua,
1665and then call the function: 1666and then call the function
1666\Deffunc{lua_settable} 1667\Deffunc{lua_settable}
1667\begin{verbatim} 1668\begin{verbatim}
1668void lua_settable (void); 1669void lua_settable (void);
@@ -1733,11 +1734,11 @@ This function never returns.
1733If the C function has been called from Lua, 1734If the C function has been called from Lua,
1734then the corresponding Lua execution terminates, 1735then the corresponding Lua execution terminates,
1735as if an error had occurred inside Lua code. 1736as if an error had occurred inside Lua code.
1736Otherwise, the whole program terminates with a call to \verb|exit(1)|. 1737Otherwise, the whole host program terminates with a call to \verb|exit(1)|.
1737The \verb|message| is passed to the error handler function, 1738The \verb|message| is passed to the error handler function,
1738\verb|_ERRORMESSAGE|. 1739\verb|_ERRORMESSAGE|.
1739If \verb|message| is \verb|NULL|, 1740If \verb|message| is \verb|NULL|,
1740\verb|_ERRORMESSAGE| is not called. 1741then \verb|_ERRORMESSAGE| is not called.
1741 1742
1742Tag methods can be changed with: \Deffunc{lua_settagmethod} 1743Tag methods can be changed with: \Deffunc{lua_settagmethod}
1743\begin{verbatim} 1744\begin{verbatim}
@@ -1794,12 +1795,13 @@ Like a Lua function, a C function called by Lua can also return
1794many results. 1795many results.
1795 1796
1796When a C function is created, 1797When a C function is created,
1797it is possible to associate some \emph{upvalues} to it; 1798it is possible to associate some \emph{upvalues} to it,
1799thus creating a C closure;
1798then these values are passed to the function whenever it is called, 1800then these values are passed to the function whenever it is called,
1799as common arguments. 1801as common arguments.
1800To associate upvalues to a function, 1802To associate upvalues to a function,
1801first these values must be pushed on C2lua. 1803first these values must be pushed on C2lua.
1802Then the function: 1804Then the function
1803\Deffunc{lua_pushcclosure} 1805\Deffunc{lua_pushcclosure}
1804\begin{verbatim} 1806\begin{verbatim}
1805void lua_pushcclosure (lua_CFunction fn, int n); 1807void lua_pushcclosure (lua_CFunction fn, int n);
@@ -1860,7 +1862,7 @@ The libraries, on the other hand, provide useful routines
1860that are implemented directly through the standard API. 1862that are implemented directly through the standard API.
1861Therefore, they are not necessary to the language, 1863Therefore, they are not necessary to the language,
1862and are provided as separate C modules. 1864and are provided as separate C modules.
1863Currently there are three standard libraries: 1865Currently, there are three standard libraries:
1864\begin{itemize} 1866\begin{itemize}
1865\item string manipulation; 1867\item string manipulation;
1866\item mathematical functions (sin, log, etc); 1868\item mathematical functions (sin, log, etc);
@@ -1908,14 +1910,14 @@ the error is propagated.
1908If the string \verb|mode| contains \verb|"x"|, 1910If the string \verb|mode| contains \verb|"x"|,
1909then the call is \emph{protected}.\index{protected calls} 1911then the call is \emph{protected}.\index{protected calls}
1910In this mode, function \verb|call| does not propagate an error, 1912In this mode, function \verb|call| does not propagate an error,
1911whatever happens during the call. 1913regardless of what happens during the call.
1912Instead, it returns \nil\ to signal the error 1914Instead, it returns \nil\ to signal the error
1913(besides calling the appropriated error handler). 1915(besides calling the appropriated error handler).
1914 1916
1915If provided, 1917If provided,
1916\verb|errhandler| is temporarily set as the error function 1918\verb|errhandler| is temporarily set as the error function
1917\verb|_ERRORMESSAGE|, while \verb|func| runs. 1919\verb|_ERRORMESSAGE|, while \verb|func| runs.
1918As a particular example, if \verb|errhandler| is \nil, 1920In particular, if \verb|errhandler| is \nil,
1919no error messages will be issued during the execution of the called function. 1921no error messages will be issued during the execution of the called function.
1920 1922
1921\subsubsection*{\ff \T{collectgarbage ([limit])}}\Deffunc{collectgarbage} 1923\subsubsection*{\ff \T{collectgarbage ([limit])}}\Deffunc{collectgarbage}
@@ -1977,7 +1979,7 @@ Therefore, the function only considers fields with non \nil\ values.
1977The order in which the indices are enumerated is not specified, 1979The order in which the indices are enumerated is not specified,
1978\emph{even for numeric indices} 1980\emph{even for numeric indices}
1979(to traverse a table in numeric order, 1981(to traverse a table in numeric order,
1980use a counter or function \verb|foreachi|). 1982use a counter or the function \verb|foreachi|).
1981If the table indices are modified in any way during a traversal, 1983If the table indices are modified in any way during a traversal,
1982the semantics of \verb|next| is undefined. 1984the semantics of \verb|next| is undefined.
1983 1985
@@ -2247,9 +2249,10 @@ except that the table accesses are all raw (that is, without tag methods):
2247 else 2249 else
2248 pos = arg[1]; value = arg[2] 2250 pos = arg[1]; value = arg[2]
2249 end 2251 end
2250 t.n = n+1; n=n+1 2252 t.n = n+1;
2251 while (n=n-1)>=pos do 2253 while n >= pos do
2252 t[n+1] = t[n] 2254 t[n+1] = t[n]
2255 n = n-1
2253 end 2256 end
2254 t[pos] = value 2257 t[pos] = value
2255 end 2258 end
@@ -2277,9 +2280,9 @@ except that the table accesses are all raw (that is, without tag methods):
2277 local value = t[pos] 2280 local value = t[pos]
2278 if n<=0 then return end 2281 if n<=0 then return end
2279 t.n = n-1 2282 t.n = n-1
2280 pos = pos-1 2283 while pos < n do
2281 while (pos=pos+1)<n do
2282 t[pos] = t[pos+1] 2284 t[pos] = t[pos+1]
2285 pos = pos+1
2283 end 2286 end
2284 return value 2287 return value
2285 end 2288 end
@@ -2452,7 +2455,7 @@ the maximum number of substitutions to occur.
2452For instance, when \verb|n| is 1 only the first occurrence of 2455For instance, when \verb|n| is 1 only the first occurrence of
2453\verb|pat| is replaced. 2456\verb|pat| is replaced.
2454 2457
2455See some examples below: 2458Here are some examples:
2456\begin{verbatim} 2459\begin{verbatim}
2457 x = gsub("hello world", "(%w%w*)", "%1 %1") 2460 x = gsub("hello world", "(%w%w*)", "%1 %1")
2458 --> x="hello hello world world" 2461 --> x="hello hello world world"
@@ -2474,8 +2477,7 @@ See some examples below:
2474 --> x="lua - 3.1" 2477 --> x="lua - 3.1"
2475 2478
2476 t = {n=0} 2479 t = {n=0}
2477 gsub("first second word", "(%w%w*)", 2480 gsub("first second word", "(%w%w*)", function (w) tinsert(%t, w) end)
2478 function (w) %t.n = %t.n+1; %t[%t.n] = w end)
2479 --> t={"first", "second", "word"; n=3} 2481 --> t={"first", "second", "word"; n=3}
2480\end{verbatim} 2482\end{verbatim}
2481 2483
@@ -2486,7 +2488,8 @@ See some examples below:
2486a \Def{character class} is used to represent a set of characters. 2488a \Def{character class} is used to represent a set of characters.
2487The following combinations are allowed in describing a character class: 2489The following combinations are allowed in describing a character class:
2488\begin{description} 2490\begin{description}
2489\item[\emph{x}] (where \emph{x} is any character not in the list \verb|()%.[*-?|) 2491\item[\emph{x}] (where \emph{x} is any character not in the list
2492\verb|()%.[]*-?|)
2490--- represents the character \emph{x} itself. 2493--- represents the character \emph{x} itself.
2491\item[\T{.}] --- (a dot) represents all characters. 2494\item[\T{.}] --- (a dot) represents all characters.
2492\item[\T{\%a}] --- represents all letters. 2495\item[\T{\%a}] --- represents all letters.
@@ -2501,7 +2504,7 @@ The following combinations are allowed in describing a character class:
2501\item[\T{\%z}] --- represents the character with representation 0. 2504\item[\T{\%z}] --- represents the character with representation 0.
2502\item[\T{\%\M{x}}] (where \M{x} is any non alphanumeric character) --- 2505\item[\T{\%\M{x}}] (where \M{x} is any non alphanumeric character) ---
2503represents the character \M{x}. 2506represents the character \M{x}.
2504This is the standard way to escape the magic characters \verb|()%.[*-?|. 2507This is the standard way to escape the magic characters \verb|()%.[]*-?|.
2505\item[\T{[char-set]}] --- 2508\item[\T{[char-set]}] ---
2506Represents the class which is the union of all 2509Represents the class which is the union of all
2507characters in char-set. 2510characters in char-set.
@@ -2651,7 +2654,7 @@ This function opens a file,
2651in the mode specified in the string \verb|mode|. 2654in the mode specified in the string \verb|mode|.
2652It returns a new file handle, 2655It returns a new file handle,
2653or, in case of errors, \nil\ plus a string describing the error. 2656or, in case of errors, \nil\ plus a string describing the error.
2654This function does not modify neither \verb|_INPUT| nor \verb|_OUTPUT|. 2657This function does not modify either \verb|_INPUT| or \verb|_OUTPUT|.
2655 2658
2656The string mode can be any of the following: 2659The string mode can be any of the following:
2657\begin{description} 2660\begin{description}
@@ -2669,7 +2672,7 @@ which is needed in some systems to open the file in binary mode.
2669\subsubsection*{\ff \T{closefile (handle)}}\Deffunc{closefile} 2672\subsubsection*{\ff \T{closefile (handle)}}\Deffunc{closefile}
2670 2673
2671This function closes the given file. 2674This function closes the given file.
2672It does not modify neither \verb|_INPUT| nor \verb|_OUTPUT|. 2675It does not modify either \verb|_INPUT| or \verb|_OUTPUT|.
2673 2676
2674\subsubsection*{\ff \T{readfrom (filename)}}\Deffunc{readfrom} 2677\subsubsection*{\ff \T{readfrom (filename)}}\Deffunc{readfrom}
2675 2678
@@ -2783,7 +2786,7 @@ Returns a string with a file name that can safely
2783be used for a temporary file. 2786be used for a temporary file.
2784The file must be explicitly removed when no longer needed. 2787The file must be explicitly removed when no longer needed.
2785 2788
2786\subsubsection*{\ff \T{read ([filehandle] {readpattern})}}\Deffunc{read} 2789\subsubsection*{\ff \T{read ([filehandle,] readpattern1, ...)}}\Deffunc{read}
2787 2790
2788Reads file \verb|_INPUT|, 2791Reads file \verb|_INPUT|,
2789or \verb|filehandle| if this argument is given, 2792or \verb|filehandle| if this argument is given,
@@ -2822,15 +2825,15 @@ but are not included in the resulting string.
2822 2825
2823There are some predefined patterns, as follows: 2826There are some predefined patterns, as follows:
2824\begin{description} 2827\begin{description}
2825\item["*n"] reads a number; 2828\item[``*n''] reads a number;
2826this is the only pattern that returns a number instead of a string. 2829this is the only pattern that returns a number instead of a string.
2827\item["*l"] returns the next line 2830\item[``*l''] returns the next line
2828(skipping the end of line), or \nil\ on end of file. 2831(skipping the end of line), or \nil\ on end of file.
2829This is the default pattern. 2832This is the default pattern.
2830It is equivalent to the pattern \verb|"[^\n]*{\n}"|. 2833It is equivalent to the pattern \verb|"[^\n]*{\n}"|.
2831\item["*a"] reads the whole file. 2834\item[``*a''] reads the whole file.
2832It is equivalent to the pattern \verb|".*"|. 2835It is equivalent to the pattern \verb|".*"|.
2833\item["*w"] returns the next word 2836\item[``*w''] returns the next word
2834(maximal sequence of non white-space characters), 2837(maximal sequence of non white-space characters),
2835skipping spaces if necessary, or \nil\ on end of file. 2838skipping spaces if necessary, or \nil\ on end of file.
2836It is equivalent to the pattern \verb|"{%s*}%S%S*"|. 2839It is equivalent to the pattern \verb|"{%s*}%S%S*"|.
@@ -2854,7 +2857,7 @@ formatted according to the given string \verb|format|,
2854following the same rules of the ANSI C function \verb|strftime|. 2857following the same rules of the ANSI C function \verb|strftime|.
2855When called without arguments, 2858When called without arguments,
2856it returns a reasonable date and time representation that depends on 2859it returns a reasonable date and time representation that depends on
2857the host system and the locale. 2860the host system and on the locale.
2858 2861
2859\subsubsection*{\ff \T{clock ()}}\Deffunc{clock} 2862\subsubsection*{\ff \T{clock ()}}\Deffunc{clock}
2860 2863
@@ -2961,11 +2964,12 @@ Because functions in Lua are first class values,
2961they do not have a fixed name: 2964they do not have a fixed name:
2962Some functions may be the value of many global variables, 2965Some functions may be the value of many global variables,
2963while others may be stored only in a table field. 2966while others may be stored only in a table field.
2964Function \verb|lua_getobjname| first checks whether the given 2967Function \verb|lua_getobjname| checks whether the given
2965function is a tag method. 2968function is a tag method or the value of a global variable.
2966If so, it returns the string \verb|"tag-method"|, 2969If the given function is a tag method, then \verb|lua_getobjname|
2970returns the string \verb|"tag-method"|,
2967and \verb|name| is set to point to the event name. 2971and \verb|name| is set to point to the event name.
2968Otherwise, if the given function is the value of a global variable, 2972If the given function is the value of a global variable,
2969then \verb|lua_getobjname| returns the string \verb|"global"|, 2973then \verb|lua_getobjname| returns the string \verb|"global"|,
2970and \verb|name| points to the variable name. 2974and \verb|name| points to the variable name.
2971If the given function is neither a tag method nor a global variable, 2975If the given function is neither a tag method nor a global variable,
@@ -3014,7 +3018,7 @@ lua_CHFunction lua_setcallhook (lua_CHFunction func);
3014typedef void (*lua_LHFunction) (int line); 3018typedef void (*lua_LHFunction) (int line);
3015lua_LHFunction lua_setlinehook (lua_LHFunction func); 3019lua_LHFunction lua_setlinehook (lua_LHFunction func);
3016\end{verbatim} 3020\end{verbatim}
3017The first one is called whenever the interpreter enters or leaves a 3021The first hook is called whenever the interpreter enters or leaves a
3018function. 3022function.
3019When entering a function, 3023When entering a function,
3020its parameters are a handle to the function activation record, 3024its parameters are a handle to the function activation record,
@@ -3043,12 +3047,12 @@ set their corresponding hooks and return their previous values.
3043The library \verb|ldblib| provides 3047The library \verb|ldblib| provides
3044the functionallity of the debugger interface to Lua programs. 3048the functionallity of the debugger interface to Lua programs.
3045If you want to use this library, 3049If you want to use this library,
3046your host application must open it first, 3050your host application must open it,
3047calling \verb|lua_dblibopen|. 3051calling \verb|lua_dblibopen|.
3048 3052
3049You should exert great care when using this library. 3053You should exert great care when using this library.
3050Functions provided here should be used exclusively for debugging 3054The functions provided here should be used exclusively for debugging
3051and similar tasks (e.g. profiling): 3055and similar tasks (e.g. profiling).
3052Please resist the temptation to use them as a 3056Please resist the temptation to use them as a
3053usual programming tool. 3057usual programming tool.
3054They are slow and violate some (otherwise) secure aspects of the 3058They are slow and violate some (otherwise) secure aspects of the
@@ -3072,7 +3076,7 @@ If the function was defined in a string,
3072If the function was defined in a file, 3076If the function was defined in a file,
3073\verb|source| starts with a \verb|@| followed by the file name. 3077\verb|source| starts with a \verb|@| followed by the file name.
3074 3078
3075\item[def_line] the line where the function was defined in the source 3079\item[def\_line] the line where the function was defined in the source
3076(only valid if this is a Lua function). 3080(only valid if this is a Lua function).
3077 3081
3078\item[where] can be \verb|"global"| if this function has a global name, 3082\item[where] can be \verb|"global"| if this function has a global name,
@@ -3225,8 +3229,8 @@ Here is a list of all these incompatibilities.
3225\item 3229\item
3226In the debug API, the old variables \verb|lua_debug|, 3230In the debug API, the old variables \verb|lua_debug|,
3227\verb|lua_callhook| and \verb|lua_linehook| now live inside \verb|lua_state|. 3231\verb|lua_callhook| and \verb|lua_linehook| now live inside \verb|lua_state|.
3228Therefore, they are no more directly accessible, and must be 3232Therefore, they are no longer directly accessible, and must be
3229manipulated through the new functions \verb|lua_setdebug|, 3233manipulated only through the new functions \verb|lua_setdebug|,
3230\verb|lua_setcallhook| and \verb|lua_setlinehook|. 3234\verb|lua_setcallhook| and \verb|lua_setlinehook|.
3231 3235
3232\item Old pre-compiled code is obsolete, and must be re-compiled. 3236\item Old pre-compiled code is obsolete, and must be re-compiled.