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 @@
1 1
2This is Lua 5.3.0 (work2), released on 21 Mar 2014.2This is Lua 5.3.0 (work3), released on 19 Jul 2014.
3 3
4For installation instructions, license details, and4For installation instructions, license details, and
5further information about Lua, see doc/readme.html.5further 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
174 174
175<P>175<P>
176<A HREF="manual.html#pdf-coroutine.create">coroutine.create</A><BR>176<A HREF="manual.html#pdf-coroutine.create">coroutine.create</A><BR>
177<A HREF="manual.html#pdf-coroutine.isyieldable">coroutine.isyieldable</A><BR>
177<A HREF="manual.html#pdf-coroutine.resume">coroutine.resume</A><BR>178<A HREF="manual.html#pdf-coroutine.resume">coroutine.resume</A><BR>
178<A HREF="manual.html#pdf-coroutine.running">coroutine.running</A><BR>179<A HREF="manual.html#pdf-coroutine.running">coroutine.running</A><BR>
179<A HREF="manual.html#pdf-coroutine.status">coroutine.status</A><BR>180<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
182 183
183<P>184<P>
184<A HREF="manual.html#pdf-debug.debug">debug.debug</A><BR>185<A HREF="manual.html#pdf-debug.debug">debug.debug</A><BR>
186<A HREF="manual.html#pdf-debug.Csize">debug.Csize</A><BR>
185<A HREF="manual.html#pdf-debug.gethook">debug.gethook</A><BR>187<A HREF="manual.html#pdf-debug.gethook">debug.gethook</A><BR>
186<A HREF="manual.html#pdf-debug.getinfo">debug.getinfo</A><BR>188<A HREF="manual.html#pdf-debug.getinfo">debug.getinfo</A><BR>
187<A HREF="manual.html#pdf-debug.getlocal">debug.getlocal</A><BR>189<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
189<A HREF="manual.html#pdf-debug.getregistry">debug.getregistry</A><BR>191<A HREF="manual.html#pdf-debug.getregistry">debug.getregistry</A><BR>
190<A HREF="manual.html#pdf-debug.getupvalue">debug.getupvalue</A><BR>192<A HREF="manual.html#pdf-debug.getupvalue">debug.getupvalue</A><BR>
191<A HREF="manual.html#pdf-debug.getuservalue">debug.getuservalue</A><BR>193<A HREF="manual.html#pdf-debug.getuservalue">debug.getuservalue</A><BR>
192<A HREF="manual.html#pdf-debug.numbits">debug.numbits</A><BR>
193<A HREF="manual.html#pdf-debug.sethook">debug.sethook</A><BR>194<A HREF="manual.html#pdf-debug.sethook">debug.sethook</A><BR>
194<A HREF="manual.html#pdf-debug.setlocal">debug.setlocal</A><BR>195<A HREF="manual.html#pdf-debug.setlocal">debug.setlocal</A><BR>
195<A HREF="manual.html#pdf-debug.setmetatable">debug.setmetatable</A><BR>196<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
228<TD>229<TD>
229<H3>&nbsp;</H3>230<H3>&nbsp;</H3>
230<P>231<P>
232<A HREF="manual.html#pdf-math">math</A><BR>
231<A HREF="manual.html#pdf-math.abs">math.abs</A><BR>233<A HREF="manual.html#pdf-math.abs">math.abs</A><BR>
232<A HREF="manual.html#pdf-math.acos">math.acos</A><BR>234<A HREF="manual.html#pdf-math.acos">math.acos</A><BR>
233<A HREF="manual.html#pdf-math.asin">math.asin</A><BR>235<A HREF="manual.html#pdf-math.asin">math.asin</A><BR>
234<A HREF="manual.html#pdf-math.atan">math.atan</A><BR>236<A HREF="manual.html#pdf-math.atan">math.atan</A><BR>
235<A HREF="manual.html#pdf-math.atan2">math.atan2</A><BR>
236<A HREF="manual.html#pdf-math.ceil">math.ceil</A><BR>237<A HREF="manual.html#pdf-math.ceil">math.ceil</A><BR>
237<A HREF="manual.html#pdf-math.cos">math.cos</A><BR>238<A HREF="manual.html#pdf-math.cos">math.cos</A><BR>
238<A HREF="manual.html#pdf-math.cosh">math.cosh</A><BR>
239<A HREF="manual.html#pdf-math.deg">math.deg</A><BR>239<A HREF="manual.html#pdf-math.deg">math.deg</A><BR>
240<A HREF="manual.html#pdf-math.exp">math.exp</A><BR>
241<A HREF="manual.html#pdf-math.floor">math.floor</A><BR>240<A HREF="manual.html#pdf-math.floor">math.floor</A><BR>
242<A HREF="manual.html#pdf-math.fmod">math.fmod</A><BR>241<A HREF="manual.html#pdf-math.fmod">math.fmod</A><BR>
243<A HREF="manual.html#pdf-math.frexp">math.frexp</A><BR>
244<A HREF="manual.html#pdf-math.huge">math.huge</A><BR>242<A HREF="manual.html#pdf-math.huge">math.huge</A><BR>
245<A HREF="manual.html#pdf-math.ifloor">math.ifloor</A><BR>243<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>
248<A HREF="manual.html#pdf-math.log">math.log</A><BR>244<A HREF="manual.html#pdf-math.log">math.log</A><BR>
249<A HREF="manual.html#pdf-math.max">math.max</A><BR>245<A HREF="manual.html#pdf-math.max">math.max</A><BR>
246<A HREF="manual.html#pdf-math.maxinteger">math.maxinteger</A><BR>
250<A HREF="manual.html#pdf-math.min">math.min</A><BR>247<A HREF="manual.html#pdf-math.min">math.min</A><BR>
248<A HREF="manual.html#pdf-math.mininteger">math.mininteger</A><BR>
251<A HREF="manual.html#pdf-math.modf">math.modf</A><BR>249<A HREF="manual.html#pdf-math.modf">math.modf</A><BR>
252<A HREF="manual.html#pdf-math.pi">math.pi</A><BR>250<A HREF="manual.html#pdf-math.pi">math.pi</A><BR>
253<A HREF="manual.html#pdf-math.pow">math.pow</A><BR>
254<A HREF="manual.html#pdf-math.rad">math.rad</A><BR>251<A HREF="manual.html#pdf-math.rad">math.rad</A><BR>
255<A HREF="manual.html#pdf-math.random">math.random</A><BR>252<A HREF="manual.html#pdf-math.random">math.random</A><BR>
256<A HREF="manual.html#pdf-math.randomseed">math.randomseed</A><BR>253<A HREF="manual.html#pdf-math.randomseed">math.randomseed</A><BR>
257<A HREF="manual.html#pdf-math.sin">math.sin</A><BR>254<A HREF="manual.html#pdf-math.sin">math.sin</A><BR>
258<A HREF="manual.html#pdf-math.sinh">math.sinh</A><BR>
259<A HREF="manual.html#pdf-math.sqrt">math.sqrt</A><BR>255<A HREF="manual.html#pdf-math.sqrt">math.sqrt</A><BR>
260<A HREF="manual.html#pdf-math.tan">math.tan</A><BR>256<A HREF="manual.html#pdf-math.tan">math.tan</A><BR>
261<A HREF="manual.html#pdf-math.tanh">math.tanh</A><BR>
262<A HREF="manual.html#pdf-math.type">math.type</A><BR>257<A HREF="manual.html#pdf-math.type">math.type</A><BR>
263 258
264
265<P>259<P>
266<A HREF="manual.html#pdf-os.clock">os.clock</A><BR>260<A HREF="manual.html#pdf-os.clock">os.clock</A><BR>
267<A HREF="manual.html#pdf-os.date">os.date</A><BR>261<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
289<A HREF="manual.html#pdf-string.byte">string.byte</A><BR>283<A HREF="manual.html#pdf-string.byte">string.byte</A><BR>
290<A HREF="manual.html#pdf-string.char">string.char</A><BR>284<A HREF="manual.html#pdf-string.char">string.char</A><BR>
291<A HREF="manual.html#pdf-string.dump">string.dump</A><BR>285<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>
292<A HREF="manual.html#pdf-string.find">string.find</A><BR>288<A HREF="manual.html#pdf-string.find">string.find</A><BR>
293<A HREF="manual.html#pdf-string.format">string.format</A><BR>289<A HREF="manual.html#pdf-string.format">string.format</A><BR>
294<A HREF="manual.html#pdf-string.gmatch">string.gmatch</A><BR>290<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
296<A HREF="manual.html#pdf-string.len">string.len</A><BR>292<A HREF="manual.html#pdf-string.len">string.len</A><BR>
297<A HREF="manual.html#pdf-string.lower">string.lower</A><BR>293<A HREF="manual.html#pdf-string.lower">string.lower</A><BR>
298<A HREF="manual.html#pdf-string.match">string.match</A><BR>294<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>
301<A HREF="manual.html#pdf-string.rep">string.rep</A><BR>295<A HREF="manual.html#pdf-string.rep">string.rep</A><BR>
302<A HREF="manual.html#pdf-string.reverse">string.reverse</A><BR>296<A HREF="manual.html#pdf-string.reverse">string.reverse</A><BR>
303<A HREF="manual.html#pdf-string.sub">string.sub</A><BR>297<A HREF="manual.html#pdf-string.sub">string.sub</A><BR>
304<A HREF="manual.html#pdf-string.unpackfloat">string.unpackfloat</A><BR>298<A HREF="manual.html#pdf-string.undumpfloat">string.undumpfloat</A><BR>
305<A HREF="manual.html#pdf-string.unpackint">string.unpackint</A><BR>299<A HREF="manual.html#pdf-string.undumpint">string.undumpint</A><BR>
306<A HREF="manual.html#pdf-string.upper">string.upper</A><BR>300<A HREF="manual.html#pdf-string.upper">string.upper</A><BR>
307 301
308<P>302<P>
@@ -330,6 +324,7 @@ Freely available under the terms of the · 21 unmodified lines
330<A HREF="manual.html#lua_Debug">lua_Debug</A><BR>324<A HREF="manual.html#lua_Debug">lua_Debug</A><BR>
331<A HREF="manual.html#lua_Hook">lua_Hook</A><BR>325<A HREF="manual.html#lua_Hook">lua_Hook</A><BR>
332<A HREF="manual.html#lua_Integer">lua_Integer</A><BR>326<A HREF="manual.html#lua_Integer">lua_Integer</A><BR>
327<A HREF="manual.html#lua_KFunction">lua_KFunction</A><BR>
333<A HREF="manual.html#lua_Number">lua_Number</A><BR>328<A HREF="manual.html#lua_Number">lua_Number</A><BR>
334<A HREF="manual.html#lua_Reader">lua_Reader</A><BR>329<A HREF="manual.html#lua_Reader">lua_Reader</A><BR>
335<A HREF="manual.html#lua_State">lua_State</A><BR>330<A HREF="manual.html#lua_State">lua_State</A><BR>
@@ -352,7 +347,6 @@ Freely available under the terms of the · 16 unmodified lines
352<A HREF="manual.html#lua_error">lua_error</A><BR>347<A HREF="manual.html#lua_error">lua_error</A><BR>
353<A HREF="manual.html#lua_gc">lua_gc</A><BR>348<A HREF="manual.html#lua_gc">lua_gc</A><BR>
354<A HREF="manual.html#lua_getallocf">lua_getallocf</A><BR>349<A HREF="manual.html#lua_getallocf">lua_getallocf</A><BR>
355<A HREF="manual.html#lua_getctx">lua_getctx</A><BR>
356<A HREF="manual.html#lua_getfield">lua_getfield</A><BR>350<A HREF="manual.html#lua_getfield">lua_getfield</A><BR>
357<A HREF="manual.html#lua_getglobal">lua_getglobal</A><BR>351<A HREF="manual.html#lua_getglobal">lua_getglobal</A><BR>
358<A HREF="manual.html#lua_gethook">lua_gethook</A><BR>352<A HREF="manual.html#lua_gethook">lua_gethook</A><BR>
@@ -380,6 +374,7 @@ Freely available under the terms of the · 21 unmodified lines
380<A HREF="manual.html#lua_istable">lua_istable</A><BR>374<A HREF="manual.html#lua_istable">lua_istable</A><BR>
381<A HREF="manual.html#lua_isthread">lua_isthread</A><BR>375<A HREF="manual.html#lua_isthread">lua_isthread</A><BR>
382<A HREF="manual.html#lua_isuserdata">lua_isuserdata</A><BR>376<A HREF="manual.html#lua_isuserdata">lua_isuserdata</A><BR>
377<A HREF="manual.html#lua_isyieldable">lua_isyieldable</A><BR>
383<A HREF="manual.html#lua_len">lua_len</A><BR>378<A HREF="manual.html#lua_len">lua_len</A><BR>
384<A HREF="manual.html#lua_load">lua_load</A><BR>379<A HREF="manual.html#lua_load">lua_load</A><BR>
385<A HREF="manual.html#lua_newstate">lua_newstate</A><BR>380<A HREF="manual.html#lua_newstate">lua_newstate</A><BR>
@@ -387,6 +382,7 @@ Freely available under the terms of the · 1 unmodified line
387<A HREF="manual.html#lua_newthread">lua_newthread</A><BR>382<A HREF="manual.html#lua_newthread">lua_newthread</A><BR>
388<A HREF="manual.html#lua_newuserdata">lua_newuserdata</A><BR>383<A HREF="manual.html#lua_newuserdata">lua_newuserdata</A><BR>
389<A HREF="manual.html#lua_next">lua_next</A><BR>384<A HREF="manual.html#lua_next">lua_next</A><BR>
385<A HREF="manual.html#lua_numtointeger">lua_numtointeger</A><BR>
390<A HREF="manual.html#lua_pcall">lua_pcall</A><BR>386<A HREF="manual.html#lua_pcall">lua_pcall</A><BR>
391<A HREF="manual.html#lua_pcallk">lua_pcallk</A><BR>387<A HREF="manual.html#lua_pcallk">lua_pcallk</A><BR>
392<A HREF="manual.html#lua_pop">lua_pop</A><BR>388<A HREF="manual.html#lua_pop">lua_pop</A><BR>
@@ -418,6 +414,7 @@ Freely available under the terms of the · 25 unmodified lines
418<A HREF="manual.html#lua_remove">lua_remove</A><BR>414<A HREF="manual.html#lua_remove">lua_remove</A><BR>
419<A HREF="manual.html#lua_replace">lua_replace</A><BR>415<A HREF="manual.html#lua_replace">lua_replace</A><BR>
420<A HREF="manual.html#lua_resume">lua_resume</A><BR>416<A HREF="manual.html#lua_resume">lua_resume</A><BR>
417<A HREF="manual.html#lua_rotate">lua_rotate</A><BR>
421<A HREF="manual.html#lua_setallocf">lua_setallocf</A><BR>418<A HREF="manual.html#lua_setallocf">lua_setallocf</A><BR>
422<A HREF="manual.html#lua_setfield">lua_setfield</A><BR>419<A HREF="manual.html#lua_setfield">lua_setfield</A><BR>
423<A HREF="manual.html#lua_setglobal">lua_setglobal</A><BR>420<A HREF="manual.html#lua_setglobal">lua_setglobal</A><BR>
@@ -534,10 +531,10 @@ Freely available under the terms of the · 110 unmodified lines
534<HR>531<HR>
535<SMALL CLASS="footer">532<SMALL CLASS="footer">
536Last update:533Last update:
537Fri Mar 21 17:34:12 BRT 2014534Thu Jun 19 17:09:46 BRT 2014
538</SMALL>535</SMALL>
539<!--536<!--
540Last change: updated for Lua 5.3.0 (work2)537Last change: updated for Lua 5.3.0 (work3)
541-->538-->
542 539
543</BODY>540</BODY>
doc/manual.html+557−397
@@ -38,7 +38,7 @@ Freely available under the terms of the · 37 unmodified lines
38<!-- ====================================================================== -->38<!-- ====================================================================== -->
39<p>39<p>
40 40
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 $ -->
42 42
43 43
44 44
@@ -139,9 +139,9 @@ one called <em>integer</em> and the other called <em>float</em>. · 94 unmodified lines
139Lua has explicit rules about when each representation is used,139Lua has explicit rules about when each representation is used,
140but it also converts between them automatically as needed (see <a href="#3.4.3">&sect;3.4.3</a>).140but it also converts between them automatically as needed (see <a href="#3.4.3">&sect;3.4.3</a>).
141Therefore,141Therefore,
142the programmer has the option of mostly ignore the difference142the programmer has the option of mostly ignoring the difference
143between integers and floats143between integers and floats
144or assume complete control about the representation of each value.144or assuming complete control over the representation of each value.
145Standard Lua uses 64-bit integers and double-precision (64-bit) floats,145Standard Lua uses 64-bit integers and double-precision (64-bit) floats,
146but you can also compile Lua so that it146but you can also compile Lua so that it
147uses 32-bit integers and/or single-precision (32-bit) floats.147uses 32-bit integers and/or single-precision (32-bit) floats.
@@ -437,7 +437,7 @@ with the following code: · 289 unmodified lines
437</pre>437</pre>
438 438
439<p>439<p>
440For the unary minus and the bitwise not operators,440For the unary operators (negation, length, and bitwise not),
441the metamethod is computed and called with a dummy second operand,441the metamethod is computed and called with a dummy second operand,
442equal to the first one.442equal to the first one.
443This extra operand is only to simplify Lua's internals443This extra operand is only to simplify Lua's internals
@@ -575,9 +575,9 @@ Otherwise, Lua raises an error. · 131 unmodified lines
575<li><b>"eq": </b>575<li><b>"eq": </b>
576the <code>==</code> (equal) operation.576the <code>==</code> (equal) operation.
577 577
578Lua will try a metamethod only when the values578Behavior similar to the "add" operation,
579being compared are both either tables or full userdata,579except that Lua will try a metamethod only when the values
580both have the same metamethod,580being compared are either both tables or both full userdata
581and they are not primitively equal.581and they are not primitively equal.
582The result of the call is always converted to a boolean.582The result of the call is always converted to a boolean.
583</li>583</li>
@@ -586,7 +586,9 @@ The result of the call is always converted to a boolean. · 2 unmodified lines
586the <code>&lt;</code> (less than) operation.586the <code>&lt;</code> (less than) operation.
587 587
588Behavior similar to the "add" operation,588Behavior similar to the "add" operation,
589except that the result of the call is always converted to a boolean.589except that Lua will try a metamethod only when the values
590being compared are neither both numbers nor both strings.
591The result of the call is always converted to a boolean.
590</li>592</li>
591 593
592<li><b>"le": </b>594<li><b>"le": </b>
@@ -595,7 +597,7 @@ the <code>&lt;=</code> (less equal) operation. · 2 unmodified lines
595Unlike other operations,597Unlike other operations,
596The less-equal operation can use two different events.598The less-equal operation can use two different events.
597First, Lua looks for the "<code>__le</code>" metamethod in both operands,599First, Lua looks for the "<code>__le</code>" metamethod in both operands,
598like in the "add" operation.600like in the "lt" operation.
599If it cannot find such a metamethod,601If it cannot find such a metamethod,
600then it will try the "<code>__lt</code>" event,602then it will try the "<code>__lt</code>" event,
601assuming that <code>a &lt;= b</code> is equivalent to <code>not (b &lt; a)</code>.603assuming 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
775<p>777<p>
776At the end of each garbage-collection cycle,778At the end of each garbage-collection cycle,
777the finalizers for objects are called in779the finalizers for objects are called in
778the reverse order that they were marked for collection,780the reverse order that they were marked for finalization,
779among those collected in that cycle;781among those collected in that cycle;
780that is, the first finalizer to be called is the one associated782that is, the first finalizer to be called is the one associated
781with the object marked last in the program.783with 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
792However, if the finalizer stores the object in some global place794However, if the finalizer stores the object in some global place
793(e.g., a global variable),795(e.g., a global variable),
794then there is a permanent resurrection.796then there is a permanent resurrection.
797Moreover, if the finalizer marks a finalizing object for finalization again,
798its finalizer will be called again in the next cycle where the
799object is unreachable.
795In any case,800In any case,
796the object memory is freed only when it becomes completely inaccessible;801the object memory is freed only in the GC cycle where
797its finalizer will never be called twice.802the object is unreachable and not marked for finalization.
798 803
799 804
800<p>805<p>
801When you close a state (see <a href="#lua_close"><code>lua_close</code></a>),806When you close a state (see <a href="#lua_close"><code>lua_close</code></a>),
802Lua calls the finalizers of all objects marked for finalization,807Lua calls the finalizers of all objects marked for finalization,
803following the reverse order that they were marked.808following the reverse order that they were marked.
804If any finalizer marks new objects for collection during that phase,809If any finalizer marks objects for collection during that phase,
805these new objects will not be finalized.810these marks have no effect.
806 811
807 812
808 813
@@ -1064,9 +1069,10 @@ and cannot be used as names: · 255 unmodified lines
1064Lua is a case-sensitive language:1069Lua is a case-sensitive language:
1065<code>and</code> is a reserved word, but <code>And</code> and <code>AND</code>1070<code>and</code> is a reserved word, but <code>And</code> and <code>AND</code>
1066are two different, valid names.1071are two different, valid names.
1067As a convention, names starting with an underscore followed by1072As a convention,
1068uppercase letters (such as <a href="#pdf-_VERSION"><code>_VERSION</code></a>)1073programs should avoid creating
1069are reserved for variables used by Lua.1074names that start with an underscore followed by
1075one or more uppercase letters (such as <a href="#pdf-_VERSION"><code>_VERSION</code></a>).
1070 1076
1071 1077
1072<p>1078<p>
@@ -1074,6 +1080,7 @@ The following strings denote other tokens: · 1 unmodified line
1074 1080
1075<pre>1081<pre>
1076 + - * / % ^ #1082 + - * / % ^ #
1083 &amp; ~ | &lt;&lt; &gt;&gt; //
1077 == ~= &lt;= &gt;= &lt; &gt; =1084 == ~= &lt;= &gt;= &lt; &gt; =
1078 ( ) { } [ ] ::1085 ( ) { } [ ] ::
1079 ; : , . .. ...1086 ; : , . .. ...
@@ -1130,11 +1137,12 @@ enclosed by <em>long brackets</em>. · 50 unmodified lines
1130We define an <em>opening long bracket of level <em>n</em></em> as an opening1137We define an <em>opening long bracket of level <em>n</em></em> as an opening
1131square bracket followed by <em>n</em> equal signs followed by another1138square bracket followed by <em>n</em> equal signs followed by another
1132opening square bracket.1139opening square bracket.
1133So, an opening long bracket of level&nbsp;0 is written as <code>[[</code>,1140So, an opening long bracket of level&nbsp;0 is written as <code>[[</code>,
1134an opening long bracket of level&nbsp;1 is written as <code>[=[</code>,1141an opening long bracket of level&nbsp;1 is written as <code>[=[</code>,
1135and so on.1142and so on.
1136A <em>closing long bracket</em> is defined similarly;1143A <em>closing long bracket</em> is defined similarly;
1137for instance, a closing long bracket of level&nbsp;4 is written as <code>]====]</code>.1144for instance,
1145a closing long bracket of level&nbsp;4 is written as <code>]====]</code>.
1138A <em>long literal</em> starts with an opening long bracket of any level and1146A <em>long literal</em> starts with an opening long bracket of any level and
1139ends at the first closing long bracket of the same level.1147ends at the first closing long bracket of the same level.
1140It can contain any text except a closing bracket of the proper level.1148It can contain any text except a closing bracket of the proper level.
@@ -1594,20 +1602,19 @@ is equivalent to the code: · 453 unmodified lines
1594 1602
1595<pre>1603<pre>
1596 do1604 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>)
1598 if not (<em>var</em> and <em>limit</em> and <em>step</em>) then error() end1606 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>) do1607 <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
1600 local v = <em>var</em>1613 local v = <em>var</em>
1601 <em>block</em>1614 <em>block</em>
1602 <em>var</em> = <em>var</em> + <em>step</em>
1603 end1615 end
1604 end1616 end
1605</pre><p>1617</pre>
1606where <code>convert</code> returns
1607its three arguments unaltered
1608if they are all integers,
1609otherwise it converts them to floats.
1610
1611 1618
1612<p>1619<p>
1613Note the following:1620Note the following:
@@ -1620,12 +1627,6 @@ before the loop starts. · 6 unmodified lines
1620They must all result in numbers.1627They must all result in numbers.
1621</li>1628</li>
1622 1629
1623<li>
1624If any of the three control values is not an integer,
1625all three values are converted to floats,
1626so that the loop is done with floats.
1627</li>
1628
1629<li>1630<li>
1630<code><em>var</em></code>, <code><em>limit</em></code>, and <code><em>step</em></code> are invisible variables.1631<code><em>var</em></code>, <code><em>limit</em></code>, and <code><em>step</em></code> are invisible variables.
1631The names shown here are for explanatory purposes only.1632The 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
1641</li>1642</li>
1642 1643
1643<li>1644<li>
1644The loop variable <code>v</code> is local to the loop;1645The loop variable <code>v</code> is local to the loop body.
1645you cannot use its value after the <b>for</b> ends or is broken.1646If you need its value after the loop,
1646If you need its value,
1647assign it to another variable before exiting the loop.1647assign it to another variable before exiting the loop.
1648</li>1648</li>
1649 1649
@@ -1840,17 +1840,20 @@ or <b>nil</b> if <code>f</code> does not return any values.) · 190 unmodified lines
1840 1840
1841<h3>3.4.1 &ndash; <a name="3.4.1">Arithmetic Operators</a></h3><p>1841<h3>3.4.1 &ndash; <a name="3.4.1">Arithmetic Operators</a></h3><p>
1842Lua supports the following arithmetic operators:1842Lua supports the following arithmetic operators:
1843the binary <code>+</code> (addition),1843<table border="1">
1844<code>-</code> (subtraction), <code>*</code> (multiplication),1844<tr><td><code>+</code></td><td>addition</td></tr>
1845<code>/</code> (division), <code>//</code> (integer division),1845<tr><td><code>-</code></td><td>subtraction</td></tr>
1846<code>%</code> (modulo), and <code>^</code> (exponentiation);1846<tr><td><code>*</code></td><td>multiplication</td></tr>
1847and unary <code>-</code> (mathematical negation).1847<tr><td><code>/</code></td><td>float division</td></tr>
1848Modulo is defined as the remainder of a division1848<tr><td><code>//</code></td><td>integer division</td></tr>
1849that rounds the quotient towards minus infinite (floor).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>
1850 1853
1851 1854
1852<p>1855<p>
1853With the exception of division and integer division,1856With the exception of divisions and exponentiation,
1854the arithmetic operators work as follows:1857the arithmetic operators work as follows:
1855If both operands are integers,1858If both operands are integers,
1856the operation is performed over integers and the result is an integer.1859the operation is performed over integers and the result is an integer.
@@ -1865,8 +1868,11 @@ and the result is a float. · 8 unmodified lines
1865 1868
1866 1869
1867<p>1870<p>
1868Division (<code>/</code>) always converts its operands to floats1871Float division (<code>/</code>) and exponentiation
1869and its result is always a float.1872always convert their operands to floats
1873and the result is always a float.
1874Exponentiation uses the ANSI&nbsp;C function <code>pow</code>,
1875so that it works for non-integer exponents too.
1870 1876
1871 1877
1872<p>1878<p>
@@ -1876,10 +1882,16 @@ and its result is always an integer. · 3 unmodified lines
1876The result is always rounded towards minus infinite (floor).1882The result is always rounded towards minus infinite (floor).
1877 1883
1878 1884
1885<p>
1886Modulo is defined as the remainder of a division
1887that rounds the quotient towards minus infinite (floor).
1888
1889
1879<p>1890<p>
1880In case of overflows in integer arithmetic,1891In case of overflows in integer arithmetic,
1881all operations <em>wrap around</em>,1892all operations <em>wrap around</em>,
1882according to the usual rules of two-complement arithmetic.1893according to the usual rules of two-complement arithmetic.
1894(In other words, they return the correct result modulo <em>2<sup>64</sup></em>.)
1883 1895
1884 1896
1885 1897
@@ -1887,10 +1899,14 @@ according to the usual rules of two-complement arithmetic. · 1 unmodified line
1887 1899
1888<h3>3.4.2 &ndash; <a name="3.4.2">Bitwise Operators</a></h3><p>1900<h3>3.4.2 &ndash; <a name="3.4.2">Bitwise Operators</a></h3><p>
1889Lua supports the following bitwise operators:1901Lua supports the following bitwise operators:
1890the binary <code>&amp;</code> (bitwise and),1902<table border="1">
1891<code>|</code> (bitwise or), <code>~</code> (bitwise exclusive or),1903<tr><td><code>&amp;</code></td><td>bitwise and</td></tr>
1892<code>&gt;&gt;</code> (right shift), <code>&lt;&lt;</code> (left shift),1904<tr><td><code>|</code></td><td>bitwise or</td></tr>
1893and unary <code>~</code> (bitwise not).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>
1894 1910
1895 1911
1896<p>1912<p>
@@ -1901,7 +1917,7 @@ and result in an integer. · 4 unmodified lines
1901 1917
1902 1918
1903<p>1919<p>
1904Both right and left shifts fill with zeros the vacant bits.1920Both right and left shifts fill the vacant bits with zeros.
1905Negative displacements shift to the other direction;1921Negative displacements shift to the other direction;
1906displacements with absolute values equal to or higher than1922displacements with absolute values equal to or higher than
1907the number of bits in an integer1923the number of bits in an integer
@@ -1917,7 +1933,7 @@ types and representations at run time. · 9 unmodified lines
1917Most arithmetic operations applied to mixed numbers1933Most arithmetic operations applied to mixed numbers
1918(integers and floats) convert the integer operand to a float;1934(integers and floats) convert the integer operand to a float;
1919this is called the <em>usual rule</em>.1935this is called the <em>usual rule</em>.
1920Division always convert integer operands to floats;1936Float division always convert integer operands to floats;
1921integer division and bitwise operators1937integer division and bitwise operators
1922always convert float operands to integers.1938always convert float operands to integers.
1923The C API also converts both integers to floats and1939The C API also converts both integers to floats and
@@ -1929,7 +1945,6 @@ besides strings. · 5 unmodified lines
1929<p>1945<p>
1930Lua also converts strings to numbers,1946Lua also converts strings to numbers,
1931whenever a number is expected.1947whenever a number is expected.
1932(This conversion may be deprecated in future versions.)
1933 1948
1934 1949
1935<p>1950<p>
@@ -1951,16 +1966,16 @@ Otherwise, the conversion fails. · 15 unmodified lines
1951 1966
1952 1967
1953<p>1968<p>
1954The conversion from strings to floats1969The conversion from strings to numbers goes as follows:
1955follows the rules of the Lua lexer.1970First, the string is converted to an integer or a float,
1956(The string may have leading and trailing spaces and a sign.)1971following its syntax and the rules of the Lua lexer.
1957There is no direct conversion from strings to integers:1972(The string may have also leading and trailing spaces and a sign.)
1958If a string is provided where an integer is needed,1973Then, the resulting number is converted to the required type
1959Lua converts the string to a float and then the float to an integer.1974(float or integer) according to the previous rules.
1960 1975
1961 1976
1962<p>1977<p>
1963The conversion from numbers to strings use a human-readable,1978The conversion from numbers to strings uses a human-readable,
1964non-specified format.1979non-specified format.
1965For complete control over how numbers are converted to strings,1980For complete control over how numbers are converted to strings,
1966use the <code>format</code> function from the string library1981use 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
1971 1986
1972 1987
1973<h3>3.4.4 &ndash; <a name="3.4.4">Relational Operators</a></h3><p>1988<h3>3.4.4 &ndash; <a name="3.4.4">Relational Operators</a></h3><p>
1974The relational operators in Lua are1989Lua supports the following relational operators:
1975 1990<table border="1">
1976<pre>1991<tr><td><code>==</code></td><td>equality</td></tr>
1977 == ~= &lt; &gt; &lt;= &gt;=1992<tr><td><code>~=</code></td><td>inequality</td></tr>
1978</pre><p>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>
1979These operators always result in <b>false</b> or <b>true</b>.1998These operators always result in <b>false</b> or <b>true</b>.
1980 1999
1981 2000
@@ -1986,8 +2005,8 @@ Otherwise, the values of the operands are compared. · 4 unmodified lines
1986Strings are compared in the obvious way.2005Strings are compared in the obvious way.
1987Numbers follow the usual rule for binary operations:2006Numbers follow the usual rule for binary operations:
1988if both operands are integers,2007if both operands are integers,
1989the are compared as integers.2008the are compared as integers;
1990Otherwise, they are converted to floats2009otherwise, they are converted to floats
1991and compared as such.2010and compared as such.
1992 2011
1993 2012
@@ -2131,10 +2150,14 @@ from lower to higher priority: · 137 unmodified lines
2131 or2150 or
2132 and2151 and
2133 &lt; &gt; &lt;= &gt;= ~= ==2152 &lt; &gt; &lt;= &gt;= ~= ==
2153 |
2154 ~
2155 &amp;
2156 &lt;&lt; &gt;&gt;
2134 ..2157 ..
2135 + -2158 + -
2136 * / // %2159 * / // %
2137 not # - (unary)2160 unary operators (not # - ~)
2138 ^2161 ^
2139</pre><p>2162</pre><p>
2140As usual,2163As usual,
@@ -2690,7 +2713,7 @@ The registry table is always located at pseudo-index · 549 unmodified lines
2690<a name="pdf-LUA_REGISTRYINDEX"><code>LUA_REGISTRYINDEX</code></a>,2713<a name="pdf-LUA_REGISTRYINDEX"><code>LUA_REGISTRYINDEX</code></a>,
2691which is a valid index.2714which is a valid index.
2692Any C&nbsp;library can store data into this table,2715Any C&nbsp;library can store data into this table,
2693but it should take care to choose keys2716but it must take care to choose keys
2694that are different from those used2717that are different from those used
2695by other libraries, to avoid collisions.2718by other libraries, to avoid collisions.
2696Typically, you should use as key a string containing your library name,2719Typically, you should use as key a string containing your library name,
@@ -2705,7 +2728,7 @@ uppercase letters are reserved for Lua. · 8 unmodified lines
2705The integer keys in the registry are used by the reference mechanism,2728The integer keys in the registry are used by the reference mechanism,
2706implemented by the auxiliary library,2729implemented by the auxiliary library,
2707and by some predefined values.2730and by some predefined values.
2708Therefore, integer keys should not be used for other purposes.2731Therefore, integer keys must not be used for other purposes.
2709 2732
2710 2733
2711<p>2734<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
2760in particular, the error message is at the top of the stack.2783in particular, the error message is at the top of the stack.
2761However, there is no guarantees about stack space.2784However, there is no guarantees about stack space.
2762To push anything on the stack,2785To push anything on the stack,
2763the panic function should first check the available space (see <a href="#4.2">&sect;4.2</a>).2786the panic function must first check the available space (see <a href="#4.2">&sect;4.2</a>).
2764 2787
2765 2788
2766<p>2789<p>
2767Most functions in the API can throw an error,2790Most functions in the API can raise an error,
2768for instance due to a memory allocation error.2791for instance due to a memory allocation error.
2769The documentation for each function indicates whether2792The documentation for each function indicates whether
2770it can throw errors.2793it can raise errors.
2771 2794
2772 2795
2773<p>2796<p>
2774Inside a C&nbsp;function you can throw an error by calling <a href="#lua_error"><code>lua_error</code></a>.2797Inside a C&nbsp;function you can raise an error by calling <a href="#lua_error"><code>lua_error</code></a>.
2775 2798
2776 2799
2777 2800
@@ -2781,7 +2804,7 @@ Inside a C&nbsp;function you can throw an error by calling <a href="#lua_error"> · 3 unmodified lines
2781 2804
2782<p>2805<p>
2783Internally, Lua uses the C <code>longjmp</code> facility to yield a coroutine.2806Internally, Lua uses the C <code>longjmp</code> facility to yield a coroutine.
2784Therefore, if a function <code>foo</code> calls an API function2807Therefore, if a C function <code>foo</code> calls an API function
2785and this API function yields2808and this API function yields
2786(directly or indirectly by calling another function that yields),2809(directly or indirectly by calling another function that yields),
2787Lua cannot return to <code>foo</code> any more,2810Lua cannot return to <code>foo</code> any more,
@@ -2823,6 +2846,67 @@ the continuation function should continue the task · 35 unmodified lines
2823of the original function.2846of the original function.
2824 2847
2825 2848
2849<p>
2850As 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>
2859Now we want to allow
2860the Lua code being ran by <a href="#lua_pcall"><code>lua_pcall</code></a> to yield.
2861First, 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>
2873In the above code,
2874the new function <code>k</code> is a
2875<em>continuation function</em> (with type <a href="#lua_KFunction"><code>lua_KFunction</code></a>),
2876which should do all the work that the original function
2877was doing after calling <a href="#lua_pcall"><code>lua_pcall</code></a>.
2878Now, we must inform Lua that it must call <code>k</code> if the Lua code
2879begin running by <a href="#lua_pcall"><code>lua_pcall</code></a> gets interrupted in some way
2880(errors or yielding),
2881so we rewrite the code as here,
2882replacing <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>
2892Besides the Lua state,
2893the continuation function has two other parameters:
2894the final status of the call plus the context value (<code>ctx</code>) that
2895was passed originally to <a href="#lua_pcallk"><code>lua_pcallk</code></a>.
2896(Lua does not use this context value;
2897it only passes this value from the original function to the
2898continuation function.)
2899For <a href="#lua_pcallk"><code>lua_pcallk</code></a>,
2900the status is the same value that would be returned by <a href="#lua_pcallk"><code>lua_pcallk</code></a>,
2901except 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>).
2903For <a href="#lua_yieldk"><code>lua_yieldk</code></a> and <a href="#lua_callk"><code>lua_callk</code></a>,
2904the status is always <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> when Lua calls the continuation.
2905(For these two functions,
2906Lua will not call the continuation in case of errors,
2907because they do not handle errors.)
2908
2909
2826<p>2910<p>
2827Lua treats the continuation function as if it were the original function.2911Lua treats the continuation function as if it were the original function.
2828The continuation function receives the same Lua stack2912The 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
2836of the original function.2920of the original function.
2837 2921
2838 2922
2839<p>
2840The only difference in the Lua state between the original function
2841and its continuation is the result of a call to <a href="#lua_getctx"><code>lua_getctx</code></a>.
2842
2843
2844 2923
2845 2924
2846 2925
@@ -2867,10 +2946,10 @@ we cannot know how many elements the function pops/pushes · 20 unmodified lines
2867by looking only at its arguments2946by looking only at its arguments
2868(e.g., they may depend on what is on the stack).2947(e.g., they may depend on what is on the stack).
2869The third field, <code>x</code>,2948The third field, <code>x</code>,
2870tells whether the function may throw errors:2949tells whether the function may raise errors:
2871'<code>-</code>' means the function never throws any error;2950'<code>-</code>' means the function never raises any error;
2872'<code>e</code>' means the function may throw errors;2951'<code>e</code>' means the function may raise errors;
2873'<code>v</code>' means the function may throw an error on purpose.2952'<code>v</code>' means the function may raise an error on purpose.
2874 2953
2875 2954
2876 2955
@@ -2928,13 +3007,13 @@ Lua assumes the following behavior from the allocator function: · 51 unmodified lines
2928 3007
2929<p>3008<p>
2930When <code>nsize</code> is zero,3009When <code>nsize</code> is zero,
2931the allocator should behave like <code>free</code>3010the allocator must behave like <code>free</code>
2932and return <code>NULL</code>.3011and return <code>NULL</code>.
2933 3012
2934 3013
2935<p>3014<p>
2936When <code>nsize</code> is not zero,3015When <code>nsize</code> is not zero,
2937the allocator should behave like <code>realloc</code>.3016the allocator must behave like <code>realloc</code>.
2938The allocator returns <code>NULL</code>3017The allocator returns <code>NULL</code>
2939if and only if it cannot fulfill the request.3018if and only if it cannot fulfill the request.
2940Lua assumes that the allocator never fails when3019Lua 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
2990<li><b><a name="pdf-LUA_OPADD"><code>LUA_OPADD</code></a>: </b> performs addition (<code>+</code>)</li>3069<li><b><a name="pdf-LUA_OPADD"><code>LUA_OPADD</code></a>: </b> performs addition (<code>+</code>)</li>
2991<li><b><a name="pdf-LUA_OPSUB"><code>LUA_OPSUB</code></a>: </b> performs subtraction (<code>-</code>)</li>3070<li><b><a name="pdf-LUA_OPSUB"><code>LUA_OPSUB</code></a>: </b> performs subtraction (<code>-</code>)</li>
2992<li><b><a name="pdf-LUA_OPMUL"><code>LUA_OPMUL</code></a>: </b> performs multiplication (<code>*</code>)</li>3071<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>
2994<li><b><a name="pdf-LUA_OPIDIV"><code>LUA_OPIDIV</code></a>: </b> performs integer division (<code>//</code>)</li>3073<li><b><a name="pdf-LUA_OPIDIV"><code>LUA_OPIDIV</code></a>: </b> performs integer division (<code>//</code>)</li>
2995<li><b><a name="pdf-LUA_OPMOD"><code>LUA_OPMOD</code></a>: </b> performs modulo (<code>%</code>)</li>3074<li><b><a name="pdf-LUA_OPMOD"><code>LUA_OPMOD</code></a>: </b> performs modulo (<code>%</code>)</li>
2996<li><b><a name="pdf-LUA_OPPOW"><code>LUA_OPPOW</code></a>: </b> performs exponentiation (<code>^</code>)</li>3075<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
3081<hr><h3><a name="lua_callk"><code>lua_callk</code></a></h3><p>3160<hr><h3><a name="lua_callk"><code>lua_callk</code></a></h3><p>
3082<span class="apii">[-(nargs + 1), +nresults, <em>e</em>]</span>3161<span class="apii">[-(nargs + 1), +nresults, <em>e</em>]</span>
3083<pre>void lua_callk (lua_State *L, int nargs, int nresults, int ctx,3162<pre>void lua_callk (lua_State *L, int nargs, int nresults, int ctx,
3084 lua_CFunction k);</pre>3163 lua_KFunction k);</pre>
3085 3164
3086<p>3165<p>
3087This function behaves exactly like <a href="#lua_call"><code>lua_call</code></a>,3166This 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
3258 3337
3259<hr><h3><a name="lua_dump"><code>lua_dump</code></a></h3><p>3338<hr><h3><a name="lua_dump"><code>lua_dump</code></a></h3><p>
3260<span class="apii">[-0, +0, <em>e</em>]</span>3339<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>
3262 3344
3263<p>3345<p>
3264Dumps a function as a binary chunk.3346Dumps a function as a binary chunk.
@@ -3272,6 +3354,12 @@ with the given <code>data</code> · 7 unmodified lines
3272to write them.3354to write them.
3273 3355
3274 3356
3357<p>
3358If <code>strip</code> is true,
3359the binary representation is created without debug information
3360about the function.
3361
3362
3275<p>3363<p>
3276The value returned is the error code returned by the last3364The value returned is the error code returned by the last
3277call to the writer;3365call to the writer;
@@ -3291,8 +3379,7 @@ This function does not pop the Lua function from the stack. · 13 unmodified lines
3291 3379
3292<p>3380<p>
3293Generates a Lua error.3381Generates a Lua error.
3294The error message (which can actually be a Lua value of any type)3382The error object must be on the stack top.
3295must be on the stack top.
3296This function does a long jump,3383This function does a long jump,
3297and therefore never returns3384and therefore never returns
3298(see <a href="#luaL_error"><code>luaL_error</code></a>).3385(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
3380 3467
3381 3468
3382 3469
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>
3388This function is called by a continuation function (see <a href="#4.7">&sect;4.7</a>)
3389to retrieve the status of the thread and a context information.
3390
3391
3392<p>
3393When 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>
3395and does not change the value of its argument <code>ctx</code>.
3396When 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
3398the value of <code>ctx</code> to be the context information
3399(the value passed as the <code>ctx</code> argument
3400to the callee together with the continuation function).
3401
3402
3403<p>
3404When the callee is <a href="#lua_pcallk"><code>lua_pcallk</code></a>,
3405Lua may also call its continuation function
3406to handle errors during the call.
3407That is, upon an error in the function called by <a href="#lua_pcallk"><code>lua_pcallk</code></a>,
3408Lua may not return to the original function
3409but instead may call the continuation function.
3410In 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>);
3412the value of <code>ctx</code> will be set to the context information,
3413as in the case of a yield.
3414
3415
3416
3417
3418
3419<hr><h3><a name="lua_getfield"><code>lua_getfield</code></a></h3><p>3470<hr><h3><a name="lua_getfield"><code>lua_getfield</code></a></h3><p>
3420<span class="apii">[-0, +1, <em>e</em>]</span>3471<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>
3422 3473
3423<p>3474<p>
3424Pushes onto the stack the value <code>t[k]</code>,3475Pushes 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
3427for the "index" event (see <a href="#2.4">&sect;2.4</a>).3478for the "index" event (see <a href="#2.4">&sect;2.4</a>).
3428 3479
3429 3480
3481<p>
3482Returns the type of the pushed value.
3483
3484
3430 3485
3431 3486
3432 3487
@@ -3495,13 +3550,17 @@ this result is equal to the number of elements in the stack · 62 unmodified lines
3495 3550
3496<hr><h3><a name="lua_getuservalue"><code>lua_getuservalue</code></a></h3><p>3551<hr><h3><a name="lua_getuservalue"><code>lua_getuservalue</code></a></h3><p>
3497<span class="apii">[-0, +1, &ndash;]</span>3552<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>
3499 3554
3500<p>3555<p>
3501Pushes onto the stack the Lua value associated with the userdata3556Pushes onto the stack the Lua value associated with the userdata
3502at the given index.3557at the given index.
3503 3558
3504 3559
3560<p>
3561Returns the type of the pushed value.
3562
3563
3505 3564
3506 3565
3507 3566
@@ -3520,7 +3579,7 @@ because a pseudo-index is not an actual stack position. · 12 unmodified lines
3520 3579
3521 3580
3522<hr><h3><a name="lua_Integer"><code>lua_Integer</code></a></h3>3581<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>
3524 3583
3525<p>3584<p>
3526The type of integers in Lua.3585The type of integers in Lua.
@@ -3534,6 +3593,12 @@ to <code>long</code> or <code>int</code> · 7 unmodified lines
3534(usually a 32-bit two-complement integer).3593(usually a 32-bit two-complement integer).
3535 3594
3536 3595
3596<p>
3597Lua 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>,
3599with the minimum and the maximum values that fit in this type.
3600
3601
3537 3602
3538 3603
3539 3604
@@ -3697,6 +3762,28 @@ Returns 1 if the value at the given index is a userdata · 157 unmodified lines
3697 3762
3698 3763
3699 3764
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>
3770Returns 1 if the given coroutine can yield,
3771and 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>
3781Type for continuation functions (see <a href="#4.7">&sect;4.7</a>).
3782
3783
3784
3785
3786
3700<hr><h3><a name="lua_len"><code>lua_len</code></a></h3><p>3787<hr><h3><a name="lua_len"><code>lua_len</code></a></h3><p>
3701<span class="apii">[-0, +1, <em>e</em>]</span>3788<span class="apii">[-0, +1, <em>e</em>]</span>
3702<pre>void lua_len (lua_State *L, int index);</pre>3789<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
3768 3855
3769<p>3856<p>
3770<code>lua_load</code> uses the stack internally,3857<code>lua_load</code> uses the stack internally,
3771so the reader function should always leave the stack3858so the reader function must always leave the stack
3772unmodified when returning.3859unmodified when returning.
3773 3860
3774 3861
@@ -3907,6 +3994,28 @@ but that can be changed in <code>luaconf.h</code> to a single float. · 132 unmodified lines
3907 3994
3908 3995
3909 3996
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>
4001Converts a Lua float to a Lua integer.
4002This macro assumes that <code>n</code> has an integral value.
4003If that value is within the range of Lua integers,
4004it is converted to an integer and assigned to <code>*p</code>.
4005The macro results in a boolean indicating whether the
4006conversion was successful.
4007(Note that this range test can be tricky to do
4008correctly without this macro,
4009due to roundings.)
4010
4011
4012<p>
4013This macro may evaluate its arguments more than once.
4014
4015
4016
4017
4018
3910<hr><h3><a name="lua_pcall"><code>lua_pcall</code></a></h3><p>4019<hr><h3><a name="lua_pcall"><code>lua_pcall</code></a></h3><p>
3911<span class="apii">[-(nargs + 1), +(nresults|1), &ndash;]</span>4020<span class="apii">[-(nargs + 1), +(nresults|1), &ndash;]</span>
3912<pre>int lua_pcall (lua_State *L, int nargs, int nresults, int msgh);</pre>4021<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
3950 4059
3951 4060
3952<p>4061<p>
3953The <a href="#lua_pcall"><code>lua_pcall</code></a> function returns one of the following codes4062The <a href="#lua_pcall"><code>lua_pcall</code></a> function returns one of the following constants
3954(defined in <code>lua.h</code>):4063(defined in <code>lua.h</code>):
3955 4064
3956<ul>4065<ul>
@@ -3973,8 +4082,7 @@ error while running the message handler. · 16 unmodified lines
3973 4082
3974<li><b><a name="pdf-LUA_ERRGCMM"><code>LUA_ERRGCMM</code></a>: </b>4083<li><b><a name="pdf-LUA_ERRGCMM"><code>LUA_ERRGCMM</code></a>: </b>
3975error while running a <code>__gc</code> metamethod.4084error while running a <code>__gc</code> metamethod.
3976(This error typically has no relation with the function being called.4085(This error typically has no relation with the function being called.)
3977It is generated by the garbage collector.)
3978</li>4086</li>
3979 4087
3980</ul>4088</ul>
@@ -3989,7 +4097,7 @@ It is generated by the garbage collector.) · 8 unmodified lines
3989 int nresults,4097 int nresults,
3990 int errfunc,4098 int errfunc,
3991 int ctx,4099 int ctx,
3992 lua_CFunction k);</pre>4100 lua_KFunction k);</pre>
3993 4101
3994<p>4102<p>
3995This function behaves exactly like <a href="#lua_pcall"><code>lua_pcall</code></a>,4103This 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
4035thus creating a C&nbsp;closure (see <a href="#4.4">&sect;4.4</a>);4143thus creating a C&nbsp;closure (see <a href="#4.4">&sect;4.4</a>);
4036these values are then accessible to the function whenever it is called.4144these values are then accessible to the function whenever it is called.
4037To associate values with a C&nbsp;function,4145To associate values with a C&nbsp;function,
4038first these values should be pushed onto the stack4146first these values must be pushed onto the stack
4039(when there are multiple values, the first value is pushed first).4147(when there are multiple values, the first value is pushed first).
4040Then <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a>4148Then <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a>
4041is called to create and push the C&nbsp;function onto the stack,4149is called to create and push the C&nbsp;function onto the stack,
4042with the argument <code>n</code> telling how many values should be4150with the argument <code>n</code> telling how many values will be
4043associated with the function.4151associated with the function.
4044<a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a> also pops these values from the stack.4152<a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a> also pops these values from the stack.
4045 4153
@@ -4052,7 +4160,7 @@ The maximum value for <code>n</code> is 255. · 6 unmodified lines
4052When <code>n</code> is zero,4160When <code>n</code> is zero,
4053this function creates a <em>light C function</em>,4161this function creates a <em>light C function</em>,
4054which is just a pointer to the C&nbsp;function.4162which is just a pointer to the C&nbsp;function.
4055In that case, it never throws a memory error.4163In that case, it never raises a memory error.
4056 4164
4057 4165
4058 4166
@@ -4540,6 +4648,22 @@ this parameter can be <code>NULL</code>. · 481 unmodified lines
4540 4648
4541 4649
4542 4650
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>
4656Rotates the stack elements from <code>idx</code> to the top <code>n</code> positions
4657in the direction of the top, for a positive <code>n</code>,
4658or <code>-n</code> positions in the direction of the bottom,
4659for a negative <code>n</code>.
4660The absolute value of <code>n</code> must not be greater than the size
4661of the slice being rotated.
4662
4663
4664
4665
4666
4543<hr><h3><a name="lua_setallocf"><code>lua_setallocf</code></a></h3><p>4667<hr><h3><a name="lua_setallocf"><code>lua_setallocf</code></a></h3><p>
4544<span class="apii">[-0, +0, &ndash;]</span>4668<span class="apii">[-0, +0, &ndash;]</span>
4545<pre>void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);</pre>4669<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
4689 4813
4690<hr><h3><a name="lua_strtonum"><code>lua_strtonum</code></a></h3><p>4814<hr><h3><a name="lua_strtonum"><code>lua_strtonum</code></a></h3><p>
4691<span class="apii">[-0, +1, &ndash;]</span>4815<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>
4693 4817
4694<p>4818<p>
4695Converts the string <code>s</code> (with length <code>len</code>) to a number and4819Converts the zero-terminated string <code>s</code> to a number,
4696pushes that number into the stack.4820pushes that number into the stack,
4821and returns the total size of the string
4822(that is, its length plus one).
4697The conversion can result in an integer or a float,4823The conversion can result in an integer or a float,
4698according to the lexical conventions of Lua (see <a href="#3.1">&sect;3.1</a>).4824according to the lexical conventions of Lua (see <a href="#3.1">&sect;3.1</a>).
4699The string may have leading and trailing spaces and a sign.4825The 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
4969 5095
4970 5096
4971<hr><h3><a name="lua_Unsigned"><code>lua_Unsigned</code></a></h3>5097<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>
4973 5099
4974<p>5100<p>
4975The unsigned version of <a href="#lua_Integer"><code>lua_Integer</code></a>.5101The unsigned version of <a href="#lua_Integer"><code>lua_Integer</code></a>.
4976 5102
4977 5103
5104<p>
5105Lua also defines the constant <a name="pdf-LUA_MAXUNSIGNED"><code>LUA_MAXUNSIGNED</code></a>,
5106with the maximum value that fits in this type.
5107
5108
4978 5109
4979 5110
4980 5111
@@ -5063,7 +5194,7 @@ the function calling <code>lua_yield</code>. · 82 unmodified lines
5063 5194
5064<hr><h3><a name="lua_yieldk"><code>lua_yieldk</code></a></h3><p>5195<hr><h3><a name="lua_yieldk"><code>lua_yieldk</code></a></h3><p>
5065<span class="apii">[-?, +?, &ndash;]</span>5196<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>
5067 5198
5068<p>5199<p>
5069Yields a coroutine.5200Yields a coroutine.
@@ -5074,13 +5205,13 @@ This function should only be called as the · 4 unmodified lines
5074return expression of a C&nbsp;function, as follows:5205return expression of a C&nbsp;function, as follows:
5075 5206
5076<pre>5207<pre>
5077 return lua_yieldk (L, n, i, k);5208 return lua_yieldk (L, n, ctx, k);
5078</pre><p>5209</pre><p>
5079When a C&nbsp;function calls <a href="#lua_yieldk"><code>lua_yieldk</code></a> in that way,5210When a C&nbsp;function calls <a href="#lua_yieldk"><code>lua_yieldk</code></a> in that way,
5080the running coroutine suspends its execution,5211the running coroutine suspends its execution,
5081and the call to <a href="#lua_resume"><code>lua_resume</code></a> that started this coroutine returns.5212and the call to <a href="#lua_resume"><code>lua_resume</code></a> that started this coroutine returns.
5082The parameter <code>nresults</code> is the number of values from the stack5213The parameter <code>nresults</code> is the number of values from the stack
5083that are passed as results to <a href="#lua_resume"><code>lua_resume</code></a>.5214that will be passed as results to <a href="#lua_resume"><code>lua_resume</code></a>.
5084 5215
5085 5216
5086<p>5217<p>
@@ -5089,11 +5220,11 @@ Lua calls the given continuation function <code>k</code> to continue · 2 unmodified lines
5089the execution of the C function that yielded (see <a href="#4.7">&sect;4.7</a>).5220the execution of the C function that yielded (see <a href="#4.7">&sect;4.7</a>).
5090This continuation function receives the same stack5221This continuation function receives the same stack
5091from the previous function,5222from the previous function,
5092with the results removed and5223with the <code>n</code> results removed and
5093replaced by the arguments passed to <a href="#lua_resume"><code>lua_resume</code></a>.5224replaced by the arguments passed to <a href="#lua_resume"><code>lua_resume</code></a>.
5094Moreover,5225Moreover,
5095the continuation function may access the value <code>ctx</code>5226the continuation function receives the value <code>ctx</code>
5096by calling <a href="#lua_getctx"><code>lua_getctx</code></a>.5227that was passed to <a href="#lua_yieldk"><code>lua_yieldk</code></a>.
5097 5228
5098 5229
5099 5230
@@ -5326,6 +5457,11 @@ numbers of the lines that are valid on the function. · 226 unmodified lines
5326(A <em>valid line</em> is a line with some associated code,5457(A <em>valid line</em> is a line with some associated code,
5327that is, a line where you can put a break point.5458that is, a line where you can put a break point.
5328Non-valid lines include empty lines and comments.)5459Non-valid lines include empty lines and comments.)
5460
5461
5462<p>
5463If this option is given together with option '<code>f</code>',
5464its table is pushed after the function.
5329</li>5465</li>
5330 5466
5331</ul>5467</ul>
@@ -5363,7 +5499,7 @@ and returns its name. · 31 unmodified lines
5363 5499
5364 5500
5365<p>5501<p>
5366In the second case, <code>ar</code> should be <code>NULL</code> and the function5502In the second case, <code>ar</code> must be <code>NULL</code> and the function
5367to be inspected must be at the top of the stack.5503to be inspected must be at the top of the stack.
5368In this case, only parameters of Lua functions are visible5504In this case, only parameters of Lua functions are visible
5369(as there is no information about what variables are active)5505(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
5656 5792
5657<p>5793<p>
5658Functions called <code>luaL_check*</code>5794Functions called <code>luaL_check*</code>
5659always throw an error if the check is not satisfied.5795always raise an error if the check is not satisfied.
5660 5796
5661 5797
5662 5798
@@ -6398,7 +6534,7 @@ returns 0. · 735 unmodified lines
6398Otherwise,6534Otherwise,
6399creates a new table to be used as a metatable for userdata,6535creates a new table to be used as a metatable for userdata,
6400adds to this new table the pair <code>__name = tname</code>,6536adds to this new table the pair <code>__name = tname</code>,
6401adds to the registry the pair <code></code>[tname] = new table,6537adds to the registry the pair <code>[tname] = new table</code>,
6402and returns 1.6538and returns 1.
6403 6539
6404 6540
@@ -6762,7 +6898,7 @@ the field value is changed to <b>nil</b> to signal that the handle is closed. · 357 unmodified lines
6762<p>6898<p>
6763This function works like <a href="#luaL_checkudata"><code>luaL_checkudata</code></a>,6899This function works like <a href="#luaL_checkudata"><code>luaL_checkudata</code></a>,
6764except that, when the test fails,6900except that, when the test fails,
6765it returns <code>NULL</code> instead of throwing an error.6901it returns <code>NULL</code> instead of raising an error.
6766 6902
6767 6903
6768 6904
@@ -6862,6 +6998,8 @@ This function is used to build a prefix for error messages. · 93 unmodified lines
6862 6998
6863 6999
6864 7000
7001
7002
6865<h1>6 &ndash; <a name="6">Standard Libraries</a></h1>7003<h1>6 &ndash; <a name="6">Standard Libraries</a></h1>
6866 7004
6867<p>7005<p>
@@ -6939,11 +7077,15 @@ implementations for some of its facilities. · 71 unmodified lines
6939 7077
6940<p>7078<p>
6941<hr><h3><a name="pdf-assert"><code>assert (v [, message])</code></a></h3>7079<hr><h3><a name="pdf-assert"><code>assert (v [, message])</code></a></h3>
6942Issues an error when7080
7081
7082<p>
7083Calls <a href="#pdf-error"><code>error</code></a> if
6943the value of its argument <code>v</code> is false (i.e., <b>nil</b> or <b>false</b>);7084the value of its argument <code>v</code> is false (i.e., <b>nil</b> or <b>false</b>);
6944otherwise, returns all its arguments.7085otherwise, returns all its arguments.
6945<code>message</code> is an error message;7086In case of error,
6946when absent, it defaults to "assertion failed!"7087<code>message</code> is the error object;
7088when absent, it defaults to "<code>assertion failed!</code>"
6947 7089
6948 7090
6949 7091
@@ -7009,16 +7151,6 @@ returns a boolean that tells whether the collector is running · 59 unmodified lines
7009(i.e., not stopped).7151(i.e., not stopped).
7010</li>7152</li>
7011 7153
7012<li><b>"<code>generational</code>": </b>
7013changes the collector to generational mode.
7014This is an experimental feature (see <a href="#2.5">&sect;2.5</a>).
7015</li>
7016
7017<li><b>"<code>incremental</code>": </b>
7018changes the collector to incremental mode.
7019This is the default mode.
7020</li>
7021
7022</ul>7154</ul>
7023 7155
7024 7156
@@ -7038,7 +7170,7 @@ to its caller (that is, <code>dofile</code> does not run in protected mode). · 13 unmodified lines
7038<p>7170<p>
7039<hr><h3><a name="pdf-error"><code>error (message [, level])</code></a></h3>7171<hr><h3><a name="pdf-error"><code>error (message [, level])</code></a></h3>
7040Terminates the last protected function called7172Terminates the last protected function called
7041and returns <code>message</code> as the error message.7173and returns <code>message</code> as the error object.
7042Function <code>error</code> never returns.7174Function <code>error</code> never returns.
7043 7175
7044 7176
@@ -7375,7 +7507,7 @@ according to the lexical conventions of Lua (see <a href="#3.1">&sect;3.1</a>). · 330 unmodified lines
7375 7507
7376<p>7508<p>
7377When called with <code>base</code>,7509When called with <code>base</code>,
7378then <code>e</code> should be a string to be interpreted as7510then <code>e</code> must be a string to be interpreted as
7379an integer numeral in that base.7511an integer numeral in that base.
7380The base may be any integer between 2 and 36, inclusive.7512The base may be any integer between 2 and 36, inclusive.
7381In bases above&nbsp;10, the letter '<code>A</code>' (in either upper or lower case)7513In 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
7466 7598
7467 7599
7468 7600
7601<p>
7602<hr><h3><a name="pdf-coroutine.isyieldable"><code>coroutine.isyieldable ()</code></a></h3>
7603
7604
7605<p>
7606Returns true when the running coroutine can yield.
7607
7608
7609<p>
7610A running coroutine is yieldable if it is not the main thread and
7611it is not inside a non-yieldable C function.
7612
7613
7614
7615
7469<p>7616<p>
7470<hr><h3><a name="pdf-coroutine.resume"><code>coroutine.resume (co [, val1, &middot;&middot;&middot;])</code></a></h3>7617<hr><h3><a name="pdf-coroutine.resume"><code>coroutine.resume (co [, val1, &middot;&middot;&middot;])</code></a></h3>
7471 7618
@@ -7485,8 +7632,8 @@ as the results from the yield. · 13 unmodified lines
7485<p>7632<p>
7486If the coroutine runs without any errors,7633If the coroutine runs without any errors,
7487<code>resume</code> returns <b>true</b> plus any values passed to <code>yield</code>7634<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 function7635(when the coroutine yields) or any values returned by the body function
7489(if the coroutine terminates).7636(when the coroutine terminates).
7490If there is any error,7637If there is any error,
7491<code>resume</code> returns <b>false</b> plus the error message.7638<code>resume</code> returns <b>false</b> plus the error message.
7492 7639
@@ -7588,7 +7735,7 @@ for <a href="#pdf-package.searchers"><code>package.searchers</code></a>. · 95 unmodified lines
7588<p>7735<p>
7589First <code>require</code> queries <code>package.preload[modname]</code>.7736First <code>require</code> queries <code>package.preload[modname]</code>.
7590If it has a value,7737If it has a value,
7591this value (which should be a function) is the loader.7738this value (which must be a function) is the loader.
7592Otherwise <code>require</code> searches for a Lua loader using the7739Otherwise <code>require</code> searches for a Lua loader using the
7593path stored in <a href="#pdf-package.path"><code>package.path</code></a>.7740path stored in <a href="#pdf-package.path"><code>package.path</code></a>.
7594If that also fails, it searches for a C&nbsp;loader using the7741If 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
7663<p>7810<p>
7664Lua initializes the C&nbsp;path <a href="#pdf-package.cpath"><code>package.cpath</code></a> in the same way7811Lua initializes the C&nbsp;path <a href="#pdf-package.cpath"><code>package.cpath</code></a> in the same way
7665it initializes the Lua path <a href="#pdf-package.path"><code>package.path</code></a>,7812it initializes the Lua path <a href="#pdf-package.path"><code>package.path</code></a>,
7666using the environment variable <a name="pdf-LUA_CPATH_5_2"><code>LUA_CPATH_5_2</code></a>7813using the environment variable <a name="pdf-LUA_CPATH_5_3"><code>LUA_CPATH_5_3</code></a>
7667or the environment variable <a name="pdf-LUA_CPATH"><code>LUA_CPATH</code></a>7814or the environment variable <a name="pdf-LUA_CPATH"><code>LUA_CPATH</code></a>
7668or a default path defined in <code>luaconf.h</code>.7815or a default path defined in <code>luaconf.h</code>.
7669 7816
@@ -7741,7 +7888,7 @@ The path used by <a href="#pdf-require"><code>require</code></a> to search for a · 71 unmodified lines
7741 7888
7742<p>7889<p>
7743At start-up, Lua initializes this variable with7890At start-up, Lua initializes this variable with
7744the value of the environment variable <a name="pdf-LUA_PATH_5_2"><code>LUA_PATH_5_2</code></a> or7891the value of the environment variable <a name="pdf-LUA_PATH_5_3"><code>LUA_PATH_5_3</code></a> or
7745the environment variable <a name="pdf-LUA_PATH"><code>LUA_PATH</code></a> or7892the environment variable <a name="pdf-LUA_PATH"><code>LUA_PATH</code></a> or
7746with a default path defined in <code>luaconf.h</code>,7893with a default path defined in <code>luaconf.h</code>,
7747if those environment variables are not defined.7894if those environment variables are not defined.
@@ -7973,6 +8120,45 @@ about the function · 225 unmodified lines
7973 8120
7974 8121
7975 8122
8123<p>
8124<hr><h3><a name="pdf-string.dumpfloat"><code>string.dumpfloat (n [, size [, endianness]])</code></a></h3>
8125Returns a string with the machine representation of float <code>n</code>,
8126with given size and endianness.
8127The <code>size</code> is the string "<code>f</code>" (single precision),
8128"<code>d</code>" (double precision), or "<code>n</code>",
8129which means the size of a <a href="#lua_Number"><code>lua_Number</code></a>;
8130its default is "<code>n</code>".
8131The endianness is the string "<code>l</code>" (little endian), "<code>b</code>" (big endian),
8132or "<code>n</code>" (native);
8133the default is the native endianness.
8134
8135
8136<p>
8137This function may not work correctly in architectures
8138with mixed endian.
8139
8140
8141<p>
8142<hr><h3><a name="pdf-string.dumpint"><code>string.dumpint (n [, size [, endianness]])</code></a></h3>
8143Returns a string with the two-complement representation of integer <code>n</code>,
8144with <code>size</code> bytes and given endianness.
8145The <code>size</code> can be any value from 1 to 12,
8146or 0, which means the size of a <a href="#lua_Integer"><code>lua_Integer</code></a>
8147(8 bytes in standard Lua);
8148its default is zero.
8149The endianness is the string "<code>l</code>" (little endian), "<code>b</code>" (big endian),
8150or "<code>n</code>" (native);
8151the default is the native endianness.
8152
8153
8154<p>
8155This function may not work correctly in architectures
8156with mixed endian or
8157that do not use a two-complement representation for integers.
8158
8159
8160
8161
7976<p>8162<p>
7977<hr><h3><a name="pdf-string.find"><code>string.find (s, pattern [, init [, plain]])</code></a></h3>8163<hr><h3><a name="pdf-string.find"><code>string.find (s, pattern [, init [, plain]])</code></a></h3>
7978 8164
@@ -8195,45 +8381,6 @@ its default value is&nbsp;1 and can be negative. · 216 unmodified lines
8195 8381
8196 8382
8197 8383
8198<p>
8199<hr><h3><a name="pdf-string.packfloat"><code>string.packfloat (n [, size [, endianess]])</code></a></h3>
8200Returns a string with the machine representation of float <code>n</code>,
8201with given size and endianess.
8202The <code>size</code> is the string "<code>f</code>" (single precision),
8203"<code>d</code>" (double precision), or "<code>n</code>",
8204which means the size of a <a href="#lua_Number"><code>lua_Number</code></a>;
8205its default is St{n}.
8206The endianess is the string "<code>l</code>" (little endian), "<code>b</code>" (big endian),
8207or "<code>n</code>" (native);
8208the default is the native endianess.
8209
8210
8211<p>
8212(This function may not work correctly for non-native endianess
8213in 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>
8220Returns a string with the machine representation of integer <code>n</code>,
8221with <code>size</code> bytes and given endianess.
8222The <code>size</code> can be any value from 1 to 8 or 0,
8223which means the size of a <a href="#lua_Integer"><code>lua_Integer</code></a>;
8224its default is zero.
8225The endianess is the string "<code>l</code>" (little endian), "<code>b</code>" (big endian),
8226or "<code>n</code>" (native);
8227the default is the native endianess.
8228
8229
8230<p>
8231(This function may not work correctly for native endianess
8232in mixed-endian architectures.)
8233
8234
8235
8236
8237<p>8384<p>
8238<hr><h3><a name="pdf-string.rep"><code>string.rep (s, n [, sep])</code></a></h3>8385<hr><h3><a name="pdf-string.rep"><code>string.rep (s, n [, sep])</code></a></h3>
8239Returns a string that is the concatenation of <code>n</code> copies of8386Returns 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
8279 8426
8280 8427
8281<p>8428<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>
8283Reads the machine representation of a float starting at position8430Reads the machine representation of a float starting at position
8284<code>pos</code> in string <code>s</code> and returns that number.8431<code>pos</code> in string <code>s</code> and returns that number.
8285See <a href="#pdf-string.packfloat"><code>string.packfloat</code></a> for details about <code>size</code> and <code>endianess</code>.8432See <a href="#pdf-string.dumpfloat"><code>string.dumpfloat</code></a> for details about <code>size</code> and <code>endianness</code>.
8286 8433
8287 8434
8288 8435
8289 8436
8290<p>8437<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>
8292Reads the machine representation of an integer starting at position8439Reads the machine representation of an integer starting at position
8293<code>pos</code> in string <code>s</code> and returns that integer.8440<code>pos</code> in string <code>s</code> and returns that integer.
8294See <a href="#pdf-string.packint"><code>string.packint</code></a> for details about <code>size</code> and <code>endianess</code>.8441See <a href="#pdf-string.dumpint"><code>string.dumpint</code></a> for details about <code>size</code> and <code>endianness</code>.
8295 8442
8296 8443
8297<p>8444<p>
@@ -8517,7 +8664,7 @@ such as character classification, is outside its scope. · 219 unmodified lines
8517 8664
8518<p>8665<p>
8519Unless stated otherwise,8666Unless stated otherwise,
8520all functions that expect a byte index as a parameter8667all functions that expect a byte position as a parameter
8521assume that the given position is either the start of a byte sequence8668assume that the given position is either the start of a byte sequence
8522or one plus the length of the subject string.8669or one plus the length of the subject string.
8523As in the string library,8670As in the string library,
@@ -8572,24 +8719,29 @@ It raises an error if it meets any invalid byte sequence. · 48 unmodified lines
8572 8719
8573 8720
8574<p>8721<p>
8575<hr><h3><a name="pdf-utf8.len"><code>utf8.len (s [, i])</code></a></h3>8722<hr><h3><a name="pdf-utf8.len"><code>utf8.len (s [, i [, j]])</code></a></h3>
8576Returns the number of UTF-8 characters in string <code>s</code>,8723Returns the number of UTF-8 characters in string <code>s</code>
8577starting from position <code>i</code>.8724that start between positions <code>i</code> and @{j} (both inclusive).
8578The default for <code>i</code> is 1.8725The default for <code>i</code> is 1 and for <code>j</code> is -1.
8579Returns <b>nil</b> if the sufix of <code>s</code> starting at <code>i</code>8726If it finds any invalid byte sequence,
8580contains any invalid byte sequence.8727returns <b>nil</b> plus the position of the first invalid byte.
8581 8728
8582 8729
8583 8730
8584 8731
8585<p>8732<p>
8586<hr><h3><a name="pdf-utf8.offset"><code>utf8.offset (s, n [, i])</code></a></h3>8733<hr><h3><a name="pdf-utf8.offset"><code>utf8.offset (s, n [, i])</code></a></h3>
8587Returns the byte index where the encoding of the8734Returns the position (in bytes) where the encoding of the
8588<code>n</code>-th character of <code>s</code> starts,8735<code>n</code>-th character of <code>s</code>
8589counting from position <code>i</code>.8736(counting from position <code>i</code>) starts.
8590A negative <code>n</code> gets characters before position <code>i</code>.8737A negative <code>n</code> gets characters before position <code>i</code>.
8591The default for <code>i</code> is 1.8738The default for <code>i</code> is 1 when <code>n</code> is non-negative
8592Returns <b>nil</b> if the subject does not have such character.8739and <code>#s + 1</code> otherwise,
8740so that <code>utf8.offset(s, -n)</code> gets the offset of the
8741<code>n</code>-th character from the end of the string.
8742If the specified character is not in the subject
8743or right after its end,
8744the function returns <b>nil</b>.
8593 8745
8594 8746
8595<p>8747<p>
@@ -8616,7 +8768,7 @@ It provides all its functions inside the table <a name="pdf-table"><code>table</ · 20 unmodified lines
8616 8768
8617<p>8769<p>
8618Remember that, whenever an operation needs the length of a table,8770Remember that, whenever an operation needs the length of a table,
8619the table should be a proper sequence8771the table must be a proper sequence
8620or have a <code>__len</code> metamethod (see <a href="#3.4.7">&sect;3.4.7</a>).8772or have a <code>__len</code> metamethod (see <a href="#3.4.7">&sect;3.4.7</a>).
8621All functions ignore non-numeric keys8773All functions ignore non-numeric keys
8622in tables given as arguments.8774in 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
8739<h2>6.7 &ndash; <a name="6.7">Mathematical Functions</a></h2>8891<h2>6.7 &ndash; <a name="6.7">Mathematical Functions</a></h2>
8740 8892
8741<p>8893<p>
8742This library is an interface to the standard C&nbsp;math library.8894This library provides basic mathematical functions.
8743It provides all its functions inside the table <a name="pdf-math"><code>math</code></a>.8895It provides all its functions and constants inside the table <a name="pdf-math"><code>math</code></a>.
8744Unless stated otherwise,8896Functions with the annotation "<code>integer/float</code>" give
8745all functions in this library operate with and return floats.8897integer results for integer arguments
8898and float results for float (or mixed) arguments.
8899Rounding 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>)
8901return an integer when the result fits in the range of an integer,
8902otherwise they return a float.
8746 8903
8747 8904
8748<p>8905<p>
@@ -8750,7 +8907,7 @@ all functions in this library operate with and return floats. · 1 unmodified line
8750 8907
8751 8908
8752<p>8909<p>
8753Returns the absolute value of <code>x</code>.8910Returns the absolute value of <code>x</code>. (integer/float)
8754 8911
8755 8912
8756 8913
@@ -8776,54 +8933,41 @@ Returns the arc sine of <code>x</code> (in radians). · 19 unmodified lines
8776 8933
8777 8934
8778<p>8935<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>
8780 8937
8781 8938
8782<p>8939<p>
8783Returns the arc tangent of <code>x</code> (in radians).
8784
8785
8786 8940
8787
8788<p>
8789<hr><h3><a name="pdf-math.atan2"><code>math.atan2 (y, x)</code></a></h3>
8790
8791
8792<p>
8793Returns the arc tangent of <code>y/x</code> (in radians),8941Returns the arc tangent of <code>y/x</code> (in radians),
8794but uses the signs of both parameters to find the8942but uses the signs of both parameters to find the
8795quadrant of the result.8943quadrant of the result.
8796(It also handles correctly the case of <code>x</code> being zero.)8944(It also handles correctly the case of <code>x</code> being zero.)
8797 8945
8798 8946
8799
8800
8801<p>
8802<hr><h3><a name="pdf-math.ceil"><code>math.ceil (x)</code></a></h3>
8803
8804
8805<p>8947<p>
8806Returns the smallest integer larger than or equal to <code>x</code>.8948The default value for <code>x</code> is 1,
8949so that the call <code>math.atan(y)</code>
8950returns the arc tangent of <code>y</code>.
8807 8951
8808 8952
8809 8953
8810 8954
8811<p>8955<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>
8813 8957
8814 8958
8815<p>8959<p>
8816Returns the cosine of <code>x</code> (assumed to be in radians).8960Returns the smaller integral value larger than or equal to <code>x</code>.
8817 8961
8818 8962
8819 8963
8820 8964
8821<p>8965<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>
8823 8967
8824 8968
8825<p>8969<p>
8826Returns the hyperbolic cosine of <code>x</code>.8970Returns the cosine of <code>x</code> (assumed to be in radians).
8827 8971
8828 8972
8829 8973
@@ -8833,17 +8977,7 @@ Returns the hyperbolic cosine of <code>x</code>. · 3 unmodified lines
8833 8977
8834 8978
8835<p>8979<p>
8836Returns the angle <code>x</code> (given in radians) in degrees.8980Converts the angle <code>x</code> from radians to 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>
8846Returns the value <em>e<sup>x</sup></em>.
8847 8981
8848 8982
8849 8983
@@ -8853,8 +8987,7 @@ Returns the value <em>e<sup>x</sup></em>. · 3 unmodified lines
8853 8987
8854 8988
8855<p>8989<p>
8856Returns a float with the largest8990Returns the largest integral value smaller than or equal to <code>x</code>.
8857integral value smaller than or equal to <code>x</code>.
8858 8991
8859 8992
8860 8993
@@ -8865,20 +8998,7 @@ integral value smaller than or equal to <code>x</code>. · 4 unmodified lines
8865 8998
8866<p>8999<p>
8867Returns the remainder of the division of <code>x</code> by <code>y</code>9000Returns the remainder of the division of <code>x</code> by <code>y</code>
8868that rounds the quotient towards zero.9001that rounds the quotient towards zero. (integer/float)
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>
8878Returns <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
8880in the range <em>[0.5, 1)</em>
8881(or zero when <code>x</code> is zero).
8882 9002
8883 9003
8884 9004
@@ -8888,8 +9008,8 @@ in the range <em>[0.5, 1)</em> · 3 unmodified lines
8888 9008
8889 9009
8890<p>9010<p>
8891The value <code>HUGE_VAL</code>,9011The float value <code>HUGE_VAL</code>,
8892a value larger than or equal to any other numerical value.9012a value larger than any other numerical value.
8893 9013
8894 9014
8895 9015
@@ -8906,16 +9026,6 @@ returns <b>nil</b>. · 10 unmodified lines
8906 9026
8907 9027
8908 9028
8909<p>
8910<hr><h3><a name="pdf-math.ldexp"><code>math.ldexp (m, e)</code></a></h3>
8911
8912
8913<p>
8914Returns <em>m2<sup>e</sup></em> (<code>e</code> should be an integer).
8915
8916
8917
8918
8919<p>9029<p>
8920<hr><h3><a name="pdf-math.log"><code>math.log (x [, base])</code></a></h3>9030<hr><h3><a name="pdf-math.log"><code>math.log (x [, base])</code></a></h3>
8921 9031
@@ -8933,49 +9043,54 @@ The default for <code>base</code> is <em>e</em> · 11 unmodified lines
8933 9043
8934 9044
8935<p>9045<p>
8936Returns the maximum value among its arguments.9046Returns the argument with the maximum value,
9047according to the Lua operator <code>&lt;</code>. (integer/float)
8937 9048
8938 9049
8939 9050
8940 9051
8941<p>9052<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>
9054An integer with the maximum value for an integer.
8943 9055
8944 9056
8945<p>
8946Returns the minimum value among its arguments.
8947 9057
8948 9058
9059<p>
9060<hr><h3><a name="pdf-math.min"><code>math.min (x, &middot;&middot;&middot;)</code></a></h3>
8949 9061
8950 9062
8951<p>9063<p>
8952<hr><h3><a name="pdf-math.modf"><code>math.modf (x)</code></a></h3>9064Returns the argument with the minimum value,
9065according to the Lua operator <code>&lt;</code>. (integer/float)
9066
9067
8953 9068
8954 9069
8955<p>9070<p>
8956Returns two numbers,9071<hr><h3><a name="pdf-math.mininteger"><code>math.mininteger</code></a></h3>
8957the integral part of <code>x</code> and the fractional part of <code>x</code>.9072An integer with the minimum value for an integer.
8958 9073
8959 9074
8960 9075
8961 9076
8962<p>9077<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>
8964 9079
8965 9080
8966<p>9081<p>
8967The value of <em>&pi;</em>.9082Returns the integral part of <code>x</code> and the fractional part of <code>x</code>.
9083Its second result is always a float.
8968 9084
8969 9085
8970 9086
8971 9087
8972<p>9088<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>
8974 9090
8975 9091
8976<p>9092<p>
8977Returns <em>x<sup>y</sup></em>.9093The value of <em>&pi;</em>.
8978(You can also use the expression <code>x^y</code> to compute this value.)
8979 9094
8980 9095
8981 9096
@@ -8985,7 +9100,7 @@ Returns <em>x<sup>y</sup></em>. · 3 unmodified lines
8985 9100
8986 9101
8987<p>9102<p>
8988Returns the angle <code>x</code> (given in degrees) in radians.9103Converts the angle <code>x</code> from degrees to radians.
8989 9104
8990 9105
8991 9106
@@ -8994,22 +9109,23 @@ Returns the angle <code>x</code> (given in degrees) in radians. · 2 unmodified lines
8994<hr><h3><a name="pdf-math.random"><code>math.random ([m [, n]])</code></a></h3>9109<hr><h3><a name="pdf-math.random"><code>math.random ([m [, n]])</code></a></h3>
8995 9110
8996 9111
8997<p>
8998This function is an interface to the simple
8999pseudo-random generator function <code>rand</code> provided by Standard&nbsp;C.
9000(No guarantees can be given for its statistical properties.)
9001
9002
9003<p>9112<p>
9004When called without arguments,9113When called without arguments,
9005returns a uniform pseudo-random float9114returns a pseudo-random float with uniform distribution
9006in the range <em>[0,1)</em>. 9115in the range <em>[0,1)</em>.
9007When called with two integers <code>m</code> and <code>n</code>,9116When called with two integers <code>m</code> and <code>n</code>,
9008<code>math.random</code> returns a uniform pseudo-random9117<code>math.random</code> returns a pseudo-random integer
9009integer in the range <em>[m, n]</em>.9118with uniform distribution in the range <em>[m, n]</em>.
9119(The interval size must fit in a Lua integer.)
9010The call <code>math.random(n)</code> is equivalent to <code>math.random(1,n)</code>.9120The call <code>math.random(n)</code> is equivalent to <code>math.random(1,n)</code>.
9011 9121
9012 9122
9123<p>
9124This function is an interface to the underling
9125pseudo-random generator function provided by C.
9126No guarantees can be given for its statistical properties.
9127
9128
9013 9129
9014 9130
9015<p>9131<p>
@@ -9034,16 +9150,6 @@ Returns the sine of <code>x</code> (assumed to be in radians). · 18 unmodified lines
9034 9150
9035 9151
9036 9152
9037<p>
9038<hr><h3><a name="pdf-math.sinh"><code>math.sinh (x)</code></a></h3>
9039
9040
9041<p>
9042Returns the hyperbolic sine of <code>x</code>.
9043
9044
9045
9046
9047<p>9153<p>
9048<hr><h3><a name="pdf-math.sqrt"><code>math.sqrt (x)</code></a></h3>9154<hr><h3><a name="pdf-math.sqrt"><code>math.sqrt (x)</code></a></h3>
9049 9155
@@ -9065,16 +9171,6 @@ Returns the tangent of <code>x</code> (assumed to be in radians). · 15 unmodified lines
9065 9171
9066 9172
9067 9173
9068<p>
9069<hr><h3><a name="pdf-math.tanh"><code>math.tanh (x)</code></a></h3>
9070
9071
9072<p>
9073Returns the hyperbolic tangent of <code>x</code>.
9074
9075
9076
9077
9078<p>9174<p>
9079<hr><h3><a name="pdf-math.type"><code>math.type (x)</code></a></h3>9175<hr><h3><a name="pdf-math.type"><code>math.type (x)</code></a></h3>
9080 9176
@@ -9337,7 +9433,7 @@ Returns an iterator function that, · 256 unmodified lines
9337each time it is called,9433each time it is called,
9338reads the file according to the given formats.9434reads the file according to the given formats.
9339When no format is given,9435When no format is given,
9340uses "*l" as a default.9436uses "<code>l</code>" as a default.
9341As an example, the construction9437As an example, the construction
9342 9438
9343<pre>9439<pre>
@@ -9376,30 +9472,33 @@ The available formats are · 32 unmodified lines
9376 9472
9377<ul>9473<ul>
9378 9474
9379<li><b>"<code>*n</code>": </b>9475<li><b>"<code>n</code>": </b>
9380reads a number and returns it as a float.9476reads a numeral and returns it as a float or an integer,
9381If the input has only a prefix of a valid number9477following the lexical conventions of Lua.
9382(e.g., "<code>0x</code>" or "<code>3.4e-</code>"),9478(The numeral may have leading spaces and a sign.)
9383this format can read the entire prefix9479This format always reads the longest input sequence that
9384even when returning <b>nil</b>.9480is a valid prefix for a number;
9481if that prefix does not form a valid number
9482(e.g., an empty string, "<code>0x</code>", or "<code>3.4e-</code>"),
9483it is discarded and the function returns <b>nil</b>.
9385</li>9484</li>
9386 9485
9387<li><b>"<code>*i</code>": </b>9486<li><b>"<code>i</code>": </b>
9388reads an integral number and returns it as an integer.9487reads an integral number and returns it as an integer.
9389</li>9488</li>
9390 9489
9391<li><b>"<code>*a</code>": </b>9490<li><b>"<code>a</code>": </b>
9392reads the whole file, starting at the current position.9491reads the whole file, starting at the current position.
9393On end of file, it returns the empty string.9492On end of file, it returns the empty string.
9394</li>9493</li>
9395 9494
9396<li><b>"<code>*l</code>": </b>9495<li><b>"<code>l</code>": </b>
9397reads the next line skipping the end of line,9496reads the next line skipping the end of line,
9398returning <b>nil</b> on end of file.9497returning <b>nil</b> on end of file.
9399This is the default format.9498This is the default format.
9400</li>9499</li>
9401 9500
9402<li><b>"<code>*L</code>": </b>9501<li><b>"<code>L</code>": </b>
9403reads the next line keeping the end-of-line character (if present),9502reads the next line keeping the end-of-line character (if present),
9404returning <b>nil</b> on end of file.9503returning <b>nil</b> on end of file.
9405</li>9504</li>
@@ -9413,7 +9512,7 @@ or <b>nil</b> on end of file. · 7 unmodified lines
9413</li>9512</li>
9414 9513
9415</ul><p>9514</ul><p>
9416The formats "<code>*l</code>" and "<code>*L</code>" should be used only for text files.9515The formats "<code>l</code>" and "<code>L</code>" should be used only for text files.
9417 9516
9418 9517
9419 9518
@@ -9622,7 +9721,7 @@ When called without a <code>command</code>, · 202 unmodified lines
9622 9721
9623 9722
9624<p>9723<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>
9626 9725
9627 9726
9628<p>9727<p>
@@ -9799,6 +9898,32 @@ thread to operate over. · 170 unmodified lines
9799The default is always the current thread.9898The default is always the current thread.
9800 9899
9801 9900
9901<p>
9902<hr><h3><a name="pdf-debug.Csize"><code>debug.Csize (t)</code></a></h3>
9903
9904
9905<p>
9906Returns the size of the underlying representation of a given C type,
9907according 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>
9919For all options except '<code>b</code>',
9920the size is given in number of bytes;
9921for option '<code>b</code>',
9922the size is its number of bits.
9923
9924
9925
9926
9802<p>9927<p>
9803<hr><h3><a name="pdf-debug.debug"><code>debug.debug ()</code></a></h3>9928<hr><h3><a name="pdf-debug.debug"><code>debug.debug ()</code></a></h3>
9804 9929
@@ -9898,7 +10023,7 @@ and raises an error when called with a level out of range. · 93 unmodified lines
9898 10023
9899 10024
9900<p>10025<p>
9901Variable names starting with '<code>(</code>' (open parenthesis)10026Variable names starting with '<code>(</code>' (open parenthesis)
9902represent variables with no known names10027represent variables with no known names
9903(internal variables like loop control variables,10028(internal variables like loop control variables,
9904and variables from chunks saved without debug information).10029and 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
9943 10068
9944 10069
9945<p>10070<p>
9946Variable names starting with '<code>(</code>' (open parenthesis)10071Variable names starting with '<code>(</code>' (open parenthesis)
9947represent variables with no known names10072represent variables with no known names
9948(variables from chunks saved without debug information).10073(variables from chunks saved without debug information).
9949 10074
@@ -9962,18 +10087,6 @@ returns <b>nil</b>. · 12 unmodified lines
9962 10087
9963 10088
9964 10089
9965<p>
9966<hr><h3><a name="pdf-debug.numbits"><code>debug.numbits (t)</code></a></h3>
9967
9968
9969<p>
9970Returns the number of bits in the underlying representation
9971of an integer (if <code>t</code> is "<code>i</code>")
9972or a float (if <code>t</code> is "<code>f</code>").
9973
9974
9975
9976
9977<p>10090<p>
9978<hr><h3><a name="pdf-debug.sethook"><code>debug.sethook ([thread,] hook, mask [, count])</code></a></h3>10091<hr><h3><a name="pdf-debug.sethook"><code>debug.sethook ([thread,] hook, mask [, count])</code></a></h3>
9979 10092
@@ -10169,7 +10282,7 @@ and as <code>lua -</code> otherwise. · 189 unmodified lines
10169 10282
10170<p>10283<p>
10171When called without option <code>-E</code>, 10284When called without option <code>-E</code>,
10172the interpreter checks for an environment variable <a name="pdf-LUA_INIT_5_2"><code>LUA_INIT_5_2</code></a>10285the interpreter checks for an environment variable <a name="pdf-LUA_INIT_5_3"><code>LUA_INIT_5_3</code></a>
10173(or <a name="pdf-LUA_INIT"><code>LUA_INIT</code></a> if it is not defined)10286(or <a name="pdf-LUA_INIT"><code>LUA_INIT</code></a> if it is not defined)
10174before running any argument.10287before running any argument.
10175If the variable content has the format <code>@<em>filename</em></code>,10288If 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
10200 10313
10201 10314
10202<p>10315<p>
10203Before starting to run the script,10316Before running any code,
10204<code>lua</code> collects all arguments in the command line10317<code>lua</code> collects all command-line arguments
10205in a global table called <code>arg</code>.10318in a global table called <code>arg</code>.
10206The script name is stored at index 0,10319The script name goes to index 0,
10207the first argument after the script name goes to index 1,10320the first argument after the script name goes to index 1,
10208and so on.10321and so on.
10209Any arguments before the script name10322Any arguments before the script name
10210(that is, the interpreter name plus the options)10323(that is, the interpreter name plus its options)
10211go to negative indices.10324go to negative indices.
10212For instance, in the call10325For instance, in the call
10213 10326
10214<pre>10327<pre>
10215 $ lua -la b.lua t1 t210328 $ lua -la b.lua t1 t2
10216</pre><p>10329</pre><p>
10217the interpreter first runs the file <code>a.lua</code>,10330the table is like this:
10218then creates a table
10219 10331
10220<pre>10332<pre>
10221 arg = { [-2] = "lua", [-1] = "-la",10333 arg = { [-2] = "lua", [-1] = "-la",
10222 [0] = "b.lua",10334 [0] = "b.lua",
10223 [1] = "t1", [2] = "t2" }10335 [1] = "t1", [2] = "t2" }
10224</pre><p>10336</pre><p>
10225and finally runs the file <code>b.lua</code>.10337If there is no script in the call,
10226The script is called with <code>arg[1]</code>, <code>arg[2]</code>, ...10338the interpreter name goes to index 0,
10227as arguments;10339followed by the other arguments.
10228it can also access these arguments with the vararg expression '<code>...</code>'.10340For instance, the call
10341
10342<pre>
10343 $ lua -e "print(arg[1])"
10344</pre><p>
10345will print "<code>-e</code>".
10229 10346
10230 10347
10231<p>10348<p>
10232In interactive mode,10349In interactive mode,
10233if you write an incomplete statement,10350Lua repeatedly prompts and waits for a line.
10351After reading a line,
10352Lua first try to interpret the line as an expression.
10353If it succeeds, it prints its value.
10354Otherwise, it interprets the line as a statement.
10355If you write an incomplete statement,
10234the interpreter waits for its completion10356the interpreter waits for its completion
10235by issuing a different prompt.10357by issuing a different prompt.
10236 10358
@@ -10301,6 +10423,12 @@ Always recompile clients of the Lua API when · 64 unmodified lines
10301using a new version.10423using a new version.
10302 10424
10303 10425
10426<p>
10427Similarly, Lua versions can always change the internal representation
10428of precompiled chunks;
10429precompiled chunks are never compatible between different Lua versions.
10430
10431
10304<p>10432<p>
10305The standard paths in the official distribution may10433The standard paths in the official distribution may
10306change between versions.10434change between versions.
@@ -10325,8 +10453,8 @@ You can fix these differences by forcing a number to be a float · 18 unmodified lines
10325in particular writing constants with an ending <code>.0</code>10453in particular writing constants with an ending <code>.0</code>
10326or using <code>x = x + 0.0</code> to convert a variable.10454or using <code>x = x + 0.0</code> to convert a variable.
10327(This recommendation is only for a quick fix10455(This recommendation is only for a quick fix
10328for an occasional incompatibility,10456for an occasional incompatibility;
10329not as a general guideline for good programming.10457it is not a general guideline for good programming.
10330For good programming,10458For good programming,
10331use floats where you need floats10459use floats where you need floats
10332and integers where you need integers.)10460and integers where you need integers.)
@@ -10335,7 +10463,22 @@ and integers where you need integers.) · 2 unmodified lines
10335<p>10463<p>
10336Although not formally an incompatibility,10464Although not formally an incompatibility,
10337the proper differentiation between floats and integers10465the proper differentiation between floats and integers
10338can sometimes have a big impact on performance.10466have an impact on performance.
10467</li>
10468
10469<li>
10470The conversion of a float to a string now adds a <code>.0</code> suffix
10471to the result if it looks like an integer.
10472(For instance, the float 2.0 will be printed as <code>2.0</code>,
10473not as <code>2</code>.)
10474You should always use an explicit format
10475when you need a specific format for numbers.
10476
10477
10478<p>
10479(Formally this is not an incompatibility,
10480because Lua does not specify how numbers are formatted as strings,
10481but some programs assumed a specific format.)
10339</li>10482</li>
10340 10483
10341<li>10484<li>
@@ -10351,24 +10494,31 @@ The generational mode for the garbage collector was removed. · 9 unmodified lines
10351<h2>8.2 &ndash; <a name="8.2">Changes in the Libraries</a></h2>10494<h2>8.2 &ndash; <a name="8.2">Changes in the Libraries</a></h2>
10352<ul>10495<ul>
10353 10496
10354<li>
10355The 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
10357number has an integral value.
10358(For instance, the float 2.0 will be printed as <code>2.0</code>,
10359not as <code>2</code>.)
10360You should always use an explicit format
10361when you need a specific format for numbers.
10362</li>
10363
10364<li>10497<li>
10365The <code>bit32</code> library has been deprecated.10498The <code>bit32</code> library has been deprecated.
10366It is easy to require a compatible external library or,10499It is easy to require a compatible external library or,
10367better yet, to replace its functions with appropriate bitwise operations.10500better 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,
10369while the bitwise operators in Standard Lua operate on 64-bit integers.)10502while the bitwise operators in Standard Lua operate on 64-bit integers.)
10370</li>10503</li>
10371 10504
10505<li>
10506Option names in <a href="#pdf-io.read"><code>io.read</code></a> do not have a starting '<code>*</code>' anymore.
10507For compatibility, Lua will continue to ignore this character.
10508</li>
10509
10510<li>
10511The 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>.
10514You can replace <code>math.pow(x,y)</code> with <code>x^y</code>;
10515you can replace <code>math.atan2</code> with <code>math.atan</code>,
10516which now accepts two parameters;
10517you can replace <code>math.ldexp(x,exp)</code> with <code>x * 2.0^exp</code>.
10518For the other operations,
10519the best choice is to use an external library.
10520</li>
10521
10372</ul>10522</ul>
10373 10523
10374 10524
@@ -10380,7 +10530,15 @@ while the bitwise operators in Standard Lua operate on 64-bit integers.) · 5 unmodified lines
10380<ul>10530<ul>
10381 10531
10382<li>10532<li>
10383No changes (yet).10533Continuation functions now receive as parameters what they needed
10534to get through <code>lua_getctx</code>,
10535so <code>lua_getctx</code> has been removed.
10536Adapt your code accordingly.
10537</li>
10538
10539<li>
10540Function <a href="#lua_dump"><code>lua_dump</code></a> has an extra parameter, <code>strip</code>.
10541Use 0 as the value of this parameter to get the old behavior.
10384</li>10542</li>
10385 10543
10386</ul>10544</ul>
@@ -10456,11 +10614,12 @@ Here is the complete syntax of Lua in extended BNF. · 69 unmodified lines
10456 10614
10457 fieldsep ::= &lsquo;<b>,</b>&rsquo; | &lsquo;<b>;</b>&rsquo;10615 fieldsep ::= &lsquo;<b>,</b>&rsquo; | &lsquo;<b>;</b>&rsquo;
10458 10616
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; |
10460 &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; | 10619 &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; |
10461 <b>and</b> | <b>or</b>10620 <b>and</b> | <b>or</b>
10462 10621
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;
10464 10623
10465</pre>10624</pre>
10466 10625
@@ -10468,17 +10627,18 @@ Here is the complete syntax of Lua in extended BNF. · 1 unmodified line
10468 10627
10469 10628
10470 10629
10471}10630
10631
10472 10632
10473 10633
10474 10634
10475<HR>10635<HR>
10476<SMALL CLASS="footer">10636<SMALL CLASS="footer">
10477Last update:10637Last update:
10478Fri Mar 21 17:33:56 BRT 201410638Thu Jun 19 17:13:19 BRT 2014
10479</SMALL>10639</SMALL>
10480<!--10640<!--
10481Last change: updated for Lua 5.3.0 (work2)10641Last change: updated for Lua 5.3.0 (work3)
10482-->10642-->
10483 10643
10484</body></html>10644</body></html>
doc/readme.html+16−19
@@ -31,7 +31,7 @@ tt, kbd, code { · 30 unmodified lines
31<HR>31<HR>
32<H1>32<H1>
33<A HREF="http://www.lua.org/"><IMG SRC="logo.gif" ALT="Lua" BORDER=0></A>33<A HREF="http://www.lua.org/"><IMG SRC="logo.gif" ALT="Lua" BORDER=0></A>
34Welcome to Lua 5.3 (work2)34Welcome to Lua 5.3 (work3)
35</H1>35</H1>
36 36
37<P>37<P>
@@ -102,8 +102,8 @@ If you don't have the time or the inclination to compile Lua yourself, · 64 unmodified lines
102get a binary from102get a binary from
103<A HREF="http://lua-users.org/wiki/LuaBinaries">LuaBinaries</A>.103<A HREF="http://lua-users.org/wiki/LuaBinaries">LuaBinaries</A>.
104Try also104Try also
105<A HREF="http://luaforwindows.luaforge.net/">Lua for Windows</A>,105<A HREF="http://luadist.org/">LuaDist</A>,
106an easy-to-use distribution of Lua that includes many useful libraries.106a multi-platform distribution of Lua that includes batteries.
107 107
108<H3>Building Lua</H3>108<H3>Building Lua</H3>
109 109
@@ -114,8 +114,8 @@ Here are the details. · 4 unmodified lines
114<OL>114<OL>
115<LI>115<LI>
116Open a terminal window and move to116Open a terminal window and move to
117the top-level directory, which is named <TT>lua-5.3.0</TT>.117the top-level directory, which is named <TT>lua-5.3.0-work3</TT>.
118The Makefile there controls both the build process and the installation process.118The <TT>Makefile</TT> there controls both the build process and the installation process.
119<P>119<P>
120<LI>120<LI>
121 Do "<KBD>make</KBD>" and see if your platform is listed.121 Do "<KBD>make</KBD>" and see if your platform is listed.
@@ -140,7 +140,7 @@ and liblua.a (the library). · 18 unmodified lines
140<P>140<P>
141<LI>141<LI>
142 To check that Lua has been built correctly, do "<KBD>make test</KBD>"142 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.
144</OL>144</OL>
145<P>145<P>
146If you're running Linux and get compilation errors,146If you're running Linux and get compilation errors,
@@ -152,7 +152,7 @@ then try "<KBD>make linux MYLIBS=-ltermcap</KBD>". · 5 unmodified lines
152<P>152<P>
153 Once you have built Lua, you may want to install it in an official153 Once you have built Lua, you may want to install it in an official
154 place in your system. In this case, do "<KBD>make install</KBD>". The official154 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'll155 place and the way to install files are defined in the <TT>Makefile</TT>. You'll
156 probably need the right permissions to install files.156 probably need the right permissions to install files.
157 157
158<P>158<P>
@@ -162,11 +162,13 @@ then try "<KBD>make linux MYLIBS=-ltermcap</KBD>". · 3 unmodified lines
162<P>162<P>
163 To install Lua locally, do "<KBD>make local</KBD>".163 To install Lua locally, do "<KBD>make local</KBD>".
164 This will create a directory <TT>install</TT> with subdirectories164 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>,
166 and install Lua as listed below.166 and install Lua as listed below.
167 167
168 To install Lua locally, but in some other directory, do168 To install Lua locally, but in some other directory, do
169 "<KBD>make install INSTALL_TOP=xxx</KBD>", where xxx is your chosen directory.169 "<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.
170 172
171<DL CLASS="display">173<DL CLASS="display">
172<DT>174<DT>
@@ -176,7 +178,7 @@ then try "<KBD>make linux MYLIBS=-ltermcap</KBD>". · 3 unmodified lines
176<DT>178<DT>
177 include:179 include:
178<DD>180<DD>
179 lua.h luaconf.h lualib.h lauxlib.h lua.hpp181 lauxlib.h lua.h lua.hpp luaconf.h lualib.h
180<DT>182<DT>
181 lib:183 lib:
182<DD>184<DD>
@@ -190,8 +192,8 @@ then try "<KBD>make linux MYLIBS=-ltermcap</KBD>". · 7 unmodified lines
190<P>192<P>
191 These are the only directories you need for development.193 These are the only directories you need for development.
192 If you only want to run Lua programs,194 If you only want to run Lua programs,
193 you only need the files in bin and man.195 you only need the files in <TT>bin</TT> and <TT>man</TT>.
194 The files in include and lib are needed for196 The files in <TT>include</TT> and <TT>lib</TT> are needed for
195 embedding Lua in C or C++ programs.197 embedding Lua in C or C++ programs.
196 198
197<H3><A NAME="customization">Customization</A></H3>199<H3><A NAME="customization">Customization</A></H3>
@@ -207,7 +209,7 @@ then try "<KBD>make linux MYLIBS=-ltermcap</KBD>". · 9 unmodified lines
207 You don't actually need to edit the Makefiles because you may set the209 You don't actually need to edit the Makefiles because you may set the
208 relevant variables in the command line when invoking make.210 relevant variables in the command line when invoking make.
209 Nevertheless, it's probably best to edit and save the Makefiles to211 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.
211 213
212<P>214<P>
213 On the other hand, if you need to customize some Lua features, you'll need215 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
216 it will be used by any Lua clients that you build, to ensure consistency.218 it will be used by any Lua clients that you build, to ensure consistency.
217 Further customization is available to experts by editing the Lua sources.219 Further customization is available to experts by editing the Lua sources.
218 220
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
224<H3><A NAME="other">Building Lua on other systems</A></H3>221<H3><A NAME="other">Building Lua on other systems</A></H3>
225 222
226<P>223<P>
@@ -357,10 +354,10 @@ THE SOFTWARE. · 130 unmodified lines
357<HR>354<HR>
358<SMALL CLASS="footer">355<SMALL CLASS="footer">
359Last update:356Last update:
360Fri Mar 21 17:33:08 BRT 2014357Wed Jun 11 22:54:18 BRT 2014
361</SMALL>358</SMALL>
362<!--359<!--
363Last change: updated for Lua 5.3.0 (work2)360Last change: updated for Lua 5.3.0 (work3)
364-->361-->
365 362
366</BODY>363</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
109 $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_MACOSX" SYSLIBS="-lreadline" CC=cc109 $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_MACOSX" SYSLIBS="-lreadline" CC=cc
110 110
111mingw:111mingw:
112 $(MAKE) "LUA_A=lua52.dll" "LUA_T=lua.exe" \112 $(MAKE) "LUA_A=lua53.dll" "LUA_T=lua.exe" \
113 "AR=$(CC) -shared -o" "RANLIB=strip --strip-unneeded" \113 "AR=$(CC) -shared -o" "RANLIB=strip --strip-unneeded" \
114 "SYSCFLAGS=-DLUA_BUILD_AS_DLL" "SYSLIBS=" "SYSLDFLAGS=-s" lua.exe114 "SYSCFLAGS=-DLUA_BUILD_AS_DLL" "SYSLIBS=" "SYSLDFLAGS=-s" lua.exe
115 $(MAKE) "LUAC_T=luac.exe" luac.exe115 $(MAKE) "LUAC_T=luac.exe" luac.exe
@@ -118,7 +118,7 @@ posix: · 2 unmodified lines
118 $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX"118 $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX"
119 119
120solaris:120solaris:
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"
122 122
123# list targets that do not create files (but not all makes understand .PHONY)123# list targets that do not create files (but not all makes understand .PHONY)
124.PHONY: all $(PLATS) default o a clean depend echo none124.PHONY: all $(PLATS) default o a clean depend echo none
src/lapi.c+46−54
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Lua API3** Lua API
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -94,6 +94,7 @@ LUA_API int lua_checkstack (lua_State *L, int size) { · 88 unmodified lines
94 int res;94 int res;
95 CallInfo *ci = L->ci;95 CallInfo *ci = L->ci;
96 lua_lock(L);96 lua_lock(L);
97 api_check(L, size >= 0, "negative 'size'");
97 if (L->stack_last - L->top > size) /* stack large enough? */98 if (L->stack_last - L->top > size) /* stack large enough? */
98 res = 1; /* yes; check is OK */99 res = 1; /* yes; check is OK */
99 else { /* no; need to grow stack */100 else { /* no; need to grow stack */
@@ -180,26 +181,35 @@ LUA_API void lua_settop (lua_State *L, int idx) { · 80 unmodified lines
180}181}
181 182
182 183
183LUA_API void lua_remove (lua_State *L, int idx) {184/*
184 StkId p;185** Reverse the stack segment from 'from' to 'to'
185 lua_lock(L);186** (auxiliar to 'lua_rotate')
186 p = index2addr(L, idx);187*/
187 api_checkstackindex(L, idx, p);188static void reverse (lua_State *L, StkId from, StkId to) {
188 while (++p < L->top) setobjs2s(L, p-1, p);189 for (; from < to; from++, to--) {
189 L->top--;190 TValue temp;
190 lua_unlock(L);191 setobj(L, &temp, from);
192 setobjs2s(L, from, to);
193 setobj2s(L, to, &temp);
194 }
191}195}
192 196
193 197
194LUA_API void lua_insert (lua_State *L, int idx) {198/*
195 StkId p;199** Let x = AB, where A is a prefix of length 'n'. Then,
196 StkId q;200** rotate x n == BA. But BA == (A^r . B^r)^r.
201*/
202LUA_API void lua_rotate (lua_State *L, int idx, int n) {
203 StkId p, t, m;
197 lua_lock(L);204 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 */
199 api_checkstackindex(L, idx, p);207 api_checkstackindex(L, idx, p);
200 for (q = L->top; q > p; q--) /* use L->top as a temporary */208 api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'");
201 setobjs2s(L, q, q - 1);209 m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */
202 setobjs2s(L, p, L->top);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 */
203 lua_unlock(L);213 lua_unlock(L);
204}214}
205 215
@@ -332,18 +342,11 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { · 126 unmodified lines
332}342}
333 343
334 344
335LUA_API int lua_strtonum (lua_State *L, const char *s, size_t len) {345LUA_API size_t lua_strtonum (lua_State *L, const char *s) {
336 lua_Integer i; lua_Number n;346 size_t sz = luaO_str2num(s, L->top);
337 if (luaO_str2int(s, len, &i)) { /* try as an integer */347 if (sz != 0)
338 setivalue(L->top, i);348 api_incr_top(L);
339 }349 return sz;
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;
347}350}
348 351
349 352
@@ -375,12 +378,12 @@ LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *pisnum) { · 25 unmodified lines
375 int isnum = 0;378 int isnum = 0;
376 switch (ttype(o)) {379 switch (ttype(o)) {
377 case LUA_TNUMINT: {380 case LUA_TNUMINT: {
378 res = cast_unsigned(ivalue(o));381 res = l_castS2U(ivalue(o));
379 isnum = 1;382 isnum = 1;
380 break;383 break;
381 }384 }
382 case LUA_TNUMFLT: { /* compute floor(n) % 2^(numbits in an integer) */385 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);
384 lua_Number n = fltvalue(o); /* get value */387 lua_Number n = fltvalue(o); /* get value */
385 int neg = 0;388 int neg = 0;
386 n = l_floor(n); /* get its floor */389 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
388 neg = 1;391 neg = 1;
389 n = -n; /* make 'n' positive, so that 'fmod' is the same as '%' */392 n = -n; /* make 'n' positive, so that 'fmod' is the same as '%' */
390 }393 }
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) */
392 if (luai_numisnan(n)) /* not a number? */395 if (luai_numisnan(n)) /* not a number? */
393 break; /* not an integer, too */396 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 */
395 if (neg) res = 0u - res; /* back to negative, if needed */398 if (neg) res = 0u - res; /* back to negative, if needed */
396 isnum = 1;399 isnum = 1;
397 break;400 break;
@@ -495,9 +498,7 @@ LUA_API void lua_pushnil (lua_State *L) { · 97 unmodified lines
495 498
496LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {499LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
497 lua_lock(L);500 lua_lock(L);
498 setnvalue(L->top, n);501 setfltvalue(L->top, n);
499 luai_checknum(L, L->top,
500 luaG_runerror(L, "C API - attempt to push a signaling NaN"));
501 api_incr_top(L);502 api_incr_top(L);
502 lua_unlock(L);503 lua_unlock(L);
503}504}
@@ -513,7 +514,7 @@ LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { · 9 unmodified lines
513 514
514LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) {515LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) {
515 lua_lock(L);516 lua_lock(L);
516 setivalue(L->top, cast_integer(u));517 setivalue(L->top, l_castU2S(u));
517 api_incr_top(L);518 api_incr_top(L);
518 lua_unlock(L);519 lua_unlock(L);
519}520}
@@ -579,15 +580,15 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { · 59 unmodified lines
579 setfvalue(L->top, fn);580 setfvalue(L->top, fn);
580 }581 }
581 else {582 else {
582 Closure *cl;583 CClosure *cl;
583 api_checknelems(L, n);584 api_checknelems(L, n);
584 api_check(L, n <= MAXUPVAL, "upvalue index too large");585 api_check(L, n <= MAXUPVAL, "upvalue index too large");
585 luaC_checkGC(L);586 luaC_checkGC(L);
586 cl = luaF_newCclosure(L, n);587 cl = luaF_newCclosure(L, n);
587 cl->c.f = fn;588 cl->f = fn;
588 L->top -= n;589 L->top -= n;
589 while (n--) {590 while (n--) {
590 setobj2n(L, &cl->c.upvalue[n], L->top + n);591 setobj2n(L, &cl->upvalue[n], L->top + n);
591 /* does not need barrier because closure is white */592 /* does not need barrier because closure is white */
592 }593 }
593 setclCvalue(L, L->top, cl);594 setclCvalue(L, L->top, cl);
@@ -741,7 +742,7 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) { · 147 unmodified lines
741}742}
742 743
743 744
744LUA_API void lua_getuservalue (lua_State *L, int idx) {745LUA_API int lua_getuservalue (lua_State *L, int idx) {
745 StkId o;746 StkId o;
746 lua_lock(L);747 lua_lock(L);
747 o = index2addr(L, idx);748 o = index2addr(L, idx);
@@ -749,6 +750,7 @@ LUA_API void lua_getuservalue (lua_State *L, int idx) { · 1 unmodified line
749 getuservalue(L, rawuvalue(o), L->top);750 getuservalue(L, rawuvalue(o), L->top);
750 api_incr_top(L);751 api_incr_top(L);
751 lua_unlock(L);752 lua_unlock(L);
753 return ttnov(L->top - 1);
752}754}
753 755
754 756
@@ -898,17 +900,8 @@ LUA_API void lua_setuservalue (lua_State *L, int idx) { · 143 unmodified lines
898 "results from function overflow current stack size")900 "results from function overflow current stack size")
899 901
900 902
901LUA_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
910LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx,903LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx,
911 lua_CFunction k) {904 lua_KFunction k) {
912 StkId func;905 StkId func;
913 lua_lock(L);906 lua_lock(L);
914 api_check(L, k == NULL || !isLua(L->ci),907 api_check(L, k == NULL || !isLua(L->ci),
@@ -947,7 +940,7 @@ static void f_call (lua_State *L, void *ud) { · 32 unmodified lines
947 940
948 941
949LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,942LUA_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) {
951 struct CallS c;944 struct CallS c;
952 int status;945 int status;
953 ptrdiff_t func;946 ptrdiff_t func;
@@ -975,11 +968,10 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, · 21 unmodified lines
975 ci->u.c.ctx = ctx; /* save context */968 ci->u.c.ctx = ctx; /* save context */
976 /* save information for error recovery */969 /* save information for error recovery */
977 ci->extra = savestack(L, c.func);970 ci->extra = savestack(L, c.func);
978 ci->u.c.old_allowhook = L->allowhook;
979 ci->u.c.old_errfunc = L->errfunc;971 ci->u.c.old_errfunc = L->errfunc;
980 L->errfunc = func;972 L->errfunc = func;
981 /* mark that function may do error recovery */973 setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */
982 ci->callstatus |= CIST_YPCALL;974 ci->callstatus |= CIST_YPCALL; /* function can do error recovery */
983 luaD_call(L, c.func, nresults, 1); /* do the call */975 luaD_call(L, c.func, nresults, 1); /* do the call */
984 ci->callstatus &= ~CIST_YPCALL;976 ci->callstatus &= ~CIST_YPCALL;
985 L->errfunc = ci->u.c.old_errfunc;977 L->errfunc = ci->u.c.old_errfunc;
src/lauxlib.c+6−11
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Auxiliary functions for building Lua libraries3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -397,7 +397,7 @@ LUALIB_API lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number def) { · 391 unmodified lines
397 397
398static void interror (lua_State *L, int arg) {398static void interror (lua_State *L, int arg) {
399 if (lua_type(L, arg) == LUA_TNUMBER)399 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");
401 else401 else
402 tag_error(L, arg, LUA_TNUMBER);402 tag_error(L, arg, LUA_TNUMBER);
403}403}
@@ -757,13 +757,8 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { · 353 unmodified lines
757 case LUA_TNUMBER: {757 case LUA_TNUMBER: {
758 if (lua_isinteger(L, idx))758 if (lua_isinteger(L, idx))
759 lua_pushfstring(L, "%I", lua_tointeger(L, idx));759 lua_pushfstring(L, "%I", lua_tointeger(L, idx));
760 else {760 else
761 const char *s = lua_pushfstring(L, "%f", lua_tonumber(L, idx));761 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 }
767 break;762 break;
768 }763 }
769 case LUA_TSTRING:764 case LUA_TSTRING:
@@ -966,12 +961,12 @@ LUALIB_API lua_State *luaL_newstate (void) { · 196 unmodified lines
966}961}
967 962
968 963
969LUALIB_API void luaL_checkversion_ (lua_State *L, int ver, size_t sz) {964LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) {
970 const lua_Number *v = lua_version(L);965 const lua_Number *v = lua_version(L);
971 if (v != lua_version(NULL))966 if (v != lua_version(NULL))
972 luaL_error(L, "multiple Lua VMs detected");967 luaL_error(L, "multiple Lua VMs detected");
973 else if (*v != ver)968 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",
975 ver, *v);970 ver, *v);
976 /* check numeric types */971 /* check numeric types */
977 if (sz != LUAL_NUMSIZES)972 if (sz != LUAL_NUMSIZES)
src/lauxlib.h+2−2
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Auxiliary functions for building Lua libraries3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -28,7 +28,7 @@ typedef struct luaL_Reg { · 22 unmodified lines
28 28
29#define LUAL_NUMSIZES (sizeof(lua_Integer)*16 + sizeof(lua_Number))29#define LUAL_NUMSIZES (sizeof(lua_Integer)*16 + sizeof(lua_Number))
30 30
31LUALIB_API void (luaL_checkversion_) (lua_State *L, int ver, size_t sz);31LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver, size_t sz);
32#define luaL_checkversion(L) \32#define luaL_checkversion(L) \
33 luaL_checkversion_(L, LUA_VERSION_NUM, LUAL_NUMSIZES)33 luaL_checkversion_(L, LUA_VERSION_NUM, LUAL_NUMSIZES)
34 34
src/lbaselib.c+51−41
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Basic library3** Basic library
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -45,31 +45,31 @@ static int luaB_print (lua_State *L) { · 39 unmodified lines
45 45
46#define SPACECHARS " \f\n\r\t\v"46#define SPACECHARS " \f\n\r\t\v"
47 47
48static int b_str2int (const char *s, const char *e, int base, lua_Integer *pn) {48static const char *b_str2int (const char *s, int base, lua_Integer *pn) {
49 lua_Unsigned n = 0;49 lua_Unsigned n = 0;
50 int neg = 0;50 int neg = 0;
51 s += strspn(s, SPACECHARS); /* skip initial spaces */51 s += strspn(s, SPACECHARS); /* skip initial spaces */
52 if (*s == '-') { s++; neg = 1; } /* handle signal */52 if (*s == '-') { s++; neg = 1; } /* handle signal */
53 else if (*s == '+') s++;53 else if (*s == '+') s++;
54 if (!isalnum((unsigned char)*s)) /* no digit? */54 if (!isalnum((unsigned char)*s)) /* no digit? */
55 return 0;55 return NULL;
56 do {56 do {
57 int digit = (isdigit((unsigned char)*s)) ? *s - '0'57 int digit = (isdigit((unsigned char)*s)) ? *s - '0'
58 : toupper((unsigned char)*s) - 'A' + 10;58 : toupper((unsigned char)*s) - 'A' + 10;
59 if (digit >= base) return 0; /* invalid numeral */59 if (digit >= base) return NULL; /* invalid numeral */
60 n = n * base + digit;60 n = n * base + digit;
61 s++;61 s++;
62 } while (isalnum((unsigned char)*s));62 } while (isalnum((unsigned char)*s));
63 s += strspn(s, SPACECHARS); /* skip trailing spaces */63 s += strspn(s, SPACECHARS); /* skip trailing spaces */
64 if (s != e) /* invalid trailing characters? */64 if (*s != '\0') /* invalid trailing characters? */
65 return 0;65 return NULL;
66 *pn = (lua_Integer)((neg) ? (0u - n) : n);66 *pn = (lua_Integer)((neg) ? (0u - n) : n);
67 return 1;67 return s;
68}68}
69 69
70 70
71static int luaB_tonumber (lua_State *L) {71static int luaB_tonumber (lua_State *L) {
72 if (lua_isnoneornil(L, 2)) { /* standard conversion */72 if (lua_isnoneornil(L, 2)) { /* standard conversion? */
73 luaL_checkany(L, 1);73 luaL_checkany(L, 1);
74 if (lua_type(L, 1) == LUA_TNUMBER) { /* already a number? */74 if (lua_type(L, 1) == LUA_TNUMBER) { /* already a number? */
75 lua_settop(L, 1); /* yes; return it */75 lua_settop(L, 1); /* yes; return it */
@@ -78,20 +78,20 @@ static int luaB_tonumber (lua_State *L) { · 2 unmodified lines
78 else {78 else {
79 size_t l;79 size_t l;
80 const char *s = lua_tolstring(L, 1, &l);80 const char *s = lua_tolstring(L, 1, &l);
81 if (s != NULL && lua_strtonum(L, s, l)) /* can convert to a number? */81 if (s != NULL && lua_strtonum(L, s) == l + 1)
82 return 1;82 return 1; /* successful conversion to number */
83 /* else not a number */83 /* else not a number */
84 }84 }
85 }85 }
86 else {86 else {
87 size_t l;87 size_t l;
88 const char *s;88 const char *s;
89 lua_Integer n;89 lua_Integer n = 0; /* to avoid warnings */
90 int base = luaL_checkint(L, 2);90 int base = luaL_checkint(L, 2);
91 luaL_checktype(L, 1, LUA_TSTRING); /* before 'luaL_checklstring'! */91 luaL_checktype(L, 1, LUA_TSTRING); /* before 'luaL_checklstring'! */
92 s = luaL_checklstring(L, 1, &l);92 s = luaL_checklstring(L, 1, &l);
93 luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");93 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) {
95 lua_pushinteger(L, n);95 lua_pushinteger(L, n);
96 return 1;96 return 1;
97 } /* else not a number */97 } /* else not a number */
@@ -341,7 +341,8 @@ static int luaB_load (lua_State *L) { · 243 unmodified lines
341/* }====================================================== */341/* }====================================================== */
342 342
343 343
344static int dofilecont (lua_State *L) {344static int dofilecont (lua_State *L, int d1, int d2) {
345 (void)d1; (void)d2; /* only to match 'lua_Kfunction' prototype */
345 return lua_gettop(L) - 1;346 return lua_gettop(L) - 1;
346}347}
347 348
@@ -352,14 +353,19 @@ static int luaB_dofile (lua_State *L) { · 4 unmodified lines
352 if (luaL_loadfile(L, fname) != LUA_OK)353 if (luaL_loadfile(L, fname) != LUA_OK)
353 return lua_error(L);354 return lua_error(L);
354 lua_callk(L, 0, LUA_MULTRET, 0, dofilecont);355 lua_callk(L, 0, LUA_MULTRET, 0, dofilecont);
355 return dofilecont(L);356 return dofilecont(L, 0, 0);
356}357}
357 358
358 359
359static int luaB_assert (lua_State *L) {360static int luaB_assert (lua_State *L) {
360 if (!lua_toboolean(L, 1))361 if (lua_toboolean(L, 1)) /* condition is true? */
361 return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!"));362 return lua_gettop(L); /* return all arguments */
362 return lua_gettop(L);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 }
363}369}
364 370
365 371
@@ -379,44 +385,48 @@ static int luaB_select (lua_State *L) { · 13 unmodified lines
379}385}
380 386
381 387
382static int finishpcall (lua_State *L, int status) {388/*
383 if (!lua_checkstack(L, 1)) { /* no space for extra boolean? */389** Continuation function for 'pcall' and 'xpcall'. Both functions
384 lua_settop(L, 0); /* create space for return values */390** already pushed a 'true' before doing the call, so in case of sucess
385 lua_pushboolean(L, 0);391** 'finishpcall' only has to return everything in the stack minus
386 lua_pushstring(L, "stack overflow");392** 'extra' values (where 'extra' is exactly the number of items to be
393** ignored).
394*/
395static 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 */
387 return 2; /* return false, msg */399 return 2; /* return false, msg */
388 }400 }
389 lua_pushboolean(L, status); /* first result (status) */401 else
390 lua_replace(L, 1); /* put first result in first slot */402 return lua_gettop(L) - extra; /* return all results */
391 return lua_gettop(L);
392}
393
394
395static int pcallcont (lua_State *L) {
396 int status = lua_getctx(L, NULL);
397 return finishpcall(L, (status == LUA_YIELD));
398}403}
399 404
400 405
401static int luaB_pcall (lua_State *L) {406static int luaB_pcall (lua_State *L) {
402 int status;407 int status;
403 luaL_checkany(L, 1);408 luaL_checkany(L, 1);
404 lua_pushnil(L);409 lua_pushboolean(L, 1); /* first result if no errors */
405 lua_insert(L, 1); /* create space for status result */410 lua_insert(L, 1); /* put it in place */
406 status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, pcallcont);411 status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, finishpcall);
407 return finishpcall(L, (status == LUA_OK));412 return finishpcall(L, status, 0);
408}413}
409 414
410 415
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*/
411static int luaB_xpcall (lua_State *L) {421static int luaB_xpcall (lua_State *L) {
412 int status;422 int status;
413 int n = lua_gettop(L);423 int n = lua_gettop(L);
414 luaL_argcheck(L, n >= 2, 2, "value expected");424 luaL_checktype(L, 2, LUA_TFUNCTION); /* check error function */
415 lua_pushvalue(L, 1); /* exchange function... */425 lua_pushboolean(L, 1); /* first result */
416 lua_copy(L, 2, 1); /* ...and error handler */426 lua_pushvalue(L, 1); /* function */
417 lua_replace(L, 2);427 lua_rotate(L, 3, 2); /* move them below function's arguments */
418 status = lua_pcallk(L, n - 2, LUA_MULTRET, 1, 0, pcallcont);428 status = lua_pcallk(L, n - 2, LUA_MULTRET, 2, 2, finishpcall);
419 return finishpcall(L, (status == LUA_OK));429 return finishpcall(L, status, 2);
420}430}
421 431
422 432
src/lbitlib.c+2−3
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Standard library for bitwise operations3** Standard library for bitwise operations
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -221,8 +221,7 @@ LUAMOD_API int luaopen_bit32 (lua_State *L) { · 215 unmodified lines
221 221
222 222
223LUAMOD_API int luaopen_bit32 (lua_State *L) {223LUAMOD_API int luaopen_bit32 (lua_State *L) {
224 lua_pushnil(L);224 return luaL_error(L, "library 'bit32' has been deprecated");
225 return 1;
226}225}
227 226
228#endif /* } */227#endif /* } */
src/lcode.c+10−11
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Code generator for Lua3** Code generator for Lua
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -46,7 +46,7 @@ static int tonumeral(expdesc *e, TValue *v) { · 40 unmodified lines
46 if (v) setivalue(v, e->u.ival);46 if (v) setivalue(v, e->u.ival);
47 return 1;47 return 1;
48 case VKFLT:48 case VKFLT:
49 if (v) setnvalue(v, e->u.nval);49 if (v) setfltvalue(v, e->u.nval);
50 return 1;50 return 1;
51 default: return 0;51 default: return 0;
52 }52 }
@@ -195,7 +195,7 @@ void luaK_patchlist (FuncState *fs, int list, int target) { · 142 unmodified lines
195}195}
196 196
197 197
198LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level) {198void luaK_patchclose (FuncState *fs, int list, int level) {
199 level++; /* argument is +1 to reserve 0 as non-op */199 level++; /* argument is +1 to reserve 0 as non-op */
200 while (list != NO_JUMP) {200 while (list != NO_JUMP) {
201 int next = getjump(fs, list);201 int next = getjump(fs, list);
@@ -317,7 +317,7 @@ static int addk (FuncState *fs, TValue *key, TValue *v) { · 115 unmodified lines
317 TValue *idx = luaH_set(L, fs->ls->h, key); /* index scanner table */317 TValue *idx = luaH_set(L, fs->ls->h, key); /* index scanner table */
318 int k, oldsize;318 int k, oldsize;
319 if (ttisinteger(idx)) { /* is there an index there? */319 if (ttisinteger(idx)) { /* is there an index there? */
320 k = ivalue(idx);320 k = cast_int(ivalue(idx));
321 /* correct value? (warning: must distinguish floats from integers!) */321 /* correct value? (warning: must distinguish floats from integers!) */
322 if (k < fs->nk && ttype(&f->k[k]) == ttype(v) &&322 if (k < fs->nk && ttype(&f->k[k]) == ttype(v) &&
323 luaV_rawequalobj(&f->k[k], v))323 luaV_rawequalobj(&f->k[k], v))
@@ -346,8 +346,9 @@ int luaK_stringK (FuncState *fs, TString *s) { · 22 unmodified lines
346 346
347 347
348/*348/*
349** use userdata as key to avoid collision with float with same value;349** Integers use userdata as keys to avoid collision with floats with same
350** conversion to 'void*' used only for hash, no "precision" problems350** value; conversion to 'void*' used only for hashing, no "precision"
351** problems
351*/352*/
352int luaK_intK (FuncState *fs, lua_Integer n) {353int luaK_intK (FuncState *fs, lua_Integer n) {
353 TValue k, o;354 TValue k, o;
@@ -359,13 +360,13 @@ int luaK_intK (FuncState *fs, lua_Integer n) { · 5 unmodified lines
359 360
360/*361/*
361** Both NaN and -0.0 should not go to the constant table, as they have362** 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** problems with the hashing. (NaN is not a valid key, -0.0 collides
363** -0.0 collides with +0.0.)364** with +0.0.)
364*/ 365*/
365static int luaK_numberK (FuncState *fs, lua_Number r) {366static int luaK_numberK (FuncState *fs, lua_Number r) {
366 TValue o;367 TValue o;
367 lua_assert(!luai_numisnan(r) && !isminuszero(r));368 lua_assert(!luai_numisnan(r) && !isminuszero(r));
368 setnvalue(&o, r);369 setfltvalue(&o, r);
369 return addk(fs, &o, &o);370 return addk(fs, &o, &o);
370}371}
371 372
@@ -768,8 +769,6 @@ static int validop (int op, TValue *v1, TValue *v2) { · 396 unmodified lines
768 return (tointeger(v1, &i) && tointeger(v2, &i));769 return (tointeger(v1, &i) && tointeger(v2, &i));
769 case LUA_OPMOD: /* integer module by 0 */770 case LUA_OPMOD: /* integer module by 0 */
770 return !(ttisinteger(v1) && ttisinteger(v2) && ivalue(v2) == 0);771 return !(ttisinteger(v1) && ttisinteger(v2) && ivalue(v2) == 0);
771 case LUA_OPPOW: /* negative integer exponentiation */
772 return !(ttisinteger(v1) && ttisinteger(v2) && ivalue(v2) < 0);
773 default: return 1; /* everything else is valid */772 default: return 1; /* everything else is valid */
774 }773 }
775}774}
src/lcorolib.c+17−5
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Coroutine Library3** Coroutine Library
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -17,6 +17,13 @@ · 11 unmodified lines
17#include "lualib.h"17#include "lualib.h"
18 18
19 19
20static 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
20static int auxresume (lua_State *L, lua_State *co, int narg) {27static int auxresume (lua_State *L, lua_State *co, int narg) {
21 int status;28 int status;
22 if (!lua_checkstack(co, narg)) {29 if (!lua_checkstack(co, narg)) {
@@ -47,9 +54,8 @@ static int auxresume (lua_State *L, lua_State *co, int narg) { · 24 unmodified lines
47 54
48 55
49static int luaB_coresume (lua_State *L) {56static int luaB_coresume (lua_State *L) {
50 lua_State *co = lua_tothread(L, 1);57 lua_State *co = getco(L);
51 int r;58 int r;
52 luaL_argcheck(L, co, 1, "coroutine expected");
53 r = auxresume(L, co, lua_gettop(L) - 1);59 r = auxresume(L, co, lua_gettop(L) - 1);
54 if (r < 0) {60 if (r < 0) {
55 lua_pushboolean(L, 0);61 lua_pushboolean(L, 0);
@@ -102,8 +108,7 @@ static int luaB_yield (lua_State *L) { · 46 unmodified lines
102 108
103 109
104static int luaB_costatus (lua_State *L) {110static int luaB_costatus (lua_State *L) {
105 lua_State *co = lua_tothread(L, 1);111 lua_State *co = getco(L);
106 luaL_argcheck(L, co, 1, "coroutine expected");
107 if (L == co) lua_pushliteral(L, "running");112 if (L == co) lua_pushliteral(L, "running");
108 else {113 else {
109 switch (lua_status(co)) {114 switch (lua_status(co)) {
@@ -129,6 +134,12 @@ static int luaB_costatus (lua_State *L) { · 19 unmodified lines
129}134}
130 135
131 136
137static int luaB_yieldable (lua_State *L) {
138 lua_pushboolean(L, lua_isyieldable(L));
139 return 1;
140}
141
142
132static int luaB_corunning (lua_State *L) {143static int luaB_corunning (lua_State *L) {
133 int ismain = lua_pushthread(L);144 int ismain = lua_pushthread(L);
134 lua_pushboolean(L, ismain);145 lua_pushboolean(L, ismain);
@@ -143,6 +154,7 @@ static const luaL_Reg co_funcs[] = { · 8 unmodified lines
143 {"status", luaB_costatus},154 {"status", luaB_costatus},
144 {"wrap", luaB_cowrap},155 {"wrap", luaB_cowrap},
145 {"yield", luaB_yield},156 {"yield", luaB_yield},
157 {"isyieldable", luaB_yieldable},
146 {NULL, NULL}158 {NULL, NULL}
147};159};
148 160
src/ldblib.c+117−70
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Interface from Lua to its debug API3** Interface from Lua to its debug API
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -21,16 +21,30 @@ · 15 unmodified lines
21#define HOOKKEY "_HKEY"21#define HOOKKEY "_HKEY"
22 22
23 23
24 24static int db_Csize (lua_State *L) {
25static int db_numbits (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 };
26 const char *s = luaL_checkstring(L, 1);39 const char *s = luaL_checkstring(L, 1);
27 if (*s == 'i')40 int i;
28 lua_pushinteger(L, sizeof(lua_Integer) * CHAR_BIT);41 for (i = 0; i < (int)(sizeof(sizes)/sizeof(sizes[0])); i++) {
29 else if (*s == 'f')42 if (*s == sizes[i].c) {
30 lua_pushinteger(L, sizeof(lua_Number) * CHAR_BIT);43 lua_pushinteger(L, sizes[i].sz);
31 else44 return 1;
32 luaL_argerror(L, 1, lua_pushfstring(L, "invalid option '%s'", s));45 }
33 return 1;46 }
47 return luaL_argerror(L, 1, lua_pushfstring(L, "invalid option '%c'", *s));
34}48}
35 49
36 50
@@ -77,24 +91,12 @@ static int db_setuservalue (lua_State *L) { · 40 unmodified lines
77}91}
78 92
79 93
80static void settabss (lua_State *L, const char *i, const char *v) {94/*
81 lua_pushstring(L, v);95** Auxiliary function used by several library functions: check for
82 lua_setfield(L, -2, i);96** an optional thread as function's first argument and set 'arg' with
83}97** 1 if this argument is present (so that functions can skip it to
84 98** access their other arguments)
85 99*/
86static void settabsi (lua_State *L, const char *i, int v) {
87 lua_pushinteger(L, v);
88 lua_setfield(L, -2, i);
89}
90
91
92static void settabsb (lua_State *L, const char *i, int v) {
93 lua_pushboolean(L, v);
94 lua_setfield(L, -2, i);
95}
96
97
98static lua_State *getthread (lua_State *L, int *arg) {100static lua_State *getthread (lua_State *L, int *arg) {
99 if (lua_isthread(L, 1)) {101 if (lua_isthread(L, 1)) {
100 *arg = 1;102 *arg = 1;
@@ -102,44 +104,70 @@ static lua_State *getthread (lua_State *L, int *arg) { · 1 unmodified line
102 }104 }
103 else {105 else {
104 *arg = 0;106 *arg = 0;
105 return L;107 return L; /* function will operate over current thread */
106 }108 }
107}109}
108 110
109 111
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*/
117static void settabss (lua_State *L, const char *k, const char *v) {
118 lua_pushstring(L, v);
119 lua_setfield(L, -2, k);
120}
121
122static void settabsi (lua_State *L, const char *k, int v) {
123 lua_pushinteger(L, v);
124 lua_setfield(L, -2, k);
125}
126
127static 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*/
110static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) {140static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) {
111 if (L == L1) {141 if (L == L1)
112 lua_pushvalue(L, -2);142 lua_rotate(L, -2, 1); /* exchange object and table */
113 lua_remove(L, -3);
114 }
115 else143 else
116 lua_xmove(L1, L, 1);144 lua_xmove(L1, L, 1); /* move object to the "main" stack */
117 lua_setfield(L, -2, fname);145 lua_setfield(L, -2, fname); /* put object into table */
118}146}
119 147
120 148
149/*
150** Calls 'lua_getinfo' and collects all results in a new table.
151*/
121static int db_getinfo (lua_State *L) {152static int db_getinfo (lua_State *L) {
122 lua_Debug ar;153 lua_Debug ar;
123 int arg;154 int arg;
124 lua_State *L1 = getthread(L, &arg);155 lua_State *L1 = getthread(L, &arg);
125 const char *options = luaL_optstring(L, arg+2, "flnStu");156 const char *options = luaL_optstring(L, arg+2, "flnStu");
126 if (lua_isnumber(L, arg+1)) {157 if (lua_isfunction(L, arg + 1)) { /* info about a function? */
127 if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), &ar)) {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)) {
128 lua_pushnil(L); /* level out of range */164 lua_pushnil(L); /* level out of range */
129 return 1;165 return 1;
130 }166 }
131 }167 }
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");
140 if (!lua_getinfo(L1, options, &ar))168 if (!lua_getinfo(L1, options, &ar))
141 return luaL_argerror(L, arg+2, "invalid option");169 return luaL_argerror(L, arg+2, "invalid option");
142 lua_createtable(L, 0, 2);170 lua_newtable(L); /* table to collect results */
143 if (strchr(options, 'S')) {171 if (strchr(options, 'S')) {
144 settabss(L, "source", ar.source);172 settabss(L, "source", ar.source);
145 settabss(L, "short_src", ar.short_src);173 settabss(L, "short_src", ar.short_src);
@@ -177,16 +205,16 @@ static int db_getlocal (lua_State *L) { · 31 unmodified lines
177 if (lua_isfunction(L, arg + 1)) { /* function argument? */205 if (lua_isfunction(L, arg + 1)) { /* function argument? */
178 lua_pushvalue(L, arg + 1); /* push function */206 lua_pushvalue(L, arg + 1); /* push function */
179 lua_pushstring(L, lua_getlocal(L, NULL, nvar)); /* push local name */207 lua_pushstring(L, lua_getlocal(L, NULL, nvar)); /* push local name */
180 return 1;208 return 1; /* return only name (there is no value) */
181 }209 }
182 else { /* stack-level argument */210 else { /* stack-level argument */
183 if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */211 if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */
184 return luaL_argerror(L, arg+1, "level out of range");212 return luaL_argerror(L, arg+1, "level out of range");
185 name = lua_getlocal(L1, &ar, nvar);213 name = lua_getlocal(L1, &ar, nvar);
186 if (name) {214 if (name) {
187 lua_xmove(L1, L, 1); /* push local value */215 lua_xmove(L1, L, 1); /* move local value */
188 lua_pushstring(L, name); /* push name */216 lua_pushstring(L, name); /* push name */
189 lua_pushvalue(L, -2); /* re-order */217 lua_rotate(L, -2, 1); /* re-order */
190 return 2;218 return 2;
191 }219 }
192 else {220 else {
@@ -211,14 +239,17 @@ static int db_setlocal (lua_State *L) { · 18 unmodified lines
211}239}
212 240
213 241
242/*
243** get (if 'get' is true) or set an upvalue from a closure
244*/
214static int auxupvalue (lua_State *L, int get) {245static int auxupvalue (lua_State *L, int get) {
215 const char *name;246 const char *name;
216 int n = luaL_checkint(L, 2);247 int n = luaL_checkint(L, 2); /* upvalue index */
217 luaL_checktype(L, 1, LUA_TFUNCTION);248 luaL_checktype(L, 1, LUA_TFUNCTION); /* closure */
218 name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);249 name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
219 if (name == NULL) return 0;250 if (name == NULL) return 0;
220 lua_pushstring(L, name);251 lua_pushstring(L, name);
221 lua_insert(L, -(get+1));252 lua_insert(L, -(get+1)); /* no-op if get is false */
222 return get + 1;253 return get + 1;
223}254}
224 255
@@ -234,13 +265,15 @@ static int db_setupvalue (lua_State *L) { · 9 unmodified lines
234}265}
235 266
236 267
268/*
269** Check whether a given upvalue from a given closure exists and
270** returns its index
271*/
237static int checkupval (lua_State *L, int argf, int argnup) {272static int checkupval (lua_State *L, int argf, int argnup) {
238 lua_Debug ar;273 int nup = luaL_checkint(L, argnup); /* upvalue index */
239 int nup = luaL_checkint(L, argnup);274 luaL_checktype(L, argf, LUA_TFUNCTION); /* closure */
240 luaL_checktype(L, argf, LUA_TFUNCTION);275 luaL_argcheck(L, (lua_getupvalue(L, argf, nup) != NULL), argnup,
241 lua_pushvalue(L, argf);276 "invalid upvalue index");
242 lua_getinfo(L, ">u", &ar);
243 luaL_argcheck(L, 1 <= nup && nup <= ar.nups, argnup, "invalid upvalue index");
244 return nup;277 return nup;
245}278}
246 279
@@ -262,25 +295,36 @@ static int db_upvaluejoin (lua_State *L) { · 15 unmodified lines
262}295}
263 296
264 297
298/*
299** The hook table (at registry[HOOKKEY]) maps threads to their current
300** hook function
301*/
265#define gethooktable(L) luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)302#define gethooktable(L) luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)
266 303
267 304
305/*
306** Call hook function registered at hook table for the current
307** thread (if there is one)
308*/
268static void hookf (lua_State *L, lua_Debug *ar) {309static void hookf (lua_State *L, lua_Debug *ar) {
269 static const char *const hooknames[] =310 static const char *const hooknames[] =
270 {"call", "return", "line", "count", "tail call"};311 {"call", "return", "line", "count", "tail call"};
271 gethooktable(L);312 gethooktable(L);
272 lua_pushthread(L);313 lua_pushthread(L);
273 if (lua_rawget(L, -2) == LUA_TFUNCTION) {314 if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */
274 lua_pushstring(L, hooknames[(int)ar->event]);315 lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */
275 if (ar->currentline >= 0)316 if (ar->currentline >= 0)
276 lua_pushinteger(L, ar->currentline);317 lua_pushinteger(L, ar->currentline); /* push current line */
277 else lua_pushnil(L);318 else lua_pushnil(L);
278 lua_assert(lua_getinfo(L, "lS", ar));319 lua_assert(lua_getinfo(L, "lS", ar));
279 lua_call(L, 2, 0);320 lua_call(L, 2, 0); /* call hook function */
280 }321 }
281}322}
282 323
283 324
325/*
326** Convert a string mask (for 'sethook') into a bit mask
327*/
284static int makemask (const char *smask, int count) {328static int makemask (const char *smask, int count) {
285 int mask = 0;329 int mask = 0;
286 if (strchr(smask, 'c')) mask |= LUA_MASKCALL;330 if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
@@ -291,6 +335,9 @@ static int makemask (const char *smask, int count) { · 4 unmodified lines
291}335}
292 336
293 337
338/*
339** Convert a bit mask (for 'gethook') into a string mask
340*/
294static char *unmakemask (int mask, char *smask) {341static char *unmakemask (int mask, char *smask) {
295 int i = 0;342 int i = 0;
296 if (mask & LUA_MASKCALL) smask[i++] = 'c';343 if (mask & LUA_MASKCALL) smask[i++] = 'c';
@@ -305,7 +352,7 @@ static int db_sethook (lua_State *L) { · 8 unmodified lines
305 int arg, mask, count;352 int arg, mask, count;
306 lua_Hook func;353 lua_Hook func;
307 lua_State *L1 = getthread(L, &arg);354 lua_State *L1 = getthread(L, &arg);
308 if (lua_isnoneornil(L, arg+1)) {355 if (lua_isnoneornil(L, arg+1)) { /* no hook? */
309 lua_settop(L, arg+1);356 lua_settop(L, arg+1);
310 func = NULL; mask = 0; count = 0; /* turn off hooks */357 func = NULL; mask = 0; count = 0; /* turn off hooks */
311 }358 }
@@ -321,9 +368,9 @@ static int db_sethook (lua_State *L) { · 9 unmodified lines
321 lua_pushvalue(L, -1);368 lua_pushvalue(L, -1);
322 lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */369 lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */
323 }370 }
324 lua_pushthread(L1); lua_xmove(L1, L, 1);371 lua_pushthread(L1); lua_xmove(L1, L, 1); /* key */
325 lua_pushvalue(L, arg+1);372 lua_pushvalue(L, arg+1); /* value */
326 lua_rawset(L, -3); /* set new hook */373 lua_rawset(L, -3); /* hooktable[L1] = new Lua hook */
327 lua_sethook(L1, func, mask, count); /* set hooks */374 lua_sethook(L1, func, mask, count); /* set hooks */
328 return 0;375 return 0;
329}376}
@@ -340,11 +387,11 @@ static int db_gethook (lua_State *L) { · 10 unmodified lines
340 else {387 else {
341 gethooktable(L);388 gethooktable(L);
342 lua_pushthread(L1); lua_xmove(L1, L, 1);389 lua_pushthread(L1); lua_xmove(L1, L, 1);
343 lua_rawget(L, -2); /* get hook */390 lua_rawget(L, -2); /* 1st result = hooktable[L1] */
344 lua_remove(L, -2); /* remove hook table */391 lua_remove(L, -2); /* remove hook table */
345 }392 }
346 lua_pushstring(L, unmakemask(mask, buff));393 lua_pushstring(L, unmakemask(mask, buff)); /* 2nd result = mask */
347 lua_pushinteger(L, lua_gethookcount(L1));394 lua_pushinteger(L, lua_gethookcount(L1)); /* 3rd result = count */
348 return 3;395 return 3;
349}396}
350 397
@@ -379,6 +426,7 @@ static int db_traceback (lua_State *L) { · 28 unmodified lines
379 426
380 427
381static const luaL_Reg dblib[] = {428static const luaL_Reg dblib[] = {
429 {"Csize", db_Csize},
382 {"debug", db_debug},430 {"debug", db_debug},
383 {"getuservalue", db_getuservalue},431 {"getuservalue", db_getuservalue},
384 {"gethook", db_gethook},432 {"gethook", db_gethook},
@@ -387,7 +435,6 @@ static const luaL_Reg dblib[] = { · 2 unmodified lines
387 {"getregistry", db_getregistry},435 {"getregistry", db_getregistry},
388 {"getmetatable", db_getmetatable},436 {"getmetatable", db_getmetatable},
389 {"getupvalue", db_getupvalue},437 {"getupvalue", db_getupvalue},
390 {"numbits", db_numbits},
391 {"upvaluejoin", db_upvaluejoin},438 {"upvaluejoin", db_upvaluejoin},
392 {"upvalueid", db_upvalueid},439 {"upvalueid", db_upvalueid},
393 {"setuservalue", db_setuservalue},440 {"setuservalue", db_setuservalue},
src/ldo.c+64−42
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Stack and Call structure of Lua3** Stack and Call structure of Lua
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -33,6 +33,8 @@ · 27 unmodified lines
33 33
34 34
35 35
36#define errorstatus(s) ((s) > LUA_YIELD)
37
36 38
37/*39/*
38** {======================================================40** {======================================================
@@ -412,24 +414,27 @@ void luaD_call (lua_State *L, StkId func, int nResults, int allowyield) { · 373 unmodified lines
412}414}
413 415
414 416
415static void finishCcall (lua_State *L) {417/*
418** Completes the execution of an interrupted C function, calling its
419** continuation function.
420*/
421static void finishCcall (lua_State *L, int status) {
416 CallInfo *ci = L->ci;422 CallInfo *ci = L->ci;
417 int n;423 int n;
418 lua_assert(ci->u.c.k != NULL); /* must have a continuation */424 /* must have a continuation and must be able to call it */
419 lua_assert(L->nny == 0);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);
420 if (ci->callstatus & CIST_YPCALL) { /* was inside a pcall? */428 if (ci->callstatus & CIST_YPCALL) { /* was inside a pcall? */
421 ci->callstatus &= ~CIST_YPCALL; /* finish 'lua_pcall' */429 ci->callstatus &= ~CIST_YPCALL; /* finish 'lua_pcall' */
422 L->errfunc = ci->u.c.old_errfunc;430 L->errfunc = ci->u.c.old_errfunc;
423 }431 }
424 /* finish 'lua_callk'/'lua_pcall' */432 /* finish 'lua_callk'/'lua_pcall'; CIST_YPCALL and 'errfunc' already
433 handled */
425 adjustresults(L, ci->nresults);434 adjustresults(L, ci->nresults);
426 /* call continuation function */435 /* 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;
431 lua_unlock(L);436 lua_unlock(L);
432 n = (*ci->u.c.k)(L);437 n = (*ci->u.c.k)(L, status, ci->u.c.ctx);
433 lua_lock(L);438 lua_lock(L);
434 api_checknelems(L, n);439 api_checknelems(L, n);
435 /* finish 'luaD_precall' */440 /* finish 'luaD_precall' */
@@ -437,13 +442,20 @@ static void finishCcall (lua_State *L) { · 1 unmodified line
437}442}
438 443
439 444
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*/
440static void unroll (lua_State *L, void *ud) {453static void unroll (lua_State *L, void *ud) {
441 UNUSED(ud);454 if (ud != NULL) /* error status? */
442 for (;;) {455 finishCcall(L, *(int *)ud); /* finish 'lua_pcallk' callee */
443 if (L->ci == &L->base_ci) /* stack is empty? */456 while (L->ci != &L->base_ci) { /* something in the stack */
444 return; /* coroutine finished normally */
445 if (!isLua(L->ci)) /* C function? */457 if (!isLua(L->ci)) /* C function? */
446 finishCcall(L);458 finishCcall(L, LUA_YIELD); /* complete its execution */
447 else { /* Lua function */459 else { /* Lua function */
448 luaV_finishOp(L); /* finish interrupted instruction */460 luaV_finishOp(L); /* finish interrupted instruction */
449 luaV_execute(L); /* execute down to higher C 'boundary' */461 luaV_execute(L); /* execute down to higher C 'boundary' */
@@ -453,7 +465,8 @@ static void unroll (lua_State *L, void *ud) { · 3 unmodified lines
453 465
454 466
455/*467/*
456** check whether thread has a suspended protected call468** Try to find a suspended protected call (a "recover point") for the
469** given thread.
457*/470*/
458static CallInfo *findpcall (lua_State *L) {471static CallInfo *findpcall (lua_State *L) {
459 CallInfo *ci;472 CallInfo *ci;
@@ -465,6 +478,11 @@ static CallInfo *findpcall (lua_State *L) { · 5 unmodified lines
465}478}
466 479
467 480
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*/
468static int recover (lua_State *L, int status) {486static int recover (lua_State *L, int status) {
469 StkId oldtop;487 StkId oldtop;
470 CallInfo *ci = findpcall(L);488 CallInfo *ci = findpcall(L);
@@ -474,12 +492,10 @@ static int recover (lua_State *L, int status) { · 3 unmodified lines
474 luaF_close(L, oldtop);492 luaF_close(L, oldtop);
475 seterrorobj(L, status, oldtop);493 seterrorobj(L, status, oldtop);
476 L->ci = ci;494 L->ci = ci;
477 L->allowhook = ci->u.c.old_allowhook;495 L->allowhook = getoah(ci->callstatus); /* restore original 'allowhook' */
478 L->nny = 0; /* should be zero to be yieldable */496 L->nny = 0; /* should be zero to be yieldable */
479 luaD_shrinkstack(L);497 luaD_shrinkstack(L);
480 L->errfunc = ci->u.c.old_errfunc;498 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) */
483 return 1; /* continue running the coroutine */499 return 1; /* continue running the coroutine */
484}500}
485 501
@@ -498,7 +514,11 @@ static l_noret resume_error (lua_State *L, const char *msg, StkId firstArg) { · 12 unmodified lines
498 514
499 515
500/*516/*
501** do the work for 'lua_resume' in protected mode517** 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.
502*/522*/
503static void resume (lua_State *L, void *ud) {523static void resume (lua_State *L, void *ud) {
504 int nCcalls = L->nCcalls;524 int nCcalls = L->nCcalls;
@@ -516,24 +536,22 @@ static void resume (lua_State *L, void *ud) { · 11 unmodified lines
516 else if (L->status != LUA_YIELD)536 else if (L->status != LUA_YIELD)
517 resume_error(L, "cannot resume dead coroutine", firstArg);537 resume_error(L, "cannot resume dead coroutine", firstArg);
518 else { /* resuming from previous yield */538 else { /* resuming from previous yield */
519 L->status = LUA_OK;539 L->status = LUA_OK; /* mark that it is running (again) */
520 ci->func = restorestack(L, ci->extra);540 ci->func = restorestack(L, ci->extra);
521 if (isLua(ci)) /* yielded inside a hook? */541 if (isLua(ci)) /* yielded inside a hook? */
522 luaV_execute(L); /* just continue running Lua code */542 luaV_execute(L); /* just continue running Lua code */
523 else { /* 'common' yield */543 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? */
525 int n;545 int n;
526 ci->u.c.status = LUA_YIELD; /* 'default' status */
527 ci->callstatus |= CIST_YIELDED;
528 lua_unlock(L);546 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 */
530 lua_lock(L);548 lua_lock(L);
531 api_checknelems(L, n);549 api_checknelems(L, n);
532 firstArg = L->top - n; /* yield results come from continuation */550 firstArg = L->top - n; /* yield results come from continuation */
533 }551 }
534 luaD_poscall(L, firstArg); /* finish 'luaD_precall' */552 luaD_poscall(L, firstArg); /* finish 'luaD_precall' */
535 }553 }
536 unroll(L, NULL);554 unroll(L, NULL); /* run continuation */
537 }555 }
538 lua_assert(nCcalls == L->nCcalls);556 lua_assert(nCcalls == L->nCcalls);
539}557}
@@ -541,7 +559,7 @@ static void resume (lua_State *L, void *ud) { · 1 unmodified line
541 559
542LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) {560LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) {
543 int status;561 int status;
544 int oldnny = L->nny; /* save 'nny' */562 int oldnny = L->nny; /* save "number of non-yieldable" calls */
545 lua_lock(L);563 lua_lock(L);
546 luai_userstateresume(L, nargs);564 luai_userstateresume(L, nargs);
547 L->nCcalls = (from) ? from->nCcalls + 1 : 1;565 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
550 status = luaD_rawrunprotected(L, resume, L->top - nargs);568 status = luaD_rawrunprotected(L, resume, L->top - nargs);
551 if (status == -1) /* error calling 'lua_resume'? */569 if (status == -1) /* error calling 'lua_resume'? */
552 status = LUA_ERRRUN;570 status = LUA_ERRRUN;
553 else { /* yield or regular error */571 else { /* continue running after recoverable errors */
554 while (status != LUA_OK && status != LUA_YIELD) { /* error? */572 while (errorstatus(status) && recover(L, status)) {
555 if (recover(L, status)) /* recover point? */573 /* unroll continuation */
556 status = luaD_rawrunprotected(L, unroll, NULL); /* run continuation */574 status = luaD_rawrunprotected(L, unroll, &status);
557 else { /* unrecoverable error */575 }
558 L->status = cast_byte(status); /* mark thread as `dead' */576 if (errorstatus(status)) { /* unrecoverable error? */
559 seterrorobj(L, status, L->top);577 L->status = cast_byte(status); /* mark thread as `dead' */
560 L->ci->top = L->top;578 seterrorobj(L, status, L->top); /* push error message */
561 break;579 L->ci->top = L->top;
562 }
563 }580 }
564 lua_assert(status == L->status);581 else lua_assert(status == L->status); /* normal end or yield */
565 }582 }
566 L->nny = oldnny; /* restore 'nny' */583 L->nny = oldnny; /* restore 'nny' */
567 L->nCcalls--;584 L->nCcalls--;
@@ -571,7 +588,12 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) { · 3 unmodified lines
571}588}
572 589
573 590
574LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k) {591LUA_API int lua_isyieldable (lua_State *L) {
592 return (L->nny == 0);
593}
594
595
596LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_KFunction k) {
575 CallInfo *ci = L->ci;597 CallInfo *ci = L->ci;
576 luai_userstateyield(L, nresults);598 luai_userstateyield(L, nresults);
577 lua_lock(L);599 lua_lock(L);
@@ -645,7 +667,7 @@ static void checkmode (lua_State *L, const char *mode, const char *x) { · 67 unmodified lines
645 667
646 668
647static void f_parser (lua_State *L, void *ud) {669static void f_parser (lua_State *L, void *ud) {
648 Closure *cl;670 LClosure *cl;
649 struct SParser *p = cast(struct SParser *, ud);671 struct SParser *p = cast(struct SParser *, ud);
650 int c = zgetc(p->z); /* read first character */672 int c = zgetc(p->z); /* read first character */
651 if (c == LUA_SIGNATURE[0]) {673 if (c == LUA_SIGNATURE[0]) {
@@ -656,8 +678,8 @@ static void f_parser (lua_State *L, void *ud) { · 4 unmodified lines
656 checkmode(L, p->mode, "text");678 checkmode(L, p->mode, "text");
657 cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c);679 cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c);
658 }680 }
659 lua_assert(cl->l.nupvalues == cl->l.p->sizeupvalues);681 lua_assert(cl->nupvalues == cl->p->sizeupvalues);
660 luaF_initupvals(L, &cl->l);682 luaF_initupvals(L, cl);
661}683}
662 684
663 685
src/ldump.c+18−9
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** save precompiled Lua chunks3** save precompiled Lua chunks
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -27,7 +27,7 @@ typedef struct { · 21 unmodified lines
27 27
28/*28/*
29** All high-level dumps go through DumpVector; you can change it to29** All high-level dumps go through DumpVector; you can change it to
30** change the endianess of the result30** change the endianness of the result
31*/31*/
32#define DumpVector(v,n,D) DumpBlock(v,(n)*sizeof((v)[0]),D)32#define DumpVector(v,n,D) DumpBlock(v,(n)*sizeof((v)[0]),D)
33 33
@@ -73,7 +73,7 @@ static void DumpString (const TString *s, DumpState *D) { · 39 unmodified lines
73 else {73 else {
74 size_t size = s->tsv.len + 1; /* include trailing '\0' */74 size_t size = s->tsv.len + 1; /* include trailing '\0' */
75 if (size < 0xFF)75 if (size < 0xFF)
76 DumpByte(size, D);76 DumpByte(cast_int(size), D);
77 else {77 else {
78 DumpByte(0xFF, D);78 DumpByte(0xFF, D);
79 DumpVar(size, D);79 DumpVar(size, D);
@@ -89,7 +89,7 @@ static void DumpCode (const Proto *f, DumpState *D) { · 9 unmodified lines
89}89}
90 90
91 91
92static void DumpFunction(const Proto *f, DumpState *D);92static void DumpFunction(const Proto *f, TString *psource, DumpState *D);
93 93
94static void DumpConstants (const Proto *f, DumpState *D) {94static void DumpConstants (const Proto *f, DumpState *D) {
95 int i;95 int i;
@@ -118,10 +118,15 @@ static void DumpConstants (const Proto *f, DumpState *D) { · 22 unmodified lines
118 lua_assert(0);118 lua_assert(0);
119 }119 }
120 }120 }
121 n = f->sizep;121}
122
123
124static void DumpProtos (const Proto *f, DumpState *D) {
125 int i;
126 int n = f->sizep;
122 DumpInt(n, D);127 DumpInt(n, D);
123 for (i = 0; i < n; i++)128 for (i = 0; i < n; i++)
124 DumpFunction(f->p[i], D);129 DumpFunction(f->p[i], f->source, D);
125}130}
126 131
127 132
@@ -137,7 +142,6 @@ static void DumpUpvalues (const Proto *f, DumpState *D) { · 9 unmodified lines
137 142
138static void DumpDebug (const Proto *f, DumpState *D) {143static void DumpDebug (const Proto *f, DumpState *D) {
139 int i, n;144 int i, n;
140 DumpString((D->strip) ? NULL : f->source, D);
141 n = (D->strip) ? 0 : f->sizelineinfo;145 n = (D->strip) ? 0 : f->sizelineinfo;
142 DumpInt(n, D);146 DumpInt(n, D);
143 DumpVector(f->lineinfo, n, D);147 DumpVector(f->lineinfo, n, D);
@@ -155,7 +159,11 @@ static void DumpDebug (const Proto *f, DumpState *D) { · 11 unmodified lines
155}159}
156 160
157 161
158static void DumpFunction (const Proto *f, DumpState *D) {162static 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);
159 DumpInt(f->linedefined, D);167 DumpInt(f->linedefined, D);
160 DumpInt(f->lastlinedefined, D);168 DumpInt(f->lastlinedefined, D);
161 DumpByte(f->numparams, D);169 DumpByte(f->numparams, D);
@@ -164,6 +172,7 @@ static void DumpFunction (const Proto *f, DumpState *D) { · 2 unmodified lines
164 DumpCode(f, D);172 DumpCode(f, D);
165 DumpConstants(f, D);173 DumpConstants(f, D);
166 DumpUpvalues(f, D);174 DumpUpvalues(f, D);
175 DumpProtos(f, D);
167 DumpDebug(f, D);176 DumpDebug(f, D);
168}177}
169 178
@@ -196,7 +205,7 @@ int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, · 26 unmodified lines
196 D.status = 0;205 D.status = 0;
197 DumpHeader(&D);206 DumpHeader(&D);
198 DumpByte(f->sizeupvalues, &D);207 DumpByte(f->sizeupvalues, &D);
199 DumpFunction(f, &D);208 DumpFunction(f, NULL, &D);
200 return D.status;209 return D.status;
201}210}
202 211
src/lfunc.c+13−10
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Auxiliary functions to manipulate prototypes and closures3** Auxiliary functions to manipulate prototypes and closures
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -20,18 +20,20 @@ · 14 unmodified lines
20 20
21 21
22 22
23Closure *luaF_newCclosure (lua_State *L, int n) {23CClosure *luaF_newCclosure (lua_State *L, int n) {
24 Closure *c = &luaC_newobj(L, LUA_TCCL, sizeCclosure(n))->cl;24 GCObject *o = luaC_newobj(L, LUA_TCCL, sizeCclosure(n));
25 c->c.nupvalues = cast_byte(n);25 CClosure *c = gco2ccl(o);
26 c->nupvalues = cast_byte(n);
26 return c;27 return c;
27}28}
28 29
29 30
30Closure *luaF_newLclosure (lua_State *L, int n) {31LClosure *luaF_newLclosure (lua_State *L, int n) {
31 Closure *c = &luaC_newobj(L, LUA_TLCL, sizeLclosure(n))->cl;32 GCObject *o = luaC_newobj(L, LUA_TLCL, sizeLclosure(n));
32 c->l.p = NULL;33 LClosure *c = gco2lcl(o);
33 c->l.nupvalues = cast_byte(n);34 c->p = NULL;
34 while (n--) c->l.upvals[n] = NULL;35 c->nupvalues = cast_byte(n);
36 while (n--) c->upvals[n] = NULL;
35 return c;37 return c;
36}38}
37 39
@@ -93,7 +95,8 @@ void luaF_close (lua_State *L, StkId level) { · 55 unmodified lines
93 95
94 96
95Proto *luaF_newproto (lua_State *L) {97Proto *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);
97 f->k = NULL;100 f->k = NULL;
98 f->sizek = 0;101 f->sizek = 0;
99 f->p = NULL;102 f->p = NULL;
src/lfunc.h+3−3
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Auxiliary functions to manipulate prototypes and closures3** Auxiliary functions to manipulate prototypes and closures
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -41,8 +41,8 @@ struct UpVal { · 35 unmodified lines
41 41
42 42
43LUAI_FUNC Proto *luaF_newproto (lua_State *L);43LUAI_FUNC Proto *luaF_newproto (lua_State *L);
44LUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems);44LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nelems);
45LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, int nelems);45LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nelems);
46LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl);46LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl);
47LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level);47LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level);
48LUAI_FUNC void luaF_close (lua_State *L, StkId level);48LUAI_FUNC void luaF_close (lua_State *L, StkId level);
src/lgc.c+70−76
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Garbage Collector3** Garbage Collector
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -172,7 +172,7 @@ void luaC_barrierback_ (lua_State *L, GCObject *o) { · 166 unmodified lines
172** closures pointing to it. So, we assume that the object being assigned172** closures pointing to it. So, we assume that the object being assigned
173** must be marked.173** must be marked.
174*/174*/
175LUAI_FUNC void luaC_upvalbarrier_ (lua_State *L, UpVal *uv) {175void luaC_upvalbarrier_ (lua_State *L, UpVal *uv) {
176 global_State *g = G(L);176 global_State *g = G(L);
177 GCObject *o = gcvalue(uv->v);177 GCObject *o = gcvalue(uv->v);
178 lua_assert(!upisopen(uv)); /* ensured by macro luaC_upvalbarrier */178 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
495}495}
496 496
497 497
498static lu_mem traversestack (global_State *g, lua_State *th) {498static lu_mem traversethread (global_State *g, lua_State *th) {
499 int n = 0;
500 StkId o = th->stack;499 StkId o = th->stack;
501 if (o == NULL)500 if (o == NULL)
502 return 1; /* stack not completely built yet */501 return 1; /* stack not completely built yet */
@@ -514,16 +513,9 @@ static lu_mem traversestack (global_State *g, lua_State *th) { · 11 unmodified lines
514 g->twups = th;513 g->twups = th;
515 }514 }
516 }515 }
517 else {516 else if (g->gckind != KGC_EMERGENCY)
518 CallInfo *ci;517 luaD_shrinkstack(th); /* do not change stack in emergency cycle */
519 for (ci = &th->base_ci; ci != th->ci; ci = ci->next)518 return (sizeof(lua_State) + sizeof(TValue) * th->stacksize);
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;
527}519}
528 520
529 521
@@ -561,7 +553,7 @@ static void propagatemark (global_State *g) { · 31 unmodified lines
561 th->gclist = g->grayagain;553 th->gclist = g->grayagain;
562 g->grayagain = o; /* insert into 'grayagain' list */554 g->grayagain = o; /* insert into 'grayagain' list */
563 black2gray(o);555 black2gray(o);
564 size = traversestack(g, th);556 size = traversethread(g, th);
565 break;557 break;
566 }558 }
567 case LUA_TPROTO: {559 case LUA_TPROTO: {
@@ -777,9 +769,11 @@ static GCObject **sweeptolive (lua_State *L, GCObject **p, int *n) { · 209 unmodified lines
777*/769*/
778static void checkSizes (lua_State *L, global_State *g) {770static void checkSizes (lua_State *L, global_State *g) {
779 if (g->gckind != KGC_EMERGENCY) {771 if (g->gckind != KGC_EMERGENCY) {
772 l_mem olddebt = g->GCdebt;
780 luaZ_freebuffer(L, &g->buff); /* free concatenation buffer */773 luaZ_freebuffer(L, &g->buff); /* free concatenation buffer */
781 if (g->strt.nuse < g->strt.size / 4) /* string table too big? */774 if (g->strt.nuse < g->strt.size / 4) /* string table too big? */
782 luaS_resize(L, g->strt.size / 2); /* shrink it a little */775 luaS_resize(L, g->strt.size / 2); /* shrink it a little */
776 g->GCestimate += g->GCdebt - olddebt; /* update estimate */
783 }777 }
784}778}
785 779
@@ -835,6 +829,21 @@ static void GCTM (lua_State *L, int propagateerrors) { · 49 unmodified lines
835}829}
836 830
837 831
832/*
833** call a few (up to 'g->gcfinnum') finalizers
834*/
835static 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
838/*847/*
839** call all pending finalizers848** call all pending finalizers
840*/849*/
@@ -888,18 +897,17 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { · 47 unmodified lines
888 return; /* nothing to be done */897 return; /* nothing to be done */
889 else { /* move 'o' to 'finobj' list */898 else { /* move 'o' to 'finobj' list */
890 GCObject **p;899 GCObject **p;
891 if (g->sweepgc == &o->gch.next) { /* avoid removing current sweep object */900 if (issweepphase(g)) {
892 lua_assert(issweepphase(g));901 makewhite(g, o); /* "sweep" object 'o' */
893 g->sweepgc = sweeptolive(L, g->sweepgc, NULL);902 if (g->sweepgc == &o->gch.next) /* shoud not remove 'sweepgc' object */
903 g->sweepgc = sweeptolive(L, g->sweepgc, NULL); /* change 'sweepgc' */
894 }904 }
895 /* search for pointer pointing to 'o' */905 /* search for pointer pointing to 'o' */
896 for (p = &g->allgc; *p != o; p = &gch(*p)->next) { /* empty */ }906 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 */
898 o->gch.next = g->finobj; /* link it in "fin" list */908 o->gch.next = g->finobj; /* link it in "fin" list */
899 g->finobj = o;909 g->finobj = o;
900 l_setbit(o->gch.marked, FINALIZEDBIT); /* mark it as such */910 l_setbit(o->gch.marked, FINALIZEDBIT); /* mark it as such */
901 if (issweepphase(g))
902 makewhite(g, o); /* "sweep" object */
903 }911 }
904}912}
905 913
@@ -918,9 +926,9 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { · 12 unmodified lines
918** set a reasonable "time" to wait before starting a new GC cycle;926** set a reasonable "time" to wait before starting a new GC cycle;
919** cycle will start when memory use hits threshold927** cycle will start when memory use hits threshold
920*/928*/
921static void setpause (global_State *g, l_mem estimate) {929static void setpause (global_State *g) {
922 l_mem threshold, debt;930 l_mem threshold, debt;
923 estimate = estimate / PAUSEADJ; /* adjust 'estimate' */931 l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */
924 threshold = (g->gcpause < MAX_LMEM / estimate) /* overflow? */932 threshold = (g->gcpause < MAX_LMEM / estimate) /* overflow? */
925 ? estimate * g->gcpause /* no overflow */933 ? estimate * g->gcpause /* no overflow */
926 : MAX_LMEM; /* overflow; truncate to maximum */934 : MAX_LMEM; /* overflow; truncate to maximum */
@@ -964,8 +972,9 @@ void luaC_freeallobjects (lua_State *L) { · 37 unmodified lines
964 972
965static l_mem atomic (lua_State *L) {973static l_mem atomic (lua_State *L) {
966 global_State *g = G(L);974 global_State *g = G(L);
967 l_mem work = -cast(l_mem, g->GCmemtrav); /* start counting work */975 l_mem work;
968 GCObject *origweak, *origall;976 GCObject *origweak, *origall;
977 g->GCmemtrav = 0; /* start counting work */
969 lua_assert(!iswhite(obj2gco(g->mainthread)));978 lua_assert(!iswhite(obj2gco(g->mainthread)));
970 g->gcstate = GCSinsideatomic;979 g->gcstate = GCSinsideatomic;
971 markobject(g, L); /* mark running thread */980 markobject(g, L); /* mark running thread */
@@ -975,10 +984,10 @@ static l_mem atomic (lua_State *L) { · 3 unmodified lines
975 /* remark occasional upvalues of (maybe) dead threads */984 /* remark occasional upvalues of (maybe) dead threads */
976 remarkupvals(g);985 remarkupvals(g);
977 propagateall(g); /* propagate changes */986 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) */
979 /* traverse objects caught by write barrier and by 'remarkupvals' */988 /* traverse objects caught by write barrier and by 'remarkupvals' */
980 retraversegrays(g);989 retraversegrays(g);
981 work -= g->GCmemtrav; /* restart counting */990 g->GCmemtrav = 0; /* restart counting */
982 convergeephemerons(g);991 convergeephemerons(g);
983 /* at this point, all strongly accessible objects are marked. */992 /* at this point, all strongly accessible objects are marked. */
984 /* Clear values from weak tables, before checking finalizers */993 /* Clear values from weak tables, before checking finalizers */
@@ -990,7 +999,7 @@ static l_mem atomic (lua_State *L) { · 5 unmodified lines
990 g->gcfinnum = 1; /* there may be objects to be finalized */999 g->gcfinnum = 1; /* there may be objects to be finalized */
991 markbeingfnz(g); /* mark objects that will be finalized */1000 markbeingfnz(g); /* mark objects that will be finalized */
992 propagateall(g); /* remark, to propagate 'resurrection' */1001 propagateall(g); /* remark, to propagate 'resurrection' */
993 work -= g->GCmemtrav; /* restart counting */1002 g->GCmemtrav = 0; /* restart counting */
994 convergeephemerons(g);1003 convergeephemerons(g);
995 /* at this point, all resurrected objects are marked. */1004 /* at this point, all resurrected objects are marked. */
996 /* remove dead objects from weak tables */1005 /* remove dead objects from weak tables */
@@ -1008,7 +1017,9 @@ static l_mem atomic (lua_State *L) { · 11 unmodified lines
1008static lu_mem sweepstep (lua_State *L, global_State *g,1017static lu_mem sweepstep (lua_State *L, global_State *g,
1009 int nextstate, GCObject **nextlist) {1018 int nextstate, GCObject **nextlist) {
1010 if (g->sweepgc) {1019 if (g->sweepgc) {
1020 l_mem olddebt = g->GCdebt;
1011 g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX);1021 g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX);
1022 g->GCestimate += g->GCdebt - olddebt; /* update estimate */
1012 if (g->sweepgc) /* is there still something to sweep? */1023 if (g->sweepgc) /* is there still something to sweep? */
1013 return (GCSWEEPMAX * GCSWEEPCOST);1024 return (GCSWEEPMAX * GCSWEEPCOST);
1014 }1025 }
@@ -1023,28 +1034,26 @@ static lu_mem singlestep (lua_State *L) { · 8 unmodified lines
1023 global_State *g = G(L);1034 global_State *g = G(L);
1024 switch (g->gcstate) {1035 switch (g->gcstate) {
1025 case GCSpause: {1036 case GCSpause: {
1026 /* start to count memory traversed */
1027 g->GCmemtrav = g->strt.size * sizeof(GCObject*);1037 g->GCmemtrav = g->strt.size * sizeof(GCObject*);
1028 restartcollection(g);1038 restartcollection(g);
1029 g->gcstate = GCSpropagate;1039 g->gcstate = GCSpropagate;
1030 return g->GCmemtrav;1040 return g->GCmemtrav;
1031 }1041 }
1032 case GCSpropagate: {1042 case GCSpropagate: {
1033 lu_mem oldtrav = g->GCmemtrav;1043 g->GCmemtrav = 0;
1034 lua_assert(g->gray);1044 lua_assert(g->gray);
1035 propagatemark(g);1045 propagatemark(g);
1036 if (g->gray == NULL) /* no more `gray' objects? */1046 if (g->gray == NULL) /* no more `gray' objects? */
1037 g->gcstate = GCSatomic; /* finish propagate phase */1047 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 */
1039 }1049 }
1040 case GCSatomic: {1050 case GCSatomic: {
1041 lu_mem work;1051 lu_mem work;
1042 int sw;1052 int sw;
1043 propagateall(g); /* make sure gray list is empty */1053 propagateall(g); /* make sure gray list is empty */
1044 g->GCestimate = g->GCmemtrav; /* save what was counted */;
1045 work = atomic(L); /* work is what was traversed by 'atomic' */1054 work = atomic(L); /* work is what was traversed by 'atomic' */
1046 g->GCestimate += work; /* estimate of total memory traversed */
1047 sw = entersweep(L);1055 sw = entersweep(L);
1056 g->GCestimate = gettotalbytes(g); /* first estimate */;
1048 return work + sw * GCSWEEPCOST;1057 return work + sw * GCSWEEPCOST;
1049 }1058 }
1050 case GCSswpallgc: { /* sweep "regular" objects */1059 case GCSswpallgc: { /* sweep "regular" objects */
@@ -1062,10 +1071,15 @@ static lu_mem singlestep (lua_State *L) { · 11 unmodified lines
1062 g->gcstate = GCScallfin;1071 g->gcstate = GCScallfin;
1063 return 0;1072 return 0;
1064 }1073 }
1065 case GCScallfin: { /* state to finish calling finalizers */1074 case GCScallfin: { /* call remaining finalizers */
1066 /* do nothing here; should be handled by 'luaC_forcestep' */1075 if (g->tobefnz && g->gckind != KGC_EMERGENCY) {
1067 g->gcstate = GCSpause; /* finish collection */1076 int n = runafewfinalizers(L);
1068 return 0;1077 return (n * GCFINALIZECOST);
1078 }
1079 else { /* emergency mode or no more finalizers */
1080 g->gcstate = GCSpause; /* finish collection */
1081 return 0;
1082 }
1069 }1083 }
1070 default: lua_assert(0); return 0;1084 default: lua_assert(0); return 0;
1071 }1085 }
@@ -1083,21 +1097,6 @@ void luaC_runtilstate (lua_State *L, int statesmask) { · 11 unmodified lines
1083}1097}
1084 1098
1085 1099
1086/*
1087** run a few (up to 'g->gcfinnum') finalizers
1088*/
1089static 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
1101/*1100/*
1102** get GC debt and convert it from Kb to 'work units' (avoid zero debt1101** get GC debt and convert it from Kb to 'work units' (avoid zero debt
1103** and overflows)1102** and overflows)
@@ -1115,23 +1114,17 @@ static l_mem getdebt (global_State *g) { · 11 unmodified lines
1115*/1114*/
1116void luaC_step (lua_State *L) {1115void luaC_step (lua_State *L) {
1117 global_State *g = G(L);1116 global_State *g = G(L);
1118 l_mem debt = getdebt(g);1117 l_mem debt = getdebt(g); /* GC deficit (be paid now) */
1119 if (!g->gcrunning) { /* not running? */1118 if (!g->gcrunning) { /* not running? */
1120 luaE_setdebt(g, -GCSTEPSIZE * 10); /* avoid being called too often */1119 luaE_setdebt(g, -GCSTEPSIZE * 10); /* avoid being called too often */
1121 return;1120 return;
1122 }1121 }
1123 do {1122 do { /* repeat until pause or enough "credit" (negative debt) */
1124 if (g->gcstate == GCScallfin && g->tobefnz) {1123 lu_mem work = singlestep(L); /* perform one single step */
1125 unsigned int n = runafewfinalizers(L);1124 debt -= work;
1126 debt -= (n * GCFINALIZECOST);
1127 }
1128 else { /* perform one single step */
1129 lu_mem work = singlestep(L);
1130 debt -= work;
1131 }
1132 } while (debt > -GCSTEPSIZE && g->gcstate != GCSpause);1125 } while (debt > -GCSTEPSIZE && g->gcstate != GCSpause);
1133 if (g->gcstate == GCSpause)1126 if (g->gcstate == GCSpause)
1134 setpause(g, g->GCestimate); /* pause until next cycle */1127 setpause(g); /* pause until next cycle */
1135 else {1128 else {
1136 debt = (debt / g->gcstepmul) * STEPMULADJ; /* convert 'work units' to Kb */1129 debt = (debt / g->gcstepmul) * STEPMULADJ; /* convert 'work units' to Kb */
1137 luaE_setdebt(g, debt);1130 luaE_setdebt(g, debt);
@@ -1141,29 +1134,30 @@ void luaC_step (lua_State *L) { · 3 unmodified lines
1141 1134
1142 1135
1143/*1136/*
1144** performs a full GC cycle; if "isemergency", does not call1137** Performs a full GC cycle; if "isemergency", set a flag to avoid
1145** finalizers (which could change stack positions)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).
1146*/1144*/
1147void luaC_fullgc (lua_State *L, int isemergency) {1145void luaC_fullgc (lua_State *L, int isemergency) {
1148 global_State *g = G(L);1146 global_State *g = G(L);
1149 lua_assert(g->gckind == KGC_NORMAL);1147 lua_assert(g->gckind == KGC_NORMAL);
1150 if (isemergency) /* do not run finalizers during emergency GC */1148 if (isemergency) g->gckind = KGC_EMERGENCY; /* set flag */
1151 g->gckind = KGC_EMERGENCY;1149 if (keepinvariant(g)) { /* black objects? */
1152 else1150 entersweep(L); /* sweep everything to turn them back to white */
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);
1158 }1151 }
1159 /* finish any pending sweep phase to start a new cycle */1152 /* finish any pending sweep phase to start a new cycle */
1160 luaC_runtilstate(L, bitmask(GCSpause));1153 luaC_runtilstate(L, bitmask(GCSpause));
1161 luaC_runtilstate(L, ~bitmask(GCSpause)); /* start new collection */1154 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 */
1163 g->gckind = KGC_NORMAL;1159 g->gckind = KGC_NORMAL;
1164 setpause(g, gettotalbytes(g));1160 setpause(g);
1165 if (!isemergency) /* do not run finalizers during emergency GC */
1166 callallpendingfinalizers(L, 1);
1167}1161}
1168 1162
1169/* }====================================================== */1163/* }====================================================== */
src/linit.c+5−3
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Initialization of libraries for lua.c and other clients3** Initialization of libraries for lua.c and other clients
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -34,10 +34,12 @@ static const luaL_Reg loadedlibs[] = { · 28 unmodified lines
34 {LUA_IOLIBNAME, luaopen_io},34 {LUA_IOLIBNAME, luaopen_io},
35 {LUA_OSLIBNAME, luaopen_os},35 {LUA_OSLIBNAME, luaopen_os},
36 {LUA_STRLIBNAME, luaopen_string},36 {LUA_STRLIBNAME, luaopen_string},
37 {LUA_UTF8LIBNAME, luaopen_utf8},
38 {LUA_BITLIBNAME, luaopen_bit32},
39 {LUA_MATHLIBNAME, luaopen_math},37 {LUA_MATHLIBNAME, luaopen_math},
40 {LUA_DBLIBNAME, luaopen_debug},38 {LUA_DBLIBNAME, luaopen_debug},
39 {LUA_UTF8LIBNAME, luaopen_utf8},
40#if defined(LUA_COMPAT_BITLIB)
41 {LUA_BITLIBNAME, luaopen_bit32},
42#endif
41 {NULL, NULL}43 {NULL, NULL}
42};44};
43 45
src/liolib.c+89−24
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Standard I/O (and system) library3** Standard I/O (and system) library
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -15,7 +15,9 @@ · 9 unmodified lines
15#endif15#endif
16 16
17 17
18#include <ctype.h>
18#include <errno.h>19#include <errno.h>
20#include <locale.h>
19#include <stdio.h>21#include <stdio.h>
20#include <stdlib.h>22#include <stdlib.h>
21#include <string.h>23#include <string.h>
@@ -104,6 +106,8 @@ · 82 unmodified lines
104 106
105#if defined(LUA_USE_POSIX) /* { */107#if defined(LUA_USE_POSIX) /* { */
106 108
109#include <sys/types.h>
110
107#define l_fseek(f,o,w) fseeko(f,o,w)111#define l_fseek(f,o,w) fseeko(f,o,w)
108#define l_ftell(f) ftello(f)112#define l_ftell(f) ftello(f)
109#define l_seeknum off_t113#define l_seeknum off_t
@@ -317,14 +321,10 @@ static int io_readline (lua_State *L); · 207 unmodified lines
317 321
318 322
319static void aux_lines (lua_State *L, int toclose) {323static void aux_lines (lua_State *L, int toclose) {
320 int i;
321 int n = lua_gettop(L) - 1; /* number of arguments to read */324 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 */
325 lua_pushinteger(L, n); /* number of arguments to read */325 lua_pushinteger(L, n); /* number of arguments to read */
326 lua_pushboolean(L, toclose); /* close/not close file when finished */326 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 */
328 lua_pushcclosure(L, io_readline, 3 + n);328 lua_pushcclosure(L, io_readline, 3 + n);
329}329}
330 330
@@ -363,26 +363,93 @@ static int io_lines (lua_State *L) { · 32 unmodified lines
363*/363*/
364 364
365 365
366static int read_integer (lua_State *L, FILE *f) {366/* maximum length of a numeral */
367 lua_Integer d;367#define MAXRN 200
368 if (fscanf(f, LUA_INTEGER_SCAN, &d) == 1) {368
369 lua_pushinteger(L, d);369/* auxiliary structure used by 'read_number' */
370 return 1;370typedef 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*/
381static int nextc (RN *rn) {
382 if (rn->n >= MAXRN) { /* buffer overflow? */
383 rn->buff[0] = '\0'; /* invalidate result */
384 return 0; /* fail */
371 }385 }
372 else {386 else {
373 lua_pushnil(L); /* "result" to be removed */387 rn->buff[rn->n++] = rn->c; /* save current char */
374 return 0; /* read fails */388 rn->c = l_getc(rn->f); /* read next one */
389 return 1;
375 }390 }
376}391}
377 392
378 393
394/*
395** Accept current char if it is in 'set' (of size 1 or 2)
396*/
397static 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*/
407static 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*/
379static int read_number (lua_State *L, FILE *f) {426static int read_number (lua_State *L, FILE *f) {
380 lua_Number d;427 RN rn;
381 if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) {428 int count = 0;
382 lua_pushnumber(L, d);429 int hexa = 0;
383 return 1;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 */
384 }439 }
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 */
386 lua_pushnil(L); /* "result" to be removed */453 lua_pushnil(L); /* "result" to be removed */
387 return 0; /* read fails */454 return 0; /* read fails */
388 }455 }
@@ -455,12 +522,9 @@ static int g_read (lua_State *L, FILE *f, int first) { · 66 unmodified lines
455 success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);522 success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
456 }523 }
457 else {524 else {
458 const char *p = lua_tostring(L, n);525 const char *p = luaL_checkstring(L, n);
459 luaL_argcheck(L, p && p[0] == '*', n, "invalid option");526 if (*p == '*') p++; /* skip optional '*' (for compatibility) */
460 switch (p[1]) {527 switch (*p) {
461 case 'i': /* integer */
462 success = read_integer(L, f);
463 break;
464 case 'n': /* number */528 case 'n': /* number */
465 success = read_number(L, f);529 success = read_number(L, f);
466 break;530 break;
@@ -507,6 +571,7 @@ static int io_readline (lua_State *L) { · 40 unmodified lines
507 if (isclosed(p)) /* file is already closed? */571 if (isclosed(p)) /* file is already closed? */
508 return luaL_error(L, "file is already closed");572 return luaL_error(L, "file is already closed");
509 lua_settop(L , 1);573 lua_settop(L , 1);
574 luaL_checkstack(L, n, "too many arguments");
510 for (i = 1; i <= n; i++) /* push arguments to 'g_read' */575 for (i = 1; i <= n; i++) /* push arguments to 'g_read' */
511 lua_pushvalue(L, lua_upvalueindex(3 + i));576 lua_pushvalue(L, lua_upvalueindex(3 + i));
512 n = g_read(L, p->f, 2); /* 'n' is number of results */577 n = g_read(L, p->f, 2); /* 'n' is number of results */
src/llex.c+69−53
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Lexical Analyzer3** Lexical Analyzer
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -183,12 +183,26 @@ void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source, · 177 unmodified lines
183*/183*/
184 184
185 185
186static int check_next1 (LexState *ls, int c) {
187 if (ls->current == c) {
188 next(ls);
189 return 1;
190 }
191 else return 0;
192}
186 193
187static int check_next (LexState *ls, const char *set) {194
188 if (ls->current == '\0' || !strchr(set, ls->current))195/*
189 return 0;196** Check whether current char is in set 'set' (with two chars) and
190 save_and_next(ls);197** saves it
191 return 1;198*/
199static 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;
192}206}
193 207
194 208
@@ -196,10 +210,12 @@ static int check_next (LexState *ls, const char *set) { · 1 unmodified line
196** change all characters 'from' in buffer to 'to'210** change all characters 'from' in buffer to 'to'
197*/211*/
198static void buffreplace (LexState *ls, char from, char to) {212static void buffreplace (LexState *ls, char from, char to) {
199 size_t n = luaZ_bufflen(ls->buff);213 if (from != to) {
200 char *p = luaZ_buffer(ls->buff);214 size_t n = luaZ_bufflen(ls->buff);
201 while (n--)215 char *p = luaZ_buffer(ls->buff);
202 if (p[n] == from) p[n] = to;216 while (n--)
217 if (p[n] == from) p[n] = to;
218 }
203}219}
204 220
205 221
@@ -208,17 +224,17 @@ static void buffreplace (LexState *ls, char from, char to) { · 2 unmodified lines
208#endif224#endif
209 225
210 226
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)
212 228
213/*229/*
214** in case of format error, try to change decimal point separator to230** in case of format error, try to change decimal point separator to
215** the one defined in the current locale and check again231** the one defined in the current locale and check again
216*/232*/
217static void trydecpoint (LexState *ls, SemInfo *seminfo) {233static void trydecpoint (LexState *ls, TValue *o) {
218 char old = ls->decpoint;234 char old = ls->decpoint;
219 ls->decpoint = getlocaledecpoint();235 ls->decpoint = getlocaledecpoint();
220 buffreplace(ls, old, ls->decpoint); /* try new decimal separator */236 buffreplace(ls, old, ls->decpoint); /* try new decimal separator */
221 if (!buff2d(ls->buff, &seminfo->r)) {237 if (!buff2num(ls->buff, o)) {
222 /* format error with correct decimal point: no more options */238 /* format error with correct decimal point: no more options */
223 buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */239 buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */
224 lexerror(ls, "malformed number", TK_FLT);240 lexerror(ls, "malformed number", TK_FLT);
@@ -228,41 +244,37 @@ static void trydecpoint (LexState *ls, SemInfo *seminfo) { · 3 unmodified lines
228 244
229/* LUA_NUMBER */245/* LUA_NUMBER */
230/*246/*
231** this function is quite liberal in what it accepts, as 'luaO_str2d'247** this function is quite liberal in what it accepts, as 'luaO_str2num'
232** will reject ill-formed numerals. 'isf' means the numeral is not248** will reject ill-formed numerals.
233** an integer (it has a dot or an exponent).
234*/249*/
235static int read_numeral (LexState *ls, SemInfo *seminfo, int isf) {250static int read_numeral (LexState *ls, SemInfo *seminfo) {
251 TValue obj;
236 const char *expo = "Ee";252 const char *expo = "Ee";
237 int first = ls->current;253 int first = ls->current;
238 lua_assert(lisdigit(ls->current));254 lua_assert(lisdigit(ls->current));
239 save_and_next(ls);255 save_and_next(ls);
240 if (first == '0' && check_next(ls, "Xx")) /* hexadecimal? */256 if (first == '0' && check_next2(ls, "xX")) /* hexadecimal? */
241 expo = "Pp";257 expo = "Pp";
242 for (;;) {258 for (;;) {
243 if (check_next(ls, expo)) { /* exponent part? */259 if (check_next2(ls, expo)) /* exponent part? */
244 check_next(ls, "+-"); /* optional exponent sign */260 check_next2(ls, "-+"); /* optional exponent sign */
245 isf = 1;
246 }
247 if (lisxdigit(ls->current))261 if (lisxdigit(ls->current))
248 save_and_next(ls);262 save_and_next(ls);
249 else if (ls->current == '.') {263 else if (ls->current == '.')
250 save_and_next(ls);264 save_and_next(ls);
251 isf = 1;
252 }
253 else break;265 else break;
254 }266 }
255 save(ls, '\0');267 save(ls, '\0');
256 if (!isf) {268 buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */
257 if (!luaO_str2int(luaZ_buffer(ls->buff), luaZ_bufflen(ls->buff) - 1,269 if (!buff2num(ls->buff, &obj)) /* format error? */
258 &seminfo->i))270 trydecpoint(ls, &obj); /* try to update decimal point separator */
259 lexerror(ls, "malformed number", TK_INT);271 if (ttisinteger(&obj)) {
272 seminfo->i = ivalue(&obj);
260 return TK_INT;273 return TK_INT;
261 }274 }
262 else {275 else {
263 buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */276 lua_assert(ttisfloat(&obj));
264 if (!buff2d(ls->buff, &seminfo->r)) /* format error? */277 seminfo->r = fltvalue(&obj);
265 trydecpoint(ls, seminfo); /* try to update decimal point separator */
266 return TK_FLT;278 return TK_FLT;
267 }279 }
268}280}
@@ -286,15 +298,19 @@ static int skip_sep (LexState *ls) { · 17 unmodified lines
286 298
287 299
288static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) {300static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) {
301 int line = ls->linenumber; /* initial line (for error message) */
289 save_and_next(ls); /* skip 2nd `[' */302 save_and_next(ls); /* skip 2nd `[' */
290 if (currIsNewline(ls)) /* string starts with a newline? */303 if (currIsNewline(ls)) /* string starts with a newline? */
291 inclinenumber(ls); /* skip it */304 inclinenumber(ls); /* skip it */
292 for (;;) {305 for (;;) {
293 switch (ls->current) {306 switch (ls->current) {
294 case EOZ:307 case EOZ: { /* error */
295 lexerror(ls, (seminfo) ? "unfinished long string" :308 const char *what = (seminfo ? "string" : "comment");
296 "unfinished long comment", TK_EOS);309 const char *msg = luaO_pushfstring(ls->L,
310 "unfinished long %s (starting at line %d)", what, line);
311 lexerror(ls, msg, TK_EOS);
297 break; /* to avoid warnings */312 break; /* to avoid warnings */
313 }
298 case ']': {314 case ']': {
299 if (skip_sep(ls) == sep) {315 if (skip_sep(ls) == sep) {
300 save_and_next(ls); /* skip 2nd `]' */316 save_and_next(ls); /* skip 2nd `]' */
@@ -488,35 +504,35 @@ static int llex (LexState *ls, SemInfo *seminfo) { · 187 unmodified lines
488 }504 }
489 case '=': {505 case '=': {
490 next(ls);506 next(ls);
491 if (ls->current != '=') return '=';507 if (check_next1(ls, '=')) return TK_EQ;
492 else { next(ls); return TK_EQ; }508 else return '=';
493 }509 }
494 case '<': {510 case '<': {
495 next(ls);511 next(ls);
496 if (ls->current == '=') { next(ls); return TK_LE; }512 if (check_next1(ls, '=')) return TK_LE;
497 if (ls->current == '<') { next(ls); return TK_SHL; }513 else if (check_next1(ls, '<')) return TK_SHL;
498 return '<';514 else return '<';
499 }515 }
500 case '>': {516 case '>': {
501 next(ls);517 next(ls);
502 if (ls->current == '=') { next(ls); return TK_GE; }518 if (check_next1(ls, '=')) return TK_GE;
503 if (ls->current == '>') { next(ls); return TK_SHR; }519 else if (check_next1(ls, '>')) return TK_SHR;
504 return '>';520 else return '>';
505 }521 }
506 case '/': {522 case '/': {
507 next(ls);523 next(ls);
508 if (ls->current != '/') return '/';524 if (check_next1(ls, '/')) return TK_IDIV;
509 else { next(ls); return TK_IDIV; }525 else return '/';
510 }526 }
511 case '~': {527 case '~': {
512 next(ls);528 next(ls);
513 if (ls->current != '=') return '~';529 if (check_next1(ls, '=')) return TK_NE;
514 else { next(ls); return TK_NE; }530 else return '~';
515 }531 }
516 case ':': {532 case ':': {
517 next(ls);533 next(ls);
518 if (ls->current != ':') return ':';534 if (check_next1(ls, ':')) return TK_DBCOLON;
519 else { next(ls); return TK_DBCOLON; }535 else return ':';
520 }536 }
521 case '"': case '\'': { /* short literal strings */537 case '"': case '\'': { /* short literal strings */
522 read_string(ls, ls->current, seminfo);538 read_string(ls, ls->current, seminfo);
@@ -524,17 +540,17 @@ static int llex (LexState *ls, SemInfo *seminfo) { · 1 unmodified line
524 }540 }
525 case '.': { /* '.', '..', '...', or number */541 case '.': { /* '.', '..', '...', or number */
526 save_and_next(ls);542 save_and_next(ls);
527 if (check_next(ls, ".")) {543 if (check_next1(ls, '.')) {
528 if (check_next(ls, "."))544 if (check_next1(ls, '.'))
529 return TK_DOTS; /* '...' */545 return TK_DOTS; /* '...' */
530 else return TK_CONCAT; /* '..' */546 else return TK_CONCAT; /* '..' */
531 }547 }
532 else if (!lisdigit(ls->current)) return '.';548 else if (!lisdigit(ls->current)) return '.';
533 else return read_numeral(ls, seminfo, 1);549 else return read_numeral(ls, seminfo);
534 }550 }
535 case '0': case '1': case '2': case '3': case '4':551 case '0': case '1': case '2': case '3': case '4':
536 case '5': case '6': case '7': case '8': case '9': {552 case '5': case '6': case '7': case '8': case '9': {
537 return read_numeral(ls, seminfo, 0);553 return read_numeral(ls, seminfo);
538 }554 }
539 case EOZ: {555 case EOZ: {
540 return TK_EOS;556 return TK_EOS;
src/llimits.h+20−13
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Limits, basic types, and some other `installation-dependent' definitions3** Limits, basic types, and some other `installation-dependent' definitions
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -31,8 +31,8 @@ typedef unsigned char lu_byte; · 25 unmodified lines
31#define MAX_SIZET ((size_t)(~(size_t)0)-2)31#define MAX_SIZET ((size_t)(~(size_t)0)-2)
32 32
33/* maximum size visible for Lua (must be representable in a lua_Integer */33/* maximum size visible for Lua (must be representable in a lua_Integer */
34#define MAX_SIZE (sizeof(size_t) <= sizeof(lua_Integer) ? MAX_SIZET \34#define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \
35 : (size_t)(~(lua_Unsigned)0)-2)35 : (size_t)(LUA_MAXINTEGER)-2)
36 36
37 37
38#define MAX_LUMEM ((lu_mem)(~(lu_mem)0)-2)38#define MAX_LUMEM ((lu_mem)(~(lu_mem)0)-2)
@@ -43,13 +43,6 @@ typedef unsigned char lu_byte; · 4 unmodified lines
43#define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */43#define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */
44 44
45 45
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
53/*46/*
54** conversion of pointer to integer47** conversion of pointer to integer
55** this is for hashing only; there is no problem if the integer48** this is for hashing only; there is no problem if the integer
@@ -67,8 +60,9 @@ typedef unsigned char lu_byte; · 11 unmodified lines
67typedef LUAI_USER_ALIGNMENT_T L_Umaxalign;60typedef LUAI_USER_ALIGNMENT_T L_Umaxalign;
68 61
69 62
70/* result of a `usual argument conversion' over lua_Number */63/* types of 'usual argument conversions' for lua_Number and lua_Integer */
71typedef LUAI_UACNUMBER l_uacNumber;64typedef LUAI_UACNUMBER l_uacNumber;
65typedef LUAI_UACINT l_uacInt;
72 66
73 67
74/* internal assertions for in-house debugging */68/* internal assertions for in-house debugging */
@@ -111,8 +105,21 @@ typedef LUAI_UACNUMBER l_uacNumber; · 36 unmodified lines
111#define cast_num(i) cast(lua_Number, (i))105#define cast_num(i) cast(lua_Number, (i))
112#define cast_int(i) cast(int, (i))106#define cast_int(i) cast(int, (i))
113#define cast_uchar(i) cast(unsigned char, (i))107#define cast_uchar(i) cast(unsigned char, (i))
114#define cast_integer(i) cast(lua_Integer, (i))108
115#define cast_unsigned(i) cast(lua_Unsigned, (i))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
116 123
117 124
118/*125/*
src/lmathlib.c+175−106
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Standard mathematical library3** Standard mathematical library
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -18,12 +18,28 @@ · 12 unmodified lines
18 18
19 19
20#undef PI20#undef PI
21#define PI ((lua_Number)(3.1415926535897932384626433832795))21#define PI (l_mathop(3.141592653589793238462643383279502884))
22#define RADIANS_PER_DEGREE ((lua_Number)(PI/180.0))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 /* } */
23 33
24 34
25static int math_abs (lua_State *L) {35static 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)));
27 return 1;43 return 1;
28}44}
29 45
@@ -32,31 +48,16 @@ static int math_sin (lua_State *L) { · 2 unmodified lines
32 return 1;48 return 1;
33}49}
34 50
35static int math_sinh (lua_State *L) {
36 lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1)));
37 return 1;
38}
39
40static int math_cos (lua_State *L) {51static int math_cos (lua_State *L) {
41 lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1)));52 lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1)));
42 return 1;53 return 1;
43}54}
44 55
45static int math_cosh (lua_State *L) {
46 lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1)));
47 return 1;
48}
49
50static int math_tan (lua_State *L) {56static int math_tan (lua_State *L) {
51 lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1)));57 lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1)));
52 return 1;58 return 1;
53}59}
54 60
55static int math_tanh (lua_State *L) {
56 lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1)));
57 return 1;
58}
59
60static int math_asin (lua_State *L) {61static int math_asin (lua_State *L) {
61 lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1)));62 lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1)));
62 return 1;63 return 1;
@@ -68,56 +69,92 @@ static int math_acos (lua_State *L) { · 5 unmodified lines
68}69}
69 70
70static int math_atan (lua_State *L) {71static 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));
72 return 1;75 return 1;
73}76}
74 77
75static 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}
80 78
81static int math_ceil (lua_State *L) {79static int math_ifloor (lua_State *L) {
82 lua_pushnumber(L, l_mathop(ceil)(luaL_checknumber(L, 1)));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 }
83 return 1;88 return 1;
84}89}
85 90
86static int math_floor (lua_State *L) {
87 lua_pushnumber(L, l_mathop(floor)(luaL_checknumber(L, 1)));
88 return 1;
89}
90 91
91static int math_ifloor (lua_State *L) {92static int math_floor (lua_State *L) {
92 int valid;93 int valid;
93 lua_Integer n = lua_tointegerx(L, 1, &valid);94 lua_Integer n = lua_tointegerx(L, 1, &valid);
94 if (valid)95 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
103static 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
112static int math_ceil (lua_State *L) {
113 if (lua_isinteger(L, 1))
114 lua_settop(L, 1); /* integer is its own ceil */
96 else {115 else {
97 luaL_checktype(L, 1, LUA_TNUMBER); /* error if not a number */116 lua_Number d = l_mathop(ceil)(luaL_checknumber(L, 1));
98 lua_pushnil(L); /* number with invalid integer value */117 pushnumint(L, d);
99 }118 }
100 return 1;119 return 1;
101}120}
102 121
122
103static int math_fmod (lua_State *L) {123static int math_fmod (lua_State *L) {
104 lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1),124 if (lua_isinteger(L, 1) && lua_isinteger(L, 2)) {
105 luaL_checknumber(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)));
106 return 1;136 return 1;
107}137}
108 138
139
109/*140/*
110** next function does not use 'modf', avoiding problems with 'double*'141** next function does not use 'modf', avoiding problems with 'double*'
111** (which is not compatible with 'float*') when lua_Number is not142** (which is not compatible with 'float*') when lua_Number is not
112** 'double'.143** 'double'.
113*/144*/
114static int math_modf (lua_State *L) {145static int math_modf (lua_State *L) {
115 lua_Number n = luaL_checknumber(L, 1);146 if (lua_isinteger(L ,1)) {
116 /* integer part (rounds toward zero) */147 lua_settop(L, 1); /* number is its own integer part */
117 lua_Number ip = (n < 0) ? -l_mathop(floor)(-n) : l_mathop(floor)(n);148 lua_pushnumber(L, 0); /* no fractionary part */
118 lua_pushnumber(L, ip);149 }
119 /* fractionary part (test handles inf/-inf) */150 else {
120 lua_pushnumber(L, (n == ip) ? 0.0 : (n - ip));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 }
121 return 2;158 return 2;
122}159}
123 160
@@ -127,13 +164,6 @@ static int math_sqrt (lua_State *L) { · 3 unmodified lines
127 return 1;164 return 1;
128}165}
129 166
130static 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
137static int math_log (lua_State *L) {167static int math_log (lua_State *L) {
138 lua_Number x = luaL_checknumber(L, 1);168 lua_Number x = luaL_checknumber(L, 1);
139 lua_Number res;169 lua_Number res;
@@ -141,84 +171,64 @@ static int math_log (lua_State *L) { · 1 unmodified line
141 res = l_mathop(log)(x);171 res = l_mathop(log)(x);
142 else {172 else {
143 lua_Number base = luaL_checknumber(L, 2);173 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);
145 else res = l_mathop(log)(x)/l_mathop(log)(base);175 else res = l_mathop(log)(x)/l_mathop(log)(base);
146 }176 }
147 lua_pushnumber(L, res);177 lua_pushnumber(L, res);
148 return 1;178 return 1;
149}179}
150 180
151#if defined(LUA_COMPAT_LOG10)
152static int math_log10 (lua_State *L) {
153 lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1)));
154 return 1;
155}
156#endif
157
158static int math_exp (lua_State *L) {181static int math_exp (lua_State *L) {
159 lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1)));182 lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1)));
160 return 1;183 return 1;
161}184}
162 185
163static int math_deg (lua_State *L) {186static 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));
165 return 1;188 return 1;
166}189}
167 190
168static int math_rad (lua_State *L) {191static 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));
170 return 1;193 return 1;
171}194}
172 195
173static 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
180static 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
188 196
189static int math_min (lua_State *L) {197static int math_min (lua_State *L) {
190 int n = lua_gettop(L); /* number of arguments */198 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 */
192 int i;200 int i;
193 for (i=2; i<=n; i++) {201 luaL_argcheck(L, n >= 1, 1, "value expected");
194 lua_Number d = luaL_checknumber(L, i);202 for (i = 2; i <= n; i++) {
195 if (d < dmin)203 if (lua_compare(L, i, imin, LUA_OPLT))
196 dmin = d;204 imin = i;
197 }205 }
198 lua_pushnumber(L, dmin);206 lua_pushvalue(L, imin);
199 return 1;207 return 1;
200}208}
201 209
202 210
203static int math_max (lua_State *L) {211static int math_max (lua_State *L) {
204 int n = lua_gettop(L); /* number of arguments */212 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 */
206 int i;214 int i;
207 for (i=2; i<=n; i++) {215 luaL_argcheck(L, n >= 1, 1, "value expected");
208 lua_Number d = luaL_checknumber(L, i);216 for (i = 2; i <= n; i++) {
209 if (d > dmax)217 if (lua_compare(L, imax, i, LUA_OPLT))
210 dmax = d;218 imax = i;
211 }219 }
212 lua_pushnumber(L, dmax);220 lua_pushvalue(L, imax);
213 return 1;221 return 1;
214}222}
215 223
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*/
217static int math_random (lua_State *L) {229static 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;
221 lua_Integer low, up;230 lua_Integer low, up;
231 double r = (double)l_rand() * (1.0 / ((double)RAND_MAX + 1.0));
222 switch (lua_gettop(L)) { /* check number of arguments */232 switch (lua_gettop(L)) { /* check number of arguments */
223 case 0: { /* no arguments */233 case 0: { /* no arguments */
224 lua_pushnumber(L, r); /* Number between 0 and 1 */234 lua_pushnumber(L, r); /* Number between 0 and 1 */
@@ -237,15 +247,17 @@ static int math_random (lua_State *L) { · 12 unmodified lines
237 default: return luaL_error(L, "wrong number of arguments");247 default: return luaL_error(L, "wrong number of arguments");
238 }248 }
239 /* random integer in the interval [low, up] */249 /* random integer in the interval [low, up] */
240 up++; /* change interval to [low, up) */250 luaL_argcheck(L, low <= up, 1, "interval is empty");
241 luaL_argcheck(L, up - low > 0, 1, "interval is empty");251 luaL_argcheck(L, (lua_Unsigned)up - low <= (lua_Unsigned)LUA_MAXINTEGER,
242 lua_pushinteger(L, (lua_Integer)(r * (lua_Number)(up - low)) + low);252 1, "interval too large");
253 r *= (double)(up - low) + 1.0;
254 lua_pushinteger(L, (lua_Integer)r + low);
243 return 1;255 return 1;
244}256}
245 257
246 258
247static int math_randomseed (lua_State *L) {259static int math_randomseed (lua_State *L) {
248 srand(luaL_checkunsigned(L, 1));260 l_srand((unsigned int)luaL_checkunsigned(L, 1));
249 (void)rand(); /* discard first value to avoid undesirable correlations */261 (void)rand(); /* discard first value to avoid undesirable correlations */
250 return 0;262 return 0;
251}263}
@@ -265,39 +277,92 @@ static int math_type (lua_State *L) { · 13 unmodified lines
265}277}
266 278
267 279
280/*
281** {==================================================================
282** Deprecated functions (for compatibility only)
283** ===================================================================
284*/
285#if defined(LUA_COMPAT_MATHLIB)
286
287static int math_cosh (lua_State *L) {
288 lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1)));
289 return 1;
290}
291
292static int math_sinh (lua_State *L) {
293 lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1)));
294 return 1;
295}
296
297static int math_tanh (lua_State *L) {
298 lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1)));
299 return 1;
300}
301
302static 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
309static 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
316static 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
323static 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
268static const luaL_Reg mathlib[] = {333static const luaL_Reg mathlib[] = {
269 {"abs", math_abs},334 {"abs", math_abs},
270 {"acos", math_acos},335 {"acos", math_acos},
271 {"asin", math_asin},336 {"asin", math_asin},
272 {"atan2", math_atan2},
273 {"atan", math_atan},337 {"atan", math_atan},
274 {"ceil", math_ceil},338 {"ceil", math_ceil},
275 {"cosh", math_cosh},
276 {"cos", math_cos},339 {"cos", math_cos},
277 {"deg", math_deg},340 {"deg", math_deg},
278 {"exp", math_exp},341 {"exp", math_exp},
279 {"floor", math_floor},
280 {"ifloor", math_ifloor},342 {"ifloor", math_ifloor},
343 {"floor", math_floor},
281 {"fmod", math_fmod},344 {"fmod", math_fmod},
282 {"frexp", math_frexp},
283 {"ldexp", math_ldexp},
284#if defined(LUA_COMPAT_LOG10)
285 {"log10", math_log10},
286#endif
287 {"log", math_log},345 {"log", math_log},
288 {"max", math_max},346 {"max", math_max},
289 {"min", math_min},347 {"min", math_min},
290 {"modf", math_modf},348 {"modf", math_modf},
291 {"pow", math_pow},
292 {"rad", math_rad},349 {"rad", math_rad},
293 {"random", math_random},350 {"random", math_random},
294 {"randomseed", math_randomseed},351 {"randomseed", math_randomseed},
295 {"sinh", math_sinh},
296 {"sin", math_sin},352 {"sin", math_sin},
297 {"sqrt", math_sqrt},353 {"sqrt", math_sqrt},
298 {"tanh", math_tanh},
299 {"tan", math_tan},354 {"tan", math_tan},
300 {"type", math_type},355 {"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
301 {NULL, NULL}366 {NULL, NULL}
302};367};
303 368
@@ -311,6 +376,10 @@ LUAMOD_API int luaopen_math (lua_State *L) { · 7 unmodified lines
311 lua_setfield(L, -2, "pi");376 lua_setfield(L, -2, "pi");
312 lua_pushnumber(L, HUGE_VAL);377 lua_pushnumber(L, HUGE_VAL);
313 lua_setfield(L, -2, "huge");378 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");
314 return 1;383 return 1;
315}384}
316 385
src/lobject.c+37−17
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Some generic functions over Lua objects3** Some generic functions over Lua objects
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -77,7 +77,6 @@ static lua_Integer intarith (lua_State *L, int op, lua_Integer v1, · 71 unmodified lines
77 case LUA_OPSUB:return intop(-, v1, v2);77 case LUA_OPSUB:return intop(-, v1, v2);
78 case LUA_OPMUL:return intop(*, v1, v2);78 case LUA_OPMUL:return intop(*, v1, v2);
79 case LUA_OPMOD: return luaV_mod(L, v1, v2);79 case LUA_OPMOD: return luaV_mod(L, v1, v2);
80 case LUA_OPPOW: return luaV_pow(L, v1, v2);
81 case LUA_OPIDIV: return luaV_div(L, v1, v2);80 case LUA_OPIDIV: return luaV_div(L, v1, v2);
82 case LUA_OPBAND: return intop(&, v1, v2);81 case LUA_OPBAND: return intop(&, v1, v2);
83 case LUA_OPBOR: return intop(|, v1, v2);82 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
85 case LUA_OPSHL: return luaV_shiftl(v1, v2);84 case LUA_OPSHL: return luaV_shiftl(v1, v2);
86 case LUA_OPSHR: return luaV_shiftl(v1, -v2);85 case LUA_OPSHR: return luaV_shiftl(v1, -v2);
87 case LUA_OPUNM: return intop(-, 0, v1);86 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);
89 default: lua_assert(0); return 0;88 default: lua_assert(0); return 0;
90 }89 }
91}90}
@@ -98,9 +97,13 @@ static lua_Number numarith (lua_State *L, int op, lua_Number v1, · 6 unmodified lines
98 case LUA_OPSUB: return luai_numsub(L, v1, v2);97 case LUA_OPSUB: return luai_numsub(L, v1, v2);
99 case LUA_OPMUL: return luai_nummul(L, v1, v2);98 case LUA_OPMUL: return luai_nummul(L, v1, v2);
100 case LUA_OPDIV: return luai_numdiv(L, v1, v2);99 case LUA_OPDIV: return luai_numdiv(L, v1, v2);
101 case LUA_OPMOD: return luai_nummod(L, v1, v2);
102 case LUA_OPPOW: return luai_numpow(L, v1, v2);100 case LUA_OPPOW: return luai_numpow(L, v1, v2);
103 case LUA_OPUNM: return luai_numunm(L, v1);101 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 }
104 default: lua_assert(0); return 0;107 default: lua_assert(0); return 0;
105 }108 }
106}109}
@@ -119,10 +122,10 @@ void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, · 12 unmodified lines
119 }122 }
120 else break; /* go to the end */123 else break; /* go to the end */
121 }124 }
122 case LUA_OPDIV: { /* operates only on floats */125 case LUA_OPDIV: case LUA_OPPOW: { /* operate only on floats */
123 lua_Number n1; lua_Number n2;126 lua_Number n1; lua_Number n2;
124 if (tonumber(p1, &n1) && tonumber(p2, &n2)) {127 if (tonumber(p1, &n1) && tonumber(p2, &n2)) {
125 setnvalue(res, numarith(L, op, n1, n2));128 setfltvalue(res, numarith(L, op, n1, n2));
126 return;129 return;
127 }130 }
128 else break; /* go to the end */131 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
134 return;137 return;
135 }138 }
136 else if (tonumber(p1, &n1) && tonumber(p2, &n2)) {139 else if (tonumber(p1, &n1) && tonumber(p2, &n2)) {
137 setnvalue(res, numarith(L, op, n1, n2));140 setfltvalue(res, numarith(L, op, n1, n2));
138 return;141 return;
139 }142 }
140 else break; /* go to the end */143 else break; /* go to the end */
@@ -249,22 +252,21 @@ static lua_Number lua_strx2number (const char *s, char **endptr) { · 108 unmodified lines
249/* }====================================================== */252/* }====================================================== */
250 253
251 254
252int luaO_str2d (const char *s, size_t len, lua_Number *result) {255static const char *l_str2d (const char *s, lua_Number *result) {
253 char *endptr;256 char *endptr;
254 if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */257 if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */
255 return 0;258 return NULL;
256 else if (strpbrk(s, "xX")) /* hexa? */259 else if (strpbrk(s, "xX")) /* hexa? */
257 *result = lua_strx2number(s, &endptr);260 *result = lua_strx2number(s, &endptr);
258 else261 else
259 *result = lua_str2number(s, &endptr);262 *result = lua_str2number(s, &endptr);
260 if (endptr == s) return 0; /* nothing recognized */263 if (endptr == s) return 0; /* nothing recognized */
261 while (lisspace(cast_uchar(*endptr))) endptr++;264 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 */
263}266}
264 267
265 268
266int luaO_str2int (const char *s, size_t len, lua_Integer *result) {269static const char *l_str2int (const char *s, lua_Integer *result) {
267 const char *ends = s + len;
268 lua_Unsigned a = 0;270 lua_Unsigned a = 0;
269 int empty = 1;271 int empty = 1;
270 int neg;272 int neg;
@@ -285,14 +287,29 @@ int luaO_str2int (const char *s, size_t len, lua_Integer *result) { · 14 unmodified lines
285 }287 }
286 }288 }
287 while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */289 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 */
289 else {291 else {
290 *result = cast_integer((neg) ? 0u - a : a);292 *result = l_castU2S((neg) ? 0u - a : a);
291 return 1;293 return s;
292 }294 }
293}295}
294 296
295 297
298size_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
296int luaO_utf8esc (char *buff, unsigned int x) {313int luaO_utf8esc (char *buff, unsigned int x) {
297 int n = 1; /* number of bytes put in buffer (backwards) */314 int n = 1; /* number of bytes put in buffer (backwards) */
298 if (x < 0x80) /* ascii? */315 if (x < 0x80) /* ascii? */
@@ -339,14 +356,17 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { · 40 unmodified lines
339 }356 }
340 case 'd': {357 case 'd': {
341 setivalue(L->top++, cast_int(va_arg(argp, int)));358 setivalue(L->top++, cast_int(va_arg(argp, int)));
359 luaV_tostring(L, L->top - 1);
342 break;360 break;
343 }361 }
344 case 'I': {362 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);
346 break;365 break;
347 }366 }
348 case 'f': {367 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);
350 break;370 break;
351 }371 }
352 case 'p': {372 case 'p': {
src/lobject.h+39−45
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Type definitions for Lua objects3** Type definitions for Lua objects
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -156,17 +156,17 @@ typedef struct lua_TValue TValue; · 150 unmodified lines
156#define fltvalue(o) check_exp(ttisfloat(o), val_(o).n)156#define fltvalue(o) check_exp(ttisfloat(o), val_(o).n)
157#define gcvalue(o) check_exp(iscollectable(o), val_(o).gc)157#define gcvalue(o) check_exp(iscollectable(o), val_(o).gc)
158#define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)158#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))
160#define tsvalue(o) (&rawtsvalue(o)->tsv)160#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))
162#define uvalue(o) (&rawuvalue(o)->uv)162#define uvalue(o) (&rawuvalue(o)->uv)
163#define clvalue(o) check_exp(ttisclosure(o), &val_(o).gc->cl)163#define clvalue(o) check_exp(ttisclosure(o), gco2cl(val_(o).gc))
164#define clLvalue(o) check_exp(ttisLclosure(o), &val_(o).gc->cl.l)164#define clLvalue(o) check_exp(ttisLclosure(o), gco2lcl(val_(o).gc))
165#define clCvalue(o) check_exp(ttisCclosure(o), &val_(o).gc->cl.c)165#define clCvalue(o) check_exp(ttisCclosure(o), gco2ccl(val_(o).gc))
166#define fvalue(o) check_exp(ttislcf(o), val_(o).f)166#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))
168#define bvalue(o) check_exp(ttisboolean(o), val_(o).b)168#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))
170/* a dead value may get the 'gc' field, but cannot access its contents */170/* a dead value may get the 'gc' field, but cannot access its contents */
171#define deadvalue(o) check_exp(ttisdeadkey(o), cast(void *, val_(o).gc))171#define deadvalue(o) check_exp(ttisdeadkey(o), cast(void *, val_(o).gc))
172 172
@@ -187,7 +187,7 @@ typedef struct lua_TValue TValue; · 14 unmodified lines
187/* Macros to set values */187/* Macros to set values */
188#define settt_(o,t) ((o)->tt_=(t))188#define settt_(o,t) ((o)->tt_=(t))
189 189
190#define setnvalue(obj,x) \190#define setfltvalue(obj,x) \
191 { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_TNUMFLT); }191 { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_TNUMFLT); }
192 192
193#define setivalue(obj,x) \193#define setivalue(obj,x) \
@@ -205,38 +205,37 @@ typedef struct lua_TValue TValue; · 11 unmodified lines
205 { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); }205 { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); }
206 206
207#define setgcovalue(L,obj,x) \207#define setgcovalue(L,obj,x) \
208 { TValue *io=(obj); GCObject *i_g=(x); \208 { TValue *io = (obj); GCObject *i_g=(x); \
209 val_(io).gc=i_g; settt_(io, ctb(gch(i_g)->tt)); }209 val_(io).gc = i_g; settt_(io, ctb(gch(i_g)->tt)); }
210 210
211#define setsvalue(L,obj,x) \211#define setsvalue(L,obj,x) \
212 { TValue *io=(obj); \212 { TValue *io = (obj); TString *x_ = (x); \
213 TString *x_ = (x); \213 val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tsv.tt)); \
214 val_(io).gc=cast(GCObject *, x_); settt_(io, ctb(x_->tsv.tt)); \
215 checkliveness(G(L),io); }214 checkliveness(G(L),io); }
216 215
217#define setuvalue(L,obj,x) \216#define setuvalue(L,obj,x) \
218 { TValue *io=(obj); \217 { TValue *io = (obj); Udata *x_ = (x); \
219 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TUSERDATA)); \218 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TUSERDATA)); \
220 checkliveness(G(L),io); }219 checkliveness(G(L),io); }
221 220
222#define setthvalue(L,obj,x) \221#define setthvalue(L,obj,x) \
223 { TValue *io=(obj); \222 { TValue *io = (obj); lua_State *x_ = (x); \
224 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTHREAD)); \223 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTHREAD)); \
225 checkliveness(G(L),io); }224 checkliveness(G(L),io); }
226 225
227#define setclLvalue(L,obj,x) \226#define setclLvalue(L,obj,x) \
228 { TValue *io=(obj); \227 { TValue *io = (obj); LClosure *x_ = (x); \
229 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TLCL)); \228 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TLCL)); \
230 checkliveness(G(L),io); }229 checkliveness(G(L),io); }
231 230
232#define setclCvalue(L,obj,x) \231#define setclCvalue(L,obj,x) \
233 { TValue *io=(obj); \232 { TValue *io = (obj); CClosure *x_ = (x); \
234 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TCCL)); \233 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TCCL)); \
235 checkliveness(G(L),io); }234 checkliveness(G(L),io); }
236 235
237#define sethvalue(L,obj,x) \236#define sethvalue(L,obj,x) \
238 { TValue *io=(obj); \237 { TValue *io = (obj); Table *x_ = (x); \
239 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTABLE)); \238 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTABLE)); \
240 checkliveness(G(L),io); }239 checkliveness(G(L),io); }
241 240
242#define setdeadvalue(obj) settt_(obj, LUA_TDEADKEY)241#define setdeadvalue(obj) settt_(obj, LUA_TDEADKEY)
@@ -246,7 +245,7 @@ typedef struct lua_TValue TValue; · 3 unmodified lines
246#define setobj(L,obj1,obj2) \245#define setobj(L,obj1,obj2) \
247 { const TValue *io2=(obj2); TValue *io1=(obj1); \246 { const TValue *io2=(obj2); TValue *io1=(obj1); \
248 io1->value_ = io2->value_; io1->tt_ = io2->tt_; \247 io1->value_ = io2->value_; io1->tt_ = io2->tt_; \
249 checkliveness(G(L),io1); }248 (void)L; checkliveness(G(L),io1); }
250 249
251 250
252/*251/*
@@ -269,10 +268,6 @@ typedef struct lua_TValue TValue; · 16 unmodified lines
269#define setsvalue2n setsvalue268#define setsvalue2n setsvalue
270 269
271 270
272/* check whether a number is valid (useful only for NaN trick) */
273#define luai_checknum(L,o,c) { /* empty */ }
274
275
276 271
277 272
278/*273/*
@@ -310,9 +305,9 @@ typedef union TString { · 31 unmodified lines
310 struct {305 struct {
311 CommonHeader;306 CommonHeader;
312 lu_byte extra; /* reserved words for short strings; "has hash" for longs */307 lu_byte extra; /* reserved words for short strings; "has hash" for longs */
308 unsigned int hash;
313 size_t len; /* number of characters in string */309 size_t len; /* number of characters in string */
314 union TString *hnext; /* linked list for hash table */310 union TString *hnext; /* linked list for hash table */
315 unsigned int hash;
316 } tsv;311 } tsv;
317} TString;312} TString;
318 313
@@ -377,14 +372,9 @@ typedef struct LocVar { · 58 unmodified lines
377*/372*/
378typedef struct Proto {373typedef struct Proto {
379 CommonHeader;374 CommonHeader;
380 TValue *k; /* constants used by the function */375 lu_byte numparams; /* number of fixed parameters */
381 Instruction *code;376 lu_byte is_vararg;
382 struct Proto **p; /* functions defined inside the function */377 lu_byte maxstacksize; /* maximum stack used by this 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 */
388 int sizeupvalues; /* size of 'upvalues' */378 int sizeupvalues; /* size of 'upvalues' */
389 int sizek; /* size of `k' */379 int sizek; /* size of `k' */
390 int sizecode;380 int sizecode;
@@ -393,10 +383,15 @@ typedef struct Proto { · 2 unmodified lines
393 int sizelocvars;383 int sizelocvars;
394 int linedefined;384 int linedefined;
395 int lastlinedefined;385 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 */
396 GCObject *gclist;394 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 */
400} Proto;395} Proto;
401 396
402 397
@@ -462,12 +457,12 @@ typedef struct Table { · 59 unmodified lines
462 CommonHeader;457 CommonHeader;
463 lu_byte flags; /* 1<<p means tagmethod(p) is not present */458 lu_byte flags; /* 1<<p means tagmethod(p) is not present */
464 lu_byte lsizenode; /* log2 of size of `node' array */459 lu_byte lsizenode; /* log2 of size of `node' array */
465 struct Table *metatable;460 int sizearray; /* size of `array' array */
466 TValue *array; /* array part */461 TValue *array; /* array part */
467 Node *node;462 Node *node;
468 Node *lastfree; /* any free position is before this position */463 Node *lastfree; /* any free position is before this position */
464 struct Table *metatable;
469 GCObject *gclist;465 GCObject *gclist;
470 int sizearray; /* size of `array' array */
471} Table;466} Table;
472 467
473 468
@@ -500,8 +495,7 @@ LUAI_FUNC int luaO_utf8esc (char *buff, unsigned int x); · 26 unmodified lines
500LUAI_FUNC int luaO_ceillog2 (unsigned int x);495LUAI_FUNC int luaO_ceillog2 (unsigned int x);
501LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1,496LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1,
502 const TValue *p2, TValue *res);497 const TValue *p2, TValue *res);
503LUAI_FUNC int luaO_str2d (const char *s, size_t len, lua_Number *result);498LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o);
504LUAI_FUNC int luaO_str2int (const char *s, size_t len, lua_Integer *result);
505LUAI_FUNC int luaO_hexavalue (int c);499LUAI_FUNC int luaO_hexavalue (int c);
506LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,500LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
507 va_list argp);501 va_list argp);
src/loslib.c+8−2
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Standard Operating System library3** Standard Operating System library
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -59,9 +59,15 @@ · 53 unmodified lines
59#if defined(LUA_USE_POSIX) /* { */59#if defined(LUA_USE_POSIX) /* { */
60 60
61#include <unistd.h>61#include <unistd.h>
62
62#define LUA_TMPNAMBUFSIZE 3263#define LUA_TMPNAMBUFSIZE 32
64
65#if !defined(LUA_TMPNAMTEMPLATE)
66#define LUA_TMPNAMTEMPLATE "/tmp/lua_XXXXXX"
67#endif
68
63#define lua_tmpnam(b,e) { \69#define lua_tmpnam(b,e) { \
64 strcpy(b, "/tmp/lua_XXXXXX"); \70 strcpy(b, LUA_TMPNAMTEMPLATE); \
65 e = mkstemp(b); \71 e = mkstemp(b); \
66 if (e != -1) close(e); \72 if (e != -1) close(e); \
67 e = (e == -1); }73 e = (e == -1); }
src/lparser.c+5−5
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Lua Parser3** Lua Parser
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -1618,17 +1618,17 @@ static void mainfunc (LexState *ls, FuncState *fs) { · 1612 unmodified lines
1618}1618}
1619 1619
1620 1620
1621Closure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,1621LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
1622 Dyndata *dyd, const char *name, int firstchar) {1622 Dyndata *dyd, const char *name, int firstchar) {
1623 LexState lexstate;1623 LexState lexstate;
1624 FuncState funcstate;1624 FuncState funcstate;
1625 Closure *cl = luaF_newLclosure(L, 1); /* create main closure */1625 LClosure *cl = luaF_newLclosure(L, 1); /* create main closure */
1626 setclLvalue(L, L->top, cl); /* anchor it (to avoid being collected) */1626 setclLvalue(L, L->top, cl); /* anchor it (to avoid being collected) */
1627 incr_top(L);1627 incr_top(L);
1628 lexstate.h = luaH_new(L); /* create table for scanner */1628 lexstate.h = luaH_new(L); /* create table for scanner */
1629 sethvalue(L, L->top, lexstate.h); /* anchor it */1629 sethvalue(L, L->top, lexstate.h); /* anchor it */
1630 incr_top(L);1630 incr_top(L);
1631 funcstate.f = cl->l.p = luaF_newproto(L);1631 funcstate.f = cl->p = luaF_newproto(L);
1632 funcstate.f->source = luaS_new(L, name); /* create and anchor TString */1632 funcstate.f->source = luaS_new(L, name); /* create and anchor TString */
1633 luaC_objbarrier(L, funcstate.f, funcstate.f->source);1633 luaC_objbarrier(L, funcstate.f, funcstate.f->source);
1634 lexstate.buff = buff;1634 lexstate.buff = buff;
src/lparser.h+3−3
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Lua Parser3** Lua Parser
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -113,8 +113,8 @@ typedef struct FuncState { · 107 unmodified lines
113} FuncState;113} FuncState;
114 114
115 115
116LUAI_FUNC Closure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,116LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
117 Dyndata *dyd, const char *name, int firstchar);117 Dyndata *dyd, const char *name, int firstchar);
118 118
119 119
120#endif120#endif
src/lstate.h+14−14
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Global State3** Global State
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -60,20 +60,18 @@ typedef struct CallInfo { · 54 unmodified lines
60 StkId func; /* function index in the stack */60 StkId func; /* function index in the stack */
61 StkId top; /* top for this function */61 StkId top; /* top for this function */
62 struct CallInfo *previous, *next; /* dynamic call link */62 struct CallInfo *previous, *next; /* dynamic call link */
63 ptrdiff_t extra;
63 short nresults; /* expected number of results from this function */64 short nresults; /* expected number of results from this function */
64 lu_byte callstatus;65 lu_byte callstatus;
65 ptrdiff_t extra;
66 union {66 union {
67 struct { /* only for Lua functions */67 struct { /* only for Lua functions */
68 StkId base; /* base for this function */68 StkId base; /* base for this function */
69 const Instruction *savedpc;69 const Instruction *savedpc;
70 } l;70 } l;
71 struct { /* only for C functions */71 struct { /* only for C functions */
72 int ctx; /* context info. in case of yields */72 lua_KFunction k; /* continuation in case of yields */
73 lua_CFunction k; /* continuation in case of yields */
74 ptrdiff_t old_errfunc;73 ptrdiff_t old_errfunc;
75 lu_byte old_allowhook;74 int ctx; /* context info. in case of yields */
76 lu_byte status;
77 } c;75 } c;
78 } u;76 } u;
79} CallInfo;77} CallInfo;
@@ -82,19 +80,21 @@ typedef struct CallInfo { · 2 unmodified lines
82/*80/*
83** Bits in CallInfo status81** Bits in CallInfo status
84*/82*/
85#define CIST_LUA (1<<0) /* call is running a Lua function */83#define CIST_OAH (1<<0) /* original value of 'allowhook' */
86#define CIST_HOOKED (1<<1) /* call is running a debug hook */84#define CIST_LUA (1<<1) /* call is running a Lua function */
87#define CIST_REENTRY (1<<2) /* call is running on same invocation of85#define CIST_HOOKED (1<<2) /* call is running a debug hook */
86#define CIST_REENTRY (1<<3) /* call is running on same invocation of
88 luaV_execute of previous call */87 luaV_execute of previous call */
89#define CIST_YIELDED (1<<3) /* call reentered after suspension */
90#define CIST_YPCALL (1<<4) /* call is a yieldable protected call */88#define CIST_YPCALL (1<<4) /* call is a yieldable protected call */
91#define CIST_STAT (1<<5) /* call has an error status (pcall) */89#define CIST_TAIL (1<<5) /* call was tail called */
92#define CIST_TAIL (1<<6) /* call was tail called */90#define CIST_HOOKYIELD (1<<6) /* last hook called yielded */
93#define CIST_HOOKYIELD (1<<7) /* last hook called yielded */
94
95 91
96#define isLua(ci) ((ci)->callstatus & CIST_LUA)92#define isLua(ci) ((ci)->callstatus & CIST_LUA)
97 93
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
98 98
99/*99/*
100** `global state', shared by all threads of this state100** `global state', shared by all threads of this state
src/lstring.c+8−4
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** String table (keeps all strings handled by Lua)3** String table (keeps all strings handled by Lua)
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -90,9 +90,11 @@ void luaS_resize (lua_State *L, int newsize) { · 84 unmodified lines
90static TString *createstrobj (lua_State *L, const char *str, size_t l,90static TString *createstrobj (lua_State *L, const char *str, size_t l,
91 int tag, unsigned int h) {91 int tag, unsigned int h) {
92 TString *ts;92 TString *ts;
93 GCObject *o;
93 size_t totalsize; /* total size of TString object */94 size_t totalsize; /* total size of TString object */
94 totalsize = sizeof(TString) + ((l + 1) * sizeof(char));95 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);
96 ts->tsv.len = l;98 ts->tsv.len = l;
97 ts->tsv.hash = h;99 ts->tsv.hash = h;
98 ts->tsv.extra = 0;100 ts->tsv.extra = 0;
@@ -102,7 +104,7 @@ static TString *createstrobj (lua_State *L, const char *str, size_t l, · 3 unmodified lines
102}104}
103 105
104 106
105LUAI_FUNC void luaS_remove (lua_State *L, TString *ts) {107void luaS_remove (lua_State *L, TString *ts) {
106 stringtable *tb = &G(L)->strt;108 stringtable *tb = &G(L)->strt;
107 TString **p = &tb->hash[lmod(ts->tsv.hash, tb->size)];109 TString **p = &tb->hash[lmod(ts->tsv.hash, tb->size)];
108 while (*p != ts) /* find previous element */110 while (*p != ts) /* find previous element */
@@ -165,9 +167,11 @@ TString *luaS_new (lua_State *L, const char *str) { · 56 unmodified lines
165 167
166Udata *luaS_newudata (lua_State *L, size_t s) {168Udata *luaS_newudata (lua_State *L, size_t s) {
167 Udata *u;169 Udata *u;
170 GCObject *o;
168 if (s > MAX_SIZE - sizeof(Udata))171 if (s > MAX_SIZE - sizeof(Udata))
169 luaM_toobig(L);172 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);
171 u->uv.len = s;175 u->uv.len = s;
172 u->uv.metatable = NULL;176 u->uv.metatable = NULL;
173 setuservalue(L, u, luaO_nilobject);177 setuservalue(L, u, luaO_nilobject);
src/lstrlib.c+54−47
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Standard library for string operations and pattern-matching3** Standard library for string operations and pattern-matching
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -104,8 +104,8 @@ static int str_upper (lua_State *L) { · 98 unmodified lines
104 104
105 105
106/* reasonable limit to avoid arithmetic overflow and strings too big */106/* reasonable limit to avoid arithmetic overflow and strings too big */
107#if INT_MAX / 2 <= 0x10000000107#if LUA_MAXINTEGER / 2 <= 0x10000000
108#define MAXSIZE ((size_t)(INT_MAX / 2))108#define MAXSIZE ((size_t)(LUA_MAXINTEGER / 2))
109#else109#else
110#define MAXSIZE ((size_t)0x10000000)110#define MAXSIZE ((size_t)0x10000000)
111#endif111#endif
@@ -946,22 +946,22 @@ static int str_format (lua_State *L) { · 834 unmodified lines
946** =======================================================946** =======================================================
947*/947*/
948 948
949/* maximum size for the binary representation of an integer */
950#define MAXINTSIZE 8
951
952 949
953/* number of bits in a character */950/* number of bits in a character */
954#define NB CHAR_BIT951#define NB CHAR_BIT
955 952
956/* mask for one character (NB ones) */953/* mask for one character (NB 1's) */
957#define MC (((lua_Integer)1 << NB) - 1)954#define MC ((1 << NB) - 1)
958
959/* mask for one character without sign ((NB - 1) ones) */
960#define SM (((lua_Integer)1 << (NB - 1)) - 1)
961 955
956/* mask for one character without sign bit ((NB - 1) 1's) */
957#define SM (MC >> 1)
962 958
959/* size of a lua_Integer */
963#define SZINT ((int)sizeof(lua_Integer))960#define SZINT ((int)sizeof(lua_Integer))
964 961
962/* maximum size for the binary representation of an integer */
963#define MAXINTSIZE 12
964
965 965
966static union {966static union {
967 int dummy;967 int dummy;
@@ -975,7 +975,7 @@ static int getendian (lua_State *L, int arg) { · 7 unmodified lines
975 if (*endian == 'n') /* native? */975 if (*endian == 'n') /* native? */
976 return nativeendian.little;976 return nativeendian.little;
977 luaL_argcheck(L, *endian == 'l' || *endian == 'b', arg,977 luaL_argcheck(L, *endian == 'l' || *endian == 'b', arg,
978 "endianess must be 'l'/'b'/'n'");978 "endianness must be 'l'/'b'/'n'");
979 return (*endian == 'l');979 return (*endian == 'l');
980}980}
981 981
@@ -989,34 +989,43 @@ static int getintsize (lua_State *L, int arg) { · 7 unmodified lines
989}989}
990 990
991 991
992static 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
996static int dumpint (char *buff, lua_Integer m, int littleendian, int size) {
993 int i;997 int i;
998 lua_Unsigned n = (lua_Unsigned)m;
999 lua_Unsigned mask = (m >= 0) ? 0 : HIGHERBYTE; /* sign extension */
994 if (littleendian) {1000 if (littleendian) {
995 for (i = 0; i < size - 1; i++) {1001 for (i = 0; i < size - 1; i++) {
996 buff[i] = (n & MC);1002 buff[i] = (n & MC);
997 n >>= NB;1003 n = (n >> NB) | mask;
998 }1004 }
999 }1005 }
1000 else {1006 else {
1001 for (i = size - 1; i > 0; i--) {1007 for (i = size - 1; i > 0; i--) {
1002 buff[i] = (n & MC);1008 buff[i] = (n & MC);
1003 n >>= NB;1009 n = (n >> NB) | mask;
1004 }1010 }
1005 }1011 }
1006 buff[i] = (n & MC); /* last byte */1012 buff[i] = (n & MC); /* last byte */
1007 /* test for overflow: OK if there are only zeros left in higher bytes,1013 if (size < SZINT) { /* need test for overflow? */
1008 or if there are only ones left and packed number is negative (signal1014 /* OK if there are only zeros left in higher bytes,
1009 bit, the higher bit in last byte, is one) */1015 or only ones left (excluding non-signal bits in last byte) */
1010 return ((n & ~MC) == 0 || (n | SM) == ~(lua_Integer)0);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 */
1011}1020}
1012 1021
1013 1022
1014static int packint_l (lua_State *L) {1023static int dumpint_l (lua_State *L) {
1015 char buff[MAXINTSIZE];1024 char buff[MAXINTSIZE];
1016 lua_Integer n = luaL_checkinteger(L, 1);1025 lua_Integer n = luaL_checkinteger(L, 1);
1017 int size = getintsize(L, 2);1026 int size = getintsize(L, 2);
1018 int endian = getendian(L, 3);1027 int endian = getendian(L, 3);
1019 if (packint(buff, n, endian, size))1028 if (dumpint(buff, n, endian, size))
1020 lua_pushlstring(L, buff, size);1029 lua_pushlstring(L, buff, size);
1021 else1030 else
1022 luaL_error(L, "integer does not fit into given size (%d)", size);1031 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
1024}1033}
1025 1034
1026 1035
1027/* mask to check higher-order byte in a Lua integer */
1028#define HIGHERBYTE (MC << (NB * (SZINT - 1)))
1029
1030/* mask to check higher-order byte + signal bit of next (lower) byte */1036/* mask to check higher-order byte + signal bit of next (lower) byte */
1031#define HIGHERBYTE1 (HIGHERBYTE | (HIGHERBYTE >> 1))1037#define HIGHERBYTE1 (HIGHERBYTE | (HIGHERBYTE >> 1))
1032 1038
1033static int unpackint (const char *buff, lua_Integer *res,1039
1040static int undumpint (const char *buff, lua_Integer *res,
1034 int littleendian, int size) {1041 int littleendian, int size) {
1035 lua_Integer n = 0;1042 lua_Unsigned n = 0;
1036 int i;1043 int i;
1037 for (i = 0; i < size; i++) {1044 for (i = 0; i < size; i++) {
1038 if (i >= SZINT) { /* will throw away a byte? */1045 if (i >= SZINT) { /* will throw away a byte? */
@@ -1042,23 +1049,23 @@ static int unpackint (const char *buff, lua_Integer *res, · 3 unmodified lines
1042 its "signal bit" */1049 its "signal bit" */
1043 if (!((n & HIGHERBYTE1) == 0 || /* zeros for positive number */1050 if (!((n & HIGHERBYTE1) == 0 || /* zeros for positive number */
1044 (n & HIGHERBYTE1) == HIGHERBYTE1 || /* ones for negative number */1051 (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 */
1046 return 0; /* overflow */1053 return 0; /* overflow */
1047 }1054 }
1048 n <<= NB;1055 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];
1050 }1057 }
1051 if (size < SZINT) { /* need sign extension? */1058 if (size < SZINT) { /* need sign extension? */
1052 lua_Integer mask = (~(lua_Integer)0) << (size*NB - 1);1059 lua_Unsigned mask = (lua_Unsigned)1 << (size*NB - 1);
1053 if (n & mask) /* negative value? */1060 *res = (lua_Integer)((n ^ mask) - mask); /* do sign extension */
1054 n |= mask; /* signal extension */
1055 }1061 }
1056 *res = n;1062 else
1063 *res = (lua_Integer)n;
1057 return 1;1064 return 1;
1058}1065}
1059 1066
1060 1067
1061static int unpackint_l (lua_State *L) {1068static int undumpint_l (lua_State *L) {
1062 lua_Integer res;1069 lua_Integer res;
1063 size_t len;1070 size_t len;
1064 const char *s = luaL_checklstring(L, 1, &len);1071 const char *s = luaL_checklstring(L, 1, &len);
@@ -1067,7 +1074,7 @@ static int unpackint_l (lua_State *L) { · 2 unmodified lines
1067 int endian = getendian(L, 4);1074 int endian = getendian(L, 4);
1068 luaL_argcheck(L, 1 <= pos && (size_t)pos + size - 1 <= len, 1,1075 luaL_argcheck(L, 1 <= pos && (size_t)pos + size - 1 <= len, 1,
1069 "string too short");1076 "string too short");
1070 if(unpackint(s + pos - 1, &res, endian, size))1077 if(undumpint(s + pos - 1, &res, endian, size))
1071 lua_pushinteger(L, res);1078 lua_pushinteger(L, res);
1072 else1079 else
1073 luaL_error(L, "result does not fit into a Lua integer");1080 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
1075}1082}
1076 1083
1077 1084
1078static void correctendianess (lua_State *L, char *b, int size, int endianarg) {1085static void correctendianness (lua_State *L, char *b, int size, int endianarg) {
1079 int endian = getendian(L, endianarg);1086 int endian = getendian(L, endianarg);
1080 if (endian != nativeendian.little) { /* not native endianess? */1087 if (endian != nativeendian.little) { /* not native endianness? */
1081 int i = 0;1088 int i = 0;
1082 while (i < --size) {1089 while (i < --size) {
1083 char temp = b[i];1090 char temp = b[i];
@@ -1097,7 +1104,7 @@ static int getfloatsize (lua_State *L, int arg) { · 13 unmodified lines
1097}1104}
1098 1105
1099 1106
1100static int packfloat_l (lua_State *L) {1107static int dumpfloat_l (lua_State *L) {
1101 float f; double d;1108 float f; double d;
1102 char *pn; /* pointer to number */1109 char *pn; /* pointer to number */
1103 lua_Number n = luaL_checknumber(L, 1);1110 lua_Number n = luaL_checknumber(L, 1);
@@ -1113,13 +1120,13 @@ static int packfloat_l (lua_State *L) { · 9 unmodified lines
1113 d = (double)n;1120 d = (double)n;
1114 pn = (char*)&d;1121 pn = (char*)&d;
1115 }1122 }
1116 correctendianess(L, pn, size, 3);1123 correctendianness(L, pn, size, 3);
1117 lua_pushlstring(L, pn, size);1124 lua_pushlstring(L, pn, size);
1118 return 1;1125 return 1;
1119}1126}
1120 1127
1121 1128
1122static int unpackfloat_l (lua_State *L) {1129static int undumpfloat_l (lua_State *L) {
1123 lua_Number res;1130 lua_Number res;
1124 size_t len;1131 size_t len;
1125 const char *s = luaL_checklstring(L, 1, &len);1132 const char *s = luaL_checklstring(L, 1, &len);
@@ -1129,19 +1136,19 @@ static int unpackfloat_l (lua_State *L) { · 3 unmodified lines
1129 "string too short");1136 "string too short");
1130 if (size == sizeof(lua_Number)) {1137 if (size == sizeof(lua_Number)) {
1131 memcpy(&res, s + pos - 1, size); 1138 memcpy(&res, s + pos - 1, size);
1132 correctendianess(L, (char*)&res, size, 4);1139 correctendianness(L, (char*)&res, size, 4);
1133 }1140 }
1134 else if (size == sizeof(float)) {1141 else if (size == sizeof(float)) {
1135 float f;1142 float f;
1136 memcpy(&f, s + pos - 1, size); 1143 memcpy(&f, s + pos - 1, size);
1137 correctendianess(L, (char*)&f, size, 4);1144 correctendianness(L, (char*)&f, size, 4);
1138 res = (lua_Number)f;1145 res = (lua_Number)f;
1139 } 1146 }
1140 else { /* native lua_Number may be neither float nor double */1147 else { /* native lua_Number may be neither float nor double */
1141 double d;1148 double d;
1142 lua_assert(size == sizeof(double));1149 lua_assert(size == sizeof(double));
1143 memcpy(&d, s + pos - 1, size); 1150 memcpy(&d, s + pos - 1, size);
1144 correctendianess(L, (char*)&d, size, 4);1151 correctendianness(L, (char*)&d, size, 4);
1145 res = (lua_Number)d;1152 res = (lua_Number)d;
1146 }1153 }
1147 lua_pushnumber(L, res);1154 lua_pushnumber(L, res);
@@ -1166,10 +1173,10 @@ static const luaL_Reg strlib[] = { · 18 unmodified lines
1166 {"reverse", str_reverse},1173 {"reverse", str_reverse},
1167 {"sub", str_sub},1174 {"sub", str_sub},
1168 {"upper", str_upper},1175 {"upper", str_upper},
1169 {"packfloat", packfloat_l},1176 {"dumpfloat", dumpfloat_l},
1170 {"packint", packint_l},1177 {"dumpint", dumpint_l},
1171 {"unpackfloat", unpackfloat_l},1178 {"undumpfloat", undumpfloat_l},
1172 {"unpackint", unpackint_l},1179 {"undumpint", undumpint_l},
1173 {NULL, NULL}1180 {NULL, NULL}
1174};1181};
1175 1182
src/ltable.c+27−21
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Lua tables (hash)3** Lua tables (hash)
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -21,6 +21,7 @@ · 15 unmodified lines
21#include <float.h>21#include <float.h>
22#include <math.h>22#include <math.h>
23#include <string.h>23#include <string.h>
24#include <limits.h>
24 25
25#define ltable_c26#define ltable_c
26#define LUA_CORE27#define LUA_CORE
@@ -39,14 +40,13 @@ · 12 unmodified lines
39 40
40 41
41/*42/*
42** max size of array part is 2^MAXBITS43** 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.)
43*/46*/
44#if LUAI_BITSINT >= 3247#define SIZEINT \
45#define MAXBITS 3048 (sizeof(int) < sizeof(LUA_INTEGER) ? sizeof(int) : sizeof(LUA_INTEGER))
46#else49#define MAXBITS cast_int(SIZEINT * CHAR_BIT - 2)
47#define MAXBITS (LUAI_BITSINT-2)
48#endif
49
50#define MAXASIZE (1 << MAXBITS)50#define MAXASIZE (1 << MAXBITS)
51 51
52 52
@@ -67,12 +67,6 @@ · 14 unmodified lines
67#define hashpointer(t,p) hashmod(t, IntPoint(p))67#define hashpointer(t,p) hashmod(t, IntPoint(p))
68 68
69 69
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
76#define dummynode (&dummynode_)70#define dummynode (&dummynode_)
77 71
78#define isdummy(n) ((n) == dummynode)72#define isdummy(n) ((n) == dummynode)
@@ -83,6 +77,17 @@ static const Node dummynode_ = { · 4 unmodified lines
83};77};
84 78
85 79
80/*
81** Checks whether a float has a value representable as a lua_Integer
82** (and does the conversion if so)
83*/
84static 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
86/*91/*
87** hash for floating-point numbers92** hash for floating-point numbers
88*/93*/
@@ -378,7 +383,8 @@ static void rehash (lua_State *L, Table *t, const TValue *ek) { · 289 unmodified lines
378 383
379 384
380Table *luaH_new (lua_State *L) {385Table *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);
382 t->metatable = NULL;388 t->metatable = NULL;
383 t->flags = cast_byte(~0);389 t->flags = cast_byte(~0);
384 t->array = NULL;390 t->array = NULL;
@@ -424,7 +430,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { · 39 unmodified lines
424 if (luai_numisnan(n))430 if (luai_numisnan(n))
425 luaG_runerror(L, "table index is NaN");431 luaG_runerror(L, "table index is NaN");
426 if (numisinteger(n, &k)) { /* index is int? */432 if (numisinteger(n, &k)) { /* index is int? */
427 setivalue(&aux, k); 433 setivalue(&aux, k);
428 key = &aux; /* insert it as an integer */434 key = &aux; /* insert it as an integer */
429 }435 }
430 }436 }
@@ -443,10 +449,10 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { · 12 unmodified lines
443 /* yes; move colliding node into free position */449 /* yes; move colliding node into free position */
444 while (othern + gnext(othern) != mp) /* find previous */450 while (othern + gnext(othern) != mp) /* find previous */
445 othern += gnext(othern);451 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' */
447 *f = *mp; /* copy colliding node into free pos. (mp->next also goes) */453 *f = *mp; /* copy colliding node into free pos. (mp->next also goes) */
448 if (gnext(mp) != 0) {454 if (gnext(mp) != 0) {
449 gnext(f) += mp - f; /* correct 'next' */455 gnext(f) += cast_int(mp - f); /* correct 'next' */
450 gnext(mp) = 0; /* now 'mp' is free */456 gnext(mp) = 0; /* now 'mp' is free */
451 }457 }
452 setnilvalue(gval(mp));458 setnilvalue(gval(mp));
@@ -454,9 +460,9 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { · 1 unmodified line
454 else { /* colliding node is in its own main position */460 else { /* colliding node is in its own main position */
455 /* new node will go into free position */461 /* new node will go into free position */
456 if (gnext(mp) != 0)462 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 */
458 else lua_assert(gnext(f) == 0);464 else lua_assert(gnext(f) == 0);
459 gnext(mp) = f - mp;465 gnext(mp) = cast_int(f - mp);
460 mp = f;466 mp = f;
461 }467 }
462 }468 }
@@ -472,7 +478,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { · 9 unmodified lines
472*/478*/
473const TValue *luaH_getint (Table *t, lua_Integer key) {479const TValue *luaH_getint (Table *t, lua_Integer key) {
474 /* (1 <= key && key <= t->sizearray) */480 /* (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))
476 return &t->array[key - 1];482 return &t->array[key - 1];
477 else {483 else {
478 Node *n = hashint(t, key);484 Node *n = hashint(t, key);
src/ltablib.c+27−28
@@ -1,10 +1,11 @@
1/*1/*
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 $
3** Library for Table Manipulation3** Library for Table Manipulation
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
6 6
7 7
8#include <limits.h>
8#include <stddef.h>9#include <stddef.h>
9 10
10#define ltablib_c11#define ltablib_c
@@ -39,16 +40,16 @@ static int maxn (lua_State *L) { · 28 unmodified lines
39 40
40 41
41static int tinsert (lua_State *L) {42static int tinsert (lua_State *L) {
42 int e = aux_getn(L, 1) + 1; /* first empty element */43 lua_Integer e = aux_getn(L, 1) + 1; /* first empty element */
43 int pos; /* where to insert new element */44 lua_Integer pos; /* where to insert new element */
44 switch (lua_gettop(L)) {45 switch (lua_gettop(L)) {
45 case 2: { /* called with only 2 arguments */46 case 2: { /* called with only 2 arguments */
46 pos = e; /* insert new element at the end */47 pos = e; /* insert new element at the end */
47 break;48 break;
48 }49 }
49 case 3: {50 case 3: {
50 int i;51 lua_Integer i;
51 pos = luaL_checkint(L, 2); /* 2nd argument is the position */52 pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */
52 luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds");53 luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds");
53 for (i = e; i > pos; i--) { /* move up elements */54 for (i = e; i > pos; i--) { /* move up elements */
54 lua_rawgeti(L, 1, i-1);55 lua_rawgeti(L, 1, i-1);
@@ -66,8 +67,8 @@ static int tinsert (lua_State *L) { · 11 unmodified lines
66 67
67 68
68static int tremove (lua_State *L) {69static int tremove (lua_State *L) {
69 int size = aux_getn(L, 1);70 lua_Integer size = aux_getn(L, 1);
70 int pos = luaL_optint(L, 2, size);71 lua_Integer pos = luaL_optinteger(L, 2, size);
71 if (pos != size) /* validate 'pos' if given */72 if (pos != size) /* validate 'pos' if given */
72 luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds");73 luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds");
73 lua_rawgeti(L, 1, pos); /* result = t[pos] */74 lua_rawgeti(L, 1, pos); /* result = t[pos] */
@@ -81,7 +82,7 @@ static int tremove (lua_State *L) { · 7 unmodified lines
81}82}
82 83
83 84
84static void addfield (lua_State *L, luaL_Buffer *b, int i) {85static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) {
85 lua_rawgeti(L, 1, i);86 lua_rawgeti(L, 1, i);
86 if (!lua_isstring(L, -1))87 if (!lua_isstring(L, -1))
87 luaL_error(L, "invalid value (%s) at index %d in table for "88 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
93static int tconcat (lua_State *L) {94static int tconcat (lua_State *L) {
94 luaL_Buffer b;95 luaL_Buffer b;
95 size_t lsep;96 size_t lsep;
96 int i, last;97 lua_Integer i, last;
97 const char *sep = luaL_optlstring(L, 2, "", &lsep);98 const char *sep = luaL_optlstring(L, 2, "", &lsep);
98 luaL_checktype(L, 1, LUA_TTABLE);99 luaL_checktype(L, 1, LUA_TTABLE);
99 i = luaL_optint(L, 3, 1);100 i = luaL_optinteger(L, 3, 1);
100 last = luaL_opt(L, luaL_checkint, 4, luaL_len(L, 1));101 last = luaL_opt(L, luaL_checkinteger, 4, luaL_len(L, 1));
101 luaL_buffinit(L, &b);102 luaL_buffinit(L, &b);
102 for (; i < last; i++) {103 for (; i < last; i++) {
103 addfield(L, &b, i);104 addfield(L, &b, i);
@@ -117,34 +118,32 @@ static int tconcat (lua_State *L) { · 13 unmodified lines
117*/118*/
118 119
119static int pack (lua_State *L) {120static int pack (lua_State *L) {
121 int i;
120 int n = lua_gettop(L); /* number of elements to pack */122 int n = lua_gettop(L); /* number of elements to pack */
121 lua_createtable(L, n, 1); /* create result table */123 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);
122 lua_pushinteger(L, n);127 lua_pushinteger(L, n);
123 lua_setfield(L, -2, "n"); /* t.n = number of elements */128 lua_setfield(L, 1, "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 }
132 return 1; /* return table */129 return 1; /* return table */
133}130}
134 131
135 132
136static int unpack (lua_State *L) {133static int unpack (lua_State *L) {
137 int i, e, n;134 lua_Integer i, e;
135 lua_Unsigned n;
138 luaL_checktype(L, 1, LUA_TTABLE);136 luaL_checktype(L, 1, LUA_TTABLE);
139 i = luaL_optint(L, 2, 1);137 i = luaL_optinteger(L, 2, 1);
140 e = luaL_opt(L, luaL_checkint, 3, luaL_len(L, 1));138 e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1));
141 if (i > e) return 0; /* empty range */139 if (i > e) return 0; /* empty range */
142 n = e - i + 1; /* number of elements */140 n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */
143 if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arithmetic overflow */141 if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, ++n))
144 return luaL_error(L, "too many results to unpack");142 return luaL_error(L, "too many results to unpack");
145 lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */143 do { /* must have at least one element */
146 while (i++ < e) /* push arg[i + 1...e] */144 lua_rawgeti(L, 1, i); /* push arg[i..e] */
147 lua_rawgeti(L, 1, i);145 } while (i++ < e);
146
148 return n;147 return n;
149}148}
150 149
src/ltm.c+5−16
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Tag methods3** Tag methods
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -116,10 +116,12 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, · 110 unmodified lines
116 case TM_CONCAT:116 case TM_CONCAT:
117 luaG_concaterror(L, p1, p2);117 luaG_concaterror(L, p1, p2);
118 case TM_IDIV: case TM_BAND: case TM_BOR: case TM_BXOR:118 case TM_IDIV: case TM_BAND: case TM_BOR: case TM_BXOR:
119 case TM_SHL: case TM_SHR: case TM_BNOT:119 case TM_SHL: case TM_SHR: case TM_BNOT: {
120 if (ttisnumber(p1) && ttisnumber(p2))120 lua_Number dummy;
121 if (tonumber(p1, &dummy) && tonumber(p2, &dummy))
121 luaG_tointerror(L, p1, p2);122 luaG_tointerror(L, p1, p2);
122 /* else go through */123 /* else go through */
124 }
123 default:125 default:
124 luaG_aritherror(L, p1, p2);126 luaG_aritherror(L, p1, p2);
125 }127 }
@@ -127,19 +129,6 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, · 1 unmodified line
127}129}
128 130
129 131
130const 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
143int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,132int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
144 TMS event) {133 TMS event) {
145 if (!luaT_callbinTM(L, p1, p2, L->top, event))134 if (!luaT_callbinTM(L, p1, p2, L->top, event))
src/ltm.h+1−2
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Tag methods3** Tag methods
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -67,7 +67,6 @@ LUAI_FUNC int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2, · 61 unmodified lines
67 StkId res, TMS event);67 StkId res, TMS event);
68LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,68LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
69 StkId res, TMS event);69 StkId res, TMS event);
70LUAI_FUNC const TValue *luaT_getequalTM (lua_State *L, Table *mt1, Table *mt2);
71LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1,70LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1,
72 const TValue *p2, TMS event);71 const TValue *p2, TMS event);
73 72
src/lua.c+223−153
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Lua stand-alone interpreter3** Lua stand-alone interpreter
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -103,17 +103,24 @@ static lua_State *globalL = NULL; · 97 unmodified lines
103static const char *progname = LUA_PROGNAME;103static const char *progname = LUA_PROGNAME;
104 104
105 105
106 106/*
107** Hook set by signal function to stop the interpreter.
108*/
107static void lstop (lua_State *L, lua_Debug *ar) {109static void lstop (lua_State *L, lua_Debug *ar) {
108 (void)ar; /* unused arg. */110 (void)ar; /* unused arg. */
109 lua_sethook(L, NULL, 0, 0);111 lua_sethook(L, NULL, 0, 0); /* reset hook */
110 luaL_error(L, "interrupted!");112 luaL_error(L, "interrupted!");
111}113}
112 114
113 115
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*/
114static void laction (int i) {122static void laction (int i) {
115 signal(i, SIG_DFL); /* if another SIGINT happens before lstop,123 signal(i, SIG_DFL); /* if another SIGINT happens, terminate process */
116 terminate process (default action) */
117 lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);124 lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
118}125}
119 126
@@ -139,27 +146,23 @@ static void print_usage (const char *badoption) { · 19 unmodified lines
139}146}
140 147
141 148
149/*
150** Prints an error message, adding the program name in front of it
151** (if present)
152*/
142static void l_message (const char *pname, const char *msg) {153static void l_message (const char *pname, const char *msg) {
143 if (pname) luai_writestringerror("%s: ", pname);154 if (pname) luai_writestringerror("%s: ", pname);
144 luai_writestringerror("%s\n", msg);155 luai_writestringerror("%s\n", msg);
145}156}
146 157
147 158
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*/
148static int report (lua_State *L, int status) {165static 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 */
162static void finalreport (lua_State *L, int status) {
163 if (status != LUA_OK) {166 if (status != LUA_OK) {
164 const char *msg = (lua_type(L, -1) == LUA_TSTRING) ? lua_tostring(L, -1)167 const char *msg = (lua_type(L, -1) == LUA_TSTRING) ? lua_tostring(L, -1)
165 : NULL;168 : NULL;
@@ -167,31 +170,39 @@ static void finalreport (lua_State *L, int status) { · 1 unmodified line
167 l_message(progname, msg);170 l_message(progname, msg);
168 lua_pop(L, 1);171 lua_pop(L, 1);
169 }172 }
173 return status;
170}174}
171 175
172 176
173static int traceback (lua_State *L) {177/*
178** Message handler to be used to run all chunks
179*/
180static int msghandler (lua_State *L) {
174 const char *msg = lua_tostring(L, 1);181 const char *msg = lua_tostring(L, 1);
175 if (msg)182 if (msg) /* is error object a string? */
176 luaL_traceback(L, L, msg, 1);183 luaL_traceback(L, L, msg, 1); /* use standard traceback */
177 else if (!lua_isnoneornil(L, 1)) { /* is there an error object? */184 else if (!lua_isnoneornil(L, 1)) { /* non-string error object? */
178 if (!luaL_callmeta(L, 1, "__tostring")) /* try its 'tostring' metamethod */185 if (!luaL_callmeta(L, 1, "__tostring")) /* try its 'tostring' metamethod */
179 lua_pushliteral(L, "(no error message)");186 lua_pushliteral(L, "(no error message)");
180 }187 } /* else no error object, does nothing */
181 return 1;188 return 1;
182}189}
183 190
184 191
192/*
193** Interface to 'lua_pcall', which sets appropriate message function
194** and C-signal handler. Used to run all chunks.
195*/
185static int docall (lua_State *L, int narg, int nres) {196static int docall (lua_State *L, int narg, int nres) {
186 int status;197 int status;
187 int base = lua_gettop(L) - narg; /* function index */198 int base = lua_gettop(L) - narg; /* function index */
188 lua_pushcfunction(L, traceback); /* push traceback function */199 lua_pushcfunction(L, msghandler); /* push message handler */
189 lua_insert(L, base); /* put it under chunk and args */200 lua_insert(L, base); /* put it under function and args */
190 globalL = L; /* to be available to 'laction' */201 globalL = L; /* to be available to 'laction' */
191 signal(SIGINT, laction);202 signal(SIGINT, laction); /* set C-signal handler */
192 status = lua_pcall(L, narg, nres, base);203 status = lua_pcall(L, narg, nres, base);
193 signal(SIGINT, SIG_DFL);204 signal(SIGINT, SIG_DFL); /* reset C-signal handler */
194 lua_remove(L, base); /* remove traceback function */205 lua_remove(L, base); /* remove message handler from the stack */
195 return status;206 return status;
196}207}
197 208
@@ -202,38 +213,47 @@ static void print_version (void) { · 4 unmodified lines
202}213}
203 214
204 215
205static int getargs (lua_State *L, char **argv, int n) {216/*
206 int narg;217** Create the 'arg' table, which stores all arguments from the
207 int i;218** command line ('argv'). It should be aligned so that, at index 0,
208 int argc = 0;219** it has 'argv[script]', which is the script name. The arguments
209 while (argv[argc]) argc++; /* count total number of arguments */220** to the script (everything after 'script') go to positive indices;
210 narg = argc - (n + 1); /* number of arguments to the script */221** other arguments (before the script name) go to negative indices.
211 luaL_checkstack(L, narg + 3, "too many arguments to script");222** If there is no script name, assume interpreter's name as base.
212 for (i=n+1; i < argc; i++)223*/
213 lua_pushstring(L, argv[i]);224static void createargtable (lua_State *L, char **argv, int argc, int script) {
214 lua_createtable(L, narg, n + 1);225 int i, narg;
215 for (i=0; i < argc; i++) {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++) {
216 lua_pushstring(L, argv[i]);230 lua_pushstring(L, argv[i]);
217 lua_rawseti(L, -2, i - n);231 lua_rawseti(L, -2, i - script);
218 }232 }
219 return narg;233 lua_setglobal(L, "arg");
220}234}
221 235
222 236
223static int dofile (lua_State *L, const char *name) {237static int dochunk (lua_State *L, int status) {
224 int status = luaL_loadfile(L, name);
225 if (status == LUA_OK) status = docall(L, 0, 0);238 if (status == LUA_OK) status = docall(L, 0, 0);
226 return report(L, status);239 return report(L, status);
227}240}
228 241
229 242
243static int dofile (lua_State *L, const char *name) {
244 return dochunk(L, luaL_loadfile(L, name));
245}
246
247
230static int dostring (lua_State *L, const char *s, const char *name) {248static int dostring (lua_State *L, const char *s, const char *name) {
231 int status = luaL_loadbuffer(L, s, strlen(s), name);249 return dochunk(L, luaL_loadbuffer(L, s, strlen(s), name));
232 if (status == LUA_OK) status = docall(L, 0, 0);
233 return report(L, status);
234}250}
235 251
236 252
253/*
254** Calls 'require(name)' and stores the result in a global variable
255** with the given name.
256*/
237static int dolibrary (lua_State *L, const char *name) {257static int dolibrary (lua_State *L, const char *name) {
238 int status;258 int status;
239 lua_getglobal(L, "require");259 lua_getglobal(L, "require");
@@ -245,6 +265,9 @@ static int dolibrary (lua_State *L, const char *name) { · 5 unmodified lines
245}265}
246 266
247 267
268/*
269** Returns the string to be used as a prompt by the interpreter.
270*/
248static const char *get_prompt (lua_State *L, int firstline) {271static const char *get_prompt (lua_State *L, int firstline) {
249 const char *p;272 const char *p;
250 lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2");273 lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2");
@@ -257,6 +280,12 @@ static const char *get_prompt (lua_State *L, int firstline) { · 6 unmodified lines
257#define EOFMARK "<eof>"280#define EOFMARK "<eof>"
258#define marklen (sizeof(EOFMARK)/sizeof(char) - 1)281#define marklen (sizeof(EOFMARK)/sizeof(char) - 1)
259 282
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*/
260static int incomplete (lua_State *L, int status) {289static int incomplete (lua_State *L, int status) {
261 if (status == LUA_ERRSYNTAX) {290 if (status == LUA_ERRSYNTAX) {
262 size_t lmsg;291 size_t lmsg;
@@ -270,7 +299,9 @@ static int incomplete (lua_State *L, int status) { · 7 unmodified lines
270}299}
271 300
272 301
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*/
274static int pushline (lua_State *L, int firstline) {305static int pushline (lua_State *L, int firstline) {
275 char buffer[LUA_MAXINPUT];306 char buffer[LUA_MAXINPUT];
276 char *b = buffer;307 char *b = buffer;
@@ -292,8 +323,10 @@ static int pushline (lua_State *L, int firstline) { · 15 unmodified lines
292}323}
293 324
294 325
295/* try to compile line on the stack as 'return <line>'; on return, stack326/*
296 has either compiled chunk or original line (if compilation failed) */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*/
297static int addreturn (lua_State *L) {330static int addreturn (lua_State *L) {
298 int status;331 int status;
299 size_t len; const char *line;332 size_t len; const char *line;
@@ -309,14 +342,16 @@ static int addreturn (lua_State *L) { · 9 unmodified lines
309}342}
310 343
311 344
312/* read multiple lines until a complete line */345/*
346** Read multiple lines until a complete Lua statement
347*/
313static int multiline (lua_State *L) {348static int multiline (lua_State *L) {
314 for (;;) { /* repeat until gets a complete line */349 for (;;) { /* repeat until gets a complete statement */
315 size_t len;350 size_t len;
316 const char *line = lua_tolstring(L, 1, &len); /* get what it has */351 const char *line = lua_tolstring(L, 1, &len); /* get what it has */
317 int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */352 int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */
318 if (!incomplete(L, status) || !pushline(L, 0))353 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 */
320 lua_pushliteral(L, "\n"); /* add newline... */355 lua_pushliteral(L, "\n"); /* add newline... */
321 lua_insert(L, -2); /* ...between the two lines */356 lua_insert(L, -2); /* ...between the two lines */
322 lua_concat(L, 3); /* join them */357 lua_concat(L, 3); /* join them */
@@ -324,6 +359,12 @@ static int multiline (lua_State *L) { · 1 unmodified line
324}359}
325 360
326 361
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*/
327static int loadline (lua_State *L) {368static int loadline (lua_State *L) {
328 int status;369 int status;
329 lua_settop(L, 0);370 lua_settop(L, 0);
@@ -331,29 +372,43 @@ static int loadline (lua_State *L) { · 1 unmodified line
331 return -1; /* no input */372 return -1; /* no input */
332 if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */373 if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */
333 status = multiline(L); /* try as command, maybe with continuation lines */374 status = multiline(L); /* try as command, maybe with continuation lines */
334 lua_saveline(L, 1);375 lua_saveline(L, 1); /* keep history */
335 lua_remove(L, 1); /* remove line */376 lua_remove(L, 1); /* remove line from the stack */
336 lua_assert(lua_gettop(L) == 1);377 lua_assert(lua_gettop(L) == 1);
337 return status;378 return status;
338}379}
339 380
340 381
341static void dotty (lua_State *L) {382/*
383** Prints (calling the Lua 'print' function) any values on the stack
384*/
385static 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*/
403static void doREPL (lua_State *L) {
342 int status;404 int status;
343 const char *oldprogname = progname;405 const char *oldprogname = progname;
344 progname = NULL;406 progname = NULL; /* no 'progname' on errors in interactive mode */
345 while ((status = loadline(L)) != -1) {407 while ((status = loadline(L)) != -1) {
346 if (status == LUA_OK) status = docall(L, 0, LUA_MULTRET);408 if (status == LUA_OK)
347 report(L, status);409 status = docall(L, 0, LUA_MULTRET);
348 if (status == LUA_OK && lua_gettop(L) > 0) { /* any result to print? */410 if (status == LUA_OK) l_print(L);
349 luaL_checkstack(L, LUA_MINSTACK, "too many results to print");411 else report(L, status);
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 }
357 }412 }
358 lua_settop(L, 0); /* clear stack */413 lua_settop(L, 0); /* clear stack */
359 luai_writeline();414 luai_writeline();
@@ -361,97 +416,109 @@ static void dotty (lua_State *L) { · 1 unmodified line
361}416}
362 417
363 418
419/*
420** Push on the stack 'n' strings from 'argv'
421*/
422static 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
364static int handle_script (lua_State *L, char **argv, int n) {430static int handle_script (lua_State *L, char **argv, int n) {
365 int status;431 int status;
366 const char *fname;432 const char *fname = argv[0];
367 int narg = getargs(L, argv, n); /* collect arguments */433 if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0)
368 lua_setglobal(L, "arg");
369 fname = argv[n];
370 if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0)
371 fname = NULL; /* stdin */434 fname = NULL; /* stdin */
372 status = luaL_loadfile(L, fname);435 status = luaL_loadfile(L, fname);
373 lua_insert(L, -(narg+1));436 if (status == LUA_OK) {
374 if (status == LUA_OK)437 pushargs(L, argv, n); /* push arguments to script */
375 status = docall(L, narg, LUA_MULTRET);438 status = docall(L, n - 1, LUA_MULTRET);
376 else439 }
377 lua_pop(L, narg);
378 return report(L, status);440 return report(L, status);
379}441}
380 442
381 443
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_*' */
393 444
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 */
394 451
395static 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*/
458static int collectargs (char **argv, int *first) {
459 int args = 0;
396 int i;460 int i;
397 for (i = 1; argv[i] != NULL; i++) {461 for (i = 1; argv[i] != NULL; i++) {
462 *first = i;
398 if (argv[i][0] != '-') /* not an option? */463 if (argv[i][0] != '-') /* not an option? */
399 return i;464 return args; /* stop handling options */
400 switch (argv[i][1]) { /* option */465 switch (argv[i][1]) { /* else check option */
401 case '-':466 case '-': /* '--' */
402 noextrachars(argv[i]);467 if (argv[i][2] != '\0') /* extra characters after '--'? */
403 return (argv[i+1] != NULL ? i+1 : 0);468 return has_error; /* invalid option */
404 case '\0':469 *first = i + 1;
405 return i;470 return args;
471 case '\0': /* '-' */
472 return args; /* script "name" is '-' */
406 case 'E':473 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;
408 break;477 break;
409 case 'i':478 case 'i':
410 noextrachars(argv[i]);479 args |= has_i; /* goes through (-i implies -v) */
411 args[has_i] = 1; /* go through */
412 case 'v':480 case 'v':
413 noextrachars(argv[i]);481 if (argv[i][2] != '\0') /* extra characters after 1st? */
414 args[has_v] = 1;482 return has_error; /* invalid option */
483 args |= has_v;
415 break;484 break;
416 case 'e':485 case 'e':
417 args[has_e] = 1; /* go through */486 args |= has_e; /* go through */
418 case 'l': /* both options need an argument */487 case 'l': /* both options need an argument */
419 if (argv[i][2] == '\0') { /* no concatenated argument? */488 if (argv[i][2] == '\0') { /* no concatenated argument? */
420 i++; /* try next 'argv' */489 i++; /* try next 'argv' */
421 if (argv[i] == NULL || argv[i][0] == '-')490 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 */
423 }492 }
424 break;493 break;
425 default: /* invalid option; return its index... */494 default: /* invalid option */
426 return -i; /* ...as a negative value */495 return has_error;
427 }496 }
428 }497 }
429 return 0;498 *first = i; /* no script name */
499 return args;
430}500}
431 501
432 502
503/*
504** Processes options 'e' and 'l', which involve running Lua code.
505** Returns 0 if some code raises an error.
506*/
433static int runargs (lua_State *L, char **argv, int n) {507static int runargs (lua_State *L, char **argv, int n) {
434 int i;508 int i;
435 for (i = 1; i < n; i++) {509 for (i = 1; i < n; i++) {
436 lua_assert(argv[i][0] == '-');510 int status;
437 switch (argv[i][1]) { /* option */511 int option = argv[i][1];
438 case 'e': {512 lua_assert(argv[i][0] == '-'); /* already checked */
439 const char *chunk = argv[i] + 2;513 if (option == 'e' || option == 'l') {
440 if (*chunk == '\0') chunk = argv[++i];514 const char *extra = argv[i] + 2; /* both options need an argument */
441 lua_assert(chunk != NULL);515 if (*extra == '\0') extra = argv[++i];
442 if (dostring(L, chunk, "=(command line)") != LUA_OK)516 lua_assert(extra != NULL);
443 return 0;517 if (option == 'e')
444 break;518 status = dostring(L, extra, "=(command line)");
445 }519 else
446 case 'l': {520 status = dolibrary(L, extra);
447 const char *filename = argv[i] + 2;521 if (status != LUA_OK) return 0;
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;
455 }522 }
456 }523 }
457 return 1;524 return 1;
@@ -473,40 +540,44 @@ static int handle_luainit (lua_State *L) { · 15 unmodified lines
473}540}
474 541
475 542
543/*
544** Main body of stand-alone interpreter (to be called in protected mode).
545** Reads the options and handles them all.
546*/
476static int pmain (lua_State *L) {547static int pmain (lua_State *L) {
477 int argc = (int)lua_tointeger(L, 1);548 int argc = (int)lua_tointeger(L, 1);
478 char **argv = (char **)lua_touserdata(L, 2);549 char **argv = (char **)lua_touserdata(L, 2);
479 int script;550 int script;
480 int args[num_has];551 int args = collectargs(argv, &script);
481 args[has_i] = args[has_v] = args[has_e] = args[has_E] = 0;552 luaL_checkversion(L); /* check that interpreter has correct version */
482 if (argv[0] && argv[0][0]) progname = argv[0];553 if (argv[0] && argv[0][0]) progname = argv[0];
483 script = collectargs(argv, args);554 if (args == has_error) { /* bad arg? */
484 if (script < 0) { /* invalid arg? */555 print_usage(argv[script]); /* 'script' has index of bad arg. */
485 print_usage(argv[-script]);
486 return 0;556 return 0;
487 }557 }
488 if (args[has_v]) print_version();558 if (args & has_v) /* option '-v'? */
489 if (args[has_E]) { /* option '-E'? */559 print_version();
560 if (args & has_E) { /* option '-E'? */
490 lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */561 lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */
491 lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");562 lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
492 }563 }
493 /* open standard libraries */564 luaL_openlibs(L); /* open standard libraries */
494 luaL_checkversion(L);565 createargtable(L, argv, argc, script); /* create table 'arg' */
495 lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */566 if (!(args & has_E)) { /* no option '-E'? */
496 luaL_openlibs(L); /* open libraries */567 if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */
497 lua_gc(L, LUA_GCRESTART, 0);568 return 0; /* error running LUA_INIT */
498 if (!args[has_E] && handle_luainit(L) != LUA_OK)569 }
499 return 0; /* error running LUA_INIT */570 if (!runargs(L, argv, script)) /* execute arguments -e and -l */
500 /* execute arguments -e and -l */571 return 0; /* something failed */
501 if (!runargs(L, argv, (script > 0) ? script : argc)) return 0;572 if (script < argc && /* execute main script (if there is one) */
502 /* execute main script (if there is one) */573 handle_script(L, argv + script, argc - script) != LUA_OK)
503 if (script && handle_script(L, argv, script) != LUA_OK) return 0;574 return 0;
504 if (args[has_i]) /* -i option? */575 if (args & has_i) /* -i option? */
505 dotty(L);576 doREPL(L); /* do read-eval-print loop */
506 else if (script == 0 && !args[has_e] && !args[has_v]) { /* no arguments? */577 else if (script == argc && !(args & (has_e | has_v))) { /* no arguments? */
507 if (lua_stdin_is_tty()) {578 if (lua_stdin_is_tty()) { /* running in interactive mode? */
508 print_version();579 print_version();
509 dotty(L);580 doREPL(L); /* do read-eval-print loop */
510 }581 }
511 else dofile(L, NULL); /* executes stdin as a file */582 else dofile(L, NULL); /* executes stdin as a file */
512 }583 }
@@ -522,13 +593,12 @@ int main (int argc, char **argv) { · 9 unmodified lines
522 l_message(argv[0], "cannot create state: not enough memory");593 l_message(argv[0], "cannot create state: not enough memory");
523 return EXIT_FAILURE;594 return EXIT_FAILURE;
524 }595 }
525 /* call 'pmain' in protected mode */596 lua_pushcfunction(L, &pmain); /* to call 'pmain' in protected mode */
526 lua_pushcfunction(L, &pmain);
527 lua_pushinteger(L, argc); /* 1st argument */597 lua_pushinteger(L, argc); /* 1st argument */
528 lua_pushlightuserdata(L, argv); /* 2nd argument */598 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 */
530 result = lua_toboolean(L, -1); /* get result */600 result = lua_toboolean(L, -1); /* get result */
531 finalreport(L, status);601 report(L, status);
532 lua_close(L);602 lua_close(L);
533 return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE;603 return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE;
534}604}
src/lua.h+25−14
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Lua - A Scripting Language3** Lua - A Scripting Language
4** Lua.org, PUC-Rio, Brazil (http://www.lua.org)4** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
5** See Copyright Notice at the end of this file5** See Copyright Notice at the end of this file
@@ -19,7 +19,7 @@ · 13 unmodified lines
19#define LUA_VERSION_MAJOR "5"19#define LUA_VERSION_MAJOR "5"
20#define LUA_VERSION_MINOR "3"20#define LUA_VERSION_MINOR "3"
21#define LUA_VERSION_NUM 50321#define LUA_VERSION_NUM 503
22#define LUA_VERSION_RELEASE "0 (work2)"22#define LUA_VERSION_RELEASE "0 (work3)"
23 23
24#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR24#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
25#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE25#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE
@@ -53,8 +53,16 @@ · 27 unmodified lines
53 53
54typedef struct lua_State lua_State;54typedef struct lua_State lua_State;
55 55
56/*
57** Type for C functions registered with Lua
58*/
56typedef int (*lua_CFunction) (lua_State *L);59typedef int (*lua_CFunction) (lua_State *L);
57 60
61/*
62** Type for continuation functions
63*/
64typedef int (*lua_KFunction) (lua_State *L, int status, int ctx);
65
58 66
59/*67/*
60** functions that read/write blocks when loading/dumping Lua chunks68** 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
145LUA_API int (lua_gettop) (lua_State *L);153LUA_API int (lua_gettop) (lua_State *L);
146LUA_API void (lua_settop) (lua_State *L, int idx);154LUA_API void (lua_settop) (lua_State *L, int idx);
147LUA_API void (lua_pushvalue) (lua_State *L, int idx);155LUA_API void (lua_pushvalue) (lua_State *L, int idx);
148LUA_API void (lua_remove) (lua_State *L, int idx);156LUA_API void (lua_rotate) (lua_State *L, int idx, int n);
149LUA_API void (lua_insert) (lua_State *L, int idx);
150LUA_API void (lua_replace) (lua_State *L, int idx);157LUA_API void (lua_replace) (lua_State *L, int idx);
151LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx);158LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx);
152LUA_API int (lua_checkstack) (lua_State *L, int sz);159LUA_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
238LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec);245LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec);
239LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz);246LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz);
240LUA_API int (lua_getmetatable) (lua_State *L, int objindex);247LUA_API int (lua_getmetatable) (lua_State *L, int objindex);
241LUA_API void (lua_getuservalue) (lua_State *L, int idx);248LUA_API int (lua_getuservalue) (lua_State *L, int idx);
242 249
243 250
244/*251/*
@@ -258,13 +265,11 @@ LUA_API void (lua_setuservalue) (lua_State *L, int idx); · 13 unmodified lines
258** 'load' and 'call' functions (load and run Lua code)265** 'load' and 'call' functions (load and run Lua code)
259*/266*/
260LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults, int ctx,267LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults, int ctx,
261 lua_CFunction k);268 lua_KFunction k);
262#define lua_call(L,n,r) lua_callk(L, (n), (r), 0, NULL)269#define lua_call(L,n,r) lua_callk(L, (n), (r), 0, NULL)
263 270
264LUA_API int (lua_getctx) (lua_State *L, int *ctx);
265
266LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,271LUA_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);
268#define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL)273#define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL)
269 274
270LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt,275LUA_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
278** coroutine functions283** coroutine functions
279*/284*/
280LUA_API int (lua_yieldk) (lua_State *L, int nresults, int ctx,285LUA_API int (lua_yieldk) (lua_State *L, int nresults, int ctx,
281 lua_CFunction k);286 lua_KFunction k);
282#define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL)287#define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL)
283LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg);288LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg);
284LUA_API int (lua_status) (lua_State *L);289LUA_API int (lua_status) (lua_State *L);
290LUA_API int lua_isyieldable (lua_State *L);
291
285 292
286/*293/*
287** garbage-collection function and options294** garbage-collection function and options
@@ -311,7 +318,7 @@ LUA_API int (lua_next) (lua_State *L, int idx); · 23 unmodified lines
311LUA_API void (lua_concat) (lua_State *L, int n);318LUA_API void (lua_concat) (lua_State *L, int n);
312LUA_API void (lua_len) (lua_State *L, int idx);319LUA_API void (lua_len) (lua_State *L, int idx);
313 320
314LUA_API int (lua_strtonum) (lua_State *L, const char *s, size_t len);321LUA_API size_t (lua_strtonum) (lua_State *L, const char *s);
315 322
316LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud);323LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud);
317LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);324LUA_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
324** ===============================================================331** ===============================================================
325*/332*/
326 333
327#define lua_tonumber(L,i) lua_tonumberx(L,i,NULL)334#define lua_tonumber(L,i) lua_tonumberx(L,(i),NULL)
328#define lua_tointeger(L,i) lua_tointegerx(L,i,NULL)335#define lua_tointeger(L,i) lua_tointegerx(L,(i),NULL)
329#define lua_tounsigned(L,i) lua_tounsignedx(L,i,NULL)336#define lua_tounsigned(L,i) lua_tounsignedx(L,(i),NULL)
330 337
331#define lua_pop(L,n) lua_settop(L, -(n)-1)338#define lua_pop(L,n) lua_settop(L, -(n)-1)
332 339
@@ -354,6 +361,10 @@ LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); · 21 unmodified lines
354#define lua_tostring(L,i) lua_tolstring(L, (i), NULL)361#define lua_tostring(L,i) lua_tolstring(L, (i), NULL)
355 362
356 363
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
357 368
358/*369/*
359** {======================================================================370** {======================================================================
src/luac.c+15−8
@@ -202,10 +202,8 @@ int main(int argc, char* argv[]) · 201 unmodified lines
202 return EXIT_SUCCESS;202 return EXIT_SUCCESS;
203}203}
204 204
205#define nvalue(x) ((lua_Number)0)
206#define ttypenv(x) ttnov(x)
207/*205/*
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 $
209** print bytecodes207** print bytecodes
210** See Copyright Notice in lua.h208** See Copyright Notice in lua.h
211*/209*/
@@ -253,7 +251,7 @@ static void PrintString(const TString* ts) · 41 unmodified lines
253static void PrintConstant(const Proto* f, int i)251static void PrintConstant(const Proto* f, int i)
254{252{
255 const TValue* o=&f->k[i];253 const TValue* o=&f->k[i];
256 switch (ttypenv(o))254 switch (ttype(o))
257 {255 {
258 case LUA_TNIL:256 case LUA_TNIL:
259 printf("nil");257 printf("nil");
@@ -261,10 +259,13 @@ static void PrintConstant(const Proto* f, int i) · 1 unmodified line
261 case LUA_TBOOLEAN:259 case LUA_TBOOLEAN:
262 printf(bvalue(o) ? "true" : "false");260 printf(bvalue(o) ? "true" : "false");
263 break;261 break;
264 case LUA_TNUMBER:262 case LUA_TNUMFLT:
265 printf(LUA_NUMBER_FMT,nvalue(o));263 printf(LUA_NUMBER_FMT,fltvalue(o));
266 break;264 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:
268 PrintString(rawtsvalue(o));269 PrintString(rawtsvalue(o));
269 break;270 break;
270 default: /* cannot happen */271 default: /* cannot happen */
@@ -339,8 +340,14 @@ static void PrintCode(const Proto* f) · 68 unmodified lines
339 case OP_ADD:340 case OP_ADD:
340 case OP_SUB:341 case OP_SUB:
341 case OP_MUL:342 case OP_MUL:
342 case OP_DIV:
343 case OP_POW:343 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:
344 case OP_EQ:351 case OP_EQ:
345 case OP_LT:352 case OP_LT:
346 case OP_LE:353 case OP_LE:
src/luaconf.h+131−41
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Configuration file for Lua3** Configuration file for Lua
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -21,16 +21,15 @@ · 15 unmodified lines
21 21
22/*22/*
23** ===================================================================23** ===================================================================
24@@ LUA_INT_INT / LUA_INT_LONG / LUA_INT_LONGLONG defines size for24@@ LUA_INT_INT / LUA_INT_LONG / LUA_INT_LONGLONG defines type for
25@* Lua integers;25@@ Lua integers;
26@@ LUA_REAL_FLOAT / LUA_REAL_DOUBLE / LUA_REAL_LONGDOUBLE defines size for26@@ LUA_REAL_FLOAT / LUA_REAL_DOUBLE / LUA_REAL_LONGDOUBLE defines
27@* Lua floats.27@@ type for Lua floats.
28**28**
29** These definitions set the numeric types for Lua. Lua should work29** 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-bit30** fine with any mix of these previous options.
31** floats. The usual configurations are 64-bit integers and floats (the31** The usual configurations are 64-bit integers and floats (the default)
32** default) and 32-bit integers and floats (Small Lua, for restricted32** and 32-bit integers and floats (Small Lua, for restricted hardware).
33** hardware).
34** =====================================================================33** =====================================================================
35*/34*/
36#define LUA_INT_LONGLONG35#define LUA_INT_LONGLONG
@@ -74,7 +73,7 @@ · 37 unmodified lines
74 73
75 74
76/*75/*
77@@ LUA_USE_C99 includes all functionality from C 99.76@@ LUA_USE_C99 includes all functionality that depends on C 99.
78** CHANGE it (define it) if your system is compatible.77** CHANGE it (define it) if your system is compatible.
79*/78*/
80#if defined(LUA_USE_C99)79#if defined(LUA_USE_C99)
@@ -84,7 +83,7 @@ · 3 unmodified lines
84 83
85/*84/*
86@@ LUA_USE_POSIX includes all functionality listed as X/Open System85@@ LUA_USE_POSIX includes all functionality listed as X/Open System
87@* Interfaces Extension (XSI).86@@ Interfaces Extension (XSI).
88** CHANGE it (define it) if your system is XSI compatible.87** CHANGE it (define it) if your system is XSI compatible.
89*/88*/
90#if defined(LUA_USE_POSIX)89#if defined(LUA_USE_POSIX)
@@ -94,33 +93,37 @@ · 3 unmodified lines
94 93
95/*94/*
96@@ LUA_PATH_DEFAULT is the default path that Lua uses to look for95@@ LUA_PATH_DEFAULT is the default path that Lua uses to look for
97@* Lua libraries.96@@ Lua libraries.
98@@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for97@@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for
99@* C libraries.98@@ C libraries.
100** CHANGE them if your machine has a non-conventional directory99** CHANGE them if your machine has a non-conventional directory
101** hierarchy or if you want to install your libraries in100** hierarchy or if you want to install your libraries in
102** non-conventional directories.101** non-conventional directories.
103*/102*/
104#if defined(_WIN32) /* { */103#define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
104#if defined(_WIN32) /* { */
105/*105/*
106** In Windows, any exclamation mark ('!') in the path is replaced by the106** In Windows, any exclamation mark ('!') in the path is replaced by the
107** path of the directory of the executable file of the current process.107** path of the directory of the executable file of the current process.
108*/108*/
109#define LUA_LDIR "!\\lua\\"109#define LUA_LDIR "!\\lua\\"
110#define LUA_CDIR "!\\"110#define LUA_CDIR "!\\"
111#define LUA_SHRDIR "!\\..\\share\\lua\\" LUA_VDIR "\\"
111#define LUA_PATH_DEFAULT \112#define LUA_PATH_DEFAULT \
112 LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \113 LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \
113 LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" \114 LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" \
115 LUA_SHRDIR"?.lua;" LUA_SHRDIR"?\\init.lua;" \
114 ".\\?.lua;" ".\\?\\init.lua"116 ".\\?.lua;" ".\\?\\init.lua"
115#define LUA_CPATH_DEFAULT \117#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"
117 121
118#else /* }{ */122#else /* }{ */
119 123
120#define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR "/"
121#define LUA_ROOT "/usr/local/"124#define LUA_ROOT "/usr/local/"
122#define LUA_LDIR LUA_ROOT "share/lua/" LUA_VDIR125#define LUA_LDIR LUA_ROOT "share/lua/" LUA_VDIR "/"
123#define LUA_CDIR LUA_ROOT "lib/lua/" LUA_VDIR126#define LUA_CDIR LUA_ROOT "lib/lua/" LUA_VDIR "/"
124#define LUA_PATH_DEFAULT \127#define LUA_PATH_DEFAULT \
125 LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \128 LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \
126 LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua;" \129 LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua;" \
@@ -181,10 +184,10 @@ · 54 unmodified lines
181 184
182/*185/*
183@@ LUAI_FUNC is a mark for all extern functions that are not to be186@@ LUAI_FUNC is a mark for all extern functions that are not to be
184@* exported to outside modules.187@@ exported to outside modules.
185@@ LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables188@@ LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables
186@* that are not to be exported to outside modules (LUAI_DDEF for189@@ that are not to be exported to outside modules (LUAI_DDEF for
187@* definitions and LUAI_DDEC for declarations).190@@ definitions and LUAI_DDEC for declarations).
188** CHANGE them if you need to mark them in some special way. Elf/gcc191** CHANGE them if you need to mark them in some special way. Elf/gcc
189** (versions 3.2 and later) mark them as "hidden" to optimize access192** (versions 3.2 and later) mark them as "hidden" to optimize access
190** when Lua is compiled as a shared library. Not all elf targets support193** when Lua is compiled as a shared library. Not all elf targets support
@@ -217,7 +220,7 @@ · 26 unmodified lines
217 220
218/*221/*
219@@ LUA_IDSIZE gives the maximum size for the description of the source222@@ 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.
221** CHANGE it if you want a different size.224** CHANGE it if you want a different size.
222*/225*/
223#define LUA_IDSIZE 60226#define LUA_IDSIZE 60
@@ -259,17 +262,38 @@ · 35 unmodified lines
259*/262*/
260 263
261/*264/*
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.
263** You can define it to get all options, or change specific options267** You can define it to get all options, or change specific options
264** to fit your specific needs.268** to fit your specific needs.
265*/269*/
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
267 277
268/*278/*
269@@ LUA_COMPAT_BITLIB controls the presence of library 'bit32'.279@@ LUA_COMPAT_BITLIB controls the presence of library 'bit32'.
270*/280*/
271#define LUA_COMPAT_BITLIB281#define LUA_COMPAT_BITLIB
272 282
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
273/*297/*
274@@ LUA_COMPAT_UNPACK controls the presence of global 'unpack'.298@@ LUA_COMPAT_UNPACK controls the presence of global 'unpack'.
275** You can replace it with 'table.unpack'.299** You can replace it with 'table.unpack'.
@@ -352,9 +376,9 @@ · 76 unmodified lines
352/*376/*
353@@ LUA_INT32 is an signed integer with exactly 32 bits.377@@ LUA_INT32 is an signed integer with exactly 32 bits.
354@@ LUAI_UMEM is an unsigned integer big enough to count the total378@@ LUAI_UMEM is an unsigned integer big enough to count the total
355@* memory used by Lua.379@@ memory used by Lua.
356@@ LUAI_MEM is a signed integer big enough to count the total memory380@@ LUAI_MEM is a signed integer big enough to count the total memory
357@* used by Lua.381@@ used by Lua.
358** CHANGE here if for some weird reason the default definitions are not382** CHANGE here if for some weird reason the default definitions are not
359** good enough for your machine. Probably you do not need to change383** good enough for your machine. Probably you do not need to change
360** this.384** this.
@@ -408,10 +432,9 @@ · 47 unmodified lines
408@@ LUA_NUMBER is the floating-point type used by Lua.432@@ LUA_NUMBER is the floating-point type used by Lua.
409**433**
410@@ LUAI_UACNUMBER is the result of an 'usual argument conversion'434@@ LUAI_UACNUMBER is the result of an 'usual argument conversion'
411@* over a floating number.435@@ over a floating number.
412**436**
413@@ LUA_NUMBER_FRMLEN is the length modifier for writing floats.437@@ LUA_NUMBER_FRMLEN is the length modifier for writing floats.
414@@ LUA_NUMBER_SCAN is the format for reading floats.
415@@ LUA_NUMBER_FMT is the format for writing floats.438@@ LUA_NUMBER_FMT is the format for writing floats.
416@@ lua_number2str converts a float to a string.439@@ lua_number2str converts a float to a string.
417**440**
@@ -427,7 +450,6 @@ · 9 unmodified lines
427#define LUAI_UACNUMBER double450#define LUAI_UACNUMBER double
428 451
429#define LUA_NUMBER_FRMLEN ""452#define LUA_NUMBER_FRMLEN ""
430#define LUA_NUMBER_SCAN "%f"
431#define LUA_NUMBER_FMT "%.7g"453#define LUA_NUMBER_FMT "%.7g"
432 454
433#define l_mathop(op) op##f455#define l_mathop(op) op##f
@@ -442,7 +464,6 @@ · 8 unmodified lines
442#define LUAI_UACNUMBER long double464#define LUAI_UACNUMBER long double
443 465
444#define LUA_NUMBER_FRMLEN "L"466#define LUA_NUMBER_FRMLEN "L"
445#define LUA_NUMBER_SCAN "%Lf"
446#define LUA_NUMBER_FMT "%.19Lg"467#define LUA_NUMBER_FMT "%.19Lg"
447 468
448#define l_mathop(op) op##l469#define l_mathop(op) op##l
@@ -456,7 +477,6 @@ · 7 unmodified lines
456#define LUAI_UACNUMBER double477#define LUAI_UACNUMBER double
457 478
458#define LUA_NUMBER_FRMLEN ""479#define LUA_NUMBER_FRMLEN ""
459#define LUA_NUMBER_SCAN "%lf"
460#define LUA_NUMBER_FMT "%.14g"480#define LUA_NUMBER_FMT "%.14g"
461 481
462#define l_mathop(op) op482#define l_mathop(op) op
@@ -489,15 +509,30 @@ · 26 unmodified lines
489#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))509#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
490 510
491 511
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
492/*526/*
493@@ The luai_num* macros define the primitive operations over numbers.527@@ 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.
495*/529*/
496 530
497/* the following operations need the math library */531/* the following operations need the math library */
498#if defined(lobject_c) || defined(lvm_c)532#if defined(lobject_c) || defined(lvm_c)
499#include <math.h>533#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); }
501#define luai_numpow(L,a,b) ((void)L, l_mathop(pow)(a,b))536#define luai_numpow(L,a,b) ((void)L, l_mathop(pow)(a,b))
502#endif537#endif
503 538
@@ -528,44 +563,99 @@ · 24 unmodified lines
528**563**
529@@ LUA_UNSIGNED is the unsigned version of LUA_INTEGER.564@@ LUA_UNSIGNED is the unsigned version of LUA_INTEGER.
530**565**
566@@ LUAI_UACINT is the result of an 'usual argument conversion'
567@@ over a lUA_INTEGER.
531@@ LUA_INTEGER_FRMLEN is the length modifier for reading/writing integers.568@@ LUA_INTEGER_FRMLEN is the length modifier for reading/writing integers.
532@@ LUA_INTEGER_SCAN is the format for reading integers.
533@@ LUA_INTEGER_FMT is the format for writing integers.569@@ 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.
534@@ lua_integer2str converts an integer to a string.573@@ lua_integer2str converts an integer to a string.
535*/574*/
536 575
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
537#if defined(LUA_INT_INT) /* { int */593#if defined(LUA_INT_INT) /* { int */
538 594
539#define LUA_INTEGER int595#define LUA_INTEGER int
540#define LUA_INTEGER_FRMLEN ""596#define LUA_INTEGER_FRMLEN ""
541 597
598#define LUA_MAXUNSIGNED UINT_MAX
599#define LUA_MAXINTEGER INT_MAX
600#define LUA_MININTEGER INT_MIN
601
542#elif defined(LUA_INT_LONG) /* }{ long */602#elif defined(LUA_INT_LONG) /* }{ long */
543 603
544#define LUA_INTEGER long604#define LUA_INTEGER long
545#define LUA_INTEGER_FRMLEN "l"605#define LUA_INTEGER_FRMLEN "l"
546 606
607#define LUA_MAXUNSIGNED ULONG_MAX
608#define LUA_MAXINTEGER LONG_MAX
609#define LUA_MININTEGER LONG_MIN
610
547#elif defined(LUA_INT_LONGLONG) /* }{ long long */611#elif defined(LUA_INT_LONGLONG) /* }{ long long */
548 612
549#if defined(_WIN32)613#if defined(_WIN32)
614
550#define LUA_INTEGER __int64615#define LUA_INTEGER __int64
551#define LUA_INTEGER_FRMLEN "I64"616#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
552#else622#else
623
553#define LUA_INTEGER long long624#define LUA_INTEGER long long
554#define LUA_INTEGER_FRMLEN "ll"625#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
555#endif631#endif
556 632
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*/
558 638
559#error "numeric integer type not defined"639#define LUA_INTEGER short int
640#define LUA_INTEGER_FRMLEN ""
560 641
561#endif /* } */642#define LUA_MAXUNSIGNED ((LUA_UNSIGNED)USHRT_MAX)
643#define LUA_MAXINTEGER SHRT_MAX
644#define LUA_MININTEGER SHRT_MIN
562 645
646#undef LUAI_UACINT
647#define LUAI_UACINT int
563 648
564#define LUA_INTEGER_SCAN "%" LUA_INTEGER_FRMLEN "d"649#undef LUAI_MAXSTACK
565#define LUA_INTEGER_FMT "%" LUA_INTEGER_FRMLEN "d"650#define LUAI_MAXSTACK 15000
566#define lua_integer2str(s,n) sprintf((s), LUA_INTEGER_FMT, (n))
567 651
568#define LUA_UNSIGNED unsigned LUA_INTEGER652#define l_castS2U(x) ((LUA_UNSIGNED)(unsigned short)(x))
653
654#else /* }{ */
655
656#error "numeric integer type not defined"
657
658#endif /* } */
569 659
570/* }================================================================== */660/* }================================================================== */
571 661
src/lundump.c+27−19
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** load precompiled Lua chunks3** load precompiled Lua chunks
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -42,7 +42,7 @@ static l_noret error(LoadState *S, const char *why) { · 36 unmodified lines
42 42
43/*43/*
44** All high-level loads go through LoadVector; you can change it to44** All high-level loads go through LoadVector; you can change it to
45** adapt to the endianess of the input45** adapt to the endianness of the input
46*/46*/
47#define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0]))47#define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0]))
48 48
@@ -105,12 +105,12 @@ static void LoadCode (LoadState *S, Proto *f) { · 56 unmodified lines
105}105}
106 106
107 107
108static void LoadFunction(LoadState *S, Proto *f);108static void LoadFunction(LoadState *S, Proto *f, TString *psource);
109 109
110 110
111static void LoadConstants (LoadState *S, Proto *f) {111static void LoadConstants (LoadState *S, Proto *f) {
112 int i, n;112 int i;
113 n = LoadInt(S);113 int n = LoadInt(S);
114 f->k = luaM_newvector(S->L, n, TValue);114 f->k = luaM_newvector(S->L, n, TValue);
115 f->sizek = n;115 f->sizek = n;
116 for (i = 0; i < n; i++)116 for (i = 0; i < n; i++)
@@ -126,7 +126,7 @@ static void LoadConstants (LoadState *S, Proto *f) { · 9 unmodified lines
126 setbvalue(o, LoadByte(S));126 setbvalue(o, LoadByte(S));
127 break;127 break;
128 case LUA_TNUMFLT:128 case LUA_TNUMFLT:
129 setnvalue(o, LoadNumber(S));129 setfltvalue(o, LoadNumber(S));
130 break;130 break;
131 case LUA_TNUMINT:131 case LUA_TNUMINT:
132 setivalue(o, LoadInteger(S));132 setivalue(o, LoadInteger(S));
@@ -139,14 +139,19 @@ static void LoadConstants (LoadState *S, Proto *f) { · 6 unmodified lines
139 lua_assert(0);139 lua_assert(0);
140 }140 }
141 }141 }
142 n = LoadInt(S);142}
143
144
145static void LoadProtos (LoadState *S, Proto *f) {
146 int i;
147 int n = LoadInt(S);
143 f->p = luaM_newvector(S->L, n, Proto *);148 f->p = luaM_newvector(S->L, n, Proto *);
144 f->sizep = n;149 f->sizep = n;
145 for (i = 0; i < n; i++)150 for (i = 0; i < n; i++)
146 f->p[i] = NULL;151 f->p[i] = NULL;
147 for (i = 0; i < n; i++) {152 for (i = 0; i < n; i++) {
148 f->p[i] = luaF_newproto(S->L);153 f->p[i] = luaF_newproto(S->L);
149 LoadFunction(S, f->p[i]);154 LoadFunction(S, f->p[i], f->source);
150 }155 }
151}156}
152 157
@@ -167,7 +172,6 @@ static void LoadUpvalues (LoadState *S, Proto *f) { · 14 unmodified lines
167 172
168static void LoadDebug (LoadState *S, Proto *f) {173static void LoadDebug (LoadState *S, Proto *f) {
169 int i, n;174 int i, n;
170 f->source = LoadString(S);
171 n = LoadInt(S);175 n = LoadInt(S);
172 f->lineinfo = luaM_newvector(S->L, n, int);176 f->lineinfo = luaM_newvector(S->L, n, int);
173 f->sizelineinfo = n;177 f->sizelineinfo = n;
@@ -188,7 +192,10 @@ static void LoadDebug (LoadState *S, Proto *f) { · 14 unmodified lines
188}192}
189 193
190 194
191static void LoadFunction (LoadState *S, Proto *f) {195static 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 */
192 f->linedefined = LoadInt(S);199 f->linedefined = LoadInt(S);
193 f->lastlinedefined = LoadInt(S);200 f->lastlinedefined = LoadInt(S);
194 f->numparams = LoadByte(S);201 f->numparams = LoadByte(S);
@@ -197,13 +204,14 @@ static void LoadFunction (LoadState *S, Proto *f) { · 2 unmodified lines
197 LoadCode(S, f);204 LoadCode(S, f);
198 LoadConstants(S, f);205 LoadConstants(S, f);
199 LoadUpvalues(S, f);206 LoadUpvalues(S, f);
207 LoadProtos(S, f);
200 LoadDebug(S, f);208 LoadDebug(S, f);
201}209}
202 210
203 211
204static void checkliteral (LoadState *S, const char *s, const char *msg) {212static void checkliteral (LoadState *S, const char *s, const char *msg) {
205 char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */213 char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */
206 int len = strlen(s);214 size_t len = strlen(s);
207 LoadVector(S, buff, len);215 LoadVector(S, buff, len);
208 if (memcmp(s, buff, len) != 0)216 if (memcmp(s, buff, len) != 0)
209 error(S, msg);217 error(S, msg);
@@ -231,7 +239,7 @@ static void checkHeader (LoadState *S) { · 21 unmodified lines
231 checksize(S, lua_Integer);239 checksize(S, lua_Integer);
232 checksize(S, lua_Number);240 checksize(S, lua_Number);
233 if (LoadInteger(S) != LUAC_INT)241 if (LoadInteger(S) != LUAC_INT)
234 error(S, "endianess mismatch in");242 error(S, "endianness mismatch in");
235 if (LoadNumber(S) != LUAC_NUM)243 if (LoadNumber(S) != LUAC_NUM)
236 error(S, "float format mismatch in");244 error(S, "float format mismatch in");
237}245}
@@ -240,10 +248,10 @@ static void checkHeader (LoadState *S) { · 2 unmodified lines
240/*248/*
241** load precompiled chunk249** load precompiled chunk
242*/250*/
243Closure *luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff,251LClosure *luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff,
244 const char *name) {252 const char *name) {
245 LoadState S;253 LoadState S;
246 Closure *cl;254 LClosure *cl;
247 if (*name == '@' || *name == '=')255 if (*name == '@' || *name == '=')
248 S.name = name + 1;256 S.name = name + 1;
249 else if (*name == LUA_SIGNATURE[0])257 else if (*name == LUA_SIGNATURE[0])
@@ -257,10 +265,10 @@ Closure *luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff, · 7 unmodified lines
257 cl = luaF_newLclosure(L, LoadByte(&S));265 cl = luaF_newLclosure(L, LoadByte(&S));
258 setclLvalue(L, L->top, cl);266 setclLvalue(L, L->top, cl);
259 incr_top(L);267 incr_top(L);
260 cl->l.p = luaF_newproto(L);268 cl->p = luaF_newproto(L);
261 LoadFunction(&S, cl->l.p);269 LoadFunction(&S, cl->p, NULL);
262 lua_assert(cl->l.nupvalues == cl->l.p->sizeupvalues);270 lua_assert(cl->nupvalues == cl->p->sizeupvalues);
263 luai_verifycode(L, buff, cl->l.p);271 luai_verifycode(L, buff, cl->p);
264 return cl;272 return cl;
265}273}
266 274
src/lundump.h+4−4
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** load precompiled Lua chunks3** load precompiled Lua chunks
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -15,7 +15,7 @@ · 9 unmodified lines
15/* data to catch conversion errors */15/* data to catch conversion errors */
16#define LUAC_DATA "\x19\x93\r\n\x1a\n"16#define LUAC_DATA "\x19\x93\r\n\x1a\n"
17 17
18#define LUAC_INT cast_integer(0xABCD)18#define LUAC_INT 0x5678
19#define LUAC_NUM cast_num(370.5)19#define LUAC_NUM cast_num(370.5)
20 20
21#define MYINT(s) (s[0]-'0')21#define MYINT(s) (s[0]-'0')
@@ -23,8 +23,8 @@ · 1 unmodified line
23#define LUAC_FORMAT 0 /* this is the official format */23#define LUAC_FORMAT 0 /* this is the official format */
24 24
25/* load one chunk; from lundump.c */25/* load one chunk; from lundump.c */
26LUAI_FUNC Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff,26LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff,
27 const char* name);27 const char* name);
28 28
29/* dump one chunk; from ldump.c */29/* dump one chunk; from ldump.c */
30LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w,30LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w,
src/lutf8lib.c+49−38
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Standard library for UTF-8 manipulation3** Standard library for UTF-8 manipulation
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -32,7 +32,7 @@ static lua_Integer u_posrelat (lua_Integer pos, size_t len) { · 26 unmodified lines
32 32
33 33
34/*34/*
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.
36*/36*/
37static const char *utf8_decode (const char *o, int *val) {37static const char *utf8_decode (const char *o, int *val) {
38 static unsigned int limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFF};38 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
61 61
62 62
63/*63/*
64** utf8len(s, [i]) --> number of codepoints in 's' after 'i';64** utf8len(s [, i [, j]]) --> number of characters that start in the
65** nil if 's' not well formed65** range [i,j], or nil + current position if 's' is not well formed in
66** that interval
66*/67*/
67static int utflen (lua_State *L) {68static int utflen (lua_State *L) {
68 int n = 0;69 int n = 0;
69 const char *ends;
70 size_t len;70 size_t len;
71 const char *s = luaL_checklstring(L, 1, &len);71 const char *s = luaL_checklstring(L, 1, &len);
72 lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), 1);72 lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
73 luaL_argcheck(L, 1 <= posi && posi <= (lua_Integer)len, 1,73 lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len);
74 luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2,
74 "initial position out of string");75 "initial position out of string");
75 ends = s + len;76 luaL_argcheck(L, --posj < (lua_Integer)len, 3,
76 s += posi - 1;77 "final position out of string");
77 while (s < ends && (s = utf8_decode(s, NULL)) != NULL)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;
78 n++;86 n++;
79 if (s == ends)87 }
80 lua_pushinteger(L, n);88 lua_pushinteger(L, n);
81 else
82 lua_pushnil(L);
83 return 1;89 return 1;
84}90}
85 91
86 92
87/*93/*
88** codepoint(s, [i, [j]]) -> returns codepoints for all characters94** codepoint(s, [i, [j]]) -> returns codepoints for all characters
89** between i and j95** that start in the range [i,j]
90*/96*/
91static int codepoint (lua_State *L) {97static int codepoint (lua_State *L) {
92 size_t len;98 size_t len;
@@ -145,41 +151,46 @@ static int utfchar (lua_State *L) { · 52 unmodified lines
145 151
146 152
147/*153/*
148** offset(s, n, [i]) -> index where n-th character *after*154** offset(s, n, [i]) -> index where n-th character counting from
149** position 'i' starts; 0 means character at 'i'.155** position 'i' starts; 0 means character at 'i'.
150*/156*/
151static int byteoffset (lua_State *L) {157static int byteoffset (lua_State *L) {
152 size_t len;158 size_t len;
153 const char *s = luaL_checklstring(L, 1, &len);159 const char *s = luaL_checklstring(L, 1, &len);
154 int n = luaL_checkint(L, 2);160 int n = luaL_checkint(L, 2);
155 lua_Integer posi = u_posrelat(luaL_optinteger(L, 3, 1), len) - 1;161 lua_Integer posi = (n >= 0) ? 1 : len + 1;
156 luaL_argcheck(L, 0 <= posi && posi <= (lua_Integer)len, 3,162 posi = u_posrelat(luaL_optinteger(L, 3, posi), len);
163 luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3,
157 "position out of range");164 "position out of range");
158 if (n == 0) {165 if (n == 0) {
159 /* find beginning of current byte sequence */166 /* find beginning of current byte sequence */
160 while (posi > 0 && iscont(s + posi)) posi--;167 while (posi > 0 && iscont(s + posi)) posi--;
161 }168 }
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 }
170 else {169 else {
171 n--; /* do not move for 1st character */170 if (iscont(s + posi))
172 while (n > 0 && posi < (lua_Integer)len) {171 luaL_error(L, "initial position is a continuation byte");
173 do { /* find beginning of next character */172 if (n < 0) {
174 posi++;173 while (n < 0 && posi > 0) { /* move back */
175 } while (iscont(s + posi)); /* ('\0' is not continuation) */174 do { /* find beginning of previous character */
176 n--;175 posi--;
177 }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 }
178 }189 }
179 if (n == 0)190 if (n == 0) /* did it find given character? */
180 lua_pushinteger(L, posi + 1);191 lua_pushinteger(L, posi + 1);
181 else192 else /* no such character */
182 lua_pushnil(L); /* no such position */193 lua_pushnil(L);
183 return 1; 194 return 1;
184}195}
185 196
@@ -187,7 +198,7 @@ static int byteoffset (lua_State *L) { · 1 unmodified line
187static int iter_aux (lua_State *L) {198static int iter_aux (lua_State *L) {
188 size_t len;199 size_t len;
189 const char *s = luaL_checklstring(L, 1, &len);200 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;
191 if (n < 0) /* first iteration? */202 if (n < 0) /* first iteration? */
192 n = 0; /* start from here */203 n = 0; /* start from here */
193 else if (n < (lua_Integer)len) {204 else if (n < (lua_Integer)len) {
@@ -231,7 +242,7 @@ static struct luaL_Reg funcs[] = { · 37 unmodified lines
231};242};
232 243
233 244
234int luaopen_utf8 (lua_State *L) {245LUAMOD_API int luaopen_utf8 (lua_State *L) {
235 luaL_newlib(L, funcs);246 luaL_newlib(L, funcs);
236 lua_pushliteral(L, UTF8PATT);247 lua_pushliteral(L, UTF8PATT);
237 lua_setfield(L, -2, "charpatt");248 lua_setfield(L, -2, "charpatt");
src/lvm.c+251−127
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Lua virtual machine3** Lua virtual machine
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -37,25 +37,100 @@ · 31 unmodified lines
37#define MAXNUMBER2STR 5037#define MAXNUMBER2STR 50
38 38
39 39
40/*
41** Similar to 'tonumber', but does not attempt to convert strings and
42** ensure correct precision (no extra bits). Used in comparisons.
43*/
44static 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*/
40int luaV_tonumber_ (const TValue *obj, lua_Number *n) {62int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
41 lua_assert(!ttisfloat(obj));63 TValue v;
64 again:
42 if (ttisinteger(obj)) {65 if (ttisinteger(obj)) {
43 *n = cast_num(ivalue(obj));66 *n = cast_num(ivalue(obj));
44 return 1;67 return 1;
45 }68 }
46 else69 else if (ttisfloat(obj)) {
47 return (ttisstring(obj) && luaO_str2d(svalue(obj), tsvalue(obj)->len, n));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*/
85static 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 */
48}103}
49 104
50 105
106/*
107** try to convert a non-integer value to an integer, rounding down
108*/
109int 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*/
51int luaV_tostring (lua_State *L, StkId obj) {117int luaV_tostring (lua_State *L, StkId obj) {
52 if (!ttisnumber(obj))118 if (!ttisnumber(obj))
53 return 0;119 return 0;
54 else {120 else {
55 char buff[MAXNUMBER2STR];121 char buff[MAXNUMBER2STR];
56 size_t len = (ttisinteger(obj))122 size_t len;
57 ? lua_integer2str(buff, ivalue(obj))123 if (ttisinteger(obj))
58 : lua_number2str(buff, fltvalue(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 }
59 setsvalue2s(L, obj, luaS_newlstr(L, buff, len));134 setsvalue2s(L, obj, luaS_newlstr(L, buff, len));
60 return 1;135 return 1;
61 }136 }
@@ -63,37 +138,46 @@ int luaV_tostring (lua_State *L, StkId obj) { · 1 unmodified line
63 138
64 139
65/*140/*
66** Check whether a float number is within the range of a lua_Integer.141** Try to convert a 'for' limit to an integer, preserving the
67** (The comparisons are tricky because of rounding, which can or142** semantics of the loop.
68** not occur depending on the relative sizes of floats and integers.)143** (The following explanation assumes a non-negative step; it is valid
69** This function should be called only when 'n' has an integral value.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.
70*/154*/
71int luaV_numtointeger (lua_Number n, lua_Integer *p) {155static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step,
72 if (cast_num(MIN_INTEGER) <= n && n < (MAX_INTEGER + cast_num(1))) {156 int *stopnow) {
73 *p = cast_integer(n);157 *stopnow = 0; /* usually, let loops run */
74 lua_assert(cast_num(*p) == n);158 if (!tointeger_aux(obj, p, (step < 0))) { /* does not fit in integer? */
75 return 1;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 }
76 }170 }
77 return 0; /* number is outside integer limits */171 return 1;
78}172}
79 173
80 174
81/*175/*
82** try to convert a non-integer value to an integer176** Main function for table access (invoking metamethods if needed).
177** Compute 'val = t[key]'
83*/178*/
84int 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
95void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {179void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
96 int loop;180 int loop; /* counter to avoid infinite loops */
97 for (loop = 0; loop < MAXTAGLOOP; loop++) {181 for (loop = 0; loop < MAXTAGLOOP; loop++) {
98 const TValue *tm;182 const TValue *tm;
99 if (ttistable(t)) { /* `t' is a table? */183 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
101 const TValue *res = luaH_get(h, key); /* do a primitive get */185 const TValue *res = luaH_get(h, key); /* do a primitive get */
102 if (!ttisnil(res) || /* result is not nil? */186 if (!ttisnil(res) || /* result is not nil? */
103 (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */187 (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 */
105 return;189 return;
106 }190 }
107 /* else will try the tag method */191 /* else will try metamethod */
108 }192 }
109 else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))193 else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))
110 luaG_typeerror(L, t, "index");194 luaG_typeerror(L, t, "index"); /* no metamethod */
111 if (ttisfunction(tm)) {195 if (ttisfunction(tm)) { /* metamethod is a function */
112 luaT_callTM(L, tm, t, key, val, 1);196 luaT_callTM(L, tm, t, key, val, 1);
113 return;197 return;
114 }198 }
115 t = tm; /* else repeat with 'tm' */199 t = tm; /* else repeat access over 'tm' */
116 }200 }
117 luaG_runerror(L, "gettable chain too long; possible loop");201 luaG_runerror(L, "gettable chain too long; possible loop");
118}202}
119 203
120 204
205/*
206** Main function for table assignment (invoking metamethods if needed).
207** Compute 't[key] = val'
208*/
121void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {209void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {
122 int loop;210 int loop; /* counter to avoid infinite loops */
123 for (loop = 0; loop < MAXTAGLOOP; loop++) {211 for (loop = 0; loop < MAXTAGLOOP; loop++) {
124 const TValue *tm;212 const TValue *tm;
125 if (ttistable(t)) { /* `t' is a table? */213 if (ttistable(t)) { /* `t' is a table? */
126 Table *h = hvalue(t);214 Table *h = hvalue(t);
127 TValue *oldval = cast(TValue *, luaH_get(h, key));215 TValue *oldval = cast(TValue *, luaH_get(h, key));
128 /* if previous value is not nil, there must be a previous entry216 /* 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 */
130 if (!ttisnil(oldval) ||218 if (!ttisnil(oldval) ||
131 /* previous value is nil; must check the metamethod */219 /* previous value is nil; must check the metamethod */
132 ((tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL &&220 ((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
146 else /* not a table; check metamethod */234 else /* not a table; check metamethod */
147 if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))235 if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
148 luaG_typeerror(L, t, "index");236 luaG_typeerror(L, t, "index");
149 /* there is a metamethod */237 /* try the metamethod */
150 if (ttisfunction(tm)) {238 if (ttisfunction(tm)) {
151 luaT_callTM(L, tm, t, key, val, 0);239 luaT_callTM(L, tm, t, key, val, 0);
152 return;240 return;
153 }241 }
154 t = tm; /* else repeat with 'tm' */242 t = tm; /* else repeat assginment over 'tm' */
155 }243 }
156 luaG_runerror(L, "settable chain too long; possible loop");244 luaG_runerror(L, "settable chain too long; possible loop");
157}245}
158 246
159 247
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*/
160static int l_strcmp (const TString *ls, const TString *rs) {255static int l_strcmp (const TString *ls, const TString *rs) {
161 const char *l = getstr(ls);256 const char *l = getstr(ls);
162 size_t ll = ls->tsv.len;257 size_t ll = ls->tsv.len;
163 const char *r = getstr(rs);258 const char *r = getstr(rs);
164 size_t lr = rs->tsv.len;259 size_t lr = rs->tsv.len;
165 for (;;) {260 for (;;) { /* for each segment */
166 int temp = strcoll(l, r);261 int temp = strcoll(l, r);
167 if (temp != 0) return temp;262 if (temp != 0) /* not equal? */
168 else { /* strings are equal up to a `\0' */263 return temp; /* done */
264 else { /* strings are equal up to a '\0' */
169 size_t len = strlen(l); /* index of first `\0' in both strings */265 size_t len = strlen(l); /* index of first `\0' in both strings */
170 if (len == lr) /* r is finished? */266 if (len == lr) /* 'rs' is finished? */
171 return (len == ll) ? 0 : 1;267 return (len == ll) ? 0 : 1; /* check 'ls' */
172 else if (len == ll) /* l is finished? */268 else if (len == ll) /* 'ls' is finished? */
173 return -1; /* l is smaller than r (because r is not finished) */269 return -1; /* 'ls' is smaller than 'rs' ('rs' is not finished) */
174 /* both strings longer than `len'; go on comparing (after the `\0') */270 /* both strings longer than `len'; go on comparing after the '\0' */
175 len++;271 len++;
176 l += len; ll -= len; r += len; lr -= len;272 l += len; ll -= len; r += len; lr -= len;
177 }273 }
@@ -179,29 +275,35 @@ static int l_strcmp (const TString *ls, const TString *rs) { · 1 unmodified line
179}275}
180 276
181 277
278/*
279** Main operation less than; return 'l < r'.
280*/
182int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {281int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
183 int res;282 int res;
184 lua_Number nl, nr;283 lua_Number nl, nr;
185 if (ttisinteger(l) && ttisinteger(r))284 if (ttisinteger(l) && ttisinteger(r)) /* both operands are integers? */
186 return (ivalue(l) < ivalue(r));285 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? */
188 return luai_numlt(nl, nr);287 return luai_numlt(nl, nr);
189 else if (ttisstring(l) && ttisstring(r))288 else if (ttisstring(l) && ttisstring(r)) /* both are strings? */
190 return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;289 return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
191 else if ((res = luaT_callorderTM(L, l, r, TM_LT)) < 0)290 else if ((res = luaT_callorderTM(L, l, r, TM_LT)) < 0) /* no metamethod? */
192 luaG_ordererror(L, l, r);291 luaG_ordererror(L, l, r); /* error */
193 return res;292 return res;
194}293}
195 294
196 295
296/*
297** Main operation less than or equal to; return 'l <= r'.
298*/
197int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) {299int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) {
198 int res;300 int res;
199 lua_Number nl, nr;301 lua_Number nl, nr;
200 if (ttisinteger(l) && ttisinteger(r))302 if (ttisinteger(l) && ttisinteger(r)) /* both operands are integers? */
201 return (ivalue(l) <= ivalue(r));303 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? */
203 return luai_numle(nl, nr);305 return luai_numle(nl, nr);
204 else if (ttisstring(l) && ttisstring(r))306 else if (ttisstring(l) && ttisstring(r)) /* both are strings? */
205 return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;307 return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
206 else if ((res = luaT_callorderTM(L, l, r, TM_LE)) >= 0) /* first try `le' */308 else if ((res = luaT_callorderTM(L, l, r, TM_LE)) >= 0) /* first try `le' */
207 return res;309 return res;
@@ -212,17 +314,18 @@ int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) { · 4 unmodified lines
212 314
213 315
214/*316/*
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)
216*/319*/
217int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) {320int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) {
218 const TValue *tm;321 const TValue *tm;
219 if (ttype(t1) != ttype(t2)) {322 if (ttype(t1) != ttype(t2)) { /* not the same variant? */
220 if (ttnov(t1) != ttnov(t2) || ttnov(t1) != LUA_TNUMBER)323 if (ttnov(t1) != ttnov(t2) || ttnov(t1) != LUA_TNUMBER)
221 return 0; /* only numbers can be equal with different variants */324 return 0; /* only numbers can be equal with different variants */
222 else { /* two numbers with different variants */325 else { /* two numbers with different variants */
223 lua_Number n1, n2;326 lua_Number n1, n2; /* compare them as floats */
224 lua_assert(ttisnumber(t1) && ttisnumber(t2));327 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));
226 return luai_numeq(n1, n2);329 return luai_numeq(n1, n2);
227 }330 }
228 }331 }
@@ -239,24 +342,36 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { · 10 unmodified lines
239 case LUA_TUSERDATA: {342 case LUA_TUSERDATA: {
240 if (uvalue(t1) == uvalue(t2)) return 1;343 if (uvalue(t1) == uvalue(t2)) return 1;
241 else if (L == NULL) return 0;344 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);
243 break; /* will try TM */348 break; /* will try TM */
244 }349 }
245 case LUA_TTABLE: {350 case LUA_TTABLE: {
246 if (hvalue(t1) == hvalue(t2)) return 1;351 if (hvalue(t1) == hvalue(t2)) return 1;
247 else if (L == NULL) return 0;352 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);
249 break; /* will try TM */356 break; /* will try TM */
250 }357 }
251 default:358 default:
252 return gcvalue(t1) == gcvalue(t2);359 return gcvalue(t1) == gcvalue(t2);
253 }360 }
254 if (tm == NULL) return 0; /* no TM? */361 if (tm == NULL) /* no TM? */
362 return 0; /* objects are different */
255 luaT_callTM(L, tm, t1, t2, L->top, 1); /* call TM */363 luaT_callTM(L, tm, t1, t2, L->top, 1); /* call TM */
256 return !l_isfalse(L->top);364 return !l_isfalse(L->top);
257}365}
258 366
259 367
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*/
260void luaV_concat (lua_State *L, int total) {375void luaV_concat (lua_State *L, int total) {
261 lua_assert(total >= 2);376 lua_assert(total >= 2);
262 do {377 do {
@@ -284,12 +399,12 @@ void luaV_concat (lua_State *L, int total) { · 21 unmodified lines
284 buffer = luaZ_openspace(L, &G(L)->buff, tl);399 buffer = luaZ_openspace(L, &G(L)->buff, tl);
285 tl = 0;400 tl = 0;
286 n = i;401 n = i;
287 do { /* concat all strings */402 do { /* copy all strings to buffer */
288 size_t l = tsvalue(top-i)->len;403 size_t l = tsvalue(top-i)->len;
289 memcpy(buffer+tl, svalue(top-i), l * sizeof(char));404 memcpy(buffer+tl, svalue(top-i), l * sizeof(char));
290 tl += l;405 tl += l;
291 } while (--i > 0);406 } 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 */
293 }408 }
294 total -= n-1; /* got 'n' strings to create 1 new */409 total -= n-1; /* got 'n' strings to create 1 new */
295 L->top -= n-1; /* popped 'n' strings and pushed one */410 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
297}412}
298 413
299 414
415/*
416** Main operation 'ra' = #rb'.
417*/
300void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {418void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
301 const TValue *tm;419 const TValue *tm;
302 switch (ttnov(rb)) {420 switch (ttnov(rb)) {
@@ -322,15 +440,19 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { · 19 unmodified lines
322}440}
323 441
324 442
325lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y) {443/*
326 if (cast_unsigned(y) + 1 <= 1U) { /* special cases: -1 or 0 */444** Integer division; return 'm // n'. (Assume that C division with
327 if (y == 0)445** negative operands follows C99 behavior.)
446*/
447lua_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)
328 luaG_runerror(L, "attempt to divide by zero");450 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 */
330 }452 }
331 else {453 else {
332 lua_Integer d = x / y; /* perform division */454 lua_Integer d = m / n; /* perform division */
333 if ((x ^ y) >= 0 || x % y == 0) /* same signal or no rest? */455 if ((m ^ n) >= 0 || m % n == 0) /* same signal or no rest? */
334 return d;456 return d;
335 else457 else
336 return d - 1; /* correct 'div' for negative case */458 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
338}460}
339 461
340 462
341lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y) {463/*
342 if (cast_unsigned(y) + 1 <= 1U) { /* special cases: -1 or 0 */464** Integer modulus; return 'm % n'. (Assume that C '%' with
343 if (y == 0)465** negative operands follows C99 behavior.)
466*/
467lua_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)
344 luaG_runerror(L, "attempt to perform 'n%%0'");470 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 */
346 }472 }
347 else {473 else {
348 lua_Integer r = x % y;474 lua_Integer r = m % n;
349 if (r == 0 || (x ^ y) >= 0)475 if (r == 0 || (m ^ n) >= 0) /* no rest or same signal? */
350 return r;476 return r;
351 else477 else
352 return r + y; /* correct 'mod' for negative case */478 return r + n; /* correct 'mod' for negative case */
353 }
354}
355
356
357lua_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;
371 }479 }
372}480}
373 481
@@ -375,14 +483,17 @@ lua_Integer luaV_pow (lua_State *L, lua_Integer x, lua_Integer y) { · 1 unmodified line
375/* number of bits in an integer */483/* number of bits in an integer */
376#define NBITS cast_int(sizeof(lua_Integer) * CHAR_BIT)484#define NBITS cast_int(sizeof(lua_Integer) * CHAR_BIT)
377 485
378LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) {486/*
487** Shift left operation. (Shift right just negates 'y'.)
488*/
489lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) {
379 if (y < 0) { /* shift right? */490 if (y < 0) { /* shift right? */
380 if (y <= -NBITS) return 0;491 if (y <= -NBITS) return 0;
381 else return cast_integer(cast_unsigned(x) >> (-y));492 else return intop(>>, x, -y);
382 }493 }
383 else { /* shift left */494 else { /* shift left */
384 if (y >= NBITS) return 0;495 if (y >= NBITS) return 0;
385 else return x << y;496 else return intop(<<, x, y);
386 }497 }
387}498}
388 499
@@ -392,15 +503,15 @@ LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) { · 3 unmodified lines
392** whether there is a cached closure with the same upvalues needed by503** whether there is a cached closure with the same upvalues needed by
393** new closure to be created.504** new closure to be created.
394*/505*/
395static Closure *getcached (Proto *p, UpVal **encup, StkId base) {506static LClosure *getcached (Proto *p, UpVal **encup, StkId base) {
396 Closure *c = p->cache;507 LClosure *c = p->cache;
397 if (c != NULL) { /* is there a cached closure? */508 if (c != NULL) { /* is there a cached closure? */
398 int nup = p->sizeupvalues;509 int nup = p->sizeupvalues;
399 Upvaldesc *uv = p->upvalues;510 Upvaldesc *uv = p->upvalues;
400 int i;511 int i;
401 for (i = 0; i < nup; i++) { /* check whether it has right upvalues */512 for (i = 0; i < nup; i++) { /* check whether it has right upvalues */
402 TValue *v = uv[i].instack ? base + uv[i].idx : encup[uv[i].idx]->v;513 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)
404 return NULL; /* wrong upvalue; cannot reuse closure */515 return NULL; /* wrong upvalue; cannot reuse closure */
405 }516 }
406 }517 }
@@ -419,15 +530,15 @@ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, · 12 unmodified lines
419 int nup = p->sizeupvalues;530 int nup = p->sizeupvalues;
420 Upvaldesc *uv = p->upvalues;531 Upvaldesc *uv = p->upvalues;
421 int i;532 int i;
422 Closure *ncl = luaF_newLclosure(L, nup);533 LClosure *ncl = luaF_newLclosure(L, nup);
423 ncl->l.p = p;534 ncl->p = p;
424 setclLvalue(L, ra, ncl); /* anchor new closure in stack */535 setclLvalue(L, ra, ncl); /* anchor new closure in stack */
425 for (i = 0; i < nup; i++) { /* fill in its upvalues */536 for (i = 0; i < nup; i++) { /* fill in its upvalues */
426 if (uv[i].instack) /* upvalue refers to local variable? */537 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);
428 else /* get upvalue from enclosing function */539 else /* get upvalue from enclosing function */
429 ncl->l.upvals[i] = encup[uv[i].idx];540 ncl->upvals[i] = encup[uv[i].idx];
430 ncl->l.upvals[i]->refcount++;541 ncl->upvals[i]->refcount++;
431 /* new closure is white, so we do not need a barrier here */542 /* new closure is white, so we do not need a barrier here */
432 }543 }
433 if (!isblack(obj2gco(p))) /* cache will not break GC invariant? */544 if (!isblack(obj2gco(p))) /* cache will not break GC invariant? */
@@ -497,6 +608,14 @@ void luaV_finishOp (lua_State *L) { · 63 unmodified lines
497 608
498 609
499 610
611
612/*
613** {==================================================================
614** Function 'luaV_execute': main interpreter loop
615** ===================================================================
616*/
617
618
500/*619/*
501** some macros for common tasks in `luaV_execute'620** some macros for common tasks in `luaV_execute'
502*/621*/
@@ -633,7 +752,7 @@ void luaV_execute (lua_State *L) { · 130 unmodified lines
633 setivalue(ra, intop(+, ib, ic));752 setivalue(ra, intop(+, ib, ic));
634 }753 }
635 else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {754 else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
636 setnvalue(ra, luai_numadd(L, nb, nc));755 setfltvalue(ra, luai_numadd(L, nb, nc));
637 }756 }
638 else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_ADD)); }757 else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_ADD)); }
639 )758 )
@@ -646,7 +765,7 @@ void luaV_execute (lua_State *L) { · 6 unmodified lines
646 setivalue(ra, intop(-, ib, ic));765 setivalue(ra, intop(-, ib, ic));
647 }766 }
648 else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {767 else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
649 setnvalue(ra, luai_numsub(L, nb, nc));768 setfltvalue(ra, luai_numsub(L, nb, nc));
650 }769 }
651 else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SUB)); }770 else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SUB)); }
652 )771 )
@@ -659,7 +778,7 @@ void luaV_execute (lua_State *L) { · 6 unmodified lines
659 setivalue(ra, intop(*, ib, ic));778 setivalue(ra, intop(*, ib, ic));
660 }779 }
661 else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {780 else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
662 setnvalue(ra, luai_nummul(L, nb, nc));781 setfltvalue(ra, luai_nummul(L, nb, nc));
663 }782 }
664 else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MUL)); }783 else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MUL)); }
665 )784 )
@@ -668,7 +787,7 @@ void luaV_execute (lua_State *L) { · 2 unmodified lines
668 TValue *rc = RKC(i);787 TValue *rc = RKC(i);
669 lua_Number nb; lua_Number nc;788 lua_Number nb; lua_Number nc;
670 if (tonumber(rb, &nb) && tonumber(rc, &nc)) {789 if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
671 setnvalue(ra, luai_numdiv(L, nb, nc));790 setfltvalue(ra, luai_numdiv(L, nb, nc));
672 }791 }
673 else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV)); }792 else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV)); }
674 )793 )
@@ -735,7 +854,9 @@ void luaV_execute (lua_State *L) { · 60 unmodified lines
735 setivalue(ra, luaV_mod(L, ib, ic));854 setivalue(ra, luaV_mod(L, ib, ic));
736 }855 }
737 else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {856 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);
739 }860 }
740 else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MOD)); }861 else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MOD)); }
741 )862 )
@@ -743,12 +864,8 @@ void luaV_execute (lua_State *L) { · 1 unmodified line
743 TValue *rb = RKB(i);864 TValue *rb = RKB(i);
744 TValue *rc = RKC(i);865 TValue *rc = RKC(i);
745 lua_Number nb; lua_Number nc;866 lua_Number nb; lua_Number nc;
746 if (ttisinteger(rb) && ttisinteger(rc)) {867 if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
747 lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);868 setfltvalue(ra, luai_numpow(L, nb, nc));
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));
752 }869 }
753 else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_POW)); }870 else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_POW)); }
754 )871 )
@@ -760,7 +877,7 @@ void luaV_execute (lua_State *L) { · 5 unmodified lines
760 setivalue(ra, intop(-, 0, ib));877 setivalue(ra, intop(-, 0, ib));
761 }878 }
762 else if (tonumber(rb, &nb)) {879 else if (tonumber(rb, &nb)) {
763 setnvalue(ra, luai_numunm(L, nb));880 setfltvalue(ra, luai_numunm(L, nb));
764 }881 }
765 else {882 else {
766 Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM));883 Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM));
@@ -770,7 +887,7 @@ void luaV_execute (lua_State *L) { · 3 unmodified lines
770 TValue *rb = RB(i);887 TValue *rb = RB(i);
771 lua_Integer ib;888 lua_Integer ib;
772 if (tointeger(rb, &ib)) {889 if (tointeger(rb, &ib)) {
773 setivalue(ra, intop(^, cast_integer(-1), ib));890 setivalue(ra, intop(^, ~l_castS2U(0), ib));
774 }891 }
775 else {892 else {
776 Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT));893 Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT));
@@ -916,8 +1033,8 @@ void luaV_execute (lua_State *L) { · 139 unmodified lines
916 if (luai_numlt(0, step) ? luai_numle(idx, limit)1033 if (luai_numlt(0, step) ? luai_numle(idx, limit)
917 : luai_numle(limit, idx)) {1034 : luai_numle(limit, idx)) {
918 ci->u.l.savedpc += GETARG_sBx(i); /* jump back */1035 ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
919 setnvalue(ra, idx); /* update internal index... */1036 setfltvalue(ra, idx); /* update internal index... */
920 setnvalue(ra + 3, idx); /* ...and external index */1037 setfltvalue(ra + 3, idx); /* ...and external index */
921 }1038 }
922 }1039 }
923 )1040 )
@@ -925,21 +1042,26 @@ void luaV_execute (lua_State *L) { · 1 unmodified line
925 TValue *init = ra;1042 TValue *init = ra;
926 TValue *plimit = ra + 1;1043 TValue *plimit = ra + 1;
927 TValue *pstep = ra + 2;1044 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)) {
929 /* all values are integer */1049 /* 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));
931 }1053 }
932 else { /* try making all values floats */1054 else { /* try making all values floats */
933 lua_Number ninit; lua_Number nlimit; lua_Number nstep;1055 lua_Number ninit; lua_Number nlimit; lua_Number nstep;
934 if (!tonumber(plimit, &nlimit))1056 if (!tonumber(plimit, &nlimit))
935 luaG_runerror(L, LUA_QL("for") " limit must be a number");1057 luaG_runerror(L, LUA_QL("for") " limit must be a number");
936 setnvalue(plimit, nlimit);1058 setfltvalue(plimit, nlimit);
937 if (!tonumber(pstep, &nstep))1059 if (!tonumber(pstep, &nstep))
938 luaG_runerror(L, LUA_QL("for") " step must be a number");1060 luaG_runerror(L, LUA_QL("for") " step must be a number");
939 setnvalue(pstep, nstep);1061 setfltvalue(pstep, nstep);
940 if (!tonumber(init, &ninit))1062 if (!tonumber(init, &ninit))
941 luaG_runerror(L, LUA_QL("for") " initial value must be a number");1063 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));
943 }1065 }
944 ci->u.l.savedpc += GETARG_sBx(i);1066 ci->u.l.savedpc += GETARG_sBx(i);
945 )1067 )
@@ -987,7 +1109,7 @@ void luaV_execute (lua_State *L) { · 41 unmodified lines
987 )1109 )
988 vmcase(OP_CLOSURE,1110 vmcase(OP_CLOSURE,
989 Proto *p = cl->p->p[GETARG_Bx(i)];1111 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 */
991 if (ncl == NULL) /* no match? */1113 if (ncl == NULL) /* no match? */
992 pushclosure(L, p, cl->upvals, base, ra); /* create a new one */1114 pushclosure(L, p, cl->upvals, base, ra); /* create a new one */
993 else1115 else
@@ -1020,3 +1142,5 @@ void luaV_execute (lua_State *L) { · 26 unmodified lines
1020 }1142 }
1021}1143}
1022 1144
1145/* }================================================================== */
1146
src/lvm.h+2−7
@@ -1,5 +1,5 @@
1/*1/*
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 $
3** Lua virtual machine3** Lua virtual machine
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -13,16 +13,13 @@ · 7 unmodified lines
13#include "ltm.h"13#include "ltm.h"
14 14
15 15
16#define tostring(L,o) (ttisstring(o) || (luaV_tostring(L, o)))
17
18#define tonumber(o,n) \16#define tonumber(o,n) \
19 (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n))17 (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n))
20 18
21#define tointeger(o,i) \19#define tointeger(o,i) \
22 (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger_(o,i))20 (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger_(o,i))
23 21
24#define intop(op,v1,v2) \22#define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2))
25 cast_integer(cast_unsigned(v1) op cast_unsigned(v2))
26 23
27#define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2)24#define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2)
28 25
@@ -32,7 +29,6 @@ LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); · 3 unmodified lines
32LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r);29LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r);
33LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n);30LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n);
34LUAI_FUNC int luaV_tointeger_ (const TValue *obj, lua_Integer *p);31LUAI_FUNC int luaV_tointeger_ (const TValue *obj, lua_Integer *p);
35LUAI_FUNC int luaV_numtointeger (lua_Number n, lua_Integer *p);
36LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj);32LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj);
37LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key,33LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key,
38 StkId val);34 StkId val);
@@ -43,7 +39,6 @@ LUAI_FUNC void luaV_execute (lua_State *L); · 4 unmodified lines
43LUAI_FUNC void luaV_concat (lua_State *L, int total);39LUAI_FUNC void luaV_concat (lua_State *L, int total);
44LUAI_FUNC lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y);40LUAI_FUNC lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y);
45LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y);41LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y);
46LUAI_FUNC lua_Integer luaV_pow (lua_State *L, lua_Integer x, lua_Integer y);
47LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y);42LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y);
48LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb);43LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb);
49 44