diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-05-29 10:41:32 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-05-29 10:41:32 -0300 |
commit | 50523b107d5bcc8069b1aec4b5b11b3fcc87da8d (patch) | |
tree | ebe9cb860c73b5d94a31b94689807e1f7d3d0d42 | |
parent | aa8d4a782d88738b3ea921cde5a450656da8fa63 (diff) | |
download | lua-50523b107d5bcc8069b1aec4b5b11b3fcc87da8d.tar.gz lua-50523b107d5bcc8069b1aec4b5b11b3fcc87da8d.tar.bz2 lua-50523b107d5bcc8069b1aec4b5b11b3fcc87da8d.zip |
Improvements in the manual
- more consistent nomenclature for error handling
- more precise definition for dead objects
- added algorithm used by 'math.random'
- added luaL_pushfail
- some other minor changes
-rw-r--r-- | manual/manual.of | 131 |
1 files changed, 86 insertions, 45 deletions
diff --git a/manual/manual.of b/manual/manual.of index 9eeb94aa..2eadbda0 100644 --- a/manual/manual.of +++ b/manual/manual.of | |||
@@ -252,33 +252,47 @@ of its first upvalue; see @See{lua_setupvalue}.) | |||
252 | 252 | ||
253 | @sect2{error| @title{Error Handling} | 253 | @sect2{error| @title{Error Handling} |
254 | 254 | ||
255 | Several operations in Lua can @emph{raise} an error. | ||
256 | An error interrupts the normal flow of the program, | ||
257 | which can continue by @emph{catching} the error. | ||
258 | |||
259 | Lua code can explicitly raise an error by calling the | ||
260 | @Lid{error} function. | ||
261 | (This function never returns.) | ||
262 | |||
263 | To catch errors in Lua, | ||
264 | you can do a @def{protected call}, | ||
265 | using @Lid{pcall} (or @Lid{xpcall}). | ||
266 | The function @Lid{pcall} calls a given function in @def{protected mode}. | ||
267 | Any error while running the function stops its execution, | ||
268 | and control returns immediately to @id{pcall}, | ||
269 | which returns a status code. | ||
270 | |||
255 | Because Lua is an embedded extension language, | 271 | Because Lua is an embedded extension language, |
256 | all Lua actions start from @N{C code} in the host program | 272 | Lua code starts running by a call |
257 | calling a function from the Lua library. | 273 | from @N{C code} in the host program. |
258 | (When you use Lua standalone, | 274 | (When you use Lua standalone, |
259 | the @id{lua} application is the host program.) | 275 | the @id{lua} application is the host program.) |
260 | Whenever an error occurs during | 276 | Usually, this call is protected; |
277 | so, when an otherwise unprotected error occurs during | ||
261 | the compilation or execution of a Lua chunk, | 278 | the compilation or execution of a Lua chunk, |
262 | control returns to the host, | 279 | control returns to the host, |
263 | which can take appropriate measures | 280 | which can take appropriate measures, |
264 | (such as printing an error message). | 281 | such as printing an error message. |
265 | |||
266 | Lua code can explicitly generate an error by calling the | ||
267 | @Lid{error} function. | ||
268 | If you need to catch errors in Lua, | ||
269 | you can use @Lid{pcall} or @Lid{xpcall} | ||
270 | to call a given function in @emphx{protected mode}. | ||
271 | 282 | ||
272 | Whenever there is an error, | 283 | Whenever there is an error, |
273 | an @def{error object} (also called an @def{error message}) | 284 | an @def{error object} |
274 | is propagated with information about the error. | 285 | is propagated with information about the error. |
275 | Lua itself only generates errors whose error object is a string, | 286 | Lua itself only generates errors whose error object is a string, |
276 | but programs may generate errors with | 287 | but programs may generate errors with |
277 | any value as the error object. | 288 | any value as the error object. |
278 | It is up to the Lua program or its host to handle such error objects. | 289 | It is up to the Lua program or its host to handle such error objects. |
290 | For historical reasons, | ||
291 | an error object is often called an @def{error message}, | ||
292 | even though it does not have to be a string. | ||
279 | 293 | ||
280 | 294 | ||
281 | When you use @Lid{xpcall} or @Lid{lua_pcall}, | 295 | When you use @Lid{xpcall} (or @Lid{lua_pcall}, in C) |
282 | you may give a @def{message handler} | 296 | you may give a @def{message handler} |
283 | to be called in case of errors. | 297 | to be called in case of errors. |
284 | This function is called with the original error object | 298 | This function is called with the original error object |
@@ -581,11 +595,30 @@ This means that | |||
581 | you do not have to worry about allocating memory for new objects | 595 | you do not have to worry about allocating memory for new objects |
582 | or freeing it when the objects are no longer needed. | 596 | or freeing it when the objects are no longer needed. |
583 | Lua manages memory automatically by running | 597 | Lua manages memory automatically by running |
584 | a @def{garbage collector} to collect all @emph{dead objects} | 598 | a @def{garbage collector} to collect all @emph{dead} objects. |
585 | (that is, objects that are no longer accessible from Lua). | ||
586 | All memory used by Lua is subject to automatic management: | 599 | All memory used by Lua is subject to automatic management: |
587 | strings, tables, userdata, functions, threads, internal structures, etc. | 600 | strings, tables, userdata, functions, threads, internal structures, etc. |
588 | 601 | ||
602 | An object is considered @def{dead} | ||
603 | as soon as the collector can be sure the object | ||
604 | will not be accessed again in the normal execution of the program. | ||
605 | (@Q{Normal execution} here excludes finalizers, | ||
606 | which can resurrect dead objects @see{finalizers}, | ||
607 | and excludes also operations using the debug library.) | ||
608 | Note that the time when the collector can be sure that an object | ||
609 | is dead may not coincide with the programmer's expectations. | ||
610 | The only guarantees are that Lua will not collect an object | ||
611 | that may still be accessed in the normal execution of the program, | ||
612 | and it will eventually collect an object | ||
613 | that is inaccessible from Lua. | ||
614 | (Here, | ||
615 | @emph{inaccessible from Lua} means that neither a variable nor | ||
616 | another live object refer to the object.) | ||
617 | Because Lua has no knowledge about @N{C code}, | ||
618 | it never collects objects accessible through the registry @see{registry}, | ||
619 | which includes the global environment @see{globalenv}. | ||
620 | |||
621 | |||
589 | The garbage collector (GC) in Lua can work in two modes: | 622 | The garbage collector (GC) in Lua can work in two modes: |
590 | incremental and generational. | 623 | incremental and generational. |
591 | 624 | ||
@@ -694,7 +727,7 @@ and, using the @N{C API}, | |||
694 | for full userdata @see{metatable}. | 727 | for full userdata @see{metatable}. |
695 | These metamethods, called @def{finalizers}, | 728 | These metamethods, called @def{finalizers}, |
696 | are called when the garbage collector detects that the | 729 | are called when the garbage collector detects that the |
697 | corresponding table or userdata is unreachable. | 730 | corresponding table or userdata is dead. |
698 | Finalizers allow you to coordinate Lua's garbage collection | 731 | Finalizers allow you to coordinate Lua's garbage collection |
699 | with external resource management such as closing files, | 732 | with external resource management such as closing files, |
700 | network or database connections, | 733 | network or database connections, |
@@ -709,7 +742,7 @@ Note that if you set a metatable without a @idx{__gc} field | |||
709 | and later create that field in the metatable, | 742 | and later create that field in the metatable, |
710 | the object will not be marked for finalization. | 743 | the object will not be marked for finalization. |
711 | 744 | ||
712 | When a marked object becomes unreachable, | 745 | When a marked object becomes dead, |
713 | it is not collected immediately by the garbage collector. | 746 | it is not collected immediately by the garbage collector. |
714 | Instead, Lua puts it in a list. | 747 | Instead, Lua puts it in a list. |
715 | After the collection, | 748 | After the collection, |
@@ -738,10 +771,10 @@ However, if the finalizer stores the object in some global place | |||
738 | then the resurrection is permanent. | 771 | then the resurrection is permanent. |
739 | Moreover, if the finalizer marks a finalizing object for finalization again, | 772 | Moreover, if the finalizer marks a finalizing object for finalization again, |
740 | its finalizer will be called again in the next cycle where the | 773 | its finalizer will be called again in the next cycle where the |
741 | object is unreachable. | 774 | object is dead. |
742 | In any case, | 775 | In any case, |
743 | the object memory is freed only in a GC cycle where | 776 | the object memory is freed only in a GC cycle where |
744 | the object is unreachable and not marked for finalization. | 777 | the object is dead and not marked for finalization. |
745 | 778 | ||
746 | When you close a state @seeF{lua_close}, | 779 | When you close a state @seeF{lua_close}, |
747 | Lua calls the finalizers of all objects marked for finalization, | 780 | Lua calls the finalizers of all objects marked for finalization, |
@@ -2611,6 +2644,9 @@ For such errors, Lua does not call the @x{message handler}. | |||
2611 | 2644 | ||
2612 | @item{@defid{LUA_YIELD}| the thread (coroutine) yields.} | 2645 | @item{@defid{LUA_YIELD}| the thread (coroutine) yields.} |
2613 | 2646 | ||
2647 | @item{@defid{LUA_ERRFILE}| a file-related error; | ||
2648 | e.g., it cannot open or read the file.} | ||
2649 | |||
2614 | } | 2650 | } |
2615 | These constants are defined in the header file @id{lua.h}. | 2651 | These constants are defined in the header file @id{lua.h}. |
2616 | 2652 | ||
@@ -3113,7 +3149,7 @@ This function does not pop the Lua function from the stack. | |||
3113 | @APIEntry{int lua_error (lua_State *L);| | 3149 | @APIEntry{int lua_error (lua_State *L);| |
3114 | @apii{1,0,v} | 3150 | @apii{1,0,v} |
3115 | 3151 | ||
3116 | Generates a Lua error, | 3152 | Raises a Lua error, |
3117 | using the value on the top of the stack as the error object. | 3153 | using the value on the top of the stack as the error object. |
3118 | This function does a long jump, | 3154 | This function does a long jump, |
3119 | and therefore never returns | 3155 | and therefore never returns |
@@ -4125,8 +4161,9 @@ Returns 0 if the userdata does not have that value. | |||
4125 | @APIEntry{int lua_setmetatable (lua_State *L, int index);| | 4161 | @APIEntry{int lua_setmetatable (lua_State *L, int index);| |
4126 | @apii{1,0,-} | 4162 | @apii{1,0,-} |
4127 | 4163 | ||
4128 | Pops a table from the stack and | 4164 | Pops a table or @nil from the stack and |
4129 | sets it as the new metatable for the value at the given index. | 4165 | sets that value as the new metatable for the value at the given index. |
4166 | (@nil means no metatable.) | ||
4130 | 4167 | ||
4131 | (For historical reasons, this function returns an @id{int}, | 4168 | (For historical reasons, this function returns an @id{int}, |
4132 | which now is always 1.) | 4169 | which now is always 1.) |
@@ -4596,7 +4633,7 @@ If @T{source} starts with a @Char{@At}, | |||
4596 | it means that the function was defined in a file where | 4633 | it means that the function was defined in a file where |
4597 | the file name follows the @Char{@At}. | 4634 | the file name follows the @Char{@At}. |
4598 | If @T{source} starts with a @Char{=}, | 4635 | If @T{source} starts with a @Char{=}, |
4599 | the remainder of its contents describe the source in a user-dependent manner. | 4636 | the remainder of its contents describes the source in a user-dependent manner. |
4600 | Otherwise, | 4637 | Otherwise, |
4601 | the function was defined in a string where | 4638 | the function was defined in a string where |
4602 | @T{source} is that string. | 4639 | @T{source} is that string. |
@@ -5212,7 +5249,7 @@ plus the final string on its top. | |||
5212 | @APIEntry{char *luaL_buffaddr (luaL_Buffer *B);| | 5249 | @APIEntry{char *luaL_buffaddr (luaL_Buffer *B);| |
5213 | @apii{0,0,-} | 5250 | @apii{0,0,-} |
5214 | 5251 | ||
5215 | Returns the address of the current contents of buffer @id{B} | 5252 | Returns the address of the current content of buffer @id{B} |
5216 | @seeC{luaL_Buffer}. | 5253 | @seeC{luaL_Buffer}. |
5217 | Note that any addition to the buffer may invalidate this address. | 5254 | Note that any addition to the buffer may invalidate this address. |
5218 | 5255 | ||
@@ -5231,7 +5268,7 @@ the buffer must be declared as a variable. | |||
5231 | @APIEntry{size_t luaL_bufflen (luaL_Buffer *B);| | 5268 | @APIEntry{size_t luaL_bufflen (luaL_Buffer *B);| |
5232 | @apii{0,0,-} | 5269 | @apii{0,0,-} |
5233 | 5270 | ||
5234 | Returns the length of the current contents of buffer @id{B} | 5271 | Returns the length of the current content of buffer @id{B} |
5235 | @seeC{luaL_Buffer}. | 5272 | @seeC{luaL_Buffer}. |
5236 | 5273 | ||
5237 | } | 5274 | } |
@@ -5384,8 +5421,8 @@ It is defined as the following macro: | |||
5384 | @verbatim{ | 5421 | @verbatim{ |
5385 | (luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0)) | 5422 | (luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0)) |
5386 | } | 5423 | } |
5387 | It returns false if there are no errors | 5424 | It returns @Lid{LUA_OK} if there are no errors, |
5388 | or true in case of errors. | 5425 | or an error code in case of errors @see{statuscodes}. |
5389 | 5426 | ||
5390 | } | 5427 | } |
5391 | 5428 | ||
@@ -5397,8 +5434,8 @@ It is defined as the following macro: | |||
5397 | @verbatim{ | 5434 | @verbatim{ |
5398 | (luaL_loadstring(L, str) || lua_pcall(L, 0, LUA_MULTRET, 0)) | 5435 | (luaL_loadstring(L, str) || lua_pcall(L, 0, LUA_MULTRET, 0)) |
5399 | } | 5436 | } |
5400 | It returns false if there are no errors | 5437 | It returns @Lid{LUA_OK} if there are no errors, |
5401 | or true in case of errors. | 5438 | or an error code in case of errors @see{statuscodes}. |
5402 | 5439 | ||
5403 | } | 5440 | } |
5404 | 5441 | ||
@@ -5548,10 +5585,8 @@ The first line in the file is ignored if it starts with a @T{#}. | |||
5548 | 5585 | ||
5549 | The string @id{mode} works as in the function @Lid{lua_load}. | 5586 | The string @id{mode} works as in the function @Lid{lua_load}. |
5550 | 5587 | ||
5551 | This function returns the same results as @Lid{lua_load}, | 5588 | This function returns the same results as @Lid{lua_load} |
5552 | but it has an extra error code @defid{LUA_ERRFILE} | 5589 | or @Lid{LUA_ERRFILE} for file-related errors. |
5553 | for file-related errors | ||
5554 | (e.g., it cannot open or read the file). | ||
5555 | 5590 | ||
5556 | As @Lid{lua_load}, this function only loads the chunk; | 5591 | As @Lid{lua_load}, this function only loads the chunk; |
5557 | it does not run it. | 5592 | it does not run it. |
@@ -5742,6 +5777,13 @@ it to the buffer. | |||
5742 | 5777 | ||
5743 | } | 5778 | } |
5744 | 5779 | ||
5780 | @APIEntry{void luaL_pushfail (lua_State *L);| | ||
5781 | @apii{0,1,-} | ||
5782 | |||
5783 | Pushes the @fail value onto the stack. | ||
5784 | |||
5785 | } | ||
5786 | |||
5745 | @APIEntry{void luaL_pushresult (luaL_Buffer *B);| | 5787 | @APIEntry{void luaL_pushresult (luaL_Buffer *B);| |
5746 | @apii{?,1,m} | 5788 | @apii{?,1,m} |
5747 | 5789 | ||
@@ -6052,7 +6094,7 @@ implementations for some of its facilities. | |||
6052 | 6094 | ||
6053 | @LibEntry{assert (v [, message])| | 6095 | @LibEntry{assert (v [, message])| |
6054 | 6096 | ||
6055 | Calls @Lid{error} if | 6097 | Raises an error if |
6056 | the value of its argument @id{v} is false (i.e., @nil or @false); | 6098 | the value of its argument @id{v} is false (i.e., @nil or @false); |
6057 | otherwise, returns all its arguments. | 6099 | otherwise, returns all its arguments. |
6058 | In case of error, | 6100 | In case of error, |
@@ -6129,9 +6171,9 @@ and some of these options. | |||
6129 | } | 6171 | } |
6130 | 6172 | ||
6131 | @LibEntry{dofile ([filename])| | 6173 | @LibEntry{dofile ([filename])| |
6132 | Opens the named file and executes its contents as a Lua chunk. | 6174 | Opens the named file and executes its content as a Lua chunk. |
6133 | When called without arguments, | 6175 | When called without arguments, |
6134 | @id{dofile} executes the contents of the standard input (@id{stdin}). | 6176 | @id{dofile} executes the content of the standard input (@id{stdin}). |
6135 | Returns all values returned by the chunk. | 6177 | Returns all values returned by the chunk. |
6136 | In case of errors, @id{dofile} propagates the error | 6178 | In case of errors, @id{dofile} propagates the error |
6137 | to its caller. | 6179 | to its caller. |
@@ -6140,8 +6182,7 @@ to its caller. | |||
6140 | } | 6182 | } |
6141 | 6183 | ||
6142 | @LibEntry{error (message [, level])| | 6184 | @LibEntry{error (message [, level])| |
6143 | Terminates the last protected function called | 6185 | Raises an error @see{error} with @{message} as the error object. |
6144 | and returns @id{message} as the error object. | ||
6145 | This function never returns. | 6186 | This function never returns. |
6146 | 6187 | ||
6147 | Usually, @id{error} adds some information about the error position | 6188 | Usually, @id{error} adds some information about the error position |
@@ -6301,7 +6342,7 @@ the table during its traversal. | |||
6301 | @LibEntry{pcall (f [, arg1, @Cdots])| | 6342 | @LibEntry{pcall (f [, arg1, @Cdots])| |
6302 | 6343 | ||
6303 | Calls the function @id{f} with | 6344 | Calls the function @id{f} with |
6304 | the given arguments in @def{protected mode}. | 6345 | the given arguments in @emphx{protected mode}. |
6305 | This means that any error @N{inside @T{f}} is not propagated; | 6346 | This means that any error @N{inside @T{f}} is not propagated; |
6306 | instead, @id{pcall} catches the error | 6347 | instead, @id{pcall} catches the error |
6307 | and returns a status code. | 6348 | and returns a status code. |
@@ -7899,17 +7940,17 @@ is equivalent to @T{math.random(1,n)}. | |||
7899 | The call @T{math.random(0)} produces an integer with | 7940 | The call @T{math.random(0)} produces an integer with |
7900 | all bits (pseudo)random. | 7941 | all bits (pseudo)random. |
7901 | 7942 | ||
7943 | This function uses the @idx{xoshiro256**} algorithm to produce | ||
7944 | pseudo-random 64-bit integers, | ||
7945 | which are the results of calls with @N{argument 0}. | ||
7946 | Other results (ranges and floats) | ||
7947 | are unbiased extracted from these integers. | ||
7948 | |||
7902 | Lua initializes its pseudo-random generator with the equivalent of | 7949 | Lua initializes its pseudo-random generator with the equivalent of |
7903 | a call to @Lid{math.randomseed} with no arguments, | 7950 | a call to @Lid{math.randomseed} with no arguments, |
7904 | so that @id{math.random} should generate | 7951 | so that @id{math.random} should generate |
7905 | different sequences of results each time the program runs. | 7952 | different sequences of results each time the program runs. |
7906 | 7953 | ||
7907 | The results from this function have good statistical qualities, | ||
7908 | but they are not cryptographically secure. | ||
7909 | (For instance, there are no guarantees that it is hard | ||
7910 | to predict future results based on the observation of | ||
7911 | some previous results.) | ||
7912 | |||
7913 | } | 7954 | } |
7914 | 7955 | ||
7915 | @LibEntry{math.randomseed ([x [, y]])| | 7956 | @LibEntry{math.randomseed ([x [, y]])| |