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