summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2020-05-29 10:41:32 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2020-05-29 10:41:32 -0300
commit50523b107d5bcc8069b1aec4b5b11b3fcc87da8d (patch)
treeebe9cb860c73b5d94a31b94689807e1f7d3d0d42
parentaa8d4a782d88738b3ea921cde5a450656da8fa63 (diff)
downloadlua-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.of131
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
255Several operations in Lua can @emph{raise} an error.
256An error interrupts the normal flow of the program,
257which can continue by @emph{catching} the error.
258
259Lua code can explicitly raise an error by calling the
260@Lid{error} function.
261(This function never returns.)
262
263To catch errors in Lua,
264you can do a @def{protected call},
265using @Lid{pcall} (or @Lid{xpcall}).
266The function @Lid{pcall} calls a given function in @def{protected mode}.
267Any error while running the function stops its execution,
268and control returns immediately to @id{pcall},
269which returns a status code.
270
255Because Lua is an embedded extension language, 271Because Lua is an embedded extension language,
256all Lua actions start from @N{C code} in the host program 272Lua code starts running by a call
257calling a function from the Lua library. 273from @N{C code} in the host program.
258(When you use Lua standalone, 274(When you use Lua standalone,
259the @id{lua} application is the host program.) 275the @id{lua} application is the host program.)
260Whenever an error occurs during 276Usually, this call is protected;
277so, when an otherwise unprotected error occurs during
261the compilation or execution of a Lua chunk, 278the compilation or execution of a Lua chunk,
262control returns to the host, 279control returns to the host,
263which can take appropriate measures 280which can take appropriate measures,
264(such as printing an error message). 281such as printing an error message.
265
266Lua code can explicitly generate an error by calling the
267@Lid{error} function.
268If you need to catch errors in Lua,
269you can use @Lid{pcall} or @Lid{xpcall}
270to call a given function in @emphx{protected mode}.
271 282
272Whenever there is an error, 283Whenever there is an error,
273an @def{error object} (also called an @def{error message}) 284an @def{error object}
274is propagated with information about the error. 285is propagated with information about the error.
275Lua itself only generates errors whose error object is a string, 286Lua itself only generates errors whose error object is a string,
276but programs may generate errors with 287but programs may generate errors with
277any value as the error object. 288any value as the error object.
278It is up to the Lua program or its host to handle such error objects. 289It is up to the Lua program or its host to handle such error objects.
290For historical reasons,
291an error object is often called an @def{error message},
292even though it does not have to be a string.
279 293
280 294
281When you use @Lid{xpcall} or @Lid{lua_pcall}, 295When you use @Lid{xpcall} (or @Lid{lua_pcall}, in C)
282you may give a @def{message handler} 296you may give a @def{message handler}
283to be called in case of errors. 297to be called in case of errors.
284This function is called with the original error object 298This function is called with the original error object
@@ -581,11 +595,30 @@ This means that
581you do not have to worry about allocating memory for new objects 595you do not have to worry about allocating memory for new objects
582or freeing it when the objects are no longer needed. 596or freeing it when the objects are no longer needed.
583Lua manages memory automatically by running 597Lua manages memory automatically by running
584a @def{garbage collector} to collect all @emph{dead objects} 598a @def{garbage collector} to collect all @emph{dead} objects.
585(that is, objects that are no longer accessible from Lua).
586All memory used by Lua is subject to automatic management: 599All memory used by Lua is subject to automatic management:
587strings, tables, userdata, functions, threads, internal structures, etc. 600strings, tables, userdata, functions, threads, internal structures, etc.
588 601
602An object is considered @def{dead}
603as soon as the collector can be sure the object
604will not be accessed again in the normal execution of the program.
605(@Q{Normal execution} here excludes finalizers,
606which can resurrect dead objects @see{finalizers},
607and excludes also operations using the debug library.)
608Note that the time when the collector can be sure that an object
609is dead may not coincide with the programmer's expectations.
610The only guarantees are that Lua will not collect an object
611that may still be accessed in the normal execution of the program,
612and it will eventually collect an object
613that is inaccessible from Lua.
614(Here,
615@emph{inaccessible from Lua} means that neither a variable nor
616another live object refer to the object.)
617Because Lua has no knowledge about @N{C code},
618it never collects objects accessible through the registry @see{registry},
619which includes the global environment @see{globalenv}.
620
621
589The garbage collector (GC) in Lua can work in two modes: 622The garbage collector (GC) in Lua can work in two modes:
590incremental and generational. 623incremental and generational.
591 624
@@ -694,7 +727,7 @@ and, using the @N{C API},
694for full userdata @see{metatable}. 727for full userdata @see{metatable}.
695These metamethods, called @def{finalizers}, 728These metamethods, called @def{finalizers},
696are called when the garbage collector detects that the 729are called when the garbage collector detects that the
697corresponding table or userdata is unreachable. 730corresponding table or userdata is dead.
698Finalizers allow you to coordinate Lua's garbage collection 731Finalizers allow you to coordinate Lua's garbage collection
699with external resource management such as closing files, 732with external resource management such as closing files,
700network or database connections, 733network or database connections,
@@ -709,7 +742,7 @@ Note that if you set a metatable without a @idx{__gc} field
709and later create that field in the metatable, 742and later create that field in the metatable,
710the object will not be marked for finalization. 743the object will not be marked for finalization.
711 744
712When a marked object becomes unreachable, 745When a marked object becomes dead,
713it is not collected immediately by the garbage collector. 746it is not collected immediately by the garbage collector.
714Instead, Lua puts it in a list. 747Instead, Lua puts it in a list.
715After the collection, 748After the collection,
@@ -738,10 +771,10 @@ However, if the finalizer stores the object in some global place
738then the resurrection is permanent. 771then the resurrection is permanent.
739Moreover, if the finalizer marks a finalizing object for finalization again, 772Moreover, if the finalizer marks a finalizing object for finalization again,
740its finalizer will be called again in the next cycle where the 773its finalizer will be called again in the next cycle where the
741object is unreachable. 774object is dead.
742In any case, 775In any case,
743the object memory is freed only in a GC cycle where 776the object memory is freed only in a GC cycle where
744the object is unreachable and not marked for finalization. 777the object is dead and not marked for finalization.
745 778
746When you close a state @seeF{lua_close}, 779When you close a state @seeF{lua_close},
747Lua calls the finalizers of all objects marked for finalization, 780Lua 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;
2648e.g., it cannot open or read the file.}
2649
2614} 2650}
2615These constants are defined in the header file @id{lua.h}. 2651These 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
3116Generates a Lua error, 3152Raises a Lua error,
3117using the value on the top of the stack as the error object. 3153using the value on the top of the stack as the error object.
3118This function does a long jump, 3154This function does a long jump,
3119and therefore never returns 3155and 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
4128Pops a table from the stack and 4164Pops a table or @nil from the stack and
4129sets it as the new metatable for the value at the given index. 4165sets 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},
4132which now is always 1.) 4169which now is always 1.)
@@ -4596,7 +4633,7 @@ If @T{source} starts with a @Char{@At},
4596it means that the function was defined in a file where 4633it means that the function was defined in a file where
4597the file name follows the @Char{@At}. 4634the file name follows the @Char{@At}.
4598If @T{source} starts with a @Char{=}, 4635If @T{source} starts with a @Char{=},
4599the remainder of its contents describe the source in a user-dependent manner. 4636the remainder of its contents describes the source in a user-dependent manner.
4600Otherwise, 4637Otherwise,
4601the function was defined in a string where 4638the 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
5215Returns the address of the current contents of buffer @id{B} 5252Returns the address of the current content of buffer @id{B}
5216@seeC{luaL_Buffer}. 5253@seeC{luaL_Buffer}.
5217Note that any addition to the buffer may invalidate this address. 5254Note 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
5234Returns the length of the current contents of buffer @id{B} 5271Returns 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}
5387It returns false if there are no errors 5424It returns @Lid{LUA_OK} if there are no errors,
5388or true in case of errors. 5425or 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}
5400It returns false if there are no errors 5437It returns @Lid{LUA_OK} if there are no errors,
5401or true in case of errors. 5438or 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
5549The string @id{mode} works as in the function @Lid{lua_load}. 5586The string @id{mode} works as in the function @Lid{lua_load}.
5550 5587
5551This function returns the same results as @Lid{lua_load}, 5588This function returns the same results as @Lid{lua_load}
5552but it has an extra error code @defid{LUA_ERRFILE} 5589or @Lid{LUA_ERRFILE} for file-related errors.
5553for file-related errors
5554(e.g., it cannot open or read the file).
5555 5590
5556As @Lid{lua_load}, this function only loads the chunk; 5591As @Lid{lua_load}, this function only loads the chunk;
5557it does not run it. 5592it 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
5783Pushes 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
6055Calls @Lid{error} if 6097Raises an error if
6056the value of its argument @id{v} is false (i.e., @nil or @false); 6098the value of its argument @id{v} is false (i.e., @nil or @false);
6057otherwise, returns all its arguments. 6099otherwise, returns all its arguments.
6058In case of error, 6100In case of error,
@@ -6129,9 +6171,9 @@ and some of these options.
6129} 6171}
6130 6172
6131@LibEntry{dofile ([filename])| 6173@LibEntry{dofile ([filename])|
6132Opens the named file and executes its contents as a Lua chunk. 6174Opens the named file and executes its content as a Lua chunk.
6133When called without arguments, 6175When 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}).
6135Returns all values returned by the chunk. 6177Returns all values returned by the chunk.
6136In case of errors, @id{dofile} propagates the error 6178In case of errors, @id{dofile} propagates the error
6137to its caller. 6179to its caller.
@@ -6140,8 +6182,7 @@ to its caller.
6140} 6182}
6141 6183
6142@LibEntry{error (message [, level])| 6184@LibEntry{error (message [, level])|
6143Terminates the last protected function called 6185Raises an error @see{error} with @{message} as the error object.
6144and returns @id{message} as the error object.
6145This function never returns. 6186This function never returns.
6146 6187
6147Usually, @id{error} adds some information about the error position 6188Usually, @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
6303Calls the function @id{f} with 6344Calls the function @id{f} with
6304the given arguments in @def{protected mode}. 6345the given arguments in @emphx{protected mode}.
6305This means that any error @N{inside @T{f}} is not propagated; 6346This means that any error @N{inside @T{f}} is not propagated;
6306instead, @id{pcall} catches the error 6347instead, @id{pcall} catches the error
6307and returns a status code. 6348and returns a status code.
@@ -7899,17 +7940,17 @@ is equivalent to @T{math.random(1,n)}.
7899The call @T{math.random(0)} produces an integer with 7940The call @T{math.random(0)} produces an integer with
7900all bits (pseudo)random. 7941all bits (pseudo)random.
7901 7942
7943This function uses the @idx{xoshiro256**} algorithm to produce
7944pseudo-random 64-bit integers,
7945which are the results of calls with @N{argument 0}.
7946Other results (ranges and floats)
7947are unbiased extracted from these integers.
7948
7902Lua initializes its pseudo-random generator with the equivalent of 7949Lua initializes its pseudo-random generator with the equivalent of
7903a call to @Lid{math.randomseed} with no arguments, 7950a call to @Lid{math.randomseed} with no arguments,
7904so that @id{math.random} should generate 7951so that @id{math.random} should generate
7905different sequences of results each time the program runs. 7952different sequences of results each time the program runs.
7906 7953
7907The results from this function have good statistical qualities,
7908but they are not cryptographically secure.
7909(For instance, there are no guarantees that it is hard
7910to predict future results based on the observation of
7911some previous results.)
7912
7913} 7954}
7914 7955
7915@LibEntry{math.randomseed ([x [, y]])| 7956@LibEntry{math.randomseed ([x [, y]])|