aboutsummaryrefslogtreecommitdiff
path: root/manual
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-03-19 10:53:18 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-03-19 10:53:18 -0300
commit9b37a4695ebf50b37b5b4fb279ae948f23b5b6a0 (patch)
tree2a6b0f6c1c2eb962bb383175eb0a67ea81a4564d /manual
parent1e0c73d5b643707335b06abd2546a83d9439d14c (diff)
downloadlua-9b37a4695ebf50b37b5b4fb279ae948f23b5b6a0.tar.gz
lua-9b37a4695ebf50b37b5b4fb279ae948f23b5b6a0.tar.bz2
lua-9b37a4695ebf50b37b5b4fb279ae948f23b5b6a0.zip
New semantics for the integer 'for' loop
The numerical 'for' loop over integers now uses a precomputed counter to control its number of iteractions. This change eliminates several weird cases caused by overflows (wrap-around) in the control variable. (It also ensures that every integer loop halts.) Also, the special opcodes for the usual case of step==1 were removed. (The new code is already somewhat complex for the usual case, but efficient.)
Diffstat (limited to 'manual')
-rw-r--r--manual/manual.of124
1 files changed, 54 insertions, 70 deletions
diff --git a/manual/manual.of b/manual/manual.of
index 8a8ebad5..b7ced443 100644
--- a/manual/manual.of
+++ b/manual/manual.of
@@ -594,7 +594,7 @@ controls how long the collector waits before starting a new cycle.
594The collector starts a new cycle when the use of memory 594The collector starts a new cycle when the use of memory
595hits @M{n%} of the use after the previous collection. 595hits @M{n%} of the use after the previous collection.
596Larger values make the collector less aggressive. 596Larger values make the collector less aggressive.
597Values smaller than 100 mean the collector will not wait to 597Values less than 100 mean the collector will not wait to
598start a new cycle. 598start a new cycle.
599A value of 200 means that the collector waits for the total memory in use 599A value of 200 means that the collector waits for the total memory in use
600to double before starting a new cycle. 600to double before starting a new cycle.
@@ -608,7 +608,7 @@ how many elements it marks or sweeps for each
608kilobyte of memory allocated. 608kilobyte of memory allocated.
609Larger values make the collector more aggressive but also increase 609Larger values make the collector more aggressive but also increase
610the size of each incremental step. 610the size of each incremental step.
611You should not use values smaller than 100, 611You should not use values less than 100,
612because they make the collector too slow and 612because they make the collector too slow and
613can result in the collector never finishing a cycle. 613can result in the collector never finishing a cycle.
614The default value is 100; the maximum value is 1000. 614The default value is 100; the maximum value is 1000.
@@ -1004,7 +1004,7 @@ the escape sequence @T{\u{@rep{XXX}}}
1004(note the mandatory enclosing brackets), 1004(note the mandatory enclosing brackets),
1005where @rep{XXX} is a sequence of one or more hexadecimal digits 1005where @rep{XXX} is a sequence of one or more hexadecimal digits
1006representing the character code point. 1006representing the character code point.
1007This code point can be any value smaller than @M{2@sp{31}}. 1007This code point can be any value less than @M{2@sp{31}}.
1008(Lua uses the original UTF-8 specification here.) 1008(Lua uses the original UTF-8 specification here.)
1009 1009
1010Literal strings can also be defined using a long format 1010Literal strings can also be defined using a long format
@@ -1370,73 +1370,49 @@ because now @Rw{return} is the last statement in its (inner) block.
1370The @Rw{for} statement has two forms: 1370The @Rw{for} statement has two forms:
1371one numerical and one generic. 1371one numerical and one generic.
1372 1372
1373@sect4{@title{The numerical @Rw{for} loop}
1374
1373The numerical @Rw{for} loop repeats a block of code while a 1375The numerical @Rw{for} loop repeats a block of code while a
1374control variable runs through an arithmetic progression. 1376control variable goes through an arithmetic progression.
1375It has the following syntax: 1377It has the following syntax:
1376@Produc{ 1378@Produc{
1377@producname{stat}@producbody{@Rw{for} @bnfNter{Name} @bnfter{=} 1379@producname{stat}@producbody{@Rw{for} @bnfNter{Name} @bnfter{=}
1378 exp @bnfter{,} exp @bnfopt{@bnfter{,} exp} @Rw{do} block @Rw{end}} 1380 exp @bnfter{,} exp @bnfopt{@bnfter{,} exp} @Rw{do} block @Rw{end}}
1379} 1381}
1380The @emph{block} is repeated for @emph{name} starting at the value of 1382The given identifier (@bnfNter{Name}) defines the control variable,
1381the first @emph{exp}, until it passes the second @emph{exp} by steps of the 1383which is local to the loop body (@emph{block}).
1382third @emph{exp}. 1384
1383More precisely, a @Rw{for} statement like 1385The loop starts by evaluating once the three control expressions;
1384@verbatim{ 1386they must all result in numbers.
1385for v = @rep{e1}, @rep{e2}, @rep{e3} do @rep{block} end 1387Their values are called respectively
1386} 1388the @emph{initial value}, the @emph{limit}, and the @emph{step}.
1387is equivalent to the code: 1389If the step is absent, it defaults @N{to 1}.
1388@verbatim{ 1390Then the loop body is repeated with the value of the control variable
1389do 1391going through an arithmetic progression,
1390 local @rep{var}, @rep{limit}, @rep{step} = tonumber(@rep{e1}), tonumber(@rep{e2}), tonumber(@rep{e3}) 1392starting at the initial value,
1391 if not (@rep{var} and @rep{limit} and @rep{step}) then error() end 1393with a common difference given by the step,
1392 @rep{var} = @rep{var} - @rep{step} 1394until that value passes the limit.
1393 while true do 1395A negative step makes a decreasing sequence;
1394 @rep{var} = @rep{var} + @rep{step} 1396a step equal to zero raises an error.
1395 if (@rep{step} >= 0 and @rep{var} > @rep{limit}) or (@rep{step} < 0 and @rep{var} < @rep{limit}) then 1397If the initial value is already greater than the limit
1396 break 1398(or less than, if the step is negative), the body is not executed.
1397 end 1399
1398 local v = @rep{var} 1400If both the initial value and the step are integers,
1399 @rep{block} 1401the loop is done with integers;
1400 end 1402in this case, the range of the control variable is limited
1401end 1403by the range of integers.
1402} 1404Otherwise, the loop is done with floats.
1403 1405(Beware of floating-point accuracy in this case.)
1404Note the following: 1406
1405@itemize{ 1407You should not change the value of the control variable
1406 1408during the loop.
1407@item{
1408All three control expressions are evaluated only once,
1409before the loop starts.
1410They must all result in numbers.
1411}
1412
1413@item{
1414@T{@rep{var}}, @T{@rep{limit}}, and @T{@rep{step}} are invisible variables.
1415The names shown here are for explanatory purposes only.
1416}
1417
1418@item{
1419If the third expression (the step) is absent,
1420then a step @N{of 1} is used.
1421}
1422
1423@item{
1424You can use @Rw{break} and @Rw{goto} to exit a @Rw{for} loop.
1425}
1426
1427@item{
1428The loop variable @T{v} is local to the loop body.
1429If you need its value after the loop, 1409If you need its value after the loop,
1430assign it to another variable before exiting the loop. 1410assign it to another variable before exiting the loop.
1431}
1432 1411
1433@item{
1434The values in @rep{var}, @rep{limit}, and @rep{step}
1435can be integers or floats.
1436All operations on them respect the usual rules in Lua.
1437} 1412}
1438 1413
1439} 1414@sect4{@title{The generic @Rw{for} loop}
1415
1440 1416
1441The generic @Rw{for} statement works over functions, 1417The generic @Rw{for} statement works over functions,
1442called @def{iterators}. 1418called @def{iterators}.
@@ -1499,6 +1475,8 @@ then assign them to other variables before breaking or exiting the loop.
1499 1475
1500} 1476}
1501 1477
1478}
1479
1502@sect3{funcstat| @title{Function Calls as Statements} 1480@sect3{funcstat| @title{Function Calls as Statements}
1503To allow possible side-effects, 1481To allow possible side-effects,
1504function calls can be executed as statements: 1482function calls can be executed as statements:
@@ -1819,7 +1797,7 @@ A comparison @T{a > b} is translated to @T{b < a}
1819and @T{a >= b} is translated to @T{b <= a}. 1797and @T{a >= b} is translated to @T{b <= a}.
1820 1798
1821Following the @x{IEEE 754} standard, 1799Following the @x{IEEE 754} standard,
1822@x{NaN} is considered neither smaller than, 1800@x{NaN} is considered neither less than,
1823nor equal to, nor greater than any value (including itself). 1801nor equal to, nor greater than any value (including itself).
1824 1802
1825} 1803}
@@ -2171,7 +2149,7 @@ then the function returns with no results.
2171@index{multiple return} 2149@index{multiple return}
2172There is a system-dependent limit on the number of values 2150There is a system-dependent limit on the number of values
2173that a function may return. 2151that a function may return.
2174This limit is guaranteed to be larger than 1000. 2152This limit is guaranteed to be greater than 1000.
2175 2153
2176The @emphx{colon} syntax 2154The @emphx{colon} syntax
2177is used for defining @def{methods}, 2155is used for defining @def{methods},
@@ -2367,7 +2345,7 @@ but it also can be any positive index after the stack top
2367within the space allocated for the stack, 2345within the space allocated for the stack,
2368that is, indices up to the stack size. 2346that is, indices up to the stack size.
2369(Note that 0 is never an acceptable index.) 2347(Note that 0 is never an acceptable index.)
2370Indices to upvalues @see{c-closure} larger than the real number 2348Indices to upvalues @see{c-closure} greater than the real number
2371of upvalues in the current @N{C function} are also acceptable (but invalid). 2349of upvalues in the current @N{C function} are also acceptable (but invalid).
2372Except when noted otherwise, 2350Except when noted otherwise,
2373functions in the API work with acceptable indices. 2351functions in the API work with acceptable indices.
@@ -2879,7 +2857,7 @@ Ensures that the stack has space for at least @id{n} extra slots
2879(that is, that you can safely push up to @id{n} values into it). 2857(that is, that you can safely push up to @id{n} values into it).
2880It returns false if it cannot fulfill the request, 2858It returns false if it cannot fulfill the request,
2881either because it would cause the stack 2859either because it would cause the stack
2882to be larger than a fixed maximum size 2860to be greater than a fixed maximum size
2883(typically at least several thousand elements) or 2861(typically at least several thousand elements) or
2884because it cannot allocate memory for the extra space. 2862because it cannot allocate memory for the extra space.
2885This function never shrinks the stack; 2863This function never shrinks the stack;
@@ -4053,7 +4031,7 @@ for the @Q{newindex} event @see{metatable}.
4053 4031
4054Accepts any index, @N{or 0}, 4032Accepts any index, @N{or 0},
4055and sets the stack top to this index. 4033and sets the stack top to this index.
4056If the new top is larger than the old one, 4034If the new top is greater than the old one,
4057then the new elements are filled with @nil. 4035then the new elements are filled with @nil.
4058If @id{index} @N{is 0}, then all stack elements are removed. 4036If @id{index} @N{is 0}, then all stack elements are removed.
4059 4037
@@ -5056,7 +5034,7 @@ size @id{sz} with a call @T{luaL_buffinitsize(L, &b, sz)}.}
5056@item{ 5034@item{
5057Finish by calling @T{luaL_pushresultsize(&b, sz)}, 5035Finish by calling @T{luaL_pushresultsize(&b, sz)},
5058where @id{sz} is the total size of the resulting string 5036where @id{sz} is the total size of the resulting string
5059copied into that space (which may be smaller than or 5037copied into that space (which may be less than or
5060equal to the preallocated size). 5038equal to the preallocated size).
5061} 5039}
5062 5040
@@ -7336,7 +7314,7 @@ Functions that interpret byte sequences only accept
7336valid sequences (well formed and not overlong). 7314valid sequences (well formed and not overlong).
7337By default, they only accept byte sequences 7315By default, they only accept byte sequences
7338that result in valid Unicode code points, 7316that result in valid Unicode code points,
7339rejecting values larger than @T{10FFFF} and surrogates. 7317rejecting values greater than @T{10FFFF} and surrogates.
7340A boolean argument @id{nonstrict}, when available, 7318A boolean argument @id{nonstrict}, when available,
7341lifts these checks, 7319lifts these checks,
7342so that all values up to @T{0x7FFFFFFF} are accepted. 7320so that all values up to @T{0x7FFFFFFF} are accepted.
@@ -7572,7 +7550,7 @@ returns the arc tangent of @id{y}.
7572 7550
7573@LibEntry{math.ceil (x)| 7551@LibEntry{math.ceil (x)|
7574 7552
7575Returns the smallest integral value larger than or equal to @id{x}. 7553Returns the smallest integral value greater than or equal to @id{x}.
7576 7554
7577} 7555}
7578 7556
@@ -7597,7 +7575,7 @@ Returns the value @M{e@sp{x}}
7597 7575
7598@LibEntry{math.floor (x)| 7576@LibEntry{math.floor (x)|
7599 7577
7600Returns the largest integral value smaller than or equal to @id{x}. 7578Returns the largest integral value less than or equal to @id{x}.
7601 7579
7602} 7580}
7603 7581
@@ -7611,7 +7589,7 @@ that rounds the quotient towards zero. (integer/float)
7611@LibEntry{math.huge| 7589@LibEntry{math.huge|
7612 7590
7613The float value @idx{HUGE_VAL}, 7591The float value @idx{HUGE_VAL},
7614a value larger than any other numeric value. 7592a value greater than any other numeric value.
7615 7593
7616} 7594}
7617 7595
@@ -8352,7 +8330,7 @@ of the given thread:
8352@N{level 1} is the function that called @id{getinfo} 8330@N{level 1} is the function that called @id{getinfo}
8353(except for tail calls, which do not count on the stack); 8331(except for tail calls, which do not count on the stack);
8354and so on. 8332and so on.
8355If @id{f} is a number larger than the number of active functions, 8333If @id{f} is a number greater than the number of active functions,
8356then @id{getinfo} returns @nil. 8334then @id{getinfo} returns @nil.
8357 8335
8358The returned table can contain all the fields returned by @Lid{lua_getinfo}, 8336The returned table can contain all the fields returned by @Lid{lua_getinfo},
@@ -8746,6 +8724,12 @@ When needed, this metamethod must be explicitly defined.
8746} 8724}
8747 8725
8748@item{ 8726@item{
8727The semantics of the numerical @Rw{for} loop
8728over integers changed in some details.
8729In particular, the control variable never wraps around.
8730}
8731
8732@item{
8749When a coroutine finishes with an error, 8733When a coroutine finishes with an error,
8750its stack is unwound (to run any pending closing methods). 8734its stack is unwound (to run any pending closing methods).
8751} 8735}