diff options
Diffstat (limited to 'manual.tex')
-rw-r--r-- | manual.tex | 85 |
1 files changed, 72 insertions, 13 deletions
@@ -1,4 +1,4 @@ | |||
1 | % $Id: manual.tex,v 1.63 2002/11/18 14:37:57 roberto Exp $ | 1 | % $Id: manual.tex,v 1.63 2002/11/18 14:39:34 roberto Exp roberto $ |
2 | %{[( | 2 | %{[( |
3 | 3 | ||
4 | \documentclass[11pt,twoside]{article} | 4 | \documentclass[11pt,twoside]{article} |
@@ -134,7 +134,7 @@ Waldemar Celes | |||
134 | \tecgraf\ --- Computer Science Department --- PUC-Rio | 134 | \tecgraf\ --- Computer Science Department --- PUC-Rio |
135 | } | 135 | } |
136 | 136 | ||
137 | %\date{{\small \tt\$Date: 2002/11/18 14:37:57 $ $}} | 137 | %\date{{\small \tt\$Date: 2002/11/18 14:39:34 $ $}} |
138 | 138 | ||
139 | \maketitle | 139 | \maketitle |
140 | 140 | ||
@@ -306,9 +306,9 @@ variables do not have types; only values do. | |||
306 | There are no type definitions in the language. | 306 | There are no type definitions in the language. |
307 | All values carry their own type. | 307 | All values carry their own type. |
308 | 308 | ||
309 | There are seven \Index{basic types} in Lua: | 309 | There are eight \Index{basic types} in Lua: |
310 | \Def{nil}, \Def{boolean}, \Def{number}, | 310 | \Def{nil}, \Def{boolean}, \Def{number}, |
311 | \Def{string}, \Def{function}, \Def{userdata}, and \Def{table}. | 311 | \Def{string}, \Def{function}, \Def{userdata}, \Def{thread}, and \Def{table}. |
312 | \emph{Nil} is the type of the value \nil, | 312 | \emph{Nil} is the type of the value \nil, |
313 | whose main property is to be different from any other value; | 313 | whose main property is to be different from any other value; |
314 | usually it represents the absence of a useful value. | 314 | usually it represents the absence of a useful value. |
@@ -341,6 +341,11 @@ Userdata values cannot be created or modified in Lua, | |||
341 | only through the C~API. | 341 | only through the C~API. |
342 | This guarantees the integrity of data owned by the host program. | 342 | This guarantees the integrity of data owned by the host program. |
343 | 343 | ||
344 | The type \Def{thread} represents independent threads of execution, | ||
345 | and it is used to implement coroutines. | ||
346 | (This is an experimental area; it needs more documentation, | ||
347 | and is subject to changes in the future.) | ||
348 | |||
344 | The type \emph{table} implements \Index{associative arrays}, | 349 | The type \emph{table} implements \Index{associative arrays}, |
345 | that is, \Index{arrays} that can be indexed not only with numbers, | 350 | that is, \Index{arrays} that can be indexed not only with numbers, |
346 | but with any value (except \nil). | 351 | but with any value (except \nil). |
@@ -862,9 +867,8 @@ A \rwd{for} statement like | |||
862 | is equivalent to the code: | 867 | is equivalent to the code: |
863 | \begin{verbatim} | 868 | \begin{verbatim} |
864 | do | 869 | do |
865 | local _f, _s, var_1 = explist | 870 | local _f, _s, var_1, ..., var_n = explist |
866 | while 1 do | 871 | while 1 do |
867 | local var_2, ..., var_n | ||
868 | var_1, ..., var_n = _f(_s, var_1) | 872 | var_1, ..., var_n = _f(_s, var_1) |
869 | if var_1 == nil then break end | 873 | if var_1 == nil then break end |
870 | block | 874 | block |
@@ -1653,14 +1657,14 @@ Unlike ``real'' threads, however, | |||
1653 | a coroutine only suspends its execution by explicitly calling | 1657 | a coroutine only suspends its execution by explicitly calling |
1654 | an yield function. | 1658 | an yield function. |
1655 | 1659 | ||
1656 | You create a coroutine with a call to \verb|coroutine.create|. | 1660 | You create a coroutine with a call to \IndexVerb{coroutine.create}. |
1657 | Its sole argument is a function, | 1661 | Its sole argument is a function, |
1658 | which is the main function of the coroutine. | 1662 | which is the main function of the coroutine. |
1659 | The \verb|coroutine.create| only creates a new coroutine and | 1663 | The \verb|coroutine.create| only creates a new coroutine and |
1660 | returns a handle to it (an object of type \emph{thread}). | 1664 | returns a handle to it (an object of type \emph{thread}). |
1661 | It does not start the coroutine execution. | 1665 | It does not start the coroutine execution. |
1662 | 1666 | ||
1663 | When you first call \verb|coroutine.resume|, | 1667 | When you first call \IndexVerb{coroutine.resume}, |
1664 | passing as argument the thread returned by \verb|coroutine.create|, | 1668 | passing as argument the thread returned by \verb|coroutine.create|, |
1665 | the coroutine starts its execution, | 1669 | the coroutine starts its execution, |
1666 | at the first line of its main function. | 1670 | at the first line of its main function. |
@@ -1678,7 +1682,7 @@ plus any values returned by the coroutine main function. | |||
1678 | In case of errors, \verb|coroutine.resume| returns \False | 1682 | In case of errors, \verb|coroutine.resume| returns \False |
1679 | plus an error message. | 1683 | plus an error message. |
1680 | 1684 | ||
1681 | A coroutine yields calling \verb|coroutine.yield|. | 1685 | A coroutine yields calling \IndexVerb{coroutine.yield}. |
1682 | When a coroutine yields, | 1686 | When a coroutine yields, |
1683 | the corresponding \verb|coroutine.resume| returns immediately, | 1687 | the corresponding \verb|coroutine.resume| returns immediately, |
1684 | even if the yield happens inside nested function calls | 1688 | even if the yield happens inside nested function calls |
@@ -1691,6 +1695,56 @@ it continues its execution from the point where it yielded, | |||
1691 | with the call to \verb|coroutine.yield| returning any extra | 1695 | with the call to \verb|coroutine.yield| returning any extra |
1692 | arguments passed to \verb|coroutine.resume|. | 1696 | arguments passed to \verb|coroutine.resume|. |
1693 | 1697 | ||
1698 | The \IndexVerb{coroutine.wrap} function creates a coroutine | ||
1699 | like \verb|coroutine.create|, | ||
1700 | but instead of returning the coroutine itself, | ||
1701 | it returns a function that, when called, resumes the coroutine. | ||
1702 | Any arguments passed to that function | ||
1703 | go as extra arguments to resume. | ||
1704 | The function returns all the values returned by resume, | ||
1705 | but the first one (the boolean error code). | ||
1706 | Unlike \verb|coroutine.resume|, | ||
1707 | this function does not catch errors; | ||
1708 | any error is propagated to the caller. | ||
1709 | |||
1710 | As a complete example, | ||
1711 | consider the next code: | ||
1712 | \begin{verbatim} | ||
1713 | function foo1 (a) | ||
1714 | print("foo", a) | ||
1715 | return coroutine.yield(2*a) | ||
1716 | end | ||
1717 | |||
1718 | co = coroutine.create(function (a,b) | ||
1719 | print("co-body", a, b) | ||
1720 | local r = foo1(a+1) | ||
1721 | print("co-body", r) | ||
1722 | local r, s = coroutine.yield(a+b, a-b) | ||
1723 | print("co-body", r, s) | ||
1724 | return b, "end" | ||
1725 | end) | ||
1726 | |||
1727 | a, b = coroutine.resume(co, 1, 10) | ||
1728 | print("main", a, b) | ||
1729 | a, b, c = coroutine.resume(co, "r") | ||
1730 | print("main", a, b, c) | ||
1731 | a, b, c = coroutine.resume(co, "x", "y") | ||
1732 | print("main", a, b, c) | ||
1733 | a, b = coroutine.resume(co, "x", "y") | ||
1734 | print("main", a, b) | ||
1735 | \end{verbatim} | ||
1736 | When you run it, it produces the following output: | ||
1737 | \begin{verbatim} | ||
1738 | co-body 1 10 | ||
1739 | foo 2 | ||
1740 | main true 4 | ||
1741 | co-body r | ||
1742 | main true 11 -9 | ||
1743 | co-body x y | ||
1744 | main true 10 end | ||
1745 | main false cannot resume dead coroutine | ||
1746 | \end{verbatim} | ||
1747 | |||
1694 | 1748 | ||
1695 | 1749 | ||
1696 | %------------------------------------------------------------------------------ | 1750 | %------------------------------------------------------------------------------ |
@@ -2814,7 +2868,6 @@ and a \emph{count} event, that happens every ``count'' instructions. | |||
2814 | Lua identifies them with the following constants: | 2868 | Lua identifies them with the following constants: |
2815 | \DefAPI{LUA_HOOKCALL}, \DefAPI{LUA_HOOKRET}, | 2869 | \DefAPI{LUA_HOOKCALL}, \DefAPI{LUA_HOOKRET}, |
2816 | \DefAPI{LUA_HOOKLINE}, and \DefAPI{LUA_HOOKCOUNT}. | 2870 | \DefAPI{LUA_HOOKLINE}, and \DefAPI{LUA_HOOKCOUNT}. |
2817 | \end{verbatim} | ||
2818 | 2871 | ||
2819 | A hook has type \verb|lua_Hook|, defined as follows: | 2872 | A hook has type \verb|lua_Hook|, defined as follows: |
2820 | \begin{verbatim} | 2873 | \begin{verbatim} |
@@ -4109,7 +4162,7 @@ The options are: | |||
4109 | \begin{description}\leftskip=20pt | 4162 | \begin{description}\leftskip=20pt |
4110 | \item[\T{-} ] executes \verb|stdin| as a file; | 4163 | \item[\T{-} ] executes \verb|stdin| as a file; |
4111 | \item[\T{-e} \rm\emph{stat}] executes string \emph{stat}; | 4164 | \item[\T{-e} \rm\emph{stat}] executes string \emph{stat}; |
4112 | \item[\T{-l} \rm\emph{file}] executes file \emph{file}; | 4165 | \item[\T{-l} \rm\emph{file}] ``requires'' \emph{file}; |
4113 | \item[\T{-i}] enters interactive mode after running \emph{script}; | 4166 | \item[\T{-i}] enters interactive mode after running \emph{script}; |
4114 | \item[\T{-v}] prints version information; | 4167 | \item[\T{-v}] prints version information; |
4115 | \item[\T{--}] stop handling options. | 4168 | \item[\T{--}] stop handling options. |
@@ -4175,11 +4228,17 @@ the program would end just after the assignment to \verb|_PROMPT|. | |||
4175 | 4228 | ||
4176 | In Unix systems, Lua scripts can be made into executable programs | 4229 | In Unix systems, Lua scripts can be made into executable programs |
4177 | by using \verb|chmod +x| and the~\verb|#!| form, | 4230 | by using \verb|chmod +x| and the~\verb|#!| form, |
4178 | as in \verb|#!/usr/local/bin/lua|. | 4231 | as in |
4232 | \begin{verbatim} | ||
4233 | #!/usr/local/bin/lua | ||
4234 | \end{verbatim} | ||
4179 | (Of course, | 4235 | (Of course, |
4180 | the location of the Lua interpreter may be different in your machine. | 4236 | the location of the Lua interpreter may be different in your machine. |
4181 | If \verb|lua| is in your \verb|PATH|, | 4237 | If \verb|lua| is in your \verb|PATH|, |
4182 | then a more portable solution is \verb|#!/usr/bin/env lua|.) | 4238 | then a more portable solution is |
4239 | \begin{verbatim} | ||
4240 | #!/usr/bin/env lua | ||
4241 | \end{verbatim} | ||
4183 | 4242 | ||
4184 | 4243 | ||
4185 | %------------------------------------------------------------------------------ | 4244 | %------------------------------------------------------------------------------ |