History

Lua 5.3.0-work3

Lua Team committed 12 years ago

Commit f8a66271 parent ad386d945 files · +2323 −1590Browse files

Unified · Split

45 files
  1. README +1 −1
  2. doc/contents.html +15 −18
  3. doc/manual.html +557 −397
  4. doc/readme.html +16 −19
  5. src/.fix +0 −2
  6. src/Makefile +2 −2
  7. src/lapi.c +46 −54
  8. src/lauxlib.c +6 −11
  9. src/lauxlib.h +2 −2
  10. src/lbaselib.c +51 −41
  11. src/lbitlib.c +2 −3
  12. src/lcode.c +10 −11
  13. src/lcorolib.c +17 −5
  14. src/ldblib.c +117 −70
  15. src/ldo.c +64 −42
  16. src/ldump.c +18 −9
  17. src/lfunc.c +13 −10
  18. src/lfunc.h +3 −3
  19. src/lgc.c +70 −76
  20. src/linit.c +5 −3
  21. src/liolib.c +89 −24
  22. src/llex.c +69 −53
  23. src/llimits.h +20 −13
  24. src/lmathlib.c +175 −106
  25. src/lobject.c +37 −17
  26. src/lobject.h +39 −45
  27. src/loslib.c +8 −2
  28. src/lparser.c +5 −5
  29. src/lparser.h +3 −3
  30. src/lstate.h +14 −14
  31. src/lstring.c +8 −4
  32. src/lstrlib.c +54 −47
  33. src/ltable.c +27 −21
  34. src/ltablib.c +27 −28
  35. src/ltm.c +5 −16
  36. src/ltm.h +1 −2
  37. src/lua.c +223 −153
  38. src/lua.h +25 −14
  39. src/luac.c +15 −8
  40. src/luaconf.h +131 −41
  41. src/lundump.c +27 −19
  42. src/lundump.h +4 −4
  43. src/lutf8lib.c +49 −38
  44. src/lvm.c +251 −127
  45. src/lvm.h +2 −7
README+1−1
@@ -1,5 +1,5 @@
11
2−This is Lua 5.3.0 (work2), released on 21 Mar 2014.
2+This is Lua 5.3.0 (work3), released on 19 Jul 2014.
33
44 For installation instructions, license details, and
55 further information about Lua, see doc/readme.html.
doc/contents.html+15−18
@@ -174,6 +174,7 @@ Freely available under the terms of the · 173 unmodified lines
174174
175175 <P>
176176 <A HREF="manual.html#pdf-coroutine.create">coroutine.create</A><BR>
177+<A HREF="manual.html#pdf-coroutine.isyieldable">coroutine.isyieldable</A><BR>
177178 <A HREF="manual.html#pdf-coroutine.resume">coroutine.resume</A><BR>
178179 <A HREF="manual.html#pdf-coroutine.running">coroutine.running</A><BR>
179180 <A HREF="manual.html#pdf-coroutine.status">coroutine.status</A><BR>
@@ -182,6 +183,7 @@ Freely available under the terms of the · 2 unmodified lines
182183
183184 <P>
184185 <A HREF="manual.html#pdf-debug.debug">debug.debug</A><BR>
186+<A HREF="manual.html#pdf-debug.Csize">debug.Csize</A><BR>
185187 <A HREF="manual.html#pdf-debug.gethook">debug.gethook</A><BR>
186188 <A HREF="manual.html#pdf-debug.getinfo">debug.getinfo</A><BR>
187189 <A HREF="manual.html#pdf-debug.getlocal">debug.getlocal</A><BR>
@@ -189,7 +191,6 @@ Freely available under the terms of the · 1 unmodified line
189191 <A HREF="manual.html#pdf-debug.getregistry">debug.getregistry</A><BR>
190192 <A HREF="manual.html#pdf-debug.getupvalue">debug.getupvalue</A><BR>
191193 <A HREF="manual.html#pdf-debug.getuservalue">debug.getuservalue</A><BR>
192−<A HREF="manual.html#pdf-debug.numbits">debug.numbits</A><BR>
193194 <A HREF="manual.html#pdf-debug.sethook">debug.sethook</A><BR>
194195 <A HREF="manual.html#pdf-debug.setlocal">debug.setlocal</A><BR>
195196 <A HREF="manual.html#pdf-debug.setmetatable">debug.setmetatable</A><BR>
@@ -228,40 +229,33 @@ Freely available under the terms of the · 32 unmodified lines
228229 <TD>
229230 <H3>&nbsp;</H3>
230231 <P>
232+<A HREF="manual.html#pdf-math">math</A><BR>
231233 <A HREF="manual.html#pdf-math.abs">math.abs</A><BR>
232234 <A HREF="manual.html#pdf-math.acos">math.acos</A><BR>
233235 <A HREF="manual.html#pdf-math.asin">math.asin</A><BR>
234236 <A HREF="manual.html#pdf-math.atan">math.atan</A><BR>
235−<A HREF="manual.html#pdf-math.atan2">math.atan2</A><BR>
236237 <A HREF="manual.html#pdf-math.ceil">math.ceil</A><BR>
237238 <A HREF="manual.html#pdf-math.cos">math.cos</A><BR>
238−<A HREF="manual.html#pdf-math.cosh">math.cosh</A><BR>
239239 <A HREF="manual.html#pdf-math.deg">math.deg</A><BR>
240−<A HREF="manual.html#pdf-math.exp">math.exp</A><BR>
241240 <A HREF="manual.html#pdf-math.floor">math.floor</A><BR>
242241 <A HREF="manual.html#pdf-math.fmod">math.fmod</A><BR>
243−<A HREF="manual.html#pdf-math.frexp">math.frexp</A><BR>
244242 <A HREF="manual.html#pdf-math.huge">math.huge</A><BR>
245243 <A HREF="manual.html#pdf-math.ifloor">math.ifloor</A><BR>
246−<A HREF="manual.html#pdf-math.isfloat">math.isfloat</A><BR>
247−<A HREF="manual.html#pdf-math.ldexp">math.ldexp</A><BR>
248244 <A HREF="manual.html#pdf-math.log">math.log</A><BR>
249245 <A HREF="manual.html#pdf-math.max">math.max</A><BR>
246+<A HREF="manual.html#pdf-math.maxinteger">math.maxinteger</A><BR>
250247 <A HREF="manual.html#pdf-math.min">math.min</A><BR>
248+<A HREF="manual.html#pdf-math.mininteger">math.mininteger</A><BR>
251249 <A HREF="manual.html#pdf-math.modf">math.modf</A><BR>
252250 <A HREF="manual.html#pdf-math.pi">math.pi</A><BR>
253−<A HREF="manual.html#pdf-math.pow">math.pow</A><BR>
254251 <A HREF="manual.html#pdf-math.rad">math.rad</A><BR>
255252 <A HREF="manual.html#pdf-math.random">math.random</A><BR>
256253 <A HREF="manual.html#pdf-math.randomseed">math.randomseed</A><BR>
257254 <A HREF="manual.html#pdf-math.sin">math.sin</A><BR>
258−<A HREF="manual.html#pdf-math.sinh">math.sinh</A><BR>
259255 <A HREF="manual.html#pdf-math.sqrt">math.sqrt</A><BR>
260256 <A HREF="manual.html#pdf-math.tan">math.tan</A><BR>
261−<A HREF="manual.html#pdf-math.tanh">math.tanh</A><BR>
262257 <A HREF="manual.html#pdf-math.type">math.type</A><BR>
263258
264−
265259 <P>
266260 <A HREF="manual.html#pdf-os.clock">os.clock</A><BR>
267261 <A HREF="manual.html#pdf-os.date">os.date</A><BR>
@@ -289,6 +283,8 @@ Freely available under the terms of the · 21 unmodified lines
289283 <A HREF="manual.html#pdf-string.byte">string.byte</A><BR>
290284 <A HREF="manual.html#pdf-string.char">string.char</A><BR>
291285 <A HREF="manual.html#pdf-string.dump">string.dump</A><BR>
286+<A HREF="manual.html#pdf-string.dumpfloat">string.dumpfloat</A><BR>
287+<A HREF="manual.html#pdf-string.dumpint">string.dumpint</A><BR>
292288 <A HREF="manual.html#pdf-string.find">string.find</A><BR>
293289 <A HREF="manual.html#pdf-string.format">string.format</A><BR>
294290 <A HREF="manual.html#pdf-string.gmatch">string.gmatch</A><BR>
@@ -296,13 +292,11 @@ Freely available under the terms of the · 1 unmodified line
296292 <A HREF="manual.html#pdf-string.len">string.len</A><BR>
297293 <A HREF="manual.html#pdf-string.lower">string.lower</A><BR>
298294 <A HREF="manual.html#pdf-string.match">string.match</A><BR>
299−<A HREF="manual.html#pdf-string.packfloat">string.packfloat</A><BR>
300−<A HREF="manual.html#pdf-string.packint">string.packint</A><BR>
301295 <A HREF="manual.html#pdf-string.rep">string.rep</A><BR>
302296 <A HREF="manual.html#pdf-string.reverse">string.reverse</A><BR>
303297 <A HREF="manual.html#pdf-string.sub">string.sub</A><BR>
304−<A HREF="manual.html#pdf-string.unpackfloat">string.unpackfloat</A><BR>
305−<A HREF="manual.html#pdf-string.unpackint">string.unpackint</A><BR>
298+<A HREF="manual.html#pdf-string.undumpfloat">string.undumpfloat</A><BR>
299+<A HREF="manual.html#pdf-string.undumpint">string.undumpint</A><BR>
306300 <A HREF="manual.html#pdf-string.upper">string.upper</A><BR>
307301
308302 <P>
@@ -330,6 +324,7 @@ Freely available under the terms of the · 21 unmodified lines
330324 <A HREF="manual.html#lua_Debug">lua_Debug</A><BR>
331325 <A HREF="manual.html#lua_Hook">lua_Hook</A><BR>
332326 <A HREF="manual.html#lua_Integer">lua_Integer</A><BR>
327+<A HREF="manual.html#lua_KFunction">lua_KFunction</A><BR>
333328 <A HREF="manual.html#lua_Number">lua_Number</A><BR>
334329 <A HREF="manual.html#lua_Reader">lua_Reader</A><BR>
335330 <A HREF="manual.html#lua_State">lua_State</A><BR>
@@ -352,7 +347,6 @@ Freely available under the terms of the · 16 unmodified lines
352347 <A HREF="manual.html#lua_error">lua_error</A><BR>
353348 <A HREF="manual.html#lua_gc">lua_gc</A><BR>
354349 <A HREF="manual.html#lua_getallocf">lua_getallocf</A><BR>
355−<A HREF="manual.html#lua_getctx">lua_getctx</A><BR>
356350 <A HREF="manual.html#lua_getfield">lua_getfield</A><BR>
357351 <A HREF="manual.html#lua_getglobal">lua_getglobal</A><BR>
358352 <A HREF="manual.html#lua_gethook">lua_gethook</A><BR>
@@ -380,6 +374,7 @@ Freely available under the terms of the · 21 unmodified lines
380374 <A HREF="manual.html#lua_istable">lua_istable</A><BR>
381375 <A HREF="manual.html#lua_isthread">lua_isthread</A><BR>
382376 <A HREF="manual.html#lua_isuserdata">lua_isuserdata</A><BR>
377+<A HREF="manual.html#lua_isyieldable">lua_isyieldable</A><BR>
383378 <A HREF="manual.html#lua_len">lua_len</A><BR>
384379 <A HREF="manual.html#lua_load">lua_load</A><BR>
385380 <A HREF="manual.html#lua_newstate">lua_newstate</A><BR>
@@ -387,6 +382,7 @@ Freely available under the terms of the · 1 unmodified line
387382 <A HREF="manual.html#lua_newthread">lua_newthread</A><BR>
388383 <A HREF="manual.html#lua_newuserdata">lua_newuserdata</A><BR>
389384 <A HREF="manual.html#lua_next">lua_next</A><BR>
385+<A HREF="manual.html#lua_numtointeger">lua_numtointeger</A><BR>
390386 <A HREF="manual.html#lua_pcall">lua_pcall</A><BR>
391387 <A HREF="manual.html#lua_pcallk">lua_pcallk</A><BR>
392388 <A HREF="manual.html#lua_pop">lua_pop</A><BR>
@@ -418,6 +414,7 @@ Freely available under the terms of the · 25 unmodified lines
418414 <A HREF="manual.html#lua_remove">lua_remove</A><BR>
419415 <A HREF="manual.html#lua_replace">lua_replace</A><BR>
420416 <A HREF="manual.html#lua_resume">lua_resume</A><BR>
417+<A HREF="manual.html#lua_rotate">lua_rotate</A><BR>
421418 <A HREF="manual.html#lua_setallocf">lua_setallocf</A><BR>
422419 <A HREF="manual.html#lua_setfield">lua_setfield</A><BR>
423420 <A HREF="manual.html#lua_setglobal">lua_setglobal</A><BR>
@@ -534,10 +531,10 @@ Freely available under the terms of the · 110 unmodified lines
534531 <HR>
535532 <SMALL CLASS="footer">
536533 Last update:
537−Fri Mar 21 17:34:12 BRT 2014
534+Thu Jun 19 17:09:46 BRT 2014
538535 </SMALL>
539536 <!--
540−Last change: updated for Lua 5.3.0 (work2)
537+Last change: updated for Lua 5.3.0 (work3)
541538 -->
542539
543540 </BODY>
doc/manual.html+557−397
@@ -38,7 +38,7 @@ Freely available under the terms of the · 37 unmodified lines
3838 <!-- ====================================================================== -->
3939 <p>
4040
41−<!-- $Id: manual.of,v 1.114 2014/03/21 18:32:52 roberto Exp $ -->
41+<!-- $Id: manual.of,v 1.121 2014/06/18 19:12:23 roberto Exp $ -->
4242
4343
4444
@@ -139,9 +139,9 @@ one called <em>integer</em> and the other called <em>float</em>. · 94 unmodified lines
139139 Lua has explicit rules about when each representation is used,
140140 but it also converts between them automatically as needed (see <a href="#3.4.3">&sect;3.4.3</a>).
141141 Therefore,
142−the programmer has the option of mostly ignore the difference
142+the programmer has the option of mostly ignoring the difference
143143 between integers and floats
144−or assume complete control about the representation of each value.
144+or assuming complete control over the representation of each value.
145145 Standard Lua uses 64-bit integers and double-precision (64-bit) floats,
146146 but you can also compile Lua so that it
147147 uses 32-bit integers and/or single-precision (32-bit) floats.
@@ -437,7 +437,7 @@ with the following code: · 289 unmodified lines
437437 </pre>
438438
439439 <p>
440−For the unary minus and the bitwise not operators,
440+For the unary operators (negation, length, and bitwise not),
441441 the metamethod is computed and called with a dummy second operand,
442442 equal to the first one.
443443 This extra operand is only to simplify Lua's internals
@@ -575,9 +575,9 @@ Otherwise, Lua raises an error. · 131 unmodified lines
575575 <li><b>"eq": </b>
576576 the <code>==</code> (equal) operation.
577577
578−Lua will try a metamethod only when the values
579−being compared are both either tables or full userdata,
580−both have the same metamethod,
578+Behavior similar to the "add" operation,
579+except that Lua will try a metamethod only when the values
580+being compared are either both tables or both full userdata
581581 and they are not primitively equal.
582582 The result of the call is always converted to a boolean.
583583 </li>
@@ -586,7 +586,9 @@ The result of the call is always converted to a boolean. · 2 unmodified lines
586586 the <code>&lt;</code> (less than) operation.
587587
588588 Behavior similar to the "add" operation,
589−except that the result of the call is always converted to a boolean.
589+except that Lua will try a metamethod only when the values
590+being compared are neither both numbers nor both strings.
591+The result of the call is always converted to a boolean.
590592 </li>
591593
592594 <li><b>"le": </b>
@@ -595,7 +597,7 @@ the <code>&lt;=</code> (less equal) operation. · 2 unmodified lines
595597 Unlike other operations,
596598 The less-equal operation can use two different events.
597599 First, Lua looks for the "<code>__le</code>" metamethod in both operands,
598−like in the "add" operation.
600+like in the "lt" operation.
599601 If it cannot find such a metamethod,
600602 then it will try the "<code>__lt</code>" event,
601603 assuming that <code>a &lt;= b</code> is equivalent to <code>not (b &lt; a)</code>.
@@ -775,7 +777,7 @@ Lua simply ignores it.) · 173 unmodified lines
775777 <p>
776778 At the end of each garbage-collection cycle,
777779 the finalizers for objects are called in
778−the reverse order that they were marked for collection,
780+the reverse order that they were marked for finalization,
779781 among those collected in that cycle;
780782 that is, the first finalizer to be called is the one associated
781783 with the object marked last in the program.
@@ -792,17 +794,20 @@ and the object memory is freed in the next garbage-collection cycle. · 10 unmodified lines
792794 However, if the finalizer stores the object in some global place
793795 (e.g., a global variable),
794796 then there is a permanent resurrection.
797+Moreover, if the finalizer marks a finalizing object for finalization again,
798+its finalizer will be called again in the next cycle where the
799+object is unreachable.
795800 In any case,
796−the object memory is freed only when it becomes completely inaccessible;
797−its finalizer will never be called twice.
801+the object memory is freed only in the GC cycle where
802+the object is unreachable and not marked for finalization.
798803
799804
800805 <p>
801806 When you close a state (see <a href="#lua_close"><code>lua_close</code></a>),
802807 Lua calls the finalizers of all objects marked for finalization,
803808 following the reverse order that they were marked.
804−If any finalizer marks new objects for collection during that phase,
805−these new objects will not be finalized.
809+If any finalizer marks objects for collection during that phase,
810+these marks have no effect.
806811
807812
808813
@@ -1064,9 +1069,10 @@ and cannot be used as names: · 255 unmodified lines
10641069 Lua is a case-sensitive language:
10651070 <code>and</code> is a reserved word, but <code>And</code> and <code>AND</code>
10661071 are two different, valid names.
1067−As a convention, names starting with an underscore followed by
1068−uppercase letters (such as <a href="#pdf-_VERSION"><code>_VERSION</code></a>)
1069−are reserved for variables used by Lua.
1072+As a convention,
1073+programs should avoid creating
1074+names that start with an underscore followed by
1075+one or more uppercase letters (such as <a href="#pdf-_VERSION"><code>_VERSION</code></a>).
10701076
10711077
10721078 <p>
@@ -1074,6 +1080,7 @@ The following strings denote other tokens: · 1 unmodified line
10741080
10751081 <pre>
10761082 + - * / % ^ #
1083+ &amp; ~ | &lt;&lt; &gt;&gt; //
10771084 == ~= &lt;= &gt;= &lt; &gt; =
10781085 ( ) { } [ ] ::
10791086 ; : , . .. ...
@@ -1130,11 +1137,12 @@ enclosed by <em>long brackets</em>. · 50 unmodified lines
11301137 We define an <em>opening long bracket of level <em>n</em></em> as an opening
11311138 square bracket followed by <em>n</em> equal signs followed by another
11321139 opening square bracket.
1133−So, an opening long bracket of level&nbsp;0 is written as <code>[[</code>,
1134−an opening long bracket of level&nbsp;1 is written as <code>[=[</code>,
1140+So, an opening long bracket of level&nbsp;0 is written as <code>[[</code>,
1141+an opening long bracket of level&nbsp;1 is written as <code>[=[</code>,
11351142 and so on.
11361143 A <em>closing long bracket</em> is defined similarly;
1137−for instance, a closing long bracket of level&nbsp;4 is written as <code>]====]</code>.
1144+for instance,
1145+a closing long bracket of level&nbsp;4 is written as <code>]====]</code>.
11381146 A <em>long literal</em> starts with an opening long bracket of any level and
11391147 ends at the first closing long bracket of the same level.
11401148 It can contain any text except a closing bracket of the proper level.
@@ -1594,20 +1602,19 @@ is equivalent to the code: · 453 unmodified lines
15941602
15951603 <pre>
15961604 do
1597− local <em>var</em>, <em>limit</em>, <em>step</em> = convert(<em>e1</em>, <em>e2</em>, <em>e3</em>)
1605+ local <em>var</em>, <em>limit</em>, <em>step</em> = tonumber(<em>e1</em>), tonumber(<em>e2</em>), tonumber(<em>e3</em>)
15981606 if not (<em>var</em> and <em>limit</em> and <em>step</em>) then error() end
1599− while (<em>step</em> &gt; 0 and <em>var</em> &lt;= <em>limit</em>) or (<em>step</em> &lt;= 0 and <em>var</em> &gt;= <em>limit</em>) do
1607+ <em>var</em> = <em>var</em> - <em>step</em>
1608+ while true do
1609+ <em>var</em> = <em>var</em> + <em>step</em>
1610+ if (<em>step</em> &gt;= 0 and <em>var</em> &gt; <em>limit</em>) or (<em>step</em> &lt; 0 and <em>var</em> &lt; <em>limit</em>) then
1611+ break
1612+ end
16001613 local v = <em>var</em>
16011614 <em>block</em>
1602− <em>var</em> = <em>var</em> + <em>step</em>
16031615 end
16041616 end
1605−</pre><p>
1606−where <code>convert</code> returns
1607−its three arguments unaltered
1608−if they are all integers,
1609−otherwise it converts them to floats.
1610−
1617+</pre>
16111618
16121619 <p>
16131620 Note the following:
@@ -1620,12 +1627,6 @@ before the loop starts. · 6 unmodified lines
16201627 They must all result in numbers.
16211628 </li>
16221629
1623−<li>
1624−If any of the three control values is not an integer,
1625−all three values are converted to floats,
1626−so that the loop is done with floats.
1627−</li>
1628−
16291630 <li>
16301631 <code><em>var</em></code>, <code><em>limit</em></code>, and <code><em>step</em></code> are invisible variables.
16311632 The names shown here are for explanatory purposes only.
@@ -1641,9 +1642,8 @@ You can use <b>break</b> and <b>goto</b> to exit a <b>for</b> loop. · 9 unmodified lines
16411642 </li>
16421643
16431644 <li>
1644−The loop variable <code>v</code> is local to the loop;
1645−you cannot use its value after the <b>for</b> ends or is broken.
1646−If you need its value,
1645+The loop variable <code>v</code> is local to the loop body.
1646+If you need its value after the loop,
16471647 assign it to another variable before exiting the loop.
16481648 </li>
16491649
@@ -1840,17 +1840,20 @@ or <b>nil</b> if <code>f</code> does not return any values.) · 190 unmodified lines
18401840
18411841 <h3>3.4.1 &ndash; <a name="3.4.1">Arithmetic Operators</a></h3><p>
18421842 Lua supports the following arithmetic operators:
1843−the binary <code>+</code> (addition),
1844−<code>-</code> (subtraction), <code>*</code> (multiplication),
1845−<code>/</code> (division), <code>//</code> (integer division),
1846−<code>%</code> (modulo), and <code>^</code> (exponentiation);
1847−and unary <code>-</code> (mathematical negation).
1848−Modulo is defined as the remainder of a division
1849−that rounds the quotient towards minus infinite (floor).
1843+<table border="1">
1844+<tr><td><code>+</code></td><td>addition</td></tr>
1845+<tr><td><code>-</code></td><td>subtraction</td></tr>
1846+<tr><td><code>*</code></td><td>multiplication</td></tr>
1847+<tr><td><code>/</code></td><td>float division</td></tr>
1848+<tr><td><code>//</code></td><td>integer division</td></tr>
1849+<tr><td><code>%</code></td><td>modulo</td></tr>
1850+<tr><td><code>^</code></td><td>exponentiation</td></tr>
1851+<tr><td><code>-</code></td><td>unary minus</td></tr>
1852+</table>
18501853
18511854
18521855 <p>
1853−With the exception of division and integer division,
1856+With the exception of divisions and exponentiation,
18541857 the arithmetic operators work as follows:
18551858 If both operands are integers,
18561859 the operation is performed over integers and the result is an integer.
@@ -1865,8 +1868,11 @@ and the result is a float. · 8 unmodified lines
18651868
18661869
18671870 <p>
1868−Division (<code>/</code>) always converts its operands to floats
1869−and its result is always a float.
1871+Float division (<code>/</code>) and exponentiation
1872+always convert their operands to floats
1873+and the result is always a float.
1874+Exponentiation uses the ANSI&nbsp;C function <code>pow</code>,
1875+so that it works for non-integer exponents too.
18701876
18711877
18721878 <p>
@@ -1876,10 +1882,16 @@ and its result is always an integer. · 3 unmodified lines
18761882 The result is always rounded towards minus infinite (floor).
18771883
18781884
1885+<p>
1886+Modulo is defined as the remainder of a division
1887+that rounds the quotient towards minus infinite (floor).
1888+
1889+
18791890 <p>
18801891 In case of overflows in integer arithmetic,
18811892 all operations <em>wrap around</em>,
18821893 according to the usual rules of two-complement arithmetic.
1894+(In other words, they return the correct result modulo <em>2<sup>64</sup></em>.)
18831895
18841896
18851897
@@ -1887,10 +1899,14 @@ according to the usual rules of two-complement arithmetic. · 1 unmodified line
18871899
18881900 <h3>3.4.2 &ndash; <a name="3.4.2">Bitwise Operators</a></h3><p>
18891901 Lua supports the following bitwise operators:
1890−the binary <code>&amp;</code> (bitwise and),
1891−<code>|</code> (bitwise or), <code>~</code> (bitwise exclusive or),
1892−<code>&gt;&gt;</code> (right shift), <code>&lt;&lt;</code> (left shift),
1893−and unary <code>~</code> (bitwise not).
1902+<table border="1">
1903+<tr><td><code>&amp;</code></td><td>bitwise and</td></tr>
1904+<tr><td><code>|</code></td><td>bitwise or</td></tr>
1905+<tr><td><code>~</code></td><td>bitwise exclusive or</td></tr>
1906+<tr><td><code>&gt;&gt;</code></td><td>right shift</td></tr>
1907+<tr><td><code>&lt;&lt;</code></td><td>left shift</td></tr>
1908+<tr><td><code>~</code></td><td>unary bitwise not</td></tr>
1909+</table>
18941910
18951911
18961912 <p>
@@ -1901,7 +1917,7 @@ and result in an integer. · 4 unmodified lines
19011917
19021918
19031919 <p>
1904−Both right and left shifts fill with zeros the vacant bits.
1920+Both right and left shifts fill the vacant bits with zeros.
19051921 Negative displacements shift to the other direction;
19061922 displacements with absolute values equal to or higher than
19071923 the number of bits in an integer
@@ -1917,7 +1933,7 @@ types and representations at run time. · 9 unmodified lines
19171933 Most arithmetic operations applied to mixed numbers
19181934 (integers and floats) convert the integer operand to a float;
19191935 this is called the <em>usual rule</em>.
1920−Division always convert integer operands to floats;
1936+Float division always convert integer operands to floats;
19211937 integer division and bitwise operators
19221938 always convert float operands to integers.
19231939 The C API also converts both integers to floats and
@@ -1929,7 +1945,6 @@ besides strings. · 5 unmodified lines
19291945 <p>
19301946 Lua also converts strings to numbers,
19311947 whenever a number is expected.
1932−(This conversion may be deprecated in future versions.)
19331948
19341949
19351950 <p>
@@ -1951,16 +1966,16 @@ Otherwise, the conversion fails. · 15 unmodified lines
19511966
19521967
19531968 <p>
1954−The conversion from strings to floats
1955−follows the rules of the Lua lexer.
1956−(The string may have leading and trailing spaces and a sign.)
1957−There is no direct conversion from strings to integers:
1958−If a string is provided where an integer is needed,
1959−Lua converts the string to a float and then the float to an integer.
1969+The conversion from strings to numbers goes as follows:
1970+First, the string is converted to an integer or a float,
1971+following its syntax and the rules of the Lua lexer.
1972+(The string may have also leading and trailing spaces and a sign.)
1973+Then, the resulting number is converted to the required type
1974+(float or integer) according to the previous rules.
19601975
19611976
19621977 <p>
1963−The conversion from numbers to strings use a human-readable,
1978+The conversion from numbers to strings uses a human-readable,
19641979 non-specified format.
19651980 For complete control over how numbers are converted to strings,
19661981 use the <code>format</code> function from the string library
@@ -1971,11 +1986,15 @@ use the <code>format</code> function from the string library · 4 unmodified lines
19711986
19721987
19731988 <h3>3.4.4 &ndash; <a name="3.4.4">Relational Operators</a></h3><p>
1974−The relational operators in Lua are
1975−
1976−<pre>
1977− == ~= &lt; &gt; &lt;= &gt;=
1978−</pre><p>
1989+Lua supports the following relational operators:
1990+<table border="1">
1991+<tr><td><code>==</code></td><td>equality</td></tr>
1992+<tr><td><code>~=</code></td><td>inequality</td></tr>
1993+<tr><td><code>&lt;</code></td><td>less than</td></tr>
1994+<tr><td><code>&gt;</code></td><td>greater than</td></tr>
1995+<tr><td><code>&lt;=</code></td><td>less or equal</td></tr>
1996+<tr><td><code>&gt;=</code></td><td>greater or equal</td></tr>
1997+</table>
19791998 These operators always result in <b>false</b> or <b>true</b>.
19801999
19812000
@@ -1986,8 +2005,8 @@ Otherwise, the values of the operands are compared. · 4 unmodified lines
19862005 Strings are compared in the obvious way.
19872006 Numbers follow the usual rule for binary operations:
19882007 if both operands are integers,
1989−the are compared as integers.
1990−Otherwise, they are converted to floats
2008+the are compared as integers;
2009+otherwise, they are converted to floats
19912010 and compared as such.
19922011
19932012
@@ -2131,10 +2150,14 @@ from lower to higher priority: · 137 unmodified lines
21312150 or
21322151 and
21332152 &lt; &gt; &lt;= &gt;= ~= ==
2153+ |
2154+ ~
2155+ &amp;
2156+ &lt;&lt; &gt;&gt;
21342157 ..
21352158 + -
21362159 * / // %
2137− not # - (unary)
2160+ unary operators (not # - ~)
21382161 ^
21392162 </pre><p>
21402163 As usual,
@@ -2690,7 +2713,7 @@ The registry table is always located at pseudo-index · 549 unmodified lines
26902713 <a name="pdf-LUA_REGISTRYINDEX"><code>LUA_REGISTRYINDEX</code></a>,
26912714 which is a valid index.
26922715 Any C&nbsp;library can store data into this table,
2693−but it should take care to choose keys
2716+but it must take care to choose keys
26942717 that are different from those used
26952718 by other libraries, to avoid collisions.
26962719 Typically, you should use as key a string containing your library name,
@@ -2705,7 +2728,7 @@ uppercase letters are reserved for Lua. · 8 unmodified lines
27052728 The integer keys in the registry are used by the reference mechanism,
27062729 implemented by the auxiliary library,
27072730 and by some predefined values.
2708−Therefore, integer keys should not be used for other purposes.
2731+Therefore, integer keys must not be used for other purposes.
27092732
27102733
27112734 <p>
@@ -2760,18 +2783,18 @@ The panic function runs as if it were a message handler (see <a href="#2.3">&sec · 48 unmodified lines
27602783 in particular, the error message is at the top of the stack.
27612784 However, there is no guarantees about stack space.
27622785 To push anything on the stack,
2763−the panic function should first check the available space (see <a href="#4.2">&sect;4.2</a>).
2786+the panic function must first check the available space (see <a href="#4.2">&sect;4.2</a>).
27642787
27652788
27662789 <p>
2767−Most functions in the API can throw an error,
2790+Most functions in the API can raise an error,
27682791 for instance due to a memory allocation error.
27692792 The documentation for each function indicates whether
2770−it can throw errors.
2793+it can raise errors.
27712794
27722795
27732796 <p>
2774−Inside a C&nbsp;function you can throw an error by calling <a href="#lua_error"><code>lua_error</code></a>.
2797+Inside a C&nbsp;function you can raise an error by calling <a href="#lua_error"><code>lua_error</code></a>.
27752798
27762799
27772800
@@ -2781,7 +2804,7 @@ Inside a C&nbsp;function you can throw an error by calling <a href="#lua_error"> · 3 unmodified lines
27812804
27822805 <p>
27832806 Internally, Lua uses the C <code>longjmp</code> facility to yield a coroutine.
2784−Therefore, if a function <code>foo</code> calls an API function
2807+Therefore, if a C function <code>foo</code> calls an API function
27852808 and this API function yields
27862809 (directly or indirectly by calling another function that yields),
27872810 Lua cannot return to <code>foo</code> any more,
@@ -2823,6 +2846,67 @@ the continuation function should continue the task · 35 unmodified lines
28232846 of the original function.
28242847
28252848
2849+<p>
2850+As an illustration, consider the following function:
2851+
2852+<pre>
2853+ int original_function (lua_State *L) {
2854+ ... /* code 1 */
2855+ status = lua_pcall(L, n, m, h); /* calls Lua */
2856+ ... /* code 2 */
2857+ }
2858+</pre><p>
2859+Now we want to allow
2860+the Lua code being ran by <a href="#lua_pcall"><code>lua_pcall</code></a> to yield.
2861+First, we can rewrite our function like here:
2862+
2863+<pre>
2864+ int k (lua_State *L, int status, int ctx) {
2865+ ... /* code 2 */
2866+ }
2867+
2868+ int original_function (lua_State *L) {
2869+ ... /* code 1 */
2870+ return k(L, lua_pcall(L, n, m, h), ctx);
2871+ }
2872+</pre><p>
2873+In the above code,
2874+the new function <code>k</code> is a
2875+<em>continuation function</em> (with type <a href="#lua_KFunction"><code>lua_KFunction</code></a>),
2876+which should do all the work that the original function
2877+was doing after calling <a href="#lua_pcall"><code>lua_pcall</code></a>.
2878+Now, we must inform Lua that it must call <code>k</code> if the Lua code
2879+begin running by <a href="#lua_pcall"><code>lua_pcall</code></a> gets interrupted in some way
2880+(errors or yielding),
2881+so we rewrite the code as here,
2882+replacing <a href="#lua_pcall"><code>lua_pcall</code></a> by <a href="#lua_pcallk"><code>lua_pcallk</code></a>:
2883+
2884+<pre>
2885+ int original_function (lua_State *L) {
2886+ ... /* code 1 */
2887+ return k(L, lua_pcallk(L, n, m, h, ctx2, k), ctx1);
2888+ }
2889+</pre>
2890+
2891+<p>
2892+Besides the Lua state,
2893+the continuation function has two other parameters:
2894+the final status of the call plus the context value (<code>ctx</code>) that
2895+was passed originally to <a href="#lua_pcallk"><code>lua_pcallk</code></a>.
2896+(Lua does not use this context value;
2897+it only passes this value from the original function to the
2898+continuation function.)
2899+For <a href="#lua_pcallk"><code>lua_pcallk</code></a>,
2900+the status is the same value that would be returned by <a href="#lua_pcallk"><code>lua_pcallk</code></a>,
2901+except that it is <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> when being executed after an yield
2902+(instead of <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>).
2903+For <a href="#lua_yieldk"><code>lua_yieldk</code></a> and <a href="#lua_callk"><code>lua_callk</code></a>,
2904+the status is always <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> when Lua calls the continuation.
2905+(For these two functions,
2906+Lua will not call the continuation in case of errors,
2907+because they do not handle errors.)
2908+
2909+
28262910 <p>
28272911 Lua treats the continuation function as if it were the original function.
28282912 The continuation function receives the same Lua stack
@@ -2836,11 +2920,6 @@ Whatever it returns is handled by Lua as if it were the return · 7 unmodified lines
28362920 of the original function.
28372921
28382922
2839−<p>
2840−The only difference in the Lua state between the original function
2841−and its continuation is the result of a call to <a href="#lua_getctx"><code>lua_getctx</code></a>.
2842−
2843−
28442923
28452924
28462925
@@ -2867,10 +2946,10 @@ we cannot know how many elements the function pops/pushes · 20 unmodified lines
28672946 by looking only at its arguments
28682947 (e.g., they may depend on what is on the stack).
28692948 The third field, <code>x</code>,
2870−tells whether the function may throw errors:
2871−'<code>-</code>' means the function never throws any error;
2872−'<code>e</code>' means the function may throw errors;
2873−'<code>v</code>' means the function may throw an error on purpose.
2949+tells whether the function may raise errors:
2950+'<code>-</code>' means the function never raises any error;
2951+'<code>e</code>' means the function may raise errors;
2952+'<code>v</code>' means the function may raise an error on purpose.
28742953
28752954
28762955
@@ -2928,13 +3007,13 @@ Lua assumes the following behavior from the allocator function: · 51 unmodified lines
29283007
29293008 <p>
29303009 When <code>nsize</code> is zero,
2931−the allocator should behave like <code>free</code>
3010+the allocator must behave like <code>free</code>
29323011 and return <code>NULL</code>.
29333012
29343013
29353014 <p>
29363015 When <code>nsize</code> is not zero,
2937−the allocator should behave like <code>realloc</code>.
3016+the allocator must behave like <code>realloc</code>.
29383017 The allocator returns <code>NULL</code>
29393018 if and only if it cannot fulfill the request.
29403019 Lua assumes that the allocator never fails when
@@ -2990,7 +3069,7 @@ The value of <code>op</code> must be one of the following constants: · 49 unmodified lines
29903069 <li><b><a name="pdf-LUA_OPADD"><code>LUA_OPADD</code></a>: </b> performs addition (<code>+</code>)</li>
29913070 <li><b><a name="pdf-LUA_OPSUB"><code>LUA_OPSUB</code></a>: </b> performs subtraction (<code>-</code>)</li>
29923071 <li><b><a name="pdf-LUA_OPMUL"><code>LUA_OPMUL</code></a>: </b> performs multiplication (<code>*</code>)</li>
2993−<li><b><a name="pdf-LUA_OPDIV"><code>LUA_OPDIV</code></a>: </b> performs division (<code>/</code>)</li>
3072+<li><b><a name="pdf-LUA_OPDIV"><code>LUA_OPDIV</code></a>: </b> performs float division (<code>/</code>)</li>
29943073 <li><b><a name="pdf-LUA_OPIDIV"><code>LUA_OPIDIV</code></a>: </b> performs integer division (<code>//</code>)</li>
29953074 <li><b><a name="pdf-LUA_OPMOD"><code>LUA_OPMOD</code></a>: </b> performs modulo (<code>%</code>)</li>
29963075 <li><b><a name="pdf-LUA_OPPOW"><code>LUA_OPPOW</code></a>: </b> performs exponentiation (<code>^</code>)</li>
@@ -3081,7 +3160,7 @@ This is considered good programming practice. · 84 unmodified lines
30813160 <hr><h3><a name="lua_callk"><code>lua_callk</code></a></h3><p>
30823161 <span class="apii">[-(nargs + 1), +nresults, <em>e</em>]</span>
30833162 <pre>void lua_callk (lua_State *L, int nargs, int nresults, int ctx,
3084− lua_CFunction k);</pre>
3163+ lua_KFunction k);</pre>
30853164
30863165 <p>
30873166 This function behaves exactly like <a href="#lua_call"><code>lua_call</code></a>,
@@ -3258,7 +3337,10 @@ Otherwise you can use the function <a href="#lua_newtable"><code>lua_newtable</c · 170 unmodified lines
32583337
32593338 <hr><h3><a name="lua_dump"><code>lua_dump</code></a></h3><p>
32603339 <span class="apii">[-0, +0, <em>e</em>]</span>
3261−<pre>int lua_dump (lua_State *L, lua_Writer writer, void *data);</pre>
3340+<pre>int lua_dump (lua_State *L,
3341+ lua_Writer writer,
3342+ void *data,
3343+ int strip);</pre>
32623344
32633345 <p>
32643346 Dumps a function as a binary chunk.
@@ -3272,6 +3354,12 @@ with the given <code>data</code> · 7 unmodified lines
32723354 to write them.
32733355
32743356
3357+<p>
3358+If <code>strip</code> is true,
3359+the binary representation is created without debug information
3360+about the function.
3361+
3362+
32753363 <p>
32763364 The value returned is the error code returned by the last
32773365 call to the writer;
@@ -3291,8 +3379,7 @@ This function does not pop the Lua function from the stack. · 13 unmodified lines
32913379
32923380 <p>
32933381 Generates a Lua error.
3294−The error message (which can actually be a Lua value of any type)
3295−must be on the stack top.
3382+The error object must be on the stack top.
32963383 This function does a long jump,
32973384 and therefore never returns
32983385 (see <a href="#luaL_error"><code>luaL_error</code></a>).
@@ -3380,45 +3467,9 @@ opaque pointer passed to <a href="#lua_newstate"><code>lua_newstate</code></a>. · 81 unmodified lines
33803467
33813468
33823469
3383−<hr><h3><a name="lua_getctx"><code>lua_getctx</code></a></h3><p>
3384−<span class="apii">[-0, +0, &ndash;]</span>
3385−<pre>int lua_getctx (lua_State *L, int *ctx);</pre>
3386−
3387−<p>
3388−This function is called by a continuation function (see <a href="#4.7">&sect;4.7</a>)
3389−to retrieve the status of the thread and a context information.
3390−
3391−
3392−<p>
3393−When called in the original function,
3394−<a href="#lua_getctx"><code>lua_getctx</code></a> always returns <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>
3395−and does not change the value of its argument <code>ctx</code>.
3396−When called inside a continuation function,
3397−<a href="#lua_getctx"><code>lua_getctx</code></a> returns <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> and sets
3398−the value of <code>ctx</code> to be the context information
3399−(the value passed as the <code>ctx</code> argument
3400−to the callee together with the continuation function).
3401−
3402−
3403−<p>
3404−When the callee is <a href="#lua_pcallk"><code>lua_pcallk</code></a>,
3405−Lua may also call its continuation function
3406−to handle errors during the call.
3407−That is, upon an error in the function called by <a href="#lua_pcallk"><code>lua_pcallk</code></a>,
3408−Lua may not return to the original function
3409−but instead may call the continuation function.
3410−In that case, a call to <a href="#lua_getctx"><code>lua_getctx</code></a> will return the error code
3411−(the value that would be returned by <a href="#lua_pcallk"><code>lua_pcallk</code></a>);
3412−the value of <code>ctx</code> will be set to the context information,
3413−as in the case of a yield.
3414−
3415−
3416−
3417−
3418−
34193470 <hr><h3><a name="lua_getfield"><code>lua_getfield</code></a></h3><p>
34203471 <span class="apii">[-0, +1, <em>e</em>]</span>
3421−<pre>void lua_getfield (lua_State *L, int index, const char *k);</pre>
3472+<pre>int lua_getfield (lua_State *L, int index, const char *k);</pre>
34223473
34233474 <p>
34243475 Pushes onto the stack the value <code>t[k]</code>,
@@ -3427,6 +3478,10 @@ As in Lua, this function may trigger a metamethod · 2 unmodified lines
34273478 for the "index" event (see <a href="#2.4">&sect;2.4</a>).
34283479
34293480
3481+<p>
3482+Returns the type of the pushed value.
3483+
3484+
34303485
34313486
34323487
@@ -3495,13 +3550,17 @@ this result is equal to the number of elements in the stack · 62 unmodified lines
34953550
34963551 <hr><h3><a name="lua_getuservalue"><code>lua_getuservalue</code></a></h3><p>
34973552 <span class="apii">[-0, +1, &ndash;]</span>
3498−<pre>void lua_getuservalue (lua_State *L, int index);</pre>
3553+<pre>int lua_getuservalue (lua_State *L, int index);</pre>
34993554
35003555 <p>
35013556 Pushes onto the stack the Lua value associated with the userdata
35023557 at the given index.
35033558
35043559
3560+<p>
3561+Returns the type of the pushed value.
3562+
3563+
35053564
35063565
35073566
@@ -3520,7 +3579,7 @@ because a pseudo-index is not an actual stack position. · 12 unmodified lines
35203579
35213580
35223581 <hr><h3><a name="lua_Integer"><code>lua_Integer</code></a></h3>
3523−<pre>typedef long long int lua_Integer;</pre>
3582+<pre>typedef ... lua_Integer;</pre>
35243583
35253584 <p>
35263585 The type of integers in Lua.
@@ -3534,6 +3593,12 @@ to <code>long</code> or <code>int</code> · 7 unmodified lines
35343593 (usually a 32-bit two-complement integer).
35353594
35363595
3596+<p>
3597+Lua also defines the constants
3598+<a name="pdf-LUA_MININTEGER"><code>LUA_MININTEGER</code></a> and <a name="pdf-LUA_MAXINTEGER"><code>LUA_MAXINTEGER</code></a>,
3599+with the minimum and the maximum values that fit in this type.
3600+
3601+
35373602
35383603
35393604
@@ -3697,6 +3762,28 @@ Returns 1 if the value at the given index is a userdata · 157 unmodified lines
36973762
36983763
36993764
3765+<hr><h3><a name="lua_isyieldable"><code>lua_isyieldable</code></a></h3><p>
3766+<span class="apii">[-0, +0, &ndash;]</span>
3767+<pre>int lua_isyieldable (lua_State *L);</pre>
3768+
3769+<p>
3770+Returns 1 if the given coroutine can yield,
3771+and 0&nbsp;otherwise.
3772+
3773+
3774+
3775+
3776+
3777+<hr><h3><a name="lua_KFunction"><code>lua_KFunction</code></a></h3>
3778+<pre>typedef int (*lua_KFunction) (lua_State *L, int status, int ctx);</pre>
3779+
3780+<p>
3781+Type for continuation functions (see <a href="#4.7">&sect;4.7</a>).
3782+
3783+
3784+
3785+
3786+
37003787 <hr><h3><a name="lua_len"><code>lua_len</code></a></h3><p>
37013788 <span class="apii">[-0, +1, <em>e</em>]</span>
37023789 <pre>void lua_len (lua_State *L, int index);</pre>
@@ -3768,7 +3855,7 @@ a <code>NULL</code> value is equivalent to the string "<code>bt</code>". · 65 unmodified lines
37683855
37693856 <p>
37703857 <code>lua_load</code> uses the stack internally,
3771−so the reader function should always leave the stack
3858+so the reader function must always leave the stack
37723859 unmodified when returning.
37733860
37743861
@@ -3907,6 +3994,28 @@ but that can be changed in <code>luaconf.h</code> to a single float. · 132 unmodified lines
39073994
39083995
39093996
3997+<hr><h3><a name="lua_numtointeger"><code>lua_numtointeger</code></a></h3>
3998+<pre>int lua_numtointeger (lua_Number n, lua_Integer *p);</pre>
3999+
4000+<p>
4001+Converts a Lua float to a Lua integer.
4002+This macro assumes that <code>n</code> has an integral value.
4003+If that value is within the range of Lua integers,
4004+it is converted to an integer and assigned to <code>*p</code>.
4005+The macro results in a boolean indicating whether the
4006+conversion was successful.
4007+(Note that this range test can be tricky to do
4008+correctly without this macro,
4009+due to roundings.)
4010+
4011+
4012+<p>
4013+This macro may evaluate its arguments more than once.
4014+
4015+
4016+
4017+
4018+
39104019 <hr><h3><a name="lua_pcall"><code>lua_pcall</code></a></h3><p>
39114020 <span class="apii">[-(nargs + 1), +(nresults|1), &ndash;]</span>
39124021 <pre>int lua_pcall (lua_State *L, int nargs, int nresults, int msgh);</pre>
@@ -3950,7 +4059,7 @@ since by then the stack has unwound. · 37 unmodified lines
39504059
39514060
39524061 <p>
3953−The <a href="#lua_pcall"><code>lua_pcall</code></a> function returns one of the following codes
4062+The <a href="#lua_pcall"><code>lua_pcall</code></a> function returns one of the following constants
39544063 (defined in <code>lua.h</code>):
39554064
39564065 <ul>
@@ -3973,8 +4082,7 @@ error while running the message handler. · 16 unmodified lines
39734082
39744083 <li><b><a name="pdf-LUA_ERRGCMM"><code>LUA_ERRGCMM</code></a>: </b>
39754084 error while running a <code>__gc</code> metamethod.
3976−(This error typically has no relation with the function being called.
3977−It is generated by the garbage collector.)
4085+(This error typically has no relation with the function being called.)
39784086 </li>
39794087
39804088 </ul>
@@ -3989,7 +4097,7 @@ It is generated by the garbage collector.) · 8 unmodified lines
39894097 int nresults,
39904098 int errfunc,
39914099 int ctx,
3992− lua_CFunction k);</pre>
4100+ lua_KFunction k);</pre>
39934101
39944102 <p>
39954103 This function behaves exactly like <a href="#lua_pcall"><code>lua_pcall</code></a>,
@@ -4035,11 +4143,11 @@ it is possible to associate some values with it, · 39 unmodified lines
40354143 thus creating a C&nbsp;closure (see <a href="#4.4">&sect;4.4</a>);
40364144 these values are then accessible to the function whenever it is called.
40374145 To associate values with a C&nbsp;function,
4038−first these values should be pushed onto the stack
4146+first these values must be pushed onto the stack
40394147 (when there are multiple values, the first value is pushed first).
40404148 Then <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a>
40414149 is called to create and push the C&nbsp;function onto the stack,
4042−with the argument <code>n</code> telling how many values should be
4150+with the argument <code>n</code> telling how many values will be
40434151 associated with the function.
40444152 <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a> also pops these values from the stack.
40454153
@@ -4052,7 +4160,7 @@ The maximum value for <code>n</code> is 255. · 6 unmodified lines
40524160 When <code>n</code> is zero,
40534161 this function creates a <em>light C function</em>,
40544162 which is just a pointer to the C&nbsp;function.
4055−In that case, it never throws a memory error.
4163+In that case, it never raises a memory error.
40564164
40574165
40584166
@@ -4540,6 +4648,22 @@ this parameter can be <code>NULL</code>. · 481 unmodified lines
45404648
45414649
45424650
4651+<hr><h3><a name="lua_rotate"><code>lua_rotate</code></a></h3><p>
4652+<span class="apii">[-0, +0, &ndash;]</span>
4653+<pre>void lua_rotate (lua_State *L, int idx, int n);</pre>
4654+
4655+<p>
4656+Rotates the stack elements from <code>idx</code> to the top <code>n</code> positions
4657+in the direction of the top, for a positive <code>n</code>,
4658+or <code>-n</code> positions in the direction of the bottom,
4659+for a negative <code>n</code>.
4660+The absolute value of <code>n</code> must not be greater than the size
4661+of the slice being rotated.
4662+
4663+
4664+
4665+
4666+
45434667 <hr><h3><a name="lua_setallocf"><code>lua_setallocf</code></a></h3><p>
45444668 <span class="apii">[-0, +0, &ndash;]</span>
45454669 <pre>void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);</pre>
@@ -4689,11 +4813,13 @@ You can resume threads with status <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> · 143 unmodified lines
46894813
46904814 <hr><h3><a name="lua_strtonum"><code>lua_strtonum</code></a></h3><p>
46914815 <span class="apii">[-0, +1, &ndash;]</span>
4692−<pre>int lua_strtonum (lua_State *L, const char *s, size_t len);</pre>
4816+<pre>size_t lua_strtonum (lua_State *L, const char *s);</pre>
46934817
46944818 <p>
4695−Converts the string <code>s</code> (with length <code>len</code>) to a number and
4696−pushes that number into the stack.
4819+Converts the zero-terminated string <code>s</code> to a number,
4820+pushes that number into the stack,
4821+and returns the total size of the string
4822+(that is, its length plus one).
46974823 The conversion can result in an integer or a float,
46984824 according to the lexical conventions of Lua (see <a href="#3.1">&sect;3.1</a>).
46994825 The string may have leading and trailing spaces and a sign.
@@ -4969,12 +5095,17 @@ which must be one the values returned by <a href="#lua_type"><code>lua_type</cod · 269 unmodified lines
49695095
49705096
49715097 <hr><h3><a name="lua_Unsigned"><code>lua_Unsigned</code></a></h3>
4972−<pre>typedef unsigned long lua_Unsigned;</pre>
5098+<pre>typedef ... lua_Unsigned;</pre>
49735099
49745100 <p>
49755101 The unsigned version of <a href="#lua_Integer"><code>lua_Integer</code></a>.
49765102
49775103
5104+<p>
5105+Lua also defines the constant <a name="pdf-LUA_MAXUNSIGNED"><code>LUA_MAXUNSIGNED</code></a>,
5106+with the maximum value that fits in this type.
5107+
5108+
49785109
49795110
49805111
@@ -5063,7 +5194,7 @@ the function calling <code>lua_yield</code>. · 82 unmodified lines
50635194
50645195 <hr><h3><a name="lua_yieldk"><code>lua_yieldk</code></a></h3><p>
50655196 <span class="apii">[-?, +?, &ndash;]</span>
5066−<pre>int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k);</pre>
5197+<pre>int lua_yieldk (lua_State *L, int nresults, int ctx, lua_KFunction k);</pre>
50675198
50685199 <p>
50695200 Yields a coroutine.
@@ -5074,13 +5205,13 @@ This function should only be called as the · 4 unmodified lines
50745205 return expression of a C&nbsp;function, as follows:
50755206
50765207 <pre>
5077− return lua_yieldk (L, n, i, k);
5208+ return lua_yieldk (L, n, ctx, k);
50785209 </pre><p>
50795210 When a C&nbsp;function calls <a href="#lua_yieldk"><code>lua_yieldk</code></a> in that way,
50805211 the running coroutine suspends its execution,
50815212 and the call to <a href="#lua_resume"><code>lua_resume</code></a> that started this coroutine returns.
50825213 The parameter <code>nresults</code> is the number of values from the stack
5083−that are passed as results to <a href="#lua_resume"><code>lua_resume</code></a>.
5214+that will be passed as results to <a href="#lua_resume"><code>lua_resume</code></a>.
50845215
50855216
50865217 <p>
@@ -5089,11 +5220,11 @@ Lua calls the given continuation function <code>k</code> to continue · 2 unmodified lines
50895220 the execution of the C function that yielded (see <a href="#4.7">&sect;4.7</a>).
50905221 This continuation function receives the same stack
50915222 from the previous function,
5092−with the results removed and
5223+with the <code>n</code> results removed and
50935224 replaced by the arguments passed to <a href="#lua_resume"><code>lua_resume</code></a>.
50945225 Moreover,
5095−the continuation function may access the value <code>ctx</code>
5096−by calling <a href="#lua_getctx"><code>lua_getctx</code></a>.
5226+the continuation function receives the value <code>ctx</code>
5227+that was passed to <a href="#lua_yieldk"><code>lua_yieldk</code></a>.
50975228
50985229
50995230
@@ -5326,6 +5457,11 @@ numbers of the lines that are valid on the function. · 226 unmodified lines
53265457 (A <em>valid line</em> is a line with some associated code,
53275458 that is, a line where you can put a break point.
53285459 Non-valid lines include empty lines and comments.)
5460+
5461+
5462+<p>
5463+If this option is given together with option '<code>f</code>',
5464+its table is pushed after the function.
53295465 </li>
53305466
53315467 </ul>
@@ -5363,7 +5499,7 @@ and returns its name. · 31 unmodified lines
53635499
53645500
53655501 <p>
5366−In the second case, <code>ar</code> should be <code>NULL</code> and the function
5502+In the second case, <code>ar</code> must be <code>NULL</code> and the function
53675503 to be inspected must be at the top of the stack.
53685504 In this case, only parameters of Lua functions are visible
53695505 (as there is no information about what variables are active)
@@ -5656,7 +5792,7 @@ you should not use these functions for other stack values. · 286 unmodified lines
56565792
56575793 <p>
56585794 Functions called <code>luaL_check*</code>
5659−always throw an error if the check is not satisfied.
5795+always raise an error if the check is not satisfied.
56605796
56615797
56625798
@@ -6398,7 +6534,7 @@ returns 0. · 735 unmodified lines
63986534 Otherwise,
63996535 creates a new table to be used as a metatable for userdata,
64006536 adds to this new table the pair <code>__name = tname</code>,
6401−adds to the registry the pair <code></code>[tname] = new table,
6537+adds to the registry the pair <code>[tname] = new table</code>,
64026538 and returns 1.
64036539
64046540
@@ -6762,7 +6898,7 @@ the field value is changed to <b>nil</b> to signal that the handle is closed. · 357 unmodified lines
67626898 <p>
67636899 This function works like <a href="#luaL_checkudata"><code>luaL_checkudata</code></a>,
67646900 except that, when the test fails,
6765−it returns <code>NULL</code> instead of throwing an error.
6901+it returns <code>NULL</code> instead of raising an error.
67666902
67676903
67686904
@@ -6862,6 +6998,8 @@ This function is used to build a prefix for error messages. · 93 unmodified lines
68626998
68636999
68647000
7001+
7002+
68657003 <h1>6 &ndash; <a name="6">Standard Libraries</a></h1>
68667004
68677005 <p>
@@ -6939,11 +7077,15 @@ implementations for some of its facilities. · 71 unmodified lines
69397077
69407078 <p>
69417079 <hr><h3><a name="pdf-assert"><code>assert (v [, message])</code></a></h3>
6942−Issues an error when
7080+
7081+
7082+<p>
7083+Calls <a href="#pdf-error"><code>error</code></a> if
69437084 the value of its argument <code>v</code> is false (i.e., <b>nil</b> or <b>false</b>);
69447085 otherwise, returns all its arguments.
6945−<code>message</code> is an error message;
6946−when absent, it defaults to "assertion failed!"
7086+In case of error,
7087+<code>message</code> is the error object;
7088+when absent, it defaults to "<code>assertion failed!</code>"
69477089
69487090
69497091
@@ -7009,16 +7151,6 @@ returns a boolean that tells whether the collector is running · 59 unmodified lines
70097151 (i.e., not stopped).
70107152 </li>
70117153
7012−<li><b>"<code>generational</code>": </b>
7013−changes the collector to generational mode.
7014−This is an experimental feature (see <a href="#2.5">&sect;2.5</a>).
7015−</li>
7016−
7017−<li><b>"<code>incremental</code>": </b>
7018−changes the collector to incremental mode.
7019−This is the default mode.
7020−</li>
7021−
70227154 </ul>
70237155
70247156
@@ -7038,7 +7170,7 @@ to its caller (that is, <code>dofile</code> does not run in protected mode). · 13 unmodified lines
70387170 <p>
70397171 <hr><h3><a name="pdf-error"><code>error (message [, level])</code></a></h3>
70407172 Terminates the last protected function called
7041−and returns <code>message</code> as the error message.
7173+and returns <code>message</code> as the error object.
70427174 Function <code>error</code> never returns.
70437175
70447176
@@ -7375,7 +7507,7 @@ according to the lexical conventions of Lua (see <a href="#3.1">&sect;3.1</a>). · 330 unmodified lines
73757507
73767508 <p>
73777509 When called with <code>base</code>,
7378−then <code>e</code> should be a string to be interpreted as
7510+then <code>e</code> must be a string to be interpreted as
73797511 an integer numeral in that base.
73807512 The base may be any integer between 2 and 36, inclusive.
73817513 In bases above&nbsp;10, the letter '<code>A</code>' (in either upper or lower case)
@@ -7466,6 +7598,21 @@ an object with type <code>"thread"</code>. · 84 unmodified lines
74667598
74677599
74687600
7601+<p>
7602+<hr><h3><a name="pdf-coroutine.isyieldable"><code>coroutine.isyieldable ()</code></a></h3>
7603+
7604+
7605+<p>
7606+Returns true when the running coroutine can yield.
7607+
7608+
7609+<p>
7610+A running coroutine is yieldable if it is not the main thread and
7611+it is not inside a non-yieldable C function.
7612+
7613+
7614+
7615+
74697616 <p>
74707617 <hr><h3><a name="pdf-coroutine.resume"><code>coroutine.resume (co [, val1, &middot;&middot;&middot;])</code></a></h3>
74717618
@@ -7485,8 +7632,8 @@ as the results from the yield. · 13 unmodified lines
74857632 <p>
74867633 If the coroutine runs without any errors,
74877634 <code>resume</code> returns <b>true</b> plus any values passed to <code>yield</code>
7488−(if the coroutine yields) or any values returned by the body function
7489−(if the coroutine terminates).
7635+(when the coroutine yields) or any values returned by the body function
7636+(when the coroutine terminates).
74907637 If there is any error,
74917638 <code>resume</code> returns <b>false</b> plus the error message.
74927639
@@ -7588,7 +7735,7 @@ for <a href="#pdf-package.searchers"><code>package.searchers</code></a>. · 95 unmodified lines
75887735 <p>
75897736 First <code>require</code> queries <code>package.preload[modname]</code>.
75907737 If it has a value,
7591−this value (which should be a function) is the loader.
7738+this value (which must be a function) is the loader.
75927739 Otherwise <code>require</code> searches for a Lua loader using the
75937740 path stored in <a href="#pdf-package.path"><code>package.path</code></a>.
75947741 If that also fails, it searches for a C&nbsp;loader using the
@@ -7663,7 +7810,7 @@ The path used by <a href="#pdf-require"><code>require</code></a> to search for a · 68 unmodified lines
76637810 <p>
76647811 Lua initializes the C&nbsp;path <a href="#pdf-package.cpath"><code>package.cpath</code></a> in the same way
76657812 it initializes the Lua path <a href="#pdf-package.path"><code>package.path</code></a>,
7666−using the environment variable <a name="pdf-LUA_CPATH_5_2"><code>LUA_CPATH_5_2</code></a>
7813+using the environment variable <a name="pdf-LUA_CPATH_5_3"><code>LUA_CPATH_5_3</code></a>
76677814 or the environment variable <a name="pdf-LUA_CPATH"><code>LUA_CPATH</code></a>
76687815 or a default path defined in <code>luaconf.h</code>.
76697816
@@ -7741,7 +7888,7 @@ The path used by <a href="#pdf-require"><code>require</code></a> to search for a · 71 unmodified lines
77417888
77427889 <p>
77437890 At start-up, Lua initializes this variable with
7744−the value of the environment variable <a name="pdf-LUA_PATH_5_2"><code>LUA_PATH_5_2</code></a> or
7891+the value of the environment variable <a name="pdf-LUA_PATH_5_3"><code>LUA_PATH_5_3</code></a> or
77457892 the environment variable <a name="pdf-LUA_PATH"><code>LUA_PATH</code></a> or
77467893 with a default path defined in <code>luaconf.h</code>,
77477894 if those environment variables are not defined.
@@ -7973,6 +8120,45 @@ about the function · 225 unmodified lines
79738120
79748121
79758122
8123+<p>
8124+<hr><h3><a name="pdf-string.dumpfloat"><code>string.dumpfloat (n [, size [, endianness]])</code></a></h3>
8125+Returns a string with the machine representation of float <code>n</code>,
8126+with given size and endianness.
8127+The <code>size</code> is the string "<code>f</code>" (single precision),
8128+"<code>d</code>" (double precision), or "<code>n</code>",
8129+which means the size of a <a href="#lua_Number"><code>lua_Number</code></a>;
8130+its default is "<code>n</code>".
8131+The endianness is the string "<code>l</code>" (little endian), "<code>b</code>" (big endian),
8132+or "<code>n</code>" (native);
8133+the default is the native endianness.
8134+
8135+
8136+<p>
8137+This function may not work correctly in architectures
8138+with mixed endian.
8139+
8140+
8141+<p>
8142+<hr><h3><a name="pdf-string.dumpint"><code>string.dumpint (n [, size [, endianness]])</code></a></h3>
8143+Returns a string with the two-complement representation of integer <code>n</code>,
8144+with <code>size</code> bytes and given endianness.
8145+The <code>size</code> can be any value from 1 to 12,
8146+or 0, which means the size of a <a href="#lua_Integer"><code>lua_Integer</code></a>
8147+(8 bytes in standard Lua);
8148+its default is zero.
8149+The endianness is the string "<code>l</code>" (little endian), "<code>b</code>" (big endian),
8150+or "<code>n</code>" (native);
8151+the default is the native endianness.
8152+
8153+
8154+<p>
8155+This function may not work correctly in architectures
8156+with mixed endian or
8157+that do not use a two-complement representation for integers.
8158+
8159+
8160+
8161+
79768162 <p>
79778163 <hr><h3><a name="pdf-string.find"><code>string.find (s, pattern [, init [, plain]])</code></a></h3>
79788164
@@ -8195,45 +8381,6 @@ its default value is&nbsp;1 and can be negative. · 216 unmodified lines
81958381
81968382
81978383
8198−<p>
8199−<hr><h3><a name="pdf-string.packfloat"><code>string.packfloat (n [, size [, endianess]])</code></a></h3>
8200−Returns a string with the machine representation of float <code>n</code>,
8201−with given size and endianess.
8202−The <code>size</code> is the string "<code>f</code>" (single precision),
8203−"<code>d</code>" (double precision), or "<code>n</code>",
8204−which means the size of a <a href="#lua_Number"><code>lua_Number</code></a>;
8205−its default is St{n}.
8206−The endianess is the string "<code>l</code>" (little endian), "<code>b</code>" (big endian),
8207−or "<code>n</code>" (native);
8208−the default is the native endianess.
8209−
8210−
8211−<p>
8212−(This function may not work correctly for non-native endianess
8213−in mixed-endian architectures.)
8214−
8215−
8216−
8217−
8218−<p>
8219−<hr><h3><a name="pdf-string.packint"><code>string.packint (n [, size [, endianess]])</code></a></h3>
8220−Returns a string with the machine representation of integer <code>n</code>,
8221−with <code>size</code> bytes and given endianess.
8222−The <code>size</code> can be any value from 1 to 8 or 0,
8223−which means the size of a <a href="#lua_Integer"><code>lua_Integer</code></a>;
8224−its default is zero.
8225−The endianess is the string "<code>l</code>" (little endian), "<code>b</code>" (big endian),
8226−or "<code>n</code>" (native);
8227−the default is the native endianess.
8228−
8229−
8230−<p>
8231−(This function may not work correctly for native endianess
8232−in mixed-endian architectures.)
8233−
8234−
8235−
8236−
82378384 <p>
82388385 <hr><h3><a name="pdf-string.rep"><code>string.rep (s, n [, sep])</code></a></h3>
82398386 Returns a string that is the concatenation of <code>n</code> copies of
@@ -8279,19 +8426,19 @@ the function returns the empty string. · 39 unmodified lines
82798426
82808427
82818428 <p>
8282−<hr><h3><a name="pdf-string.unpackfloat"><code>string.unpackfloat (s [, pos [, size [, endianess]]])</code></a></h3>
8429+<hr><h3><a name="pdf-string.undumpfloat"><code>string.undumpfloat (s [, pos [, size [, endianness]]])</code></a></h3>
82838430 Reads the machine representation of a float starting at position
82848431 <code>pos</code> in string <code>s</code> and returns that number.
8285−See <a href="#pdf-string.packfloat"><code>string.packfloat</code></a> for details about <code>size</code> and <code>endianess</code>.
8432+See <a href="#pdf-string.dumpfloat"><code>string.dumpfloat</code></a> for details about <code>size</code> and <code>endianness</code>.
82868433
82878434
82888435
82898436
82908437 <p>
8291−<hr><h3><a name="pdf-string.unpackint"><code>string.unpackint (s [, pos [, size [, endianess]]])</code></a></h3>
8438+<hr><h3><a name="pdf-string.undumpint"><code>string.undumpint (s [, pos [, size [, endianness]]])</code></a></h3>
82928439 Reads the machine representation of an integer starting at position
82938440 <code>pos</code> in string <code>s</code> and returns that integer.
8294−See <a href="#pdf-string.packint"><code>string.packint</code></a> for details about <code>size</code> and <code>endianess</code>.
8441+See <a href="#pdf-string.dumpint"><code>string.dumpint</code></a> for details about <code>size</code> and <code>endianness</code>.
82958442
82968443
82978444 <p>
@@ -8517,7 +8664,7 @@ such as character classification, is outside its scope. · 219 unmodified lines
85178664
85188665 <p>
85198666 Unless stated otherwise,
8520−all functions that expect a byte index as a parameter
8667+all functions that expect a byte position as a parameter
85218668 assume that the given position is either the start of a byte sequence
85228669 or one plus the length of the subject string.
85238670 As in the string library,
@@ -8572,24 +8719,29 @@ It raises an error if it meets any invalid byte sequence. · 48 unmodified lines
85728719
85738720
85748721 <p>
8575−<hr><h3><a name="pdf-utf8.len"><code>utf8.len (s [, i])</code></a></h3>
8576−Returns the number of UTF-8 characters in string <code>s</code>,
8577−starting from position <code>i</code>.
8578−The default for <code>i</code> is 1.
8579−Returns <b>nil</b> if the sufix of <code>s</code> starting at <code>i</code>
8580−contains any invalid byte sequence.
8722+<hr><h3><a name="pdf-utf8.len"><code>utf8.len (s [, i [, j]])</code></a></h3>
8723+Returns the number of UTF-8 characters in string <code>s</code>
8724+that start between positions <code>i</code> and @{j} (both inclusive).
8725+The default for <code>i</code> is 1 and for <code>j</code> is -1.
8726+If it finds any invalid byte sequence,
8727+returns <b>nil</b> plus the position of the first invalid byte.
85818728
85828729
85838730
85848731
85858732 <p>
85868733 <hr><h3><a name="pdf-utf8.offset"><code>utf8.offset (s, n [, i])</code></a></h3>
8587−Returns the byte index where the encoding of the
8588−<code>n</code>-th character of <code>s</code> starts,
8589−counting from position <code>i</code>.
8734+Returns the position (in bytes) where the encoding of the
8735+<code>n</code>-th character of <code>s</code>
8736+(counting from position <code>i</code>) starts.
85908737 A negative <code>n</code> gets characters before position <code>i</code>.
8591−The default for <code>i</code> is 1.
8592−Returns <b>nil</b> if the subject does not have such character.
8738+The default for <code>i</code> is 1 when <code>n</code> is non-negative
8739+and <code>#s + 1</code> otherwise,
8740+so that <code>utf8.offset(s, -n)</code> gets the offset of the
8741+<code>n</code>-th character from the end of the string.
8742+If the specified character is not in the subject
8743+or right after its end,
8744+the function returns <b>nil</b>.
85938745
85948746
85958747 <p>
@@ -8616,7 +8768,7 @@ It provides all its functions inside the table <a name="pdf-table"><code>table</ · 20 unmodified lines
86168768
86178769 <p>
86188770 Remember that, whenever an operation needs the length of a table,
8619−the table should be a proper sequence
8771+the table must be a proper sequence
86208772 or have a <code>__len</code> metamethod (see <a href="#3.4.7">&sect;3.4.7</a>).
86218773 All functions ignore non-numeric keys
86228774 in tables given as arguments.
@@ -8739,10 +8891,15 @@ By default, <code>i</code> is&nbsp;1 and <code>j</code> is <code>#list</code>. · 116 unmodified lines
87398891 <h2>6.7 &ndash; <a name="6.7">Mathematical Functions</a></h2>
87408892
87418893 <p>
8742−This library is an interface to the standard C&nbsp;math library.
8743−It provides all its functions inside the table <a name="pdf-math"><code>math</code></a>.
8744−Unless stated otherwise,
8745−all functions in this library operate with and return floats.
8894+This library provides basic mathematical functions.
8895+It provides all its functions and constants inside the table <a name="pdf-math"><code>math</code></a>.
8896+Functions with the annotation "<code>integer/float</code>" give
8897+integer results for integer arguments
8898+and float results for float (or mixed) arguments.
8899+Rounding functions
8900+(<a href="#pdf-math.ceil"><code>math.ceil</code></a>, <a href="#pdf-math.floor"><code>math.floor</code></a>, and <a href="#pdf-math.modf"><code>math.modf</code></a>)
8901+return an integer when the result fits in the range of an integer,
8902+otherwise they return a float.
87468903
87478904
87488905 <p>
@@ -8750,7 +8907,7 @@ all functions in this library operate with and return floats. · 1 unmodified line
87508907
87518908
87528909 <p>
8753−Returns the absolute value of <code>x</code>.
8910+Returns the absolute value of <code>x</code>. (integer/float)
87548911
87558912
87568913
@@ -8776,54 +8933,41 @@ Returns the arc sine of <code>x</code> (in radians). · 19 unmodified lines
87768933
87778934
87788935 <p>
8779−<hr><h3><a name="pdf-math.atan"><code>math.atan (x)</code></a></h3>
8936+<hr><h3><a name="pdf-math.atan"><code>math.atan (y [, x])</code></a></h3>
87808937
87818938
87828939 <p>
8783−Returns the arc tangent of <code>x</code> (in radians).
8784−
8785−
87868940
8787−
8788−<p>
8789−<hr><h3><a name="pdf-math.atan2"><code>math.atan2 (y, x)</code></a></h3>
8790−
8791−
8792−<p>
87938941 Returns the arc tangent of <code>y/x</code> (in radians),
87948942 but uses the signs of both parameters to find the
87958943 quadrant of the result.
87968944 (It also handles correctly the case of <code>x</code> being zero.)
87978945
87988946
8799−
8800−
8801−<p>
8802−<hr><h3><a name="pdf-math.ceil"><code>math.ceil (x)</code></a></h3>
8803−
8804−
88058947 <p>
8806−Returns the smallest integer larger than or equal to <code>x</code>.
8948+The default value for <code>x</code> is 1,
8949+so that the call <code>math.atan(y)</code>
8950+returns the arc tangent of <code>y</code>.
88078951
88088952
88098953
88108954
88118955 <p>
8812−<hr><h3><a name="pdf-math.cos"><code>math.cos (x)</code></a></h3>
8956+<hr><h3><a name="pdf-math.ceil"><code>math.ceil (x)</code></a></h3>
88138957
88148958
88158959 <p>
8816−Returns the cosine of <code>x</code> (assumed to be in radians).
8960+Returns the smaller integral value larger than or equal to <code>x</code>.
88178961
88188962
88198963
88208964
88218965 <p>
8822−<hr><h3><a name="pdf-math.cosh"><code>math.cosh (x)</code></a></h3>
8966+<hr><h3><a name="pdf-math.cos"><code>math.cos (x)</code></a></h3>
88238967
88248968
88258969 <p>
8826−Returns the hyperbolic cosine of <code>x</code>.
8970+Returns the cosine of <code>x</code> (assumed to be in radians).
88278971
88288972
88298973
@@ -8833,17 +8977,7 @@ Returns the hyperbolic cosine of <code>x</code>. · 3 unmodified lines
88338977
88348978
88358979 <p>
8836−Returns the angle <code>x</code> (given in radians) in degrees.
8837−
8838−
8839−
8840−
8841−<p>
8842−<hr><h3><a name="pdf-math.exp"><code>math.exp (x)</code></a></h3>
8843−
8844−
8845−<p>
8846−Returns the value <em>e<sup>x</sup></em>.
8980+Converts the angle <code>x</code> from radians to degrees.
88478981
88488982
88498983
@@ -8853,8 +8987,7 @@ Returns the value <em>e<sup>x</sup></em>. · 3 unmodified lines
88538987
88548988
88558989 <p>
8856−Returns a float with the largest
8857−integral value smaller than or equal to <code>x</code>.
8990+Returns the largest integral value smaller than or equal to <code>x</code>.
88588991
88598992
88608993
@@ -8865,20 +8998,7 @@ integral value smaller than or equal to <code>x</code>. · 4 unmodified lines
88658998
88668999 <p>
88679000 Returns the remainder of the division of <code>x</code> by <code>y</code>
8868−that rounds the quotient towards zero.
8869−
8870−
8871−
8872−
8873−<p>
8874−<hr><h3><a name="pdf-math.frexp"><code>math.frexp (x)</code></a></h3>
8875−
8876−
8877−<p>
8878−Returns <code>m</code> and <code>e</code> such that <em>x = m2<sup>e</sup></em>,
8879−<code>e</code> is an integer and the absolute value of <code>m</code> is
8880−in the range <em>[0.5, 1)</em>
8881−(or zero when <code>x</code> is zero).
9001+that rounds the quotient towards zero. (integer/float)
88829002
88839003
88849004
@@ -8888,8 +9008,8 @@ in the range <em>[0.5, 1)</em> · 3 unmodified lines
88889008
88899009
88909010 <p>
8891−The value <code>HUGE_VAL</code>,
8892−a value larger than or equal to any other numerical value.
9011+The float value <code>HUGE_VAL</code>,
9012+a value larger than any other numerical value.
88939013
88949014
88959015
@@ -8906,16 +9026,6 @@ returns <b>nil</b>. · 10 unmodified lines
89069026
89079027
89089028
8909−<p>
8910−<hr><h3><a name="pdf-math.ldexp"><code>math.ldexp (m, e)</code></a></h3>
8911−
8912−
8913−<p>
8914−Returns <em>m2<sup>e</sup></em> (<code>e</code> should be an integer).
8915−
8916−
8917−
8918−
89199029 <p>
89209030 <hr><h3><a name="pdf-math.log"><code>math.log (x [, base])</code></a></h3>
89219031
@@ -8933,49 +9043,54 @@ The default for <code>base</code> is <em>e</em> · 11 unmodified lines
89339043
89349044
89359045 <p>
8936−Returns the maximum value among its arguments.
9046+Returns the argument with the maximum value,
9047+according to the Lua operator <code>&lt;</code>. (integer/float)
89379048
89389049
89399050
89409051
89419052 <p>
8942−<hr><h3><a name="pdf-math.min"><code>math.min (x, &middot;&middot;&middot;)</code></a></h3>
9053+<hr><h3><a name="pdf-math.maxinteger"><code>math.maxinteger</code></a></h3>
9054+An integer with the maximum value for an integer.
89439055
89449056
8945−<p>
8946−Returns the minimum value among its arguments.
89479057
89489058
9059+<p>
9060+<hr><h3><a name="pdf-math.min"><code>math.min (x, &middot;&middot;&middot;)</code></a></h3>
89499061
89509062
89519063 <p>
8952−<hr><h3><a name="pdf-math.modf"><code>math.modf (x)</code></a></h3>
9064+Returns the argument with the minimum value,
9065+according to the Lua operator <code>&lt;</code>. (integer/float)
9066+
9067+
89539068
89549069
89559070 <p>
8956−Returns two numbers,
8957−the integral part of <code>x</code> and the fractional part of <code>x</code>.
9071+<hr><h3><a name="pdf-math.mininteger"><code>math.mininteger</code></a></h3>
9072+An integer with the minimum value for an integer.
89589073
89599074
89609075
89619076
89629077 <p>
8963−<hr><h3><a name="pdf-math.pi"><code>math.pi</code></a></h3>
9078+<hr><h3><a name="pdf-math.modf"><code>math.modf (x)</code></a></h3>
89649079
89659080
89669081 <p>
8967−The value of <em>&pi;</em>.
9082+Returns the integral part of <code>x</code> and the fractional part of <code>x</code>.
9083+Its second result is always a float.
89689084
89699085
89709086
89719087
89729088 <p>
8973−<hr><h3><a name="pdf-math.pow"><code>math.pow (x, y)</code></a></h3>
9089+<hr><h3><a name="pdf-math.pi"><code>math.pi</code></a></h3>
89749090
89759091
89769092 <p>
8977−Returns <em>x<sup>y</sup></em>.
8978−(You can also use the expression <code>x^y</code> to compute this value.)
9093+The value of <em>&pi;</em>.
89799094
89809095
89819096
@@ -8985,7 +9100,7 @@ Returns <em>x<sup>y</sup></em>. · 3 unmodified lines
89859100
89869101
89879102 <p>
8988−Returns the angle <code>x</code> (given in degrees) in radians.
9103+Converts the angle <code>x</code> from degrees to radians.
89899104
89909105
89919106
@@ -8994,22 +9109,23 @@ Returns the angle <code>x</code> (given in degrees) in radians. · 2 unmodified lines
89949109 <hr><h3><a name="pdf-math.random"><code>math.random ([m [, n]])</code></a></h3>
89959110
89969111
8997−<p>
8998−This function is an interface to the simple
8999−pseudo-random generator function <code>rand</code> provided by Standard&nbsp;C.
9000−(No guarantees can be given for its statistical properties.)
9001−
9002−
90039112 <p>
90049113 When called without arguments,
9005−returns a uniform pseudo-random float
9006−in the range <em>[0,1)</em>.
9114+returns a pseudo-random float with uniform distribution
9115+in the range <em>[0,1)</em>.
90079116 When called with two integers <code>m</code> and <code>n</code>,
9008−<code>math.random</code> returns a uniform pseudo-random
9009−integer in the range <em>[m, n]</em>.
9117+<code>math.random</code> returns a pseudo-random integer
9118+with uniform distribution in the range <em>[m, n]</em>.
9119+(The interval size must fit in a Lua integer.)
90109120 The call <code>math.random(n)</code> is equivalent to <code>math.random(1,n)</code>.
90119121
90129122
9123+<p>
9124+This function is an interface to the underling
9125+pseudo-random generator function provided by C.
9126+No guarantees can be given for its statistical properties.
9127+
9128+
90139129
90149130
90159131 <p>
@@ -9034,16 +9150,6 @@ Returns the sine of <code>x</code> (assumed to be in radians). · 18 unmodified lines
90349150
90359151
90369152
9037−<p>
9038−<hr><h3><a name="pdf-math.sinh"><code>math.sinh (x)</code></a></h3>
9039−
9040−
9041−<p>
9042−Returns the hyperbolic sine of <code>x</code>.
9043−
9044−
9045−
9046−
90479153 <p>
90489154 <hr><h3><a name="pdf-math.sqrt"><code>math.sqrt (x)</code></a></h3>
90499155
@@ -9065,16 +9171,6 @@ Returns the tangent of <code>x</code> (assumed to be in radians). · 15 unmodified lines
90659171
90669172
90679173
9068−<p>
9069−<hr><h3><a name="pdf-math.tanh"><code>math.tanh (x)</code></a></h3>
9070−
9071−
9072−<p>
9073−Returns the hyperbolic tangent of <code>x</code>.
9074−
9075−
9076−
9077−
90789174 <p>
90799175 <hr><h3><a name="pdf-math.type"><code>math.type (x)</code></a></h3>
90809176
@@ -9337,7 +9433,7 @@ Returns an iterator function that, · 256 unmodified lines
93379433 each time it is called,
93389434 reads the file according to the given formats.
93399435 When no format is given,
9340−uses "*l" as a default.
9436+uses "<code>l</code>" as a default.
93419437 As an example, the construction
93429438
93439439 <pre>
@@ -9376,30 +9472,33 @@ The available formats are · 32 unmodified lines
93769472
93779473 <ul>
93789474
9379−<li><b>"<code>*n</code>": </b>
9380−reads a number and returns it as a float.
9381−If the input has only a prefix of a valid number
9382−(e.g., "<code>0x</code>" or "<code>3.4e-</code>"),
9383−this format can read the entire prefix
9384−even when returning <b>nil</b>.
9475+<li><b>"<code>n</code>": </b>
9476+reads a numeral and returns it as a float or an integer,
9477+following the lexical conventions of Lua.
9478+(The numeral may have leading spaces and a sign.)
9479+This format always reads the longest input sequence that
9480+is a valid prefix for a number;
9481+if that prefix does not form a valid number
9482+(e.g., an empty string, "<code>0x</code>", or "<code>3.4e-</code>"),
9483+it is discarded and the function returns <b>nil</b>.
93859484 </li>
93869485
9387−<li><b>"<code>*i</code>": </b>
9486+<li><b>"<code>i</code>": </b>
93889487 reads an integral number and returns it as an integer.
93899488 </li>
93909489
9391−<li><b>"<code>*a</code>": </b>
9490+<li><b>"<code>a</code>": </b>
93929491 reads the whole file, starting at the current position.
93939492 On end of file, it returns the empty string.
93949493 </li>
93959494
9396−<li><b>"<code>*l</code>": </b>
9495+<li><b>"<code>l</code>": </b>
93979496 reads the next line skipping the end of line,
93989497 returning <b>nil</b> on end of file.
93999498 This is the default format.
94009499 </li>
94019500
9402−<li><b>"<code>*L</code>": </b>
9501+<li><b>"<code>L</code>": </b>
94039502 reads the next line keeping the end-of-line character (if present),
94049503 returning <b>nil</b> on end of file.
94059504 </li>
@@ -9413,7 +9512,7 @@ or <b>nil</b> on end of file. · 7 unmodified lines
94139512 </li>
94149513
94159514 </ul><p>
9416−The formats "<code>*l</code>" and "<code>*L</code>" should be used only for text files.
9515+The formats "<code>l</code>" and "<code>L</code>" should be used only for text files.
94179516
94189517
94199518
@@ -9622,7 +9721,7 @@ When called without a <code>command</code>, · 202 unmodified lines
96229721
96239722
96249723 <p>
9625−<hr><h3><a name="pdf-os.exit"><code>os.exit ([code [, close])</code></a></h3>
9724+<hr><h3><a name="pdf-os.exit"><code>os.exit ([code [, close]])</code></a></h3>
96269725
96279726
96289727 <p>
@@ -9799,6 +9898,32 @@ thread to operate over. · 170 unmodified lines
97999898 The default is always the current thread.
98009899
98019900
9901+<p>
9902+<hr><h3><a name="pdf-debug.Csize"><code>debug.Csize (t)</code></a></h3>
9903+
9904+
9905+<p>
9906+Returns the size of the underlying representation of a given C type,
9907+according to the following table:
9908+<table border="1">
9909+<tr><td>'<code>I</code>'</td><td><code>lua_Integer</code></td></tr>
9910+<tr><td>'<code>F</code>'</td><td><code>lua_Number</code></td></tr>
9911+<tr><td>'<code>b</code>'</td><td><code>byte</code></td></tr>
9912+<tr><td>'<code>d</code>'</td><td><code>double</code></td></tr>
9913+<tr><td>'<code>f</code>'</td><td><code>float</code></td></tr>
9914+<tr><td>'<code>h</code>'</td><td><code>short int</code></td></tr>
9915+<tr><td>'<code>i</code>'</td><td><code>int</code></td></tr>
9916+<tr><td>'<code>l</code>'</td><td><code>long int</code></td></tr>
9917+<tr><td>'<code>z</code>'</td><td><code>size_t</code></td></tr>
9918+</table>
9919+For all options except '<code>b</code>',
9920+the size is given in number of bytes;
9921+for option '<code>b</code>',
9922+the size is its number of bits.
9923+
9924+
9925+
9926+
98029927 <p>
98039928 <hr><h3><a name="pdf-debug.debug"><code>debug.debug ()</code></a></h3>
98049929
@@ -9898,7 +10023,7 @@ and raises an error when called with a level out of range. · 93 unmodified lines
989810023
989910024
990010025 <p>
9901−Variable names starting with '<code>(</code>' (open parenthesis)
10026+Variable names starting with '<code>(</code>' (open parenthesis)
990210027 represent variables with no known names
990310028 (internal variables like loop control variables,
990410029 and variables from chunks saved without debug information).
@@ -9943,7 +10068,7 @@ The function returns <b>nil</b> if there is no upvalue with the given index. · 38 unmodified lines
994310068
994410069
994510070 <p>
9946−Variable names starting with '<code>(</code>' (open parenthesis)
10071+Variable names starting with '<code>(</code>' (open parenthesis)
994710072 represent variables with no known names
994810073 (variables from chunks saved without debug information).
994910074
@@ -9962,18 +10087,6 @@ returns <b>nil</b>. · 12 unmodified lines
996210087
996310088
996410089
9965−<p>
9966−<hr><h3><a name="pdf-debug.numbits"><code>debug.numbits (t)</code></a></h3>
9967−
9968−
9969−<p>
9970−Returns the number of bits in the underlying representation
9971−of an integer (if <code>t</code> is "<code>i</code>")
9972−or a float (if <code>t</code> is "<code>f</code>").
9973−
9974−
9975−
9976−
997710090 <p>
997810091 <hr><h3><a name="pdf-debug.sethook"><code>debug.sethook ([thread,] hook, mask [, count])</code></a></h3>
997910092
@@ -10169,7 +10282,7 @@ and as <code>lua -</code> otherwise. · 189 unmodified lines
1016910282
1017010283 <p>
1017110284 When called without option <code>-E</code>,
10172−the interpreter checks for an environment variable <a name="pdf-LUA_INIT_5_2"><code>LUA_INIT_5_2</code></a>
10285+the interpreter checks for an environment variable <a name="pdf-LUA_INIT_5_3"><code>LUA_INIT_5_3</code></a>
1017310286 (or <a name="pdf-LUA_INIT"><code>LUA_INIT</code></a> if it is not defined)
1017410287 before running any argument.
1017510288 If the variable content has the format <code>@<em>filename</em></code>,
@@ -10200,37 +10313,46 @@ and finally run the file <code>script.lua</code> with no arguments. · 24 unmodified lines
1020010313
1020110314
1020210315 <p>
10203−Before starting to run the script,
10204−<code>lua</code> collects all arguments in the command line
10316+Before running any code,
10317+<code>lua</code> collects all command-line arguments
1020510318 in a global table called <code>arg</code>.
10206−The script name is stored at index 0,
10319+The script name goes to index 0,
1020710320 the first argument after the script name goes to index 1,
1020810321 and so on.
1020910322 Any arguments before the script name
10210−(that is, the interpreter name plus the options)
10323+(that is, the interpreter name plus its options)
1021110324 go to negative indices.
1021210325 For instance, in the call
1021310326
1021410327 <pre>
1021510328 $ lua -la b.lua t1 t2
1021610329 </pre><p>
10217−the interpreter first runs the file <code>a.lua</code>,
10218−then creates a table
10330+the table is like this:
1021910331
1022010332 <pre>
1022110333 arg = { [-2] = "lua", [-1] = "-la",
1022210334 [0] = "b.lua",
1022310335 [1] = "t1", [2] = "t2" }
1022410336 </pre><p>
10225−and finally runs the file <code>b.lua</code>.
10226−The script is called with <code>arg[1]</code>, <code>arg[2]</code>, ...
10227−as arguments;
10228−it can also access these arguments with the vararg expression '<code>...</code>'.
10337+If there is no script in the call,
10338+the interpreter name goes to index 0,
10339+followed by the other arguments.
10340+For instance, the call
10341+
10342+<pre>
10343+ $ lua -e "print(arg[1])"
10344+</pre><p>
10345+will print "<code>-e</code>".
1022910346
1023010347
1023110348 <p>
1023210349 In interactive mode,
10233−if you write an incomplete statement,
10350+Lua repeatedly prompts and waits for a line.
10351+After reading a line,
10352+Lua first try to interpret the line as an expression.
10353+If it succeeds, it prints its value.
10354+Otherwise, it interprets the line as a statement.
10355+If you write an incomplete statement,
1023410356 the interpreter waits for its completion
1023510357 by issuing a different prompt.
1023610358
@@ -10301,6 +10423,12 @@ Always recompile clients of the Lua API when · 64 unmodified lines
1030110423 using a new version.
1030210424
1030310425
10426+<p>
10427+Similarly, Lua versions can always change the internal representation
10428+of precompiled chunks;
10429+precompiled chunks are never compatible between different Lua versions.
10430+
10431+
1030410432 <p>
1030510433 The standard paths in the official distribution may
1030610434 change between versions.
@@ -10325,8 +10453,8 @@ You can fix these differences by forcing a number to be a float · 18 unmodified lines
1032510453 in particular writing constants with an ending <code>.0</code>
1032610454 or using <code>x = x + 0.0</code> to convert a variable.
1032710455 (This recommendation is only for a quick fix
10328−for an occasional incompatibility,
10329−not as a general guideline for good programming.
10456+for an occasional incompatibility;
10457+it is not a general guideline for good programming.
1033010458 For good programming,
1033110459 use floats where you need floats
1033210460 and integers where you need integers.)
@@ -10335,7 +10463,22 @@ and integers where you need integers.) · 2 unmodified lines
1033510463 <p>
1033610464 Although not formally an incompatibility,
1033710465 the proper differentiation between floats and integers
10338−can sometimes have a big impact on performance.
10466+have an impact on performance.
10467+</li>
10468+
10469+<li>
10470+The conversion of a float to a string now adds a <code>.0</code> suffix
10471+to the result if it looks like an integer.
10472+(For instance, the float 2.0 will be printed as <code>2.0</code>,
10473+not as <code>2</code>.)
10474+You should always use an explicit format
10475+when you need a specific format for numbers.
10476+
10477+
10478+<p>
10479+(Formally this is not an incompatibility,
10480+because Lua does not specify how numbers are formatted as strings,
10481+but some programs assumed a specific format.)
1033910482 </li>
1034010483
1034110484 <li>
@@ -10351,24 +10494,31 @@ The generational mode for the garbage collector was removed. · 9 unmodified lines
1035110494 <h2>8.2 &ndash; <a name="8.2">Changes in the Libraries</a></h2>
1035210495 <ul>
1035310496
10354−<li>
10355−The conversion of a float to a string through <a href="#pdf-tostring"><code>tostring</code></a>
10356−(which is used by <a href="#pdf-print"><code>print</code></a>) now adds a <code>.0</code> suffix if the
10357−number has an integral value.
10358−(For instance, the float 2.0 will be printed as <code>2.0</code>,
10359−not as <code>2</code>.)
10360−You should always use an explicit format
10361−when you need a specific format for numbers.
10362−</li>
10363−
1036410497 <li>
1036510498 The <code>bit32</code> library has been deprecated.
1036610499 It is easy to require a compatible external library or,
1036710500 better yet, to replace its functions with appropriate bitwise operations.
10368−(Just keep in mind that <code>bit32</code> operates on 32-bit integers,
10501+(Keep in mind that <code>bit32</code> operates on 32-bit integers,
1036910502 while the bitwise operators in Standard Lua operate on 64-bit integers.)
1037010503 </li>
1037110504
10505+<li>
10506+Option names in <a href="#pdf-io.read"><code>io.read</code></a> do not have a starting '<code>*</code>' anymore.
10507+For compatibility, Lua will continue to ignore this character.
10508+</li>
10509+
10510+<li>
10511+The following functions were deprecated in the mathematical library:
10512+<code>atan2</code>, <code>cosh</code>, <code>sinh</code>, <code>tanh</code>, <code>pow</code>,
10513+<code>frexp</code>, and <code>ldexp</code>.
10514+You can replace <code>math.pow(x,y)</code> with <code>x^y</code>;
10515+you can replace <code>math.atan2</code> with <code>math.atan</code>,
10516+which now accepts two parameters;
10517+you can replace <code>math.ldexp(x,exp)</code> with <code>x * 2.0^exp</code>.
10518+For the other operations,
10519+the best choice is to use an external library.
10520+</li>
10521+
1037210522 </ul>
1037310523
1037410524
@@ -10380,7 +10530,15 @@ while the bitwise operators in Standard Lua operate on 64-bit integers.) · 5 unmodified lines
1038010530 <ul>
1038110531
1038210532 <li>
10383−No changes (yet).
10533+Continuation functions now receive as parameters what they needed
10534+to get through <code>lua_getctx</code>,
10535+so <code>lua_getctx</code> has been removed.
10536+Adapt your code accordingly.
10537+</li>
10538+
10539+<li>
10540+Function <a href="#lua_dump"><code>lua_dump</code></a> has an extra parameter, <code>strip</code>.
10541+Use 0 as the value of this parameter to get the old behavior.
1038410542 </li>
1038510543
1038610544 </ul>
@@ -10456,11 +10614,12 @@ Here is the complete syntax of Lua in extended BNF. · 69 unmodified lines
1045610614
1045710615 fieldsep ::= &lsquo;<b>,</b>&rsquo; | &lsquo;<b>;</b>&rsquo;
1045810616
10459− binop ::= &lsquo;<b>+</b>&rsquo; | &lsquo;<b>-</b>&rsquo; | &lsquo;<b>*</b>&rsquo; | &lsquo;<b>/</b>&rsquo; | &lsquo;<b>^</b>&rsquo; | &lsquo;<b>%</b>&rsquo; | &lsquo;<b>..</b>&rsquo; |
10617+ binop ::= &lsquo;<b>+</b>&rsquo; | &lsquo;<b>-</b>&rsquo; | &lsquo;<b>*</b>&rsquo; | &lsquo;<b>/</b>&rsquo; | &lsquo;<b>//</b>&rsquo; | &lsquo;<b>^</b>&rsquo; | &lsquo;<b>%</b>&rsquo; |
10618+ &lsquo;<b>&amp;</b>&rsquo; | &lsquo;<b>~</b>&rsquo; | &lsquo;<b>|</b>&rsquo; | &lsquo;<b>&gt;&gt;</b>&rsquo; | &lsquo;<b>&lt;&lt;</b>&rsquo; | &lsquo;<b>..</b>&rsquo; |
1046010619 &lsquo;<b>&lt;</b>&rsquo; | &lsquo;<b>&lt;=</b>&rsquo; | &lsquo;<b>&gt;</b>&rsquo; | &lsquo;<b>&gt;=</b>&rsquo; | &lsquo;<b>==</b>&rsquo; | &lsquo;<b>~=</b>&rsquo; |
1046110620 <b>and</b> | <b>or</b>
1046210621
10463− unop ::= &lsquo;<b>-</b>&rsquo; | <b>not</b> | &lsquo;<b>#</b>&rsquo;
10622+ unop ::= &lsquo;<b>-</b>&rsquo; | <b>not</b> | &lsquo;<b>#</b>&rsquo; | &lsquo;<b>~</b>&rsquo;
1046410623
1046510624 </pre>
1046610625
@@ -10468,17 +10627,18 @@ Here is the complete syntax of Lua in extended BNF. · 1 unmodified line
1046810627
1046910628
1047010629
10471−}
10630+
10631+
1047210632
1047310633
1047410634
1047510635 <HR>
1047610636 <SMALL CLASS="footer">
1047710637 Last update:
10478−Fri Mar 21 17:33:56 BRT 2014
10638+Thu Jun 19 17:13:19 BRT 2014
1047910639 </SMALL>
1048010640 <!--
10481−Last change: updated for Lua 5.3.0 (work2)
10641+Last change: updated for Lua 5.3.0 (work3)
1048210642 -->
1048310643
1048410644 </body></html>
doc/readme.html+16−19
@@ -31,7 +31,7 @@ tt, kbd, code { · 30 unmodified lines
3131 <HR>
3232 <H1>
3333 <A HREF="http://www.lua.org/"><IMG SRC="logo.gif" ALT="Lua" BORDER=0></A>
34−Welcome to Lua 5.3 (work2)
34+Welcome to Lua 5.3 (work3)
3535 </H1>
3636
3737 <P>
@@ -102,8 +102,8 @@ If you don't have the time or the inclination to compile Lua yourself, · 64 unmodified lines
102102 get a binary from
103103 <A HREF="http://lua-users.org/wiki/LuaBinaries">LuaBinaries</A>.
104104 Try also
105−<A HREF="http://luaforwindows.luaforge.net/">Lua for Windows</A>,
106−an easy-to-use distribution of Lua that includes many useful libraries.
105+<A HREF="http://luadist.org/">LuaDist</A>,
106+a multi-platform distribution of Lua that includes batteries.
107107
108108 <H3>Building Lua</H3>
109109
@@ -114,8 +114,8 @@ Here are the details. · 4 unmodified lines
114114 <OL>
115115 <LI>
116116 Open a terminal window and move to
117−the top-level directory, which is named <TT>lua-5.3.0</TT>.
118−The Makefile there controls both the build process and the installation process.
117+the top-level directory, which is named <TT>lua-5.3.0-work3</TT>.
118+The <TT>Makefile</TT> there controls both the build process and the installation process.
119119 <P>
120120 <LI>
121121 Do "<KBD>make</KBD>" and see if your platform is listed.
@@ -140,7 +140,7 @@ and liblua.a (the library). · 18 unmodified lines
140140 <P>
141141 <LI>
142142 To check that Lua has been built correctly, do "<KBD>make test</KBD>"
143− after building Lua. This will run the interpreter and print its version string.
143+ after building Lua. This will run the interpreter and print its version.
144144 </OL>
145145 <P>
146146 If you're running Linux and get compilation errors,
@@ -152,7 +152,7 @@ then try "<KBD>make linux MYLIBS=-ltermcap</KBD>". · 5 unmodified lines
152152 <P>
153153 Once you have built Lua, you may want to install it in an official
154154 place in your system. In this case, do "<KBD>make install</KBD>". The official
155− place and the way to install files are defined in the Makefile. You'll
155+ place and the way to install files are defined in the <TT>Makefile</TT>. You'll
156156 probably need the right permissions to install files.
157157
158158 <P>
@@ -162,11 +162,13 @@ then try "<KBD>make linux MYLIBS=-ltermcap</KBD>". · 3 unmodified lines
162162 <P>
163163 To install Lua locally, do "<KBD>make local</KBD>".
164164 This will create a directory <TT>install</TT> with subdirectories
165− <TT>bin</TT>, <TT>include</TT>, <TT>lib</TT>, <TT>man</TT>,
165+ <TT>bin</TT>, <TT>include</TT>, <TT>lib</TT>, <TT>man</TT>, <TT>share</TT>,
166166 and install Lua as listed below.
167167
168168 To install Lua locally, but in some other directory, do
169169 "<KBD>make install INSTALL_TOP=xxx</KBD>", where xxx is your chosen directory.
170+ The installation starts in the <TT>src</TT> and <TT>doc</TT> directories,
171+ so take care if <TT>INSTALL_TOP</TT> is not an absolute path.
170172
171173 <DL CLASS="display">
172174 <DT>
@@ -176,7 +178,7 @@ then try "<KBD>make linux MYLIBS=-ltermcap</KBD>". · 3 unmodified lines
176178 <DT>
177179 include:
178180 <DD>
179− lua.h luaconf.h lualib.h lauxlib.h lua.hpp
181+ lauxlib.h lua.h lua.hpp luaconf.h lualib.h
180182 <DT>
181183 lib:
182184 <DD>
@@ -190,8 +192,8 @@ then try "<KBD>make linux MYLIBS=-ltermcap</KBD>". · 7 unmodified lines
190192 <P>
191193 These are the only directories you need for development.
192194 If you only want to run Lua programs,
193− you only need the files in bin and man.
194− The files in include and lib are needed for
195+ you only need the files in <TT>bin</TT> and <TT>man</TT>.
196+ The files in <TT>include</TT> and <TT>lib</TT> are needed for
195197 embedding Lua in C or C++ programs.
196198
197199 <H3><A NAME="customization">Customization</A></H3>
@@ -207,7 +209,7 @@ then try "<KBD>make linux MYLIBS=-ltermcap</KBD>". · 9 unmodified lines
207209 You don't actually need to edit the Makefiles because you may set the
208210 relevant variables in the command line when invoking make.
209211 Nevertheless, it's probably best to edit and save the Makefiles to
210− record the changes you need.
212+ record the changes you've made.
211213
212214 <P>
213215 On the other hand, if you need to customize some Lua features, you'll need
@@ -216,11 +218,6 @@ then try "<KBD>make linux MYLIBS=-ltermcap</KBD>". · 2 unmodified lines
216218 it will be used by any Lua clients that you build, to ensure consistency.
217219 Further customization is available to experts by editing the Lua sources.
218220
219−<P>
220− We strongly recommend that you enable dynamic loading in <TT>src/luaconf.h</TT>.
221− This is done automatically for all platforms listed above that have
222− this feature and also for Windows.
223−
224221 <H3><A NAME="other">Building Lua on other systems</A></H3>
225222
226223 <P>
@@ -357,10 +354,10 @@ THE SOFTWARE. · 130 unmodified lines
357354 <HR>
358355 <SMALL CLASS="footer">
359356 Last update:
360−Fri Mar 21 17:33:08 BRT 2014
357+Wed Jun 11 22:54:18 BRT 2014
361358 </SMALL>
362359 <!--
363−Last change: updated for Lua 5.3.0 (work2)
360+Last change: updated for Lua 5.3.0 (work3)
364361 -->
365362
366363 </BODY>
src/.fix+0−2
@@ -1,2 +0,0 @@
1−#define nvalue(x) ((lua_Number)0)
2−#define ttypenv(x) ttnov(x)
src/Makefile+2−2
@@ -109,7 +109,7 @@ macosx: · 108 unmodified lines
109109 $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_MACOSX" SYSLIBS="-lreadline" CC=cc
110110
111111 mingw:
112− $(MAKE) "LUA_A=lua52.dll" "LUA_T=lua.exe" \
112+ $(MAKE) "LUA_A=lua53.dll" "LUA_T=lua.exe" \
113113 "AR=$(CC) -shared -o" "RANLIB=strip --strip-unneeded" \
114114 "SYSCFLAGS=-DLUA_BUILD_AS_DLL" "SYSLIBS=" "SYSLDFLAGS=-s" lua.exe
115115 $(MAKE) "LUAC_T=luac.exe" luac.exe
@@ -118,7 +118,7 @@ posix: · 2 unmodified lines
118118 $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX"
119119
120120 solaris:
121− $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-ldl"
121+ $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN -D_REENTRANT" SYSLIBS="-ldl"
122122
123123 # list targets that do not create files (but not all makes understand .PHONY)
124124 .PHONY: all $(PLATS) default o a clean depend echo none
src/lapi.c+46−54
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lapi.c,v 2.201 2014/03/12 20:57:40 roberto Exp $
2+** $Id: lapi.c,v 2.219 2014/06/19 18:27:20 roberto Exp $
33 ** Lua API
44 ** See Copyright Notice in lua.h
55 */
@@ -94,6 +94,7 @@ LUA_API int lua_checkstack (lua_State *L, int size) { · 88 unmodified lines
9494 int res;
9595 CallInfo *ci = L->ci;
9696 lua_lock(L);
97+ api_check(L, size >= 0, "negative 'size'");
9798 if (L->stack_last - L->top > size) /* stack large enough? */
9899 res = 1; /* yes; check is OK */
99100 else { /* no; need to grow stack */
@@ -180,26 +181,35 @@ LUA_API void lua_settop (lua_State *L, int idx) { · 80 unmodified lines
180181 }
181182
182183
183−LUA_API void lua_remove (lua_State *L, int idx) {
184− StkId p;
185− lua_lock(L);
186− p = index2addr(L, idx);
187− api_checkstackindex(L, idx, p);
188− while (++p < L->top) setobjs2s(L, p-1, p);
189− L->top--;
190− lua_unlock(L);
184+/*
185+** Reverse the stack segment from 'from' to 'to'
186+** (auxiliar to 'lua_rotate')
187+*/
188+static void reverse (lua_State *L, StkId from, StkId to) {
189+ for (; from < to; from++, to--) {
190+ TValue temp;
191+ setobj(L, &temp, from);
192+ setobjs2s(L, from, to);
193+ setobj2s(L, to, &temp);
194+ }
191195 }
192196
193197
194−LUA_API void lua_insert (lua_State *L, int idx) {
195− StkId p;
196− StkId q;
198+/*
199+** Let x = AB, where A is a prefix of length 'n'. Then,
200+** rotate x n == BA. But BA == (A^r . B^r)^r.
201+*/
202+LUA_API void lua_rotate (lua_State *L, int idx, int n) {
203+ StkId p, t, m;
197204 lua_lock(L);
198− p = index2addr(L, idx);
205+ t = L->top - 1; /* end of stack segment being rotated */
206+ p = index2addr(L, idx); /* start of segment */
199207 api_checkstackindex(L, idx, p);
200− for (q = L->top; q > p; q--) /* use L->top as a temporary */
201− setobjs2s(L, q, q - 1);
202− setobjs2s(L, p, L->top);
208+ api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'");
209+ m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */
210+ reverse(L, p, m); /* reverse the prefix with length 'n' */
211+ reverse(L, m + 1, t); /* reverse the suffix */
212+ reverse(L, p, t); /* reverse the entire segment */
203213 lua_unlock(L);
204214 }
205215
@@ -332,18 +342,11 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { · 126 unmodified lines
332342 }
333343
334344
335−LUA_API int lua_strtonum (lua_State *L, const char *s, size_t len) {
336− lua_Integer i; lua_Number n;
337− if (luaO_str2int(s, len, &i)) { /* try as an integer */
338− setivalue(L->top, i);
339− }
340− else if (luaO_str2d(s, len, &n)) { /* else try as a float */
341− setnvalue(L->top, n);
342− }
343− else
344− return 0; /* conversion failed */
345− api_incr_top(L);
346− return 1;
345+LUA_API size_t lua_strtonum (lua_State *L, const char *s) {
346+ size_t sz = luaO_str2num(s, L->top);
347+ if (sz != 0)
348+ api_incr_top(L);
349+ return sz;
347350 }
348351
349352
@@ -375,12 +378,12 @@ LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *pisnum) { · 25 unmodified lines
375378 int isnum = 0;
376379 switch (ttype(o)) {
377380 case LUA_TNUMINT: {
378− res = cast_unsigned(ivalue(o));
381+ res = l_castS2U(ivalue(o));
379382 isnum = 1;
380383 break;
381384 }
382385 case LUA_TNUMFLT: { /* compute floor(n) % 2^(numbits in an integer) */
383− const lua_Number twop = cast_num(MAX_UINTEGER) + cast_num(1); /* 2^n */
386+ const lua_Number two2n = cast_num(LUA_MAXUNSIGNED) + cast_num(1);
384387 lua_Number n = fltvalue(o); /* get value */
385388 int neg = 0;
386389 n = l_floor(n); /* get its floor */
@@ -388,10 +391,10 @@ LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *pisnum) { · 1 unmodified line
388391 neg = 1;
389392 n = -n; /* make 'n' positive, so that 'fmod' is the same as '%' */
390393 }
391− n = l_mathop(fmod)(n, twop); /* n = n % 2^(numbits in an integer) */
394+ n = l_mathop(fmod)(n, two2n); /* n = n % 2^(numbits in an integer) */
392395 if (luai_numisnan(n)) /* not a number? */
393396 break; /* not an integer, too */
394− res = cast_unsigned(n); /* 'n' now must fit in an unsigned */
397+ res = cast(lua_Unsigned, n); /* 'n' now must fit in an unsigned */
395398 if (neg) res = 0u - res; /* back to negative, if needed */
396399 isnum = 1;
397400 break;
@@ -495,9 +498,7 @@ LUA_API void lua_pushnil (lua_State *L) { · 97 unmodified lines
495498
496499 LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
497500 lua_lock(L);
498− setnvalue(L->top, n);
499− luai_checknum(L, L->top,
500− luaG_runerror(L, "C API - attempt to push a signaling NaN"));
501+ setfltvalue(L->top, n);
501502 api_incr_top(L);
502503 lua_unlock(L);
503504 }
@@ -513,7 +514,7 @@ LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { · 9 unmodified lines
513514
514515 LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) {
515516 lua_lock(L);
516− setivalue(L->top, cast_integer(u));
517+ setivalue(L->top, l_castU2S(u));
517518 api_incr_top(L);
518519 lua_unlock(L);
519520 }
@@ -579,15 +580,15 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { · 59 unmodified lines
579580 setfvalue(L->top, fn);
580581 }
581582 else {
582− Closure *cl;
583+ CClosure *cl;
583584 api_checknelems(L, n);
584585 api_check(L, n <= MAXUPVAL, "upvalue index too large");
585586 luaC_checkGC(L);
586587 cl = luaF_newCclosure(L, n);
587− cl->c.f = fn;
588+ cl->f = fn;
588589 L->top -= n;
589590 while (n--) {
590− setobj2n(L, &cl->c.upvalue[n], L->top + n);
591+ setobj2n(L, &cl->upvalue[n], L->top + n);
591592 /* does not need barrier because closure is white */
592593 }
593594 setclCvalue(L, L->top, cl);
@@ -741,7 +742,7 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) { · 147 unmodified lines
741742 }
742743
743744
744−LUA_API void lua_getuservalue (lua_State *L, int idx) {
745+LUA_API int lua_getuservalue (lua_State *L, int idx) {
745746 StkId o;
746747 lua_lock(L);
747748 o = index2addr(L, idx);
@@ -749,6 +750,7 @@ LUA_API void lua_getuservalue (lua_State *L, int idx) { · 1 unmodified line
749750 getuservalue(L, rawuvalue(o), L->top);
750751 api_incr_top(L);
751752 lua_unlock(L);
753+ return ttnov(L->top - 1);
752754 }
753755
754756
@@ -898,17 +900,8 @@ LUA_API void lua_setuservalue (lua_State *L, int idx) { · 143 unmodified lines
898900 "results from function overflow current stack size")
899901
900902
901−LUA_API int lua_getctx (lua_State *L, int *ctx) {
902− if (L->ci->callstatus & CIST_YIELDED) {
903− if (ctx) *ctx = L->ci->u.c.ctx;
904− return L->ci->u.c.status;
905− }
906− else return LUA_OK;
907−}
908−
909−
910903 LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx,
911− lua_CFunction k) {
904+ lua_KFunction k) {
912905 StkId func;
913906 lua_lock(L);
914907 api_check(L, k == NULL || !isLua(L->ci),
@@ -947,7 +940,7 @@ static void f_call (lua_State *L, void *ud) { · 32 unmodified lines
947940
948941
949942 LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
950− int ctx, lua_CFunction k) {
943+ int ctx, lua_KFunction k) {
951944 struct CallS c;
952945 int status;
953946 ptrdiff_t func;
@@ -975,11 +968,10 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, · 21 unmodified lines
975968 ci->u.c.ctx = ctx; /* save context */
976969 /* save information for error recovery */
977970 ci->extra = savestack(L, c.func);
978− ci->u.c.old_allowhook = L->allowhook;
979971 ci->u.c.old_errfunc = L->errfunc;
980972 L->errfunc = func;
981− /* mark that function may do error recovery */
982− ci->callstatus |= CIST_YPCALL;
973+ setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */
974+ ci->callstatus |= CIST_YPCALL; /* function can do error recovery */
983975 luaD_call(L, c.func, nresults, 1); /* do the call */
984976 ci->callstatus &= ~CIST_YPCALL;
985977 L->errfunc = ci->u.c.old_errfunc;
src/lauxlib.c+6−11
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lauxlib.c,v 1.260 2014/03/12 20:57:40 roberto Exp $
2+** $Id: lauxlib.c,v 1.263 2014/05/12 21:44:17 roberto Exp $
33 ** Auxiliary functions for building Lua libraries
44 ** See Copyright Notice in lua.h
55 */
@@ -397,7 +397,7 @@ LUALIB_API lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number def) { · 391 unmodified lines
397397
398398 static void interror (lua_State *L, int arg) {
399399 if (lua_type(L, arg) == LUA_TNUMBER)
400− luaL_argerror(L, arg, "float value out of range");
400+ luaL_argerror(L, arg, "float value out of integer range");
401401 else
402402 tag_error(L, arg, LUA_TNUMBER);
403403 }
@@ -757,13 +757,8 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { · 353 unmodified lines
757757 case LUA_TNUMBER: {
758758 if (lua_isinteger(L, idx))
759759 lua_pushfstring(L, "%I", lua_tointeger(L, idx));
760− else {
761− const char *s = lua_pushfstring(L, "%f", lua_tonumber(L, idx));
762− if (s[strspn(s, "-0123456789")] == '\0') { /* looks like an int? */
763− lua_pushliteral(L, ".0"); /* add a '.0' to result */
764− lua_concat(L, 2);
765− }
766− }
760+ else
761+ lua_pushfstring(L, "%f", lua_tonumber(L, idx));
767762 break;
768763 }
769764 case LUA_TSTRING:
@@ -966,12 +961,12 @@ LUALIB_API lua_State *luaL_newstate (void) { · 196 unmodified lines
966961 }
967962
968963
969−LUALIB_API void luaL_checkversion_ (lua_State *L, int ver, size_t sz) {
964+LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) {
970965 const lua_Number *v = lua_version(L);
971966 if (v != lua_version(NULL))
972967 luaL_error(L, "multiple Lua VMs detected");
973968 else if (*v != ver)
974− luaL_error(L, "version mismatch: app. needs %d, Lua core provides %f",
969+ luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f",
975970 ver, *v);
976971 /* check numeric types */
977972 if (sz != LUAL_NUMSIZES)
src/lauxlib.h+2−2
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lauxlib.h,v 1.123 2014/01/05 14:04:46 roberto Exp $
2+** $Id: lauxlib.h,v 1.124 2014/04/15 18:25:49 roberto Exp $
33 ** Auxiliary functions for building Lua libraries
44 ** See Copyright Notice in lua.h
55 */
@@ -28,7 +28,7 @@ typedef struct luaL_Reg { · 22 unmodified lines
2828
2929 #define LUAL_NUMSIZES (sizeof(lua_Integer)*16 + sizeof(lua_Number))
3030
31−LUALIB_API void (luaL_checkversion_) (lua_State *L, int ver, size_t sz);
31+LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver, size_t sz);
3232 #define luaL_checkversion(L) \
3333 luaL_checkversion_(L, LUA_VERSION_NUM, LUAL_NUMSIZES)
3434
src/lbaselib.c+51−41
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lbaselib.c,v 1.285 2014/03/12 20:57:40 roberto Exp $
2+** $Id: lbaselib.c,v 1.289 2014/06/10 17:41:38 roberto Exp $
33 ** Basic library
44 ** See Copyright Notice in lua.h
55 */
@@ -45,31 +45,31 @@ static int luaB_print (lua_State *L) { · 39 unmodified lines
4545
4646 #define SPACECHARS " \f\n\r\t\v"
4747
48−static int b_str2int (const char *s, const char *e, int base, lua_Integer *pn) {
48+static const char *b_str2int (const char *s, int base, lua_Integer *pn) {
4949 lua_Unsigned n = 0;
5050 int neg = 0;
5151 s += strspn(s, SPACECHARS); /* skip initial spaces */
5252 if (*s == '-') { s++; neg = 1; } /* handle signal */
5353 else if (*s == '+') s++;
5454 if (!isalnum((unsigned char)*s)) /* no digit? */
55− return 0;
55+ return NULL;
5656 do {
5757 int digit = (isdigit((unsigned char)*s)) ? *s - '0'
5858 : toupper((unsigned char)*s) - 'A' + 10;
59− if (digit >= base) return 0; /* invalid numeral */
59+ if (digit >= base) return NULL; /* invalid numeral */
6060 n = n * base + digit;
6161 s++;
6262 } while (isalnum((unsigned char)*s));
6363 s += strspn(s, SPACECHARS); /* skip trailing spaces */
64− if (s != e) /* invalid trailing characters? */
65− return 0;
64+ if (*s != '\0') /* invalid trailing characters? */
65+ return NULL;
6666 *pn = (lua_Integer)((neg) ? (0u - n) : n);
67− return 1;
67+ return s;
6868 }
6969
7070
7171 static int luaB_tonumber (lua_State *L) {
72− if (lua_isnoneornil(L, 2)) { /* standard conversion */
72+ if (lua_isnoneornil(L, 2)) { /* standard conversion? */
7373 luaL_checkany(L, 1);
7474 if (lua_type(L, 1) == LUA_TNUMBER) { /* already a number? */
7575 lua_settop(L, 1); /* yes; return it */
@@ -78,20 +78,20 @@ static int luaB_tonumber (lua_State *L) { · 2 unmodified lines
7878 else {
7979 size_t l;
8080 const char *s = lua_tolstring(L, 1, &l);
81− if (s != NULL && lua_strtonum(L, s, l)) /* can convert to a number? */
82− return 1;
81+ if (s != NULL && lua_strtonum(L, s) == l + 1)
82+ return 1; /* successful conversion to number */
8383 /* else not a number */
8484 }
8585 }
8686 else {
8787 size_t l;
8888 const char *s;
89− lua_Integer n;
89+ lua_Integer n = 0; /* to avoid warnings */
9090 int base = luaL_checkint(L, 2);
9191 luaL_checktype(L, 1, LUA_TSTRING); /* before 'luaL_checklstring'! */
9292 s = luaL_checklstring(L, 1, &l);
9393 luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
94− if (b_str2int(s, s + l, base, &n)) {
94+ if (b_str2int(s, base, &n) == s + l) {
9595 lua_pushinteger(L, n);
9696 return 1;
9797 } /* else not a number */
@@ -341,7 +341,8 @@ static int luaB_load (lua_State *L) { · 243 unmodified lines
341341 /* }====================================================== */
342342
343343
344−static int dofilecont (lua_State *L) {
344+static int dofilecont (lua_State *L, int d1, int d2) {
345+ (void)d1; (void)d2; /* only to match 'lua_Kfunction' prototype */
345346 return lua_gettop(L) - 1;
346347 }
347348
@@ -352,14 +353,19 @@ static int luaB_dofile (lua_State *L) { · 4 unmodified lines
352353 if (luaL_loadfile(L, fname) != LUA_OK)
353354 return lua_error(L);
354355 lua_callk(L, 0, LUA_MULTRET, 0, dofilecont);
355− return dofilecont(L);
356+ return dofilecont(L, 0, 0);
356357 }
357358
358359
359360 static int luaB_assert (lua_State *L) {
360− if (!lua_toboolean(L, 1))
361− return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!"));
362− return lua_gettop(L);
361+ if (lua_toboolean(L, 1)) /* condition is true? */
362+ return lua_gettop(L); /* return all arguments */
363+ else { /* error */
364+ if (lua_isnone(L, 2)) /* no error message? */
365+ lua_pushliteral(L, "assertion failed!"); /* use standard message */
366+ lua_remove(L, 1); /* remove the condition (if there is one...) */
367+ return luaB_error(L); /* call 'error' */
368+ }
363369 }
364370
365371
@@ -379,44 +385,48 @@ static int luaB_select (lua_State *L) { · 13 unmodified lines
379385 }
380386
381387
382−static int finishpcall (lua_State *L, int status) {
383− if (!lua_checkstack(L, 1)) { /* no space for extra boolean? */
384− lua_settop(L, 0); /* create space for return values */
385− lua_pushboolean(L, 0);
386− lua_pushstring(L, "stack overflow");
388+/*
389+** Continuation function for 'pcall' and 'xpcall'. Both functions
390+** already pushed a 'true' before doing the call, so in case of sucess
391+** 'finishpcall' only has to return everything in the stack minus
392+** 'extra' values (where 'extra' is exactly the number of items to be
393+** ignored).
394+*/
395+static int finishpcall (lua_State *L, int status, int extra) {
396+ if (status != LUA_OK && status != LUA_YIELD) { /* error? */
397+ lua_pushboolean(L, 0); /* first result (false) */
398+ lua_pushvalue(L, -2); /* error message */
387399 return 2; /* return false, msg */
388400 }
389− lua_pushboolean(L, status); /* first result (status) */
390− lua_replace(L, 1); /* put first result in first slot */
391− return lua_gettop(L);
392−}
393−
394−
395−static int pcallcont (lua_State *L) {
396− int status = lua_getctx(L, NULL);
397− return finishpcall(L, (status == LUA_YIELD));
401+ else
402+ return lua_gettop(L) - extra; /* return all results */
398403 }
399404
400405
401406 static int luaB_pcall (lua_State *L) {
402407 int status;
403408 luaL_checkany(L, 1);
404− lua_pushnil(L);
405− lua_insert(L, 1); /* create space for status result */
406− status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, pcallcont);
407− return finishpcall(L, (status == LUA_OK));
409+ lua_pushboolean(L, 1); /* first result if no errors */
410+ lua_insert(L, 1); /* put it in place */
411+ status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, finishpcall);
412+ return finishpcall(L, status, 0);
408413 }
409414
410415
416+/*
417+** Do a protected call with error handling. After 'lua_rotate', the
418+** stack will have <f, err, true, f, [args...]>; so, the function passes
419+** 2 to 'finishpcall' to skip the 2 first values when returning results.
420+*/
411421 static int luaB_xpcall (lua_State *L) {
412422 int status;
413423 int n = lua_gettop(L);
414− luaL_argcheck(L, n >= 2, 2, "value expected");
415− lua_pushvalue(L, 1); /* exchange function... */
416− lua_copy(L, 2, 1); /* ...and error handler */
417− lua_replace(L, 2);
418− status = lua_pcallk(L, n - 2, LUA_MULTRET, 1, 0, pcallcont);
419− return finishpcall(L, (status == LUA_OK));
424+ luaL_checktype(L, 2, LUA_TFUNCTION); /* check error function */
425+ lua_pushboolean(L, 1); /* first result */
426+ lua_pushvalue(L, 1); /* function */
427+ lua_rotate(L, 3, 2); /* move them below function's arguments */
428+ status = lua_pcallk(L, n - 2, LUA_MULTRET, 2, 2, finishpcall);
429+ return finishpcall(L, status, 2);
420430 }
421431
422432
src/lbitlib.c+2−3
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lbitlib.c,v 1.25 2014/03/20 19:22:16 roberto Exp $
2+** $Id: lbitlib.c,v 1.26 2014/05/15 19:28:34 roberto Exp $
33 ** Standard library for bitwise operations
44 ** See Copyright Notice in lua.h
55 */
@@ -221,8 +221,7 @@ LUAMOD_API int luaopen_bit32 (lua_State *L) { · 215 unmodified lines
221221
222222
223223 LUAMOD_API int luaopen_bit32 (lua_State *L) {
224− lua_pushnil(L);
225− return 1;
224+ return luaL_error(L, "library 'bit32' has been deprecated");
226225 }
227226
228227 #endif /* } */
src/lcode.c+10−11
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lcode.c,v 2.85 2014/03/21 13:52:33 roberto Exp $
2+** $Id: lcode.c,v 2.90 2014/05/08 18:58:46 roberto Exp $
33 ** Code generator for Lua
44 ** See Copyright Notice in lua.h
55 */
@@ -46,7 +46,7 @@ static int tonumeral(expdesc *e, TValue *v) { · 40 unmodified lines
4646 if (v) setivalue(v, e->u.ival);
4747 return 1;
4848 case VKFLT:
49− if (v) setnvalue(v, e->u.nval);
49+ if (v) setfltvalue(v, e->u.nval);
5050 return 1;
5151 default: return 0;
5252 }
@@ -195,7 +195,7 @@ void luaK_patchlist (FuncState *fs, int list, int target) { · 142 unmodified lines
195195 }
196196
197197
198−LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level) {
198+void luaK_patchclose (FuncState *fs, int list, int level) {
199199 level++; /* argument is +1 to reserve 0 as non-op */
200200 while (list != NO_JUMP) {
201201 int next = getjump(fs, list);
@@ -317,7 +317,7 @@ static int addk (FuncState *fs, TValue *key, TValue *v) { · 115 unmodified lines
317317 TValue *idx = luaH_set(L, fs->ls->h, key); /* index scanner table */
318318 int k, oldsize;
319319 if (ttisinteger(idx)) { /* is there an index there? */
320− k = ivalue(idx);
320+ k = cast_int(ivalue(idx));
321321 /* correct value? (warning: must distinguish floats from integers!) */
322322 if (k < fs->nk && ttype(&f->k[k]) == ttype(v) &&
323323 luaV_rawequalobj(&f->k[k], v))
@@ -346,8 +346,9 @@ int luaK_stringK (FuncState *fs, TString *s) { · 22 unmodified lines
346346
347347
348348 /*
349−** use userdata as key to avoid collision with float with same value;
350−** conversion to 'void*' used only for hash, no "precision" problems
349+** Integers use userdata as keys to avoid collision with floats with same
350+** value; conversion to 'void*' used only for hashing, no "precision"
351+** problems
351352 */
352353 int luaK_intK (FuncState *fs, lua_Integer n) {
353354 TValue k, o;
@@ -359,13 +360,13 @@ int luaK_intK (FuncState *fs, lua_Integer n) { · 5 unmodified lines
359360
360361 /*
361362 ** Both NaN and -0.0 should not go to the constant table, as they have
362−** problems with the hashing. (NaN is not ** a valid key,
363−** -0.0 collides with +0.0.)
363+** problems with the hashing. (NaN is not a valid key, -0.0 collides
364+** with +0.0.)
364365 */
365366 static int luaK_numberK (FuncState *fs, lua_Number r) {
366367 TValue o;
367368 lua_assert(!luai_numisnan(r) && !isminuszero(r));
368− setnvalue(&o, r);
369+ setfltvalue(&o, r);
369370 return addk(fs, &o, &o);
370371 }
371372
@@ -768,8 +769,6 @@ static int validop (int op, TValue *v1, TValue *v2) { · 396 unmodified lines
768769 return (tointeger(v1, &i) && tointeger(v2, &i));
769770 case LUA_OPMOD: /* integer module by 0 */
770771 return !(ttisinteger(v1) && ttisinteger(v2) && ivalue(v2) == 0);
771− case LUA_OPPOW: /* negative integer exponentiation */
772− return !(ttisinteger(v1) && ttisinteger(v2) && ivalue(v2) < 0);
773772 default: return 1; /* everything else is valid */
774773 }
775774 }
src/lcorolib.c+17−5
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lcorolib.c,v 1.5 2013/02/21 13:44:53 roberto Exp $
2+** $Id: lcorolib.c,v 1.6 2014/05/08 13:52:20 roberto Exp $
33 ** Coroutine Library
44 ** See Copyright Notice in lua.h
55 */
@@ -17,6 +17,13 @@ · 11 unmodified lines
1717 #include "lualib.h"
1818
1919
20+static lua_State *getco (lua_State *L) {
21+ lua_State *co = lua_tothread(L, 1);
22+ luaL_argcheck(L, co, 1, "coroutine expected");
23+ return co;
24+}
25+
26+
2027 static int auxresume (lua_State *L, lua_State *co, int narg) {
2128 int status;
2229 if (!lua_checkstack(co, narg)) {
@@ -47,9 +54,8 @@ static int auxresume (lua_State *L, lua_State *co, int narg) { · 24 unmodified lines
4754
4855
4956 static int luaB_coresume (lua_State *L) {
50− lua_State *co = lua_tothread(L, 1);
57+ lua_State *co = getco(L);
5158 int r;
52− luaL_argcheck(L, co, 1, "coroutine expected");
5359 r = auxresume(L, co, lua_gettop(L) - 1);
5460 if (r < 0) {
5561 lua_pushboolean(L, 0);
@@ -102,8 +108,7 @@ static int luaB_yield (lua_State *L) { · 46 unmodified lines
102108
103109
104110 static int luaB_costatus (lua_State *L) {
105− lua_State *co = lua_tothread(L, 1);
106− luaL_argcheck(L, co, 1, "coroutine expected");
111+ lua_State *co = getco(L);
107112 if (L == co) lua_pushliteral(L, "running");
108113 else {
109114 switch (lua_status(co)) {
@@ -129,6 +134,12 @@ static int luaB_costatus (lua_State *L) { · 19 unmodified lines
129134 }
130135
131136
137+static int luaB_yieldable (lua_State *L) {
138+ lua_pushboolean(L, lua_isyieldable(L));
139+ return 1;
140+}
141+
142+
132143 static int luaB_corunning (lua_State *L) {
133144 int ismain = lua_pushthread(L);
134145 lua_pushboolean(L, ismain);
@@ -143,6 +154,7 @@ static const luaL_Reg co_funcs[] = { · 8 unmodified lines
143154 {"status", luaB_costatus},
144155 {"wrap", luaB_cowrap},
145156 {"yield", luaB_yield},
157+ {"isyieldable", luaB_yieldable},
146158 {NULL, NULL}
147159 };
148160
src/ldblib.c+117−70
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ldblib.c,v 1.137 2014/03/12 20:57:40 roberto Exp $
2+** $Id: ldblib.c,v 1.139 2014/05/15 19:27:33 roberto Exp $
33 ** Interface from Lua to its debug API
44 ** See Copyright Notice in lua.h
55 */
@@ -21,16 +21,30 @@ · 15 unmodified lines
2121 #define HOOKKEY "_HKEY"
2222
2323
24−
25−static int db_numbits (lua_State *L) {
24+static int db_Csize (lua_State *L) {
25+ static struct {
26+ char c;
27+ unsigned char sz;
28+ } sizes[] = {
29+ {'I', sizeof(lua_Integer)},
30+ {'F', sizeof(lua_Number)},
31+ {'b', CHAR_BIT}, /* here is number of bits (not bytes) */
32+ {'h', sizeof(short)},
33+ {'i', sizeof(int)},
34+ {'l', sizeof(long)},
35+ {'z', sizeof(size_t)},
36+ {'f', sizeof(float)},
37+ {'d', sizeof(double)}
38+ };
2639 const char *s = luaL_checkstring(L, 1);
27− if (*s == 'i')
28− lua_pushinteger(L, sizeof(lua_Integer) * CHAR_BIT);
29− else if (*s == 'f')
30− lua_pushinteger(L, sizeof(lua_Number) * CHAR_BIT);
31− else
32− luaL_argerror(L, 1, lua_pushfstring(L, "invalid option '%s'", s));
33− return 1;
40+ int i;
41+ for (i = 0; i < (int)(sizeof(sizes)/sizeof(sizes[0])); i++) {
42+ if (*s == sizes[i].c) {
43+ lua_pushinteger(L, sizes[i].sz);
44+ return 1;
45+ }
46+ }
47+ return luaL_argerror(L, 1, lua_pushfstring(L, "invalid option '%c'", *s));
3448 }
3549
3650
@@ -77,24 +91,12 @@ static int db_setuservalue (lua_State *L) { · 40 unmodified lines
7791 }
7892
7993
80−static void settabss (lua_State *L, const char *i, const char *v) {
81− lua_pushstring(L, v);
82− lua_setfield(L, -2, i);
83−}
84−
85−
86−static void settabsi (lua_State *L, const char *i, int v) {
87− lua_pushinteger(L, v);
88− lua_setfield(L, -2, i);
89−}
90−
91−
92−static void settabsb (lua_State *L, const char *i, int v) {
93− lua_pushboolean(L, v);
94− lua_setfield(L, -2, i);
95−}
96−
97−
94+/*
95+** Auxiliary function used by several library functions: check for
96+** an optional thread as function's first argument and set 'arg' with
97+** 1 if this argument is present (so that functions can skip it to
98+** access their other arguments)
99+*/
98100 static lua_State *getthread (lua_State *L, int *arg) {
99101 if (lua_isthread(L, 1)) {
100102 *arg = 1;
@@ -102,44 +104,70 @@ static lua_State *getthread (lua_State *L, int *arg) { · 1 unmodified line
102104 }
103105 else {
104106 *arg = 0;
105− return L;
107+ return L; /* function will operate over current thread */
106108 }
107109 }
108110
109111
112+/*
113+** Variations of 'lua_settable', used by 'db_getinfo' to put results
114+** from 'lua_getinfo' into result table. Key is always a string;
115+** value can be a string, an int, or a boolean.
116+*/
117+static void settabss (lua_State *L, const char *k, const char *v) {
118+ lua_pushstring(L, v);
119+ lua_setfield(L, -2, k);
120+}
121+
122+static void settabsi (lua_State *L, const char *k, int v) {
123+ lua_pushinteger(L, v);
124+ lua_setfield(L, -2, k);
125+}
126+
127+static void settabsb (lua_State *L, const char *k, int v) {
128+ lua_pushboolean(L, v);
129+ lua_setfield(L, -2, k);
130+}
131+
132+
133+/*
134+** In function 'db_getinfo', the call to 'lua_getinfo' may push
135+** results on the stack; later it creates the result table to put
136+** these objects. Function 'treatstackoption' puts the result from
137+** 'lua_getinfo' on top of the result table so that it can call
138+** 'lua_setfield'.
139+*/
110140 static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) {
111− if (L == L1) {
112− lua_pushvalue(L, -2);
113− lua_remove(L, -3);
114− }
141+ if (L == L1)
142+ lua_rotate(L, -2, 1); /* exchange object and table */
115143 else
116− lua_xmove(L1, L, 1);
117− lua_setfield(L, -2, fname);
144+ lua_xmove(L1, L, 1); /* move object to the "main" stack */
145+ lua_setfield(L, -2, fname); /* put object into table */
118146 }
119147
120148
149+/*
150+** Calls 'lua_getinfo' and collects all results in a new table.
151+*/
121152 static int db_getinfo (lua_State *L) {
122153 lua_Debug ar;
123154 int arg;
124155 lua_State *L1 = getthread(L, &arg);
125156 const char *options = luaL_optstring(L, arg+2, "flnStu");
126− if (lua_isnumber(L, arg+1)) {
127− if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), &ar)) {
157+ if (lua_isfunction(L, arg + 1)) { /* info about a function? */
158+ options = lua_pushfstring(L, ">%s", options); /* add '>' to 'options' */
159+ lua_pushvalue(L, arg + 1); /* move function to 'L1' stack */
160+ lua_xmove(L, L1, 1);
161+ }
162+ else { /* stack level */
163+ if (!lua_getstack(L1, luaL_checkint(L, arg + 1), &ar)) {
128164 lua_pushnil(L); /* level out of range */
129165 return 1;
130166 }
131167 }
132− else if (lua_isfunction(L, arg+1)) {
133− lua_pushfstring(L, ">%s", options);
134− options = lua_tostring(L, -1);
135− lua_pushvalue(L, arg+1);
136− lua_xmove(L, L1, 1);
137− }
138− else
139− return luaL_argerror(L, arg+1, "function or level expected");
140168 if (!lua_getinfo(L1, options, &ar))
141169 return luaL_argerror(L, arg+2, "invalid option");
142− lua_createtable(L, 0, 2);
170+ lua_newtable(L); /* table to collect results */
143171 if (strchr(options, 'S')) {
144172 settabss(L, "source", ar.source);
145173 settabss(L, "short_src", ar.short_src);
@@ -177,16 +205,16 @@ static int db_getlocal (lua_State *L) { · 31 unmodified lines
177205 if (lua_isfunction(L, arg + 1)) { /* function argument? */
178206 lua_pushvalue(L, arg + 1); /* push function */
179207 lua_pushstring(L, lua_getlocal(L, NULL, nvar)); /* push local name */
180− return 1;
208+ return 1; /* return only name (there is no value) */
181209 }
182210 else { /* stack-level argument */
183211 if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */
184212 return luaL_argerror(L, arg+1, "level out of range");
185213 name = lua_getlocal(L1, &ar, nvar);
186214 if (name) {
187− lua_xmove(L1, L, 1); /* push local value */
215+ lua_xmove(L1, L, 1); /* move local value */
188216 lua_pushstring(L, name); /* push name */
189− lua_pushvalue(L, -2); /* re-order */
217+ lua_rotate(L, -2, 1); /* re-order */
190218 return 2;
191219 }
192220 else {
@@ -211,14 +239,17 @@ static int db_setlocal (lua_State *L) { · 18 unmodified lines
211239 }
212240
213241
242+/*
243+** get (if 'get' is true) or set an upvalue from a closure
244+*/
214245 static int auxupvalue (lua_State *L, int get) {
215246 const char *name;
216− int n = luaL_checkint(L, 2);
217− luaL_checktype(L, 1, LUA_TFUNCTION);
247+ int n = luaL_checkint(L, 2); /* upvalue index */
248+ luaL_checktype(L, 1, LUA_TFUNCTION); /* closure */
218249 name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
219250 if (name == NULL) return 0;
220251 lua_pushstring(L, name);
221− lua_insert(L, -(get+1));
252+ lua_insert(L, -(get+1)); /* no-op if get is false */
222253 return get + 1;
223254 }
224255
@@ -234,13 +265,15 @@ static int db_setupvalue (lua_State *L) { · 9 unmodified lines
234265 }
235266
236267
268+/*
269+** Check whether a given upvalue from a given closure exists and
270+** returns its index
271+*/
237272 static int checkupval (lua_State *L, int argf, int argnup) {
238− lua_Debug ar;
239− int nup = luaL_checkint(L, argnup);
240− luaL_checktype(L, argf, LUA_TFUNCTION);
241− lua_pushvalue(L, argf);
242− lua_getinfo(L, ">u", &ar);
243− luaL_argcheck(L, 1 <= nup && nup <= ar.nups, argnup, "invalid upvalue index");
273+ int nup = luaL_checkint(L, argnup); /* upvalue index */
274+ luaL_checktype(L, argf, LUA_TFUNCTION); /* closure */
275+ luaL_argcheck(L, (lua_getupvalue(L, argf, nup) != NULL), argnup,
276+ "invalid upvalue index");
244277 return nup;
245278 }
246279
@@ -262,25 +295,36 @@ static int db_upvaluejoin (lua_State *L) { · 15 unmodified lines
262295 }
263296
264297
298+/*
299+** The hook table (at registry[HOOKKEY]) maps threads to their current
300+** hook function
301+*/
265302 #define gethooktable(L) luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)
266303
267304
305+/*
306+** Call hook function registered at hook table for the current
307+** thread (if there is one)
308+*/
268309 static void hookf (lua_State *L, lua_Debug *ar) {
269310 static const char *const hooknames[] =
270311 {"call", "return", "line", "count", "tail call"};
271312 gethooktable(L);
272313 lua_pushthread(L);
273− if (lua_rawget(L, -2) == LUA_TFUNCTION) {
274− lua_pushstring(L, hooknames[(int)ar->event]);
314+ if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */
315+ lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */
275316 if (ar->currentline >= 0)
276− lua_pushinteger(L, ar->currentline);
317+ lua_pushinteger(L, ar->currentline); /* push current line */
277318 else lua_pushnil(L);
278319 lua_assert(lua_getinfo(L, "lS", ar));
279− lua_call(L, 2, 0);
320+ lua_call(L, 2, 0); /* call hook function */
280321 }
281322 }
282323
283324
325+/*
326+** Convert a string mask (for 'sethook') into a bit mask
327+*/
284328 static int makemask (const char *smask, int count) {
285329 int mask = 0;
286330 if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
@@ -291,6 +335,9 @@ static int makemask (const char *smask, int count) { · 4 unmodified lines
291335 }
292336
293337
338+/*
339+** Convert a bit mask (for 'gethook') into a string mask
340+*/
294341 static char *unmakemask (int mask, char *smask) {
295342 int i = 0;
296343 if (mask & LUA_MASKCALL) smask[i++] = 'c';
@@ -305,7 +352,7 @@ static int db_sethook (lua_State *L) { · 8 unmodified lines
305352 int arg, mask, count;
306353 lua_Hook func;
307354 lua_State *L1 = getthread(L, &arg);
308− if (lua_isnoneornil(L, arg+1)) {
355+ if (lua_isnoneornil(L, arg+1)) { /* no hook? */
309356 lua_settop(L, arg+1);
310357 func = NULL; mask = 0; count = 0; /* turn off hooks */
311358 }
@@ -321,9 +368,9 @@ static int db_sethook (lua_State *L) { · 9 unmodified lines
321368 lua_pushvalue(L, -1);
322369 lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */
323370 }
324− lua_pushthread(L1); lua_xmove(L1, L, 1);
325− lua_pushvalue(L, arg+1);
326− lua_rawset(L, -3); /* set new hook */
371+ lua_pushthread(L1); lua_xmove(L1, L, 1); /* key */
372+ lua_pushvalue(L, arg+1); /* value */
373+ lua_rawset(L, -3); /* hooktable[L1] = new Lua hook */
327374 lua_sethook(L1, func, mask, count); /* set hooks */
328375 return 0;
329376 }
@@ -340,11 +387,11 @@ static int db_gethook (lua_State *L) { · 10 unmodified lines
340387 else {
341388 gethooktable(L);
342389 lua_pushthread(L1); lua_xmove(L1, L, 1);
343− lua_rawget(L, -2); /* get hook */
390+ lua_rawget(L, -2); /* 1st result = hooktable[L1] */
344391 lua_remove(L, -2); /* remove hook table */
345392 }
346− lua_pushstring(L, unmakemask(mask, buff));
347− lua_pushinteger(L, lua_gethookcount(L1));
393+ lua_pushstring(L, unmakemask(mask, buff)); /* 2nd result = mask */
394+ lua_pushinteger(L, lua_gethookcount(L1)); /* 3rd result = count */
348395 return 3;
349396 }
350397
@@ -379,6 +426,7 @@ static int db_traceback (lua_State *L) { · 28 unmodified lines
379426
380427
381428 static const luaL_Reg dblib[] = {
429+ {"Csize", db_Csize},
382430 {"debug", db_debug},
383431 {"getuservalue", db_getuservalue},
384432 {"gethook", db_gethook},
@@ -387,7 +435,6 @@ static const luaL_Reg dblib[] = { · 2 unmodified lines
387435 {"getregistry", db_getregistry},
388436 {"getmetatable", db_getmetatable},
389437 {"getupvalue", db_getupvalue},
390− {"numbits", db_numbits},
391438 {"upvaluejoin", db_upvaluejoin},
392439 {"upvalueid", db_upvalueid},
393440 {"setuservalue", db_setuservalue},
src/ldo.c+64−42
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ldo.c,v 2.115 2014/03/21 13:52:33 roberto Exp $
2+** $Id: ldo.c,v 2.123 2014/06/19 18:27:20 roberto Exp $
33 ** Stack and Call structure of Lua
44 ** See Copyright Notice in lua.h
55 */
@@ -33,6 +33,8 @@ · 27 unmodified lines
3333
3434
3535
36+#define errorstatus(s) ((s) > LUA_YIELD)
37+
3638
3739 /*
3840 ** {======================================================
@@ -412,24 +414,27 @@ void luaD_call (lua_State *L, StkId func, int nResults, int allowyield) { · 373 unmodified lines
412414 }
413415
414416
415−static void finishCcall (lua_State *L) {
417+/*
418+** Completes the execution of an interrupted C function, calling its
419+** continuation function.
420+*/
421+static void finishCcall (lua_State *L, int status) {
416422 CallInfo *ci = L->ci;
417423 int n;
418− lua_assert(ci->u.c.k != NULL); /* must have a continuation */
419− lua_assert(L->nny == 0);
424+ /* must have a continuation and must be able to call it */
425+ lua_assert(ci->u.c.k != NULL && L->nny == 0);
426+ /* error status can only happen in a protected call */
427+ lua_assert((ci->callstatus & CIST_YPCALL) || status == LUA_YIELD);
420428 if (ci->callstatus & CIST_YPCALL) { /* was inside a pcall? */
421429 ci->callstatus &= ~CIST_YPCALL; /* finish 'lua_pcall' */
422430 L->errfunc = ci->u.c.old_errfunc;
423431 }
424− /* finish 'lua_callk'/'lua_pcall' */
432+ /* finish 'lua_callk'/'lua_pcall'; CIST_YPCALL and 'errfunc' already
433+ handled */
425434 adjustresults(L, ci->nresults);
426435 /* call continuation function */
427− if (!(ci->callstatus & CIST_STAT)) /* no call status? */
428− ci->u.c.status = LUA_YIELD; /* 'default' status */
429− lua_assert(ci->u.c.status != LUA_OK);
430− ci->callstatus = (ci->callstatus & ~(CIST_YPCALL | CIST_STAT)) | CIST_YIELDED;
431436 lua_unlock(L);
432− n = (*ci->u.c.k)(L);
437+ n = (*ci->u.c.k)(L, status, ci->u.c.ctx);
433438 lua_lock(L);
434439 api_checknelems(L, n);
435440 /* finish 'luaD_precall' */
@@ -437,13 +442,20 @@ static void finishCcall (lua_State *L) { · 1 unmodified line
437442 }
438443
439444
445+/*
446+** Executes "full continuation" (everything in the stack) of a
447+** previously interrupted coroutine until the stack is empty (or another
448+** interruption long-jumps out of the loop). If the coroutine is
449+** recovering from an error, 'ud' points to the error status, which must
450+** be passed to the first continuation function (otherwise the default
451+** status is LUA_YIELD).
452+*/
440453 static void unroll (lua_State *L, void *ud) {
441− UNUSED(ud);
442− for (;;) {
443− if (L->ci == &L->base_ci) /* stack is empty? */
444− return; /* coroutine finished normally */
454+ if (ud != NULL) /* error status? */
455+ finishCcall(L, *(int *)ud); /* finish 'lua_pcallk' callee */
456+ while (L->ci != &L->base_ci) { /* something in the stack */
445457 if (!isLua(L->ci)) /* C function? */
446− finishCcall(L);
458+ finishCcall(L, LUA_YIELD); /* complete its execution */
447459 else { /* Lua function */
448460 luaV_finishOp(L); /* finish interrupted instruction */
449461 luaV_execute(L); /* execute down to higher C 'boundary' */
@@ -453,7 +465,8 @@ static void unroll (lua_State *L, void *ud) { · 3 unmodified lines
453465
454466
455467 /*
456−** check whether thread has a suspended protected call
468+** Try to find a suspended protected call (a "recover point") for the
469+** given thread.
457470 */
458471 static CallInfo *findpcall (lua_State *L) {
459472 CallInfo *ci;
@@ -465,6 +478,11 @@ static CallInfo *findpcall (lua_State *L) { · 5 unmodified lines
465478 }
466479
467480
481+/*
482+** Recovers from an error in a coroutine. Finds a recover point (if
483+** there is one) and completes the execution of the interrupted
484+** 'luaD_pcall'. If there is no recover point, returns zero.
485+*/
468486 static int recover (lua_State *L, int status) {
469487 StkId oldtop;
470488 CallInfo *ci = findpcall(L);
@@ -474,12 +492,10 @@ static int recover (lua_State *L, int status) { · 3 unmodified lines
474492 luaF_close(L, oldtop);
475493 seterrorobj(L, status, oldtop);
476494 L->ci = ci;
477− L->allowhook = ci->u.c.old_allowhook;
495+ L->allowhook = getoah(ci->callstatus); /* restore original 'allowhook' */
478496 L->nny = 0; /* should be zero to be yieldable */
479497 luaD_shrinkstack(L);
480498 L->errfunc = ci->u.c.old_errfunc;
481− ci->callstatus |= CIST_STAT; /* call has error status */
482− ci->u.c.status = status; /* (here it is) */
483499 return 1; /* continue running the coroutine */
484500 }
485501
@@ -498,7 +514,11 @@ static l_noret resume_error (lua_State *L, const char *msg, StkId firstArg) { · 12 unmodified lines
498514
499515
500516 /*
501−** do the work for 'lua_resume' in protected mode
517+** Do the work for 'lua_resume' in protected mode. Most of the work
518+** depends on the status of the coroutine: initial state, suspended
519+** inside a hook, or regulary suspended (optionally with a continuation
520+** function), plus erroneous cases: non-suspended coroutine or dead
521+** coroutine.
502522 */
503523 static void resume (lua_State *L, void *ud) {
504524 int nCcalls = L->nCcalls;
@@ -516,24 +536,22 @@ static void resume (lua_State *L, void *ud) { · 11 unmodified lines
516536 else if (L->status != LUA_YIELD)
517537 resume_error(L, "cannot resume dead coroutine", firstArg);
518538 else { /* resuming from previous yield */
519− L->status = LUA_OK;
539+ L->status = LUA_OK; /* mark that it is running (again) */
520540 ci->func = restorestack(L, ci->extra);
521541 if (isLua(ci)) /* yielded inside a hook? */
522542 luaV_execute(L); /* just continue running Lua code */
523543 else { /* 'common' yield */
524− if (ci->u.c.k != NULL) { /* does it have a continuation? */
544+ if (ci->u.c.k != NULL) { /* does it have a continuation function? */
525545 int n;
526− ci->u.c.status = LUA_YIELD; /* 'default' status */
527− ci->callstatus |= CIST_YIELDED;
528546 lua_unlock(L);
529− n = (*ci->u.c.k)(L); /* call continuation */
547+ n = (*ci->u.c.k)(L, LUA_YIELD, ci->u.c.ctx); /* call continuation */
530548 lua_lock(L);
531549 api_checknelems(L, n);
532550 firstArg = L->top - n; /* yield results come from continuation */
533551 }
534552 luaD_poscall(L, firstArg); /* finish 'luaD_precall' */
535553 }
536− unroll(L, NULL);
554+ unroll(L, NULL); /* run continuation */
537555 }
538556 lua_assert(nCcalls == L->nCcalls);
539557 }
@@ -541,7 +559,7 @@ static void resume (lua_State *L, void *ud) { · 1 unmodified line
541559
542560 LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) {
543561 int status;
544− int oldnny = L->nny; /* save 'nny' */
562+ int oldnny = L->nny; /* save "number of non-yieldable" calls */
545563 lua_lock(L);
546564 luai_userstateresume(L, nargs);
547565 L->nCcalls = (from) ? from->nCcalls + 1 : 1;
@@ -550,18 +568,17 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) { · 2 unmodified lines
550568 status = luaD_rawrunprotected(L, resume, L->top - nargs);
551569 if (status == -1) /* error calling 'lua_resume'? */
552570 status = LUA_ERRRUN;
553− else { /* yield or regular error */
554− while (status != LUA_OK && status != LUA_YIELD) { /* error? */
555− if (recover(L, status)) /* recover point? */
556− status = luaD_rawrunprotected(L, unroll, NULL); /* run continuation */
557− else { /* unrecoverable error */
558− L->status = cast_byte(status); /* mark thread as `dead' */
559− seterrorobj(L, status, L->top);
560− L->ci->top = L->top;
561− break;
562− }
571+ else { /* continue running after recoverable errors */
572+ while (errorstatus(status) && recover(L, status)) {
573+ /* unroll continuation */
574+ status = luaD_rawrunprotected(L, unroll, &status);
575+ }
576+ if (errorstatus(status)) { /* unrecoverable error? */
577+ L->status = cast_byte(status); /* mark thread as `dead' */
578+ seterrorobj(L, status, L->top); /* push error message */
579+ L->ci->top = L->top;
563580 }
564− lua_assert(status == L->status);
581+ else lua_assert(status == L->status); /* normal end or yield */
565582 }
566583 L->nny = oldnny; /* restore 'nny' */
567584 L->nCcalls--;
@@ -571,7 +588,12 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) { · 3 unmodified lines
571588 }
572589
573590
574−LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k) {
591+LUA_API int lua_isyieldable (lua_State *L) {
592+ return (L->nny == 0);
593+}
594+
595+
596+LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_KFunction k) {
575597 CallInfo *ci = L->ci;
576598 luai_userstateyield(L, nresults);
577599 lua_lock(L);
@@ -645,7 +667,7 @@ static void checkmode (lua_State *L, const char *mode, const char *x) { · 67 unmodified lines
645667
646668
647669 static void f_parser (lua_State *L, void *ud) {
648− Closure *cl;
670+ LClosure *cl;
649671 struct SParser *p = cast(struct SParser *, ud);
650672 int c = zgetc(p->z); /* read first character */
651673 if (c == LUA_SIGNATURE[0]) {
@@ -656,8 +678,8 @@ static void f_parser (lua_State *L, void *ud) { · 4 unmodified lines
656678 checkmode(L, p->mode, "text");
657679 cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c);
658680 }
659− lua_assert(cl->l.nupvalues == cl->l.p->sizeupvalues);
660− luaF_initupvals(L, &cl->l);
681+ lua_assert(cl->nupvalues == cl->p->sizeupvalues);
682+ luaF_initupvals(L, cl);
661683 }
662684
663685
src/ldump.c+18−9
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ldump.c,v 2.27 2014/03/11 18:56:27 roberto Exp $
2+** $Id: ldump.c,v 2.32 2014/06/18 18:35:43 roberto Exp $
33 ** save precompiled Lua chunks
44 ** See Copyright Notice in lua.h
55 */
@@ -27,7 +27,7 @@ typedef struct { · 21 unmodified lines
2727
2828 /*
2929 ** All high-level dumps go through DumpVector; you can change it to
30−** change the endianess of the result
30+** change the endianness of the result
3131 */
3232 #define DumpVector(v,n,D) DumpBlock(v,(n)*sizeof((v)[0]),D)
3333
@@ -73,7 +73,7 @@ static void DumpString (const TString *s, DumpState *D) { · 39 unmodified lines
7373 else {
7474 size_t size = s->tsv.len + 1; /* include trailing '\0' */
7575 if (size < 0xFF)
76− DumpByte(size, D);
76+ DumpByte(cast_int(size), D);
7777 else {
7878 DumpByte(0xFF, D);
7979 DumpVar(size, D);
@@ -89,7 +89,7 @@ static void DumpCode (const Proto *f, DumpState *D) { · 9 unmodified lines
8989 }
9090
9191
92−static void DumpFunction(const Proto *f, DumpState *D);
92+static void DumpFunction(const Proto *f, TString *psource, DumpState *D);
9393
9494 static void DumpConstants (const Proto *f, DumpState *D) {
9595 int i;
@@ -118,10 +118,15 @@ static void DumpConstants (const Proto *f, DumpState *D) { · 22 unmodified lines
118118 lua_assert(0);
119119 }
120120 }
121− n = f->sizep;
121+}
122+
123+
124+static void DumpProtos (const Proto *f, DumpState *D) {
125+ int i;
126+ int n = f->sizep;
122127 DumpInt(n, D);
123128 for (i = 0; i < n; i++)
124− DumpFunction(f->p[i], D);
129+ DumpFunction(f->p[i], f->source, D);
125130 }
126131
127132
@@ -137,7 +142,6 @@ static void DumpUpvalues (const Proto *f, DumpState *D) { · 9 unmodified lines
137142
138143 static void DumpDebug (const Proto *f, DumpState *D) {
139144 int i, n;
140− DumpString((D->strip) ? NULL : f->source, D);
141145 n = (D->strip) ? 0 : f->sizelineinfo;
142146 DumpInt(n, D);
143147 DumpVector(f->lineinfo, n, D);
@@ -155,7 +159,11 @@ static void DumpDebug (const Proto *f, DumpState *D) { · 11 unmodified lines
155159 }
156160
157161
158−static void DumpFunction (const Proto *f, DumpState *D) {
162+static void DumpFunction (const Proto *f, TString *psource, DumpState *D) {
163+ if (D->strip || f->source == psource)
164+ DumpString(NULL, D); /* no debug info or same source as its parent */
165+ else
166+ DumpString(f->source, D);
159167 DumpInt(f->linedefined, D);
160168 DumpInt(f->lastlinedefined, D);
161169 DumpByte(f->numparams, D);
@@ -164,6 +172,7 @@ static void DumpFunction (const Proto *f, DumpState *D) { · 2 unmodified lines
164172 DumpCode(f, D);
165173 DumpConstants(f, D);
166174 DumpUpvalues(f, D);
175+ DumpProtos(f, D);
167176 DumpDebug(f, D);
168177 }
169178
@@ -196,7 +205,7 @@ int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, · 26 unmodified lines
196205 D.status = 0;
197206 DumpHeader(&D);
198207 DumpByte(f->sizeupvalues, &D);
199− DumpFunction(f, &D);
208+ DumpFunction(f, NULL, &D);
200209 return D.status;
201210 }
202211
src/lfunc.c+13−10
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lfunc.c,v 2.41 2014/02/18 13:39:37 roberto Exp $
2+** $Id: lfunc.c,v 2.43 2014/06/19 18:27:20 roberto Exp $
33 ** Auxiliary functions to manipulate prototypes and closures
44 ** See Copyright Notice in lua.h
55 */
@@ -20,18 +20,20 @@ · 14 unmodified lines
2020
2121
2222
23−Closure *luaF_newCclosure (lua_State *L, int n) {
24− Closure *c = &luaC_newobj(L, LUA_TCCL, sizeCclosure(n))->cl;
25− c->c.nupvalues = cast_byte(n);
23+CClosure *luaF_newCclosure (lua_State *L, int n) {
24+ GCObject *o = luaC_newobj(L, LUA_TCCL, sizeCclosure(n));
25+ CClosure *c = gco2ccl(o);
26+ c->nupvalues = cast_byte(n);
2627 return c;
2728 }
2829
2930
30−Closure *luaF_newLclosure (lua_State *L, int n) {
31− Closure *c = &luaC_newobj(L, LUA_TLCL, sizeLclosure(n))->cl;
32− c->l.p = NULL;
33− c->l.nupvalues = cast_byte(n);
34− while (n--) c->l.upvals[n] = NULL;
31+LClosure *luaF_newLclosure (lua_State *L, int n) {
32+ GCObject *o = luaC_newobj(L, LUA_TLCL, sizeLclosure(n));
33+ LClosure *c = gco2lcl(o);
34+ c->p = NULL;
35+ c->nupvalues = cast_byte(n);
36+ while (n--) c->upvals[n] = NULL;
3537 return c;
3638 }
3739
@@ -93,7 +95,8 @@ void luaF_close (lua_State *L, StkId level) { · 55 unmodified lines
9395
9496
9597 Proto *luaF_newproto (lua_State *L) {
96− Proto *f = &luaC_newobj(L, LUA_TPROTO, sizeof(Proto))->p;
98+ GCObject *o = luaC_newobj(L, LUA_TPROTO, sizeof(Proto));
99+ Proto *f = gco2p(o);
97100 f->k = NULL;
98101 f->sizek = 0;
99102 f->p = NULL;
src/lfunc.h+3−3
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lfunc.h,v 2.13 2014/02/18 13:39:37 roberto Exp $
2+** $Id: lfunc.h,v 2.14 2014/06/19 18:27:20 roberto Exp $
33 ** Auxiliary functions to manipulate prototypes and closures
44 ** See Copyright Notice in lua.h
55 */
@@ -41,8 +41,8 @@ struct UpVal { · 35 unmodified lines
4141
4242
4343 LUAI_FUNC Proto *luaF_newproto (lua_State *L);
44−LUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems);
45−LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, int nelems);
44+LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nelems);
45+LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nelems);
4646 LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl);
4747 LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level);
4848 LUAI_FUNC void luaF_close (lua_State *L, StkId level);
src/lgc.c+70−76
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lgc.c,v 2.179 2014/03/21 13:52:33 roberto Exp $
2+** $Id: lgc.c,v 2.183 2014/05/25 19:08:32 roberto Exp $
33 ** Garbage Collector
44 ** See Copyright Notice in lua.h
55 */
@@ -172,7 +172,7 @@ void luaC_barrierback_ (lua_State *L, GCObject *o) { · 166 unmodified lines
172172 ** closures pointing to it. So, we assume that the object being assigned
173173 ** must be marked.
174174 */
175−LUAI_FUNC void luaC_upvalbarrier_ (lua_State *L, UpVal *uv) {
175+void luaC_upvalbarrier_ (lua_State *L, UpVal *uv) {
176176 global_State *g = G(L);
177177 GCObject *o = gcvalue(uv->v);
178178 lua_assert(!upisopen(uv)); /* ensured by macro luaC_upvalbarrier */
@@ -495,8 +495,7 @@ static lu_mem traverseLclosure (global_State *g, LClosure *cl) { · 316 unmodified lines
495495 }
496496
497497
498−static lu_mem traversestack (global_State *g, lua_State *th) {
499− int n = 0;
498+static lu_mem traversethread (global_State *g, lua_State *th) {
500499 StkId o = th->stack;
501500 if (o == NULL)
502501 return 1; /* stack not completely built yet */
@@ -514,16 +513,9 @@ static lu_mem traversestack (global_State *g, lua_State *th) { · 11 unmodified lines
514513 g->twups = th;
515514 }
516515 }
517− else {
518− CallInfo *ci;
519− for (ci = &th->base_ci; ci != th->ci; ci = ci->next)
520− n++; /* count call infos to compute size */
521− /* should not change the stack during an emergency gc cycle */
522− if (g->gckind != KGC_EMERGENCY)
523− luaD_shrinkstack(th);
524− }
525− return sizeof(lua_State) + sizeof(TValue) * th->stacksize +
526− sizeof(CallInfo) * n;
516+ else if (g->gckind != KGC_EMERGENCY)
517+ luaD_shrinkstack(th); /* do not change stack in emergency cycle */
518+ return (sizeof(lua_State) + sizeof(TValue) * th->stacksize);
527519 }
528520
529521
@@ -561,7 +553,7 @@ static void propagatemark (global_State *g) { · 31 unmodified lines
561553 th->gclist = g->grayagain;
562554 g->grayagain = o; /* insert into 'grayagain' list */
563555 black2gray(o);
564− size = traversestack(g, th);
556+ size = traversethread(g, th);
565557 break;
566558 }
567559 case LUA_TPROTO: {
@@ -777,9 +769,11 @@ static GCObject **sweeptolive (lua_State *L, GCObject **p, int *n) { · 209 unmodified lines
777769 */
778770 static void checkSizes (lua_State *L, global_State *g) {
779771 if (g->gckind != KGC_EMERGENCY) {
772+ l_mem olddebt = g->GCdebt;
780773 luaZ_freebuffer(L, &g->buff); /* free concatenation buffer */
781774 if (g->strt.nuse < g->strt.size / 4) /* string table too big? */
782775 luaS_resize(L, g->strt.size / 2); /* shrink it a little */
776+ g->GCestimate += g->GCdebt - olddebt; /* update estimate */
783777 }
784778 }
785779
@@ -835,6 +829,21 @@ static void GCTM (lua_State *L, int propagateerrors) { · 49 unmodified lines
835829 }
836830
837831
832+/*
833+** call a few (up to 'g->gcfinnum') finalizers
834+*/
835+static int runafewfinalizers (lua_State *L) {
836+ global_State *g = G(L);
837+ unsigned int i;
838+ lua_assert(!g->tobefnz || g->gcfinnum > 0);
839+ for (i = 0; g->tobefnz && i < g->gcfinnum; i++)
840+ GCTM(L, 1); /* call one finalizer */
841+ g->gcfinnum = (!g->tobefnz) ? 0 /* nothing more to finalize? */
842+ : g->gcfinnum * 2; /* else call a few more next time */
843+ return i;
844+}
845+
846+
838847 /*
839848 ** call all pending finalizers
840849 */
@@ -888,18 +897,17 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { · 47 unmodified lines
888897 return; /* nothing to be done */
889898 else { /* move 'o' to 'finobj' list */
890899 GCObject **p;
891− if (g->sweepgc == &o->gch.next) { /* avoid removing current sweep object */
892− lua_assert(issweepphase(g));
893− g->sweepgc = sweeptolive(L, g->sweepgc, NULL);
900+ if (issweepphase(g)) {
901+ makewhite(g, o); /* "sweep" object 'o' */
902+ if (g->sweepgc == &o->gch.next) /* shoud not remove 'sweepgc' object */
903+ g->sweepgc = sweeptolive(L, g->sweepgc, NULL); /* change 'sweepgc' */
894904 }
895905 /* search for pointer pointing to 'o' */
896906 for (p = &g->allgc; *p != o; p = &gch(*p)->next) { /* empty */ }
897− *p = o->gch.next; /* remove 'o' from its list */
907+ *p = o->gch.next; /* remove 'o' from 'allgc' list */
898908 o->gch.next = g->finobj; /* link it in "fin" list */
899909 g->finobj = o;
900910 l_setbit(o->gch.marked, FINALIZEDBIT); /* mark it as such */
901− if (issweepphase(g))
902− makewhite(g, o); /* "sweep" object */
903911 }
904912 }
905913
@@ -918,9 +926,9 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { · 12 unmodified lines
918926 ** set a reasonable "time" to wait before starting a new GC cycle;
919927 ** cycle will start when memory use hits threshold
920928 */
921−static void setpause (global_State *g, l_mem estimate) {
929+static void setpause (global_State *g) {
922930 l_mem threshold, debt;
923− estimate = estimate / PAUSEADJ; /* adjust 'estimate' */
931+ l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */
924932 threshold = (g->gcpause < MAX_LMEM / estimate) /* overflow? */
925933 ? estimate * g->gcpause /* no overflow */
926934 : MAX_LMEM; /* overflow; truncate to maximum */
@@ -964,8 +972,9 @@ void luaC_freeallobjects (lua_State *L) { · 37 unmodified lines
964972
965973 static l_mem atomic (lua_State *L) {
966974 global_State *g = G(L);
967− l_mem work = -cast(l_mem, g->GCmemtrav); /* start counting work */
975+ l_mem work;
968976 GCObject *origweak, *origall;
977+ g->GCmemtrav = 0; /* start counting work */
969978 lua_assert(!iswhite(obj2gco(g->mainthread)));
970979 g->gcstate = GCSinsideatomic;
971980 markobject(g, L); /* mark running thread */
@@ -975,10 +984,10 @@ static l_mem atomic (lua_State *L) { · 3 unmodified lines
975984 /* remark occasional upvalues of (maybe) dead threads */
976985 remarkupvals(g);
977986 propagateall(g); /* propagate changes */
978− work += g->GCmemtrav; /* stop counting (do not (re)count grays) */
987+ work = g->GCmemtrav; /* stop counting (do not (re)count grays) */
979988 /* traverse objects caught by write barrier and by 'remarkupvals' */
980989 retraversegrays(g);
981− work -= g->GCmemtrav; /* restart counting */
990+ g->GCmemtrav = 0; /* restart counting */
982991 convergeephemerons(g);
983992 /* at this point, all strongly accessible objects are marked. */
984993 /* Clear values from weak tables, before checking finalizers */
@@ -990,7 +999,7 @@ static l_mem atomic (lua_State *L) { · 5 unmodified lines
990999 g->gcfinnum = 1; /* there may be objects to be finalized */
9911000 markbeingfnz(g); /* mark objects that will be finalized */
9921001 propagateall(g); /* remark, to propagate 'resurrection' */
993− work -= g->GCmemtrav; /* restart counting */
1002+ g->GCmemtrav = 0; /* restart counting */
9941003 convergeephemerons(g);
9951004 /* at this point, all resurrected objects are marked. */
9961005 /* remove dead objects from weak tables */
@@ -1008,7 +1017,9 @@ static l_mem atomic (lua_State *L) { · 11 unmodified lines
10081017 static lu_mem sweepstep (lua_State *L, global_State *g,
10091018 int nextstate, GCObject **nextlist) {
10101019 if (g->sweepgc) {
1020+ l_mem olddebt = g->GCdebt;
10111021 g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX);
1022+ g->GCestimate += g->GCdebt - olddebt; /* update estimate */
10121023 if (g->sweepgc) /* is there still something to sweep? */
10131024 return (GCSWEEPMAX * GCSWEEPCOST);
10141025 }
@@ -1023,28 +1034,26 @@ static lu_mem singlestep (lua_State *L) { · 8 unmodified lines
10231034 global_State *g = G(L);
10241035 switch (g->gcstate) {
10251036 case GCSpause: {
1026− /* start to count memory traversed */
10271037 g->GCmemtrav = g->strt.size * sizeof(GCObject*);
10281038 restartcollection(g);
10291039 g->gcstate = GCSpropagate;
10301040 return g->GCmemtrav;
10311041 }
10321042 case GCSpropagate: {
1033− lu_mem oldtrav = g->GCmemtrav;
1043+ g->GCmemtrav = 0;
10341044 lua_assert(g->gray);
10351045 propagatemark(g);
10361046 if (g->gray == NULL) /* no more `gray' objects? */
10371047 g->gcstate = GCSatomic; /* finish propagate phase */
1038− return g->GCmemtrav - oldtrav; /* memory traversed in this step */
1048+ return g->GCmemtrav; /* memory traversed in this step */
10391049 }
10401050 case GCSatomic: {
10411051 lu_mem work;
10421052 int sw;
10431053 propagateall(g); /* make sure gray list is empty */
1044− g->GCestimate = g->GCmemtrav; /* save what was counted */;
10451054 work = atomic(L); /* work is what was traversed by 'atomic' */
1046− g->GCestimate += work; /* estimate of total memory traversed */
10471055 sw = entersweep(L);
1056+ g->GCestimate = gettotalbytes(g); /* first estimate */;
10481057 return work + sw * GCSWEEPCOST;
10491058 }
10501059 case GCSswpallgc: { /* sweep "regular" objects */
@@ -1062,10 +1071,15 @@ static lu_mem singlestep (lua_State *L) { · 11 unmodified lines
10621071 g->gcstate = GCScallfin;
10631072 return 0;
10641073 }
1065− case GCScallfin: { /* state to finish calling finalizers */
1066− /* do nothing here; should be handled by 'luaC_forcestep' */
1067− g->gcstate = GCSpause; /* finish collection */
1068− return 0;
1074+ case GCScallfin: { /* call remaining finalizers */
1075+ if (g->tobefnz && g->gckind != KGC_EMERGENCY) {
1076+ int n = runafewfinalizers(L);
1077+ return (n * GCFINALIZECOST);
1078+ }
1079+ else { /* emergency mode or no more finalizers */
1080+ g->gcstate = GCSpause; /* finish collection */
1081+ return 0;
1082+ }
10691083 }
10701084 default: lua_assert(0); return 0;
10711085 }
@@ -1083,21 +1097,6 @@ void luaC_runtilstate (lua_State *L, int statesmask) { · 11 unmodified lines
10831097 }
10841098
10851099
1086−/*
1087−** run a few (up to 'g->gcfinnum') finalizers
1088−*/
1089−static int runafewfinalizers (lua_State *L) {
1090− global_State *g = G(L);
1091− unsigned int i;
1092− lua_assert(!g->tobefnz || g->gcfinnum > 0);
1093− for (i = 0; g->tobefnz && i < g->gcfinnum; i++)
1094− GCTM(L, 1); /* call one finalizer */
1095− g->gcfinnum = (!g->tobefnz) ? 0 /* nothing more to finalize? */
1096− : g->gcfinnum * 2; /* else call a few more next time */
1097− return i;
1098−}
1099−
1100−
11011100 /*
11021101 ** get GC debt and convert it from Kb to 'work units' (avoid zero debt
11031102 ** and overflows)
@@ -1115,23 +1114,17 @@ static l_mem getdebt (global_State *g) { · 11 unmodified lines
11151114 */
11161115 void luaC_step (lua_State *L) {
11171116 global_State *g = G(L);
1118− l_mem debt = getdebt(g);
1117+ l_mem debt = getdebt(g); /* GC deficit (be paid now) */
11191118 if (!g->gcrunning) { /* not running? */
11201119 luaE_setdebt(g, -GCSTEPSIZE * 10); /* avoid being called too often */
11211120 return;
11221121 }
1123− do {
1124− if (g->gcstate == GCScallfin && g->tobefnz) {
1125− unsigned int n = runafewfinalizers(L);
1126− debt -= (n * GCFINALIZECOST);
1127− }
1128− else { /* perform one single step */
1129− lu_mem work = singlestep(L);
1130− debt -= work;
1131− }
1122+ do { /* repeat until pause or enough "credit" (negative debt) */
1123+ lu_mem work = singlestep(L); /* perform one single step */
1124+ debt -= work;
11321125 } while (debt > -GCSTEPSIZE && g->gcstate != GCSpause);
11331126 if (g->gcstate == GCSpause)
1134− setpause(g, g->GCestimate); /* pause until next cycle */
1127+ setpause(g); /* pause until next cycle */
11351128 else {
11361129 debt = (debt / g->gcstepmul) * STEPMULADJ; /* convert 'work units' to Kb */
11371130 luaE_setdebt(g, debt);
@@ -1141,29 +1134,30 @@ void luaC_step (lua_State *L) { · 3 unmodified lines
11411134
11421135
11431136 /*
1144−** performs a full GC cycle; if "isemergency", does not call
1145−** finalizers (which could change stack positions)
1137+** Performs a full GC cycle; if "isemergency", set a flag to avoid
1138+** some operations which could change the interpreter state in some
1139+** unexpected ways (running finalizers and shrinking some structures).
1140+** Before running the collection, check 'keepinvariant'; if it is true,
1141+** there may be some objects marked as black, so the collector has
1142+** to sweep all objects to turn them back to white (as white has not
1143+** changed, nothing will be collected).
11461144 */
11471145 void luaC_fullgc (lua_State *L, int isemergency) {
11481146 global_State *g = G(L);
11491147 lua_assert(g->gckind == KGC_NORMAL);
1150− if (isemergency) /* do not run finalizers during emergency GC */
1151− g->gckind = KGC_EMERGENCY;
1152− else
1153− callallpendingfinalizers(L, 1);
1154− if (keepinvariant(g)) { /* may there be some black objects? */
1155− /* must sweep all objects to turn them back to white
1156− (as white has not changed, nothing will be collected) */
1157− entersweep(L);
1148+ if (isemergency) g->gckind = KGC_EMERGENCY; /* set flag */
1149+ if (keepinvariant(g)) { /* black objects? */
1150+ entersweep(L); /* sweep everything to turn them back to white */
11581151 }
11591152 /* finish any pending sweep phase to start a new cycle */
11601153 luaC_runtilstate(L, bitmask(GCSpause));
11611154 luaC_runtilstate(L, ~bitmask(GCSpause)); /* start new collection */
1162− luaC_runtilstate(L, bitmask(GCSpause)); /* run entire collection */
1155+ luaC_runtilstate(L, bitmask(GCScallfin)); /* run up to finalizers */
1156+ /* estimate must be correct after a full GC cycle */
1157+ lua_assert(g->GCestimate == gettotalbytes(g));
1158+ luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */
11631159 g->gckind = KGC_NORMAL;
1164− setpause(g, gettotalbytes(g));
1165− if (!isemergency) /* do not run finalizers during emergency GC */
1166− callallpendingfinalizers(L, 1);
1160+ setpause(g);
11671161 }
11681162
11691163 /* }====================================================== */
src/linit.c+5−3
@@ -1,5 +1,5 @@
11 /*
2−** $Id: linit.c,v 1.33 2014/02/06 17:32:33 roberto Exp $
2+** $Id: linit.c,v 1.34 2014/05/15 19:28:34 roberto Exp $
33 ** Initialization of libraries for lua.c and other clients
44 ** See Copyright Notice in lua.h
55 */
@@ -34,10 +34,12 @@ static const luaL_Reg loadedlibs[] = { · 28 unmodified lines
3434 {LUA_IOLIBNAME, luaopen_io},
3535 {LUA_OSLIBNAME, luaopen_os},
3636 {LUA_STRLIBNAME, luaopen_string},
37− {LUA_UTF8LIBNAME, luaopen_utf8},
38− {LUA_BITLIBNAME, luaopen_bit32},
3937 {LUA_MATHLIBNAME, luaopen_math},
4038 {LUA_DBLIBNAME, luaopen_debug},
39+ {LUA_UTF8LIBNAME, luaopen_utf8},
40+#if defined(LUA_COMPAT_BITLIB)
41+ {LUA_BITLIBNAME, luaopen_bit32},
42+#endif
4143 {NULL, NULL}
4244 };
4345
src/liolib.c+89−24
@@ -1,5 +1,5 @@
11 /*
2−** $Id: liolib.c,v 2.120 2014/03/19 18:57:42 roberto Exp $
2+** $Id: liolib.c,v 2.126 2014/06/02 03:00:51 roberto Exp $
33 ** Standard I/O (and system) library
44 ** See Copyright Notice in lua.h
55 */
@@ -15,7 +15,9 @@ · 9 unmodified lines
1515 #endif
1616
1717
18+#include <ctype.h>
1819 #include <errno.h>
20+#include <locale.h>
1921 #include <stdio.h>
2022 #include <stdlib.h>
2123 #include <string.h>
@@ -104,6 +106,8 @@ · 82 unmodified lines
104106
105107 #if defined(LUA_USE_POSIX) /* { */
106108
109+#include <sys/types.h>
110+
107111 #define l_fseek(f,o,w) fseeko(f,o,w)
108112 #define l_ftell(f) ftello(f)
109113 #define l_seeknum off_t
@@ -317,14 +321,10 @@ static int io_readline (lua_State *L); · 207 unmodified lines
317321
318322
319323 static void aux_lines (lua_State *L, int toclose) {
320− int i;
321324 int n = lua_gettop(L) - 1; /* number of arguments to read */
322− /* ensure that arguments will fit here and into 'io_readline' stack */
323− luaL_argcheck(L, n <= LUA_MINSTACK - 3, LUA_MINSTACK - 3, "too many options");
324− lua_pushvalue(L, 1); /* file handle */
325325 lua_pushinteger(L, n); /* number of arguments to read */
326326 lua_pushboolean(L, toclose); /* close/not close file when finished */
327− for (i = 1; i <= n; i++) lua_pushvalue(L, i + 1); /* copy arguments */
327+ lua_rotate(L, 2, 2); /* move 'n' and 'toclose' to their positions */
328328 lua_pushcclosure(L, io_readline, 3 + n);
329329 }
330330
@@ -363,26 +363,93 @@ static int io_lines (lua_State *L) { · 32 unmodified lines
363363 */
364364
365365
366−static int read_integer (lua_State *L, FILE *f) {
367− lua_Integer d;
368− if (fscanf(f, LUA_INTEGER_SCAN, &d) == 1) {
369− lua_pushinteger(L, d);
370− return 1;
366+/* maximum length of a numeral */
367+#define MAXRN 200
368+
369+/* auxiliary structure used by 'read_number' */
370+typedef struct {
371+ FILE *f; /* file being read */
372+ int c; /* current character (look ahead) */
373+ int n; /* number of elements in buffer 'buff' */
374+ char buff[MAXRN + 1]; /* +1 for ending '\0' */
375+} RN;
376+
377+
378+/*
379+** Add current char to buffer (if not out of space) and read next one
380+*/
381+static int nextc (RN *rn) {
382+ if (rn->n >= MAXRN) { /* buffer overflow? */
383+ rn->buff[0] = '\0'; /* invalidate result */
384+ return 0; /* fail */
371385 }
372386 else {
373− lua_pushnil(L); /* "result" to be removed */
374− return 0; /* read fails */
387+ rn->buff[rn->n++] = rn->c; /* save current char */
388+ rn->c = l_getc(rn->f); /* read next one */
389+ return 1;
375390 }
376391 }
377392
378393
394+/*
395+** Accept current char if it is in 'set' (of size 1 or 2)
396+*/
397+static int test2 (RN *rn, const char *set) {
398+ if (rn->c == set[0] || (rn->c == set[1] && rn->c != '\0'))
399+ return nextc(rn);
400+ else return 0;
401+}
402+
403+
404+/*
405+** Read a sequence of (hexa)digits
406+*/
407+static int readdigits (RN *rn, int hexa) {
408+ int count = 0;
409+ while ((hexa ? isxdigit(rn->c) : isdigit(rn->c)) && nextc(rn))
410+ count++;
411+ return count;
412+}
413+
414+
415+/* access to locale "radix character" (decimal point) */
416+#if !defined(getlocaledecpoint)
417+#define getlocaledecpoint() (localeconv()->decimal_point[0])
418+#endif
419+
420+
421+/*
422+** Read a number: first reads a valid prefix of a numeral into a buffer.
423+** Then it calls 'lua_strtonum' to check whether the format is correct
424+** and to convert it to a Lua number
425+*/
379426 static int read_number (lua_State *L, FILE *f) {
380− lua_Number d;
381− if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) {
382− lua_pushnumber(L, d);
383− return 1;
427+ RN rn;
428+ int count = 0;
429+ int hexa = 0;
430+ char decp[2] = ".";
431+ rn.f = f; rn.n = 0;
432+ decp[0] = getlocaledecpoint(); /* get decimal point from locale */
433+ l_lockfile(rn.f);
434+ do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */
435+ test2(&rn, "-+"); /* optional signal */
436+ if (test2(&rn, "0")) {
437+ if (test2(&rn, "xX")) hexa = 1; /* numeral is hexadecimal */
438+ else count = 1; /* count initial '0' as a valid digit */
384439 }
385− else {
440+ count += readdigits(&rn, hexa); /* integral part */
441+ if (test2(&rn, decp)) /* decimal point? */
442+ count += readdigits(&rn, hexa); /* fractionary part */
443+ if (count > 0 && test2(&rn, (hexa ? "pP" : "eE"))) { /* exponent mark? */
444+ test2(&rn, "-+"); /* exponent signal */
445+ readdigits(&rn, 0); /* exponent digits */
446+ }
447+ ungetc(rn.c, rn.f); /* unread look-ahead char */
448+ l_unlockfile(rn.f);
449+ rn.buff[rn.n] = '\0'; /* finish string */
450+ if (lua_strtonum(L, rn.buff)) /* is this a valid number? */
451+ return 1; /* ok */
452+ else { /* invalid format */
386453 lua_pushnil(L); /* "result" to be removed */
387454 return 0; /* read fails */
388455 }
@@ -455,12 +522,9 @@ static int g_read (lua_State *L, FILE *f, int first) { · 66 unmodified lines
455522 success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
456523 }
457524 else {
458− const char *p = lua_tostring(L, n);
459− luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
460− switch (p[1]) {
461− case 'i': /* integer */
462− success = read_integer(L, f);
463− break;
525+ const char *p = luaL_checkstring(L, n);
526+ if (*p == '*') p++; /* skip optional '*' (for compatibility) */
527+ switch (*p) {
464528 case 'n': /* number */
465529 success = read_number(L, f);
466530 break;
@@ -507,6 +571,7 @@ static int io_readline (lua_State *L) { · 40 unmodified lines
507571 if (isclosed(p)) /* file is already closed? */
508572 return luaL_error(L, "file is already closed");
509573 lua_settop(L , 1);
574+ luaL_checkstack(L, n, "too many arguments");
510575 for (i = 1; i <= n; i++) /* push arguments to 'g_read' */
511576 lua_pushvalue(L, lua_upvalueindex(3 + i));
512577 n = g_read(L, p->f, 2); /* 'n' is number of results */
src/llex.c+69−53
@@ -1,5 +1,5 @@
11 /*
2−** $Id: llex.c,v 2.74 2014/02/14 15:23:51 roberto Exp $
2+** $Id: llex.c,v 2.78 2014/05/21 15:22:02 roberto Exp $
33 ** Lexical Analyzer
44 ** See Copyright Notice in lua.h
55 */
@@ -183,12 +183,26 @@ void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source, · 177 unmodified lines
183183 */
184184
185185
186+static int check_next1 (LexState *ls, int c) {
187+ if (ls->current == c) {
188+ next(ls);
189+ return 1;
190+ }
191+ else return 0;
192+}
186193
187−static int check_next (LexState *ls, const char *set) {
188− if (ls->current == '\0' || !strchr(set, ls->current))
189− return 0;
190− save_and_next(ls);
191− return 1;
194+
195+/*
196+** Check whether current char is in set 'set' (with two chars) and
197+** saves it
198+*/
199+static int check_next2 (LexState *ls, const char *set) {
200+ lua_assert(set[2] == '\0');
201+ if (ls->current == set[0] || ls->current == set[1]) {
202+ save_and_next(ls);
203+ return 1;
204+ }
205+ else return 0;
192206 }
193207
194208
@@ -196,10 +210,12 @@ static int check_next (LexState *ls, const char *set) { · 1 unmodified line
196210 ** change all characters 'from' in buffer to 'to'
197211 */
198212 static void buffreplace (LexState *ls, char from, char to) {
199− size_t n = luaZ_bufflen(ls->buff);
200− char *p = luaZ_buffer(ls->buff);
201− while (n--)
202− if (p[n] == from) p[n] = to;
213+ if (from != to) {
214+ size_t n = luaZ_bufflen(ls->buff);
215+ char *p = luaZ_buffer(ls->buff);
216+ while (n--)
217+ if (p[n] == from) p[n] = to;
218+ }
203219 }
204220
205221
@@ -208,17 +224,17 @@ static void buffreplace (LexState *ls, char from, char to) { · 2 unmodified lines
208224 #endif
209225
210226
211−#define buff2d(b,e) luaO_str2d(luaZ_buffer(b), luaZ_bufflen(b) - 1, e)
227+#define buff2num(b,o) (luaO_str2num(luaZ_buffer(b), o) != 0)
212228
213229 /*
214230 ** in case of format error, try to change decimal point separator to
215231 ** the one defined in the current locale and check again
216232 */
217−static void trydecpoint (LexState *ls, SemInfo *seminfo) {
233+static void trydecpoint (LexState *ls, TValue *o) {
218234 char old = ls->decpoint;
219235 ls->decpoint = getlocaledecpoint();
220236 buffreplace(ls, old, ls->decpoint); /* try new decimal separator */
221− if (!buff2d(ls->buff, &seminfo->r)) {
237+ if (!buff2num(ls->buff, o)) {
222238 /* format error with correct decimal point: no more options */
223239 buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */
224240 lexerror(ls, "malformed number", TK_FLT);
@@ -228,41 +244,37 @@ static void trydecpoint (LexState *ls, SemInfo *seminfo) { · 3 unmodified lines
228244
229245 /* LUA_NUMBER */
230246 /*
231−** this function is quite liberal in what it accepts, as 'luaO_str2d'
232−** will reject ill-formed numerals. 'isf' means the numeral is not
233−** an integer (it has a dot or an exponent).
247+** this function is quite liberal in what it accepts, as 'luaO_str2num'
248+** will reject ill-formed numerals.
234249 */
235−static int read_numeral (LexState *ls, SemInfo *seminfo, int isf) {
250+static int read_numeral (LexState *ls, SemInfo *seminfo) {
251+ TValue obj;
236252 const char *expo = "Ee";
237253 int first = ls->current;
238254 lua_assert(lisdigit(ls->current));
239255 save_and_next(ls);
240− if (first == '0' && check_next(ls, "Xx")) /* hexadecimal? */
256+ if (first == '0' && check_next2(ls, "xX")) /* hexadecimal? */
241257 expo = "Pp";
242258 for (;;) {
243− if (check_next(ls, expo)) { /* exponent part? */
244− check_next(ls, "+-"); /* optional exponent sign */
245− isf = 1;
246− }
259+ if (check_next2(ls, expo)) /* exponent part? */
260+ check_next2(ls, "-+"); /* optional exponent sign */
247261 if (lisxdigit(ls->current))
248262 save_and_next(ls);
249− else if (ls->current == '.') {
263+ else if (ls->current == '.')
250264 save_and_next(ls);
251− isf = 1;
252− }
253265 else break;
254266 }
255267 save(ls, '\0');
256− if (!isf) {
257− if (!luaO_str2int(luaZ_buffer(ls->buff), luaZ_bufflen(ls->buff) - 1,
258− &seminfo->i))
259− lexerror(ls, "malformed number", TK_INT);
268+ buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */
269+ if (!buff2num(ls->buff, &obj)) /* format error? */
270+ trydecpoint(ls, &obj); /* try to update decimal point separator */
271+ if (ttisinteger(&obj)) {
272+ seminfo->i = ivalue(&obj);
260273 return TK_INT;
261274 }
262275 else {
263− buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */
264− if (!buff2d(ls->buff, &seminfo->r)) /* format error? */
265− trydecpoint(ls, seminfo); /* try to update decimal point separator */
276+ lua_assert(ttisfloat(&obj));
277+ seminfo->r = fltvalue(&obj);
266278 return TK_FLT;
267279 }
268280 }
@@ -286,15 +298,19 @@ static int skip_sep (LexState *ls) { · 17 unmodified lines
286298
287299
288300 static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) {
301+ int line = ls->linenumber; /* initial line (for error message) */
289302 save_and_next(ls); /* skip 2nd `[' */
290303 if (currIsNewline(ls)) /* string starts with a newline? */
291304 inclinenumber(ls); /* skip it */
292305 for (;;) {
293306 switch (ls->current) {
294− case EOZ:
295− lexerror(ls, (seminfo) ? "unfinished long string" :
296− "unfinished long comment", TK_EOS);
307+ case EOZ: { /* error */
308+ const char *what = (seminfo ? "string" : "comment");
309+ const char *msg = luaO_pushfstring(ls->L,
310+ "unfinished long %s (starting at line %d)", what, line);
311+ lexerror(ls, msg, TK_EOS);
297312 break; /* to avoid warnings */
313+ }
298314 case ']': {
299315 if (skip_sep(ls) == sep) {
300316 save_and_next(ls); /* skip 2nd `]' */
@@ -488,35 +504,35 @@ static int llex (LexState *ls, SemInfo *seminfo) { · 187 unmodified lines
488504 }
489505 case '=': {
490506 next(ls);
491− if (ls->current != '=') return '=';
492− else { next(ls); return TK_EQ; }
507+ if (check_next1(ls, '=')) return TK_EQ;
508+ else return '=';
493509 }
494510 case '<': {
495511 next(ls);
496− if (ls->current == '=') { next(ls); return TK_LE; }
497− if (ls->current == '<') { next(ls); return TK_SHL; }
498− return '<';
512+ if (check_next1(ls, '=')) return TK_LE;
513+ else if (check_next1(ls, '<')) return TK_SHL;
514+ else return '<';
499515 }
500516 case '>': {
501517 next(ls);
502− if (ls->current == '=') { next(ls); return TK_GE; }
503− if (ls->current == '>') { next(ls); return TK_SHR; }
504− return '>';
518+ if (check_next1(ls, '=')) return TK_GE;
519+ else if (check_next1(ls, '>')) return TK_SHR;
520+ else return '>';
505521 }
506522 case '/': {
507523 next(ls);
508− if (ls->current != '/') return '/';
509− else { next(ls); return TK_IDIV; }
524+ if (check_next1(ls, '/')) return TK_IDIV;
525+ else return '/';
510526 }
511527 case '~': {
512528 next(ls);
513− if (ls->current != '=') return '~';
514− else { next(ls); return TK_NE; }
529+ if (check_next1(ls, '=')) return TK_NE;
530+ else return '~';
515531 }
516532 case ':': {
517533 next(ls);
518− if (ls->current != ':') return ':';
519− else { next(ls); return TK_DBCOLON; }
534+ if (check_next1(ls, ':')) return TK_DBCOLON;
535+ else return ':';
520536 }
521537 case '"': case '\'': { /* short literal strings */
522538 read_string(ls, ls->current, seminfo);
@@ -524,17 +540,17 @@ static int llex (LexState *ls, SemInfo *seminfo) { · 1 unmodified line
524540 }
525541 case '.': { /* '.', '..', '...', or number */
526542 save_and_next(ls);
527− if (check_next(ls, ".")) {
528− if (check_next(ls, "."))
543+ if (check_next1(ls, '.')) {
544+ if (check_next1(ls, '.'))
529545 return TK_DOTS; /* '...' */
530546 else return TK_CONCAT; /* '..' */
531547 }
532548 else if (!lisdigit(ls->current)) return '.';
533− else return read_numeral(ls, seminfo, 1);
549+ else return read_numeral(ls, seminfo);
534550 }
535551 case '0': case '1': case '2': case '3': case '4':
536552 case '5': case '6': case '7': case '8': case '9': {
537− return read_numeral(ls, seminfo, 0);
553+ return read_numeral(ls, seminfo);
538554 }
539555 case EOZ: {
540556 return TK_EOS;
src/llimits.h+20−13
@@ -1,5 +1,5 @@
11 /*
2−** $Id: llimits.h,v 1.111 2014/03/07 16:19:00 roberto Exp $
2+** $Id: llimits.h,v 1.116 2014/04/15 16:32:49 roberto Exp $
33 ** Limits, basic types, and some other `installation-dependent' definitions
44 ** See Copyright Notice in lua.h
55 */
@@ -31,8 +31,8 @@ typedef unsigned char lu_byte; · 25 unmodified lines
3131 #define MAX_SIZET ((size_t)(~(size_t)0)-2)
3232
3333 /* maximum size visible for Lua (must be representable in a lua_Integer */
34−#define MAX_SIZE (sizeof(size_t) <= sizeof(lua_Integer) ? MAX_SIZET \
35− : (size_t)(~(lua_Unsigned)0)-2)
34+#define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \
35+ : (size_t)(LUA_MAXINTEGER)-2)
3636
3737
3838 #define MAX_LUMEM ((lu_mem)(~(lu_mem)0)-2)
@@ -43,13 +43,6 @@ typedef unsigned char lu_byte; · 4 unmodified lines
4343 #define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */
4444
4545
46−/* maximum value for a lua_Unsigned */
47−#define MAX_UINTEGER (~(lua_Unsigned)0)
48−
49−/* minimum and maximum values for lua_Integer */
50−#define MAX_INTEGER ((lua_Integer)(MAX_UINTEGER >> 1))
51−#define MIN_INTEGER (~MAX_INTEGER)
52−
5346 /*
5447 ** conversion of pointer to integer
5548 ** this is for hashing only; there is no problem if the integer
@@ -67,8 +60,9 @@ typedef unsigned char lu_byte; · 11 unmodified lines
6760 typedef LUAI_USER_ALIGNMENT_T L_Umaxalign;
6861
6962
70−/* result of a `usual argument conversion' over lua_Number */
63+/* types of 'usual argument conversions' for lua_Number and lua_Integer */
7164 typedef LUAI_UACNUMBER l_uacNumber;
65+typedef LUAI_UACINT l_uacInt;
7266
7367
7468 /* internal assertions for in-house debugging */
@@ -111,8 +105,21 @@ typedef LUAI_UACNUMBER l_uacNumber; · 36 unmodified lines
111105 #define cast_num(i) cast(lua_Number, (i))
112106 #define cast_int(i) cast(int, (i))
113107 #define cast_uchar(i) cast(unsigned char, (i))
114−#define cast_integer(i) cast(lua_Integer, (i))
115−#define cast_unsigned(i) cast(lua_Unsigned, (i))
108+
109+
110+/* cast a signed lua_Integer to lua_Unsigned */
111+#if !defined(l_castS2U)
112+#define l_castS2U(i) ((lua_Unsigned)(i))
113+#endif
114+
115+/*
116+** cast a lua_Unsigned to a signed lua_Integer; this cast is
117+** not strict ANSI C, but two-complement architectures should
118+** work fine.
119+*/
120+#if !defined(l_castU2S)
121+#define l_castU2S(i) ((lua_Integer)(i))
122+#endif
116123
117124
118125 /*
src/lmathlib.c+175−106
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lmathlib.c,v 1.92 2013/07/22 16:05:53 roberto Exp $
2+** $Id: lmathlib.c,v 1.103 2014/06/18 12:35:53 roberto Exp $
33 ** Standard mathematical library
44 ** See Copyright Notice in lua.h
55 */
@@ -18,12 +18,28 @@ · 12 unmodified lines
1818
1919
2020 #undef PI
21−#define PI ((lua_Number)(3.1415926535897932384626433832795))
22−#define RADIANS_PER_DEGREE ((lua_Number)(PI/180.0))
21+#define PI (l_mathop(3.141592653589793238462643383279502884))
22+
23+
24+#if !defined(l_rand) /* { */
25+#if defined(LUA_USE_POSIX)
26+#define l_rand() random()
27+#define l_srand(x) srandom(x)
28+#else
29+#define l_rand() rand()
30+#define l_srand(x) srand(x)
31+#endif
32+#endif /* } */
2333
2434
2535 static int math_abs (lua_State *L) {
26− lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1)));
36+ if (lua_isinteger(L, 1)) {
37+ lua_Integer n = lua_tointeger(L, 1);
38+ if (n < 0) n = (lua_Integer)(0u - n);
39+ lua_pushinteger(L, n);
40+ }
41+ else
42+ lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1)));
2743 return 1;
2844 }
2945
@@ -32,31 +48,16 @@ static int math_sin (lua_State *L) { · 2 unmodified lines
3248 return 1;
3349 }
3450
35−static int math_sinh (lua_State *L) {
36− lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1)));
37− return 1;
38−}
39−
4051 static int math_cos (lua_State *L) {
4152 lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1)));
4253 return 1;
4354 }
4455
45−static int math_cosh (lua_State *L) {
46− lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1)));
47− return 1;
48−}
49−
5056 static int math_tan (lua_State *L) {
5157 lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1)));
5258 return 1;
5359 }
5460
55−static int math_tanh (lua_State *L) {
56− lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1)));
57− return 1;
58−}
59−
6061 static int math_asin (lua_State *L) {
6162 lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1)));
6263 return 1;
@@ -68,56 +69,92 @@ static int math_acos (lua_State *L) { · 5 unmodified lines
6869 }
6970
7071 static int math_atan (lua_State *L) {
71− lua_pushnumber(L, l_mathop(atan)(luaL_checknumber(L, 1)));
72+ lua_Number y = luaL_checknumber(L, 1);
73+ lua_Number x = luaL_optnumber(L, 2, 1);
74+ lua_pushnumber(L, l_mathop(atan2)(y, x));
7275 return 1;
7376 }
7477
75−static int math_atan2 (lua_State *L) {
76− lua_pushnumber(L, l_mathop(atan2)(luaL_checknumber(L, 1),
77− luaL_checknumber(L, 2)));
78− return 1;
79−}
8078
81−static int math_ceil (lua_State *L) {
82− lua_pushnumber(L, l_mathop(ceil)(luaL_checknumber(L, 1)));
79+static int math_ifloor (lua_State *L) {
80+ int valid;
81+ lua_Integer n = lua_tointegerx(L, 1, &valid);
82+ if (valid)
83+ lua_pushinteger(L, n); /* floor computed by Lua */
84+ else {
85+ luaL_checktype(L, 1, LUA_TNUMBER); /* argument must be a number */
86+ lua_pushnil(L); /* number is not convertible to integer */
87+ }
8388 return 1;
8489 }
8590
86−static int math_floor (lua_State *L) {
87− lua_pushnumber(L, l_mathop(floor)(luaL_checknumber(L, 1)));
88− return 1;
89−}
9091
91−static int math_ifloor (lua_State *L) {
92+static int math_floor (lua_State *L) {
9293 int valid;
9394 lua_Integer n = lua_tointegerx(L, 1, &valid);
9495 if (valid)
95− lua_pushinteger(L, n);
96+ lua_pushinteger(L, n); /* floor computed by Lua */
97+ else
98+ lua_pushnumber(L, l_mathop(floor)(luaL_checknumber(L, 1)));
99+ return 1;
100+}
101+
102+
103+static void pushnumint (lua_State *L, lua_Number d) {
104+ lua_Integer n;
105+ if (lua_numtointeger(d, &n)) /* fits in an integer? */
106+ lua_pushinteger(L, n); /* result is integer */
107+ else
108+ lua_pushnumber(L, d); /* result is float */
109+}
110+
111+
112+static int math_ceil (lua_State *L) {
113+ if (lua_isinteger(L, 1))
114+ lua_settop(L, 1); /* integer is its own ceil */
96115 else {
97− luaL_checktype(L, 1, LUA_TNUMBER); /* error if not a number */
98− lua_pushnil(L); /* number with invalid integer value */
116+ lua_Number d = l_mathop(ceil)(luaL_checknumber(L, 1));
117+ pushnumint(L, d);
99118 }
100119 return 1;
101120 }
102121
122+
103123 static int math_fmod (lua_State *L) {
104− lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1),
105− luaL_checknumber(L, 2)));
124+ if (lua_isinteger(L, 1) && lua_isinteger(L, 2)) {
125+ lua_Integer d = lua_tointeger(L, 2);
126+ if ((lua_Unsigned)d + 1u <= 1u) { /* special cases: -1 or 0 */
127+ luaL_argcheck(L, d != 0, 2, "zero");
128+ lua_pushinteger(L, 0); /* avoid overflow with 0x80000... / -1 */
129+ }
130+ else
131+ lua_pushinteger(L, lua_tointeger(L, 1) % d);
132+ }
133+ else
134+ lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1),
135+ luaL_checknumber(L, 2)));
106136 return 1;
107137 }
108138
139+
109140 /*
110141 ** next function does not use 'modf', avoiding problems with 'double*'
111142 ** (which is not compatible with 'float*') when lua_Number is not
112143 ** 'double'.
113144 */
114145 static int math_modf (lua_State *L) {
115− lua_Number n = luaL_checknumber(L, 1);
116− /* integer part (rounds toward zero) */
117− lua_Number ip = (n < 0) ? -l_mathop(floor)(-n) : l_mathop(floor)(n);
118− lua_pushnumber(L, ip);
119− /* fractionary part (test handles inf/-inf) */
120− lua_pushnumber(L, (n == ip) ? 0.0 : (n - ip));
146+ if (lua_isinteger(L ,1)) {
147+ lua_settop(L, 1); /* number is its own integer part */
148+ lua_pushnumber(L, 0); /* no fractionary part */
149+ }
150+ else {
151+ lua_Number n = luaL_checknumber(L, 1);
152+ /* integer part (rounds toward zero) */
153+ lua_Number ip = (n < 0) ? l_mathop(ceil)(n) : l_mathop(floor)(n);
154+ pushnumint(L, ip);
155+ /* fractionary part (test needed for inf/-inf) */
156+ lua_pushnumber(L, (n == ip) ? 0.0 : (n - ip));
157+ }
121158 return 2;
122159 }
123160
@@ -127,13 +164,6 @@ static int math_sqrt (lua_State *L) { · 3 unmodified lines
127164 return 1;
128165 }
129166
130−static int math_pow (lua_State *L) {
131− lua_Number x = luaL_checknumber(L, 1);
132− lua_Number y = luaL_checknumber(L, 2);
133− lua_pushnumber(L, l_mathop(pow)(x, y));
134− return 1;
135−}
136−
137167 static int math_log (lua_State *L) {
138168 lua_Number x = luaL_checknumber(L, 1);
139169 lua_Number res;
@@ -141,84 +171,64 @@ static int math_log (lua_State *L) { · 1 unmodified line
141171 res = l_mathop(log)(x);
142172 else {
143173 lua_Number base = luaL_checknumber(L, 2);
144− if (base == (lua_Number)10.0) res = l_mathop(log10)(x);
174+ if (base == 10.0) res = l_mathop(log10)(x);
145175 else res = l_mathop(log)(x)/l_mathop(log)(base);
146176 }
147177 lua_pushnumber(L, res);
148178 return 1;
149179 }
150180
151−#if defined(LUA_COMPAT_LOG10)
152−static int math_log10 (lua_State *L) {
153− lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1)));
154− return 1;
155−}
156−#endif
157−
158181 static int math_exp (lua_State *L) {
159182 lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1)));
160183 return 1;
161184 }
162185
163186 static int math_deg (lua_State *L) {
164− lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE);
187+ lua_pushnumber(L, luaL_checknumber(L, 1) * (180.0 / PI));
165188 return 1;
166189 }
167190
168191 static int math_rad (lua_State *L) {
169− lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE);
192+ lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / 180.0));
170193 return 1;
171194 }
172195
173−static int math_frexp (lua_State *L) {
174− int e;
175− lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e));
176− lua_pushinteger(L, e);
177− return 2;
178−}
179−
180−static int math_ldexp (lua_State *L) {
181− lua_Number x = luaL_checknumber(L, 1);
182− int ep = luaL_checkint(L, 2);
183− lua_pushnumber(L, l_mathop(ldexp)(x, ep));
184− return 1;
185−}
186−
187−
188196
189197 static int math_min (lua_State *L) {
190198 int n = lua_gettop(L); /* number of arguments */
191− lua_Number dmin = luaL_checknumber(L, 1);
199+ int imin = 1; /* index of current minimum value */
192200 int i;
193− for (i=2; i<=n; i++) {
194− lua_Number d = luaL_checknumber(L, i);
195− if (d < dmin)
196− dmin = d;
201+ luaL_argcheck(L, n >= 1, 1, "value expected");
202+ for (i = 2; i <= n; i++) {
203+ if (lua_compare(L, i, imin, LUA_OPLT))
204+ imin = i;
197205 }
198− lua_pushnumber(L, dmin);
206+ lua_pushvalue(L, imin);
199207 return 1;
200208 }
201209
202210
203211 static int math_max (lua_State *L) {
204212 int n = lua_gettop(L); /* number of arguments */
205− lua_Number dmax = luaL_checknumber(L, 1);
213+ int imax = 1; /* index of current maximum value */
206214 int i;
207− for (i=2; i<=n; i++) {
208− lua_Number d = luaL_checknumber(L, i);
209− if (d > dmax)
210− dmax = d;
215+ luaL_argcheck(L, n >= 1, 1, "value expected");
216+ for (i = 2; i <= n; i++) {
217+ if (lua_compare(L, imax, i, LUA_OPLT))
218+ imax = i;
211219 }
212− lua_pushnumber(L, dmax);
220+ lua_pushvalue(L, imax);
213221 return 1;
214222 }
215223
216−
224+/*
225+** This function uses 'double' (instead of 'lua_Number') to ensure that
226+** all bits from 'l_rand' can be represented, and that 'RAND_MAX + 1.0'
227+** will keep full precision (ensuring that 'r' is always less than 1.0.)
228+*/
217229 static int math_random (lua_State *L) {
218− /* the `%' avoids the (rare) case of r==1, and is needed also because on
219− some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
220− lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;
221230 lua_Integer low, up;
231+ double r = (double)l_rand() * (1.0 / ((double)RAND_MAX + 1.0));
222232 switch (lua_gettop(L)) { /* check number of arguments */
223233 case 0: { /* no arguments */
224234 lua_pushnumber(L, r); /* Number between 0 and 1 */
@@ -237,15 +247,17 @@ static int math_random (lua_State *L) { · 12 unmodified lines
237247 default: return luaL_error(L, "wrong number of arguments");
238248 }
239249 /* random integer in the interval [low, up] */
240− up++; /* change interval to [low, up) */
241− luaL_argcheck(L, up - low > 0, 1, "interval is empty");
242− lua_pushinteger(L, (lua_Integer)(r * (lua_Number)(up - low)) + low);
250+ luaL_argcheck(L, low <= up, 1, "interval is empty");
251+ luaL_argcheck(L, (lua_Unsigned)up - low <= (lua_Unsigned)LUA_MAXINTEGER,
252+ 1, "interval too large");
253+ r *= (double)(up - low) + 1.0;
254+ lua_pushinteger(L, (lua_Integer)r + low);
243255 return 1;
244256 }
245257
246258
247259 static int math_randomseed (lua_State *L) {
248− srand(luaL_checkunsigned(L, 1));
260+ l_srand((unsigned int)luaL_checkunsigned(L, 1));
249261 (void)rand(); /* discard first value to avoid undesirable correlations */
250262 return 0;
251263 }
@@ -265,39 +277,92 @@ static int math_type (lua_State *L) { · 13 unmodified lines
265277 }
266278
267279
280+/*
281+** {==================================================================
282+** Deprecated functions (for compatibility only)
283+** ===================================================================
284+*/
285+#if defined(LUA_COMPAT_MATHLIB)
286+
287+static int math_cosh (lua_State *L) {
288+ lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1)));
289+ return 1;
290+}
291+
292+static int math_sinh (lua_State *L) {
293+ lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1)));
294+ return 1;
295+}
296+
297+static int math_tanh (lua_State *L) {
298+ lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1)));
299+ return 1;
300+}
301+
302+static int math_pow (lua_State *L) {
303+ lua_Number x = luaL_checknumber(L, 1);
304+ lua_Number y = luaL_checknumber(L, 2);
305+ lua_pushnumber(L, l_mathop(pow)(x, y));
306+ return 1;
307+}
308+
309+static int math_frexp (lua_State *L) {
310+ int e;
311+ lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e));
312+ lua_pushinteger(L, e);
313+ return 2;
314+}
315+
316+static int math_ldexp (lua_State *L) {
317+ lua_Number x = luaL_checknumber(L, 1);
318+ int ep = luaL_checkint(L, 2);
319+ lua_pushnumber(L, l_mathop(ldexp)(x, ep));
320+ return 1;
321+}
322+
323+static int math_log10 (lua_State *L) {
324+ lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1)));
325+ return 1;
326+}
327+
328+#endif
329+/* }================================================================== */
330+
331+
332+
268333 static const luaL_Reg mathlib[] = {
269334 {"abs", math_abs},
270335 {"acos", math_acos},
271336 {"asin", math_asin},
272− {"atan2", math_atan2},
273337 {"atan", math_atan},
274338 {"ceil", math_ceil},
275− {"cosh", math_cosh},
276339 {"cos", math_cos},
277340 {"deg", math_deg},
278341 {"exp", math_exp},
279− {"floor", math_floor},
280342 {"ifloor", math_ifloor},
343+ {"floor", math_floor},
281344 {"fmod", math_fmod},
282− {"frexp", math_frexp},
283− {"ldexp", math_ldexp},
284−#if defined(LUA_COMPAT_LOG10)
285− {"log10", math_log10},
286−#endif
287345 {"log", math_log},
288346 {"max", math_max},
289347 {"min", math_min},
290348 {"modf", math_modf},
291− {"pow", math_pow},
292349 {"rad", math_rad},
293350 {"random", math_random},
294351 {"randomseed", math_randomseed},
295− {"sinh", math_sinh},
296352 {"sin", math_sin},
297353 {"sqrt", math_sqrt},
298− {"tanh", math_tanh},
299354 {"tan", math_tan},
300355 {"type", math_type},
356+#if defined(LUA_COMPAT_MATHLIB)
357+ {"atan2", math_atan},
358+ {"cosh", math_cosh},
359+ {"sinh", math_sinh},
360+ {"tanh", math_tanh},
361+ {"pow", math_pow},
362+ {"frexp", math_frexp},
363+ {"ldexp", math_ldexp},
364+ {"log10", math_log10},
365+#endif
301366 {NULL, NULL}
302367 };
303368
@@ -311,6 +376,10 @@ LUAMOD_API int luaopen_math (lua_State *L) { · 7 unmodified lines
311376 lua_setfield(L, -2, "pi");
312377 lua_pushnumber(L, HUGE_VAL);
313378 lua_setfield(L, -2, "huge");
379+ lua_pushinteger(L, LUA_MAXINTEGER);
380+ lua_setfield(L, -2, "maxinteger");
381+ lua_pushinteger(L, LUA_MININTEGER);
382+ lua_setfield(L, -2, "mininteger");
314383 return 1;
315384 }
316385
src/lobject.c+37−17
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lobject.c,v 2.76 2014/03/21 13:52:33 roberto Exp $
2+** $Id: lobject.c,v 2.86 2014/05/12 21:44:17 roberto Exp $
33 ** Some generic functions over Lua objects
44 ** See Copyright Notice in lua.h
55 */
@@ -77,7 +77,6 @@ static lua_Integer intarith (lua_State *L, int op, lua_Integer v1, · 71 unmodified lines
7777 case LUA_OPSUB:return intop(-, v1, v2);
7878 case LUA_OPMUL:return intop(*, v1, v2);
7979 case LUA_OPMOD: return luaV_mod(L, v1, v2);
80− case LUA_OPPOW: return luaV_pow(L, v1, v2);
8180 case LUA_OPIDIV: return luaV_div(L, v1, v2);
8281 case LUA_OPBAND: return intop(&, v1, v2);
8382 case LUA_OPBOR: return intop(|, v1, v2);
@@ -85,7 +84,7 @@ static lua_Integer intarith (lua_State *L, int op, lua_Integer v1, · 1 unmodified line
8584 case LUA_OPSHL: return luaV_shiftl(v1, v2);
8685 case LUA_OPSHR: return luaV_shiftl(v1, -v2);
8786 case LUA_OPUNM: return intop(-, 0, v1);
88− case LUA_OPBNOT: return intop(^, cast_integer(-1), v1);
87+ case LUA_OPBNOT: return intop(^, ~l_castS2U(0), v1);
8988 default: lua_assert(0); return 0;
9089 }
9190 }
@@ -98,9 +97,13 @@ static lua_Number numarith (lua_State *L, int op, lua_Number v1, · 6 unmodified lines
9897 case LUA_OPSUB: return luai_numsub(L, v1, v2);
9998 case LUA_OPMUL: return luai_nummul(L, v1, v2);
10099 case LUA_OPDIV: return luai_numdiv(L, v1, v2);
101− case LUA_OPMOD: return luai_nummod(L, v1, v2);
102100 case LUA_OPPOW: return luai_numpow(L, v1, v2);
103101 case LUA_OPUNM: return luai_numunm(L, v1);
102+ case LUA_OPMOD: {
103+ lua_Number m;
104+ luai_nummod(L, v1, v2, m);
105+ return m;
106+ }
104107 default: lua_assert(0); return 0;
105108 }
106109 }
@@ -119,10 +122,10 @@ void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, · 12 unmodified lines
119122 }
120123 else break; /* go to the end */
121124 }
122− case LUA_OPDIV: { /* operates only on floats */
125+ case LUA_OPDIV: case LUA_OPPOW: { /* operate only on floats */
123126 lua_Number n1; lua_Number n2;
124127 if (tonumber(p1, &n1) && tonumber(p2, &n2)) {
125− setnvalue(res, numarith(L, op, n1, n2));
128+ setfltvalue(res, numarith(L, op, n1, n2));
126129 return;
127130 }
128131 else break; /* go to the end */
@@ -134,7 +137,7 @@ void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, · 5 unmodified lines
134137 return;
135138 }
136139 else if (tonumber(p1, &n1) && tonumber(p2, &n2)) {
137− setnvalue(res, numarith(L, op, n1, n2));
140+ setfltvalue(res, numarith(L, op, n1, n2));
138141 return;
139142 }
140143 else break; /* go to the end */
@@ -249,22 +252,21 @@ static lua_Number lua_strx2number (const char *s, char **endptr) { · 108 unmodified lines
249252 /* }====================================================== */
250253
251254
252−int luaO_str2d (const char *s, size_t len, lua_Number *result) {
255+static const char *l_str2d (const char *s, lua_Number *result) {
253256 char *endptr;
254257 if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */
255− return 0;
258+ return NULL;
256259 else if (strpbrk(s, "xX")) /* hexa? */
257260 *result = lua_strx2number(s, &endptr);
258261 else
259262 *result = lua_str2number(s, &endptr);
260263 if (endptr == s) return 0; /* nothing recognized */
261264 while (lisspace(cast_uchar(*endptr))) endptr++;
262− return (endptr == s + len); /* OK if no trailing characters */
265+ return (*endptr == '\0' ? endptr : NULL); /* OK if no trailing characters */
263266 }
264267
265268
266−int luaO_str2int (const char *s, size_t len, lua_Integer *result) {
267− const char *ends = s + len;
269+static const char *l_str2int (const char *s, lua_Integer *result) {
268270 lua_Unsigned a = 0;
269271 int empty = 1;
270272 int neg;
@@ -285,14 +287,29 @@ int luaO_str2int (const char *s, size_t len, lua_Integer *result) { · 14 unmodified lines
285287 }
286288 }
287289 while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */
288− if (empty || s != ends) return 0; /* something wrong in the numeral */
290+ if (empty || *s != '\0') return NULL; /* something wrong in the numeral */
289291 else {
290− *result = cast_integer((neg) ? 0u - a : a);
291− return 1;
292+ *result = l_castU2S((neg) ? 0u - a : a);
293+ return s;
292294 }
293295 }
294296
295297
298+size_t luaO_str2num (const char *s, TValue *o) {
299+ lua_Integer i; lua_Number n;
300+ const char *e;
301+ if ((e = l_str2int(s, &i)) != NULL) { /* try as an integer */
302+ setivalue(o, i);
303+ }
304+ else if ((e = l_str2d(s, &n)) != NULL) { /* else try as a float */
305+ setfltvalue(o, n);
306+ }
307+ else
308+ return 0; /* conversion failed */
309+ return (e - s + 1); /* success; return string size */
310+}
311+
312+
296313 int luaO_utf8esc (char *buff, unsigned int x) {
297314 int n = 1; /* number of bytes put in buffer (backwards) */
298315 if (x < 0x80) /* ascii? */
@@ -339,14 +356,17 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { · 40 unmodified lines
339356 }
340357 case 'd': {
341358 setivalue(L->top++, cast_int(va_arg(argp, int)));
359+ luaV_tostring(L, L->top - 1);
342360 break;
343361 }
344362 case 'I': {
345− setivalue(L->top++, cast_integer(va_arg(argp, lua_Integer)));
363+ setivalue(L->top++, cast(lua_Integer, va_arg(argp, l_uacInt)));
364+ luaV_tostring(L, L->top - 1);
346365 break;
347366 }
348367 case 'f': {
349− setnvalue(L->top++, cast_num(va_arg(argp, l_uacNumber)));
368+ setfltvalue(L->top++, cast_num(va_arg(argp, l_uacNumber)));
369+ luaV_tostring(L, L->top - 1);
350370 break;
351371 }
352372 case 'p': {
src/lobject.h+39−45
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lobject.h,v 2.86 2014/02/19 13:52:42 roberto Exp $
2+** $Id: lobject.h,v 2.94 2014/06/19 18:39:36 roberto Exp $
33 ** Type definitions for Lua objects
44 ** See Copyright Notice in lua.h
55 */
@@ -156,17 +156,17 @@ typedef struct lua_TValue TValue; · 150 unmodified lines
156156 #define fltvalue(o) check_exp(ttisfloat(o), val_(o).n)
157157 #define gcvalue(o) check_exp(iscollectable(o), val_(o).gc)
158158 #define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
159−#define rawtsvalue(o) check_exp(ttisstring(o), &val_(o).gc->ts)
159+#define rawtsvalue(o) check_exp(ttisstring(o), rawgco2ts(val_(o).gc))
160160 #define tsvalue(o) (&rawtsvalue(o)->tsv)
161−#define rawuvalue(o) check_exp(ttisfulluserdata(o), &val_(o).gc->u)
161+#define rawuvalue(o) check_exp(ttisfulluserdata(o), rawgco2u(val_(o).gc))
162162 #define uvalue(o) (&rawuvalue(o)->uv)
163−#define clvalue(o) check_exp(ttisclosure(o), &val_(o).gc->cl)
164−#define clLvalue(o) check_exp(ttisLclosure(o), &val_(o).gc->cl.l)
165−#define clCvalue(o) check_exp(ttisCclosure(o), &val_(o).gc->cl.c)
163+#define clvalue(o) check_exp(ttisclosure(o), gco2cl(val_(o).gc))
164+#define clLvalue(o) check_exp(ttisLclosure(o), gco2lcl(val_(o).gc))
165+#define clCvalue(o) check_exp(ttisCclosure(o), gco2ccl(val_(o).gc))
166166 #define fvalue(o) check_exp(ttislcf(o), val_(o).f)
167−#define hvalue(o) check_exp(ttistable(o), &val_(o).gc->h)
167+#define hvalue(o) check_exp(ttistable(o), gco2t(val_(o).gc))
168168 #define bvalue(o) check_exp(ttisboolean(o), val_(o).b)
169−#define thvalue(o) check_exp(ttisthread(o), &val_(o).gc->th)
169+#define thvalue(o) check_exp(ttisthread(o), gco2th(val_(o).gc))
170170 /* a dead value may get the 'gc' field, but cannot access its contents */
171171 #define deadvalue(o) check_exp(ttisdeadkey(o), cast(void *, val_(o).gc))
172172
@@ -187,7 +187,7 @@ typedef struct lua_TValue TValue; · 14 unmodified lines
187187 /* Macros to set values */
188188 #define settt_(o,t) ((o)->tt_=(t))
189189
190−#define setnvalue(obj,x) \
190+#define setfltvalue(obj,x) \
191191 { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_TNUMFLT); }
192192
193193 #define setivalue(obj,x) \
@@ -205,38 +205,37 @@ typedef struct lua_TValue TValue; · 11 unmodified lines
205205 { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); }
206206
207207 #define setgcovalue(L,obj,x) \
208− { TValue *io=(obj); GCObject *i_g=(x); \
209− val_(io).gc=i_g; settt_(io, ctb(gch(i_g)->tt)); }
208+ { TValue *io = (obj); GCObject *i_g=(x); \
209+ val_(io).gc = i_g; settt_(io, ctb(gch(i_g)->tt)); }
210210
211211 #define setsvalue(L,obj,x) \
212− { TValue *io=(obj); \
213− TString *x_ = (x); \
214− val_(io).gc=cast(GCObject *, x_); settt_(io, ctb(x_->tsv.tt)); \
212+ { TValue *io = (obj); TString *x_ = (x); \
213+ val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tsv.tt)); \
215214 checkliveness(G(L),io); }
216215
217216 #define setuvalue(L,obj,x) \
218− { TValue *io=(obj); \
219− val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TUSERDATA)); \
217+ { TValue *io = (obj); Udata *x_ = (x); \
218+ val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TUSERDATA)); \
220219 checkliveness(G(L),io); }
221220
222221 #define setthvalue(L,obj,x) \
223− { TValue *io=(obj); \
224− val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTHREAD)); \
222+ { TValue *io = (obj); lua_State *x_ = (x); \
223+ val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTHREAD)); \
225224 checkliveness(G(L),io); }
226225
227226 #define setclLvalue(L,obj,x) \
228− { TValue *io=(obj); \
229− val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TLCL)); \
227+ { TValue *io = (obj); LClosure *x_ = (x); \
228+ val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TLCL)); \
230229 checkliveness(G(L),io); }
231230
232231 #define setclCvalue(L,obj,x) \
233− { TValue *io=(obj); \
234− val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TCCL)); \
232+ { TValue *io = (obj); CClosure *x_ = (x); \
233+ val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TCCL)); \
235234 checkliveness(G(L),io); }
236235
237236 #define sethvalue(L,obj,x) \
238− { TValue *io=(obj); \
239− val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTABLE)); \
237+ { TValue *io = (obj); Table *x_ = (x); \
238+ val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTABLE)); \
240239 checkliveness(G(L),io); }
241240
242241 #define setdeadvalue(obj) settt_(obj, LUA_TDEADKEY)
@@ -246,7 +245,7 @@ typedef struct lua_TValue TValue; · 3 unmodified lines
246245 #define setobj(L,obj1,obj2) \
247246 { const TValue *io2=(obj2); TValue *io1=(obj1); \
248247 io1->value_ = io2->value_; io1->tt_ = io2->tt_; \
249− checkliveness(G(L),io1); }
248+ (void)L; checkliveness(G(L),io1); }
250249
251250
252251 /*
@@ -269,10 +268,6 @@ typedef struct lua_TValue TValue; · 16 unmodified lines
269268 #define setsvalue2n setsvalue
270269
271270
272−/* check whether a number is valid (useful only for NaN trick) */
273−#define luai_checknum(L,o,c) { /* empty */ }
274−
275−
276271
277272
278273 /*
@@ -310,9 +305,9 @@ typedef union TString { · 31 unmodified lines
310305 struct {
311306 CommonHeader;
312307 lu_byte extra; /* reserved words for short strings; "has hash" for longs */
308+ unsigned int hash;
313309 size_t len; /* number of characters in string */
314310 union TString *hnext; /* linked list for hash table */
315− unsigned int hash;
316311 } tsv;
317312 } TString;
318313
@@ -377,14 +372,9 @@ typedef struct LocVar { · 58 unmodified lines
377372 */
378373 typedef struct Proto {
379374 CommonHeader;
380− TValue *k; /* constants used by the function */
381− Instruction *code;
382− struct Proto **p; /* functions defined inside the function */
383− int *lineinfo; /* map from opcodes to source lines (debug information) */
384− LocVar *locvars; /* information about local variables (debug information) */
385− Upvaldesc *upvalues; /* upvalue information */
386− union Closure *cache; /* last created closure with this prototype */
387− TString *source; /* used for debug information */
375+ lu_byte numparams; /* number of fixed parameters */
376+ lu_byte is_vararg;
377+ lu_byte maxstacksize; /* maximum stack used by this function */
388378 int sizeupvalues; /* size of 'upvalues' */
389379 int sizek; /* size of `k' */
390380 int sizecode;
@@ -393,10 +383,15 @@ typedef struct Proto { · 2 unmodified lines
393383 int sizelocvars;
394384 int linedefined;
395385 int lastlinedefined;
386+ TValue *k; /* constants used by the function */
387+ Instruction *code;
388+ struct Proto **p; /* functions defined inside the function */
389+ int *lineinfo; /* map from opcodes to source lines (debug information) */
390+ LocVar *locvars; /* information about local variables (debug information) */
391+ Upvaldesc *upvalues; /* upvalue information */
392+ struct LClosure *cache; /* last created closure with this prototype */
393+ TString *source; /* used for debug information */
396394 GCObject *gclist;
397− lu_byte numparams; /* number of fixed parameters */
398− lu_byte is_vararg;
399− lu_byte maxstacksize; /* maximum stack used by this function */
400395 } Proto;
401396
402397
@@ -462,12 +457,12 @@ typedef struct Table { · 59 unmodified lines
462457 CommonHeader;
463458 lu_byte flags; /* 1<<p means tagmethod(p) is not present */
464459 lu_byte lsizenode; /* log2 of size of `node' array */
465− struct Table *metatable;
460+ int sizearray; /* size of `array' array */
466461 TValue *array; /* array part */
467462 Node *node;
468463 Node *lastfree; /* any free position is before this position */
464+ struct Table *metatable;
469465 GCObject *gclist;
470− int sizearray; /* size of `array' array */
471466 } Table;
472467
473468
@@ -500,8 +495,7 @@ LUAI_FUNC int luaO_utf8esc (char *buff, unsigned int x); · 26 unmodified lines
500495 LUAI_FUNC int luaO_ceillog2 (unsigned int x);
501496 LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1,
502497 const TValue *p2, TValue *res);
503−LUAI_FUNC int luaO_str2d (const char *s, size_t len, lua_Number *result);
504−LUAI_FUNC int luaO_str2int (const char *s, size_t len, lua_Integer *result);
498+LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o);
505499 LUAI_FUNC int luaO_hexavalue (int c);
506500 LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
507501 va_list argp);
src/loslib.c+8−2
@@ -1,5 +1,5 @@
11 /*
2−** $Id: loslib.c,v 1.45 2014/03/20 19:18:54 roberto Exp $
2+** $Id: loslib.c,v 1.46 2014/04/29 17:05:13 roberto Exp $
33 ** Standard Operating System library
44 ** See Copyright Notice in lua.h
55 */
@@ -59,9 +59,15 @@ · 53 unmodified lines
5959 #if defined(LUA_USE_POSIX) /* { */
6060
6161 #include <unistd.h>
62+
6263 #define LUA_TMPNAMBUFSIZE 32
64+
65+#if !defined(LUA_TMPNAMTEMPLATE)
66+#define LUA_TMPNAMTEMPLATE "/tmp/lua_XXXXXX"
67+#endif
68+
6369 #define lua_tmpnam(b,e) { \
64− strcpy(b, "/tmp/lua_XXXXXX"); \
70+ strcpy(b, LUA_TMPNAMTEMPLATE); \
6571 e = mkstemp(b); \
6672 if (e != -1) close(e); \
6773 e = (e == -1); }
src/lparser.c+5−5
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lparser.c,v 2.138 2013/12/30 20:47:58 roberto Exp $
2+** $Id: lparser.c,v 2.139 2014/06/19 18:27:20 roberto Exp $
33 ** Lua Parser
44 ** See Copyright Notice in lua.h
55 */
@@ -1618,17 +1618,17 @@ static void mainfunc (LexState *ls, FuncState *fs) { · 1612 unmodified lines
16181618 }
16191619
16201620
1621−Closure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
1622− Dyndata *dyd, const char *name, int firstchar) {
1621+LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
1622+ Dyndata *dyd, const char *name, int firstchar) {
16231623 LexState lexstate;
16241624 FuncState funcstate;
1625− Closure *cl = luaF_newLclosure(L, 1); /* create main closure */
1625+ LClosure *cl = luaF_newLclosure(L, 1); /* create main closure */
16261626 setclLvalue(L, L->top, cl); /* anchor it (to avoid being collected) */
16271627 incr_top(L);
16281628 lexstate.h = luaH_new(L); /* create table for scanner */
16291629 sethvalue(L, L->top, lexstate.h); /* anchor it */
16301630 incr_top(L);
1631− funcstate.f = cl->l.p = luaF_newproto(L);
1631+ funcstate.f = cl->p = luaF_newproto(L);
16321632 funcstate.f->source = luaS_new(L, name); /* create and anchor TString */
16331633 luaC_objbarrier(L, funcstate.f, funcstate.f->source);
16341634 lexstate.buff = buff;
src/lparser.h+3−3
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lparser.h,v 1.72 2013/08/30 16:01:37 roberto Exp $
2+** $Id: lparser.h,v 1.73 2014/06/19 18:27:20 roberto Exp $
33 ** Lua Parser
44 ** See Copyright Notice in lua.h
55 */
@@ -113,8 +113,8 @@ typedef struct FuncState { · 107 unmodified lines
113113 } FuncState;
114114
115115
116−LUAI_FUNC Closure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
117− Dyndata *dyd, const char *name, int firstchar);
116+LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
117+ Dyndata *dyd, const char *name, int firstchar);
118118
119119
120120 #endif
src/lstate.h+14−14
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lstate.h,v 2.102 2014/02/18 13:46:26 roberto Exp $
2+** $Id: lstate.h,v 2.107 2014/06/12 19:07:30 roberto Exp $
33 ** Global State
44 ** See Copyright Notice in lua.h
55 */
@@ -60,20 +60,18 @@ typedef struct CallInfo { · 54 unmodified lines
6060 StkId func; /* function index in the stack */
6161 StkId top; /* top for this function */
6262 struct CallInfo *previous, *next; /* dynamic call link */
63+ ptrdiff_t extra;
6364 short nresults; /* expected number of results from this function */
6465 lu_byte callstatus;
65− ptrdiff_t extra;
6666 union {
6767 struct { /* only for Lua functions */
6868 StkId base; /* base for this function */
6969 const Instruction *savedpc;
7070 } l;
7171 struct { /* only for C functions */
72− int ctx; /* context info. in case of yields */
73− lua_CFunction k; /* continuation in case of yields */
72+ lua_KFunction k; /* continuation in case of yields */
7473 ptrdiff_t old_errfunc;
75− lu_byte old_allowhook;
76− lu_byte status;
74+ int ctx; /* context info. in case of yields */
7775 } c;
7876 } u;
7977 } CallInfo;
@@ -82,19 +80,21 @@ typedef struct CallInfo { · 2 unmodified lines
8280 /*
8381 ** Bits in CallInfo status
8482 */
85−#define CIST_LUA (1<<0) /* call is running a Lua function */
86−#define CIST_HOOKED (1<<1) /* call is running a debug hook */
87−#define CIST_REENTRY (1<<2) /* call is running on same invocation of
83+#define CIST_OAH (1<<0) /* original value of 'allowhook' */
84+#define CIST_LUA (1<<1) /* call is running a Lua function */
85+#define CIST_HOOKED (1<<2) /* call is running a debug hook */
86+#define CIST_REENTRY (1<<3) /* call is running on same invocation of
8887 luaV_execute of previous call */
89−#define CIST_YIELDED (1<<3) /* call reentered after suspension */
9088 #define CIST_YPCALL (1<<4) /* call is a yieldable protected call */
91−#define CIST_STAT (1<<5) /* call has an error status (pcall) */
92−#define CIST_TAIL (1<<6) /* call was tail called */
93−#define CIST_HOOKYIELD (1<<7) /* last hook called yielded */
94−
89+#define CIST_TAIL (1<<5) /* call was tail called */
90+#define CIST_HOOKYIELD (1<<6) /* last hook called yielded */
9591
9692 #define isLua(ci) ((ci)->callstatus & CIST_LUA)
9793
94+/* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */
95+#define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v))
96+#define getoah(st) ((st) & CIST_OAH)
97+
9898
9999 /*
100100 ** `global state', shared by all threads of this state
src/lstring.c+8−4
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lstring.c,v 2.38 2014/03/19 18:51:42 roberto Exp $
2+** $Id: lstring.c,v 2.40 2014/06/18 22:59:29 roberto Exp $
33 ** String table (keeps all strings handled by Lua)
44 ** See Copyright Notice in lua.h
55 */
@@ -90,9 +90,11 @@ void luaS_resize (lua_State *L, int newsize) { · 84 unmodified lines
9090 static TString *createstrobj (lua_State *L, const char *str, size_t l,
9191 int tag, unsigned int h) {
9292 TString *ts;
93+ GCObject *o;
9394 size_t totalsize; /* total size of TString object */
9495 totalsize = sizeof(TString) + ((l + 1) * sizeof(char));
95− ts = &luaC_newobj(L, tag, totalsize)->ts;
96+ o = luaC_newobj(L, tag, totalsize);
97+ ts = rawgco2ts(o);
9698 ts->tsv.len = l;
9799 ts->tsv.hash = h;
98100 ts->tsv.extra = 0;
@@ -102,7 +104,7 @@ static TString *createstrobj (lua_State *L, const char *str, size_t l, · 3 unmodified lines
102104 }
103105
104106
105−LUAI_FUNC void luaS_remove (lua_State *L, TString *ts) {
107+void luaS_remove (lua_State *L, TString *ts) {
106108 stringtable *tb = &G(L)->strt;
107109 TString **p = &tb->hash[lmod(ts->tsv.hash, tb->size)];
108110 while (*p != ts) /* find previous element */
@@ -165,9 +167,11 @@ TString *luaS_new (lua_State *L, const char *str) { · 56 unmodified lines
165167
166168 Udata *luaS_newudata (lua_State *L, size_t s) {
167169 Udata *u;
170+ GCObject *o;
168171 if (s > MAX_SIZE - sizeof(Udata))
169172 luaM_toobig(L);
170− u = &luaC_newobj(L, LUA_TUSERDATA, sizeof(Udata) + s)->u;
173+ o = luaC_newobj(L, LUA_TUSERDATA, sizeof(Udata) + s);
174+ u = rawgco2u(o);
171175 u->uv.len = s;
172176 u->uv.metatable = NULL;
173177 setuservalue(L, u, luaO_nilobject);
src/lstrlib.c+54−47
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lstrlib.c,v 1.189 2014/03/21 14:26:44 roberto Exp $
2+** $Id: lstrlib.c,v 1.198 2014/04/27 14:42:26 roberto Exp $
33 ** Standard library for string operations and pattern-matching
44 ** See Copyright Notice in lua.h
55 */
@@ -104,8 +104,8 @@ static int str_upper (lua_State *L) { · 98 unmodified lines
104104
105105
106106 /* reasonable limit to avoid arithmetic overflow and strings too big */
107−#if INT_MAX / 2 <= 0x10000000
108−#define MAXSIZE ((size_t)(INT_MAX / 2))
107+#if LUA_MAXINTEGER / 2 <= 0x10000000
108+#define MAXSIZE ((size_t)(LUA_MAXINTEGER / 2))
109109 #else
110110 #define MAXSIZE ((size_t)0x10000000)
111111 #endif
@@ -946,22 +946,22 @@ static int str_format (lua_State *L) { · 834 unmodified lines
946946 ** =======================================================
947947 */
948948
949−/* maximum size for the binary representation of an integer */
950−#define MAXINTSIZE 8
951−
952949
953950 /* number of bits in a character */
954951 #define NB CHAR_BIT
955952
956−/* mask for one character (NB ones) */
957−#define MC (((lua_Integer)1 << NB) - 1)
958−
959−/* mask for one character without sign ((NB - 1) ones) */
960−#define SM (((lua_Integer)1 << (NB - 1)) - 1)
953+/* mask for one character (NB 1's) */
954+#define MC ((1 << NB) - 1)
961955
956+/* mask for one character without sign bit ((NB - 1) 1's) */
957+#define SM (MC >> 1)
962958
959+/* size of a lua_Integer */
963960 #define SZINT ((int)sizeof(lua_Integer))
964961
962+/* maximum size for the binary representation of an integer */
963+#define MAXINTSIZE 12
964+
965965
966966 static union {
967967 int dummy;
@@ -975,7 +975,7 @@ static int getendian (lua_State *L, int arg) { · 7 unmodified lines
975975 if (*endian == 'n') /* native? */
976976 return nativeendian.little;
977977 luaL_argcheck(L, *endian == 'l' || *endian == 'b', arg,
978− "endianess must be 'l'/'b'/'n'");
978+ "endianness must be 'l'/'b'/'n'");
979979 return (*endian == 'l');
980980 }
981981
@@ -989,34 +989,43 @@ static int getintsize (lua_State *L, int arg) { · 7 unmodified lines
989989 }
990990
991991
992−static int packint (char *buff, lua_Integer n, int littleendian, int size) {
992+/* mask for all ones in last byte in a lua Integer */
993+#define HIGHERBYTE ((lua_Unsigned)MC << (NB * (SZINT - 1)))
994+
995+
996+static int dumpint (char *buff, lua_Integer m, int littleendian, int size) {
993997 int i;
998+ lua_Unsigned n = (lua_Unsigned)m;
999+ lua_Unsigned mask = (m >= 0) ? 0 : HIGHERBYTE; /* sign extension */
9941000 if (littleendian) {
9951001 for (i = 0; i < size - 1; i++) {
9961002 buff[i] = (n & MC);
997− n >>= NB;
1003+ n = (n >> NB) | mask;
9981004 }
9991005 }
10001006 else {
10011007 for (i = size - 1; i > 0; i--) {
10021008 buff[i] = (n & MC);
1003− n >>= NB;
1009+ n = (n >> NB) | mask;
10041010 }
10051011 }
10061012 buff[i] = (n & MC); /* last byte */
1007− /* test for overflow: OK if there are only zeros left in higher bytes,
1008− or if there are only ones left and packed number is negative (signal
1009− bit, the higher bit in last byte, is one) */
1010− return ((n & ~MC) == 0 || (n | SM) == ~(lua_Integer)0);
1013+ if (size < SZINT) { /* need test for overflow? */
1014+ /* OK if there are only zeros left in higher bytes,
1015+ or only ones left (excluding non-signal bits in last byte) */
1016+ return ((n & ~(lua_Unsigned)MC) == 0 ||
1017+ (n | SM) == ~(lua_Unsigned)0);
1018+ }
1019+ else return 1; /* no overflow can occur with full size */
10111020 }
10121021
10131022
1014−static int packint_l (lua_State *L) {
1023+static int dumpint_l (lua_State *L) {
10151024 char buff[MAXINTSIZE];
10161025 lua_Integer n = luaL_checkinteger(L, 1);
10171026 int size = getintsize(L, 2);
10181027 int endian = getendian(L, 3);
1019− if (packint(buff, n, endian, size))
1028+ if (dumpint(buff, n, endian, size))
10201029 lua_pushlstring(L, buff, size);
10211030 else
10221031 luaL_error(L, "integer does not fit into given size (%d)", size);
@@ -1024,15 +1033,13 @@ static int packint_l (lua_State *L) { · 1 unmodified line
10241033 }
10251034
10261035
1027−/* mask to check higher-order byte in a Lua integer */
1028−#define HIGHERBYTE (MC << (NB * (SZINT - 1)))
1029−
10301036 /* mask to check higher-order byte + signal bit of next (lower) byte */
1031−#define HIGHERBYTE1 (HIGHERBYTE | (HIGHERBYTE >> 1))
1037+#define HIGHERBYTE1 (HIGHERBYTE | (HIGHERBYTE >> 1))
10321038
1033−static int unpackint (const char *buff, lua_Integer *res,
1039+
1040+static int undumpint (const char *buff, lua_Integer *res,
10341041 int littleendian, int size) {
1035− lua_Integer n = 0;
1042+ lua_Unsigned n = 0;
10361043 int i;
10371044 for (i = 0; i < size; i++) {
10381045 if (i >= SZINT) { /* will throw away a byte? */
@@ -1042,23 +1049,23 @@ static int unpackint (const char *buff, lua_Integer *res, · 3 unmodified lines
10421049 its "signal bit" */
10431050 if (!((n & HIGHERBYTE1) == 0 || /* zeros for positive number */
10441051 (n & HIGHERBYTE1) == HIGHERBYTE1 || /* ones for negative number */
1045− ((n & HIGHERBYTE) == 0 && i == size - 1))) /* leading zero */
1052+ (i == size - 1 && (n & HIGHERBYTE) == 0))) /* leading zero */
10461053 return 0; /* overflow */
10471054 }
10481055 n <<= NB;
1049− n |= (lua_Integer)(unsigned char)buff[littleendian ? size - 1 - i : i];
1056+ n |= (lua_Unsigned)(unsigned char)buff[littleendian ? size - 1 - i : i];
10501057 }
10511058 if (size < SZINT) { /* need sign extension? */
1052− lua_Integer mask = (~(lua_Integer)0) << (size*NB - 1);
1053− if (n & mask) /* negative value? */
1054− n |= mask; /* signal extension */
1059+ lua_Unsigned mask = (lua_Unsigned)1 << (size*NB - 1);
1060+ *res = (lua_Integer)((n ^ mask) - mask); /* do sign extension */
10551061 }
1056− *res = n;
1062+ else
1063+ *res = (lua_Integer)n;
10571064 return 1;
10581065 }
10591066
10601067
1061−static int unpackint_l (lua_State *L) {
1068+static int undumpint_l (lua_State *L) {
10621069 lua_Integer res;
10631070 size_t len;
10641071 const char *s = luaL_checklstring(L, 1, &len);
@@ -1067,7 +1074,7 @@ static int unpackint_l (lua_State *L) { · 2 unmodified lines
10671074 int endian = getendian(L, 4);
10681075 luaL_argcheck(L, 1 <= pos && (size_t)pos + size - 1 <= len, 1,
10691076 "string too short");
1070− if(unpackint(s + pos - 1, &res, endian, size))
1077+ if(undumpint(s + pos - 1, &res, endian, size))
10711078 lua_pushinteger(L, res);
10721079 else
10731080 luaL_error(L, "result does not fit into a Lua integer");
@@ -1075,9 +1082,9 @@ static int unpackint_l (lua_State *L) { · 1 unmodified line
10751082 }
10761083
10771084
1078−static void correctendianess (lua_State *L, char *b, int size, int endianarg) {
1085+static void correctendianness (lua_State *L, char *b, int size, int endianarg) {
10791086 int endian = getendian(L, endianarg);
1080− if (endian != nativeendian.little) { /* not native endianess? */
1087+ if (endian != nativeendian.little) { /* not native endianness? */
10811088 int i = 0;
10821089 while (i < --size) {
10831090 char temp = b[i];
@@ -1097,7 +1104,7 @@ static int getfloatsize (lua_State *L, int arg) { · 13 unmodified lines
10971104 }
10981105
10991106
1100−static int packfloat_l (lua_State *L) {
1107+static int dumpfloat_l (lua_State *L) {
11011108 float f; double d;
11021109 char *pn; /* pointer to number */
11031110 lua_Number n = luaL_checknumber(L, 1);
@@ -1113,13 +1120,13 @@ static int packfloat_l (lua_State *L) { · 9 unmodified lines
11131120 d = (double)n;
11141121 pn = (char*)&d;
11151122 }
1116− correctendianess(L, pn, size, 3);
1123+ correctendianness(L, pn, size, 3);
11171124 lua_pushlstring(L, pn, size);
11181125 return 1;
11191126 }
11201127
11211128
1122−static int unpackfloat_l (lua_State *L) {
1129+static int undumpfloat_l (lua_State *L) {
11231130 lua_Number res;
11241131 size_t len;
11251132 const char *s = luaL_checklstring(L, 1, &len);
@@ -1129,19 +1136,19 @@ static int unpackfloat_l (lua_State *L) { · 3 unmodified lines
11291136 "string too short");
11301137 if (size == sizeof(lua_Number)) {
11311138 memcpy(&res, s + pos - 1, size);
1132− correctendianess(L, (char*)&res, size, 4);
1139+ correctendianness(L, (char*)&res, size, 4);
11331140 }
11341141 else if (size == sizeof(float)) {
11351142 float f;
11361143 memcpy(&f, s + pos - 1, size);
1137− correctendianess(L, (char*)&f, size, 4);
1144+ correctendianness(L, (char*)&f, size, 4);
11381145 res = (lua_Number)f;
11391146 }
11401147 else { /* native lua_Number may be neither float nor double */
11411148 double d;
11421149 lua_assert(size == sizeof(double));
11431150 memcpy(&d, s + pos - 1, size);
1144− correctendianess(L, (char*)&d, size, 4);
1151+ correctendianness(L, (char*)&d, size, 4);
11451152 res = (lua_Number)d;
11461153 }
11471154 lua_pushnumber(L, res);
@@ -1166,10 +1173,10 @@ static const luaL_Reg strlib[] = { · 18 unmodified lines
11661173 {"reverse", str_reverse},
11671174 {"sub", str_sub},
11681175 {"upper", str_upper},
1169− {"packfloat", packfloat_l},
1170− {"packint", packint_l},
1171− {"unpackfloat", unpackfloat_l},
1172− {"unpackint", unpackint_l},
1176+ {"dumpfloat", dumpfloat_l},
1177+ {"dumpint", dumpint_l},
1178+ {"undumpfloat", undumpfloat_l},
1179+ {"undumpint", undumpint_l},
11731180 {NULL, NULL}
11741181 };
11751182
src/ltable.c+27−21
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ltable.c,v 2.84 2014/01/27 13:34:32 roberto Exp $
2+** $Id: ltable.c,v 2.90 2014/06/18 22:59:29 roberto Exp $
33 ** Lua tables (hash)
44 ** See Copyright Notice in lua.h
55 */
@@ -21,6 +21,7 @@ · 15 unmodified lines
2121 #include <float.h>
2222 #include <math.h>
2323 #include <string.h>
24+#include <limits.h>
2425
2526 #define ltable_c
2627 #define LUA_CORE
@@ -39,14 +40,13 @@ · 12 unmodified lines
3940
4041
4142 /*
42−** max size of array part is 2^MAXBITS
43+** Maximum size of array part (MAXASIZE) is 2^MAXBITS. (SIZEINT is the
44+** minimum between size of int and size of LUA_INTEGER; array indices
45+** are limited by both types.)
4346 */
44−#if LUAI_BITSINT >= 32
45−#define MAXBITS 30
46−#else
47−#define MAXBITS (LUAI_BITSINT-2)
48−#endif
49−
47+#define SIZEINT \
48+ (sizeof(int) < sizeof(LUA_INTEGER) ? sizeof(int) : sizeof(LUA_INTEGER))
49+#define MAXBITS cast_int(SIZEINT * CHAR_BIT - 2)
5050 #define MAXASIZE (1 << MAXBITS)
5151
5252
@@ -67,12 +67,6 @@ · 14 unmodified lines
6767 #define hashpointer(t,p) hashmod(t, IntPoint(p))
6868
6969
70−/* checks whether a float has a value representable as a lua_Integer
71− (and does the conversion if so) */
72−#define numisinteger(x,i) \
73− (((x) == l_floor(x)) && luaV_numtointeger(x, i))
74−
75−
7670 #define dummynode (&dummynode_)
7771
7872 #define isdummy(n) ((n) == dummynode)
@@ -83,6 +77,17 @@ static const Node dummynode_ = { · 4 unmodified lines
8377 };
8478
8579
80+/*
81+** Checks whether a float has a value representable as a lua_Integer
82+** (and does the conversion if so)
83+*/
84+static int numisinteger (lua_Number x, lua_Integer *p) {
85+ if ((x) == l_floor(x)) /* integral value? */
86+ return lua_numtointeger(x, p); /* try as an integer */
87+ else return 0;
88+}
89+
90+
8691 /*
8792 ** hash for floating-point numbers
8893 */
@@ -378,7 +383,8 @@ static void rehash (lua_State *L, Table *t, const TValue *ek) { · 289 unmodified lines
378383
379384
380385 Table *luaH_new (lua_State *L) {
381− Table *t = &luaC_newobj(L, LUA_TTABLE, sizeof(Table))->h;
386+ GCObject *o = luaC_newobj(L, LUA_TTABLE, sizeof(Table));
387+ Table *t = gco2t(o);
382388 t->metatable = NULL;
383389 t->flags = cast_byte(~0);
384390 t->array = NULL;
@@ -424,7 +430,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { · 39 unmodified lines
424430 if (luai_numisnan(n))
425431 luaG_runerror(L, "table index is NaN");
426432 if (numisinteger(n, &k)) { /* index is int? */
427− setivalue(&aux, k);
433+ setivalue(&aux, k);
428434 key = &aux; /* insert it as an integer */
429435 }
430436 }
@@ -443,10 +449,10 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { · 12 unmodified lines
443449 /* yes; move colliding node into free position */
444450 while (othern + gnext(othern) != mp) /* find previous */
445451 othern += gnext(othern);
446− gnext(othern) = f - othern; /* re-chain with 'f' in place of 'mp' */
452+ gnext(othern) = cast_int(f - othern); /* rechain to point to 'f' */
447453 *f = *mp; /* copy colliding node into free pos. (mp->next also goes) */
448454 if (gnext(mp) != 0) {
449− gnext(f) += mp - f; /* correct 'next' */
455+ gnext(f) += cast_int(mp - f); /* correct 'next' */
450456 gnext(mp) = 0; /* now 'mp' is free */
451457 }
452458 setnilvalue(gval(mp));
@@ -454,9 +460,9 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { · 1 unmodified line
454460 else { /* colliding node is in its own main position */
455461 /* new node will go into free position */
456462 if (gnext(mp) != 0)
457− gnext(f) = (mp + gnext(mp)) - f; /* chain new position */
463+ gnext(f) = cast_int((mp + gnext(mp)) - f); /* chain new position */
458464 else lua_assert(gnext(f) == 0);
459− gnext(mp) = f - mp;
465+ gnext(mp) = cast_int(f - mp);
460466 mp = f;
461467 }
462468 }
@@ -472,7 +478,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { · 9 unmodified lines
472478 */
473479 const TValue *luaH_getint (Table *t, lua_Integer key) {
474480 /* (1 <= key && key <= t->sizearray) */
475− if (cast_unsigned(key - 1) < cast_unsigned(t->sizearray))
481+ if (l_castS2U(key - 1) < cast(unsigned int, t->sizearray))
476482 return &t->array[key - 1];
477483 else {
478484 Node *n = hashint(t, key);
src/ltablib.c+27−28
@@ -1,10 +1,11 @@
11 /*
2−** $Id: ltablib.c,v 1.66 2014/03/21 13:52:33 roberto Exp $
2+** $Id: ltablib.c,v 1.70 2014/05/16 18:53:25 roberto Exp $
33 ** Library for Table Manipulation
44 ** See Copyright Notice in lua.h
55 */
66
77
8+#include <limits.h>
89 #include <stddef.h>
910
1011 #define ltablib_c
@@ -39,16 +40,16 @@ static int maxn (lua_State *L) { · 28 unmodified lines
3940
4041
4142 static int tinsert (lua_State *L) {
42− int e = aux_getn(L, 1) + 1; /* first empty element */
43− int pos; /* where to insert new element */
43+ lua_Integer e = aux_getn(L, 1) + 1; /* first empty element */
44+ lua_Integer pos; /* where to insert new element */
4445 switch (lua_gettop(L)) {
4546 case 2: { /* called with only 2 arguments */
4647 pos = e; /* insert new element at the end */
4748 break;
4849 }
4950 case 3: {
50− int i;
51− pos = luaL_checkint(L, 2); /* 2nd argument is the position */
51+ lua_Integer i;
52+ pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */
5253 luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds");
5354 for (i = e; i > pos; i--) { /* move up elements */
5455 lua_rawgeti(L, 1, i-1);
@@ -66,8 +67,8 @@ static int tinsert (lua_State *L) { · 11 unmodified lines
6667
6768
6869 static int tremove (lua_State *L) {
69− int size = aux_getn(L, 1);
70− int pos = luaL_optint(L, 2, size);
70+ lua_Integer size = aux_getn(L, 1);
71+ lua_Integer pos = luaL_optinteger(L, 2, size);
7172 if (pos != size) /* validate 'pos' if given */
7273 luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds");
7374 lua_rawgeti(L, 1, pos); /* result = t[pos] */
@@ -81,7 +82,7 @@ static int tremove (lua_State *L) { · 7 unmodified lines
8182 }
8283
8384
84−static void addfield (lua_State *L, luaL_Buffer *b, int i) {
85+static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) {
8586 lua_rawgeti(L, 1, i);
8687 if (!lua_isstring(L, -1))
8788 luaL_error(L, "invalid value (%s) at index %d in table for "
@@ -93,11 +94,11 @@ static void addfield (lua_State *L, luaL_Buffer *b, int i) { · 5 unmodified lines
9394 static int tconcat (lua_State *L) {
9495 luaL_Buffer b;
9596 size_t lsep;
96− int i, last;
97+ lua_Integer i, last;
9798 const char *sep = luaL_optlstring(L, 2, "", &lsep);
9899 luaL_checktype(L, 1, LUA_TTABLE);
99− i = luaL_optint(L, 3, 1);
100− last = luaL_opt(L, luaL_checkint, 4, luaL_len(L, 1));
100+ i = luaL_optinteger(L, 3, 1);
101+ last = luaL_opt(L, luaL_checkinteger, 4, luaL_len(L, 1));
101102 luaL_buffinit(L, &b);
102103 for (; i < last; i++) {
103104 addfield(L, &b, i);
@@ -117,34 +118,32 @@ static int tconcat (lua_State *L) { · 13 unmodified lines
117118 */
118119
119120 static int pack (lua_State *L) {
121+ int i;
120122 int n = lua_gettop(L); /* number of elements to pack */
121123 lua_createtable(L, n, 1); /* create result table */
124+ lua_insert(L, 1); /* put it at index 1 */
125+ for (i = n; i >= 1; i--) /* assign elements */
126+ lua_rawseti(L, 1, i);
122127 lua_pushinteger(L, n);
123− lua_setfield(L, -2, "n"); /* t.n = number of elements */
124− if (n > 0) { /* at least one element? */
125− int i;
126− lua_pushvalue(L, 1);
127− lua_rawseti(L, -2, 1); /* insert first element */
128− lua_replace(L, 1); /* move table into index 1 */
129− for (i = n; i >= 2; i--) /* assign other elements */
130− lua_rawseti(L, 1, i);
131− }
128+ lua_setfield(L, 1, "n"); /* t.n = number of elements */
132129 return 1; /* return table */
133130 }
134131
135132
136133 static int unpack (lua_State *L) {
137− int i, e, n;
134+ lua_Integer i, e;
135+ lua_Unsigned n;
138136 luaL_checktype(L, 1, LUA_TTABLE);
139− i = luaL_optint(L, 2, 1);
140− e = luaL_opt(L, luaL_checkint, 3, luaL_len(L, 1));
137+ i = luaL_optinteger(L, 2, 1);
138+ e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1));
141139 if (i > e) return 0; /* empty range */
142− n = e - i + 1; /* number of elements */
143− if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arithmetic overflow */
140+ n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */
141+ if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, ++n))
144142 return luaL_error(L, "too many results to unpack");
145− lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */
146− while (i++ < e) /* push arg[i + 1...e] */
147− lua_rawgeti(L, 1, i);
143+ do { /* must have at least one element */
144+ lua_rawgeti(L, 1, i); /* push arg[i..e] */
145+ } while (i++ < e);
146+
148147 return n;
149148 }
150149
src/ltm.c+5−16
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ltm.c,v 2.25 2013/12/30 20:47:58 roberto Exp $
2+** $Id: ltm.c,v 2.27 2014/06/10 18:53:18 roberto Exp $
33 ** Tag methods
44 ** See Copyright Notice in lua.h
55 */
@@ -116,10 +116,12 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, · 110 unmodified lines
116116 case TM_CONCAT:
117117 luaG_concaterror(L, p1, p2);
118118 case TM_IDIV: case TM_BAND: case TM_BOR: case TM_BXOR:
119− case TM_SHL: case TM_SHR: case TM_BNOT:
120− if (ttisnumber(p1) && ttisnumber(p2))
119+ case TM_SHL: case TM_SHR: case TM_BNOT: {
120+ lua_Number dummy;
121+ if (tonumber(p1, &dummy) && tonumber(p2, &dummy))
121122 luaG_tointerror(L, p1, p2);
122123 /* else go through */
124+ }
123125 default:
124126 luaG_aritherror(L, p1, p2);
125127 }
@@ -127,19 +129,6 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, · 1 unmodified line
127129 }
128130
129131
130−const TValue *luaT_getequalTM (lua_State *L, Table *mt1, Table *mt2) {
131− const TValue *tm1 = fasttm(L, mt1, TM_EQ);
132− const TValue *tm2;
133− if (tm1 == NULL) return NULL; /* no metamethod */
134− if (mt1 == mt2) return tm1; /* same metatables => same metamethods */
135− tm2 = fasttm(L, mt2, TM_EQ);
136− if (tm2 == NULL) return NULL; /* no metamethod */
137− if (luaV_rawequalobj(tm1, tm2)) /* same metamethods? */
138− return tm1;
139− return NULL;
140−}
141−
142−
143132 int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
144133 TMS event) {
145134 if (!luaT_callbinTM(L, p1, p2, L->top, event))
src/ltm.h+1−2
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ltm.h,v 2.19 2013/12/30 20:47:58 roberto Exp $
2+** $Id: ltm.h,v 2.20 2014/06/10 18:53:18 roberto Exp $
33 ** Tag methods
44 ** See Copyright Notice in lua.h
55 */
@@ -67,7 +67,6 @@ LUAI_FUNC int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2, · 61 unmodified lines
6767 StkId res, TMS event);
6868 LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
6969 StkId res, TMS event);
70−LUAI_FUNC const TValue *luaT_getequalTM (lua_State *L, Table *mt1, Table *mt2);
7170 LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1,
7271 const TValue *p2, TMS event);
7372
src/lua.c+223−153
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lua.c,v 1.210 2014/02/26 15:27:56 roberto Exp $
2+** $Id: lua.c,v 1.211 2014/06/05 20:42:06 roberto Exp $
33 ** Lua stand-alone interpreter
44 ** See Copyright Notice in lua.h
55 */
@@ -103,17 +103,24 @@ static lua_State *globalL = NULL; · 97 unmodified lines
103103 static const char *progname = LUA_PROGNAME;
104104
105105
106−
106+/*
107+** Hook set by signal function to stop the interpreter.
108+*/
107109 static void lstop (lua_State *L, lua_Debug *ar) {
108110 (void)ar; /* unused arg. */
109− lua_sethook(L, NULL, 0, 0);
111+ lua_sethook(L, NULL, 0, 0); /* reset hook */
110112 luaL_error(L, "interrupted!");
111113 }
112114
113115
116+/*
117+** Function to be called at a C signal. Because a C signal cannot
118+** just change a Lua state (as there is no proper syncronization),
119+** this function only sets a hook that, when called, will stop the
120+** interpreter.
121+*/
114122 static void laction (int i) {
115− signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
116− terminate process (default action) */
123+ signal(i, SIG_DFL); /* if another SIGINT happens, terminate process */
117124 lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
118125 }
119126
@@ -139,27 +146,23 @@ static void print_usage (const char *badoption) { · 19 unmodified lines
139146 }
140147
141148
149+/*
150+** Prints an error message, adding the program name in front of it
151+** (if present)
152+*/
142153 static void l_message (const char *pname, const char *msg) {
143154 if (pname) luai_writestringerror("%s: ", pname);
144155 luai_writestringerror("%s\n", msg);
145156 }
146157
147158
159+/*
160+** Check whether 'status' is not OK and, if so, prints the error
161+** message on the top of the stack. Because this function can be called
162+** unprotected, it only accepts actual strings as error messages. (A
163+** coercion could raise a memory error.)
164+*/
148165 static int report (lua_State *L, int status) {
149− if (status != LUA_OK && !lua_isnil(L, -1)) {
150− const char *msg = lua_tostring(L, -1);
151− if (msg == NULL) msg = "(error object is not a string)";
152− l_message(progname, msg);
153− lua_pop(L, 1);
154− /* force a complete garbage collection in case of errors */
155− lua_gc(L, LUA_GCCOLLECT, 0);
156− }
157− return status;
158−}
159−
160−
161−/* the next function is called unprotected, so it must avoid errors */
162−static void finalreport (lua_State *L, int status) {
163166 if (status != LUA_OK) {
164167 const char *msg = (lua_type(L, -1) == LUA_TSTRING) ? lua_tostring(L, -1)
165168 : NULL;
@@ -167,31 +170,39 @@ static void finalreport (lua_State *L, int status) { · 1 unmodified line
167170 l_message(progname, msg);
168171 lua_pop(L, 1);
169172 }
173+ return status;
170174 }
171175
172176
173−static int traceback (lua_State *L) {
177+/*
178+** Message handler to be used to run all chunks
179+*/
180+static int msghandler (lua_State *L) {
174181 const char *msg = lua_tostring(L, 1);
175− if (msg)
176− luaL_traceback(L, L, msg, 1);
177− else if (!lua_isnoneornil(L, 1)) { /* is there an error object? */
182+ if (msg) /* is error object a string? */
183+ luaL_traceback(L, L, msg, 1); /* use standard traceback */
184+ else if (!lua_isnoneornil(L, 1)) { /* non-string error object? */
178185 if (!luaL_callmeta(L, 1, "__tostring")) /* try its 'tostring' metamethod */
179186 lua_pushliteral(L, "(no error message)");
180− }
187+ } /* else no error object, does nothing */
181188 return 1;
182189 }
183190
184191
192+/*
193+** Interface to 'lua_pcall', which sets appropriate message function
194+** and C-signal handler. Used to run all chunks.
195+*/
185196 static int docall (lua_State *L, int narg, int nres) {
186197 int status;
187198 int base = lua_gettop(L) - narg; /* function index */
188− lua_pushcfunction(L, traceback); /* push traceback function */
189− lua_insert(L, base); /* put it under chunk and args */
199+ lua_pushcfunction(L, msghandler); /* push message handler */
200+ lua_insert(L, base); /* put it under function and args */
190201 globalL = L; /* to be available to 'laction' */
191− signal(SIGINT, laction);
202+ signal(SIGINT, laction); /* set C-signal handler */
192203 status = lua_pcall(L, narg, nres, base);
193− signal(SIGINT, SIG_DFL);
194− lua_remove(L, base); /* remove traceback function */
204+ signal(SIGINT, SIG_DFL); /* reset C-signal handler */
205+ lua_remove(L, base); /* remove message handler from the stack */
195206 return status;
196207 }
197208
@@ -202,38 +213,47 @@ static void print_version (void) { · 4 unmodified lines
202213 }
203214
204215
205−static int getargs (lua_State *L, char **argv, int n) {
206− int narg;
207− int i;
208− int argc = 0;
209− while (argv[argc]) argc++; /* count total number of arguments */
210− narg = argc - (n + 1); /* number of arguments to the script */
211− luaL_checkstack(L, narg + 3, "too many arguments to script");
212− for (i=n+1; i < argc; i++)
213− lua_pushstring(L, argv[i]);
214− lua_createtable(L, narg, n + 1);
215− for (i=0; i < argc; i++) {
216+/*
217+** Create the 'arg' table, which stores all arguments from the
218+** command line ('argv'). It should be aligned so that, at index 0,
219+** it has 'argv[script]', which is the script name. The arguments
220+** to the script (everything after 'script') go to positive indices;
221+** other arguments (before the script name) go to negative indices.
222+** If there is no script name, assume interpreter's name as base.
223+*/
224+static void createargtable (lua_State *L, char **argv, int argc, int script) {
225+ int i, narg;
226+ if (script == argc) script = 0; /* no script name? */
227+ narg = argc - (script + 1); /* number of positive indices */
228+ lua_createtable(L, narg, script + 1);
229+ for (i = 0; i < argc; i++) {
216230 lua_pushstring(L, argv[i]);
217− lua_rawseti(L, -2, i - n);
231+ lua_rawseti(L, -2, i - script);
218232 }
219− return narg;
233+ lua_setglobal(L, "arg");
220234 }
221235
222236
223−static int dofile (lua_State *L, const char *name) {
224− int status = luaL_loadfile(L, name);
237+static int dochunk (lua_State *L, int status) {
225238 if (status == LUA_OK) status = docall(L, 0, 0);
226239 return report(L, status);
227240 }
228241
229242
243+static int dofile (lua_State *L, const char *name) {
244+ return dochunk(L, luaL_loadfile(L, name));
245+}
246+
247+
230248 static int dostring (lua_State *L, const char *s, const char *name) {
231− int status = luaL_loadbuffer(L, s, strlen(s), name);
232− if (status == LUA_OK) status = docall(L, 0, 0);
233− return report(L, status);
249+ return dochunk(L, luaL_loadbuffer(L, s, strlen(s), name));
234250 }
235251
236252
253+/*
254+** Calls 'require(name)' and stores the result in a global variable
255+** with the given name.
256+*/
237257 static int dolibrary (lua_State *L, const char *name) {
238258 int status;
239259 lua_getglobal(L, "require");
@@ -245,6 +265,9 @@ static int dolibrary (lua_State *L, const char *name) { · 5 unmodified lines
245265 }
246266
247267
268+/*
269+** Returns the string to be used as a prompt by the interpreter.
270+*/
248271 static const char *get_prompt (lua_State *L, int firstline) {
249272 const char *p;
250273 lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2");
@@ -257,6 +280,12 @@ static const char *get_prompt (lua_State *L, int firstline) { · 6 unmodified lines
257280 #define EOFMARK "<eof>"
258281 #define marklen (sizeof(EOFMARK)/sizeof(char) - 1)
259282
283+
284+/*
285+** Check whether 'status' signals a syntax error and the error
286+** message at the top of the stack ends with the above mark for
287+** incoplete statements.
288+*/
260289 static int incomplete (lua_State *L, int status) {
261290 if (status == LUA_ERRSYNTAX) {
262291 size_t lmsg;
@@ -270,7 +299,9 @@ static int incomplete (lua_State *L, int status) { · 7 unmodified lines
270299 }
271300
272301
273−/* prompt the user, read a line, and push it into the Lua stack */
302+/*
303+** Prompt the user, read a line, and push it into the Lua stack.
304+*/
274305 static int pushline (lua_State *L, int firstline) {
275306 char buffer[LUA_MAXINPUT];
276307 char *b = buffer;
@@ -292,8 +323,10 @@ static int pushline (lua_State *L, int firstline) { · 15 unmodified lines
292323 }
293324
294325
295−/* try to compile line on the stack as 'return <line>'; on return, stack
296− has either compiled chunk or original line (if compilation failed) */
326+/*
327+** Try to compile line on the stack as 'return <line>'; on return, stack
328+** has either compiled chunk or original line (if compilation failed).
329+*/
297330 static int addreturn (lua_State *L) {
298331 int status;
299332 size_t len; const char *line;
@@ -309,14 +342,16 @@ static int addreturn (lua_State *L) { · 9 unmodified lines
309342 }
310343
311344
312−/* read multiple lines until a complete line */
345+/*
346+** Read multiple lines until a complete Lua statement
347+*/
313348 static int multiline (lua_State *L) {
314− for (;;) { /* repeat until gets a complete line */
349+ for (;;) { /* repeat until gets a complete statement */
315350 size_t len;
316351 const char *line = lua_tolstring(L, 1, &len); /* get what it has */
317352 int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */
318353 if (!incomplete(L, status) || !pushline(L, 0))
319− return status; /* cannot/should not try to add continuation line */
354+ return status; /* cannot or should not try to add continuation line */
320355 lua_pushliteral(L, "\n"); /* add newline... */
321356 lua_insert(L, -2); /* ...between the two lines */
322357 lua_concat(L, 3); /* join them */
@@ -324,6 +359,12 @@ static int multiline (lua_State *L) { · 1 unmodified line
324359 }
325360
326361
362+/*
363+** Read a line and try to load (compile) it first as an expression (by
364+** adding "return " in front of it) and second as a statement. Return
365+** the final status of load/call with the resulting function (if any)
366+** in the top of the stack.
367+*/
327368 static int loadline (lua_State *L) {
328369 int status;
329370 lua_settop(L, 0);
@@ -331,29 +372,43 @@ static int loadline (lua_State *L) { · 1 unmodified line
331372 return -1; /* no input */
332373 if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */
333374 status = multiline(L); /* try as command, maybe with continuation lines */
334− lua_saveline(L, 1);
335− lua_remove(L, 1); /* remove line */
375+ lua_saveline(L, 1); /* keep history */
376+ lua_remove(L, 1); /* remove line from the stack */
336377 lua_assert(lua_gettop(L) == 1);
337378 return status;
338379 }
339380
340381
341−static void dotty (lua_State *L) {
382+/*
383+** Prints (calling the Lua 'print' function) any values on the stack
384+*/
385+static void l_print (lua_State *L) {
386+ int n = lua_gettop(L);
387+ if (n > 0) { /* any result to be printed? */
388+ luaL_checkstack(L, LUA_MINSTACK, "too many results to print");
389+ lua_getglobal(L, "print");
390+ lua_insert(L, 1);
391+ if (lua_pcall(L, n, 0, 0) != LUA_OK)
392+ l_message(progname, lua_pushfstring(L,
393+ "error calling " LUA_QL("print") " (%s)",
394+ lua_tostring(L, -1)));
395+ }
396+}
397+
398+
399+/*
400+** Do the REPL: repeatedly read (load) a line, evaluate (call) it, and
401+** print any results.
402+*/
403+static void doREPL (lua_State *L) {
342404 int status;
343405 const char *oldprogname = progname;
344− progname = NULL;
406+ progname = NULL; /* no 'progname' on errors in interactive mode */
345407 while ((status = loadline(L)) != -1) {
346− if (status == LUA_OK) status = docall(L, 0, LUA_MULTRET);
347− report(L, status);
348− if (status == LUA_OK && lua_gettop(L) > 0) { /* any result to print? */
349− luaL_checkstack(L, LUA_MINSTACK, "too many results to print");
350− lua_getglobal(L, "print");
351− lua_insert(L, 1);
352− if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != LUA_OK)
353− l_message(progname, lua_pushfstring(L,
354− "error calling " LUA_QL("print") " (%s)",
355− lua_tostring(L, -1)));
356− }
408+ if (status == LUA_OK)
409+ status = docall(L, 0, LUA_MULTRET);
410+ if (status == LUA_OK) l_print(L);
411+ else report(L, status);
357412 }
358413 lua_settop(L, 0); /* clear stack */
359414 luai_writeline();
@@ -361,97 +416,109 @@ static void dotty (lua_State *L) { · 1 unmodified line
361416 }
362417
363418
419+/*
420+** Push on the stack 'n' strings from 'argv'
421+*/
422+static void pushargs (lua_State *L, char **argv, int n) {
423+ int i;
424+ luaL_checkstack(L, n + 3, "too many arguments to script");
425+ for (i = 1; i < n; i++) /* skip 0 (the script name) */
426+ lua_pushstring(L, argv[i]);
427+}
428+
429+
364430 static int handle_script (lua_State *L, char **argv, int n) {
365431 int status;
366− const char *fname;
367− int narg = getargs(L, argv, n); /* collect arguments */
368− lua_setglobal(L, "arg");
369− fname = argv[n];
370− if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0)
432+ const char *fname = argv[0];
433+ if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0)
371434 fname = NULL; /* stdin */
372435 status = luaL_loadfile(L, fname);
373− lua_insert(L, -(narg+1));
374− if (status == LUA_OK)
375− status = docall(L, narg, LUA_MULTRET);
376− else
377− lua_pop(L, narg);
436+ if (status == LUA_OK) {
437+ pushargs(L, argv, n); /* push arguments to script */
438+ status = docall(L, n - 1, LUA_MULTRET);
439+ }
378440 return report(L, status);
379441 }
380442
381443
382−/* check that argument has no extra characters at the end */
383−#define noextrachars(x) {if ((x)[2] != '\0') return -1;}
384−
385−
386−/* indices of various argument indicators in array args */
387−#define has_i 0 /* -i */
388−#define has_v 1 /* -v */
389−#define has_e 2 /* -e */
390−#define has_E 3 /* -E */
391−
392−#define num_has 4 /* number of 'has_*' */
393444
445+/* bits of various argument indicators in 'args' */
446+#define has_error 1 /* bad option */
447+#define has_i 2 /* -i */
448+#define has_v 4 /* -v */
449+#define has_e 8 /* -e */
450+#define has_E 16 /* -E */
394451
395−static int collectargs (char **argv, int *args) {
452+/*
453+** Traverses all arguments from 'argv', returning a mask with those
454+** needed before running any Lua code (or an error code if it finds
455+** any invalid argument). 'first' returns the first not-handled argument
456+** (either the script name or a bad argument in case of error).
457+*/
458+static int collectargs (char **argv, int *first) {
459+ int args = 0;
396460 int i;
397461 for (i = 1; argv[i] != NULL; i++) {
462+ *first = i;
398463 if (argv[i][0] != '-') /* not an option? */
399− return i;
400− switch (argv[i][1]) { /* option */
401− case '-':
402− noextrachars(argv[i]);
403− return (argv[i+1] != NULL ? i+1 : 0);
404− case '\0':
405− return i;
464+ return args; /* stop handling options */
465+ switch (argv[i][1]) { /* else check option */
466+ case '-': /* '--' */
467+ if (argv[i][2] != '\0') /* extra characters after '--'? */
468+ return has_error; /* invalid option */
469+ *first = i + 1;
470+ return args;
471+ case '\0': /* '-' */
472+ return args; /* script "name" is '-' */
406473 case 'E':
407− args[has_E] = 1;
474+ if (argv[i][2] != '\0') /* extra characters after 1st? */
475+ return has_error; /* invalid option */
476+ args |= has_E;
408477 break;
409478 case 'i':
410− noextrachars(argv[i]);
411− args[has_i] = 1; /* go through */
479+ args |= has_i; /* goes through (-i implies -v) */
412480 case 'v':
413− noextrachars(argv[i]);
414− args[has_v] = 1;
481+ if (argv[i][2] != '\0') /* extra characters after 1st? */
482+ return has_error; /* invalid option */
483+ args |= has_v;
415484 break;
416485 case 'e':
417− args[has_e] = 1; /* go through */
486+ args |= has_e; /* go through */
418487 case 'l': /* both options need an argument */
419488 if (argv[i][2] == '\0') { /* no concatenated argument? */
420489 i++; /* try next 'argv' */
421490 if (argv[i] == NULL || argv[i][0] == '-')
422− return -(i - 1); /* no next argument or it is another option */
491+ return has_error; /* no next argument or it is another option */
423492 }
424493 break;
425− default: /* invalid option; return its index... */
426− return -i; /* ...as a negative value */
494+ default: /* invalid option */
495+ return has_error;
427496 }
428497 }
429− return 0;
498+ *first = i; /* no script name */
499+ return args;
430500 }
431501
432502
503+/*
504+** Processes options 'e' and 'l', which involve running Lua code.
505+** Returns 0 if some code raises an error.
506+*/
433507 static int runargs (lua_State *L, char **argv, int n) {
434508 int i;
435509 for (i = 1; i < n; i++) {
436− lua_assert(argv[i][0] == '-');
437− switch (argv[i][1]) { /* option */
438− case 'e': {
439− const char *chunk = argv[i] + 2;
440− if (*chunk == '\0') chunk = argv[++i];
441− lua_assert(chunk != NULL);
442− if (dostring(L, chunk, "=(command line)") != LUA_OK)
443− return 0;
444− break;
445− }
446− case 'l': {
447− const char *filename = argv[i] + 2;
448− if (*filename == '\0') filename = argv[++i];
449− lua_assert(filename != NULL);
450− if (dolibrary(L, filename) != LUA_OK)
451− return 0; /* stop if file fails */
452− break;
453− }
454− default: break;
510+ int status;
511+ int option = argv[i][1];
512+ lua_assert(argv[i][0] == '-'); /* already checked */
513+ if (option == 'e' || option == 'l') {
514+ const char *extra = argv[i] + 2; /* both options need an argument */
515+ if (*extra == '\0') extra = argv[++i];
516+ lua_assert(extra != NULL);
517+ if (option == 'e')
518+ status = dostring(L, extra, "=(command line)");
519+ else
520+ status = dolibrary(L, extra);
521+ if (status != LUA_OK) return 0;
455522 }
456523 }
457524 return 1;
@@ -473,40 +540,44 @@ static int handle_luainit (lua_State *L) { · 15 unmodified lines
473540 }
474541
475542
543+/*
544+** Main body of stand-alone interpreter (to be called in protected mode).
545+** Reads the options and handles them all.
546+*/
476547 static int pmain (lua_State *L) {
477548 int argc = (int)lua_tointeger(L, 1);
478549 char **argv = (char **)lua_touserdata(L, 2);
479550 int script;
480− int args[num_has];
481− args[has_i] = args[has_v] = args[has_e] = args[has_E] = 0;
551+ int args = collectargs(argv, &script);
552+ luaL_checkversion(L); /* check that interpreter has correct version */
482553 if (argv[0] && argv[0][0]) progname = argv[0];
483− script = collectargs(argv, args);
484− if (script < 0) { /* invalid arg? */
485− print_usage(argv[-script]);
554+ if (args == has_error) { /* bad arg? */
555+ print_usage(argv[script]); /* 'script' has index of bad arg. */
486556 return 0;
487557 }
488− if (args[has_v]) print_version();
489− if (args[has_E]) { /* option '-E'? */
558+ if (args & has_v) /* option '-v'? */
559+ print_version();
560+ if (args & has_E) { /* option '-E'? */
490561 lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */
491562 lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
492563 }
493− /* open standard libraries */
494− luaL_checkversion(L);
495− lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */
496− luaL_openlibs(L); /* open libraries */
497− lua_gc(L, LUA_GCRESTART, 0);
498− if (!args[has_E] && handle_luainit(L) != LUA_OK)
499− return 0; /* error running LUA_INIT */
500− /* execute arguments -e and -l */
501− if (!runargs(L, argv, (script > 0) ? script : argc)) return 0;
502− /* execute main script (if there is one) */
503− if (script && handle_script(L, argv, script) != LUA_OK) return 0;
504− if (args[has_i]) /* -i option? */
505− dotty(L);
506− else if (script == 0 && !args[has_e] && !args[has_v]) { /* no arguments? */
507− if (lua_stdin_is_tty()) {
564+ luaL_openlibs(L); /* open standard libraries */
565+ createargtable(L, argv, argc, script); /* create table 'arg' */
566+ if (!(args & has_E)) { /* no option '-E'? */
567+ if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */
568+ return 0; /* error running LUA_INIT */
569+ }
570+ if (!runargs(L, argv, script)) /* execute arguments -e and -l */
571+ return 0; /* something failed */
572+ if (script < argc && /* execute main script (if there is one) */
573+ handle_script(L, argv + script, argc - script) != LUA_OK)
574+ return 0;
575+ if (args & has_i) /* -i option? */
576+ doREPL(L); /* do read-eval-print loop */
577+ else if (script == argc && !(args & (has_e | has_v))) { /* no arguments? */
578+ if (lua_stdin_is_tty()) { /* running in interactive mode? */
508579 print_version();
509− dotty(L);
580+ doREPL(L); /* do read-eval-print loop */
510581 }
511582 else dofile(L, NULL); /* executes stdin as a file */
512583 }
@@ -522,13 +593,12 @@ int main (int argc, char **argv) { · 9 unmodified lines
522593 l_message(argv[0], "cannot create state: not enough memory");
523594 return EXIT_FAILURE;
524595 }
525− /* call 'pmain' in protected mode */
526− lua_pushcfunction(L, &pmain);
596+ lua_pushcfunction(L, &pmain); /* to call 'pmain' in protected mode */
527597 lua_pushinteger(L, argc); /* 1st argument */
528598 lua_pushlightuserdata(L, argv); /* 2nd argument */
529− status = lua_pcall(L, 2, 1, 0);
599+ status = lua_pcall(L, 2, 1, 0); /* do the call */
530600 result = lua_toboolean(L, -1); /* get result */
531− finalreport(L, status);
601+ report(L, status);
532602 lua_close(L);
533603 return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE;
534604 }
src/lua.h+25−14
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lua.h,v 1.302 2014/03/20 19:42:35 roberto Exp $
2+** $Id: lua.h,v 1.307 2014/06/10 17:41:38 roberto Exp $
33 ** Lua - A Scripting Language
44 ** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
55 ** See Copyright Notice at the end of this file
@@ -19,7 +19,7 @@ · 13 unmodified lines
1919 #define LUA_VERSION_MAJOR "5"
2020 #define LUA_VERSION_MINOR "3"
2121 #define LUA_VERSION_NUM 503
22−#define LUA_VERSION_RELEASE "0 (work2)"
22+#define LUA_VERSION_RELEASE "0 (work3)"
2323
2424 #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
2525 #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE
@@ -53,8 +53,16 @@ · 27 unmodified lines
5353
5454 typedef struct lua_State lua_State;
5555
56+/*
57+** Type for C functions registered with Lua
58+*/
5659 typedef int (*lua_CFunction) (lua_State *L);
5760
61+/*
62+** Type for continuation functions
63+*/
64+typedef int (*lua_KFunction) (lua_State *L, int status, int ctx);
65+
5866
5967 /*
6068 ** functions that read/write blocks when loading/dumping Lua chunks
@@ -145,8 +153,7 @@ LUA_API int (lua_absindex) (lua_State *L, int idx); · 84 unmodified lines
145153 LUA_API int (lua_gettop) (lua_State *L);
146154 LUA_API void (lua_settop) (lua_State *L, int idx);
147155 LUA_API void (lua_pushvalue) (lua_State *L, int idx);
148−LUA_API void (lua_remove) (lua_State *L, int idx);
149−LUA_API void (lua_insert) (lua_State *L, int idx);
156+LUA_API void (lua_rotate) (lua_State *L, int idx, int n);
150157 LUA_API void (lua_replace) (lua_State *L, int idx);
151158 LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx);
152159 LUA_API int (lua_checkstack) (lua_State *L, int sz);
@@ -238,7 +245,7 @@ LUA_API int (lua_rawgetp) (lua_State *L, int idx, const void *p); · 85 unmodified lines
238245 LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec);
239246 LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz);
240247 LUA_API int (lua_getmetatable) (lua_State *L, int objindex);
241−LUA_API void (lua_getuservalue) (lua_State *L, int idx);
248+LUA_API int (lua_getuservalue) (lua_State *L, int idx);
242249
243250
244251 /*
@@ -258,13 +265,11 @@ LUA_API void (lua_setuservalue) (lua_State *L, int idx); · 13 unmodified lines
258265 ** 'load' and 'call' functions (load and run Lua code)
259266 */
260267 LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults, int ctx,
261− lua_CFunction k);
268+ lua_KFunction k);
262269 #define lua_call(L,n,r) lua_callk(L, (n), (r), 0, NULL)
263270
264−LUA_API int (lua_getctx) (lua_State *L, int *ctx);
265−
266271 LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
267− int ctx, lua_CFunction k);
272+ int ctx, lua_KFunction k);
268273 #define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL)
269274
270275 LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt,
@@ -278,10 +283,12 @@ LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip); · 7 unmodified lines
278283 ** coroutine functions
279284 */
280285 LUA_API int (lua_yieldk) (lua_State *L, int nresults, int ctx,
281− lua_CFunction k);
286+ lua_KFunction k);
282287 #define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL)
283288 LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg);
284289 LUA_API int (lua_status) (lua_State *L);
290+LUA_API int lua_isyieldable (lua_State *L);
291+
285292
286293 /*
287294 ** garbage-collection function and options
@@ -311,7 +318,7 @@ LUA_API int (lua_next) (lua_State *L, int idx); · 23 unmodified lines
311318 LUA_API void (lua_concat) (lua_State *L, int n);
312319 LUA_API void (lua_len) (lua_State *L, int idx);
313320
314−LUA_API int (lua_strtonum) (lua_State *L, const char *s, size_t len);
321+LUA_API size_t (lua_strtonum) (lua_State *L, const char *s);
315322
316323 LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud);
317324 LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);
@@ -324,9 +331,9 @@ LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); · 6 unmodified lines
324331 ** ===============================================================
325332 */
326333
327−#define lua_tonumber(L,i) lua_tonumberx(L,i,NULL)
328−#define lua_tointeger(L,i) lua_tointegerx(L,i,NULL)
329−#define lua_tounsigned(L,i) lua_tounsignedx(L,i,NULL)
334+#define lua_tonumber(L,i) lua_tonumberx(L,(i),NULL)
335+#define lua_tointeger(L,i) lua_tointegerx(L,(i),NULL)
336+#define lua_tounsigned(L,i) lua_tounsignedx(L,(i),NULL)
330337
331338 #define lua_pop(L,n) lua_settop(L, -(n)-1)
332339
@@ -354,6 +361,10 @@ LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); · 21 unmodified lines
354361 #define lua_tostring(L,i) lua_tolstring(L, (i), NULL)
355362
356363
364+#define lua_insert(L,idx) lua_rotate(L, (idx), 1)
365+
366+#define lua_remove(L,idx) (lua_rotate(L, (idx), -1), lua_pop(L, 1))
367+
357368
358369 /*
359370 ** {======================================================================
src/luac.c+15−8
@@ -202,10 +202,8 @@ int main(int argc, char* argv[]) · 201 unmodified lines
202202 return EXIT_SUCCESS;
203203 }
204204
205−#define nvalue(x) ((lua_Number)0)
206−#define ttypenv(x) ttnov(x)
207205 /*
208−** $Id: print.c,v 1.69 2013/07/04 01:03:46 lhf Exp $
206+** $Id: print.c,v 1.73 2014/06/12 02:41:25 lhf Exp $
209207 ** print bytecodes
210208 ** See Copyright Notice in lua.h
211209 */
@@ -253,7 +251,7 @@ static void PrintString(const TString* ts) · 41 unmodified lines
253251 static void PrintConstant(const Proto* f, int i)
254252 {
255253 const TValue* o=&f->k[i];
256− switch (ttypenv(o))
254+ switch (ttype(o))
257255 {
258256 case LUA_TNIL:
259257 printf("nil");
@@ -261,10 +259,13 @@ static void PrintConstant(const Proto* f, int i) · 1 unmodified line
261259 case LUA_TBOOLEAN:
262260 printf(bvalue(o) ? "true" : "false");
263261 break;
264− case LUA_TNUMBER:
265− printf(LUA_NUMBER_FMT,nvalue(o));
262+ case LUA_TNUMFLT:
263+ printf(LUA_NUMBER_FMT,fltvalue(o));
266264 break;
267− case LUA_TSTRING:
265+ case LUA_TNUMINT:
266+ printf(LUA_INTEGER_FMT,ivalue(o));
267+ break;
268+ case LUA_TSHRSTR: case LUA_TLNGSTR:
268269 PrintString(rawtsvalue(o));
269270 break;
270271 default: /* cannot happen */
@@ -339,8 +340,14 @@ static void PrintCode(const Proto* f) · 68 unmodified lines
339340 case OP_ADD:
340341 case OP_SUB:
341342 case OP_MUL:
342− case OP_DIV:
343343 case OP_POW:
344+ case OP_DIV:
345+ case OP_IDIV:
346+ case OP_BAND:
347+ case OP_BOR:
348+ case OP_BXOR:
349+ case OP_SHL:
350+ case OP_SHR:
344351 case OP_EQ:
345352 case OP_LT:
346353 case OP_LE:
src/luaconf.h+131−41
@@ -1,5 +1,5 @@
11 /*
2−** $Id: luaconf.h,v 1.193 2014/03/21 14:27:16 roberto Exp $
2+** $Id: luaconf.h,v 1.207 2014/06/10 19:21:20 roberto Exp $
33 ** Configuration file for Lua
44 ** See Copyright Notice in lua.h
55 */
@@ -21,16 +21,15 @@ · 15 unmodified lines
2121
2222 /*
2323 ** ===================================================================
24−@@ LUA_INT_INT / LUA_INT_LONG / LUA_INT_LONGLONG defines size for
25−@* Lua integers;
26−@@ LUA_REAL_FLOAT / LUA_REAL_DOUBLE / LUA_REAL_LONGDOUBLE defines size for
27−@* Lua floats.
24+@@ LUA_INT_INT / LUA_INT_LONG / LUA_INT_LONGLONG defines type for
25+@@ Lua integers;
26+@@ LUA_REAL_FLOAT / LUA_REAL_DOUBLE / LUA_REAL_LONGDOUBLE defines
27+@@ type for Lua floats.
2828 **
2929 ** These definitions set the numeric types for Lua. Lua should work
30−** fine with 32-bit or 64-bit integers mixed with 32-bit or 64-bit
31−** floats. The usual configurations are 64-bit integers and floats (the
32−** default) and 32-bit integers and floats (Small Lua, for restricted
33−** hardware).
30+** fine with any mix of these previous options.
31+** The usual configurations are 64-bit integers and floats (the default)
32+** and 32-bit integers and floats (Small Lua, for restricted hardware).
3433 ** =====================================================================
3534 */
3635 #define LUA_INT_LONGLONG
@@ -74,7 +73,7 @@ · 37 unmodified lines
7473
7574
7675 /*
77−@@ LUA_USE_C99 includes all functionality from C 99.
76+@@ LUA_USE_C99 includes all functionality that depends on C 99.
7877 ** CHANGE it (define it) if your system is compatible.
7978 */
8079 #if defined(LUA_USE_C99)
@@ -84,7 +83,7 @@ · 3 unmodified lines
8483
8584 /*
8685 @@ LUA_USE_POSIX includes all functionality listed as X/Open System
87−@* Interfaces Extension (XSI).
86+@@ Interfaces Extension (XSI).
8887 ** CHANGE it (define it) if your system is XSI compatible.
8988 */
9089 #if defined(LUA_USE_POSIX)
@@ -94,33 +93,37 @@ · 3 unmodified lines
9493
9594 /*
9695 @@ LUA_PATH_DEFAULT is the default path that Lua uses to look for
97−@* Lua libraries.
96+@@ Lua libraries.
9897 @@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for
99−@* C libraries.
98+@@ C libraries.
10099 ** CHANGE them if your machine has a non-conventional directory
101100 ** hierarchy or if you want to install your libraries in
102101 ** non-conventional directories.
103102 */
104−#if defined(_WIN32) /* { */
103+#define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
104+#if defined(_WIN32) /* { */
105105 /*
106106 ** In Windows, any exclamation mark ('!') in the path is replaced by the
107107 ** path of the directory of the executable file of the current process.
108108 */
109109 #define LUA_LDIR "!\\lua\\"
110110 #define LUA_CDIR "!\\"
111+#define LUA_SHRDIR "!\\..\\share\\lua\\" LUA_VDIR "\\"
111112 #define LUA_PATH_DEFAULT \
112113 LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \
113114 LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" \
115+ LUA_SHRDIR"?.lua;" LUA_SHRDIR"?\\init.lua;" \
114116 ".\\?.lua;" ".\\?\\init.lua"
115117 #define LUA_CPATH_DEFAULT \
116− LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll;" ".\\?.dll"
118+ LUA_CDIR"?.dll;" \
119+ LUA_CDIR"..\\lib\\lua\\" LUA_VDIR "\\?.dll;" \
120+ LUA_CDIR"loadall.dll;" ".\\?.dll"
117121
118122 #else /* }{ */
119123
120−#define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR "/"
121124 #define LUA_ROOT "/usr/local/"
122−#define LUA_LDIR LUA_ROOT "share/lua/" LUA_VDIR
123−#define LUA_CDIR LUA_ROOT "lib/lua/" LUA_VDIR
125+#define LUA_LDIR LUA_ROOT "share/lua/" LUA_VDIR "/"
126+#define LUA_CDIR LUA_ROOT "lib/lua/" LUA_VDIR "/"
124127 #define LUA_PATH_DEFAULT \
125128 LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \
126129 LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua;" \
@@ -181,10 +184,10 @@ · 54 unmodified lines
181184
182185 /*
183186 @@ LUAI_FUNC is a mark for all extern functions that are not to be
184−@* exported to outside modules.
187+@@ exported to outside modules.
185188 @@ LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables
186−@* that are not to be exported to outside modules (LUAI_DDEF for
187−@* definitions and LUAI_DDEC for declarations).
189+@@ that are not to be exported to outside modules (LUAI_DDEF for
190+@@ definitions and LUAI_DDEC for declarations).
188191 ** CHANGE them if you need to mark them in some special way. Elf/gcc
189192 ** (versions 3.2 and later) mark them as "hidden" to optimize access
190193 ** when Lua is compiled as a shared library. Not all elf targets support
@@ -217,7 +220,7 @@ · 26 unmodified lines
217220
218221 /*
219222 @@ LUA_IDSIZE gives the maximum size for the description of the source
220−@* of a function in debug information.
223+@@ of a function in debug information.
221224 ** CHANGE it if you want a different size.
222225 */
223226 #define LUA_IDSIZE 60
@@ -259,17 +262,38 @@ · 35 unmodified lines
259262 */
260263
261264 /*
262−@@ LUA_COMPAT_ALL controls all compatibility options.
265+@@ LUA_COMPAT_5_2 controls other macros for compatibility with Lua 5.2.
266+@@ LUA_COMPAT_5_1 controls other macros for compatibility with Lua 5.1.
263267 ** You can define it to get all options, or change specific options
264268 ** to fit your specific needs.
265269 */
266−#if defined(LUA_COMPAT_ALL) /* { */
270+#if defined(LUA_COMPAT_5_2) /* { */
271+
272+/*
273+@@ LUA_COMPAT_MATHLIB controls the presence of several deprecated
274+** functions in the mathematical library.
275+*/
276+#define LUA_COMPAT_MATHLIB
267277
268278 /*
269279 @@ LUA_COMPAT_BITLIB controls the presence of library 'bit32'.
270280 */
271281 #define LUA_COMPAT_BITLIB
272282
283+
284+/*
285+@@ LUA_COMPAT_FLOATSTRING makes Lua format integral floats without a
286+@@ a float mark ('.0').
287+** This macro is not on by default even in compatibility mode,
288+** because this is not really an incompatibility.
289+*/
290+/* #define LUA_COMPAT_FLOATSTRING */
291+
292+#endif /* } */
293+
294+
295+#if defined(LUA_COMPAT_5_1) /* { */
296+
273297 /*
274298 @@ LUA_COMPAT_UNPACK controls the presence of global 'unpack'.
275299 ** You can replace it with 'table.unpack'.
@@ -352,9 +376,9 @@ · 76 unmodified lines
352376 /*
353377 @@ LUA_INT32 is an signed integer with exactly 32 bits.
354378 @@ LUAI_UMEM is an unsigned integer big enough to count the total
355−@* memory used by Lua.
379+@@ memory used by Lua.
356380 @@ LUAI_MEM is a signed integer big enough to count the total memory
357−@* used by Lua.
381+@@ used by Lua.
358382 ** CHANGE here if for some weird reason the default definitions are not
359383 ** good enough for your machine. Probably you do not need to change
360384 ** this.
@@ -408,10 +432,9 @@ · 47 unmodified lines
408432 @@ LUA_NUMBER is the floating-point type used by Lua.
409433 **
410434 @@ LUAI_UACNUMBER is the result of an 'usual argument conversion'
411−@* over a floating number.
435+@@ over a floating number.
412436 **
413437 @@ LUA_NUMBER_FRMLEN is the length modifier for writing floats.
414−@@ LUA_NUMBER_SCAN is the format for reading floats.
415438 @@ LUA_NUMBER_FMT is the format for writing floats.
416439 @@ lua_number2str converts a float to a string.
417440 **
@@ -427,7 +450,6 @@ · 9 unmodified lines
427450 #define LUAI_UACNUMBER double
428451
429452 #define LUA_NUMBER_FRMLEN ""
430−#define LUA_NUMBER_SCAN "%f"
431453 #define LUA_NUMBER_FMT "%.7g"
432454
433455 #define l_mathop(op) op##f
@@ -442,7 +464,6 @@ · 8 unmodified lines
442464 #define LUAI_UACNUMBER long double
443465
444466 #define LUA_NUMBER_FRMLEN "L"
445−#define LUA_NUMBER_SCAN "%Lf"
446467 #define LUA_NUMBER_FMT "%.19Lg"
447468
448469 #define l_mathop(op) op##l
@@ -456,7 +477,6 @@ · 7 unmodified lines
456477 #define LUAI_UACNUMBER double
457478
458479 #define LUA_NUMBER_FRMLEN ""
459−#define LUA_NUMBER_SCAN "%lf"
460480 #define LUA_NUMBER_FMT "%.14g"
461481
462482 #define l_mathop(op) op
@@ -489,15 +509,30 @@ · 26 unmodified lines
489509 #define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
490510
491511
512+/*
513+@@ lua_numtointeger converts a float number to an integer, or
514+** returns 0 if float is not within the range of a lua_Integer.
515+** (The range comparisons are tricky because of rounding. The tests
516+** here assume a two-complement representation, where MININTEGER always
517+** has an exact representation as a float; MAXINTEGER may not have one,
518+** and therefore its conversion to float may have an ill-defined value.)
519+*/
520+#define lua_numtointeger(n,p) \
521+ ((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \
522+ (n) < -(LUA_NUMBER)(LUA_MININTEGER) && \
523+ (*(p) = (LUA_INTEGER)(n), 1))
524+
525+
492526 /*
493527 @@ The luai_num* macros define the primitive operations over numbers.
494−@* They should work for any size of floating numbers.
528+** They should work for any size of floating numbers.
495529 */
496530
497531 /* the following operations need the math library */
498532 #if defined(lobject_c) || defined(lvm_c)
499533 #include <math.h>
500−#define luai_nummod(L,a,b) ((void)L, (a) - l_floor((a)/(b))*(b))
534+#define luai_nummod(L,a,b,m) \
535+ { (m) = l_mathop(fmod)(a,b); if ((m) != 0 && (a)*(b) < 0) (m) += (b); }
501536 #define luai_numpow(L,a,b) ((void)L, l_mathop(pow)(a,b))
502537 #endif
503538
@@ -528,44 +563,99 @@ · 24 unmodified lines
528563 **
529564 @@ LUA_UNSIGNED is the unsigned version of LUA_INTEGER.
530565 **
566+@@ LUAI_UACINT is the result of an 'usual argument conversion'
567+@@ over a lUA_INTEGER.
531568 @@ LUA_INTEGER_FRMLEN is the length modifier for reading/writing integers.
532−@@ LUA_INTEGER_SCAN is the format for reading integers.
533569 @@ LUA_INTEGER_FMT is the format for writing integers.
570+@@ LUA_MAXINTEGER is the maximum value for a LUA_INTEGER.
571+@@ LUA_MININTEGER is the minimum value for a LUA_INTEGER.
572+@@ LUA_MAXUNSIGNED is the maximum value for a LUA_UNSIGNED.
534573 @@ lua_integer2str converts an integer to a string.
535574 */
536575
576+
577+/* The following definitions are good for most cases here */
578+
579+#define LUA_INTEGER_FMT "%" LUA_INTEGER_FRMLEN "d"
580+#define lua_integer2str(s,n) sprintf((s), LUA_INTEGER_FMT, (n))
581+
582+#define LUAI_UACINT LUA_INTEGER
583+
584+/*
585+** use LUAI_UACINT here to avoid problems with promotions (which
586+** can turn a comparison between unsigneds into a signed comparison)
587+*/
588+#define LUA_UNSIGNED unsigned LUAI_UACINT
589+
590+
591+/* now the variable definitions */
592+
537593 #if defined(LUA_INT_INT) /* { int */
538594
539595 #define LUA_INTEGER int
540596 #define LUA_INTEGER_FRMLEN ""
541597
598+#define LUA_MAXUNSIGNED UINT_MAX
599+#define LUA_MAXINTEGER INT_MAX
600+#define LUA_MININTEGER INT_MIN
601+
542602 #elif defined(LUA_INT_LONG) /* }{ long */
543603
544604 #define LUA_INTEGER long
545605 #define LUA_INTEGER_FRMLEN "l"
546606
607+#define LUA_MAXUNSIGNED ULONG_MAX
608+#define LUA_MAXINTEGER LONG_MAX
609+#define LUA_MININTEGER LONG_MIN
610+
547611 #elif defined(LUA_INT_LONGLONG) /* }{ long long */
548612
549613 #if defined(_WIN32)
614+
550615 #define LUA_INTEGER __int64
551616 #define LUA_INTEGER_FRMLEN "I64"
617+
618+#define LUA_MAXUNSIGNED _UI64_MAX
619+#define LUA_MAXINTEGER _I64_MAX
620+#define LUA_MININTEGER _I64_MIN
621+
552622 #else
623+
553624 #define LUA_INTEGER long long
554625 #define LUA_INTEGER_FRMLEN "ll"
626+
627+#define LUA_MAXUNSIGNED ULLONG_MAX
628+#define LUA_MAXINTEGER LLONG_MAX
629+#define LUA_MININTEGER LLONG_MIN
630+
555631 #endif
556632
557−#else /* }{ */
633+#elif defined(LUA_INT_SHORT) /* }{ short int */
634+/*
635+** this option is for tests only; it is not particularly useful and
636+** it does not pass the test suit.
637+*/
558638
559−#error "numeric integer type not defined"
639+#define LUA_INTEGER short int
640+#define LUA_INTEGER_FRMLEN ""
560641
561−#endif /* } */
642+#define LUA_MAXUNSIGNED ((LUA_UNSIGNED)USHRT_MAX)
643+#define LUA_MAXINTEGER SHRT_MAX
644+#define LUA_MININTEGER SHRT_MIN
562645
646+#undef LUAI_UACINT
647+#define LUAI_UACINT int
563648
564−#define LUA_INTEGER_SCAN "%" LUA_INTEGER_FRMLEN "d"
565−#define LUA_INTEGER_FMT "%" LUA_INTEGER_FRMLEN "d"
566−#define lua_integer2str(s,n) sprintf((s), LUA_INTEGER_FMT, (n))
649+#undef LUAI_MAXSTACK
650+#define LUAI_MAXSTACK 15000
567651
568−#define LUA_UNSIGNED unsigned LUA_INTEGER
652+#define l_castS2U(x) ((LUA_UNSIGNED)(unsigned short)(x))
653+
654+#else /* }{ */
655+
656+#error "numeric integer type not defined"
657+
658+#endif /* } */
569659
570660 /* }================================================================== */
571661
src/lundump.c+27−19
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lundump.c,v 2.34 2014/03/11 18:56:27 roberto Exp $
2+** $Id: lundump.c,v 2.40 2014/06/19 18:27:20 roberto Exp $
33 ** load precompiled Lua chunks
44 ** See Copyright Notice in lua.h
55 */
@@ -42,7 +42,7 @@ static l_noret error(LoadState *S, const char *why) { · 36 unmodified lines
4242
4343 /*
4444 ** All high-level loads go through LoadVector; you can change it to
45−** adapt to the endianess of the input
45+** adapt to the endianness of the input
4646 */
4747 #define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0]))
4848
@@ -105,12 +105,12 @@ static void LoadCode (LoadState *S, Proto *f) { · 56 unmodified lines
105105 }
106106
107107
108−static void LoadFunction(LoadState *S, Proto *f);
108+static void LoadFunction(LoadState *S, Proto *f, TString *psource);
109109
110110
111111 static void LoadConstants (LoadState *S, Proto *f) {
112− int i, n;
113− n = LoadInt(S);
112+ int i;
113+ int n = LoadInt(S);
114114 f->k = luaM_newvector(S->L, n, TValue);
115115 f->sizek = n;
116116 for (i = 0; i < n; i++)
@@ -126,7 +126,7 @@ static void LoadConstants (LoadState *S, Proto *f) { · 9 unmodified lines
126126 setbvalue(o, LoadByte(S));
127127 break;
128128 case LUA_TNUMFLT:
129− setnvalue(o, LoadNumber(S));
129+ setfltvalue(o, LoadNumber(S));
130130 break;
131131 case LUA_TNUMINT:
132132 setivalue(o, LoadInteger(S));
@@ -139,14 +139,19 @@ static void LoadConstants (LoadState *S, Proto *f) { · 6 unmodified lines
139139 lua_assert(0);
140140 }
141141 }
142− n = LoadInt(S);
142+}
143+
144+
145+static void LoadProtos (LoadState *S, Proto *f) {
146+ int i;
147+ int n = LoadInt(S);
143148 f->p = luaM_newvector(S->L, n, Proto *);
144149 f->sizep = n;
145150 for (i = 0; i < n; i++)
146151 f->p[i] = NULL;
147152 for (i = 0; i < n; i++) {
148153 f->p[i] = luaF_newproto(S->L);
149− LoadFunction(S, f->p[i]);
154+ LoadFunction(S, f->p[i], f->source);
150155 }
151156 }
152157
@@ -167,7 +172,6 @@ static void LoadUpvalues (LoadState *S, Proto *f) { · 14 unmodified lines
167172
168173 static void LoadDebug (LoadState *S, Proto *f) {
169174 int i, n;
170− f->source = LoadString(S);
171175 n = LoadInt(S);
172176 f->lineinfo = luaM_newvector(S->L, n, int);
173177 f->sizelineinfo = n;
@@ -188,7 +192,10 @@ static void LoadDebug (LoadState *S, Proto *f) { · 14 unmodified lines
188192 }
189193
190194
191−static void LoadFunction (LoadState *S, Proto *f) {
195+static void LoadFunction (LoadState *S, Proto *f, TString *psource) {
196+ f->source = LoadString(S);
197+ if (f->source == NULL) /* no source in dump? */
198+ f->source = psource; /* reuse parent's source */
192199 f->linedefined = LoadInt(S);
193200 f->lastlinedefined = LoadInt(S);
194201 f->numparams = LoadByte(S);
@@ -197,13 +204,14 @@ static void LoadFunction (LoadState *S, Proto *f) { · 2 unmodified lines
197204 LoadCode(S, f);
198205 LoadConstants(S, f);
199206 LoadUpvalues(S, f);
207+ LoadProtos(S, f);
200208 LoadDebug(S, f);
201209 }
202210
203211
204212 static void checkliteral (LoadState *S, const char *s, const char *msg) {
205213 char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */
206− int len = strlen(s);
214+ size_t len = strlen(s);
207215 LoadVector(S, buff, len);
208216 if (memcmp(s, buff, len) != 0)
209217 error(S, msg);
@@ -231,7 +239,7 @@ static void checkHeader (LoadState *S) { · 21 unmodified lines
231239 checksize(S, lua_Integer);
232240 checksize(S, lua_Number);
233241 if (LoadInteger(S) != LUAC_INT)
234− error(S, "endianess mismatch in");
242+ error(S, "endianness mismatch in");
235243 if (LoadNumber(S) != LUAC_NUM)
236244 error(S, "float format mismatch in");
237245 }
@@ -240,10 +248,10 @@ static void checkHeader (LoadState *S) { · 2 unmodified lines
240248 /*
241249 ** load precompiled chunk
242250 */
243−Closure *luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff,
244− const char *name) {
251+LClosure *luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff,
252+ const char *name) {
245253 LoadState S;
246− Closure *cl;
254+ LClosure *cl;
247255 if (*name == '@' || *name == '=')
248256 S.name = name + 1;
249257 else if (*name == LUA_SIGNATURE[0])
@@ -257,10 +265,10 @@ Closure *luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff, · 7 unmodified lines
257265 cl = luaF_newLclosure(L, LoadByte(&S));
258266 setclLvalue(L, L->top, cl);
259267 incr_top(L);
260− cl->l.p = luaF_newproto(L);
261− LoadFunction(&S, cl->l.p);
262− lua_assert(cl->l.nupvalues == cl->l.p->sizeupvalues);
263− luai_verifycode(L, buff, cl->l.p);
268+ cl->p = luaF_newproto(L);
269+ LoadFunction(&S, cl->p, NULL);
270+ lua_assert(cl->nupvalues == cl->p->sizeupvalues);
271+ luai_verifycode(L, buff, cl->p);
264272 return cl;
265273 }
266274
src/lundump.h+4−4
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lundump.h,v 1.42 2014/03/11 14:22:54 roberto Exp $
2+** $Id: lundump.h,v 1.44 2014/06/19 18:27:20 roberto Exp $
33 ** load precompiled Lua chunks
44 ** See Copyright Notice in lua.h
55 */
@@ -15,7 +15,7 @@ · 9 unmodified lines
1515 /* data to catch conversion errors */
1616 #define LUAC_DATA "\x19\x93\r\n\x1a\n"
1717
18−#define LUAC_INT cast_integer(0xABCD)
18+#define LUAC_INT 0x5678
1919 #define LUAC_NUM cast_num(370.5)
2020
2121 #define MYINT(s) (s[0]-'0')
@@ -23,8 +23,8 @@ · 1 unmodified line
2323 #define LUAC_FORMAT 0 /* this is the official format */
2424
2525 /* load one chunk; from lundump.c */
26−LUAI_FUNC Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff,
27− const char* name);
26+LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff,
27+ const char* name);
2828
2929 /* dump one chunk; from ldump.c */
3030 LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w,
src/lutf8lib.c+49−38
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lutf8lib.c,v 1.4 2014/03/20 19:36:02 roberto Exp $
2+** $Id: lutf8lib.c,v 1.9 2014/05/14 18:33:37 roberto Exp $
33 ** Standard library for UTF-8 manipulation
44 ** See Copyright Notice in lua.h
55 */
@@ -32,7 +32,7 @@ static lua_Integer u_posrelat (lua_Integer pos, size_t len) { · 26 unmodified lines
3232
3333
3434 /*
35−** Decode an UTF-8 sequence, returning NULL if byte sequence is invalid.
35+** Decode one UTF-8 sequence, returning NULL if byte sequence is invalid.
3636 */
3737 static const char *utf8_decode (const char *o, int *val) {
3838 static unsigned int limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFF};
@@ -61,32 +61,38 @@ static const char *utf8_decode (const char *o, int *val) { · 22 unmodified lines
6161
6262
6363 /*
64−** utf8len(s, [i]) --> number of codepoints in 's' after 'i';
65−** nil if 's' not well formed
64+** utf8len(s [, i [, j]]) --> number of characters that start in the
65+** range [i,j], or nil + current position if 's' is not well formed in
66+** that interval
6667 */
6768 static int utflen (lua_State *L) {
6869 int n = 0;
69− const char *ends;
7070 size_t len;
7171 const char *s = luaL_checklstring(L, 1, &len);
72− lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), 1);
73− luaL_argcheck(L, 1 <= posi && posi <= (lua_Integer)len, 1,
72+ lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
73+ lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len);
74+ luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2,
7475 "initial position out of string");
75− ends = s + len;
76− s += posi - 1;
77− while (s < ends && (s = utf8_decode(s, NULL)) != NULL)
76+ luaL_argcheck(L, --posj < (lua_Integer)len, 3,
77+ "final position out of string");
78+ while (posi <= posj) {
79+ const char *s1 = utf8_decode(s + posi, NULL);
80+ if (s1 == NULL) { /* conversion error? */
81+ lua_pushnil(L); /* return nil ... */
82+ lua_pushinteger(L, posi + 1); /* ... and current position */
83+ return 2;
84+ }
85+ posi = s1 - s;
7886 n++;
79− if (s == ends)
80− lua_pushinteger(L, n);
81− else
82− lua_pushnil(L);
87+ }
88+ lua_pushinteger(L, n);
8389 return 1;
8490 }
8591
8692
8793 /*
8894 ** codepoint(s, [i, [j]]) -> returns codepoints for all characters
89−** between i and j
95+** that start in the range [i,j]
9096 */
9197 static int codepoint (lua_State *L) {
9298 size_t len;
@@ -145,41 +151,46 @@ static int utfchar (lua_State *L) { · 52 unmodified lines
145151
146152
147153 /*
148−** offset(s, n, [i]) -> index where n-th character *after*
154+** offset(s, n, [i]) -> index where n-th character counting from
149155 ** position 'i' starts; 0 means character at 'i'.
150156 */
151157 static int byteoffset (lua_State *L) {
152158 size_t len;
153159 const char *s = luaL_checklstring(L, 1, &len);
154160 int n = luaL_checkint(L, 2);
155− lua_Integer posi = u_posrelat(luaL_optinteger(L, 3, 1), len) - 1;
156− luaL_argcheck(L, 0 <= posi && posi <= (lua_Integer)len, 3,
161+ lua_Integer posi = (n >= 0) ? 1 : len + 1;
162+ posi = u_posrelat(luaL_optinteger(L, 3, posi), len);
163+ luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3,
157164 "position out of range");
158165 if (n == 0) {
159166 /* find beginning of current byte sequence */
160167 while (posi > 0 && iscont(s + posi)) posi--;
161168 }
162− else if (n < 0) {
163− while (n < 0 && posi > 0) { /* move back */
164− do { /* find beginning of previous character */
165− posi--;
166− } while (posi > 0 && iscont(s + posi));
167− n++;
168− }
169− }
170169 else {
171− n--; /* do not move for 1st character */
172− while (n > 0 && posi < (lua_Integer)len) {
173− do { /* find beginning of next character */
174− posi++;
175− } while (iscont(s + posi)); /* ('\0' is not continuation) */
176− n--;
177− }
170+ if (iscont(s + posi))
171+ luaL_error(L, "initial position is a continuation byte");
172+ if (n < 0) {
173+ while (n < 0 && posi > 0) { /* move back */
174+ do { /* find beginning of previous character */
175+ posi--;
176+ } while (posi > 0 && iscont(s + posi));
177+ n++;
178+ }
179+ }
180+ else {
181+ n--; /* do not move for 1st character */
182+ while (n > 0 && posi < (lua_Integer)len) {
183+ do { /* find beginning of next character */
184+ posi++;
185+ } while (iscont(s + posi)); /* (cannot pass final '\0') */
186+ n--;
187+ }
188+ }
178189 }
179− if (n == 0)
190+ if (n == 0) /* did it find given character? */
180191 lua_pushinteger(L, posi + 1);
181− else
182− lua_pushnil(L); /* no such position */
192+ else /* no such character */
193+ lua_pushnil(L);
183194 return 1;
184195 }
185196
@@ -187,7 +198,7 @@ static int byteoffset (lua_State *L) { · 1 unmodified line
187198 static int iter_aux (lua_State *L) {
188199 size_t len;
189200 const char *s = luaL_checklstring(L, 1, &len);
190− int n = lua_tointeger(L, 2) - 1;
201+ lua_Integer n = lua_tointeger(L, 2) - 1;
191202 if (n < 0) /* first iteration? */
192203 n = 0; /* start from here */
193204 else if (n < (lua_Integer)len) {
@@ -231,7 +242,7 @@ static struct luaL_Reg funcs[] = { · 37 unmodified lines
231242 };
232243
233244
234−int luaopen_utf8 (lua_State *L) {
245+LUAMOD_API int luaopen_utf8 (lua_State *L) {
235246 luaL_newlib(L, funcs);
236247 lua_pushliteral(L, UTF8PATT);
237248 lua_setfield(L, -2, "charpatt");
src/lvm.c+251−127
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lvm.c,v 2.190 2014/03/15 12:29:48 roberto Exp $
2+** $Id: lvm.c,v 2.216 2014/06/19 18:27:20 roberto Exp $
33 ** Lua virtual machine
44 ** See Copyright Notice in lua.h
55 */
@@ -37,25 +37,100 @@ · 31 unmodified lines
3737 #define MAXNUMBER2STR 50
3838
3939
40+/*
41+** Similar to 'tonumber', but does not attempt to convert strings and
42+** ensure correct precision (no extra bits). Used in comparisons.
43+*/
44+static int tofloat (const TValue *obj, lua_Number *n) {
45+ if (ttisfloat(obj)) *n = fltvalue(obj);
46+ else if (ttisinteger(obj)) {
47+ volatile lua_Number x = cast_num(ivalue(obj)); /* avoid extra precision */
48+ *n = x;
49+ }
50+ else {
51+ *n = 0; /* to avoid warnings */
52+ return 0;
53+ }
54+ return 1;
55+}
56+
57+
58+/*
59+** Try to convert a value to a float. Check 'isinteger' first, because
60+** in general the float case is already handled by the macro 'tonumber'.
61+*/
4062 int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
41− lua_assert(!ttisfloat(obj));
63+ TValue v;
64+ again:
4265 if (ttisinteger(obj)) {
4366 *n = cast_num(ivalue(obj));
4467 return 1;
4568 }
46− else
47− return (ttisstring(obj) && luaO_str2d(svalue(obj), tsvalue(obj)->len, n));
69+ else if (ttisfloat(obj)) {
70+ *n = fltvalue(obj);
71+ return 1;
72+ }
73+ else if (ttisstring(obj) && /* string convertible to number? */
74+ luaO_str2num(svalue(obj), &v) == tsvalue(obj)->len + 1) {
75+ obj = &v;
76+ goto again; /* convert result from 'luaO_str2num' to a float */
77+ }
78+ return 0; /* conversion failed */
79+}
80+
81+
82+/*
83+** try to convert a value to an integer, rounding up if 'up' is true
84+*/
85+static int tointeger_aux (const TValue *obj, lua_Integer *p, int up) {
86+ TValue v;
87+ again:
88+ if (ttisfloat(obj)) {
89+ lua_Number n = fltvalue(obj);
90+ n = (up ? -l_floor(-n) : l_floor(n));
91+ return lua_numtointeger(n, p);
92+ }
93+ else if (ttisinteger(obj)) {
94+ *p = ivalue(obj);
95+ return 1;
96+ }
97+ else if (ttisstring(obj) &&
98+ luaO_str2num(svalue(obj), &v) == tsvalue(obj)->len + 1) {
99+ obj = &v;
100+ goto again; /* convert result from 'luaO_str2num' to an integer */
101+ }
102+ return 0; /* conversion failed */
48103 }
49104
50105
106+/*
107+** try to convert a non-integer value to an integer, rounding down
108+*/
109+int luaV_tointeger_ (const TValue *obj, lua_Integer *p) {
110+ return tointeger_aux(obj, p, 0);
111+}
112+
113+
114+/*
115+** Convert a number object to a string
116+*/
51117 int luaV_tostring (lua_State *L, StkId obj) {
52118 if (!ttisnumber(obj))
53119 return 0;
54120 else {
55121 char buff[MAXNUMBER2STR];
56− size_t len = (ttisinteger(obj))
57− ? lua_integer2str(buff, ivalue(obj))
58− : lua_number2str(buff, fltvalue(obj));
122+ size_t len;
123+ if (ttisinteger(obj))
124+ len = lua_integer2str(buff, ivalue(obj));
125+ else {
126+ len = lua_number2str(buff, fltvalue(obj));
127+#if !defined(LUA_COMPAT_FLOATSTRING)
128+ if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */
129+ buff[len++] = '.';
130+ buff[len++] = '0'; /* adds '.0' to result */
131+ }
132+#endif
133+ }
59134 setsvalue2s(L, obj, luaS_newlstr(L, buff, len));
60135 return 1;
61136 }
@@ -63,37 +138,46 @@ int luaV_tostring (lua_State *L, StkId obj) { · 1 unmodified line
63138
64139
65140 /*
66−** Check whether a float number is within the range of a lua_Integer.
67−** (The comparisons are tricky because of rounding, which can or
68−** not occur depending on the relative sizes of floats and integers.)
69−** This function should be called only when 'n' has an integral value.
141+** Try to convert a 'for' limit to an integer, preserving the
142+** semantics of the loop.
143+** (The following explanation assumes a non-negative step; it is valid
144+** for negative steps mutatis mutandis.)
145+** If the limit can be converted to an integer, rounding down, that is
146+** it.
147+** Otherwise, check whether the limit can be converted to a number. If
148+** the number is too large, it is OK to set the limit as LUA_MAXINTEGER,
149+** which means no limit. If the number is too negative, the loop
150+** should not run, because any initial integer value is larger than the
151+** limit. So, it sets the limit to LUA_MININTEGER. 'stopnow' corrects
152+** the extreme case when the initial value is LUA_MININTEGER, in which
153+** case the LUA_MININTEGER limit would still run the loop once.
70154 */
71−int luaV_numtointeger (lua_Number n, lua_Integer *p) {
72− if (cast_num(MIN_INTEGER) <= n && n < (MAX_INTEGER + cast_num(1))) {
73− *p = cast_integer(n);
74− lua_assert(cast_num(*p) == n);
75− return 1;
155+static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step,
156+ int *stopnow) {
157+ *stopnow = 0; /* usually, let loops run */
158+ if (!tointeger_aux(obj, p, (step < 0))) { /* does not fit in integer? */
159+ lua_Number n; /* try to convert to float */
160+ if (!tonumber(obj, &n)) /* cannot convert to float? */
161+ return 0; /* not a number */
162+ if (n > 0) { /* if true, float is larger than max integer */
163+ *p = LUA_MAXINTEGER;
164+ if (step < 0) *stopnow = 1;
165+ }
166+ else { /* float is smaller than min integer */
167+ *p = LUA_MININTEGER;
168+ if (step >= 0) *stopnow = 1;
169+ }
76170 }
77− return 0; /* number is outside integer limits */
171+ return 1;
78172 }
79173
80174
81175 /*
82−** try to convert a non-integer value to an integer
176+** Main function for table access (invoking metamethods if needed).
177+** Compute 'val = t[key]'
83178 */
84−int luaV_tointeger_ (const TValue *obj, lua_Integer *p) {
85− lua_Number n;
86− lua_assert(!ttisinteger(obj));
87− if (tonumber(obj, &n)) {
88− n = l_floor(n);
89− return luaV_numtointeger(n, p);
90− }
91− else return 0;
92−}
93−
94−
95179 void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
96− int loop;
180+ int loop; /* counter to avoid infinite loops */
97181 for (loop = 0; loop < MAXTAGLOOP; loop++) {
98182 const TValue *tm;
99183 if (ttistable(t)) { /* `t' is a table? */
@@ -101,32 +185,36 @@ void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) { · 1 unmodified line
101185 const TValue *res = luaH_get(h, key); /* do a primitive get */
102186 if (!ttisnil(res) || /* result is not nil? */
103187 (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */
104− setobj2s(L, val, res);
188+ setobj2s(L, val, res); /* result is the raw get */
105189 return;
106190 }
107− /* else will try the tag method */
191+ /* else will try metamethod */
108192 }
109193 else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))
110− luaG_typeerror(L, t, "index");
111− if (ttisfunction(tm)) {
194+ luaG_typeerror(L, t, "index"); /* no metamethod */
195+ if (ttisfunction(tm)) { /* metamethod is a function */
112196 luaT_callTM(L, tm, t, key, val, 1);
113197 return;
114198 }
115− t = tm; /* else repeat with 'tm' */
199+ t = tm; /* else repeat access over 'tm' */
116200 }
117201 luaG_runerror(L, "gettable chain too long; possible loop");
118202 }
119203
120204
205+/*
206+** Main function for table assignment (invoking metamethods if needed).
207+** Compute 't[key] = val'
208+*/
121209 void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {
122− int loop;
210+ int loop; /* counter to avoid infinite loops */
123211 for (loop = 0; loop < MAXTAGLOOP; loop++) {
124212 const TValue *tm;
125213 if (ttistable(t)) { /* `t' is a table? */
126214 Table *h = hvalue(t);
127215 TValue *oldval = cast(TValue *, luaH_get(h, key));
128216 /* if previous value is not nil, there must be a previous entry
129− in the table; moreover, a metamethod has no relevance */
217+ in the table; a metamethod has no relevance */
130218 if (!ttisnil(oldval) ||
131219 /* previous value is nil; must check the metamethod */
132220 ((tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL &&
@@ -146,32 +234,40 @@ void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) { · 13 unmodified lines
146234 else /* not a table; check metamethod */
147235 if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
148236 luaG_typeerror(L, t, "index");
149− /* there is a metamethod */
237+ /* try the metamethod */
150238 if (ttisfunction(tm)) {
151239 luaT_callTM(L, tm, t, key, val, 0);
152240 return;
153241 }
154− t = tm; /* else repeat with 'tm' */
242+ t = tm; /* else repeat assginment over 'tm' */
155243 }
156244 luaG_runerror(L, "settable chain too long; possible loop");
157245 }
158246
159247
248+/*
249+** Compare two strings 'ls' x 'rs', returning an integer smaller-equal-
250+** -larger than zero if 'ls' is smaller-equal-larger than 'rs'.
251+** The code is a little tricky because it allows '\0' in the strings
252+** and it uses 'strcoll' (to respect locales) for each segments
253+** of the strings.
254+*/
160255 static int l_strcmp (const TString *ls, const TString *rs) {
161256 const char *l = getstr(ls);
162257 size_t ll = ls->tsv.len;
163258 const char *r = getstr(rs);
164259 size_t lr = rs->tsv.len;
165− for (;;) {
260+ for (;;) { /* for each segment */
166261 int temp = strcoll(l, r);
167− if (temp != 0) return temp;
168− else { /* strings are equal up to a `\0' */
262+ if (temp != 0) /* not equal? */
263+ return temp; /* done */
264+ else { /* strings are equal up to a '\0' */
169265 size_t len = strlen(l); /* index of first `\0' in both strings */
170− if (len == lr) /* r is finished? */
171− return (len == ll) ? 0 : 1;
172− else if (len == ll) /* l is finished? */
173− return -1; /* l is smaller than r (because r is not finished) */
174− /* both strings longer than `len'; go on comparing (after the `\0') */
266+ if (len == lr) /* 'rs' is finished? */
267+ return (len == ll) ? 0 : 1; /* check 'ls' */
268+ else if (len == ll) /* 'ls' is finished? */
269+ return -1; /* 'ls' is smaller than 'rs' ('rs' is not finished) */
270+ /* both strings longer than `len'; go on comparing after the '\0' */
175271 len++;
176272 l += len; ll -= len; r += len; lr -= len;
177273 }
@@ -179,29 +275,35 @@ static int l_strcmp (const TString *ls, const TString *rs) { · 1 unmodified line
179275 }
180276
181277
278+/*
279+** Main operation less than; return 'l < r'.
280+*/
182281 int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
183282 int res;
184283 lua_Number nl, nr;
185− if (ttisinteger(l) && ttisinteger(r))
284+ if (ttisinteger(l) && ttisinteger(r)) /* both operands are integers? */
186285 return (ivalue(l) < ivalue(r));
187− else if (tonumber(l, &nl) && tonumber(r, &nr))
286+ else if (tofloat(l, &nl) && tofloat(r, &nr)) /* both are numbers? */
188287 return luai_numlt(nl, nr);
189− else if (ttisstring(l) && ttisstring(r))
288+ else if (ttisstring(l) && ttisstring(r)) /* both are strings? */
190289 return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
191− else if ((res = luaT_callorderTM(L, l, r, TM_LT)) < 0)
192− luaG_ordererror(L, l, r);
290+ else if ((res = luaT_callorderTM(L, l, r, TM_LT)) < 0) /* no metamethod? */
291+ luaG_ordererror(L, l, r); /* error */
193292 return res;
194293 }
195294
196295
296+/*
297+** Main operation less than or equal to; return 'l <= r'.
298+*/
197299 int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) {
198300 int res;
199301 lua_Number nl, nr;
200− if (ttisinteger(l) && ttisinteger(r))
302+ if (ttisinteger(l) && ttisinteger(r)) /* both operands are integers? */
201303 return (ivalue(l) <= ivalue(r));
202− else if (tonumber(l, &nl) && tonumber(r, &nr))
304+ else if (tofloat(l, &nl) && tofloat(r, &nr)) /* both are numbers? */
203305 return luai_numle(nl, nr);
204− else if (ttisstring(l) && ttisstring(r))
306+ else if (ttisstring(l) && ttisstring(r)) /* both are strings? */
205307 return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
206308 else if ((res = luaT_callorderTM(L, l, r, TM_LE)) >= 0) /* first try `le' */
207309 return res;
@@ -212,17 +314,18 @@ int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) { · 4 unmodified lines
212314
213315
214316 /*
215−** equality of Lua values. L == NULL means raw equality (no metamethods)
317+** Main operation for equality of Lua values; return 't1 == t2'.
318+** L == NULL means raw equality (no metamethods)
216319 */
217320 int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) {
218321 const TValue *tm;
219− if (ttype(t1) != ttype(t2)) {
322+ if (ttype(t1) != ttype(t2)) { /* not the same variant? */
220323 if (ttnov(t1) != ttnov(t2) || ttnov(t1) != LUA_TNUMBER)
221324 return 0; /* only numbers can be equal with different variants */
222325 else { /* two numbers with different variants */
223− lua_Number n1, n2;
326+ lua_Number n1, n2; /* compare them as floats */
224327 lua_assert(ttisnumber(t1) && ttisnumber(t2));
225− cast_void(tonumber(t1, &n1)); cast_void(tonumber(t2, &n2));
328+ cast_void(tofloat(t1, &n1)); cast_void(tofloat(t2, &n2));
226329 return luai_numeq(n1, n2);
227330 }
228331 }
@@ -239,24 +342,36 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { · 10 unmodified lines
239342 case LUA_TUSERDATA: {
240343 if (uvalue(t1) == uvalue(t2)) return 1;
241344 else if (L == NULL) return 0;
242− tm = luaT_getequalTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable);
345+ tm = fasttm(L, uvalue(t1)->metatable, TM_EQ);
346+ if (tm == NULL)
347+ tm = fasttm(L, uvalue(t2)->metatable, TM_EQ);
243348 break; /* will try TM */
244349 }
245350 case LUA_TTABLE: {
246351 if (hvalue(t1) == hvalue(t2)) return 1;
247352 else if (L == NULL) return 0;
248− tm = luaT_getequalTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable);
353+ tm = fasttm(L, hvalue(t1)->metatable, TM_EQ);
354+ if (tm == NULL)
355+ tm = fasttm(L, hvalue(t2)->metatable, TM_EQ);
249356 break; /* will try TM */
250357 }
251358 default:
252359 return gcvalue(t1) == gcvalue(t2);
253360 }
254− if (tm == NULL) return 0; /* no TM? */
361+ if (tm == NULL) /* no TM? */
362+ return 0; /* objects are different */
255363 luaT_callTM(L, tm, t1, t2, L->top, 1); /* call TM */
256364 return !l_isfalse(L->top);
257365 }
258366
259367
368+/* macro used by 'luaV_concat' to ensure that element at 'o' is a string */
369+#define tostring(L,o) (ttisstring(o) || (luaV_tostring(L, o)))
370+
371+/*
372+** Main operation for concatenation: concat 'total' values in the stack,
373+** from 'L->top - total' up to 'L->top - 1'.
374+*/
260375 void luaV_concat (lua_State *L, int total) {
261376 lua_assert(total >= 2);
262377 do {
@@ -284,12 +399,12 @@ void luaV_concat (lua_State *L, int total) { · 21 unmodified lines
284399 buffer = luaZ_openspace(L, &G(L)->buff, tl);
285400 tl = 0;
286401 n = i;
287− do { /* concat all strings */
402+ do { /* copy all strings to buffer */
288403 size_t l = tsvalue(top-i)->len;
289404 memcpy(buffer+tl, svalue(top-i), l * sizeof(char));
290405 tl += l;
291406 } while (--i > 0);
292− setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
407+ setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl)); /* create result */
293408 }
294409 total -= n-1; /* got 'n' strings to create 1 new */
295410 L->top -= n-1; /* popped 'n' strings and pushed one */
@@ -297,6 +412,9 @@ void luaV_concat (lua_State *L, int total) { · 1 unmodified line
297412 }
298413
299414
415+/*
416+** Main operation 'ra' = #rb'.
417+*/
300418 void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
301419 const TValue *tm;
302420 switch (ttnov(rb)) {
@@ -322,15 +440,19 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { · 19 unmodified lines
322440 }
323441
324442
325−lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y) {
326− if (cast_unsigned(y) + 1 <= 1U) { /* special cases: -1 or 0 */
327− if (y == 0)
443+/*
444+** Integer division; return 'm // n'. (Assume that C division with
445+** negative operands follows C99 behavior.)
446+*/
447+lua_Integer luaV_div (lua_State *L, lua_Integer m, lua_Integer n) {
448+ if (l_castS2U(n) + 1u <= 1u) { /* special cases: -1 or 0 */
449+ if (n == 0)
328450 luaG_runerror(L, "attempt to divide by zero");
329− return intop(-, 0, x); /* y==-1; avoid overflow with 0x80000...//-1 */
451+ return intop(-, 0, m); /* n==-1; avoid overflow with 0x80000...//-1 */
330452 }
331453 else {
332− lua_Integer d = x / y; /* perform division */
333− if ((x ^ y) >= 0 || x % y == 0) /* same signal or no rest? */
454+ lua_Integer d = m / n; /* perform division */
455+ if ((m ^ n) >= 0 || m % n == 0) /* same signal or no rest? */
334456 return d;
335457 else
336458 return d - 1; /* correct 'div' for negative case */
@@ -338,36 +460,22 @@ lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y) { · 1 unmodified line
338460 }
339461
340462
341−lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y) {
342− if (cast_unsigned(y) + 1 <= 1U) { /* special cases: -1 or 0 */
343− if (y == 0)
463+/*
464+** Integer modulus; return 'm % n'. (Assume that C '%' with
465+** negative operands follows C99 behavior.)
466+*/
467+lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) {
468+ if (l_castS2U(n) + 1u <= 1u) { /* special cases: -1 or 0 */
469+ if (n == 0)
344470 luaG_runerror(L, "attempt to perform 'n%%0'");
345− return 0; /* y==-1; avoid overflow with 0x80000...%-1 */
471+ return 0; /* m % -1 == 0; avoid overflow with 0x80000...%-1 */
346472 }
347473 else {
348− lua_Integer r = x % y;
349− if (r == 0 || (x ^ y) >= 0)
474+ lua_Integer r = m % n;
475+ if (r == 0 || (m ^ n) >= 0) /* no rest or same signal? */
350476 return r;
351477 else
352− return r + y; /* correct 'mod' for negative case */
353− }
354−}
355−
356−
357−lua_Integer luaV_pow (lua_State *L, lua_Integer x, lua_Integer y) {
358− if (y <= 0) { /* special cases: 0 or negative exponent */
359− if (y < 0)
360− luaG_runerror(L, "integer exponentiation with negative exponent");
361− return 1; /* x^0 == 1 */
362− }
363− else {
364− lua_Integer r = 1;
365− for (; y > 1; y >>= 1) {
366− if (y & 1) r = intop(*, r, x);
367− x = intop(*, x, x);
368− }
369− r = intop(*, r, x);
370− return r;
478+ return r + n; /* correct 'mod' for negative case */
371479 }
372480 }
373481
@@ -375,14 +483,17 @@ lua_Integer luaV_pow (lua_State *L, lua_Integer x, lua_Integer y) { · 1 unmodified line
375483 /* number of bits in an integer */
376484 #define NBITS cast_int(sizeof(lua_Integer) * CHAR_BIT)
377485
378−LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) {
486+/*
487+** Shift left operation. (Shift right just negates 'y'.)
488+*/
489+lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) {
379490 if (y < 0) { /* shift right? */
380491 if (y <= -NBITS) return 0;
381− else return cast_integer(cast_unsigned(x) >> (-y));
492+ else return intop(>>, x, -y);
382493 }
383494 else { /* shift left */
384495 if (y >= NBITS) return 0;
385− else return x << y;
496+ else return intop(<<, x, y);
386497 }
387498 }
388499
@@ -392,15 +503,15 @@ LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) { · 3 unmodified lines
392503 ** whether there is a cached closure with the same upvalues needed by
393504 ** new closure to be created.
394505 */
395−static Closure *getcached (Proto *p, UpVal **encup, StkId base) {
396− Closure *c = p->cache;
506+static LClosure *getcached (Proto *p, UpVal **encup, StkId base) {
507+ LClosure *c = p->cache;
397508 if (c != NULL) { /* is there a cached closure? */
398509 int nup = p->sizeupvalues;
399510 Upvaldesc *uv = p->upvalues;
400511 int i;
401512 for (i = 0; i < nup; i++) { /* check whether it has right upvalues */
402513 TValue *v = uv[i].instack ? base + uv[i].idx : encup[uv[i].idx]->v;
403− if (c->l.upvals[i]->v != v)
514+ if (c->upvals[i]->v != v)
404515 return NULL; /* wrong upvalue; cannot reuse closure */
405516 }
406517 }
@@ -419,15 +530,15 @@ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, · 12 unmodified lines
419530 int nup = p->sizeupvalues;
420531 Upvaldesc *uv = p->upvalues;
421532 int i;
422− Closure *ncl = luaF_newLclosure(L, nup);
423− ncl->l.p = p;
533+ LClosure *ncl = luaF_newLclosure(L, nup);
534+ ncl->p = p;
424535 setclLvalue(L, ra, ncl); /* anchor new closure in stack */
425536 for (i = 0; i < nup; i++) { /* fill in its upvalues */
426537 if (uv[i].instack) /* upvalue refers to local variable? */
427− ncl->l.upvals[i] = luaF_findupval(L, base + uv[i].idx);
538+ ncl->upvals[i] = luaF_findupval(L, base + uv[i].idx);
428539 else /* get upvalue from enclosing function */
429− ncl->l.upvals[i] = encup[uv[i].idx];
430− ncl->l.upvals[i]->refcount++;
540+ ncl->upvals[i] = encup[uv[i].idx];
541+ ncl->upvals[i]->refcount++;
431542 /* new closure is white, so we do not need a barrier here */
432543 }
433544 if (!isblack(obj2gco(p))) /* cache will not break GC invariant? */
@@ -497,6 +608,14 @@ void luaV_finishOp (lua_State *L) { · 63 unmodified lines
497608
498609
499610
611+
612+/*
613+** {==================================================================
614+** Function 'luaV_execute': main interpreter loop
615+** ===================================================================
616+*/
617+
618+
500619 /*
501620 ** some macros for common tasks in `luaV_execute'
502621 */
@@ -633,7 +752,7 @@ void luaV_execute (lua_State *L) { · 130 unmodified lines
633752 setivalue(ra, intop(+, ib, ic));
634753 }
635754 else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
636− setnvalue(ra, luai_numadd(L, nb, nc));
755+ setfltvalue(ra, luai_numadd(L, nb, nc));
637756 }
638757 else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_ADD)); }
639758 )
@@ -646,7 +765,7 @@ void luaV_execute (lua_State *L) { · 6 unmodified lines
646765 setivalue(ra, intop(-, ib, ic));
647766 }
648767 else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
649− setnvalue(ra, luai_numsub(L, nb, nc));
768+ setfltvalue(ra, luai_numsub(L, nb, nc));
650769 }
651770 else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SUB)); }
652771 )
@@ -659,7 +778,7 @@ void luaV_execute (lua_State *L) { · 6 unmodified lines
659778 setivalue(ra, intop(*, ib, ic));
660779 }
661780 else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
662− setnvalue(ra, luai_nummul(L, nb, nc));
781+ setfltvalue(ra, luai_nummul(L, nb, nc));
663782 }
664783 else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MUL)); }
665784 )
@@ -668,7 +787,7 @@ void luaV_execute (lua_State *L) { · 2 unmodified lines
668787 TValue *rc = RKC(i);
669788 lua_Number nb; lua_Number nc;
670789 if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
671− setnvalue(ra, luai_numdiv(L, nb, nc));
790+ setfltvalue(ra, luai_numdiv(L, nb, nc));
672791 }
673792 else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV)); }
674793 )
@@ -735,7 +854,9 @@ void luaV_execute (lua_State *L) { · 60 unmodified lines
735854 setivalue(ra, luaV_mod(L, ib, ic));
736855 }
737856 else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
738− setnvalue(ra, luai_nummod(L, nb, nc));
857+ lua_Number m;
858+ luai_nummod(L, nb, nc, m);
859+ setfltvalue(ra, m);
739860 }
740861 else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MOD)); }
741862 )
@@ -743,12 +864,8 @@ void luaV_execute (lua_State *L) { · 1 unmodified line
743864 TValue *rb = RKB(i);
744865 TValue *rc = RKC(i);
745866 lua_Number nb; lua_Number nc;
746− if (ttisinteger(rb) && ttisinteger(rc)) {
747− lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
748− setivalue(ra, luaV_pow(L, ib, ic));
749− }
750− else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
751− setnvalue(ra, luai_numpow(L, nb, nc));
867+ if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
868+ setfltvalue(ra, luai_numpow(L, nb, nc));
752869 }
753870 else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_POW)); }
754871 )
@@ -760,7 +877,7 @@ void luaV_execute (lua_State *L) { · 5 unmodified lines
760877 setivalue(ra, intop(-, 0, ib));
761878 }
762879 else if (tonumber(rb, &nb)) {
763− setnvalue(ra, luai_numunm(L, nb));
880+ setfltvalue(ra, luai_numunm(L, nb));
764881 }
765882 else {
766883 Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM));
@@ -770,7 +887,7 @@ void luaV_execute (lua_State *L) { · 3 unmodified lines
770887 TValue *rb = RB(i);
771888 lua_Integer ib;
772889 if (tointeger(rb, &ib)) {
773− setivalue(ra, intop(^, cast_integer(-1), ib));
890+ setivalue(ra, intop(^, ~l_castS2U(0), ib));
774891 }
775892 else {
776893 Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT));
@@ -916,8 +1033,8 @@ void luaV_execute (lua_State *L) { · 139 unmodified lines
9161033 if (luai_numlt(0, step) ? luai_numle(idx, limit)
9171034 : luai_numle(limit, idx)) {
9181035 ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
919− setnvalue(ra, idx); /* update internal index... */
920− setnvalue(ra + 3, idx); /* ...and external index */
1036+ setfltvalue(ra, idx); /* update internal index... */
1037+ setfltvalue(ra + 3, idx); /* ...and external index */
9211038 }
9221039 }
9231040 )
@@ -925,21 +1042,26 @@ void luaV_execute (lua_State *L) { · 1 unmodified line
9251042 TValue *init = ra;
9261043 TValue *plimit = ra + 1;
9271044 TValue *pstep = ra + 2;
928− if (ttisinteger(init) && ttisinteger(plimit) && ttisinteger(pstep)) {
1045+ lua_Integer ilimit;
1046+ int stopnow;
1047+ if (ttisinteger(init) && ttisinteger(pstep) &&
1048+ forlimit(plimit, &ilimit, ivalue(pstep), &stopnow)) {
9291049 /* all values are integer */
930− setivalue(init, ivalue(init) - ivalue(pstep));
1050+ lua_Integer initv = (stopnow ? 0 : ivalue(init));
1051+ setivalue(plimit, ilimit);
1052+ setivalue(init, initv - ivalue(pstep));
9311053 }
9321054 else { /* try making all values floats */
9331055 lua_Number ninit; lua_Number nlimit; lua_Number nstep;
9341056 if (!tonumber(plimit, &nlimit))
9351057 luaG_runerror(L, LUA_QL("for") " limit must be a number");
936− setnvalue(plimit, nlimit);
1058+ setfltvalue(plimit, nlimit);
9371059 if (!tonumber(pstep, &nstep))
9381060 luaG_runerror(L, LUA_QL("for") " step must be a number");
939− setnvalue(pstep, nstep);
1061+ setfltvalue(pstep, nstep);
9401062 if (!tonumber(init, &ninit))
9411063 luaG_runerror(L, LUA_QL("for") " initial value must be a number");
942− setnvalue(init, luai_numsub(L, ninit, nstep));
1064+ setfltvalue(init, luai_numsub(L, ninit, nstep));
9431065 }
9441066 ci->u.l.savedpc += GETARG_sBx(i);
9451067 )
@@ -987,7 +1109,7 @@ void luaV_execute (lua_State *L) { · 41 unmodified lines
9871109 )
9881110 vmcase(OP_CLOSURE,
9891111 Proto *p = cl->p->p[GETARG_Bx(i)];
990− Closure *ncl = getcached(p, cl->upvals, base); /* cached closure */
1112+ LClosure *ncl = getcached(p, cl->upvals, base); /* cached closure */
9911113 if (ncl == NULL) /* no match? */
9921114 pushclosure(L, p, cl->upvals, base, ra); /* create a new one */
9931115 else
@@ -1020,3 +1142,5 @@ void luaV_execute (lua_State *L) { · 26 unmodified lines
10201142 }
10211143 }
10221144
1145+/* }================================================================== */
1146+
src/lvm.h+2−7
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lvm.h,v 2.25 2013/12/30 20:47:58 roberto Exp $
2+** $Id: lvm.h,v 2.31 2014/05/26 17:10:22 roberto Exp $
33 ** Lua virtual machine
44 ** See Copyright Notice in lua.h
55 */
@@ -13,16 +13,13 @@ · 7 unmodified lines
1313 #include "ltm.h"
1414
1515
16−#define tostring(L,o) (ttisstring(o) || (luaV_tostring(L, o)))
17−
1816 #define tonumber(o,n) \
1917 (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n))
2018
2119 #define tointeger(o,i) \
2220 (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger_(o,i))
2321
24−#define intop(op,v1,v2) \
25− cast_integer(cast_unsigned(v1) op cast_unsigned(v2))
22+#define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2))
2623
2724 #define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2)
2825
@@ -32,7 +29,6 @@ LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); · 3 unmodified lines
3229 LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r);
3330 LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n);
3431 LUAI_FUNC int luaV_tointeger_ (const TValue *obj, lua_Integer *p);
35−LUAI_FUNC int luaV_numtointeger (lua_Number n, lua_Integer *p);
3632 LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj);
3733 LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key,
3834 StkId val);
@@ -43,7 +39,6 @@ LUAI_FUNC void luaV_execute (lua_State *L); · 4 unmodified lines
4339 LUAI_FUNC void luaV_concat (lua_State *L, int total);
4440 LUAI_FUNC lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y);
4541 LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y);
46−LUAI_FUNC lua_Integer luaV_pow (lua_State *L, lua_Integer x, lua_Integer y);
4742 LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y);
4843 LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb);
4944