Unified · Split
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | |
| 2 | −This is Lua 5.3.0 (work2), released on 21 Mar 2014. | |
| 2 | +This is Lua 5.3.0 (work3), released on 19 Jul 2014. | |
| 3 | 3 | |
| 4 | 4 | For installation instructions, license details, and |
| 5 | 5 | further information about Lua, see doc/readme.html. |
| @@ -174,6 +174,7 @@ Freely available under the terms of the · 173 unmodified lines | ||
| 174 | 174 | |
| 175 | 175 | <P> |
| 176 | 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 | 178 | <A HREF="manual.html#pdf-coroutine.resume">coroutine.resume</A><BR> |
| 178 | 179 | <A HREF="manual.html#pdf-coroutine.running">coroutine.running</A><BR> |
| 179 | 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 | 184 | <P> |
| 184 | 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 | 187 | <A HREF="manual.html#pdf-debug.gethook">debug.gethook</A><BR> |
| 186 | 188 | <A HREF="manual.html#pdf-debug.getinfo">debug.getinfo</A><BR> |
| 187 | 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 | 191 | <A HREF="manual.html#pdf-debug.getregistry">debug.getregistry</A><BR> |
| 190 | 192 | <A HREF="manual.html#pdf-debug.getupvalue">debug.getupvalue</A><BR> |
| 191 | 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 | 194 | <A HREF="manual.html#pdf-debug.sethook">debug.sethook</A><BR> |
| 194 | 195 | <A HREF="manual.html#pdf-debug.setlocal">debug.setlocal</A><BR> |
| 195 | 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 | 229 | <TD> |
| 229 | 230 | <H3> </H3> |
| 230 | 231 | <P> |
| 232 | +<A HREF="manual.html#pdf-math">math</A><BR> | |
| 231 | 233 | <A HREF="manual.html#pdf-math.abs">math.abs</A><BR> |
| 232 | 234 | <A HREF="manual.html#pdf-math.acos">math.acos</A><BR> |
| 233 | 235 | <A HREF="manual.html#pdf-math.asin">math.asin</A><BR> |
| 234 | 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 | 237 | <A HREF="manual.html#pdf-math.ceil">math.ceil</A><BR> |
| 237 | 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 | 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 | 240 | <A HREF="manual.html#pdf-math.floor">math.floor</A><BR> |
| 242 | 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 | 242 | <A HREF="manual.html#pdf-math.huge">math.huge</A><BR> |
| 245 | 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 | 244 | <A HREF="manual.html#pdf-math.log">math.log</A><BR> |
| 249 | 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 | 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 | 249 | <A HREF="manual.html#pdf-math.modf">math.modf</A><BR> |
| 252 | 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 | 251 | <A HREF="manual.html#pdf-math.rad">math.rad</A><BR> |
| 255 | 252 | <A HREF="manual.html#pdf-math.random">math.random</A><BR> |
| 256 | 253 | <A HREF="manual.html#pdf-math.randomseed">math.randomseed</A><BR> |
| 257 | 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 | 255 | <A HREF="manual.html#pdf-math.sqrt">math.sqrt</A><BR> |
| 260 | 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 | 257 | <A HREF="manual.html#pdf-math.type">math.type</A><BR> |
| 263 | 258 | |
| 264 | − | |
| 265 | 259 | <P> |
| 266 | 260 | <A HREF="manual.html#pdf-os.clock">os.clock</A><BR> |
| 267 | 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 | 283 | <A HREF="manual.html#pdf-string.byte">string.byte</A><BR> |
| 290 | 284 | <A HREF="manual.html#pdf-string.char">string.char</A><BR> |
| 291 | 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 | 288 | <A HREF="manual.html#pdf-string.find">string.find</A><BR> |
| 293 | 289 | <A HREF="manual.html#pdf-string.format">string.format</A><BR> |
| 294 | 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 | 292 | <A HREF="manual.html#pdf-string.len">string.len</A><BR> |
| 297 | 293 | <A HREF="manual.html#pdf-string.lower">string.lower</A><BR> |
| 298 | 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 | 295 | <A HREF="manual.html#pdf-string.rep">string.rep</A><BR> |
| 302 | 296 | <A HREF="manual.html#pdf-string.reverse">string.reverse</A><BR> |
| 303 | 297 | <A HREF="manual.html#pdf-string.sub">string.sub</A><BR> |
| 304 | −<A HREF="manual.html#pdf-string.unpackfloat">string.unpackfloat</A><BR> | |
| 305 | −<A HREF="manual.html#pdf-string.unpackint">string.unpackint</A><BR> | |
| 298 | +<A HREF="manual.html#pdf-string.undumpfloat">string.undumpfloat</A><BR> | |
| 299 | +<A HREF="manual.html#pdf-string.undumpint">string.undumpint</A><BR> | |
| 306 | 300 | <A HREF="manual.html#pdf-string.upper">string.upper</A><BR> |
| 307 | 301 | |
| 308 | 302 | <P> |
| @@ -330,6 +324,7 @@ Freely available under the terms of the · 21 unmodified lines | ||
| 330 | 324 | <A HREF="manual.html#lua_Debug">lua_Debug</A><BR> |
| 331 | 325 | <A HREF="manual.html#lua_Hook">lua_Hook</A><BR> |
| 332 | 326 | <A HREF="manual.html#lua_Integer">lua_Integer</A><BR> |
| 327 | +<A HREF="manual.html#lua_KFunction">lua_KFunction</A><BR> | |
| 333 | 328 | <A HREF="manual.html#lua_Number">lua_Number</A><BR> |
| 334 | 329 | <A HREF="manual.html#lua_Reader">lua_Reader</A><BR> |
| 335 | 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 | 347 | <A HREF="manual.html#lua_error">lua_error</A><BR> |
| 353 | 348 | <A HREF="manual.html#lua_gc">lua_gc</A><BR> |
| 354 | 349 | <A HREF="manual.html#lua_getallocf">lua_getallocf</A><BR> |
| 355 | −<A HREF="manual.html#lua_getctx">lua_getctx</A><BR> | |
| 356 | 350 | <A HREF="manual.html#lua_getfield">lua_getfield</A><BR> |
| 357 | 351 | <A HREF="manual.html#lua_getglobal">lua_getglobal</A><BR> |
| 358 | 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 | 374 | <A HREF="manual.html#lua_istable">lua_istable</A><BR> |
| 381 | 375 | <A HREF="manual.html#lua_isthread">lua_isthread</A><BR> |
| 382 | 376 | <A HREF="manual.html#lua_isuserdata">lua_isuserdata</A><BR> |
| 377 | +<A HREF="manual.html#lua_isyieldable">lua_isyieldable</A><BR> | |
| 383 | 378 | <A HREF="manual.html#lua_len">lua_len</A><BR> |
| 384 | 379 | <A HREF="manual.html#lua_load">lua_load</A><BR> |
| 385 | 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 | 382 | <A HREF="manual.html#lua_newthread">lua_newthread</A><BR> |
| 388 | 383 | <A HREF="manual.html#lua_newuserdata">lua_newuserdata</A><BR> |
| 389 | 384 | <A HREF="manual.html#lua_next">lua_next</A><BR> |
| 385 | +<A HREF="manual.html#lua_numtointeger">lua_numtointeger</A><BR> | |
| 390 | 386 | <A HREF="manual.html#lua_pcall">lua_pcall</A><BR> |
| 391 | 387 | <A HREF="manual.html#lua_pcallk">lua_pcallk</A><BR> |
| 392 | 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 | 414 | <A HREF="manual.html#lua_remove">lua_remove</A><BR> |
| 419 | 415 | <A HREF="manual.html#lua_replace">lua_replace</A><BR> |
| 420 | 416 | <A HREF="manual.html#lua_resume">lua_resume</A><BR> |
| 417 | +<A HREF="manual.html#lua_rotate">lua_rotate</A><BR> | |
| 421 | 418 | <A HREF="manual.html#lua_setallocf">lua_setallocf</A><BR> |
| 422 | 419 | <A HREF="manual.html#lua_setfield">lua_setfield</A><BR> |
| 423 | 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 | 531 | <HR> |
| 535 | 532 | <SMALL CLASS="footer"> |
| 536 | 533 | Last update: |
| 537 | −Fri Mar 21 17:34:12 BRT 2014 | |
| 534 | +Thu Jun 19 17:09:46 BRT 2014 | |
| 538 | 535 | </SMALL> |
| 539 | 536 | <!-- |
| 540 | −Last change: updated for Lua 5.3.0 (work2) | |
| 537 | +Last change: updated for Lua 5.3.0 (work3) | |
| 541 | 538 | --> |
| 542 | 539 | |
| 543 | 540 | </BODY> |
| @@ -38,7 +38,7 @@ Freely available under the terms of the · 37 unmodified lines | ||
| 38 | 38 | <!-- ====================================================================== --> |
| 39 | 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 | ||
| 139 | 139 | Lua has explicit rules about when each representation is used, |
| 140 | 140 | but it also converts between them automatically as needed (see <a href="#3.4.3">§3.4.3</a>). |
| 141 | 141 | Therefore, |
| 142 | −the programmer has the option of mostly ignore the difference | |
| 142 | +the programmer has the option of mostly ignoring the difference | |
| 143 | 143 | between integers and floats |
| 144 | −or assume complete control about the representation of each value. | |
| 144 | +or assuming complete control over the representation of each value. | |
| 145 | 145 | Standard Lua uses 64-bit integers and double-precision (64-bit) floats, |
| 146 | 146 | but you can also compile Lua so that it |
| 147 | 147 | uses 32-bit integers and/or single-precision (32-bit) floats. |
| @@ -437,7 +437,7 @@ with the following code: · 289 unmodified lines | ||
| 437 | 437 | </pre> |
| 438 | 438 | |
| 439 | 439 | <p> |
| 440 | −For the unary minus and the bitwise not operators, | |
| 440 | +For the unary operators (negation, length, and bitwise not), | |
| 441 | 441 | the metamethod is computed and called with a dummy second operand, |
| 442 | 442 | equal to the first one. |
| 443 | 443 | This extra operand is only to simplify Lua's internals |
| @@ -575,9 +575,9 @@ Otherwise, Lua raises an error. · 131 unmodified lines | ||
| 575 | 575 | <li><b>"eq": </b> |
| 576 | 576 | the <code>==</code> (equal) operation. |
| 577 | 577 | |
| 578 | −Lua will try a metamethod only when the values | |
| 579 | −being compared are both either tables or full userdata, | |
| 580 | −both have the same metamethod, | |
| 578 | +Behavior similar to the "add" operation, | |
| 579 | +except that Lua will try a metamethod only when the values | |
| 580 | +being compared are either both tables or both full userdata | |
| 581 | 581 | and they are not primitively equal. |
| 582 | 582 | The result of the call is always converted to a boolean. |
| 583 | 583 | </li> |
| @@ -586,7 +586,9 @@ The result of the call is always converted to a boolean. · 2 unmodified lines | ||
| 586 | 586 | the <code><</code> (less than) operation. |
| 587 | 587 | |
| 588 | 588 | Behavior similar to the "add" operation, |
| 589 | −except that the result of the call is always converted to a boolean. | |
| 589 | +except that Lua will try a metamethod only when the values | |
| 590 | +being compared are neither both numbers nor both strings. | |
| 591 | +The result of the call is always converted to a boolean. | |
| 590 | 592 | </li> |
| 591 | 593 | |
| 592 | 594 | <li><b>"le": </b> |
| @@ -595,7 +597,7 @@ the <code><=</code> (less equal) operation. · 2 unmodified lines | ||
| 595 | 597 | Unlike other operations, |
| 596 | 598 | The less-equal operation can use two different events. |
| 597 | 599 | First, Lua looks for the "<code>__le</code>" metamethod in both operands, |
| 598 | −like in the "add" operation. | |
| 600 | +like in the "lt" operation. | |
| 599 | 601 | If it cannot find such a metamethod, |
| 600 | 602 | then it will try the "<code>__lt</code>" event, |
| 601 | 603 | assuming that <code>a <= b</code> is equivalent to <code>not (b < a)</code>. |
| @@ -775,7 +777,7 @@ Lua simply ignores it.) · 173 unmodified lines | ||
| 775 | 777 | <p> |
| 776 | 778 | At the end of each garbage-collection cycle, |
| 777 | 779 | the finalizers for objects are called in |
| 778 | −the reverse order that they were marked for collection, | |
| 780 | +the reverse order that they were marked for finalization, | |
| 779 | 781 | among those collected in that cycle; |
| 780 | 782 | that is, the first finalizer to be called is the one associated |
| 781 | 783 | with the object marked last in the program. |
| @@ -792,17 +794,20 @@ and the object memory is freed in the next garbage-collection cycle. · 10 unmodified lines | ||
| 792 | 794 | However, if the finalizer stores the object in some global place |
| 793 | 795 | (e.g., a global variable), |
| 794 | 796 | then there is a permanent resurrection. |
| 797 | +Moreover, if the finalizer marks a finalizing object for finalization again, | |
| 798 | +its finalizer will be called again in the next cycle where the | |
| 799 | +object is unreachable. | |
| 795 | 800 | In any case, |
| 796 | −the object memory is freed only when it becomes completely inaccessible; | |
| 797 | −its finalizer will never be called twice. | |
| 801 | +the object memory is freed only in the GC cycle where | |
| 802 | +the object is unreachable and not marked for finalization. | |
| 798 | 803 | |
| 799 | 804 | |
| 800 | 805 | <p> |
| 801 | 806 | When you close a state (see <a href="#lua_close"><code>lua_close</code></a>), |
| 802 | 807 | Lua calls the finalizers of all objects marked for finalization, |
| 803 | 808 | following the reverse order that they were marked. |
| 804 | −If any finalizer marks new objects for collection during that phase, | |
| 805 | −these new objects will not be finalized. | |
| 809 | +If any finalizer marks objects for collection during that phase, | |
| 810 | +these marks have no effect. | |
| 806 | 811 | |
| 807 | 812 | |
| 808 | 813 | |
| @@ -1064,9 +1069,10 @@ and cannot be used as names: · 255 unmodified lines | ||
| 1064 | 1069 | Lua is a case-sensitive language: |
| 1065 | 1070 | <code>and</code> is a reserved word, but <code>And</code> and <code>AND</code> |
| 1066 | 1071 | are two different, valid names. |
| 1067 | −As a convention, names starting with an underscore followed by | |
| 1068 | −uppercase letters (such as <a href="#pdf-_VERSION"><code>_VERSION</code></a>) | |
| 1069 | −are reserved for variables used by Lua. | |
| 1072 | +As a convention, | |
| 1073 | +programs should avoid creating | |
| 1074 | +names that start with an underscore followed by | |
| 1075 | +one or more uppercase letters (such as <a href="#pdf-_VERSION"><code>_VERSION</code></a>). | |
| 1070 | 1076 | |
| 1071 | 1077 | |
| 1072 | 1078 | <p> |
| @@ -1074,6 +1080,7 @@ The following strings denote other tokens: · 1 unmodified line | ||
| 1074 | 1080 | |
| 1075 | 1081 | <pre> |
| 1076 | 1082 | + - * / % ^ # |
| 1083 | + & ~ | << >> // | |
| 1077 | 1084 | == ~= <= >= < > = |
| 1078 | 1085 | ( ) { } [ ] :: |
| 1079 | 1086 | ; : , . .. ... |
| @@ -1130,11 +1137,12 @@ enclosed by <em>long brackets</em>. · 50 unmodified lines | ||
| 1130 | 1137 | We define an <em>opening long bracket of level <em>n</em></em> as an opening |
| 1131 | 1138 | square bracket followed by <em>n</em> equal signs followed by another |
| 1132 | 1139 | opening square bracket. |
| 1133 | −So, an opening long bracket of level 0 is written as <code>[[</code>, | |
| 1134 | −an opening long bracket of level 1 is written as <code>[=[</code>, | |
| 1140 | +So, an opening long bracket of level 0 is written as <code>[[</code>, | |
| 1141 | +an opening long bracket of level 1 is written as <code>[=[</code>, | |
| 1135 | 1142 | and so on. |
| 1136 | 1143 | A <em>closing long bracket</em> is defined similarly; |
| 1137 | −for instance, a closing long bracket of level 4 is written as <code>]====]</code>. | |
| 1144 | +for instance, | |
| 1145 | +a closing long bracket of level 4 is written as <code>]====]</code>. | |
| 1138 | 1146 | A <em>long literal</em> starts with an opening long bracket of any level and |
| 1139 | 1147 | ends at the first closing long bracket of the same level. |
| 1140 | 1148 | It can contain any text except a closing bracket of the proper level. |
| @@ -1594,20 +1602,19 @@ is equivalent to the code: · 453 unmodified lines | ||
| 1594 | 1602 | |
| 1595 | 1603 | <pre> |
| 1596 | 1604 | 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 | 1606 | if not (<em>var</em> and <em>limit</em> and <em>step</em>) then error() end |
| 1599 | − while (<em>step</em> > 0 and <em>var</em> <= <em>limit</em>) or (<em>step</em> <= 0 and <em>var</em> >= <em>limit</em>) do | |
| 1607 | + <em>var</em> = <em>var</em> - <em>step</em> | |
| 1608 | + while true do | |
| 1609 | + <em>var</em> = <em>var</em> + <em>step</em> | |
| 1610 | + if (<em>step</em> >= 0 and <em>var</em> > <em>limit</em>) or (<em>step</em> < 0 and <em>var</em> < <em>limit</em>) then | |
| 1611 | + break | |
| 1612 | + end | |
| 1600 | 1613 | local v = <em>var</em> |
| 1601 | 1614 | <em>block</em> |
| 1602 | − <em>var</em> = <em>var</em> + <em>step</em> | |
| 1603 | 1615 | end |
| 1604 | 1616 | end |
| 1605 | −</pre><p> | |
| 1606 | −where <code>convert</code> returns | |
| 1607 | −its three arguments unaltered | |
| 1608 | −if they are all integers, | |
| 1609 | −otherwise it converts them to floats. | |
| 1610 | − | |
| 1617 | +</pre> | |
| 1611 | 1618 | |
| 1612 | 1619 | <p> |
| 1613 | 1620 | Note the following: |
| @@ -1620,12 +1627,6 @@ before the loop starts. · 6 unmodified lines | ||
| 1620 | 1627 | They must all result in numbers. |
| 1621 | 1628 | </li> |
| 1622 | 1629 | |
| 1623 | −<li> | |
| 1624 | −If any of the three control values is not an integer, | |
| 1625 | −all three values are converted to floats, | |
| 1626 | −so that the loop is done with floats. | |
| 1627 | −</li> | |
| 1628 | − | |
| 1629 | 1630 | <li> |
| 1630 | 1631 | <code><em>var</em></code>, <code><em>limit</em></code>, and <code><em>step</em></code> are invisible variables. |
| 1631 | 1632 | The names shown here are for explanatory purposes only. |
| @@ -1641,9 +1642,8 @@ You can use <b>break</b> and <b>goto</b> to exit a <b>for</b> loop. · 9 unmodified lines | ||
| 1641 | 1642 | </li> |
| 1642 | 1643 | |
| 1643 | 1644 | <li> |
| 1644 | −The loop variable <code>v</code> is local to the loop; | |
| 1645 | −you cannot use its value after the <b>for</b> ends or is broken. | |
| 1646 | −If you need its value, | |
| 1645 | +The loop variable <code>v</code> is local to the loop body. | |
| 1646 | +If you need its value after the loop, | |
| 1647 | 1647 | assign it to another variable before exiting the loop. |
| 1648 | 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 | 1841 | <h3>3.4.1 – <a name="3.4.1">Arithmetic Operators</a></h3><p> |
| 1842 | 1842 | Lua supports the following arithmetic operators: |
| 1843 | −the binary <code>+</code> (addition), | |
| 1844 | −<code>-</code> (subtraction), <code>*</code> (multiplication), | |
| 1845 | −<code>/</code> (division), <code>//</code> (integer division), | |
| 1846 | −<code>%</code> (modulo), and <code>^</code> (exponentiation); | |
| 1847 | −and unary <code>-</code> (mathematical negation). | |
| 1848 | −Modulo is defined as the remainder of a division | |
| 1849 | −that rounds the quotient towards minus infinite (floor). | |
| 1843 | +<table border="1"> | |
| 1844 | +<tr><td><code>+</code></td><td>addition</td></tr> | |
| 1845 | +<tr><td><code>-</code></td><td>subtraction</td></tr> | |
| 1846 | +<tr><td><code>*</code></td><td>multiplication</td></tr> | |
| 1847 | +<tr><td><code>/</code></td><td>float division</td></tr> | |
| 1848 | +<tr><td><code>//</code></td><td>integer division</td></tr> | |
| 1849 | +<tr><td><code>%</code></td><td>modulo</td></tr> | |
| 1850 | +<tr><td><code>^</code></td><td>exponentiation</td></tr> | |
| 1851 | +<tr><td><code>-</code></td><td>unary minus</td></tr> | |
| 1852 | +</table> | |
| 1850 | 1853 | |
| 1851 | 1854 | |
| 1852 | 1855 | <p> |
| 1853 | −With the exception of division and integer division, | |
| 1856 | +With the exception of divisions and exponentiation, | |
| 1854 | 1857 | the arithmetic operators work as follows: |
| 1855 | 1858 | If both operands are integers, |
| 1856 | 1859 | the operation is performed over integers and the result is an integer. |
| @@ -1865,8 +1868,11 @@ and the result is a float. · 8 unmodified lines | ||
| 1865 | 1868 | |
| 1866 | 1869 | |
| 1867 | 1870 | <p> |
| 1868 | −Division (<code>/</code>) always converts its operands to floats | |
| 1869 | −and its result is always a float. | |
| 1871 | +Float division (<code>/</code>) and exponentiation | |
| 1872 | +always convert their operands to floats | |
| 1873 | +and the result is always a float. | |
| 1874 | +Exponentiation uses the ANSI C function <code>pow</code>, | |
| 1875 | +so that it works for non-integer exponents too. | |
| 1870 | 1876 | |
| 1871 | 1877 | |
| 1872 | 1878 | <p> |
| @@ -1876,10 +1882,16 @@ and its result is always an integer. · 3 unmodified lines | ||
| 1876 | 1882 | The result is always rounded towards minus infinite (floor). |
| 1877 | 1883 | |
| 1878 | 1884 | |
| 1885 | +<p> | |
| 1886 | +Modulo is defined as the remainder of a division | |
| 1887 | +that rounds the quotient towards minus infinite (floor). | |
| 1888 | + | |
| 1889 | + | |
| 1879 | 1890 | <p> |
| 1880 | 1891 | In case of overflows in integer arithmetic, |
| 1881 | 1892 | all operations <em>wrap around</em>, |
| 1882 | 1893 | according to the usual rules of two-complement arithmetic. |
| 1894 | +(In other words, they return the correct result modulo <em>2<sup>64</sup></em>.) | |
| 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 | 1900 | <h3>3.4.2 – <a name="3.4.2">Bitwise Operators</a></h3><p> |
| 1889 | 1901 | Lua supports the following bitwise operators: |
| 1890 | −the binary <code>&</code> (bitwise and), | |
| 1891 | −<code>|</code> (bitwise or), <code>~</code> (bitwise exclusive or), | |
| 1892 | −<code>>></code> (right shift), <code><<</code> (left shift), | |
| 1893 | −and unary <code>~</code> (bitwise not). | |
| 1902 | +<table border="1"> | |
| 1903 | +<tr><td><code>&</code></td><td>bitwise and</td></tr> | |
| 1904 | +<tr><td><code>|</code></td><td>bitwise or</td></tr> | |
| 1905 | +<tr><td><code>~</code></td><td>bitwise exclusive or</td></tr> | |
| 1906 | +<tr><td><code>>></code></td><td>right shift</td></tr> | |
| 1907 | +<tr><td><code><<</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 | 1912 | <p> |
| @@ -1901,7 +1917,7 @@ and result in an integer. · 4 unmodified lines | ||
| 1901 | 1917 | |
| 1902 | 1918 | |
| 1903 | 1919 | <p> |
| 1904 | −Both right and left shifts fill with zeros the vacant bits. | |
| 1920 | +Both right and left shifts fill the vacant bits with zeros. | |
| 1905 | 1921 | Negative displacements shift to the other direction; |
| 1906 | 1922 | displacements with absolute values equal to or higher than |
| 1907 | 1923 | the number of bits in an integer |
| @@ -1917,7 +1933,7 @@ types and representations at run time. · 9 unmodified lines | ||
| 1917 | 1933 | Most arithmetic operations applied to mixed numbers |
| 1918 | 1934 | (integers and floats) convert the integer operand to a float; |
| 1919 | 1935 | this is called the <em>usual rule</em>. |
| 1920 | −Division always convert integer operands to floats; | |
| 1936 | +Float division always convert integer operands to floats; | |
| 1921 | 1937 | integer division and bitwise operators |
| 1922 | 1938 | always convert float operands to integers. |
| 1923 | 1939 | The C API also converts both integers to floats and |
| @@ -1929,7 +1945,6 @@ besides strings. · 5 unmodified lines | ||
| 1929 | 1945 | <p> |
| 1930 | 1946 | Lua also converts strings to numbers, |
| 1931 | 1947 | whenever a number is expected. |
| 1932 | −(This conversion may be deprecated in future versions.) | |
| 1933 | 1948 | |
| 1934 | 1949 | |
| 1935 | 1950 | <p> |
| @@ -1951,16 +1966,16 @@ Otherwise, the conversion fails. · 15 unmodified lines | ||
| 1951 | 1966 | |
| 1952 | 1967 | |
| 1953 | 1968 | <p> |
| 1954 | −The conversion from strings to floats | |
| 1955 | −follows the rules of the Lua lexer. | |
| 1956 | −(The string may have leading and trailing spaces and a sign.) | |
| 1957 | −There is no direct conversion from strings to integers: | |
| 1958 | −If a string is provided where an integer is needed, | |
| 1959 | −Lua converts the string to a float and then the float to an integer. | |
| 1969 | +The conversion from strings to numbers goes as follows: | |
| 1970 | +First, the string is converted to an integer or a float, | |
| 1971 | +following its syntax and the rules of the Lua lexer. | |
| 1972 | +(The string may have also leading and trailing spaces and a sign.) | |
| 1973 | +Then, the resulting number is converted to the required type | |
| 1974 | +(float or integer) according to the previous rules. | |
| 1960 | 1975 | |
| 1961 | 1976 | |
| 1962 | 1977 | <p> |
| 1963 | −The conversion from numbers to strings use a human-readable, | |
| 1978 | +The conversion from numbers to strings uses a human-readable, | |
| 1964 | 1979 | non-specified format. |
| 1965 | 1980 | For complete control over how numbers are converted to strings, |
| 1966 | 1981 | use the <code>format</code> function from the string library |
| @@ -1971,11 +1986,15 @@ use the <code>format</code> function from the string library · 4 unmodified lines | ||
| 1971 | 1986 | |
| 1972 | 1987 | |
| 1973 | 1988 | <h3>3.4.4 – <a name="3.4.4">Relational Operators</a></h3><p> |
| 1974 | −The relational operators in Lua are | |
| 1975 | − | |
| 1976 | −<pre> | |
| 1977 | − == ~= < > <= >= | |
| 1978 | −</pre><p> | |
| 1989 | +Lua supports the following relational operators: | |
| 1990 | +<table border="1"> | |
| 1991 | +<tr><td><code>==</code></td><td>equality</td></tr> | |
| 1992 | +<tr><td><code>~=</code></td><td>inequality</td></tr> | |
| 1993 | +<tr><td><code><</code></td><td>less than</td></tr> | |
| 1994 | +<tr><td><code>></code></td><td>greater than</td></tr> | |
| 1995 | +<tr><td><code><=</code></td><td>less or equal</td></tr> | |
| 1996 | +<tr><td><code>>=</code></td><td>greater or equal</td></tr> | |
| 1997 | +</table> | |
| 1979 | 1998 | These 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 | ||
| 1986 | 2005 | Strings are compared in the obvious way. |
| 1987 | 2006 | Numbers follow the usual rule for binary operations: |
| 1988 | 2007 | if both operands are integers, |
| 1989 | −the are compared as integers. | |
| 1990 | −Otherwise, they are converted to floats | |
| 2008 | +the are compared as integers; | |
| 2009 | +otherwise, they are converted to floats | |
| 1991 | 2010 | and compared as such. |
| 1992 | 2011 | |
| 1993 | 2012 | |
| @@ -2131,10 +2150,14 @@ from lower to higher priority: · 137 unmodified lines | ||
| 2131 | 2150 | or |
| 2132 | 2151 | and |
| 2133 | 2152 | < > <= >= ~= == |
| 2153 | + | | |
| 2154 | + ~ | |
| 2155 | + & | |
| 2156 | + << >> | |
| 2134 | 2157 | .. |
| 2135 | 2158 | + - |
| 2136 | 2159 | * / // % |
| 2137 | − not # - (unary) | |
| 2160 | + unary operators (not # - ~) | |
| 2138 | 2161 | ^ |
| 2139 | 2162 | </pre><p> |
| 2140 | 2163 | As usual, |
| @@ -2690,7 +2713,7 @@ The registry table is always located at pseudo-index · 549 unmodified lines | ||
| 2690 | 2713 | <a name="pdf-LUA_REGISTRYINDEX"><code>LUA_REGISTRYINDEX</code></a>, |
| 2691 | 2714 | which is a valid index. |
| 2692 | 2715 | Any C library can store data into this table, |
| 2693 | −but it should take care to choose keys | |
| 2716 | +but it must take care to choose keys | |
| 2694 | 2717 | that are different from those used |
| 2695 | 2718 | by other libraries, to avoid collisions. |
| 2696 | 2719 | Typically, you should use as key a string containing your library name, |
| @@ -2705,7 +2728,7 @@ uppercase letters are reserved for Lua. · 8 unmodified lines | ||
| 2705 | 2728 | The integer keys in the registry are used by the reference mechanism, |
| 2706 | 2729 | implemented by the auxiliary library, |
| 2707 | 2730 | and by some predefined values. |
| 2708 | −Therefore, integer keys should not be used for other purposes. | |
| 2731 | +Therefore, integer keys must not be used for other purposes. | |
| 2709 | 2732 | |
| 2710 | 2733 | |
| 2711 | 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 | ||
| 2760 | 2783 | in particular, the error message is at the top of the stack. |
| 2761 | 2784 | However, there is no guarantees about stack space. |
| 2762 | 2785 | To push anything on the stack, |
| 2763 | −the panic function should first check the available space (see <a href="#4.2">§4.2</a>). | |
| 2786 | +the panic function must first check the available space (see <a href="#4.2">§4.2</a>). | |
| 2764 | 2787 | |
| 2765 | 2788 | |
| 2766 | 2789 | <p> |
| 2767 | −Most functions in the API can throw an error, | |
| 2790 | +Most functions in the API can raise an error, | |
| 2768 | 2791 | for instance due to a memory allocation error. |
| 2769 | 2792 | The documentation for each function indicates whether |
| 2770 | −it can throw errors. | |
| 2793 | +it can raise errors. | |
| 2771 | 2794 | |
| 2772 | 2795 | |
| 2773 | 2796 | <p> |
| 2774 | −Inside a C function you can throw an error by calling <a href="#lua_error"><code>lua_error</code></a>. | |
| 2797 | +Inside a C 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 function you can throw an error by calling <a href="#lua_error"> · 3 unmodified lines | ||
| 2781 | 2804 | |
| 2782 | 2805 | <p> |
| 2783 | 2806 | Internally, Lua uses the C <code>longjmp</code> facility to yield a coroutine. |
| 2784 | −Therefore, if a function <code>foo</code> calls an API function | |
| 2807 | +Therefore, if a C function <code>foo</code> calls an API function | |
| 2785 | 2808 | and this API function yields |
| 2786 | 2809 | (directly or indirectly by calling another function that yields), |
| 2787 | 2810 | Lua cannot return to <code>foo</code> any more, |
| @@ -2823,6 +2846,67 @@ the continuation function should continue the task · 35 unmodified lines | ||
| 2823 | 2846 | of the original function. |
| 2824 | 2847 | |
| 2825 | 2848 | |
| 2849 | +<p> | |
| 2850 | +As an illustration, consider the following function: | |
| 2851 | + | |
| 2852 | +<pre> | |
| 2853 | + int original_function (lua_State *L) { | |
| 2854 | + ... /* code 1 */ | |
| 2855 | + status = lua_pcall(L, n, m, h); /* calls Lua */ | |
| 2856 | + ... /* code 2 */ | |
| 2857 | + } | |
| 2858 | +</pre><p> | |
| 2859 | +Now we want to allow | |
| 2860 | +the Lua code being ran by <a href="#lua_pcall"><code>lua_pcall</code></a> to yield. | |
| 2861 | +First, we can rewrite our function like here: | |
| 2862 | + | |
| 2863 | +<pre> | |
| 2864 | + int k (lua_State *L, int status, int ctx) { | |
| 2865 | + ... /* code 2 */ | |
| 2866 | + } | |
| 2867 | + | |
| 2868 | + int original_function (lua_State *L) { | |
| 2869 | + ... /* code 1 */ | |
| 2870 | + return k(L, lua_pcall(L, n, m, h), ctx); | |
| 2871 | + } | |
| 2872 | +</pre><p> | |
| 2873 | +In the above code, | |
| 2874 | +the new function <code>k</code> is a | |
| 2875 | +<em>continuation function</em> (with type <a href="#lua_KFunction"><code>lua_KFunction</code></a>), | |
| 2876 | +which should do all the work that the original function | |
| 2877 | +was doing after calling <a href="#lua_pcall"><code>lua_pcall</code></a>. | |
| 2878 | +Now, we must inform Lua that it must call <code>k</code> if the Lua code | |
| 2879 | +begin running by <a href="#lua_pcall"><code>lua_pcall</code></a> gets interrupted in some way | |
| 2880 | +(errors or yielding), | |
| 2881 | +so we rewrite the code as here, | |
| 2882 | +replacing <a href="#lua_pcall"><code>lua_pcall</code></a> by <a href="#lua_pcallk"><code>lua_pcallk</code></a>: | |
| 2883 | + | |
| 2884 | +<pre> | |
| 2885 | + int original_function (lua_State *L) { | |
| 2886 | + ... /* code 1 */ | |
| 2887 | + return k(L, lua_pcallk(L, n, m, h, ctx2, k), ctx1); | |
| 2888 | + } | |
| 2889 | +</pre> | |
| 2890 | + | |
| 2891 | +<p> | |
| 2892 | +Besides the Lua state, | |
| 2893 | +the continuation function has two other parameters: | |
| 2894 | +the final status of the call plus the context value (<code>ctx</code>) that | |
| 2895 | +was passed originally to <a href="#lua_pcallk"><code>lua_pcallk</code></a>. | |
| 2896 | +(Lua does not use this context value; | |
| 2897 | +it only passes this value from the original function to the | |
| 2898 | +continuation function.) | |
| 2899 | +For <a href="#lua_pcallk"><code>lua_pcallk</code></a>, | |
| 2900 | +the status is the same value that would be returned by <a href="#lua_pcallk"><code>lua_pcallk</code></a>, | |
| 2901 | +except that it is <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> when being executed after an yield | |
| 2902 | +(instead of <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>). | |
| 2903 | +For <a href="#lua_yieldk"><code>lua_yieldk</code></a> and <a href="#lua_callk"><code>lua_callk</code></a>, | |
| 2904 | +the status is always <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> when Lua calls the continuation. | |
| 2905 | +(For these two functions, | |
| 2906 | +Lua will not call the continuation in case of errors, | |
| 2907 | +because they do not handle errors.) | |
| 2908 | + | |
| 2909 | + | |
| 2826 | 2910 | <p> |
| 2827 | 2911 | Lua treats the continuation function as if it were the original function. |
| 2828 | 2912 | The continuation function receives the same Lua stack |
| @@ -2836,11 +2920,6 @@ Whatever it returns is handled by Lua as if it were the return · 7 unmodified lines | ||
| 2836 | 2920 | of the original function. |
| 2837 | 2921 | |
| 2838 | 2922 | |
| 2839 | −<p> | |
| 2840 | −The only difference in the Lua state between the original function | |
| 2841 | −and its continuation is the result of a call to <a href="#lua_getctx"><code>lua_getctx</code></a>. | |
| 2842 | − | |
| 2843 | − | |
| 2844 | 2923 | |
| 2845 | 2924 | |
| 2846 | 2925 | |
| @@ -2867,10 +2946,10 @@ we cannot know how many elements the function pops/pushes · 20 unmodified lines | ||
| 2867 | 2946 | by looking only at its arguments |
| 2868 | 2947 | (e.g., they may depend on what is on the stack). |
| 2869 | 2948 | The third field, <code>x</code>, |
| 2870 | −tells whether the function may throw errors: | |
| 2871 | −'<code>-</code>' means the function never throws any error; | |
| 2872 | −'<code>e</code>' means the function may throw errors; | |
| 2873 | −'<code>v</code>' means the function may throw an error on purpose. | |
| 2949 | +tells whether the function may raise errors: | |
| 2950 | +'<code>-</code>' means the function never raises any error; | |
| 2951 | +'<code>e</code>' means the function may raise errors; | |
| 2952 | +'<code>v</code>' means the function may raise an error on purpose. | |
| 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 | 3008 | <p> |
| 2930 | 3009 | When <code>nsize</code> is zero, |
| 2931 | −the allocator should behave like <code>free</code> | |
| 3010 | +the allocator must behave like <code>free</code> | |
| 2932 | 3011 | and return <code>NULL</code>. |
| 2933 | 3012 | |
| 2934 | 3013 | |
| 2935 | 3014 | <p> |
| 2936 | 3015 | When <code>nsize</code> is not zero, |
| 2937 | −the allocator should behave like <code>realloc</code>. | |
| 3016 | +the allocator must behave like <code>realloc</code>. | |
| 2938 | 3017 | The allocator returns <code>NULL</code> |
| 2939 | 3018 | if and only if it cannot fulfill the request. |
| 2940 | 3019 | Lua assumes that the allocator never fails when |
| @@ -2990,7 +3069,7 @@ The value of <code>op</code> must be one of the following constants: · 49 unmodified lines | ||
| 2990 | 3069 | <li><b><a name="pdf-LUA_OPADD"><code>LUA_OPADD</code></a>: </b> performs addition (<code>+</code>)</li> |
| 2991 | 3070 | <li><b><a name="pdf-LUA_OPSUB"><code>LUA_OPSUB</code></a>: </b> performs subtraction (<code>-</code>)</li> |
| 2992 | 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 | 3073 | <li><b><a name="pdf-LUA_OPIDIV"><code>LUA_OPIDIV</code></a>: </b> performs integer division (<code>//</code>)</li> |
| 2995 | 3074 | <li><b><a name="pdf-LUA_OPMOD"><code>LUA_OPMOD</code></a>: </b> performs modulo (<code>%</code>)</li> |
| 2996 | 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 | 3160 | <hr><h3><a name="lua_callk"><code>lua_callk</code></a></h3><p> |
| 3082 | 3161 | <span class="apii">[-(nargs + 1), +nresults, <em>e</em>]</span> |
| 3083 | 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 | 3165 | <p> |
| 3087 | 3166 | This function behaves exactly like <a href="#lua_call"><code>lua_call</code></a>, |
| @@ -3258,7 +3337,10 @@ Otherwise you can use the function <a href="#lua_newtable"><code>lua_newtable</c · 170 unmodified lines | ||
| 3258 | 3337 | |
| 3259 | 3338 | <hr><h3><a name="lua_dump"><code>lua_dump</code></a></h3><p> |
| 3260 | 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 | 3345 | <p> |
| 3264 | 3346 | Dumps a function as a binary chunk. |
| @@ -3272,6 +3354,12 @@ with the given <code>data</code> · 7 unmodified lines | ||
| 3272 | 3354 | to write them. |
| 3273 | 3355 | |
| 3274 | 3356 | |
| 3357 | +<p> | |
| 3358 | +If <code>strip</code> is true, | |
| 3359 | +the binary representation is created without debug information | |
| 3360 | +about the function. | |
| 3361 | + | |
| 3362 | + | |
| 3275 | 3363 | <p> |
| 3276 | 3364 | The value returned is the error code returned by the last |
| 3277 | 3365 | call to the writer; |
| @@ -3291,8 +3379,7 @@ This function does not pop the Lua function from the stack. · 13 unmodified lines | ||
| 3291 | 3379 | |
| 3292 | 3380 | <p> |
| 3293 | 3381 | Generates a Lua error. |
| 3294 | −The error message (which can actually be a Lua value of any type) | |
| 3295 | −must be on the stack top. | |
| 3382 | +The error object must be on the stack top. | |
| 3296 | 3383 | This function does a long jump, |
| 3297 | 3384 | and therefore never returns |
| 3298 | 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, –]</span> | |
| 3385 | −<pre>int lua_getctx (lua_State *L, int *ctx);</pre> | |
| 3386 | − | |
| 3387 | −<p> | |
| 3388 | −This function is called by a continuation function (see <a href="#4.7">§4.7</a>) | |
| 3389 | −to retrieve the status of the thread and a context information. | |
| 3390 | − | |
| 3391 | − | |
| 3392 | −<p> | |
| 3393 | −When called in the original function, | |
| 3394 | −<a href="#lua_getctx"><code>lua_getctx</code></a> always returns <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> | |
| 3395 | −and does not change the value of its argument <code>ctx</code>. | |
| 3396 | −When called inside a continuation function, | |
| 3397 | −<a href="#lua_getctx"><code>lua_getctx</code></a> returns <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> and sets | |
| 3398 | −the value of <code>ctx</code> to be the context information | |
| 3399 | −(the value passed as the <code>ctx</code> argument | |
| 3400 | −to the callee together with the continuation function). | |
| 3401 | − | |
| 3402 | − | |
| 3403 | −<p> | |
| 3404 | −When the callee is <a href="#lua_pcallk"><code>lua_pcallk</code></a>, | |
| 3405 | −Lua may also call its continuation function | |
| 3406 | −to handle errors during the call. | |
| 3407 | −That is, upon an error in the function called by <a href="#lua_pcallk"><code>lua_pcallk</code></a>, | |
| 3408 | −Lua may not return to the original function | |
| 3409 | −but instead may call the continuation function. | |
| 3410 | −In that case, a call to <a href="#lua_getctx"><code>lua_getctx</code></a> will return the error code | |
| 3411 | −(the value that would be returned by <a href="#lua_pcallk"><code>lua_pcallk</code></a>); | |
| 3412 | −the value of <code>ctx</code> will be set to the context information, | |
| 3413 | −as in the case of a yield. | |
| 3414 | − | |
| 3415 | − | |
| 3416 | − | |
| 3417 | − | |
| 3418 | − | |
| 3419 | 3470 | <hr><h3><a name="lua_getfield"><code>lua_getfield</code></a></h3><p> |
| 3420 | 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 | 3474 | <p> |
| 3424 | 3475 | Pushes onto the stack the value <code>t[k]</code>, |
| @@ -3427,6 +3478,10 @@ As in Lua, this function may trigger a metamethod · 2 unmodified lines | ||
| 3427 | 3478 | for the "index" event (see <a href="#2.4">§2.4</a>). |
| 3428 | 3479 | |
| 3429 | 3480 | |
| 3481 | +<p> | |
| 3482 | +Returns 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 | 3551 | <hr><h3><a name="lua_getuservalue"><code>lua_getuservalue</code></a></h3><p> |
| 3497 | 3552 | <span class="apii">[-0, +1, –]</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 | 3555 | <p> |
| 3501 | 3556 | Pushes onto the stack the Lua value associated with the userdata |
| 3502 | 3557 | at the given index. |
| 3503 | 3558 | |
| 3504 | 3559 | |
| 3560 | +<p> | |
| 3561 | +Returns 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 | 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 | 3584 | <p> |
| 3526 | 3585 | The type of integers in Lua. |
| @@ -3534,6 +3593,12 @@ to <code>long</code> or <code>int</code> · 7 unmodified lines | ||
| 3534 | 3593 | (usually a 32-bit two-complement integer). |
| 3535 | 3594 | |
| 3536 | 3595 | |
| 3596 | +<p> | |
| 3597 | +Lua also defines the constants | |
| 3598 | +<a name="pdf-LUA_MININTEGER"><code>LUA_MININTEGER</code></a> and <a name="pdf-LUA_MAXINTEGER"><code>LUA_MAXINTEGER</code></a>, | |
| 3599 | +with the minimum and the maximum values that fit in this type. | |
| 3600 | + | |
| 3601 | + | |
| 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, –]</span> | |
| 3767 | +<pre>int lua_isyieldable (lua_State *L);</pre> | |
| 3768 | + | |
| 3769 | +<p> | |
| 3770 | +Returns 1 if the given coroutine can yield, | |
| 3771 | +and 0 otherwise. | |
| 3772 | + | |
| 3773 | + | |
| 3774 | + | |
| 3775 | + | |
| 3776 | + | |
| 3777 | +<hr><h3><a name="lua_KFunction"><code>lua_KFunction</code></a></h3> | |
| 3778 | +<pre>typedef int (*lua_KFunction) (lua_State *L, int status, int ctx);</pre> | |
| 3779 | + | |
| 3780 | +<p> | |
| 3781 | +Type for continuation functions (see <a href="#4.7">§4.7</a>). | |
| 3782 | + | |
| 3783 | + | |
| 3784 | + | |
| 3785 | + | |
| 3786 | + | |
| 3700 | 3787 | <hr><h3><a name="lua_len"><code>lua_len</code></a></h3><p> |
| 3701 | 3788 | <span class="apii">[-0, +1, <em>e</em>]</span> |
| 3702 | 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 | 3856 | <p> |
| 3770 | 3857 | <code>lua_load</code> uses the stack internally, |
| 3771 | −so the reader function should always leave the stack | |
| 3858 | +so the reader function must always leave the stack | |
| 3772 | 3859 | unmodified 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> | |
| 4001 | +Converts a Lua float to a Lua integer. | |
| 4002 | +This macro assumes that <code>n</code> has an integral value. | |
| 4003 | +If that value is within the range of Lua integers, | |
| 4004 | +it is converted to an integer and assigned to <code>*p</code>. | |
| 4005 | +The macro results in a boolean indicating whether the | |
| 4006 | +conversion was successful. | |
| 4007 | +(Note that this range test can be tricky to do | |
| 4008 | +correctly without this macro, | |
| 4009 | +due to roundings.) | |
| 4010 | + | |
| 4011 | + | |
| 4012 | +<p> | |
| 4013 | +This macro may evaluate its arguments more than once. | |
| 4014 | + | |
| 4015 | + | |
| 4016 | + | |
| 4017 | + | |
| 4018 | + | |
| 3910 | 4019 | <hr><h3><a name="lua_pcall"><code>lua_pcall</code></a></h3><p> |
| 3911 | 4020 | <span class="apii">[-(nargs + 1), +(nresults|1), –]</span> |
| 3912 | 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 | 4061 | <p> |
| 3953 | −The <a href="#lua_pcall"><code>lua_pcall</code></a> function returns one of the following codes | |
| 4062 | +The <a href="#lua_pcall"><code>lua_pcall</code></a> function returns one of the following constants | |
| 3954 | 4063 | (defined in <code>lua.h</code>): |
| 3955 | 4064 | |
| 3956 | 4065 | <ul> |
| @@ -3973,8 +4082,7 @@ error while running the message handler. · 16 unmodified lines | ||
| 3973 | 4082 | |
| 3974 | 4083 | <li><b><a name="pdf-LUA_ERRGCMM"><code>LUA_ERRGCMM</code></a>: </b> |
| 3975 | 4084 | error while running a <code>__gc</code> metamethod. |
| 3976 | −(This error typically has no relation with the function being called. | |
| 3977 | −It is generated by the garbage collector.) | |
| 4085 | +(This error typically has no relation with the function being called.) | |
| 3978 | 4086 | </li> |
| 3979 | 4087 | |
| 3980 | 4088 | </ul> |
| @@ -3989,7 +4097,7 @@ It is generated by the garbage collector.) · 8 unmodified lines | ||
| 3989 | 4097 | int nresults, |
| 3990 | 4098 | int errfunc, |
| 3991 | 4099 | int ctx, |
| 3992 | − lua_CFunction k);</pre> | |
| 4100 | + lua_KFunction k);</pre> | |
| 3993 | 4101 | |
| 3994 | 4102 | <p> |
| 3995 | 4103 | This function behaves exactly like <a href="#lua_pcall"><code>lua_pcall</code></a>, |
| @@ -4035,11 +4143,11 @@ it is possible to associate some values with it, · 39 unmodified lines | ||
| 4035 | 4143 | thus creating a C closure (see <a href="#4.4">§4.4</a>); |
| 4036 | 4144 | these values are then accessible to the function whenever it is called. |
| 4037 | 4145 | To associate values with a C function, |
| 4038 | −first these values should be pushed onto the stack | |
| 4146 | +first these values must be pushed onto the stack | |
| 4039 | 4147 | (when there are multiple values, the first value is pushed first). |
| 4040 | 4148 | Then <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a> |
| 4041 | 4149 | is called to create and push the C function onto the stack, |
| 4042 | −with the argument <code>n</code> telling how many values should be | |
| 4150 | +with the argument <code>n</code> telling how many values will be | |
| 4043 | 4151 | associated with the function. |
| 4044 | 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 | ||
| 4052 | 4160 | When <code>n</code> is zero, |
| 4053 | 4161 | this function creates a <em>light C function</em>, |
| 4054 | 4162 | which is just a pointer to the C function. |
| 4055 | −In that case, it never throws a memory error. | |
| 4163 | +In 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, –]</span> | |
| 4653 | +<pre>void lua_rotate (lua_State *L, int idx, int n);</pre> | |
| 4654 | + | |
| 4655 | +<p> | |
| 4656 | +Rotates the stack elements from <code>idx</code> to the top <code>n</code> positions | |
| 4657 | +in the direction of the top, for a positive <code>n</code>, | |
| 4658 | +or <code>-n</code> positions in the direction of the bottom, | |
| 4659 | +for a negative <code>n</code>. | |
| 4660 | +The absolute value of <code>n</code> must not be greater than the size | |
| 4661 | +of the slice being rotated. | |
| 4662 | + | |
| 4663 | + | |
| 4664 | + | |
| 4665 | + | |
| 4666 | + | |
| 4543 | 4667 | <hr><h3><a name="lua_setallocf"><code>lua_setallocf</code></a></h3><p> |
| 4544 | 4668 | <span class="apii">[-0, +0, –]</span> |
| 4545 | 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 | 4814 | <hr><h3><a name="lua_strtonum"><code>lua_strtonum</code></a></h3><p> |
| 4691 | 4815 | <span class="apii">[-0, +1, –]</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 | 4818 | <p> |
| 4695 | −Converts the string <code>s</code> (with length <code>len</code>) to a number and | |
| 4696 | −pushes that number into the stack. | |
| 4819 | +Converts the zero-terminated string <code>s</code> to a number, | |
| 4820 | +pushes that number into the stack, | |
| 4821 | +and returns the total size of the string | |
| 4822 | +(that is, its length plus one). | |
| 4697 | 4823 | The conversion can result in an integer or a float, |
| 4698 | 4824 | according to the lexical conventions of Lua (see <a href="#3.1">§3.1</a>). |
| 4699 | 4825 | The string may have leading and trailing spaces and a sign. |
| @@ -4969,12 +5095,17 @@ which must be one the values returned by <a href="#lua_type"><code>lua_type</cod · 269 unmodified lines | ||
| 4969 | 5095 | |
| 4970 | 5096 | |
| 4971 | 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 | 5100 | <p> |
| 4975 | 5101 | The unsigned version of <a href="#lua_Integer"><code>lua_Integer</code></a>. |
| 4976 | 5102 | |
| 4977 | 5103 | |
| 5104 | +<p> | |
| 5105 | +Lua also defines the constant <a name="pdf-LUA_MAXUNSIGNED"><code>LUA_MAXUNSIGNED</code></a>, | |
| 5106 | +with the maximum value that fits in this type. | |
| 5107 | + | |
| 5108 | + | |
| 4978 | 5109 | |
| 4979 | 5110 | |
| 4980 | 5111 | |
| @@ -5063,7 +5194,7 @@ the function calling <code>lua_yield</code>. · 82 unmodified lines | ||
| 5063 | 5194 | |
| 5064 | 5195 | <hr><h3><a name="lua_yieldk"><code>lua_yieldk</code></a></h3><p> |
| 5065 | 5196 | <span class="apii">[-?, +?, –]</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 | 5199 | <p> |
| 5069 | 5200 | Yields a coroutine. |
| @@ -5074,13 +5205,13 @@ This function should only be called as the · 4 unmodified lines | ||
| 5074 | 5205 | return expression of a C function, as follows: |
| 5075 | 5206 | |
| 5076 | 5207 | <pre> |
| 5077 | − return lua_yieldk (L, n, i, k); | |
| 5208 | + return lua_yieldk (L, n, ctx, k); | |
| 5078 | 5209 | </pre><p> |
| 5079 | 5210 | When a C function calls <a href="#lua_yieldk"><code>lua_yieldk</code></a> in that way, |
| 5080 | 5211 | the running coroutine suspends its execution, |
| 5081 | 5212 | and the call to <a href="#lua_resume"><code>lua_resume</code></a> that started this coroutine returns. |
| 5082 | 5213 | The parameter <code>nresults</code> is the number of values from the stack |
| 5083 | −that are passed as results to <a href="#lua_resume"><code>lua_resume</code></a>. | |
| 5214 | +that will be passed as results to <a href="#lua_resume"><code>lua_resume</code></a>. | |
| 5084 | 5215 | |
| 5085 | 5216 | |
| 5086 | 5217 | <p> |
| @@ -5089,11 +5220,11 @@ Lua calls the given continuation function <code>k</code> to continue · 2 unmodified lines | ||
| 5089 | 5220 | the execution of the C function that yielded (see <a href="#4.7">§4.7</a>). |
| 5090 | 5221 | This continuation function receives the same stack |
| 5091 | 5222 | from the previous function, |
| 5092 | −with the results removed and | |
| 5223 | +with the <code>n</code> results removed and | |
| 5093 | 5224 | replaced by the arguments passed to <a href="#lua_resume"><code>lua_resume</code></a>. |
| 5094 | 5225 | Moreover, |
| 5095 | −the continuation function may access the value <code>ctx</code> | |
| 5096 | −by calling <a href="#lua_getctx"><code>lua_getctx</code></a>. | |
| 5226 | +the continuation function receives the value <code>ctx</code> | |
| 5227 | +that was passed to <a href="#lua_yieldk"><code>lua_yieldk</code></a>. | |
| 5097 | 5228 | |
| 5098 | 5229 | |
| 5099 | 5230 | |
| @@ -5326,6 +5457,11 @@ numbers of the lines that are valid on the function. · 226 unmodified lines | ||
| 5326 | 5457 | (A <em>valid line</em> is a line with some associated code, |
| 5327 | 5458 | that is, a line where you can put a break point. |
| 5328 | 5459 | Non-valid lines include empty lines and comments.) |
| 5460 | + | |
| 5461 | + | |
| 5462 | +<p> | |
| 5463 | +If this option is given together with option '<code>f</code>', | |
| 5464 | +its table is pushed after the function. | |
| 5329 | 5465 | </li> |
| 5330 | 5466 | |
| 5331 | 5467 | </ul> |
| @@ -5363,7 +5499,7 @@ and returns its name. · 31 unmodified lines | ||
| 5363 | 5499 | |
| 5364 | 5500 | |
| 5365 | 5501 | <p> |
| 5366 | −In the second case, <code>ar</code> should be <code>NULL</code> and the function | |
| 5502 | +In the second case, <code>ar</code> must be <code>NULL</code> and the function | |
| 5367 | 5503 | to be inspected must be at the top of the stack. |
| 5368 | 5504 | In this case, only parameters of Lua functions are visible |
| 5369 | 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 | 5793 | <p> |
| 5658 | 5794 | Functions called <code>luaL_check*</code> |
| 5659 | −always throw an error if the check is not satisfied. | |
| 5795 | +always raise an error if the check is not satisfied. | |
| 5660 | 5796 | |
| 5661 | 5797 | |
| 5662 | 5798 | |
| @@ -6398,7 +6534,7 @@ returns 0. · 735 unmodified lines | ||
| 6398 | 6534 | Otherwise, |
| 6399 | 6535 | creates a new table to be used as a metatable for userdata, |
| 6400 | 6536 | adds to this new table the pair <code>__name = tname</code>, |
| 6401 | −adds to the registry the pair <code></code>[tname] = new table, | |
| 6537 | +adds to the registry the pair <code>[tname] = new table</code>, | |
| 6402 | 6538 | and 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 | 6898 | <p> |
| 6763 | 6899 | This function works like <a href="#luaL_checkudata"><code>luaL_checkudata</code></a>, |
| 6764 | 6900 | except that, when the test fails, |
| 6765 | −it returns <code>NULL</code> instead of throwing an error. | |
| 6901 | +it returns <code>NULL</code> instead of raising an error. | |
| 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 | 7003 | <h1>6 – <a name="6">Standard Libraries</a></h1> |
| 6866 | 7004 | |
| 6867 | 7005 | <p> |
| @@ -6939,11 +7077,15 @@ implementations for some of its facilities. · 71 unmodified lines | ||
| 6939 | 7077 | |
| 6940 | 7078 | <p> |
| 6941 | 7079 | <hr><h3><a name="pdf-assert"><code>assert (v [, message])</code></a></h3> |
| 6942 | −Issues an error when | |
| 7080 | + | |
| 7081 | + | |
| 7082 | +<p> | |
| 7083 | +Calls <a href="#pdf-error"><code>error</code></a> if | |
| 6943 | 7084 | the value of its argument <code>v</code> is false (i.e., <b>nil</b> or <b>false</b>); |
| 6944 | 7085 | otherwise, returns all its arguments. |
| 6945 | −<code>message</code> is an error message; | |
| 6946 | −when absent, it defaults to "assertion failed!" | |
| 7086 | +In case of error, | |
| 7087 | +<code>message</code> is the error object; | |
| 7088 | +when absent, it defaults to "<code>assertion failed!</code>" | |
| 6947 | 7089 | |
| 6948 | 7090 | |
| 6949 | 7091 | |
| @@ -7009,16 +7151,6 @@ returns a boolean that tells whether the collector is running · 59 unmodified lines | ||
| 7009 | 7151 | (i.e., not stopped). |
| 7010 | 7152 | </li> |
| 7011 | 7153 | |
| 7012 | −<li><b>"<code>generational</code>": </b> | |
| 7013 | −changes the collector to generational mode. | |
| 7014 | −This is an experimental feature (see <a href="#2.5">§2.5</a>). | |
| 7015 | −</li> | |
| 7016 | − | |
| 7017 | −<li><b>"<code>incremental</code>": </b> | |
| 7018 | −changes the collector to incremental mode. | |
| 7019 | −This is the default mode. | |
| 7020 | −</li> | |
| 7021 | − | |
| 7022 | 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 | 7170 | <p> |
| 7039 | 7171 | <hr><h3><a name="pdf-error"><code>error (message [, level])</code></a></h3> |
| 7040 | 7172 | Terminates the last protected function called |
| 7041 | −and returns <code>message</code> as the error message. | |
| 7173 | +and returns <code>message</code> as the error object. | |
| 7042 | 7174 | Function <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">§3.1</a>). · 330 unmodified lines | ||
| 7375 | 7507 | |
| 7376 | 7508 | <p> |
| 7377 | 7509 | When called with <code>base</code>, |
| 7378 | −then <code>e</code> should be a string to be interpreted as | |
| 7510 | +then <code>e</code> must be a string to be interpreted as | |
| 7379 | 7511 | an integer numeral in that base. |
| 7380 | 7512 | The base may be any integer between 2 and 36, inclusive. |
| 7381 | 7513 | In bases above 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> | |
| 7606 | +Returns true when the running coroutine can yield. | |
| 7607 | + | |
| 7608 | + | |
| 7609 | +<p> | |
| 7610 | +A running coroutine is yieldable if it is not the main thread and | |
| 7611 | +it is not inside a non-yieldable C function. | |
| 7612 | + | |
| 7613 | + | |
| 7614 | + | |
| 7615 | + | |
| 7469 | 7616 | <p> |
| 7470 | 7617 | <hr><h3><a name="pdf-coroutine.resume"><code>coroutine.resume (co [, val1, ···])</code></a></h3> |
| 7471 | 7618 | |
| @@ -7485,8 +7632,8 @@ as the results from the yield. · 13 unmodified lines | ||
| 7485 | 7632 | <p> |
| 7486 | 7633 | If the coroutine runs without any errors, |
| 7487 | 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 function | |
| 7489 | −(if the coroutine terminates). | |
| 7635 | +(when the coroutine yields) or any values returned by the body function | |
| 7636 | +(when the coroutine terminates). | |
| 7490 | 7637 | If there is any error, |
| 7491 | 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 | 7735 | <p> |
| 7589 | 7736 | First <code>require</code> queries <code>package.preload[modname]</code>. |
| 7590 | 7737 | If it has a value, |
| 7591 | −this value (which should be a function) is the loader. | |
| 7738 | +this value (which must be a function) is the loader. | |
| 7592 | 7739 | Otherwise <code>require</code> searches for a Lua loader using the |
| 7593 | 7740 | path stored in <a href="#pdf-package.path"><code>package.path</code></a>. |
| 7594 | 7741 | If that also fails, it searches for a C 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 | 7810 | <p> |
| 7664 | 7811 | Lua initializes the C path <a href="#pdf-package.cpath"><code>package.cpath</code></a> in the same way |
| 7665 | 7812 | it initializes the Lua path <a href="#pdf-package.path"><code>package.path</code></a>, |
| 7666 | −using the environment variable <a name="pdf-LUA_CPATH_5_2"><code>LUA_CPATH_5_2</code></a> | |
| 7813 | +using the environment variable <a name="pdf-LUA_CPATH_5_3"><code>LUA_CPATH_5_3</code></a> | |
| 7667 | 7814 | or the environment variable <a name="pdf-LUA_CPATH"><code>LUA_CPATH</code></a> |
| 7668 | 7815 | or 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 | 7889 | <p> |
| 7743 | 7890 | At start-up, Lua initializes this variable with |
| 7744 | −the value of the environment variable <a name="pdf-LUA_PATH_5_2"><code>LUA_PATH_5_2</code></a> or | |
| 7891 | +the value of the environment variable <a name="pdf-LUA_PATH_5_3"><code>LUA_PATH_5_3</code></a> or | |
| 7745 | 7892 | the environment variable <a name="pdf-LUA_PATH"><code>LUA_PATH</code></a> or |
| 7746 | 7893 | with a default path defined in <code>luaconf.h</code>, |
| 7747 | 7894 | if 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> | |
| 8125 | +Returns a string with the machine representation of float <code>n</code>, | |
| 8126 | +with given size and endianness. | |
| 8127 | +The <code>size</code> is the string "<code>f</code>" (single precision), | |
| 8128 | +"<code>d</code>" (double precision), or "<code>n</code>", | |
| 8129 | +which means the size of a <a href="#lua_Number"><code>lua_Number</code></a>; | |
| 8130 | +its default is "<code>n</code>". | |
| 8131 | +The endianness is the string "<code>l</code>" (little endian), "<code>b</code>" (big endian), | |
| 8132 | +or "<code>n</code>" (native); | |
| 8133 | +the default is the native endianness. | |
| 8134 | + | |
| 8135 | + | |
| 8136 | +<p> | |
| 8137 | +This function may not work correctly in architectures | |
| 8138 | +with mixed endian. | |
| 8139 | + | |
| 8140 | + | |
| 8141 | +<p> | |
| 8142 | +<hr><h3><a name="pdf-string.dumpint"><code>string.dumpint (n [, size [, endianness]])</code></a></h3> | |
| 8143 | +Returns a string with the two-complement representation of integer <code>n</code>, | |
| 8144 | +with <code>size</code> bytes and given endianness. | |
| 8145 | +The <code>size</code> can be any value from 1 to 12, | |
| 8146 | +or 0, which means the size of a <a href="#lua_Integer"><code>lua_Integer</code></a> | |
| 8147 | +(8 bytes in standard Lua); | |
| 8148 | +its default is zero. | |
| 8149 | +The endianness is the string "<code>l</code>" (little endian), "<code>b</code>" (big endian), | |
| 8150 | +or "<code>n</code>" (native); | |
| 8151 | +the default is the native endianness. | |
| 8152 | + | |
| 8153 | + | |
| 8154 | +<p> | |
| 8155 | +This function may not work correctly in architectures | |
| 8156 | +with mixed endian or | |
| 8157 | +that do not use a two-complement representation for integers. | |
| 8158 | + | |
| 8159 | + | |
| 8160 | + | |
| 8161 | + | |
| 7976 | 8162 | <p> |
| 7977 | 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 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> | |
| 8200 | −Returns a string with the machine representation of float <code>n</code>, | |
| 8201 | −with given size and endianess. | |
| 8202 | −The <code>size</code> is the string "<code>f</code>" (single precision), | |
| 8203 | −"<code>d</code>" (double precision), or "<code>n</code>", | |
| 8204 | −which means the size of a <a href="#lua_Number"><code>lua_Number</code></a>; | |
| 8205 | −its default is St{n}. | |
| 8206 | −The endianess is the string "<code>l</code>" (little endian), "<code>b</code>" (big endian), | |
| 8207 | −or "<code>n</code>" (native); | |
| 8208 | −the default is the native endianess. | |
| 8209 | − | |
| 8210 | − | |
| 8211 | −<p> | |
| 8212 | −(This function may not work correctly for non-native endianess | |
| 8213 | −in mixed-endian architectures.) | |
| 8214 | − | |
| 8215 | − | |
| 8216 | − | |
| 8217 | − | |
| 8218 | −<p> | |
| 8219 | −<hr><h3><a name="pdf-string.packint"><code>string.packint (n [, size [, endianess]])</code></a></h3> | |
| 8220 | −Returns a string with the machine representation of integer <code>n</code>, | |
| 8221 | −with <code>size</code> bytes and given endianess. | |
| 8222 | −The <code>size</code> can be any value from 1 to 8 or 0, | |
| 8223 | −which means the size of a <a href="#lua_Integer"><code>lua_Integer</code></a>; | |
| 8224 | −its default is zero. | |
| 8225 | −The endianess is the string "<code>l</code>" (little endian), "<code>b</code>" (big endian), | |
| 8226 | −or "<code>n</code>" (native); | |
| 8227 | −the default is the native endianess. | |
| 8228 | − | |
| 8229 | − | |
| 8230 | −<p> | |
| 8231 | −(This function may not work correctly for native endianess | |
| 8232 | −in mixed-endian architectures.) | |
| 8233 | − | |
| 8234 | − | |
| 8235 | − | |
| 8236 | − | |
| 8237 | 8384 | <p> |
| 8238 | 8385 | <hr><h3><a name="pdf-string.rep"><code>string.rep (s, n [, sep])</code></a></h3> |
| 8239 | 8386 | Returns a string that is the concatenation of <code>n</code> copies of |
| @@ -8279,19 +8426,19 @@ the function returns the empty string. · 39 unmodified lines | ||
| 8279 | 8426 | |
| 8280 | 8427 | |
| 8281 | 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> | |
| 8283 | 8430 | Reads the machine representation of a float starting at position |
| 8284 | 8431 | <code>pos</code> in string <code>s</code> and returns that number. |
| 8285 | −See <a href="#pdf-string.packfloat"><code>string.packfloat</code></a> for details about <code>size</code> and <code>endianess</code>. | |
| 8432 | +See <a href="#pdf-string.dumpfloat"><code>string.dumpfloat</code></a> for details about <code>size</code> and <code>endianness</code>. | |
| 8286 | 8433 | |
| 8287 | 8434 | |
| 8288 | 8435 | |
| 8289 | 8436 | |
| 8290 | 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> | |
| 8292 | 8439 | Reads the machine representation of an integer starting at position |
| 8293 | 8440 | <code>pos</code> in string <code>s</code> and returns that integer. |
| 8294 | −See <a href="#pdf-string.packint"><code>string.packint</code></a> for details about <code>size</code> and <code>endianess</code>. | |
| 8441 | +See <a href="#pdf-string.dumpint"><code>string.dumpint</code></a> for details about <code>size</code> and <code>endianness</code>. | |
| 8295 | 8442 | |
| 8296 | 8443 | |
| 8297 | 8444 | <p> |
| @@ -8517,7 +8664,7 @@ such as character classification, is outside its scope. · 219 unmodified lines | ||
| 8517 | 8664 | |
| 8518 | 8665 | <p> |
| 8519 | 8666 | Unless stated otherwise, |
| 8520 | −all functions that expect a byte index as a parameter | |
| 8667 | +all functions that expect a byte position as a parameter | |
| 8521 | 8668 | assume that the given position is either the start of a byte sequence |
| 8522 | 8669 | or one plus the length of the subject string. |
| 8523 | 8670 | As 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 | 8721 | <p> |
| 8575 | −<hr><h3><a name="pdf-utf8.len"><code>utf8.len (s [, i])</code></a></h3> | |
| 8576 | −Returns the number of UTF-8 characters in string <code>s</code>, | |
| 8577 | −starting from position <code>i</code>. | |
| 8578 | −The default for <code>i</code> is 1. | |
| 8579 | −Returns <b>nil</b> if the sufix of <code>s</code> starting at <code>i</code> | |
| 8580 | −contains any invalid byte sequence. | |
| 8722 | +<hr><h3><a name="pdf-utf8.len"><code>utf8.len (s [, i [, j]])</code></a></h3> | |
| 8723 | +Returns the number of UTF-8 characters in string <code>s</code> | |
| 8724 | +that start between positions <code>i</code> and @{j} (both inclusive). | |
| 8725 | +The default for <code>i</code> is 1 and for <code>j</code> is -1. | |
| 8726 | +If it finds any invalid byte sequence, | |
| 8727 | +returns <b>nil</b> plus the position of the first invalid byte. | |
| 8581 | 8728 | |
| 8582 | 8729 | |
| 8583 | 8730 | |
| 8584 | 8731 | |
| 8585 | 8732 | <p> |
| 8586 | 8733 | <hr><h3><a name="pdf-utf8.offset"><code>utf8.offset (s, n [, i])</code></a></h3> |
| 8587 | −Returns the byte index where the encoding of the | |
| 8588 | −<code>n</code>-th character of <code>s</code> starts, | |
| 8589 | −counting from position <code>i</code>. | |
| 8734 | +Returns the position (in bytes) where the encoding of the | |
| 8735 | +<code>n</code>-th character of <code>s</code> | |
| 8736 | +(counting from position <code>i</code>) starts. | |
| 8590 | 8737 | A negative <code>n</code> gets characters before position <code>i</code>. |
| 8591 | −The default for <code>i</code> is 1. | |
| 8592 | −Returns <b>nil</b> if the subject does not have such character. | |
| 8738 | +The default for <code>i</code> is 1 when <code>n</code> is non-negative | |
| 8739 | +and <code>#s + 1</code> otherwise, | |
| 8740 | +so that <code>utf8.offset(s, -n)</code> gets the offset of the | |
| 8741 | +<code>n</code>-th character from the end of the string. | |
| 8742 | +If the specified character is not in the subject | |
| 8743 | +or right after its end, | |
| 8744 | +the function returns <b>nil</b>. | |
| 8593 | 8745 | |
| 8594 | 8746 | |
| 8595 | 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 | 8769 | <p> |
| 8618 | 8770 | Remember that, whenever an operation needs the length of a table, |
| 8619 | −the table should be a proper sequence | |
| 8771 | +the table must be a proper sequence | |
| 8620 | 8772 | or have a <code>__len</code> metamethod (see <a href="#3.4.7">§3.4.7</a>). |
| 8621 | 8773 | All functions ignore non-numeric keys |
| 8622 | 8774 | in tables given as arguments. |
| @@ -8739,10 +8891,15 @@ By default, <code>i</code> is 1 and <code>j</code> is <code>#list</code>. · 116 unmodified lines | ||
| 8739 | 8891 | <h2>6.7 – <a name="6.7">Mathematical Functions</a></h2> |
| 8740 | 8892 | |
| 8741 | 8893 | <p> |
| 8742 | −This library is an interface to the standard C math library. | |
| 8743 | −It provides all its functions inside the table <a name="pdf-math"><code>math</code></a>. | |
| 8744 | −Unless stated otherwise, | |
| 8745 | −all functions in this library operate with and return floats. | |
| 8894 | +This library provides basic mathematical functions. | |
| 8895 | +It provides all its functions and constants inside the table <a name="pdf-math"><code>math</code></a>. | |
| 8896 | +Functions with the annotation "<code>integer/float</code>" give | |
| 8897 | +integer results for integer arguments | |
| 8898 | +and float results for float (or mixed) arguments. | |
| 8899 | +Rounding functions | |
| 8900 | +(<a href="#pdf-math.ceil"><code>math.ceil</code></a>, <a href="#pdf-math.floor"><code>math.floor</code></a>, and <a href="#pdf-math.modf"><code>math.modf</code></a>) | |
| 8901 | +return an integer when the result fits in the range of an integer, | |
| 8902 | +otherwise they return a float. | |
| 8746 | 8903 | |
| 8747 | 8904 | |
| 8748 | 8905 | <p> |
| @@ -8750,7 +8907,7 @@ all functions in this library operate with and return floats. · 1 unmodified line | ||
| 8750 | 8907 | |
| 8751 | 8908 | |
| 8752 | 8909 | <p> |
| 8753 | −Returns the absolute value of <code>x</code>. | |
| 8910 | +Returns 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 | 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 | 8939 | <p> |
| 8783 | −Returns 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> | |
| 8793 | 8941 | Returns the arc tangent of <code>y/x</code> (in radians), |
| 8794 | 8942 | but uses the signs of both parameters to find the |
| 8795 | 8943 | quadrant of the result. |
| 8796 | 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 | 8947 | <p> |
| 8806 | −Returns the smallest integer larger than or equal to <code>x</code>. | |
| 8948 | +The default value for <code>x</code> is 1, | |
| 8949 | +so that the call <code>math.atan(y)</code> | |
| 8950 | +returns the arc tangent of <code>y</code>. | |
| 8807 | 8951 | |
| 8808 | 8952 | |
| 8809 | 8953 | |
| 8810 | 8954 | |
| 8811 | 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 | 8959 | <p> |
| 8816 | −Returns the cosine of <code>x</code> (assumed to be in radians). | |
| 8960 | +Returns the smaller integral value larger than or equal to <code>x</code>. | |
| 8817 | 8961 | |
| 8818 | 8962 | |
| 8819 | 8963 | |
| 8820 | 8964 | |
| 8821 | 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 | 8969 | <p> |
| 8826 | −Returns the hyperbolic cosine of <code>x</code>. | |
| 8970 | +Returns 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 | 8979 | <p> |
| 8836 | −Returns the angle <code>x</code> (given in radians) in degrees. | |
| 8837 | − | |
| 8838 | − | |
| 8839 | − | |
| 8840 | − | |
| 8841 | −<p> | |
| 8842 | −<hr><h3><a name="pdf-math.exp"><code>math.exp (x)</code></a></h3> | |
| 8843 | − | |
| 8844 | − | |
| 8845 | −<p> | |
| 8846 | −Returns the value <em>e<sup>x</sup></em>. | |
| 8980 | +Converts the angle <code>x</code> from radians to degrees. | |
| 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 | 8989 | <p> |
| 8856 | −Returns a float with the largest | |
| 8857 | −integral value smaller than or equal to <code>x</code>. | |
| 8990 | +Returns the largest integral value smaller than or equal to <code>x</code>. | |
| 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 | 8999 | <p> |
| 8867 | 9000 | Returns the remainder of the division of <code>x</code> by <code>y</code> |
| 8868 | −that rounds the quotient towards zero. | |
| 8869 | − | |
| 8870 | − | |
| 8871 | − | |
| 8872 | − | |
| 8873 | −<p> | |
| 8874 | −<hr><h3><a name="pdf-math.frexp"><code>math.frexp (x)</code></a></h3> | |
| 8875 | − | |
| 8876 | − | |
| 8877 | −<p> | |
| 8878 | −Returns <code>m</code> and <code>e</code> such that <em>x = m2<sup>e</sup></em>, | |
| 8879 | −<code>e</code> is an integer and the absolute value of <code>m</code> is | |
| 8880 | −in the range <em>[0.5, 1)</em> | |
| 8881 | −(or zero when <code>x</code> is zero). | |
| 9001 | +that rounds the quotient towards zero. (integer/float) | |
| 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 | 9010 | <p> |
| 8891 | −The value <code>HUGE_VAL</code>, | |
| 8892 | −a value larger than or equal to any other numerical value. | |
| 9011 | +The float value <code>HUGE_VAL</code>, | |
| 9012 | +a value larger than any other numerical value. | |
| 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> | |
| 8914 | −Returns <em>m2<sup>e</sup></em> (<code>e</code> should be an integer). | |
| 8915 | − | |
| 8916 | − | |
| 8917 | − | |
| 8918 | − | |
| 8919 | 9029 | <p> |
| 8920 | 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 | 9045 | <p> |
| 8936 | −Returns the maximum value among its arguments. | |
| 9046 | +Returns the argument with the maximum value, | |
| 9047 | +according to the Lua operator <code><</code>. (integer/float) | |
| 8937 | 9048 | |
| 8938 | 9049 | |
| 8939 | 9050 | |
| 8940 | 9051 | |
| 8941 | 9052 | <p> |
| 8942 | −<hr><h3><a name="pdf-math.min"><code>math.min (x, ···)</code></a></h3> | |
| 9053 | +<hr><h3><a name="pdf-math.maxinteger"><code>math.maxinteger</code></a></h3> | |
| 9054 | +An integer with the maximum value for an integer. | |
| 8943 | 9055 | |
| 8944 | 9056 | |
| 8945 | −<p> | |
| 8946 | −Returns the minimum value among its arguments. | |
| 8947 | 9057 | |
| 8948 | 9058 | |
| 9059 | +<p> | |
| 9060 | +<hr><h3><a name="pdf-math.min"><code>math.min (x, ···)</code></a></h3> | |
| 8949 | 9061 | |
| 8950 | 9062 | |
| 8951 | 9063 | <p> |
| 8952 | −<hr><h3><a name="pdf-math.modf"><code>math.modf (x)</code></a></h3> | |
| 9064 | +Returns the argument with the minimum value, | |
| 9065 | +according to the Lua operator <code><</code>. (integer/float) | |
| 9066 | + | |
| 9067 | + | |
| 8953 | 9068 | |
| 8954 | 9069 | |
| 8955 | 9070 | <p> |
| 8956 | −Returns two numbers, | |
| 8957 | −the integral part of <code>x</code> and the fractional part of <code>x</code>. | |
| 9071 | +<hr><h3><a name="pdf-math.mininteger"><code>math.mininteger</code></a></h3> | |
| 9072 | +An integer with the minimum value for an integer. | |
| 8958 | 9073 | |
| 8959 | 9074 | |
| 8960 | 9075 | |
| 8961 | 9076 | |
| 8962 | 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 | 9081 | <p> |
| 8967 | −The value of <em>π</em>. | |
| 9082 | +Returns the integral part of <code>x</code> and the fractional part of <code>x</code>. | |
| 9083 | +Its second result is always a float. | |
| 8968 | 9084 | |
| 8969 | 9085 | |
| 8970 | 9086 | |
| 8971 | 9087 | |
| 8972 | 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 | 9092 | <p> |
| 8977 | −Returns <em>x<sup>y</sup></em>. | |
| 8978 | −(You can also use the expression <code>x^y</code> to compute this value.) | |
| 9093 | +The value of <em>π</em>. | |
| 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 | 9102 | <p> |
| 8988 | −Returns the angle <code>x</code> (given in degrees) in radians. | |
| 9103 | +Converts 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 | 9109 | <hr><h3><a name="pdf-math.random"><code>math.random ([m [, n]])</code></a></h3> |
| 8995 | 9110 | |
| 8996 | 9111 | |
| 8997 | −<p> | |
| 8998 | −This function is an interface to the simple | |
| 8999 | −pseudo-random generator function <code>rand</code> provided by Standard C. | |
| 9000 | −(No guarantees can be given for its statistical properties.) | |
| 9001 | − | |
| 9002 | − | |
| 9003 | 9112 | <p> |
| 9004 | 9113 | When called without arguments, |
| 9005 | −returns a uniform pseudo-random float | |
| 9006 | −in the range <em>[0,1)</em>. | |
| 9114 | +returns a pseudo-random float with uniform distribution | |
| 9115 | +in the range <em>[0,1)</em>. | |
| 9007 | 9116 | When called with two integers <code>m</code> and <code>n</code>, |
| 9008 | −<code>math.random</code> returns a uniform pseudo-random | |
| 9009 | −integer in the range <em>[m, n]</em>. | |
| 9117 | +<code>math.random</code> returns a pseudo-random integer | |
| 9118 | +with uniform distribution in the range <em>[m, n]</em>. | |
| 9119 | +(The interval size must fit in a Lua integer.) | |
| 9010 | 9120 | The call <code>math.random(n)</code> is equivalent to <code>math.random(1,n)</code>. |
| 9011 | 9121 | |
| 9012 | 9122 | |
| 9123 | +<p> | |
| 9124 | +This function is an interface to the underling | |
| 9125 | +pseudo-random generator function provided by C. | |
| 9126 | +No guarantees can be given for its statistical properties. | |
| 9127 | + | |
| 9128 | + | |
| 9013 | 9129 | |
| 9014 | 9130 | |
| 9015 | 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> | |
| 9042 | −Returns the hyperbolic sine of <code>x</code>. | |
| 9043 | − | |
| 9044 | − | |
| 9045 | − | |
| 9046 | − | |
| 9047 | 9153 | <p> |
| 9048 | 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> | |
| 9073 | −Returns the hyperbolic tangent of <code>x</code>. | |
| 9074 | − | |
| 9075 | − | |
| 9076 | − | |
| 9077 | − | |
| 9078 | 9174 | <p> |
| 9079 | 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 | ||
| 9337 | 9433 | each time it is called, |
| 9338 | 9434 | reads the file according to the given formats. |
| 9339 | 9435 | When no format is given, |
| 9340 | −uses "*l" as a default. | |
| 9436 | +uses "<code>l</code>" as a default. | |
| 9341 | 9437 | As an example, the construction |
| 9342 | 9438 | |
| 9343 | 9439 | <pre> |
| @@ -9376,30 +9472,33 @@ The available formats are · 32 unmodified lines | ||
| 9376 | 9472 | |
| 9377 | 9473 | <ul> |
| 9378 | 9474 | |
| 9379 | −<li><b>"<code>*n</code>": </b> | |
| 9380 | −reads a number and returns it as a float. | |
| 9381 | −If the input has only a prefix of a valid number | |
| 9382 | −(e.g., "<code>0x</code>" or "<code>3.4e-</code>"), | |
| 9383 | −this format can read the entire prefix | |
| 9384 | −even when returning <b>nil</b>. | |
| 9475 | +<li><b>"<code>n</code>": </b> | |
| 9476 | +reads a numeral and returns it as a float or an integer, | |
| 9477 | +following the lexical conventions of Lua. | |
| 9478 | +(The numeral may have leading spaces and a sign.) | |
| 9479 | +This format always reads the longest input sequence that | |
| 9480 | +is a valid prefix for a number; | |
| 9481 | +if that prefix does not form a valid number | |
| 9482 | +(e.g., an empty string, "<code>0x</code>", or "<code>3.4e-</code>"), | |
| 9483 | +it is discarded and the function returns <b>nil</b>. | |
| 9385 | 9484 | </li> |
| 9386 | 9485 | |
| 9387 | −<li><b>"<code>*i</code>": </b> | |
| 9486 | +<li><b>"<code>i</code>": </b> | |
| 9388 | 9487 | reads an integral number and returns it as an integer. |
| 9389 | 9488 | </li> |
| 9390 | 9489 | |
| 9391 | −<li><b>"<code>*a</code>": </b> | |
| 9490 | +<li><b>"<code>a</code>": </b> | |
| 9392 | 9491 | reads the whole file, starting at the current position. |
| 9393 | 9492 | On end of file, it returns the empty string. |
| 9394 | 9493 | </li> |
| 9395 | 9494 | |
| 9396 | −<li><b>"<code>*l</code>": </b> | |
| 9495 | +<li><b>"<code>l</code>": </b> | |
| 9397 | 9496 | reads the next line skipping the end of line, |
| 9398 | 9497 | returning <b>nil</b> on end of file. |
| 9399 | 9498 | This is the default format. |
| 9400 | 9499 | </li> |
| 9401 | 9500 | |
| 9402 | −<li><b>"<code>*L</code>": </b> | |
| 9501 | +<li><b>"<code>L</code>": </b> | |
| 9403 | 9502 | reads the next line keeping the end-of-line character (if present), |
| 9404 | 9503 | returning <b>nil</b> on end of file. |
| 9405 | 9504 | </li> |
| @@ -9413,7 +9512,7 @@ or <b>nil</b> on end of file. · 7 unmodified lines | ||
| 9413 | 9512 | </li> |
| 9414 | 9513 | |
| 9415 | 9514 | </ul><p> |
| 9416 | −The formats "<code>*l</code>" and "<code>*L</code>" should be used only for text files. | |
| 9515 | +The formats "<code>l</code>" and "<code>L</code>" should be used only for text files. | |
| 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 | 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 | 9727 | <p> |
| @@ -9799,6 +9898,32 @@ thread to operate over. · 170 unmodified lines | ||
| 9799 | 9898 | The 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> | |
| 9906 | +Returns the size of the underlying representation of a given C type, | |
| 9907 | +according to the following table: | |
| 9908 | +<table border="1"> | |
| 9909 | +<tr><td>'<code>I</code>'</td><td><code>lua_Integer</code></td></tr> | |
| 9910 | +<tr><td>'<code>F</code>'</td><td><code>lua_Number</code></td></tr> | |
| 9911 | +<tr><td>'<code>b</code>'</td><td><code>byte</code></td></tr> | |
| 9912 | +<tr><td>'<code>d</code>'</td><td><code>double</code></td></tr> | |
| 9913 | +<tr><td>'<code>f</code>'</td><td><code>float</code></td></tr> | |
| 9914 | +<tr><td>'<code>h</code>'</td><td><code>short int</code></td></tr> | |
| 9915 | +<tr><td>'<code>i</code>'</td><td><code>int</code></td></tr> | |
| 9916 | +<tr><td>'<code>l</code>'</td><td><code>long int</code></td></tr> | |
| 9917 | +<tr><td>'<code>z</code>'</td><td><code>size_t</code></td></tr> | |
| 9918 | +</table> | |
| 9919 | +For all options except '<code>b</code>', | |
| 9920 | +the size is given in number of bytes; | |
| 9921 | +for option '<code>b</code>', | |
| 9922 | +the size is its number of bits. | |
| 9923 | + | |
| 9924 | + | |
| 9925 | + | |
| 9926 | + | |
| 9802 | 9927 | <p> |
| 9803 | 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 | 10025 | <p> |
| 9901 | −Variable names starting with '<code>(</code>' (open parenthesis) | |
| 10026 | +Variable names starting with '<code>(</code>' (open parenthesis) | |
| 9902 | 10027 | represent variables with no known names |
| 9903 | 10028 | (internal variables like loop control variables, |
| 9904 | 10029 | and variables from chunks saved without debug information). |
| @@ -9943,7 +10068,7 @@ The function returns <b>nil</b> if there is no upvalue with the given index. · 38 unmodified lines | ||
| 9943 | 10068 | |
| 9944 | 10069 | |
| 9945 | 10070 | <p> |
| 9946 | −Variable names starting with '<code>(</code>' (open parenthesis) | |
| 10071 | +Variable names starting with '<code>(</code>' (open parenthesis) | |
| 9947 | 10072 | represent variables with no known names |
| 9948 | 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> | |
| 9970 | −Returns the number of bits in the underlying representation | |
| 9971 | −of an integer (if <code>t</code> is "<code>i</code>") | |
| 9972 | −or a float (if <code>t</code> is "<code>f</code>"). | |
| 9973 | − | |
| 9974 | − | |
| 9975 | − | |
| 9976 | − | |
| 9977 | 10090 | <p> |
| 9978 | 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 | 10283 | <p> |
| 10171 | 10284 | When called without option <code>-E</code>, |
| 10172 | −the interpreter checks for an environment variable <a name="pdf-LUA_INIT_5_2"><code>LUA_INIT_5_2</code></a> | |
| 10285 | +the interpreter checks for an environment variable <a name="pdf-LUA_INIT_5_3"><code>LUA_INIT_5_3</code></a> | |
| 10173 | 10286 | (or <a name="pdf-LUA_INIT"><code>LUA_INIT</code></a> if it is not defined) |
| 10174 | 10287 | before running any argument. |
| 10175 | 10288 | If the variable content has the format <code>@<em>filename</em></code>, |
| @@ -10200,37 +10313,46 @@ and finally run the file <code>script.lua</code> with no arguments. · 24 unmodified lines | ||
| 10200 | 10313 | |
| 10201 | 10314 | |
| 10202 | 10315 | <p> |
| 10203 | −Before starting to run the script, | |
| 10204 | −<code>lua</code> collects all arguments in the command line | |
| 10316 | +Before running any code, | |
| 10317 | +<code>lua</code> collects all command-line arguments | |
| 10205 | 10318 | in a global table called <code>arg</code>. |
| 10206 | −The script name is stored at index 0, | |
| 10319 | +The script name goes to index 0, | |
| 10207 | 10320 | the first argument after the script name goes to index 1, |
| 10208 | 10321 | and so on. |
| 10209 | 10322 | Any arguments before the script name |
| 10210 | −(that is, the interpreter name plus the options) | |
| 10323 | +(that is, the interpreter name plus its options) | |
| 10211 | 10324 | go to negative indices. |
| 10212 | 10325 | For instance, in the call |
| 10213 | 10326 | |
| 10214 | 10327 | <pre> |
| 10215 | 10328 | $ lua -la b.lua t1 t2 |
| 10216 | 10329 | </pre><p> |
| 10217 | −the interpreter first runs the file <code>a.lua</code>, | |
| 10218 | −then creates a table | |
| 10330 | +the table is like this: | |
| 10219 | 10331 | |
| 10220 | 10332 | <pre> |
| 10221 | 10333 | arg = { [-2] = "lua", [-1] = "-la", |
| 10222 | 10334 | [0] = "b.lua", |
| 10223 | 10335 | [1] = "t1", [2] = "t2" } |
| 10224 | 10336 | </pre><p> |
| 10225 | −and finally runs the file <code>b.lua</code>. | |
| 10226 | −The script is called with <code>arg[1]</code>, <code>arg[2]</code>, ... | |
| 10227 | −as arguments; | |
| 10228 | −it can also access these arguments with the vararg expression '<code>...</code>'. | |
| 10337 | +If there is no script in the call, | |
| 10338 | +the interpreter name goes to index 0, | |
| 10339 | +followed by the other arguments. | |
| 10340 | +For instance, the call | |
| 10341 | + | |
| 10342 | +<pre> | |
| 10343 | + $ lua -e "print(arg[1])" | |
| 10344 | +</pre><p> | |
| 10345 | +will print "<code>-e</code>". | |
| 10229 | 10346 | |
| 10230 | 10347 | |
| 10231 | 10348 | <p> |
| 10232 | 10349 | In interactive mode, |
| 10233 | −if you write an incomplete statement, | |
| 10350 | +Lua repeatedly prompts and waits for a line. | |
| 10351 | +After reading a line, | |
| 10352 | +Lua first try to interpret the line as an expression. | |
| 10353 | +If it succeeds, it prints its value. | |
| 10354 | +Otherwise, it interprets the line as a statement. | |
| 10355 | +If you write an incomplete statement, | |
| 10234 | 10356 | the interpreter waits for its completion |
| 10235 | 10357 | by issuing a different prompt. |
| 10236 | 10358 | |
| @@ -10301,6 +10423,12 @@ Always recompile clients of the Lua API when · 64 unmodified lines | ||
| 10301 | 10423 | using a new version. |
| 10302 | 10424 | |
| 10303 | 10425 | |
| 10426 | +<p> | |
| 10427 | +Similarly, Lua versions can always change the internal representation | |
| 10428 | +of precompiled chunks; | |
| 10429 | +precompiled chunks are never compatible between different Lua versions. | |
| 10430 | + | |
| 10431 | + | |
| 10304 | 10432 | <p> |
| 10305 | 10433 | The standard paths in the official distribution may |
| 10306 | 10434 | change between versions. |
| @@ -10325,8 +10453,8 @@ You can fix these differences by forcing a number to be a float · 18 unmodified lines | ||
| 10325 | 10453 | in particular writing constants with an ending <code>.0</code> |
| 10326 | 10454 | or using <code>x = x + 0.0</code> to convert a variable. |
| 10327 | 10455 | (This recommendation is only for a quick fix |
| 10328 | −for an occasional incompatibility, | |
| 10329 | −not as a general guideline for good programming. | |
| 10456 | +for an occasional incompatibility; | |
| 10457 | +it is not a general guideline for good programming. | |
| 10330 | 10458 | For good programming, |
| 10331 | 10459 | use floats where you need floats |
| 10332 | 10460 | and integers where you need integers.) |
| @@ -10335,7 +10463,22 @@ and integers where you need integers.) · 2 unmodified lines | ||
| 10335 | 10463 | <p> |
| 10336 | 10464 | Although not formally an incompatibility, |
| 10337 | 10465 | the proper differentiation between floats and integers |
| 10338 | −can sometimes have a big impact on performance. | |
| 10466 | +have an impact on performance. | |
| 10467 | +</li> | |
| 10468 | + | |
| 10469 | +<li> | |
| 10470 | +The conversion of a float to a string now adds a <code>.0</code> suffix | |
| 10471 | +to the result if it looks like an integer. | |
| 10472 | +(For instance, the float 2.0 will be printed as <code>2.0</code>, | |
| 10473 | +not as <code>2</code>.) | |
| 10474 | +You should always use an explicit format | |
| 10475 | +when you need a specific format for numbers. | |
| 10476 | + | |
| 10477 | + | |
| 10478 | +<p> | |
| 10479 | +(Formally this is not an incompatibility, | |
| 10480 | +because Lua does not specify how numbers are formatted as strings, | |
| 10481 | +but some programs assumed a specific format.) | |
| 10339 | 10482 | </li> |
| 10340 | 10483 | |
| 10341 | 10484 | <li> |
| @@ -10351,24 +10494,31 @@ The generational mode for the garbage collector was removed. · 9 unmodified lines | ||
| 10351 | 10494 | <h2>8.2 – <a name="8.2">Changes in the Libraries</a></h2> |
| 10352 | 10495 | <ul> |
| 10353 | 10496 | |
| 10354 | −<li> | |
| 10355 | −The conversion of a float to a string through <a href="#pdf-tostring"><code>tostring</code></a> | |
| 10356 | −(which is used by <a href="#pdf-print"><code>print</code></a>) now adds a <code>.0</code> suffix if the | |
| 10357 | −number has an integral value. | |
| 10358 | −(For instance, the float 2.0 will be printed as <code>2.0</code>, | |
| 10359 | −not as <code>2</code>.) | |
| 10360 | −You should always use an explicit format | |
| 10361 | −when you need a specific format for numbers. | |
| 10362 | −</li> | |
| 10363 | − | |
| 10364 | 10497 | <li> |
| 10365 | 10498 | The <code>bit32</code> library has been deprecated. |
| 10366 | 10499 | It is easy to require a compatible external library or, |
| 10367 | 10500 | better yet, to replace its functions with appropriate bitwise operations. |
| 10368 | −(Just keep in mind that <code>bit32</code> operates on 32-bit integers, | |
| 10501 | +(Keep in mind that <code>bit32</code> operates on 32-bit integers, | |
| 10369 | 10502 | while the bitwise operators in Standard Lua operate on 64-bit integers.) |
| 10370 | 10503 | </li> |
| 10371 | 10504 | |
| 10505 | +<li> | |
| 10506 | +Option names in <a href="#pdf-io.read"><code>io.read</code></a> do not have a starting '<code>*</code>' anymore. | |
| 10507 | +For compatibility, Lua will continue to ignore this character. | |
| 10508 | +</li> | |
| 10509 | + | |
| 10510 | +<li> | |
| 10511 | +The following functions were deprecated in the mathematical library: | |
| 10512 | +<code>atan2</code>, <code>cosh</code>, <code>sinh</code>, <code>tanh</code>, <code>pow</code>, | |
| 10513 | +<code>frexp</code>, and <code>ldexp</code>. | |
| 10514 | +You can replace <code>math.pow(x,y)</code> with <code>x^y</code>; | |
| 10515 | +you can replace <code>math.atan2</code> with <code>math.atan</code>, | |
| 10516 | +which now accepts two parameters; | |
| 10517 | +you can replace <code>math.ldexp(x,exp)</code> with <code>x * 2.0^exp</code>. | |
| 10518 | +For the other operations, | |
| 10519 | +the best choice is to use an external library. | |
| 10520 | +</li> | |
| 10521 | + | |
| 10372 | 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 | 10530 | <ul> |
| 10381 | 10531 | |
| 10382 | 10532 | <li> |
| 10383 | −No changes (yet). | |
| 10533 | +Continuation functions now receive as parameters what they needed | |
| 10534 | +to get through <code>lua_getctx</code>, | |
| 10535 | +so <code>lua_getctx</code> has been removed. | |
| 10536 | +Adapt your code accordingly. | |
| 10537 | +</li> | |
| 10538 | + | |
| 10539 | +<li> | |
| 10540 | +Function <a href="#lua_dump"><code>lua_dump</code></a> has an extra parameter, <code>strip</code>. | |
| 10541 | +Use 0 as the value of this parameter to get the old behavior. | |
| 10384 | 10542 | </li> |
| 10385 | 10543 | |
| 10386 | 10544 | </ul> |
| @@ -10456,11 +10614,12 @@ Here is the complete syntax of Lua in extended BNF. · 69 unmodified lines | ||
| 10456 | 10614 | |
| 10457 | 10615 | fieldsep ::= ‘<b>,</b>’ | ‘<b>;</b>’ |
| 10458 | 10616 | |
| 10459 | − binop ::= ‘<b>+</b>’ | ‘<b>-</b>’ | ‘<b>*</b>’ | ‘<b>/</b>’ | ‘<b>^</b>’ | ‘<b>%</b>’ | ‘<b>..</b>’ | | |
| 10617 | + binop ::= ‘<b>+</b>’ | ‘<b>-</b>’ | ‘<b>*</b>’ | ‘<b>/</b>’ | ‘<b>//</b>’ | ‘<b>^</b>’ | ‘<b>%</b>’ | | |
| 10618 | + ‘<b>&</b>’ | ‘<b>~</b>’ | ‘<b>|</b>’ | ‘<b>>></b>’ | ‘<b><<</b>’ | ‘<b>..</b>’ | | |
| 10460 | 10619 | ‘<b><</b>’ | ‘<b><=</b>’ | ‘<b>></b>’ | ‘<b>>=</b>’ | ‘<b>==</b>’ | ‘<b>~=</b>’ | |
| 10461 | 10620 | <b>and</b> | <b>or</b> |
| 10462 | 10621 | |
| 10463 | − unop ::= ‘<b>-</b>’ | <b>not</b> | ‘<b>#</b>’ | |
| 10622 | + unop ::= ‘<b>-</b>’ | <b>not</b> | ‘<b>#</b>’ | ‘<b>~</b>’ | |
| 10464 | 10623 | |
| 10465 | 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 | 10635 | <HR> |
| 10476 | 10636 | <SMALL CLASS="footer"> |
| 10477 | 10637 | Last update: |
| 10478 | −Fri Mar 21 17:33:56 BRT 2014 | |
| 10638 | +Thu Jun 19 17:13:19 BRT 2014 | |
| 10479 | 10639 | </SMALL> |
| 10480 | 10640 | <!-- |
| 10481 | −Last change: updated for Lua 5.3.0 (work2) | |
| 10641 | +Last change: updated for Lua 5.3.0 (work3) | |
| 10482 | 10642 | --> |
| 10483 | 10643 | |
| 10484 | 10644 | </body></html> |
| @@ -31,7 +31,7 @@ tt, kbd, code { · 30 unmodified lines | ||
| 31 | 31 | <HR> |
| 32 | 32 | <H1> |
| 33 | 33 | <A HREF="http://www.lua.org/"><IMG SRC="logo.gif" ALT="Lua" BORDER=0></A> |
| 34 | −Welcome to Lua 5.3 (work2) | |
| 34 | +Welcome to Lua 5.3 (work3) | |
| 35 | 35 | </H1> |
| 36 | 36 | |
| 37 | 37 | <P> |
| @@ -102,8 +102,8 @@ If you don't have the time or the inclination to compile Lua yourself, · 64 unmodified lines | ||
| 102 | 102 | get a binary from |
| 103 | 103 | <A HREF="http://lua-users.org/wiki/LuaBinaries">LuaBinaries</A>. |
| 104 | 104 | Try also |
| 105 | −<A HREF="http://luaforwindows.luaforge.net/">Lua for Windows</A>, | |
| 106 | −an easy-to-use distribution of Lua that includes many useful libraries. | |
| 105 | +<A HREF="http://luadist.org/">LuaDist</A>, | |
| 106 | +a multi-platform distribution of Lua that includes batteries. | |
| 107 | 107 | |
| 108 | 108 | <H3>Building Lua</H3> |
| 109 | 109 | |
| @@ -114,8 +114,8 @@ Here are the details. · 4 unmodified lines | ||
| 114 | 114 | <OL> |
| 115 | 115 | <LI> |
| 116 | 116 | Open a terminal window and move to |
| 117 | −the top-level directory, which is named <TT>lua-5.3.0</TT>. | |
| 118 | −The Makefile there controls both the build process and the installation process. | |
| 117 | +the top-level directory, which is named <TT>lua-5.3.0-work3</TT>. | |
| 118 | +The <TT>Makefile</TT> there controls both the build process and the installation process. | |
| 119 | 119 | <P> |
| 120 | 120 | <LI> |
| 121 | 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 | 140 | <P> |
| 141 | 141 | <LI> |
| 142 | 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 | 144 | </OL> |
| 145 | 145 | <P> |
| 146 | 146 | If you're running Linux and get compilation errors, |
| @@ -152,7 +152,7 @@ then try "<KBD>make linux MYLIBS=-ltermcap</KBD>". · 5 unmodified lines | ||
| 152 | 152 | <P> |
| 153 | 153 | Once you have built Lua, you may want to install it in an official |
| 154 | 154 | place in your system. In this case, do "<KBD>make install</KBD>". The official |
| 155 | − place and the way to install files are defined in the Makefile. You'll | |
| 155 | + place and the way to install files are defined in the <TT>Makefile</TT>. You'll | |
| 156 | 156 | probably need the right permissions to install files. |
| 157 | 157 | |
| 158 | 158 | <P> |
| @@ -162,11 +162,13 @@ then try "<KBD>make linux MYLIBS=-ltermcap</KBD>". · 3 unmodified lines | ||
| 162 | 162 | <P> |
| 163 | 163 | To install Lua locally, do "<KBD>make local</KBD>". |
| 164 | 164 | 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 | 166 | and install Lua as listed below. |
| 167 | 167 | |
| 168 | 168 | To install Lua locally, but in some other directory, do |
| 169 | 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 | 173 | <DL CLASS="display"> |
| 172 | 174 | <DT> |
| @@ -176,7 +178,7 @@ then try "<KBD>make linux MYLIBS=-ltermcap</KBD>". · 3 unmodified lines | ||
| 176 | 178 | <DT> |
| 177 | 179 | include: |
| 178 | 180 | <DD> |
| 179 | − lua.h luaconf.h lualib.h lauxlib.h lua.hpp | |
| 181 | + lauxlib.h lua.h lua.hpp luaconf.h lualib.h | |
| 180 | 182 | <DT> |
| 181 | 183 | lib: |
| 182 | 184 | <DD> |
| @@ -190,8 +192,8 @@ then try "<KBD>make linux MYLIBS=-ltermcap</KBD>". · 7 unmodified lines | ||
| 190 | 192 | <P> |
| 191 | 193 | These are the only directories you need for development. |
| 192 | 194 | If you only want to run Lua programs, |
| 193 | − you only need the files in bin and man. | |
| 194 | − The files in include and lib are needed for | |
| 195 | + you only need the files in <TT>bin</TT> and <TT>man</TT>. | |
| 196 | + The files in <TT>include</TT> and <TT>lib</TT> are needed for | |
| 195 | 197 | embedding Lua in C or C++ programs. |
| 196 | 198 | |
| 197 | 199 | <H3><A NAME="customization">Customization</A></H3> |
| @@ -207,7 +209,7 @@ then try "<KBD>make linux MYLIBS=-ltermcap</KBD>". · 9 unmodified lines | ||
| 207 | 209 | You don't actually need to edit the Makefiles because you may set the |
| 208 | 210 | relevant variables in the command line when invoking make. |
| 209 | 211 | 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 | 214 | <P> |
| 213 | 215 | 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 | 218 | it will be used by any Lua clients that you build, to ensure consistency. |
| 217 | 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 | 221 | <H3><A NAME="other">Building Lua on other systems</A></H3> |
| 225 | 222 | |
| 226 | 223 | <P> |
| @@ -357,10 +354,10 @@ THE SOFTWARE. · 130 unmodified lines | ||
| 357 | 354 | <HR> |
| 358 | 355 | <SMALL CLASS="footer"> |
| 359 | 356 | Last update: |
| 360 | −Fri Mar 21 17:33:08 BRT 2014 | |
| 357 | +Wed Jun 11 22:54:18 BRT 2014 | |
| 361 | 358 | </SMALL> |
| 362 | 359 | <!-- |
| 363 | −Last change: updated for Lua 5.3.0 (work2) | |
| 360 | +Last change: updated for Lua 5.3.0 (work3) | |
| 364 | 361 | --> |
| 365 | 362 | |
| 366 | 363 | </BODY> |
| @@ -1,2 +0,0 @@ | ||
| 1 | −#define nvalue(x) ((lua_Number)0) | |
| 2 | −#define ttypenv(x) ttnov(x) | |
| @@ -109,7 +109,7 @@ macosx: · 108 unmodified lines | ||
| 109 | 109 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_MACOSX" SYSLIBS="-lreadline" CC=cc |
| 110 | 110 | |
| 111 | 111 | mingw: |
| 112 | − $(MAKE) "LUA_A=lua52.dll" "LUA_T=lua.exe" \ | |
| 112 | + $(MAKE) "LUA_A=lua53.dll" "LUA_T=lua.exe" \ | |
| 113 | 113 | "AR=$(CC) -shared -o" "RANLIB=strip --strip-unneeded" \ |
| 114 | 114 | "SYSCFLAGS=-DLUA_BUILD_AS_DLL" "SYSLIBS=" "SYSLDFLAGS=-s" lua.exe |
| 115 | 115 | $(MAKE) "LUAC_T=luac.exe" luac.exe |
| @@ -118,7 +118,7 @@ posix: · 2 unmodified lines | ||
| 118 | 118 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX" |
| 119 | 119 | |
| 120 | 120 | solaris: |
| 121 | − $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-ldl" | |
| 121 | + $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN -D_REENTRANT" SYSLIBS="-ldl" | |
| 122 | 122 | |
| 123 | 123 | # list targets that do not create files (but not all makes understand .PHONY) |
| 124 | 124 | .PHONY: all $(PLATS) default o a clean depend echo none |
| @@ -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 | 3 | ** Lua API |
| 4 | 4 | ** 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 | 94 | int res; |
| 95 | 95 | CallInfo *ci = L->ci; |
| 96 | 96 | lua_lock(L); |
| 97 | + api_check(L, size >= 0, "negative 'size'"); | |
| 97 | 98 | if (L->stack_last - L->top > size) /* stack large enough? */ |
| 98 | 99 | res = 1; /* yes; check is OK */ |
| 99 | 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 | |
| 183 | −LUA_API void lua_remove (lua_State *L, int idx) { | |
| 184 | − StkId p; | |
| 185 | − lua_lock(L); | |
| 186 | − p = index2addr(L, idx); | |
| 187 | − api_checkstackindex(L, idx, p); | |
| 188 | − while (++p < L->top) setobjs2s(L, p-1, p); | |
| 189 | − L->top--; | |
| 190 | − lua_unlock(L); | |
| 184 | +/* | |
| 185 | +** Reverse the stack segment from 'from' to 'to' | |
| 186 | +** (auxiliar to 'lua_rotate') | |
| 187 | +*/ | |
| 188 | +static void reverse (lua_State *L, StkId from, StkId to) { | |
| 189 | + for (; from < to; from++, to--) { | |
| 190 | + TValue temp; | |
| 191 | + setobj(L, &temp, from); | |
| 192 | + setobjs2s(L, from, to); | |
| 193 | + setobj2s(L, to, &temp); | |
| 194 | + } | |
| 191 | 195 | } |
| 192 | 196 | |
| 193 | 197 | |
| 194 | −LUA_API void lua_insert (lua_State *L, int idx) { | |
| 195 | − StkId p; | |
| 196 | − StkId q; | |
| 198 | +/* | |
| 199 | +** Let x = AB, where A is a prefix of length 'n'. Then, | |
| 200 | +** rotate x n == BA. But BA == (A^r . B^r)^r. | |
| 201 | +*/ | |
| 202 | +LUA_API void lua_rotate (lua_State *L, int idx, int n) { | |
| 203 | + StkId p, t, m; | |
| 197 | 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 | 207 | api_checkstackindex(L, idx, p); |
| 200 | − for (q = L->top; q > p; q--) /* use L->top as a temporary */ | |
| 201 | − setobjs2s(L, q, q - 1); | |
| 202 | − setobjs2s(L, p, L->top); | |
| 208 | + api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'"); | |
| 209 | + m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */ | |
| 210 | + reverse(L, p, m); /* reverse the prefix with length 'n' */ | |
| 211 | + reverse(L, m + 1, t); /* reverse the suffix */ | |
| 212 | + reverse(L, p, t); /* reverse the entire segment */ | |
| 203 | 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 | |
| 335 | −LUA_API int lua_strtonum (lua_State *L, const char *s, size_t len) { | |
| 336 | − lua_Integer i; lua_Number n; | |
| 337 | − if (luaO_str2int(s, len, &i)) { /* try as an integer */ | |
| 338 | − setivalue(L->top, i); | |
| 339 | − } | |
| 340 | − else if (luaO_str2d(s, len, &n)) { /* else try as a float */ | |
| 341 | − setnvalue(L->top, n); | |
| 342 | − } | |
| 343 | − else | |
| 344 | − return 0; /* conversion failed */ | |
| 345 | − api_incr_top(L); | |
| 346 | − return 1; | |
| 345 | +LUA_API size_t lua_strtonum (lua_State *L, const char *s) { | |
| 346 | + size_t sz = luaO_str2num(s, L->top); | |
| 347 | + if (sz != 0) | |
| 348 | + api_incr_top(L); | |
| 349 | + return sz; | |
| 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 | 378 | int isnum = 0; |
| 376 | 379 | switch (ttype(o)) { |
| 377 | 380 | case LUA_TNUMINT: { |
| 378 | − res = cast_unsigned(ivalue(o)); | |
| 381 | + res = l_castS2U(ivalue(o)); | |
| 379 | 382 | isnum = 1; |
| 380 | 383 | break; |
| 381 | 384 | } |
| 382 | 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 | 387 | lua_Number n = fltvalue(o); /* get value */ |
| 385 | 388 | int neg = 0; |
| 386 | 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 | 391 | neg = 1; |
| 389 | 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 | 395 | if (luai_numisnan(n)) /* not a number? */ |
| 393 | 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 | 398 | if (neg) res = 0u - res; /* back to negative, if needed */ |
| 396 | 399 | isnum = 1; |
| 397 | 400 | break; |
| @@ -495,9 +498,7 @@ LUA_API void lua_pushnil (lua_State *L) { · 97 unmodified lines | ||
| 495 | 498 | |
| 496 | 499 | LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { |
| 497 | 500 | lua_lock(L); |
| 498 | − setnvalue(L->top, n); | |
| 499 | − luai_checknum(L, L->top, | |
| 500 | − luaG_runerror(L, "C API - attempt to push a signaling NaN")); | |
| 501 | + setfltvalue(L->top, n); | |
| 501 | 502 | api_incr_top(L); |
| 502 | 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 | |
| 514 | 515 | LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) { |
| 515 | 516 | lua_lock(L); |
| 516 | − setivalue(L->top, cast_integer(u)); | |
| 517 | + setivalue(L->top, l_castU2S(u)); | |
| 517 | 518 | api_incr_top(L); |
| 518 | 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 | 580 | setfvalue(L->top, fn); |
| 580 | 581 | } |
| 581 | 582 | else { |
| 582 | − Closure *cl; | |
| 583 | + CClosure *cl; | |
| 583 | 584 | api_checknelems(L, n); |
| 584 | 585 | api_check(L, n <= MAXUPVAL, "upvalue index too large"); |
| 585 | 586 | luaC_checkGC(L); |
| 586 | 587 | cl = luaF_newCclosure(L, n); |
| 587 | − cl->c.f = fn; | |
| 588 | + cl->f = fn; | |
| 588 | 589 | L->top -= n; |
| 589 | 590 | while (n--) { |
| 590 | − setobj2n(L, &cl->c.upvalue[n], L->top + n); | |
| 591 | + setobj2n(L, &cl->upvalue[n], L->top + n); | |
| 591 | 592 | /* does not need barrier because closure is white */ |
| 592 | 593 | } |
| 593 | 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 | |
| 744 | −LUA_API void lua_getuservalue (lua_State *L, int idx) { | |
| 745 | +LUA_API int lua_getuservalue (lua_State *L, int idx) { | |
| 745 | 746 | StkId o; |
| 746 | 747 | lua_lock(L); |
| 747 | 748 | o = index2addr(L, idx); |
| @@ -749,6 +750,7 @@ LUA_API void lua_getuservalue (lua_State *L, int idx) { · 1 unmodified line | ||
| 749 | 750 | getuservalue(L, rawuvalue(o), L->top); |
| 750 | 751 | api_incr_top(L); |
| 751 | 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 | 900 | "results from function overflow current stack size") |
| 899 | 901 | |
| 900 | 902 | |
| 901 | −LUA_API int lua_getctx (lua_State *L, int *ctx) { | |
| 902 | − if (L->ci->callstatus & CIST_YIELDED) { | |
| 903 | − if (ctx) *ctx = L->ci->u.c.ctx; | |
| 904 | − return L->ci->u.c.status; | |
| 905 | − } | |
| 906 | − else return LUA_OK; | |
| 907 | −} | |
| 908 | − | |
| 909 | − | |
| 910 | 903 | LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx, |
| 911 | − lua_CFunction k) { | |
| 904 | + lua_KFunction k) { | |
| 912 | 905 | StkId func; |
| 913 | 906 | lua_lock(L); |
| 914 | 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 | |
| 949 | 942 | LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, |
| 950 | − int ctx, lua_CFunction k) { | |
| 943 | + int ctx, lua_KFunction k) { | |
| 951 | 944 | struct CallS c; |
| 952 | 945 | int status; |
| 953 | 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 | 968 | ci->u.c.ctx = ctx; /* save context */ |
| 976 | 969 | /* save information for error recovery */ |
| 977 | 970 | ci->extra = savestack(L, c.func); |
| 978 | − ci->u.c.old_allowhook = L->allowhook; | |
| 979 | 971 | ci->u.c.old_errfunc = L->errfunc; |
| 980 | 972 | L->errfunc = func; |
| 981 | − /* mark that function may do error recovery */ | |
| 982 | − ci->callstatus |= CIST_YPCALL; | |
| 973 | + setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */ | |
| 974 | + ci->callstatus |= CIST_YPCALL; /* function can do error recovery */ | |
| 983 | 975 | luaD_call(L, c.func, nresults, 1); /* do the call */ |
| 984 | 976 | ci->callstatus &= ~CIST_YPCALL; |
| 985 | 977 | L->errfunc = ci->u.c.old_errfunc; |
| @@ -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 | 3 | ** Auxiliary functions for building Lua libraries |
| 4 | 4 | ** 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 | |
| 398 | 398 | static void interror (lua_State *L, int arg) { |
| 399 | 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 | 401 | else |
| 402 | 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 | 757 | case LUA_TNUMBER: { |
| 758 | 758 | if (lua_isinteger(L, idx)) |
| 759 | 759 | lua_pushfstring(L, "%I", lua_tointeger(L, idx)); |
| 760 | − else { | |
| 761 | − const char *s = lua_pushfstring(L, "%f", lua_tonumber(L, idx)); | |
| 762 | − if (s[strspn(s, "-0123456789")] == '\0') { /* looks like an int? */ | |
| 763 | − lua_pushliteral(L, ".0"); /* add a '.0' to result */ | |
| 764 | − lua_concat(L, 2); | |
| 765 | − } | |
| 766 | − } | |
| 760 | + else | |
| 761 | + lua_pushfstring(L, "%f", lua_tonumber(L, idx)); | |
| 767 | 762 | break; |
| 768 | 763 | } |
| 769 | 764 | case LUA_TSTRING: |
| @@ -966,12 +961,12 @@ LUALIB_API lua_State *luaL_newstate (void) { · 196 unmodified lines | ||
| 966 | 961 | } |
| 967 | 962 | |
| 968 | 963 | |
| 969 | −LUALIB_API void luaL_checkversion_ (lua_State *L, int ver, size_t sz) { | |
| 964 | +LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) { | |
| 970 | 965 | const lua_Number *v = lua_version(L); |
| 971 | 966 | if (v != lua_version(NULL)) |
| 972 | 967 | luaL_error(L, "multiple Lua VMs detected"); |
| 973 | 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 | 970 | ver, *v); |
| 976 | 971 | /* check numeric types */ |
| 977 | 972 | if (sz != LUAL_NUMSIZES) |
| @@ -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 | 3 | ** Auxiliary functions for building Lua libraries |
| 4 | 4 | ** See Copyright Notice in lua.h |
| 5 | 5 | */ |
| @@ -28,7 +28,7 @@ typedef struct luaL_Reg { · 22 unmodified lines | ||
| 28 | 28 | |
| 29 | 29 | #define LUAL_NUMSIZES (sizeof(lua_Integer)*16 + sizeof(lua_Number)) |
| 30 | 30 | |
| 31 | −LUALIB_API void (luaL_checkversion_) (lua_State *L, int ver, size_t sz); | |
| 31 | +LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver, size_t sz); | |
| 32 | 32 | #define luaL_checkversion(L) \ |
| 33 | 33 | luaL_checkversion_(L, LUA_VERSION_NUM, LUAL_NUMSIZES) |
| 34 | 34 | |
| @@ -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 | 3 | ** Basic library |
| 4 | 4 | ** 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 | 46 | #define SPACECHARS " \f\n\r\t\v" |
| 47 | 47 | |
| 48 | −static int b_str2int (const char *s, const char *e, int base, lua_Integer *pn) { | |
| 48 | +static const char *b_str2int (const char *s, int base, lua_Integer *pn) { | |
| 49 | 49 | lua_Unsigned n = 0; |
| 50 | 50 | int neg = 0; |
| 51 | 51 | s += strspn(s, SPACECHARS); /* skip initial spaces */ |
| 52 | 52 | if (*s == '-') { s++; neg = 1; } /* handle signal */ |
| 53 | 53 | else if (*s == '+') s++; |
| 54 | 54 | if (!isalnum((unsigned char)*s)) /* no digit? */ |
| 55 | − return 0; | |
| 55 | + return NULL; | |
| 56 | 56 | do { |
| 57 | 57 | int digit = (isdigit((unsigned char)*s)) ? *s - '0' |
| 58 | 58 | : toupper((unsigned char)*s) - 'A' + 10; |
| 59 | − if (digit >= base) return 0; /* invalid numeral */ | |
| 59 | + if (digit >= base) return NULL; /* invalid numeral */ | |
| 60 | 60 | n = n * base + digit; |
| 61 | 61 | s++; |
| 62 | 62 | } while (isalnum((unsigned char)*s)); |
| 63 | 63 | s += strspn(s, SPACECHARS); /* skip trailing spaces */ |
| 64 | − if (s != e) /* invalid trailing characters? */ | |
| 65 | − return 0; | |
| 64 | + if (*s != '\0') /* invalid trailing characters? */ | |
| 65 | + return NULL; | |
| 66 | 66 | *pn = (lua_Integer)((neg) ? (0u - n) : n); |
| 67 | − return 1; | |
| 67 | + return s; | |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | |
| 71 | 71 | static int luaB_tonumber (lua_State *L) { |
| 72 | − if (lua_isnoneornil(L, 2)) { /* standard conversion */ | |
| 72 | + if (lua_isnoneornil(L, 2)) { /* standard conversion? */ | |
| 73 | 73 | luaL_checkany(L, 1); |
| 74 | 74 | if (lua_type(L, 1) == LUA_TNUMBER) { /* already a number? */ |
| 75 | 75 | lua_settop(L, 1); /* yes; return it */ |
| @@ -78,20 +78,20 @@ static int luaB_tonumber (lua_State *L) { · 2 unmodified lines | ||
| 78 | 78 | else { |
| 79 | 79 | size_t l; |
| 80 | 80 | const char *s = lua_tolstring(L, 1, &l); |
| 81 | − if (s != NULL && lua_strtonum(L, s, l)) /* can convert to a number? */ | |
| 82 | − return 1; | |
| 81 | + if (s != NULL && lua_strtonum(L, s) == l + 1) | |
| 82 | + return 1; /* successful conversion to number */ | |
| 83 | 83 | /* else not a number */ |
| 84 | 84 | } |
| 85 | 85 | } |
| 86 | 86 | else { |
| 87 | 87 | size_t l; |
| 88 | 88 | const char *s; |
| 89 | − lua_Integer n; | |
| 89 | + lua_Integer n = 0; /* to avoid warnings */ | |
| 90 | 90 | int base = luaL_checkint(L, 2); |
| 91 | 91 | luaL_checktype(L, 1, LUA_TSTRING); /* before 'luaL_checklstring'! */ |
| 92 | 92 | s = luaL_checklstring(L, 1, &l); |
| 93 | 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 | 95 | lua_pushinteger(L, n); |
| 96 | 96 | return 1; |
| 97 | 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 | |
| 344 | −static int dofilecont (lua_State *L) { | |
| 344 | +static int dofilecont (lua_State *L, int d1, int d2) { | |
| 345 | + (void)d1; (void)d2; /* only to match 'lua_Kfunction' prototype */ | |
| 345 | 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 | 353 | if (luaL_loadfile(L, fname) != LUA_OK) |
| 353 | 354 | return lua_error(L); |
| 354 | 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 | |
| 359 | 360 | static int luaB_assert (lua_State *L) { |
| 360 | − if (!lua_toboolean(L, 1)) | |
| 361 | − return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!")); | |
| 362 | − return lua_gettop(L); | |
| 361 | + if (lua_toboolean(L, 1)) /* condition is true? */ | |
| 362 | + return lua_gettop(L); /* return all arguments */ | |
| 363 | + else { /* error */ | |
| 364 | + if (lua_isnone(L, 2)) /* no error message? */ | |
| 365 | + lua_pushliteral(L, "assertion failed!"); /* use standard message */ | |
| 366 | + lua_remove(L, 1); /* remove the condition (if there is one...) */ | |
| 367 | + return luaB_error(L); /* call 'error' */ | |
| 368 | + } | |
| 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 | |
| 382 | −static int finishpcall (lua_State *L, int status) { | |
| 383 | − if (!lua_checkstack(L, 1)) { /* no space for extra boolean? */ | |
| 384 | − lua_settop(L, 0); /* create space for return values */ | |
| 385 | − lua_pushboolean(L, 0); | |
| 386 | − lua_pushstring(L, "stack overflow"); | |
| 388 | +/* | |
| 389 | +** Continuation function for 'pcall' and 'xpcall'. Both functions | |
| 390 | +** already pushed a 'true' before doing the call, so in case of sucess | |
| 391 | +** 'finishpcall' only has to return everything in the stack minus | |
| 392 | +** 'extra' values (where 'extra' is exactly the number of items to be | |
| 393 | +** ignored). | |
| 394 | +*/ | |
| 395 | +static int finishpcall (lua_State *L, int status, int extra) { | |
| 396 | + if (status != LUA_OK && status != LUA_YIELD) { /* error? */ | |
| 397 | + lua_pushboolean(L, 0); /* first result (false) */ | |
| 398 | + lua_pushvalue(L, -2); /* error message */ | |
| 387 | 399 | return 2; /* return false, msg */ |
| 388 | 400 | } |
| 389 | − lua_pushboolean(L, status); /* first result (status) */ | |
| 390 | − lua_replace(L, 1); /* put first result in first slot */ | |
| 391 | − return lua_gettop(L); | |
| 392 | −} | |
| 393 | − | |
| 394 | − | |
| 395 | −static int pcallcont (lua_State *L) { | |
| 396 | − int status = lua_getctx(L, NULL); | |
| 397 | − return finishpcall(L, (status == LUA_YIELD)); | |
| 401 | + else | |
| 402 | + return lua_gettop(L) - extra; /* return all results */ | |
| 398 | 403 | } |
| 399 | 404 | |
| 400 | 405 | |
| 401 | 406 | static int luaB_pcall (lua_State *L) { |
| 402 | 407 | int status; |
| 403 | 408 | luaL_checkany(L, 1); |
| 404 | − lua_pushnil(L); | |
| 405 | − lua_insert(L, 1); /* create space for status result */ | |
| 406 | − status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, pcallcont); | |
| 407 | − return finishpcall(L, (status == LUA_OK)); | |
| 409 | + lua_pushboolean(L, 1); /* first result if no errors */ | |
| 410 | + lua_insert(L, 1); /* put it in place */ | |
| 411 | + status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, finishpcall); | |
| 412 | + return finishpcall(L, status, 0); | |
| 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 | +*/ | |
| 411 | 421 | static int luaB_xpcall (lua_State *L) { |
| 412 | 422 | int status; |
| 413 | 423 | int n = lua_gettop(L); |
| 414 | − luaL_argcheck(L, n >= 2, 2, "value expected"); | |
| 415 | − lua_pushvalue(L, 1); /* exchange function... */ | |
| 416 | − lua_copy(L, 2, 1); /* ...and error handler */ | |
| 417 | − lua_replace(L, 2); | |
| 418 | − status = lua_pcallk(L, n - 2, LUA_MULTRET, 1, 0, pcallcont); | |
| 419 | − return finishpcall(L, (status == LUA_OK)); | |
| 424 | + luaL_checktype(L, 2, LUA_TFUNCTION); /* check error function */ | |
| 425 | + lua_pushboolean(L, 1); /* first result */ | |
| 426 | + lua_pushvalue(L, 1); /* function */ | |
| 427 | + lua_rotate(L, 3, 2); /* move them below function's arguments */ | |
| 428 | + status = lua_pcallk(L, n - 2, LUA_MULTRET, 2, 2, finishpcall); | |
| 429 | + return finishpcall(L, status, 2); | |
| 420 | 430 | } |
| 421 | 431 | |
| 422 | 432 | |
| @@ -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 | 3 | ** Standard library for bitwise operations |
| 4 | 4 | ** 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 | |
| 223 | 223 | LUAMOD_API int luaopen_bit32 (lua_State *L) { |
| 224 | − lua_pushnil(L); | |
| 225 | − return 1; | |
| 224 | + return luaL_error(L, "library 'bit32' has been deprecated"); | |
| 226 | 225 | } |
| 227 | 226 | |
| 228 | 227 | #endif /* } */ |
| @@ -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 | 3 | ** Code generator for Lua |
| 4 | 4 | ** See Copyright Notice in lua.h |
| 5 | 5 | */ |
| @@ -46,7 +46,7 @@ static int tonumeral(expdesc *e, TValue *v) { · 40 unmodified lines | ||
| 46 | 46 | if (v) setivalue(v, e->u.ival); |
| 47 | 47 | return 1; |
| 48 | 48 | case VKFLT: |
| 49 | − if (v) setnvalue(v, e->u.nval); | |
| 49 | + if (v) setfltvalue(v, e->u.nval); | |
| 50 | 50 | return 1; |
| 51 | 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 | |
| 198 | −LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level) { | |
| 198 | +void luaK_patchclose (FuncState *fs, int list, int level) { | |
| 199 | 199 | level++; /* argument is +1 to reserve 0 as non-op */ |
| 200 | 200 | while (list != NO_JUMP) { |
| 201 | 201 | int next = getjump(fs, list); |
| @@ -317,7 +317,7 @@ static int addk (FuncState *fs, TValue *key, TValue *v) { · 115 unmodified lines | ||
| 317 | 317 | TValue *idx = luaH_set(L, fs->ls->h, key); /* index scanner table */ |
| 318 | 318 | int k, oldsize; |
| 319 | 319 | if (ttisinteger(idx)) { /* is there an index there? */ |
| 320 | − k = ivalue(idx); | |
| 320 | + k = cast_int(ivalue(idx)); | |
| 321 | 321 | /* correct value? (warning: must distinguish floats from integers!) */ |
| 322 | 322 | if (k < fs->nk && ttype(&f->k[k]) == ttype(v) && |
| 323 | 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; | |
| 350 | −** conversion to 'void*' used only for hash, no "precision" problems | |
| 349 | +** Integers use userdata as keys to avoid collision with floats with same | |
| 350 | +** value; conversion to 'void*' used only for hashing, no "precision" | |
| 351 | +** problems | |
| 351 | 352 | */ |
| 352 | 353 | int luaK_intK (FuncState *fs, lua_Integer n) { |
| 353 | 354 | TValue k, o; |
| @@ -359,13 +360,13 @@ int luaK_intK (FuncState *fs, lua_Integer n) { · 5 unmodified lines | ||
| 359 | 360 | |
| 360 | 361 | /* |
| 361 | 362 | ** Both NaN and -0.0 should not go to the constant table, as they have |
| 362 | −** problems with the hashing. (NaN is not ** a valid key, | |
| 363 | −** -0.0 collides with +0.0.) | |
| 363 | +** problems with the hashing. (NaN is not a valid key, -0.0 collides | |
| 364 | +** with +0.0.) | |
| 364 | 365 | */ |
| 365 | 366 | static int luaK_numberK (FuncState *fs, lua_Number r) { |
| 366 | 367 | TValue o; |
| 367 | 368 | lua_assert(!luai_numisnan(r) && !isminuszero(r)); |
| 368 | − setnvalue(&o, r); | |
| 369 | + setfltvalue(&o, r); | |
| 369 | 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 | 769 | return (tointeger(v1, &i) && tointeger(v2, &i)); |
| 769 | 770 | case LUA_OPMOD: /* integer module by 0 */ |
| 770 | 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 | 772 | default: return 1; /* everything else is valid */ |
| 774 | 773 | } |
| 775 | 774 | } |
| @@ -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 | 3 | ** Coroutine Library |
| 4 | 4 | ** See Copyright Notice in lua.h |
| 5 | 5 | */ |
| @@ -17,6 +17,13 @@ · 11 unmodified lines | ||
| 17 | 17 | #include "lualib.h" |
| 18 | 18 | |
| 19 | 19 | |
| 20 | +static lua_State *getco (lua_State *L) { | |
| 21 | + lua_State *co = lua_tothread(L, 1); | |
| 22 | + luaL_argcheck(L, co, 1, "coroutine expected"); | |
| 23 | + return co; | |
| 24 | +} | |
| 25 | + | |
| 26 | + | |
| 20 | 27 | static int auxresume (lua_State *L, lua_State *co, int narg) { |
| 21 | 28 | int status; |
| 22 | 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 | |
| 49 | 56 | static int luaB_coresume (lua_State *L) { |
| 50 | − lua_State *co = lua_tothread(L, 1); | |
| 57 | + lua_State *co = getco(L); | |
| 51 | 58 | int r; |
| 52 | − luaL_argcheck(L, co, 1, "coroutine expected"); | |
| 53 | 59 | r = auxresume(L, co, lua_gettop(L) - 1); |
| 54 | 60 | if (r < 0) { |
| 55 | 61 | lua_pushboolean(L, 0); |
| @@ -102,8 +108,7 @@ static int luaB_yield (lua_State *L) { · 46 unmodified lines | ||
| 102 | 108 | |
| 103 | 109 | |
| 104 | 110 | static int luaB_costatus (lua_State *L) { |
| 105 | − lua_State *co = lua_tothread(L, 1); | |
| 106 | − luaL_argcheck(L, co, 1, "coroutine expected"); | |
| 111 | + lua_State *co = getco(L); | |
| 107 | 112 | if (L == co) lua_pushliteral(L, "running"); |
| 108 | 113 | else { |
| 109 | 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 | |
| 137 | +static int luaB_yieldable (lua_State *L) { | |
| 138 | + lua_pushboolean(L, lua_isyieldable(L)); | |
| 139 | + return 1; | |
| 140 | +} | |
| 141 | + | |
| 142 | + | |
| 132 | 143 | static int luaB_corunning (lua_State *L) { |
| 133 | 144 | int ismain = lua_pushthread(L); |
| 134 | 145 | lua_pushboolean(L, ismain); |
| @@ -143,6 +154,7 @@ static const luaL_Reg co_funcs[] = { · 8 unmodified lines | ||
| 143 | 154 | {"status", luaB_costatus}, |
| 144 | 155 | {"wrap", luaB_cowrap}, |
| 145 | 156 | {"yield", luaB_yield}, |
| 157 | + {"isyieldable", luaB_yieldable}, | |
| 146 | 158 | {NULL, NULL} |
| 147 | 159 | }; |
| 148 | 160 | |
| @@ -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 | 3 | ** Interface from Lua to its debug API |
| 4 | 4 | ** See Copyright Notice in lua.h |
| 5 | 5 | */ |
| @@ -21,16 +21,30 @@ · 15 unmodified lines | ||
| 21 | 21 | #define HOOKKEY "_HKEY" |
| 22 | 22 | |
| 23 | 23 | |
| 24 | − | |
| 25 | −static int db_numbits (lua_State *L) { | |
| 24 | +static int db_Csize (lua_State *L) { | |
| 25 | + static struct { | |
| 26 | + char c; | |
| 27 | + unsigned char sz; | |
| 28 | + } sizes[] = { | |
| 29 | + {'I', sizeof(lua_Integer)}, | |
| 30 | + {'F', sizeof(lua_Number)}, | |
| 31 | + {'b', CHAR_BIT}, /* here is number of bits (not bytes) */ | |
| 32 | + {'h', sizeof(short)}, | |
| 33 | + {'i', sizeof(int)}, | |
| 34 | + {'l', sizeof(long)}, | |
| 35 | + {'z', sizeof(size_t)}, | |
| 36 | + {'f', sizeof(float)}, | |
| 37 | + {'d', sizeof(double)} | |
| 38 | + }; | |
| 26 | 39 | const char *s = luaL_checkstring(L, 1); |
| 27 | − if (*s == 'i') | |
| 28 | − lua_pushinteger(L, sizeof(lua_Integer) * CHAR_BIT); | |
| 29 | − else if (*s == 'f') | |
| 30 | − lua_pushinteger(L, sizeof(lua_Number) * CHAR_BIT); | |
| 31 | − else | |
| 32 | − luaL_argerror(L, 1, lua_pushfstring(L, "invalid option '%s'", s)); | |
| 33 | − return 1; | |
| 40 | + int i; | |
| 41 | + for (i = 0; i < (int)(sizeof(sizes)/sizeof(sizes[0])); i++) { | |
| 42 | + if (*s == sizes[i].c) { | |
| 43 | + lua_pushinteger(L, sizes[i].sz); | |
| 44 | + return 1; | |
| 45 | + } | |
| 46 | + } | |
| 47 | + return luaL_argerror(L, 1, lua_pushfstring(L, "invalid option '%c'", *s)); | |
| 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 | |
| 80 | −static void settabss (lua_State *L, const char *i, const char *v) { | |
| 81 | − lua_pushstring(L, v); | |
| 82 | − lua_setfield(L, -2, i); | |
| 83 | −} | |
| 84 | − | |
| 85 | − | |
| 86 | −static void settabsi (lua_State *L, const char *i, int v) { | |
| 87 | − lua_pushinteger(L, v); | |
| 88 | − lua_setfield(L, -2, i); | |
| 89 | −} | |
| 90 | − | |
| 91 | − | |
| 92 | −static void settabsb (lua_State *L, const char *i, int v) { | |
| 93 | − lua_pushboolean(L, v); | |
| 94 | − lua_setfield(L, -2, i); | |
| 95 | −} | |
| 96 | − | |
| 97 | − | |
| 94 | +/* | |
| 95 | +** Auxiliary function used by several library functions: check for | |
| 96 | +** an optional thread as function's first argument and set 'arg' with | |
| 97 | +** 1 if this argument is present (so that functions can skip it to | |
| 98 | +** access their other arguments) | |
| 99 | +*/ | |
| 98 | 100 | static lua_State *getthread (lua_State *L, int *arg) { |
| 99 | 101 | if (lua_isthread(L, 1)) { |
| 100 | 102 | *arg = 1; |
| @@ -102,44 +104,70 @@ static lua_State *getthread (lua_State *L, int *arg) { · 1 unmodified line | ||
| 102 | 104 | } |
| 103 | 105 | else { |
| 104 | 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 | +*/ | |
| 117 | +static void settabss (lua_State *L, const char *k, const char *v) { | |
| 118 | + lua_pushstring(L, v); | |
| 119 | + lua_setfield(L, -2, k); | |
| 120 | +} | |
| 121 | + | |
| 122 | +static void settabsi (lua_State *L, const char *k, int v) { | |
| 123 | + lua_pushinteger(L, v); | |
| 124 | + lua_setfield(L, -2, k); | |
| 125 | +} | |
| 126 | + | |
| 127 | +static void settabsb (lua_State *L, const char *k, int v) { | |
| 128 | + lua_pushboolean(L, v); | |
| 129 | + lua_setfield(L, -2, k); | |
| 130 | +} | |
| 131 | + | |
| 132 | + | |
| 133 | +/* | |
| 134 | +** In function 'db_getinfo', the call to 'lua_getinfo' may push | |
| 135 | +** results on the stack; later it creates the result table to put | |
| 136 | +** these objects. Function 'treatstackoption' puts the result from | |
| 137 | +** 'lua_getinfo' on top of the result table so that it can call | |
| 138 | +** 'lua_setfield'. | |
| 139 | +*/ | |
| 110 | 140 | static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) { |
| 111 | − if (L == L1) { | |
| 112 | − lua_pushvalue(L, -2); | |
| 113 | − lua_remove(L, -3); | |
| 114 | − } | |
| 141 | + if (L == L1) | |
| 142 | + lua_rotate(L, -2, 1); /* exchange object and table */ | |
| 115 | 143 | else |
| 116 | − lua_xmove(L1, L, 1); | |
| 117 | − lua_setfield(L, -2, fname); | |
| 144 | + lua_xmove(L1, L, 1); /* move object to the "main" stack */ | |
| 145 | + lua_setfield(L, -2, fname); /* put object into table */ | |
| 118 | 146 | } |
| 119 | 147 | |
| 120 | 148 | |
| 149 | +/* | |
| 150 | +** Calls 'lua_getinfo' and collects all results in a new table. | |
| 151 | +*/ | |
| 121 | 152 | static int db_getinfo (lua_State *L) { |
| 122 | 153 | lua_Debug ar; |
| 123 | 154 | int arg; |
| 124 | 155 | lua_State *L1 = getthread(L, &arg); |
| 125 | 156 | const char *options = luaL_optstring(L, arg+2, "flnStu"); |
| 126 | − if (lua_isnumber(L, arg+1)) { | |
| 127 | − if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), &ar)) { | |
| 157 | + if (lua_isfunction(L, arg + 1)) { /* info about a function? */ | |
| 158 | + options = lua_pushfstring(L, ">%s", options); /* add '>' to 'options' */ | |
| 159 | + lua_pushvalue(L, arg + 1); /* move function to 'L1' stack */ | |
| 160 | + lua_xmove(L, L1, 1); | |
| 161 | + } | |
| 162 | + else { /* stack level */ | |
| 163 | + if (!lua_getstack(L1, luaL_checkint(L, arg + 1), &ar)) { | |
| 128 | 164 | lua_pushnil(L); /* level out of range */ |
| 129 | 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 | 168 | if (!lua_getinfo(L1, options, &ar)) |
| 141 | 169 | return luaL_argerror(L, arg+2, "invalid option"); |
| 142 | − lua_createtable(L, 0, 2); | |
| 170 | + lua_newtable(L); /* table to collect results */ | |
| 143 | 171 | if (strchr(options, 'S')) { |
| 144 | 172 | settabss(L, "source", ar.source); |
| 145 | 173 | settabss(L, "short_src", ar.short_src); |
| @@ -177,16 +205,16 @@ static int db_getlocal (lua_State *L) { · 31 unmodified lines | ||
| 177 | 205 | if (lua_isfunction(L, arg + 1)) { /* function argument? */ |
| 178 | 206 | lua_pushvalue(L, arg + 1); /* push function */ |
| 179 | 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 | 210 | else { /* stack-level argument */ |
| 183 | 211 | if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */ |
| 184 | 212 | return luaL_argerror(L, arg+1, "level out of range"); |
| 185 | 213 | name = lua_getlocal(L1, &ar, nvar); |
| 186 | 214 | if (name) { |
| 187 | − lua_xmove(L1, L, 1); /* push local value */ | |
| 215 | + lua_xmove(L1, L, 1); /* move local value */ | |
| 188 | 216 | lua_pushstring(L, name); /* push name */ |
| 189 | − lua_pushvalue(L, -2); /* re-order */ | |
| 217 | + lua_rotate(L, -2, 1); /* re-order */ | |
| 190 | 218 | return 2; |
| 191 | 219 | } |
| 192 | 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 | +*/ | |
| 214 | 245 | static int auxupvalue (lua_State *L, int get) { |
| 215 | 246 | const char *name; |
| 216 | − int n = luaL_checkint(L, 2); | |
| 217 | − luaL_checktype(L, 1, LUA_TFUNCTION); | |
| 247 | + int n = luaL_checkint(L, 2); /* upvalue index */ | |
| 248 | + luaL_checktype(L, 1, LUA_TFUNCTION); /* closure */ | |
| 218 | 249 | name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n); |
| 219 | 250 | if (name == NULL) return 0; |
| 220 | 251 | lua_pushstring(L, name); |
| 221 | − lua_insert(L, -(get+1)); | |
| 252 | + lua_insert(L, -(get+1)); /* no-op if get is false */ | |
| 222 | 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 | +*/ | |
| 237 | 272 | static int checkupval (lua_State *L, int argf, int argnup) { |
| 238 | − lua_Debug ar; | |
| 239 | − int nup = luaL_checkint(L, argnup); | |
| 240 | − luaL_checktype(L, argf, LUA_TFUNCTION); | |
| 241 | − lua_pushvalue(L, argf); | |
| 242 | − lua_getinfo(L, ">u", &ar); | |
| 243 | − luaL_argcheck(L, 1 <= nup && nup <= ar.nups, argnup, "invalid upvalue index"); | |
| 273 | + int nup = luaL_checkint(L, argnup); /* upvalue index */ | |
| 274 | + luaL_checktype(L, argf, LUA_TFUNCTION); /* closure */ | |
| 275 | + luaL_argcheck(L, (lua_getupvalue(L, argf, nup) != NULL), argnup, | |
| 276 | + "invalid upvalue index"); | |
| 244 | 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 | 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 | +*/ | |
| 268 | 309 | static void hookf (lua_State *L, lua_Debug *ar) { |
| 269 | 310 | static const char *const hooknames[] = |
| 270 | 311 | {"call", "return", "line", "count", "tail call"}; |
| 271 | 312 | gethooktable(L); |
| 272 | 313 | lua_pushthread(L); |
| 273 | − if (lua_rawget(L, -2) == LUA_TFUNCTION) { | |
| 274 | − lua_pushstring(L, hooknames[(int)ar->event]); | |
| 314 | + if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */ | |
| 315 | + lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */ | |
| 275 | 316 | if (ar->currentline >= 0) |
| 276 | − lua_pushinteger(L, ar->currentline); | |
| 317 | + lua_pushinteger(L, ar->currentline); /* push current line */ | |
| 277 | 318 | else lua_pushnil(L); |
| 278 | 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 | +*/ | |
| 284 | 328 | static int makemask (const char *smask, int count) { |
| 285 | 329 | int mask = 0; |
| 286 | 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 | +*/ | |
| 294 | 341 | static char *unmakemask (int mask, char *smask) { |
| 295 | 342 | int i = 0; |
| 296 | 343 | if (mask & LUA_MASKCALL) smask[i++] = 'c'; |
| @@ -305,7 +352,7 @@ static int db_sethook (lua_State *L) { · 8 unmodified lines | ||
| 305 | 352 | int arg, mask, count; |
| 306 | 353 | lua_Hook func; |
| 307 | 354 | lua_State *L1 = getthread(L, &arg); |
| 308 | − if (lua_isnoneornil(L, arg+1)) { | |
| 355 | + if (lua_isnoneornil(L, arg+1)) { /* no hook? */ | |
| 309 | 356 | lua_settop(L, arg+1); |
| 310 | 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 | 368 | lua_pushvalue(L, -1); |
| 322 | 369 | lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */ |
| 323 | 370 | } |
| 324 | − lua_pushthread(L1); lua_xmove(L1, L, 1); | |
| 325 | − lua_pushvalue(L, arg+1); | |
| 326 | − lua_rawset(L, -3); /* set new hook */ | |
| 371 | + lua_pushthread(L1); lua_xmove(L1, L, 1); /* key */ | |
| 372 | + lua_pushvalue(L, arg+1); /* value */ | |
| 373 | + lua_rawset(L, -3); /* hooktable[L1] = new Lua hook */ | |
| 327 | 374 | lua_sethook(L1, func, mask, count); /* set hooks */ |
| 328 | 375 | return 0; |
| 329 | 376 | } |
| @@ -340,11 +387,11 @@ static int db_gethook (lua_State *L) { · 10 unmodified lines | ||
| 340 | 387 | else { |
| 341 | 388 | gethooktable(L); |
| 342 | 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 | 391 | lua_remove(L, -2); /* remove hook table */ |
| 345 | 392 | } |
| 346 | − lua_pushstring(L, unmakemask(mask, buff)); | |
| 347 | − lua_pushinteger(L, lua_gethookcount(L1)); | |
| 393 | + lua_pushstring(L, unmakemask(mask, buff)); /* 2nd result = mask */ | |
| 394 | + lua_pushinteger(L, lua_gethookcount(L1)); /* 3rd result = count */ | |
| 348 | 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 | |
| 381 | 428 | static const luaL_Reg dblib[] = { |
| 429 | + {"Csize", db_Csize}, | |
| 382 | 430 | {"debug", db_debug}, |
| 383 | 431 | {"getuservalue", db_getuservalue}, |
| 384 | 432 | {"gethook", db_gethook}, |
| @@ -387,7 +435,6 @@ static const luaL_Reg dblib[] = { · 2 unmodified lines | ||
| 387 | 435 | {"getregistry", db_getregistry}, |
| 388 | 436 | {"getmetatable", db_getmetatable}, |
| 389 | 437 | {"getupvalue", db_getupvalue}, |
| 390 | − {"numbits", db_numbits}, | |
| 391 | 438 | {"upvaluejoin", db_upvaluejoin}, |
| 392 | 439 | {"upvalueid", db_upvalueid}, |
| 393 | 440 | {"setuservalue", db_setuservalue}, |
| @@ -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 | 3 | ** Stack and Call structure of Lua |
| 4 | 4 | ** 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 | |
| 415 | −static void finishCcall (lua_State *L) { | |
| 417 | +/* | |
| 418 | +** Completes the execution of an interrupted C function, calling its | |
| 419 | +** continuation function. | |
| 420 | +*/ | |
| 421 | +static void finishCcall (lua_State *L, int status) { | |
| 416 | 422 | CallInfo *ci = L->ci; |
| 417 | 423 | int n; |
| 418 | − lua_assert(ci->u.c.k != NULL); /* must have a continuation */ | |
| 419 | − lua_assert(L->nny == 0); | |
| 424 | + /* must have a continuation and must be able to call it */ | |
| 425 | + lua_assert(ci->u.c.k != NULL && L->nny == 0); | |
| 426 | + /* error status can only happen in a protected call */ | |
| 427 | + lua_assert((ci->callstatus & CIST_YPCALL) || status == LUA_YIELD); | |
| 420 | 428 | if (ci->callstatus & CIST_YPCALL) { /* was inside a pcall? */ |
| 421 | 429 | ci->callstatus &= ~CIST_YPCALL; /* finish 'lua_pcall' */ |
| 422 | 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 | 434 | adjustresults(L, ci->nresults); |
| 426 | 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 | 436 | lua_unlock(L); |
| 432 | − n = (*ci->u.c.k)(L); | |
| 437 | + n = (*ci->u.c.k)(L, status, ci->u.c.ctx); | |
| 433 | 438 | lua_lock(L); |
| 434 | 439 | api_checknelems(L, n); |
| 435 | 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 | +*/ | |
| 440 | 453 | static void unroll (lua_State *L, void *ud) { |
| 441 | − UNUSED(ud); | |
| 442 | − for (;;) { | |
| 443 | − if (L->ci == &L->base_ci) /* stack is empty? */ | |
| 444 | − return; /* coroutine finished normally */ | |
| 454 | + if (ud != NULL) /* error status? */ | |
| 455 | + finishCcall(L, *(int *)ud); /* finish 'lua_pcallk' callee */ | |
| 456 | + while (L->ci != &L->base_ci) { /* something in the stack */ | |
| 445 | 457 | if (!isLua(L->ci)) /* C function? */ |
| 446 | − finishCcall(L); | |
| 458 | + finishCcall(L, LUA_YIELD); /* complete its execution */ | |
| 447 | 459 | else { /* Lua function */ |
| 448 | 460 | luaV_finishOp(L); /* finish interrupted instruction */ |
| 449 | 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 call | |
| 468 | +** Try to find a suspended protected call (a "recover point") for the | |
| 469 | +** given thread. | |
| 457 | 470 | */ |
| 458 | 471 | static CallInfo *findpcall (lua_State *L) { |
| 459 | 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 | +*/ | |
| 468 | 486 | static int recover (lua_State *L, int status) { |
| 469 | 487 | StkId oldtop; |
| 470 | 488 | CallInfo *ci = findpcall(L); |
| @@ -474,12 +492,10 @@ static int recover (lua_State *L, int status) { · 3 unmodified lines | ||
| 474 | 492 | luaF_close(L, oldtop); |
| 475 | 493 | seterrorobj(L, status, oldtop); |
| 476 | 494 | L->ci = ci; |
| 477 | − L->allowhook = ci->u.c.old_allowhook; | |
| 495 | + L->allowhook = getoah(ci->callstatus); /* restore original 'allowhook' */ | |
| 478 | 496 | L->nny = 0; /* should be zero to be yieldable */ |
| 479 | 497 | luaD_shrinkstack(L); |
| 480 | 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 | 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 mode | |
| 517 | +** Do the work for 'lua_resume' in protected mode. Most of the work | |
| 518 | +** depends on the status of the coroutine: initial state, suspended | |
| 519 | +** inside a hook, or regulary suspended (optionally with a continuation | |
| 520 | +** function), plus erroneous cases: non-suspended coroutine or dead | |
| 521 | +** coroutine. | |
| 502 | 522 | */ |
| 503 | 523 | static void resume (lua_State *L, void *ud) { |
| 504 | 524 | int nCcalls = L->nCcalls; |
| @@ -516,24 +536,22 @@ static void resume (lua_State *L, void *ud) { · 11 unmodified lines | ||
| 516 | 536 | else if (L->status != LUA_YIELD) |
| 517 | 537 | resume_error(L, "cannot resume dead coroutine", firstArg); |
| 518 | 538 | else { /* resuming from previous yield */ |
| 519 | − L->status = LUA_OK; | |
| 539 | + L->status = LUA_OK; /* mark that it is running (again) */ | |
| 520 | 540 | ci->func = restorestack(L, ci->extra); |
| 521 | 541 | if (isLua(ci)) /* yielded inside a hook? */ |
| 522 | 542 | luaV_execute(L); /* just continue running Lua code */ |
| 523 | 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 | 545 | int n; |
| 526 | − ci->u.c.status = LUA_YIELD; /* 'default' status */ | |
| 527 | − ci->callstatus |= CIST_YIELDED; | |
| 528 | 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 | 548 | lua_lock(L); |
| 531 | 549 | api_checknelems(L, n); |
| 532 | 550 | firstArg = L->top - n; /* yield results come from continuation */ |
| 533 | 551 | } |
| 534 | 552 | luaD_poscall(L, firstArg); /* finish 'luaD_precall' */ |
| 535 | 553 | } |
| 536 | − unroll(L, NULL); | |
| 554 | + unroll(L, NULL); /* run continuation */ | |
| 537 | 555 | } |
| 538 | 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 | |
| 542 | 560 | LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) { |
| 543 | 561 | int status; |
| 544 | − int oldnny = L->nny; /* save 'nny' */ | |
| 562 | + int oldnny = L->nny; /* save "number of non-yieldable" calls */ | |
| 545 | 563 | lua_lock(L); |
| 546 | 564 | luai_userstateresume(L, nargs); |
| 547 | 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 | 568 | status = luaD_rawrunprotected(L, resume, L->top - nargs); |
| 551 | 569 | if (status == -1) /* error calling 'lua_resume'? */ |
| 552 | 570 | status = LUA_ERRRUN; |
| 553 | − else { /* yield or regular error */ | |
| 554 | − while (status != LUA_OK && status != LUA_YIELD) { /* error? */ | |
| 555 | − if (recover(L, status)) /* recover point? */ | |
| 556 | − status = luaD_rawrunprotected(L, unroll, NULL); /* run continuation */ | |
| 557 | − else { /* unrecoverable error */ | |
| 558 | − L->status = cast_byte(status); /* mark thread as `dead' */ | |
| 559 | − seterrorobj(L, status, L->top); | |
| 560 | − L->ci->top = L->top; | |
| 561 | − break; | |
| 562 | − } | |
| 571 | + else { /* continue running after recoverable errors */ | |
| 572 | + while (errorstatus(status) && recover(L, status)) { | |
| 573 | + /* unroll continuation */ | |
| 574 | + status = luaD_rawrunprotected(L, unroll, &status); | |
| 575 | + } | |
| 576 | + if (errorstatus(status)) { /* unrecoverable error? */ | |
| 577 | + L->status = cast_byte(status); /* mark thread as `dead' */ | |
| 578 | + seterrorobj(L, status, L->top); /* push error message */ | |
| 579 | + L->ci->top = L->top; | |
| 563 | 580 | } |
| 564 | − lua_assert(status == L->status); | |
| 581 | + else lua_assert(status == L->status); /* normal end or yield */ | |
| 565 | 582 | } |
| 566 | 583 | L->nny = oldnny; /* restore 'nny' */ |
| 567 | 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 | |
| 574 | −LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k) { | |
| 591 | +LUA_API int lua_isyieldable (lua_State *L) { | |
| 592 | + return (L->nny == 0); | |
| 593 | +} | |
| 594 | + | |
| 595 | + | |
| 596 | +LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_KFunction k) { | |
| 575 | 597 | CallInfo *ci = L->ci; |
| 576 | 598 | luai_userstateyield(L, nresults); |
| 577 | 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 | |
| 647 | 669 | static void f_parser (lua_State *L, void *ud) { |
| 648 | − Closure *cl; | |
| 670 | + LClosure *cl; | |
| 649 | 671 | struct SParser *p = cast(struct SParser *, ud); |
| 650 | 672 | int c = zgetc(p->z); /* read first character */ |
| 651 | 673 | if (c == LUA_SIGNATURE[0]) { |
| @@ -656,8 +678,8 @@ static void f_parser (lua_State *L, void *ud) { · 4 unmodified lines | ||
| 656 | 678 | checkmode(L, p->mode, "text"); |
| 657 | 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); | |
| 660 | − luaF_initupvals(L, &cl->l); | |
| 681 | + lua_assert(cl->nupvalues == cl->p->sizeupvalues); | |
| 682 | + luaF_initupvals(L, cl); | |
| 661 | 683 | } |
| 662 | 684 | |
| 663 | 685 | |
| @@ -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 | 3 | ** save precompiled Lua chunks |
| 4 | 4 | ** See Copyright Notice in lua.h |
| 5 | 5 | */ |
| @@ -27,7 +27,7 @@ typedef struct { · 21 unmodified lines | ||
| 27 | 27 | |
| 28 | 28 | /* |
| 29 | 29 | ** All high-level dumps go through DumpVector; you can change it to |
| 30 | −** change the endianess of the result | |
| 30 | +** change the endianness of the result | |
| 31 | 31 | */ |
| 32 | 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 | 73 | else { |
| 74 | 74 | size_t size = s->tsv.len + 1; /* include trailing '\0' */ |
| 75 | 75 | if (size < 0xFF) |
| 76 | − DumpByte(size, D); | |
| 76 | + DumpByte(cast_int(size), D); | |
| 77 | 77 | else { |
| 78 | 78 | DumpByte(0xFF, D); |
| 79 | 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 | |
| 92 | −static void DumpFunction(const Proto *f, DumpState *D); | |
| 92 | +static void DumpFunction(const Proto *f, TString *psource, DumpState *D); | |
| 93 | 93 | |
| 94 | 94 | static void DumpConstants (const Proto *f, DumpState *D) { |
| 95 | 95 | int i; |
| @@ -118,10 +118,15 @@ static void DumpConstants (const Proto *f, DumpState *D) { · 22 unmodified lines | ||
| 118 | 118 | lua_assert(0); |
| 119 | 119 | } |
| 120 | 120 | } |
| 121 | − n = f->sizep; | |
| 121 | +} | |
| 122 | + | |
| 123 | + | |
| 124 | +static void DumpProtos (const Proto *f, DumpState *D) { | |
| 125 | + int i; | |
| 126 | + int n = f->sizep; | |
| 122 | 127 | DumpInt(n, D); |
| 123 | 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 | |
| 138 | 143 | static void DumpDebug (const Proto *f, DumpState *D) { |
| 139 | 144 | int i, n; |
| 140 | − DumpString((D->strip) ? NULL : f->source, D); | |
| 141 | 145 | n = (D->strip) ? 0 : f->sizelineinfo; |
| 142 | 146 | DumpInt(n, D); |
| 143 | 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 | |
| 158 | −static void DumpFunction (const Proto *f, DumpState *D) { | |
| 162 | +static void DumpFunction (const Proto *f, TString *psource, DumpState *D) { | |
| 163 | + if (D->strip || f->source == psource) | |
| 164 | + DumpString(NULL, D); /* no debug info or same source as its parent */ | |
| 165 | + else | |
| 166 | + DumpString(f->source, D); | |
| 159 | 167 | DumpInt(f->linedefined, D); |
| 160 | 168 | DumpInt(f->lastlinedefined, D); |
| 161 | 169 | DumpByte(f->numparams, D); |
| @@ -164,6 +172,7 @@ static void DumpFunction (const Proto *f, DumpState *D) { · 2 unmodified lines | ||
| 164 | 172 | DumpCode(f, D); |
| 165 | 173 | DumpConstants(f, D); |
| 166 | 174 | DumpUpvalues(f, D); |
| 175 | + DumpProtos(f, D); | |
| 167 | 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 | 205 | D.status = 0; |
| 197 | 206 | DumpHeader(&D); |
| 198 | 207 | DumpByte(f->sizeupvalues, &D); |
| 199 | − DumpFunction(f, &D); | |
| 208 | + DumpFunction(f, NULL, &D); | |
| 200 | 209 | return D.status; |
| 201 | 210 | } |
| 202 | 211 | |
| @@ -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 | 3 | ** Auxiliary functions to manipulate prototypes and closures |
| 4 | 4 | ** See Copyright Notice in lua.h |
| 5 | 5 | */ |
| @@ -20,18 +20,20 @@ · 14 unmodified lines | ||
| 20 | 20 | |
| 21 | 21 | |
| 22 | 22 | |
| 23 | −Closure *luaF_newCclosure (lua_State *L, int n) { | |
| 24 | − Closure *c = &luaC_newobj(L, LUA_TCCL, sizeCclosure(n))->cl; | |
| 25 | − c->c.nupvalues = cast_byte(n); | |
| 23 | +CClosure *luaF_newCclosure (lua_State *L, int n) { | |
| 24 | + GCObject *o = luaC_newobj(L, LUA_TCCL, sizeCclosure(n)); | |
| 25 | + CClosure *c = gco2ccl(o); | |
| 26 | + c->nupvalues = cast_byte(n); | |
| 26 | 27 | return c; |
| 27 | 28 | } |
| 28 | 29 | |
| 29 | 30 | |
| 30 | −Closure *luaF_newLclosure (lua_State *L, int n) { | |
| 31 | − Closure *c = &luaC_newobj(L, LUA_TLCL, sizeLclosure(n))->cl; | |
| 32 | − c->l.p = NULL; | |
| 33 | − c->l.nupvalues = cast_byte(n); | |
| 34 | − while (n--) c->l.upvals[n] = NULL; | |
| 31 | +LClosure *luaF_newLclosure (lua_State *L, int n) { | |
| 32 | + GCObject *o = luaC_newobj(L, LUA_TLCL, sizeLclosure(n)); | |
| 33 | + LClosure *c = gco2lcl(o); | |
| 34 | + c->p = NULL; | |
| 35 | + c->nupvalues = cast_byte(n); | |
| 36 | + while (n--) c->upvals[n] = NULL; | |
| 35 | 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 | |
| 95 | 97 | Proto *luaF_newproto (lua_State *L) { |
| 96 | − Proto *f = &luaC_newobj(L, LUA_TPROTO, sizeof(Proto))->p; | |
| 98 | + GCObject *o = luaC_newobj(L, LUA_TPROTO, sizeof(Proto)); | |
| 99 | + Proto *f = gco2p(o); | |
| 97 | 100 | f->k = NULL; |
| 98 | 101 | f->sizek = 0; |
| 99 | 102 | f->p = NULL; |
| @@ -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 | 3 | ** Auxiliary functions to manipulate prototypes and closures |
| 4 | 4 | ** See Copyright Notice in lua.h |
| 5 | 5 | */ |
| @@ -41,8 +41,8 @@ struct UpVal { · 35 unmodified lines | ||
| 41 | 41 | |
| 42 | 42 | |
| 43 | 43 | LUAI_FUNC Proto *luaF_newproto (lua_State *L); |
| 44 | −LUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems); | |
| 45 | −LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, int nelems); | |
| 44 | +LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nelems); | |
| 45 | +LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nelems); | |
| 46 | 46 | LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); |
| 47 | 47 | LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); |
| 48 | 48 | LUAI_FUNC void luaF_close (lua_State *L, StkId level); |
| @@ -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 | 3 | ** Garbage Collector |
| 4 | 4 | ** See Copyright Notice in lua.h |
| 5 | 5 | */ |
| @@ -172,7 +172,7 @@ void luaC_barrierback_ (lua_State *L, GCObject *o) { · 166 unmodified lines | ||
| 172 | 172 | ** closures pointing to it. So, we assume that the object being assigned |
| 173 | 173 | ** must be marked. |
| 174 | 174 | */ |
| 175 | −LUAI_FUNC void luaC_upvalbarrier_ (lua_State *L, UpVal *uv) { | |
| 175 | +void luaC_upvalbarrier_ (lua_State *L, UpVal *uv) { | |
| 176 | 176 | global_State *g = G(L); |
| 177 | 177 | GCObject *o = gcvalue(uv->v); |
| 178 | 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 | |
| 498 | −static lu_mem traversestack (global_State *g, lua_State *th) { | |
| 499 | − int n = 0; | |
| 498 | +static lu_mem traversethread (global_State *g, lua_State *th) { | |
| 500 | 499 | StkId o = th->stack; |
| 501 | 500 | if (o == NULL) |
| 502 | 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 | 513 | g->twups = th; |
| 515 | 514 | } |
| 516 | 515 | } |
| 517 | − else { | |
| 518 | − CallInfo *ci; | |
| 519 | − for (ci = &th->base_ci; ci != th->ci; ci = ci->next) | |
| 520 | − n++; /* count call infos to compute size */ | |
| 521 | − /* should not change the stack during an emergency gc cycle */ | |
| 522 | − if (g->gckind != KGC_EMERGENCY) | |
| 523 | − luaD_shrinkstack(th); | |
| 524 | − } | |
| 525 | − return sizeof(lua_State) + sizeof(TValue) * th->stacksize + | |
| 526 | − sizeof(CallInfo) * n; | |
| 516 | + else if (g->gckind != KGC_EMERGENCY) | |
| 517 | + luaD_shrinkstack(th); /* do not change stack in emergency cycle */ | |
| 518 | + return (sizeof(lua_State) + sizeof(TValue) * th->stacksize); | |
| 527 | 519 | } |
| 528 | 520 | |
| 529 | 521 | |
| @@ -561,7 +553,7 @@ static void propagatemark (global_State *g) { · 31 unmodified lines | ||
| 561 | 553 | th->gclist = g->grayagain; |
| 562 | 554 | g->grayagain = o; /* insert into 'grayagain' list */ |
| 563 | 555 | black2gray(o); |
| 564 | − size = traversestack(g, th); | |
| 556 | + size = traversethread(g, th); | |
| 565 | 557 | break; |
| 566 | 558 | } |
| 567 | 559 | case LUA_TPROTO: { |
| @@ -777,9 +769,11 @@ static GCObject **sweeptolive (lua_State *L, GCObject **p, int *n) { · 209 unmodified lines | ||
| 777 | 769 | */ |
| 778 | 770 | static void checkSizes (lua_State *L, global_State *g) { |
| 779 | 771 | if (g->gckind != KGC_EMERGENCY) { |
| 772 | + l_mem olddebt = g->GCdebt; | |
| 780 | 773 | luaZ_freebuffer(L, &g->buff); /* free concatenation buffer */ |
| 781 | 774 | if (g->strt.nuse < g->strt.size / 4) /* string table too big? */ |
| 782 | 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 | +*/ | |
| 835 | +static int runafewfinalizers (lua_State *L) { | |
| 836 | + global_State *g = G(L); | |
| 837 | + unsigned int i; | |
| 838 | + lua_assert(!g->tobefnz || g->gcfinnum > 0); | |
| 839 | + for (i = 0; g->tobefnz && i < g->gcfinnum; i++) | |
| 840 | + GCTM(L, 1); /* call one finalizer */ | |
| 841 | + g->gcfinnum = (!g->tobefnz) ? 0 /* nothing more to finalize? */ | |
| 842 | + : g->gcfinnum * 2; /* else call a few more next time */ | |
| 843 | + return i; | |
| 844 | +} | |
| 845 | + | |
| 846 | + | |
| 838 | 847 | /* |
| 839 | 848 | ** call all pending finalizers |
| 840 | 849 | */ |
| @@ -888,18 +897,17 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { · 47 unmodified lines | ||
| 888 | 897 | return; /* nothing to be done */ |
| 889 | 898 | else { /* move 'o' to 'finobj' list */ |
| 890 | 899 | GCObject **p; |
| 891 | − if (g->sweepgc == &o->gch.next) { /* avoid removing current sweep object */ | |
| 892 | − lua_assert(issweepphase(g)); | |
| 893 | − g->sweepgc = sweeptolive(L, g->sweepgc, NULL); | |
| 900 | + if (issweepphase(g)) { | |
| 901 | + makewhite(g, o); /* "sweep" object 'o' */ | |
| 902 | + if (g->sweepgc == &o->gch.next) /* shoud not remove 'sweepgc' object */ | |
| 903 | + g->sweepgc = sweeptolive(L, g->sweepgc, NULL); /* change 'sweepgc' */ | |
| 894 | 904 | } |
| 895 | 905 | /* search for pointer pointing to 'o' */ |
| 896 | 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 | 908 | o->gch.next = g->finobj; /* link it in "fin" list */ |
| 899 | 909 | g->finobj = o; |
| 900 | 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 | 926 | ** set a reasonable "time" to wait before starting a new GC cycle; |
| 919 | 927 | ** cycle will start when memory use hits threshold |
| 920 | 928 | */ |
| 921 | −static void setpause (global_State *g, l_mem estimate) { | |
| 929 | +static void setpause (global_State *g) { | |
| 922 | 930 | l_mem threshold, debt; |
| 923 | − estimate = estimate / PAUSEADJ; /* adjust 'estimate' */ | |
| 931 | + l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */ | |
| 924 | 932 | threshold = (g->gcpause < MAX_LMEM / estimate) /* overflow? */ |
| 925 | 933 | ? estimate * g->gcpause /* no overflow */ |
| 926 | 934 | : MAX_LMEM; /* overflow; truncate to maximum */ |
| @@ -964,8 +972,9 @@ void luaC_freeallobjects (lua_State *L) { · 37 unmodified lines | ||
| 964 | 972 | |
| 965 | 973 | static l_mem atomic (lua_State *L) { |
| 966 | 974 | global_State *g = G(L); |
| 967 | − l_mem work = -cast(l_mem, g->GCmemtrav); /* start counting work */ | |
| 975 | + l_mem work; | |
| 968 | 976 | GCObject *origweak, *origall; |
| 977 | + g->GCmemtrav = 0; /* start counting work */ | |
| 969 | 978 | lua_assert(!iswhite(obj2gco(g->mainthread))); |
| 970 | 979 | g->gcstate = GCSinsideatomic; |
| 971 | 980 | markobject(g, L); /* mark running thread */ |
| @@ -975,10 +984,10 @@ static l_mem atomic (lua_State *L) { · 3 unmodified lines | ||
| 975 | 984 | /* remark occasional upvalues of (maybe) dead threads */ |
| 976 | 985 | remarkupvals(g); |
| 977 | 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 | 988 | /* traverse objects caught by write barrier and by 'remarkupvals' */ |
| 980 | 989 | retraversegrays(g); |
| 981 | − work -= g->GCmemtrav; /* restart counting */ | |
| 990 | + g->GCmemtrav = 0; /* restart counting */ | |
| 982 | 991 | convergeephemerons(g); |
| 983 | 992 | /* at this point, all strongly accessible objects are marked. */ |
| 984 | 993 | /* Clear values from weak tables, before checking finalizers */ |
| @@ -990,7 +999,7 @@ static l_mem atomic (lua_State *L) { · 5 unmodified lines | ||
| 990 | 999 | g->gcfinnum = 1; /* there may be objects to be finalized */ |
| 991 | 1000 | markbeingfnz(g); /* mark objects that will be finalized */ |
| 992 | 1001 | propagateall(g); /* remark, to propagate 'resurrection' */ |
| 993 | − work -= g->GCmemtrav; /* restart counting */ | |
| 1002 | + g->GCmemtrav = 0; /* restart counting */ | |
| 994 | 1003 | convergeephemerons(g); |
| 995 | 1004 | /* at this point, all resurrected objects are marked. */ |
| 996 | 1005 | /* remove dead objects from weak tables */ |
| @@ -1008,7 +1017,9 @@ static l_mem atomic (lua_State *L) { · 11 unmodified lines | ||
| 1008 | 1017 | static lu_mem sweepstep (lua_State *L, global_State *g, |
| 1009 | 1018 | int nextstate, GCObject **nextlist) { |
| 1010 | 1019 | if (g->sweepgc) { |
| 1020 | + l_mem olddebt = g->GCdebt; | |
| 1011 | 1021 | g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX); |
| 1022 | + g->GCestimate += g->GCdebt - olddebt; /* update estimate */ | |
| 1012 | 1023 | if (g->sweepgc) /* is there still something to sweep? */ |
| 1013 | 1024 | return (GCSWEEPMAX * GCSWEEPCOST); |
| 1014 | 1025 | } |
| @@ -1023,28 +1034,26 @@ static lu_mem singlestep (lua_State *L) { · 8 unmodified lines | ||
| 1023 | 1034 | global_State *g = G(L); |
| 1024 | 1035 | switch (g->gcstate) { |
| 1025 | 1036 | case GCSpause: { |
| 1026 | − /* start to count memory traversed */ | |
| 1027 | 1037 | g->GCmemtrav = g->strt.size * sizeof(GCObject*); |
| 1028 | 1038 | restartcollection(g); |
| 1029 | 1039 | g->gcstate = GCSpropagate; |
| 1030 | 1040 | return g->GCmemtrav; |
| 1031 | 1041 | } |
| 1032 | 1042 | case GCSpropagate: { |
| 1033 | − lu_mem oldtrav = g->GCmemtrav; | |
| 1043 | + g->GCmemtrav = 0; | |
| 1034 | 1044 | lua_assert(g->gray); |
| 1035 | 1045 | propagatemark(g); |
| 1036 | 1046 | if (g->gray == NULL) /* no more `gray' objects? */ |
| 1037 | 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 | 1050 | case GCSatomic: { |
| 1041 | 1051 | lu_mem work; |
| 1042 | 1052 | int sw; |
| 1043 | 1053 | propagateall(g); /* make sure gray list is empty */ |
| 1044 | − g->GCestimate = g->GCmemtrav; /* save what was counted */; | |
| 1045 | 1054 | work = atomic(L); /* work is what was traversed by 'atomic' */ |
| 1046 | − g->GCestimate += work; /* estimate of total memory traversed */ | |
| 1047 | 1055 | sw = entersweep(L); |
| 1056 | + g->GCestimate = gettotalbytes(g); /* first estimate */; | |
| 1048 | 1057 | return work + sw * GCSWEEPCOST; |
| 1049 | 1058 | } |
| 1050 | 1059 | case GCSswpallgc: { /* sweep "regular" objects */ |
| @@ -1062,10 +1071,15 @@ static lu_mem singlestep (lua_State *L) { · 11 unmodified lines | ||
| 1062 | 1071 | g->gcstate = GCScallfin; |
| 1063 | 1072 | return 0; |
| 1064 | 1073 | } |
| 1065 | − case GCScallfin: { /* state to finish calling finalizers */ | |
| 1066 | − /* do nothing here; should be handled by 'luaC_forcestep' */ | |
| 1067 | − g->gcstate = GCSpause; /* finish collection */ | |
| 1068 | − return 0; | |
| 1074 | + case GCScallfin: { /* call remaining finalizers */ | |
| 1075 | + if (g->tobefnz && g->gckind != KGC_EMERGENCY) { | |
| 1076 | + int n = runafewfinalizers(L); | |
| 1077 | + return (n * GCFINALIZECOST); | |
| 1078 | + } | |
| 1079 | + else { /* emergency mode or no more finalizers */ | |
| 1080 | + g->gcstate = GCSpause; /* finish collection */ | |
| 1081 | + return 0; | |
| 1082 | + } | |
| 1069 | 1083 | } |
| 1070 | 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 | −*/ | |
| 1089 | −static int runafewfinalizers (lua_State *L) { | |
| 1090 | − global_State *g = G(L); | |
| 1091 | − unsigned int i; | |
| 1092 | − lua_assert(!g->tobefnz || g->gcfinnum > 0); | |
| 1093 | − for (i = 0; g->tobefnz && i < g->gcfinnum; i++) | |
| 1094 | − GCTM(L, 1); /* call one finalizer */ | |
| 1095 | − g->gcfinnum = (!g->tobefnz) ? 0 /* nothing more to finalize? */ | |
| 1096 | − : g->gcfinnum * 2; /* else call a few more next time */ | |
| 1097 | − return i; | |
| 1098 | −} | |
| 1099 | − | |
| 1100 | − | |
| 1101 | 1100 | /* |
| 1102 | 1101 | ** get GC debt and convert it from Kb to 'work units' (avoid zero debt |
| 1103 | 1102 | ** and overflows) |
| @@ -1115,23 +1114,17 @@ static l_mem getdebt (global_State *g) { · 11 unmodified lines | ||
| 1115 | 1114 | */ |
| 1116 | 1115 | void luaC_step (lua_State *L) { |
| 1117 | 1116 | global_State *g = G(L); |
| 1118 | − l_mem debt = getdebt(g); | |
| 1117 | + l_mem debt = getdebt(g); /* GC deficit (be paid now) */ | |
| 1119 | 1118 | if (!g->gcrunning) { /* not running? */ |
| 1120 | 1119 | luaE_setdebt(g, -GCSTEPSIZE * 10); /* avoid being called too often */ |
| 1121 | 1120 | return; |
| 1122 | 1121 | } |
| 1123 | − do { | |
| 1124 | − if (g->gcstate == GCScallfin && g->tobefnz) { | |
| 1125 | − unsigned int n = runafewfinalizers(L); | |
| 1126 | − debt -= (n * GCFINALIZECOST); | |
| 1127 | − } | |
| 1128 | − else { /* perform one single step */ | |
| 1129 | − lu_mem work = singlestep(L); | |
| 1130 | − debt -= work; | |
| 1131 | − } | |
| 1122 | + do { /* repeat until pause or enough "credit" (negative debt) */ | |
| 1123 | + lu_mem work = singlestep(L); /* perform one single step */ | |
| 1124 | + debt -= work; | |
| 1132 | 1125 | } while (debt > -GCSTEPSIZE && g->gcstate != GCSpause); |
| 1133 | 1126 | if (g->gcstate == GCSpause) |
| 1134 | − setpause(g, g->GCestimate); /* pause until next cycle */ | |
| 1127 | + setpause(g); /* pause until next cycle */ | |
| 1135 | 1128 | else { |
| 1136 | 1129 | debt = (debt / g->gcstepmul) * STEPMULADJ; /* convert 'work units' to Kb */ |
| 1137 | 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 call | |
| 1145 | −** finalizers (which could change stack positions) | |
| 1137 | +** Performs a full GC cycle; if "isemergency", set a flag to avoid | |
| 1138 | +** some operations which could change the interpreter state in some | |
| 1139 | +** unexpected ways (running finalizers and shrinking some structures). | |
| 1140 | +** Before running the collection, check 'keepinvariant'; if it is true, | |
| 1141 | +** there may be some objects marked as black, so the collector has | |
| 1142 | +** to sweep all objects to turn them back to white (as white has not | |
| 1143 | +** changed, nothing will be collected). | |
| 1146 | 1144 | */ |
| 1147 | 1145 | void luaC_fullgc (lua_State *L, int isemergency) { |
| 1148 | 1146 | global_State *g = G(L); |
| 1149 | 1147 | lua_assert(g->gckind == KGC_NORMAL); |
| 1150 | − if (isemergency) /* do not run finalizers during emergency GC */ | |
| 1151 | − g->gckind = KGC_EMERGENCY; | |
| 1152 | − else | |
| 1153 | − callallpendingfinalizers(L, 1); | |
| 1154 | − if (keepinvariant(g)) { /* may there be some black objects? */ | |
| 1155 | − /* must sweep all objects to turn them back to white | |
| 1156 | − (as white has not changed, nothing will be collected) */ | |
| 1157 | − entersweep(L); | |
| 1148 | + if (isemergency) g->gckind = KGC_EMERGENCY; /* set flag */ | |
| 1149 | + if (keepinvariant(g)) { /* black objects? */ | |
| 1150 | + entersweep(L); /* sweep everything to turn them back to white */ | |
| 1158 | 1151 | } |
| 1159 | 1152 | /* finish any pending sweep phase to start a new cycle */ |
| 1160 | 1153 | luaC_runtilstate(L, bitmask(GCSpause)); |
| 1161 | 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 | 1159 | g->gckind = KGC_NORMAL; |
| 1164 | − setpause(g, gettotalbytes(g)); | |
| 1165 | − if (!isemergency) /* do not run finalizers during emergency GC */ | |
| 1166 | − callallpendingfinalizers(L, 1); | |
| 1160 | + setpause(g); | |
| 1167 | 1161 | } |
| 1168 | 1162 | |
| 1169 | 1163 | /* }====================================================== */ |
| @@ -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 | 3 | ** Initialization of libraries for lua.c and other clients |
| 4 | 4 | ** See Copyright Notice in lua.h |
| 5 | 5 | */ |
| @@ -34,10 +34,12 @@ static const luaL_Reg loadedlibs[] = { · 28 unmodified lines | ||
| 34 | 34 | {LUA_IOLIBNAME, luaopen_io}, |
| 35 | 35 | {LUA_OSLIBNAME, luaopen_os}, |
| 36 | 36 | {LUA_STRLIBNAME, luaopen_string}, |
| 37 | − {LUA_UTF8LIBNAME, luaopen_utf8}, | |
| 38 | − {LUA_BITLIBNAME, luaopen_bit32}, | |
| 39 | 37 | {LUA_MATHLIBNAME, luaopen_math}, |
| 40 | 38 | {LUA_DBLIBNAME, luaopen_debug}, |
| 39 | + {LUA_UTF8LIBNAME, luaopen_utf8}, | |
| 40 | +#if defined(LUA_COMPAT_BITLIB) | |
| 41 | + {LUA_BITLIBNAME, luaopen_bit32}, | |
| 42 | +#endif | |
| 41 | 43 | {NULL, NULL} |
| 42 | 44 | }; |
| 43 | 45 | |
| @@ -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 | 3 | ** Standard I/O (and system) library |
| 4 | 4 | ** See Copyright Notice in lua.h |
| 5 | 5 | */ |
| @@ -15,7 +15,9 @@ · 9 unmodified lines | ||
| 15 | 15 | #endif |
| 16 | 16 | |
| 17 | 17 | |
| 18 | +#include <ctype.h> | |
| 18 | 19 | #include <errno.h> |
| 20 | +#include <locale.h> | |
| 19 | 21 | #include <stdio.h> |
| 20 | 22 | #include <stdlib.h> |
| 21 | 23 | #include <string.h> |
| @@ -104,6 +106,8 @@ · 82 unmodified lines | ||
| 104 | 106 | |
| 105 | 107 | #if defined(LUA_USE_POSIX) /* { */ |
| 106 | 108 | |
| 109 | +#include <sys/types.h> | |
| 110 | + | |
| 107 | 111 | #define l_fseek(f,o,w) fseeko(f,o,w) |
| 108 | 112 | #define l_ftell(f) ftello(f) |
| 109 | 113 | #define l_seeknum off_t |
| @@ -317,14 +321,10 @@ static int io_readline (lua_State *L); · 207 unmodified lines | ||
| 317 | 321 | |
| 318 | 322 | |
| 319 | 323 | static void aux_lines (lua_State *L, int toclose) { |
| 320 | − int i; | |
| 321 | 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 | 325 | lua_pushinteger(L, n); /* number of arguments to read */ |
| 326 | 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 | 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 | |
| 366 | −static int read_integer (lua_State *L, FILE *f) { | |
| 367 | − lua_Integer d; | |
| 368 | − if (fscanf(f, LUA_INTEGER_SCAN, &d) == 1) { | |
| 369 | − lua_pushinteger(L, d); | |
| 370 | − return 1; | |
| 366 | +/* maximum length of a numeral */ | |
| 367 | +#define MAXRN 200 | |
| 368 | + | |
| 369 | +/* auxiliary structure used by 'read_number' */ | |
| 370 | +typedef struct { | |
| 371 | + FILE *f; /* file being read */ | |
| 372 | + int c; /* current character (look ahead) */ | |
| 373 | + int n; /* number of elements in buffer 'buff' */ | |
| 374 | + char buff[MAXRN + 1]; /* +1 for ending '\0' */ | |
| 375 | +} RN; | |
| 376 | + | |
| 377 | + | |
| 378 | +/* | |
| 379 | +** Add current char to buffer (if not out of space) and read next one | |
| 380 | +*/ | |
| 381 | +static int nextc (RN *rn) { | |
| 382 | + if (rn->n >= MAXRN) { /* buffer overflow? */ | |
| 383 | + rn->buff[0] = '\0'; /* invalidate result */ | |
| 384 | + return 0; /* fail */ | |
| 371 | 385 | } |
| 372 | 386 | else { |
| 373 | − lua_pushnil(L); /* "result" to be removed */ | |
| 374 | − return 0; /* read fails */ | |
| 387 | + rn->buff[rn->n++] = rn->c; /* save current char */ | |
| 388 | + rn->c = l_getc(rn->f); /* read next one */ | |
| 389 | + return 1; | |
| 375 | 390 | } |
| 376 | 391 | } |
| 377 | 392 | |
| 378 | 393 | |
| 394 | +/* | |
| 395 | +** Accept current char if it is in 'set' (of size 1 or 2) | |
| 396 | +*/ | |
| 397 | +static int test2 (RN *rn, const char *set) { | |
| 398 | + if (rn->c == set[0] || (rn->c == set[1] && rn->c != '\0')) | |
| 399 | + return nextc(rn); | |
| 400 | + else return 0; | |
| 401 | +} | |
| 402 | + | |
| 403 | + | |
| 404 | +/* | |
| 405 | +** Read a sequence of (hexa)digits | |
| 406 | +*/ | |
| 407 | +static int readdigits (RN *rn, int hexa) { | |
| 408 | + int count = 0; | |
| 409 | + while ((hexa ? isxdigit(rn->c) : isdigit(rn->c)) && nextc(rn)) | |
| 410 | + count++; | |
| 411 | + return count; | |
| 412 | +} | |
| 413 | + | |
| 414 | + | |
| 415 | +/* access to locale "radix character" (decimal point) */ | |
| 416 | +#if !defined(getlocaledecpoint) | |
| 417 | +#define getlocaledecpoint() (localeconv()->decimal_point[0]) | |
| 418 | +#endif | |
| 419 | + | |
| 420 | + | |
| 421 | +/* | |
| 422 | +** Read a number: first reads a valid prefix of a numeral into a buffer. | |
| 423 | +** Then it calls 'lua_strtonum' to check whether the format is correct | |
| 424 | +** and to convert it to a Lua number | |
| 425 | +*/ | |
| 379 | 426 | static int read_number (lua_State *L, FILE *f) { |
| 380 | − lua_Number d; | |
| 381 | − if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) { | |
| 382 | − lua_pushnumber(L, d); | |
| 383 | − return 1; | |
| 427 | + RN rn; | |
| 428 | + int count = 0; | |
| 429 | + int hexa = 0; | |
| 430 | + char decp[2] = "."; | |
| 431 | + rn.f = f; rn.n = 0; | |
| 432 | + decp[0] = getlocaledecpoint(); /* get decimal point from locale */ | |
| 433 | + l_lockfile(rn.f); | |
| 434 | + do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */ | |
| 435 | + test2(&rn, "-+"); /* optional signal */ | |
| 436 | + if (test2(&rn, "0")) { | |
| 437 | + if (test2(&rn, "xX")) hexa = 1; /* numeral is hexadecimal */ | |
| 438 | + else count = 1; /* count initial '0' as a valid digit */ | |
| 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 | 453 | lua_pushnil(L); /* "result" to be removed */ |
| 387 | 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 | 522 | success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); |
| 456 | 523 | } |
| 457 | 524 | else { |
| 458 | − const char *p = lua_tostring(L, n); | |
| 459 | − luaL_argcheck(L, p && p[0] == '*', n, "invalid option"); | |
| 460 | − switch (p[1]) { | |
| 461 | − case 'i': /* integer */ | |
| 462 | − success = read_integer(L, f); | |
| 463 | − break; | |
| 525 | + const char *p = luaL_checkstring(L, n); | |
| 526 | + if (*p == '*') p++; /* skip optional '*' (for compatibility) */ | |
| 527 | + switch (*p) { | |
| 464 | 528 | case 'n': /* number */ |
| 465 | 529 | success = read_number(L, f); |
| 466 | 530 | break; |
| @@ -507,6 +571,7 @@ static int io_readline (lua_State *L) { · 40 unmodified lines | ||
| 507 | 571 | if (isclosed(p)) /* file is already closed? */ |
| 508 | 572 | return luaL_error(L, "file is already closed"); |
| 509 | 573 | lua_settop(L , 1); |
| 574 | + luaL_checkstack(L, n, "too many arguments"); | |
| 510 | 575 | for (i = 1; i <= n; i++) /* push arguments to 'g_read' */ |
| 511 | 576 | lua_pushvalue(L, lua_upvalueindex(3 + i)); |
| 512 | 577 | n = g_read(L, p->f, 2); /* 'n' is number of results */ |
| @@ -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 | 3 | ** Lexical Analyzer |
| 4 | 4 | ** 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 | |
| 186 | +static int check_next1 (LexState *ls, int c) { | |
| 187 | + if (ls->current == c) { | |
| 188 | + next(ls); | |
| 189 | + return 1; | |
| 190 | + } | |
| 191 | + else return 0; | |
| 192 | +} | |
| 186 | 193 | |
| 187 | −static int check_next (LexState *ls, const char *set) { | |
| 188 | − if (ls->current == '\0' || !strchr(set, ls->current)) | |
| 189 | − return 0; | |
| 190 | − save_and_next(ls); | |
| 191 | − return 1; | |
| 194 | + | |
| 195 | +/* | |
| 196 | +** Check whether current char is in set 'set' (with two chars) and | |
| 197 | +** saves it | |
| 198 | +*/ | |
| 199 | +static int check_next2 (LexState *ls, const char *set) { | |
| 200 | + lua_assert(set[2] == '\0'); | |
| 201 | + if (ls->current == set[0] || ls->current == set[1]) { | |
| 202 | + save_and_next(ls); | |
| 203 | + return 1; | |
| 204 | + } | |
| 205 | + else return 0; | |
| 192 | 206 | } |
| 193 | 207 | |
| 194 | 208 | |
| @@ -196,10 +210,12 @@ static int check_next (LexState *ls, const char *set) { · 1 unmodified line | ||
| 196 | 210 | ** change all characters 'from' in buffer to 'to' |
| 197 | 211 | */ |
| 198 | 212 | static void buffreplace (LexState *ls, char from, char to) { |
| 199 | − size_t n = luaZ_bufflen(ls->buff); | |
| 200 | − char *p = luaZ_buffer(ls->buff); | |
| 201 | − while (n--) | |
| 202 | − if (p[n] == from) p[n] = to; | |
| 213 | + if (from != to) { | |
| 214 | + size_t n = luaZ_bufflen(ls->buff); | |
| 215 | + char *p = luaZ_buffer(ls->buff); | |
| 216 | + while (n--) | |
| 217 | + if (p[n] == from) p[n] = to; | |
| 218 | + } | |
| 203 | 219 | } |
| 204 | 220 | |
| 205 | 221 | |
| @@ -208,17 +224,17 @@ static void buffreplace (LexState *ls, char from, char to) { · 2 unmodified lines | ||
| 208 | 224 | #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 | 230 | ** in case of format error, try to change decimal point separator to |
| 215 | 231 | ** the one defined in the current locale and check again |
| 216 | 232 | */ |
| 217 | −static void trydecpoint (LexState *ls, SemInfo *seminfo) { | |
| 233 | +static void trydecpoint (LexState *ls, TValue *o) { | |
| 218 | 234 | char old = ls->decpoint; |
| 219 | 235 | ls->decpoint = getlocaledecpoint(); |
| 220 | 236 | buffreplace(ls, old, ls->decpoint); /* try new decimal separator */ |
| 221 | − if (!buff2d(ls->buff, &seminfo->r)) { | |
| 237 | + if (!buff2num(ls->buff, o)) { | |
| 222 | 238 | /* format error with correct decimal point: no more options */ |
| 223 | 239 | buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */ |
| 224 | 240 | lexerror(ls, "malformed number", TK_FLT); |
| @@ -228,41 +244,37 @@ static void trydecpoint (LexState *ls, SemInfo *seminfo) { · 3 unmodified lines | ||
| 228 | 244 | |
| 229 | 245 | /* LUA_NUMBER */ |
| 230 | 246 | /* |
| 231 | −** this function is quite liberal in what it accepts, as 'luaO_str2d' | |
| 232 | −** will reject ill-formed numerals. 'isf' means the numeral is not | |
| 233 | −** an integer (it has a dot or an exponent). | |
| 247 | +** this function is quite liberal in what it accepts, as 'luaO_str2num' | |
| 248 | +** will reject ill-formed numerals. | |
| 234 | 249 | */ |
| 235 | −static int read_numeral (LexState *ls, SemInfo *seminfo, int isf) { | |
| 250 | +static int read_numeral (LexState *ls, SemInfo *seminfo) { | |
| 251 | + TValue obj; | |
| 236 | 252 | const char *expo = "Ee"; |
| 237 | 253 | int first = ls->current; |
| 238 | 254 | lua_assert(lisdigit(ls->current)); |
| 239 | 255 | save_and_next(ls); |
| 240 | − if (first == '0' && check_next(ls, "Xx")) /* hexadecimal? */ | |
| 256 | + if (first == '0' && check_next2(ls, "xX")) /* hexadecimal? */ | |
| 241 | 257 | expo = "Pp"; |
| 242 | 258 | for (;;) { |
| 243 | − if (check_next(ls, expo)) { /* exponent part? */ | |
| 244 | − check_next(ls, "+-"); /* optional exponent sign */ | |
| 245 | − isf = 1; | |
| 246 | − } | |
| 259 | + if (check_next2(ls, expo)) /* exponent part? */ | |
| 260 | + check_next2(ls, "-+"); /* optional exponent sign */ | |
| 247 | 261 | if (lisxdigit(ls->current)) |
| 248 | 262 | save_and_next(ls); |
| 249 | − else if (ls->current == '.') { | |
| 263 | + else if (ls->current == '.') | |
| 250 | 264 | save_and_next(ls); |
| 251 | − isf = 1; | |
| 252 | − } | |
| 253 | 265 | else break; |
| 254 | 266 | } |
| 255 | 267 | save(ls, '\0'); |
| 256 | − if (!isf) { | |
| 257 | − if (!luaO_str2int(luaZ_buffer(ls->buff), luaZ_bufflen(ls->buff) - 1, | |
| 258 | − &seminfo->i)) | |
| 259 | − lexerror(ls, "malformed number", TK_INT); | |
| 268 | + buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */ | |
| 269 | + if (!buff2num(ls->buff, &obj)) /* format error? */ | |
| 270 | + trydecpoint(ls, &obj); /* try to update decimal point separator */ | |
| 271 | + if (ttisinteger(&obj)) { | |
| 272 | + seminfo->i = ivalue(&obj); | |
| 260 | 273 | return TK_INT; |
| 261 | 274 | } |
| 262 | 275 | else { |
| 263 | − buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */ | |
| 264 | − if (!buff2d(ls->buff, &seminfo->r)) /* format error? */ | |
| 265 | − trydecpoint(ls, seminfo); /* try to update decimal point separator */ | |
| 276 | + lua_assert(ttisfloat(&obj)); | |
| 277 | + seminfo->r = fltvalue(&obj); | |
| 266 | 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 | |
| 288 | 300 | static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) { |
| 301 | + int line = ls->linenumber; /* initial line (for error message) */ | |
| 289 | 302 | save_and_next(ls); /* skip 2nd `[' */ |
| 290 | 303 | if (currIsNewline(ls)) /* string starts with a newline? */ |
| 291 | 304 | inclinenumber(ls); /* skip it */ |
| 292 | 305 | for (;;) { |
| 293 | 306 | switch (ls->current) { |
| 294 | − case EOZ: | |
| 295 | − lexerror(ls, (seminfo) ? "unfinished long string" : | |
| 296 | − "unfinished long comment", TK_EOS); | |
| 307 | + case EOZ: { /* error */ | |
| 308 | + const char *what = (seminfo ? "string" : "comment"); | |
| 309 | + const char *msg = luaO_pushfstring(ls->L, | |
| 310 | + "unfinished long %s (starting at line %d)", what, line); | |
| 311 | + lexerror(ls, msg, TK_EOS); | |
| 297 | 312 | break; /* to avoid warnings */ |
| 313 | + } | |
| 298 | 314 | case ']': { |
| 299 | 315 | if (skip_sep(ls) == sep) { |
| 300 | 316 | save_and_next(ls); /* skip 2nd `]' */ |
| @@ -488,35 +504,35 @@ static int llex (LexState *ls, SemInfo *seminfo) { · 187 unmodified lines | ||
| 488 | 504 | } |
| 489 | 505 | case '=': { |
| 490 | 506 | next(ls); |
| 491 | − if (ls->current != '=') return '='; | |
| 492 | − else { next(ls); return TK_EQ; } | |
| 507 | + if (check_next1(ls, '=')) return TK_EQ; | |
| 508 | + else return '='; | |
| 493 | 509 | } |
| 494 | 510 | case '<': { |
| 495 | 511 | next(ls); |
| 496 | − if (ls->current == '=') { next(ls); return TK_LE; } | |
| 497 | − if (ls->current == '<') { next(ls); return TK_SHL; } | |
| 498 | − return '<'; | |
| 512 | + if (check_next1(ls, '=')) return TK_LE; | |
| 513 | + else if (check_next1(ls, '<')) return TK_SHL; | |
| 514 | + else return '<'; | |
| 499 | 515 | } |
| 500 | 516 | case '>': { |
| 501 | 517 | next(ls); |
| 502 | − if (ls->current == '=') { next(ls); return TK_GE; } | |
| 503 | − if (ls->current == '>') { next(ls); return TK_SHR; } | |
| 504 | − return '>'; | |
| 518 | + if (check_next1(ls, '=')) return TK_GE; | |
| 519 | + else if (check_next1(ls, '>')) return TK_SHR; | |
| 520 | + else return '>'; | |
| 505 | 521 | } |
| 506 | 522 | case '/': { |
| 507 | 523 | next(ls); |
| 508 | − if (ls->current != '/') return '/'; | |
| 509 | − else { next(ls); return TK_IDIV; } | |
| 524 | + if (check_next1(ls, '/')) return TK_IDIV; | |
| 525 | + else return '/'; | |
| 510 | 526 | } |
| 511 | 527 | case '~': { |
| 512 | 528 | next(ls); |
| 513 | − if (ls->current != '=') return '~'; | |
| 514 | − else { next(ls); return TK_NE; } | |
| 529 | + if (check_next1(ls, '=')) return TK_NE; | |
| 530 | + else return '~'; | |
| 515 | 531 | } |
| 516 | 532 | case ':': { |
| 517 | 533 | next(ls); |
| 518 | − if (ls->current != ':') return ':'; | |
| 519 | − else { next(ls); return TK_DBCOLON; } | |
| 534 | + if (check_next1(ls, ':')) return TK_DBCOLON; | |
| 535 | + else return ':'; | |
| 520 | 536 | } |
| 521 | 537 | case '"': case '\'': { /* short literal strings */ |
| 522 | 538 | read_string(ls, ls->current, seminfo); |
| @@ -524,17 +540,17 @@ static int llex (LexState *ls, SemInfo *seminfo) { · 1 unmodified line | ||
| 524 | 540 | } |
| 525 | 541 | case '.': { /* '.', '..', '...', or number */ |
| 526 | 542 | save_and_next(ls); |
| 527 | − if (check_next(ls, ".")) { | |
| 528 | − if (check_next(ls, ".")) | |
| 543 | + if (check_next1(ls, '.')) { | |
| 544 | + if (check_next1(ls, '.')) | |
| 529 | 545 | return TK_DOTS; /* '...' */ |
| 530 | 546 | else return TK_CONCAT; /* '..' */ |
| 531 | 547 | } |
| 532 | 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 | 551 | case '0': case '1': case '2': case '3': case '4': |
| 536 | 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 | 555 | case EOZ: { |
| 540 | 556 | return TK_EOS; |
| @@ -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 | 3 | ** Limits, basic types, and some other `installation-dependent' definitions |
| 4 | 4 | ** See Copyright Notice in lua.h |
| 5 | 5 | */ |
| @@ -31,8 +31,8 @@ typedef unsigned char lu_byte; · 25 unmodified lines | ||
| 31 | 31 | #define MAX_SIZET ((size_t)(~(size_t)0)-2) |
| 32 | 32 | |
| 33 | 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 \ | |
| 35 | − : (size_t)(~(lua_Unsigned)0)-2) | |
| 34 | +#define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \ | |
| 35 | + : (size_t)(LUA_MAXINTEGER)-2) | |
| 36 | 36 | |
| 37 | 37 | |
| 38 | 38 | #define MAX_LUMEM ((lu_mem)(~(lu_mem)0)-2) |
| @@ -43,13 +43,6 @@ typedef unsigned char lu_byte; · 4 unmodified lines | ||
| 43 | 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 | 47 | ** conversion of pointer to integer |
| 55 | 48 | ** this is for hashing only; there is no problem if the integer |
| @@ -67,8 +60,9 @@ typedef unsigned char lu_byte; · 11 unmodified lines | ||
| 67 | 60 | typedef 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 */ | |
| 71 | 64 | typedef LUAI_UACNUMBER l_uacNumber; |
| 65 | +typedef LUAI_UACINT l_uacInt; | |
| 72 | 66 | |
| 73 | 67 | |
| 74 | 68 | /* internal assertions for in-house debugging */ |
| @@ -111,8 +105,21 @@ typedef LUAI_UACNUMBER l_uacNumber; · 36 unmodified lines | ||
| 111 | 105 | #define cast_num(i) cast(lua_Number, (i)) |
| 112 | 106 | #define cast_int(i) cast(int, (i)) |
| 113 | 107 | #define cast_uchar(i) cast(unsigned char, (i)) |
| 114 | −#define cast_integer(i) cast(lua_Integer, (i)) | |
| 115 | −#define cast_unsigned(i) cast(lua_Unsigned, (i)) | |
| 108 | + | |
| 109 | + | |
| 110 | +/* cast a signed lua_Integer to lua_Unsigned */ | |
| 111 | +#if !defined(l_castS2U) | |
| 112 | +#define l_castS2U(i) ((lua_Unsigned)(i)) | |
| 113 | +#endif | |
| 114 | + | |
| 115 | +/* | |
| 116 | +** cast a lua_Unsigned to a signed lua_Integer; this cast is | |
| 117 | +** not strict ANSI C, but two-complement architectures should | |
| 118 | +** work fine. | |
| 119 | +*/ | |
| 120 | +#if !defined(l_castU2S) | |
| 121 | +#define l_castU2S(i) ((lua_Integer)(i)) | |
| 122 | +#endif | |
| 116 | 123 | |
| 117 | 124 | |
| 118 | 125 | /* |
| @@ -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 | 3 | ** Standard mathematical library |
| 4 | 4 | ** See Copyright Notice in lua.h |
| 5 | 5 | */ |
| @@ -18,12 +18,28 @@ · 12 unmodified lines | ||
| 18 | 18 | |
| 19 | 19 | |
| 20 | 20 | #undef PI |
| 21 | −#define PI ((lua_Number)(3.1415926535897932384626433832795)) | |
| 22 | −#define RADIANS_PER_DEGREE ((lua_Number)(PI/180.0)) | |
| 21 | +#define PI (l_mathop(3.141592653589793238462643383279502884)) | |
| 22 | + | |
| 23 | + | |
| 24 | +#if !defined(l_rand) /* { */ | |
| 25 | +#if defined(LUA_USE_POSIX) | |
| 26 | +#define l_rand() random() | |
| 27 | +#define l_srand(x) srandom(x) | |
| 28 | +#else | |
| 29 | +#define l_rand() rand() | |
| 30 | +#define l_srand(x) srand(x) | |
| 31 | +#endif | |
| 32 | +#endif /* } */ | |
| 23 | 33 | |
| 24 | 34 | |
| 25 | 35 | static int math_abs (lua_State *L) { |
| 26 | − lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1))); | |
| 36 | + if (lua_isinteger(L, 1)) { | |
| 37 | + lua_Integer n = lua_tointeger(L, 1); | |
| 38 | + if (n < 0) n = (lua_Integer)(0u - n); | |
| 39 | + lua_pushinteger(L, n); | |
| 40 | + } | |
| 41 | + else | |
| 42 | + lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1))); | |
| 27 | 43 | return 1; |
| 28 | 44 | } |
| 29 | 45 | |
| @@ -32,31 +48,16 @@ static int math_sin (lua_State *L) { · 2 unmodified lines | ||
| 32 | 48 | return 1; |
| 33 | 49 | } |
| 34 | 50 | |
| 35 | −static int math_sinh (lua_State *L) { | |
| 36 | − lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1))); | |
| 37 | − return 1; | |
| 38 | −} | |
| 39 | − | |
| 40 | 51 | static int math_cos (lua_State *L) { |
| 41 | 52 | lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1))); |
| 42 | 53 | return 1; |
| 43 | 54 | } |
| 44 | 55 | |
| 45 | −static int math_cosh (lua_State *L) { | |
| 46 | − lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1))); | |
| 47 | − return 1; | |
| 48 | −} | |
| 49 | − | |
| 50 | 56 | static int math_tan (lua_State *L) { |
| 51 | 57 | lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1))); |
| 52 | 58 | return 1; |
| 53 | 59 | } |
| 54 | 60 | |
| 55 | −static int math_tanh (lua_State *L) { | |
| 56 | − lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1))); | |
| 57 | − return 1; | |
| 58 | −} | |
| 59 | − | |
| 60 | 61 | static int math_asin (lua_State *L) { |
| 61 | 62 | lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1))); |
| 62 | 63 | return 1; |
| @@ -68,56 +69,92 @@ static int math_acos (lua_State *L) { · 5 unmodified lines | ||
| 68 | 69 | } |
| 69 | 70 | |
| 70 | 71 | static int math_atan (lua_State *L) { |
| 71 | − lua_pushnumber(L, l_mathop(atan)(luaL_checknumber(L, 1))); | |
| 72 | + lua_Number y = luaL_checknumber(L, 1); | |
| 73 | + lua_Number x = luaL_optnumber(L, 2, 1); | |
| 74 | + lua_pushnumber(L, l_mathop(atan2)(y, x)); | |
| 72 | 75 | return 1; |
| 73 | 76 | } |
| 74 | 77 | |
| 75 | −static int math_atan2 (lua_State *L) { | |
| 76 | − lua_pushnumber(L, l_mathop(atan2)(luaL_checknumber(L, 1), | |
| 77 | − luaL_checknumber(L, 2))); | |
| 78 | − return 1; | |
| 79 | −} | |
| 80 | 78 | |
| 81 | −static int math_ceil (lua_State *L) { | |
| 82 | − lua_pushnumber(L, l_mathop(ceil)(luaL_checknumber(L, 1))); | |
| 79 | +static int math_ifloor (lua_State *L) { | |
| 80 | + int valid; | |
| 81 | + lua_Integer n = lua_tointegerx(L, 1, &valid); | |
| 82 | + if (valid) | |
| 83 | + lua_pushinteger(L, n); /* floor computed by Lua */ | |
| 84 | + else { | |
| 85 | + luaL_checktype(L, 1, LUA_TNUMBER); /* argument must be a number */ | |
| 86 | + lua_pushnil(L); /* number is not convertible to integer */ | |
| 87 | + } | |
| 83 | 88 | return 1; |
| 84 | 89 | } |
| 85 | 90 | |
| 86 | −static int math_floor (lua_State *L) { | |
| 87 | − lua_pushnumber(L, l_mathop(floor)(luaL_checknumber(L, 1))); | |
| 88 | − return 1; | |
| 89 | −} | |
| 90 | 91 | |
| 91 | −static int math_ifloor (lua_State *L) { | |
| 92 | +static int math_floor (lua_State *L) { | |
| 92 | 93 | int valid; |
| 93 | 94 | lua_Integer n = lua_tointegerx(L, 1, &valid); |
| 94 | 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 | + | |
| 103 | +static void pushnumint (lua_State *L, lua_Number d) { | |
| 104 | + lua_Integer n; | |
| 105 | + if (lua_numtointeger(d, &n)) /* fits in an integer? */ | |
| 106 | + lua_pushinteger(L, n); /* result is integer */ | |
| 107 | + else | |
| 108 | + lua_pushnumber(L, d); /* result is float */ | |
| 109 | +} | |
| 110 | + | |
| 111 | + | |
| 112 | +static int math_ceil (lua_State *L) { | |
| 113 | + if (lua_isinteger(L, 1)) | |
| 114 | + lua_settop(L, 1); /* integer is its own ceil */ | |
| 96 | 115 | else { |
| 97 | − luaL_checktype(L, 1, LUA_TNUMBER); /* error if not a number */ | |
| 98 | − lua_pushnil(L); /* number with invalid integer value */ | |
| 116 | + lua_Number d = l_mathop(ceil)(luaL_checknumber(L, 1)); | |
| 117 | + pushnumint(L, d); | |
| 99 | 118 | } |
| 100 | 119 | return 1; |
| 101 | 120 | } |
| 102 | 121 | |
| 122 | + | |
| 103 | 123 | static int math_fmod (lua_State *L) { |
| 104 | − lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1), | |
| 105 | − luaL_checknumber(L, 2))); | |
| 124 | + if (lua_isinteger(L, 1) && lua_isinteger(L, 2)) { | |
| 125 | + lua_Integer d = lua_tointeger(L, 2); | |
| 126 | + if ((lua_Unsigned)d + 1u <= 1u) { /* special cases: -1 or 0 */ | |
| 127 | + luaL_argcheck(L, d != 0, 2, "zero"); | |
| 128 | + lua_pushinteger(L, 0); /* avoid overflow with 0x80000... / -1 */ | |
| 129 | + } | |
| 130 | + else | |
| 131 | + lua_pushinteger(L, lua_tointeger(L, 1) % d); | |
| 132 | + } | |
| 133 | + else | |
| 134 | + lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1), | |
| 135 | + luaL_checknumber(L, 2))); | |
| 106 | 136 | return 1; |
| 107 | 137 | } |
| 108 | 138 | |
| 139 | + | |
| 109 | 140 | /* |
| 110 | 141 | ** next function does not use 'modf', avoiding problems with 'double*' |
| 111 | 142 | ** (which is not compatible with 'float*') when lua_Number is not |
| 112 | 143 | ** 'double'. |
| 113 | 144 | */ |
| 114 | 145 | static int math_modf (lua_State *L) { |
| 115 | − lua_Number n = luaL_checknumber(L, 1); | |
| 116 | − /* integer part (rounds toward zero) */ | |
| 117 | − lua_Number ip = (n < 0) ? -l_mathop(floor)(-n) : l_mathop(floor)(n); | |
| 118 | − lua_pushnumber(L, ip); | |
| 119 | − /* fractionary part (test handles inf/-inf) */ | |
| 120 | − lua_pushnumber(L, (n == ip) ? 0.0 : (n - ip)); | |
| 146 | + if (lua_isinteger(L ,1)) { | |
| 147 | + lua_settop(L, 1); /* number is its own integer part */ | |
| 148 | + lua_pushnumber(L, 0); /* no fractionary part */ | |
| 149 | + } | |
| 150 | + else { | |
| 151 | + lua_Number n = luaL_checknumber(L, 1); | |
| 152 | + /* integer part (rounds toward zero) */ | |
| 153 | + lua_Number ip = (n < 0) ? l_mathop(ceil)(n) : l_mathop(floor)(n); | |
| 154 | + pushnumint(L, ip); | |
| 155 | + /* fractionary part (test needed for inf/-inf) */ | |
| 156 | + lua_pushnumber(L, (n == ip) ? 0.0 : (n - ip)); | |
| 157 | + } | |
| 121 | 158 | return 2; |
| 122 | 159 | } |
| 123 | 160 | |
| @@ -127,13 +164,6 @@ static int math_sqrt (lua_State *L) { · 3 unmodified lines | ||
| 127 | 164 | return 1; |
| 128 | 165 | } |
| 129 | 166 | |
| 130 | −static int math_pow (lua_State *L) { | |
| 131 | − lua_Number x = luaL_checknumber(L, 1); | |
| 132 | − lua_Number y = luaL_checknumber(L, 2); | |
| 133 | − lua_pushnumber(L, l_mathop(pow)(x, y)); | |
| 134 | − return 1; | |
| 135 | −} | |
| 136 | − | |
| 137 | 167 | static int math_log (lua_State *L) { |
| 138 | 168 | lua_Number x = luaL_checknumber(L, 1); |
| 139 | 169 | lua_Number res; |
| @@ -141,84 +171,64 @@ static int math_log (lua_State *L) { · 1 unmodified line | ||
| 141 | 171 | res = l_mathop(log)(x); |
| 142 | 172 | else { |
| 143 | 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 | 175 | else res = l_mathop(log)(x)/l_mathop(log)(base); |
| 146 | 176 | } |
| 147 | 177 | lua_pushnumber(L, res); |
| 148 | 178 | return 1; |
| 149 | 179 | } |
| 150 | 180 | |
| 151 | −#if defined(LUA_COMPAT_LOG10) | |
| 152 | −static int math_log10 (lua_State *L) { | |
| 153 | − lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1))); | |
| 154 | − return 1; | |
| 155 | −} | |
| 156 | −#endif | |
| 157 | − | |
| 158 | 181 | static int math_exp (lua_State *L) { |
| 159 | 182 | lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1))); |
| 160 | 183 | return 1; |
| 161 | 184 | } |
| 162 | 185 | |
| 163 | 186 | static int math_deg (lua_State *L) { |
| 164 | − lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE); | |
| 187 | + lua_pushnumber(L, luaL_checknumber(L, 1) * (180.0 / PI)); | |
| 165 | 188 | return 1; |
| 166 | 189 | } |
| 167 | 190 | |
| 168 | 191 | static int math_rad (lua_State *L) { |
| 169 | − lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE); | |
| 192 | + lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / 180.0)); | |
| 170 | 193 | return 1; |
| 171 | 194 | } |
| 172 | 195 | |
| 173 | −static int math_frexp (lua_State *L) { | |
| 174 | − int e; | |
| 175 | − lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e)); | |
| 176 | − lua_pushinteger(L, e); | |
| 177 | − return 2; | |
| 178 | −} | |
| 179 | − | |
| 180 | −static int math_ldexp (lua_State *L) { | |
| 181 | − lua_Number x = luaL_checknumber(L, 1); | |
| 182 | − int ep = luaL_checkint(L, 2); | |
| 183 | − lua_pushnumber(L, l_mathop(ldexp)(x, ep)); | |
| 184 | − return 1; | |
| 185 | −} | |
| 186 | − | |
| 187 | − | |
| 188 | 196 | |
| 189 | 197 | static int math_min (lua_State *L) { |
| 190 | 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 | 200 | int i; |
| 193 | − for (i=2; i<=n; i++) { | |
| 194 | − lua_Number d = luaL_checknumber(L, i); | |
| 195 | − if (d < dmin) | |
| 196 | − dmin = d; | |
| 201 | + luaL_argcheck(L, n >= 1, 1, "value expected"); | |
| 202 | + for (i = 2; i <= n; i++) { | |
| 203 | + if (lua_compare(L, i, imin, LUA_OPLT)) | |
| 204 | + imin = i; | |
| 197 | 205 | } |
| 198 | − lua_pushnumber(L, dmin); | |
| 206 | + lua_pushvalue(L, imin); | |
| 199 | 207 | return 1; |
| 200 | 208 | } |
| 201 | 209 | |
| 202 | 210 | |
| 203 | 211 | static int math_max (lua_State *L) { |
| 204 | 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 | 214 | int i; |
| 207 | − for (i=2; i<=n; i++) { | |
| 208 | − lua_Number d = luaL_checknumber(L, i); | |
| 209 | − if (d > dmax) | |
| 210 | − dmax = d; | |
| 215 | + luaL_argcheck(L, n >= 1, 1, "value expected"); | |
| 216 | + for (i = 2; i <= n; i++) { | |
| 217 | + if (lua_compare(L, imax, i, LUA_OPLT)) | |
| 218 | + imax = i; | |
| 211 | 219 | } |
| 212 | − lua_pushnumber(L, dmax); | |
| 220 | + lua_pushvalue(L, imax); | |
| 213 | 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 | +*/ | |
| 217 | 229 | static int math_random (lua_State *L) { |
| 218 | − /* the `%' avoids the (rare) case of r==1, and is needed also because on | |
| 219 | − some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */ | |
| 220 | − lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX; | |
| 221 | 230 | lua_Integer low, up; |
| 231 | + double r = (double)l_rand() * (1.0 / ((double)RAND_MAX + 1.0)); | |
| 222 | 232 | switch (lua_gettop(L)) { /* check number of arguments */ |
| 223 | 233 | case 0: { /* no arguments */ |
| 224 | 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 | 247 | default: return luaL_error(L, "wrong number of arguments"); |
| 238 | 248 | } |
| 239 | 249 | /* random integer in the interval [low, up] */ |
| 240 | − up++; /* change interval to [low, up) */ | |
| 241 | − luaL_argcheck(L, up - low > 0, 1, "interval is empty"); | |
| 242 | − lua_pushinteger(L, (lua_Integer)(r * (lua_Number)(up - low)) + low); | |
| 250 | + luaL_argcheck(L, low <= up, 1, "interval is empty"); | |
| 251 | + luaL_argcheck(L, (lua_Unsigned)up - low <= (lua_Unsigned)LUA_MAXINTEGER, | |
| 252 | + 1, "interval too large"); | |
| 253 | + r *= (double)(up - low) + 1.0; | |
| 254 | + lua_pushinteger(L, (lua_Integer)r + low); | |
| 243 | 255 | return 1; |
| 244 | 256 | } |
| 245 | 257 | |
| 246 | 258 | |
| 247 | 259 | static int math_randomseed (lua_State *L) { |
| 248 | − srand(luaL_checkunsigned(L, 1)); | |
| 260 | + l_srand((unsigned int)luaL_checkunsigned(L, 1)); | |
| 249 | 261 | (void)rand(); /* discard first value to avoid undesirable correlations */ |
| 250 | 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 | + | |
| 287 | +static int math_cosh (lua_State *L) { | |
| 288 | + lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1))); | |
| 289 | + return 1; | |
| 290 | +} | |
| 291 | + | |
| 292 | +static int math_sinh (lua_State *L) { | |
| 293 | + lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1))); | |
| 294 | + return 1; | |
| 295 | +} | |
| 296 | + | |
| 297 | +static int math_tanh (lua_State *L) { | |
| 298 | + lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1))); | |
| 299 | + return 1; | |
| 300 | +} | |
| 301 | + | |
| 302 | +static int math_pow (lua_State *L) { | |
| 303 | + lua_Number x = luaL_checknumber(L, 1); | |
| 304 | + lua_Number y = luaL_checknumber(L, 2); | |
| 305 | + lua_pushnumber(L, l_mathop(pow)(x, y)); | |
| 306 | + return 1; | |
| 307 | +} | |
| 308 | + | |
| 309 | +static int math_frexp (lua_State *L) { | |
| 310 | + int e; | |
| 311 | + lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e)); | |
| 312 | + lua_pushinteger(L, e); | |
| 313 | + return 2; | |
| 314 | +} | |
| 315 | + | |
| 316 | +static int math_ldexp (lua_State *L) { | |
| 317 | + lua_Number x = luaL_checknumber(L, 1); | |
| 318 | + int ep = luaL_checkint(L, 2); | |
| 319 | + lua_pushnumber(L, l_mathop(ldexp)(x, ep)); | |
| 320 | + return 1; | |
| 321 | +} | |
| 322 | + | |
| 323 | +static int math_log10 (lua_State *L) { | |
| 324 | + lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1))); | |
| 325 | + return 1; | |
| 326 | +} | |
| 327 | + | |
| 328 | +#endif | |
| 329 | +/* }================================================================== */ | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 268 | 333 | static const luaL_Reg mathlib[] = { |
| 269 | 334 | {"abs", math_abs}, |
| 270 | 335 | {"acos", math_acos}, |
| 271 | 336 | {"asin", math_asin}, |
| 272 | − {"atan2", math_atan2}, | |
| 273 | 337 | {"atan", math_atan}, |
| 274 | 338 | {"ceil", math_ceil}, |
| 275 | − {"cosh", math_cosh}, | |
| 276 | 339 | {"cos", math_cos}, |
| 277 | 340 | {"deg", math_deg}, |
| 278 | 341 | {"exp", math_exp}, |
| 279 | − {"floor", math_floor}, | |
| 280 | 342 | {"ifloor", math_ifloor}, |
| 343 | + {"floor", math_floor}, | |
| 281 | 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 | 345 | {"log", math_log}, |
| 288 | 346 | {"max", math_max}, |
| 289 | 347 | {"min", math_min}, |
| 290 | 348 | {"modf", math_modf}, |
| 291 | − {"pow", math_pow}, | |
| 292 | 349 | {"rad", math_rad}, |
| 293 | 350 | {"random", math_random}, |
| 294 | 351 | {"randomseed", math_randomseed}, |
| 295 | − {"sinh", math_sinh}, | |
| 296 | 352 | {"sin", math_sin}, |
| 297 | 353 | {"sqrt", math_sqrt}, |
| 298 | − {"tanh", math_tanh}, | |
| 299 | 354 | {"tan", math_tan}, |
| 300 | 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 | 366 | {NULL, NULL} |
| 302 | 367 | }; |
| 303 | 368 | |
| @@ -311,6 +376,10 @@ LUAMOD_API int luaopen_math (lua_State *L) { · 7 unmodified lines | ||
| 311 | 376 | lua_setfield(L, -2, "pi"); |
| 312 | 377 | lua_pushnumber(L, HUGE_VAL); |
| 313 | 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 | 383 | return 1; |
| 315 | 384 | } |
| 316 | 385 | |
| @@ -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 | 3 | ** Some generic functions over Lua objects |
| 4 | 4 | ** 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 | 77 | case LUA_OPSUB:return intop(-, v1, v2); |
| 78 | 78 | case LUA_OPMUL:return intop(*, v1, v2); |
| 79 | 79 | case LUA_OPMOD: return luaV_mod(L, v1, v2); |
| 80 | − case LUA_OPPOW: return luaV_pow(L, v1, v2); | |
| 81 | 80 | case LUA_OPIDIV: return luaV_div(L, v1, v2); |
| 82 | 81 | case LUA_OPBAND: return intop(&, v1, v2); |
| 83 | 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 | 84 | case LUA_OPSHL: return luaV_shiftl(v1, v2); |
| 86 | 85 | case LUA_OPSHR: return luaV_shiftl(v1, -v2); |
| 87 | 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 | 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 | 97 | case LUA_OPSUB: return luai_numsub(L, v1, v2); |
| 99 | 98 | case LUA_OPMUL: return luai_nummul(L, v1, v2); |
| 100 | 99 | case LUA_OPDIV: return luai_numdiv(L, v1, v2); |
| 101 | − case LUA_OPMOD: return luai_nummod(L, v1, v2); | |
| 102 | 100 | case LUA_OPPOW: return luai_numpow(L, v1, v2); |
| 103 | 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 | 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 | 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 | 126 | lua_Number n1; lua_Number n2; |
| 124 | 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 | 129 | return; |
| 127 | 130 | } |
| 128 | 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 | 137 | return; |
| 135 | 138 | } |
| 136 | 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 | 141 | return; |
| 139 | 142 | } |
| 140 | 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 | |
| 252 | −int luaO_str2d (const char *s, size_t len, lua_Number *result) { | |
| 255 | +static const char *l_str2d (const char *s, lua_Number *result) { | |
| 253 | 256 | char *endptr; |
| 254 | 257 | if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */ |
| 255 | − return 0; | |
| 258 | + return NULL; | |
| 256 | 259 | else if (strpbrk(s, "xX")) /* hexa? */ |
| 257 | 260 | *result = lua_strx2number(s, &endptr); |
| 258 | 261 | else |
| 259 | 262 | *result = lua_str2number(s, &endptr); |
| 260 | 263 | if (endptr == s) return 0; /* nothing recognized */ |
| 261 | 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 | |
| 266 | −int luaO_str2int (const char *s, size_t len, lua_Integer *result) { | |
| 267 | − const char *ends = s + len; | |
| 269 | +static const char *l_str2int (const char *s, lua_Integer *result) { | |
| 268 | 270 | lua_Unsigned a = 0; |
| 269 | 271 | int empty = 1; |
| 270 | 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 | 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 | 291 | else { |
| 290 | − *result = cast_integer((neg) ? 0u - a : a); | |
| 291 | − return 1; | |
| 292 | + *result = l_castU2S((neg) ? 0u - a : a); | |
| 293 | + return s; | |
| 292 | 294 | } |
| 293 | 295 | } |
| 294 | 296 | |
| 295 | 297 | |
| 298 | +size_t luaO_str2num (const char *s, TValue *o) { | |
| 299 | + lua_Integer i; lua_Number n; | |
| 300 | + const char *e; | |
| 301 | + if ((e = l_str2int(s, &i)) != NULL) { /* try as an integer */ | |
| 302 | + setivalue(o, i); | |
| 303 | + } | |
| 304 | + else if ((e = l_str2d(s, &n)) != NULL) { /* else try as a float */ | |
| 305 | + setfltvalue(o, n); | |
| 306 | + } | |
| 307 | + else | |
| 308 | + return 0; /* conversion failed */ | |
| 309 | + return (e - s + 1); /* success; return string size */ | |
| 310 | +} | |
| 311 | + | |
| 312 | + | |
| 296 | 313 | int luaO_utf8esc (char *buff, unsigned int x) { |
| 297 | 314 | int n = 1; /* number of bytes put in buffer (backwards) */ |
| 298 | 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 | 357 | case 'd': { |
| 341 | 358 | setivalue(L->top++, cast_int(va_arg(argp, int))); |
| 359 | + luaV_tostring(L, L->top - 1); | |
| 342 | 360 | break; |
| 343 | 361 | } |
| 344 | 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 | 365 | break; |
| 347 | 366 | } |
| 348 | 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 | 370 | break; |
| 351 | 371 | } |
| 352 | 372 | case 'p': { |
| @@ -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 | 3 | ** Type definitions for Lua objects |
| 4 | 4 | ** See Copyright Notice in lua.h |
| 5 | 5 | */ |
| @@ -156,17 +156,17 @@ typedef struct lua_TValue TValue; · 150 unmodified lines | ||
| 156 | 156 | #define fltvalue(o) check_exp(ttisfloat(o), val_(o).n) |
| 157 | 157 | #define gcvalue(o) check_exp(iscollectable(o), val_(o).gc) |
| 158 | 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 | 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 | 162 | #define uvalue(o) (&rawuvalue(o)->uv) |
| 163 | −#define clvalue(o) check_exp(ttisclosure(o), &val_(o).gc->cl) | |
| 164 | −#define clLvalue(o) check_exp(ttisLclosure(o), &val_(o).gc->cl.l) | |
| 165 | −#define clCvalue(o) check_exp(ttisCclosure(o), &val_(o).gc->cl.c) | |
| 163 | +#define clvalue(o) check_exp(ttisclosure(o), gco2cl(val_(o).gc)) | |
| 164 | +#define clLvalue(o) check_exp(ttisLclosure(o), gco2lcl(val_(o).gc)) | |
| 165 | +#define clCvalue(o) check_exp(ttisCclosure(o), gco2ccl(val_(o).gc)) | |
| 166 | 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 | 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 | 170 | /* a dead value may get the 'gc' field, but cannot access its contents */ |
| 171 | 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 | 187 | /* Macros to set values */ |
| 188 | 188 | #define settt_(o,t) ((o)->tt_=(t)) |
| 189 | 189 | |
| 190 | −#define setnvalue(obj,x) \ | |
| 190 | +#define setfltvalue(obj,x) \ | |
| 191 | 191 | { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_TNUMFLT); } |
| 192 | 192 | |
| 193 | 193 | #define setivalue(obj,x) \ |
| @@ -205,38 +205,37 @@ typedef struct lua_TValue TValue; · 11 unmodified lines | ||
| 205 | 205 | { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); } |
| 206 | 206 | |
| 207 | 207 | #define setgcovalue(L,obj,x) \ |
| 208 | − { TValue *io=(obj); GCObject *i_g=(x); \ | |
| 209 | − val_(io).gc=i_g; settt_(io, ctb(gch(i_g)->tt)); } | |
| 208 | + { TValue *io = (obj); GCObject *i_g=(x); \ | |
| 209 | + val_(io).gc = i_g; settt_(io, ctb(gch(i_g)->tt)); } | |
| 210 | 210 | |
| 211 | 211 | #define setsvalue(L,obj,x) \ |
| 212 | − { TValue *io=(obj); \ | |
| 213 | − TString *x_ = (x); \ | |
| 214 | − val_(io).gc=cast(GCObject *, x_); settt_(io, ctb(x_->tsv.tt)); \ | |
| 212 | + { TValue *io = (obj); TString *x_ = (x); \ | |
| 213 | + val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tsv.tt)); \ | |
| 215 | 214 | checkliveness(G(L),io); } |
| 216 | 215 | |
| 217 | 216 | #define setuvalue(L,obj,x) \ |
| 218 | − { TValue *io=(obj); \ | |
| 219 | − val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TUSERDATA)); \ | |
| 217 | + { TValue *io = (obj); Udata *x_ = (x); \ | |
| 218 | + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TUSERDATA)); \ | |
| 220 | 219 | checkliveness(G(L),io); } |
| 221 | 220 | |
| 222 | 221 | #define setthvalue(L,obj,x) \ |
| 223 | − { TValue *io=(obj); \ | |
| 224 | − val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTHREAD)); \ | |
| 222 | + { TValue *io = (obj); lua_State *x_ = (x); \ | |
| 223 | + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTHREAD)); \ | |
| 225 | 224 | checkliveness(G(L),io); } |
| 226 | 225 | |
| 227 | 226 | #define setclLvalue(L,obj,x) \ |
| 228 | − { TValue *io=(obj); \ | |
| 229 | − val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TLCL)); \ | |
| 227 | + { TValue *io = (obj); LClosure *x_ = (x); \ | |
| 228 | + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TLCL)); \ | |
| 230 | 229 | checkliveness(G(L),io); } |
| 231 | 230 | |
| 232 | 231 | #define setclCvalue(L,obj,x) \ |
| 233 | − { TValue *io=(obj); \ | |
| 234 | − val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TCCL)); \ | |
| 232 | + { TValue *io = (obj); CClosure *x_ = (x); \ | |
| 233 | + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TCCL)); \ | |
| 235 | 234 | checkliveness(G(L),io); } |
| 236 | 235 | |
| 237 | 236 | #define sethvalue(L,obj,x) \ |
| 238 | − { TValue *io=(obj); \ | |
| 239 | − val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTABLE)); \ | |
| 237 | + { TValue *io = (obj); Table *x_ = (x); \ | |
| 238 | + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTABLE)); \ | |
| 240 | 239 | checkliveness(G(L),io); } |
| 241 | 240 | |
| 242 | 241 | #define setdeadvalue(obj) settt_(obj, LUA_TDEADKEY) |
| @@ -246,7 +245,7 @@ typedef struct lua_TValue TValue; · 3 unmodified lines | ||
| 246 | 245 | #define setobj(L,obj1,obj2) \ |
| 247 | 246 | { const TValue *io2=(obj2); TValue *io1=(obj1); \ |
| 248 | 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 | 268 | #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 | 305 | struct { |
| 311 | 306 | CommonHeader; |
| 312 | 307 | lu_byte extra; /* reserved words for short strings; "has hash" for longs */ |
| 308 | + unsigned int hash; | |
| 313 | 309 | size_t len; /* number of characters in string */ |
| 314 | 310 | union TString *hnext; /* linked list for hash table */ |
| 315 | − unsigned int hash; | |
| 316 | 311 | } tsv; |
| 317 | 312 | } TString; |
| 318 | 313 | |
| @@ -377,14 +372,9 @@ typedef struct LocVar { · 58 unmodified lines | ||
| 377 | 372 | */ |
| 378 | 373 | typedef struct Proto { |
| 379 | 374 | CommonHeader; |
| 380 | − TValue *k; /* constants used by the function */ | |
| 381 | − Instruction *code; | |
| 382 | − struct Proto **p; /* functions defined inside the function */ | |
| 383 | − int *lineinfo; /* map from opcodes to source lines (debug information) */ | |
| 384 | − LocVar *locvars; /* information about local variables (debug information) */ | |
| 385 | − Upvaldesc *upvalues; /* upvalue information */ | |
| 386 | − union Closure *cache; /* last created closure with this prototype */ | |
| 387 | − TString *source; /* used for debug information */ | |
| 375 | + lu_byte numparams; /* number of fixed parameters */ | |
| 376 | + lu_byte is_vararg; | |
| 377 | + lu_byte maxstacksize; /* maximum stack used by this function */ | |
| 388 | 378 | int sizeupvalues; /* size of 'upvalues' */ |
| 389 | 379 | int sizek; /* size of `k' */ |
| 390 | 380 | int sizecode; |
| @@ -393,10 +383,15 @@ typedef struct Proto { · 2 unmodified lines | ||
| 393 | 383 | int sizelocvars; |
| 394 | 384 | int linedefined; |
| 395 | 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 | 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 | 395 | } Proto; |
| 401 | 396 | |
| 402 | 397 | |
| @@ -462,12 +457,12 @@ typedef struct Table { · 59 unmodified lines | ||
| 462 | 457 | CommonHeader; |
| 463 | 458 | lu_byte flags; /* 1<<p means tagmethod(p) is not present */ |
| 464 | 459 | lu_byte lsizenode; /* log2 of size of `node' array */ |
| 465 | − struct Table *metatable; | |
| 460 | + int sizearray; /* size of `array' array */ | |
| 466 | 461 | TValue *array; /* array part */ |
| 467 | 462 | Node *node; |
| 468 | 463 | Node *lastfree; /* any free position is before this position */ |
| 464 | + struct Table *metatable; | |
| 469 | 465 | GCObject *gclist; |
| 470 | − int sizearray; /* size of `array' array */ | |
| 471 | 466 | } Table; |
| 472 | 467 | |
| 473 | 468 | |
| @@ -500,8 +495,7 @@ LUAI_FUNC int luaO_utf8esc (char *buff, unsigned int x); · 26 unmodified lines | ||
| 500 | 495 | LUAI_FUNC int luaO_ceillog2 (unsigned int x); |
| 501 | 496 | LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1, |
| 502 | 497 | const TValue *p2, TValue *res); |
| 503 | −LUAI_FUNC int luaO_str2d (const char *s, size_t len, lua_Number *result); | |
| 504 | −LUAI_FUNC int luaO_str2int (const char *s, size_t len, lua_Integer *result); | |
| 498 | +LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o); | |
| 505 | 499 | LUAI_FUNC int luaO_hexavalue (int c); |
| 506 | 500 | LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, |
| 507 | 501 | va_list argp); |
| @@ -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 | 3 | ** Standard Operating System library |
| 4 | 4 | ** See Copyright Notice in lua.h |
| 5 | 5 | */ |
| @@ -59,9 +59,15 @@ · 53 unmodified lines | ||
| 59 | 59 | #if defined(LUA_USE_POSIX) /* { */ |
| 60 | 60 | |
| 61 | 61 | #include <unistd.h> |
| 62 | + | |
| 62 | 63 | #define LUA_TMPNAMBUFSIZE 32 |
| 64 | + | |
| 65 | +#if !defined(LUA_TMPNAMTEMPLATE) | |
| 66 | +#define LUA_TMPNAMTEMPLATE "/tmp/lua_XXXXXX" | |
| 67 | +#endif | |
| 68 | + | |
| 63 | 69 | #define lua_tmpnam(b,e) { \ |
| 64 | − strcpy(b, "/tmp/lua_XXXXXX"); \ | |
| 70 | + strcpy(b, LUA_TMPNAMTEMPLATE); \ | |
| 65 | 71 | e = mkstemp(b); \ |
| 66 | 72 | if (e != -1) close(e); \ |
| 67 | 73 | e = (e == -1); } |
| @@ -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 | 3 | ** Lua Parser |
| 4 | 4 | ** 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 | |
| 1621 | −Closure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, | |
| 1622 | − Dyndata *dyd, const char *name, int firstchar) { | |
| 1621 | +LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, | |
| 1622 | + Dyndata *dyd, const char *name, int firstchar) { | |
| 1623 | 1623 | LexState lexstate; |
| 1624 | 1624 | FuncState funcstate; |
| 1625 | − Closure *cl = luaF_newLclosure(L, 1); /* create main closure */ | |
| 1625 | + LClosure *cl = luaF_newLclosure(L, 1); /* create main closure */ | |
| 1626 | 1626 | setclLvalue(L, L->top, cl); /* anchor it (to avoid being collected) */ |
| 1627 | 1627 | incr_top(L); |
| 1628 | 1628 | lexstate.h = luaH_new(L); /* create table for scanner */ |
| 1629 | 1629 | sethvalue(L, L->top, lexstate.h); /* anchor it */ |
| 1630 | 1630 | incr_top(L); |
| 1631 | − funcstate.f = cl->l.p = luaF_newproto(L); | |
| 1631 | + funcstate.f = cl->p = luaF_newproto(L); | |
| 1632 | 1632 | funcstate.f->source = luaS_new(L, name); /* create and anchor TString */ |
| 1633 | 1633 | luaC_objbarrier(L, funcstate.f, funcstate.f->source); |
| 1634 | 1634 | lexstate.buff = buff; |
| @@ -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 | 3 | ** Lua Parser |
| 4 | 4 | ** See Copyright Notice in lua.h |
| 5 | 5 | */ |
| @@ -113,8 +113,8 @@ typedef struct FuncState { · 107 unmodified lines | ||
| 113 | 113 | } FuncState; |
| 114 | 114 | |
| 115 | 115 | |
| 116 | −LUAI_FUNC Closure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, | |
| 117 | − Dyndata *dyd, const char *name, int firstchar); | |
| 116 | +LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, | |
| 117 | + Dyndata *dyd, const char *name, int firstchar); | |
| 118 | 118 | |
| 119 | 119 | |
| 120 | 120 | #endif |
| @@ -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 | 3 | ** Global State |
| 4 | 4 | ** See Copyright Notice in lua.h |
| 5 | 5 | */ |
| @@ -60,20 +60,18 @@ typedef struct CallInfo { · 54 unmodified lines | ||
| 60 | 60 | StkId func; /* function index in the stack */ |
| 61 | 61 | StkId top; /* top for this function */ |
| 62 | 62 | struct CallInfo *previous, *next; /* dynamic call link */ |
| 63 | + ptrdiff_t extra; | |
| 63 | 64 | short nresults; /* expected number of results from this function */ |
| 64 | 65 | lu_byte callstatus; |
| 65 | − ptrdiff_t extra; | |
| 66 | 66 | union { |
| 67 | 67 | struct { /* only for Lua functions */ |
| 68 | 68 | StkId base; /* base for this function */ |
| 69 | 69 | const Instruction *savedpc; |
| 70 | 70 | } l; |
| 71 | 71 | struct { /* only for C functions */ |
| 72 | − int ctx; /* context info. in case of yields */ | |
| 73 | − lua_CFunction k; /* continuation in case of yields */ | |
| 72 | + lua_KFunction k; /* continuation in case of yields */ | |
| 74 | 73 | ptrdiff_t old_errfunc; |
| 75 | − lu_byte old_allowhook; | |
| 76 | − lu_byte status; | |
| 74 | + int ctx; /* context info. in case of yields */ | |
| 77 | 75 | } c; |
| 78 | 76 | } u; |
| 79 | 77 | } CallInfo; |
| @@ -82,19 +80,21 @@ typedef struct CallInfo { · 2 unmodified lines | ||
| 82 | 80 | /* |
| 83 | 81 | ** Bits in CallInfo status |
| 84 | 82 | */ |
| 85 | −#define CIST_LUA (1<<0) /* call is running a Lua function */ | |
| 86 | −#define CIST_HOOKED (1<<1) /* call is running a debug hook */ | |
| 87 | −#define CIST_REENTRY (1<<2) /* call is running on same invocation of | |
| 83 | +#define CIST_OAH (1<<0) /* original value of 'allowhook' */ | |
| 84 | +#define CIST_LUA (1<<1) /* call is running a Lua function */ | |
| 85 | +#define CIST_HOOKED (1<<2) /* call is running a debug hook */ | |
| 86 | +#define CIST_REENTRY (1<<3) /* call is running on same invocation of | |
| 88 | 87 | luaV_execute of previous call */ |
| 89 | −#define CIST_YIELDED (1<<3) /* call reentered after suspension */ | |
| 90 | 88 | #define CIST_YPCALL (1<<4) /* call is a yieldable protected call */ |
| 91 | −#define CIST_STAT (1<<5) /* call has an error status (pcall) */ | |
| 92 | −#define CIST_TAIL (1<<6) /* call was tail called */ | |
| 93 | −#define CIST_HOOKYIELD (1<<7) /* last hook called yielded */ | |
| 94 | − | |
| 89 | +#define CIST_TAIL (1<<5) /* call was tail called */ | |
| 90 | +#define CIST_HOOKYIELD (1<<6) /* last hook called yielded */ | |
| 95 | 91 | |
| 96 | 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 | 100 | ** `global state', shared by all threads of this state |
| @@ -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 | 3 | ** String table (keeps all strings handled by Lua) |
| 4 | 4 | ** See Copyright Notice in lua.h |
| 5 | 5 | */ |
| @@ -90,9 +90,11 @@ void luaS_resize (lua_State *L, int newsize) { · 84 unmodified lines | ||
| 90 | 90 | static TString *createstrobj (lua_State *L, const char *str, size_t l, |
| 91 | 91 | int tag, unsigned int h) { |
| 92 | 92 | TString *ts; |
| 93 | + GCObject *o; | |
| 93 | 94 | size_t totalsize; /* total size of TString object */ |
| 94 | 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 | 98 | ts->tsv.len = l; |
| 97 | 99 | ts->tsv.hash = h; |
| 98 | 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 | |
| 105 | −LUAI_FUNC void luaS_remove (lua_State *L, TString *ts) { | |
| 107 | +void luaS_remove (lua_State *L, TString *ts) { | |
| 106 | 108 | stringtable *tb = &G(L)->strt; |
| 107 | 109 | TString **p = &tb->hash[lmod(ts->tsv.hash, tb->size)]; |
| 108 | 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 | |
| 166 | 168 | Udata *luaS_newudata (lua_State *L, size_t s) { |
| 167 | 169 | Udata *u; |
| 170 | + GCObject *o; | |
| 168 | 171 | if (s > MAX_SIZE - sizeof(Udata)) |
| 169 | 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 | 175 | u->uv.len = s; |
| 172 | 176 | u->uv.metatable = NULL; |
| 173 | 177 | setuservalue(L, u, luaO_nilobject); |
| @@ -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 | 3 | ** Standard library for string operations and pattern-matching |
| 4 | 4 | ** 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 | 106 | /* reasonable limit to avoid arithmetic overflow and strings too big */ |
| 107 | −#if INT_MAX / 2 <= 0x10000000 | |
| 108 | −#define MAXSIZE ((size_t)(INT_MAX / 2)) | |
| 107 | +#if LUA_MAXINTEGER / 2 <= 0x10000000 | |
| 108 | +#define MAXSIZE ((size_t)(LUA_MAXINTEGER / 2)) | |
| 109 | 109 | #else |
| 110 | 110 | #define MAXSIZE ((size_t)0x10000000) |
| 111 | 111 | #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 | 950 | /* number of bits in a character */ |
| 954 | 951 | #define NB CHAR_BIT |
| 955 | 952 | |
| 956 | −/* mask for one character (NB ones) */ | |
| 957 | −#define MC (((lua_Integer)1 << NB) - 1) | |
| 958 | − | |
| 959 | −/* mask for one character without sign ((NB - 1) ones) */ | |
| 960 | −#define SM (((lua_Integer)1 << (NB - 1)) - 1) | |
| 953 | +/* mask for one character (NB 1's) */ | |
| 954 | +#define MC ((1 << NB) - 1) | |
| 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 | 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 | |
| 966 | 966 | static union { |
| 967 | 967 | int dummy; |
| @@ -975,7 +975,7 @@ static int getendian (lua_State *L, int arg) { · 7 unmodified lines | ||
| 975 | 975 | if (*endian == 'n') /* native? */ |
| 976 | 976 | return nativeendian.little; |
| 977 | 977 | luaL_argcheck(L, *endian == 'l' || *endian == 'b', arg, |
| 978 | − "endianess must be 'l'/'b'/'n'"); | |
| 978 | + "endianness must be 'l'/'b'/'n'"); | |
| 979 | 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 | |
| 992 | −static int packint (char *buff, lua_Integer n, int littleendian, int size) { | |
| 992 | +/* mask for all ones in last byte in a lua Integer */ | |
| 993 | +#define HIGHERBYTE ((lua_Unsigned)MC << (NB * (SZINT - 1))) | |
| 994 | + | |
| 995 | + | |
| 996 | +static int dumpint (char *buff, lua_Integer m, int littleendian, int size) { | |
| 993 | 997 | int i; |
| 998 | + lua_Unsigned n = (lua_Unsigned)m; | |
| 999 | + lua_Unsigned mask = (m >= 0) ? 0 : HIGHERBYTE; /* sign extension */ | |
| 994 | 1000 | if (littleendian) { |
| 995 | 1001 | for (i = 0; i < size - 1; i++) { |
| 996 | 1002 | buff[i] = (n & MC); |
| 997 | − n >>= NB; | |
| 1003 | + n = (n >> NB) | mask; | |
| 998 | 1004 | } |
| 999 | 1005 | } |
| 1000 | 1006 | else { |
| 1001 | 1007 | for (i = size - 1; i > 0; i--) { |
| 1002 | 1008 | buff[i] = (n & MC); |
| 1003 | − n >>= NB; | |
| 1009 | + n = (n >> NB) | mask; | |
| 1004 | 1010 | } |
| 1005 | 1011 | } |
| 1006 | 1012 | buff[i] = (n & MC); /* last byte */ |
| 1007 | − /* test for overflow: OK if there are only zeros left in higher bytes, | |
| 1008 | − or if there are only ones left and packed number is negative (signal | |
| 1009 | − bit, the higher bit in last byte, is one) */ | |
| 1010 | − return ((n & ~MC) == 0 || (n | SM) == ~(lua_Integer)0); | |
| 1013 | + if (size < SZINT) { /* need test for overflow? */ | |
| 1014 | + /* OK if there are only zeros left in higher bytes, | |
| 1015 | + or only ones left (excluding non-signal bits in last byte) */ | |
| 1016 | + return ((n & ~(lua_Unsigned)MC) == 0 || | |
| 1017 | + (n | SM) == ~(lua_Unsigned)0); | |
| 1018 | + } | |
| 1019 | + else return 1; /* no overflow can occur with full size */ | |
| 1011 | 1020 | } |
| 1012 | 1021 | |
| 1013 | 1022 | |
| 1014 | −static int packint_l (lua_State *L) { | |
| 1023 | +static int dumpint_l (lua_State *L) { | |
| 1015 | 1024 | char buff[MAXINTSIZE]; |
| 1016 | 1025 | lua_Integer n = luaL_checkinteger(L, 1); |
| 1017 | 1026 | int size = getintsize(L, 2); |
| 1018 | 1027 | int endian = getendian(L, 3); |
| 1019 | − if (packint(buff, n, endian, size)) | |
| 1028 | + if (dumpint(buff, n, endian, size)) | |
| 1020 | 1029 | lua_pushlstring(L, buff, size); |
| 1021 | 1030 | else |
| 1022 | 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 | 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 | |
| 1033 | −static int unpackint (const char *buff, lua_Integer *res, | |
| 1039 | + | |
| 1040 | +static int undumpint (const char *buff, lua_Integer *res, | |
| 1034 | 1041 | int littleendian, int size) { |
| 1035 | − lua_Integer n = 0; | |
| 1042 | + lua_Unsigned n = 0; | |
| 1036 | 1043 | int i; |
| 1037 | 1044 | for (i = 0; i < size; i++) { |
| 1038 | 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 | 1049 | its "signal bit" */ |
| 1043 | 1050 | if (!((n & HIGHERBYTE1) == 0 || /* zeros for positive number */ |
| 1044 | 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 | 1053 | return 0; /* overflow */ |
| 1047 | 1054 | } |
| 1048 | 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 | 1058 | if (size < SZINT) { /* need sign extension? */ |
| 1052 | − lua_Integer mask = (~(lua_Integer)0) << (size*NB - 1); | |
| 1053 | − if (n & mask) /* negative value? */ | |
| 1054 | − n |= mask; /* signal extension */ | |
| 1059 | + lua_Unsigned mask = (lua_Unsigned)1 << (size*NB - 1); | |
| 1060 | + *res = (lua_Integer)((n ^ mask) - mask); /* do sign extension */ | |
| 1055 | 1061 | } |
| 1056 | − *res = n; | |
| 1062 | + else | |
| 1063 | + *res = (lua_Integer)n; | |
| 1057 | 1064 | return 1; |
| 1058 | 1065 | } |
| 1059 | 1066 | |
| 1060 | 1067 | |
| 1061 | −static int unpackint_l (lua_State *L) { | |
| 1068 | +static int undumpint_l (lua_State *L) { | |
| 1062 | 1069 | lua_Integer res; |
| 1063 | 1070 | size_t len; |
| 1064 | 1071 | const char *s = luaL_checklstring(L, 1, &len); |
| @@ -1067,7 +1074,7 @@ static int unpackint_l (lua_State *L) { · 2 unmodified lines | ||
| 1067 | 1074 | int endian = getendian(L, 4); |
| 1068 | 1075 | luaL_argcheck(L, 1 <= pos && (size_t)pos + size - 1 <= len, 1, |
| 1069 | 1076 | "string too short"); |
| 1070 | − if(unpackint(s + pos - 1, &res, endian, size)) | |
| 1077 | + if(undumpint(s + pos - 1, &res, endian, size)) | |
| 1071 | 1078 | lua_pushinteger(L, res); |
| 1072 | 1079 | else |
| 1073 | 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 | |
| 1078 | −static void correctendianess (lua_State *L, char *b, int size, int endianarg) { | |
| 1085 | +static void correctendianness (lua_State *L, char *b, int size, int endianarg) { | |
| 1079 | 1086 | int endian = getendian(L, endianarg); |
| 1080 | − if (endian != nativeendian.little) { /* not native endianess? */ | |
| 1087 | + if (endian != nativeendian.little) { /* not native endianness? */ | |
| 1081 | 1088 | int i = 0; |
| 1082 | 1089 | while (i < --size) { |
| 1083 | 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 | |
| 1100 | −static int packfloat_l (lua_State *L) { | |
| 1107 | +static int dumpfloat_l (lua_State *L) { | |
| 1101 | 1108 | float f; double d; |
| 1102 | 1109 | char *pn; /* pointer to number */ |
| 1103 | 1110 | lua_Number n = luaL_checknumber(L, 1); |
| @@ -1113,13 +1120,13 @@ static int packfloat_l (lua_State *L) { · 9 unmodified lines | ||
| 1113 | 1120 | d = (double)n; |
| 1114 | 1121 | pn = (char*)&d; |
| 1115 | 1122 | } |
| 1116 | − correctendianess(L, pn, size, 3); | |
| 1123 | + correctendianness(L, pn, size, 3); | |
| 1117 | 1124 | lua_pushlstring(L, pn, size); |
| 1118 | 1125 | return 1; |
| 1119 | 1126 | } |
| 1120 | 1127 | |
| 1121 | 1128 | |
| 1122 | −static int unpackfloat_l (lua_State *L) { | |
| 1129 | +static int undumpfloat_l (lua_State *L) { | |
| 1123 | 1130 | lua_Number res; |
| 1124 | 1131 | size_t len; |
| 1125 | 1132 | const char *s = luaL_checklstring(L, 1, &len); |
| @@ -1129,19 +1136,19 @@ static int unpackfloat_l (lua_State *L) { · 3 unmodified lines | ||
| 1129 | 1136 | "string too short"); |
| 1130 | 1137 | if (size == sizeof(lua_Number)) { |
| 1131 | 1138 | memcpy(&res, s + pos - 1, size); |
| 1132 | − correctendianess(L, (char*)&res, size, 4); | |
| 1139 | + correctendianness(L, (char*)&res, size, 4); | |
| 1133 | 1140 | } |
| 1134 | 1141 | else if (size == sizeof(float)) { |
| 1135 | 1142 | float f; |
| 1136 | 1143 | memcpy(&f, s + pos - 1, size); |
| 1137 | − correctendianess(L, (char*)&f, size, 4); | |
| 1144 | + correctendianness(L, (char*)&f, size, 4); | |
| 1138 | 1145 | res = (lua_Number)f; |
| 1139 | 1146 | } |
| 1140 | 1147 | else { /* native lua_Number may be neither float nor double */ |
| 1141 | 1148 | double d; |
| 1142 | 1149 | lua_assert(size == sizeof(double)); |
| 1143 | 1150 | memcpy(&d, s + pos - 1, size); |
| 1144 | − correctendianess(L, (char*)&d, size, 4); | |
| 1151 | + correctendianness(L, (char*)&d, size, 4); | |
| 1145 | 1152 | res = (lua_Number)d; |
| 1146 | 1153 | } |
| 1147 | 1154 | lua_pushnumber(L, res); |
| @@ -1166,10 +1173,10 @@ static const luaL_Reg strlib[] = { · 18 unmodified lines | ||
| 1166 | 1173 | {"reverse", str_reverse}, |
| 1167 | 1174 | {"sub", str_sub}, |
| 1168 | 1175 | {"upper", str_upper}, |
| 1169 | − {"packfloat", packfloat_l}, | |
| 1170 | − {"packint", packint_l}, | |
| 1171 | − {"unpackfloat", unpackfloat_l}, | |
| 1172 | − {"unpackint", unpackint_l}, | |
| 1176 | + {"dumpfloat", dumpfloat_l}, | |
| 1177 | + {"dumpint", dumpint_l}, | |
| 1178 | + {"undumpfloat", undumpfloat_l}, | |
| 1179 | + {"undumpint", undumpint_l}, | |
| 1173 | 1180 | {NULL, NULL} |
| 1174 | 1181 | }; |
| 1175 | 1182 | |
| @@ -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 | 3 | ** Lua tables (hash) |
| 4 | 4 | ** See Copyright Notice in lua.h |
| 5 | 5 | */ |
| @@ -21,6 +21,7 @@ · 15 unmodified lines | ||
| 21 | 21 | #include <float.h> |
| 22 | 22 | #include <math.h> |
| 23 | 23 | #include <string.h> |
| 24 | +#include <limits.h> | |
| 24 | 25 | |
| 25 | 26 | #define ltable_c |
| 26 | 27 | #define LUA_CORE |
| @@ -39,14 +40,13 @@ · 12 unmodified lines | ||
| 39 | 40 | |
| 40 | 41 | |
| 41 | 42 | /* |
| 42 | −** max size of array part is 2^MAXBITS | |
| 43 | +** Maximum size of array part (MAXASIZE) is 2^MAXBITS. (SIZEINT is the | |
| 44 | +** minimum between size of int and size of LUA_INTEGER; array indices | |
| 45 | +** are limited by both types.) | |
| 43 | 46 | */ |
| 44 | −#if LUAI_BITSINT >= 32 | |
| 45 | −#define MAXBITS 30 | |
| 46 | −#else | |
| 47 | −#define MAXBITS (LUAI_BITSINT-2) | |
| 48 | −#endif | |
| 49 | − | |
| 47 | +#define SIZEINT \ | |
| 48 | + (sizeof(int) < sizeof(LUA_INTEGER) ? sizeof(int) : sizeof(LUA_INTEGER)) | |
| 49 | +#define MAXBITS cast_int(SIZEINT * CHAR_BIT - 2) | |
| 50 | 50 | #define MAXASIZE (1 << MAXBITS) |
| 51 | 51 | |
| 52 | 52 | |
| @@ -67,12 +67,6 @@ · 14 unmodified lines | ||
| 67 | 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 | 70 | #define dummynode (&dummynode_) |
| 77 | 71 | |
| 78 | 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 | +*/ | |
| 84 | +static int numisinteger (lua_Number x, lua_Integer *p) { | |
| 85 | + if ((x) == l_floor(x)) /* integral value? */ | |
| 86 | + return lua_numtointeger(x, p); /* try as an integer */ | |
| 87 | + else return 0; | |
| 88 | +} | |
| 89 | + | |
| 90 | + | |
| 86 | 91 | /* |
| 87 | 92 | ** 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 | |
| 380 | 385 | Table *luaH_new (lua_State *L) { |
| 381 | − Table *t = &luaC_newobj(L, LUA_TTABLE, sizeof(Table))->h; | |
| 386 | + GCObject *o = luaC_newobj(L, LUA_TTABLE, sizeof(Table)); | |
| 387 | + Table *t = gco2t(o); | |
| 382 | 388 | t->metatable = NULL; |
| 383 | 389 | t->flags = cast_byte(~0); |
| 384 | 390 | t->array = NULL; |
| @@ -424,7 +430,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { · 39 unmodified lines | ||
| 424 | 430 | if (luai_numisnan(n)) |
| 425 | 431 | luaG_runerror(L, "table index is NaN"); |
| 426 | 432 | if (numisinteger(n, &k)) { /* index is int? */ |
| 427 | − setivalue(&aux, k); | |
| 433 | + setivalue(&aux, k); | |
| 428 | 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 | 449 | /* yes; move colliding node into free position */ |
| 444 | 450 | while (othern + gnext(othern) != mp) /* find previous */ |
| 445 | 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 | 453 | *f = *mp; /* copy colliding node into free pos. (mp->next also goes) */ |
| 448 | 454 | if (gnext(mp) != 0) { |
| 449 | − gnext(f) += mp - f; /* correct 'next' */ | |
| 455 | + gnext(f) += cast_int(mp - f); /* correct 'next' */ | |
| 450 | 456 | gnext(mp) = 0; /* now 'mp' is free */ |
| 451 | 457 | } |
| 452 | 458 | setnilvalue(gval(mp)); |
| @@ -454,9 +460,9 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { · 1 unmodified line | ||
| 454 | 460 | else { /* colliding node is in its own main position */ |
| 455 | 461 | /* new node will go into free position */ |
| 456 | 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 | 464 | else lua_assert(gnext(f) == 0); |
| 459 | − gnext(mp) = f - mp; | |
| 465 | + gnext(mp) = cast_int(f - mp); | |
| 460 | 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 | */ |
| 473 | 479 | const TValue *luaH_getint (Table *t, lua_Integer key) { |
| 474 | 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 | 482 | return &t->array[key - 1]; |
| 477 | 483 | else { |
| 478 | 484 | Node *n = hashint(t, key); |
| @@ -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 | 3 | ** Library for Table Manipulation |
| 4 | 4 | ** See Copyright Notice in lua.h |
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | 7 | |
| 8 | +#include <limits.h> | |
| 8 | 9 | #include <stddef.h> |
| 9 | 10 | |
| 10 | 11 | #define ltablib_c |
| @@ -39,16 +40,16 @@ static int maxn (lua_State *L) { · 28 unmodified lines | ||
| 39 | 40 | |
| 40 | 41 | |
| 41 | 42 | static int tinsert (lua_State *L) { |
| 42 | − int e = aux_getn(L, 1) + 1; /* first empty element */ | |
| 43 | − int pos; /* where to insert new element */ | |
| 43 | + lua_Integer e = aux_getn(L, 1) + 1; /* first empty element */ | |
| 44 | + lua_Integer pos; /* where to insert new element */ | |
| 44 | 45 | switch (lua_gettop(L)) { |
| 45 | 46 | case 2: { /* called with only 2 arguments */ |
| 46 | 47 | pos = e; /* insert new element at the end */ |
| 47 | 48 | break; |
| 48 | 49 | } |
| 49 | 50 | case 3: { |
| 50 | − int i; | |
| 51 | − pos = luaL_checkint(L, 2); /* 2nd argument is the position */ | |
| 51 | + lua_Integer i; | |
| 52 | + pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */ | |
| 52 | 53 | luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds"); |
| 53 | 54 | for (i = e; i > pos; i--) { /* move up elements */ |
| 54 | 55 | lua_rawgeti(L, 1, i-1); |
| @@ -66,8 +67,8 @@ static int tinsert (lua_State *L) { · 11 unmodified lines | ||
| 66 | 67 | |
| 67 | 68 | |
| 68 | 69 | static int tremove (lua_State *L) { |
| 69 | − int size = aux_getn(L, 1); | |
| 70 | − int pos = luaL_optint(L, 2, size); | |
| 70 | + lua_Integer size = aux_getn(L, 1); | |
| 71 | + lua_Integer pos = luaL_optinteger(L, 2, size); | |
| 71 | 72 | if (pos != size) /* validate 'pos' if given */ |
| 72 | 73 | luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds"); |
| 73 | 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 | |
| 84 | −static void addfield (lua_State *L, luaL_Buffer *b, int i) { | |
| 85 | +static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) { | |
| 85 | 86 | lua_rawgeti(L, 1, i); |
| 86 | 87 | if (!lua_isstring(L, -1)) |
| 87 | 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 | ||
| 93 | 94 | static int tconcat (lua_State *L) { |
| 94 | 95 | luaL_Buffer b; |
| 95 | 96 | size_t lsep; |
| 96 | − int i, last; | |
| 97 | + lua_Integer i, last; | |
| 97 | 98 | const char *sep = luaL_optlstring(L, 2, "", &lsep); |
| 98 | 99 | luaL_checktype(L, 1, LUA_TTABLE); |
| 99 | − i = luaL_optint(L, 3, 1); | |
| 100 | − last = luaL_opt(L, luaL_checkint, 4, luaL_len(L, 1)); | |
| 100 | + i = luaL_optinteger(L, 3, 1); | |
| 101 | + last = luaL_opt(L, luaL_checkinteger, 4, luaL_len(L, 1)); | |
| 101 | 102 | luaL_buffinit(L, &b); |
| 102 | 103 | for (; i < last; i++) { |
| 103 | 104 | addfield(L, &b, i); |
| @@ -117,34 +118,32 @@ static int tconcat (lua_State *L) { · 13 unmodified lines | ||
| 117 | 118 | */ |
| 118 | 119 | |
| 119 | 120 | static int pack (lua_State *L) { |
| 121 | + int i; | |
| 120 | 122 | int n = lua_gettop(L); /* number of elements to pack */ |
| 121 | 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 | 127 | lua_pushinteger(L, n); |
| 123 | − lua_setfield(L, -2, "n"); /* t.n = number of elements */ | |
| 124 | − if (n > 0) { /* at least one element? */ | |
| 125 | − int i; | |
| 126 | − lua_pushvalue(L, 1); | |
| 127 | − lua_rawseti(L, -2, 1); /* insert first element */ | |
| 128 | − lua_replace(L, 1); /* move table into index 1 */ | |
| 129 | − for (i = n; i >= 2; i--) /* assign other elements */ | |
| 130 | − lua_rawseti(L, 1, i); | |
| 131 | − } | |
| 128 | + lua_setfield(L, 1, "n"); /* t.n = number of elements */ | |
| 132 | 129 | return 1; /* return table */ |
| 133 | 130 | } |
| 134 | 131 | |
| 135 | 132 | |
| 136 | 133 | static int unpack (lua_State *L) { |
| 137 | − int i, e, n; | |
| 134 | + lua_Integer i, e; | |
| 135 | + lua_Unsigned n; | |
| 138 | 136 | luaL_checktype(L, 1, LUA_TTABLE); |
| 139 | − i = luaL_optint(L, 2, 1); | |
| 140 | − e = luaL_opt(L, luaL_checkint, 3, luaL_len(L, 1)); | |
| 137 | + i = luaL_optinteger(L, 2, 1); | |
| 138 | + e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1)); | |
| 141 | 139 | if (i > e) return 0; /* empty range */ |
| 142 | − n = e - i + 1; /* number of elements */ | |
| 143 | − if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arithmetic overflow */ | |
| 140 | + n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */ | |
| 141 | + if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, ++n)) | |
| 144 | 142 | return luaL_error(L, "too many results to unpack"); |
| 145 | − lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */ | |
| 146 | − while (i++ < e) /* push arg[i + 1...e] */ | |
| 147 | − lua_rawgeti(L, 1, i); | |
| 143 | + do { /* must have at least one element */ | |
| 144 | + lua_rawgeti(L, 1, i); /* push arg[i..e] */ | |
| 145 | + } while (i++ < e); | |
| 146 | + | |
| 148 | 147 | return n; |
| 149 | 148 | } |
| 150 | 149 | |
| @@ -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 | 3 | ** Tag methods |
| 4 | 4 | ** 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 | 116 | case TM_CONCAT: |
| 117 | 117 | luaG_concaterror(L, p1, p2); |
| 118 | 118 | case TM_IDIV: case TM_BAND: case TM_BOR: case TM_BXOR: |
| 119 | − case TM_SHL: case TM_SHR: case TM_BNOT: | |
| 120 | − if (ttisnumber(p1) && ttisnumber(p2)) | |
| 119 | + case TM_SHL: case TM_SHR: case TM_BNOT: { | |
| 120 | + lua_Number dummy; | |
| 121 | + if (tonumber(p1, &dummy) && tonumber(p2, &dummy)) | |
| 121 | 122 | luaG_tointerror(L, p1, p2); |
| 122 | 123 | /* else go through */ |
| 124 | + } | |
| 123 | 125 | default: |
| 124 | 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 | |
| 130 | −const TValue *luaT_getequalTM (lua_State *L, Table *mt1, Table *mt2) { | |
| 131 | − const TValue *tm1 = fasttm(L, mt1, TM_EQ); | |
| 132 | − const TValue *tm2; | |
| 133 | − if (tm1 == NULL) return NULL; /* no metamethod */ | |
| 134 | − if (mt1 == mt2) return tm1; /* same metatables => same metamethods */ | |
| 135 | − tm2 = fasttm(L, mt2, TM_EQ); | |
| 136 | − if (tm2 == NULL) return NULL; /* no metamethod */ | |
| 137 | − if (luaV_rawequalobj(tm1, tm2)) /* same metamethods? */ | |
| 138 | − return tm1; | |
| 139 | − return NULL; | |
| 140 | −} | |
| 141 | − | |
| 142 | − | |
| 143 | 132 | int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, |
| 144 | 133 | TMS event) { |
| 145 | 134 | if (!luaT_callbinTM(L, p1, p2, L->top, event)) |
| @@ -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 | 3 | ** Tag methods |
| 4 | 4 | ** 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 | 67 | StkId res, TMS event); |
| 68 | 68 | LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, |
| 69 | 69 | StkId res, TMS event); |
| 70 | −LUAI_FUNC const TValue *luaT_getequalTM (lua_State *L, Table *mt1, Table *mt2); | |
| 71 | 70 | LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, |
| 72 | 71 | const TValue *p2, TMS event); |
| 73 | 72 | |
| @@ -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 | 3 | ** Lua stand-alone interpreter |
| 4 | 4 | ** See Copyright Notice in lua.h |
| 5 | 5 | */ |
| @@ -103,17 +103,24 @@ static lua_State *globalL = NULL; · 97 unmodified lines | ||
| 103 | 103 | static const char *progname = LUA_PROGNAME; |
| 104 | 104 | |
| 105 | 105 | |
| 106 | − | |
| 106 | +/* | |
| 107 | +** Hook set by signal function to stop the interpreter. | |
| 108 | +*/ | |
| 107 | 109 | static void lstop (lua_State *L, lua_Debug *ar) { |
| 108 | 110 | (void)ar; /* unused arg. */ |
| 109 | − lua_sethook(L, NULL, 0, 0); | |
| 111 | + lua_sethook(L, NULL, 0, 0); /* reset hook */ | |
| 110 | 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 | +*/ | |
| 114 | 122 | static void laction (int i) { |
| 115 | − signal(i, SIG_DFL); /* if another SIGINT happens before lstop, | |
| 116 | − terminate process (default action) */ | |
| 123 | + signal(i, SIG_DFL); /* if another SIGINT happens, terminate process */ | |
| 117 | 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 | +*/ | |
| 142 | 153 | static void l_message (const char *pname, const char *msg) { |
| 143 | 154 | if (pname) luai_writestringerror("%s: ", pname); |
| 144 | 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 | +*/ | |
| 148 | 165 | static int report (lua_State *L, int status) { |
| 149 | − if (status != LUA_OK && !lua_isnil(L, -1)) { | |
| 150 | − const char *msg = lua_tostring(L, -1); | |
| 151 | − if (msg == NULL) msg = "(error object is not a string)"; | |
| 152 | − l_message(progname, msg); | |
| 153 | − lua_pop(L, 1); | |
| 154 | − /* force a complete garbage collection in case of errors */ | |
| 155 | − lua_gc(L, LUA_GCCOLLECT, 0); | |
| 156 | − } | |
| 157 | − return status; | |
| 158 | −} | |
| 159 | − | |
| 160 | − | |
| 161 | −/* the next function is called unprotected, so it must avoid errors */ | |
| 162 | −static void finalreport (lua_State *L, int status) { | |
| 163 | 166 | if (status != LUA_OK) { |
| 164 | 167 | const char *msg = (lua_type(L, -1) == LUA_TSTRING) ? lua_tostring(L, -1) |
| 165 | 168 | : NULL; |
| @@ -167,31 +170,39 @@ static void finalreport (lua_State *L, int status) { · 1 unmodified line | ||
| 167 | 170 | l_message(progname, msg); |
| 168 | 171 | lua_pop(L, 1); |
| 169 | 172 | } |
| 173 | + return status; | |
| 170 | 174 | } |
| 171 | 175 | |
| 172 | 176 | |
| 173 | −static int traceback (lua_State *L) { | |
| 177 | +/* | |
| 178 | +** Message handler to be used to run all chunks | |
| 179 | +*/ | |
| 180 | +static int msghandler (lua_State *L) { | |
| 174 | 181 | const char *msg = lua_tostring(L, 1); |
| 175 | − if (msg) | |
| 176 | − luaL_traceback(L, L, msg, 1); | |
| 177 | − else if (!lua_isnoneornil(L, 1)) { /* is there an error object? */ | |
| 182 | + if (msg) /* is error object a string? */ | |
| 183 | + luaL_traceback(L, L, msg, 1); /* use standard traceback */ | |
| 184 | + else if (!lua_isnoneornil(L, 1)) { /* non-string error object? */ | |
| 178 | 185 | if (!luaL_callmeta(L, 1, "__tostring")) /* try its 'tostring' metamethod */ |
| 179 | 186 | lua_pushliteral(L, "(no error message)"); |
| 180 | − } | |
| 187 | + } /* else no error object, does nothing */ | |
| 181 | 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 | +*/ | |
| 185 | 196 | static int docall (lua_State *L, int narg, int nres) { |
| 186 | 197 | int status; |
| 187 | 198 | int base = lua_gettop(L) - narg; /* function index */ |
| 188 | − lua_pushcfunction(L, traceback); /* push traceback function */ | |
| 189 | − lua_insert(L, base); /* put it under chunk and args */ | |
| 199 | + lua_pushcfunction(L, msghandler); /* push message handler */ | |
| 200 | + lua_insert(L, base); /* put it under function and args */ | |
| 190 | 201 | globalL = L; /* to be available to 'laction' */ |
| 191 | − signal(SIGINT, laction); | |
| 202 | + signal(SIGINT, laction); /* set C-signal handler */ | |
| 192 | 203 | status = lua_pcall(L, narg, nres, base); |
| 193 | − signal(SIGINT, SIG_DFL); | |
| 194 | − lua_remove(L, base); /* remove traceback function */ | |
| 204 | + signal(SIGINT, SIG_DFL); /* reset C-signal handler */ | |
| 205 | + lua_remove(L, base); /* remove message handler from the stack */ | |
| 195 | 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 | |
| 205 | −static int getargs (lua_State *L, char **argv, int n) { | |
| 206 | − int narg; | |
| 207 | − int i; | |
| 208 | − int argc = 0; | |
| 209 | − while (argv[argc]) argc++; /* count total number of arguments */ | |
| 210 | − narg = argc - (n + 1); /* number of arguments to the script */ | |
| 211 | − luaL_checkstack(L, narg + 3, "too many arguments to script"); | |
| 212 | − for (i=n+1; i < argc; i++) | |
| 213 | − lua_pushstring(L, argv[i]); | |
| 214 | − lua_createtable(L, narg, n + 1); | |
| 215 | − for (i=0; i < argc; i++) { | |
| 216 | +/* | |
| 217 | +** Create the 'arg' table, which stores all arguments from the | |
| 218 | +** command line ('argv'). It should be aligned so that, at index 0, | |
| 219 | +** it has 'argv[script]', which is the script name. The arguments | |
| 220 | +** to the script (everything after 'script') go to positive indices; | |
| 221 | +** other arguments (before the script name) go to negative indices. | |
| 222 | +** If there is no script name, assume interpreter's name as base. | |
| 223 | +*/ | |
| 224 | +static void createargtable (lua_State *L, char **argv, int argc, int script) { | |
| 225 | + int i, narg; | |
| 226 | + if (script == argc) script = 0; /* no script name? */ | |
| 227 | + narg = argc - (script + 1); /* number of positive indices */ | |
| 228 | + lua_createtable(L, narg, script + 1); | |
| 229 | + for (i = 0; i < argc; i++) { | |
| 216 | 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 | |
| 223 | −static int dofile (lua_State *L, const char *name) { | |
| 224 | − int status = luaL_loadfile(L, name); | |
| 237 | +static int dochunk (lua_State *L, int status) { | |
| 225 | 238 | if (status == LUA_OK) status = docall(L, 0, 0); |
| 226 | 239 | return report(L, status); |
| 227 | 240 | } |
| 228 | 241 | |
| 229 | 242 | |
| 243 | +static int dofile (lua_State *L, const char *name) { | |
| 244 | + return dochunk(L, luaL_loadfile(L, name)); | |
| 245 | +} | |
| 246 | + | |
| 247 | + | |
| 230 | 248 | static int dostring (lua_State *L, const char *s, const char *name) { |
| 231 | − int status = luaL_loadbuffer(L, s, strlen(s), name); | |
| 232 | − if (status == LUA_OK) status = docall(L, 0, 0); | |
| 233 | − return report(L, status); | |
| 249 | + return dochunk(L, luaL_loadbuffer(L, s, strlen(s), name)); | |
| 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 | +*/ | |
| 237 | 257 | static int dolibrary (lua_State *L, const char *name) { |
| 238 | 258 | int status; |
| 239 | 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 | +*/ | |
| 248 | 271 | static const char *get_prompt (lua_State *L, int firstline) { |
| 249 | 272 | const char *p; |
| 250 | 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 | 280 | #define EOFMARK "<eof>" |
| 258 | 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 | +*/ | |
| 260 | 289 | static int incomplete (lua_State *L, int status) { |
| 261 | 290 | if (status == LUA_ERRSYNTAX) { |
| 262 | 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 | +*/ | |
| 274 | 305 | static int pushline (lua_State *L, int firstline) { |
| 275 | 306 | char buffer[LUA_MAXINPUT]; |
| 276 | 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, stack | |
| 296 | − has either compiled chunk or original line (if compilation failed) */ | |
| 326 | +/* | |
| 327 | +** Try to compile line on the stack as 'return <line>'; on return, stack | |
| 328 | +** has either compiled chunk or original line (if compilation failed). | |
| 329 | +*/ | |
| 297 | 330 | static int addreturn (lua_State *L) { |
| 298 | 331 | int status; |
| 299 | 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 | +*/ | |
| 313 | 348 | static int multiline (lua_State *L) { |
| 314 | − for (;;) { /* repeat until gets a complete line */ | |
| 349 | + for (;;) { /* repeat until gets a complete statement */ | |
| 315 | 350 | size_t len; |
| 316 | 351 | const char *line = lua_tolstring(L, 1, &len); /* get what it has */ |
| 317 | 352 | int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */ |
| 318 | 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 | 355 | lua_pushliteral(L, "\n"); /* add newline... */ |
| 321 | 356 | lua_insert(L, -2); /* ...between the two lines */ |
| 322 | 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 | +*/ | |
| 327 | 368 | static int loadline (lua_State *L) { |
| 328 | 369 | int status; |
| 329 | 370 | lua_settop(L, 0); |
| @@ -331,29 +372,43 @@ static int loadline (lua_State *L) { · 1 unmodified line | ||
| 331 | 372 | return -1; /* no input */ |
| 332 | 373 | if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */ |
| 333 | 374 | status = multiline(L); /* try as command, maybe with continuation lines */ |
| 334 | − lua_saveline(L, 1); | |
| 335 | − lua_remove(L, 1); /* remove line */ | |
| 375 | + lua_saveline(L, 1); /* keep history */ | |
| 376 | + lua_remove(L, 1); /* remove line from the stack */ | |
| 336 | 377 | lua_assert(lua_gettop(L) == 1); |
| 337 | 378 | return status; |
| 338 | 379 | } |
| 339 | 380 | |
| 340 | 381 | |
| 341 | −static void dotty (lua_State *L) { | |
| 382 | +/* | |
| 383 | +** Prints (calling the Lua 'print' function) any values on the stack | |
| 384 | +*/ | |
| 385 | +static void l_print (lua_State *L) { | |
| 386 | + int n = lua_gettop(L); | |
| 387 | + if (n > 0) { /* any result to be printed? */ | |
| 388 | + luaL_checkstack(L, LUA_MINSTACK, "too many results to print"); | |
| 389 | + lua_getglobal(L, "print"); | |
| 390 | + lua_insert(L, 1); | |
| 391 | + if (lua_pcall(L, n, 0, 0) != LUA_OK) | |
| 392 | + l_message(progname, lua_pushfstring(L, | |
| 393 | + "error calling " LUA_QL("print") " (%s)", | |
| 394 | + lua_tostring(L, -1))); | |
| 395 | + } | |
| 396 | +} | |
| 397 | + | |
| 398 | + | |
| 399 | +/* | |
| 400 | +** Do the REPL: repeatedly read (load) a line, evaluate (call) it, and | |
| 401 | +** print any results. | |
| 402 | +*/ | |
| 403 | +static void doREPL (lua_State *L) { | |
| 342 | 404 | int status; |
| 343 | 405 | const char *oldprogname = progname; |
| 344 | − progname = NULL; | |
| 406 | + progname = NULL; /* no 'progname' on errors in interactive mode */ | |
| 345 | 407 | while ((status = loadline(L)) != -1) { |
| 346 | − if (status == LUA_OK) status = docall(L, 0, LUA_MULTRET); | |
| 347 | − report(L, status); | |
| 348 | − if (status == LUA_OK && lua_gettop(L) > 0) { /* any result to print? */ | |
| 349 | − luaL_checkstack(L, LUA_MINSTACK, "too many results to print"); | |
| 350 | − lua_getglobal(L, "print"); | |
| 351 | − lua_insert(L, 1); | |
| 352 | − if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != LUA_OK) | |
| 353 | − l_message(progname, lua_pushfstring(L, | |
| 354 | − "error calling " LUA_QL("print") " (%s)", | |
| 355 | − lua_tostring(L, -1))); | |
| 356 | − } | |
| 408 | + if (status == LUA_OK) | |
| 409 | + status = docall(L, 0, LUA_MULTRET); | |
| 410 | + if (status == LUA_OK) l_print(L); | |
| 411 | + else report(L, status); | |
| 357 | 412 | } |
| 358 | 413 | lua_settop(L, 0); /* clear stack */ |
| 359 | 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 | +*/ | |
| 422 | +static void pushargs (lua_State *L, char **argv, int n) { | |
| 423 | + int i; | |
| 424 | + luaL_checkstack(L, n + 3, "too many arguments to script"); | |
| 425 | + for (i = 1; i < n; i++) /* skip 0 (the script name) */ | |
| 426 | + lua_pushstring(L, argv[i]); | |
| 427 | +} | |
| 428 | + | |
| 429 | + | |
| 364 | 430 | static int handle_script (lua_State *L, char **argv, int n) { |
| 365 | 431 | int status; |
| 366 | − const char *fname; | |
| 367 | − int narg = getargs(L, argv, n); /* collect arguments */ | |
| 368 | − lua_setglobal(L, "arg"); | |
| 369 | − fname = argv[n]; | |
| 370 | − if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0) | |
| 432 | + const char *fname = argv[0]; | |
| 433 | + if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0) | |
| 371 | 434 | fname = NULL; /* stdin */ |
| 372 | 435 | status = luaL_loadfile(L, fname); |
| 373 | − lua_insert(L, -(narg+1)); | |
| 374 | − if (status == LUA_OK) | |
| 375 | − status = docall(L, narg, LUA_MULTRET); | |
| 376 | − else | |
| 377 | − lua_pop(L, narg); | |
| 436 | + if (status == LUA_OK) { | |
| 437 | + pushargs(L, argv, n); /* push arguments to script */ | |
| 438 | + status = docall(L, n - 1, LUA_MULTRET); | |
| 439 | + } | |
| 378 | 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 | |
| 395 | −static int collectargs (char **argv, int *args) { | |
| 452 | +/* | |
| 453 | +** Traverses all arguments from 'argv', returning a mask with those | |
| 454 | +** needed before running any Lua code (or an error code if it finds | |
| 455 | +** any invalid argument). 'first' returns the first not-handled argument | |
| 456 | +** (either the script name or a bad argument in case of error). | |
| 457 | +*/ | |
| 458 | +static int collectargs (char **argv, int *first) { | |
| 459 | + int args = 0; | |
| 396 | 460 | int i; |
| 397 | 461 | for (i = 1; argv[i] != NULL; i++) { |
| 462 | + *first = i; | |
| 398 | 463 | if (argv[i][0] != '-') /* not an option? */ |
| 399 | − return i; | |
| 400 | − switch (argv[i][1]) { /* option */ | |
| 401 | − case '-': | |
| 402 | − noextrachars(argv[i]); | |
| 403 | − return (argv[i+1] != NULL ? i+1 : 0); | |
| 404 | − case '\0': | |
| 405 | − return i; | |
| 464 | + return args; /* stop handling options */ | |
| 465 | + switch (argv[i][1]) { /* else check option */ | |
| 466 | + case '-': /* '--' */ | |
| 467 | + if (argv[i][2] != '\0') /* extra characters after '--'? */ | |
| 468 | + return has_error; /* invalid option */ | |
| 469 | + *first = i + 1; | |
| 470 | + return args; | |
| 471 | + case '\0': /* '-' */ | |
| 472 | + return args; /* script "name" is '-' */ | |
| 406 | 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 | 477 | break; |
| 409 | 478 | case 'i': |
| 410 | − noextrachars(argv[i]); | |
| 411 | − args[has_i] = 1; /* go through */ | |
| 479 | + args |= has_i; /* goes through (-i implies -v) */ | |
| 412 | 480 | case 'v': |
| 413 | − noextrachars(argv[i]); | |
| 414 | − args[has_v] = 1; | |
| 481 | + if (argv[i][2] != '\0') /* extra characters after 1st? */ | |
| 482 | + return has_error; /* invalid option */ | |
| 483 | + args |= has_v; | |
| 415 | 484 | break; |
| 416 | 485 | case 'e': |
| 417 | − args[has_e] = 1; /* go through */ | |
| 486 | + args |= has_e; /* go through */ | |
| 418 | 487 | case 'l': /* both options need an argument */ |
| 419 | 488 | if (argv[i][2] == '\0') { /* no concatenated argument? */ |
| 420 | 489 | i++; /* try next 'argv' */ |
| 421 | 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 | 493 | break; |
| 425 | − default: /* invalid option; return its index... */ | |
| 426 | − return -i; /* ...as a negative value */ | |
| 494 | + default: /* invalid option */ | |
| 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 | +*/ | |
| 433 | 507 | static int runargs (lua_State *L, char **argv, int n) { |
| 434 | 508 | int i; |
| 435 | 509 | for (i = 1; i < n; i++) { |
| 436 | − lua_assert(argv[i][0] == '-'); | |
| 437 | − switch (argv[i][1]) { /* option */ | |
| 438 | − case 'e': { | |
| 439 | − const char *chunk = argv[i] + 2; | |
| 440 | − if (*chunk == '\0') chunk = argv[++i]; | |
| 441 | − lua_assert(chunk != NULL); | |
| 442 | − if (dostring(L, chunk, "=(command line)") != LUA_OK) | |
| 443 | − return 0; | |
| 444 | − break; | |
| 445 | − } | |
| 446 | − case 'l': { | |
| 447 | − const char *filename = argv[i] + 2; | |
| 448 | − if (*filename == '\0') filename = argv[++i]; | |
| 449 | − lua_assert(filename != NULL); | |
| 450 | − if (dolibrary(L, filename) != LUA_OK) | |
| 451 | − return 0; /* stop if file fails */ | |
| 452 | − break; | |
| 453 | − } | |
| 454 | − default: break; | |
| 510 | + int status; | |
| 511 | + int option = argv[i][1]; | |
| 512 | + lua_assert(argv[i][0] == '-'); /* already checked */ | |
| 513 | + if (option == 'e' || option == 'l') { | |
| 514 | + const char *extra = argv[i] + 2; /* both options need an argument */ | |
| 515 | + if (*extra == '\0') extra = argv[++i]; | |
| 516 | + lua_assert(extra != NULL); | |
| 517 | + if (option == 'e') | |
| 518 | + status = dostring(L, extra, "=(command line)"); | |
| 519 | + else | |
| 520 | + status = dolibrary(L, extra); | |
| 521 | + if (status != LUA_OK) return 0; | |
| 455 | 522 | } |
| 456 | 523 | } |
| 457 | 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 | +*/ | |
| 476 | 547 | static int pmain (lua_State *L) { |
| 477 | 548 | int argc = (int)lua_tointeger(L, 1); |
| 478 | 549 | char **argv = (char **)lua_touserdata(L, 2); |
| 479 | 550 | int script; |
| 480 | − int args[num_has]; | |
| 481 | − args[has_i] = args[has_v] = args[has_e] = args[has_E] = 0; | |
| 551 | + int args = collectargs(argv, &script); | |
| 552 | + luaL_checkversion(L); /* check that interpreter has correct version */ | |
| 482 | 553 | if (argv[0] && argv[0][0]) progname = argv[0]; |
| 483 | − script = collectargs(argv, args); | |
| 484 | − if (script < 0) { /* invalid arg? */ | |
| 485 | − print_usage(argv[-script]); | |
| 554 | + if (args == has_error) { /* bad arg? */ | |
| 555 | + print_usage(argv[script]); /* 'script' has index of bad arg. */ | |
| 486 | 556 | return 0; |
| 487 | 557 | } |
| 488 | − if (args[has_v]) print_version(); | |
| 489 | − if (args[has_E]) { /* option '-E'? */ | |
| 558 | + if (args & has_v) /* option '-v'? */ | |
| 559 | + print_version(); | |
| 560 | + if (args & has_E) { /* option '-E'? */ | |
| 490 | 561 | lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */ |
| 491 | 562 | lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); |
| 492 | 563 | } |
| 493 | − /* open standard libraries */ | |
| 494 | − luaL_checkversion(L); | |
| 495 | − lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */ | |
| 496 | − luaL_openlibs(L); /* open libraries */ | |
| 497 | − lua_gc(L, LUA_GCRESTART, 0); | |
| 498 | − if (!args[has_E] && handle_luainit(L) != LUA_OK) | |
| 499 | − return 0; /* error running LUA_INIT */ | |
| 500 | − /* execute arguments -e and -l */ | |
| 501 | − if (!runargs(L, argv, (script > 0) ? script : argc)) return 0; | |
| 502 | − /* execute main script (if there is one) */ | |
| 503 | − if (script && handle_script(L, argv, script) != LUA_OK) return 0; | |
| 504 | − if (args[has_i]) /* -i option? */ | |
| 505 | − dotty(L); | |
| 506 | − else if (script == 0 && !args[has_e] && !args[has_v]) { /* no arguments? */ | |
| 507 | − if (lua_stdin_is_tty()) { | |
| 564 | + luaL_openlibs(L); /* open standard libraries */ | |
| 565 | + createargtable(L, argv, argc, script); /* create table 'arg' */ | |
| 566 | + if (!(args & has_E)) { /* no option '-E'? */ | |
| 567 | + if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ | |
| 568 | + return 0; /* error running LUA_INIT */ | |
| 569 | + } | |
| 570 | + if (!runargs(L, argv, script)) /* execute arguments -e and -l */ | |
| 571 | + return 0; /* something failed */ | |
| 572 | + if (script < argc && /* execute main script (if there is one) */ | |
| 573 | + handle_script(L, argv + script, argc - script) != LUA_OK) | |
| 574 | + return 0; | |
| 575 | + if (args & has_i) /* -i option? */ | |
| 576 | + doREPL(L); /* do read-eval-print loop */ | |
| 577 | + else if (script == argc && !(args & (has_e | has_v))) { /* no arguments? */ | |
| 578 | + if (lua_stdin_is_tty()) { /* running in interactive mode? */ | |
| 508 | 579 | print_version(); |
| 509 | − dotty(L); | |
| 580 | + doREPL(L); /* do read-eval-print loop */ | |
| 510 | 581 | } |
| 511 | 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 | 593 | l_message(argv[0], "cannot create state: not enough memory"); |
| 523 | 594 | return EXIT_FAILURE; |
| 524 | 595 | } |
| 525 | − /* call 'pmain' in protected mode */ | |
| 526 | − lua_pushcfunction(L, &pmain); | |
| 596 | + lua_pushcfunction(L, &pmain); /* to call 'pmain' in protected mode */ | |
| 527 | 597 | lua_pushinteger(L, argc); /* 1st argument */ |
| 528 | 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 | 600 | result = lua_toboolean(L, -1); /* get result */ |
| 531 | − finalreport(L, status); | |
| 601 | + report(L, status); | |
| 532 | 602 | lua_close(L); |
| 533 | 603 | return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE; |
| 534 | 604 | } |
| @@ -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 | 3 | ** Lua - A Scripting Language |
| 4 | 4 | ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) |
| 5 | 5 | ** See Copyright Notice at the end of this file |
| @@ -19,7 +19,7 @@ · 13 unmodified lines | ||
| 19 | 19 | #define LUA_VERSION_MAJOR "5" |
| 20 | 20 | #define LUA_VERSION_MINOR "3" |
| 21 | 21 | #define LUA_VERSION_NUM 503 |
| 22 | −#define LUA_VERSION_RELEASE "0 (work2)" | |
| 22 | +#define LUA_VERSION_RELEASE "0 (work3)" | |
| 23 | 23 | |
| 24 | 24 | #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR |
| 25 | 25 | #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE |
| @@ -53,8 +53,16 @@ · 27 unmodified lines | ||
| 53 | 53 | |
| 54 | 54 | typedef struct lua_State lua_State; |
| 55 | 55 | |
| 56 | +/* | |
| 57 | +** Type for C functions registered with Lua | |
| 58 | +*/ | |
| 56 | 59 | typedef int (*lua_CFunction) (lua_State *L); |
| 57 | 60 | |
| 61 | +/* | |
| 62 | +** Type for continuation functions | |
| 63 | +*/ | |
| 64 | +typedef int (*lua_KFunction) (lua_State *L, int status, int ctx); | |
| 65 | + | |
| 58 | 66 | |
| 59 | 67 | /* |
| 60 | 68 | ** 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 | ||
| 145 | 153 | LUA_API int (lua_gettop) (lua_State *L); |
| 146 | 154 | LUA_API void (lua_settop) (lua_State *L, int idx); |
| 147 | 155 | LUA_API void (lua_pushvalue) (lua_State *L, int idx); |
| 148 | −LUA_API void (lua_remove) (lua_State *L, int idx); | |
| 149 | −LUA_API void (lua_insert) (lua_State *L, int idx); | |
| 156 | +LUA_API void (lua_rotate) (lua_State *L, int idx, int n); | |
| 150 | 157 | LUA_API void (lua_replace) (lua_State *L, int idx); |
| 151 | 158 | LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx); |
| 152 | 159 | LUA_API int (lua_checkstack) (lua_State *L, int sz); |
| @@ -238,7 +245,7 @@ LUA_API int (lua_rawgetp) (lua_State *L, int idx, const void *p); · 85 unmodified lines | ||
| 238 | 245 | LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); |
| 239 | 246 | LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz); |
| 240 | 247 | LUA_API int (lua_getmetatable) (lua_State *L, int objindex); |
| 241 | −LUA_API void (lua_getuservalue) (lua_State *L, int idx); | |
| 248 | +LUA_API int (lua_getuservalue) (lua_State *L, int idx); | |
| 242 | 249 | |
| 243 | 250 | |
| 244 | 251 | /* |
| @@ -258,13 +265,11 @@ LUA_API void (lua_setuservalue) (lua_State *L, int idx); · 13 unmodified lines | ||
| 258 | 265 | ** 'load' and 'call' functions (load and run Lua code) |
| 259 | 266 | */ |
| 260 | 267 | LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults, int ctx, |
| 261 | − lua_CFunction k); | |
| 268 | + lua_KFunction k); | |
| 262 | 269 | #define lua_call(L,n,r) lua_callk(L, (n), (r), 0, NULL) |
| 263 | 270 | |
| 264 | −LUA_API int (lua_getctx) (lua_State *L, int *ctx); | |
| 265 | − | |
| 266 | 271 | LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc, |
| 267 | − int ctx, lua_CFunction k); | |
| 272 | + int ctx, lua_KFunction k); | |
| 268 | 273 | #define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL) |
| 269 | 274 | |
| 270 | 275 | LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt, |
| @@ -278,10 +283,12 @@ LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip); · 7 unmodified lines | ||
| 278 | 283 | ** coroutine functions |
| 279 | 284 | */ |
| 280 | 285 | LUA_API int (lua_yieldk) (lua_State *L, int nresults, int ctx, |
| 281 | − lua_CFunction k); | |
| 286 | + lua_KFunction k); | |
| 282 | 287 | #define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL) |
| 283 | 288 | LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg); |
| 284 | 289 | LUA_API int (lua_status) (lua_State *L); |
| 290 | +LUA_API int lua_isyieldable (lua_State *L); | |
| 291 | + | |
| 285 | 292 | |
| 286 | 293 | /* |
| 287 | 294 | ** garbage-collection function and options |
| @@ -311,7 +318,7 @@ LUA_API int (lua_next) (lua_State *L, int idx); · 23 unmodified lines | ||
| 311 | 318 | LUA_API void (lua_concat) (lua_State *L, int n); |
| 312 | 319 | LUA_API void (lua_len) (lua_State *L, int idx); |
| 313 | 320 | |
| 314 | −LUA_API int (lua_strtonum) (lua_State *L, const char *s, size_t len); | |
| 321 | +LUA_API size_t (lua_strtonum) (lua_State *L, const char *s); | |
| 315 | 322 | |
| 316 | 323 | LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); |
| 317 | 324 | LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); |
| @@ -324,9 +331,9 @@ LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); · 6 unmodified lines | ||
| 324 | 331 | ** =============================================================== |
| 325 | 332 | */ |
| 326 | 333 | |
| 327 | −#define lua_tonumber(L,i) lua_tonumberx(L,i,NULL) | |
| 328 | −#define lua_tointeger(L,i) lua_tointegerx(L,i,NULL) | |
| 329 | −#define lua_tounsigned(L,i) lua_tounsignedx(L,i,NULL) | |
| 334 | +#define lua_tonumber(L,i) lua_tonumberx(L,(i),NULL) | |
| 335 | +#define lua_tointeger(L,i) lua_tointegerx(L,(i),NULL) | |
| 336 | +#define lua_tounsigned(L,i) lua_tounsignedx(L,(i),NULL) | |
| 330 | 337 | |
| 331 | 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 | 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 | ** {====================================================================== |
| @@ -202,10 +202,8 @@ int main(int argc, char* argv[]) · 201 unmodified lines | ||
| 202 | 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 | 207 | ** print bytecodes |
| 210 | 208 | ** See Copyright Notice in lua.h |
| 211 | 209 | */ |
| @@ -253,7 +251,7 @@ static void PrintString(const TString* ts) · 41 unmodified lines | ||
| 253 | 251 | static void PrintConstant(const Proto* f, int i) |
| 254 | 252 | { |
| 255 | 253 | const TValue* o=&f->k[i]; |
| 256 | − switch (ttypenv(o)) | |
| 254 | + switch (ttype(o)) | |
| 257 | 255 | { |
| 258 | 256 | case LUA_TNIL: |
| 259 | 257 | printf("nil"); |
| @@ -261,10 +259,13 @@ static void PrintConstant(const Proto* f, int i) · 1 unmodified line | ||
| 261 | 259 | case LUA_TBOOLEAN: |
| 262 | 260 | printf(bvalue(o) ? "true" : "false"); |
| 263 | 261 | break; |
| 264 | − case LUA_TNUMBER: | |
| 265 | − printf(LUA_NUMBER_FMT,nvalue(o)); | |
| 262 | + case LUA_TNUMFLT: | |
| 263 | + printf(LUA_NUMBER_FMT,fltvalue(o)); | |
| 266 | 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 | 269 | PrintString(rawtsvalue(o)); |
| 269 | 270 | break; |
| 270 | 271 | default: /* cannot happen */ |
| @@ -339,8 +340,14 @@ static void PrintCode(const Proto* f) · 68 unmodified lines | ||
| 339 | 340 | case OP_ADD: |
| 340 | 341 | case OP_SUB: |
| 341 | 342 | case OP_MUL: |
| 342 | − case OP_DIV: | |
| 343 | 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 | 351 | case OP_EQ: |
| 345 | 352 | case OP_LT: |
| 346 | 353 | case OP_LE: |
| @@ -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 | 3 | ** Configuration file for Lua |
| 4 | 4 | ** 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 for | |
| 25 | −@* Lua integers; | |
| 26 | −@@ LUA_REAL_FLOAT / LUA_REAL_DOUBLE / LUA_REAL_LONGDOUBLE defines size for | |
| 27 | −@* Lua floats. | |
| 24 | +@@ LUA_INT_INT / LUA_INT_LONG / LUA_INT_LONGLONG defines type for | |
| 25 | +@@ Lua integers; | |
| 26 | +@@ LUA_REAL_FLOAT / LUA_REAL_DOUBLE / LUA_REAL_LONGDOUBLE defines | |
| 27 | +@@ type for Lua floats. | |
| 28 | 28 | ** |
| 29 | 29 | ** These definitions set the numeric types for Lua. Lua should work |
| 30 | −** fine with 32-bit or 64-bit integers mixed with 32-bit or 64-bit | |
| 31 | −** floats. The usual configurations are 64-bit integers and floats (the | |
| 32 | −** default) and 32-bit integers and floats (Small Lua, for restricted | |
| 33 | −** hardware). | |
| 30 | +** fine with any mix of these previous options. | |
| 31 | +** The usual configurations are 64-bit integers and floats (the default) | |
| 32 | +** and 32-bit integers and floats (Small Lua, for restricted hardware). | |
| 34 | 33 | ** ===================================================================== |
| 35 | 34 | */ |
| 36 | 35 | #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 | 77 | ** CHANGE it (define it) if your system is compatible. |
| 79 | 78 | */ |
| 80 | 79 | #if defined(LUA_USE_C99) |
| @@ -84,7 +83,7 @@ · 3 unmodified lines | ||
| 84 | 83 | |
| 85 | 84 | /* |
| 86 | 85 | @@ LUA_USE_POSIX includes all functionality listed as X/Open System |
| 87 | −@* Interfaces Extension (XSI). | |
| 86 | +@@ Interfaces Extension (XSI). | |
| 88 | 87 | ** CHANGE it (define it) if your system is XSI compatible. |
| 89 | 88 | */ |
| 90 | 89 | #if defined(LUA_USE_POSIX) |
| @@ -94,33 +93,37 @@ · 3 unmodified lines | ||
| 94 | 93 | |
| 95 | 94 | /* |
| 96 | 95 | @@ LUA_PATH_DEFAULT is the default path that Lua uses to look for |
| 97 | −@* Lua libraries. | |
| 96 | +@@ Lua libraries. | |
| 98 | 97 | @@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for |
| 99 | −@* C libraries. | |
| 98 | +@@ C libraries. | |
| 100 | 99 | ** CHANGE them if your machine has a non-conventional directory |
| 101 | 100 | ** hierarchy or if you want to install your libraries in |
| 102 | 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 | 106 | ** In Windows, any exclamation mark ('!') in the path is replaced by the |
| 107 | 107 | ** path of the directory of the executable file of the current process. |
| 108 | 108 | */ |
| 109 | 109 | #define LUA_LDIR "!\\lua\\" |
| 110 | 110 | #define LUA_CDIR "!\\" |
| 111 | +#define LUA_SHRDIR "!\\..\\share\\lua\\" LUA_VDIR "\\" | |
| 111 | 112 | #define LUA_PATH_DEFAULT \ |
| 112 | 113 | LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \ |
| 113 | 114 | LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" \ |
| 115 | + LUA_SHRDIR"?.lua;" LUA_SHRDIR"?\\init.lua;" \ | |
| 114 | 116 | ".\\?.lua;" ".\\?\\init.lua" |
| 115 | 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 | 122 | #else /* }{ */ |
| 119 | 123 | |
| 120 | −#define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR "/" | |
| 121 | 124 | #define LUA_ROOT "/usr/local/" |
| 122 | −#define LUA_LDIR LUA_ROOT "share/lua/" LUA_VDIR | |
| 123 | −#define LUA_CDIR LUA_ROOT "lib/lua/" LUA_VDIR | |
| 125 | +#define LUA_LDIR LUA_ROOT "share/lua/" LUA_VDIR "/" | |
| 126 | +#define LUA_CDIR LUA_ROOT "lib/lua/" LUA_VDIR "/" | |
| 124 | 127 | #define LUA_PATH_DEFAULT \ |
| 125 | 128 | LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \ |
| 126 | 129 | LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua;" \ |
| @@ -181,10 +184,10 @@ · 54 unmodified lines | ||
| 181 | 184 | |
| 182 | 185 | /* |
| 183 | 186 | @@ 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 | 188 | @@ LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables |
| 186 | −@* that are not to be exported to outside modules (LUAI_DDEF for | |
| 187 | −@* definitions and LUAI_DDEC for declarations). | |
| 189 | +@@ that are not to be exported to outside modules (LUAI_DDEF for | |
| 190 | +@@ definitions and LUAI_DDEC for declarations). | |
| 188 | 191 | ** CHANGE them if you need to mark them in some special way. Elf/gcc |
| 189 | 192 | ** (versions 3.2 and later) mark them as "hidden" to optimize access |
| 190 | 193 | ** 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 | 222 | @@ 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 | 224 | ** CHANGE it if you want a different size. |
| 222 | 225 | */ |
| 223 | 226 | #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 | 267 | ** You can define it to get all options, or change specific options |
| 264 | 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 | 279 | @@ LUA_COMPAT_BITLIB controls the presence of library 'bit32'. |
| 270 | 280 | */ |
| 271 | 281 | #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 | 298 | @@ LUA_COMPAT_UNPACK controls the presence of global 'unpack'. |
| 275 | 299 | ** You can replace it with 'table.unpack'. |
| @@ -352,9 +376,9 @@ · 76 unmodified lines | ||
| 352 | 376 | /* |
| 353 | 377 | @@ LUA_INT32 is an signed integer with exactly 32 bits. |
| 354 | 378 | @@ LUAI_UMEM is an unsigned integer big enough to count the total |
| 355 | −@* memory used by Lua. | |
| 379 | +@@ memory used by Lua. | |
| 356 | 380 | @@ LUAI_MEM is a signed integer big enough to count the total memory |
| 357 | −@* used by Lua. | |
| 381 | +@@ used by Lua. | |
| 358 | 382 | ** CHANGE here if for some weird reason the default definitions are not |
| 359 | 383 | ** good enough for your machine. Probably you do not need to change |
| 360 | 384 | ** this. |
| @@ -408,10 +432,9 @@ · 47 unmodified lines | ||
| 408 | 432 | @@ LUA_NUMBER is the floating-point type used by Lua. |
| 409 | 433 | ** |
| 410 | 434 | @@ LUAI_UACNUMBER is the result of an 'usual argument conversion' |
| 411 | −@* over a floating number. | |
| 435 | +@@ over a floating number. | |
| 412 | 436 | ** |
| 413 | 437 | @@ LUA_NUMBER_FRMLEN is the length modifier for writing floats. |
| 414 | −@@ LUA_NUMBER_SCAN is the format for reading floats. | |
| 415 | 438 | @@ LUA_NUMBER_FMT is the format for writing floats. |
| 416 | 439 | @@ lua_number2str converts a float to a string. |
| 417 | 440 | ** |
| @@ -427,7 +450,6 @@ · 9 unmodified lines | ||
| 427 | 450 | #define LUAI_UACNUMBER double |
| 428 | 451 | |
| 429 | 452 | #define LUA_NUMBER_FRMLEN "" |
| 430 | −#define LUA_NUMBER_SCAN "%f" | |
| 431 | 453 | #define LUA_NUMBER_FMT "%.7g" |
| 432 | 454 | |
| 433 | 455 | #define l_mathop(op) op##f |
| @@ -442,7 +464,6 @@ · 8 unmodified lines | ||
| 442 | 464 | #define LUAI_UACNUMBER long double |
| 443 | 465 | |
| 444 | 466 | #define LUA_NUMBER_FRMLEN "L" |
| 445 | −#define LUA_NUMBER_SCAN "%Lf" | |
| 446 | 467 | #define LUA_NUMBER_FMT "%.19Lg" |
| 447 | 468 | |
| 448 | 469 | #define l_mathop(op) op##l |
| @@ -456,7 +477,6 @@ · 7 unmodified lines | ||
| 456 | 477 | #define LUAI_UACNUMBER double |
| 457 | 478 | |
| 458 | 479 | #define LUA_NUMBER_FRMLEN "" |
| 459 | −#define LUA_NUMBER_SCAN "%lf" | |
| 460 | 480 | #define LUA_NUMBER_FMT "%.14g" |
| 461 | 481 | |
| 462 | 482 | #define l_mathop(op) op |
| @@ -489,15 +509,30 @@ · 26 unmodified lines | ||
| 489 | 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 | 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 | 531 | /* the following operations need the math library */ |
| 498 | 532 | #if defined(lobject_c) || defined(lvm_c) |
| 499 | 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 | 536 | #define luai_numpow(L,a,b) ((void)L, l_mathop(pow)(a,b)) |
| 502 | 537 | #endif |
| 503 | 538 | |
| @@ -528,44 +563,99 @@ · 24 unmodified lines | ||
| 528 | 563 | ** |
| 529 | 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 | 568 | @@ LUA_INTEGER_FRMLEN is the length modifier for reading/writing integers. |
| 532 | −@@ LUA_INTEGER_SCAN is the format for reading integers. | |
| 533 | 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 | 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 | 593 | #if defined(LUA_INT_INT) /* { int */ |
| 538 | 594 | |
| 539 | 595 | #define LUA_INTEGER int |
| 540 | 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 | 602 | #elif defined(LUA_INT_LONG) /* }{ long */ |
| 543 | 603 | |
| 544 | 604 | #define LUA_INTEGER long |
| 545 | 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 | 611 | #elif defined(LUA_INT_LONGLONG) /* }{ long long */ |
| 548 | 612 | |
| 549 | 613 | #if defined(_WIN32) |
| 614 | + | |
| 550 | 615 | #define LUA_INTEGER __int64 |
| 551 | 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 | 622 | #else |
| 623 | + | |
| 553 | 624 | #define LUA_INTEGER long long |
| 554 | 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 | 631 | #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" | |
| 565 | −#define LUA_INTEGER_FMT "%" LUA_INTEGER_FRMLEN "d" | |
| 566 | −#define lua_integer2str(s,n) sprintf((s), LUA_INTEGER_FMT, (n)) | |
| 649 | +#undef LUAI_MAXSTACK | |
| 650 | +#define LUAI_MAXSTACK 15000 | |
| 567 | 651 | |
| 568 | −#define LUA_UNSIGNED unsigned LUA_INTEGER | |
| 652 | +#define l_castS2U(x) ((LUA_UNSIGNED)(unsigned short)(x)) | |
| 653 | + | |
| 654 | +#else /* }{ */ | |
| 655 | + | |
| 656 | +#error "numeric integer type not defined" | |
| 657 | + | |
| 658 | +#endif /* } */ | |
| 569 | 659 | |
| 570 | 660 | /* }================================================================== */ |
| 571 | 661 | |
| @@ -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 | 3 | ** load precompiled Lua chunks |
| 4 | 4 | ** 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 | 44 | ** All high-level loads go through LoadVector; you can change it to |
| 45 | −** adapt to the endianess of the input | |
| 45 | +** adapt to the endianness of the input | |
| 46 | 46 | */ |
| 47 | 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 | |
| 108 | −static void LoadFunction(LoadState *S, Proto *f); | |
| 108 | +static void LoadFunction(LoadState *S, Proto *f, TString *psource); | |
| 109 | 109 | |
| 110 | 110 | |
| 111 | 111 | static void LoadConstants (LoadState *S, Proto *f) { |
| 112 | − int i, n; | |
| 113 | − n = LoadInt(S); | |
| 112 | + int i; | |
| 113 | + int n = LoadInt(S); | |
| 114 | 114 | f->k = luaM_newvector(S->L, n, TValue); |
| 115 | 115 | f->sizek = n; |
| 116 | 116 | for (i = 0; i < n; i++) |
| @@ -126,7 +126,7 @@ static void LoadConstants (LoadState *S, Proto *f) { · 9 unmodified lines | ||
| 126 | 126 | setbvalue(o, LoadByte(S)); |
| 127 | 127 | break; |
| 128 | 128 | case LUA_TNUMFLT: |
| 129 | − setnvalue(o, LoadNumber(S)); | |
| 129 | + setfltvalue(o, LoadNumber(S)); | |
| 130 | 130 | break; |
| 131 | 131 | case LUA_TNUMINT: |
| 132 | 132 | setivalue(o, LoadInteger(S)); |
| @@ -139,14 +139,19 @@ static void LoadConstants (LoadState *S, Proto *f) { · 6 unmodified lines | ||
| 139 | 139 | lua_assert(0); |
| 140 | 140 | } |
| 141 | 141 | } |
| 142 | − n = LoadInt(S); | |
| 142 | +} | |
| 143 | + | |
| 144 | + | |
| 145 | +static void LoadProtos (LoadState *S, Proto *f) { | |
| 146 | + int i; | |
| 147 | + int n = LoadInt(S); | |
| 143 | 148 | f->p = luaM_newvector(S->L, n, Proto *); |
| 144 | 149 | f->sizep = n; |
| 145 | 150 | for (i = 0; i < n; i++) |
| 146 | 151 | f->p[i] = NULL; |
| 147 | 152 | for (i = 0; i < n; i++) { |
| 148 | 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 | |
| 168 | 173 | static void LoadDebug (LoadState *S, Proto *f) { |
| 169 | 174 | int i, n; |
| 170 | − f->source = LoadString(S); | |
| 171 | 175 | n = LoadInt(S); |
| 172 | 176 | f->lineinfo = luaM_newvector(S->L, n, int); |
| 173 | 177 | f->sizelineinfo = n; |
| @@ -188,7 +192,10 @@ static void LoadDebug (LoadState *S, Proto *f) { · 14 unmodified lines | ||
| 188 | 192 | } |
| 189 | 193 | |
| 190 | 194 | |
| 191 | −static void LoadFunction (LoadState *S, Proto *f) { | |
| 195 | +static void LoadFunction (LoadState *S, Proto *f, TString *psource) { | |
| 196 | + f->source = LoadString(S); | |
| 197 | + if (f->source == NULL) /* no source in dump? */ | |
| 198 | + f->source = psource; /* reuse parent's source */ | |
| 192 | 199 | f->linedefined = LoadInt(S); |
| 193 | 200 | f->lastlinedefined = LoadInt(S); |
| 194 | 201 | f->numparams = LoadByte(S); |
| @@ -197,13 +204,14 @@ static void LoadFunction (LoadState *S, Proto *f) { · 2 unmodified lines | ||
| 197 | 204 | LoadCode(S, f); |
| 198 | 205 | LoadConstants(S, f); |
| 199 | 206 | LoadUpvalues(S, f); |
| 207 | + LoadProtos(S, f); | |
| 200 | 208 | LoadDebug(S, f); |
| 201 | 209 | } |
| 202 | 210 | |
| 203 | 211 | |
| 204 | 212 | static void checkliteral (LoadState *S, const char *s, const char *msg) { |
| 205 | 213 | char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */ |
| 206 | − int len = strlen(s); | |
| 214 | + size_t len = strlen(s); | |
| 207 | 215 | LoadVector(S, buff, len); |
| 208 | 216 | if (memcmp(s, buff, len) != 0) |
| 209 | 217 | error(S, msg); |
| @@ -231,7 +239,7 @@ static void checkHeader (LoadState *S) { · 21 unmodified lines | ||
| 231 | 239 | checksize(S, lua_Integer); |
| 232 | 240 | checksize(S, lua_Number); |
| 233 | 241 | if (LoadInteger(S) != LUAC_INT) |
| 234 | − error(S, "endianess mismatch in"); | |
| 242 | + error(S, "endianness mismatch in"); | |
| 235 | 243 | if (LoadNumber(S) != LUAC_NUM) |
| 236 | 244 | error(S, "float format mismatch in"); |
| 237 | 245 | } |
| @@ -240,10 +248,10 @@ static void checkHeader (LoadState *S) { · 2 unmodified lines | ||
| 240 | 248 | /* |
| 241 | 249 | ** load precompiled chunk |
| 242 | 250 | */ |
| 243 | −Closure *luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff, | |
| 244 | − const char *name) { | |
| 251 | +LClosure *luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff, | |
| 252 | + const char *name) { | |
| 245 | 253 | LoadState S; |
| 246 | − Closure *cl; | |
| 254 | + LClosure *cl; | |
| 247 | 255 | if (*name == '@' || *name == '=') |
| 248 | 256 | S.name = name + 1; |
| 249 | 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 | 265 | cl = luaF_newLclosure(L, LoadByte(&S)); |
| 258 | 266 | setclLvalue(L, L->top, cl); |
| 259 | 267 | incr_top(L); |
| 260 | − cl->l.p = luaF_newproto(L); | |
| 261 | − LoadFunction(&S, cl->l.p); | |
| 262 | − lua_assert(cl->l.nupvalues == cl->l.p->sizeupvalues); | |
| 263 | − luai_verifycode(L, buff, cl->l.p); | |
| 268 | + cl->p = luaF_newproto(L); | |
| 269 | + LoadFunction(&S, cl->p, NULL); | |
| 270 | + lua_assert(cl->nupvalues == cl->p->sizeupvalues); | |
| 271 | + luai_verifycode(L, buff, cl->p); | |
| 264 | 272 | return cl; |
| 265 | 273 | } |
| 266 | 274 | |
| @@ -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 | 3 | ** load precompiled Lua chunks |
| 4 | 4 | ** See Copyright Notice in lua.h |
| 5 | 5 | */ |
| @@ -15,7 +15,7 @@ · 9 unmodified lines | ||
| 15 | 15 | /* data to catch conversion errors */ |
| 16 | 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 | 19 | #define LUAC_NUM cast_num(370.5) |
| 20 | 20 | |
| 21 | 21 | #define MYINT(s) (s[0]-'0') |
| @@ -23,8 +23,8 @@ · 1 unmodified line | ||
| 23 | 23 | #define LUAC_FORMAT 0 /* this is the official format */ |
| 24 | 24 | |
| 25 | 25 | /* load one chunk; from lundump.c */ |
| 26 | −LUAI_FUNC Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, | |
| 27 | − const char* name); | |
| 26 | +LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, | |
| 27 | + const char* name); | |
| 28 | 28 | |
| 29 | 29 | /* dump one chunk; from ldump.c */ |
| 30 | 30 | LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, |
| @@ -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 | 3 | ** Standard library for UTF-8 manipulation |
| 4 | 4 | ** 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 | */ |
| 37 | 37 | static const char *utf8_decode (const char *o, int *val) { |
| 38 | 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'; | |
| 65 | −** nil if 's' not well formed | |
| 64 | +** utf8len(s [, i [, j]]) --> number of characters that start in the | |
| 65 | +** range [i,j], or nil + current position if 's' is not well formed in | |
| 66 | +** that interval | |
| 66 | 67 | */ |
| 67 | 68 | static int utflen (lua_State *L) { |
| 68 | 69 | int n = 0; |
| 69 | − const char *ends; | |
| 70 | 70 | size_t len; |
| 71 | 71 | const char *s = luaL_checklstring(L, 1, &len); |
| 72 | − lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), 1); | |
| 73 | − luaL_argcheck(L, 1 <= posi && posi <= (lua_Integer)len, 1, | |
| 72 | + lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len); | |
| 73 | + lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len); | |
| 74 | + luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2, | |
| 74 | 75 | "initial position out of string"); |
| 75 | − ends = s + len; | |
| 76 | − s += posi - 1; | |
| 77 | − while (s < ends && (s = utf8_decode(s, NULL)) != NULL) | |
| 76 | + luaL_argcheck(L, --posj < (lua_Integer)len, 3, | |
| 77 | + "final position out of string"); | |
| 78 | + while (posi <= posj) { | |
| 79 | + const char *s1 = utf8_decode(s + posi, NULL); | |
| 80 | + if (s1 == NULL) { /* conversion error? */ | |
| 81 | + lua_pushnil(L); /* return nil ... */ | |
| 82 | + lua_pushinteger(L, posi + 1); /* ... and current position */ | |
| 83 | + return 2; | |
| 84 | + } | |
| 85 | + posi = s1 - s; | |
| 78 | 86 | n++; |
| 79 | − if (s == ends) | |
| 80 | − lua_pushinteger(L, n); | |
| 81 | − else | |
| 82 | − lua_pushnil(L); | |
| 87 | + } | |
| 88 | + lua_pushinteger(L, n); | |
| 83 | 89 | return 1; |
| 84 | 90 | } |
| 85 | 91 | |
| 86 | 92 | |
| 87 | 93 | /* |
| 88 | 94 | ** codepoint(s, [i, [j]]) -> returns codepoints for all characters |
| 89 | −** between i and j | |
| 95 | +** that start in the range [i,j] | |
| 90 | 96 | */ |
| 91 | 97 | static int codepoint (lua_State *L) { |
| 92 | 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 | 155 | ** position 'i' starts; 0 means character at 'i'. |
| 150 | 156 | */ |
| 151 | 157 | static int byteoffset (lua_State *L) { |
| 152 | 158 | size_t len; |
| 153 | 159 | const char *s = luaL_checklstring(L, 1, &len); |
| 154 | 160 | int n = luaL_checkint(L, 2); |
| 155 | − lua_Integer posi = u_posrelat(luaL_optinteger(L, 3, 1), len) - 1; | |
| 156 | − luaL_argcheck(L, 0 <= posi && posi <= (lua_Integer)len, 3, | |
| 161 | + lua_Integer posi = (n >= 0) ? 1 : len + 1; | |
| 162 | + posi = u_posrelat(luaL_optinteger(L, 3, posi), len); | |
| 163 | + luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3, | |
| 157 | 164 | "position out of range"); |
| 158 | 165 | if (n == 0) { |
| 159 | 166 | /* find beginning of current byte sequence */ |
| 160 | 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 | 169 | else { |
| 171 | − n--; /* do not move for 1st character */ | |
| 172 | − while (n > 0 && posi < (lua_Integer)len) { | |
| 173 | − do { /* find beginning of next character */ | |
| 174 | − posi++; | |
| 175 | − } while (iscont(s + posi)); /* ('\0' is not continuation) */ | |
| 176 | − n--; | |
| 177 | − } | |
| 170 | + if (iscont(s + posi)) | |
| 171 | + luaL_error(L, "initial position is a continuation byte"); | |
| 172 | + if (n < 0) { | |
| 173 | + while (n < 0 && posi > 0) { /* move back */ | |
| 174 | + do { /* find beginning of previous character */ | |
| 175 | + posi--; | |
| 176 | + } while (posi > 0 && iscont(s + posi)); | |
| 177 | + n++; | |
| 178 | + } | |
| 179 | + } | |
| 180 | + else { | |
| 181 | + n--; /* do not move for 1st character */ | |
| 182 | + while (n > 0 && posi < (lua_Integer)len) { | |
| 183 | + do { /* find beginning of next character */ | |
| 184 | + posi++; | |
| 185 | + } while (iscont(s + posi)); /* (cannot pass final '\0') */ | |
| 186 | + n--; | |
| 187 | + } | |
| 188 | + } | |
| 178 | 189 | } |
| 179 | − if (n == 0) | |
| 190 | + if (n == 0) /* did it find given character? */ | |
| 180 | 191 | lua_pushinteger(L, posi + 1); |
| 181 | − else | |
| 182 | − lua_pushnil(L); /* no such position */ | |
| 192 | + else /* no such character */ | |
| 193 | + lua_pushnil(L); | |
| 183 | 194 | return 1; |
| 184 | 195 | } |
| 185 | 196 | |
| @@ -187,7 +198,7 @@ static int byteoffset (lua_State *L) { · 1 unmodified line | ||
| 187 | 198 | static int iter_aux (lua_State *L) { |
| 188 | 199 | size_t len; |
| 189 | 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 | 202 | if (n < 0) /* first iteration? */ |
| 192 | 203 | n = 0; /* start from here */ |
| 193 | 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 | |
| 234 | −int luaopen_utf8 (lua_State *L) { | |
| 245 | +LUAMOD_API int luaopen_utf8 (lua_State *L) { | |
| 235 | 246 | luaL_newlib(L, funcs); |
| 236 | 247 | lua_pushliteral(L, UTF8PATT); |
| 237 | 248 | lua_setfield(L, -2, "charpatt"); |
| @@ -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 | 3 | ** Lua virtual machine |
| 4 | 4 | ** See Copyright Notice in lua.h |
| 5 | 5 | */ |
| @@ -37,25 +37,100 @@ · 31 unmodified lines | ||
| 37 | 37 | #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 | +*/ | |
| 44 | +static int tofloat (const TValue *obj, lua_Number *n) { | |
| 45 | + if (ttisfloat(obj)) *n = fltvalue(obj); | |
| 46 | + else if (ttisinteger(obj)) { | |
| 47 | + volatile lua_Number x = cast_num(ivalue(obj)); /* avoid extra precision */ | |
| 48 | + *n = x; | |
| 49 | + } | |
| 50 | + else { | |
| 51 | + *n = 0; /* to avoid warnings */ | |
| 52 | + return 0; | |
| 53 | + } | |
| 54 | + return 1; | |
| 55 | +} | |
| 56 | + | |
| 57 | + | |
| 58 | +/* | |
| 59 | +** Try to convert a value to a float. Check 'isinteger' first, because | |
| 60 | +** in general the float case is already handled by the macro 'tonumber'. | |
| 61 | +*/ | |
| 40 | 62 | int luaV_tonumber_ (const TValue *obj, lua_Number *n) { |
| 41 | − lua_assert(!ttisfloat(obj)); | |
| 63 | + TValue v; | |
| 64 | + again: | |
| 42 | 65 | if (ttisinteger(obj)) { |
| 43 | 66 | *n = cast_num(ivalue(obj)); |
| 44 | 67 | return 1; |
| 45 | 68 | } |
| 46 | − else | |
| 47 | − return (ttisstring(obj) && luaO_str2d(svalue(obj), tsvalue(obj)->len, n)); | |
| 69 | + else if (ttisfloat(obj)) { | |
| 70 | + *n = fltvalue(obj); | |
| 71 | + return 1; | |
| 72 | + } | |
| 73 | + else if (ttisstring(obj) && /* string convertible to number? */ | |
| 74 | + luaO_str2num(svalue(obj), &v) == tsvalue(obj)->len + 1) { | |
| 75 | + obj = &v; | |
| 76 | + goto again; /* convert result from 'luaO_str2num' to a float */ | |
| 77 | + } | |
| 78 | + return 0; /* conversion failed */ | |
| 79 | +} | |
| 80 | + | |
| 81 | + | |
| 82 | +/* | |
| 83 | +** try to convert a value to an integer, rounding up if 'up' is true | |
| 84 | +*/ | |
| 85 | +static int tointeger_aux (const TValue *obj, lua_Integer *p, int up) { | |
| 86 | + TValue v; | |
| 87 | + again: | |
| 88 | + if (ttisfloat(obj)) { | |
| 89 | + lua_Number n = fltvalue(obj); | |
| 90 | + n = (up ? -l_floor(-n) : l_floor(n)); | |
| 91 | + return lua_numtointeger(n, p); | |
| 92 | + } | |
| 93 | + else if (ttisinteger(obj)) { | |
| 94 | + *p = ivalue(obj); | |
| 95 | + return 1; | |
| 96 | + } | |
| 97 | + else if (ttisstring(obj) && | |
| 98 | + luaO_str2num(svalue(obj), &v) == tsvalue(obj)->len + 1) { | |
| 99 | + obj = &v; | |
| 100 | + goto again; /* convert result from 'luaO_str2num' to an integer */ | |
| 101 | + } | |
| 102 | + return 0; /* conversion failed */ | |
| 48 | 103 | } |
| 49 | 104 | |
| 50 | 105 | |
| 106 | +/* | |
| 107 | +** try to convert a non-integer value to an integer, rounding down | |
| 108 | +*/ | |
| 109 | +int luaV_tointeger_ (const TValue *obj, lua_Integer *p) { | |
| 110 | + return tointeger_aux(obj, p, 0); | |
| 111 | +} | |
| 112 | + | |
| 113 | + | |
| 114 | +/* | |
| 115 | +** Convert a number object to a string | |
| 116 | +*/ | |
| 51 | 117 | int luaV_tostring (lua_State *L, StkId obj) { |
| 52 | 118 | if (!ttisnumber(obj)) |
| 53 | 119 | return 0; |
| 54 | 120 | else { |
| 55 | 121 | char buff[MAXNUMBER2STR]; |
| 56 | − size_t len = (ttisinteger(obj)) | |
| 57 | − ? lua_integer2str(buff, ivalue(obj)) | |
| 58 | − : lua_number2str(buff, fltvalue(obj)); | |
| 122 | + size_t len; | |
| 123 | + if (ttisinteger(obj)) | |
| 124 | + len = lua_integer2str(buff, ivalue(obj)); | |
| 125 | + else { | |
| 126 | + len = lua_number2str(buff, fltvalue(obj)); | |
| 127 | +#if !defined(LUA_COMPAT_FLOATSTRING) | |
| 128 | + if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */ | |
| 129 | + buff[len++] = '.'; | |
| 130 | + buff[len++] = '0'; /* adds '.0' to result */ | |
| 131 | + } | |
| 132 | +#endif | |
| 133 | + } | |
| 59 | 134 | setsvalue2s(L, obj, luaS_newlstr(L, buff, len)); |
| 60 | 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. | |
| 67 | −** (The comparisons are tricky because of rounding, which can or | |
| 68 | −** not occur depending on the relative sizes of floats and integers.) | |
| 69 | −** This function should be called only when 'n' has an integral value. | |
| 141 | +** Try to convert a 'for' limit to an integer, preserving the | |
| 142 | +** semantics of the loop. | |
| 143 | +** (The following explanation assumes a non-negative step; it is valid | |
| 144 | +** for negative steps mutatis mutandis.) | |
| 145 | +** If the limit can be converted to an integer, rounding down, that is | |
| 146 | +** it. | |
| 147 | +** Otherwise, check whether the limit can be converted to a number. If | |
| 148 | +** the number is too large, it is OK to set the limit as LUA_MAXINTEGER, | |
| 149 | +** which means no limit. If the number is too negative, the loop | |
| 150 | +** should not run, because any initial integer value is larger than the | |
| 151 | +** limit. So, it sets the limit to LUA_MININTEGER. 'stopnow' corrects | |
| 152 | +** the extreme case when the initial value is LUA_MININTEGER, in which | |
| 153 | +** case the LUA_MININTEGER limit would still run the loop once. | |
| 70 | 154 | */ |
| 71 | −int luaV_numtointeger (lua_Number n, lua_Integer *p) { | |
| 72 | − if (cast_num(MIN_INTEGER) <= n && n < (MAX_INTEGER + cast_num(1))) { | |
| 73 | − *p = cast_integer(n); | |
| 74 | − lua_assert(cast_num(*p) == n); | |
| 75 | − return 1; | |
| 155 | +static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step, | |
| 156 | + int *stopnow) { | |
| 157 | + *stopnow = 0; /* usually, let loops run */ | |
| 158 | + if (!tointeger_aux(obj, p, (step < 0))) { /* does not fit in integer? */ | |
| 159 | + lua_Number n; /* try to convert to float */ | |
| 160 | + if (!tonumber(obj, &n)) /* cannot convert to float? */ | |
| 161 | + return 0; /* not a number */ | |
| 162 | + if (n > 0) { /* if true, float is larger than max integer */ | |
| 163 | + *p = LUA_MAXINTEGER; | |
| 164 | + if (step < 0) *stopnow = 1; | |
| 165 | + } | |
| 166 | + else { /* float is smaller than min integer */ | |
| 167 | + *p = LUA_MININTEGER; | |
| 168 | + if (step >= 0) *stopnow = 1; | |
| 169 | + } | |
| 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 integer | |
| 176 | +** Main function for table access (invoking metamethods if needed). | |
| 177 | +** Compute 'val = t[key]' | |
| 83 | 178 | */ |
| 84 | −int luaV_tointeger_ (const TValue *obj, lua_Integer *p) { | |
| 85 | − lua_Number n; | |
| 86 | − lua_assert(!ttisinteger(obj)); | |
| 87 | − if (tonumber(obj, &n)) { | |
| 88 | − n = l_floor(n); | |
| 89 | − return luaV_numtointeger(n, p); | |
| 90 | − } | |
| 91 | − else return 0; | |
| 92 | −} | |
| 93 | − | |
| 94 | − | |
| 95 | 179 | void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) { |
| 96 | − int loop; | |
| 180 | + int loop; /* counter to avoid infinite loops */ | |
| 97 | 181 | for (loop = 0; loop < MAXTAGLOOP; loop++) { |
| 98 | 182 | const TValue *tm; |
| 99 | 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 | 185 | const TValue *res = luaH_get(h, key); /* do a primitive get */ |
| 102 | 186 | if (!ttisnil(res) || /* result is not nil? */ |
| 103 | 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 | 189 | return; |
| 106 | 190 | } |
| 107 | − /* else will try the tag method */ | |
| 191 | + /* else will try metamethod */ | |
| 108 | 192 | } |
| 109 | 193 | else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX))) |
| 110 | − luaG_typeerror(L, t, "index"); | |
| 111 | − if (ttisfunction(tm)) { | |
| 194 | + luaG_typeerror(L, t, "index"); /* no metamethod */ | |
| 195 | + if (ttisfunction(tm)) { /* metamethod is a function */ | |
| 112 | 196 | luaT_callTM(L, tm, t, key, val, 1); |
| 113 | 197 | return; |
| 114 | 198 | } |
| 115 | − t = tm; /* else repeat with 'tm' */ | |
| 199 | + t = tm; /* else repeat access over 'tm' */ | |
| 116 | 200 | } |
| 117 | 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 | +*/ | |
| 121 | 209 | void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) { |
| 122 | − int loop; | |
| 210 | + int loop; /* counter to avoid infinite loops */ | |
| 123 | 211 | for (loop = 0; loop < MAXTAGLOOP; loop++) { |
| 124 | 212 | const TValue *tm; |
| 125 | 213 | if (ttistable(t)) { /* `t' is a table? */ |
| 126 | 214 | Table *h = hvalue(t); |
| 127 | 215 | TValue *oldval = cast(TValue *, luaH_get(h, key)); |
| 128 | 216 | /* 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 | 218 | if (!ttisnil(oldval) || |
| 131 | 219 | /* previous value is nil; must check the metamethod */ |
| 132 | 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 | 234 | else /* not a table; check metamethod */ |
| 147 | 235 | if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX))) |
| 148 | 236 | luaG_typeerror(L, t, "index"); |
| 149 | − /* there is a metamethod */ | |
| 237 | + /* try the metamethod */ | |
| 150 | 238 | if (ttisfunction(tm)) { |
| 151 | 239 | luaT_callTM(L, tm, t, key, val, 0); |
| 152 | 240 | return; |
| 153 | 241 | } |
| 154 | − t = tm; /* else repeat with 'tm' */ | |
| 242 | + t = tm; /* else repeat assginment over 'tm' */ | |
| 155 | 243 | } |
| 156 | 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 | +*/ | |
| 160 | 255 | static int l_strcmp (const TString *ls, const TString *rs) { |
| 161 | 256 | const char *l = getstr(ls); |
| 162 | 257 | size_t ll = ls->tsv.len; |
| 163 | 258 | const char *r = getstr(rs); |
| 164 | 259 | size_t lr = rs->tsv.len; |
| 165 | − for (;;) { | |
| 260 | + for (;;) { /* for each segment */ | |
| 166 | 261 | int temp = strcoll(l, r); |
| 167 | − if (temp != 0) return temp; | |
| 168 | − else { /* strings are equal up to a `\0' */ | |
| 262 | + if (temp != 0) /* not equal? */ | |
| 263 | + return temp; /* done */ | |
| 264 | + else { /* strings are equal up to a '\0' */ | |
| 169 | 265 | size_t len = strlen(l); /* index of first `\0' in both strings */ |
| 170 | − if (len == lr) /* r is finished? */ | |
| 171 | − return (len == ll) ? 0 : 1; | |
| 172 | − else if (len == ll) /* l is finished? */ | |
| 173 | − return -1; /* l is smaller than r (because r is not finished) */ | |
| 174 | − /* both strings longer than `len'; go on comparing (after the `\0') */ | |
| 266 | + if (len == lr) /* 'rs' is finished? */ | |
| 267 | + return (len == ll) ? 0 : 1; /* check 'ls' */ | |
| 268 | + else if (len == ll) /* 'ls' is finished? */ | |
| 269 | + return -1; /* 'ls' is smaller than 'rs' ('rs' is not finished) */ | |
| 270 | + /* both strings longer than `len'; go on comparing after the '\0' */ | |
| 175 | 271 | len++; |
| 176 | 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 | +*/ | |
| 182 | 281 | int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { |
| 183 | 282 | int res; |
| 184 | 283 | lua_Number nl, nr; |
| 185 | − if (ttisinteger(l) && ttisinteger(r)) | |
| 284 | + if (ttisinteger(l) && ttisinteger(r)) /* both operands are integers? */ | |
| 186 | 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 | 287 | return luai_numlt(nl, nr); |
| 189 | − else if (ttisstring(l) && ttisstring(r)) | |
| 288 | + else if (ttisstring(l) && ttisstring(r)) /* both are strings? */ | |
| 190 | 289 | return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0; |
| 191 | − else if ((res = luaT_callorderTM(L, l, r, TM_LT)) < 0) | |
| 192 | − luaG_ordererror(L, l, r); | |
| 290 | + else if ((res = luaT_callorderTM(L, l, r, TM_LT)) < 0) /* no metamethod? */ | |
| 291 | + luaG_ordererror(L, l, r); /* error */ | |
| 193 | 292 | return res; |
| 194 | 293 | } |
| 195 | 294 | |
| 196 | 295 | |
| 296 | +/* | |
| 297 | +** Main operation less than or equal to; return 'l <= r'. | |
| 298 | +*/ | |
| 197 | 299 | int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) { |
| 198 | 300 | int res; |
| 199 | 301 | lua_Number nl, nr; |
| 200 | − if (ttisinteger(l) && ttisinteger(r)) | |
| 302 | + if (ttisinteger(l) && ttisinteger(r)) /* both operands are integers? */ | |
| 201 | 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 | 305 | return luai_numle(nl, nr); |
| 204 | − else if (ttisstring(l) && ttisstring(r)) | |
| 306 | + else if (ttisstring(l) && ttisstring(r)) /* both are strings? */ | |
| 205 | 307 | return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0; |
| 206 | 308 | else if ((res = luaT_callorderTM(L, l, r, TM_LE)) >= 0) /* first try `le' */ |
| 207 | 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 | */ |
| 217 | 320 | int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { |
| 218 | 321 | const TValue *tm; |
| 219 | − if (ttype(t1) != ttype(t2)) { | |
| 322 | + if (ttype(t1) != ttype(t2)) { /* not the same variant? */ | |
| 220 | 323 | if (ttnov(t1) != ttnov(t2) || ttnov(t1) != LUA_TNUMBER) |
| 221 | 324 | return 0; /* only numbers can be equal with different variants */ |
| 222 | 325 | else { /* two numbers with different variants */ |
| 223 | − lua_Number n1, n2; | |
| 326 | + lua_Number n1, n2; /* compare them as floats */ | |
| 224 | 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 | 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 | 342 | case LUA_TUSERDATA: { |
| 240 | 343 | if (uvalue(t1) == uvalue(t2)) return 1; |
| 241 | 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 | 348 | break; /* will try TM */ |
| 244 | 349 | } |
| 245 | 350 | case LUA_TTABLE: { |
| 246 | 351 | if (hvalue(t1) == hvalue(t2)) return 1; |
| 247 | 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 | 356 | break; /* will try TM */ |
| 250 | 357 | } |
| 251 | 358 | default: |
| 252 | 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 | 363 | luaT_callTM(L, tm, t1, t2, L->top, 1); /* call TM */ |
| 256 | 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 | +*/ | |
| 260 | 375 | void luaV_concat (lua_State *L, int total) { |
| 261 | 376 | lua_assert(total >= 2); |
| 262 | 377 | do { |
| @@ -284,12 +399,12 @@ void luaV_concat (lua_State *L, int total) { · 21 unmodified lines | ||
| 284 | 399 | buffer = luaZ_openspace(L, &G(L)->buff, tl); |
| 285 | 400 | tl = 0; |
| 286 | 401 | n = i; |
| 287 | − do { /* concat all strings */ | |
| 402 | + do { /* copy all strings to buffer */ | |
| 288 | 403 | size_t l = tsvalue(top-i)->len; |
| 289 | 404 | memcpy(buffer+tl, svalue(top-i), l * sizeof(char)); |
| 290 | 405 | tl += l; |
| 291 | 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 | 409 | total -= n-1; /* got 'n' strings to create 1 new */ |
| 295 | 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 | +*/ | |
| 300 | 418 | void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { |
| 301 | 419 | const TValue *tm; |
| 302 | 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 | |
| 325 | −lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y) { | |
| 326 | − if (cast_unsigned(y) + 1 <= 1U) { /* special cases: -1 or 0 */ | |
| 327 | − if (y == 0) | |
| 443 | +/* | |
| 444 | +** Integer division; return 'm // n'. (Assume that C division with | |
| 445 | +** negative operands follows C99 behavior.) | |
| 446 | +*/ | |
| 447 | +lua_Integer luaV_div (lua_State *L, lua_Integer m, lua_Integer n) { | |
| 448 | + if (l_castS2U(n) + 1u <= 1u) { /* special cases: -1 or 0 */ | |
| 449 | + if (n == 0) | |
| 328 | 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 | 453 | else { |
| 332 | − lua_Integer d = x / y; /* perform division */ | |
| 333 | − if ((x ^ y) >= 0 || x % y == 0) /* same signal or no rest? */ | |
| 454 | + lua_Integer d = m / n; /* perform division */ | |
| 455 | + if ((m ^ n) >= 0 || m % n == 0) /* same signal or no rest? */ | |
| 334 | 456 | return d; |
| 335 | 457 | else |
| 336 | 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 | |
| 341 | −lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y) { | |
| 342 | − if (cast_unsigned(y) + 1 <= 1U) { /* special cases: -1 or 0 */ | |
| 343 | − if (y == 0) | |
| 463 | +/* | |
| 464 | +** Integer modulus; return 'm % n'. (Assume that C '%' with | |
| 465 | +** negative operands follows C99 behavior.) | |
| 466 | +*/ | |
| 467 | +lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) { | |
| 468 | + if (l_castS2U(n) + 1u <= 1u) { /* special cases: -1 or 0 */ | |
| 469 | + if (n == 0) | |
| 344 | 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 | 473 | else { |
| 348 | − lua_Integer r = x % y; | |
| 349 | − if (r == 0 || (x ^ y) >= 0) | |
| 474 | + lua_Integer r = m % n; | |
| 475 | + if (r == 0 || (m ^ n) >= 0) /* no rest or same signal? */ | |
| 350 | 476 | return r; |
| 351 | 477 | else |
| 352 | − return r + y; /* correct 'mod' for negative case */ | |
| 353 | − } | |
| 354 | −} | |
| 355 | − | |
| 356 | − | |
| 357 | −lua_Integer luaV_pow (lua_State *L, lua_Integer x, lua_Integer y) { | |
| 358 | − if (y <= 0) { /* special cases: 0 or negative exponent */ | |
| 359 | − if (y < 0) | |
| 360 | − luaG_runerror(L, "integer exponentiation with negative exponent"); | |
| 361 | − return 1; /* x^0 == 1 */ | |
| 362 | − } | |
| 363 | − else { | |
| 364 | − lua_Integer r = 1; | |
| 365 | − for (; y > 1; y >>= 1) { | |
| 366 | − if (y & 1) r = intop(*, r, x); | |
| 367 | − x = intop(*, x, x); | |
| 368 | − } | |
| 369 | − r = intop(*, r, x); | |
| 370 | − return r; | |
| 478 | + return r + n; /* correct 'mod' for negative case */ | |
| 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 | 483 | /* number of bits in an integer */ |
| 376 | 484 | #define NBITS cast_int(sizeof(lua_Integer) * CHAR_BIT) |
| 377 | 485 | |
| 378 | −LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) { | |
| 486 | +/* | |
| 487 | +** Shift left operation. (Shift right just negates 'y'.) | |
| 488 | +*/ | |
| 489 | +lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) { | |
| 379 | 490 | if (y < 0) { /* shift right? */ |
| 380 | 491 | if (y <= -NBITS) return 0; |
| 381 | − else return cast_integer(cast_unsigned(x) >> (-y)); | |
| 492 | + else return intop(>>, x, -y); | |
| 382 | 493 | } |
| 383 | 494 | else { /* shift left */ |
| 384 | 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 | 503 | ** whether there is a cached closure with the same upvalues needed by |
| 393 | 504 | ** new closure to be created. |
| 394 | 505 | */ |
| 395 | −static Closure *getcached (Proto *p, UpVal **encup, StkId base) { | |
| 396 | − Closure *c = p->cache; | |
| 506 | +static LClosure *getcached (Proto *p, UpVal **encup, StkId base) { | |
| 507 | + LClosure *c = p->cache; | |
| 397 | 508 | if (c != NULL) { /* is there a cached closure? */ |
| 398 | 509 | int nup = p->sizeupvalues; |
| 399 | 510 | Upvaldesc *uv = p->upvalues; |
| 400 | 511 | int i; |
| 401 | 512 | for (i = 0; i < nup; i++) { /* check whether it has right upvalues */ |
| 402 | 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 | 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 | 530 | int nup = p->sizeupvalues; |
| 420 | 531 | Upvaldesc *uv = p->upvalues; |
| 421 | 532 | int i; |
| 422 | − Closure *ncl = luaF_newLclosure(L, nup); | |
| 423 | − ncl->l.p = p; | |
| 533 | + LClosure *ncl = luaF_newLclosure(L, nup); | |
| 534 | + ncl->p = p; | |
| 424 | 535 | setclLvalue(L, ra, ncl); /* anchor new closure in stack */ |
| 425 | 536 | for (i = 0; i < nup; i++) { /* fill in its upvalues */ |
| 426 | 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 | 539 | else /* get upvalue from enclosing function */ |
| 429 | − ncl->l.upvals[i] = encup[uv[i].idx]; | |
| 430 | − ncl->l.upvals[i]->refcount++; | |
| 540 | + ncl->upvals[i] = encup[uv[i].idx]; | |
| 541 | + ncl->upvals[i]->refcount++; | |
| 431 | 542 | /* new closure is white, so we do not need a barrier here */ |
| 432 | 543 | } |
| 433 | 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 | 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 | 752 | setivalue(ra, intop(+, ib, ic)); |
| 634 | 753 | } |
| 635 | 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 | 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 | 765 | setivalue(ra, intop(-, ib, ic)); |
| 647 | 766 | } |
| 648 | 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 | 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 | 778 | setivalue(ra, intop(*, ib, ic)); |
| 660 | 779 | } |
| 661 | 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 | 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 | 787 | TValue *rc = RKC(i); |
| 669 | 788 | lua_Number nb; lua_Number nc; |
| 670 | 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 | 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 | 854 | setivalue(ra, luaV_mod(L, ib, ic)); |
| 736 | 855 | } |
| 737 | 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 | 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 | 864 | TValue *rb = RKB(i); |
| 744 | 865 | TValue *rc = RKC(i); |
| 745 | 866 | lua_Number nb; lua_Number nc; |
| 746 | − if (ttisinteger(rb) && ttisinteger(rc)) { | |
| 747 | − lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); | |
| 748 | − setivalue(ra, luaV_pow(L, ib, ic)); | |
| 749 | − } | |
| 750 | − else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { | |
| 751 | − setnvalue(ra, luai_numpow(L, nb, nc)); | |
| 867 | + if (tonumber(rb, &nb) && tonumber(rc, &nc)) { | |
| 868 | + setfltvalue(ra, luai_numpow(L, nb, nc)); | |
| 752 | 869 | } |
| 753 | 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 | 877 | setivalue(ra, intop(-, 0, ib)); |
| 761 | 878 | } |
| 762 | 879 | else if (tonumber(rb, &nb)) { |
| 763 | − setnvalue(ra, luai_numunm(L, nb)); | |
| 880 | + setfltvalue(ra, luai_numunm(L, nb)); | |
| 764 | 881 | } |
| 765 | 882 | else { |
| 766 | 883 | Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM)); |
| @@ -770,7 +887,7 @@ void luaV_execute (lua_State *L) { · 3 unmodified lines | ||
| 770 | 887 | TValue *rb = RB(i); |
| 771 | 888 | lua_Integer ib; |
| 772 | 889 | if (tointeger(rb, &ib)) { |
| 773 | − setivalue(ra, intop(^, cast_integer(-1), ib)); | |
| 890 | + setivalue(ra, intop(^, ~l_castS2U(0), ib)); | |
| 774 | 891 | } |
| 775 | 892 | else { |
| 776 | 893 | Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT)); |
| @@ -916,8 +1033,8 @@ void luaV_execute (lua_State *L) { · 139 unmodified lines | ||
| 916 | 1033 | if (luai_numlt(0, step) ? luai_numle(idx, limit) |
| 917 | 1034 | : luai_numle(limit, idx)) { |
| 918 | 1035 | ci->u.l.savedpc += GETARG_sBx(i); /* jump back */ |
| 919 | − setnvalue(ra, idx); /* update internal index... */ | |
| 920 | − setnvalue(ra + 3, idx); /* ...and external index */ | |
| 1036 | + setfltvalue(ra, idx); /* update internal index... */ | |
| 1037 | + setfltvalue(ra + 3, idx); /* ...and external index */ | |
| 921 | 1038 | } |
| 922 | 1039 | } |
| 923 | 1040 | ) |
| @@ -925,21 +1042,26 @@ void luaV_execute (lua_State *L) { · 1 unmodified line | ||
| 925 | 1042 | TValue *init = ra; |
| 926 | 1043 | TValue *plimit = ra + 1; |
| 927 | 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 | 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 | 1054 | else { /* try making all values floats */ |
| 933 | 1055 | lua_Number ninit; lua_Number nlimit; lua_Number nstep; |
| 934 | 1056 | if (!tonumber(plimit, &nlimit)) |
| 935 | 1057 | luaG_runerror(L, LUA_QL("for") " limit must be a number"); |
| 936 | − setnvalue(plimit, nlimit); | |
| 1058 | + setfltvalue(plimit, nlimit); | |
| 937 | 1059 | if (!tonumber(pstep, &nstep)) |
| 938 | 1060 | luaG_runerror(L, LUA_QL("for") " step must be a number"); |
| 939 | − setnvalue(pstep, nstep); | |
| 1061 | + setfltvalue(pstep, nstep); | |
| 940 | 1062 | if (!tonumber(init, &ninit)) |
| 941 | 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 | 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 | 1110 | vmcase(OP_CLOSURE, |
| 989 | 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 | 1113 | if (ncl == NULL) /* no match? */ |
| 992 | 1114 | pushclosure(L, p, cl->upvals, base, ra); /* create a new one */ |
| 993 | 1115 | else |
| @@ -1020,3 +1142,5 @@ void luaV_execute (lua_State *L) { · 26 unmodified lines | ||
| 1020 | 1142 | } |
| 1021 | 1143 | } |
| 1022 | 1144 | |
| 1145 | +/* }================================================================== */ | |
| 1146 | + | |
| @@ -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 | 3 | ** Lua virtual machine |
| 4 | 4 | ** See Copyright Notice in lua.h |
| 5 | 5 | */ |
| @@ -13,16 +13,13 @@ · 7 unmodified lines | ||
| 13 | 13 | #include "ltm.h" |
| 14 | 14 | |
| 15 | 15 | |
| 16 | −#define tostring(L,o) (ttisstring(o) || (luaV_tostring(L, o))) | |
| 17 | − | |
| 18 | 16 | #define tonumber(o,n) \ |
| 19 | 17 | (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n)) |
| 20 | 18 | |
| 21 | 19 | #define tointeger(o,i) \ |
| 22 | 20 | (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger_(o,i)) |
| 23 | 21 | |
| 24 | −#define intop(op,v1,v2) \ | |
| 25 | − cast_integer(cast_unsigned(v1) op cast_unsigned(v2)) | |
| 22 | +#define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) | |
| 26 | 23 | |
| 27 | 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 | ||
| 32 | 29 | LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); |
| 33 | 30 | LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); |
| 34 | 31 | LUAI_FUNC int luaV_tointeger_ (const TValue *obj, lua_Integer *p); |
| 35 | −LUAI_FUNC int luaV_numtointeger (lua_Number n, lua_Integer *p); | |
| 36 | 32 | LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj); |
| 37 | 33 | LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, |
| 38 | 34 | StkId val); |
| @@ -43,7 +39,6 @@ LUAI_FUNC void luaV_execute (lua_State *L); · 4 unmodified lines | ||
| 43 | 39 | LUAI_FUNC void luaV_concat (lua_State *L, int total); |
| 44 | 40 | LUAI_FUNC lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y); |
| 45 | 41 | LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y); |
| 46 | −LUAI_FUNC lua_Integer luaV_pow (lua_State *L, lua_Integer x, lua_Integer y); | |
| 47 | 42 | LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y); |
| 48 | 43 | LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb); |
| 49 | 44 | |