aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2003-01-20 09:03:05 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2003-01-20 09:03:05 -0200
commit60a8b94fd0e865fab202fbbd61f211fb4fde8181 (patch)
tree9169fbb4e2b9bc468a93f86e8d8cfb427d13ae5c
parentc499442e5f19c0b99cc14fa009c46aefb7f69cb1 (diff)
downloadlua-60a8b94fd0e865fab202fbbd61f211fb4fde8181.tar.gz
lua-60a8b94fd0e865fab202fbbd61f211fb4fde8181.tar.bz2
lua-60a8b94fd0e865fab202fbbd61f211fb4fde8181.zip
corrections/suggestions by Ed Ferguson
-rw-r--r--manual.tex286
1 files changed, 147 insertions, 139 deletions
diff --git a/manual.tex b/manual.tex
index af5a72aa..fdba4a8d 100644
--- a/manual.tex
+++ b/manual.tex
@@ -1,4 +1,4 @@
1% $Id: manual.tex,v 1.63 2002/11/18 14:39:34 roberto Exp roberto $ 1% $Id: manual.tex,v 1.64 2002/12/11 13:43:15 roberto Exp roberto $
2%{[( 2%{[(
3 3
4\documentclass[11pt,twoside]{article} 4\documentclass[11pt,twoside]{article}
@@ -134,7 +134,7 @@ Waldemar Celes
134\tecgraf\ --- Computer Science Department --- PUC-Rio 134\tecgraf\ --- Computer Science Department --- PUC-Rio
135} 135}
136 136
137%\date{{\small \tt\$Date: 2002/11/18 14:39:34 $ $}} 137%\date{{\small \tt\$Date: 2002/12/11 13:43:15 $ $}}
138 138
139\maketitle 139\maketitle
140 140
@@ -273,7 +273,7 @@ This environment is initialized with a call from the embedding program to
273persists until a call to \verb|lua_close| 273persists until a call to \verb|lua_close|
274or the end of the embedding program. 274or the end of the embedding program.
275The host program can create multiple independent global 275The host program can create multiple independent global
276environments, and freely switch between them \see{mangstate}. 276environments, and freely switch among them \see{mangstate}.
277 277
278The unit of execution of Lua is called a \Def{chunk}. 278The unit of execution of Lua is called a \Def{chunk}.
279A chunk is simply a sequence of statements. 279A chunk is simply a sequence of statements.
@@ -284,7 +284,7 @@ When a chunk is executed, first it is pre-compiled into opcodes for
284a virtual machine, 284a virtual machine,
285and then the compiled statements are executed 285and then the compiled statements are executed
286by an interpreter for the virtual machine. 286by an interpreter for the virtual machine.
287All modifications a chunk makes to the global environment persist 287All modifications that a chunk makes to the global environment persist
288after the chunk ends. 288after the chunk ends.
289 289
290Chunks may also be pre-compiled into binary form and stored in files; 290Chunks may also be pre-compiled into binary form and stored in files;
@@ -316,10 +316,13 @@ usually it represents the absence of a useful value.
316In Lua, both \nil{} and \False{} make a condition false, 316In Lua, both \nil{} and \False{} make a condition false,
317and any other value makes it true. 317and any other value makes it true.
318\emph{Number} represents real (double-precision floating-point) numbers. 318\emph{Number} represents real (double-precision floating-point) numbers.
319(It is not difficult to build Lua interpreters that use other
320internal representations for numbers,
321such as single-precision float or long integers.)
319\emph{String} represents arrays of characters. 322\emph{String} represents arrays of characters.
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, 325so strings may contain any 8-bit character,
323including embedded zeros (\verb|'\0'|) \see{lexical}. 326including embedded zeros (\verb|'\0'|) \see{lexical}.
324 327
325Functions are \emph{first-class values} in Lua. 328Functions are \emph{first-class values} in Lua.
@@ -351,9 +354,9 @@ that is, \Index{arrays} that can be indexed not only with numbers,
351but with any value (except \nil). 354but with any value (except \nil).
352Moreover, 355Moreover,
353tables can be \emph{heterogeneous}, 356tables can be \emph{heterogeneous},
354that is, they can contain values of all types. 357that is, they can contain values of all types (except \nil).
355Tables are the sole data structuring mechanism in Lua; 358Tables are the sole data structuring mechanism in Lua;
356they may be used not only to represent ordinary arrays, 359they may be used to represent not only ordinary arrays,
357but also symbol tables, sets, records, graphs, trees, etc. 360but also symbol tables, sets, records, graphs, trees, etc.
358To represent \Index{records}, Lua uses the field name as an index. 361To represent \Index{records}, Lua uses the field name as an index.
359The language supports this representation by 362The language supports this representation by
@@ -365,7 +368,7 @@ Like indices, the value of a table field can be of any type.
365In particular, 368In particular,
366because functions are first class values, 369because functions are first class values,
367table fields may contain functions. 370table fields may contain functions.
368So, tables may also carry \emph{methods} \see{func-def}. 371Thus tables may also carry \emph{methods} \see{func-def}.
369 372
370Tables, functions, and userdata values are \emph{objects}: 373Tables, functions, and userdata values are \emph{objects}:
371variables do not actually \emph{contain} these values, 374variables do not actually \emph{contain} these values,
@@ -413,7 +416,7 @@ Lua provides automatic conversion between
413string and number values at run time. 416string and number values at run time.
414Any arithmetic operation applied to a string tries to convert 417Any arithmetic operation applied to a string tries to convert
415that string to a number, following the usual rules. 418that string to a number, following the usual rules.
416Conversely, whenever a number is used when a string is expected, 419Conversely, whenever a number is used where a string is expected,
417the number is converted to a string, in a reasonable format. 420the number is converted to a string, in a reasonable format.
418The format is chosen so that 421The format is chosen so that
419a conversion from number to string then back to number 422a conversion from number to string then back to number
@@ -429,7 +432,7 @@ global variables
429and local variables. 432and local variables.
430Variables are assumed to be global unless explicitly declared local 433Variables are assumed to be global unless explicitly declared local
431\see{localvar}. 434\see{localvar}.
432Before the first assignment, the value of a variable is \nil. 435Before the first assignment to a variable, its value is \nil.
433 436
434All global variables live as fields in ordinary Lua tables. 437All global variables live as fields in ordinary Lua tables.
435Usually, globals live in a table called \Index{table of globals}. 438Usually, globals live in a table called \Index{table of globals}.
@@ -473,8 +476,8 @@ and the other is a threshold.
473When the number of bytes crosses the threshold, 476When the number of bytes crosses the threshold,
474Lua runs the garbage collector, 477Lua runs the garbage collector,
475which reclaims the memory of all dead objects. 478which reclaims the memory of all dead objects.
476The byte counter is corrected, 479The byte counter is adjusted,
477and then the threshold is reset to twice the value of the byte counter. 480and then the threshold is reset to twice the new value of the byte counter.
478 481
479Through the C~API, you can query those numbers, 482Through the C~API, you can query those numbers,
480and change the threshold \see{GC-API}. 483and change the threshold \see{GC-API}.
@@ -502,9 +505,9 @@ A table with both weak keys and weak values allows the collection of
502both keys and values. 505both keys and values.
503In any case, if either the key or the value is collected, 506In any case, if either the key or the value is collected,
504the whole pair is removed from the table. 507the whole pair is removed from the table.
505The weakness of a table is controled by the value of the 508The weakness of a table is controlled by the value of the
506\verb|__mode| field of its metatable. 509\verb|__mode| field of its metatable.
507If the \verb|__mode| field is a string containing the \verb|k| character, 510If the \verb|__mode| field is a string containing the character \verb|k|,
508the keys in the table are weak. 511the keys in the table are weak.
509If \verb|__mode| contains \verb|v|, 512If \verb|__mode| contains \verb|v|,
510the values in the table are weak. 513the values in the table are weak.
@@ -546,7 +549,7 @@ Lua is a case-sensitive language:
546(if the locale permits) are two different, valid identifiers. 549(if the locale permits) are two different, valid identifiers.
547As a convention, identifiers starting with an underscore followed by 550As a convention, identifiers starting with an underscore followed by
548uppercase letters (such as \verb|_VERSION|) 551uppercase letters (such as \verb|_VERSION|)
549are reserved for internal variables. 552are reserved for internal variables used by Lua.
550 553
551The following strings denote other \Index{tokens}: 554The following strings denote other \Index{tokens}:
552\begin{verbatim} 555\begin{verbatim}
@@ -571,8 +574,8 @@ and can contain the C-like escape sequences
571`\verb|\'|' (single quote), 574`\verb|\'|' (single quote),
572and `\verb|\|\emph{newline}' (that is, a backslash followed by a real newline, 575and `\verb|\|\emph{newline}' (that is, a backslash followed by a real newline,
573which results in a newline in the string). 576which results in a newline in the string).
574A character in a string may also be specified by its numerical value, 577A character in a string may also be specified by its numerical value
575through the escape sequence `\verb|\|\emph{ddd}', 578using the escape sequence `\verb|\|\emph{ddd}',
576where \emph{ddd} is a sequence of up to three \emph{decimal} digits. 579where \emph{ddd} is a sequence of up to three \emph{decimal} digits.
577Strings in Lua may contain any 8-bit value, including embedded zeros, 580Strings in Lua may contain any 8-bit value, including embedded zeros,
578which can be specified as `\verb|\0|'. 581which can be specified as `\verb|\0|'.
@@ -648,7 +651,7 @@ The syntax \verb|var.NAME| is just syntactic sugar for
648\end{Produc}% 651\end{Produc}%
649 652
650The expression denoting the table to be indexed has a restricted syntax; 653The expression denoting the table to be indexed has a restricted syntax;
651\See{expressions} for details. 654see \See{expressions} for details.
652 655
653The meaning of assignments and evaluations of global and 656The meaning of assignments and evaluations of global and
654indexed variables can be changed via metatables. 657indexed variables can be changed via metatables.
@@ -656,17 +659,17 @@ An assignment to a global variable \verb|x = val|
656is equivalent to the assignment 659is equivalent to the assignment
657\verb|_glob.x = val|, 660\verb|_glob.x = val|,
658where \verb|_glob| is the table of globals of the running function 661where \verb|_glob| is the table of globals of the running function
659(\see{global-table} for a discussion about the table of globals). 662(see \See{global-table} for a discussion about the table of globals).
660An assignment to an indexed variable \verb|t[i] = val| is equivalent to 663An assignment to an indexed variable \verb|t[i] = val| is equivalent to
661\verb|settable_event(t,i,val)|. 664\verb|settable_event(t,i,val)|.
662An access to a global variable \verb|x| 665An access to a global variable \verb|x|
663is equivalent to \verb|_glob.x| 666is equivalent to \verb|_glob.x|
664(again, \see{global-table} for a discussion about \verb|_glob|). 667(again, see \See{global-table} for a discussion about \verb|_glob|).
665An access to an indexed variable \verb|t[i]| is equivalent to 668An access to an indexed variable \verb|t[i]| is equivalent to
666a call \verb|gettable_event(t,i)|. 669a call \verb|gettable_event(t,i)|.
667See \See{metatable} for a complete description of the 670See \See{metatable} for a complete description of the
668\verb|settable_event| and \verb|gettable_event| functions. 671\verb|settable_event| and \verb|gettable_event| functions.
669(These functions are not defined in Lua. 672(These functions are not defined or callable in Lua.
670We use them here only for explanatory purposes.) 673We use them here only for explanatory purposes.)
671 674
672 675
@@ -724,16 +727,16 @@ the list of values is \emph{adjusted} to the length of
724the list of variables.\index{adjustment} 727the list of variables.\index{adjustment}
725If there are more values than needed, 728If there are more values than needed,
726the excess values are thrown away. 729the excess values are thrown away.
727If there are less values than needed, 730If there are fewer values than needed,
728the list is extended with as many \nil's as needed. 731the list is extended with as many \nil's as needed.
729If the list of expressions ends with a function call, 732If the list of expressions ends with a function call,
730then all values returned by that function call enter in the list of values, 733then all values returned by that function call enter in the list of values,
731before the adjust 734before the adjustment
732(except when the call is enclosed in parentheses; see \See{expressions}). 735(except when the call is enclosed in parentheses; see \See{expressions}).
733 736
734The assignment statement first evaluates all its expressions, 737The assignment statement first evaluates all its expressions,
735and only then makes the assignments. 738and only then are the assignments performed.
736So, the code 739Thus the code
737\begin{verbatim} 740\begin{verbatim}
738 i = 3 741 i = 3
739 i, a[i] = i+1, 20 742 i, a[i] = i+1, 20
@@ -765,23 +768,23 @@ Lua also has a \rwd{for} statement, in two flavors \see{for}.
765 768
766The \Index{condition expression} \M{exp} of a 769The \Index{condition expression} \M{exp} of a
767control structure may return any value. 770control structure may return any value.
771Both \False{} and \nil{} are considered false.
768All values different from \nil{} and \False{} are considered true 772All values different from \nil{} and \False{} are considered true
769(in particular, the number 0 and the empty string are also true); 773(in particular, the number 0 and the empty string are also true).
770both \False{} and \nil{} are considered false.
771 774
772The \rwd{return} statement is used to return values 775The \rwd{return} statement is used to return values
773from a function or from a chunk.\IndexKW{return} 776from a function or from a chunk.\IndexKW{return}
774\label{return}% 777\label{return}%
775\index{return statement}% 778\index{return statement}%
776Functions and chunks may return more than one value, 779Functions and chunks may return more than one value,
777and so the syntax for the \rwd{return} statement is 780so the syntax for the \rwd{return} statement is
778\begin{Produc} 781\begin{Produc}
779\produc{stat}{\rwd{return} \opt{explist1}} 782\produc{stat}{\rwd{return} \opt{explist1}}
780\end{Produc}% 783\end{Produc}%
781 784
782The \rwd{break} statement can be used to terminate the execution of a 785The \rwd{break} statement can be used to terminate the execution of a
783\rwd{while}, \rwd{repeat}, or \rwd{for} loop, 786\rwd{while}, \rwd{repeat}, or \rwd{for} loop,
784skipping to the next statement after the loop:\IndexKW{break} 787and to skip to the next statement after the loop:\IndexKW{break}
785\index{break statement} 788\index{break statement}
786\begin{Produc} 789\begin{Produc}
787\produc{stat}{\rwd{break}} 790\produc{stat}{\rwd{break}}
@@ -807,10 +810,10 @@ beginning of a chunk for syntax checking only.)
807\subsubsection{For Statement} \label{for}\index{for statement} 810\subsubsection{For Statement} \label{for}\index{for statement}
808 811
809The \rwd{for} statement has two forms, 812The \rwd{for} statement has two forms,
810one for numbers and one generic. 813one numeric and one generic.
811\IndexKW{for}\IndexKW{in} 814\IndexKW{for}\IndexKW{in}
812 815
813The numerical \rwd{for} loop repeats a block of code while a 816The numeric \rwd{for} loop repeats a block of code while a
814control variable runs through an arithmetic progression. 817control variable runs through an arithmetic progression.
815It has the following syntax: 818It has the following syntax:
816\begin{Produc} 819\begin{Produc}
@@ -818,7 +821,7 @@ It has the following syntax:
818 \rwd{do} block \rwd{end}} 821 \rwd{do} block \rwd{end}}
819\end{Produc}% 822\end{Produc}%
820The \emph{block} is repeated for \emph{name} starting at the value of 823The \emph{block} is repeated for \emph{name} starting at the value of
821the first \emph{exp}, until it reaches the second \emph{exp} by steps of the 824the first \emph{exp}, until it passes the second \emph{exp} by steps of the
822third \emph{exp}. 825third \emph{exp}.
823More precisely, a \rwd{for} statement like 826More precisely, a \rwd{for} statement like
824\begin{verbatim} 827\begin{verbatim}
@@ -868,7 +871,7 @@ is equivalent to the code:
868\begin{verbatim} 871\begin{verbatim}
869 do 872 do
870 local _f, _s, var_1, ..., var_n = explist 873 local _f, _s, var_1, ..., var_n = explist
871 while 1 do 874 while true do
872 var_1, ..., var_n = _f(_s, var_1) 875 var_1, ..., var_n = _f(_s, var_1)
873 if var_1 == nil then break end 876 if var_1 == nil then break end
874 block 877 block
@@ -879,7 +882,7 @@ Note the following:
879\begin{itemize}\itemsep=0pt 882\begin{itemize}\itemsep=0pt
880\item \verb|explist| is evaluated only once. 883\item \verb|explist| is evaluated only once.
881Its results are a ``generator'' function, 884Its results are a ``generator'' function,
882a ``state'', and an initial value for the ``iterator variable''. 885a ``state'', and an initial value for the first ``iterator variable''.
883\item \verb|_f| and \verb|_s| are invisible variables. 886\item \verb|_f| and \verb|_s| are invisible variables.
884The names are here for explanatory purposes only. 887The names are here for explanatory purposes only.
885\item The behavior is \emph{undefined} if you assign to any 888\item The behavior is \emph{undefined} if you assign to any
@@ -913,7 +916,7 @@ of a multiple assignment \see{assignment}.
913Otherwise, all variables are initialized with \nil. 916Otherwise, all variables are initialized with \nil.
914 917
915A chunk is also a block \see{chunks}, 918A chunk is also a block \see{chunks},
916and so local variables can be declared outside any explicit block. 919so local variables can be declared outside any explicit block.
917Such local variables die when the chunk ends. 920Such local variables die when the chunk ends.
918 921
919Visibility rules for local variables are explained in \See{visibility}. 922Visibility rules for local variables are explained in \See{visibility}.
@@ -948,7 +951,7 @@ function calls are explained in \See{functioncall};
948table constructors are explained in \See{tableconstructor}. 951table constructors are explained in \See{tableconstructor}.
949 952
950Expressions can also be built with arithmetic operators, relational operators, 953Expressions can also be built with arithmetic operators, relational operators,
951and logical operadors, all of which are explained below. 954and logical operators, all of which are explained below.
952 955
953\subsubsection{Arithmetic Operators} 956\subsubsection{Arithmetic Operators}
954Lua supports the usual \Index{arithmetic operators}: 957Lua supports the usual \Index{arithmetic operators}:
@@ -1019,7 +1022,7 @@ The conjunction operator \rwd{and} returns its first argument
1019if its value is \False{} or \nil; 1022if its value is \False{} or \nil;
1020otherwise, \rwd{and} returns its second argument. 1023otherwise, \rwd{and} returns its second argument.
1021The disjunction operator \rwd{or} returns its first argument 1024The disjunction operator \rwd{or} returns its first argument
1022if it is different from \nil and \False; 1025if it is different from \nil{} and \False;
1023otherwise, \rwd{or} returns its second argument. 1026otherwise, \rwd{or} returns its second argument.
1024Both \rwd{and} and \rwd{or} use \Index{short-cut evaluation}, 1027Both \rwd{and} and \rwd{or} use \Index{short-cut evaluation},
1025that is, 1028that is,
@@ -1249,7 +1252,7 @@ Different instances of the same function
1249may refer to different non-local variables \see{visibility} 1252may refer to different non-local variables \see{visibility}
1250and may have different tables of globals \see{global-table}. 1253and may have different tables of globals \see{global-table}.
1251 1254
1252Parameters act as local variables, 1255Parameters act as local variables that are
1253initialized with the argument values: 1256initialized with the argument values:
1254\begin{Produc} 1257\begin{Produc}
1255\produc{parlist1}{namelist \opt{\ter{,} \ter{\ldots}}} 1258\produc{parlist1}{namelist \opt{\ter{,} \ter{\ldots}}}
@@ -1267,7 +1270,7 @@ instead, it collects all extra arguments into an implicit parameter,
1267called \IndexLIB{arg}. 1270called \IndexLIB{arg}.
1268The value of \verb|arg| is a table, 1271The value of \verb|arg| is a table,
1269with a field~\verb|n| whose value is the number of extra arguments, 1272with a field~\verb|n| whose value is the number of extra arguments,
1270and the extra arguments at positions 1,~2,~\ldots,~\verb|n|. 1273and with the extra arguments at positions 1,~2,~\ldots,~\verb|n|.
1271 1274
1272As an example, consider the following definitions: 1275As an example, consider the following definitions:
1273\begin{verbatim} 1276\begin{verbatim}
@@ -1335,7 +1338,7 @@ Notice that, in a declaration like \verb|local x = x|,
1335the new \verb|x| being declared is not in scope yet, 1338the new \verb|x| being declared is not in scope yet,
1336so the second \verb|x| refers to the ``outside'' variable. 1339so the second \verb|x| refers to the ``outside'' variable.
1337 1340
1338Because of those \Index{lexical scoping} rules, 1341Because of these \Index{lexical scoping} rules,
1339local variables can be freely accessed by functions 1342local variables can be freely accessed by functions
1340defined inside their scope. 1343defined inside their scope.
1341For instance: 1344For instance:
@@ -1358,8 +1361,9 @@ Consider the following example:
1358 a[i] = function () y=y+1; return x+y end 1361 a[i] = function () y=y+1; return x+y end
1359 end 1362 end
1360\end{verbatim} 1363\end{verbatim}
1361In that code, 1364The loop creates ten closures
1362each function uses a different \verb|y| variable, 1365(that is, instances of the anonymous function).
1366Each of these closures uses a different \verb|y| variable,
1363while all of them share the same \verb|x|. 1367while all of them share the same \verb|x|.
1364 1368
1365\subsection{Error Handling} \label{error} 1369\subsection{Error Handling} \label{error}
@@ -1382,11 +1386,11 @@ you can use the \verb|pcall| function \see{pdf-pcall}.
1382 1386
1383Every table and userdata value in Lua may have a \emph{metatable}. 1387Every table and userdata value in Lua may have a \emph{metatable}.
1384This \IndexEmph{metatable} is a table that defines the behavior of 1388This \IndexEmph{metatable} is a table that defines the behavior of
1385the original table and userdata under some operations. 1389the original table and userdata under certain special operations.
1386You can query and change the metatable of an object with 1390You can query and change the metatable of an object with
1387functions \verb|setmetatable| and \verb|getmetatable| \see{pdf-getmetatable}. 1391functions \verb|setmetatable| and \verb|getmetatable| \see{pdf-getmetatable}.
1388 1392
1389For each of those operations Lua associates a specific key, 1393For each of those operations Lua associates a specific key
1390called an \emph{event}. 1394called an \emph{event}.
1391When Lua performs one of those operations over a table or a userdata, 1395When Lua performs one of those operations over a table or a userdata,
1392if checks whether that object has a metatable with the corresponding event. 1396if checks whether that object has a metatable with the corresponding event.
@@ -1613,7 +1617,7 @@ called when Lua calls a value.
1613 else 1617 else
1614 local h = metatable(func).__call 1618 local h = metatable(func).__call
1615 if h then 1619 if h then
1616 tinsert(arg, 1, func) 1620 table.insert(arg, 1, func)
1617 return h(unpack(arg)) 1621 return h(unpack(arg))
1618 else 1622 else
1619 error("call expression not a function") 1623 error("call expression not a function")
@@ -1650,16 +1654,16 @@ with the last userdata created in the program
1650\subsection{Coroutines} 1654\subsection{Coroutines}
1651 1655
1652Lua supports coroutines, 1656Lua supports coroutines,
1653also called \emph{semi-coroutines}, \emph{generators}, 1657also called \emph{semi-coroutines}
1654or \emph{colaborative multithreading}. 1658or \emph{collaborative multithreading}.
1655A coroutine in Lua represents an independent thread of execution. 1659A coroutine in Lua represents an independent thread of execution.
1656Unlike ``real'' threads, however, 1660Unlike ``real'' threads, however,
1657a coroutine only suspends its execution by explicitly calling 1661a coroutine only suspends its execution by explicitly calling
1658an yield function. 1662an yield function.
1659 1663
1660You create a coroutine with a call to \IndexVerb{coroutine.create}. 1664You create a coroutine with a call to \IndexVerb{coroutine.create}.
1661Its sole argument is a function, 1665Its sole argument is a function
1662which is the main function of the coroutine. 1666that is the main function of the coroutine.
1663The \verb|coroutine.create| only creates a new coroutine and 1667The \verb|coroutine.create| only creates a new coroutine and
1664returns a handle to it (an object of type \emph{thread}). 1668returns a handle to it (an object of type \emph{thread}).
1665It does not start the coroutine execution. 1669It does not start the coroutine execution.
@@ -1679,10 +1683,10 @@ Normally, when its main function returns
1679and abnormally, if there is an unprotected error. 1683and abnormally, if there is an unprotected error.
1680In the first case, \verb|coroutine.resume| returns \True, 1684In the first case, \verb|coroutine.resume| returns \True,
1681plus any values returned by the coroutine main function. 1685plus any values returned by the coroutine main function.
1682In case of errors, \verb|coroutine.resume| returns \False 1686In case of errors, \verb|coroutine.resume| returns \False{}
1683plus an error message. 1687plus an error message.
1684 1688
1685A coroutine yields calling \IndexVerb{coroutine.yield}. 1689A coroutine yields by calling \IndexVerb{coroutine.yield}.
1686When a coroutine yields, 1690When a coroutine yields,
1687the corresponding \verb|coroutine.resume| returns immediately, 1691the corresponding \verb|coroutine.resume| returns immediately,
1688even if the yield happens inside nested function calls 1692even if the yield happens inside nested function calls
@@ -1818,7 +1822,7 @@ The following function creates a new ``thread'' in Lua:
1818The new state returned by this function shares with the original state 1822The new state returned by this function shares with the original state
1819all global environment (such as tables), 1823all global environment (such as tables),
1820but has an independent run-time stack. 1824but has an independent run-time stack.
1821(The use of these multiple stacks must be ``syncronized'' with C. 1825(The use of these multiple stacks must be ``synchronized'' with C.
1822How to explain that? TO BE WRITTEN.) 1826How to explain that? TO BE WRITTEN.)
1823 1827
1824Each thread has an independent table for global variables. 1828Each thread has an independent table for global variables.
@@ -1841,9 +1845,9 @@ Each element in this stack represents a Lua value
1841 1845
1842Each C invocation has its own stack. 1846Each C invocation has its own stack.
1843Whenever Lua calls C, the called function gets a new stack, 1847Whenever Lua calls C, the called function gets a new stack,
1844which is independent of previous stacks or of stacks of still 1848which is independent of previous stacks and of stacks of still
1845active C functions. 1849active C functions.
1846That stack contains initially any arguments to the C function, 1850That stack initially contains any arguments to the C function,
1847and it is where the C function pushes its results \see{LuacallC}. 1851and it is where the C function pushes its results \see{LuacallC}.
1848 1852
1849For convenience, 1853For convenience,
@@ -1906,7 +1910,7 @@ as follows:
1906\end{verbatim} 1910\end{verbatim}
1907Note that 0 is never an acceptable index. 1911Note that 0 is never an acceptable index.
1908 1912
1909Unless otherwise noticed, 1913Unless otherwise noted,
1910any function that accepts valid indices can also be called with 1914any function that accepts valid indices can also be called with
1911\Index{pseudo-indices}, 1915\Index{pseudo-indices},
1912which represent some Lua values that are accessible to the C~code 1916which represent some Lua values that are accessible to the C~code
@@ -2004,6 +2008,7 @@ defined in \verb|lua.h|:
2004\verb|LUA_TTABLE|, 2008\verb|LUA_TTABLE|,
2005\verb|LUA_TFUNCTION|, 2009\verb|LUA_TFUNCTION|,
2006\verb|LUA_TUSERDATA|, 2010\verb|LUA_TUSERDATA|,
2011\verb|LUA_TTHREAD|,
2007\verb|LUA_TLIGHTUSERDATA|. 2012\verb|LUA_TLIGHTUSERDATA|.
2008The following function translates such constants to a type name: 2013The following function translates such constants to a type name:
2009\begin{verbatim} 2014\begin{verbatim}
@@ -2021,10 +2026,10 @@ They always return 0 for a non-valid index.
2021\verb|lua_isnumber| accepts numbers and numerical strings, 2026\verb|lua_isnumber| accepts numbers and numerical strings,
2022\verb|lua_isstring| accepts strings and numbers \see{coercion}, 2027\verb|lua_isstring| accepts strings and numbers \see{coercion},
2023\verb|lua_isfunction| accepts both Lua functions and C~functions, 2028\verb|lua_isfunction| accepts both Lua functions and C~functions,
2024and \verb|lua_isuserdata| accepts both full and ligth userdata. 2029and \verb|lua_isuserdata| accepts both full and light userdata.
2025To distinguish between Lua functions and C~functions, 2030To distinguish between Lua functions and C~functions,
2026you should use \verb|lua_iscfunction|. 2031you should use \verb|lua_iscfunction|.
2027To distinguish between full and ligth userdata, 2032To distinguish between full and light userdata,
2028you can use \verb|lua_islightuserdata|. 2033you can use \verb|lua_islightuserdata|.
2029To distinguish between numbers and numerical strings, 2034To distinguish between numbers and numerical strings,
2030you can use \verb|lua_type|. 2035you can use \verb|lua_type|.
@@ -2090,7 +2095,7 @@ you can use \verb|lua_strlen| to get its actual length.
2090Because Lua has garbage collection, 2095Because Lua has garbage collection,
2091there is no guarantee that the pointer returned by \verb|lua_tostring| 2096there is no guarantee that the pointer returned by \verb|lua_tostring|
2092will be valid after the corresponding value is removed from the stack. 2097will be valid after the corresponding value is removed from the stack.
2093So, if you need the string after the current function returns, 2098If you need the string after the current function returns,
2094then you should duplicate it (or put it into the registry \see{registry}). 2099then you should duplicate it (or put it into the registry \see{registry}).
2095 2100
2096\verb|lua_tocfunction| converts a value in the stack to a C~function. 2101\verb|lua_tocfunction| converts a value in the stack to a C~function.
@@ -2158,7 +2163,7 @@ The conversion specifiers can be simply
2158 2163
2159Lua uses two numbers to control its garbage collection: 2164Lua uses two numbers to control its garbage collection:
2160the \emph{count} and the \emph{threshold} \see{GC}. 2165the \emph{count} and the \emph{threshold} \see{GC}.
2161The first counts the ammount of memory in use by Lua; 2166The first counts the amount of memory in use by Lua;
2162when the count reaches the threshold, 2167when the count reaches the threshold,
2163Lua runs its garbage collector. 2168Lua runs its garbage collector.
2164After the collection, the count is updated, 2169After the collection, the count is updated,
@@ -2183,7 +2188,7 @@ Lua sets the new threshold and checks it against the byte counter.
2183If the new threshold is smaller than the byte counter, 2188If the new threshold is smaller than the byte counter,
2184then Lua immediately runs the garbage collector. 2189then Lua immediately runs the garbage collector.
2185In particular 2190In particular
2186\verb|lua_setgcthreshold(L,0)| forces a garbage collectiion. 2191\verb|lua_setgcthreshold(L,0)| forces a garbage collection.
2187After the collection, 2192After the collection,
2188a new threshold is set according to the previous rule. 2193a new threshold is set according to the previous rule.
2189 2194
@@ -2203,8 +2208,8 @@ Lua supports two types of userdata:
2203A full userdata represents a block of memory. 2208A full userdata represents a block of memory.
2204It is an object (like a table): 2209It is an object (like a table):
2205You must create it, it can have its own metatable, 2210You must create it, it can have its own metatable,
2206you can detect when it is being collected. 2211and you can detect when it is being collected.
2207A full userdata is only equal to itself. 2212A full userdata is only equal to itself (under raw equality).
2208 2213
2209A light userdata represents a pointer. 2214A light userdata represents a pointer.
2210It is a value (like a number): 2215It is a value (like a number):
@@ -2305,7 +2310,7 @@ to ensure that, it always receives \verb|NULL| as the Lua state.
2305The \emph{chunkname} is used for error messages 2310The \emph{chunkname} is used for error messages
2306and debug information \see{debugI}. 2311and debug information \see{debugI}.
2307 2312
2308See the auxiliar library (\verb|lauxlib|) 2313See the auxiliary library (\verb|lauxlib|)
2309for examples of how to use \verb|lua_load|, 2314for examples of how to use \verb|lua_load|,
2310and for some ready-to-use functions to load chunks 2315and for some ready-to-use functions to load chunks
2311from files and from strings. 2316from files and from strings.
@@ -2457,7 +2462,7 @@ The function results are pushed onto the stack in direct order
2457so that after the call the last result is on the top. 2462so that after the call the last result is on the top.
2458 2463
2459The following example shows how the host program may do the 2464The following example shows how the host program may do the
2460equivalent to the Lua code: 2465equivalent to this Lua code:
2461\begin{verbatim} 2466\begin{verbatim}
2462 a = f("how", t.x, 14) 2467 a = f("how", t.x, 14)
2463\end{verbatim} 2468\end{verbatim}
@@ -2482,7 +2487,7 @@ This is considered good programming practice.
2482 2487
2483(We did this example using only the raw functions provided by Lua's API, 2488(We did this example using only the raw functions provided by Lua's API,
2484to show all the details. 2489to show all the details.
2485Usually programmers use several macros and auxiliar functions that 2490Usually programmers use several macros and auxiliary functions that
2486provide higher level access to Lua.) 2491provide higher level access to Lua.)
2487 2492
2488 2493
@@ -2591,6 +2596,8 @@ its first argument (if any) is at index 1.
2591To return values to Lua, a C~function just pushes them onto the stack, 2596To return values to Lua, a C~function just pushes them onto the stack,
2592in direct order (the first result is pushed first), 2597in direct order (the first result is pushed first),
2593and returns the number of results. 2598and returns the number of results.
2599Any other value in the stack below the results will be properly
2600discharged by Lua.
2594Like a Lua function, a C~function called by Lua can also return 2601Like a Lua function, a C~function called by Lua can also return
2595many results. 2602many results.
2596 2603
@@ -2637,10 +2644,10 @@ by calling
2637\subsection{Defining C Closures} \label{c-closure} 2644\subsection{Defining C Closures} \label{c-closure}
2638 2645
2639When a C~function is created, 2646When a C~function is created,
2640it is possible to associate some values to it, 2647it is possible to associate some values with it,
2641thus creating a \IndexEmph{C~closure}; 2648thus creating a \IndexEmph{C~closure};
2642these values are then accessible to the function whenever it is called. 2649these values are then accessible to the function whenever it is called.
2643To associate values to a C~function, 2650To associate values with a C~function,
2644first these values should be pushed onto the stack 2651first these values should be pushed onto the stack
2645(when there are multiple values, the first value is pushed first). 2652(when there are multiple values, the first value is pushed first).
2646Then the function 2653Then the function
@@ -2680,7 +2687,7 @@ Typically, you should use as key a string containing your library name,
2680or a light userdata with the address of a C object in your code. 2687or a light userdata with the address of a C object in your code.
2681 2688
2682The integer keys in the registry are used by the reference mechanism, 2689The integer keys in the registry are used by the reference mechanism,
2683implemented by the auxiliar library, 2690implemented by the auxiliary library,
2684and therefore should not be used by other purposes. 2691and therefore should not be used by other purposes.
2685 2692
2686 2693
@@ -2688,9 +2695,9 @@ and therefore should not be used by other purposes.
2688\section{The Debug Interface} \label{debugI} 2695\section{The Debug Interface} \label{debugI}
2689 2696
2690Lua has no built-in debugging facilities. 2697Lua has no built-in debugging facilities.
2691Instead, it offers a special interface, 2698Instead, it offers a special interface
2692by means of functions and \emph{hooks}, 2699by means of functions and \emph{hooks}.
2693which allows the construction of different 2700This interface allows the construction of different
2694kinds of debuggers, profilers, and other tools 2701kinds of debuggers, profilers, and other tools
2695that need ``inside information'' from the interpreter. 2702that need ``inside information'' from the interpreter.
2696 2703
@@ -2825,16 +2832,15 @@ local variables of a given activation record:
2825 const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n); 2832 const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n);
2826\end{verbatim} 2833\end{verbatim}
2827\DefAPI{lua_getlocal}\DefAPI{lua_setlocal} 2834\DefAPI{lua_getlocal}\DefAPI{lua_setlocal}
2828The parameter \verb|ar| must be a valid activation record, 2835The parameter \verb|ar| must be a valid activation record that was
2829filled by a previous call to \verb|lua_getstack| or 2836filled by a previous call to \verb|lua_getstack| or
2830given as argument to a hook \see{sub-hooks}. 2837was given as argument to a hook \see{sub-hooks}.
2831\verb|lua_getlocal| gets the index \verb|n| of a local variable, 2838\verb|lua_getlocal| gets the index \verb|n| of a local variable,
2832pushes its value onto the stack, 2839pushes the variable's value onto the stack,
2833and returns its name. 2840and returns its name.
2834\verb|lua_setlocal| assigns the value at the top of the stack 2841\verb|lua_setlocal| assigns the value at the top of the stack
2835to the variable and returns its name. 2842to the variable and returns its name.
2836Both functions return \verb|NULL| on failure, 2843Both functions return \verb|NULL|
2837that is
2838when the index is greater than 2844when the index is greater than
2839the number of active local variables. 2845the number of active local variables.
2840 2846
@@ -2858,16 +2864,18 @@ local variables for a function at a given level of the stack:
2858 2864
2859\subsection{Hooks}\label{sub-hooks} 2865\subsection{Hooks}\label{sub-hooks}
2860 2866
2861The Lua interpreter offers a mechanism of hooks: 2867The Lua interpreter offers a mechanism of hooks, which are
2862user-defined C functions that are called during the program execution. 2868user-defined C functions that are called during the program execution.
2863A hook may be called in four different events: 2869A hook may be called in four different events:
2864a \emph{call} event, when Lua calls a function; 2870a \emph{call} event, when Lua calls a function;
2865a \emph{return} event, when Lua returns from a function; 2871a \emph{return} event, when Lua returns from a function;
2866a \emph{line} event, when Lua starts executing a new line of code; 2872a \emph{line} event, when Lua starts executing a new line of code;
2867and a \emph{count} event, that happens every ``count'' instructions. 2873and a \emph{count} event, which happens every ``count'' instructions.
2868Lua identifies them with the following constants: 2874Lua identifies them with the following constants:
2869\DefAPI{LUA_HOOKCALL}, \DefAPI{LUA_HOOKRET}, 2875\verb|LUA_HOOKCALL|\DefAPI{LUA_HOOKCALL},
2870\DefAPI{LUA_HOOKLINE}, and \DefAPI{LUA_HOOKCOUNT}. 2876\verb|LUA_HOOKRET|\DefAPI{LUA_HOOKRET},
2877\verb|LUA_HOOKLINE|\DefAPI{LUA_HOOKLINE},
2878and \verb|LUA_HOOKCOUNT|\DefAPI{LUA_HOOKCOUNT}.
2871 2879
2872A hook has type \verb|lua_Hook|, defined as follows: 2880A hook has type \verb|lua_Hook|, defined as follows:
2873\begin{verbatim} 2881\begin{verbatim}
@@ -2888,18 +2896,18 @@ It is formed by a disjunction of the constants
2888plus the macro \verb|LUA_MASKCOUNT(count)|. 2896plus the macro \verb|LUA_MASKCOUNT(count)|.
2889For each event, the hook is called as explained below: 2897For each event, the hook is called as explained below:
2890\begin{description} 2898\begin{description}
2891\item{call hook} called when the interpreter calls a function. 2899\item{The call hook} is called when the interpreter calls a function.
2892The hook is called just after Lua ``enters'' the new function. 2900The hook is called just after Lua ``enters'' the new function.
2893\item{return hook} called when the interpreter returns from a function. 2901\item{The return hook} is called when the interpreter returns from a function.
2894The hook is called just before Lua ``leaves'' the function. 2902The hook is called just before Lua ``leaves'' the function.
2895\item{line hook} called when the interpreter is about to 2903\item{The line hook} is called when the interpreter is about to
2896start the execution of a new line of code, 2904start the execution of a new line of code,
2897or when it jumps back (even for the same line). 2905or when it jumps back (even for the same line).
2898(For obvious reasons, this event does not happens while Lua is executing 2906(For obvious reasons, this event does not happen while Lua is executing
2899a C function.) 2907a C function.)
2900\item{count hook} called after the interpreter executes every 2908\item{The count hook} is called after the interpreter executes every
2901\verb|count| instructions. 2909\verb|count| instructions.
2902(For obvious reasons, this event does not happens while Lua is executing 2910(For obvious reasons, this event does not happen while Lua is executing
2903a C function.) 2911a C function.)
2904\end{description} 2912\end{description}
2905 2913
@@ -2920,7 +2928,7 @@ For the value of any other field, the hook must call \verb|lua_getinfo|.
2920 2928
2921While Lua is running a hook, it disables other calls to hooks. 2929While Lua is running a hook, it disables other calls to hooks.
2922Therefore, if a hook calls Lua to execute a function or a chunk, 2930Therefore, if a hook calls Lua to execute a function or a chunk,
2923that execution ocurrs without any calls to hooks. 2931that execution occurs without any calls to hooks.
2924 2932
2925 2933
2926%------------------------------------------------------------------------------ 2934%------------------------------------------------------------------------------
@@ -2930,7 +2938,7 @@ The standard libraries provide useful functions
2930that are implemented directly through the standard C~API. 2938that are implemented directly through the standard C~API.
2931Some of these functions provide essential services to the language 2939Some of these functions provide essential services to the language
2932(e.g. \verb|type| and \verb|getmetatable|); 2940(e.g. \verb|type| and \verb|getmetatable|);
2933others provide access to ``outside'' servides (e.g. I/O); 2941others provide access to ``outside'' services (e.g. I/O);
2934and others could be implemented in Lua itself, 2942and others could be implemented in Lua itself,
2935but are quite useful or have critical performance to 2943but are quite useful or have critical performance to
2936deserve an implementation in C (e.g. \verb|sort|). 2944deserve an implementation in C (e.g. \verb|sort|).
@@ -2995,7 +3003,7 @@ This function is equivalent to the following Lua function:
2995 3003
2996\subsubsection*{\ff \T{collectgarbage ([limit])}}\DefLIB{collectgarbage} 3004\subsubsection*{\ff \T{collectgarbage ([limit])}}\DefLIB{collectgarbage}
2997 3005
2998Sets the garbage-collection threshold for the given limit 3006Sets the garbage-collection threshold to the given limit
2999(in Kbytes), and checks it against the byte counter. 3007(in Kbytes), and checks it against the byte counter.
3000If the new threshold is smaller than the byte counter, 3008If the new threshold is smaller than the byte counter,
3001then Lua immediately runs the garbage collector \see{GC}. 3009then Lua immediately runs the garbage collector \see{GC}.
@@ -3010,12 +3018,12 @@ When called without arguments,
3010Returns any value returned by the chunk. 3018Returns any value returned by the chunk.
3011 3019
3012\subsubsection*{\ff \T{error (message [, level])}} 3020\subsubsection*{\ff \T{error (message [, level])}}
3013DefLIB{error}\label{pdf-error} 3021\DefLIB{error}\label{pdf-error}
3014Terminates the last protected function called, 3022Terminates the last protected function called,
3015and returns \verb|message| as the error message. 3023and returns \verb|message| as the error message.
3016Function \verb|error| never returns. 3024Function \verb|error| never returns.
3017 3025
3018The \verb|level| argument affects to where the error 3026The \verb|level| argument specifies where the error
3019message points the error. 3027message points the error.
3020With level 1 (the default), the error position is where the 3028With level 1 (the default), the error position is where the
3021\verb|error| function was called. 3029\verb|error| function was called.
@@ -3025,7 +3033,7 @@ that called \verb|error| was called; and so on.
3025\subsubsection*{\ff \T{getglobals (function)}}\DefLIB{getglobals} 3033\subsubsection*{\ff \T{getglobals (function)}}\DefLIB{getglobals}
3026Returns the current table of globals in use by the function. 3034Returns the current table of globals in use by the function.
3027\verb|function| can be a Lua function or a number, 3035\verb|function| can be a Lua function or a number,
3028meaning the function at that stack level: 3036which specifies the function at that stack level:
3029Level 1 is the function calling \verb|getglobals|. 3037Level 1 is the function calling \verb|getglobals|.
3030If the given function is not a Lua function, 3038If the given function is not a Lua function,
3031returns the ``global'' table of globals. 3039returns the ``global'' table of globals.
@@ -3054,19 +3062,19 @@ up to the first nil value of the table.
3054 3062
3055\subsubsection*{\ff \T{loadfile (filename)}}\DefLIB{loadfile} 3063\subsubsection*{\ff \T{loadfile (filename)}}\DefLIB{loadfile}
3056Loads a file as a Lua chunk. 3064Loads a file as a Lua chunk.
3057If there is no errors, 3065If there are no errors,
3058returns the compiled chunk as a function; 3066returns the compiled chunk as a function;
3059otherwise, returns \nil{} plus an error message. 3067otherwise, returns \nil{} plus an error message.
3060 3068
3061\subsubsection*{\ff \T{loadstring (string [, chunkname])}}\DefLIB{loadstring} 3069\subsubsection*{\ff \T{loadstring (string [, chunkname])}}\DefLIB{loadstring}
3062Loads a string as a Lua chunk. 3070Loads a string as a Lua chunk.
3063If there is no errors, 3071If there are no errors,
3064returns the compiled chunk as a function; 3072returns the compiled chunk as a function;
3065otherwise, returns \nil{} plus an error message. 3073otherwise, returns \nil{} plus an error message.
3066 3074
3067The optional parameter \verb|chunkname| 3075The optional parameter \verb|chunkname|
3068is the ``name of the chunk'', 3076is the ``name of the chunk'',
3069used in error messages and debug information. 3077which is used in error messages and debug information.
3070 3078
3071To load and run a given string, use the idiom 3079To load and run a given string, use the idiom
3072\begin{verbatim} 3080\begin{verbatim}
@@ -3092,16 +3100,16 @@ semantically, there is no difference between a
3092field not present in a table or a field with value \nil. 3100field not present in a table or a field with value \nil.
3093Therefore, \verb|next| only considers fields with non-\nil{} values. 3101Therefore, \verb|next| only considers fields with non-\nil{} values.
3094The order in which the indices are enumerated is not specified, 3102The order in which the indices are enumerated is not specified,
3095\emph{even for numeric indices} 3103\emph{even for numeric indices}.
3096(to traverse a table in numeric order, 3104(To traverse a table in numeric order,
3097use a numerical \rwd{for} or the function \verb|ipairs|). 3105use a numerical \rwd{for} or the function \verb|ipairs|.)
3098 3106
3099The behavior of \verb|next| is \emph{undefined} if you change 3107The behavior of \verb|next| is \emph{undefined} if you modify
3100the table during the traversal. 3108the table during the traversal.
3101 3109
3102\subsubsection*{\ff \T{pairs (t)}}\DefLIB{pairs} 3110\subsubsection*{\ff \T{pairs (t)}}\DefLIB{pairs}
3103 3111
3104Returns the function \verb|next| and the table \verb|t|, 3112Returns the function \verb|next| and the table \verb|t| (plus a \nil),
3105so that the construction 3113so that the construction
3106\begin{verbatim} 3114\begin{verbatim}
3107 for k,v in pairs(t) do ... end 3115 for k,v in pairs(t) do ... end
@@ -3113,10 +3121,10 @@ will iterate over all pairs of key--value of table \verb|t|.
3113Calls function \verb|func| with 3121Calls function \verb|func| with
3114the given arguments in protected mode. 3122the given arguments in protected mode.
3115That means that any error inside \verb|func| is not propagated; 3123That means that any error inside \verb|func| is not propagated;
3116instead, \verb|pcall| catches the error, 3124instead, \verb|pcall| catches the error
3117returning a status code. 3125and returns a status code.
3118Its first result is the status code (a boolean), 3126Its first result is the status code (a boolean),
3119true if the call succeeds without errors. 3127which is true if the call succeeds without errors.
3120In such case, \verb|pcall| also returns all results from the call, 3128In such case, \verb|pcall| also returns all results from the call,
3121after this first result. 3129after this first result.
3122In case of any error, \verb|pcall| returns false plus the error message. 3130In case of any error, \verb|pcall| returns false plus the error message.
@@ -3147,14 +3155,14 @@ and \verb|value| is any Lua value.
3147 3155
3148Loads the given package. 3156Loads the given package.
3149The function starts by looking into the table \IndexVerb{_LOADED} 3157The function starts by looking into the table \IndexVerb{_LOADED}
3150whether \verb|packagename| is already loaded. 3158to determine whether \verb|packagename| is already loaded.
3151If it is, then \verb|require| is done. 3159If it is, then \verb|require| is done.
3152Otherwise, it searches a path looking for a file to load. 3160Otherwise, it searches a path looking for a file to load.
3153 3161
3154If the global variable \IndexVerb{LUA_PATH} is a string, 3162If the global variable \IndexVerb{LUA_PATH} is a string,
3155this string is the path. 3163this string is the path.
3156Otherwise, \verb|require| tries the environment variable \verb|LUA_PATH|. 3164Otherwise, \verb|require| tries the environment variable \verb|LUA_PATH|.
3157In the last resort, it uses a predefined path. 3165As a last resort, it uses a predefined path.
3158 3166
3159The path is a sequence of \emph{templates} separated by semicolons. 3167The path is a sequence of \emph{templates} separated by semicolons.
3160For each template, \verb|require| will change an eventual interrogation 3168For each template, \verb|require| will change an eventual interrogation
@@ -3185,7 +3193,7 @@ with the package name.
3185\subsubsection*{\ff \T{setglobals (function, table)}}\DefLIB{setglobals} 3193\subsubsection*{\ff \T{setglobals (function, table)}}\DefLIB{setglobals}
3186Sets the current table of globals to be used by the given function. 3194Sets the current table of globals to be used by the given function.
3187\verb|function| can be a Lua function or a number, 3195\verb|function| can be a Lua function or a number,
3188meaning the function at that stack level: 3196which specifies the function at that stack level:
3189Level 1 is the function calling \verb|setglobals|. 3197Level 1 is the function calling \verb|setglobals|.
3190 3198
3191\subsubsection*{\ff \T{setmetatable (table, metatable)}}\DefLIB{setmetatable} 3199\subsubsection*{\ff \T{setmetatable (table, metatable)}}\DefLIB{setmetatable}
@@ -3286,7 +3294,7 @@ Note that if \verb|plain| is given, then \verb|init| must be given too.
3286Receives a string and returns its length. 3294Receives a string and returns its length.
3287The empty string \verb|""| has length 0. 3295The empty string \verb|""| has length 0.
3288Embedded zeros are counted, 3296Embedded zeros are counted,
3289and so \verb|"a\000b\000c"| has length 5. 3297so \verb|"a\000b\000c"| has length 5.
3290 3298
3291\subsubsection*{\ff \T{string.lower (s)}}\DefLIB{string.lower} 3299\subsubsection*{\ff \T{string.lower (s)}}\DefLIB{string.lower}
3292Receives a string and returns a copy of that string with all 3300Receives a string and returns a copy of that string with all
@@ -3299,8 +3307,8 @@ Returns a string that is the concatenation of \verb|n| copies of
3299the string \verb|s|. 3307the string \verb|s|.
3300 3308
3301\subsubsection*{\ff \T{string.sub (s, i [, j])}}\DefLIB{string.sub} 3309\subsubsection*{\ff \T{string.sub (s, i [, j])}}\DefLIB{string.sub}
3302Returns another string, which is a substring of \verb|s|, 3310Returns another string, which is the substring of \verb|s| that
3303starting at \verb|i| and running until \verb|j|; 3311starts at \verb|i| and continues until \verb|j|;
3304\verb|i| and \verb|j| may be negative. 3312\verb|i| and \verb|j| may be negative.
3305If \verb|j| is absent, then it is assumed to be equal to \Math{-1} 3313If \verb|j| is absent, then it is assumed to be equal to \Math{-1}
3306(which is the same as the string length). 3314(which is the same as the string length).
@@ -3371,7 +3379,7 @@ As an example, the following loop
3371 end 3379 end
3372\end{verbatim} 3380\end{verbatim}
3373will iterate over all the words from string \verb|s|, 3381will iterate over all the words from string \verb|s|,
3374printing each one in a line. 3382printing one per line.
3375The next example collects all pairs \verb|key=value| from the 3383The next example collects all pairs \verb|key=value| from the
3376given string into a table: 3384given string into a table:
3377\begin{verbatim} 3385\begin{verbatim}
@@ -3561,10 +3569,10 @@ A pattern cannot contain embedded zeros. Use \verb|%z| instead.
3561 3569
3562 3570
3563\subsection{Table Manipulation} 3571\subsection{Table Manipulation}
3564This library provides generic functions for table manipulation, 3572This library provides generic functions for table manipulation.
3565It provides all its functions inside the table \IndexLIB{table}. 3573It provides all its functions inside the table \IndexLIB{table}.
3566 3574
3567Most functions in the table library library assume that the table 3575Most functions in the table library assume that the table
3568represents an array or a list. 3576represents an array or a list.
3569For those functions, an important concept is the \emph{size} of the array. 3577For those functions, an important concept is the \emph{size} of the array.
3570There are three ways to specify that size: 3578There are three ways to specify that size:
@@ -3655,7 +3663,7 @@ The default value for \verb|pos| is \verb|n+1|,
3655where \verb|n| is the size of the table \see{getn}, 3663where \verb|n| is the size of the table \see{getn},
3656so that a call \verb|table.insert(t,x)| inserts \verb|x| at the end 3664so that a call \verb|table.insert(t,x)| inserts \verb|x| at the end
3657of table \verb|t|. 3665of table \verb|t|.
3658This function also updates the size of the table, 3666This function also updates the size of the table by
3659calling \verb|table.setn(table, n+1)|. 3667calling \verb|table.setn(table, n+1)|.
3660 3668
3661\subsubsection*{\ff \T{table.remove (table [, pos])}}\DefLIB{table.remove} 3669\subsubsection*{\ff \T{table.remove (table [, pos])}}\DefLIB{table.remove}
@@ -3667,7 +3675,7 @@ The default value for \verb|pos| is \verb|n|,
3667where \verb|n| is the size of the table \see{getn}, 3675where \verb|n| is the size of the table \see{getn},
3668so that a call \verb|tremove(t)| removes the last element 3676so that a call \verb|tremove(t)| removes the last element
3669of table \verb|t|. 3677of table \verb|t|.
3670This function also updates the size of the table, 3678This function also updates the size of the table by
3671calling \verb|table.setn(table, n-1)|. 3679calling \verb|table.setn(table, n-1)|.
3672 3680
3673\subsubsection*{\ff \T{table.setn (table, n)}}\DefLIB{table.setn} 3681\subsubsection*{\ff \T{table.setn (table, n)}}\DefLIB{table.setn}
@@ -3681,9 +3689,10 @@ so that subsequent calls to \verb|table.getn(table)| return \verb|n|.
3681 3689
3682\subsection{Mathematical Functions} \label{mathlib} 3690\subsection{Mathematical Functions} \label{mathlib}
3683 3691
3684This library is an interface to most functions of the standard C~math library. 3692This library is an interface to most of the functions of the
3693standard C~math library.
3685(Some have slightly different names.) 3694(Some have slightly different names.)
3686It provides all its functions inside the table \DefLIB{math}. 3695It provides all its functions inside the table \verb|math|\DefLIB{math}.
3687In addition, 3696In addition,
3688it registers a ??tag method for the binary exponentiation operator \verb|^| 3697it registers a ??tag method for the binary exponentiation operator \verb|^|
3689that returns \Math{x^y} when applied to numbers \verb|x^y|. 3698that returns \Math{x^y} when applied to numbers \verb|x^y|.
@@ -3707,7 +3716,7 @@ The library provides the following functions:
3707\end{verbatim} 3716\end{verbatim}
3708plus a variable \IndexLIB{math.pi}. 3717plus a variable \IndexLIB{math.pi}.
3709Most of them 3718Most of them
3710are only interfaces to the homonymous functions in the C~library. 3719are only interfaces to the corresponding functions in the C~library.
3711All trigonometric functions work in radians. 3720All trigonometric functions work in radians.
3712The functions \verb|math.deg| and \verb|math.rad| convert 3721The functions \verb|math.deg| and \verb|math.rad| convert
3713between radians and degrees. 3722between radians and degrees.
@@ -3719,7 +3728,7 @@ Both can be used with 1, 2, or more arguments.
3719 3728
3720The functions \verb|math.random| and \verb|math.randomseed| 3729The functions \verb|math.random| and \verb|math.randomseed|
3721are interfaces to the simple random generator functions 3730are interfaces to the simple random generator functions
3722\verb|rand| and \verb|srand|, provided by ANSI~C. 3731\verb|rand| and \verb|srand| that are provided by ANSI~C.
3723(No guarantees can be given for their statistical properties.) 3732(No guarantees can be given for their statistical properties.)
3724When called without arguments, 3733When called without arguments,
3725\verb|math.random| returns a pseudo-random real number 3734\verb|math.random| returns a pseudo-random real number
@@ -3748,10 +3757,9 @@ When using explicit file descriptors,
3748the operation \IndexLIB{io.open} returns a file descriptor, 3757the operation \IndexLIB{io.open} returns a file descriptor,
3749and then all operations are supplied as methods by the file descriptor. 3758and then all operations are supplied as methods by the file descriptor.
3750 3759
3751Moreover, the table \verb|io| also provides 3760The table \verb|io| also provides
3752three predefined file descriptors: 3761three predefined file descriptors with their usual meanings from C:
3753\IndexLIB{io.stdin}, \IndexLIB{io.stdout}, and \IndexLIB{io.stderr}, 3762\IndexLIB{io.stdin}, \IndexLIB{io.stdout}, and \IndexLIB{io.stderr}.
3754with their usual meaning from C.
3755 3763
3756A file handle is a userdata containing the file stream (\verb|FILE*|), 3764A file handle is a userdata containing the file stream (\verb|FILE*|),
3757with a distinctive metatable created by the I/O library. 3765with a distinctive metatable created by the I/O library.
@@ -3761,10 +3769,10 @@ all I/O functions return \nil{} on failure
3761(plus an error message as a second result) 3769(plus an error message as a second result)
3762and some value different from \nil{} on success. 3770and some value different from \nil{} on success.
3763 3771
3764\subsubsection*{\ff \T{io.close ([handle])}}\DefLIB{io.close} 3772\subsubsection*{\ff \T{io.close ([file])}}\DefLIB{io.close}
3765 3773
3766Equivalent to \verb|file:close()|. 3774Equivalent to \verb|file:close()|.
3767Without a \verb|handle|, closes the default output file. 3775Without a \verb|file|, closes the default output file.
3768 3776
3769\subsubsection*{\ff \T{io.flush ()}}\DefLIB{io.flush} 3777\subsubsection*{\ff \T{io.flush ()}}\DefLIB{io.flush}
3770 3778
@@ -3791,7 +3799,7 @@ each time it is called,
3791returns a new line from the file. 3799returns a new line from the file.
3792Therefore, the construction 3800Therefore, the construction
3793\begin{verbatim} 3801\begin{verbatim}
3794 for lines in io.lines(filename) do ... end 3802 for line in io.lines(filename) do ... end
3795\end{verbatim} 3803\end{verbatim}
3796will iterate over all lines of the file. 3804will iterate over all lines of the file.
3797When the generator function detects the end of file, 3805When the generator function detects the end of file,
@@ -3801,7 +3809,7 @@ The call \verb|io.lines()| (without a file name) is equivalent
3801to \verb|io.input():lines()|, that is, it iterates over the 3809to \verb|io.input():lines()|, that is, it iterates over the
3802lines of the default input file. 3810lines of the default input file.
3803 3811
3804\subsubsection*{\ff \T{io.open (filename, mode)}}\DefLIB{io.open} 3812\subsubsection*{\ff \T{io.open (filename [, mode])}}\DefLIB{io.open}
3805 3813
3806This function opens a file, 3814This function opens a file,
3807in the mode specified in the string \verb|mode|. 3815in the mode specified in the string \verb|mode|.
@@ -3810,7 +3818,7 @@ or, in case of errors, \nil{} plus an error message.
3810 3818
3811The \verb|mode| string can be any of the following: 3819The \verb|mode| string can be any of the following:
3812\begin{description}\leftskip=20pt 3820\begin{description}\leftskip=20pt
3813\item[``r''] read mode; 3821\item[``r''] read mode (the default);
3814\item[``w''] write mode; 3822\item[``w''] write mode;
3815\item[``a''] append mode; 3823\item[``a''] append mode;
3816\item[``r+''] update mode, all previous data is preserved; 3824\item[``r+''] update mode, all previous data is preserved;
@@ -3857,7 +3865,7 @@ each time it is called,
3857returns a new line from the file. 3865returns a new line from the file.
3858Therefore, the construction 3866Therefore, the construction
3859\begin{verbatim} 3867\begin{verbatim}
3860 for lines in file:lines() do ... end 3868 for line in file:lines() do ... end
3861\end{verbatim} 3869\end{verbatim}
3862will iterate over all lines of the file. 3870will iterate over all lines of the file.
3863(Unlike \verb|io.lines|, this function does not close the file 3871(Unlike \verb|io.lines|, this function does not close the file
@@ -3965,11 +3973,11 @@ When called without arguments,
3965the host system and on the current locale 3973the host system and on the current locale
3966(that is, \verb|os.date()| is equivalent to \verb|os.date("%c")|). 3974(that is, \verb|os.date()| is equivalent to \verb|os.date("%c")|).
3967 3975
3968\subsubsection*{\ff \T{os.difftime (t1, t2)}}\DefLIB{os.difftime} 3976\subsubsection*{\ff \T{os.difftime (t2, t1)}}\DefLIB{os.difftime}
3969 3977
3970Returns the number of seconds from time \verb|t1| to time \verb|t2|. 3978Returns the number of seconds from time \verb|t1| to time \verb|t2|.
3971In Posix, Windows, and some other systems, 3979In Posix, Windows, and some other systems,
3972this value is exactly \verb|t1|\Math{-}\verb|t2|. 3980this value is exactly \verb|t2|\Math{-}\verb|t1|.
3973 3981
3974\subsubsection*{\ff \T{os.execute (command)}}\DefLIB{os.execute} 3982\subsubsection*{\ff \T{os.execute (command)}}\DefLIB{os.execute}
3975 3983
@@ -4024,7 +4032,7 @@ The returned value is a number, whose meaning depends on your system.
4024In Posix, Windows, and some other systems, this number counts the number 4032In Posix, Windows, and some other systems, this number counts the number
4025of seconds since some given start time (the ``epoch''). 4033of seconds since some given start time (the ``epoch'').
4026In other systems, the meaning is not specified, 4034In other systems, the meaning is not specified,
4027and the number returned bt \verb|time| can be used only as an argument to 4035and the number returned by \verb|time| can be used only as an argument to
4028\verb|date| and \verb|difftime|. 4036\verb|date| and \verb|difftime|.
4029 4037
4030\subsubsection*{\ff \T{os.tmpname ()}}\DefLIB{os.tmpname} 4038\subsubsection*{\ff \T{os.tmpname ()}}\DefLIB{os.tmpname}
@@ -4036,7 +4044,7 @@ and removed when no longer needed.
4036 4044
4037This function is equivalent to the \verb|tmpnam| C~function, 4045This function is equivalent to the \verb|tmpnam| C~function,
4038and many people (and even some compilers!) advise against its use, 4046and many people (and even some compilers!) advise against its use,
4039because between the time you call the function 4047because between the time you call this function
4040and the time you open the file, 4048and the time you open the file,
4041it is possible for another process 4049it is possible for another process
4042to create a file with the same name. 4050to create a file with the same name.
@@ -4073,15 +4081,15 @@ If \verb|function| is a number larger than the number of active functions,
4073then \verb|getinfo| returns \nil. 4081then \verb|getinfo| returns \nil.
4074 4082
4075The returned table contains all the fields returned by \verb|lua_getinfo|, 4083The returned table contains all the fields returned by \verb|lua_getinfo|,
4076with the string \verb|what| describing what to get. 4084with the string \verb|what| describing which fields to fill in.
4077The default for \verb|what| is to get all information available. 4085The default for \verb|what| is to get all information available.
4078If present, 4086If present,
4079the option \verb|f| 4087the option \verb|f|
4080adds a field named \verb|func| with the function itself. 4088adds a field named \verb|func| with the function itself.
4081 4089
4082For instance, the expression \verb|getinfo(1,"n").name| returns 4090For instance, the expression \verb|debug.getinfo(1,"n").name| returns
4083the name of the current function, if a reasonable name can be found, 4091the name of the current function, if a reasonable name can be found,
4084and \verb|getinfo(print)| returns a table with all available information 4092and \verb|debug.getinfo(print)| returns a table with all available information
4085about the \verb|print| function. 4093about the \verb|print| function.
4086 4094
4087 4095
@@ -4174,7 +4182,7 @@ When called without arguments,
4174and as \verb|lua -| otherwise. 4182and as \verb|lua -| otherwise.
4175 4183
4176Before running any argument, 4184Before running any argument,
4177the intepreter checks for an environment variable \IndexVerb{LUA_INIT}. 4185the interpreter checks for an environment variable \IndexVerb{LUA_INIT}.
4178If its format is \verb|@|\emph{filename}, 4186If its format is \verb|@|\emph{filename},
4179then lua executes the file. 4187then lua executes the file.
4180Otherwise, lua executes the string itself. 4188Otherwise, lua executes the string itself.
@@ -4235,11 +4243,11 @@ as in
4235(Of course, 4243(Of course,
4236the location of the Lua interpreter may be different in your machine. 4244the location of the Lua interpreter may be different in your machine.
4237If \verb|lua| is in your \verb|PATH|, 4245If \verb|lua| is in your \verb|PATH|,
4238then a more portable solution is 4246then
4239\begin{verbatim} 4247\begin{verbatim}
4240#!/usr/bin/env lua 4248#!/usr/bin/env lua
4241\end{verbatim} 4249\end{verbatim}
4242 4250is a more portable solution.)
4243 4251
4244%------------------------------------------------------------------------------ 4252%------------------------------------------------------------------------------
4245\section*{Acknowledgments} 4253\section*{Acknowledgments}
@@ -4342,7 +4350,7 @@ for unprotected calls,
4342or the new \verb|pcall| function for protected calls. 4350or the new \verb|pcall| function for protected calls.
4343 4351
4344\item 4352\item
4345\verb|dofile| do not handle errors, but simply propagate them. 4353\verb|dofile| do not handle errors, but simply propagates them.
4346 4354
4347\item 4355\item
4348The \verb|read| option \verb|*w| is obsolete. 4356The \verb|read| option \verb|*w| is obsolete.