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