diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-08-09 18:03:19 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-08-09 18:03:19 -0300 |
| commit | 3b058177af1f940b8f6128821b3f4a3f3dc79ed0 (patch) | |
| tree | 1bfc78ee1f090c6ab501e974caec143282c04915 | |
| parent | 586e510577aac83ab1a94e6da9b7760389d3a7f1 (diff) | |
| download | lua-3b058177af1f940b8f6128821b3f4a3f3dc79ed0.tar.gz lua-3b058177af1f940b8f6128821b3f4a3f3dc79ed0.tar.bz2 lua-3b058177af1f940b8f6128821b3f4a3f3dc79ed0.zip | |
towards 5.0 alpha...
| -rw-r--r-- | manual.tex | 315 |
1 files changed, 179 insertions, 136 deletions
| @@ -1,4 +1,4 @@ | |||
| 1 | % $Id: manual.tex,v 1.56 2002/06/06 12:49:28 roberto Exp roberto $ | 1 | % $Id: manual.tex,v 1.57 2002/08/06 19:10:44 roberto Exp roberto $ |
| 2 | 2 | ||
| 3 | \documentclass[11pt,twoside,draft]{article} | 3 | \documentclass[11pt,twoside,draft]{article} |
| 4 | \usepackage{fullpage} | 4 | \usepackage{fullpage} |
| @@ -133,7 +133,7 @@ Waldemar Celes | |||
| 133 | \tecgraf\ --- Computer Science Department --- PUC-Rio | 133 | \tecgraf\ --- Computer Science Department --- PUC-Rio |
| 134 | } | 134 | } |
| 135 | 135 | ||
| 136 | %\date{{\small \tt\$Date: 2002/06/06 12:49:28 $ $}} | 136 | %\date{{\small \tt\$Date: 2002/08/06 19:10:44 $ $}} |
| 137 | 137 | ||
| 138 | \maketitle | 138 | \maketitle |
| 139 | 139 | ||
| @@ -251,6 +251,8 @@ The evolution of an extension language: a history of Lua, | |||
| 251 | \emph{Proceedings of V Brazilian Symposium on Programming Languages} (2001) B-14--B-28. | 251 | \emph{Proceedings of V Brazilian Symposium on Programming Languages} (2001) B-14--B-28. |
| 252 | \end{itemize} | 252 | \end{itemize} |
| 253 | 253 | ||
| 254 | Lua means ``moon'' in Portuguese. | ||
| 255 | |||
| 254 | %------------------------------------------------------------------------------ | 256 | %------------------------------------------------------------------------------ |
| 255 | \section{Lua Concepts}\label{concepts} | 257 | \section{Lua Concepts}\label{concepts} |
| 256 | 258 | ||
| @@ -293,6 +295,10 @@ Lua automatically detects the file type and acts accordingly. | |||
| 293 | \index{pre-compilation} | 295 | \index{pre-compilation} |
| 294 | 296 | ||
| 295 | 297 | ||
| 298 | \subsection{Table of Globals} \label{global-table} | ||
| 299 | |||
| 300 | ???? | ||
| 301 | |||
| 296 | \subsection{\Index{Values and Types}} \label{TypesSec} | 302 | \subsection{\Index{Values and Types}} \label{TypesSec} |
| 297 | 303 | ||
| 298 | Lua is a \emph{dynamically typed language}. | 304 | Lua is a \emph{dynamically typed language}. |
| @@ -331,7 +337,7 @@ and has no pre-defined operations in Lua, | |||
| 331 | except assignment and identity test. | 337 | except assignment and identity test. |
| 332 | However, by using \emph{metatables}, | 338 | However, by using \emph{metatables}, |
| 333 | the programmer can define operations for userdata values | 339 | the programmer can define operations for userdata values |
| 334 | \see{metatables}. | 340 | \see{metatable}. |
| 335 | Userdata values cannot be created or modified in Lua, | 341 | Userdata values cannot be created or modified in Lua, |
| 336 | only through the C~API. | 342 | only through the C~API. |
| 337 | This guarantees the integrity of data owned by the host program. | 343 | This guarantees the integrity of data owned by the host program. |
| @@ -966,7 +972,7 @@ Tables, userdata, and functions are compared \emph{by reference}, | |||
| 966 | that is, | 972 | that is, |
| 967 | two tables are considered equal only if they are the \emph{same} table. | 973 | two tables are considered equal only if they are the \emph{same} table. |
| 968 | 974 | ||
| 969 | ??eq metamethod?? | 975 | %% TODO eq metamethod |
| 970 | 976 | ||
| 971 | Every time you create a new table (or userdata, or function), | 977 | Every time you create a new table (or userdata, or function), |
| 972 | this new value is different from any previously existing value. | 978 | this new value is different from any previously existing value. |
| @@ -1358,43 +1364,18 @@ while all of them share the same \verb|x|. | |||
| 1358 | 1364 | ||
| 1359 | \subsection{Error Handling} \label{error} | 1365 | \subsection{Error Handling} \label{error} |
| 1360 | 1366 | ||
| 1361 | %% TODO Must be rewritten!!! | ||
| 1362 | |||
| 1363 | Because Lua is an extension language, | 1367 | Because Lua is an extension language, |
| 1364 | all Lua actions start from C~code in the host program | 1368 | all Lua actions start from C~code in the host program |
| 1365 | calling a function from the Lua library. | 1369 | calling a function from the Lua library \see{pcall}. |
| 1366 | Whenever an error occurs during Lua compilation or execution, | 1370 | Whenever an error occurs during Lua compilation or execution, |
| 1367 | the function \verb|_ERRORMESSAGE| is called \DefLIB{_ERRORMESSAGE} | 1371 | control returns to C, |
| 1368 | (provided it is different from \nil), | 1372 | which can take appropriate measures |
| 1369 | and then the corresponding function from the library | 1373 | (such as to print an error message). |
| 1370 | (\verb|lua_dofile|, \verb|lua_dostring|, | ||
| 1371 | \verb|lua_dobuffer|, or \verb|lua_call|) | ||
| 1372 | is terminated, returning an error condition. | ||
| 1373 | |||
| 1374 | Memory allocation errors are an exception to the previous rule. | ||
| 1375 | When memory allocation fails, Lua may not be able to execute the | ||
| 1376 | \verb|_ERRORMESSAGE| function. | ||
| 1377 | So, for this kind of error, Lua does not call | ||
| 1378 | the \verb|_ERRORMESSAGE| function; | ||
| 1379 | instead, the corresponding function from the library | ||
| 1380 | returns immediately with a special error code (\verb|LUA_ERRMEM|). | ||
| 1381 | This and other error codes are defined in \verb|lua.h| | ||
| 1382 | \see{luado}. | ||
| 1383 | |||
| 1384 | The only argument to \verb|_ERRORMESSAGE| is a string | ||
| 1385 | describing the error. | ||
| 1386 | The default definition for | ||
| 1387 | this function calls \verb|_ALERT|, \DefLIB{_ALERT} | ||
| 1388 | which prints the message to \verb|stderr| \see{alert}. | ||
| 1389 | The standard I/O library redefines \verb|_ERRORMESSAGE| | ||
| 1390 | and uses the debug interface \see{debugI} | ||
| 1391 | to print some extra information, | ||
| 1392 | such as a call-stack traceback. | ||
| 1393 | 1374 | ||
| 1394 | Lua code can explicitly generate an error by calling the | 1375 | Lua code can explicitly generate an error by calling the |
| 1395 | function \verb|error| \see{pdf-error}. | 1376 | function \verb|error| \see{pdf-error}. |
| 1396 | Lua code can ``catch'' an error using the function | 1377 | If you need to catch errors in Lua, |
| 1397 | \verb|call| \see{pdf-call}. | 1378 | you can use the \verb|pcall| function \see{pdf-pcall}. |
| 1398 | 1379 | ||
| 1399 | 1380 | ||
| 1400 | \subsection{Metatables} \label{metatable} | 1381 | \subsection{Metatables} \label{metatable} |
| @@ -2161,14 +2142,32 @@ when applied on a non-userdata value, it returns \verb|NULL|. | |||
| 2161 | 2142 | ||
| 2162 | When Lua collects a full userdata, | 2143 | When Lua collects a full userdata, |
| 2163 | it calls its \verb|gc| metamethod, if any, | 2144 | it calls its \verb|gc| metamethod, if any, |
| 2164 | and then it automatically frees its corresponding memory. | 2145 | and then it frees its corresponding memory. |
| 2165 | 2146 | ||
| 2166 | 2147 | ||
| 2167 | \subsection{Metatables} | 2148 | \subsection{Metatables} |
| 2168 | 2149 | ||
| 2169 | %% TODO | 2150 | The following functions allow you do manipulate the metatables |
| 2151 | of an object: | ||
| 2152 | \begin{verbatim} | ||
| 2153 | int lua_getmetatable (lua_State *L, int objindex); | ||
| 2154 | int lua_setmetatable (lua_State *L, int objindex); | ||
| 2155 | \end{verbatim} | ||
| 2156 | \DefAPI{lua_getmetatable}\DefAPI{lua_setmetatable} | ||
| 2157 | Both get at \verb|objindex| a valid index for an object. | ||
| 2158 | \verb|lua_getmetatable| pushes on the stack the metatable of that object; | ||
| 2159 | \verb|lua_setmetatable| sets the table on the top of the stack as the | ||
| 2160 | new metatable for that object (and pops the table). | ||
| 2161 | |||
| 2162 | If the object does not have a metatable, | ||
| 2163 | \verb|lua_getmetatable| returns 0, and pushes nothing on the stack. | ||
| 2164 | \verb|lua_setmetatable| returns 0 when it cannot | ||
| 2165 | set the metatable of the given object | ||
| 2166 | (that is, when the object is not a userdata nor a table); | ||
| 2167 | even then it pops the table from the stack. | ||
| 2170 | 2168 | ||
| 2171 | \subsection{Loading Lua Chunks} | 2169 | \subsection{Loading Lua Chunks} |
| 2170 | |||
| 2172 | You can load a Lua chunk with | 2171 | You can load a Lua chunk with |
| 2173 | \begin{verbatim} | 2172 | \begin{verbatim} |
| 2174 | typedef const char * (*lua_Chunkreader) | 2173 | typedef const char * (*lua_Chunkreader) |
| @@ -2183,9 +2182,10 @@ Everytime it needs another piece of the chunk, | |||
| 2183 | it calls the reader, | 2182 | it calls the reader, |
| 2184 | passing along its \verb|data| parameter. | 2183 | passing along its \verb|data| parameter. |
| 2185 | The reader must return a pointer to a block of memory | 2184 | The reader must return a pointer to a block of memory |
| 2186 | with the part of the chunk, | 2185 | with a new part of the chunk, |
| 2187 | and set \verb|size| to the block size. | 2186 | and set \verb|size| to the block size. |
| 2188 | To signal the end of the chunk, the reader must return \verb|NULL|. | 2187 | To signal the end of the chunk, the reader must return \verb|NULL|. |
| 2188 | The reader function may return pieces of any size greater than zero. | ||
| 2189 | 2189 | ||
| 2190 | In the current implementation, | 2190 | In the current implementation, |
| 2191 | the reader function cannot call any Lua function; | 2191 | the reader function cannot call any Lua function; |
| @@ -2214,43 +2214,6 @@ for examples of how to use \verb|lua_load|, | |||
| 2214 | and for some ready-to-use functions to load chunks | 2214 | and for some ready-to-use functions to load chunks |
| 2215 | from files and from strings. | 2215 | from files and from strings. |
| 2216 | 2216 | ||
| 2217 | |||
| 2218 | \subsection{Executing Lua Chunks}\label{luado} | ||
| 2219 | >>>> | ||
| 2220 | A host program can execute Lua chunks written in a file or in a string | ||
| 2221 | by using the following functions: | ||
| 2222 | \begin{verbatim} | ||
| 2223 | int lua_dofile (lua_State *L, const char *filename); | ||
| 2224 | int lua_dostring (lua_State *L, const char *string); | ||
| 2225 | int lua_dobuffer (lua_State *L, const char *buff, | ||
| 2226 | size_t size, const char *name); | ||
| 2227 | \end{verbatim} | ||
| 2228 | \DefAPI{lua_dofile}\DefAPI{lua_dostring}\DefAPI{lua_dobuffer}% | ||
| 2229 | These functions return | ||
| 2230 | 0 in case of success, or one of the following error codes | ||
| 2231 | (defined in \verb|lua.h|) | ||
| 2232 | if they fail: | ||
| 2233 | \begin{itemize} | ||
| 2234 | \item \IndexAPI{LUA_ERRRUN} --- | ||
| 2235 | error while running the chunk. | ||
| 2236 | \item \IndexAPI{LUA_ERRSYNTAX} --- | ||
| 2237 | syntax error during pre-compilation. | ||
| 2238 | \item \IndexAPI{LUA_ERRMEM} --- | ||
| 2239 | memory allocation error. | ||
| 2240 | For such errors, Lua does not call \verb|_ERRORMESSAGE| \see{error}. | ||
| 2241 | \item \IndexAPI{LUA_ERRERR} --- | ||
| 2242 | error while running \verb|_ERRORMESSAGE|. | ||
| 2243 | For such errors, Lua does not call \verb|_ERRORMESSAGE| again, to avoid loops. | ||
| 2244 | \item \IndexAPI{LUA_ERRFILE} --- | ||
| 2245 | error opening the file (only for \verb|lua_dofile|). | ||
| 2246 | In this case, | ||
| 2247 | you may want to | ||
| 2248 | check \verb|errno|, | ||
| 2249 | call \verb|strerror|, | ||
| 2250 | or call \verb|perror| to tell the user what went wrong. | ||
| 2251 | \end{itemize} | ||
| 2252 | |||
| 2253 | |||
| 2254 | \subsection{Manipulating Tables} | 2217 | \subsection{Manipulating Tables} |
| 2255 | 2218 | ||
| 2256 | Tables are created by calling | 2219 | Tables are created by calling |
| @@ -2426,7 +2389,54 @@ to show all the details. | |||
| 2426 | Usually programmers use several macros and auxiliar functions that | 2389 | Usually programmers use several macros and auxiliar functions that |
| 2427 | provide higher level access to Lua.) | 2390 | provide higher level access to Lua.) |
| 2428 | 2391 | ||
| 2429 | %% TODO: pcall | 2392 | |
| 2393 | \subsection{Protected Calls}\label{pcall} | ||
| 2394 | |||
| 2395 | When you call a function with \verb|lua_call|, | ||
| 2396 | any error inside the called function is propagated upwards | ||
| 2397 | (with a \verb|longjmp|). | ||
| 2398 | If you need to handle errors, | ||
| 2399 | then you should use \verb|lua_pcall|: | ||
| 2400 | \begin{verbatim} | ||
| 2401 | int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc); | ||
| 2402 | \end{verbatim} | ||
| 2403 | Both \verb|nargs| and \verb|nresults| have the same meaning as | ||
| 2404 | in \verb|lua_call|. | ||
| 2405 | If there are no errors during the call, | ||
| 2406 | \verb|lua_pcall| behaves exactly like \verb|lua_call|. | ||
| 2407 | Like \verb|lua_call|, | ||
| 2408 | \verb|lua_pcall| always removes the function | ||
| 2409 | and its arguments from the stack. | ||
| 2410 | However, if there is any error, | ||
| 2411 | \verb|lua_pcall| catches it, | ||
| 2412 | pushes a single value at the stack (the error message), | ||
| 2413 | and returns an error code. | ||
| 2414 | |||
| 2415 | If \verb|errfunc| is 0, | ||
| 2416 | then the error message returned is exactly the original error message. | ||
| 2417 | Otherwise, \verb|errfunc| gives the stack index for an | ||
| 2418 | \emph{error handler function}. | ||
| 2419 | (In the current implementation, that index cannot be a pseudo-index.) | ||
| 2420 | In case of runtime errors, | ||
| 2421 | that function will be called with the error message, | ||
| 2422 | and its return value will be the message returned by \verb|lua_pcall|. | ||
| 2423 | |||
| 2424 | Typically, the error handler function is used to add more debug | ||
| 2425 | information to the error message, such as a stack traceback. | ||
| 2426 | Such information cannot be gathered after the return of \verb|lua_pcall|, | ||
| 2427 | since by then the stack has unwound. | ||
| 2428 | |||
| 2429 | The \verb|lua_pcall| function returns 0 in case of success, | ||
| 2430 | or one of the following error codes | ||
| 2431 | (defined in \verb|lua.h|): | ||
| 2432 | \begin{itemize} | ||
| 2433 | \item \IndexAPI{LUA_ERRRUN} --- a runtime error. | ||
| 2434 | \item \IndexAPI{LUA_ERRMEM} --- memory allocation error. | ||
| 2435 | For such errors, Lua does not call the error handler function. | ||
| 2436 | \item \IndexAPI{LUA_ERRERR} --- | ||
| 2437 | error while running the error handler function. | ||
| 2438 | \end{itemize} | ||
| 2439 | |||
| 2430 | 2440 | ||
| 2431 | \medskip | 2441 | \medskip |
| 2432 | 2442 | ||
| @@ -2494,8 +2504,10 @@ of numerical arguments and returns their average and sum: | |||
| 2494 | lua_Number sum = 0; | 2504 | lua_Number sum = 0; |
| 2495 | int i; | 2505 | int i; |
| 2496 | for (i = 1; i <= n; i++) { | 2506 | for (i = 1; i <= n; i++) { |
| 2497 | if (!lua_isnumber(L, i)) | 2507 | if (!lua_isnumber(L, i)) { |
| 2498 | lua_error(L, "incorrect argument to function `average'"); | 2508 | lua_pushstring(L, "incorrect argument to function `average'"); |
| 2509 | lua_error(L); | ||
| 2510 | } | ||
| 2499 | sum += lua_tonumber(L, i); | 2511 | sum += lua_tonumber(L, i); |
| 2500 | } | 2512 | } |
| 2501 | lua_pushnumber(L, sum/n); /* first result */ | 2513 | lua_pushnumber(L, sum/n); /* first result */ |
| @@ -2565,8 +2577,8 @@ outside the life span of a C~function. | |||
| 2565 | This table is always located at pseudo-index | 2577 | This table is always located at pseudo-index |
| 2566 | \IndexAPI{LUA_REGISTRYINDEX}. | 2578 | \IndexAPI{LUA_REGISTRYINDEX}. |
| 2567 | Any C~library can store data into this table, | 2579 | Any C~library can store data into this table, |
| 2568 | as long as it chooses a key different from other libraries. | 2580 | as long as it chooses keys different from other libraries. |
| 2569 | Typically, you can use as key a string containing the library name, | 2581 | Typically, you should use as key a string containing your library name, |
| 2570 | or a light userdata with the address of a C object in your code. | 2582 | or a light userdata with the address of a C object in your code. |
| 2571 | 2583 | ||
| 2572 | The integer keys in the registry are used by the reference mechanism, | 2584 | The integer keys in the registry are used by the reference mechanism, |
| @@ -2583,7 +2595,6 @@ by means of functions and \emph{hooks}, | |||
| 2583 | which allows the construction of different | 2595 | which allows the construction of different |
| 2584 | kinds of debuggers, profilers, and other tools | 2596 | kinds of debuggers, profilers, and other tools |
| 2585 | that need ``inside information'' from the interpreter. | 2597 | that need ``inside information'' from the interpreter. |
| 2586 | This interface is declared in \verb|luadebug.h|. | ||
| 2587 | 2598 | ||
| 2588 | \subsection{Stack and Function Information} | 2599 | \subsection{Stack and Function Information} |
| 2589 | 2600 | ||
| @@ -2683,15 +2694,10 @@ Because functions in Lua are first class values, | |||
| 2683 | they do not have a fixed name: | 2694 | they do not have a fixed name: |
| 2684 | Some functions may be the value of many global variables, | 2695 | Some functions may be the value of many global variables, |
| 2685 | while others may be stored only in a table field. | 2696 | while others may be stored only in a table field. |
| 2686 | The \verb|lua_getinfo| function checks whether the given | 2697 | The \verb|lua_getinfo| function checks how the function was |
| 2687 | function is a tag method or the value of a global variable. | 2698 | called or whether it is the value of a global variable to |
| 2688 | If the given function is a tag method, | 2699 | find a suitable name. |
| 2689 | then \verb|name| points to the event name. | 2700 | If it cannot find a name, |
| 2690 | %% TODO: mas qual o tag? Agora que temos tipos com nome, seria util saber | ||
| 2691 | %% o tipo de TM. Em particular para mensagens de erro. | ||
| 2692 | If the given function is the value of a global variable, | ||
| 2693 | then \verb|name| points to the variable name. | ||
| 2694 | If the given function is neither a tag method nor a global variable, | ||
| 2695 | then \verb|name| is set to \verb|NULL|. | 2701 | then \verb|name| is set to \verb|NULL|. |
| 2696 | 2702 | ||
| 2697 | \item[namewhat] | 2703 | \item[namewhat] |
| @@ -2727,7 +2733,6 @@ given as argument to a hook \see{sub-hooks}. | |||
| 2727 | \verb|lua_getlocal| gets the index \verb|n| of a local variable, | 2733 | \verb|lua_getlocal| gets the index \verb|n| of a local variable, |
| 2728 | pushes its value onto the stack, | 2734 | pushes its value onto the stack, |
| 2729 | and returns its name. | 2735 | and returns its name. |
| 2730 | %% TODO: why return name? | ||
| 2731 | \verb|lua_setlocal| assigns the value at the top of the stack | 2736 | \verb|lua_setlocal| assigns the value at the top of the stack |
| 2732 | to the variable and returns its name. | 2737 | to the variable and returns its name. |
| 2733 | Both functions return \verb|NULL| on failure, | 2738 | Both functions return \verb|NULL| on failure, |
| @@ -2860,33 +2865,6 @@ This function is equivalent to the following Lua function: | |||
| 2860 | end | 2865 | end |
| 2861 | \end{verbatim} | 2866 | \end{verbatim} |
| 2862 | 2867 | ||
| 2863 | ??\subsubsection*{\ff \T{call (func, arg [, mode [, errhandler]])}}\DefLIB{call} | ||
| 2864 | \label{pdf-call} | ||
| 2865 | Calls function \verb|func| with | ||
| 2866 | the arguments given by the table \verb|arg|. | ||
| 2867 | The call is equivalent to | ||
| 2868 | \begin{verbatim} | ||
| 2869 | func(arg[1], arg[2], ..., arg[n]) | ||
| 2870 | \end{verbatim} | ||
| 2871 | where \verb|n| is the result of \verb|getn(arg)| \see{getn}. | ||
| 2872 | All results from \verb|func| are simply returned by \verb|call|. | ||
| 2873 | |||
| 2874 | By default, | ||
| 2875 | if an error occurs during the call to \verb|func|, | ||
| 2876 | the error is propagated. | ||
| 2877 | If the string \verb|mode| contains \verb|"x"|, | ||
| 2878 | then the call is \emph{protected}.\index{protected calls} | ||
| 2879 | In this mode, function \verb|call| does not propagate an error, | ||
| 2880 | regardless of what happens during the call. | ||
| 2881 | Instead, it returns \nil{} to signal the error | ||
| 2882 | (besides calling the appropriated error handler). | ||
| 2883 | |||
| 2884 | If \verb|errhandler| is provided, | ||
| 2885 | the error function \verb|_ERRORMESSAGE| is temporarily set to \verb|errhandler|, | ||
| 2886 | while \verb|func| runs. | ||
| 2887 | In particular, if \verb|errhandler| is \nil, | ||
| 2888 | no error messages will be issued during the execution of the called function. | ||
| 2889 | |||
| 2890 | \subsubsection*{\ff \T{collectgarbage ([limit])}}\DefLIB{collectgarbage} | 2868 | \subsubsection*{\ff \T{collectgarbage ([limit])}}\DefLIB{collectgarbage} |
| 2891 | 2869 | ||
| 2892 | Sets the garbage-collection threshold for the given limit | 2870 | Sets the garbage-collection threshold for the given limit |
| @@ -2935,6 +2913,16 @@ Returns the number of Kbytes of dynamic memory Lua is using, | |||
| 2935 | and (as a second result) the | 2913 | and (as a second result) the |
| 2936 | current garbage collector threshold (also in Kbytes). | 2914 | current garbage collector threshold (also in Kbytes). |
| 2937 | 2915 | ||
| 2916 | \subsubsection*{\ff \T{ipairs (t)}}\DefLIB{ipairs} | ||
| 2917 | |||
| 2918 | Returns the table \verb|t| and a generator function | ||
| 2919 | so that the construction | ||
| 2920 | \begin{verbatim} | ||
| 2921 | for i,v in ipairs(t) do ... end | ||
| 2922 | \end{verbatim} | ||
| 2923 | will iterate over the pairs \verb|1, t[1]|, \verb|2, t[2]|, \ldots, | ||
| 2924 | up to the first nil value of the table. | ||
| 2925 | |||
| 2938 | \subsubsection*{\ff \T{loadfile (filename)}}\DefLIB{loadfile} | 2926 | \subsubsection*{\ff \T{loadfile (filename)}}\DefLIB{loadfile} |
| 2939 | Loads a file as a Lua chunk. | 2927 | Loads a file as a Lua chunk. |
| 2940 | If there is no errors, | 2928 | If there is no errors, |
| @@ -2982,6 +2970,24 @@ use a numerical \rwd{for} or the function \verb|ipairs|). | |||
| 2982 | The behavior of \verb|next| is \emph{undefined} if you change | 2970 | The behavior of \verb|next| is \emph{undefined} if you change |
| 2983 | the table during the traversal. | 2971 | the table during the traversal. |
| 2984 | 2972 | ||
| 2973 | \subsubsection*{\ff \T{pairs (t)}}\DefLIB{pairs} | ||
| 2974 | |||
| 2975 | Returns the table \verb|t| and the function \verb|next|, | ||
| 2976 | so that the construction | ||
| 2977 | \begin{verbatim} | ||
| 2978 | for k,v in pairs(t) do ... end | ||
| 2979 | \end{verbatim} | ||
| 2980 | will iterate over all pairs of key--value of table \verb|t|. | ||
| 2981 | |||
| 2982 | \subsubsection*{\ff \T{pcall (func, arg1, arg2, ...)}}\DefLIB{pcall} | ||
| 2983 | \label{pdf-pcall} | ||
| 2984 | Calls function \verb|func| with | ||
| 2985 | the given arguments in protected mode. | ||
| 2986 | Its first result is a boolean, true if the call succeeds without errors. | ||
| 2987 | In such case, \verb|pcall| also returns all results from the call, | ||
| 2988 | after this first result. | ||
| 2989 | In case of any error, \verb|pcall| returns false plus the error message. | ||
| 2990 | |||
| 2985 | \subsubsection*{\ff \T{print (e1, e2, ...)}}\DefLIB{print} | 2991 | \subsubsection*{\ff \T{print (e1, e2, ...)}}\DefLIB{print} |
| 2986 | Receives any number of arguments, | 2992 | Receives any number of arguments, |
| 2987 | and prints their values in \verb|stdout|, | 2993 | and prints their values in \verb|stdout|, |
| @@ -3225,6 +3231,10 @@ For example, \verb|"%*g"| can be simulated with | |||
| 3225 | String values to be formatted with | 3231 | String values to be formatted with |
| 3226 | \verb|%s| cannot contain embedded zeros. | 3232 | \verb|%s| cannot contain embedded zeros. |
| 3227 | 3233 | ||
| 3234 | \subsubsection*{\ff \T{string.gfind (s, pat)}} | ||
| 3235 | |||
| 3236 | % TODO | ||
| 3237 | |||
| 3228 | \subsubsection*{\ff \T{string.gsub (s, pat, repl [, n])}} | 3238 | \subsubsection*{\ff \T{string.gsub (s, pat, repl [, n])}} |
| 3229 | \DefLIB{string.gsub} | 3239 | \DefLIB{string.gsub} |
| 3230 | Returns a copy of \verb|s| | 3240 | Returns a copy of \verb|s| |
| @@ -3410,7 +3420,8 @@ that value is assumed as its size. | |||
| 3410 | You can call the \verb|table.setn| function to explicitly set | 3420 | You can call the \verb|table.setn| function to explicitly set |
| 3411 | the size of a table. | 3421 | the size of a table. |
| 3412 | \item implicit size --- | 3422 | \item implicit size --- |
| 3413 | %% TODO | 3423 | Otherwise, the size of the object is one less the first integer index |
| 3424 | with a \nil{} value. | ||
| 3414 | \end{itemize} | 3425 | \end{itemize} |
| 3415 | For more details, see the descriptions of the \verb|table.getn| and | 3426 | For more details, see the descriptions of the \verb|table.getn| and |
| 3416 | \verb|table.setn| functions. | 3427 | \verb|table.setn| functions. |
| @@ -3977,9 +3988,9 @@ The program name is stored in index 0, | |||
| 3977 | the first argument after the program goes to index 1, | 3988 | the first argument after the program goes to index 1, |
| 3978 | and so on. | 3989 | and so on. |
| 3979 | The field \verb|n| gets the number of arguments after the program name. | 3990 | The field \verb|n| gets the number of arguments after the program name. |
| 3980 | Any argument before the program name | 3991 | Any arguments before the program name |
| 3981 | (that is, the options plus the interpreter name) | 3992 | (that is, the interpreter name plus the options) |
| 3982 | goes to negative indices. | 3993 | go to negative indices. |
| 3983 | For instance, in the call | 3994 | For instance, in the call |
| 3984 | \begin{verbatim} | 3995 | \begin{verbatim} |
| 3985 | $ lua -la.lua b.lua t1 t2 | 3996 | $ lua -la.lua b.lua t1 t2 |
| @@ -4020,14 +4031,44 @@ then a more portable solution is \verb|#!/usr/bin/env lua|.) | |||
| 4020 | %------------------------------------------------------------------------------ | 4031 | %------------------------------------------------------------------------------ |
| 4021 | \section*{Acknowledgments} | 4032 | \section*{Acknowledgments} |
| 4022 | 4033 | ||
| 4023 | %% TODO rever isso? | 4034 | The Lua team is grateful to \tecgraf{} for its continued support to Lua. |
| 4024 | 4035 | We thank everyone at \tecgraf{}, | |
| 4025 | The authors thank CENPES/PETROBRAS which, | 4036 | specially the head of the group, Marcelo Gattass. |
| 4026 | jointly with \tecgraf, used early versions of | 4037 | At the risk of omitting several names, |
| 4027 | this system extensively and gave valuable comments. | 4038 | we also thank the following individuals for supporting, |
| 4028 | The authors also thank Carlos Henrique Levy, | 4039 | contributing to, and spreading the word about Lua: |
| 4029 | who found the name of the game. | 4040 | Alan Watson, |
| 4030 | Lua means ``moon'' in Portuguese. | 4041 | Andr\'e Clinio, |
| 4042 | Andr\'e Costa, | ||
| 4043 | Bret Mogilefsky, | ||
| 4044 | Cameron Laird, | ||
| 4045 | Carlos Cassino, | ||
| 4046 | Carlos Henrique Levy, | ||
| 4047 | Claudio Terra, | ||
| 4048 | David Jeske, | ||
| 4049 | Edgar Toernig, | ||
| 4050 | Erik Hougaard, | ||
| 4051 | Jim Mathies, | ||
| 4052 | John Belmonte, | ||
| 4053 | John Passaniti, | ||
| 4054 | John Roll, | ||
| 4055 | Jon Erickson, | ||
| 4056 | Jon Kleiser, | ||
| 4057 | Mark Ian Barlow, | ||
| 4058 | Nick Trout, | ||
| 4059 | Noemi Rodriguez, | ||
| 4060 | Norman Ramsey, | ||
| 4061 | Philippe Lhost, | ||
| 4062 | Renata Ratton, | ||
| 4063 | Renato Borges, | ||
| 4064 | Renato Cerqueira, | ||
| 4065 | Reuben Thomas, | ||
| 4066 | Stephan Herrmann, | ||
| 4067 | Steve Dekorte, | ||
| 4068 | Thatcher Ulrich, | ||
| 4069 | Tom\'as Gorham, | ||
| 4070 | Vincent Penquerc'h. | ||
| 4071 | Thank you! | ||
| 4031 | 4072 | ||
| 4032 | 4073 | ||
| 4033 | \appendix | 4074 | \appendix |
| @@ -4035,12 +4076,6 @@ Lua means ``moon'' in Portuguese. | |||
| 4035 | \section*{Incompatibilities with Previous Versions} | 4076 | \section*{Incompatibilities with Previous Versions} |
| 4036 | \addcontentsline{toc}{section}{Incompatibilities with Previous Versions} | 4077 | \addcontentsline{toc}{section}{Incompatibilities with Previous Versions} |
| 4037 | 4078 | ||
| 4038 | We took a great care to avoid incompatibilities with | ||
| 4039 | the previous public versions of Lua, | ||
| 4040 | but some differences had to be introduced. | ||
| 4041 | Here is a list of all these incompatibilities. | ||
| 4042 | |||
| 4043 | |||
| 4044 | \subsection*{Incompatibilities with \Index{version 4.0}} | 4079 | \subsection*{Incompatibilities with \Index{version 4.0}} |
| 4045 | 4080 | ||
| 4046 | \subsubsection*{Changes in the Language} | 4081 | \subsubsection*{Changes in the Language} |
| @@ -4069,6 +4104,14 @@ this newline is ignored. | |||
| 4069 | \begin{itemize} | 4104 | \begin{itemize} |
| 4070 | 4105 | ||
| 4071 | \item | 4106 | \item |
| 4107 | Most library functions now are defined inside tables. | ||
| 4108 | There is a compatibility script (\verb|compat.lua|) that | ||
| 4109 | redefine most of them as global names. | ||
| 4110 | |||
| 4111 | \item | ||
| 4112 | \verb|dofile| do not handle errors, but simply propagate them. | ||
| 4113 | |||
| 4114 | \item | ||
| 4072 | The \verb|read| option \verb|*w| is obsolete. | 4115 | The \verb|read| option \verb|*w| is obsolete. |
| 4073 | 4116 | ||
| 4074 | \item | 4117 | \item |
