diff options
| -rw-r--r-- | manual.tex | 286 |
1 files changed, 147 insertions, 139 deletions
| @@ -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 | |||
| 273 | persists until a call to \verb|lua_close| | 273 | persists until a call to \verb|lua_close| |
| 274 | or the end of the embedding program. | 274 | or the end of the embedding program. |
| 275 | The host program can create multiple independent global | 275 | The host program can create multiple independent global |
| 276 | environments, and freely switch between them \see{mangstate}. | 276 | environments, and freely switch among them \see{mangstate}. |
| 277 | 277 | ||
| 278 | The unit of execution of Lua is called a \Def{chunk}. | 278 | The unit of execution of Lua is called a \Def{chunk}. |
| 279 | A chunk is simply a sequence of statements. | 279 | A chunk is simply a sequence of statements. |
| @@ -284,7 +284,7 @@ When a chunk is executed, first it is pre-compiled into opcodes for | |||
| 284 | a virtual machine, | 284 | a virtual machine, |
| 285 | and then the compiled statements are executed | 285 | and then the compiled statements are executed |
| 286 | by an interpreter for the virtual machine. | 286 | by an interpreter for the virtual machine. |
| 287 | All modifications a chunk makes to the global environment persist | 287 | All modifications that a chunk makes to the global environment persist |
| 288 | after the chunk ends. | 288 | after the chunk ends. |
| 289 | 289 | ||
| 290 | Chunks may also be pre-compiled into binary form and stored in files; | 290 | Chunks 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. | |||
| 316 | In Lua, both \nil{} and \False{} make a condition false, | 316 | In Lua, both \nil{} and \False{} make a condition false, |
| 317 | and any other value makes it true. | 317 | and 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 | ||
| 320 | internal representations for numbers, | ||
| 321 | such 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} |
| 321 | Lua is 8-bit clean, | 324 | Lua is 8-bit clean, |
| 322 | and so strings may contain any 8-bit character, | 325 | so strings may contain any 8-bit character, |
| 323 | including embedded zeros (\verb|'\0'|) \see{lexical}. | 326 | including embedded zeros (\verb|'\0'|) \see{lexical}. |
| 324 | 327 | ||
| 325 | Functions are \emph{first-class values} in Lua. | 328 | Functions are \emph{first-class values} in Lua. |
| @@ -351,9 +354,9 @@ that is, \Index{arrays} that can be indexed not only with numbers, | |||
| 351 | but with any value (except \nil). | 354 | but with any value (except \nil). |
| 352 | Moreover, | 355 | Moreover, |
| 353 | tables can be \emph{heterogeneous}, | 356 | tables can be \emph{heterogeneous}, |
| 354 | that is, they can contain values of all types. | 357 | that is, they can contain values of all types (except \nil). |
| 355 | Tables are the sole data structuring mechanism in Lua; | 358 | Tables are the sole data structuring mechanism in Lua; |
| 356 | they may be used not only to represent ordinary arrays, | 359 | they may be used to represent not only ordinary arrays, |
| 357 | but also symbol tables, sets, records, graphs, trees, etc. | 360 | but also symbol tables, sets, records, graphs, trees, etc. |
| 358 | To represent \Index{records}, Lua uses the field name as an index. | 361 | To represent \Index{records}, Lua uses the field name as an index. |
| 359 | The language supports this representation by | 362 | The language supports this representation by |
| @@ -365,7 +368,7 @@ Like indices, the value of a table field can be of any type. | |||
| 365 | In particular, | 368 | In particular, |
| 366 | because functions are first class values, | 369 | because functions are first class values, |
| 367 | table fields may contain functions. | 370 | table fields may contain functions. |
| 368 | So, tables may also carry \emph{methods} \see{func-def}. | 371 | Thus tables may also carry \emph{methods} \see{func-def}. |
| 369 | 372 | ||
| 370 | Tables, functions, and userdata values are \emph{objects}: | 373 | Tables, functions, and userdata values are \emph{objects}: |
| 371 | variables do not actually \emph{contain} these values, | 374 | variables do not actually \emph{contain} these values, |
| @@ -413,7 +416,7 @@ Lua provides automatic conversion between | |||
| 413 | string and number values at run time. | 416 | string and number values at run time. |
| 414 | Any arithmetic operation applied to a string tries to convert | 417 | Any arithmetic operation applied to a string tries to convert |
| 415 | that string to a number, following the usual rules. | 418 | that string to a number, following the usual rules. |
| 416 | Conversely, whenever a number is used when a string is expected, | 419 | Conversely, whenever a number is used where a string is expected, |
| 417 | the number is converted to a string, in a reasonable format. | 420 | the number is converted to a string, in a reasonable format. |
| 418 | The format is chosen so that | 421 | The format is chosen so that |
| 419 | a conversion from number to string then back to number | 422 | a conversion from number to string then back to number |
| @@ -429,7 +432,7 @@ global variables | |||
| 429 | and local variables. | 432 | and local variables. |
| 430 | Variables are assumed to be global unless explicitly declared local | 433 | Variables are assumed to be global unless explicitly declared local |
| 431 | \see{localvar}. | 434 | \see{localvar}. |
| 432 | Before the first assignment, the value of a variable is \nil. | 435 | Before the first assignment to a variable, its value is \nil. |
| 433 | 436 | ||
| 434 | All global variables live as fields in ordinary Lua tables. | 437 | All global variables live as fields in ordinary Lua tables. |
| 435 | Usually, globals live in a table called \Index{table of globals}. | 438 | Usually, globals live in a table called \Index{table of globals}. |
| @@ -473,8 +476,8 @@ and the other is a threshold. | |||
| 473 | When the number of bytes crosses the threshold, | 476 | When the number of bytes crosses the threshold, |
| 474 | Lua runs the garbage collector, | 477 | Lua runs the garbage collector, |
| 475 | which reclaims the memory of all dead objects. | 478 | which reclaims the memory of all dead objects. |
| 476 | The byte counter is corrected, | 479 | The byte counter is adjusted, |
| 477 | and then the threshold is reset to twice the value of the byte counter. | 480 | and then the threshold is reset to twice the new value of the byte counter. |
| 478 | 481 | ||
| 479 | Through the C~API, you can query those numbers, | 482 | Through the C~API, you can query those numbers, |
| 480 | and change the threshold \see{GC-API}. | 483 | and change the threshold \see{GC-API}. |
| @@ -502,9 +505,9 @@ A table with both weak keys and weak values allows the collection of | |||
| 502 | both keys and values. | 505 | both keys and values. |
| 503 | In any case, if either the key or the value is collected, | 506 | In any case, if either the key or the value is collected, |
| 504 | the whole pair is removed from the table. | 507 | the whole pair is removed from the table. |
| 505 | The weakness of a table is controled by the value of the | 508 | The 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. |
| 507 | If the \verb|__mode| field is a string containing the \verb|k| character, | 510 | If the \verb|__mode| field is a string containing the character \verb|k|, |
| 508 | the keys in the table are weak. | 511 | the keys in the table are weak. |
| 509 | If \verb|__mode| contains \verb|v|, | 512 | If \verb|__mode| contains \verb|v|, |
| 510 | the values in the table are weak. | 513 | the 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. |
| 547 | As a convention, identifiers starting with an underscore followed by | 550 | As a convention, identifiers starting with an underscore followed by |
| 548 | uppercase letters (such as \verb|_VERSION|) | 551 | uppercase letters (such as \verb|_VERSION|) |
| 549 | are reserved for internal variables. | 552 | are reserved for internal variables used by Lua. |
| 550 | 553 | ||
| 551 | The following strings denote other \Index{tokens}: | 554 | The 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), |
| 572 | and `\verb|\|\emph{newline}' (that is, a backslash followed by a real newline, | 575 | and `\verb|\|\emph{newline}' (that is, a backslash followed by a real newline, |
| 573 | which results in a newline in the string). | 576 | which results in a newline in the string). |
| 574 | A character in a string may also be specified by its numerical value, | 577 | A character in a string may also be specified by its numerical value |
| 575 | through the escape sequence `\verb|\|\emph{ddd}', | 578 | using the escape sequence `\verb|\|\emph{ddd}', |
| 576 | where \emph{ddd} is a sequence of up to three \emph{decimal} digits. | 579 | where \emph{ddd} is a sequence of up to three \emph{decimal} digits. |
| 577 | Strings in Lua may contain any 8-bit value, including embedded zeros, | 580 | Strings in Lua may contain any 8-bit value, including embedded zeros, |
| 578 | which can be specified as `\verb|\0|'. | 581 | which 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 | ||
| 650 | The expression denoting the table to be indexed has a restricted syntax; | 653 | The expression denoting the table to be indexed has a restricted syntax; |
| 651 | \See{expressions} for details. | 654 | see \See{expressions} for details. |
| 652 | 655 | ||
| 653 | The meaning of assignments and evaluations of global and | 656 | The meaning of assignments and evaluations of global and |
| 654 | indexed variables can be changed via metatables. | 657 | indexed variables can be changed via metatables. |
| @@ -656,17 +659,17 @@ An assignment to a global variable \verb|x = val| | |||
| 656 | is equivalent to the assignment | 659 | is equivalent to the assignment |
| 657 | \verb|_glob.x = val|, | 660 | \verb|_glob.x = val|, |
| 658 | where \verb|_glob| is the table of globals of the running function | 661 | where \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). |
| 660 | An assignment to an indexed variable \verb|t[i] = val| is equivalent to | 663 | An 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)|. |
| 662 | An access to a global variable \verb|x| | 665 | An access to a global variable \verb|x| |
| 663 | is equivalent to \verb|_glob.x| | 666 | is 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|). |
| 665 | An access to an indexed variable \verb|t[i]| is equivalent to | 668 | An access to an indexed variable \verb|t[i]| is equivalent to |
| 666 | a call \verb|gettable_event(t,i)|. | 669 | a call \verb|gettable_event(t,i)|. |
| 667 | See \See{metatable} for a complete description of the | 670 | See \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. |
| 670 | We use them here only for explanatory purposes.) | 673 | We 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 | |||
| 724 | the list of variables.\index{adjustment} | 727 | the list of variables.\index{adjustment} |
| 725 | If there are more values than needed, | 728 | If there are more values than needed, |
| 726 | the excess values are thrown away. | 729 | the excess values are thrown away. |
| 727 | If there are less values than needed, | 730 | If there are fewer values than needed, |
| 728 | the list is extended with as many \nil's as needed. | 731 | the list is extended with as many \nil's as needed. |
| 729 | If the list of expressions ends with a function call, | 732 | If the list of expressions ends with a function call, |
| 730 | then all values returned by that function call enter in the list of values, | 733 | then all values returned by that function call enter in the list of values, |
| 731 | before the adjust | 734 | before 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 | ||
| 734 | The assignment statement first evaluates all its expressions, | 737 | The assignment statement first evaluates all its expressions, |
| 735 | and only then makes the assignments. | 738 | and only then are the assignments performed. |
| 736 | So, the code | 739 | Thus 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 | ||
| 766 | The \Index{condition expression} \M{exp} of a | 769 | The \Index{condition expression} \M{exp} of a |
| 767 | control structure may return any value. | 770 | control structure may return any value. |
| 771 | Both \False{} and \nil{} are considered false. | ||
| 768 | All values different from \nil{} and \False{} are considered true | 772 | All 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). |
| 770 | both \False{} and \nil{} are considered false. | ||
| 771 | 774 | ||
| 772 | The \rwd{return} statement is used to return values | 775 | The \rwd{return} statement is used to return values |
| 773 | from a function or from a chunk.\IndexKW{return} | 776 | from a function or from a chunk.\IndexKW{return} |
| 774 | \label{return}% | 777 | \label{return}% |
| 775 | \index{return statement}% | 778 | \index{return statement}% |
| 776 | Functions and chunks may return more than one value, | 779 | Functions and chunks may return more than one value, |
| 777 | and so the syntax for the \rwd{return} statement is | 780 | so 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 | ||
| 782 | The \rwd{break} statement can be used to terminate the execution of a | 785 | The \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, |
| 784 | skipping to the next statement after the loop:\IndexKW{break} | 787 | and 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 | ||
| 809 | The \rwd{for} statement has two forms, | 812 | The \rwd{for} statement has two forms, |
| 810 | one for numbers and one generic. | 813 | one numeric and one generic. |
| 811 | \IndexKW{for}\IndexKW{in} | 814 | \IndexKW{for}\IndexKW{in} |
| 812 | 815 | ||
| 813 | The numerical \rwd{for} loop repeats a block of code while a | 816 | The numeric \rwd{for} loop repeats a block of code while a |
| 814 | control variable runs through an arithmetic progression. | 817 | control variable runs through an arithmetic progression. |
| 815 | It has the following syntax: | 818 | It 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}% |
| 820 | The \emph{block} is repeated for \emph{name} starting at the value of | 823 | The \emph{block} is repeated for \emph{name} starting at the value of |
| 821 | the first \emph{exp}, until it reaches the second \emph{exp} by steps of the | 824 | the first \emph{exp}, until it passes the second \emph{exp} by steps of the |
| 822 | third \emph{exp}. | 825 | third \emph{exp}. |
| 823 | More precisely, a \rwd{for} statement like | 826 | More 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. |
| 881 | Its results are a ``generator'' function, | 884 | Its results are a ``generator'' function, |
| 882 | a ``state'', and an initial value for the ``iterator variable''. | 885 | a ``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. |
| 884 | The names are here for explanatory purposes only. | 887 | The 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}. | |||
| 913 | Otherwise, all variables are initialized with \nil. | 916 | Otherwise, all variables are initialized with \nil. |
| 914 | 917 | ||
| 915 | A chunk is also a block \see{chunks}, | 918 | A chunk is also a block \see{chunks}, |
| 916 | and so local variables can be declared outside any explicit block. | 919 | so local variables can be declared outside any explicit block. |
| 917 | Such local variables die when the chunk ends. | 920 | Such local variables die when the chunk ends. |
| 918 | 921 | ||
| 919 | Visibility rules for local variables are explained in \See{visibility}. | 922 | Visibility rules for local variables are explained in \See{visibility}. |
| @@ -948,7 +951,7 @@ function calls are explained in \See{functioncall}; | |||
| 948 | table constructors are explained in \See{tableconstructor}. | 951 | table constructors are explained in \See{tableconstructor}. |
| 949 | 952 | ||
| 950 | Expressions can also be built with arithmetic operators, relational operators, | 953 | Expressions can also be built with arithmetic operators, relational operators, |
| 951 | and logical operadors, all of which are explained below. | 954 | and logical operators, all of which are explained below. |
| 952 | 955 | ||
| 953 | \subsubsection{Arithmetic Operators} | 956 | \subsubsection{Arithmetic Operators} |
| 954 | Lua supports the usual \Index{arithmetic operators}: | 957 | Lua supports the usual \Index{arithmetic operators}: |
| @@ -1019,7 +1022,7 @@ The conjunction operator \rwd{and} returns its first argument | |||
| 1019 | if its value is \False{} or \nil; | 1022 | if its value is \False{} or \nil; |
| 1020 | otherwise, \rwd{and} returns its second argument. | 1023 | otherwise, \rwd{and} returns its second argument. |
| 1021 | The disjunction operator \rwd{or} returns its first argument | 1024 | The disjunction operator \rwd{or} returns its first argument |
| 1022 | if it is different from \nil and \False; | 1025 | if it is different from \nil{} and \False; |
| 1023 | otherwise, \rwd{or} returns its second argument. | 1026 | otherwise, \rwd{or} returns its second argument. |
| 1024 | Both \rwd{and} and \rwd{or} use \Index{short-cut evaluation}, | 1027 | Both \rwd{and} and \rwd{or} use \Index{short-cut evaluation}, |
| 1025 | that is, | 1028 | that is, |
| @@ -1249,7 +1252,7 @@ Different instances of the same function | |||
| 1249 | may refer to different non-local variables \see{visibility} | 1252 | may refer to different non-local variables \see{visibility} |
| 1250 | and may have different tables of globals \see{global-table}. | 1253 | and may have different tables of globals \see{global-table}. |
| 1251 | 1254 | ||
| 1252 | Parameters act as local variables, | 1255 | Parameters act as local variables that are |
| 1253 | initialized with the argument values: | 1256 | initialized 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, | |||
| 1267 | called \IndexLIB{arg}. | 1270 | called \IndexLIB{arg}. |
| 1268 | The value of \verb|arg| is a table, | 1271 | The value of \verb|arg| is a table, |
| 1269 | with a field~\verb|n| whose value is the number of extra arguments, | 1272 | with a field~\verb|n| whose value is the number of extra arguments, |
| 1270 | and the extra arguments at positions 1,~2,~\ldots,~\verb|n|. | 1273 | and with the extra arguments at positions 1,~2,~\ldots,~\verb|n|. |
| 1271 | 1274 | ||
| 1272 | As an example, consider the following definitions: | 1275 | As 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|, | |||
| 1335 | the new \verb|x| being declared is not in scope yet, | 1338 | the new \verb|x| being declared is not in scope yet, |
| 1336 | so the second \verb|x| refers to the ``outside'' variable. | 1339 | so the second \verb|x| refers to the ``outside'' variable. |
| 1337 | 1340 | ||
| 1338 | Because of those \Index{lexical scoping} rules, | 1341 | Because of these \Index{lexical scoping} rules, |
| 1339 | local variables can be freely accessed by functions | 1342 | local variables can be freely accessed by functions |
| 1340 | defined inside their scope. | 1343 | defined inside their scope. |
| 1341 | For instance: | 1344 | For 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} |
| 1361 | In that code, | 1364 | The loop creates ten closures |
| 1362 | each function uses a different \verb|y| variable, | 1365 | (that is, instances of the anonymous function). |
| 1366 | Each of these closures uses a different \verb|y| variable, | ||
| 1363 | while all of them share the same \verb|x|. | 1367 | while 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 | ||
| 1383 | Every table and userdata value in Lua may have a \emph{metatable}. | 1387 | Every table and userdata value in Lua may have a \emph{metatable}. |
| 1384 | This \IndexEmph{metatable} is a table that defines the behavior of | 1388 | This \IndexEmph{metatable} is a table that defines the behavior of |
| 1385 | the original table and userdata under some operations. | 1389 | the original table and userdata under certain special operations. |
| 1386 | You can query and change the metatable of an object with | 1390 | You can query and change the metatable of an object with |
| 1387 | functions \verb|setmetatable| and \verb|getmetatable| \see{pdf-getmetatable}. | 1391 | functions \verb|setmetatable| and \verb|getmetatable| \see{pdf-getmetatable}. |
| 1388 | 1392 | ||
| 1389 | For each of those operations Lua associates a specific key, | 1393 | For each of those operations Lua associates a specific key |
| 1390 | called an \emph{event}. | 1394 | called an \emph{event}. |
| 1391 | When Lua performs one of those operations over a table or a userdata, | 1395 | When Lua performs one of those operations over a table or a userdata, |
| 1392 | if checks whether that object has a metatable with the corresponding event. | 1396 | if 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 | ||
| 1652 | Lua supports coroutines, | 1656 | Lua supports coroutines, |
| 1653 | also called \emph{semi-coroutines}, \emph{generators}, | 1657 | also called \emph{semi-coroutines} |
| 1654 | or \emph{colaborative multithreading}. | 1658 | or \emph{collaborative multithreading}. |
| 1655 | A coroutine in Lua represents an independent thread of execution. | 1659 | A coroutine in Lua represents an independent thread of execution. |
| 1656 | Unlike ``real'' threads, however, | 1660 | Unlike ``real'' threads, however, |
| 1657 | a coroutine only suspends its execution by explicitly calling | 1661 | a coroutine only suspends its execution by explicitly calling |
| 1658 | an yield function. | 1662 | an yield function. |
| 1659 | 1663 | ||
| 1660 | You create a coroutine with a call to \IndexVerb{coroutine.create}. | 1664 | You create a coroutine with a call to \IndexVerb{coroutine.create}. |
| 1661 | Its sole argument is a function, | 1665 | Its sole argument is a function |
| 1662 | which is the main function of the coroutine. | 1666 | that is the main function of the coroutine. |
| 1663 | The \verb|coroutine.create| only creates a new coroutine and | 1667 | The \verb|coroutine.create| only creates a new coroutine and |
| 1664 | returns a handle to it (an object of type \emph{thread}). | 1668 | returns a handle to it (an object of type \emph{thread}). |
| 1665 | It does not start the coroutine execution. | 1669 | It does not start the coroutine execution. |
| @@ -1679,10 +1683,10 @@ Normally, when its main function returns | |||
| 1679 | and abnormally, if there is an unprotected error. | 1683 | and abnormally, if there is an unprotected error. |
| 1680 | In the first case, \verb|coroutine.resume| returns \True, | 1684 | In the first case, \verb|coroutine.resume| returns \True, |
| 1681 | plus any values returned by the coroutine main function. | 1685 | plus any values returned by the coroutine main function. |
| 1682 | In case of errors, \verb|coroutine.resume| returns \False | 1686 | In case of errors, \verb|coroutine.resume| returns \False{} |
| 1683 | plus an error message. | 1687 | plus an error message. |
| 1684 | 1688 | ||
| 1685 | A coroutine yields calling \IndexVerb{coroutine.yield}. | 1689 | A coroutine yields by calling \IndexVerb{coroutine.yield}. |
| 1686 | When a coroutine yields, | 1690 | When a coroutine yields, |
| 1687 | the corresponding \verb|coroutine.resume| returns immediately, | 1691 | the corresponding \verb|coroutine.resume| returns immediately, |
| 1688 | even if the yield happens inside nested function calls | 1692 | even if the yield happens inside nested function calls |
| @@ -1818,7 +1822,7 @@ The following function creates a new ``thread'' in Lua: | |||
| 1818 | The new state returned by this function shares with the original state | 1822 | The new state returned by this function shares with the original state |
| 1819 | all global environment (such as tables), | 1823 | all global environment (such as tables), |
| 1820 | but has an independent run-time stack. | 1824 | but 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. |
| 1822 | How to explain that? TO BE WRITTEN.) | 1826 | How to explain that? TO BE WRITTEN.) |
| 1823 | 1827 | ||
| 1824 | Each thread has an independent table for global variables. | 1828 | Each thread has an independent table for global variables. |
| @@ -1841,9 +1845,9 @@ Each element in this stack represents a Lua value | |||
| 1841 | 1845 | ||
| 1842 | Each C invocation has its own stack. | 1846 | Each C invocation has its own stack. |
| 1843 | Whenever Lua calls C, the called function gets a new stack, | 1847 | Whenever Lua calls C, the called function gets a new stack, |
| 1844 | which is independent of previous stacks or of stacks of still | 1848 | which is independent of previous stacks and of stacks of still |
| 1845 | active C functions. | 1849 | active C functions. |
| 1846 | That stack contains initially any arguments to the C function, | 1850 | That stack initially contains any arguments to the C function, |
| 1847 | and it is where the C function pushes its results \see{LuacallC}. | 1851 | and it is where the C function pushes its results \see{LuacallC}. |
| 1848 | 1852 | ||
| 1849 | For convenience, | 1853 | For convenience, |
| @@ -1906,7 +1910,7 @@ as follows: | |||
| 1906 | \end{verbatim} | 1910 | \end{verbatim} |
| 1907 | Note that 0 is never an acceptable index. | 1911 | Note that 0 is never an acceptable index. |
| 1908 | 1912 | ||
| 1909 | Unless otherwise noticed, | 1913 | Unless otherwise noted, |
| 1910 | any function that accepts valid indices can also be called with | 1914 | any function that accepts valid indices can also be called with |
| 1911 | \Index{pseudo-indices}, | 1915 | \Index{pseudo-indices}, |
| 1912 | which represent some Lua values that are accessible to the C~code | 1916 | which 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|. |
| 2008 | The following function translates such constants to a type name: | 2013 | The 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, |
| 2024 | and \verb|lua_isuserdata| accepts both full and ligth userdata. | 2029 | and \verb|lua_isuserdata| accepts both full and light userdata. |
| 2025 | To distinguish between Lua functions and C~functions, | 2030 | To distinguish between Lua functions and C~functions, |
| 2026 | you should use \verb|lua_iscfunction|. | 2031 | you should use \verb|lua_iscfunction|. |
| 2027 | To distinguish between full and ligth userdata, | 2032 | To distinguish between full and light userdata, |
| 2028 | you can use \verb|lua_islightuserdata|. | 2033 | you can use \verb|lua_islightuserdata|. |
| 2029 | To distinguish between numbers and numerical strings, | 2034 | To distinguish between numbers and numerical strings, |
| 2030 | you can use \verb|lua_type|. | 2035 | you can use \verb|lua_type|. |
| @@ -2090,7 +2095,7 @@ you can use \verb|lua_strlen| to get its actual length. | |||
| 2090 | Because Lua has garbage collection, | 2095 | Because Lua has garbage collection, |
| 2091 | there is no guarantee that the pointer returned by \verb|lua_tostring| | 2096 | there is no guarantee that the pointer returned by \verb|lua_tostring| |
| 2092 | will be valid after the corresponding value is removed from the stack. | 2097 | will be valid after the corresponding value is removed from the stack. |
| 2093 | So, if you need the string after the current function returns, | 2098 | If you need the string after the current function returns, |
| 2094 | then you should duplicate it (or put it into the registry \see{registry}). | 2099 | then 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 | ||
| 2159 | Lua uses two numbers to control its garbage collection: | 2164 | Lua uses two numbers to control its garbage collection: |
| 2160 | the \emph{count} and the \emph{threshold} \see{GC}. | 2165 | the \emph{count} and the \emph{threshold} \see{GC}. |
| 2161 | The first counts the ammount of memory in use by Lua; | 2166 | The first counts the amount of memory in use by Lua; |
| 2162 | when the count reaches the threshold, | 2167 | when the count reaches the threshold, |
| 2163 | Lua runs its garbage collector. | 2168 | Lua runs its garbage collector. |
| 2164 | After the collection, the count is updated, | 2169 | After the collection, the count is updated, |
| @@ -2183,7 +2188,7 @@ Lua sets the new threshold and checks it against the byte counter. | |||
| 2183 | If the new threshold is smaller than the byte counter, | 2188 | If the new threshold is smaller than the byte counter, |
| 2184 | then Lua immediately runs the garbage collector. | 2189 | then Lua immediately runs the garbage collector. |
| 2185 | In particular | 2190 | In particular |
| 2186 | \verb|lua_setgcthreshold(L,0)| forces a garbage collectiion. | 2191 | \verb|lua_setgcthreshold(L,0)| forces a garbage collection. |
| 2187 | After the collection, | 2192 | After the collection, |
| 2188 | a new threshold is set according to the previous rule. | 2193 | a new threshold is set according to the previous rule. |
| 2189 | 2194 | ||
| @@ -2203,8 +2208,8 @@ Lua supports two types of userdata: | |||
| 2203 | A full userdata represents a block of memory. | 2208 | A full userdata represents a block of memory. |
| 2204 | It is an object (like a table): | 2209 | It is an object (like a table): |
| 2205 | You must create it, it can have its own metatable, | 2210 | You must create it, it can have its own metatable, |
| 2206 | you can detect when it is being collected. | 2211 | and you can detect when it is being collected. |
| 2207 | A full userdata is only equal to itself. | 2212 | A full userdata is only equal to itself (under raw equality). |
| 2208 | 2213 | ||
| 2209 | A light userdata represents a pointer. | 2214 | A light userdata represents a pointer. |
| 2210 | It is a value (like a number): | 2215 | It is a value (like a number): |
| @@ -2305,7 +2310,7 @@ to ensure that, it always receives \verb|NULL| as the Lua state. | |||
| 2305 | The \emph{chunkname} is used for error messages | 2310 | The \emph{chunkname} is used for error messages |
| 2306 | and debug information \see{debugI}. | 2311 | and debug information \see{debugI}. |
| 2307 | 2312 | ||
| 2308 | See the auxiliar library (\verb|lauxlib|) | 2313 | See the auxiliary library (\verb|lauxlib|) |
| 2309 | for examples of how to use \verb|lua_load|, | 2314 | for examples of how to use \verb|lua_load|, |
| 2310 | and for some ready-to-use functions to load chunks | 2315 | and for some ready-to-use functions to load chunks |
| 2311 | from files and from strings. | 2316 | from files and from strings. |
| @@ -2457,7 +2462,7 @@ The function results are pushed onto the stack in direct order | |||
| 2457 | so that after the call the last result is on the top. | 2462 | so that after the call the last result is on the top. |
| 2458 | 2463 | ||
| 2459 | The following example shows how the host program may do the | 2464 | The following example shows how the host program may do the |
| 2460 | equivalent to the Lua code: | 2465 | equivalent 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, |
| 2484 | to show all the details. | 2489 | to show all the details. |
| 2485 | Usually programmers use several macros and auxiliar functions that | 2490 | Usually programmers use several macros and auxiliary functions that |
| 2486 | provide higher level access to Lua.) | 2491 | provide higher level access to Lua.) |
| 2487 | 2492 | ||
| 2488 | 2493 | ||
| @@ -2591,6 +2596,8 @@ its first argument (if any) is at index 1. | |||
| 2591 | To return values to Lua, a C~function just pushes them onto the stack, | 2596 | To return values to Lua, a C~function just pushes them onto the stack, |
| 2592 | in direct order (the first result is pushed first), | 2597 | in direct order (the first result is pushed first), |
| 2593 | and returns the number of results. | 2598 | and returns the number of results. |
| 2599 | Any other value in the stack below the results will be properly | ||
| 2600 | discharged by Lua. | ||
| 2594 | Like a Lua function, a C~function called by Lua can also return | 2601 | Like a Lua function, a C~function called by Lua can also return |
| 2595 | many results. | 2602 | many 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 | ||
| 2639 | When a C~function is created, | 2646 | When a C~function is created, |
| 2640 | it is possible to associate some values to it, | 2647 | it is possible to associate some values with it, |
| 2641 | thus creating a \IndexEmph{C~closure}; | 2648 | thus creating a \IndexEmph{C~closure}; |
| 2642 | these values are then accessible to the function whenever it is called. | 2649 | these values are then accessible to the function whenever it is called. |
| 2643 | To associate values to a C~function, | 2650 | To associate values with a C~function, |
| 2644 | first these values should be pushed onto the stack | 2651 | first 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). |
| 2646 | Then the function | 2653 | Then the function |
| @@ -2680,7 +2687,7 @@ Typically, you should use as key a string containing your library name, | |||
| 2680 | or a light userdata with the address of a C object in your code. | 2687 | or a light userdata with the address of a C object in your code. |
| 2681 | 2688 | ||
| 2682 | The integer keys in the registry are used by the reference mechanism, | 2689 | The integer keys in the registry are used by the reference mechanism, |
| 2683 | implemented by the auxiliar library, | 2690 | implemented by the auxiliary library, |
| 2684 | and therefore should not be used by other purposes. | 2691 | and 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 | ||
| 2690 | Lua has no built-in debugging facilities. | 2697 | Lua has no built-in debugging facilities. |
| 2691 | Instead, it offers a special interface, | 2698 | Instead, it offers a special interface |
| 2692 | by means of functions and \emph{hooks}, | 2699 | by means of functions and \emph{hooks}. |
| 2693 | which allows the construction of different | 2700 | This interface allows the construction of different |
| 2694 | kinds of debuggers, profilers, and other tools | 2701 | kinds of debuggers, profilers, and other tools |
| 2695 | that need ``inside information'' from the interpreter. | 2702 | that 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} |
| 2828 | The parameter \verb|ar| must be a valid activation record, | 2835 | The parameter \verb|ar| must be a valid activation record that was |
| 2829 | filled by a previous call to \verb|lua_getstack| or | 2836 | filled by a previous call to \verb|lua_getstack| or |
| 2830 | given as argument to a hook \see{sub-hooks}. | 2837 | was 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, |
| 2832 | pushes its value onto the stack, | 2839 | pushes the variable's value onto the stack, |
| 2833 | and returns its name. | 2840 | and 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 |
| 2835 | to the variable and returns its name. | 2842 | to the variable and returns its name. |
| 2836 | Both functions return \verb|NULL| on failure, | 2843 | Both functions return \verb|NULL| |
| 2837 | that is | ||
| 2838 | when the index is greater than | 2844 | when the index is greater than |
| 2839 | the number of active local variables. | 2845 | the 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 | ||
| 2861 | The Lua interpreter offers a mechanism of hooks: | 2867 | The Lua interpreter offers a mechanism of hooks, which are |
| 2862 | user-defined C functions that are called during the program execution. | 2868 | user-defined C functions that are called during the program execution. |
| 2863 | A hook may be called in four different events: | 2869 | A hook may be called in four different events: |
| 2864 | a \emph{call} event, when Lua calls a function; | 2870 | a \emph{call} event, when Lua calls a function; |
| 2865 | a \emph{return} event, when Lua returns from a function; | 2871 | a \emph{return} event, when Lua returns from a function; |
| 2866 | a \emph{line} event, when Lua starts executing a new line of code; | 2872 | a \emph{line} event, when Lua starts executing a new line of code; |
| 2867 | and a \emph{count} event, that happens every ``count'' instructions. | 2873 | and a \emph{count} event, which happens every ``count'' instructions. |
| 2868 | Lua identifies them with the following constants: | 2874 | Lua 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}, | ||
| 2878 | and \verb|LUA_HOOKCOUNT|\DefAPI{LUA_HOOKCOUNT}. | ||
| 2871 | 2879 | ||
| 2872 | A hook has type \verb|lua_Hook|, defined as follows: | 2880 | A 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 | |||
| 2888 | plus the macro \verb|LUA_MASKCOUNT(count)|. | 2896 | plus the macro \verb|LUA_MASKCOUNT(count)|. |
| 2889 | For each event, the hook is called as explained below: | 2897 | For 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. |
| 2892 | The hook is called just after Lua ``enters'' the new function. | 2900 | The 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. |
| 2894 | The hook is called just before Lua ``leaves'' the function. | 2902 | The 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 |
| 2896 | start the execution of a new line of code, | 2904 | start the execution of a new line of code, |
| 2897 | or when it jumps back (even for the same line). | 2905 | or 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 |
| 2899 | a C function.) | 2907 | a 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 |
| 2903 | a C function.) | 2911 | a 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 | ||
| 2921 | While Lua is running a hook, it disables other calls to hooks. | 2929 | While Lua is running a hook, it disables other calls to hooks. |
| 2922 | Therefore, if a hook calls Lua to execute a function or a chunk, | 2930 | Therefore, if a hook calls Lua to execute a function or a chunk, |
| 2923 | that execution ocurrs without any calls to hooks. | 2931 | that execution occurs without any calls to hooks. |
| 2924 | 2932 | ||
| 2925 | 2933 | ||
| 2926 | %------------------------------------------------------------------------------ | 2934 | %------------------------------------------------------------------------------ |
| @@ -2930,7 +2938,7 @@ The standard libraries provide useful functions | |||
| 2930 | that are implemented directly through the standard C~API. | 2938 | that are implemented directly through the standard C~API. |
| 2931 | Some of these functions provide essential services to the language | 2939 | Some 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|); |
| 2933 | others provide access to ``outside'' servides (e.g. I/O); | 2941 | others provide access to ``outside'' services (e.g. I/O); |
| 2934 | and others could be implemented in Lua itself, | 2942 | and others could be implemented in Lua itself, |
| 2935 | but are quite useful or have critical performance to | 2943 | but are quite useful or have critical performance to |
| 2936 | deserve an implementation in C (e.g. \verb|sort|). | 2944 | deserve 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 | ||
| 2998 | Sets the garbage-collection threshold for the given limit | 3006 | Sets 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. |
| 3000 | If the new threshold is smaller than the byte counter, | 3008 | If the new threshold is smaller than the byte counter, |
| 3001 | then Lua immediately runs the garbage collector \see{GC}. | 3009 | then Lua immediately runs the garbage collector \see{GC}. |
| @@ -3010,12 +3018,12 @@ When called without arguments, | |||
| 3010 | Returns any value returned by the chunk. | 3018 | Returns any value returned by the chunk. |
| 3011 | 3019 | ||
| 3012 | \subsubsection*{\ff \T{error (message [, level])}} | 3020 | \subsubsection*{\ff \T{error (message [, level])}} |
| 3013 | DefLIB{error}\label{pdf-error} | 3021 | \DefLIB{error}\label{pdf-error} |
| 3014 | Terminates the last protected function called, | 3022 | Terminates the last protected function called, |
| 3015 | and returns \verb|message| as the error message. | 3023 | and returns \verb|message| as the error message. |
| 3016 | Function \verb|error| never returns. | 3024 | Function \verb|error| never returns. |
| 3017 | 3025 | ||
| 3018 | The \verb|level| argument affects to where the error | 3026 | The \verb|level| argument specifies where the error |
| 3019 | message points the error. | 3027 | message points the error. |
| 3020 | With level 1 (the default), the error position is where the | 3028 | With 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} |
| 3026 | Returns the current table of globals in use by the function. | 3034 | Returns 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, |
| 3028 | meaning the function at that stack level: | 3036 | which specifies the function at that stack level: |
| 3029 | Level 1 is the function calling \verb|getglobals|. | 3037 | Level 1 is the function calling \verb|getglobals|. |
| 3030 | If the given function is not a Lua function, | 3038 | If the given function is not a Lua function, |
| 3031 | returns the ``global'' table of globals. | 3039 | returns 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} |
| 3056 | Loads a file as a Lua chunk. | 3064 | Loads a file as a Lua chunk. |
| 3057 | If there is no errors, | 3065 | If there are no errors, |
| 3058 | returns the compiled chunk as a function; | 3066 | returns the compiled chunk as a function; |
| 3059 | otherwise, returns \nil{} plus an error message. | 3067 | otherwise, 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} |
| 3062 | Loads a string as a Lua chunk. | 3070 | Loads a string as a Lua chunk. |
| 3063 | If there is no errors, | 3071 | If there are no errors, |
| 3064 | returns the compiled chunk as a function; | 3072 | returns the compiled chunk as a function; |
| 3065 | otherwise, returns \nil{} plus an error message. | 3073 | otherwise, returns \nil{} plus an error message. |
| 3066 | 3074 | ||
| 3067 | The optional parameter \verb|chunkname| | 3075 | The optional parameter \verb|chunkname| |
| 3068 | is the ``name of the chunk'', | 3076 | is the ``name of the chunk'', |
| 3069 | used in error messages and debug information. | 3077 | which is used in error messages and debug information. |
| 3070 | 3078 | ||
| 3071 | To load and run a given string, use the idiom | 3079 | To 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 | |||
| 3092 | field not present in a table or a field with value \nil. | 3100 | field not present in a table or a field with value \nil. |
| 3093 | Therefore, \verb|next| only considers fields with non-\nil{} values. | 3101 | Therefore, \verb|next| only considers fields with non-\nil{} values. |
| 3094 | The order in which the indices are enumerated is not specified, | 3102 | The 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, |
| 3097 | use a numerical \rwd{for} or the function \verb|ipairs|). | 3105 | use a numerical \rwd{for} or the function \verb|ipairs|.) |
| 3098 | 3106 | ||
| 3099 | The behavior of \verb|next| is \emph{undefined} if you change | 3107 | The behavior of \verb|next| is \emph{undefined} if you modify |
| 3100 | the table during the traversal. | 3108 | the table during the traversal. |
| 3101 | 3109 | ||
| 3102 | \subsubsection*{\ff \T{pairs (t)}}\DefLIB{pairs} | 3110 | \subsubsection*{\ff \T{pairs (t)}}\DefLIB{pairs} |
| 3103 | 3111 | ||
| 3104 | Returns the function \verb|next| and the table \verb|t|, | 3112 | Returns the function \verb|next| and the table \verb|t| (plus a \nil), |
| 3105 | so that the construction | 3113 | so 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|. | |||
| 3113 | Calls function \verb|func| with | 3121 | Calls function \verb|func| with |
| 3114 | the given arguments in protected mode. | 3122 | the given arguments in protected mode. |
| 3115 | That means that any error inside \verb|func| is not propagated; | 3123 | That means that any error inside \verb|func| is not propagated; |
| 3116 | instead, \verb|pcall| catches the error, | 3124 | instead, \verb|pcall| catches the error |
| 3117 | returning a status code. | 3125 | and returns a status code. |
| 3118 | Its first result is the status code (a boolean), | 3126 | Its first result is the status code (a boolean), |
| 3119 | true if the call succeeds without errors. | 3127 | which is true if the call succeeds without errors. |
| 3120 | In such case, \verb|pcall| also returns all results from the call, | 3128 | In such case, \verb|pcall| also returns all results from the call, |
| 3121 | after this first result. | 3129 | after this first result. |
| 3122 | In case of any error, \verb|pcall| returns false plus the error message. | 3130 | In 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 | ||
| 3148 | Loads the given package. | 3156 | Loads the given package. |
| 3149 | The function starts by looking into the table \IndexVerb{_LOADED} | 3157 | The function starts by looking into the table \IndexVerb{_LOADED} |
| 3150 | whether \verb|packagename| is already loaded. | 3158 | to determine whether \verb|packagename| is already loaded. |
| 3151 | If it is, then \verb|require| is done. | 3159 | If it is, then \verb|require| is done. |
| 3152 | Otherwise, it searches a path looking for a file to load. | 3160 | Otherwise, it searches a path looking for a file to load. |
| 3153 | 3161 | ||
| 3154 | If the global variable \IndexVerb{LUA_PATH} is a string, | 3162 | If the global variable \IndexVerb{LUA_PATH} is a string, |
| 3155 | this string is the path. | 3163 | this string is the path. |
| 3156 | Otherwise, \verb|require| tries the environment variable \verb|LUA_PATH|. | 3164 | Otherwise, \verb|require| tries the environment variable \verb|LUA_PATH|. |
| 3157 | In the last resort, it uses a predefined path. | 3165 | As a last resort, it uses a predefined path. |
| 3158 | 3166 | ||
| 3159 | The path is a sequence of \emph{templates} separated by semicolons. | 3167 | The path is a sequence of \emph{templates} separated by semicolons. |
| 3160 | For each template, \verb|require| will change an eventual interrogation | 3168 | For 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} |
| 3186 | Sets the current table of globals to be used by the given function. | 3194 | Sets 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, |
| 3188 | meaning the function at that stack level: | 3196 | which specifies the function at that stack level: |
| 3189 | Level 1 is the function calling \verb|setglobals|. | 3197 | Level 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. | |||
| 3286 | Receives a string and returns its length. | 3294 | Receives a string and returns its length. |
| 3287 | The empty string \verb|""| has length 0. | 3295 | The empty string \verb|""| has length 0. |
| 3288 | Embedded zeros are counted, | 3296 | Embedded zeros are counted, |
| 3289 | and so \verb|"a\000b\000c"| has length 5. | 3297 | so \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} |
| 3292 | Receives a string and returns a copy of that string with all | 3300 | Receives 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 | |||
| 3299 | the string \verb|s|. | 3307 | the 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} |
| 3302 | Returns another string, which is a substring of \verb|s|, | 3310 | Returns another string, which is the substring of \verb|s| that |
| 3303 | starting at \verb|i| and running until \verb|j|; | 3311 | starts 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. |
| 3305 | If \verb|j| is absent, then it is assumed to be equal to \Math{-1} | 3313 | If \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} |
| 3373 | will iterate over all the words from string \verb|s|, | 3381 | will iterate over all the words from string \verb|s|, |
| 3374 | printing each one in a line. | 3382 | printing one per line. |
| 3375 | The next example collects all pairs \verb|key=value| from the | 3383 | The next example collects all pairs \verb|key=value| from the |
| 3376 | given string into a table: | 3384 | given 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} |
| 3564 | This library provides generic functions for table manipulation, | 3572 | This library provides generic functions for table manipulation. |
| 3565 | It provides all its functions inside the table \IndexLIB{table}. | 3573 | It provides all its functions inside the table \IndexLIB{table}. |
| 3566 | 3574 | ||
| 3567 | Most functions in the table library library assume that the table | 3575 | Most functions in the table library assume that the table |
| 3568 | represents an array or a list. | 3576 | represents an array or a list. |
| 3569 | For those functions, an important concept is the \emph{size} of the array. | 3577 | For those functions, an important concept is the \emph{size} of the array. |
| 3570 | There are three ways to specify that size: | 3578 | There are three ways to specify that size: |
| @@ -3655,7 +3663,7 @@ The default value for \verb|pos| is \verb|n+1|, | |||
| 3655 | where \verb|n| is the size of the table \see{getn}, | 3663 | where \verb|n| is the size of the table \see{getn}, |
| 3656 | so that a call \verb|table.insert(t,x)| inserts \verb|x| at the end | 3664 | so that a call \verb|table.insert(t,x)| inserts \verb|x| at the end |
| 3657 | of table \verb|t|. | 3665 | of table \verb|t|. |
| 3658 | This function also updates the size of the table, | 3666 | This function also updates the size of the table by |
| 3659 | calling \verb|table.setn(table, n+1)|. | 3667 | calling \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|, | |||
| 3667 | where \verb|n| is the size of the table \see{getn}, | 3675 | where \verb|n| is the size of the table \see{getn}, |
| 3668 | so that a call \verb|tremove(t)| removes the last element | 3676 | so that a call \verb|tremove(t)| removes the last element |
| 3669 | of table \verb|t|. | 3677 | of table \verb|t|. |
| 3670 | This function also updates the size of the table, | 3678 | This function also updates the size of the table by |
| 3671 | calling \verb|table.setn(table, n-1)|. | 3679 | calling \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 | ||
| 3684 | This library is an interface to most functions of the standard C~math library. | 3692 | This library is an interface to most of the functions of the |
| 3693 | standard C~math library. | ||
| 3685 | (Some have slightly different names.) | 3694 | (Some have slightly different names.) |
| 3686 | It provides all its functions inside the table \DefLIB{math}. | 3695 | It provides all its functions inside the table \verb|math|\DefLIB{math}. |
| 3687 | In addition, | 3696 | In addition, |
| 3688 | it registers a ??tag method for the binary exponentiation operator \verb|^| | 3697 | it registers a ??tag method for the binary exponentiation operator \verb|^| |
| 3689 | that returns \Math{x^y} when applied to numbers \verb|x^y|. | 3698 | that 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} |
| 3708 | plus a variable \IndexLIB{math.pi}. | 3717 | plus a variable \IndexLIB{math.pi}. |
| 3709 | Most of them | 3718 | Most of them |
| 3710 | are only interfaces to the homonymous functions in the C~library. | 3719 | are only interfaces to the corresponding functions in the C~library. |
| 3711 | All trigonometric functions work in radians. | 3720 | All trigonometric functions work in radians. |
| 3712 | The functions \verb|math.deg| and \verb|math.rad| convert | 3721 | The functions \verb|math.deg| and \verb|math.rad| convert |
| 3713 | between radians and degrees. | 3722 | between radians and degrees. |
| @@ -3719,7 +3728,7 @@ Both can be used with 1, 2, or more arguments. | |||
| 3719 | 3728 | ||
| 3720 | The functions \verb|math.random| and \verb|math.randomseed| | 3729 | The functions \verb|math.random| and \verb|math.randomseed| |
| 3721 | are interfaces to the simple random generator functions | 3730 | are 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.) |
| 3724 | When called without arguments, | 3733 | When 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, | |||
| 3748 | the operation \IndexLIB{io.open} returns a file descriptor, | 3757 | the operation \IndexLIB{io.open} returns a file descriptor, |
| 3749 | and then all operations are supplied as methods by the file descriptor. | 3758 | and then all operations are supplied as methods by the file descriptor. |
| 3750 | 3759 | ||
| 3751 | Moreover, the table \verb|io| also provides | 3760 | The table \verb|io| also provides |
| 3752 | three predefined file descriptors: | 3761 | three 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}. |
| 3754 | with their usual meaning from C. | ||
| 3755 | 3763 | ||
| 3756 | A file handle is a userdata containing the file stream (\verb|FILE*|), | 3764 | A file handle is a userdata containing the file stream (\verb|FILE*|), |
| 3757 | with a distinctive metatable created by the I/O library. | 3765 | with 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) |
| 3762 | and some value different from \nil{} on success. | 3770 | and 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 | ||
| 3766 | Equivalent to \verb|file:close()|. | 3774 | Equivalent to \verb|file:close()|. |
| 3767 | Without a \verb|handle|, closes the default output file. | 3775 | Without 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, | |||
| 3791 | returns a new line from the file. | 3799 | returns a new line from the file. |
| 3792 | Therefore, the construction | 3800 | Therefore, 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} |
| 3796 | will iterate over all lines of the file. | 3804 | will iterate over all lines of the file. |
| 3797 | When the generator function detects the end of file, | 3805 | When the generator function detects the end of file, |
| @@ -3801,7 +3809,7 @@ The call \verb|io.lines()| (without a file name) is equivalent | |||
| 3801 | to \verb|io.input():lines()|, that is, it iterates over the | 3809 | to \verb|io.input():lines()|, that is, it iterates over the |
| 3802 | lines of the default input file. | 3810 | lines 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 | ||
| 3806 | This function opens a file, | 3814 | This function opens a file, |
| 3807 | in the mode specified in the string \verb|mode|. | 3815 | in the mode specified in the string \verb|mode|. |
| @@ -3810,7 +3818,7 @@ or, in case of errors, \nil{} plus an error message. | |||
| 3810 | 3818 | ||
| 3811 | The \verb|mode| string can be any of the following: | 3819 | The \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, | |||
| 3857 | returns a new line from the file. | 3865 | returns a new line from the file. |
| 3858 | Therefore, the construction | 3866 | Therefore, 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} |
| 3862 | will iterate over all lines of the file. | 3870 | will 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, | |||
| 3965 | the host system and on the current locale | 3973 | the 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 | ||
| 3970 | Returns the number of seconds from time \verb|t1| to time \verb|t2|. | 3978 | Returns the number of seconds from time \verb|t1| to time \verb|t2|. |
| 3971 | In Posix, Windows, and some other systems, | 3979 | In Posix, Windows, and some other systems, |
| 3972 | this value is exactly \verb|t1|\Math{-}\verb|t2|. | 3980 | this 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. | |||
| 4024 | In Posix, Windows, and some other systems, this number counts the number | 4032 | In Posix, Windows, and some other systems, this number counts the number |
| 4025 | of seconds since some given start time (the ``epoch''). | 4033 | of seconds since some given start time (the ``epoch''). |
| 4026 | In other systems, the meaning is not specified, | 4034 | In other systems, the meaning is not specified, |
| 4027 | and the number returned bt \verb|time| can be used only as an argument to | 4035 | and 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 | ||
| 4037 | This function is equivalent to the \verb|tmpnam| C~function, | 4045 | This function is equivalent to the \verb|tmpnam| C~function, |
| 4038 | and many people (and even some compilers!) advise against its use, | 4046 | and many people (and even some compilers!) advise against its use, |
| 4039 | because between the time you call the function | 4047 | because between the time you call this function |
| 4040 | and the time you open the file, | 4048 | and the time you open the file, |
| 4041 | it is possible for another process | 4049 | it is possible for another process |
| 4042 | to create a file with the same name. | 4050 | to create a file with the same name. |
| @@ -4073,15 +4081,15 @@ If \verb|function| is a number larger than the number of active functions, | |||
| 4073 | then \verb|getinfo| returns \nil. | 4081 | then \verb|getinfo| returns \nil. |
| 4074 | 4082 | ||
| 4075 | The returned table contains all the fields returned by \verb|lua_getinfo|, | 4083 | The returned table contains all the fields returned by \verb|lua_getinfo|, |
| 4076 | with the string \verb|what| describing what to get. | 4084 | with the string \verb|what| describing which fields to fill in. |
| 4077 | The default for \verb|what| is to get all information available. | 4085 | The default for \verb|what| is to get all information available. |
| 4078 | If present, | 4086 | If present, |
| 4079 | the option \verb|f| | 4087 | the option \verb|f| |
| 4080 | adds a field named \verb|func| with the function itself. | 4088 | adds a field named \verb|func| with the function itself. |
| 4081 | 4089 | ||
| 4082 | For instance, the expression \verb|getinfo(1,"n").name| returns | 4090 | For instance, the expression \verb|debug.getinfo(1,"n").name| returns |
| 4083 | the name of the current function, if a reasonable name can be found, | 4091 | the name of the current function, if a reasonable name can be found, |
| 4084 | and \verb|getinfo(print)| returns a table with all available information | 4092 | and \verb|debug.getinfo(print)| returns a table with all available information |
| 4085 | about the \verb|print| function. | 4093 | about the \verb|print| function. |
| 4086 | 4094 | ||
| 4087 | 4095 | ||
| @@ -4174,7 +4182,7 @@ When called without arguments, | |||
| 4174 | and as \verb|lua -| otherwise. | 4182 | and as \verb|lua -| otherwise. |
| 4175 | 4183 | ||
| 4176 | Before running any argument, | 4184 | Before running any argument, |
| 4177 | the intepreter checks for an environment variable \IndexVerb{LUA_INIT}. | 4185 | the interpreter checks for an environment variable \IndexVerb{LUA_INIT}. |
| 4178 | If its format is \verb|@|\emph{filename}, | 4186 | If its format is \verb|@|\emph{filename}, |
| 4179 | then lua executes the file. | 4187 | then lua executes the file. |
| 4180 | Otherwise, lua executes the string itself. | 4188 | Otherwise, lua executes the string itself. |
| @@ -4235,11 +4243,11 @@ as in | |||
| 4235 | (Of course, | 4243 | (Of course, |
| 4236 | the location of the Lua interpreter may be different in your machine. | 4244 | the location of the Lua interpreter may be different in your machine. |
| 4237 | If \verb|lua| is in your \verb|PATH|, | 4245 | If \verb|lua| is in your \verb|PATH|, |
| 4238 | then a more portable solution is | 4246 | then |
| 4239 | \begin{verbatim} | 4247 | \begin{verbatim} |
| 4240 | #!/usr/bin/env lua | 4248 | #!/usr/bin/env lua |
| 4241 | \end{verbatim} | 4249 | \end{verbatim} |
| 4242 | 4250 | is a more portable solution.) | |
| 4243 | 4251 | ||
| 4244 | %------------------------------------------------------------------------------ | 4252 | %------------------------------------------------------------------------------ |
| 4245 | \section*{Acknowledgments} | 4253 | \section*{Acknowledgments} |
| @@ -4342,7 +4350,7 @@ for unprotected calls, | |||
| 4342 | or the new \verb|pcall| function for protected calls. | 4350 | or 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 |
| 4348 | The \verb|read| option \verb|*w| is obsolete. | 4356 | The \verb|read| option \verb|*w| is obsolete. |
