summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--manual.tex85
1 files changed, 72 insertions, 13 deletions
diff --git a/manual.tex b/manual.tex
index 0ae9459b..af5a72aa 100644
--- a/manual.tex
+++ b/manual.tex
@@ -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.
306There are no type definitions in the language. 306There are no type definitions in the language.
307All values carry their own type. 307All values carry their own type.
308 308
309There are seven \Index{basic types} in Lua: 309There 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,
313whose main property is to be different from any other value; 313whose main property is to be different from any other value;
314usually it represents the absence of a useful value. 314usually it represents the absence of a useful value.
@@ -341,6 +341,11 @@ Userdata values cannot be created or modified in Lua,
341only through the C~API. 341only through the C~API.
342This guarantees the integrity of data owned by the host program. 342This guarantees the integrity of data owned by the host program.
343 343
344The type \Def{thread} represents independent threads of execution,
345and it is used to implement coroutines.
346(This is an experimental area; it needs more documentation,
347and is subject to changes in the future.)
348
344The type \emph{table} implements \Index{associative arrays}, 349The type \emph{table} implements \Index{associative arrays},
345that is, \Index{arrays} that can be indexed not only with numbers, 350that is, \Index{arrays} that can be indexed not only with numbers,
346but with any value (except \nil). 351but with any value (except \nil).
@@ -862,9 +867,8 @@ A \rwd{for} statement like
862is equivalent to the code: 867is 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,
1653a coroutine only suspends its execution by explicitly calling 1657a coroutine only suspends its execution by explicitly calling
1654an yield function. 1658an yield function.
1655 1659
1656You create a coroutine with a call to \verb|coroutine.create|. 1660You create a coroutine with a call to \IndexVerb{coroutine.create}.
1657Its sole argument is a function, 1661Its sole argument is a function,
1658which is the main function of the coroutine. 1662which is the main function of the coroutine.
1659The \verb|coroutine.create| only creates a new coroutine and 1663The \verb|coroutine.create| only creates a new coroutine and
1660returns a handle to it (an object of type \emph{thread}). 1664returns a handle to it (an object of type \emph{thread}).
1661It does not start the coroutine execution. 1665It does not start the coroutine execution.
1662 1666
1663When you first call \verb|coroutine.resume|, 1667When you first call \IndexVerb{coroutine.resume},
1664passing as argument the thread returned by \verb|coroutine.create|, 1668passing as argument the thread returned by \verb|coroutine.create|,
1665the coroutine starts its execution, 1669the coroutine starts its execution,
1666at the first line of its main function. 1670at the first line of its main function.
@@ -1678,7 +1682,7 @@ plus any values returned by the coroutine main function.
1678In case of errors, \verb|coroutine.resume| returns \False 1682In case of errors, \verb|coroutine.resume| returns \False
1679plus an error message. 1683plus an error message.
1680 1684
1681A coroutine yields calling \verb|coroutine.yield|. 1685A coroutine yields calling \IndexVerb{coroutine.yield}.
1682When a coroutine yields, 1686When a coroutine yields,
1683the corresponding \verb|coroutine.resume| returns immediately, 1687the corresponding \verb|coroutine.resume| returns immediately,
1684even if the yield happens inside nested function calls 1688even if the yield happens inside nested function calls
@@ -1691,6 +1695,56 @@ it continues its execution from the point where it yielded,
1691with the call to \verb|coroutine.yield| returning any extra 1695with the call to \verb|coroutine.yield| returning any extra
1692arguments passed to \verb|coroutine.resume|. 1696arguments passed to \verb|coroutine.resume|.
1693 1697
1698The \IndexVerb{coroutine.wrap} function creates a coroutine
1699like \verb|coroutine.create|,
1700but instead of returning the coroutine itself,
1701it returns a function that, when called, resumes the coroutine.
1702Any arguments passed to that function
1703go as extra arguments to resume.
1704The function returns all the values returned by resume,
1705but the first one (the boolean error code).
1706Unlike \verb|coroutine.resume|,
1707this function does not catch errors;
1708any error is propagated to the caller.
1709
1710As a complete example,
1711consider the next code:
1712\begin{verbatim}
1713function foo1 (a)
1714 print("foo", a)
1715 return coroutine.yield(2*a)
1716end
1717
1718co = 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"
1725end)
1726
1727a, b = coroutine.resume(co, 1, 10)
1728print("main", a, b)
1729a, b, c = coroutine.resume(co, "r")
1730print("main", a, b, c)
1731a, b, c = coroutine.resume(co, "x", "y")
1732print("main", a, b, c)
1733a, b = coroutine.resume(co, "x", "y")
1734print("main", a, b)
1735\end{verbatim}
1736When you run it, it produces the following output:
1737\begin{verbatim}
1738co-body 1 10
1739foo 2
1740main true 4
1741co-body r
1742main true 11 -9
1743co-body x y
1744main true 10 end
1745main 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.
2814Lua identifies them with the following constants: 2868Lua 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
2819A hook has type \verb|lua_Hook|, defined as follows: 2872A 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
4176In Unix systems, Lua scripts can be made into executable programs 4229In Unix systems, Lua scripts can be made into executable programs
4177by using \verb|chmod +x| and the~\verb|#!| form, 4230by using \verb|chmod +x| and the~\verb|#!| form,
4178as in \verb|#!/usr/local/bin/lua|. 4231as in
4232\begin{verbatim}
4233#!/usr/local/bin/lua
4234\end{verbatim}
4179(Of course, 4235(Of course,
4180the location of the Lua interpreter may be different in your machine. 4236the location of the Lua interpreter may be different in your machine.
4181If \verb|lua| is in your \verb|PATH|, 4237If \verb|lua| is in your \verb|PATH|,
4182then a more portable solution is \verb|#!/usr/bin/env lua|.) 4238then a more portable solution is
4239\begin{verbatim}
4240#!/usr/bin/env lua
4241\end{verbatim}
4183 4242
4184 4243
4185%------------------------------------------------------------------------------ 4244%------------------------------------------------------------------------------