diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-10-23 10:41:47 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-10-23 10:41:47 -0300 |
commit | 4c32d9300c77a70b7c20b2eebda40e97541e3f01 (patch) | |
tree | 34a7652f4100a1aa533972a9edfb5ca42ef1b0df | |
parent | 6e285e539274920830eeec5cd2dde5eeca5863e4 (diff) | |
download | lua-4c32d9300c77a70b7c20b2eebda40e97541e3f01.tar.gz lua-4c32d9300c77a70b7c20b2eebda40e97541e3f01.tar.bz2 lua-4c32d9300c77a70b7c20b2eebda40e97541e3f01.zip |
Several enhancements in the manual
-rw-r--r-- | manual/manual.of | 97 |
1 files changed, 54 insertions, 43 deletions
diff --git a/manual/manual.of b/manual/manual.of index df7b74b4..3d79e7e2 100644 --- a/manual/manual.of +++ b/manual/manual.of | |||
@@ -82,7 +82,8 @@ whose main property is to be different from any other value; | |||
82 | it often represents the absence of a useful value. | 82 | it often represents the absence of a useful value. |
83 | The type @emph{boolean} has two values, @false and @true. | 83 | The type @emph{boolean} has two values, @false and @true. |
84 | Both @nil and @false make a condition false; | 84 | Both @nil and @false make a condition false; |
85 | any other value makes it true. | 85 | they are collectively called @def{false values}. |
86 | Any other value makes a condition true. | ||
86 | 87 | ||
87 | The type @emph{number} represents both | 88 | The type @emph{number} represents both |
88 | integer numbers and real (floating-point) numbers, | 89 | integer numbers and real (floating-point) numbers, |
@@ -278,9 +279,9 @@ so, an error inside the message handler | |||
278 | will call the message handler again. | 279 | will call the message handler again. |
279 | If this loop goes on for too long, | 280 | If this loop goes on for too long, |
280 | Lua breaks it and returns an appropriate message. | 281 | Lua breaks it and returns an appropriate message. |
281 | (The message handler is called only for regular runtime errors. | 282 | The message handler is called only for regular runtime errors. |
282 | It is not called for memory-allocation errors | 283 | It is not called for memory-allocation errors |
283 | nor for errors while running finalizers.) | 284 | nor for errors while running finalizers or other message handlers. |
284 | 285 | ||
285 | Lua also offers a system of @emph{warnings} @seeF{warn}. | 286 | Lua also offers a system of @emph{warnings} @seeF{warn}. |
286 | Unlike errors, warnings do not interfere | 287 | Unlike errors, warnings do not interfere |
@@ -467,7 +468,7 @@ Behavior similar to the less than operation. | |||
467 | The indexing access operation @T{table[key]}. | 468 | The indexing access operation @T{table[key]}. |
468 | This event happens when @id{table} is not a table or | 469 | This event happens when @id{table} is not a table or |
469 | when @id{key} is not present in @id{table}. | 470 | when @id{key} is not present in @id{table}. |
470 | The metamethod is looked up in @id{table}. | 471 | The metamethod is looked up in the metatable of @id{table}. |
471 | 472 | ||
472 | Despite the name, | 473 | Despite the name, |
473 | the metamethod for this event can be either a function or a table. | 474 | the metamethod for this event can be either a function or a table. |
@@ -528,7 +529,7 @@ the interpreter also respects the following keys in metatables: | |||
528 | and @idx{__name}. | 529 | and @idx{__name}. |
529 | (The entry @idx{__name}, | 530 | (The entry @idx{__name}, |
530 | when it contains a string, | 531 | when it contains a string, |
531 | is used by some error-reporting functions to build error messages.) | 532 | may be used by @Lid{tostring} and in error messages.) |
532 | 533 | ||
533 | For the unary operators (negation, length, and bitwise NOT), | 534 | For the unary operators (negation, length, and bitwise NOT), |
534 | the metamethod is computed and called with a dummy second operand, | 535 | the metamethod is computed and called with a dummy second operand, |
@@ -638,7 +639,7 @@ In generational mode, | |||
638 | the collector does frequent @emph{minor} collections, | 639 | the collector does frequent @emph{minor} collections, |
639 | which traverses only objects recently created. | 640 | which traverses only objects recently created. |
640 | If after a minor collection the use of memory is still above a limit, | 641 | If after a minor collection the use of memory is still above a limit, |
641 | the collector does a @emph{major} collection, | 642 | the collector does a stop-the-world @emph{major} collection, |
642 | which traverses all objects. | 643 | which traverses all objects. |
643 | The generational mode uses two parameters: | 644 | The generational mode uses two parameters: |
644 | the @def{minor multiplier} and the @def{the major multiplier}. | 645 | the @def{minor multiplier} and the @def{the major multiplier}. |
@@ -943,7 +944,7 @@ Lua is a @x{free-form} language. | |||
943 | It ignores spaces and comments between lexical elements (@x{tokens}), | 944 | It ignores spaces and comments between lexical elements (@x{tokens}), |
944 | except as delimiters between two tokens. | 945 | except as delimiters between two tokens. |
945 | In source code, | 946 | In source code, |
946 | Lua recognizes as spaces the standard ASCII white-space | 947 | Lua recognizes as spaces the standard ASCII whitespace |
947 | characters space, form feed, newline, | 948 | characters space, form feed, newline, |
948 | carriage return, horizontal tab, and vertical tab. | 949 | carriage return, horizontal tab, and vertical tab. |
949 | 950 | ||
@@ -998,7 +999,7 @@ and @Char{\'} (apostrophe [single quote]). | |||
998 | A backslash followed by a line break | 999 | A backslash followed by a line break |
999 | results in a newline in the string. | 1000 | results in a newline in the string. |
1000 | The escape sequence @Char{\z} skips the following span | 1001 | The escape sequence @Char{\z} skips the following span |
1001 | of white-space characters, | 1002 | of whitespace characters, |
1002 | including line breaks; | 1003 | including line breaks; |
1003 | it is particularly useful to break and indent a long literal string | 1004 | it is particularly useful to break and indent a long literal string |
1004 | into multiple lines without adding the newlines and spaces | 1005 | into multiple lines without adding the newlines and spaces |
@@ -1769,7 +1770,7 @@ Otherwise, the conversion fails. | |||
1769 | Several places in Lua coerce strings to numbers when necessary. | 1770 | Several places in Lua coerce strings to numbers when necessary. |
1770 | A string is converted to an integer or a float | 1771 | A string is converted to an integer or a float |
1771 | following its syntax and the rules of the Lua lexer. | 1772 | following its syntax and the rules of the Lua lexer. |
1772 | (The string may have also leading and trailing spaces and a sign.) | 1773 | (The string may have also leading and trailing whitespaces and a sign.) |
1773 | All conversions from strings to numbers | 1774 | All conversions from strings to numbers |
1774 | accept both a dot and the current locale mark | 1775 | accept both a dot and the current locale mark |
1775 | as the radix character. | 1776 | as the radix character. |
@@ -2182,7 +2183,7 @@ function r() return 1,2,3 end | |||
2182 | Then, we have the following mapping from arguments to parameters and | 2183 | Then, we have the following mapping from arguments to parameters and |
2183 | to the vararg expression: | 2184 | to the vararg expression: |
2184 | @verbatim{ | 2185 | @verbatim{ |
2185 | CALL PARAMETERS | 2186 | CALL PARAMETERS |
2186 | 2187 | ||
2187 | f(3) a=3, b=nil | 2188 | f(3) a=3, b=nil |
2188 | f(3, 4) a=3, b=4 | 2189 | f(3, 4) a=3, b=4 |
@@ -2802,9 +2803,13 @@ Sets a new panic function and returns the old one @see{C-error}. | |||
2802 | @apii{nargs+1,nresults,e} | 2803 | @apii{nargs+1,nresults,e} |
2803 | 2804 | ||
2804 | Calls a function. | 2805 | Calls a function. |
2806 | Like regular Lua calls, | ||
2807 | @id{lua_call} respects the @idx{__call} metamethod. | ||
2808 | So, here the word @Q{function} | ||
2809 | means any callable value. | ||
2805 | 2810 | ||
2806 | To do a call you must use the following protocol: | 2811 | To do a call you must use the following protocol: |
2807 | first, the value to be called is pushed onto the stack; | 2812 | first, the function to be called is pushed onto the stack; |
2808 | then, the arguments to the call are pushed | 2813 | then, the arguments to the call are pushed |
2809 | in direct order; | 2814 | in direct order; |
2810 | that is, the first argument is pushed first. | 2815 | that is, the first argument is pushed first. |
@@ -2812,7 +2817,7 @@ Finally you call @Lid{lua_call}; | |||
2812 | @id{nargs} is the number of arguments that you pushed onto the stack. | 2817 | @id{nargs} is the number of arguments that you pushed onto the stack. |
2813 | When the function returns, | 2818 | When the function returns, |
2814 | all arguments and the function value are popped | 2819 | all arguments and the function value are popped |
2815 | and the function results are pushed onto the stack. | 2820 | and the call results are pushed onto the stack. |
2816 | The number of results is adjusted to @id{nresults}, | 2821 | The number of results is adjusted to @id{nresults}, |
2817 | unless @id{nresults} is @defid{LUA_MULTRET}. | 2822 | unless @id{nresults} is @defid{LUA_MULTRET}. |
2818 | In this case, all results from the function are pushed; | 2823 | In this case, all results from the function are pushed; |
@@ -2824,8 +2829,6 @@ so that after the call the last result is on the top of the stack. | |||
2824 | 2829 | ||
2825 | Any error while calling and running the function is propagated upwards | 2830 | Any error while calling and running the function is propagated upwards |
2826 | (with a @id{longjmp}). | 2831 | (with a @id{longjmp}). |
2827 | Like regular Lua calls, | ||
2828 | this function respects the @idx{__call} metamethod. | ||
2829 | 2832 | ||
2830 | The following example shows how the host program can do the | 2833 | The following example shows how the host program can do the |
2831 | equivalent to this Lua code: | 2834 | equivalent to this Lua code: |
@@ -3971,7 +3974,8 @@ leaves the error object on the top of the stack, | |||
3971 | Starts and resumes a coroutine in the given thread @id{L}. | 3974 | Starts and resumes a coroutine in the given thread @id{L}. |
3972 | 3975 | ||
3973 | To start a coroutine, | 3976 | To start a coroutine, |
3974 | you push onto the thread stack the main function plus any arguments; | 3977 | you push the main function plus any arguments |
3978 | onto the empty stack of the thread. | ||
3975 | then you call @Lid{lua_resume}, | 3979 | then you call @Lid{lua_resume}, |
3976 | with @id{nargs} being the number of arguments. | 3980 | with @id{nargs} being the number of arguments. |
3977 | This call returns when the coroutine suspends or finishes its execution. | 3981 | This call returns when the coroutine suspends or finishes its execution. |
@@ -3988,8 +3992,9 @@ or an error code in case of errors @seeC{lua_pcall}. | |||
3988 | In case of errors, | 3992 | In case of errors, |
3989 | the error object is on the top of the stack. | 3993 | the error object is on the top of the stack. |
3990 | 3994 | ||
3991 | To resume a coroutine, you clear its stack, | 3995 | To resume a coroutine, |
3992 | push only the values to be passed as results from @id{yield}, | 3996 | you remove the @id{*nresults} yielded values from its stack, |
3997 | push the values to be passed as results from @id{yield}, | ||
3993 | and then call @Lid{lua_resume}. | 3998 | and then call @Lid{lua_resume}. |
3994 | 3999 | ||
3995 | The parameter @id{from} represents the coroutine that is resuming @id{L}. | 4000 | The parameter @id{from} represents the coroutine that is resuming @id{L}. |
@@ -4152,7 +4157,7 @@ and returns the total size of the string, | |||
4152 | that is, its length plus one. | 4157 | that is, its length plus one. |
4153 | The conversion can result in an integer or a float, | 4158 | The conversion can result in an integer or a float, |
4154 | according to the lexical conventions of Lua @see{lexical}. | 4159 | according to the lexical conventions of Lua @see{lexical}. |
4155 | The string may have leading and trailing spaces and a sign. | 4160 | The string may have leading and trailing whitespaces and a sign. |
4156 | If the string is not a valid numeral, | 4161 | If the string is not a valid numeral, |
4157 | returns 0 and pushes nothing. | 4162 | returns 0 and pushes nothing. |
4158 | (Note that the result can be used as a boolean, | 4163 | (Note that the result can be used as a boolean, |
@@ -5791,7 +5796,7 @@ The field @id{closef} points to a Lua function | |||
5791 | that will be called to close the stream | 5796 | that will be called to close the stream |
5792 | when the handle is closed or collected; | 5797 | when the handle is closed or collected; |
5793 | this function receives the file handle as its sole argument and | 5798 | this function receives the file handle as its sole argument and |
5794 | must return either @true, in case of success, | 5799 | must return either a true value, in case of success, |
5795 | or a false value plus an error message, in case of error. | 5800 | or a false value plus an error message, in case of error. |
5796 | Once Lua calls this field, | 5801 | Once Lua calls this field, |
5797 | it changes the field value to @id{NULL} | 5802 | it changes the field value to @id{NULL} |
@@ -5914,12 +5919,12 @@ to its expected parameters. | |||
5914 | For instance, a function documented as @T{foo(arg)} | 5919 | For instance, a function documented as @T{foo(arg)} |
5915 | should not be called without an argument. | 5920 | should not be called without an argument. |
5916 | 5921 | ||
5917 | The notation @fail means a return value representing | 5922 | The notation @fail means a false value representing |
5918 | some kind of failure or the absence of a better value to return. | 5923 | some kind of failure. |
5919 | Currently, @fail is equal to @nil, | 5924 | (Currently, @fail is equal to @nil, |
5920 | but that may change in future versions. | 5925 | but that may change in future versions. |
5921 | The recommendation is to always test the success of these functions | 5926 | The recommendation is to always test the success of these functions |
5922 | with @T{(not status)}, instead of @T{(status == nil)}. | 5927 | with @T{(not status)}, instead of @T{(status == nil)}.) |
5923 | 5928 | ||
5924 | 5929 | ||
5925 | Currently, Lua has the following standard libraries: | 5930 | Currently, Lua has the following standard libraries: |
@@ -6338,19 +6343,25 @@ the function returns @fail. | |||
6338 | } | 6343 | } |
6339 | 6344 | ||
6340 | @LibEntry{tostring (v)| | 6345 | @LibEntry{tostring (v)| |
6346 | |||
6341 | Receives a value of any type and | 6347 | Receives a value of any type and |
6342 | converts it to a string in a human-readable format. | 6348 | converts it to a string in a human-readable format. |
6343 | (For complete control of how numbers are converted, | ||
6344 | use @Lid{string.format}.) | ||
6345 | 6349 | ||
6346 | If the metatable of @id{v} has a @idx{__tostring} field, | 6350 | If the metatable of @id{v} has a @idx{__tostring} field, |
6347 | then @id{tostring} calls the corresponding value | 6351 | then @id{tostring} calls the corresponding value |
6348 | with @id{v} as argument, | 6352 | with @id{v} as argument, |
6349 | and uses the result of the call as its result. | 6353 | and uses the result of the call as its result. |
6354 | Otherwise, if the metatable of @id{v} has a @idx{__name} field | ||
6355 | with a string value, | ||
6356 | @id{tostring} may use that string in its final result. | ||
6357 | |||
6358 | For complete control of how numbers are converted, | ||
6359 | use @Lid{string.format}. | ||
6350 | 6360 | ||
6351 | } | 6361 | } |
6352 | 6362 | ||
6353 | @LibEntry{type (v)| | 6363 | @LibEntry{type (v)| |
6364 | |||
6354 | Returns the type of its only argument, coded as a string. | 6365 | Returns the type of its only argument, coded as a string. |
6355 | The possible results of this function are | 6366 | The possible results of this function are |
6356 | @St{nil} (a string, not the value @nil), | 6367 | @St{nil} (a string, not the value @nil), |
@@ -6599,7 +6610,8 @@ Default is @Char{-}.} | |||
6599 | 6610 | ||
6600 | @LibEntry{package.cpath| | 6611 | @LibEntry{package.cpath| |
6601 | 6612 | ||
6602 | The path used by @Lid{require} to search for a @N{C loader}. | 6613 | A string with the path used by @Lid{require} |
6614 | to search for a @N{C loader}. | ||
6603 | 6615 | ||
6604 | Lua initializes the @N{C path} @Lid{package.cpath} in the same way | 6616 | Lua initializes the @N{C path} @Lid{package.cpath} in the same way |
6605 | it initializes the Lua path @Lid{package.path}, | 6617 | it initializes the Lua path @Lid{package.path}, |
@@ -6656,7 +6668,8 @@ plus other Unix systems that support the @id{dlfcn} standard). | |||
6656 | 6668 | ||
6657 | @LibEntry{package.path| | 6669 | @LibEntry{package.path| |
6658 | 6670 | ||
6659 | The path used by @Lid{require} to search for a Lua loader. | 6671 | A string with the path used by @Lid{require} |
6672 | to search for a Lua loader. | ||
6660 | 6673 | ||
6661 | At start-up, Lua initializes this variable with | 6674 | At start-up, Lua initializes this variable with |
6662 | the value of the environment variable @defid{LUA_PATH_5_4} or | 6675 | the value of the environment variable @defid{LUA_PATH_5_4} or |
@@ -7397,7 +7410,7 @@ coded as an unsigned integer with @id{n} bytes | |||
7397 | @item{@T{X@rep{op}}|an empty item that aligns | 7410 | @item{@T{X@rep{op}}|an empty item that aligns |
7398 | according to option @id{op} | 7411 | according to option @id{op} |
7399 | (which is otherwise ignored)} | 7412 | (which is otherwise ignored)} |
7400 | @item{@Char{ }|(empty space) ignored} | 7413 | @item{@Char{ }|(space) ignored} |
7401 | } | 7414 | } |
7402 | (A @St{[@rep{n}]} means an optional integral numeral.) | 7415 | (A @St{[@rep{n}]} means an optional integral numeral.) |
7403 | Except for padding, spaces, and configurations | 7416 | Except for padding, spaces, and configurations |
@@ -8106,7 +8119,7 @@ The available formats are | |||
8106 | @item{@St{n}| | 8119 | @item{@St{n}| |
8107 | reads a numeral and returns it as a float or an integer, | 8120 | reads a numeral and returns it as a float or an integer, |
8108 | following the lexical conventions of Lua. | 8121 | following the lexical conventions of Lua. |
8109 | (The numeral may have leading spaces and a sign.) | 8122 | (The numeral may have leading whitespaces and a sign.) |
8110 | This format always reads the longest input sequence that | 8123 | This format always reads the longest input sequence that |
8111 | is a valid prefix for a numeral; | 8124 | is a valid prefix for a numeral; |
8112 | if that prefix does not form a valid numeral | 8125 | if that prefix does not form a valid numeral |
@@ -8594,7 +8607,7 @@ This function has the following restrictions: | |||
8594 | @item{@id{limit} cannot be less than the amount of C stack in use.} | 8607 | @item{@id{limit} cannot be less than the amount of C stack in use.} |
8595 | } | 8608 | } |
8596 | If a call does not respect some restriction, | 8609 | If a call does not respect some restriction, |
8597 | it returns a falsy value. | 8610 | it returns a false value. |
8598 | Otherwise, | 8611 | Otherwise, |
8599 | the call returns the old limit. | 8612 | the call returns the old limit. |
8600 | 8613 | ||
@@ -8736,15 +8749,15 @@ lua [options] [script [args]] | |||
8736 | } | 8749 | } |
8737 | The options are: | 8750 | The options are: |
8738 | @description{ | 8751 | @description{ |
8739 | @item{@T{-e @rep{stat}}| executes string @rep{stat};} | 8752 | @item{@T{-e @rep{stat}}| execute string @rep{stat};} |
8740 | @item{@T{-l @rep{mod}}| @Q{requires} @rep{mod} and assigns the | 8753 | @item{@T{-i}| enter interactive mode after running @rep{script};} |
8754 | @item{@T{-l @rep{mod}}| @Q{require} @rep{mod} and assign the | ||
8741 | result to global @rep{mod};} | 8755 | result to global @rep{mod};} |
8742 | @item{@T{-i}| enters interactive mode after running @rep{script};} | 8756 | @item{@T{-v}| print version information;} |
8743 | @item{@T{-v}| prints version information;} | 8757 | @item{@T{-E}| ignore environment variables;} |
8744 | @item{@T{-E}| ignores environment variables;} | ||
8745 | @item{@T{-W}| turn warnings on;} | 8758 | @item{@T{-W}| turn warnings on;} |
8746 | @item{@T{--}| stops handling options;} | 8759 | @item{@T{--}| stop handling options;} |
8747 | @item{@T{-}| executes @id{stdin} as a file and stops handling options.} | 8760 | @item{@T{-}| execute @id{stdin} as a file and stop handling options.} |
8748 | } | 8761 | } |
8749 | After handling its options, @id{lua} runs the given @emph{script}. | 8762 | After handling its options, @id{lua} runs the given @emph{script}. |
8750 | When called without arguments, | 8763 | When called without arguments, |
@@ -8761,12 +8774,10 @@ then @id{lua} executes the file. | |||
8761 | Otherwise, @id{lua} executes the string itself. | 8774 | Otherwise, @id{lua} executes the string itself. |
8762 | 8775 | ||
8763 | When called with the option @T{-E}, | 8776 | When called with the option @T{-E}, |
8764 | besides ignoring @id{LUA_INIT}, | 8777 | Lua does not consult any environment variables. |
8765 | Lua also ignores | 8778 | In particular, |
8766 | the values of @id{LUA_PATH} and @id{LUA_CPATH}, | 8779 | the values of @Lid{package.path} and @Lid{package.cpath} |
8767 | setting the values of | 8780 | are set with the default paths defined in @id{luaconf.h}. |
8768 | @Lid{package.path} and @Lid{package.cpath} | ||
8769 | with the default paths defined in @id{luaconf.h}. | ||
8770 | 8781 | ||
8771 | The options @T{-e}, @T{-l}, and @T{-W} are handled in | 8782 | The options @T{-e}, @T{-l}, and @T{-W} are handled in |
8772 | the order they appear. | 8783 | the order they appear. |