aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2022-08-19 14:10:18 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2022-08-19 14:10:18 -0300
commitc6cea857a4845940c833e39a149d20bb64a9af85 (patch)
tree9cae6b73bcd5be5fa7feb01130d903cb4dbf6d4e
parentd61b0c60287c38008d312ddd11724a15b1737f7b (diff)
downloadlua-c6cea857a4845940c833e39a149d20bb64a9af85.tar.gz
lua-c6cea857a4845940c833e39a149d20bb64a9af85.tar.bz2
lua-c6cea857a4845940c833e39a149d20bb64a9af85.zip
Better documentation for 'multires' expressions
Manual has a new section explaining multires expressions, lists of expressions, and adjustments. This commit also corrects some comments in the code.
-rw-r--r--lauxlib.c5
-rw-r--r--lfunc.c2
-rw-r--r--manual/manual.of188
3 files changed, 120 insertions, 75 deletions
diff --git a/lauxlib.c b/lauxlib.c
index 8ed1da11..413d8f97 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -526,7 +526,8 @@ static void newbox (lua_State *L) {
526 526
527/* 527/*
528** Compute new size for buffer 'B', enough to accommodate extra 'sz' 528** Compute new size for buffer 'B', enough to accommodate extra 'sz'
529** bytes. 529** bytes. (The test for "double is not big enough" also gets the
530** case when the multiplication by 2 overflows.)
530*/ 531*/
531static size_t newbuffsize (luaL_Buffer *B, size_t sz) { 532static size_t newbuffsize (luaL_Buffer *B, size_t sz) {
532 size_t newsize = B->size * 2; /* double buffer size */ 533 size_t newsize = B->size * 2; /* double buffer size */
@@ -611,7 +612,7 @@ LUALIB_API void luaL_pushresultsize (luaL_Buffer *B, size_t sz) {
611** box (if existent) is not on the top of the stack. So, instead of 612** box (if existent) is not on the top of the stack. So, instead of
612** calling 'luaL_addlstring', it replicates the code using -2 as the 613** calling 'luaL_addlstring', it replicates the code using -2 as the
613** last argument to 'prepbuffsize', signaling that the box is (or will 614** last argument to 'prepbuffsize', signaling that the box is (or will
614** be) bellow the string being added to the buffer. (Box creation can 615** be) below the string being added to the buffer. (Box creation can
615** trigger an emergency GC, so we should not remove the string from the 616** trigger an emergency GC, so we should not remove the string from the
616** stack before we have the space guaranteed.) 617** stack before we have the space guaranteed.)
617*/ 618*/
diff --git a/lfunc.c b/lfunc.c
index 3ed65de2..daba0abf 100644
--- a/lfunc.c
+++ b/lfunc.c
@@ -209,7 +209,7 @@ void luaF_closeupval (lua_State *L, StkId level) {
209 209
210 210
211/* 211/*
212** Remove firt element from the tbclist plus its dummy nodes. 212** Remove first element from the tbclist plus its dummy nodes.
213*/ 213*/
214static void poptbclist (lua_State *L) { 214static void poptbclist (lua_State *L) {
215 StkId tbc = L->tbclist; 215 StkId tbc = L->tbclist;
diff --git a/manual/manual.of b/manual/manual.of
index 30f92d60..ca7f9933 100644
--- a/manual/manual.of
+++ b/manual/manual.of
@@ -1333,19 +1333,11 @@ Expressions are discussed in @See{expressions}.
1333 1333
1334Before the assignment, 1334Before the assignment,
1335the list of values is @emph{adjusted} to the length of 1335the list of values is @emph{adjusted} to the length of
1336the list of variables.@index{adjustment} 1336the list of variables @see{multires}.
1337If there are more values than needed,
1338the excess values are thrown away.
1339If there are fewer values than needed,
1340the list is extended with @nil's.
1341If the list of expressions ends with a function call,
1342then all values returned by that call enter the list of values,
1343before the adjustment
1344(except when the call is enclosed in parentheses; see @See{expressions}).
1345 1337
1346If a variable is both assigned and read 1338If a variable is both assigned and read
1347inside a multiple assignment, 1339inside a multiple assignment,
1348Lua ensures all reads get the value of the variable 1340Lua ensures that all reads get the value of the variable
1349before the assignment. 1341before the assignment.
1350Thus the code 1342Thus the code
1351@verbatim{ 1343@verbatim{
@@ -1684,9 +1676,10 @@ function calls are explained in @See{functioncall};
1684table constructors are explained in @See{tableconstructor}. 1676table constructors are explained in @See{tableconstructor}.
1685Vararg expressions, 1677Vararg expressions,
1686denoted by three dots (@Char{...}), can only be used when 1678denoted by three dots (@Char{...}), can only be used when
1687directly inside a vararg function; 1679directly inside a variadic function;
1688they are explained in @See{func-def}. 1680they are explained in @See{func-def}.
1689 1681
1682
1690Binary operators comprise arithmetic operators @see{arith}, 1683Binary operators comprise arithmetic operators @see{arith},
1691bitwise operators @see{bitwise}, 1684bitwise operators @see{bitwise},
1692relational operators @see{rel-ops}, logical operators @see{logic}, 1685relational operators @see{rel-ops}, logical operators @see{logic},
@@ -1696,47 +1689,8 @@ the unary bitwise NOT @see{bitwise},
1696the unary logical @Rw{not} @see{logic}, 1689the unary logical @Rw{not} @see{logic},
1697and the unary @def{length operator} @see{len-op}. 1690and the unary @def{length operator} @see{len-op}.
1698 1691
1699Both function calls and vararg expressions can result in multiple values.
1700If a function call is used as a statement @see{funcstat},
1701then its return list is adjusted to zero elements,
1702thus discarding all returned values.
1703If an expression is used as the last (or the only) element
1704of a list of expressions,
1705then no adjustment is made
1706(unless the expression is enclosed in parentheses).
1707In all other contexts,
1708Lua adjusts the result list to one element,
1709either discarding all values except the first one
1710or adding a single @nil if there are no values.
1711
1712Here are some examples:
1713@verbatim{
1714f() -- adjusted to 0 results
1715g(f(), x) -- f() is adjusted to 1 result
1716g(x, f()) -- g gets x plus all results from f()
1717a,b,c = f(), x -- f() is adjusted to 1 result (c gets nil)
1718a,b = ... -- a gets the first vararg argument, b gets
1719 -- the second (both a and b can get nil if there
1720 -- is no corresponding vararg argument)
1721
1722a,b,c = x, f() -- f() is adjusted to 2 results
1723a,b,c = f() -- f() is adjusted to 3 results
1724return f() -- returns all results from f()
1725return ... -- returns all received vararg arguments
1726return x,y,f() -- returns x, y, and all results from f()
1727{f()} -- creates a list with all results from f()
1728{...} -- creates a list with all vararg arguments
1729{f(), nil} -- f() is adjusted to 1 result
1730} 1692}
1731 1693
1732Any expression enclosed in parentheses always results in only one value.
1733Thus,
1734@T{(f(x,y,z))} is always a single value,
1735even if @id{f} returns several values.
1736(The value of @T{(f(x,y,z))} is the first value returned by @id{f}
1737or @nil if @id{f} does not return any values.)
1738
1739}
1740 1694
1741 1695
1742@sect3{arith| @title{Arithmetic Operators} 1696@sect3{arith| @title{Arithmetic Operators}
@@ -1843,8 +1797,9 @@ the library calls the metamethod of the other operand
1843(if present) or it raises an error. 1797(if present) or it raises an error.
1844Note that bitwise operators do not do this coercion. 1798Note that bitwise operators do not do this coercion.
1845 1799
1846Nonetheless, it is always a good practice not to rely on these 1800It is always a good practice not to rely on the
1847implicit coercions, as they are not always applied; 1801implicit coercions from strings to numbers,
1802as they are not always applied;
1848in particular, @T{"1"==1} is false and @T{"1"<1} raises an error 1803in particular, @T{"1"==1} is false and @T{"1"<1} raises an error
1849@see{rel-ops}. 1804@see{rel-ops}.
1850These coercions exist mainly for compatibility and may be removed 1805These coercions exist mainly for compatibility and may be removed
@@ -2095,9 +2050,9 @@ The order of the assignments in a constructor is undefined.
2095(This order would be relevant only when there are repeated keys.) 2050(This order would be relevant only when there are repeated keys.)
2096 2051
2097If the last field in the list has the form @id{exp} 2052If the last field in the list has the form @id{exp}
2098and the expression is a function call or a vararg expression, 2053and the expression is a multires expression,
2099then all values returned by this expression enter the list consecutively 2054then all values returned by this expression enter the list consecutively
2100@see{functioncall}. 2055@see{multires}.
2101 2056
2102The field list can have an optional trailing separator, 2057The field list can have an optional trailing separator,
2103as a convenience for machine-generated code. 2058as a convenience for machine-generated code.
@@ -2148,7 +2103,7 @@ A call of the form @T{return @rep{functioncall}} not in the
2148scope of a to-be-closed variable is called a @def{tail call}. 2103scope of a to-be-closed variable is called a @def{tail call}.
2149Lua implements @def{proper tail calls} 2104Lua implements @def{proper tail calls}
2150(or @def{proper tail recursion}): 2105(or @def{proper tail recursion}):
2151in a tail call, 2106In a tail call,
2152the called function reuses the stack entry of the calling function. 2107the called function reuses the stack entry of the calling function.
2153Therefore, there is no limit on the number of nested tail calls that 2108Therefore, there is no limit on the number of nested tail calls that
2154a program can execute. 2109a program can execute.
@@ -2234,22 +2189,16 @@ initialized with the argument values:
2234} 2189}
2235When a Lua function is called, 2190When a Lua function is called,
2236it adjusts its list of @x{arguments} to 2191it adjusts its list of @x{arguments} to
2237the length of its list of parameters, 2192the length of its list of parameters @see{multires},
2238unless the function is a @def{vararg function}, 2193unless the function is a @def{variadic function},
2239which is indicated by three dots (@Char{...}) 2194which is indicated by three dots (@Char{...})
2240at the end of its parameter list. 2195at the end of its parameter list.
2241A vararg function does not adjust its argument list; 2196A variadic function does not adjust its argument list;
2242instead, it collects all extra arguments and supplies them 2197instead, it collects all extra arguments and supplies them
2243to the function through a @def{vararg expression}, 2198to the function through a @def{vararg expression},
2244which is also written as three dots. 2199which is also written as three dots.
2245The value of this expression is a list of all actual extra arguments, 2200The value of this expression is a list of all actual extra arguments,
2246similar to a function with multiple results. 2201similar to a function with multiple results @see{multires}.
2247If a vararg expression is used inside another expression
2248or in the middle of a list of expressions,
2249then its return list is adjusted to one element.
2250If the expression is used as the last element of a list of expressions,
2251then no adjustment is made
2252(unless that last expression is enclosed in parentheses).
2253 2202
2254 2203
2255As an example, consider the following definitions: 2204As an example, consider the following definitions:
@@ -2299,6 +2248,99 @@ t.a.b.c.f = function (self, @rep{params}) @rep{body} end
2299 2248
2300} 2249}
2301 2250
2251@sect3{multires| @title{Lists of expressions, multiple results,
2252and adjustment}
2253
2254Both function calls and vararg expressions can result in multiple values.
2255These expressions are called @def{multires expressions}.
2256
2257When a multires expression is used as the last element
2258of a list of expressions,
2259all results from the expression are added to the
2260list of values produced by the list of expressions.
2261Note that a single expression
2262in a place that expects a list of expressions
2263is the last expression in that (singleton) list.
2264
2265These are the places where Lua expects a list of expressions:
2266@description{
2267
2268@item{A @rw{return} statement,
2269for instance @T{return e1, e2, e3} @see{control}.}
2270
2271@item{A table constructor,
2272for instance @T{{e1, e2, e3}} @see{tableconstructor}.}
2273
2274@item{The arguments of a function call,
2275for instance @T{foo(e1, e2, e3)} @see{functioncall}.}
2276
2277@item{A multiple assignment,
2278for instance @T{a , b, c = e1, e2, e3} @see{assignment}.}
2279
2280@item{A local declaration,
2281for instance @T{local a , b, c = e1, e2, e3} @see{localvar}.}
2282
2283@item{The initial values in a generic @rw{for} loop,
2284for instance @T{for k in e1, e2, e3 do ... end} @see{for}.}
2285
2286}
2287In the last four cases,
2288the list of values from the list of expressions
2289must be @emph{adjusted} to a specific length:
2290the number of parameters in a call to a non-variadic function
2291@see{func-def},
2292the number of variables in a multiple assignment or
2293a local declaration,
2294and exactly four for a generic @rw{for} loop.
2295The @def{adjustment} follows these rules:
2296If there are more values than needed,
2297the extra values are thrown away;
2298if there are fewer values than needed,
2299the list is extended with @nil's.
2300When the list of expressions ends with a multires expression,
2301all results from that expression enter the list of values
2302before the adjustment.
2303
2304When a multires expression is used
2305in a list of expressions without being the last element,
2306or in a place where the syntax expects a single expression,
2307Lua adjusts the result list of that expression to one element.
2308As a particular case,
2309the syntax expects a single expression inside a parenthesized expression;
2310therefore, adding parentheses around a multires expression
2311forces it to produce exactly one result.
2312
2313Here are some examples.
2314In all cases, when the construction needs
2315@Q{the n-th result} and there is no such result,
2316it uses a @nil.
2317@verbatim{
2318print(x, f()) -- prints x and all results from f().
2319print(x, (f())) -- prints x and the first result from f().
2320print(f(), x) -- prints the first result from f() and x.
2321print(1 + f()) -- prints 1 added to the first result from f().
2322x,y = ... -- x gets the first vararg argument,
2323 -- y gets the second vararg argument.
2324x,y,z = w, f() -- x gets w, y gets the first result from f(),
2325 -- z gets the second result from f().
2326x,y,z = f() -- x gets the first result from f(),
2327 -- y gets the second result from f(),
2328 -- z gets the third result from f().
2329x,y,z = f(), g() -- x gets the first result from f(),
2330 -- y gets the first result from g(),
2331 -- z gets the second result from g().
2332x,y,z = (f()) -- x gets the first result from f(), y and z get nil.
2333return f() -- returns all results from f().
2334return ... -- returns all received vararg arguments.
2335return (...) -- returns the first received vararg argument.
2336return x,y,f() -- returns x, y, and all results from f().
2337{f()} -- creates a list with all results from f().
2338{...} -- creates a list with all vararg arguments.
2339{f(), 5} -- creates a list with the first result from f() and 5.
2340}
2341
2342}
2343
2302} 2344}
2303 2345
2304@sect2{visibility| @title{Visibility Rules} 2346@sect2{visibility| @title{Visibility Rules}
@@ -4780,7 +4822,7 @@ the number of parameters of the function
4780} 4822}
4781 4823
4782@item{@id{isvararg}| 4824@item{@id{isvararg}|
4783true if the function is a vararg function 4825true if the function is a variadic function
4784(always true for @N{C functions}). 4826(always true for @N{C functions}).
4785} 4827}
4786 4828
@@ -6017,9 +6059,7 @@ to start the traceback.
6017 6059
6018} 6060}
6019 6061
6020@APIEntry{const char *luaL_typeerror (lua_State *L, 6062@APIEntry{int luaL_typeerror (lua_State *L, int arg, const char *tname);|
6021 int arg,
6022 const char *tname);|
6023@apii{0,0,v} 6063@apii{0,0,v}
6024 6064
6025Raises a type error for the argument @id{arg} 6065Raises a type error for the argument @id{arg}
@@ -6816,6 +6856,8 @@ When you require a module @id{modname} and
6816This variable is only a reference to the real table; 6856This variable is only a reference to the real table;
6817assignments to this variable do not change the 6857assignments to this variable do not change the
6818table used by @Lid{require}. 6858table used by @Lid{require}.
6859The real table is stored in the C registry @see{registry},
6860indexed by the key @defid{LUA_LOADED_TABLE}, a string.
6819 6861
6820} 6862}
6821 6863
@@ -6883,6 +6925,8 @@ A table to store loaders for specific modules
6883This variable is only a reference to the real table; 6925This variable is only a reference to the real table;
6884assignments to this variable do not change the 6926assignments to this variable do not change the
6885table used by @Lid{require}. 6927table used by @Lid{require}.
6928The real table is stored in the C registry @see{registry},
6929indexed by the key @defid{LUA_PRELOAD_TABLE}, a string.
6886 6930
6887} 6931}
6888 6932
@@ -7904,9 +7948,9 @@ Returns the arc sine of @id{x} (in radians).
7904 7948
7905@LibEntry{math.atan (y [, x])| 7949@LibEntry{math.atan (y [, x])|
7906 7950
7907@index{atan2} 7951@index{atan} @index{atan2}
7908Returns the arc tangent of @T{y/x} (in radians), 7952Returns the arc tangent of @T{y/x} (in radians),
7909but uses the signs of both arguments to find the 7953using the signs of both arguments to find the
7910quadrant of the result. 7954quadrant of the result.
7911It also handles correctly the case of @id{x} being zero. 7955It also handles correctly the case of @id{x} being zero.
7912 7956
@@ -8997,7 +9041,7 @@ If there is a script,
8997the script is called with arguments 9041the script is called with arguments
8998@T{arg[1]}, @Cdots, @T{arg[#arg]}. 9042@T{arg[1]}, @Cdots, @T{arg[#arg]}.
8999Like all chunks in Lua, 9043Like all chunks in Lua,
9000the script is compiled as a vararg function. 9044the script is compiled as a variadic function.
9001 9045
9002In interactive mode, 9046In interactive mode,
9003Lua repeatedly prompts and waits for a line. 9047Lua repeatedly prompts and waits for a line.