diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-04-10 15:44:48 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-04-10 15:44:48 -0300 |
commit | 7ccc6d8290143009d2bab8f4330bbf443fc25846 (patch) | |
tree | e59c44cccd98d725bc7fe9b4ad2d85420dc04802 | |
parent | 7288528a1e081d101a1bc19346a974088b6b8315 (diff) | |
download | lua-7ccc6d8290143009d2bab8f4330bbf443fc25846.tar.gz lua-7ccc6d8290143009d2bab8f4330bbf443fc25846.tar.bz2 lua-7ccc6d8290143009d2bab8f4330bbf443fc25846.zip |
Improvements in the manual
Several small improvements, in particular a new subsection consolidating
all status codes in the API.
-rw-r--r-- | manual/manual.of | 110 |
1 files changed, 64 insertions, 46 deletions
diff --git a/manual/manual.of b/manual/manual.of index d5b4a572..3ba82b09 100644 --- a/manual/manual.of +++ b/manual/manual.of | |||
@@ -96,6 +96,14 @@ is particularly attractive | |||
96 | for small machines and embedded systems. | 96 | for small machines and embedded systems. |
97 | (See macro @id{LUA_32BITS} in file @id{luaconf.h}.) | 97 | (See macro @id{LUA_32BITS} in file @id{luaconf.h}.) |
98 | 98 | ||
99 | Unless stated otherwise, | ||
100 | any overflow when manipulating integer values @def{wrap around}, | ||
101 | according to the usual rules of two-complement arithmetic. | ||
102 | (In other words, | ||
103 | the actual result is the unique representable integer | ||
104 | that is equal modulo @M{2@sp{n}} to the mathematical result, | ||
105 | where @M{n} is the number of bits of the integer type.) | ||
106 | |||
99 | Lua has explicit rules about when each subtype is used, | 107 | Lua has explicit rules about when each subtype is used, |
100 | but it also converts between them automatically as needed @see{coercion}. | 108 | but it also converts between them automatically as needed @see{coercion}. |
101 | Therefore, | 109 | Therefore, |
@@ -1098,8 +1106,11 @@ if its value fits in an integer or it is a hexadecimal constant, | |||
1098 | it denotes an integer; | 1106 | it denotes an integer; |
1099 | otherwise (that is, a decimal integer numeral that overflows), | 1107 | otherwise (that is, a decimal integer numeral that overflows), |
1100 | it denotes a float. | 1108 | it denotes a float. |
1101 | (Hexadecimal integer numerals that overflow @emph{wrap around}; | 1109 | Hexadecimal numerals with neither a radix point nor an exponent |
1102 | they always denote an integer value.) | 1110 | always denote an integer value; |
1111 | if the value overflows, it @emph{wraps around} | ||
1112 | to fit into a valid integer. | ||
1113 | |||
1103 | Examples of valid integer constants are | 1114 | Examples of valid integer constants are |
1104 | @verbatim{ | 1115 | @verbatim{ |
1105 | 3 345 0xff 0xBEBADA | 1116 | 3 345 0xff 0xBEBADA |
@@ -1712,12 +1723,7 @@ Modulo is defined as the remainder of a division | |||
1712 | that rounds the quotient towards minus infinity (floor division). | 1723 | that rounds the quotient towards minus infinity (floor division). |
1713 | 1724 | ||
1714 | In case of overflows in integer arithmetic, | 1725 | In case of overflows in integer arithmetic, |
1715 | all operations @emphx{wrap around}, | 1726 | all operations @emphx{wrap around}. |
1716 | according to the usual rules of two-complement arithmetic. | ||
1717 | (In other words, | ||
1718 | they return the unique representable integer | ||
1719 | that is equal modulo @M{2@sp{n}} to the mathematical result, | ||
1720 | where @M{n} is the number of bits of the integer type.) | ||
1721 | } | 1727 | } |
1722 | 1728 | ||
1723 | @sect3{bitwise| @title{Bitwise Operators} | 1729 | @sect3{bitwise| @title{Bitwise Operators} |
@@ -2364,9 +2370,8 @@ and | |||
2364 | (that is, the element at @N{the top}) | 2370 | (that is, the element at @N{the top}) |
2365 | and index @M{-n} represents the first element. | 2371 | and index @M{-n} represents the first element. |
2366 | 2372 | ||
2367 | } | ||
2368 | 2373 | ||
2369 | @sect2{stacksize| @title{Stack Size} | 2374 | @sect3{stacksize| @title{Stack Size} |
2370 | 2375 | ||
2371 | When you interact with the Lua API, | 2376 | When you interact with the Lua API, |
2372 | you are responsible for ensuring consistency. | 2377 | you are responsible for ensuring consistency. |
@@ -2391,7 +2396,7 @@ you should use @Lid{lua_checkstack}. | |||
2391 | 2396 | ||
2392 | } | 2397 | } |
2393 | 2398 | ||
2394 | @sect2{@title{Valid and Acceptable Indices} | 2399 | @sect3{@title{Valid and Acceptable Indices} |
2395 | 2400 | ||
2396 | Any function in the API that receives stack indices | 2401 | Any function in the API that receives stack indices |
2397 | works only with @emphx{valid indices} or @emphx{acceptable indices}. | 2402 | works only with @emphx{valid indices} or @emphx{acceptable indices}. |
@@ -2433,6 +2438,8 @@ which behaves like a nil value. | |||
2433 | 2438 | ||
2434 | } | 2439 | } |
2435 | 2440 | ||
2441 | } | ||
2442 | |||
2436 | @sect2{c-closure| @title{C Closures} | 2443 | @sect2{c-closure| @title{C Closures} |
2437 | 2444 | ||
2438 | When a @N{C function} is created, | 2445 | When a @N{C function} is created, |
@@ -2552,6 +2559,33 @@ However, there is no guarantee about stack space. | |||
2552 | To push anything on the stack, | 2559 | To push anything on the stack, |
2553 | the panic function must first check the available space @see{stacksize}. | 2560 | the panic function must first check the available space @see{stacksize}. |
2554 | 2561 | ||
2562 | |||
2563 | @sect3{statuscodes|@title{Status Codes} | ||
2564 | |||
2565 | Several functions that report errors in the API use the following | ||
2566 | status codes to indicate different kinds of errors or other conditions: | ||
2567 | @description{ | ||
2568 | |||
2569 | @item{@defid{LUA_OK} (0)| no errors.} | ||
2570 | |||
2571 | @item{@defid{LUA_ERRRUN}| a runtime error.} | ||
2572 | |||
2573 | @item{@defid{LUA_ERRMEM}| | ||
2574 | @x{memory allocation error}. | ||
2575 | For such errors, Lua does not call the @x{message handler}. | ||
2576 | } | ||
2577 | |||
2578 | @item{@defid{LUA_ERRERR}| error while running the @x{message handler}.} | ||
2579 | |||
2580 | @item{@defid{LUA_ERRSYNTAX}| syntax error during precompilation.} | ||
2581 | |||
2582 | @item{@defid{LUA_YIELD}| the thread (coroutine) yields.} | ||
2583 | |||
2584 | } | ||
2585 | These constants are defined in the header file @id{lua.h}. | ||
2586 | |||
2587 | } | ||
2588 | |||
2555 | } | 2589 | } |
2556 | 2590 | ||
2557 | @sect2{continuations|@title{Handling Yields in C} | 2591 | @sect2{continuations|@title{Handling Yields in C} |
@@ -3407,19 +3441,6 @@ If there are no errors, | |||
3407 | function on top of the stack. | 3441 | function on top of the stack. |
3408 | Otherwise, it pushes an error message. | 3442 | Otherwise, it pushes an error message. |
3409 | 3443 | ||
3410 | The return values of @id{lua_load} are: | ||
3411 | @description{ | ||
3412 | |||
3413 | @item{@Lid{LUA_OK}| no errors;} | ||
3414 | |||
3415 | @item{@defid{LUA_ERRSYNTAX}| | ||
3416 | syntax error during precompilation;} | ||
3417 | |||
3418 | @item{@Lid{LUA_ERRMEM}| | ||
3419 | @x{memory allocation (out-of-memory) error};} | ||
3420 | |||
3421 | } | ||
3422 | |||
3423 | The @id{lua_load} function uses a user-supplied @id{reader} function | 3444 | The @id{lua_load} function uses a user-supplied @id{reader} function |
3424 | to read the chunk @seeC{lua_Reader}. | 3445 | to read the chunk @seeC{lua_Reader}. |
3425 | The @id{data} argument is an opaque value passed to the reader function. | 3446 | The @id{data} argument is an opaque value passed to the reader function. |
@@ -3437,6 +3458,11 @@ a @id{NULL} value is equivalent to the string @St{bt}. | |||
3437 | so the reader function must always leave the stack | 3458 | so the reader function must always leave the stack |
3438 | unmodified when returning. | 3459 | unmodified when returning. |
3439 | 3460 | ||
3461 | @id{lua_load} can return | ||
3462 | @Lid{LUA_OK}, @Lid{LUA_ERRSYNTAX}, or @Lid{LUA_ERRMEM}. | ||
3463 | The function may also return other values corresponding to | ||
3464 | errors raised by the read function @see{statuscodes}. | ||
3465 | |||
3440 | If the resulting function has upvalues, | 3466 | If the resulting function has upvalues, |
3441 | its first upvalue is set to the value of the @x{global environment} | 3467 | its first upvalue is set to the value of the @x{global environment} |
3442 | stored at index @id{LUA_RIDX_GLOBALS} in the registry @see{registry}. | 3468 | stored at index @id{LUA_RIDX_GLOBALS} in the registry @see{registry}. |
@@ -3590,25 +3616,8 @@ information to the error object, such as a stack traceback. | |||
3590 | Such information cannot be gathered after the return of @Lid{lua_pcall}, | 3616 | Such information cannot be gathered after the return of @Lid{lua_pcall}, |
3591 | since by then the stack has unwound. | 3617 | since by then the stack has unwound. |
3592 | 3618 | ||
3593 | The @Lid{lua_pcall} function returns one of the following constants | 3619 | The @Lid{lua_pcall} function returns one of the following status codes: |
3594 | (defined in @id{lua.h}): | 3620 | @Lid{LUA_OK}, @Lid{LUA_ERRRUN}, @Lid{LUA_ERRMEM}, or @Lid{LUA_ERRERR}. |
3595 | @description{ | ||
3596 | |||
3597 | @item{@defid{LUA_OK} (0)| | ||
3598 | success.} | ||
3599 | |||
3600 | @item{@defid{LUA_ERRRUN}| | ||
3601 | a runtime error. | ||
3602 | } | ||
3603 | |||
3604 | @item{@defid{LUA_ERRMEM}| | ||
3605 | @x{memory allocation error}. | ||
3606 | For such errors, Lua does not call the @x{message handler}. | ||
3607 | } | ||
3608 | |||
3609 | @item{@defid{LUA_ERRERR}| | ||
3610 | error while running the @x{message handler}. | ||
3611 | } | ||
3612 | 3621 | ||
3613 | } | 3622 | } |
3614 | 3623 | ||
@@ -3624,7 +3633,7 @@ int lua_pcallk (lua_State *L, | |||
3624 | @apii{nargs + 1,nresults|1,-} | 3633 | @apii{nargs + 1,nresults|1,-} |
3625 | 3634 | ||
3626 | This function behaves exactly like @Lid{lua_pcall}, | 3635 | This function behaves exactly like @Lid{lua_pcall}, |
3627 | but allows the called function to yield @see{continuations}. | 3636 | except that it allows the called function to yield @see{continuations}. |
3628 | 3637 | ||
3629 | } | 3638 | } |
3630 | 3639 | ||
@@ -4002,7 +4011,7 @@ or returned by the body function. | |||
4002 | @Lid{LUA_YIELD} if the coroutine yields, | 4011 | @Lid{LUA_YIELD} if the coroutine yields, |
4003 | @Lid{LUA_OK} if the coroutine finishes its execution | 4012 | @Lid{LUA_OK} if the coroutine finishes its execution |
4004 | without errors, | 4013 | without errors, |
4005 | or an error code in case of errors @seeC{lua_pcall}. | 4014 | or an error code in case of errors @see{statuscodes}. |
4006 | In case of errors, | 4015 | In case of errors, |
4007 | the error object is on the top of the stack. | 4016 | the error object is on the top of the stack. |
4008 | 4017 | ||
@@ -4153,7 +4162,7 @@ Returns the status of the thread @id{L}. | |||
4153 | The status can be @Lid{LUA_OK} for a normal thread, | 4162 | The status can be @Lid{LUA_OK} for a normal thread, |
4154 | an error code if the thread finished the execution | 4163 | an error code if the thread finished the execution |
4155 | of a @Lid{lua_resume} with an error, | 4164 | of a @Lid{lua_resume} with an error, |
4156 | or @defid{LUA_YIELD} if the thread is suspended. | 4165 | or @Lid{LUA_YIELD} if the thread is suspended. |
4157 | 4166 | ||
4158 | You can call functions only in threads with status @Lid{LUA_OK}. | 4167 | You can call functions only in threads with status @Lid{LUA_OK}. |
4159 | You can resume threads with status @Lid{LUA_OK} | 4168 | You can resume threads with status @Lid{LUA_OK} |
@@ -6263,6 +6272,7 @@ which is true if the call succeeds without errors. | |||
6263 | In such case, @id{pcall} also returns all results from the call, | 6272 | In such case, @id{pcall} also returns all results from the call, |
6264 | after this first result. | 6273 | after this first result. |
6265 | In case of any error, @id{pcall} returns @false plus the error object. | 6274 | In case of any error, @id{pcall} returns @false plus the error object. |
6275 | Note that errors caught by @id{pcall} do not call a message handler. | ||
6266 | 6276 | ||
6267 | } | 6277 | } |
6268 | 6278 | ||
@@ -8950,6 +8960,14 @@ not a float. | |||
8950 | } | 8960 | } |
8951 | 8961 | ||
8952 | @item{ | 8962 | @item{ |
8963 | Literal decimal integer constants that overflow are read as floats, | ||
8964 | instead of wrapping around. | ||
8965 | You can use hexadecimal notation for such constants if you | ||
8966 | want the old bevhavior | ||
8967 | (reading them as integers with wrap around). | ||
8968 | } | ||
8969 | |||
8970 | @item{ | ||
8953 | The use of the @idx{__lt} metamethod to emulate @id{__le} | 8971 | The use of the @idx{__lt} metamethod to emulate @id{__le} |
8954 | has been removed. | 8972 | has been removed. |
8955 | When needed, this metamethod must be explicitly defined. | 8973 | When needed, this metamethod must be explicitly defined. |