diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-11-18 12:39:34 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-11-18 12:39:34 -0200 |
commit | f4d3bc52f486bb1f0d32c6240faffb0e63bbb8fe (patch) | |
tree | a3ee309c1a748c31ad096426ce2807a5fbb73f3a | |
parent | 18765d900e759713d9501e6434191c641f508e58 (diff) | |
download | lua-f4d3bc52f486bb1f0d32c6240faffb0e63bbb8fe.tar.gz lua-f4d3bc52f486bb1f0d32c6240faffb0e63bbb8fe.tar.bz2 lua-f4d3bc52f486bb1f0d32c6240faffb0e63bbb8fe.zip |
towards 5.0 beta
-rw-r--r-- | manual.tex | 456 |
1 files changed, 293 insertions, 163 deletions
@@ -1,4 +1,4 @@ | |||
1 | % $Id: manual.tex,v 1.61 2002/08/13 20:00:51 roberto Exp roberto $ | 1 | % $Id: manual.tex,v 1.63 2002/11/18 14:37:57 roberto Exp $ |
2 | %{[( | 2 | %{[( |
3 | 3 | ||
4 | \documentclass[11pt,twoside]{article} | 4 | \documentclass[11pt,twoside]{article} |
@@ -26,7 +26,7 @@ | |||
26 | \newcommand{\Index}[1]{#1\index{#1@{\lowercase{#1}}}} | 26 | \newcommand{\Index}[1]{#1\index{#1@{\lowercase{#1}}}} |
27 | \newcommand{\IndexVerb}[1]{\T{#1}\index{#1@{\tt #1}}} | 27 | \newcommand{\IndexVerb}[1]{\T{#1}\index{#1@{\tt #1}}} |
28 | \newcommand{\IndexEmph}[1]{\emph{#1}\index{#1@{\lowercase{#1}}}} | 28 | \newcommand{\IndexEmph}[1]{\emph{#1}\index{#1@{\lowercase{#1}}}} |
29 | \newcommand{\IndexTM}[1]{\index{#1 event@{``#1'' event}}\index{tag method!#1}} | 29 | \newcommand{\IndexTM}[1]{\index{#1 event@{``#1'' event}}\index{metamethod!#1}} |
30 | \newcommand{\Def}[1]{\emph{#1}\index{#1}} | 30 | \newcommand{\Def}[1]{\emph{#1}\index{#1}} |
31 | \newcommand{\IndexAPI}[1]{\T{#1}\DefAPI{#1}} | 31 | \newcommand{\IndexAPI}[1]{\T{#1}\DefAPI{#1}} |
32 | \newcommand{\IndexLIB}[1]{\T{#1}\DefLIB{#1}} | 32 | \newcommand{\IndexLIB}[1]{\T{#1}\DefLIB{#1}} |
@@ -36,7 +36,7 @@ | |||
36 | 36 | ||
37 | \newcommand{\ff}{$\bullet$\ } | 37 | \newcommand{\ff}{$\bullet$\ } |
38 | 38 | ||
39 | \newcommand{\Version}{5.0 (alpha)} | 39 | \newcommand{\Version}{5.0 (beta)} |
40 | 40 | ||
41 | % changes to bnf.sty by LHF | 41 | % changes to bnf.sty by LHF |
42 | \renewcommand{\Or}{$|$ } | 42 | \renewcommand{\Or}{$|$ } |
@@ -58,7 +58,7 @@ | |||
58 | \parindent=0pt | 58 | \parindent=0pt |
59 | \vglue1.5in | 59 | \vglue1.5in |
60 | {\LARGE\bf | 60 | {\LARGE\bf |
61 | The Programming Language Lua} | 61 | The Lua Programming Language} |
62 | \hfill | 62 | \hfill |
63 | \vskip4pt \hrule height 4pt width \hsize \vskip4pt | 63 | \vskip4pt \hrule height 4pt width \hsize \vskip4pt |
64 | \hfill | 64 | \hfill |
@@ -134,7 +134,7 @@ Waldemar Celes | |||
134 | \tecgraf\ --- Computer Science Department --- PUC-Rio | 134 | \tecgraf\ --- Computer Science Department --- PUC-Rio |
135 | } | 135 | } |
136 | 136 | ||
137 | %\date{{\small \tt\$Date: 2002/08/13 20:00:51 $ $}} | 137 | %\date{{\small \tt\$Date: 2002/11/18 14:37:57 $ $}} |
138 | 138 | ||
139 | \maketitle | 139 | \maketitle |
140 | 140 | ||
@@ -272,8 +272,7 @@ This environment is initialized with a call from the embedding program to | |||
272 | \verb|lua_open| and | 272 | \verb|lua_open| and |
273 | persists until a call to \verb|lua_close| | 273 | persists until a call to \verb|lua_close| |
274 | or the end of the embedding program. | 274 | or the end of the embedding program. |
275 | If necessary, | 275 | The host program can create multiple independent global |
276 | the host programmer can create multiple independent global | ||
277 | environments, and freely switch between them \see{mangstate}. | 276 | environments, and freely switch between them \see{mangstate}. |
278 | 277 | ||
279 | The unit of execution of Lua is called a \Def{chunk}. | 278 | The unit of execution of Lua is called a \Def{chunk}. |
@@ -285,13 +284,12 @@ When a chunk is executed, first it is pre-compiled into opcodes for | |||
285 | a virtual machine, | 284 | a virtual machine, |
286 | and then the compiled statements are executed | 285 | and then the compiled statements are executed |
287 | by an interpreter for the virtual machine. | 286 | by an interpreter for the virtual machine. |
288 | All modifications a chunk effects on the global environment persist | 287 | All modifications a chunk makes to the global environment persist |
289 | after the chunk ends. | 288 | after the chunk ends. |
290 | 289 | ||
291 | Chunks may also be pre-compiled into binary form and stored in files; | 290 | Chunks may also be pre-compiled into binary form and stored in files; |
292 | see program \IndexVerb{luac} for details. | 291 | see program \IndexVerb{luac} for details. |
293 | Text files with chunks and their binary pre-compiled forms | 292 | Programs in source and compiled forms are interchangeable; |
294 | are interchangeable; | ||
295 | Lua automatically detects the file type and acts accordingly. | 293 | Lua automatically detects the file type and acts accordingly. |
296 | \index{pre-compilation} | 294 | \index{pre-compilation} |
297 | 295 | ||
@@ -315,8 +313,8 @@ There are seven \Index{basic types} in Lua: | |||
315 | whose main property is to be different from any other value; | 313 | whose main property is to be different from any other value; |
316 | usually it represents the absence of a useful value. | 314 | usually it represents the absence of a useful value. |
317 | \emph{Boolean} is the type of the values \False{} and \True. | 315 | \emph{Boolean} is the type of the values \False{} and \True. |
318 | In Lua, both \nil{} and \False{} make a condition fails, | 316 | In Lua, both \nil{} and \False{} make a condition false, |
319 | and any other value makes it succeeds. | 317 | and any other value makes it true. |
320 | \emph{Number} represents real (double-precision floating-point) numbers. | 318 | \emph{Number} represents real (double-precision floating-point) numbers. |
321 | \emph{String} represents arrays of characters. | 319 | \emph{String} represents arrays of characters. |
322 | \index{eight-bit clean} | 320 | \index{eight-bit clean} |
@@ -331,8 +329,8 @@ Lua can call (and manipulate) functions written in Lua and | |||
331 | functions written in C | 329 | functions written in C |
332 | \see{functioncall}. | 330 | \see{functioncall}. |
333 | 331 | ||
334 | The type \emph{userdata} is provided to allow the store of | 332 | The type \emph{userdata} is provided to allow arbitrary C data to |
335 | arbitrary C data in Lua variables. | 333 | be stored in Lua variables. |
336 | This type corresponds to a block of raw memory | 334 | This type corresponds to a block of raw memory |
337 | and has no pre-defined operations in Lua, | 335 | and has no pre-defined operations in Lua, |
338 | except assignment and identity test. | 336 | except assignment and identity test. |
@@ -367,7 +365,7 @@ So, tables may also carry \emph{methods} \see{func-def}. | |||
367 | Tables, functions, and userdata values are \emph{objects}: | 365 | Tables, functions, and userdata values are \emph{objects}: |
368 | variables do not actually \emph{contain} these values, | 366 | variables do not actually \emph{contain} these values, |
369 | only \emph{references} to them. | 367 | only \emph{references} to them. |
370 | Assignment, parameter passing, and returns from functions | 368 | Assignment, parameter passing, and function returns |
371 | always manipulate references to these values, | 369 | always manipulate references to these values, |
372 | and do not imply any kind of copy. | 370 | and do not imply any kind of copy. |
373 | 371 | ||
@@ -377,7 +375,7 @@ of a given value \see{pdf-type}. | |||
377 | 375 | ||
378 | \subsubsection{Metatables} | 376 | \subsubsection{Metatables} |
379 | 377 | ||
380 | Each table or userdata object in Lua may have a \Index{metatable}. | 378 | Each table and userdata object in Lua may have a \Index{metatable}. |
381 | 379 | ||
382 | You can change several aspects of the behavior | 380 | You can change several aspects of the behavior |
383 | of an object by setting specific fields in its metatable. | 381 | of an object by setting specific fields in its metatable. |
@@ -389,11 +387,11 @@ Lua calls that function to perform the addition. | |||
389 | We call the keys in a metatable \Index{events}, | 387 | We call the keys in a metatable \Index{events}, |
390 | and the values \Index{metamethods}. | 388 | and the values \Index{metamethods}. |
391 | In the previous example, \verb|"add"| is the event, | 389 | In the previous example, \verb|"add"| is the event, |
392 | and the metamethod is the function that performs the addition. | 390 | and the function is the metamethod that performs the addition. |
393 | 391 | ||
394 | A metatable controls how an object behaves in arithmetic operations, | 392 | A metatable controls how an object behaves in arithmetic operations, |
395 | order comparisons, concatenation, and indexing. | 393 | order comparisons, concatenation, and indexing. |
396 | A metatable can also defines a function to be called when a userdata | 394 | A metatable can also define a function to be called when a userdata |
397 | is garbage collected. | 395 | is garbage collected. |
398 | \See{metatable} gives a detailed description of which events you | 396 | \See{metatable} gives a detailed description of which events you |
399 | can control with metatables. | 397 | can control with metatables. |
@@ -499,7 +497,12 @@ A table with both weak keys and weak values allows the collection of | |||
499 | both keys and values. | 497 | both keys and values. |
500 | In any case, if either the key or the value is collected, | 498 | In any case, if either the key or the value is collected, |
501 | the whole pair is removed from the table. | 499 | the whole pair is removed from the table. |
502 | The weakness of a table is set with the \verb|setmode| function. | 500 | The weakness of a table is controled by the value of the |
501 | \verb|__mode| field of its metatable. | ||
502 | If the \verb|__mode| field is a string containing the \verb|k| character, | ||
503 | the keys in the table are weak. | ||
504 | If \verb|__mode| contains \verb|v|, | ||
505 | the values in the table are weak. | ||
503 | 506 | ||
504 | 507 | ||
505 | %------------------------------------------------------------------------------ | 508 | %------------------------------------------------------------------------------ |
@@ -527,10 +530,10 @@ and cannot be used as identifiers: | |||
527 | \index{reserved words} | 530 | \index{reserved words} |
528 | \begin{verbatim} | 531 | \begin{verbatim} |
529 | and break do else elseif | 532 | and break do else elseif |
530 | end false for function global | 533 | end false for function if |
531 | if in local nil not | 534 | in local nil not or |
532 | or repeat return then true | 535 | repeat return then true until |
533 | until while | 536 | while |
534 | \end{verbatim} | 537 | \end{verbatim} |
535 | 538 | ||
536 | Lua is a case-sensitive language: | 539 | Lua is a case-sensitive language: |
@@ -542,8 +545,8 @@ are reserved for internal variables. | |||
542 | 545 | ||
543 | The following strings denote other \Index{tokens}: | 546 | The following strings denote other \Index{tokens}: |
544 | \begin{verbatim} | 547 | \begin{verbatim} |
545 | + - * / ^ % | 548 | + - * / ^ = |
546 | ~= <= >= < > == = | 549 | ~= <= >= < > == |
547 | ( ) { } [ ] | 550 | ( ) { } [ ] |
548 | ; : , . .. ... | 551 | ; : , . .. ... |
549 | \end{verbatim} | 552 | \end{verbatim} |
@@ -1048,17 +1051,9 @@ from lower to higher priority: | |||
1048 | not - (unary) | 1051 | not - (unary) |
1049 | ^ | 1052 | ^ |
1050 | \end{verbatim} | 1053 | \end{verbatim} |
1051 | All binary operators are left associative, | 1054 | The \verb|..| (concatenation) and \verb|^| (exponentiation) |
1052 | except for \verb|^| (exponentiation), | 1055 | operators are right associative. |
1053 | which is right associative. | 1056 | All other binary operators are left associative. |
1054 | \NOTE | ||
1055 | The pre-compiler may rearrange the order of evaluation of | ||
1056 | associative operators, | ||
1057 | and may exchange the operands of commutative operators, | ||
1058 | as long as these optimizations do not change normal results. | ||
1059 | However, these optimizations may change some results | ||
1060 | if you define non-associative (or non-commutative) | ||
1061 | metamethods for those operators. | ||
1062 | 1057 | ||
1063 | \subsubsection{Table Constructors} \label{tableconstructor} | 1058 | \subsubsection{Table Constructors} \label{tableconstructor} |
1064 | Table \Index{constructors} are expressions that create tables; | 1059 | Table \Index{constructors} are expressions that create tables; |
@@ -1561,12 +1556,7 @@ the \verb|..| (concatenation) operation. | |||
1561 | \end{verbatim} | 1556 | \end{verbatim} |
1562 | 1557 | ||
1563 | \item[``index'':]\IndexTM{index} | 1558 | \item[``index'':]\IndexTM{index} |
1564 | This handler is called when Lua tries to retrieve the value of an index | 1559 | The ``gettable'' operation \verb|table[key]|. |
1565 | not present in a table. | ||
1566 | See the ``gettable'' operation for its semantics. | ||
1567 | |||
1568 | \item[``gettable'':]\IndexTM{gettable} | ||
1569 | called whenever Lua accesses an indexed variable. | ||
1570 | \begin{verbatim} | 1560 | \begin{verbatim} |
1571 | function gettable_event (table, key) | 1561 | function gettable_event (table, key) |
1572 | local h | 1562 | local h |
@@ -1576,7 +1566,7 @@ called whenever Lua accesses an indexed variable. | |||
1576 | h = metatable(table).__index | 1566 | h = metatable(table).__index |
1577 | if h == nil then return nil end | 1567 | if h == nil then return nil end |
1578 | else | 1568 | else |
1579 | h = metatable(table).__gettable | 1569 | h = metatable(table).__index |
1580 | if h == nil then | 1570 | if h == nil then |
1581 | error("indexed expression not a table"); | 1571 | error("indexed expression not a table"); |
1582 | end | 1572 | end |
@@ -1588,12 +1578,7 @@ called whenever Lua accesses an indexed variable. | |||
1588 | \end{verbatim} | 1578 | \end{verbatim} |
1589 | 1579 | ||
1590 | \item[``newindex'':]\IndexTM{index} | 1580 | \item[``newindex'':]\IndexTM{index} |
1591 | This handler is called when Lua tries to insert the value of an index | 1581 | The ``settable'' operation \verb|table[key] = value|. |
1592 | not present in a table. | ||
1593 | See the ``settable'' operation for its semantics. | ||
1594 | |||
1595 | \item[``settable'':]\IndexTM{settable} | ||
1596 | called when Lua assigns to an indexed variable. | ||
1597 | \begin{verbatim} | 1582 | \begin{verbatim} |
1598 | function settable_event (table, key, value) | 1583 | function settable_event (table, key, value) |
1599 | local h | 1584 | local h |
@@ -1603,7 +1588,7 @@ called when Lua assigns to an indexed variable. | |||
1603 | h = metatable(table).__newindex | 1588 | h = metatable(table).__newindex |
1604 | if h == nil then rawset(table, key, value); return end | 1589 | if h == nil then rawset(table, key, value); return end |
1605 | else | 1590 | else |
1606 | h = metatable(table).__settable | 1591 | h = metatable(table).__newindex |
1607 | if h == nil then | 1592 | if h == nil then |
1608 | error("indexed expression not a table"); | 1593 | error("indexed expression not a table"); |
1609 | end | 1594 | end |
@@ -1658,6 +1643,56 @@ with the last userdata created in the program | |||
1658 | 1643 | ||
1659 | 1644 | ||
1660 | 1645 | ||
1646 | \subsection{Coroutines} | ||
1647 | |||
1648 | Lua supports coroutines, | ||
1649 | also called \emph{semi-coroutines}, \emph{generators}, | ||
1650 | or \emph{colaborative multithreading}. | ||
1651 | A coroutine in Lua represents an independent thread of execution. | ||
1652 | Unlike ``real'' threads, however, | ||
1653 | a coroutine only suspends its execution by explicitly calling | ||
1654 | an yield function. | ||
1655 | |||
1656 | You create a coroutine with a call to \verb|coroutine.create|. | ||
1657 | Its sole argument is a function, | ||
1658 | which is the main function of the coroutine. | ||
1659 | The \verb|coroutine.create| only creates a new coroutine and | ||
1660 | returns a handle to it (an object of type \emph{thread}). | ||
1661 | It does not start the coroutine execution. | ||
1662 | |||
1663 | When you first call \verb|coroutine.resume|, | ||
1664 | passing as argument the thread returned by \verb|coroutine.create|, | ||
1665 | the coroutine starts its execution, | ||
1666 | at the first line of its main function. | ||
1667 | Extra arguments passed to \verb|coroutine.resume| are given as | ||
1668 | parameters for the coroutine main function. | ||
1669 | After the coroutine starts running, | ||
1670 | it runs until it terminates or it \emph{yields}. | ||
1671 | |||
1672 | A coroutine can terminate its execution in two ways: | ||
1673 | Normally, when its main function returns | ||
1674 | (explicitly or implicitly, after the last instruction); | ||
1675 | and abnormally, if there is an unprotected error. | ||
1676 | In the first case, \verb|coroutine.resume| returns \True, | ||
1677 | plus any values returned by the coroutine main function. | ||
1678 | In case of errors, \verb|coroutine.resume| returns \False | ||
1679 | plus an error message. | ||
1680 | |||
1681 | A coroutine yields calling \verb|coroutine.yield|. | ||
1682 | When a coroutine yields, | ||
1683 | the corresponding \verb|coroutine.resume| returns immediately, | ||
1684 | even if the yield happens inside nested function calls | ||
1685 | (that is, not in the main function, | ||
1686 | but in a function directly or indirectly called by the main function). | ||
1687 | In the case of a yield, \verb|coroutine.resume| also returns \True, | ||
1688 | plus any values passed to \verb|coroutine.yield|. | ||
1689 | The next time you resume the same coroutine, | ||
1690 | it continues its execution from the point where it yielded, | ||
1691 | with the call to \verb|coroutine.yield| returning any extra | ||
1692 | arguments passed to \verb|coroutine.resume|. | ||
1693 | |||
1694 | |||
1695 | |||
1661 | %------------------------------------------------------------------------------ | 1696 | %------------------------------------------------------------------------------ |
1662 | \section{The Application Program Interface}\label{API} | 1697 | \section{The Application Program Interface}\label{API} |
1663 | \index{C API} | 1698 | \index{C API} |
@@ -1682,7 +1717,7 @@ The Lua library is fully reentrant: | |||
1682 | it has no global variables. | 1717 | it has no global variables. |
1683 | \index{state} | 1718 | \index{state} |
1684 | The whole state of the Lua interpreter | 1719 | The whole state of the Lua interpreter |
1685 | (global variables, stack, tag methods, etc.)\ | 1720 | (global variables, stack, etc.) |
1686 | is stored in a dynamically allocated structure of type \verb|lua_State|; | 1721 | is stored in a dynamically allocated structure of type \verb|lua_State|; |
1687 | \DefAPI{lua_State} | 1722 | \DefAPI{lua_State} |
1688 | this state must be passed as the first argument to | 1723 | this state must be passed as the first argument to |
@@ -1703,8 +1738,8 @@ To release a state created with \verb|lua_open|, call | |||
1703 | This function destroys all objects in the given Lua environment | 1738 | This function destroys all objects in the given Lua environment |
1704 | (calling the corresponding garbage-collection metamethods, if any) | 1739 | (calling the corresponding garbage-collection metamethods, if any) |
1705 | and frees all dynamic memory used by that state. | 1740 | and frees all dynamic memory used by that state. |
1706 | Usually, you do not need to call this function, | 1741 | On several platforms, you may not need to call this function, |
1707 | because all resources are naturally released when your program ends. | 1742 | because all resources are naturally released when the host program ends. |
1708 | On the other hand, | 1743 | On the other hand, |
1709 | long-running programs --- | 1744 | long-running programs --- |
1710 | like a daemon or a web server --- | 1745 | like a daemon or a web server --- |
@@ -1727,7 +1762,7 @@ The following function creates a new ``thread'' in Lua: | |||
1727 | \end{verbatim} | 1762 | \end{verbatim} |
1728 | \DefAPI{lua_newthread} | 1763 | \DefAPI{lua_newthread} |
1729 | The new state returned by this function shares with the original state | 1764 | The new state returned by this function shares with the original state |
1730 | all global environment (such as tables, tag methods, etc.), | 1765 | all global environment (such as tables), |
1731 | but has an independent run-time stack. | 1766 | but has an independent run-time stack. |
1732 | (The use of these multiple stacks must be ``syncronized'' with C. | 1767 | (The use of these multiple stacks must be ``syncronized'' with C. |
1733 | How to explain that? TO BE WRITTEN.) | 1768 | How to explain that? TO BE WRITTEN.) |
@@ -1754,6 +1789,8 @@ Each C invocation has its own stack. | |||
1754 | Whenever Lua calls C, the called function gets a new stack, | 1789 | Whenever Lua calls C, the called function gets a new stack, |
1755 | which is independent of previous stacks or of stacks of still | 1790 | which is independent of previous stacks or of stacks of still |
1756 | active C functions. | 1791 | active C functions. |
1792 | That stack contains initially any arguments to the C function, | ||
1793 | and it is where the C function pushes its results \see{LuacallC}. | ||
1757 | 1794 | ||
1758 | For convenience, | 1795 | For convenience, |
1759 | most query operations in the API do not follow a strict stack discipline. | 1796 | most query operations in the API do not follow a strict stack discipline. |
@@ -1872,8 +1909,8 @@ then | |||
1872 | lua_insert(L, 1) --> 30 10 20 30 40* | 1909 | lua_insert(L, 1) --> 30 10 20 30 40* |
1873 | lua_insert(L, -1) --> 30 10 20 30 40* (no effect) | 1910 | lua_insert(L, -1) --> 30 10 20 30 40* (no effect) |
1874 | lua_replace(L, 2) --> 30 40 20 30* | 1911 | lua_replace(L, 2) --> 30 40 20 30* |
1875 | lua_settop(L, -3) --> 30 40 20* | 1912 | lua_settop(L, -3) --> 30 40* |
1876 | lua_settop(L, 6) --> 30 40 20 nil nil nil* | 1913 | lua_settop(L, 6) --> 30 40 nil nil nil nil* |
1877 | \end{verbatim} | 1914 | \end{verbatim} |
1878 | 1915 | ||
1879 | 1916 | ||
@@ -1883,22 +1920,22 @@ then | |||
1883 | To check the type of a stack element, | 1920 | To check the type of a stack element, |
1884 | the following functions are available: | 1921 | the following functions are available: |
1885 | \begin{verbatim} | 1922 | \begin{verbatim} |
1886 | int lua_type (lua_State *L, int index); | 1923 | int lua_type (lua_State *L, int index); |
1887 | int lua_isnil (lua_State *L, int index); | 1924 | int lua_isnil (lua_State *L, int index); |
1888 | int lua_isboolean (lua_State *L, int index); | 1925 | int lua_isboolean (lua_State *L, int index); |
1889 | int lua_isnumber (lua_State *L, int index); | 1926 | int lua_isnumber (lua_State *L, int index); |
1890 | int lua_isstring (lua_State *L, int index); | 1927 | int lua_isstring (lua_State *L, int index); |
1891 | int lua_istable (lua_State *L, int index); | 1928 | int lua_istable (lua_State *L, int index); |
1892 | int lua_isfunction (lua_State *L, int index); | 1929 | int lua_isfunction (lua_State *L, int index); |
1893 | int lua_iscfunction (lua_State *L, int index); | 1930 | int lua_iscfunction (lua_State *L, int index); |
1894 | int lua_isuserdata (lua_State *L, int index); | 1931 | int lua_isuserdata (lua_State *L, int index); |
1895 | int lua_isdataval (lua_State *L, int index); | 1932 | int lua_islightuserdata (lua_State *L, int index); |
1896 | \end{verbatim} | 1933 | \end{verbatim} |
1897 | \DefAPI{lua_type} | 1934 | \DefAPI{lua_type} |
1898 | \DefAPI{lua_isnil}\DefAPI{lua_isnumber}\DefAPI{lua_isstring} | 1935 | \DefAPI{lua_isnil}\DefAPI{lua_isnumber}\DefAPI{lua_isstring} |
1899 | \DefAPI{lua_istable}\DefAPI{lua_isboolean} | 1936 | \DefAPI{lua_istable}\DefAPI{lua_isboolean} |
1900 | \DefAPI{lua_isfunction}\DefAPI{lua_iscfunction} | 1937 | \DefAPI{lua_isfunction}\DefAPI{lua_iscfunction} |
1901 | \DefAPI{lua_isuserdata}\DefAPI{lua_isdataval} | 1938 | \DefAPI{lua_isuserdata}\DefAPI{lua_islightuserdata} |
1902 | These functions can be called with any acceptable index. | 1939 | These functions can be called with any acceptable index. |
1903 | 1940 | ||
1904 | \verb|lua_type| returns the type of a value in the stack, | 1941 | \verb|lua_type| returns the type of a value in the stack, |
@@ -1925,13 +1962,16 @@ with the given type, and 0 otherwise. | |||
1925 | \verb|lua_isboolean| is an exception to this rule, | 1962 | \verb|lua_isboolean| is an exception to this rule, |
1926 | and it succeeds only for boolean values | 1963 | and it succeeds only for boolean values |
1927 | (otherwise it would be useless, | 1964 | (otherwise it would be useless, |
1928 | as any value is compatible with a boolean). | 1965 | as any value has a boolean value). |
1929 | They always return 0 for a non-valid index. | 1966 | They always return 0 for a non-valid index. |
1930 | \verb|lua_isnumber| accepts numbers and numerical strings, | 1967 | \verb|lua_isnumber| accepts numbers and numerical strings, |
1931 | \verb|lua_isstring| accepts strings and numbers \see{coercion}, | 1968 | \verb|lua_isstring| accepts strings and numbers \see{coercion}, |
1932 | and \verb|lua_isfunction| accepts both Lua functions and C~functions. | 1969 | \verb|lua_isfunction| accepts both Lua functions and C~functions, |
1970 | and \verb|lua_isuserdata| accepts both full and ligth userdata. | ||
1933 | To distinguish between Lua functions and C~functions, | 1971 | To distinguish between Lua functions and C~functions, |
1934 | you should use \verb|lua_iscfunction|. | 1972 | you should use \verb|lua_iscfunction|. |
1973 | To distinguish between full and ligth userdata, | ||
1974 | you can use \verb|lua_islightuserdata|. | ||
1935 | To distinguish between numbers and numerical strings, | 1975 | To distinguish between numbers and numerical strings, |
1936 | you can use \verb|lua_type|. | 1976 | you can use \verb|lua_type|. |
1937 | 1977 | ||
@@ -2178,6 +2218,22 @@ You can load a Lua chunk with | |||
2178 | const char *chunkname); | 2218 | const char *chunkname); |
2179 | \end{verbatim} | 2219 | \end{verbatim} |
2180 | \DefAPI{Chunkreader}\DefAPI{lua_load} | 2220 | \DefAPI{Chunkreader}\DefAPI{lua_load} |
2221 | The return values of \verb|lua_load| are: | ||
2222 | \begin{itemize} | ||
2223 | \item 0 --- no errors; | ||
2224 | \item \IndexAPI{LUA_ERRSYNTAX} --- | ||
2225 | syntax error during pre-compilation. | ||
2226 | \item \IndexAPI{LUA_ERRMEM} --- | ||
2227 | memory allocation error. | ||
2228 | \end{itemize} | ||
2229 | If there are no errors, | ||
2230 | \verb|lua_load| pushes the compiled chunk as a Lua | ||
2231 | function on top of the stack. | ||
2232 | Otherwise, it pushes an error message. | ||
2233 | |||
2234 | \verb|lua_load| automatically detects whether the chunk is text or binary, | ||
2235 | and loads it accordingly (see program \IndexVerb{luac}). | ||
2236 | |||
2181 | \verb|lua_load| uses the \emph{reader} to read the chunk. | 2237 | \verb|lua_load| uses the \emph{reader} to read the chunk. |
2182 | Everytime it needs another piece of the chunk, | 2238 | Everytime it needs another piece of the chunk, |
2183 | it calls the reader, | 2239 | it calls the reader, |
@@ -2192,21 +2248,6 @@ In the current implementation, | |||
2192 | the reader function cannot call any Lua function; | 2248 | the reader function cannot call any Lua function; |
2193 | to ensure that, it always receives \verb|NULL| as the Lua state. | 2249 | to ensure that, it always receives \verb|NULL| as the Lua state. |
2194 | 2250 | ||
2195 | \verb|lua_load| automatically detects whether the chunk is text or binary, | ||
2196 | and loads it accordingly (see program \IndexVerb{luac}). | ||
2197 | |||
2198 | The return values of \verb|lua_load| are: | ||
2199 | \begin{itemize} | ||
2200 | \item 0 --- no errors; | ||
2201 | \item \IndexAPI{LUA_ERRSYNTAX} --- | ||
2202 | syntax error during pre-compilation. | ||
2203 | \item \IndexAPI{LUA_ERRMEM} --- | ||
2204 | memory allocation error. | ||
2205 | \end{itemize} | ||
2206 | If there are no errors, | ||
2207 | the compiled chunk is pushed as a Lua function on top of the stack. | ||
2208 | Otherwise, an error message is pushed. | ||
2209 | |||
2210 | The \emph{chunkname} is used for error messages | 2251 | The \emph{chunkname} is used for error messages |
2211 | and debug information \see{debugI}. | 2252 | and debug information \see{debugI}. |
2212 | 2253 | ||
@@ -2446,20 +2487,19 @@ error while running the error handler function. | |||
2446 | Some special Lua functions have their own C~interfaces. | 2487 | Some special Lua functions have their own C~interfaces. |
2447 | The host program can generate a Lua error calling the function | 2488 | The host program can generate a Lua error calling the function |
2448 | \begin{verbatim} | 2489 | \begin{verbatim} |
2449 | void lua_error (lua_State *L, const char *message); | 2490 | void lua_error (lua_State *L); |
2450 | \end{verbatim} | 2491 | \end{verbatim} |
2451 | \DefAPI{lua_error} | 2492 | \DefAPI{lua_error} |
2493 | The error message (which actually can be any type of object) | ||
2494 | is popped from the stack. | ||
2452 | This function never returns. | 2495 | This function never returns. |
2453 | If \verb|lua_error| is called from a C~function that has been called from Lua, | 2496 | If \verb|lua_error| is called from a C~function that |
2497 | has been called from Lua, | ||
2454 | then the corresponding Lua execution terminates, | 2498 | then the corresponding Lua execution terminates, |
2455 | as if an error had occurred inside Lua code. | 2499 | as if an error had occurred inside Lua code. |
2456 | Otherwise, the whole host program terminates with a call to | 2500 | Otherwise, the whole host program terminates with a call to |
2457 | \verb|exit(EXIT_FAILURE)|. | 2501 | \verb|exit(EXIT_FAILURE)|. |
2458 | Before terminating execution, | 2502 | %% TODO: at_panic |
2459 | the \verb|message| is passed to the error handler function, | ||
2460 | \verb|_ERRORMESSAGE| \see{error}. | ||
2461 | If \verb|message| is \verb|NULL|, | ||
2462 | then \verb|_ERRORMESSAGE| is not called. | ||
2463 | 2503 | ||
2464 | The function | 2504 | The function |
2465 | \begin{verbatim} | 2505 | \begin{verbatim} |
@@ -2476,6 +2516,7 @@ Concatenation is done following the usual semantics of Lua | |||
2476 | 2516 | ||
2477 | 2517 | ||
2478 | \subsection{Defining C Functions} \label{LuacallC} | 2518 | \subsection{Defining C Functions} \label{LuacallC} |
2519 | |||
2479 | Lua can be extended with functions written in~C. | 2520 | Lua can be extended with functions written in~C. |
2480 | These functions must be of type \verb|lua_CFunction|, | 2521 | These functions must be of type \verb|lua_CFunction|, |
2481 | which is defined as | 2522 | which is defined as |
@@ -2489,8 +2530,10 @@ the number of values it has returned to Lua. | |||
2489 | In order to communicate properly with Lua, | 2530 | In order to communicate properly with Lua, |
2490 | a C~function must follow the following protocol, | 2531 | a C~function must follow the following protocol, |
2491 | which defines the way parameters and results are passed: | 2532 | which defines the way parameters and results are passed: |
2492 | A C~function receives its arguments from Lua in the stack, | 2533 | A C~function receives its arguments from Lua in its stack, |
2493 | in direct order (the first argument is pushed first). | 2534 | in direct order (the first argument is pushed first). |
2535 | So, when the function starts, | ||
2536 | its first argument (if any) is at index 1. | ||
2494 | To return values to Lua, a C~function just pushes them onto the stack, | 2537 | To return values to Lua, a C~function just pushes them onto the stack, |
2495 | in direct order (the first result is pushed first), | 2538 | in direct order (the first result is pushed first), |
2496 | and returns the number of results. | 2539 | and returns the number of results. |
@@ -2617,7 +2660,7 @@ The structure \verb|lua_Debug| is used to carry different pieces of | |||
2617 | information about an active function: | 2660 | information about an active function: |
2618 | \begin{verbatim} | 2661 | \begin{verbatim} |
2619 | typedef struct lua_Debug { | 2662 | typedef struct lua_Debug { |
2620 | lua_Hookevent event; | 2663 | int event; |
2621 | const char *name; /* (n) */ | 2664 | const char *name; /* (n) */ |
2622 | const char *namewhat; /* (n) `global', `local', `field', `method' */ | 2665 | const char *namewhat; /* (n) `global', `local', `field', `method' */ |
2623 | const char *what; /* (S) `Lua' function, `C' function, Lua `main' */ | 2666 | const char *what; /* (S) `Lua' function, `C' function, Lua `main' */ |
@@ -2768,11 +2811,9 @@ a \emph{call} event, when Lua calls a function; | |||
2768 | a \emph{return} event, when Lua returns from a function; | 2811 | a \emph{return} event, when Lua returns from a function; |
2769 | a \emph{line} event, when Lua starts executing a new line of code; | 2812 | a \emph{line} event, when Lua starts executing a new line of code; |
2770 | and a \emph{count} event, that happens every ``count'' instructions. | 2813 | and a \emph{count} event, that happens every ``count'' instructions. |
2771 | Lua identifies them with the following enumeration: | 2814 | Lua identifies them with the following constants: |
2772 | \begin{verbatim} | 2815 | \DefAPI{LUA_HOOKCALL}, \DefAPI{LUA_HOOKRET}, |
2773 | typedef enum lua_Hookevent { | 2816 | \DefAPI{LUA_HOOKLINE}, and \DefAPI{LUA_HOOKCOUNT}. |
2774 | LUA_HOOKCALL, LUA_HOOKRET, LUA_HOOKLINE, LUA_HOOKCOUNT | ||
2775 | } lua_Hookevent; | ||
2776 | \end{verbatim} | 2817 | \end{verbatim} |
2777 | 2818 | ||
2778 | A hook has type \verb|lua_Hook|, defined as follows: | 2819 | A hook has type \verb|lua_Hook|, defined as follows: |
@@ -2792,7 +2833,22 @@ It is formed by a disjunction of the constants | |||
2792 | \verb|LUA_MASKRET|, | 2833 | \verb|LUA_MASKRET|, |
2793 | \verb|LUA_MASKLINE|, | 2834 | \verb|LUA_MASKLINE|, |
2794 | plus the macro \verb|LUA_MASKCOUNT(count)|. | 2835 | plus the macro \verb|LUA_MASKCOUNT(count)|. |
2795 | %TODO explicar melhor... | 2836 | For each event, the hook is called as explained below: |
2837 | \begin{description} | ||
2838 | \item{call hook} called when the interpreter calls a function. | ||
2839 | The hook is called just after Lua ``enters'' the new function. | ||
2840 | \item{return hook} called when the interpreter returns from a function. | ||
2841 | The hook is called just before Lua ``leaves'' the function. | ||
2842 | \item{line hook} called when the interpreter is about to | ||
2843 | start the execution of a new line of code, | ||
2844 | or when it jumps back (even for the same line). | ||
2845 | (For obvious reasons, this event does not happens while Lua is executing | ||
2846 | a C function.) | ||
2847 | \item{count hook} called after the interpreter executes every | ||
2848 | \verb|count| instructions. | ||
2849 | (For obvious reasons, this event does not happens while Lua is executing | ||
2850 | a C function.) | ||
2851 | \end{description} | ||
2796 | 2852 | ||
2797 | A hook is disabled with the mask zero. | 2853 | A hook is disabled with the mask zero. |
2798 | 2854 | ||
@@ -2811,7 +2867,7 @@ For the value of any other field, the hook must call \verb|lua_getinfo|. | |||
2811 | 2867 | ||
2812 | While Lua is running a hook, it disables other calls to hooks. | 2868 | While Lua is running a hook, it disables other calls to hooks. |
2813 | Therefore, if a hook calls Lua to execute a function or a chunk, | 2869 | Therefore, if a hook calls Lua to execute a function or a chunk, |
2814 | this execution ocurrs without any calls to hooks. | 2870 | that execution ocurrs without any calls to hooks. |
2815 | 2871 | ||
2816 | 2872 | ||
2817 | %------------------------------------------------------------------------------ | 2873 | %------------------------------------------------------------------------------ |
@@ -2844,16 +2900,19 @@ or as methods of its objects. | |||
2844 | 2900 | ||
2845 | To have access to these libraries, | 2901 | To have access to these libraries, |
2846 | the C~host program must call the functions | 2902 | the C~host program must call the functions |
2847 | \verb|lua_baselibopen|, | 2903 | \verb|lua_baselibopen| (for the basic library), |
2848 | \verb|lua_strlibopen|, | 2904 | \verb|lua_strlibopen| (for the string library), |
2849 | \verb|lua_tablibopen|, | 2905 | \verb|lua_tablibopen| (for the table library), |
2850 | \verb|lua_mathlibopen|, | 2906 | \verb|lua_mathlibopen| (for the mathematical library), |
2851 | and \verb|lua_iolibopen|, which are declared in \verb|lualib.h|. | 2907 | \verb|lua_iolibopen| (for the I/O and the Operating System libraries), |
2908 | and \verb|lua_dblibopen| (for the debug library), | ||
2909 | which are declared in \verb|lualib.h|. | ||
2852 | \DefAPI{lua_baselibopen} | 2910 | \DefAPI{lua_baselibopen} |
2853 | \DefAPI{lua_strlibopen} | 2911 | \DefAPI{lua_strlibopen} |
2854 | \DefAPI{lua_tablibopen} | 2912 | \DefAPI{lua_tablibopen} |
2855 | \DefAPI{lua_mathlibopen} | 2913 | \DefAPI{lua_mathlibopen} |
2856 | \DefAPI{lua_iolibopen} | 2914 | \DefAPI{lua_iolibopen} |
2915 | \DefAPI{lua_dblibopen} | ||
2857 | 2916 | ||
2858 | 2917 | ||
2859 | \subsection{Basic Functions} \label{predefined} | 2918 | \subsection{Basic Functions} \label{predefined} |
@@ -2863,7 +2922,7 @@ If you do not include this library in your application, | |||
2863 | you should check carefully whether you need to provide some alternative | 2922 | you should check carefully whether you need to provide some alternative |
2864 | implementation for some facilities. | 2923 | implementation for some facilities. |
2865 | 2924 | ||
2866 | The basic library also defines a global variable \IndexAPI{_VERSION} | 2925 | The basic library also defines a global variable \IndexLIB{_VERSION} |
2867 | with a string containing the current interpreter version. | 2926 | with a string containing the current interpreter version. |
2868 | The current content of this string is {\tt "Lua \Version"}. | 2927 | The current content of this string is {\tt "Lua \Version"}. |
2869 | 2928 | ||
@@ -2897,11 +2956,19 @@ When called without arguments, | |||
2897 | \verb|dofile| executes the contents of the standard input (\verb|stdin|). | 2956 | \verb|dofile| executes the contents of the standard input (\verb|stdin|). |
2898 | Returns any value returned by the chunk. | 2957 | Returns any value returned by the chunk. |
2899 | 2958 | ||
2900 | \subsubsection*{\ff \T{error ([message])}}\DefLIB{error}\label{pdf-error} | 2959 | \subsubsection*{\ff \T{error (message [, level])}} |
2960 | DefLIB{error}\label{pdf-error} | ||
2901 | Terminates the last protected function called, | 2961 | Terminates the last protected function called, |
2902 | and returns \verb|message| as the error message. | 2962 | and returns \verb|message| as the error message. |
2903 | Function \verb|error| never returns. | 2963 | Function \verb|error| never returns. |
2904 | 2964 | ||
2965 | The \verb|level| argument affects to where the error | ||
2966 | message points the error. | ||
2967 | With level 1 (the default), the error position is where the | ||
2968 | \verb|error| function was called. | ||
2969 | Level 2 points the error to where the function | ||
2970 | that called \verb|error| was called; and so on. | ||
2971 | |||
2905 | \subsubsection*{\ff \T{getglobals (function)}}\DefLIB{getglobals} | 2972 | \subsubsection*{\ff \T{getglobals (function)}}\DefLIB{getglobals} |
2906 | Returns the current table of globals in use by the function. | 2973 | Returns the current table of globals in use by the function. |
2907 | \verb|function| can be a Lua function or a number, | 2974 | \verb|function| can be a Lua function or a number, |
@@ -2917,13 +2984,6 @@ The default for \verb|function| is 1. | |||
2917 | Returns the metatable of the given object. | 2984 | Returns the metatable of the given object. |
2918 | If the object does not have a metatable, returns \nil. | 2985 | If the object does not have a metatable, returns \nil. |
2919 | 2986 | ||
2920 | \subsubsection*{\ff \T{getmode (table)}}\DefLIB{getmode} | ||
2921 | |||
2922 | Returns the weak mode of a table, as a string. | ||
2923 | Valid values for this string are \verb|""| for regular (non-weak) tables, | ||
2924 | \verb|"k"| for weak keys, \verb|"v"| for weak values, | ||
2925 | and \verb|"kv"| for both. | ||
2926 | |||
2927 | \subsubsection*{\ff \T{gcinfo ()}}\DefLIB{gcinfo} | 2987 | \subsubsection*{\ff \T{gcinfo ()}}\DefLIB{gcinfo} |
2928 | Returns the number of Kbytes of dynamic memory Lua is using, | 2988 | Returns the number of Kbytes of dynamic memory Lua is using, |
2929 | and (as a second result) the | 2989 | and (as a second result) the |
@@ -2931,7 +2991,7 @@ current garbage collector threshold (also in Kbytes). | |||
2931 | 2991 | ||
2932 | \subsubsection*{\ff \T{ipairs (t)}}\DefLIB{ipairs} | 2992 | \subsubsection*{\ff \T{ipairs (t)}}\DefLIB{ipairs} |
2933 | 2993 | ||
2934 | Returns the table \verb|t| and a generator function | 2994 | Returns a generator function and the table \verb|t|, |
2935 | so that the construction | 2995 | so that the construction |
2936 | \begin{verbatim} | 2996 | \begin{verbatim} |
2937 | for i,v in ipairs(t) do ... end | 2997 | for i,v in ipairs(t) do ... end |
@@ -2988,7 +3048,7 @@ the table during the traversal. | |||
2988 | 3048 | ||
2989 | \subsubsection*{\ff \T{pairs (t)}}\DefLIB{pairs} | 3049 | \subsubsection*{\ff \T{pairs (t)}}\DefLIB{pairs} |
2990 | 3050 | ||
2991 | Returns the table \verb|t| and the function \verb|next|, | 3051 | Returns the function \verb|next| and the table \verb|t|, |
2992 | so that the construction | 3052 | so that the construction |
2993 | \begin{verbatim} | 3053 | \begin{verbatim} |
2994 | for k,v in pairs(t) do ... end | 3054 | for k,v in pairs(t) do ... end |
@@ -2999,7 +3059,11 @@ will iterate over all pairs of key--value of table \verb|t|. | |||
2999 | \label{pdf-pcall} | 3059 | \label{pdf-pcall} |
3000 | Calls function \verb|func| with | 3060 | Calls function \verb|func| with |
3001 | the given arguments in protected mode. | 3061 | the given arguments in protected mode. |
3002 | Its first result is a boolean, true if the call succeeds without errors. | 3062 | That means that any error inside \verb|func| is not propagated; |
3063 | instead, \verb|pcall| catches the error, | ||
3064 | returning a status code. | ||
3065 | Its first result is the status code (a boolean), | ||
3066 | true if the call succeeds without errors. | ||
3003 | In such case, \verb|pcall| also returns all results from the call, | 3067 | In such case, \verb|pcall| also returns all results from the call, |
3004 | after this first result. | 3068 | after this first result. |
3005 | In case of any error, \verb|pcall| returns false plus the error message. | 3069 | In case of any error, \verb|pcall| returns false plus the error message. |
@@ -3015,13 +3079,13 @@ For formatted output, see \verb|format| \see{format}. | |||
3015 | 3079 | ||
3016 | \subsubsection*{\ff \T{rawget (table, index)}}\DefLIB{rawget} | 3080 | \subsubsection*{\ff \T{rawget (table, index)}}\DefLIB{rawget} |
3017 | Gets the real value of \verb|table[index]|, | 3081 | Gets the real value of \verb|table[index]|, |
3018 | without invoking any tag method. | 3082 | without invoking any metamethod. |
3019 | \verb|table| must be a table; | 3083 | \verb|table| must be a table; |
3020 | \verb|index| is any value different from \nil. | 3084 | \verb|index| is any value different from \nil. |
3021 | 3085 | ||
3022 | \subsubsection*{\ff \T{rawset (table, index, value)}}\DefLIB{rawset} | 3086 | \subsubsection*{\ff \T{rawset (table, index, value)}}\DefLIB{rawset} |
3023 | Sets the real value of \verb|table[index]| to \verb|value|, | 3087 | Sets the real value of \verb|table[index]| to \verb|value|, |
3024 | without invoking any tag method. | 3088 | without invoking any metamethod. |
3025 | \verb|table| must be a table; | 3089 | \verb|table| must be a table; |
3026 | \verb|index| is any value different from \nil; | 3090 | \verb|index| is any value different from \nil; |
3027 | and \verb|value| is any Lua value. | 3091 | and \verb|value| is any Lua value. |
@@ -3077,16 +3141,6 @@ Sets the metatable for the given table. | |||
3077 | (You cannot change the metatable of a userdata from Lua.) | 3141 | (You cannot change the metatable of a userdata from Lua.) |
3078 | If \verb|metatable| is \nil, removes the metatable of the given table. | 3142 | If \verb|metatable| is \nil, removes the metatable of the given table. |
3079 | 3143 | ||
3080 | \subsubsection*{\ff \T{setmode (table, mode)}}\DefLIB{setmode} | ||
3081 | |||
3082 | Set the weak mode of a table. | ||
3083 | The new mode is described by the \verb|mode| string. | ||
3084 | Valid values for this string are \verb|""| for regular (non-weak) tables, | ||
3085 | \verb|"k"| for weak keys, \verb|"v"| for weak values, | ||
3086 | and \verb|"kv"| for both. | ||
3087 | |||
3088 | This function returns its first argument (\verb|table|). | ||
3089 | |||
3090 | \subsubsection*{\ff \T{tonumber (e [, base])}}\DefLIB{tonumber} | 3144 | \subsubsection*{\ff \T{tonumber (e [, base])}}\DefLIB{tonumber} |
3091 | Tries to convert its argument to a number. | 3145 | Tries to convert its argument to a number. |
3092 | If the argument is already a number or a string convertible | 3146 | If the argument is already a number or a string convertible |
@@ -3249,7 +3303,31 @@ String values to be formatted with | |||
3249 | 3303 | ||
3250 | \subsubsection*{\ff \T{string.gfind (s, pat)}} | 3304 | \subsubsection*{\ff \T{string.gfind (s, pat)}} |
3251 | 3305 | ||
3252 | % TODO | 3306 | Returns a generator function that, |
3307 | each time it is called, | ||
3308 | returns the next captures from pattern \verb|pat| over string \verb|s|. | ||
3309 | |||
3310 | If \verb|pat| specifies no captures, | ||
3311 | then the whole match is produced in each call. | ||
3312 | |||
3313 | As an example, the following loop | ||
3314 | \begin{verbatim} | ||
3315 | s = "hello world from Lua" | ||
3316 | for w in string.gfind(s, "%a+") do | ||
3317 | print(w) | ||
3318 | end | ||
3319 | \end{verbatim} | ||
3320 | will iterate over all the words from string \verb|s|, | ||
3321 | printing each one in a line. | ||
3322 | The next example collects all pairs \verb|key=value| from the | ||
3323 | given string into a table: | ||
3324 | \begin{verbatim} | ||
3325 | t = {} | ||
3326 | s = "from=world, to=Lua" | ||
3327 | for k, v in string.gfind(s, "(%w+)=(%w+)") do | ||
3328 | t[k] = v | ||
3329 | end | ||
3330 | \end{verbatim} | ||
3253 | 3331 | ||
3254 | \subsubsection*{\ff \T{string.gsub (s, pat, repl [, n])}} | 3332 | \subsubsection*{\ff \T{string.gsub (s, pat, repl [, n])}} |
3255 | \DefLIB{string.gsub} | 3333 | \DefLIB{string.gsub} |
@@ -3280,24 +3358,28 @@ For instance, when \verb|n| is 1 only the first occurrence of | |||
3280 | 3358 | ||
3281 | Here are some examples: | 3359 | Here are some examples: |
3282 | \begin{verbatim} | 3360 | \begin{verbatim} |
3283 | x = gsub("hello world", "(%w+)", "%1 %1") | 3361 | x = string.gsub("hello world", "(%w+)", "%1 %1") |
3284 | --> x="hello hello world world" | 3362 | --> x="hello hello world world" |
3285 | 3363 | ||
3286 | x = gsub("hello world", "(%w+)", "%1 %1", 1) | 3364 | x = string.gsub("hello world", "(%w+)", "%1 %1", 1) |
3287 | --> x="hello hello world" | 3365 | --> x="hello hello world" |
3288 | 3366 | ||
3289 | x = gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1") | 3367 | x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1") |
3290 | --> x="world hello Lua from" | 3368 | --> x="world hello Lua from" |
3291 | 3369 | ||
3292 | x = gsub("home = $HOME, user = $USER", "%$(%w+)", getenv) | 3370 | x = string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv) |
3293 | --> x="home = /home/roberto, user = roberto" (for instance) | 3371 | --> x="home = /home/roberto, user = roberto" (for instance) |
3294 | 3372 | ||
3295 | x = gsub("4+5 = $return 4+5$", "%$(.-)%$", dostring) | 3373 | x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s) |
3374 | return loadstring(s)() | ||
3375 | end) | ||
3296 | --> x="4+5 = 9" | 3376 | --> x="4+5 = 9" |
3297 | 3377 | ||
3298 | local t = {name="Lua", version="4.1"} | 3378 | local t = {name="Lua", version="5.0"} |
3299 | x = gsub("$name - $version", "%$(%w+)", function (v) return t[v] end) | 3379 | x = string.gsub("$name - $version", "%$(%w+)", function (v) |
3300 | --> x="Lua - 4.1" | 3380 | return t[v] |
3381 | end) | ||
3382 | --> x="Lua - 5.0" | ||
3301 | \end{verbatim} | 3383 | \end{verbatim} |
3302 | 3384 | ||
3303 | 3385 | ||
@@ -3416,6 +3498,11 @@ stored as the first capture (and therefore has number~1); | |||
3416 | the character matching \verb|.| is captured with number~2, | 3498 | the character matching \verb|.| is captured with number~2, |
3417 | and the part matching \verb|%s*| has number~3. | 3499 | and the part matching \verb|%s*| has number~3. |
3418 | 3500 | ||
3501 | As a special case, the empty capture \verb|()| captures | ||
3502 | the current string position (a number). | ||
3503 | For instance, if we apply the pattern \verb|"()aa()"| on the | ||
3504 | string \verb|"flaaap"|, there will be two captures: 3 and 5. | ||
3505 | |||
3419 | \NOTE | 3506 | \NOTE |
3420 | A pattern cannot contain embedded zeros. Use \verb|%z| instead. | 3507 | A pattern cannot contain embedded zeros. Use \verb|%z| instead. |
3421 | 3508 | ||
@@ -3442,6 +3529,14 @@ with a \nil{} value. | |||
3442 | For more details, see the descriptions of the \verb|table.getn| and | 3529 | For more details, see the descriptions of the \verb|table.getn| and |
3443 | \verb|table.setn| functions. | 3530 | \verb|table.setn| functions. |
3444 | 3531 | ||
3532 | \subsubsection*{\ff \T{table.concat (table [, sep [, i [, j]]])}} | ||
3533 | \DefLIB{table.concat} | ||
3534 | Returns \verb|table[i]..sep..table[i+1] ... sep..table[j]|. | ||
3535 | The default value for \verb|sep| is the empty string, | ||
3536 | the default for \verb|i| is 1, | ||
3537 | and the default for \verb|j| is the size of the table. | ||
3538 | If \verb|i| is greater than \verb|j|, returns the empty string. | ||
3539 | |||
3445 | \subsubsection*{\ff \T{table.foreach (table, func)}}\DefLIB{table.foreach} | 3540 | \subsubsection*{\ff \T{table.foreach (table, func)}}\DefLIB{table.foreach} |
3446 | Executes the given \verb|func| over all elements of \verb|table|. | 3541 | Executes the given \verb|func| over all elements of \verb|table|. |
3447 | For each element, \verb|func| is called with the index and | 3542 | For each element, \verb|func| is called with the index and |
@@ -3560,6 +3655,7 @@ The library provides the following functions: | |||
3560 | plus a variable \IndexLIB{math.pi}. | 3655 | plus a variable \IndexLIB{math.pi}. |
3561 | Most of them | 3656 | Most of them |
3562 | are only interfaces to the homonymous functions in the C~library. | 3657 | are only interfaces to the homonymous functions in the C~library. |
3658 | All trigonometric functions work in radians. | ||
3563 | The functions \verb|math.deg| and \verb|math.rad| convert | 3659 | The functions \verb|math.deg| and \verb|math.rad| convert |
3564 | between radians and degrees. | 3660 | between radians and degrees. |
3565 | 3661 | ||
@@ -3579,6 +3675,9 @@ When called with a number \Math{n}, | |||
3579 | \verb|math.random| returns a pseudo-random integer in the range \Math{[1,n]}. | 3675 | \verb|math.random| returns a pseudo-random integer in the range \Math{[1,n]}. |
3580 | When called with two arguments, \Math{l} and \Math{u}, | 3676 | When called with two arguments, \Math{l} and \Math{u}, |
3581 | \verb|math.random| returns a pseudo-random integer in the range \Math{[l,u]}. | 3677 | \verb|math.random| returns a pseudo-random integer in the range \Math{[l,u]}. |
3678 | The \verb|math.randomseed| function sets a ``seed'' | ||
3679 | for the pseudo-random generator: | ||
3680 | Equal seeds produce equal sequences of numbers. | ||
3582 | 3681 | ||
3583 | 3682 | ||
3584 | \subsection{Input and Output Facilities} \label{libio} | 3683 | \subsection{Input and Output Facilities} \label{libio} |
@@ -3611,12 +3710,12 @@ and some value different from \nil{} on success. | |||
3611 | 3710 | ||
3612 | \subsubsection*{\ff \T{io.close ([handle])}}\DefLIB{io.close} | 3711 | \subsubsection*{\ff \T{io.close ([handle])}}\DefLIB{io.close} |
3613 | 3712 | ||
3614 | Equivalent to \verb|handle:close()|. | 3713 | Equivalent to \verb|file:close()|. |
3615 | Without a \verb|handle|, closes the default output file. | 3714 | Without a \verb|handle|, closes the default output file. |
3616 | 3715 | ||
3617 | \subsubsection*{\ff \T{io.flush ()}}\DefLIB{io.flush} | 3716 | \subsubsection*{\ff \T{io.flush ()}}\DefLIB{io.flush} |
3618 | 3717 | ||
3619 | Equivalent to \verb|fh:flush| over the default output file. | 3718 | Equivalent to \verb|file:flush| over the default output file. |
3620 | 3719 | ||
3621 | \subsubsection*{\ff \T{io.input ([file])}}\DefLIB{io.input} | 3720 | \subsubsection*{\ff \T{io.input ([file])}}\DefLIB{io.input} |
3622 | 3721 | ||
@@ -3631,6 +3730,24 @@ it returns the current default input file. | |||
3631 | In case of errors this function raises the error, | 3730 | In case of errors this function raises the error, |
3632 | instead of returning an error code. | 3731 | instead of returning an error code. |
3633 | 3732 | ||
3733 | \subsubsection*{\ff \T{io.lines ([filename])}}\DefLIB{io.lines} | ||
3734 | |||
3735 | Opens the given file name in read mode, | ||
3736 | and returns a generator function that, | ||
3737 | each time it is called, | ||
3738 | returns a new line from the file. | ||
3739 | Therefore, the construction | ||
3740 | \begin{verbatim} | ||
3741 | for lines in io.lines(filename) do ... end | ||
3742 | \end{verbatim} | ||
3743 | will iterate over all lines of the file. | ||
3744 | When the generator function detects the end of file, | ||
3745 | it returns nil (to finish the loop) and automatically closes the file. | ||
3746 | |||
3747 | The call \verb|io.lines()| (without a file name) is equivalent | ||
3748 | to \verb|io.input():lines()|, that is, it iterates over the | ||
3749 | lines of the default input file. | ||
3750 | |||
3634 | \subsubsection*{\ff \T{io.open (filename, mode)}}\DefLIB{io.open} | 3751 | \subsubsection*{\ff \T{io.open (filename, mode)}}\DefLIB{io.open} |
3635 | 3752 | ||
3636 | This function opens a file, | 3753 | This function opens a file, |
@@ -3658,7 +3775,7 @@ Similar to \verb|io.input|, but operates over the default output file. | |||
3658 | 3775 | ||
3659 | \subsubsection*{\ff \T{io.read (format1, ...)}}\DefLIB{io.read} | 3776 | \subsubsection*{\ff \T{io.read (format1, ...)}}\DefLIB{io.read} |
3660 | 3777 | ||
3661 | Equivalent to \verb|fh:read| over the default input file. | 3778 | Equivalent to \verb|file:read| over the default input file. |
3662 | 3779 | ||
3663 | \subsubsection*{\ff \T{io.tmpfile ()}}\DefLIB{io.tmpfile} | 3780 | \subsubsection*{\ff \T{io.tmpfile ()}}\DefLIB{io.tmpfile} |
3664 | 3781 | ||
@@ -3668,21 +3785,34 @@ and it is automatically removed when the program ends. | |||
3668 | 3785 | ||
3669 | \subsubsection*{\ff \T{io.write (value1, ...)}}\DefLIB{io.write} | 3786 | \subsubsection*{\ff \T{io.write (value1, ...)}}\DefLIB{io.write} |
3670 | 3787 | ||
3671 | Equivalent to \verb|fh:write| over the default output file. | 3788 | Equivalent to \verb|file:write| over the default output file. |
3789 | |||
3672 | 3790 | ||
3673 | 3791 | ||
3792 | \subsubsection*{\ff \T{file:close ()}}\DefLIB{file:close} | ||
3674 | 3793 | ||
3675 | \subsubsection*{\ff \T{fh:close ([handle])}}\DefLIB{fh:close} | 3794 | Closes the file \verb|file|. |
3676 | 3795 | ||
3677 | Closes the file \verb|fh|. | 3796 | \subsubsection*{\ff \T{file:flush ()}}\DefLIB{file:flush} |
3678 | 3797 | ||
3679 | \subsubsection*{\ff \T{fh:flush ()}}\DefLIB{fh:flush} | 3798 | Saves any written data to the file \verb|file|. |
3680 | 3799 | ||
3681 | Saves any written data to the file \verb|fh|. | 3800 | \subsubsection*{\ff \T{file:lines ()}}\DefLIB{file:lines} |
3801 | |||
3802 | Returns a generator function that, | ||
3803 | each time it is called, | ||
3804 | returns a new line from the file. | ||
3805 | Therefore, the construction | ||
3806 | \begin{verbatim} | ||
3807 | for lines in file:lines() do ... end | ||
3808 | \end{verbatim} | ||
3809 | will iterate over all lines of the file. | ||
3810 | (Unlike \verb|io.lines|, this function does not close the file | ||
3811 | when the loop ends.) | ||
3682 | 3812 | ||
3683 | \subsubsection*{\ff \T{fh:read (format1, ...)}}\DefLIB{fh:read} | 3813 | \subsubsection*{\ff \T{file:read (format1, ...)}}\DefLIB{file:read} |
3684 | 3814 | ||
3685 | Reads the file \verb|fh|, | 3815 | Reads the file \verb|file|, |
3686 | according to the given formats, which specify what to read. | 3816 | according to the given formats, which specify what to read. |
3687 | For each format, | 3817 | For each format, |
3688 | the function returns a string (or a number) with the characters read, | 3818 | the function returns a string (or a number) with the characters read, |
@@ -3707,7 +3837,7 @@ it reads nothing and returns an empty string, | |||
3707 | or \nil{} on end of file. | 3837 | or \nil{} on end of file. |
3708 | \end{description} | 3838 | \end{description} |
3709 | 3839 | ||
3710 | \subsubsection*{\ff \T{fh:seek ([whence] [, offset])}}\DefLIB{fh:seek} | 3840 | \subsubsection*{\ff \T{file:seek ([whence] [, offset])}}\DefLIB{file:seek} |
3711 | 3841 | ||
3712 | Sets and gets the file position, | 3842 | Sets and gets the file position, |
3713 | measured in bytes from the beginning of the file, | 3843 | measured in bytes from the beginning of the file, |
@@ -3732,10 +3862,10 @@ beginning of the file (and returns 0); | |||
3732 | and the call \verb|file:seek("end")| sets the position to the | 3862 | and the call \verb|file:seek("end")| sets the position to the |
3733 | end of the file, and returns its size. | 3863 | end of the file, and returns its size. |
3734 | 3864 | ||
3735 | \subsubsection*{\ff \T{fh:write (value1, ...)}}\DefLIB{fh:write} | 3865 | \subsubsection*{\ff \T{file:write (value1, ...)}}\DefLIB{file:write} |
3736 | 3866 | ||
3737 | Writes the value of each of its arguments to | 3867 | Writes the value of each of its arguments to |
3738 | the filehandle \verb|fh|. | 3868 | the filehandle \verb|file|. |
3739 | The arguments must be strings or numbers. | 3869 | The arguments must be strings or numbers. |
3740 | To write other values, | 3870 | To write other values, |
3741 | use \verb|tostring| or \verb|format| before \verb|write|. | 3871 | use \verb|tostring| or \verb|format| before \verb|write|. |
@@ -3929,7 +4059,7 @@ and raises an error when called with a \verb|level| out of range. | |||
3929 | Sets the given function as a hook. | 4059 | Sets the given function as a hook. |
3930 | The string \verb|mask| and the number \verb|count| describe | 4060 | The string \verb|mask| and the number \verb|count| describe |
3931 | when the hook will be called. | 4061 | when the hook will be called. |
3932 | The string mask may have the following characteres, | 4062 | The string mask may have the following characters, |
3933 | with the given meaning: | 4063 | with the given meaning: |
3934 | \begin{description} | 4064 | \begin{description} |
3935 | \item[{\tt "c"}] The hook is called every time Lua calls a function; | 4065 | \item[{\tt "c"}] The hook is called every time Lua calls a function; |
@@ -3973,18 +4103,18 @@ The stand-alone interpreter includes | |||
3973 | all standard libraries plus the reflexive debug interface. | 4103 | all standard libraries plus the reflexive debug interface. |
3974 | Its usage is: | 4104 | Its usage is: |
3975 | \begin{verbatim} | 4105 | \begin{verbatim} |
3976 | lua [options] [prog [args]] | 4106 | lua [options] [script [args]] |
3977 | \end{verbatim} | 4107 | \end{verbatim} |
3978 | The options are: | 4108 | The options are: |
3979 | \begin{description}\leftskip=20pt | 4109 | \begin{description}\leftskip=20pt |
3980 | \item[\T{-} ] executes \verb|stdin| as a file; | 4110 | \item[\T{-} ] executes \verb|stdin| as a file; |
3981 | \item[\T{-e} \rm\emph{stat}] executes string \emph{stat}; | 4111 | \item[\T{-e} \rm\emph{stat}] executes string \emph{stat}; |
3982 | \item[\T{-l} \rm\emph{file}] executes file \emph{file}; | 4112 | \item[\T{-l} \rm\emph{file}] executes file \emph{file}; |
3983 | \item[\T{-i}] enters interactive mode after running \emph{prog}; | 4113 | \item[\T{-i}] enters interactive mode after running \emph{script}; |
3984 | \item[\T{-v}] prints version information; | 4114 | \item[\T{-v}] prints version information; |
3985 | \item[\T{--}] stop handling options. | 4115 | \item[\T{--}] stop handling options. |
3986 | \end{description} | 4116 | \end{description} |
3987 | After handling its options, \verb|lua| runs the given \emph{prog}, | 4117 | After handling its options, \verb|lua| runs the given \emph{script}, |
3988 | passing to it the given \emph{args}. | 4118 | passing to it the given \emph{args}. |
3989 | When called without arguments, | 4119 | When called without arguments, |
3990 | \verb|lua| behaves as \verb|lua -v -i| when \verb|stdin| is a terminal, | 4120 | \verb|lua| behaves as \verb|lua -v -i| when \verb|stdin| is a terminal, |
@@ -3999,20 +4129,20 @@ Otherwise, lua executes the string itself. | |||
3999 | All options are handled in order, except \verb|-i|. | 4129 | All options are handled in order, except \verb|-i|. |
4000 | For instance, an invocation like | 4130 | For instance, an invocation like |
4001 | \begin{verbatim} | 4131 | \begin{verbatim} |
4002 | $ lua -e'a=1' -e 'print(a)' prog.lua | 4132 | $ lua -e'a=1' -e 'print(a)' script.lua |
4003 | \end{verbatim} | 4133 | \end{verbatim} |
4004 | will first set \verb|a| to 1, then print \verb|a|, | 4134 | will first set \verb|a| to 1, then print \verb|a|, |
4005 | and finally run the file \verb|prog.lua|. | 4135 | and finally run the file \verb|script.lua|. |
4006 | (Here, \verb|$| is the shell prompt. Your prompt may be different.) | 4136 | (Here, \verb|$| is the shell prompt. Your prompt may be different.) |
4007 | 4137 | ||
4008 | Before starting to run the program, | 4138 | Before starting to run the script, |
4009 | \verb|lua| collects all arguments in the command line | 4139 | \verb|lua| collects all arguments in the command line |
4010 | in a global table called \verb|arg|. | 4140 | in a global table called \verb|arg|. |
4011 | The program name is stored in index 0, | 4141 | The script name is stored in index 0, |
4012 | the first argument after the program goes to index 1, | 4142 | the first argument after the script name goes to index 1, |
4013 | and so on. | 4143 | and so on. |
4014 | The field \verb|n| gets the number of arguments after the program name. | 4144 | The field \verb|n| gets the number of arguments after the script name. |
4015 | Any arguments before the program name | 4145 | Any arguments before the script name |
4016 | (that is, the interpreter name plus the options) | 4146 | (that is, the interpreter name plus the options) |
4017 | go to negative indices. | 4147 | go to negative indices. |
4018 | For instance, in the call | 4148 | For instance, in the call |
@@ -4083,7 +4213,7 @@ Mark Ian Barlow, | |||
4083 | Nick Trout, | 4213 | Nick Trout, |
4084 | Noemi Rodriguez, | 4214 | Noemi Rodriguez, |
4085 | Norman Ramsey, | 4215 | Norman Ramsey, |
4086 | Philippe Lhost, | 4216 | Philippe Lhoste, |
4087 | Renata Ratton, | 4217 | Renata Ratton, |
4088 | Renato Borges, | 4218 | Renato Borges, |
4089 | Renato Cerqueira, | 4219 | Renato Cerqueira, |