diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-03-19 10:53:18 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-03-19 10:53:18 -0300 |
| commit | 9b37a4695ebf50b37b5b4fb279ae948f23b5b6a0 (patch) | |
| tree | 2a6b0f6c1c2eb962bb383175eb0a67ea81a4564d /manual | |
| parent | 1e0c73d5b643707335b06abd2546a83d9439d14c (diff) | |
| download | lua-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.of | 124 |
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. | |||
| 594 | The collector starts a new cycle when the use of memory | 594 | The collector starts a new cycle when the use of memory |
| 595 | hits @M{n%} of the use after the previous collection. | 595 | hits @M{n%} of the use after the previous collection. |
| 596 | Larger values make the collector less aggressive. | 596 | Larger values make the collector less aggressive. |
| 597 | Values smaller than 100 mean the collector will not wait to | 597 | Values less than 100 mean the collector will not wait to |
| 598 | start a new cycle. | 598 | start a new cycle. |
| 599 | A value of 200 means that the collector waits for the total memory in use | 599 | A value of 200 means that the collector waits for the total memory in use |
| 600 | to double before starting a new cycle. | 600 | to double before starting a new cycle. |
| @@ -608,7 +608,7 @@ how many elements it marks or sweeps for each | |||
| 608 | kilobyte of memory allocated. | 608 | kilobyte of memory allocated. |
| 609 | Larger values make the collector more aggressive but also increase | 609 | Larger values make the collector more aggressive but also increase |
| 610 | the size of each incremental step. | 610 | the size of each incremental step. |
| 611 | You should not use values smaller than 100, | 611 | You should not use values less than 100, |
| 612 | because they make the collector too slow and | 612 | because they make the collector too slow and |
| 613 | can result in the collector never finishing a cycle. | 613 | can result in the collector never finishing a cycle. |
| 614 | The default value is 100; the maximum value is 1000. | 614 | The 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), |
| 1005 | where @rep{XXX} is a sequence of one or more hexadecimal digits | 1005 | where @rep{XXX} is a sequence of one or more hexadecimal digits |
| 1006 | representing the character code point. | 1006 | representing the character code point. |
| 1007 | This code point can be any value smaller than @M{2@sp{31}}. | 1007 | This 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 | ||
| 1010 | Literal strings can also be defined using a long format | 1010 | Literal 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. | |||
| 1370 | The @Rw{for} statement has two forms: | 1370 | The @Rw{for} statement has two forms: |
| 1371 | one numerical and one generic. | 1371 | one numerical and one generic. |
| 1372 | 1372 | ||
| 1373 | @sect4{@title{The numerical @Rw{for} loop} | ||
| 1374 | |||
| 1373 | The numerical @Rw{for} loop repeats a block of code while a | 1375 | The numerical @Rw{for} loop repeats a block of code while a |
| 1374 | control variable runs through an arithmetic progression. | 1376 | control variable goes through an arithmetic progression. |
| 1375 | It has the following syntax: | 1377 | It 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 | } |
| 1380 | The @emph{block} is repeated for @emph{name} starting at the value of | 1382 | The given identifier (@bnfNter{Name}) defines the control variable, |
| 1381 | the first @emph{exp}, until it passes the second @emph{exp} by steps of the | 1383 | which is local to the loop body (@emph{block}). |
| 1382 | third @emph{exp}. | 1384 | |
| 1383 | More precisely, a @Rw{for} statement like | 1385 | The loop starts by evaluating once the three control expressions; |
| 1384 | @verbatim{ | 1386 | they must all result in numbers. |
| 1385 | for v = @rep{e1}, @rep{e2}, @rep{e3} do @rep{block} end | 1387 | Their values are called respectively |
| 1386 | } | 1388 | the @emph{initial value}, the @emph{limit}, and the @emph{step}. |
| 1387 | is equivalent to the code: | 1389 | If the step is absent, it defaults @N{to 1}. |
| 1388 | @verbatim{ | 1390 | Then the loop body is repeated with the value of the control variable |
| 1389 | do | 1391 | going through an arithmetic progression, |
| 1390 | local @rep{var}, @rep{limit}, @rep{step} = tonumber(@rep{e1}), tonumber(@rep{e2}), tonumber(@rep{e3}) | 1392 | starting at the initial value, |
| 1391 | if not (@rep{var} and @rep{limit} and @rep{step}) then error() end | 1393 | with a common difference given by the step, |
| 1392 | @rep{var} = @rep{var} - @rep{step} | 1394 | until that value passes the limit. |
| 1393 | while true do | 1395 | A negative step makes a decreasing sequence; |
| 1394 | @rep{var} = @rep{var} + @rep{step} | 1396 | a 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 | 1397 | If 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} | 1400 | If both the initial value and the step are integers, |
| 1399 | @rep{block} | 1401 | the loop is done with integers; |
| 1400 | end | 1402 | in this case, the range of the control variable is limited |
| 1401 | end | 1403 | by the range of integers. |
| 1402 | } | 1404 | Otherwise, the loop is done with floats. |
| 1403 | 1405 | (Beware of floating-point accuracy in this case.) | |
| 1404 | Note the following: | 1406 | |
| 1405 | @itemize{ | 1407 | You should not change the value of the control variable |
| 1406 | 1408 | during the loop. | |
| 1407 | @item{ | ||
| 1408 | All three control expressions are evaluated only once, | ||
| 1409 | before the loop starts. | ||
| 1410 | They must all result in numbers. | ||
| 1411 | } | ||
| 1412 | |||
| 1413 | @item{ | ||
| 1414 | @T{@rep{var}}, @T{@rep{limit}}, and @T{@rep{step}} are invisible variables. | ||
| 1415 | The names shown here are for explanatory purposes only. | ||
| 1416 | } | ||
| 1417 | |||
| 1418 | @item{ | ||
| 1419 | If the third expression (the step) is absent, | ||
| 1420 | then a step @N{of 1} is used. | ||
| 1421 | } | ||
| 1422 | |||
| 1423 | @item{ | ||
| 1424 | You can use @Rw{break} and @Rw{goto} to exit a @Rw{for} loop. | ||
| 1425 | } | ||
| 1426 | |||
| 1427 | @item{ | ||
| 1428 | The loop variable @T{v} is local to the loop body. | ||
| 1429 | If you need its value after the loop, | 1409 | If you need its value after the loop, |
| 1430 | assign it to another variable before exiting the loop. | 1410 | assign it to another variable before exiting the loop. |
| 1431 | } | ||
| 1432 | 1411 | ||
| 1433 | @item{ | ||
| 1434 | The values in @rep{var}, @rep{limit}, and @rep{step} | ||
| 1435 | can be integers or floats. | ||
| 1436 | All 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 | ||
| 1441 | The generic @Rw{for} statement works over functions, | 1417 | The generic @Rw{for} statement works over functions, |
| 1442 | called @def{iterators}. | 1418 | called @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} |
| 1503 | To allow possible side-effects, | 1481 | To allow possible side-effects, |
| 1504 | function calls can be executed as statements: | 1482 | function calls can be executed as statements: |
| @@ -1819,7 +1797,7 @@ A comparison @T{a > b} is translated to @T{b < a} | |||
| 1819 | and @T{a >= b} is translated to @T{b <= a}. | 1797 | and @T{a >= b} is translated to @T{b <= a}. |
| 1820 | 1798 | ||
| 1821 | Following the @x{IEEE 754} standard, | 1799 | Following the @x{IEEE 754} standard, |
| 1822 | @x{NaN} is considered neither smaller than, | 1800 | @x{NaN} is considered neither less than, |
| 1823 | nor equal to, nor greater than any value (including itself). | 1801 | nor 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} |
| 2172 | There is a system-dependent limit on the number of values | 2150 | There is a system-dependent limit on the number of values |
| 2173 | that a function may return. | 2151 | that a function may return. |
| 2174 | This limit is guaranteed to be larger than 1000. | 2152 | This limit is guaranteed to be greater than 1000. |
| 2175 | 2153 | ||
| 2176 | The @emphx{colon} syntax | 2154 | The @emphx{colon} syntax |
| 2177 | is used for defining @def{methods}, | 2155 | is used for defining @def{methods}, |
| @@ -2367,7 +2345,7 @@ but it also can be any positive index after the stack top | |||
| 2367 | within the space allocated for the stack, | 2345 | within the space allocated for the stack, |
| 2368 | that is, indices up to the stack size. | 2346 | that 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.) |
| 2370 | Indices to upvalues @see{c-closure} larger than the real number | 2348 | Indices to upvalues @see{c-closure} greater than the real number |
| 2371 | of upvalues in the current @N{C function} are also acceptable (but invalid). | 2349 | of upvalues in the current @N{C function} are also acceptable (but invalid). |
| 2372 | Except when noted otherwise, | 2350 | Except when noted otherwise, |
| 2373 | functions in the API work with acceptable indices. | 2351 | functions 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). |
| 2880 | It returns false if it cannot fulfill the request, | 2858 | It returns false if it cannot fulfill the request, |
| 2881 | either because it would cause the stack | 2859 | either because it would cause the stack |
| 2882 | to be larger than a fixed maximum size | 2860 | to be greater than a fixed maximum size |
| 2883 | (typically at least several thousand elements) or | 2861 | (typically at least several thousand elements) or |
| 2884 | because it cannot allocate memory for the extra space. | 2862 | because it cannot allocate memory for the extra space. |
| 2885 | This function never shrinks the stack; | 2863 | This function never shrinks the stack; |
| @@ -4053,7 +4031,7 @@ for the @Q{newindex} event @see{metatable}. | |||
| 4053 | 4031 | ||
| 4054 | Accepts any index, @N{or 0}, | 4032 | Accepts any index, @N{or 0}, |
| 4055 | and sets the stack top to this index. | 4033 | and sets the stack top to this index. |
| 4056 | If the new top is larger than the old one, | 4034 | If the new top is greater than the old one, |
| 4057 | then the new elements are filled with @nil. | 4035 | then the new elements are filled with @nil. |
| 4058 | If @id{index} @N{is 0}, then all stack elements are removed. | 4036 | If @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{ |
| 5057 | Finish by calling @T{luaL_pushresultsize(&b, sz)}, | 5035 | Finish by calling @T{luaL_pushresultsize(&b, sz)}, |
| 5058 | where @id{sz} is the total size of the resulting string | 5036 | where @id{sz} is the total size of the resulting string |
| 5059 | copied into that space (which may be smaller than or | 5037 | copied into that space (which may be less than or |
| 5060 | equal to the preallocated size). | 5038 | equal to the preallocated size). |
| 5061 | } | 5039 | } |
| 5062 | 5040 | ||
| @@ -7336,7 +7314,7 @@ Functions that interpret byte sequences only accept | |||
| 7336 | valid sequences (well formed and not overlong). | 7314 | valid sequences (well formed and not overlong). |
| 7337 | By default, they only accept byte sequences | 7315 | By default, they only accept byte sequences |
| 7338 | that result in valid Unicode code points, | 7316 | that result in valid Unicode code points, |
| 7339 | rejecting values larger than @T{10FFFF} and surrogates. | 7317 | rejecting values greater than @T{10FFFF} and surrogates. |
| 7340 | A boolean argument @id{nonstrict}, when available, | 7318 | A boolean argument @id{nonstrict}, when available, |
| 7341 | lifts these checks, | 7319 | lifts these checks, |
| 7342 | so that all values up to @T{0x7FFFFFFF} are accepted. | 7320 | so 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 | ||
| 7575 | Returns the smallest integral value larger than or equal to @id{x}. | 7553 | Returns 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 | ||
| 7600 | Returns the largest integral value smaller than or equal to @id{x}. | 7578 | Returns 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 | ||
| 7613 | The float value @idx{HUGE_VAL}, | 7591 | The float value @idx{HUGE_VAL}, |
| 7614 | a value larger than any other numeric value. | 7592 | a 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); |
| 8354 | and so on. | 8332 | and so on. |
| 8355 | If @id{f} is a number larger than the number of active functions, | 8333 | If @id{f} is a number greater than the number of active functions, |
| 8356 | then @id{getinfo} returns @nil. | 8334 | then @id{getinfo} returns @nil. |
| 8357 | 8335 | ||
| 8358 | The returned table can contain all the fields returned by @Lid{lua_getinfo}, | 8336 | The 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{ |
| 8727 | The semantics of the numerical @Rw{for} loop | ||
| 8728 | over integers changed in some details. | ||
| 8729 | In particular, the control variable never wraps around. | ||
| 8730 | } | ||
| 8731 | |||
| 8732 | @item{ | ||
| 8749 | When a coroutine finishes with an error, | 8733 | When a coroutine finishes with an error, |
| 8750 | its stack is unwound (to run any pending closing methods). | 8734 | its stack is unwound (to run any pending closing methods). |
| 8751 | } | 8735 | } |
