diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1998-06-18 14:36:27 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1998-06-18 14:36:27 -0300 |
commit | bec9bc4154e54fbc19b134874f6301f1e4654d5d (patch) | |
tree | da7ca157669975b942d0edf4ddf2a310733e30c9 | |
parent | 955a811aa1ab50cc4165d88cdf5a259b7b77582a (diff) | |
download | lua-bec9bc4154e54fbc19b134874f6301f1e4654d5d.tar.gz lua-bec9bc4154e54fbc19b134874f6301f1e4654d5d.tar.bz2 lua-bec9bc4154e54fbc19b134874f6301f1e4654d5d.zip |
sugestions by lhf
-rw-r--r-- | manual.tex | 440 |
1 files changed, 232 insertions, 208 deletions
@@ -1,4 +1,4 @@ | |||
1 | % $Id: manual.tex,v 1.13 1998/06/06 21:05:52 roberto Exp roberto $ | 1 | % $Id: manual.tex,v 1.14 1998/06/15 21:34:14 roberto Exp roberto $ |
2 | 2 | ||
3 | \documentclass[11pt]{article} | 3 | \documentclass[11pt]{article} |
4 | \usepackage{fullpage,bnf} | 4 | \usepackage{fullpage,bnf} |
@@ -39,7 +39,7 @@ Waldemar Celes | |||
39 | \tecgraf\ --- Computer Science Department --- PUC-Rio | 39 | \tecgraf\ --- Computer Science Department --- PUC-Rio |
40 | } | 40 | } |
41 | 41 | ||
42 | %\date{\small \verb$Date: 1998/06/06 21:05:52 $} | 42 | %\date{\small \verb$Date: 1998/06/15 21:34:14 $} |
43 | 43 | ||
44 | \maketitle | 44 | \maketitle |
45 | 45 | ||
@@ -57,17 +57,6 @@ Lua is dynamically typed, interpreted from bytecodes, | |||
57 | and has automatic memory management with garbage collection, | 57 | and has automatic memory management with garbage collection, |
58 | making it ideal for configuration, scripting, and rapid prototyping. | 58 | making it ideal for configuration, scripting, and rapid prototyping. |
59 | 59 | ||
60 | Lua is implemented as a small library of C functions, | ||
61 | written in ANSI C, and compiles unmodified in all known platforms. | ||
62 | The implementation goals are simplicity, efficiency, portability, | ||
63 | and low embedding cost. | ||
64 | |||
65 | Lua has been developed at TeCGraf, | ||
66 | the Computer Graphics Technology Group of PUC-Rio | ||
67 | (the Pontifical Catholic University of Rio de Janeiro in Brazil). | ||
68 | TeCGraf is a laboratory of the Department of Computer Science. | ||
69 | Dozens of industrial products developed by TeCGraf use Lua. | ||
70 | |||
71 | This document describes version \Version\ of the Lua programming language | 60 | This document describes version \Version\ of the Lua programming language |
72 | and the API that allows interaction between Lua programs and their | 61 | and the API that allows interaction between Lua programs and their |
73 | host C programs. | 62 | host C programs. |
@@ -90,12 +79,6 @@ e tem gerenciamento autom\'atico de mem\'oria com coleta de lixo, | |||
90 | tornando-se ideal para configura\c{c}\~ao, scripting, | 79 | tornando-se ideal para configura\c{c}\~ao, scripting, |
91 | e prototipagem r\'apida. | 80 | e prototipagem r\'apida. |
92 | 81 | ||
93 | Lua \'e implementada como uma pequena biblioteca de fun\c{c}\~oes C, | ||
94 | escrita em ANSI C, e compila sem modifica\c{c}\~oes em todas as | ||
95 | plataformas conhecidas. | ||
96 | Os objetivos da implementa\c{c}\~ao s\~ao simplicidade, efici\^encia, | ||
97 | portabilidade, e baixo custo. | ||
98 | |||
99 | Este documento descreve a vers\~ao \Version\ da linguagem de | 82 | Este documento descreve a vers\~ao \Version\ da linguagem de |
100 | programa\c{c}\~ao Lua e a Interface de Programa\c{c}\~ao (API) que permite | 83 | programa\c{c}\~ao Lua e a Interface de Programa\c{c}\~ao (API) que permite |
101 | a intera\c{c}\~ao entre programas Lua e programas C hospedeiros. | 84 | a intera\c{c}\~ao entre programas Lua e programas C hospedeiros. |
@@ -108,6 +91,7 @@ a intera\c{c}\~ao entre programas Lua e programas C hospedeiros. | |||
108 | \footnotesize | 91 | \footnotesize |
109 | Copyright \copyright\ 1994--1998 TeCGraf, PUC-Rio. All rights reserved. | 92 | Copyright \copyright\ 1994--1998 TeCGraf, PUC-Rio. All rights reserved. |
110 | 93 | ||
94 | \noindent | ||
111 | Permission is hereby granted, without written agreement and without license | 95 | Permission is hereby granted, without written agreement and without license |
112 | or royalty fees, to use, copy, modify, and distribute this software and its | 96 | or royalty fees, to use, copy, modify, and distribute this software and its |
113 | documentation for any purpose, including commercial applications, subject to | 97 | documentation for any purpose, including commercial applications, subject to |
@@ -126,18 +110,19 @@ the following conditions: | |||
126 | \end{itemize} | 110 | \end{itemize} |
127 | The authors specifically disclaim any warranties, including, but not limited | 111 | The authors specifically disclaim any warranties, including, but not limited |
128 | to, the implied warranties of merchantability and fitness for a particular | 112 | to, the implied warranties of merchantability and fitness for a particular |
129 | purpose. The software provided hereunder is on an "as is" basis, and the | 113 | purpose. The software provided hereunder is on an ``as is'' basis, and the |
130 | authors have no obligation to provide maintenance, support, updates, | 114 | authors have no obligation to provide maintenance, support, updates, |
131 | enhancements, or modifications. In no event shall TeCGraf, PUC-Rio, or the | 115 | enhancements, or modifications. In no event shall TeCGraf, PUC-Rio, or the |
132 | authors be held liable to any party for direct, indirect, special, | 116 | authors be held liable to any party for direct, indirect, special, |
133 | incidental, or consequential damages arising out of the use of this software | 117 | incidental, or consequential damages arising out of the use of this software |
134 | and its documentation. | 118 | and its documentation. |
135 | 119 | ||
136 | The Lua language and this implementation have been entirely designed and | 120 | \noindent |
137 | written by Waldemar Celes Filho, Roberto Ierusalimschy and | 121 | The Lua language and this implementation have been entirely designed and |
138 | Luiz Henrique de Figueiredo at TeCGraf, PUC-Rio. | 122 | written by Waldemar Celes, Roberto Ierusalimschy and Luiz Henrique de |
139 | This implementation contains no third-party code. | 123 | Figueiredo at TeCGraf, PUC-Rio. This implementation contains no third-party |
140 | \end{quotation} | 124 | code. |
125 | \end{quotation} | ||
141 | 126 | ||
142 | \newpage | 127 | \newpage |
143 | 128 | ||
@@ -153,7 +138,7 @@ This implementation contains no third-party code. | |||
153 | Lua is an extension programming language designed to support | 138 | Lua is an extension programming language designed to support |
154 | general procedural programming with data description | 139 | general procedural programming with data description |
155 | facilities. | 140 | facilities. |
156 | It is intended to be used as a light-weight, but powerful, | 141 | Lua is intended to be used as a light-weight, but powerful, |
157 | configuration language for any program that needs one. | 142 | configuration language for any program that needs one. |
158 | Lua has been designed and implemented by | 143 | Lua has been designed and implemented by |
159 | W.~Celes, | 144 | W.~Celes, |
@@ -173,7 +158,7 @@ thus creating customized programming languages sharing a syntactical framework. | |||
173 | 158 | ||
174 | Lua is free-distribution software, | 159 | Lua is free-distribution software, |
175 | and provided as usual with no guarantees, | 160 | and provided as usual with no guarantees, |
176 | as stated in the copyright notice in the front page of this manual. | 161 | as stated in the copyright notice. |
177 | The implementation described in this manual is available | 162 | The implementation described in this manual is available |
178 | at the following URL's: | 163 | at the following URL's: |
179 | \begin{verbatim} | 164 | \begin{verbatim} |
@@ -188,6 +173,8 @@ All statements in Lua are executed in a \Def{global environment}. | |||
188 | This environment, which keeps all global variables, | 173 | This environment, which keeps all global variables, |
189 | is initialized at the beginning of the embedding program and | 174 | is initialized at the beginning of the embedding program and |
190 | persists until its end. | 175 | persists until its end. |
176 | Optionally, a user can create multiple independent global | ||
177 | environments \see{mangstate}. | ||
191 | 178 | ||
192 | The global environment can be manipulated by Lua code or | 179 | The global environment can be manipulated by Lua code or |
193 | by the embedding program, | 180 | by the embedding program, |
@@ -236,23 +223,23 @@ There are six \Index{basic types} in Lua: \Def{nil}, \Def{number}, | |||
236 | \Def{string}, \Def{function}, \Def{userdata}, and \Def{table}. | 223 | \Def{string}, \Def{function}, \Def{userdata}, and \Def{table}. |
237 | \emph{Nil} is the type of the value \nil, | 224 | \emph{Nil} is the type of the value \nil, |
238 | whose main property is to be different from any other value. | 225 | whose main property is to be different from any other value. |
239 | \emph{Number} represents real (floating-point with double precision) numbers, | 226 | \emph{Number} represents real (double precision floating point) numbers, |
240 | while \emph{string} has the usual meaning; | 227 | while \emph{string} has the usual meaning. |
241 | notice that Lua is \Index{eight-bit clean}, | 228 | Lua is \Index{eight-bit clean}, |
242 | and so strings can contain any ISO character, | 229 | and so strings may contain any 8-bit character, |
243 | \emph{including} \verb|'\0'|. | 230 | \emph{including} embedded zeros (\verb|'\0'|). |
244 | The function \verb|type| returns a string describing the type | 231 | The function \verb|type| returns a string describing the type |
245 | of a given value \see{pdf-type}. | 232 | of a given value \see{pdf-type}. |
246 | 233 | ||
247 | Functions are considered first-class values in Lua. | 234 | Functions are considered first-class values in Lua. |
248 | This means that functions can be stored in variables, | 235 | This means that functions can be stored in variables, |
249 | passed as arguments to other functions and returned as results. | 236 | passed as arguments to other functions, and returned as results. |
250 | Lua can call (and manipulate) functions written in Lua and | 237 | Lua can call (and manipulate) functions written in Lua and |
251 | functions written in C. | 238 | functions written in C. |
252 | They can be distinguished by their tags: | 239 | They can be distinguished by their tags: |
253 | all Lua functions have the same tag, | 240 | all Lua functions have the same tag, |
254 | and all C functions have the same tag, | 241 | and all C functions have the same tag, |
255 | which is different from the tag of a Lua function. | 242 | which is different from the tag of Lua functions. |
256 | 243 | ||
257 | The type \emph{userdata} is provided to allow | 244 | The type \emph{userdata} is provided to allow |
258 | arbitrary \Index{C pointers} to be stored in Lua variables. | 245 | arbitrary \Index{C pointers} to be stored in Lua variables. |
@@ -278,9 +265,9 @@ The form \verb|t:f(x)| is syntactic sugar for \verb|t.f(t,x)|, | |||
278 | which calls the method \verb|f| from the table \verb|t| passing | 265 | which calls the method \verb|f| from the table \verb|t| passing |
279 | itself as the first parameter \see{func-def}. | 266 | itself as the first parameter \see{func-def}. |
280 | 267 | ||
281 | It is important to notice that tables are \emph{objects}, and not values. | 268 | Note that tables are \emph{objects}, and not values. |
282 | Variables cannot contain tables, only \emph{references} to them. | 269 | Variables cannot contain tables, only \emph{references} to them. |
283 | Assignment, parameter passing and returns always manipulate references | 270 | Assignment, parameter passing, and returns always manipulate references |
284 | to tables, and do not imply any kind of copy. | 271 | to tables, and do not imply any kind of copy. |
285 | Moreover, tables must be explicitly created before used | 272 | Moreover, tables must be explicitly created before used |
286 | \see{tableconstructor}. | 273 | \see{tableconstructor}. |
@@ -309,7 +296,8 @@ This section describes the lexis, the syntax and the semantics of Lua. | |||
309 | 296 | ||
310 | \subsection{Lexical Conventions} \label{lexical} | 297 | \subsection{Lexical Conventions} \label{lexical} |
311 | 298 | ||
312 | \Index{Identifiers} can be any string of letters, digits, and underscores, | 299 | \Index{Identifiers} in Lua can be any string of letters, |
300 | digits, and underscores, | ||
313 | not beginning with a digit. | 301 | not beginning with a digit. |
314 | The definition of letter depends on the current locale: | 302 | The definition of letter depends on the current locale: |
315 | Any character considered alphabetic by the current locale | 303 | Any character considered alphabetic by the current locale |
@@ -330,8 +318,8 @@ uppercase letters should not be used in regular programs. | |||
330 | 318 | ||
331 | The following strings denote other \Index{tokens}: | 319 | The following strings denote other \Index{tokens}: |
332 | \begin{verbatim} | 320 | \begin{verbatim} |
333 | ~= <= >= < > == = .. + - * / | 321 | ~= <= >= < > == = + - * / % |
334 | % ( ) { } [ ] ; , . ... | 322 | ( ) { } [ ] ; , . .. ... |
335 | \end{verbatim} | 323 | \end{verbatim} |
336 | 324 | ||
337 | \Index{Literal strings} can be delimited by matching single or double quotes, | 325 | \Index{Literal strings} can be delimited by matching single or double quotes, |
@@ -343,13 +331,13 @@ and can contain the C-like escape sequences | |||
343 | \verb|'\r'| (carriage return), | 331 | \verb|'\r'| (carriage return), |
344 | \verb|'\t'| (horizontal tab), | 332 | \verb|'\t'| (horizontal tab), |
345 | \verb|'\v'| (vertical tab), | 333 | \verb|'\v'| (vertical tab), |
346 | \verb|'\\'|, | 334 | \verb|'\\'|, (backslash), |
347 | \verb|'\"'|, | 335 | \verb|'\"'|, (double quote), |
348 | and \verb|'\''|. | 336 | and \verb|'\''| (single quote). |
349 | A character in a string may also be specified by its numerical value, | 337 | A character in a string may also be specified by its numerical value, |
350 | through the escape sequence \verb|'\ddd'|, | 338 | through the escape sequence \verb|'\ddd'|, |
351 | where \verb|ddd| is a sequence of up to three \emph{decimal} digits. | 339 | where \verb|ddd| is a sequence of up to three \emph{decimal} digits. |
352 | Strings in Lua may contain any 8-bit value, including 0. | 340 | Strings in Lua may contain any 8-bit value, including embedded 0. |
353 | 341 | ||
354 | Literal strings can also be delimited by matching \verb|[[ ... ]]|. | 342 | Literal strings can also be delimited by matching \verb|[[ ... ]]|. |
355 | Literals in this bracketed form may run for several lines, | 343 | Literals in this bracketed form may run for several lines, |
@@ -371,7 +359,7 @@ the following three literals are equivalent: | |||
371 | \Index{Comments} start anywhere outside a string with a | 359 | \Index{Comments} start anywhere outside a string with a |
372 | double hyphen (\verb|--|) and run until the end of the line. | 360 | double hyphen (\verb|--|) and run until the end of the line. |
373 | Moreover, | 361 | Moreover, |
374 | the first line of a chunk file is skipped if it starts with \verb|#|. | 362 | the first line of a chunk is skipped if it starts with \verb|#|. |
375 | This facility allows the use of Lua as a script interpreter | 363 | This facility allows the use of Lua as a script interpreter |
376 | in Unix systems \see{lua-sa}. | 364 | in Unix systems \see{lua-sa}. |
377 | 365 | ||
@@ -384,18 +372,19 @@ Examples of valid numerical constants are: | |||
384 | 372 | ||
385 | \subsection{The \Index{Pre-processor}} \label{pre-processor} | 373 | \subsection{The \Index{Pre-processor}} \label{pre-processor} |
386 | 374 | ||
387 | All lines that start with a \verb|$| are handled by a pre-processor. | 375 | All lines that start with a \verb|$| sign are handled by a pre-processor. |
388 | The \verb|$| must be immediately followed by one of the following directives: | 376 | The \verb|$| sign must be immediately |
377 | followed by one of the following directives: | ||
389 | \begin{description} | 378 | \begin{description} |
390 | \item[\T{debug}] --- turn on some debugging facilities \see{pragma}. | 379 | \item[\T{debug}] --- turn on debugging facilities \see{pragma}. |
391 | \item[\T{nodebug}] --- turn off some debugging facilities \see{pragma}. | 380 | \item[\T{nodebug}] --- turn off debugging facilities \see{pragma}. |
392 | \item[\T{if \M{cond}}] --- starts a conditional part. | 381 | \item[\T{if \M{cond}}] --- starts a conditional part. |
393 | If \M{cond} is false, then this part is skipped by the lexical analyzer. | 382 | If \M{cond} is false, then this part is skipped by the lexical analyzer. |
394 | \item[\T{ifnot \M{cond}}] --- starts a conditional part. | 383 | \item[\T{ifnot \M{cond}}] --- starts a conditional part. |
395 | If \M{cond} is true, then this part is skipped by the lexical analyzer. | 384 | If \M{cond} is true, then this part is skipped by the lexical analyzer. |
396 | \item[\T{end}] --- ends a conditional part. | 385 | \item[\T{end}] --- ends a conditional part. |
397 | \item[\T{else}] --- starts an ``else'' conditional part, | 386 | \item[\T{else}] --- starts an ``else'' conditional part, |
398 | flopping the ``skip'' status. | 387 | flipping the ``skip'' status. |
399 | \item[\T{endinput}] --- ends the lexical parse of the file. | 388 | \item[\T{endinput}] --- ends the lexical parse of the file. |
400 | \end{description} | 389 | \end{description} |
401 | 390 | ||
@@ -409,7 +398,7 @@ A \M{cond} part may be: | |||
409 | \item[\T{1}] --- always true. | 398 | \item[\T{1}] --- always true. |
410 | \item[\M{name}] --- true if the value of the | 399 | \item[\M{name}] --- true if the value of the |
411 | global variable \M{name} is different from \nil. | 400 | global variable \M{name} is different from \nil. |
412 | Notice that \M{name} is evaluated before the chunk starts its execution. | 401 | Note that \M{name} is evaluated \emph{before} the chunk starts its execution. |
413 | Therefore, actions in a chunk do not affect its own conditional directives. | 402 | Therefore, actions in a chunk do not affect its own conditional directives. |
414 | \end{description} | 403 | \end{description} |
415 | 404 | ||
@@ -666,10 +655,18 @@ Both \verb|and| and \verb|or| use \Index{short-cut evaluation}, | |||
666 | that is, | 655 | that is, |
667 | the second operand is evaluated only when necessary. | 656 | the second operand is evaluated only when necessary. |
668 | 657 | ||
658 | A useful Lua idiom is \verb|x = x or v|, | ||
659 | which is equivalent to | ||
660 | \begin{verbatim} | ||
661 | if x == nil then x = v end | ||
662 | \end{verbatim} | ||
663 | i.e., it sets \verb|x| to a default value \verb|v| when | ||
664 | \verb|x| is not set. | ||
665 | |||
669 | \subsubsection{Concatenation} | 666 | \subsubsection{Concatenation} |
670 | Lua offers a string \Index{concatenation} operator, | 667 | The string \Index{concatenation} operator in Lua is |
671 | denoted by ``\IndexVerb{..}''. | 668 | denoted by ``\IndexVerb{..}''. |
672 | If operands are strings or numbers, then they are converted to | 669 | If both operands are strings or numbers, they are converted to |
673 | strings according to the rules in \See{coercion}. | 670 | strings according to the rules in \See{coercion}. |
674 | Otherwise, the ``concat'' tag method is called \see{tag-method}. | 671 | Otherwise, the ``concat'' tag method is called \see{tag-method}. |
675 | 672 | ||
@@ -748,7 +745,7 @@ is equivalent to: | |||
748 | An expression like \verb|{x = 1, y = 4}| is | 745 | An expression like \verb|{x = 1, y = 4}| is |
749 | in fact syntactic sugar for \verb|{["x"] = 1, ["y"] = 4}|. | 746 | in fact syntactic sugar for \verb|{["x"] = 1, ["y"] = 4}|. |
750 | 747 | ||
751 | Both forms may have an optional ending comma, | 748 | Both forms may have an optional trailing comma, |
752 | and can be used in the same constructor separated by | 749 | and can be used in the same constructor separated by |
753 | a semi-collon. | 750 | a semi-collon. |
754 | For example, all forms below are correct: | 751 | For example, all forms below are correct: |
@@ -812,6 +809,17 @@ thus discarding all returned values but the first one. | |||
812 | If the function is called in a place that can hold many values | 809 | If the function is called in a place that can hold many values |
813 | (syntactically denoted by the non-terminal \M{exp}), | 810 | (syntactically denoted by the non-terminal \M{exp}), |
814 | then no adjustment is made. | 811 | then no adjustment is made. |
812 | Note that the only place that can hold many values | ||
813 | is the last expression (or the only one) in an assignment | ||
814 | or in a return statement; see examples below. | ||
815 | \begin{verbatim} | ||
816 | f(); -- adjusted to 0 | ||
817 | g(x, f()); -- f() is adjusted to 1 | ||
818 | a,b,c = f(), x; -- f() is adjusted to 1 result (and c gets nil) | ||
819 | a,b,c = x, f(); -- f() is adjusted to 2 | ||
820 | a,b,c = f(); -- f() is adjusted to 3 | ||
821 | return f(); -- returns all values returned by f() | ||
822 | \end{verbatim} | ||
815 | 823 | ||
816 | \subsubsection{\Index{Function Definitions}} \label{func-def} | 824 | \subsubsection{\Index{Function Definitions}} \label{func-def} |
817 | 825 | ||
@@ -890,7 +898,7 @@ If control reaches the end of a function without a return instruction, | |||
890 | then the function returns with no results. | 898 | then the function returns with no results. |
891 | 899 | ||
892 | There is a special syntax for defining \Index{methods}, | 900 | There is a special syntax for defining \Index{methods}, |
893 | that is, functions that have an extra parameter \IndexVerb{self}. | 901 | that is, functions that have an implicit extra parameter \IndexVerb{self}. |
894 | \begin{Produc} | 902 | \begin{Produc} |
895 | \produc{function}{\rwd{function} name \ter{:} name \ter{(} \opt{parlist1} | 903 | \produc{function}{\rwd{function} name \ter{:} name \ter{(} \opt{parlist1} |
896 | \ter{)} block \rwd{end}} | 904 | \ter{)} block \rwd{end}} |
@@ -908,7 +916,7 @@ v.f = function (self, ...) | |||
908 | end | 916 | end |
909 | \end{verbatim} | 917 | \end{verbatim} |
910 | that is, the function gets an extra formal parameter called \verb|self|. | 918 | that is, the function gets an extra formal parameter called \verb|self|. |
911 | Notice that the variable \verb|v| must have been | 919 | Note that the variable \verb|v| must have been |
912 | previously initialized with a table value. | 920 | previously initialized with a table value. |
913 | 921 | ||
914 | 922 | ||
@@ -936,19 +944,19 @@ at the point where the function is defined. | |||
936 | 944 | ||
937 | Here are some examples: | 945 | Here are some examples: |
938 | \begin{verbatim} | 946 | \begin{verbatim} |
939 | a,b,c = 1 -- global variables | 947 | a,b,c = 1,2,3 -- global variables |
940 | function f () | 948 | function f (x) |
941 | local x,b -- x and b are local to f | 949 | local b -- x and b are local to f |
942 | function g () | 950 | local g = function (a) |
943 | local a,y -- a and y are local to g | 951 | local y -- a and y are local to g |
944 | p = a -- OK, access local 'a' | 952 | p = a -- OK, access local 'a' |
945 | p = c -- OK, access global 'c' | 953 | p = c -- OK, access global 'c' |
946 | p = b -- ERROR: cannot access a variable in outer scope | 954 | p = b -- ERROR: cannot access a variable in outer scope |
947 | p = %b -- OK, access frozen value of 'b' (local to 'f') | 955 | p = %b -- OK, access frozen value of 'b' (local to 'f') |
948 | p = %c -- OK, access frozen value of global 'c' | 956 | p = %c -- OK, access frozen value of global 'c' |
949 | p = %y -- ERROR: 'y' is not visible where 'g' is defined | 957 | p = %y -- ERROR: 'y' is not visible where 'g' is defined |
950 | end | 958 | end -- g |
951 | end | 959 | end -- f |
952 | \end{verbatim} | 960 | \end{verbatim} |
953 | 961 | ||
954 | 962 | ||
@@ -956,9 +964,9 @@ end | |||
956 | 964 | ||
957 | Lua provides a powerful mechanism to extend its semantics, | 965 | Lua provides a powerful mechanism to extend its semantics, |
958 | called \Def{Tag Methods}. | 966 | called \Def{Tag Methods}. |
959 | A tag method (TM) is a programmer-defined function | 967 | A tag method is a programmer-defined function |
960 | that is called at specific key points during the evaluation of a program, | 968 | that is called at specific key points during the evaluation of a program, |
961 | allowing a programmer to change the standard Lua behavior at these points. | 969 | allowing the programmer to change the standard Lua behavior at these points. |
962 | Each of these points is called an \Def{event}. | 970 | Each of these points is called an \Def{event}. |
963 | 971 | ||
964 | The tag method called for any specific event is selected | 972 | The tag method called for any specific event is selected |
@@ -966,11 +974,11 @@ according to the tag of the values involved | |||
966 | in the event \see{TypesSec}. | 974 | in the event \see{TypesSec}. |
967 | The function \IndexVerb{settagmethod} changes the tag method | 975 | The function \IndexVerb{settagmethod} changes the tag method |
968 | associated with a given pair \M{(tag, event)}. | 976 | associated with a given pair \M{(tag, event)}. |
969 | Its first parameter is the tag, the second the event name | 977 | Its first parameter is the tag, the second is the event name |
970 | (a string, see below), | 978 | (a string, see below), |
971 | and the third parameter is the new method (a function), | 979 | and the third parameter is the new method (a function), |
972 | or \nil\ to restore the default behavior. | 980 | or \nil\ to restore the default behavior. |
973 | The function returns the previous tag method. | 981 | The function returns the previous tag method for that pair. |
974 | Another function, \IndexVerb{gettagmethod}, | 982 | Another function, \IndexVerb{gettagmethod}, |
975 | receives a tag and an event name and returns the | 983 | receives a tag and an event name and returns the |
976 | current method associated with the pair. | 984 | current method associated with the pair. |
@@ -995,7 +1003,7 @@ called when a \verb|+| operation is applied to non numerical operands. | |||
995 | 1003 | ||
996 | The function \verb|getbinmethod| defines how Lua chooses a tag method | 1004 | The function \verb|getbinmethod| defines how Lua chooses a tag method |
997 | for a binary operation. | 1005 | for a binary operation. |
998 | First Lua tries the first operand. | 1006 | First, Lua tries the first operand. |
999 | If its tag does not define a tag method for the operation, | 1007 | If its tag does not define a tag method for the operation, |
1000 | then Lua tries the second operand. | 1008 | then Lua tries the second operand. |
1001 | If it also fails, then it gets a tag method from tag~0: | 1009 | If it also fails, then it gets a tag method from tag~0: |
@@ -1011,7 +1019,7 @@ If it also fails, then it gets a tag method from tag~0: | |||
1011 | local o1, o2 = tonumber(op1), tonumber(op2) | 1019 | local o1, o2 = tonumber(op1), tonumber(op2) |
1012 | if o1 and o2 then -- both operands are numeric | 1020 | if o1 and o2 then -- both operands are numeric |
1013 | return o1+o2 -- '+' here is the primitive 'add' | 1021 | return o1+o2 -- '+' here is the primitive 'add' |
1014 | else -- at least one of the operands is not numeric. | 1022 | else -- at least one of the operands is not numeric |
1015 | local tm = getbinmethod(op1, op2, "add") | 1023 | local tm = getbinmethod(op1, op2, "add") |
1016 | if tm then | 1024 | if tm then |
1017 | -- call the method with both operands and an extra | 1025 | -- call the method with both operands and an extra |
@@ -1147,7 +1155,7 @@ created by \verb|newtag|. | |||
1147 | end | 1155 | end |
1148 | end | 1156 | end |
1149 | \end{verbatim} | 1157 | \end{verbatim} |
1150 | Notice: the function \verb|getglobal| is pre-defined in Lua \see{predefined}. | 1158 | The function \verb|getglobal| is pre-defined in Lua \see{predefined}. |
1151 | 1159 | ||
1152 | \item[``setglobal'':]\index{setglobal event} | 1160 | \item[``setglobal'':]\index{setglobal event} |
1153 | called whenever Lua assigns to a global variable. | 1161 | called whenever Lua assigns to a global variable. |
@@ -1179,7 +1187,7 @@ This method cannot be set for tables with default tag. | |||
1179 | else | 1187 | else |
1180 | local v = rawgettable(table, index) | 1188 | local v = rawgettable(table, index) |
1181 | tm = gettagmethod(tag(table), "index") | 1189 | tm = gettagmethod(tag(table), "index") |
1182 | if (v == nil) and tm then | 1190 | if v == nil and tm then |
1183 | return tm(table, index) | 1191 | return tm(table, index) |
1184 | else | 1192 | else |
1185 | return v | 1193 | return v |
@@ -1257,12 +1265,13 @@ calling a function from the Lua library. | |||
1257 | Whenever an error occurs during Lua compilation or execution, | 1265 | Whenever an error occurs during Lua compilation or execution, |
1258 | the \Def{error method} is called, | 1266 | the \Def{error method} is called, |
1259 | and then the corresponding function from the library | 1267 | and then the corresponding function from the library |
1260 | (\verb|lua_dofile|, \verb|lua_dostring|, or \verb|lua_callfunction|) | 1268 | (\verb|lua_dofile|, \verb|lua_dostring|, |
1261 | is terminated returning an error condition. | 1269 | \verb|lua_dobuffer|, or \verb|lua_callfunction|) |
1270 | is terminated, returning an error condition. | ||
1262 | 1271 | ||
1263 | The only argument to the error method is a string | 1272 | The only argument to the error method is a string |
1264 | describing the error. | 1273 | describing the error. |
1265 | The default method prints this message in \verb|stderr|. | 1274 | The default method prints this message to \verb|stderr|. |
1266 | If needed, it is possible to change the error method with the | 1275 | If needed, it is possible to change the error method with the |
1267 | function \verb|seterrormethod|, | 1276 | function \verb|seterrormethod|, |
1268 | which gets the new error handler as its only parameter | 1277 | which gets the new error handler as its only parameter |
@@ -1304,7 +1313,7 @@ The API functions can be classified in the following categories: | |||
1304 | All API functions and related types and constants | 1313 | All API functions and related types and constants |
1305 | are declared in the header file \verb|lua.h|. | 1314 | are declared in the header file \verb|lua.h|. |
1306 | 1315 | ||
1307 | \subsection{Managing States} | 1316 | \subsection{Managing States} \label{mangstate} |
1308 | The whole state of the Lua interpreter | 1317 | The whole state of the Lua interpreter |
1309 | (global variables, stack, tag methods, etc) | 1318 | (global variables, stack, tag methods, etc) |
1310 | is stored in a dynamic structure pointed by\Deffunc{lua_state} | 1319 | is stored in a dynamic structure pointed by\Deffunc{lua_state} |
@@ -1317,23 +1326,23 @@ Before calling any API function, | |||
1317 | this state must be initialized. | 1326 | this state must be initialized. |
1318 | This is done by calling\Deffunc{lua_open} | 1327 | This is done by calling\Deffunc{lua_open} |
1319 | \begin{verbatim} | 1328 | \begin{verbatim} |
1320 | void lua_open (void); | 1329 | void lua_open (void); |
1321 | \end{verbatim} | 1330 | \end{verbatim} |
1322 | This function allocates and initializes some internal structures, | 1331 | This function allocates and initializes some internal structures, |
1323 | and defines all pre-defined functions of Lua. | 1332 | and defines all pre-defined functions of Lua. |
1324 | If \verb|lua_state| is already different from \verb|NULL|, | 1333 | If \verb|lua_state| is already different from \verb|NULL|, |
1325 | this function has no effect; | 1334 | \verb|lua_open| has no effect; |
1326 | therefore, it is safe to call this function multiple times. | 1335 | therefore, it is safe to call this function multiple times. |
1327 | All standard libraries call \verb|lua_open| when they are opened. | 1336 | All standard libraries call \verb|lua_open| when they are opened. |
1328 | 1337 | ||
1329 | Function \verb|lua_setstate| is used to change the current state | 1338 | Function \verb|lua_setstate| is used to change the current state |
1330 | of Lua:\Deffunc{lua_setstate} | 1339 | of Lua:\Deffunc{lua_setstate} |
1331 | \begin{verbatim} | 1340 | \begin{verbatim} |
1332 | lua_State *lua_setstate (lua_State *st); | 1341 | lua_State *lua_setstate (lua_State *st); |
1333 | \end{verbatim} | 1342 | \end{verbatim} |
1334 | It sets \verb|lua_state| to \verb|st| and returns the old state. | 1343 | It sets \verb|lua_state| to \verb|st| and returns the old state. |
1335 | 1344 | ||
1336 | Multiple, independent, states may be created. | 1345 | Multiple, independent states may be created. |
1337 | For that, you must set \verb|lua_state| back to \verb|NULL| before | 1346 | For that, you must set \verb|lua_state| back to \verb|NULL| before |
1338 | calling \verb|lua_open|. | 1347 | calling \verb|lua_open|. |
1339 | An easy way to do that is defining an auxiliary function: | 1348 | An easy way to do that is defining an auxiliary function: |
@@ -1346,13 +1355,13 @@ lua_State *lua_newstate (void) { | |||
1346 | \end{verbatim} | 1355 | \end{verbatim} |
1347 | This function creates a new state without changing the current state | 1356 | This function creates a new state without changing the current state |
1348 | of the interpreter. | 1357 | of the interpreter. |
1349 | Notice that any new state is built with all predefined functions; | 1358 | Note that any new state is built with all predefined functions, |
1350 | any additional library (such as the standard libraries) must be | 1359 | but any additional library (such as the standard libraries) must be |
1351 | explicitly open in the new state, if needed. | 1360 | explicitly open in the new state, if needed. |
1352 | 1361 | ||
1353 | If necessary, a state may be released:\Deffunc{lua_close} | 1362 | If necessary, a state may be released:\Deffunc{lua_close} |
1354 | \begin{verbatim} | 1363 | \begin{verbatim} |
1355 | void lua_close (void); | 1364 | void lua_close (void); |
1356 | \end{verbatim} | 1365 | \end{verbatim} |
1357 | This function destroys all objects in the current Lua environment | 1366 | This function destroys all objects in the current Lua environment |
1358 | (calling the correspondent garbage collector tag methods), | 1367 | (calling the correspondent garbage collector tag methods), |
@@ -1361,7 +1370,7 @@ and then sets \verb|lua_state| to \verb|NULL|. | |||
1361 | Usually, there is no need to call this function, | 1370 | Usually, there is no need to call this function, |
1362 | since these resources are naturally released when the program ends. | 1371 | since these resources are naturally released when the program ends. |
1363 | If \verb|lua_state| is already \verb|NULL|, | 1372 | If \verb|lua_state| is already \verb|NULL|, |
1364 | this function has no effect. | 1373 | \verb|lua_close| has no effect. |
1365 | 1374 | ||
1366 | If you are using multiple states, | 1375 | If you are using multiple states, |
1367 | you may find useful the following function, | 1376 | you may find useful the following function, |
@@ -1389,13 +1398,13 @@ the following functions are available: | |||
1389 | \Deffunc{lua_istable}\Deffunc{lua_iscfunction}\Deffunc{lua_isuserdata} | 1398 | \Deffunc{lua_istable}\Deffunc{lua_iscfunction}\Deffunc{lua_isuserdata} |
1390 | \Deffunc{lua_isfunction} | 1399 | \Deffunc{lua_isfunction} |
1391 | \begin{verbatim} | 1400 | \begin{verbatim} |
1392 | int lua_isnil (lua_Object object); | 1401 | int lua_isnil (lua_Object object); |
1393 | int lua_isnumber (lua_Object object); | 1402 | int lua_isnumber (lua_Object object); |
1394 | int lua_isstring (lua_Object object); | 1403 | int lua_isstring (lua_Object object); |
1395 | int lua_istable (lua_Object object); | 1404 | int lua_istable (lua_Object object); |
1396 | int lua_isfunction (lua_Object object); | 1405 | int lua_isfunction (lua_Object object); |
1397 | int lua_iscfunction (lua_Object object); | 1406 | int lua_iscfunction (lua_Object object); |
1398 | int lua_isuserdata (lua_Object object); | 1407 | int lua_isuserdata (lua_Object object); |
1399 | \end{verbatim} | 1408 | \end{verbatim} |
1400 | All macros return 1 if the object is compatible with the given type, | 1409 | All macros return 1 if the object is compatible with the given type, |
1401 | and 0 otherwise. | 1410 | and 0 otherwise. |
@@ -1408,7 +1417,7 @@ To get the tag of a \verb|lua_Object|, | |||
1408 | the following function is available: | 1417 | the following function is available: |
1409 | \Deffunc{lua_tag} | 1418 | \Deffunc{lua_tag} |
1410 | \begin{verbatim} | 1419 | \begin{verbatim} |
1411 | int lua_tag (lua_Object object); | 1420 | int lua_tag (lua_Object object); |
1412 | \end{verbatim} | 1421 | \end{verbatim} |
1413 | 1422 | ||
1414 | To translate a value from type \verb|lua_Object| to a specific C type, | 1423 | To translate a value from type \verb|lua_Object| to a specific C type, |
@@ -1416,16 +1425,16 @@ the programmer can use: | |||
1416 | \Deffunc{lua_getnumber}\Deffunc{lua_getstring}\Deffunc{lua_strlen} | 1425 | \Deffunc{lua_getnumber}\Deffunc{lua_getstring}\Deffunc{lua_strlen} |
1417 | \Deffunc{lua_getcfunction}\Deffunc{lua_getuserdata} | 1426 | \Deffunc{lua_getcfunction}\Deffunc{lua_getuserdata} |
1418 | \begin{verbatim} | 1427 | \begin{verbatim} |
1419 | double lua_getnumber (lua_Object object); | 1428 | double lua_getnumber (lua_Object object); |
1420 | char *lua_getstring (lua_Object object); | 1429 | char *lua_getstring (lua_Object object); |
1421 | long lua_strlen (lua_Object object); | 1430 | long lua_strlen (lua_Object object); |
1422 | lua_CFunction lua_getcfunction (lua_Object object); | 1431 | lua_CFunction lua_getcfunction (lua_Object object); |
1423 | void *lua_getuserdata (lua_Object object); | 1432 | void *lua_getuserdata (lua_Object object); |
1424 | \end{verbatim} | 1433 | \end{verbatim} |
1425 | 1434 | ||
1426 | \verb|lua_getnumber| converts a \verb|lua_Object| to a floating-point number. | 1435 | \verb|lua_getnumber| converts a \verb|lua_Object| to a floating-point number. |
1427 | This \verb|lua_Object| must be a number or a string convertible to number | 1436 | This \verb|lua_Object| must be a number or a string convertible to number |
1428 | \see{coercion}; otherwise, the function returns~0. | 1437 | \see{coercion}; otherwise, \verb|lua_getnumber| returns~0. |
1429 | 1438 | ||
1430 | \verb|lua_getstring| converts a \verb|lua_Object| to a string (\verb|char*|). | 1439 | \verb|lua_getstring| converts a \verb|lua_Object| to a string (\verb|char*|). |
1431 | This \verb|lua_Object| must be a string or a number; | 1440 | This \verb|lua_Object| must be a string or a number; |
@@ -1435,19 +1444,19 @@ but returns a pointer to a string inside the Lua environment. | |||
1435 | Those strings always have a 0 after their last character (like in C), | 1444 | Those strings always have a 0 after their last character (like in C), |
1436 | but may contain other zeros in their body. | 1445 | but may contain other zeros in their body. |
1437 | If you do not know whether a string may contain zeros, | 1446 | If you do not know whether a string may contain zeros, |
1438 | you can use \verb|lua_strlen| to check the actual length. | 1447 | you can use \verb|lua_strlen| to get the actual length. |
1439 | Because Lua has garbage collection, | 1448 | Because Lua has garbage collection, |
1440 | there is no guarantee that such pointer will be valid after the block ends | 1449 | there is no guarantee that such pointer will be valid after the block ends |
1441 | \see{GC}. | 1450 | \see{GC}. |
1442 | 1451 | ||
1443 | \verb|lua_getcfunction| converts a \verb|lua_Object| to a C function. | 1452 | \verb|lua_getcfunction| converts a \verb|lua_Object| to a C function. |
1444 | This \verb|lua_Object| must have type \emph{CFunction}; | 1453 | This \verb|lua_Object| must have type \emph{CFunction}; |
1445 | otherwise, the function returns 0 (the \verb|NULL| pointer). | 1454 | otherwise, \verb|lua_getcfunction| returns 0 (the \verb|NULL| pointer). |
1446 | The type \verb|lua_CFunction| is explained in \See{LuacallC}. | 1455 | The type \verb|lua_CFunction| is explained in \See{LuacallC}. |
1447 | 1456 | ||
1448 | \verb|lua_getuserdata| converts a \verb|lua_Object| to \verb|void*|. | 1457 | \verb|lua_getuserdata| converts a \verb|lua_Object| to \verb|void*|. |
1449 | This \verb|lua_Object| must have type \emph{userdata}; | 1458 | This \verb|lua_Object| must have type \emph{userdata}; |
1450 | otherwise, the function returns 0 (the \verb|NULL| pointer). | 1459 | otherwise, \verb|lua_getuserdata| returns 0 (the \verb|NULL| pointer). |
1451 | 1460 | ||
1452 | \subsection{Garbage Collection}\label{GC} | 1461 | \subsection{Garbage Collection}\label{GC} |
1453 | Because Lua has automatic memory management and garbage collection, | 1462 | Because Lua has automatic memory management and garbage collection, |
@@ -1462,12 +1471,12 @@ and never to store \verb|lua_Object|s in C global variables. | |||
1462 | A garbage collection cycle can be forced by: | 1471 | A garbage collection cycle can be forced by: |
1463 | \Deffunc{lua_collectgarbage} | 1472 | \Deffunc{lua_collectgarbage} |
1464 | \begin{verbatim} | 1473 | \begin{verbatim} |
1465 | long lua_collectgarbage (long limit); | 1474 | long lua_collectgarbage (long limit); |
1466 | \end{verbatim} | 1475 | \end{verbatim} |
1467 | This function returns the number of objects collected. | 1476 | This function returns the number of objects collected. |
1468 | The argument \verb|limit| makes the next cycle occur only | 1477 | The argument \verb|limit| makes the next cycle occur only |
1469 | after that number of new objects have been created. | 1478 | after that number of new objects have been created. |
1470 | If \verb|limit|=0, then Lua uses an adaptable heuristics to set this limit. | 1479 | If \verb|limit|=0, then Lua uses an adaptive heuristics to set this limit. |
1471 | 1480 | ||
1472 | 1481 | ||
1473 | All communication between Lua and C is done through two | 1482 | All communication between Lua and C is done through two |
@@ -1482,7 +1491,7 @@ The structure lua2C is an abstract array, | |||
1482 | which can be indexed with the function: | 1491 | which can be indexed with the function: |
1483 | \Deffunc{lua_lua2C} | 1492 | \Deffunc{lua_lua2C} |
1484 | \begin{verbatim} | 1493 | \begin{verbatim} |
1485 | lua_Object lua_lua2C (int number); | 1494 | lua_Object lua_lua2C (int number); |
1486 | \end{verbatim} | 1495 | \end{verbatim} |
1487 | where \verb|number| starts with 1. | 1496 | where \verb|number| starts with 1. |
1488 | When called with a number larger than the array size, | 1497 | When called with a number larger than the array size, |
@@ -1490,7 +1499,7 @@ this function returns \verb|LUA_NOOBJECT|\Deffunc{LUA_NOOBJECT}. | |||
1490 | In this way, it is possible to write C functions that receive | 1499 | In this way, it is possible to write C functions that receive |
1491 | a variable number of parameters, | 1500 | a variable number of parameters, |
1492 | and to call Lua functions that return a variable number of results. | 1501 | and to call Lua functions that return a variable number of results. |
1493 | Notice that the structure lua2C cannot be directly modified by C code. | 1502 | Note that the structure lua2C cannot be directly modified by C code. |
1494 | 1503 | ||
1495 | The second structure, C2lua, is an abstract stack. | 1504 | The second structure, C2lua, is an abstract stack. |
1496 | Pushing elements into this stack | 1505 | Pushing elements into this stack |
@@ -1500,13 +1509,13 @@ is done with the following functions and macros: | |||
1500 | \Deffunc{lua_pushnil}\Deffunc{lua_pushobject} | 1509 | \Deffunc{lua_pushnil}\Deffunc{lua_pushobject} |
1501 | \Deffunc{lua_pushuserdata}\label{pushing} | 1510 | \Deffunc{lua_pushuserdata}\label{pushing} |
1502 | \begin{verbatim} | 1511 | \begin{verbatim} |
1503 | void lua_pushnumber (double n); | 1512 | void lua_pushnumber (double n); |
1504 | void lua_pushlstring (char *s, long len); | 1513 | void lua_pushlstring (char *s, long len); |
1505 | void lua_pushstring (char *s); | 1514 | void lua_pushstring (char *s); |
1506 | void lua_pushusertag (void *u, int tag); | 1515 | void lua_pushusertag (void *u, int tag); |
1507 | void lua_pushnil (void); | 1516 | void lua_pushnil (void); |
1508 | void lua_pushobject (lua_Object object); | 1517 | void lua_pushobject (lua_Object object); |
1509 | void lua_pushcfunction (lua_CFunction f); /* macro */ | 1518 | void lua_pushcfunction (lua_CFunction f); /* macro */ |
1510 | \end{verbatim} | 1519 | \end{verbatim} |
1511 | All of them receive a C value, | 1520 | All of them receive a C value, |
1512 | convert it to a corresponding \verb|lua_Object|, | 1521 | convert it to a corresponding \verb|lua_Object|, |
@@ -1514,12 +1523,12 @@ and leave the result on the top of C2lua. | |||
1514 | Particularly, functions \verb|lua_pushlstring| and \verb|lua_pushstring| | 1523 | Particularly, functions \verb|lua_pushlstring| and \verb|lua_pushstring| |
1515 | make an internal copy of the given string. | 1524 | make an internal copy of the given string. |
1516 | Function \verb|lua_pushstring| can only be used to push proper C strings | 1525 | Function \verb|lua_pushstring| can only be used to push proper C strings |
1517 | (that is, strings which do not contain zeros and end with a zero); | 1526 | (that is, strings that do not contain zeros and end with a zero); |
1518 | otherwise you can use the more generic \verb|lua_pushlstring|. | 1527 | otherwise you should use the more generic \verb|lua_pushlstring|. |
1519 | The function | 1528 | The function |
1520 | \Deffunc{lua_pop} | 1529 | \Deffunc{lua_pop} |
1521 | \begin{verbatim} | 1530 | \begin{verbatim} |
1522 | lua_Object lua_pop (void); | 1531 | lua_Object lua_pop (void); |
1523 | \end{verbatim} | 1532 | \end{verbatim} |
1524 | returns a reference to the object at the top of the C2lua stack, | 1533 | returns a reference to the object at the top of the C2lua stack, |
1525 | and pops it. | 1534 | and pops it. |
@@ -1533,25 +1542,25 @@ If Lua has a userdata with the given value (\verb|void*|) and tag, | |||
1533 | that userdata is pushed. | 1542 | that userdata is pushed. |
1534 | Otherwise, a new userdata is created, with the given value and tag. | 1543 | Otherwise, a new userdata is created, with the given value and tag. |
1535 | If this function is called with | 1544 | If this function is called with |
1536 | \verb|tag|=\verb|LUA_ANYTAG|\Deffunc{LUA_ANYTAG}, | 1545 | \verb|tag| equal to \verb|LUA_ANYTAG|\Deffunc{LUA_ANYTAG}, |
1537 | then Lua will try to find any userdata with the given value, | 1546 | then Lua will try to find any userdata with the given value, |
1538 | regardless of its tag. | 1547 | regardless of its tag. |
1539 | If there is no userdata with that value, then a new one is created, | 1548 | If there is no userdata with that value, then a new one is created, |
1540 | with tag equals to 0. | 1549 | with tag equal to 0. |
1541 | 1550 | ||
1542 | Userdata can have different tags, | 1551 | Userdata can have different tags, |
1543 | whose semantics are only known to the host program. | 1552 | whose semantics are only known to the host program. |
1544 | Tags are created with the function: | 1553 | Tags are created with the function: |
1545 | \Deffunc{lua_newtag} | 1554 | \Deffunc{lua_newtag} |
1546 | \begin{verbatim} | 1555 | \begin{verbatim} |
1547 | int lua_newtag (void); | 1556 | int lua_newtag (void); |
1548 | \end{verbatim} | 1557 | \end{verbatim} |
1549 | The function \verb|lua_settag| changes the tag of | 1558 | The function \verb|lua_settag| changes the tag of |
1550 | the object on the top of C2lua (and pops it); | 1559 | the object on the top of C2lua (and pops it); |
1551 | the object must be a userdata or a table. | 1560 | the object must be a userdata or a table. |
1552 | \Deffunc{lua_settag} | 1561 | \Deffunc{lua_settag} |
1553 | \begin{verbatim} | 1562 | \begin{verbatim} |
1554 | void lua_settag (int tag); | 1563 | void lua_settag (int tag); |
1555 | \end{verbatim} | 1564 | \end{verbatim} |
1556 | \verb|tag| must be a value created with \verb|lua_newtag|. | 1565 | \verb|tag| must be a value created with \verb|lua_newtag|. |
1557 | 1566 | ||
@@ -1561,16 +1570,17 @@ and may cause a stack overflow. | |||
1561 | To avoid this, | 1570 | To avoid this, |
1562 | nested blocks can be defined with the functions: | 1571 | nested blocks can be defined with the functions: |
1563 | \begin{verbatim} | 1572 | \begin{verbatim} |
1564 | void lua_beginblock (void); | 1573 | void lua_beginblock (void); |
1565 | void lua_endblock (void); | 1574 | void lua_endblock (void); |
1566 | \end{verbatim} | 1575 | \end{verbatim} |
1567 | After the end of the block, | 1576 | After the end of the block, |
1568 | all \verb|lua_Object|'s created inside it are released. | 1577 | all \verb|lua_Object|'s created inside it are released. |
1569 | The use of explicit nested blocks is strongly encouraged. | 1578 | The use of explicit nested blocks is good progamming practice |
1579 | and is strongly encouraged. | ||
1570 | 1580 | ||
1571 | \subsection{Executing Lua Code} | 1581 | \subsection{Executing Lua Code} |
1572 | A host program can execute Lua chunks written in a file or in a string | 1582 | A host program can execute Lua chunks written in a file or in a string |
1573 | using the following functions: | 1583 | using the following functions:% |
1574 | \Deffunc{lua_dofile}\Deffunc{lua_dostring}\Deffunc{lua_dobuffer} | 1584 | \Deffunc{lua_dofile}\Deffunc{lua_dostring}\Deffunc{lua_dobuffer} |
1575 | \begin{verbatim} | 1585 | \begin{verbatim} |
1576 | int lua_dofile (char *filename); | 1586 | int lua_dofile (char *filename); |
@@ -1581,8 +1591,8 @@ All these functions return an error code: | |||
1581 | 0, in case of success; non zero, in case of errors. | 1591 | 0, in case of success; non zero, in case of errors. |
1582 | More specifically, \verb|lua_dofile| returns 2 if for any reason | 1592 | More specifically, \verb|lua_dofile| returns 2 if for any reason |
1583 | it could not open the file. | 1593 | it could not open the file. |
1584 | The function \verb|lua_dofile|, if called with argument \verb|NULL|, | 1594 | When called with argument \verb|NULL|, |
1585 | executes the \verb|stdin| stream. | 1595 | \verb|lua_dofile| executes the \verb|stdin| stream. |
1586 | Functions \verb|lua_dofile| and \verb|lua_dobuffer| | 1596 | Functions \verb|lua_dofile| and \verb|lua_dobuffer| |
1587 | are both able to execute pre-compiled chunks. | 1597 | are both able to execute pre-compiled chunks. |
1588 | They automatically detect whether the chunk is text or binary, | 1598 | They automatically detect whether the chunk is text or binary, |
@@ -1606,47 +1616,47 @@ To read the value of any global Lua variable, | |||
1606 | one uses the function: | 1616 | one uses the function: |
1607 | \Deffunc{lua_getglobal} | 1617 | \Deffunc{lua_getglobal} |
1608 | \begin{verbatim} | 1618 | \begin{verbatim} |
1609 | lua_Object lua_getglobal (char *varname); | 1619 | lua_Object lua_getglobal (char *varname); |
1610 | \end{verbatim} | 1620 | \end{verbatim} |
1611 | As in Lua, this function may trigger a tag method. | 1621 | As in Lua, this function may trigger a tag method. |
1612 | To read the real value of any global variable, | 1622 | To read the real value of any global variable, |
1613 | without invoking any tag method, | 1623 | without invoking any tag method, |
1614 | this function has a \emph{raw} version: | 1624 | use the \emph{raw} version: |
1615 | \Deffunc{lua_rawgetglobal} | 1625 | \Deffunc{lua_rawgetglobal} |
1616 | \begin{verbatim} | 1626 | \begin{verbatim} |
1617 | lua_Object lua_rawgetglobal (char *varname); | 1627 | lua_Object lua_rawgetglobal (char *varname); |
1618 | \end{verbatim} | 1628 | \end{verbatim} |
1619 | 1629 | ||
1620 | To store a value previously pushed onto C2lua in a global variable, | 1630 | To store a value previously pushed onto C2lua in a global variable, |
1621 | there is the function: | 1631 | there is the function: |
1622 | \Deffunc{lua_setglobal} | 1632 | \Deffunc{lua_setglobal} |
1623 | \begin{verbatim} | 1633 | \begin{verbatim} |
1624 | void lua_setglobal (char *varname); | 1634 | void lua_setglobal (char *varname); |
1625 | \end{verbatim} | 1635 | \end{verbatim} |
1626 | As in Lua, this function may trigger a tag method. | 1636 | As in Lua, this function may trigger a tag method. |
1627 | To set the real value of any global variable, | 1637 | To set the real value of any global variable, |
1628 | without invoking any tag method, | 1638 | without invoking any tag method, |
1629 | this function has a \emph{raw} version: | 1639 | use the \emph{raw} version: |
1630 | \Deffunc{lua_rawgetglobal} | 1640 | \Deffunc{lua_rawgetglobal} |
1631 | \begin{verbatim} | 1641 | \begin{verbatim} |
1632 | void lua_rawsetglobal (char *varname); | 1642 | void lua_rawsetglobal (char *varname); |
1633 | \end{verbatim} | 1643 | \end{verbatim} |
1634 | 1644 | ||
1635 | Tables can also be manipulated via the API. | 1645 | Tables can also be manipulated via the API. |
1636 | The function | 1646 | The function |
1637 | \Deffunc{lua_gettable} | 1647 | \Deffunc{lua_gettable} |
1638 | \begin{verbatim} | 1648 | \begin{verbatim} |
1639 | lua_Object lua_gettable (void); | 1649 | lua_Object lua_gettable (void); |
1640 | \end{verbatim} | 1650 | \end{verbatim} |
1641 | pops from the stack C2lua a table and an index, | 1651 | pops from the stack C2lua a table and an index, |
1642 | and returns the contents of the table at that index. | 1652 | and returns the contents of the table at that index. |
1643 | As in Lua, this operation may trigger a tag method. | 1653 | As in Lua, this operation may trigger a tag method. |
1644 | To get the real value of any table index, | 1654 | To get the real value of any table index, |
1645 | without invoking any tag method, | 1655 | without invoking any tag method, |
1646 | this function has a \emph{raw} version: | 1656 | use the \emph{raw} version: |
1647 | \Deffunc{lua_rawgetglobal} | 1657 | \Deffunc{lua_rawgetglobal} |
1648 | \begin{verbatim} | 1658 | \begin{verbatim} |
1649 | lua_Object lua_rawgettable (void); | 1659 | lua_Object lua_rawgettable (void); |
1650 | \end{verbatim} | 1660 | \end{verbatim} |
1651 | 1661 | ||
1652 | To store a value in an index, | 1662 | To store a value in an index, |
@@ -1655,35 +1665,35 @@ and the value onto C2lua, | |||
1655 | and then call the function: | 1665 | and then call the function: |
1656 | \Deffunc{lua_settable} | 1666 | \Deffunc{lua_settable} |
1657 | \begin{verbatim} | 1667 | \begin{verbatim} |
1658 | void lua_settable (void); | 1668 | void lua_settable (void); |
1659 | \end{verbatim} | 1669 | \end{verbatim} |
1660 | Again, the tag method for ``settable'' may be called. | 1670 | Again, the tag method for ``settable'' may be called. |
1661 | To set the real value of any table index, | 1671 | To set the real value of any table index, |
1662 | without invoking any tag method, | 1672 | without invoking any tag method, |
1663 | this function has a \emph{raw} version: | 1673 | use the \emph{raw} version: |
1664 | \Deffunc{lua_rawsettable} | 1674 | \Deffunc{lua_rawsettable} |
1665 | \begin{verbatim} | 1675 | \begin{verbatim} |
1666 | void lua_rawsettable (void); | 1676 | void lua_rawsettable (void); |
1667 | \end{verbatim} | 1677 | \end{verbatim} |
1668 | 1678 | ||
1669 | Finally, the function | 1679 | Finally, the function |
1670 | \Deffunc{lua_createtable} | 1680 | \Deffunc{lua_createtable} |
1671 | \begin{verbatim} | 1681 | \begin{verbatim} |
1672 | lua_Object lua_createtable (void); | 1682 | lua_Object lua_createtable (void); |
1673 | \end{verbatim} | 1683 | \end{verbatim} |
1674 | creates and returns a new, empty table. | 1684 | creates and returns a new, empty table. |
1675 | 1685 | ||
1676 | 1686 | ||
1677 | \subsection{Calling Lua Functions} | 1687 | \subsection{Calling Lua Functions} |
1678 | Functions defined in Lua by a chunk executed with | 1688 | Functions defined in Lua by a chunk |
1679 | \verb|dofile| or \verb|dostring| can be called from the host program. | 1689 | can be called from the host program. |
1680 | This is done using the following protocol: | 1690 | This is done using the following protocol: |
1681 | first, the arguments to the function are pushed onto C2lua | 1691 | first, the arguments to the function are pushed onto C2lua |
1682 | \see{pushing}, in direct order, i.e., the first argument is pushed first. | 1692 | \see{pushing}, in direct order, i.e., the first argument is pushed first. |
1683 | Then, the function is called using | 1693 | Then, the function is called using |
1684 | \Deffunc{lua_callfunction} | 1694 | \Deffunc{lua_callfunction} |
1685 | \begin{verbatim} | 1695 | \begin{verbatim} |
1686 | int lua_callfunction (lua_Object function); | 1696 | int lua_callfunction (lua_Object function); |
1687 | \end{verbatim} | 1697 | \end{verbatim} |
1688 | This function returns an error code: | 1698 | This function returns an error code: |
1689 | 0, in case of success; non zero, in case of errors. | 1699 | 0, in case of success; non zero, in case of errors. |
@@ -1691,24 +1701,26 @@ Finally, the results (a Lua function may return many values) | |||
1691 | are returned in structure lua2C, | 1701 | are returned in structure lua2C, |
1692 | and can be retrieved with the macro \verb|lua_getresult|, | 1702 | and can be retrieved with the macro \verb|lua_getresult|, |
1693 | \Deffunc{lua_getresult} | 1703 | \Deffunc{lua_getresult} |
1694 | which is just another name to the function \verb|lua_lua2C|. | 1704 | which is just another name to function \verb|lua_lua2C|. |
1695 | Notice that the function \verb|lua_callfunction| | 1705 | Note that function \verb|lua_callfunction| |
1696 | pops all elements from the C2lua stack. | 1706 | pops all elements from the C2lua stack. |
1697 | 1707 | ||
1698 | The following example shows how a C program may do the | 1708 | The following example shows how a C program may do the |
1699 | equivalent to the Lua code: | 1709 | equivalent to the Lua code: |
1700 | \begin{verbatim} | 1710 | \begin{verbatim} |
1701 | a = f("how", t.x, 4) | 1711 | a,b = f("how", t.x, 4) |
1702 | \end{verbatim} | 1712 | \end{verbatim} |
1703 | \begin{verbatim} | 1713 | \begin{verbatim} |
1704 | lua_pushstring("how"); /* 1st argument */ | 1714 | lua_pushstring("how"); /* 1st argument */ |
1705 | lua_pushobject(lua_getglobal("t")); /* push value of global 't' */ | 1715 | lua_pushobject(lua_getglobal("t")); /* push value of global 't' */ |
1706 | lua_pushstring("x"); /* push the string 'x' */ | 1716 | lua_pushstring("x"); /* push the string 'x' */ |
1707 | lua_pushobject(lua_gettable()); /* push result of t.x (2nd arg) */ | 1717 | lua_pushobject(lua_gettable()); /* push result of t.x (2nd arg) */ |
1708 | lua_pushnumber(4); /* 3th argument */ | 1718 | lua_pushnumber(4); /* 3rd argument */ |
1709 | lua_callfunction(lua_getglobal("f")); /* call Lua function */ | 1719 | lua_callfunction(lua_getglobal("f")); /* call Lua function */ |
1710 | lua_pushobject(lua_getresult(1)); /* push first result of the call */ | 1720 | lua_pushobject(lua_getresult(1)); /* push first result of the call */ |
1711 | lua_setglobal("a"); /* sets global variable 'a' */ | 1721 | lua_setglobal("a"); /* sets global variable 'a' */ |
1722 | lua_pushobject(lua_getresult(2)); /* push second result of the call */ | ||
1723 | lua_setglobal("b"); /* sets global variable 'b' */ | ||
1712 | \end{verbatim} | 1724 | \end{verbatim} |
1713 | 1725 | ||
1714 | Some special Lua functions have exclusive interfaces. | 1726 | Some special Lua functions have exclusive interfaces. |
@@ -1726,36 +1738,34 @@ The \verb|message| is passed to the error handler method. | |||
1726 | If \verb|message| is \verb|NULL|, | 1738 | If \verb|message| is \verb|NULL|, |
1727 | the error handler method is not called. | 1739 | the error handler method is not called. |
1728 | 1740 | ||
1729 | The error handler method \see{error} can be changed with: | 1741 | The error handler method \see{error} can be |
1730 | \Deffunc{lua_seterrormethod} | 1742 | changed with: \Deffunc{lua_seterrormethod} |
1731 | \begin{verbatim} | 1743 | \begin{verbatim} |
1732 | lua_Object lua_seterrormethod (void); | 1744 | lua_Object lua_seterrormethod (void); |
1733 | \end{verbatim} | 1745 | \end{verbatim} |
1734 | This function sets the object at the top of C2lua | 1746 | This function sets the object at the top of C2lua |
1735 | as the new error method, | 1747 | as the new error method, |
1736 | and returns the old error method value. | 1748 | and returns the old error method value. |
1737 | 1749 | ||
1738 | Tag methods can be changed with: | 1750 | Tag methods can be changed with: \Deffunc{lua_settagmethod} |
1739 | \Deffunc{lua_settagmethod} | ||
1740 | \begin{verbatim} | 1751 | \begin{verbatim} |
1741 | lua_Object lua_settagmethod (int tag, char *event); | 1752 | lua_Object lua_settagmethod (int tag, char *event); |
1742 | \end{verbatim} | 1753 | \end{verbatim} |
1743 | The first parameter is the tag, | 1754 | The first parameter is the tag, |
1744 | the second is the event name \see{tag-method}; | 1755 | and the second is the event name \see{tag-method}; |
1745 | the new method is pushed from C2lua. | 1756 | the new method is pushed from C2lua. |
1746 | This function returns a \verb|lua_Object|, | 1757 | This function returns a \verb|lua_Object|, |
1747 | which is the old tag method value. | 1758 | which is the old tag method value. |
1748 | To get just the current value of a tag method, | 1759 | To get just the current value of a tag method, |
1749 | there is the function | 1760 | use the function \Deffunc{lua_gettagmethod} |
1750 | \Deffunc{lua_gettagmethod} | ||
1751 | \begin{verbatim} | 1761 | \begin{verbatim} |
1752 | lua_Object lua_gettagmethod (int tag, char *event); | 1762 | lua_Object lua_gettagmethod (int tag, char *event); |
1753 | \end{verbatim} | 1763 | \end{verbatim} |
1754 | 1764 | ||
1755 | It is also possible to copy all tag methods from one tag to another: | 1765 | It is also possible to copy all tag methods from one tag |
1756 | \Deffunc{lua_copytagmethods} | 1766 | to another: \Deffunc{lua_copytagmethods} |
1757 | \begin{verbatim} | 1767 | \begin{verbatim} |
1758 | int lua_copytagmethods (int tagto, int tagfrom); | 1768 | int lua_copytagmethods (int tagto, int tagfrom); |
1759 | \end{verbatim} | 1769 | \end{verbatim} |
1760 | This function returns \verb|tagto|. | 1770 | This function returns \verb|tagto|. |
1761 | 1771 | ||
@@ -1798,15 +1808,15 @@ as common arguments. | |||
1798 | To associate upvalues to a function, | 1808 | To associate upvalues to a function, |
1799 | first these values must be pushed on C2lua. | 1809 | first these values must be pushed on C2lua. |
1800 | Then the function: | 1810 | Then the function: |
1801 | \Deffunc{lua_pushCclosure} | 1811 | \Deffunc{lua_pushcclosure} |
1802 | \begin{verbatim} | 1812 | \begin{verbatim} |
1803 | void lua_pushCclosure (lua_CFunction fn, int n); | 1813 | void lua_pushcclosure (lua_CFunction fn, int n); |
1804 | \end{verbatim} | 1814 | \end{verbatim} |
1805 | is used to put the C function on C2lua, | 1815 | is used to put the C function on C2lua, |
1806 | with the argument \verb|n| telling how many upvalues must be | 1816 | with the argument \verb|n| telling how many upvalues must be |
1807 | associated with the function; | 1817 | associated with the function; |
1808 | in fact, the macro \verb|lua_pushcfunction| is defined as | 1818 | in fact, the macro \verb|lua_pushcfunction| is defined as |
1809 | \verb|lua_pushCclosure| with \verb|n| set to 0. | 1819 | \verb|lua_pushcclosure| with \verb|n| set to 0. |
1810 | Then, any time the function is called, | 1820 | Then, any time the function is called, |
1811 | these upvalues are inserted as the first arguments to the function, | 1821 | these upvalues are inserted as the first arguments to the function, |
1812 | before the actual arguments provided in the call. | 1822 | before the actual arguments provided in the call. |
@@ -1824,16 +1834,16 @@ The routines to manipulate references are the following: | |||
1824 | \Deffunc{lua_ref}\Deffunc{lua_getref} | 1834 | \Deffunc{lua_ref}\Deffunc{lua_getref} |
1825 | \Deffunc{lua_unref} | 1835 | \Deffunc{lua_unref} |
1826 | \begin{verbatim} | 1836 | \begin{verbatim} |
1827 | int lua_ref (int lock); | 1837 | int lua_ref (int lock); |
1828 | lua_Object lua_getref (int ref); | 1838 | lua_Object lua_getref (int ref); |
1829 | void lua_unref (int ref); | 1839 | void lua_unref (int ref); |
1830 | \end{verbatim} | 1840 | \end{verbatim} |
1831 | The function \verb|lua_ref| creates a reference | 1841 | The function \verb|lua_ref| creates a reference |
1832 | to the object that is on the top of the stack, | 1842 | to the object that is on the top of the stack, |
1833 | and returns this reference. | 1843 | and returns this reference. |
1834 | If \verb|lock| is true, the object is \emph{locked}: | 1844 | If \verb|lock| is true, the object is \emph{locked}: |
1835 | this means the object will not be garbage collected. | 1845 | this means the object will not be garbage collected. |
1836 | Notice that an unlocked reference may be garbage collected. | 1846 | Note that an unlocked reference may be garbage collected. |
1837 | Whenever the referenced object is needed, | 1847 | Whenever the referenced object is needed, |
1838 | a call to \verb|lua_getref| | 1848 | a call to \verb|lua_getref| |
1839 | returns a handle to it; | 1849 | returns a handle to it; |
@@ -1868,7 +1878,7 @@ To have access to these libraries, | |||
1868 | the C host program must call the functions | 1878 | the C host program must call the functions |
1869 | \verb|lua_strlibopen|, \verb|lua_mathlibopen|, | 1879 | \verb|lua_strlibopen|, \verb|lua_mathlibopen|, |
1870 | and \verb|lua_iolibopen|, declared in \verb|lualib.h|. | 1880 | and \verb|lua_iolibopen|, declared in \verb|lualib.h|. |
1871 | \Deffunc{lua_strlibopen}\Deffunc{lua_mathlibopen}\Deffunc{lua_iolibopen}. | 1881 | \Deffunc{lua_strlibopen}\Deffunc{lua_mathlibopen}\Deffunc{lua_iolibopen} |
1872 | 1882 | ||
1873 | 1883 | ||
1874 | \subsection{Predefined Functions} \label{predefined} | 1884 | \subsection{Predefined Functions} \label{predefined} |
@@ -1882,7 +1892,7 @@ The call is equivalent to | |||
1882 | func(arg[1], arg[2], ..., arg[arg.n]) | 1892 | func(arg[1], arg[2], ..., arg[arg.n]) |
1883 | \end{verbatim} | 1893 | \end{verbatim} |
1884 | If \verb|arg.n| is not defined, | 1894 | If \verb|arg.n| is not defined, |
1885 | then Lua stops getting arguments at the first nil value. | 1895 | then Lua stops getting arguments at the first \nil\ value. |
1886 | 1896 | ||
1887 | By default, | 1897 | By default, |
1888 | all results from \verb|func| are just returned by the call. | 1898 | all results from \verb|func| are just returned by the call. |
@@ -1921,7 +1931,7 @@ Returns the number of objects collected. | |||
1921 | An optional argument, \verb|limit|, is a number that | 1931 | An optional argument, \verb|limit|, is a number that |
1922 | makes the next cycle occur only after that number of new | 1932 | makes the next cycle occur only after that number of new |
1923 | objects have been created. | 1933 | objects have been created. |
1924 | If absent, Lua uses an adaptable algorithm to set | 1934 | If absent, Lua uses an adaptive algorithm to set |
1925 | this limit. | 1935 | this limit. |
1926 | \verb|collectgarbage| is equivalent to | 1936 | \verb|collectgarbage| is equivalent to |
1927 | the API function \verb|lua_collectgarbage|. | 1937 | the API function \verb|lua_collectgarbage|. |
@@ -1941,7 +1951,8 @@ It issues an error when called with a non string argument. | |||
1941 | 1951 | ||
1942 | \subsubsection*{\ff \T{dostring (string)}}\Deffunc{dostring} | 1952 | \subsubsection*{\ff \T{dostring (string)}}\Deffunc{dostring} |
1943 | This function executes a given string as a Lua chunk. | 1953 | This function executes a given string as a Lua chunk. |
1944 | If there is any error executing the string, it returns \nil. | 1954 | If there is any error executing the string, |
1955 | \verb|dostring| returns \nil. | ||
1945 | Otherwise, it returns the values returned by the chunk, | 1956 | Otherwise, it returns the values returned by the chunk, |
1946 | or a non \nil\ value if the chunk returns no values. | 1957 | or a non \nil\ value if the chunk returns no values. |
1947 | \verb|dostring| is equivalent to the API function \verb|lua_dostring|. | 1958 | \verb|dostring| is equivalent to the API function \verb|lua_dostring|. |
@@ -1977,7 +1988,7 @@ This function cannot be written with the standard API. | |||
1977 | 1988 | ||
1978 | \subsubsection*{\ff \T{nextvar (name)}}\Deffunc{nextvar} | 1989 | \subsubsection*{\ff \T{nextvar (name)}}\Deffunc{nextvar} |
1979 | This function is similar to the function \verb|next|, | 1990 | This function is similar to the function \verb|next|, |
1980 | but iterates over the global variables. | 1991 | but iterates instead over the global variables. |
1981 | Its single argument is the name of a global variable, | 1992 | Its single argument is the name of a global variable, |
1982 | or \nil\ to get a first name. | 1993 | or \nil\ to get a first name. |
1983 | Similarly to \verb|next|, it returns the name of another variable | 1994 | Similarly to \verb|next|, it returns the name of another variable |
@@ -1992,7 +2003,7 @@ This function cannot be written with the standard API. | |||
1992 | Executes the given \verb|function| over all elements of \verb|table|. | 2003 | Executes the given \verb|function| over all elements of \verb|table|. |
1993 | For each element, the function is called with the index and | 2004 | For each element, the function is called with the index and |
1994 | respective value as arguments. | 2005 | respective value as arguments. |
1995 | If the function returns any non-nil value, | 2006 | If the function returns any non-\nil\ value, |
1996 | the loop is broken, and the value is returned | 2007 | the loop is broken, and the value is returned |
1997 | as the final value of \verb|foreach|. | 2008 | as the final value of \verb|foreach|. |
1998 | 2009 | ||
@@ -2031,12 +2042,14 @@ end | |||
2031 | \subsubsection*{\ff \T{tostring (e)}}\Deffunc{tostring} | 2042 | \subsubsection*{\ff \T{tostring (e)}}\Deffunc{tostring} |
2032 | This function receives an argument of any type and | 2043 | This function receives an argument of any type and |
2033 | converts it to a string in a reasonable format. | 2044 | converts it to a string in a reasonable format. |
2045 | For complete control on how numbers are converted, | ||
2046 | use function \verb|format|. | ||
2034 | 2047 | ||
2035 | \subsubsection*{\ff \T{print (e1, e2, ...)}}\Deffunc{print} | 2048 | \subsubsection*{\ff \T{print (e1, e2, ...)}}\Deffunc{print} |
2036 | This function receives any number of arguments, | 2049 | This function receives any number of arguments, |
2037 | and prints their values using the strings returned by \verb|tostring|. | 2050 | and prints their values using the strings returned by \verb|tostring|. |
2038 | This function is not intended for formatted output, | 2051 | This function is not intended for formatted output, |
2039 | but as a quick way to show a value, | 2052 | but only as a quick way to show a value, |
2040 | for instance for error messages or debugging. | 2053 | for instance for error messages or debugging. |
2041 | See \See{libio} for functions for formatted output. | 2054 | See \See{libio} for functions for formatted output. |
2042 | 2055 | ||
@@ -2044,7 +2057,7 @@ See \See{libio} for functions for formatted output. | |||
2044 | This function receives one argument, | 2057 | This function receives one argument, |
2045 | and tries to convert it to a number. | 2058 | and tries to convert it to a number. |
2046 | If the argument is already a number or a string convertible | 2059 | If the argument is already a number or a string convertible |
2047 | to a number, then it returns that number; | 2060 | to a number, then \verb|tonumber| returns that number; |
2048 | otherwise, it returns \nil. | 2061 | otherwise, it returns \nil. |
2049 | 2062 | ||
2050 | An optional argument specifies the base to interpret the numeral. | 2063 | An optional argument specifies the base to interpret the numeral. |
@@ -2054,7 +2067,7 @@ represents 10, `B' represents 11, and so forth, with `Z' representing 35. | |||
2054 | 2067 | ||
2055 | In base 10 (the default), the number may have a decimal part, | 2068 | In base 10 (the default), the number may have a decimal part, |
2056 | as well as an optional exponent part \see{coercion}. | 2069 | as well as an optional exponent part \see{coercion}. |
2057 | In other bases only integers are accepted. | 2070 | In other bases, only integers are accepted. |
2058 | 2071 | ||
2059 | \subsubsection*{\ff \T{type (v)}}\Deffunc{type}\label{pdf-type} | 2072 | \subsubsection*{\ff \T{type (v)}}\Deffunc{type}\label{pdf-type} |
2060 | This function allows Lua to test the type of a value. | 2073 | This function allows Lua to test the type of a value. |
@@ -2097,10 +2110,11 @@ end | |||
2097 | \subsubsection*{\ff \T{error (message)}}\Deffunc{error}\label{pdf-error} | 2110 | \subsubsection*{\ff \T{error (message)}}\Deffunc{error}\label{pdf-error} |
2098 | This function calls the error handler and then terminates | 2111 | This function calls the error handler and then terminates |
2099 | the last protected function called | 2112 | the last protected function called |
2100 | (in C: \verb|lua_dofile|, \verb|lua_dostring|, or \verb|lua_callfunction|; | 2113 | (in~C: \verb|lua_dofile|, \verb|lua_dostring|, |
2114 | \verb|lua_dobuffer|, or \verb|lua_callfunction|; | ||
2101 | in Lua: \verb|dofile|, \verb|dostring|, or \verb|call| in protected mode). | 2115 | in Lua: \verb|dofile|, \verb|dostring|, or \verb|call| in protected mode). |
2102 | If \verb|message| is \nil, the error handler is not called. | 2116 | If \verb|message| is \nil, the error handler is not called. |
2103 | It never returns. | 2117 | Function \verb|error| never returns. |
2104 | \verb|error| is equivalent to the API function \verb|lua_error|. | 2118 | \verb|error| is equivalent to the API function \verb|lua_error|. |
2105 | 2119 | ||
2106 | \subsubsection*{\ff \T{rawgettable (table, index)}}\Deffunc{rawgettable} | 2120 | \subsubsection*{\ff \T{rawgettable (table, index)}}\Deffunc{rawgettable} |
@@ -2110,7 +2124,7 @@ without invoking any tag method. | |||
2110 | and \verb|index| is any value different from \nil. | 2124 | and \verb|index| is any value different from \nil. |
2111 | 2125 | ||
2112 | \subsubsection*{\ff \T{rawsettable (table, index, value)}}\Deffunc{rawsettable} | 2126 | \subsubsection*{\ff \T{rawsettable (table, index, value)}}\Deffunc{rawsettable} |
2113 | Sets the real value \verb|table[index]=value|, | 2127 | Sets the real value of \verb|table[index]| to \verb|value|, |
2114 | without invoking any tag method. | 2128 | without invoking any tag method. |
2115 | \verb|table| must be a table, | 2129 | \verb|table| must be a table, |
2116 | \verb|index| is any value different from \nil, | 2130 | \verb|index| is any value different from \nil, |
@@ -2123,10 +2137,7 @@ syntactically valid variable name. | |||
2123 | Therefore, | 2137 | Therefore, |
2124 | this function can set global variables with strange names like | 2138 | this function can set global variables with strange names like |
2125 | \verb|"m v 1"| or \verb|34|. | 2139 | \verb|"m v 1"| or \verb|34|. |
2126 | It returns the value of its second argument. | 2140 | Function \verb|rawsetglobal| returns the value of its second argument. |
2127 | |||
2128 | The string \verb|name| does not need to be a | ||
2129 | syntactically valid variable name. | ||
2130 | 2141 | ||
2131 | \subsubsection*{\ff \T{setglobal (name, value)}}\Deffunc{setglobal} | 2142 | \subsubsection*{\ff \T{setglobal (name, value)}}\Deffunc{setglobal} |
2132 | This function assigns the given value to a global variable, | 2143 | This function assigns the given value to a global variable, |
@@ -2134,6 +2145,7 @@ or calls a tag method. | |||
2134 | Its full semantics is explained in \See{tag-method}. | 2145 | Its full semantics is explained in \See{tag-method}. |
2135 | The string \verb|name| does not need to be a | 2146 | The string \verb|name| does not need to be a |
2136 | syntactically valid variable name. | 2147 | syntactically valid variable name. |
2148 | Function \verb|setglobal| returns the value of its second argument. | ||
2137 | 2149 | ||
2138 | \subsubsection*{\ff \T{rawgetglobal (name)}}\Deffunc{rawgetglobal} | 2150 | \subsubsection*{\ff \T{rawgetglobal (name)}}\Deffunc{rawgetglobal} |
2139 | This function retrieves the value of a global variable. | 2151 | This function retrieves the value of a global variable. |
@@ -2144,7 +2156,6 @@ syntactically valid variable name. | |||
2144 | This function retrieves the value of a global variable, | 2156 | This function retrieves the value of a global variable, |
2145 | or calls a tag method. | 2157 | or calls a tag method. |
2146 | Its full semantics is explained in \See{tag-method}. | 2158 | Its full semantics is explained in \See{tag-method}. |
2147 | |||
2148 | The string \verb|name| does not need to be a | 2159 | The string \verb|name| does not need to be a |
2149 | syntactically valid variable name. | 2160 | syntactically valid variable name. |
2150 | 2161 | ||
@@ -2160,7 +2171,7 @@ Returns the old error handler. | |||
2160 | This function sets a new tag method to the given pair \M{(tag, event)}. | 2171 | This function sets a new tag method to the given pair \M{(tag, event)}. |
2161 | It returns the old method. | 2172 | It returns the old method. |
2162 | If \verb|newmethod| is \nil, | 2173 | If \verb|newmethod| is \nil, |
2163 | it restores the default behavior for the given event. | 2174 | \verb|settagmethod| restores the default behavior for the given event. |
2164 | 2175 | ||
2165 | \subsubsection*{\ff \T{gettagmethod (tag, event)}} | 2176 | \subsubsection*{\ff \T{gettagmethod (tag, event)}} |
2166 | \Deffunc{gettagmethod} | 2177 | \Deffunc{gettagmethod} |
@@ -2193,7 +2204,7 @@ its default value is 1. | |||
2193 | If \verb|init| is negative, | 2204 | If \verb|init| is negative, |
2194 | it is replaced by the length of the string minus its | 2205 | it is replaced by the length of the string minus its |
2195 | absolute value plus 1. | 2206 | absolute value plus 1. |
2196 | Therefore, \M{-1} points to the last character of \verb|str|. | 2207 | Therefore, \Math{-1} points to the last character of \verb|str|. |
2197 | A value of 1 as a fourth optional argument | 2208 | A value of 1 as a fourth optional argument |
2198 | turns off the pattern matching facilities, | 2209 | turns off the pattern matching facilities, |
2199 | so the function does a plain ``find substring'' operation, | 2210 | so the function does a plain ``find substring'' operation, |
@@ -2208,9 +2219,9 @@ starting at \verb|i| and running until \verb|j|. | |||
2208 | If \verb|i| or \verb|j| are negative, | 2219 | If \verb|i| or \verb|j| are negative, |
2209 | they are replaced by the length of the string minus their | 2220 | they are replaced by the length of the string minus their |
2210 | absolute value plus 1. | 2221 | absolute value plus 1. |
2211 | Therefore, \M{-1} points to the last character of \verb|s| | 2222 | Therefore, \Math{-1} points to the last character of \verb|s| |
2212 | and \M{-2} to the previous one. | 2223 | and \Math{-2} to the previous one. |
2213 | If \verb|j| is absent, it is assumed to be equal to \M{-1} | 2224 | If \verb|j| is absent, it is assumed to be equal to \Math{-1} |
2214 | (which is the same as the string length). | 2225 | (which is the same as the string length). |
2215 | In particular, | 2226 | In particular, |
2216 | the call \verb|strsub(s,1,j)| returns a prefix of \verb|s| | 2227 | the call \verb|strsub(s,1,j)| returns a prefix of \verb|s| |
@@ -2242,7 +2253,9 @@ If \verb|i| is absent, then it is assumed to be 1. | |||
2242 | If \verb|i| is negative, | 2253 | If \verb|i| is negative, |
2243 | it is replaced by the length of the string minus its | 2254 | it is replaced by the length of the string minus its |
2244 | absolute value plus 1. | 2255 | absolute value plus 1. |
2245 | Therefore, \M{-1} points to the last character of \verb|s|. | 2256 | Therefore, \Math{-1} points to the last character of \verb|s|. |
2257 | |||
2258 | Note that numerical codes are not necessarily portable across platforms. | ||
2246 | 2259 | ||
2247 | \subsubsection*{\ff \T{strchar (i1, i2, \ldots)}}\Deffunc{strchar} | 2260 | \subsubsection*{\ff \T{strchar (i1, i2, \ldots)}}\Deffunc{strchar} |
2248 | Receives 0 or more integers. | 2261 | Receives 0 or more integers. |
@@ -2250,6 +2263,8 @@ Returns a string with length equal to the number of arguments, | |||
2250 | wherein each character has the internal numerical code equal | 2263 | wherein each character has the internal numerical code equal |
2251 | to its correspondent argument. | 2264 | to its correspondent argument. |
2252 | 2265 | ||
2266 | Note that numerical codes are not necessarily portable across platforms. | ||
2267 | |||
2253 | \subsubsection*{\ff \T{format (formatstring, e1, e2, \ldots)}}\Deffunc{format} | 2268 | \subsubsection*{\ff \T{format (formatstring, e1, e2, \ldots)}}\Deffunc{format} |
2254 | \label{format} | 2269 | \label{format} |
2255 | This function returns a formatted version of its variable number of arguments | 2270 | This function returns a formatted version of its variable number of arguments |
@@ -2295,7 +2310,7 @@ the appropriate format string. | |||
2295 | For example, \verb|"%*g"| can be simulated with | 2310 | For example, \verb|"%*g"| can be simulated with |
2296 | \verb|"%"..width.."g"|. | 2311 | \verb|"%"..width.."g"|. |
2297 | 2312 | ||
2298 | \emph{Function \T{format} can only be used with strings that do not | 2313 | \emph{Note: function \T{format} can only be used with strings that do not |
2299 | contain zeros.} | 2314 | contain zeros.} |
2300 | 2315 | ||
2301 | \subsubsection*{\ff \T{gsub (s, pat, repl [, n])}} | 2316 | \subsubsection*{\ff \T{gsub (s, pat, repl [, n])}} |
@@ -2331,15 +2346,18 @@ See some examples below: | |||
2331 | x = gsub("hello world", "(%w%w*)", "%1 %1", 1) | 2346 | x = gsub("hello world", "(%w%w*)", "%1 %1", 1) |
2332 | --> x="hello hello world" | 2347 | --> x="hello hello world" |
2333 | 2348 | ||
2349 | x = gsub("hello world from Lua", "(%w%w*)%s*(%w%w*)", "%2 %1") | ||
2350 | --> x="world hello Lua from" | ||
2351 | |||
2334 | x = gsub("home = $HOME, user = $USER", "$(%w%w*)", getenv) | 2352 | x = gsub("home = $HOME, user = $USER", "$(%w%w*)", getenv) |
2335 | --> x="home = /home/roberto, user = roberto" (for instance) | 2353 | --> x="home = /home/roberto, user = roberto" (for instance) |
2336 | 2354 | ||
2337 | x = gsub("4+5 = $return 4+5$", "$(.-)%$", dostring) | 2355 | x = gsub("4+5 = $return 4+5$", "$(.-)%$", dostring) |
2338 | --> x="4+5 = 9" | 2356 | --> x="4+5 = 9" |
2339 | 2357 | ||
2340 | local t = {name="lua", version="3.0"} | 2358 | local t = {name="lua", version="3.1"} |
2341 | x = gsub("$name - $version", "$(%w%w*)", function (v) return %t[v] end) | 2359 | x = gsub("$name - $version", "$(%w%w*)", function (v) return %t[v] end) |
2342 | --> x="lua - 3.0" | 2360 | --> x="lua - 3.1" |
2343 | 2361 | ||
2344 | t = {n=0} | 2362 | t = {n=0} |
2345 | gsub("first second word", "(%w%w*)", | 2363 | gsub("first second word", "(%w%w*)", |
@@ -2356,7 +2374,7 @@ The following combinations are allowed in describing a character class: | |||
2356 | \begin{description} | 2374 | \begin{description} |
2357 | \item[\emph{x}] (where \emph{x} is any character not in the list \verb|()%.[*-?|) | 2375 | \item[\emph{x}] (where \emph{x} is any character not in the list \verb|()%.[*-?|) |
2358 | --- represents the character \emph{x} itself. | 2376 | --- represents the character \emph{x} itself. |
2359 | \item[\T{.}] --- represents all characters. | 2377 | \item[\T{.}] --- (a dot) represents all characters. |
2360 | \item[\T{\%a}] --- represents all letters. | 2378 | \item[\T{\%a}] --- represents all letters. |
2361 | \item[\T{\%A}] --- represents all non letter characters. | 2379 | \item[\T{\%A}] --- represents all non letter characters. |
2362 | \item[\T{\%d}] --- represents all digits. | 2380 | \item[\T{\%d}] --- represents all digits. |
@@ -2377,13 +2395,14 @@ Represents the class which is the union of all | |||
2377 | characters in char-set. | 2395 | characters in char-set. |
2378 | To include a \verb|]| in char-set, it must be the first character. | 2396 | To include a \verb|]| in char-set, it must be the first character. |
2379 | A range of characters may be specified by | 2397 | A range of characters may be specified by |
2380 | separating the end characters of the range with a \verb|-|; | 2398 | separating the end characters of the range with a \verb|-|. |
2381 | e.g., \verb|A-Z| specifies the English upper case characters. | ||
2382 | If \verb|-| appears as the first or last character of char-set, | 2399 | If \verb|-| appears as the first or last character of char-set, |
2383 | then it represents itself. | 2400 | then it represents itself. |
2384 | All classes \verb|%|\emph{x} described above can also be used as | 2401 | All classes \verb|%|\emph{x} described above can also be used as |
2385 | components in a char-set. | 2402 | components in a char-set. |
2386 | All other characters in char-set represent themselves. | 2403 | All other characters in char-set represent themselves. |
2404 | E.g., assuming an \emph{ascii} character set, | ||
2405 | \verb|[%dA-Fa-f]| specifies the hexa-decimal digits. | ||
2387 | \item[\T{[\^{ }char-set]}] --- | 2406 | \item[\T{[\^{ }char-set]}] --- |
2388 | represents the complement of char-set, | 2407 | represents the complement of char-set, |
2389 | where char-set is interpreted as above. | 2408 | where char-set is interpreted as above. |
@@ -2441,9 +2460,9 @@ that match captures are stored (\emph{captured}) for future use. | |||
2441 | Captures are numbered according to their left parentheses. | 2460 | Captures are numbered according to their left parentheses. |
2442 | For instance, in the pattern \verb|"(a*(.)%w(%s*))"|, | 2461 | For instance, in the pattern \verb|"(a*(.)%w(%s*))"|, |
2443 | the part of the string matching \verb|"a*(.)%w(%s*)"| is | 2462 | the part of the string matching \verb|"a*(.)%w(%s*)"| is |
2444 | stored as the first capture (and therefore has number 1); | 2463 | stored as the first capture (and therefore has number~1); |
2445 | the character matching \verb|.| is captured with number 2, | 2464 | the character matching \verb|.| is captured with number~2, |
2446 | and the part matching \verb|%s*| has number 3. | 2465 | and the part matching \verb|%s*| has number~3. |
2447 | 2466 | ||
2448 | 2467 | ||
2449 | \subsection{Mathematical Functions} \label{mathlib} | 2468 | \subsection{Mathematical Functions} \label{mathlib} |
@@ -2540,7 +2559,7 @@ it opens the named file, | |||
2540 | sets its handle as the value of \verb|_OUTPUT|, | 2559 | sets its handle as the value of \verb|_OUTPUT|, |
2541 | and returns this value. | 2560 | and returns this value. |
2542 | It does not close the current output file. | 2561 | It does not close the current output file. |
2543 | Notice that, if the file already exists, | 2562 | Note that, if the file already exists, |
2544 | then it will be \emph{completely erased} with this operation. | 2563 | then it will be \emph{completely erased} with this operation. |
2545 | When called without parameters, | 2564 | When called without parameters, |
2546 | this function closes the \verb|_OUTPUT| file, | 2565 | this function closes the \verb|_OUTPUT| file, |
@@ -2569,7 +2588,7 @@ this function does not erase any previous content of the file. | |||
2569 | If this function fails, it returns \nil, | 2588 | If this function fails, it returns \nil, |
2570 | plus a string describing the error. | 2589 | plus a string describing the error. |
2571 | 2590 | ||
2572 | Notice that function \verb|writeto| is | 2591 | Note that function \verb|writeto| is |
2573 | available to close an output file opened by \verb|appendto|. | 2592 | available to close an output file opened by \verb|appendto|. |
2574 | 2593 | ||
2575 | \subsubsection*{\ff \T{remove (filename)}}\Deffunc{remove} | 2594 | \subsubsection*{\ff \T{remove (filename)}}\Deffunc{remove} |
@@ -2594,7 +2613,7 @@ The file must be explicitly removed when no longer needed. | |||
2594 | 2613 | ||
2595 | This function reads the file \verb|_INPUT|, | 2614 | This function reads the file \verb|_INPUT|, |
2596 | or from \verb|filehandle| if this argument is given, | 2615 | or from \verb|filehandle| if this argument is given, |
2597 | according to a read pattern, that specifies how much to read; | 2616 | according to a read pattern, which specifies how much to read; |
2598 | characters are read from the input file until | 2617 | characters are read from the input file until |
2599 | the read pattern fails or ends. | 2618 | the read pattern fails or ends. |
2600 | The function \verb|read| returns a string with the characters read, | 2619 | The function \verb|read| returns a string with the characters read, |
@@ -2616,7 +2635,7 @@ it never fails. | |||
2616 | A character class followed by \verb|*| reads until a character that | 2635 | A character class followed by \verb|*| reads until a character that |
2617 | does not belong to the class, or end of file; | 2636 | does not belong to the class, or end of file; |
2618 | since it can match a sequence of zero characters, it never fails. | 2637 | since it can match a sequence of zero characters, it never fails. |
2619 | Notice that the behavior of read patterns is slightly different from | 2638 | Note that the behavior of read patterns is slightly different from |
2620 | the regular pattern matching behavior, | 2639 | the regular pattern matching behavior, |
2621 | where a \verb|*| expands to the maximum length \emph{such that} | 2640 | where a \verb|*| expands to the maximum length \emph{such that} |
2622 | the rest of the pattern does not fail. | 2641 | the rest of the pattern does not fail. |
@@ -2677,7 +2696,7 @@ The default value for \verb|code| is 1. | |||
2677 | 2696 | ||
2678 | \subsubsection*{\ff \T{getenv (varname)}}\Deffunc{getenv} | 2697 | \subsubsection*{\ff \T{getenv (varname)}}\Deffunc{getenv} |
2679 | 2698 | ||
2680 | Returns the value of the environment variable \verb|varname|, | 2699 | Returns the value of the process environment variable \verb|varname|, |
2681 | or \nil\ if the variable is not defined. | 2700 | or \nil\ if the variable is not defined. |
2682 | 2701 | ||
2683 | \subsubsection*{\ff \T{execute (command)}}\Deffunc{execute} | 2702 | \subsubsection*{\ff \T{execute (command)}}\Deffunc{execute} |
@@ -2690,10 +2709,10 @@ It returns an error code, which is system-dependent. | |||
2690 | 2709 | ||
2691 | This function is an interface to the ANSI C function \verb|setlocale|. | 2710 | This function is an interface to the ANSI C function \verb|setlocale|. |
2692 | \verb|locale| is a string specifying a locale; | 2711 | \verb|locale| is a string specifying a locale; |
2693 | \verb|category| is a number describing which category to change: | 2712 | \verb|category| is an optional string describing which category to change: |
2694 | 0 is \verb|LC_ALL|, 1 is \verb|LC_COLLATE|, 2 is \verb|LC_CTYPE|, | 2713 | \verb|"all"|, \verb|"collate"|, \verb|"ctype"|, |
2695 | 3 is \verb|LC_MONETARY|, 4 is \verb|LC_NUMERIC|, and 5 is \verb|LC_TIME|; | 2714 | \verb|"monetary"|, \verb|"numeric"|, or \verb|"time"|; |
2696 | the default category is \verb|LC_ALL|. | 2715 | the default category is \verb|"all"|. |
2697 | The function returns the name of the new locale, | 2716 | The function returns the name of the new locale, |
2698 | or \nil\ if the request cannot be honored. | 2717 | or \nil\ if the request cannot be honored. |
2699 | 2718 | ||
@@ -2741,13 +2760,14 @@ given function has been defined. | |||
2741 | If the ``function'' is in fact the main code of a chunk, | 2760 | If the ``function'' is in fact the main code of a chunk, |
2742 | then \verb|linedefined| is 0. | 2761 | then \verb|linedefined| is 0. |
2743 | If the function is a C function, | 2762 | If the function is a C function, |
2744 | then \verb|linedefined| is \M{-1}, and \verb|filename| is \verb|"(C)"|. | 2763 | then \verb|linedefined| is \Math{-1}, and \verb|filename| is \verb|"(C)"|. |
2745 | 2764 | ||
2746 | The function \verb|lua_currentline| gives the current line where | 2765 | The function \verb|lua_currentline| gives the current line where |
2747 | a given function is executing. | 2766 | a given function is executing. |
2748 | It only works if the function has been compiled with debug | 2767 | It only works if the function has been compiled with debug |
2749 | information \see{pragma}. | 2768 | information \see{pragma}. |
2750 | When no line information is available, it returns \M{-1}. | 2769 | When no line information is available, |
2770 | \verb|lua_currentline| returns \Math{-1}. | ||
2751 | 2771 | ||
2752 | Function \verb|lua_getobjname| tries to find a reasonable name for | 2772 | Function \verb|lua_getobjname| tries to find a reasonable name for |
2753 | a given function. | 2773 | a given function. |
@@ -2870,6 +2890,10 @@ Therefore, the prompt can be changed like below: | |||
2870 | $ lua _PROMPT='myprompt> ' -i | 2890 | $ lua _PROMPT='myprompt> ' -i |
2871 | \end{verbatim} | 2891 | \end{verbatim} |
2872 | 2892 | ||
2893 | In Unix systems, Lua scripts can be made into executable programs | ||
2894 | by using the \verb|#!| form, | ||
2895 | as in \verb|#!/usr/local/bin/lua|. | ||
2896 | |||
2873 | \section*{Acknowledgments} | 2897 | \section*{Acknowledgments} |
2874 | 2898 | ||
2875 | The authors would like to thank CENPES/PETROBRAS which, | 2899 | The authors would like to thank CENPES/PETROBRAS which, |
@@ -2897,8 +2921,8 @@ Here is a list of all these incompatibilities. | |||
2897 | Lua 3.1 must be explicitly opened before used, | 2921 | Lua 3.1 must be explicitly opened before used, |
2898 | with function \verb|lua_open|. | 2922 | with function \verb|lua_open|. |
2899 | However, all standard libraries check whether Lua is already opened, | 2923 | However, all standard libraries check whether Lua is already opened, |
2900 | so any program that opens at least one standard library before calling | 2924 | so any existing program that opens at least one standard |
2901 | Lua does not need to be modified. | 2925 | library before calling Lua does not need to be modified. |
2902 | 2926 | ||
2903 | \item Function \verb|dostring| no longer accepts an optional second argument, | 2927 | \item Function \verb|dostring| no longer accepts an optional second argument, |
2904 | with a temporary error method. | 2928 | with a temporary error method. |