diff options
-rw-r--r-- | manual.tex | 82 |
1 files changed, 44 insertions, 38 deletions
@@ -1,4 +1,4 @@ | |||
1 | % $Id: manual.tex,v 1.34 1999/10/04 17:51:04 roberto Exp roberto $ | 1 | % $Id: manual.tex,v 1.35 2000/04/14 17:47:55 roberto Exp roberto $ |
2 | 2 | ||
3 | \documentclass[11pt]{article} | 3 | \documentclass[11pt]{article} |
4 | \usepackage{fullpage,bnf} | 4 | \usepackage{fullpage,bnf} |
@@ -41,7 +41,7 @@ Waldemar Celes | |||
41 | \tecgraf\ --- Computer Science Department --- PUC-Rio | 41 | \tecgraf\ --- Computer Science Department --- PUC-Rio |
42 | } | 42 | } |
43 | 43 | ||
44 | \date{{\small \tt\$Date: 1999/10/04 17:51:04 $ $}} | 44 | \date{{\small \tt\$Date: 2000/04/14 17:47:55 $ $}} |
45 | 45 | ||
46 | \maketitle | 46 | \maketitle |
47 | 47 | ||
@@ -456,7 +456,7 @@ and can be optionally followed by a semicolon: | |||
456 | \begin{Produc} | 456 | \begin{Produc} |
457 | \produc{block}{\opt{label} \rep{stat sc}} | 457 | \produc{block}{\opt{label} \rep{stat sc}} |
458 | \produc{sc}{\opt{\ter{;}}} | 458 | \produc{sc}{\opt{\ter{;}}} |
459 | \produc{label}{\ter{|} name \ter{|}} | 459 | \produc{label}{\ter{$\vert$} name \ter{$\vert$}} |
460 | \end{Produc}% | 460 | \end{Produc}% |
461 | For syntactic reasons, \rwd{return} and | 461 | For syntactic reasons, \rwd{return} and |
462 | \rwd{break} statements can only be written | 462 | \rwd{break} statements can only be written |
@@ -680,7 +680,7 @@ giving the expected meaning to \Index{exponentiation} | |||
680 | \subsubsection{Relational Operators} | 680 | \subsubsection{Relational Operators} |
681 | Lua provides the following \Index{relational operators}: | 681 | Lua provides the following \Index{relational operators}: |
682 | \begin{verbatim} | 682 | \begin{verbatim} |
683 | < > <= >= ~= == | 683 | == ~= < > <= >= |
684 | \end{verbatim} | 684 | \end{verbatim} |
685 | All these return \nil\ as false and a value different from \nil\ as true. | 685 | All these return \nil\ as false and a value different from \nil\ as true. |
686 | 686 | ||
@@ -698,7 +698,7 @@ Thus, \verb|"0"==0| evaluates to false, | |||
698 | and \verb|t[0]| and \verb|t["0"]| denote different | 698 | and \verb|t[0]| and \verb|t["0"]| denote different |
699 | entries in a table. | 699 | entries in a table. |
700 | 700 | ||
701 | The other operators work as follows. | 701 | The order operators work as follows. |
702 | If both arguments are numbers, then they are compared as such. | 702 | If both arguments are numbers, then they are compared as such. |
703 | Otherwise, if both arguments are strings, | 703 | Otherwise, if both arguments are strings, |
704 | then their values are compared using lexicographical order. | 704 | then their values are compared using lexicographical order. |
@@ -1311,10 +1311,8 @@ called when Lua tries to call a non function value. | |||
1311 | else | 1311 | else |
1312 | local tm = gettagmethod(tag(func), "function") | 1312 | local tm = gettagmethod(tag(func), "function") |
1313 | if tm then | 1313 | if tm then |
1314 | local i = arg.n | 1314 | for i=arg.n,1,-1 do |
1315 | while i > 0 do | ||
1316 | arg[i+1] = arg[i] | 1315 | arg[i+1] = arg[i] |
1317 | i = i-1 | ||
1318 | end | 1316 | end |
1319 | arg.n = arg.n+1 | 1317 | arg.n = arg.n+1 |
1320 | arg[1] = func | 1318 | arg[1] = func |
@@ -1430,7 +1428,7 @@ Currently, the function accepts the following options: | |||
1430 | \item \verb|"stack"| - the stack size. | 1428 | \item \verb|"stack"| - the stack size. |
1431 | Each function call needs one stack position for each local variable | 1429 | Each function call needs one stack position for each local variable |
1432 | and temporary variables, plus one position. | 1430 | and temporary variables, plus one position. |
1433 | The stack must also have at least ten positions available. | 1431 | The stack must also have at least ten extra positions available. |
1434 | For very small implementations, without recursive functions, | 1432 | For very small implementations, without recursive functions, |
1435 | a size of 100 should be enough. | 1433 | a size of 100 should be enough. |
1436 | The default is 1K. | 1434 | The default is 1K. |
@@ -1459,7 +1457,7 @@ void lua_close (lua_State *L); | |||
1459 | This function destroys all objects in the current Lua environment | 1457 | This function destroys all objects in the current Lua environment |
1460 | (calling the correspondent garbage collector tag methods), | 1458 | (calling the correspondent garbage collector tag methods), |
1461 | and frees all dynamic memory used by the state. | 1459 | and frees all dynamic memory used by the state. |
1462 | Frequently, you do not need to call this function, | 1460 | Usually, you do not need to call this function, |
1463 | because these resources are naturally released when the program ends. | 1461 | because these resources are naturally released when the program ends. |
1464 | 1462 | ||
1465 | With the exception of \verb|lua_newstate|, | 1463 | With the exception of \verb|lua_newstate|, |
@@ -1470,6 +1468,9 @@ functions, and also to keep compatibility with old versions of Lua, | |||
1470 | the API provides a set of macros and one global variable that | 1468 | the API provides a set of macros and one global variable that |
1471 | take care of this state argument for single-state applications: | 1469 | take care of this state argument for single-state applications: |
1472 | \begin{verbatim} | 1470 | \begin{verbatim} |
1471 | #ifndef LUA_REENTRANT | ||
1472 | \end{verbatim} | ||
1473 | \begin{verbatim} | ||
1473 | extern lua_State *lua_state; | 1474 | extern lua_State *lua_state; |
1474 | \end{verbatim} | 1475 | \end{verbatim} |
1475 | \begin{verbatim} | 1476 | \begin{verbatim} |
@@ -1478,9 +1479,12 @@ extern lua_State *lua_state; | |||
1478 | #define lua_dostring(str) (lua_dostring)(lua_state, str) | 1479 | #define lua_dostring(str) (lua_dostring)(lua_state, str) |
1479 | ... | 1480 | ... |
1480 | \end{verbatim} | 1481 | \end{verbatim} |
1482 | \begin{verbatim} | ||
1483 | #endif | ||
1484 | \end{verbatim} | ||
1481 | For each function in the API, there is a macro with the same name | 1485 | For each function in the API, there is a macro with the same name |
1482 | that supplies \verb|lua_state| as the first argument to the call. | 1486 | that supplies \verb|lua_state| as the first argument to the call. |
1483 | (The parentheses around the function name is to avoid it being expanded | 1487 | (The parentheses around the function name avoid it being expanded |
1484 | again as a macro.) | 1488 | again as a macro.) |
1485 | The only exception is \verb|lua_newstate|; | 1489 | The only exception is \verb|lua_newstate|; |
1486 | in this case, the corresponding macro is | 1490 | in this case, the corresponding macro is |
@@ -1488,7 +1492,7 @@ in this case, the corresponding macro is | |||
1488 | #define lua_open() ((void)(lua_state?0:(lua_state=lua_newstate(0)))) | 1492 | #define lua_open() ((void)(lua_state?0:(lua_state=lua_newstate(0)))) |
1489 | \end{verbatim} | 1493 | \end{verbatim} |
1490 | It checks whether the global state has been initialized; | 1494 | It checks whether the global state has been initialized; |
1491 | if not, it then creates a new state with default settings and | 1495 | if not, it creates a new state with default settings and |
1492 | assigns it to \verb|lua_newstate|. | 1496 | assigns it to \verb|lua_newstate|. |
1493 | 1497 | ||
1494 | By default, those macros are all active. | 1498 | By default, those macros are all active. |
@@ -2030,8 +2034,11 @@ void lua_unref (int ref); | |||
2030 | The function \verb|lua_ref| creates a reference | 2034 | The function \verb|lua_ref| creates a reference |
2031 | to the object that is on the top of the stack, | 2035 | to the object that is on the top of the stack, |
2032 | and returns this reference. | 2036 | and returns this reference. |
2033 | For a \nil{} object, the reference is always -1; | 2037 | For a \nil{} object, |
2038 | the reference is always \verb|LUA_REFNIL|;\Deffunc{LUA_REFNIL} | ||
2034 | otherwise, it is a non-negative integer. | 2039 | otherwise, it is a non-negative integer. |
2040 | The constant \verb|LUA_NOREF| \Deffunc{LUA_NOREF} | ||
2041 | is different from any valid reference. | ||
2035 | If \verb|lock| is true, the object is \emph{locked}: | 2042 | If \verb|lock| is true, the object is \emph{locked}: |
2036 | this means the object will not be garbage collected. | 2043 | this means the object will not be garbage collected. |
2037 | Note that an unlocked reference may be garbage collected. | 2044 | Note that an unlocked reference may be garbage collected. |
@@ -2347,11 +2354,11 @@ This function could be defined in Lua: | |||
2347 | \begin{verbatim} | 2354 | \begin{verbatim} |
2348 | function getn (t) | 2355 | function getn (t) |
2349 | if type(t.n) == 'number' then return t.n end | 2356 | if type(t.n) == 'number' then return t.n end |
2350 | local max = 0 | 2357 | local max, i = 0, nil |
2351 | local i = next(t, nil) | 2358 | while 1 do |
2352 | while i do | ||
2353 | if type(i) == 'number' and i>max then max=i end | ||
2354 | i = next(t, i) | 2359 | i = next(t, i) |
2360 | if not i then break end | ||
2361 | if type(i) == 'number' and i>max then max=i end | ||
2355 | end | 2362 | end |
2356 | return max | 2363 | return max |
2357 | end | 2364 | end |
@@ -2369,11 +2376,12 @@ as the final value of \verb|foreach|. | |||
2369 | This function could be defined in Lua: | 2376 | This function could be defined in Lua: |
2370 | \begin{verbatim} | 2377 | \begin{verbatim} |
2371 | function foreach (t, f) | 2378 | function foreach (t, f) |
2372 | local i, v = next(t, nil) | 2379 | local i, v = nil |
2373 | while i do | 2380 | while 1 do |
2381 | i, v = next(t, i) | ||
2382 | if not i then break end | ||
2374 | local res = f(i, v) | 2383 | local res = f(i, v) |
2375 | if res then return res end | 2384 | if res then return res end |
2376 | i, v = next(t, i) | ||
2377 | end | 2385 | end |
2378 | end | 2386 | end |
2379 | \end{verbatim} | 2387 | \end{verbatim} |
@@ -2398,11 +2406,9 @@ as the final value of \verb|foreachi|. | |||
2398 | This function could be defined in Lua: | 2406 | This function could be defined in Lua: |
2399 | \begin{verbatim} | 2407 | \begin{verbatim} |
2400 | function foreachi (t, f) | 2408 | function foreachi (t, f) |
2401 | local i, n = 1, getn(t) | 2409 | for i=1,getn(t) do |
2402 | while i <= n do | ||
2403 | local res = f(i, t[i]) | 2410 | local res = f(i, t[i]) |
2404 | if res then return res end | 2411 | if res then return res end |
2405 | i = i+1 | ||
2406 | end | 2412 | end |
2407 | end | 2413 | end |
2408 | \end{verbatim} | 2414 | \end{verbatim} |
@@ -2418,11 +2424,12 @@ as the final value of \verb|foreachvar|. | |||
2418 | This function could be defined in Lua: | 2424 | This function could be defined in Lua: |
2419 | \begin{verbatim} | 2425 | \begin{verbatim} |
2420 | function foreachvar (f) | 2426 | function foreachvar (f) |
2421 | local n, v = nextvar(nil) | 2427 | local n, v = nil |
2422 | while n do | 2428 | while 1 do |
2429 | n, v = nextvar(n) | ||
2430 | if not n then break end | ||
2423 | local res = f(n, v) | 2431 | local res = f(n, v) |
2424 | if res then return res end | 2432 | if res then return res end |
2425 | n, v = nextvar(n) | ||
2426 | end | 2433 | end |
2427 | end | 2434 | end |
2428 | \end{verbatim} | 2435 | \end{verbatim} |
@@ -2454,9 +2461,8 @@ except that the table accesses are all raw (that is, without tag methods): | |||
2454 | pos, value = arg[1], arg[2] | 2461 | pos, value = arg[1], arg[2] |
2455 | end | 2462 | end |
2456 | t.n = n+1; | 2463 | t.n = n+1; |
2457 | while n >= pos do | 2464 | for i=n,pos,-1 do |
2458 | t[n+1] = t[n] | 2465 | t[i+1] = t[i] |
2459 | n = n-1 | ||
2460 | end | 2466 | end |
2461 | t[pos] = value | 2467 | t[pos] = value |
2462 | end | 2468 | end |
@@ -2480,12 +2486,11 @@ except that the table accesses are all raw (that is, without tag methods): | |||
2480 | \begin{verbatim} | 2486 | \begin{verbatim} |
2481 | function tremove (t, pos) | 2487 | function tremove (t, pos) |
2482 | local n = getn(t) | 2488 | local n = getn(t) |
2489 | if n<=0 then return end | ||
2483 | pos = pos or n | 2490 | pos = pos or n |
2484 | local value = t[pos] | 2491 | local value = t[pos] |
2485 | if n<=0 then return end | 2492 | for i=pos,n-1 do |
2486 | while pos < n do | 2493 | t[i] = t[i+1] |
2487 | t[pos] = t[pos+1] | ||
2488 | pos = pos+1 | ||
2489 | end | 2494 | end |
2490 | t[n] = nil | 2495 | t[n] = nil |
2491 | t.n = n-1 | 2496 | t.n = n-1 |
@@ -3266,18 +3271,19 @@ int listvars (lua_State *L, int level) { | |||
3266 | 3271 | ||
3267 | The Lua interpreter offers two hooks for debugging purposes: | 3272 | The Lua interpreter offers two hooks for debugging purposes: |
3268 | a \emph{call} hook and a \emph{line} hook. | 3273 | a \emph{call} hook and a \emph{line} hook. |
3269 | Both have the same type, and you can set them with the | 3274 | Both have the same type, |
3270 | following functions: | ||
3271 | \Deffunc{lua_Hook}\Deffunc{lua_setcallhook}\Deffunc{lua_setlinehook} | ||
3272 | \begin{verbatim} | 3275 | \begin{verbatim} |
3273 | typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); | 3276 | typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); |
3274 | 3277 | \end{verbatim} | |
3278 | and you can set them with the following functions: | ||
3279 | \Deffunc{lua_Hook}\Deffunc{lua_setcallhook}\Deffunc{lua_setlinehook} | ||
3280 | \begin{verbatim} | ||
3275 | lua_Hook lua_setcallhook (lua_State *L, lua_Hook func); | 3281 | lua_Hook lua_setcallhook (lua_State *L, lua_Hook func); |
3276 | lua_Hook lua_setlinehook (lua_State *L, lua_Hook func); | 3282 | lua_Hook lua_setlinehook (lua_State *L, lua_Hook func); |
3277 | \end{verbatim} | 3283 | \end{verbatim} |
3278 | A hook is disabled when its value is \verb|NULL|, | 3284 | A hook is disabled when its value is \verb|NULL|, |
3279 | which is the initial value of both hooks. | 3285 | which is the initial value of both hooks. |
3280 | Both \verb|lua_setcallhook| and \verb|lua_setlinehook| | 3286 | The functions \verb|lua_setcallhook| and \verb|lua_setlinehook| |
3281 | set their corresponding hooks and return their previous values. | 3287 | set their corresponding hooks and return their previous values. |
3282 | 3288 | ||
3283 | The call hook is called whenever the | 3289 | The call hook is called whenever the |