diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1998-03-30 15:17:55 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1998-03-30 15:17:55 -0300 |
| commit | 0a0c9593b846c33781f3adea84a8e9125a747131 (patch) | |
| tree | 80d3830f4ef1f4db8f43981144c5ab8332ef88e1 | |
| parent | d4707925176f94a29e69ff92f6618f36ca1928dd (diff) | |
| download | lua-0a0c9593b846c33781f3adea84a8e9125a747131.tar.gz lua-0a0c9593b846c33781f3adea84a8e9125a747131.tar.bz2 lua-0a0c9593b846c33781f3adea84a8e9125a747131.zip | |
many small corrections; strings with '\0'
| -rw-r--r-- | manual.tex | 413 |
1 files changed, 265 insertions, 148 deletions
| @@ -1,4 +1,4 @@ | |||
| 1 | % $Id: manual.tex,v 1.5 1998/01/13 15:42:43 roberto Exp roberto $ | 1 | % $Id: manual.tex,v 1.6 1998/01/14 13:49:44 roberto Exp roberto $ |
| 2 | 2 | ||
| 3 | \documentstyle[fullpage,11pt,bnf]{article} | 3 | \documentstyle[fullpage,11pt,bnf]{article} |
| 4 | 4 | ||
| @@ -38,7 +38,7 @@ Waldemar Celes | |||
| 38 | \tecgraf\ --- Computer Science Department --- PUC-Rio | 38 | \tecgraf\ --- Computer Science Department --- PUC-Rio |
| 39 | } | 39 | } |
| 40 | 40 | ||
| 41 | \date{\small \verb$Date: 1998/01/13 15:42:43 $} | 41 | \date{\small \verb$Date: 1998/01/14 13:49:44 $} |
| 42 | 42 | ||
| 43 | \maketitle | 43 | \maketitle |
| 44 | 44 | ||
| @@ -47,21 +47,54 @@ Waldemar Celes | |||
| 47 | 47 | ||
| 48 | \begin{abstract} | 48 | \begin{abstract} |
| 49 | \noindent | 49 | \noindent |
| 50 | Lua is an extension programming language designed to be used | 50 | Lua is a programming language originally designed for extending applications, |
| 51 | as a configuration language for any program that needs one. | 51 | but also frequently used as a general-purpose, stand-alone language. |
| 52 | This document describes version \Version\ of the Lua programming language and | 52 | Lua combines simple procedural syntax (similar to Pascal) |
| 53 | the API that allows interaction between Lua programs and their host C programs. | 53 | with powerful data description constructs based on associative |
| 54 | arrays and extensible semantics. | ||
| 55 | Lua is dynamically typed, interpreted from bytecodes, | ||
| 56 | and has automatic memory management with garbage collection, | ||
| 57 | making it ideal for configuration, scripting, and rapid prototyping. | ||
| 58 | |||
| 59 | Lua is implemented as a small library of C functions, | ||
| 60 | written in ANSI C, and compiles unmodified in all known platforms. | ||
| 61 | The implementation goals are simplicity, efficiency, portability, | ||
| 62 | and low embedding cost. | ||
| 63 | |||
| 64 | Lua has been developed at TeCGraf, | ||
| 65 | the Computer Graphics Technology Group of PUC-Rio | ||
| 66 | (the Pontifical Catholic University of Rio de Janeiro in Brazil). | ||
| 67 | TeCGraf is a laboratory of the Department of Computer Science. | ||
| 68 | Dozens of industrial products developed by TeCGraf use Lua. | ||
| 69 | |||
| 70 | This document describes version \Version\ of the Lua programming language | ||
| 71 | and the API that allows interaction between Lua programs and their | ||
| 72 | host C programs. | ||
| 54 | \end{abstract} | 73 | \end{abstract} |
| 55 | |||
| 56 | \vspace{4ex} | 74 | \vspace{4ex} |
| 57 | \begin{quotation} | 75 | \begin{quotation} |
| 58 | \small | 76 | \small |
| 59 | \begin{center}{\bf Sum\'ario}\end{center} | 77 | \begin{center}{\bf Sum\'ario}\end{center} |
| 60 | \vspace{1ex} | 78 | \vspace{1ex} |
| 61 | \noindent | 79 | \noindent |
| 62 | Lua \'e uma linguagem de extens\~ao projetada para ser usada como | 80 | Lua \'e uma linguagem de programa\c{c}\~ao originalmente projetada para |
| 63 | linguagem de configura\c{c}\~ao em qualquer programa que precise de | 81 | extens\~ao de aplica\c{c}\~oes, |
| 64 | uma. | 82 | e que \'e tamb\'em frequentemente usada como uma linguagem de |
| 83 | prop\'osito geral. | ||
| 84 | Lua combina uma sintaxe procedural simples (similar a Pascal) | ||
| 85 | com poderosas facilidades para descri\c{c}\~ao de dados baseadas | ||
| 86 | em tabelas associativas e uma sem\^antica estens\'{\i}vel. | ||
| 87 | Lua tem tipagem din\^amica, \'e interpretada via bytecodes, | ||
| 88 | e tem gerenciamento autom\'atico de mem\'oria com coleta de lixo, | ||
| 89 | tornando-se ideal para configura\c{c}\~ao, scripting, | ||
| 90 | e prototipagem r\'apida. | ||
| 91 | |||
| 92 | Lua \'e implementada como uma pequena biblioteca de fun\c{c}\~oes C, | ||
| 93 | escrita em ANSI C, e compila sem modifica\c{c}\~oes em todas as | ||
| 94 | plataformas conhecidas. | ||
| 95 | Os objetivos da implementa\c{c}\~ao s\~ao simplicidade, efici\^encia, | ||
| 96 | portabilidade, e baixo custo. | ||
| 97 | |||
| 65 | Este documento descreve a vers\~ao \Version\ da linguagem de | 98 | Este documento descreve a vers\~ao \Version\ da linguagem de |
| 66 | programa\c{c}\~ao Lua e a Interface de Programa\c{c}\~ao (API) que permite | 99 | programa\c{c}\~ao Lua e a Interface de Programa\c{c}\~ao (API) que permite |
| 67 | a intera\c{c}\~ao entre programas Lua e programas C hospedeiros. | 100 | a intera\c{c}\~ao entre programas Lua e programas C hospedeiros. |
| @@ -74,7 +107,8 @@ a intera\c{c}\~ao entre programas Lua e programas C hospedeiros. | |||
| 74 | \footnotesize | 107 | \footnotesize |
| 75 | Copyright \copyright\ 1994--1998 TeCGraf, PUC-Rio. | 108 | Copyright \copyright\ 1994--1998 TeCGraf, PUC-Rio. |
| 76 | Written by Waldemar Celes Filho, | 109 | Written by Waldemar Celes Filho, |
| 77 | Roberto Ierusalimschy, Luiz Henrique de Figueiredo. | 110 | Roberto Ierusalimschy, |
| 111 | and Luiz Henrique de Figueiredo. | ||
| 78 | All rights reserved. | 112 | All rights reserved. |
| 79 | % | 113 | % |
| 80 | Permission is hereby granted, without written agreement and without license or | 114 | Permission is hereby granted, without written agreement and without license or |
| @@ -170,6 +204,7 @@ A chunk is simply a sequence of statements: | |||
| 170 | \begin{Produc} | 204 | \begin{Produc} |
| 171 | \produc{chunk}{\rep{stat} \opt{ret}} | 205 | \produc{chunk}{\rep{stat} \opt{ret}} |
| 172 | \end{Produc}% | 206 | \end{Produc}% |
| 207 | Statements are described in \See{stats}. | ||
| 173 | (As usual, \rep{\emph{a}} means 0 or more \emph{a}'s, | 208 | (As usual, \rep{\emph{a}} means 0 or more \emph{a}'s, |
| 174 | \opt{\emph{a}} means an optional \emph{a} and \oneormore{\emph{a}} means | 209 | \opt{\emph{a}} means an optional \emph{a} and \oneormore{\emph{a}} means |
| 175 | one or more \emph{a}'s.) | 210 | one or more \emph{a}'s.) |
| @@ -203,7 +238,8 @@ whose main property is to be different from any other value. | |||
| 203 | \emph{Number} represents real (floating-point) numbers, | 238 | \emph{Number} represents real (floating-point) numbers, |
| 204 | while \emph{string} has the usual meaning; | 239 | while \emph{string} has the usual meaning; |
| 205 | notice that Lua is \Index{eight-bit clean}, | 240 | notice that Lua is \Index{eight-bit clean}, |
| 206 | so strings can have ISO characters. | 241 | and so strings can contain any ISO character, |
| 242 | \emph{including} \verb|'\0'|. | ||
| 207 | The function \verb|type| returns a string describing the type | 243 | The function \verb|type| returns a string describing the type |
| 208 | of a given value \see{pdf-type}. | 244 | of a given value \see{pdf-type}. |
| 209 | 245 | ||
| @@ -222,7 +258,7 @@ arbitrary \Index{C pointers} to be stored in Lua variables. | |||
| 222 | It corresponds to a \verb|void*| and has no pre-defined operations in Lua, | 258 | It corresponds to a \verb|void*| and has no pre-defined operations in Lua, |
| 223 | besides assignment and equality test. | 259 | besides assignment and equality test. |
| 224 | However, by using \emph{tag methods}, | 260 | However, by using \emph{tag methods}, |
| 225 | the programmer may define operations for \emph{userdata} values | 261 | the programmer can define operations for \emph{userdata} values |
| 226 | \see{tag-method}. | 262 | \see{tag-method}. |
| 227 | 263 | ||
| 228 | The type \emph{table} implements \Index{associative arrays}, | 264 | The type \emph{table} implements \Index{associative arrays}, |
| @@ -230,6 +266,7 @@ that is, \Index{arrays} that can be indexed not only with numbers, | |||
| 230 | but with any value (except \nil). | 266 | but with any value (except \nil). |
| 231 | Therefore, this type may be used not only to represent ordinary arrays, | 267 | Therefore, this type may be used not only to represent ordinary arrays, |
| 232 | but also symbol tables, sets, records, etc. | 268 | but also symbol tables, sets, records, etc. |
| 269 | Tables are the main data structuring mechanism in Lua. | ||
| 233 | To represent \Index{records}, Lua uses the field name as an index. | 270 | To represent \Index{records}, Lua uses the field name as an index. |
| 234 | The language supports this representation by | 271 | The language supports this representation by |
| 235 | providing \verb|a.name| as syntactic sugar for \verb|a["name"]|. | 272 | providing \verb|a.name| as syntactic sugar for \verb|a["name"]|. |
| @@ -248,13 +285,15 @@ Moreover, tables must be explicitly created before used | |||
| 248 | \see{tableconstructor}. | 285 | \see{tableconstructor}. |
| 249 | 286 | ||
| 250 | Tags are mainly used to select tag methods when | 287 | Tags are mainly used to select tag methods when |
| 251 | some events occur \see{tag-method}. | 288 | some events occur. |
| 252 | Each of the types nil, number and string has a different tag. | 289 | Tag methods are the main mechanism for extending the |
| 290 | semantics of Lua \see{tag-method}. | ||
| 291 | Each of the types \M{nil}, \M{number} and \M{string} has a different tag. | ||
| 253 | All values of each of these types have this same pre-defined tag. | 292 | All values of each of these types have this same pre-defined tag. |
| 254 | Values of type function can have two different tags, | 293 | Values of type \M{function} can have two different tags, |
| 255 | depending on whether they are Lua or C functions. | 294 | depending on whether they are Lua or C functions. |
| 256 | Finally, | 295 | Finally, |
| 257 | values of type userdata and table can have | 296 | values of type \M{userdata} and \M{table} can have |
| 258 | as many different tags as needed \see{tag-method}. | 297 | as many different tags as needed \see{tag-method}. |
| 259 | Tags are created with the function \verb|newtag|, | 298 | Tags are created with the function \verb|newtag|, |
| 260 | and the function \verb|tag| returns the tag of a given value. | 299 | and the function \verb|tag| returns the tag of a given value. |
| @@ -296,7 +335,20 @@ The following strings denote other \Index{tokens}: | |||
| 296 | 335 | ||
| 297 | \Index{Literal strings} can be delimited by matching single or double quotes, | 336 | \Index{Literal strings} can be delimited by matching single or double quotes, |
| 298 | and can contain the C-like escape sequences | 337 | and can contain the C-like escape sequences |
| 299 | \verb|'\n'|, \verb|'\t'| and \verb|'\r'|. | 338 | \verb|'\a'| (bell), |
| 339 | \verb|'\b'| (back space), | ||
| 340 | \verb|'\f'| (form feed), | ||
| 341 | \verb|'\n'| (new line), | ||
| 342 | \verb|'\r'| (carriage return), | ||
| 343 | \verb|'\t'| (horizontal tab), | ||
| 344 | \verb|'\v'| (vertical tab), | ||
| 345 | \verb|'\\'|, | ||
| 346 | \verb|'\"'|, | ||
| 347 | and \verb|'\''|. | ||
| 348 | A character in a string may also be specified by its numerical value, | ||
| 349 | through the escape sequence \verb|'\ddd'|, | ||
| 350 | where \verb|ddd| is a sequence of up to three \emph{decimal} digits. | ||
| 351 | Strings in Lua may contain any 8-bit value, including 0. | ||
| 300 | Literal strings can also be delimited by matching \verb|[[ ... ]]|. | 352 | Literal strings can also be delimited by matching \verb|[[ ... ]]|. |
| 301 | Literals in this bracketed form may run for several lines, | 353 | Literals in this bracketed form may run for several lines, |
| 302 | may contain nested \verb|[[ ... ]]| pairs, | 354 | may contain nested \verb|[[ ... ]]| pairs, |
| @@ -304,6 +356,15 @@ and do not interpret escape sequences. | |||
| 304 | This form is specially convenient for | 356 | This form is specially convenient for |
| 305 | writing strings that contain program pieces or | 357 | writing strings that contain program pieces or |
| 306 | other quoted strings. | 358 | other quoted strings. |
| 359 | As an example, in a system using ASCII, | ||
| 360 | the following three literals are equivalent: | ||
| 361 | \begin{verbatim} | ||
| 362 | 1) "alo\n123\"" | ||
| 363 | 2) '\97lo\10\04923"' | ||
| 364 | 3) [[alo | ||
| 365 | 123"]] | ||
| 366 | \end{verbatim} | ||
| 367 | |||
| 307 | 368 | ||
| 308 | \Index{Comments} start anywhere outside a string with a | 369 | \Index{Comments} start anywhere outside a string with a |
| 309 | double hyphen (\verb|--|) and run until the end of the line. | 370 | double hyphen (\verb|--|) and run until the end of the line. |
| @@ -322,7 +383,7 @@ Examples of valid numerical constants are: | |||
| 322 | \subsection{The \Index{Pre-processor}} \label{pre-processor} | 383 | \subsection{The \Index{Pre-processor}} \label{pre-processor} |
| 323 | 384 | ||
| 324 | All lines that start with a \verb|$| are handled by a pre-processor. | 385 | All lines that start with a \verb|$| are handled by a pre-processor. |
| 325 | The \verb|$| can be followed by any of the following directives: | 386 | The \verb|$| must be immediately followed by one of the following directives: |
| 326 | \begin{description} | 387 | \begin{description} |
| 327 | \item[\T{debug}] --- turn on some debugging facilities \see{pragma}. | 388 | \item[\T{debug}] --- turn on some debugging facilities \see{pragma}. |
| 328 | \item[\T{nodebug}] --- turn off some debugging facilities \see{pragma}. | 389 | \item[\T{nodebug}] --- turn off some debugging facilities \see{pragma}. |
| @@ -332,11 +393,11 @@ If \M{cond} is false, then this part is skipped by the lexical analyzer. | |||
| 332 | If \M{cond} is true, then this part is skipped by the lexical analyzer. | 393 | If \M{cond} is true, then this part is skipped by the lexical analyzer. |
| 333 | \item[\T{end}] --- ends a conditional part. | 394 | \item[\T{end}] --- ends a conditional part. |
| 334 | \item[\T{else}] --- starts an ``else'' conditional part, | 395 | \item[\T{else}] --- starts an ``else'' conditional part, |
| 335 | switching the ``skip'' status. | 396 | flopping the ``skip'' status. |
| 336 | \item[\T{endinput}] --- ends the lexical parse of the file. | 397 | \item[\T{endinput}] --- ends the lexical parse of the file. |
| 337 | \end{description} | 398 | \end{description} |
| 338 | 399 | ||
| 339 | Directives can be freely nested. | 400 | Directives may be freely nested. |
| 340 | Particularly, a \verb|$endinput| may occur inside a \verb|$if|; | 401 | Particularly, a \verb|$endinput| may occur inside a \verb|$if|; |
| 341 | in that case, even the matching \verb|$end| is not parsed. | 402 | in that case, even the matching \verb|$end| is not parsed. |
| 342 | 403 | ||
| @@ -352,7 +413,7 @@ Therefore, actions in a chunk do not affect its own conditional directives. | |||
| 352 | 413 | ||
| 353 | \subsection{\Index{Coercion}} \label{coercion} | 414 | \subsection{\Index{Coercion}} \label{coercion} |
| 354 | 415 | ||
| 355 | Lua provides some automatic conversions between values. | 416 | Lua provides some automatic conversions between values at run time. |
| 356 | Any arithmetic operation applied to a string tries to convert | 417 | Any arithmetic operation applied to a string tries to convert |
| 357 | that string to a number, following the usual rules. | 418 | that string to a number, following the usual rules. |
| 358 | Conversely, whenever a number is used when a string is expected, | 419 | Conversely, whenever a number is used when a string is expected, |
| @@ -373,13 +434,15 @@ the system does not know how many values a function will return, | |||
| 373 | or how many parameters it needs. | 434 | or how many parameters it needs. |
| 374 | Therefore, sometimes, a list of values must be \emph{adjusted}, at run time, | 435 | Therefore, sometimes, a list of values must be \emph{adjusted}, at run time, |
| 375 | to a given length. | 436 | to a given length. |
| 376 | If there are more values than are needed, then the last values are thrown away. | 437 | If there are more values than are needed, |
| 377 | If there are more needs than values, then the list is extended with as | 438 | then the last values are thrown away. |
| 378 | many \nil's as needed. | 439 | If there are more needs than values, |
| 379 | Adjustment occurs in multiple assignment and function calls. | 440 | then the list is extended with as many \nil's as needed. |
| 441 | Adjustment occurs in multiple assignment \see{assignment} | ||
| 442 | and function calls \see{functioncall}. | ||
| 380 | 443 | ||
| 381 | 444 | ||
| 382 | \subsection{Statements} | 445 | \subsection{Statements}\label{stats} |
| 383 | 446 | ||
| 384 | Lua supports an almost conventional set of \Index{statements}, | 447 | Lua supports an almost conventional set of \Index{statements}, |
| 385 | similar to those in Pascal or C. | 448 | similar to those in Pascal or C. |
| @@ -391,7 +454,7 @@ and local variable declarations \see{localvar}. | |||
| 391 | 454 | ||
| 392 | \subsubsection{Blocks} | 455 | \subsubsection{Blocks} |
| 393 | A \Index{block} is a list of statements, which are executed sequentially. | 456 | A \Index{block} is a list of statements, which are executed sequentially. |
| 394 | Any statement can be optionally followed by a semicolon: | 457 | A statement may be optionally followed by a semicolon: |
| 395 | \begin{Produc} | 458 | \begin{Produc} |
| 396 | \produc{block}{\rep{stat sc} \opt{ret}} | 459 | \produc{block}{\rep{stat sc} \opt{ret}} |
| 397 | \produc{sc}{\opt{\ter{;}}} | 460 | \produc{sc}{\opt{\ter{;}}} |
| @@ -404,7 +467,7 @@ A block may be explicitly delimited: | |||
| 404 | \begin{Produc} | 467 | \begin{Produc} |
| 405 | \produc{stat}{\rwd{do} block \rwd{end}} | 468 | \produc{stat}{\rwd{do} block \rwd{end}} |
| 406 | \end{Produc}% | 469 | \end{Produc}% |
| 407 | This is useful to control the scope of local variables. | 470 | This is useful to control the scope of local variables \see{localvar}. |
| 408 | 471 | ||
| 409 | \subsubsection{\Index{Assignment}} \label{assignment} | 472 | \subsubsection{\Index{Assignment}} \label{assignment} |
| 410 | The language allows \Index{multiple assignment}. | 473 | The language allows \Index{multiple assignment}. |
| @@ -436,8 +499,15 @@ Square brackets are used to index a table: | |||
| 436 | \begin{Produc} | 499 | \begin{Produc} |
| 437 | \produc{var}{simpleexp \ter{[} exp1 \ter{]}} | 500 | \produc{var}{simpleexp \ter{[} exp1 \ter{]}} |
| 438 | \end{Produc}% | 501 | \end{Produc}% |
| 439 | The \verb|var| should result in a table value, | 502 | The \M{simpleexp} should result in a table value, |
| 440 | where the field indexed by the expression value gets the assigned value. | 503 | from where the field indexed by the expression |
| 504 | value gets the assigned value. | ||
| 505 | |||
| 506 | The syntax \verb|var.NAME| is just syntactic sugar for | ||
| 507 | \verb|var["NAME"]|: | ||
| 508 | \begin{Produc} | ||
| 509 | \produc{var}{simpleexp \ter{.} name} | ||
| 510 | \end{Produc}% | ||
| 441 | 511 | ||
| 442 | The meaning of assignments and evaluations of global variables and | 512 | The meaning of assignments and evaluations of global variables and |
| 443 | indexed variables can be changed by tag methods \see{tag-method}. | 513 | indexed variables can be changed by tag methods \see{tag-method}. |
| @@ -446,16 +516,10 @@ an assignment \verb|x = val|, where \verb|x| is a global variable, | |||
| 446 | is equivalent to a call \verb|setglobal('x', val)|; | 516 | is equivalent to a call \verb|setglobal('x', val)|; |
| 447 | an assignment \verb|t[i] = val| is equivalent to | 517 | an assignment \verb|t[i] = val| is equivalent to |
| 448 | \verb|settable_event(t, i, val)|. | 518 | \verb|settable_event(t, i, val)|. |
| 449 | See \See{tag-method} for a description of these functions. | 519 | See \See{tag-method} for a complete description of these functions. |
| 450 | (Function \verb|setglobal| is pre-defined in Lua. | 520 | (Function \verb|setglobal| is pre-defined in Lua. |
| 451 | Function \T{settable\_event} is used only for explanatory purposes.) | 521 | Function \T{settable\_event} is used only for explanatory purposes.) |
| 452 | 522 | ||
| 453 | The syntax \verb|var.NAME| is just syntactic sugar for | ||
| 454 | \verb|var["NAME"]|: | ||
| 455 | \begin{Produc} | ||
| 456 | \produc{var}{simpleexp \ter{.} name} | ||
| 457 | \end{Produc}% | ||
| 458 | |||
| 459 | \subsubsection{Control Structures} | 523 | \subsubsection{Control Structures} |
| 460 | The \Index{condition expression} of a control structure may return any value. | 524 | The \Index{condition expression} of a control structure may return any value. |
| 461 | All values different from \nil\ are considered true; | 525 | All values different from \nil\ are considered true; |
| @@ -471,7 +535,7 @@ only \nil\ is considered false. | |||
| 471 | \produc{elseif}{\rwd{elseif} exp1 \rwd{then} block} | 535 | \produc{elseif}{\rwd{elseif} exp1 \rwd{then} block} |
| 472 | \end{Produc} | 536 | \end{Produc} |
| 473 | 537 | ||
| 474 | A \T{return} is used to return values from a function or a chunk. | 538 | A \T{return} is used to return values from a function or from a chunk. |
| 475 | \label{return} | 539 | \label{return} |
| 476 | Because they may return more than one value, | 540 | Because they may return more than one value, |
| 477 | the syntax for a \Index{return statement} is: | 541 | the syntax for a \Index{return statement} is: |
| @@ -485,7 +549,7 @@ function calls can be executed as statements: | |||
| 485 | \begin{Produc} | 549 | \begin{Produc} |
| 486 | \produc{stat}{functioncall} | 550 | \produc{stat}{functioncall} |
| 487 | \end{Produc}% | 551 | \end{Produc}% |
| 488 | In this case, returned values are thrown away. | 552 | In this case, all returned values are thrown away. |
| 489 | Function calls are explained in \See{functioncall}. | 553 | Function calls are explained in \See{functioncall}. |
| 490 | 554 | ||
| 491 | \subsubsection{Local Declarations} \label{localvar} | 555 | \subsubsection{Local Declarations} \label{localvar} |
| @@ -549,7 +613,7 @@ the binary \verb|+| (addition), | |||
| 549 | \verb|/| (division) and \verb|^| (exponentiation), | 613 | \verb|/| (division) and \verb|^| (exponentiation), |
| 550 | and unary \verb|-| (negation). | 614 | and unary \verb|-| (negation). |
| 551 | If the operands are numbers, or strings that can be converted to | 615 | If the operands are numbers, or strings that can be converted to |
| 552 | numbers, according to the rules given in \See{coercion}, | 616 | numbers (according to the rules given in \See{coercion}), |
| 553 | then all operations except exponentiation have the usual meaning. | 617 | then all operations except exponentiation have the usual meaning. |
| 554 | Otherwise, an appropriate tag method is called \see{tag-method}. | 618 | Otherwise, an appropriate tag method is called \see{tag-method}. |
| 555 | An exponentiation always calls a tag method. | 619 | An exponentiation always calls a tag method. |
| @@ -564,7 +628,7 @@ Lua provides the following \Index{relational operators}: | |||
| 564 | \end{verbatim} | 628 | \end{verbatim} |
| 565 | All these return \nil\ as false and a value different from \nil\ as true. | 629 | All these return \nil\ as false and a value different from \nil\ as true. |
| 566 | 630 | ||
| 567 | Equality first compares the types of its operands. | 631 | Equality first compares the tags of its operands. |
| 568 | If they are different, then the result is \nil. | 632 | If they are different, then the result is \nil. |
| 569 | Otherwise, their values are compared. | 633 | Otherwise, their values are compared. |
| 570 | Numbers and strings are compared in the usual way. | 634 | Numbers and strings are compared in the usual way. |
| @@ -573,22 +637,24 @@ that is, two tables are considered equal only if they are the same table. | |||
| 573 | The operator \verb|~=| is exactly the negation of equality (\verb|==|). | 637 | The operator \verb|~=| is exactly the negation of equality (\verb|==|). |
| 574 | Note that the conversion rules of \See{coercion} | 638 | Note that the conversion rules of \See{coercion} |
| 575 | \emph{do not} apply to equality comparisons. | 639 | \emph{do not} apply to equality comparisons. |
| 576 | Thus, \verb|"0"==0| evaluates to false. | 640 | Thus, \verb|"0"==0| evaluates to false, |
| 641 | and \verb|t[0]| and \verb|t["0"]| denote different | ||
| 642 | entries in a table. | ||
| 577 | 643 | ||
| 578 | The other operators work as follows. | 644 | The other operators work as follows. |
| 579 | If both arguments are numbers, then they are compared as such. | 645 | If both arguments are numbers, then they are compared as such. |
| 580 | Otherwise, if both arguments are strings, | 646 | Otherwise, if both arguments are strings, |
| 581 | their values are compared using lexicographical order. | 647 | then their values are compared using lexicographical order. |
| 582 | Otherwise, the ``order'' tag method is called \see{tag-method}. | 648 | Otherwise, the ``order'' tag method is called \see{tag-method}. |
| 583 | 649 | ||
| 584 | \subsubsection{Logical Operators} | 650 | \subsubsection{Logical Operators} |
| 585 | Like control structures, all logical operators | ||
| 586 | consider \nil\ as false and anything else as true. | ||
| 587 | The \Index{logical operators} are: | 651 | The \Index{logical operators} are: |
| 588 | \index{and}\index{or}\index{not} | 652 | \index{and}\index{or}\index{not} |
| 589 | \begin{verbatim} | 653 | \begin{verbatim} |
| 590 | and or not | 654 | and or not |
| 591 | \end{verbatim} | 655 | \end{verbatim} |
| 656 | Like control structures, all logical operators | ||
| 657 | consider \nil\ as false and anything else as true. | ||
| 592 | The operator \verb|and| returns \nil\ if its first argument is \nil; | 658 | The operator \verb|and| returns \nil\ if its first argument is \nil; |
| 593 | otherwise, it returns its second argument. | 659 | otherwise, it returns its second argument. |
| 594 | The operator \verb|or| returns its first argument | 660 | The operator \verb|or| returns its first argument |
| @@ -646,13 +712,15 @@ For example: | |||
| 646 | \begin{verbatim} | 712 | \begin{verbatim} |
| 647 | a = {"v1", "v2", 34} | 713 | a = {"v1", "v2", 34} |
| 648 | \end{verbatim} | 714 | \end{verbatim} |
| 649 | is essentially equivalent to: | 715 | is equivalent to: |
| 650 | \begin{verbatim} | 716 | \begin{verbatim} |
| 651 | temp = {} | 717 | do |
| 652 | temp[1] = "v1" | 718 | local temp = {} |
| 653 | temp[2] = "v2" | 719 | temp[1] = "v1" |
| 654 | temp[3] = 34 | 720 | temp[2] = "v2" |
| 655 | a = temp | 721 | temp[3] = 34 |
| 722 | a = temp | ||
| 723 | end | ||
| 656 | \end{verbatim} | 724 | \end{verbatim} |
| 657 | 725 | ||
| 658 | The form \emph{ffieldlist1} initializes other fields in a table: | 726 | The form \emph{ffieldlist1} initializes other fields in a table: |
| @@ -664,14 +732,16 @@ For example: | |||
| 664 | \begin{verbatim} | 732 | \begin{verbatim} |
| 665 | a = {[f(k)] = g(y), x = 1, y = 3, [0] = b+c} | 733 | a = {[f(k)] = g(y), x = 1, y = 3, [0] = b+c} |
| 666 | \end{verbatim} | 734 | \end{verbatim} |
| 667 | is essentially equivalent to: | 735 | is equivalent to: |
| 668 | \begin{verbatim} | 736 | \begin{verbatim} |
| 669 | temp = {} | 737 | do |
| 670 | temp[f(k)] = g(y) | 738 | local temp = {} |
| 671 | temp.x = 1 -- or temp["x"] = 1 | 739 | temp[f(k)] = g(y) |
| 672 | temp.y = 3 -- or temp["y"] = 3 | 740 | temp.x = 1 -- or temp["x"] = 1 |
| 673 | temp[0] = b+c | 741 | temp.y = 3 -- or temp["y"] = 3 |
| 674 | a = temp | 742 | temp[0] = b+c |
| 743 | a = temp | ||
| 744 | end | ||
| 675 | \end{verbatim} | 745 | \end{verbatim} |
| 676 | An expression like \verb|{x = 1, y = 4}| is | 746 | An expression like \verb|{x = 1, y = 4}| is |
| 677 | in fact syntactic sugar for \verb|{["x"] = 1, ["y"] = 4}|. | 747 | in fact syntactic sugar for \verb|{["x"] = 1, ["y"] = 4}|. |
| @@ -681,25 +751,28 @@ and can be used in the same constructor separated by | |||
| 681 | a semi-collon. | 751 | a semi-collon. |
| 682 | For example, all forms below are correct: | 752 | For example, all forms below are correct: |
| 683 | \begin{verbatim} | 753 | \begin{verbatim} |
| 684 | x = {;}; x = {'a', 'b',}; x = {type='list'; 'a', 'b'} | 754 | x = {;} |
| 755 | x = {'a', 'b',} | ||
| 756 | x = {type='list'; 'a', 'b'} | ||
| 685 | x = {f(0), f(1), f(2),; n=3} | 757 | x = {f(0), f(1), f(2),; n=3} |
| 686 | \end{verbatim} | 758 | \end{verbatim} |
| 687 | 759 | ||
| 688 | \subsubsection{Function Calls} \label{functioncall} | 760 | \subsubsection{Function Calls} \label{functioncall} |
| 689 | A \Index{function call} has the following syntax: | 761 | A \Index{function call} has the following syntax: |
| 690 | \begin{Produc} | 762 | \begin{Produc} |
| 691 | \produc{functioncall}{simpleexp realParams} | 763 | \produc{functioncall}{simpleexp args} |
| 692 | \end{Produc}% | 764 | \end{Produc}% |
| 693 | First, \M{simpleexp} is evaluated. | 765 | First, \M{simpleexp} is evaluated. |
| 694 | If its value has type \emph{function}, | 766 | If its value has type \emph{function}, |
| 695 | then this function is called. | 767 | then this function is called, |
| 768 | with the given arguments. | ||
| 696 | Otherwise, the ``function'' tag method is called, | 769 | Otherwise, the ``function'' tag method is called, |
| 697 | having as first parameter the value of \M{simpleexp}, | 770 | having as first parameter the value of \M{simpleexp}, |
| 698 | and then the original call parameters. | 771 | and then the original call parameters. |
| 699 | 772 | ||
| 700 | The form: | 773 | The form: |
| 701 | \begin{Produc} | 774 | \begin{Produc} |
| 702 | \produc{functioncall}{simpleexp \ter{:} name realParams} | 775 | \produc{functioncall}{simpleexp \ter{:} name args} |
| 703 | \end{Produc}% | 776 | \end{Produc}% |
| 704 | can be used to call ``methods''. | 777 | can be used to call ``methods''. |
| 705 | A call \verb|simpleexp:name(...)| | 778 | A call \verb|simpleexp:name(...)| |
| @@ -710,9 +783,9 @@ is syntactic sugar for | |||
| 710 | except that \verb|simpleexp| is evaluated only once. | 783 | except that \verb|simpleexp| is evaluated only once. |
| 711 | 784 | ||
| 712 | \begin{Produc} | 785 | \begin{Produc} |
| 713 | \produc{realParams}{\ter{(} \opt{explist1} \ter{)}} | 786 | \produc{args}{\ter{(} \opt{explist1} \ter{)}} |
| 714 | \produc{realParams}{tableconstructor} | 787 | \produc{args}{tableconstructor} |
| 715 | \produc{realParams}{\ter{literal}} | 788 | \produc{args}{\ter{literal}} |
| 716 | \produc{explist1}{exp1 \rep{\ter{,} exp1}} | 789 | \produc{explist1}{exp1 \rep{\ter{,} exp1}} |
| 717 | \end{Produc}% | 790 | \end{Produc}% |
| 718 | All argument expressions are evaluated before the call. | 791 | All argument expressions are evaluated before the call. |
| @@ -761,7 +834,7 @@ is just syntactic sugar for: | |||
| 761 | end | 834 | end |
| 762 | \end{verbatim} | 835 | \end{verbatim} |
| 763 | 836 | ||
| 764 | A function definition is an executable expresion, | 837 | A function definition is an executable expression, |
| 765 | whose value has type \emph{function}. | 838 | whose value has type \emph{function}. |
| 766 | When Lua pre-compiles a chunk, | 839 | When Lua pre-compiles a chunk, |
| 767 | all its function bodies are pre-compiled, too. | 840 | all its function bodies are pre-compiled, too. |
| @@ -774,7 +847,7 @@ Different instances of a same function | |||
| 774 | may have different upvalues. | 847 | may have different upvalues. |
| 775 | 848 | ||
| 776 | Parameters act as local variables, | 849 | Parameters act as local variables, |
| 777 | initialized with the argument values. | 850 | initialized with the argument values: |
| 778 | \begin{Produc} | 851 | \begin{Produc} |
| 779 | \produc{parlist1}{\ter{\ldots}} | 852 | \produc{parlist1}{\ter{\ldots}} |
| 780 | \produc{parlist1}{name \rep{\ter{,} name} \opt{\ter{,} \ter{\ldots}}} | 853 | \produc{parlist1}{name \rep{\ter{,} name} \opt{\ter{,} \ter{\ldots}}} |
| @@ -786,8 +859,8 @@ the length of the list of parameters \see{adjust}, | |||
| 786 | unless the function is a \Def{vararg} function, | 859 | unless the function is a \Def{vararg} function, |
| 787 | indicated by the dots (\ldots) at the end of its parameter list. | 860 | indicated by the dots (\ldots) at the end of its parameter list. |
| 788 | A vararg function does not adjust its argument list; | 861 | A vararg function does not adjust its argument list; |
| 789 | instead, it collects any extra arguments in an implicit parameter, | 862 | instead, it collects any extra arguments into an implicit parameter, |
| 790 | called \Def{arg}. | 863 | called \IndexVerb{arg}. |
| 791 | This parameter is always initialized as a table, | 864 | This parameter is always initialized as a table, |
| 792 | with a field \verb|n| with the number of extra arguments, | 865 | with a field \verb|n| with the number of extra arguments, |
| 793 | and the extra arguments at positions 1, 2, \ldots | 866 | and the extra arguments at positions 1, 2, \ldots |
| @@ -815,7 +888,7 @@ If control reaches the end of a function without a return instruction, | |||
| 815 | then the function returns with no results. | 888 | then the function returns with no results. |
| 816 | 889 | ||
| 817 | There is a special syntax for defining \Index{methods}, | 890 | There is a special syntax for defining \Index{methods}, |
| 818 | that is, functions that have an extra parameter \Def{self}. | 891 | that is, functions that have an extra parameter \IndexVerb{self}. |
| 819 | \begin{Produc} | 892 | \begin{Produc} |
| 820 | \produc{function}{\rwd{function} name \ter{:} name \ter{(} \opt{parlist1} | 893 | \produc{function}{\rwd{function} name \ter{:} name \ter{(} \opt{parlist1} |
| 821 | \ter{)} block \rwd{end}} | 894 | \ter{)} block \rwd{end}} |
| @@ -858,17 +931,17 @@ appears is instantiated. | |||
| 858 | The name used in an upvalue may be the name of any variable visible | 931 | The name used in an upvalue may be the name of any variable visible |
| 859 | at the point where the function is defined. | 932 | at the point where the function is defined. |
| 860 | 933 | ||
| 861 | See some examples below: | 934 | Here are some examples: |
| 862 | \begin{verbatim} | 935 | \begin{verbatim} |
| 863 | a,b,c = 1 -- global variables | 936 | a,b,c = 1 -- global variables |
| 864 | function f () | 937 | function f () |
| 865 | local x,b; -- x and b are locals to f | 938 | local x,b -- x and b are local to f |
| 866 | function g () | 939 | function g () |
| 867 | local a,y -- a and y are locals go g | 940 | local a,y -- a and y are local to g |
| 868 | p = a -- OK, access local 'a' | 941 | p = a -- OK, access local 'a' |
| 869 | p = c -- OK, access global 'c' | 942 | p = c -- OK, access global 'c' |
| 870 | p = b -- ERROR: cannot access a variable in outer scope | 943 | p = b -- ERROR: cannot access a variable in outer scope |
| 871 | p = %b -- OK, access frozen value of 'b' | 944 | p = %b -- OK, access frozen value of 'b' (local to 'f') |
| 872 | p = %c -- OK, access frozen value of global 'c' | 945 | p = %c -- OK, access frozen value of global 'c' |
| 873 | p = %y -- ERROR: 'y' is not visible where 'g' is defined | 946 | p = %y -- ERROR: 'y' is not visible where 'g' is defined |
| 874 | end | 947 | end |
| @@ -881,7 +954,7 @@ end | |||
| 881 | Lua provides a powerful mechanism to extend its semantics, | 954 | Lua provides a powerful mechanism to extend its semantics, |
| 882 | called \Def{Tag Methods}. | 955 | called \Def{Tag Methods}. |
| 883 | A tag method (TM) is a programmer-defined function | 956 | A tag method (TM) is a programmer-defined function |
| 884 | that can be called at many key points of the evaluation of a program, | 957 | that is called at specific key points during the evaluation of a program, |
| 885 | allowing a programmer to change the standard Lua behavior at these points. | 958 | allowing a programmer to change the standard Lua behavior at these points. |
| 886 | Each of these points is called an \Def{event}. | 959 | Each of these points is called an \Def{event}. |
| 887 | 960 | ||
| @@ -889,7 +962,7 @@ The tag method called for any specific event is selected | |||
| 889 | according to the tag of the values involved | 962 | according to the tag of the values involved |
| 890 | in the event \see{TypesSec}. | 963 | in the event \see{TypesSec}. |
| 891 | The function \IndexVerb{settagmethod} changes the tag method | 964 | The function \IndexVerb{settagmethod} changes the tag method |
| 892 | associated with a given pair \M{<tag, event>}. | 965 | associated with a given pair \M{(tag, event)}. |
| 893 | Its first parameter is the tag, the second the event name | 966 | Its first parameter is the tag, the second the event name |
| 894 | (a string, see below), | 967 | (a string, see below), |
| 895 | and the third parameter is the new method (a function), | 968 | and the third parameter is the new method (a function), |
| @@ -941,7 +1014,7 @@ If it also fails, then it gets a tag method from tag~0: | |||
| 941 | -- call the method with both operands and an extra | 1014 | -- call the method with both operands and an extra |
| 942 | -- argument with the event name | 1015 | -- argument with the event name |
| 943 | return tm(op1, op2, "add") | 1016 | return tm(op1, op2, "add") |
| 944 | else -- no tag method available: Default behavior | 1017 | else -- no tag method available: default behavior |
| 945 | error("unexpected type at arithmetic operation") | 1018 | error("unexpected type at arithmetic operation") |
| 946 | end | 1019 | end |
| 947 | end | 1020 | end |
| @@ -950,15 +1023,15 @@ If it also fails, then it gets a tag method from tag~0: | |||
| 950 | 1023 | ||
| 951 | \item[``sub'':]\index{sub event} | 1024 | \item[``sub'':]\index{sub event} |
| 952 | called when a \verb|-| operation is applied to non numerical operands. | 1025 | called when a \verb|-| operation is applied to non numerical operands. |
| 953 | Behavior similar to \verb|"add"| event. | 1026 | Behavior similar to the \verb|"add"| event. |
| 954 | 1027 | ||
| 955 | \item[``mul'':]\index{mul event} | 1028 | \item[``mul'':]\index{mul event} |
| 956 | called when a \verb|*| operation is applied to non numerical operands. | 1029 | called when a \verb|*| operation is applied to non numerical operands. |
| 957 | Behavior similar to \verb|"add"| event. | 1030 | Behavior similar to the \verb|"add"| event. |
| 958 | 1031 | ||
| 959 | \item[``div'':]\index{div event} | 1032 | \item[``div'':]\index{div event} |
| 960 | called when a \verb|/| operation is applied to non numerical operands. | 1033 | called when a \verb|/| operation is applied to non numerical operands. |
| 961 | Behavior similar to \verb|"add"| event. | 1034 | Behavior similar to the \verb|"add"| event. |
| 962 | 1035 | ||
| 963 | \item[``pow'':]\index{pow event} | 1036 | \item[``pow'':]\index{pow event} |
| 964 | called when a \verb|^| operation is applied. | 1037 | called when a \verb|^| operation is applied. |
| @@ -969,7 +1042,7 @@ called when a \verb|^| operation is applied. | |||
| 969 | -- call the method with both operands and an extra | 1042 | -- call the method with both operands and an extra |
| 970 | -- argument with the event name | 1043 | -- argument with the event name |
| 971 | return tm(op1, op2, "pow") | 1044 | return tm(op1, op2, "pow") |
| 972 | else -- no tag method available: Default behavior | 1045 | else -- no tag method available: default behavior |
| 973 | error("unexpected type at arithmetic operation") | 1046 | error("unexpected type at arithmetic operation") |
| 974 | end | 1047 | end |
| 975 | end | 1048 | end |
| @@ -991,7 +1064,7 @@ called when an unary \verb|-| operation is applied to a non numerical operand. | |||
| 991 | -- call the method with the operand, nil, and an extra | 1064 | -- call the method with the operand, nil, and an extra |
| 992 | -- argument with the event name | 1065 | -- argument with the event name |
| 993 | return tm(op, nil, "unm") | 1066 | return tm(op, nil, "unm") |
| 994 | else -- no tag method available: Default behavior | 1067 | else -- no tag method available: default behavior |
| 995 | error("unexpected type at arithmetic operation") | 1068 | error("unexpected type at arithmetic operation") |
| 996 | end | 1069 | end |
| 997 | end | 1070 | end |
| @@ -1021,17 +1094,17 @@ or non string operands. | |||
| 1021 | \item[``gt'':]\index{gt event} | 1094 | \item[``gt'':]\index{gt event} |
| 1022 | called when a \verb|>| operation is applied to non numerical | 1095 | called when a \verb|>| operation is applied to non numerical |
| 1023 | or non string operands. | 1096 | or non string operands. |
| 1024 | Behavior similar to \verb|"lt"| event. | 1097 | Behavior similar to the \verb|"lt"| event. |
| 1025 | 1098 | ||
| 1026 | \item[``le'':]\index{le event} | 1099 | \item[``le'':]\index{le event} |
| 1027 | called when a \verb|<=| operation is applied to non numerical | 1100 | called when a \verb|<=| operation is applied to non numerical |
| 1028 | or non string operands. | 1101 | or non string operands. |
| 1029 | Behavior similar to \verb|"lt"| event. | 1102 | Behavior similar to the \verb|"lt"| event. |
| 1030 | 1103 | ||
| 1031 | \item[``ge'':]\index{ge event} | 1104 | \item[``ge'':]\index{ge event} |
| 1032 | called when a \verb|>=| operation is applied to non numerical | 1105 | called when a \verb|>=| operation is applied to non numerical |
| 1033 | or non string operands. | 1106 | or non string operands. |
| 1034 | Behavior similar to \verb|"lt"| event. | 1107 | Behavior similar to the \verb|"lt"| event. |
| 1035 | 1108 | ||
| 1036 | \item[``concat'':]\index{concatenation event} | 1109 | \item[``concat'':]\index{concatenation event} |
| 1037 | called when a concatenation is applied to non string operands. | 1110 | called when a concatenation is applied to non string operands. |
| @@ -1057,7 +1130,7 @@ not present in a table. | |||
| 1057 | See event \verb|"gettable"| for its semantics. | 1130 | See event \verb|"gettable"| for its semantics. |
| 1058 | 1131 | ||
| 1059 | \item[``getglobal'':]\index{getglobal event} | 1132 | \item[``getglobal'':]\index{getglobal event} |
| 1060 | called whenever Lua accesses a global variable. | 1133 | called whenever Lua needs the value of a global variable. |
| 1061 | This method can only be set for \nil\ and for tags | 1134 | This method can only be set for \nil\ and for tags |
| 1062 | created by \verb|newtag|. | 1135 | created by \verb|newtag|. |
| 1063 | \begin{verbatim} | 1136 | \begin{verbatim} |
| @@ -1176,7 +1249,8 @@ Lua does the equivalent of the call \verb|gc_event(nil)|. | |||
| 1176 | \subsection{Error Handling} \label{error} | 1249 | \subsection{Error Handling} \label{error} |
| 1177 | 1250 | ||
| 1178 | Because Lua is an extension language, | 1251 | Because Lua is an extension language, |
| 1179 | all Lua actions start from C code calling a function from the Lua library. | 1252 | all Lua actions start from C code in the host program |
| 1253 | calling a function from the Lua library. | ||
| 1180 | Whenever an error occurs during Lua compilation or execution, | 1254 | Whenever an error occurs during Lua compilation or execution, |
| 1181 | the \Def{error method} is called, | 1255 | the \Def{error method} is called, |
| 1182 | and then the corresponding function from the library | 1256 | and then the corresponding function from the library |
| @@ -1193,7 +1267,7 @@ which gets the new error handler as its only parameter | |||
| 1193 | The standard I/O library uses this facility to redefine the error method, | 1267 | The standard I/O library uses this facility to redefine the error method, |
| 1194 | using the debug facilities \see{debugI}, | 1268 | using the debug facilities \see{debugI}, |
| 1195 | in order to print some extra information, | 1269 | in order to print some extra information, |
| 1196 | like the call stack. | 1270 | such as the call stack. |
| 1197 | 1271 | ||
| 1198 | To provide more information about errors, | 1272 | To provide more information about errors, |
| 1199 | Lua programs should include the compilation pragma \verb|$debug|. | 1273 | Lua programs should include the compilation pragma \verb|$debug|. |
| @@ -1236,6 +1310,7 @@ This function allocates and initializes some internal structures, | |||
| 1236 | and defines all pre-defined functions of Lua. | 1310 | and defines all pre-defined functions of Lua. |
| 1237 | If the library is already opened, | 1311 | If the library is already opened, |
| 1238 | this function has no effect. | 1312 | this function has no effect. |
| 1313 | All standard libraries call \verb|lua_open| when they are opened. | ||
| 1239 | 1314 | ||
| 1240 | If necessary, the library may be closed:\Deffunc{lua_close} | 1315 | If necessary, the library may be closed:\Deffunc{lua_close} |
| 1241 | \begin{verbatim} | 1316 | \begin{verbatim} |
| @@ -1277,9 +1352,9 @@ and 0 otherwise. | |||
| 1277 | The function \verb|lua_isnumber| accepts numbers and numerical strings, | 1352 | The function \verb|lua_isnumber| accepts numbers and numerical strings, |
| 1278 | whereas | 1353 | whereas |
| 1279 | \verb|lua_isstring| accepts strings and numbers \see{coercion}, | 1354 | \verb|lua_isstring| accepts strings and numbers \see{coercion}, |
| 1280 | and \verb|lua_isfunction| accepts Lua and C functions. | 1355 | and \verb|lua_isfunction| accepts Lua functions and C functions. |
| 1281 | 1356 | ||
| 1282 | To check the tag of a \verb|lua_Object|, | 1357 | To get the tag of a \verb|lua_Object|, |
| 1283 | the following function is available: | 1358 | the following function is available: |
| 1284 | \Deffunc{lua_tag} | 1359 | \Deffunc{lua_tag} |
| 1285 | \begin{verbatim} | 1360 | \begin{verbatim} |
| @@ -1288,11 +1363,12 @@ int lua_tag (lua_Object object); | |||
| 1288 | 1363 | ||
| 1289 | To translate a value from type \verb|lua_Object| to a specific C type, | 1364 | To translate a value from type \verb|lua_Object| to a specific C type, |
| 1290 | the programmer can use: | 1365 | the programmer can use: |
| 1291 | \Deffunc{lua_getnumber}\Deffunc{lua_getstring} | 1366 | \Deffunc{lua_getnumber}\Deffunc{lua_getstring}\Deffunc{lua_strlen} |
| 1292 | \Deffunc{lua_getcfunction}\Deffunc{lua_getuserdata} | 1367 | \Deffunc{lua_getcfunction}\Deffunc{lua_getuserdata} |
| 1293 | \begin{verbatim} | 1368 | \begin{verbatim} |
| 1294 | float lua_getnumber (lua_Object object); | 1369 | double lua_getnumber (lua_Object object); |
| 1295 | char *lua_getstring (lua_Object object); | 1370 | char *lua_getstring (lua_Object object); |
| 1371 | long lua_strlen (lua_Object object); | ||
| 1296 | lua_CFunction lua_getcfunction (lua_Object object); | 1372 | lua_CFunction lua_getcfunction (lua_Object object); |
| 1297 | void *lua_getuserdata (lua_Object object); | 1373 | void *lua_getuserdata (lua_Object object); |
| 1298 | \end{verbatim} | 1374 | \end{verbatim} |
| @@ -1306,9 +1382,13 @@ This \verb|lua_Object| must be a string or a number; | |||
| 1306 | otherwise, the function returns~0 (the \verb|NULL| pointer). | 1382 | otherwise, the function returns~0 (the \verb|NULL| pointer). |
| 1307 | This function does not create a new string, | 1383 | This function does not create a new string, |
| 1308 | but returns a pointer to a string inside the Lua environment. | 1384 | but returns a pointer to a string inside the Lua environment. |
| 1385 | Those strings always have a 0 after their last character (like in C), | ||
| 1386 | but may contain other zeros in their body. | ||
| 1387 | If you do not know whether a string may contain zeros, | ||
| 1388 | you can use \verb|lua_strlen| to check the actual length. | ||
| 1309 | Because Lua has garbage collection, | 1389 | Because Lua has garbage collection, |
| 1310 | there is no guarantee that such pointer will be valid after the block ends | 1390 | there is no guarantee that such pointer will be valid after the block ends |
| 1311 | (see below). | 1391 | \see{GC}. |
| 1312 | 1392 | ||
| 1313 | \verb|lua_getcfunction| converts a \verb|lua_Object| to a C function. | 1393 | \verb|lua_getcfunction| converts a \verb|lua_Object| to a C function. |
| 1314 | This \verb|lua_Object| must have type \emph{CFunction}; | 1394 | This \verb|lua_Object| must have type \emph{CFunction}; |
| @@ -1319,6 +1399,7 @@ The type \verb|lua_CFunction| is explained in \See{LuacallC}. | |||
| 1319 | This \verb|lua_Object| must have type \emph{userdata}; | 1399 | This \verb|lua_Object| must have type \emph{userdata}; |
| 1320 | otherwise, the function returns 0 (the \verb|NULL| pointer). | 1400 | otherwise, the function returns 0 (the \verb|NULL| pointer). |
| 1321 | 1401 | ||
| 1402 | \subsection{Garbage Collection}\label{GC} | ||
| 1322 | Because Lua has automatic memory management and garbage collection, | 1403 | Because Lua has automatic memory management and garbage collection, |
| 1323 | a \verb|lua_Object| has a limited scope, | 1404 | a \verb|lua_Object| has a limited scope, |
| 1324 | and is only valid inside the \emph{block} where it has been created. | 1405 | and is only valid inside the \emph{block} where it has been created. |
| @@ -1335,7 +1416,7 @@ long lua_collectgarbage (long limit); | |||
| 1335 | \end{verbatim} | 1416 | \end{verbatim} |
| 1336 | This function returns the number of objects collected. | 1417 | This function returns the number of objects collected. |
| 1337 | The argument \verb|limit| makes the next cycle occur only | 1418 | The argument \verb|limit| makes the next cycle occur only |
| 1338 | when that number of new objects have been created. | 1419 | after that number of new objects have been created. |
| 1339 | If \verb|limit|=0, then Lua uses an adaptable heuristics to set this limit. | 1420 | If \verb|limit|=0, then Lua uses an adaptable heuristics to set this limit. |
| 1340 | 1421 | ||
| 1341 | 1422 | ||
| @@ -1343,15 +1424,15 @@ All communication between Lua and C is done through two | |||
| 1343 | abstract data types, called \Def{lua2C} and \Def{C2lua}. | 1424 | abstract data types, called \Def{lua2C} and \Def{C2lua}. |
| 1344 | The first one, as the name implies, is used to pass values | 1425 | The first one, as the name implies, is used to pass values |
| 1345 | from Lua to C: | 1426 | from Lua to C: |
| 1346 | Parameters when Lua calls C and results when C calls Lua. | 1427 | parameters when Lua calls C and results when C calls Lua. |
| 1347 | The structure C2lua is used in the reverse direction: | 1428 | The structure C2lua is used in the reverse direction: |
| 1348 | Parameters when C calls Lua and results when Lua calls C. | 1429 | parameters when C calls Lua and results when Lua calls C. |
| 1349 | 1430 | ||
| 1350 | The structure lua2C is an abstract array, | 1431 | The structure lua2C is an abstract array, |
| 1351 | which can be indexed with the function: | 1432 | which can be indexed with the function: |
| 1352 | \Deffunc{lua_lua2C} | 1433 | \Deffunc{lua_lua2C} |
| 1353 | \begin{verbatim} | 1434 | \begin{verbatim} |
| 1354 | lua_Object lua_lua2C (int number); | 1435 | lua_Object lua_lua2C (int number); |
| 1355 | \end{verbatim} | 1436 | \end{verbatim} |
| 1356 | where \verb|number| starts with 1. | 1437 | where \verb|number| starts with 1. |
| 1357 | When called with a number larger than the array size, | 1438 | When called with a number larger than the array size, |
| @@ -1361,15 +1442,16 @@ a variable number of parameters, | |||
| 1361 | and to call Lua functions that return a variable number of results. | 1442 | and to call Lua functions that return a variable number of results. |
| 1362 | Notice that the structure lua2C cannot be directly modified by C code. | 1443 | Notice that the structure lua2C cannot be directly modified by C code. |
| 1363 | 1444 | ||
| 1364 | The second structure, C2lua, is a stack. | 1445 | The second structure, C2lua, is an abstract stack. |
| 1365 | Pushing elements into this stack | 1446 | Pushing elements into this stack |
| 1366 | is done with the following functions and macros: | 1447 | is done with the following functions and macros: |
| 1367 | \Deffunc{lua_pushnumber}\Deffunc{lua_pushstring} | 1448 | \Deffunc{lua_pushnumber}\Deffunc{lua_pushlstring}\Deffunc{lua_pushstring} |
| 1368 | \Deffunc{lua_pushcfunction}\Deffunc{lua_pushusertag} | 1449 | \Deffunc{lua_pushcfunction}\Deffunc{lua_pushusertag} |
| 1369 | \Deffunc{lua_pushnil}\Deffunc{lua_pushobject} | 1450 | \Deffunc{lua_pushnil}\Deffunc{lua_pushobject} |
| 1370 | \Deffunc{lua_pushuserdata}\label{pushing} | 1451 | \Deffunc{lua_pushuserdata}\label{pushing} |
| 1371 | \begin{verbatim} | 1452 | \begin{verbatim} |
| 1372 | void lua_pushnumber (double n); | 1453 | void lua_pushnumber (double n); |
| 1454 | void lua_pushlstring (char *s, long len); | ||
| 1373 | void lua_pushstring (char *s); | 1455 | void lua_pushstring (char *s); |
| 1374 | void lua_pushusertag (void *u, int tag); | 1456 | void lua_pushusertag (void *u, int tag); |
| 1375 | void lua_pushnil (void); | 1457 | void lua_pushnil (void); |
| @@ -1379,6 +1461,11 @@ void lua_pushcfunction (lua_CFunction f); /* macro */ | |||
| 1379 | All of them receive a C value, | 1461 | All of them receive a C value, |
| 1380 | convert it to a corresponding \verb|lua_Object|, | 1462 | convert it to a corresponding \verb|lua_Object|, |
| 1381 | and leave the result on the top of C2lua. | 1463 | and leave the result on the top of C2lua. |
| 1464 | Particularly, functions \verb|lua_pushlstring| and \verb|lua_pushstring| | ||
| 1465 | make an internal copy of the given string. | ||
| 1466 | Function \verb|lua_pushstring| can only be used to push proper C strings | ||
| 1467 | (that is, strings which do not contain zeros and end with a zero); | ||
| 1468 | otherwise you can use the more generic \verb|lua_pushlstring|. | ||
| 1382 | The function | 1469 | The function |
| 1383 | \Deffunc{lua_pop} | 1470 | \Deffunc{lua_pop} |
| 1384 | \begin{verbatim} | 1471 | \begin{verbatim} |
| @@ -1398,9 +1485,9 @@ Otherwise, a new userdata is created, with the given value and tag. | |||
| 1398 | If this function is called with | 1485 | If this function is called with |
| 1399 | \verb|tag|=\verb|LUA_ANYTAG|\Deffunc{LUA_ANYTAG}, | 1486 | \verb|tag|=\verb|LUA_ANYTAG|\Deffunc{LUA_ANYTAG}, |
| 1400 | then Lua will try to find any userdata with the given value, | 1487 | then Lua will try to find any userdata with the given value, |
| 1401 | no matter its tag. | 1488 | regardless of its tag. |
| 1402 | If there is no userdata with that value, then a new one is created, | 1489 | If there is no userdata with that value, then a new one is created, |
| 1403 | with tag=0. | 1490 | with tag equals to 0. |
| 1404 | 1491 | ||
| 1405 | Userdata can have different tags, | 1492 | Userdata can have different tags, |
| 1406 | whose semantics are only known to the host program. | 1493 | whose semantics are only known to the host program. |
| @@ -1533,7 +1620,6 @@ Functions defined in Lua by a chunk executed with | |||
| 1533 | This is done using the following protocol: | 1620 | This is done using the following protocol: |
| 1534 | first, the arguments to the function are pushed onto C2lua | 1621 | first, the arguments to the function are pushed onto C2lua |
| 1535 | \see{pushing}, in direct order, i.e., the first argument is pushed first. | 1622 | \see{pushing}, in direct order, i.e., the first argument is pushed first. |
| 1536 | |||
| 1537 | Then, the function is called using | 1623 | Then, the function is called using |
| 1538 | \Deffunc{lua_callfunction} | 1624 | \Deffunc{lua_callfunction} |
| 1539 | \begin{verbatim} | 1625 | \begin{verbatim} |
| @@ -1558,7 +1644,7 @@ equivalent to the Lua code: | |||
| 1558 | lua_pushstring("how"); /* 1st argument */ | 1644 | lua_pushstring("how"); /* 1st argument */ |
| 1559 | lua_pushobject(lua_getglobal("t")); /* push value of global 't' */ | 1645 | lua_pushobject(lua_getglobal("t")); /* push value of global 't' */ |
| 1560 | lua_pushstring("x"); /* push the string 'x' */ | 1646 | lua_pushstring("x"); /* push the string 'x' */ |
| 1561 | lua_pushobject(lua_gettable()); /* push result of t.x (= t['x']) */ | 1647 | lua_pushobject(lua_gettable()); /* push result of t.x (2nd arg) */ |
| 1562 | lua_pushnumber(4); /* 3th argument */ | 1648 | lua_pushnumber(4); /* 3th argument */ |
| 1563 | lua_callfunction(lua_getglobal("f")); /* call Lua function */ | 1649 | lua_callfunction(lua_getglobal("f")); /* call Lua function */ |
| 1564 | lua_pushobject(lua_getresult(1)); /* push first result of the call */ | 1650 | lua_pushobject(lua_getresult(1)); /* push first result of the call */ |
| @@ -1639,7 +1725,7 @@ which defines the way parameters and results are passed. | |||
| 1639 | 1725 | ||
| 1640 | A C function receives its arguments in structure lua2C; | 1726 | A C function receives its arguments in structure lua2C; |
| 1641 | to access them, it uses the macro \verb|lua_getparam|, \Deffunc{lua_getparam} | 1727 | to access them, it uses the macro \verb|lua_getparam|, \Deffunc{lua_getparam} |
| 1642 | again just another name to \verb|lua_lua2C|. | 1728 | again just another name for \verb|lua_lua2C|. |
| 1643 | To return values, a C function just pushes them onto the stack C2lua, | 1729 | To return values, a C function just pushes them onto the stack C2lua, |
| 1644 | in direct order \see{valuesCLua}. | 1730 | in direct order \see{valuesCLua}. |
| 1645 | Like a Lua function, a C function called by Lua can also return | 1731 | Like a Lua function, a C function called by Lua can also return |
| @@ -1666,11 +1752,11 @@ these upvalues are inserted as the first arguments to the function, | |||
| 1666 | before the actual arguments provided in the call. | 1752 | before the actual arguments provided in the call. |
| 1667 | 1753 | ||
| 1668 | For some examples of C functions, see files \verb|lstrlib.c|, | 1754 | For some examples of C functions, see files \verb|lstrlib.c|, |
| 1669 | \verb|liolib.c| and \verb|lmathlib.c| in Lua distribution. | 1755 | \verb|liolib.c| and \verb|lmathlib.c| in the official Lua distribution. |
| 1670 | 1756 | ||
| 1671 | \subsection{References to Lua Objects} | 1757 | \subsection{References to Lua Objects} |
| 1672 | 1758 | ||
| 1673 | As noted in \See{LuacallC}, \verb|lua_Object|s are volatile. | 1759 | As noted in \See{GC}, \verb|lua_Object|s are volatile. |
| 1674 | If the C code needs to keep a \verb|lua_Object| | 1760 | If the C code needs to keep a \verb|lua_Object| |
| 1675 | outside block boundaries, | 1761 | outside block boundaries, |
| 1676 | then it must create a \Def{reference} to the object. | 1762 | then it must create a \Def{reference} to the object. |
| @@ -1695,7 +1781,7 @@ if the object has been collected, | |||
| 1695 | \verb|lua_getref| returns \verb|LUA_NOOBJECT|. | 1781 | \verb|lua_getref| returns \verb|LUA_NOOBJECT|. |
| 1696 | 1782 | ||
| 1697 | When a reference is no longer needed, | 1783 | When a reference is no longer needed, |
| 1698 | it can be freed with a call to \verb|lua_unref|. | 1784 | it can be released with a call to \verb|lua_unref|. |
| 1699 | 1785 | ||
| 1700 | 1786 | ||
| 1701 | 1787 | ||
| @@ -1740,7 +1826,7 @@ then Lua stops getting arguments at the first nil value. | |||
| 1740 | 1826 | ||
| 1741 | By default, | 1827 | By default, |
| 1742 | all results from \verb|func| are just returned by the call. | 1828 | all results from \verb|func| are just returned by the call. |
| 1743 | If the string \verb|mode| contains \verb|p|, | 1829 | If the string \verb|mode| contains \verb|"p"|, |
| 1744 | the results are \emph{packed} in a single table.\index{packed results} | 1830 | the results are \emph{packed} in a single table.\index{packed results} |
| 1745 | That is, \verb|call| returns just one table; | 1831 | That is, \verb|call| returns just one table; |
| 1746 | at index \verb|n|, the table has the total number of results | 1832 | at index \verb|n|, the table has the total number of results |
| @@ -1748,8 +1834,8 @@ from the call; | |||
| 1748 | the first result is at index 1, etc. | 1834 | the first result is at index 1, etc. |
| 1749 | For instance, the following calls produce the following results: | 1835 | For instance, the following calls produce the following results: |
| 1750 | \begin{verbatim} | 1836 | \begin{verbatim} |
| 1751 | a = call(sin, {5}) --> a = 0.0871557 = sin(5) | 1837 | a = call(sin, {5}) --> a = 0.0871557 = sin(5) |
| 1752 | a = call(max, {1,4,5; n=2}) --> a = 4 (only 1 and 4 are arguments) | 1838 | a = call(max, {1,4,5; n=2}) --> a = 4 (only 1 and 4 are arguments) |
| 1753 | t = {x=1} | 1839 | t = {x=1} |
| 1754 | a = call(next, {t,nil;n=2}, "p") --> a={"x", 1; n=2} | 1840 | a = call(next, {t,nil;n=2}, "p") --> a={"x", 1; n=2} |
| 1755 | \end{verbatim} | 1841 | \end{verbatim} |
| @@ -1757,7 +1843,7 @@ a = call(next, {t,nil;n=2}, "p") --> a={"x", 1; n=2} | |||
| 1757 | By default, | 1843 | By default, |
| 1758 | if an error occurs during the function call, | 1844 | if an error occurs during the function call, |
| 1759 | the error is propagated. | 1845 | the error is propagated. |
| 1760 | If the string \verb|mode| contains \verb|x|, | 1846 | If the string \verb|mode| contains \verb|"x"|, |
| 1761 | then the call is \emph{protected}.\index{protected calls} | 1847 | then the call is \emph{protected}.\index{protected calls} |
| 1762 | In this mode, function \verb|call| does not generate an error, | 1848 | In this mode, function \verb|call| does not generate an error, |
| 1763 | whatever happens during the call. | 1849 | whatever happens during the call. |
| @@ -1773,7 +1859,7 @@ no error messages will be issued during the execution of the called function. | |||
| 1773 | Forces a garbage collection cycle. | 1859 | Forces a garbage collection cycle. |
| 1774 | Returns the number of objects collected. | 1860 | Returns the number of objects collected. |
| 1775 | An optional argument, \verb|limit|, is a number that | 1861 | An optional argument, \verb|limit|, is a number that |
| 1776 | makes the next cycle occur when that number of new | 1862 | makes the next cycle occur only after that number of new |
| 1777 | objects have been created. | 1863 | objects have been created. |
| 1778 | If absent, Lua uses an adaptable algorithm to set | 1864 | If absent, Lua uses an adaptable algorithm to set |
| 1779 | this limit. | 1865 | this limit. |
| @@ -1782,10 +1868,10 @@ the API function \verb|lua_collectgarbage|. | |||
| 1782 | 1868 | ||
| 1783 | \subsubsection*{\ff \T{dofile (filename)}}\Deffunc{dofile} | 1869 | \subsubsection*{\ff \T{dofile (filename)}}\Deffunc{dofile} |
| 1784 | This function receives a file name, | 1870 | This function receives a file name, |
| 1785 | opens it, and executes its contents as a Lua chunk, | 1871 | opens the file, and executes the file contents as a Lua chunk, |
| 1786 | or as pre-compiled chunks. | 1872 | or as pre-compiled chunks. |
| 1787 | When called without arguments, | 1873 | When called without arguments, |
| 1788 | it executes the contents of the standard input (\verb|stdin|). | 1874 | \verb|dofile| executes the contents of the standard input (\verb|stdin|). |
| 1789 | If there is any error executing the file, | 1875 | If there is any error executing the file, |
| 1790 | then \verb|dofile| returns \nil. | 1876 | then \verb|dofile| returns \nil. |
| 1791 | Otherwise, it returns the values returned by the chunk, | 1877 | Otherwise, it returns the values returned by the chunk, |
| @@ -1843,12 +1929,12 @@ otherwise the semantics of \verb|nextvar| is undefined. | |||
| 1843 | This function cannot be written with the standard API. | 1929 | This function cannot be written with the standard API. |
| 1844 | 1930 | ||
| 1845 | \subsubsection*{\ff \T{foreach (table, function)}}\Deffunc{foreach} | 1931 | \subsubsection*{\ff \T{foreach (table, function)}}\Deffunc{foreach} |
| 1846 | Executes \verb|function| over all elements of \verb|table|. | 1932 | Executes the given \verb|function| over all elements of \verb|table|. |
| 1847 | For each element, the function is called with the index and | 1933 | For each element, the function is called with the index and |
| 1848 | respective value as arguments. | 1934 | respective value as arguments. |
| 1849 | If the function returns any non-nil value, | 1935 | If the function returns any non-nil value, |
| 1850 | the loop is broken, and the value is returned | 1936 | the loop is broken, and the value is returned |
| 1851 | as the final value of |verb|foreach|. | 1937 | as the final value of \verb|foreach|. |
| 1852 | 1938 | ||
| 1853 | This function could be defined in Lua: | 1939 | This function could be defined in Lua: |
| 1854 | \begin{verbatim} | 1940 | \begin{verbatim} |
| @@ -1868,7 +1954,7 @@ For each variable, | |||
| 1868 | the function is called with its name and its value as arguments. | 1954 | the function is called with its name and its value as arguments. |
| 1869 | If the function returns any non-nil value, | 1955 | If the function returns any non-nil value, |
| 1870 | the loop is broken, and the value is returned | 1956 | the loop is broken, and the value is returned |
| 1871 | as the final value of |verb|foreachvar|. | 1957 | as the final value of \verb|foreachvar|. |
| 1872 | 1958 | ||
| 1873 | This function could be defined in Lua: | 1959 | This function could be defined in Lua: |
| 1874 | \begin{verbatim} | 1960 | \begin{verbatim} |
| @@ -1908,7 +1994,7 @@ represents 10, `B' represents 11, and so forth, with `Z' representing 35. | |||
| 1908 | 1994 | ||
| 1909 | In base 10 (the default), the number may have a decimal part, | 1995 | In base 10 (the default), the number may have a decimal part, |
| 1910 | as well as an optional exponent part \see{coercion}. | 1996 | as well as an optional exponent part \see{coercion}. |
| 1911 | In other bases only integer numbers are accepted. | 1997 | In other bases only integers are accepted. |
| 1912 | 1998 | ||
| 1913 | \subsubsection*{\ff \T{type (v)}}\Deffunc{type}\label{pdf-type} | 1999 | \subsubsection*{\ff \T{type (v)}}\Deffunc{type}\label{pdf-type} |
| 1914 | This function allows Lua to test the type of a value. | 2000 | This function allows Lua to test the type of a value. |
| @@ -1930,14 +2016,15 @@ It receives one argument, and returns its tag (a number). | |||
| 1930 | This function sets the tag of a given table \see{TypesSec}. | 2016 | This function sets the tag of a given table \see{TypesSec}. |
| 1931 | \verb|tag| must be a value created with \verb|newtag| | 2017 | \verb|tag| must be a value created with \verb|newtag| |
| 1932 | \see{pdf-newtag}. | 2018 | \see{pdf-newtag}. |
| 2019 | It returns the value of its first argument (the table). | ||
| 1933 | For security reasons, | 2020 | For security reasons, |
| 1934 | it is impossible to change the tag of a userdata from Lua. | 2021 | it is impossible to change the tag of a userdata from Lua. |
| 1935 | 2022 | ||
| 2023 | |||
| 1936 | \subsubsection*{\ff \T{assert (v [, message])}}\Deffunc{assert} | 2024 | \subsubsection*{\ff \T{assert (v [, message])}}\Deffunc{assert} |
| 1937 | This function issues an \emph{``assertion failed!''} error | 2025 | This function issues an \emph{``assertion failed!''} error |
| 1938 | when its argument is \nil. | 2026 | when its argument is \nil. |
| 1939 | 2027 | This function is equivalent to the following Lua function: | |
| 1940 | This function could be defined in Lua: | ||
| 1941 | \begin{verbatim} | 2028 | \begin{verbatim} |
| 1942 | function assert (v, m) | 2029 | function assert (v, m) |
| 1943 | if not v then | 2030 | if not v then |
| @@ -1978,10 +2065,15 @@ this function can set global variables with strange names like | |||
| 1978 | \verb|"m v 1"| or \verb|34|. | 2065 | \verb|"m v 1"| or \verb|34|. |
| 1979 | It returns the value of its second argument. | 2066 | It returns the value of its second argument. |
| 1980 | 2067 | ||
| 2068 | The string \verb|name| does not need to be a | ||
| 2069 | syntactically valid variable name. | ||
| 2070 | |||
| 1981 | \subsubsection*{\ff \T{setglobal (name, value)}}\Deffunc{setglobal} | 2071 | \subsubsection*{\ff \T{setglobal (name, value)}}\Deffunc{setglobal} |
| 1982 | This function assigns the given value to a global variable, | 2072 | This function assigns the given value to a global variable, |
| 1983 | or calls a tag method. | 2073 | or calls a tag method. |
| 1984 | Its full semantics is explained in \See{tag-method}. | 2074 | Its full semantics is explained in \See{tag-method}. |
| 2075 | The string \verb|name| does not need to be a | ||
| 2076 | syntactically valid variable name. | ||
| 1985 | 2077 | ||
| 1986 | \subsubsection*{\ff \T{rawgetglobal (name)}}\Deffunc{rawgetglobal} | 2078 | \subsubsection*{\ff \T{rawgetglobal (name)}}\Deffunc{rawgetglobal} |
| 1987 | This function retrieves the value of a global variable. | 2079 | This function retrieves the value of a global variable. |
| @@ -1993,6 +2085,9 @@ This function retrieves the value of a global variable, | |||
| 1993 | or calls a tag method. | 2085 | or calls a tag method. |
| 1994 | Its full semantics is explained in \See{tag-method}. | 2086 | Its full semantics is explained in \See{tag-method}. |
| 1995 | 2087 | ||
| 2088 | The string \verb|name| does not need to be a | ||
| 2089 | syntactically valid variable name. | ||
| 2090 | |||
| 1996 | \subsubsection*{\ff \T{seterrormethod (newmethod)}} | 2091 | \subsubsection*{\ff \T{seterrormethod (newmethod)}} |
| 1997 | \label{pdf-seterrormethod} | 2092 | \label{pdf-seterrormethod} |
| 1998 | Sets the error handler \see{error}. | 2093 | Sets the error handler \see{error}. |
| @@ -2002,7 +2097,7 @@ Returns the old error handler. | |||
| 2002 | 2097 | ||
| 2003 | \subsubsection*{\ff \T{settagmethod (tag, event, newmethod)}} | 2098 | \subsubsection*{\ff \T{settagmethod (tag, event, newmethod)}} |
| 2004 | \Deffunc{settagmethod} | 2099 | \Deffunc{settagmethod} |
| 2005 | This function sets a new tag method to the given pair \M{<tag, event>}. | 2100 | This function sets a new tag method to the given pair \M{(tag, event)}. |
| 2006 | It returns the old method. | 2101 | It returns the old method. |
| 2007 | If \verb|newmethod| is \nil, | 2102 | If \verb|newmethod| is \nil, |
| 2008 | it restores the default behavior for the given event. | 2103 | it restores the default behavior for the given event. |
| @@ -2010,7 +2105,7 @@ it restores the default behavior for the given event. | |||
| 2010 | \subsubsection*{\ff \T{gettagmethod (tag, event)}} | 2105 | \subsubsection*{\ff \T{gettagmethod (tag, event)}} |
| 2011 | \Deffunc{gettagmethod} | 2106 | \Deffunc{gettagmethod} |
| 2012 | This function returns the current tag method | 2107 | This function returns the current tag method |
| 2013 | for a given pair \M{<tag, event>}. | 2108 | for a given pair \M{(tag, event)}. |
| 2014 | 2109 | ||
| 2015 | \subsubsection*{\ff \T{copytagmethods (tagto, tagfrom)}} | 2110 | \subsubsection*{\ff \T{copytagmethods (tagto, tagfrom)}} |
| 2016 | \Deffunc{copytagmethods} | 2111 | \Deffunc{copytagmethods} |
| @@ -2022,7 +2117,7 @@ it returns \verb|tagto|. | |||
| 2022 | This library provides generic functions for string manipulation, | 2117 | This library provides generic functions for string manipulation, |
| 2023 | such as finding and extracting substrings and pattern matching. | 2118 | such as finding and extracting substrings and pattern matching. |
| 2024 | When indexing a string, the first character is at position~1 | 2119 | When indexing a string, the first character is at position~1 |
| 2025 | (not~0, as in C). | 2120 | (not at~0, as in C). |
| 2026 | 2121 | ||
| 2027 | \subsubsection*{\ff \T{strfind (str, pattern [, init [, plain]])}} | 2122 | \subsubsection*{\ff \T{strfind (str, pattern [, init [, plain]])}} |
| 2028 | \Deffunc{strfind} | 2123 | \Deffunc{strfind} |
| @@ -2035,6 +2130,10 @@ If the pattern specifies captures, | |||
| 2035 | the captured strings are returned as extra results. | 2130 | the captured strings are returned as extra results. |
| 2036 | A third optional numerical argument specifies where to start the search; | 2131 | A third optional numerical argument specifies where to start the search; |
| 2037 | its default value is 1. | 2132 | its default value is 1. |
| 2133 | If \verb|init| is negative, | ||
| 2134 | it is replaced by the length of the string minus its | ||
| 2135 | absolute value plus 1. | ||
| 2136 | Therefore, \M{-1} points to the last character of \verb|str|. | ||
| 2038 | A value of 1 as a fourth optional argument | 2137 | A value of 1 as a fourth optional argument |
| 2039 | turns off the pattern matching facilities, | 2138 | turns off the pattern matching facilities, |
| 2040 | so the function does a plain ``find substring'' operation, | 2139 | so the function does a plain ``find substring'' operation, |
| @@ -2049,9 +2148,9 @@ starting at \verb|i| and running until \verb|j|. | |||
| 2049 | If \verb|i| or \verb|j| are negative, | 2148 | If \verb|i| or \verb|j| are negative, |
| 2050 | they are replaced by the length of the string minus their | 2149 | they are replaced by the length of the string minus their |
| 2051 | absolute value plus 1. | 2150 | absolute value plus 1. |
| 2052 | Therefore, -1 points to the last character of \verb|s| | 2151 | Therefore, \M{-1} points to the last character of \verb|s| |
| 2053 | and -2 to the previous one. | 2152 | and \M{-2} to the previous one. |
| 2054 | If \verb|j| is absent, it is assumed to be equal to -1 | 2153 | If \verb|j| is absent, it is assumed to be equal to \M{-1} |
| 2055 | (which is the same as the string length). | 2154 | (which is the same as the string length). |
| 2056 | In particular, | 2155 | In particular, |
| 2057 | the call \verb|strsub(s,1,j)| returns a prefix of \verb|s| | 2156 | the call \verb|strsub(s,1,j)| returns a prefix of \verb|s| |
| @@ -2063,19 +2162,33 @@ with length \verb|i|. | |||
| 2063 | Receives a string and returns a copy of that string with all | 2162 | Receives a string and returns a copy of that string with all |
| 2064 | upper case letters changed to lower case. | 2163 | upper case letters changed to lower case. |
| 2065 | All other characters are left unchanged. | 2164 | All other characters are left unchanged. |
| 2165 | The definition of what is an upper case | ||
| 2166 | letter depends on the current locale. | ||
| 2066 | 2167 | ||
| 2067 | \subsubsection*{\ff \T{strupper (s)}}\Deffunc{strupper} | 2168 | \subsubsection*{\ff \T{strupper (s)}}\Deffunc{strupper} |
| 2068 | Receives a string and returns a copy of that string with all | 2169 | Receives a string and returns a copy of that string with all |
| 2069 | lower case letters changed to upper case. | 2170 | lower case letters changed to upper case. |
| 2070 | All other characters are left unchanged. | 2171 | All other characters are left unchanged. |
| 2172 | The definition of what is a lower case | ||
| 2173 | letter depends on the current locale. | ||
| 2071 | 2174 | ||
| 2072 | \subsubsection*{\ff \T{strrep (s, n)}}\Deffunc{strrep} | 2175 | \subsubsection*{\ff \T{strrep (s, n)}}\Deffunc{strrep} |
| 2073 | Returns a string which is the concatenation of \verb|n| copies of | 2176 | Returns a string that is the concatenation of \verb|n| copies of |
| 2074 | the string \verb|s|. | 2177 | the string \verb|s|. |
| 2075 | 2178 | ||
| 2076 | \subsubsection*{\ff \T{ascii (s [, i])}}\Deffunc{ascii} | 2179 | \subsubsection*{\ff \T{ascii (s [, i])}}\Deffunc{ascii} |
| 2077 | Returns the ASCII code of the character \verb|s[i]|. | 2180 | Returns the internal numerical code of the character \verb|s[i]|. |
| 2078 | If \verb|i| is absent, then it is assumed to be 1. | 2181 | If \verb|i| is absent, then it is assumed to be 1. |
| 2182 | If \verb|i| is negative, | ||
| 2183 | it is replaced by the length of the string minus its | ||
| 2184 | absolute value plus 1. | ||
| 2185 | Therefore, \M{-1} points to the last character of \verb|s|. | ||
| 2186 | |||
| 2187 | \subsubsection*{\ff \T{int2str (i1, i2, \ldots)}}\Deffunc{int2str} | ||
| 2188 | Receives 0 or more integers. | ||
| 2189 | Returns a string with length equal to the number of arguments, | ||
| 2190 | wherein each character has ascii value equal | ||
| 2191 | to its correspondent argument. | ||
| 2079 | 2192 | ||
| 2080 | \subsubsection*{\ff \T{format (formatstring, e1, e2, \ldots)}}\Deffunc{format} | 2193 | \subsubsection*{\ff \T{format (formatstring, e1, e2, \ldots)}}\Deffunc{format} |
| 2081 | \label{format} | 2194 | \label{format} |
| @@ -2122,6 +2235,9 @@ the appropriate format string. | |||
| 2122 | For example, \verb|"%*g"| can be simulated with | 2235 | For example, \verb|"%*g"| can be simulated with |
| 2123 | \verb|"%"..width.."g"|. | 2236 | \verb|"%"..width.."g"|. |
| 2124 | 2237 | ||
| 2238 | \emph{Function \verb|format| can only be used with strings that do not | ||
| 2239 | contain zeros.} | ||
| 2240 | |||
| 2125 | \subsubsection*{\ff \T{gsub (s, pat, repl [, n])}} | 2241 | \subsubsection*{\ff \T{gsub (s, pat, repl [, n])}} |
| 2126 | \Deffunc{gsub} | 2242 | \Deffunc{gsub} |
| 2127 | Returns a copy of \verb|s|, | 2243 | Returns a copy of \verb|s|, |
| @@ -2137,7 +2253,7 @@ stands for the value of the n-th captured substring. | |||
| 2137 | 2253 | ||
| 2138 | If \verb|repl| is a function, then this function is called every time a | 2254 | If \verb|repl| is a function, then this function is called every time a |
| 2139 | match occurs, with all captured substrings passed as arguments, | 2255 | match occurs, with all captured substrings passed as arguments, |
| 2140 | in order (see below); | 2256 | in order (see below). |
| 2141 | If the value returned by this function is a string, | 2257 | If the value returned by this function is a string, |
| 2142 | then it is used as the replacement string; | 2258 | then it is used as the replacement string; |
| 2143 | otherwise, the replacement string is the empty string. | 2259 | otherwise, the replacement string is the empty string. |
| @@ -2213,7 +2329,7 @@ represents the complement of char-set, | |||
| 2213 | where char-set is interpreted as above. | 2329 | where char-set is interpreted as above. |
| 2214 | \end{description} | 2330 | \end{description} |
| 2215 | 2331 | ||
| 2216 | The definitions of letter, space, etc depend on the current locale. | 2332 | The definitions of letter, space, etc. depend on the current locale. |
| 2217 | In particular, the class \verb|[a-z]| may not be equivalent to \verb|%l|. | 2333 | In particular, the class \verb|[a-z]| may not be equivalent to \verb|%l|. |
| 2218 | The second form should be preferred for more portable programs. | 2334 | The second form should be preferred for more portable programs. |
| 2219 | 2335 | ||
| @@ -2283,8 +2399,8 @@ The library provides the following functions: | |||
| 2283 | \Deffunc{mod}\Deffunc{sin}\Deffunc{sqrt}\Deffunc{tan} | 2399 | \Deffunc{mod}\Deffunc{sin}\Deffunc{sqrt}\Deffunc{tan} |
| 2284 | \Deffunc{random}\Deffunc{randomseed} | 2400 | \Deffunc{random}\Deffunc{randomseed} |
| 2285 | \begin{verbatim} | 2401 | \begin{verbatim} |
| 2286 | abs acos asin atan atan2 ceil cos floor log log10 | 2402 | abs acos asin atan atan2 ceil cos deg floor log log10 |
| 2287 | max min mod sin sqrt tan random randomseed | 2403 | max min mod rad sin sqrt tan random randomseed |
| 2288 | \end{verbatim} | 2404 | \end{verbatim} |
| 2289 | plus a global variable \IndexVerb{PI}. | 2405 | plus a global variable \IndexVerb{PI}. |
| 2290 | Most of them | 2406 | Most of them |
| @@ -2297,7 +2413,7 @@ between radians and degrees. | |||
| 2297 | The function \verb|max| returns the maximum | 2413 | The function \verb|max| returns the maximum |
| 2298 | value of its numeric arguments. | 2414 | value of its numeric arguments. |
| 2299 | Similarly, \verb|min| computes the minimum. | 2415 | Similarly, \verb|min| computes the minimum. |
| 2300 | Both can be used with an unlimited number of arguments. | 2416 | Both can be used with 1, 2 or more arguments. |
| 2301 | 2417 | ||
| 2302 | The functions \verb|random| and \verb|randomseed| are interfaces to | 2418 | The functions \verb|random| and \verb|randomseed| are interfaces to |
| 2303 | the simple random generator functions \verb|rand| and \verb|srand|, | 2419 | the simple random generator functions \verb|rand| and \verb|srand|, |
| @@ -2305,7 +2421,7 @@ provided by ANSI C. | |||
| 2305 | The function \verb|random|, when called without arguments, | 2421 | The function \verb|random|, when called without arguments, |
| 2306 | returns a pseudo-random real number in the range \Math{[0,1)}. | 2422 | returns a pseudo-random real number in the range \Math{[0,1)}. |
| 2307 | When called with a number \Math{n}, | 2423 | When called with a number \Math{n}, |
| 2308 | returns a pseudo-random integer in the range \Math{[1,n]}. | 2424 | \verb|random| returns a pseudo-random integer in the range \Math{[1,n]}. |
| 2309 | 2425 | ||
| 2310 | 2426 | ||
| 2311 | \subsection{I/O Facilities} \label{libio} | 2427 | \subsection{I/O Facilities} \label{libio} |
| @@ -2347,7 +2463,7 @@ plus a string describing the error. | |||
| 2347 | \begin{quotation} | 2463 | \begin{quotation} |
| 2348 | \noindent | 2464 | \noindent |
| 2349 | \emph{System dependent}: if \verb|filename| starts with a \verb-|-, | 2465 | \emph{System dependent}: if \verb|filename| starts with a \verb-|-, |
| 2350 | then a \Index{piped input} is open, via function \IndexVerb{popen}. | 2466 | then a \Index{piped input} is opened, via function \IndexVerb{popen}. |
| 2351 | Not all systems implement pipes. | 2467 | Not all systems implement pipes. |
| 2352 | Moreover, | 2468 | Moreover, |
| 2353 | the number of files that can be open at the same time is | 2469 | the number of files that can be open at the same time is |
| @@ -2375,7 +2491,7 @@ plus a string describing the error. | |||
| 2375 | \begin{quotation} | 2491 | \begin{quotation} |
| 2376 | \noindent | 2492 | \noindent |
| 2377 | \emph{System dependent}: if \verb|filename| starts with a \verb-|-, | 2493 | \emph{System dependent}: if \verb|filename| starts with a \verb-|-, |
| 2378 | then a \Index{piped output} is open, via function \IndexVerb{popen}. | 2494 | then a \Index{piped output} is opened, via function \IndexVerb{popen}. |
| 2379 | Not all systems implement pipes. | 2495 | Not all systems implement pipes. |
| 2380 | Moreover, | 2496 | Moreover, |
| 2381 | the number of files that can be open at the same time is | 2497 | the number of files that can be open at the same time is |
| @@ -2391,7 +2507,8 @@ this function does not erase any previous content of the file. | |||
| 2391 | If this function fails, it returns \nil, | 2507 | If this function fails, it returns \nil, |
| 2392 | plus a string describing the error. | 2508 | plus a string describing the error. |
| 2393 | 2509 | ||
| 2394 | Notice that function \verb|writeto| is available to close an output file. | 2510 | Notice that function \verb|writeto| is |
| 2511 | available to close an output file opened by \verb|appendto|. | ||
| 2395 | 2512 | ||
| 2396 | \subsubsection*{\ff \T{remove (filename)}}\Deffunc{remove} | 2513 | \subsubsection*{\ff \T{remove (filename)}}\Deffunc{remove} |
| 2397 | 2514 | ||
| @@ -2557,13 +2674,13 @@ given function has been defined. | |||
| 2557 | If the ``function'' is in fact the main code of a chunk, | 2674 | If the ``function'' is in fact the main code of a chunk, |
| 2558 | then \verb|linedefined| is 0. | 2675 | then \verb|linedefined| is 0. |
| 2559 | If the function is a C function, | 2676 | If the function is a C function, |
| 2560 | then \verb|linedefined| is -1, and \verb|filename| is \verb|"(C)"|. | 2677 | then \verb|linedefined| is \M{-1}, and \verb|filename| is \verb|"(C)"|. |
| 2561 | 2678 | ||
| 2562 | The function \verb|lua_currentline| gives the current line where | 2679 | The function \verb|lua_currentline| gives the current line where |
| 2563 | a given function is executing. | 2680 | a given function is executing. |
| 2564 | It only works if the function has been compiled with debug | 2681 | It only works if the function has been compiled with debug |
| 2565 | information \see{pragma}. | 2682 | information \see{pragma}. |
| 2566 | When no line information is available, it returns -1. | 2683 | When no line information is available, it returns \M{-1}. |
| 2567 | 2684 | ||
| 2568 | Function \verb|lua_getobjname| tries to find a reasonable name for | 2685 | Function \verb|lua_getobjname| tries to find a reasonable name for |
| 2569 | a given function. | 2686 | a given function. |
| @@ -2661,12 +2778,12 @@ accepting commands from standard input until an \verb|EOF|. | |||
| 2661 | Each line entered is immediately executed. | 2778 | Each line entered is immediately executed. |
| 2662 | \item[\T{-q}] same as \T{-i}, but without a prompt (quiet mode). | 2779 | \item[\T{-q}] same as \T{-i}, but without a prompt (quiet mode). |
| 2663 | \item[\T{-}] executes \verb|stdin| as a file. | 2780 | \item[\T{-}] executes \verb|stdin| as a file. |
| 2664 | \item[\T{var=value}] sets global \verb|var| with string \verb|value|. | 2781 | \item[\T{var=value}] sets global \verb|var| with string \verb|"value"|. |
| 2665 | \item[\T{filename}] executes file \verb|filename| as a Lua chunk. | 2782 | \item[\T{filename}] executes file \verb|filename| as a Lua chunk. |
| 2666 | \end{description} | 2783 | \end{description} |
| 2667 | When called without arguments, | 2784 | When called without arguments, |
| 2668 | Lua behaves as \verb|lua -v -i| when \verb|stdin| is a terminal, | 2785 | Lua behaves as \verb|lua -v -i| when \verb|stdin| is a terminal, |
| 2669 | and \verb|lua -| otherwise. | 2786 | and as \verb|lua -| otherwise. |
| 2670 | 2787 | ||
| 2671 | All arguments are handled in order. | 2788 | All arguments are handled in order. |
| 2672 | For instance, an invocation like | 2789 | For instance, an invocation like |
| @@ -2675,13 +2792,13 @@ $ lua -i a=test prog.lua | |||
| 2675 | \end{verbatim} | 2792 | \end{verbatim} |
| 2676 | will first interact with the user until an \verb|EOF|, | 2793 | will first interact with the user until an \verb|EOF|, |
| 2677 | then will set \verb|a| to \verb|"test"|, | 2794 | then will set \verb|a| to \verb|"test"|, |
| 2678 | and finally will run file \verb|prog.lua|. | 2795 | and finally will run the file \verb|prog.lua|. |
| 2679 | 2796 | ||
| 2680 | When in interactive mode, | 2797 | When in interactive mode, |
| 2681 | a multi-line statement can be written finishing intermediate | 2798 | a multi-line statement can be written finishing intermediate |
| 2682 | lines with a backslash (\verb|\|). | 2799 | lines with a backslash (\verb|\|). |
| 2683 | The prompt presented is the value of the global variable \verb|_PROMPT|. | 2800 | The prompt presented is the value of the global variable \verb|_PROMPT|. |
| 2684 | For instance, the prompt can be changed like below: | 2801 | Therefore, the prompt can be changed like below: |
| 2685 | \begin{verbatim} | 2802 | \begin{verbatim} |
| 2686 | $ lua _PROMPT='myprompt> ' -i | 2803 | $ lua _PROMPT='myprompt> ' -i |
| 2687 | \end{verbatim} | 2804 | \end{verbatim} |
| @@ -2712,8 +2829,8 @@ Here is a list of all these incompatibilities. | |||
| 2712 | \item To support multiple contexts, | 2829 | \item To support multiple contexts, |
| 2713 | Lua 3.1 must be explicitly opened before used. | 2830 | Lua 3.1 must be explicitly opened before used. |
| 2714 | However, all standard libraries check whether Lua is already opened, | 2831 | However, all standard libraries check whether Lua is already opened, |
| 2715 | so any program that opens at least one standard library before using | 2832 | so any program that opens at least one standard library before calling |
| 2716 | Lua API does not need to be modified. | 2833 | Lua does not need to be modified. |
| 2717 | 2834 | ||
| 2718 | \item Function \verb|dostring| no longer accepts an optional second argument, | 2835 | \item Function \verb|dostring| no longer accepts an optional second argument, |
| 2719 | with a temporary error method. | 2836 | with a temporary error method. |
| @@ -2725,9 +2842,9 @@ Closures replace this feature with advantage. | |||
| 2725 | 2842 | ||
| 2726 | \item The syntax for function declaration is now more restricted; | 2843 | \item The syntax for function declaration is now more restricted; |
| 2727 | for instance, the old syntax \verb|function f[exp] (x) ... end| is not | 2844 | for instance, the old syntax \verb|function f[exp] (x) ... end| is not |
| 2728 | accepted in 3.1. | 2845 | accepted in Lua 3.1. |
| 2729 | In these cases, | 2846 | In these cases, |
| 2730 | programs should use an explicit assignment instead, like | 2847 | programs should use an explicit assignment instead, such as |
| 2731 | \verb|f[exp] = function (x) ... end|. | 2848 | \verb|f[exp] = function (x) ... end|. |
| 2732 | 2849 | ||
| 2733 | \item Old pre-compiled code is obsolete, and must be re-compiled. | 2850 | \item Old pre-compiled code is obsolete, and must be re-compiled. |
