History

Lua 5.3.0-alpha

Lua Team committed 12 years ago

Commit ff8c9ca1 parent f8a662741 files · +1212 −882Browse files

Unified · Split

41 files
  1. README +1 −1
  2. doc/contents.html +9 −11
  3. doc/lua.css +5 −2
  4. doc/manual.html +231 −185
  5. doc/readme.html +18 −16
  6. src/Makefile +1 −1
  7. src/lapi.c +84 −130
  8. src/lapi.h +3 −3
  9. src/lauxlib.c +19 −31
  10. src/lauxlib.h +17 −4
  11. src/lbaselib.c +64 −15
  12. src/ldebug.c +9 −8
  13. src/ldo.c +5 −4
  14. src/ldump.c +3 −3
  15. src/lgc.c +50 −52
  16. src/lgc.h +13 −13
  17. src/liolib.c +10 −10
  18. src/llex.c +4 −4
  19. src/llimits.h +15 −17
  20. src/lmathlib.c +38 −23
  21. src/lmem.c +4 −6
  22. src/loadlib.c +103 −46
  23. src/lobject.c +34 −9
  24. src/lobject.h +69 −38
  25. src/lparser.c +1 −1
  26. src/lstate.c +7 −5
  27. src/lstate.h +33 −33
  28. src/lstring.c +26 −26
  29. src/lstring.h +7 −5
  30. src/lstrlib.c +12 −9
  31. src/ltable.c +15 −14
  32. src/ltable.h +7 −2
  33. src/ltablib.c +137 −46
  34. src/ltm.c +1 −1
  35. src/lua.c +18 −12
  36. src/lua.h +54 −34
  37. src/luac.c +3 −3
  38. src/luaconf.h +36 −7
  39. src/lutf8lib.c +3 −1
  40. src/lvm.c +28 −49
  41. src/lvm.h +15 −2
README+1−1
@@ -1,5 +1,5 @@
11
2−This is Lua 5.3.0 (work3), released on 19 Jul 2014.
2+This is Lua 5.3.0 (alpha), released on 31 Jul 2014.
33
44 For installation instructions, license details, and
55 further information about Lua, see doc/readme.html.
doc/contents.html+9−11
@@ -22,7 +22,7 @@ Lua 5.3 Reference Manual · 21 unmodified lines
2222
2323 <P>
2424 <IMG SRC="alert.png" ALIGN="absbottom">
25−<EM>All details may change in the final version.</EM>
25+<EM>Some details may change in the final version.</EM>
2626
2727 <P>
2828 The reference manual is the official definition of the Lua language.
@@ -37,7 +37,7 @@ For a complete introduction to Lua programming, see the book · 8 unmodified lines
3737 <A HREF="#index">index</A>
3838 <HR>
3939 <SMALL>
40−Copyright &copy; 2011&ndash;2014 Lua.org, PUC-Rio.
40+Copyright &copy; 2014 Lua.org, PUC-Rio.
4141 Freely available under the terms of the
4242 <A HREF="http://www.lua.org/license.html">Lua license</A>.
4343 </SMALL>
@@ -229,7 +229,6 @@ Freely available under the terms of the · 185 unmodified lines
229229 <TD>
230230 <H3>&nbsp;</H3>
231231 <P>
232−<A HREF="manual.html#pdf-math">math</A><BR>
233232 <A HREF="manual.html#pdf-math.abs">math.abs</A><BR>
234233 <A HREF="manual.html#pdf-math.acos">math.acos</A><BR>
235234 <A HREF="manual.html#pdf-math.asin">math.asin</A><BR>
@@ -240,7 +239,6 @@ Freely available under the terms of the · 4 unmodified lines
240239 <A HREF="manual.html#pdf-math.floor">math.floor</A><BR>
241240 <A HREF="manual.html#pdf-math.fmod">math.fmod</A><BR>
242241 <A HREF="manual.html#pdf-math.huge">math.huge</A><BR>
243−<A HREF="manual.html#pdf-math.ifloor">math.ifloor</A><BR>
244242 <A HREF="manual.html#pdf-math.log">math.log</A><BR>
245243 <A HREF="manual.html#pdf-math.max">math.max</A><BR>
246244 <A HREF="manual.html#pdf-math.maxinteger">math.maxinteger</A><BR>
@@ -254,7 +252,9 @@ Freely available under the terms of the · 7 unmodified lines
254252 <A HREF="manual.html#pdf-math.sin">math.sin</A><BR>
255253 <A HREF="manual.html#pdf-math.sqrt">math.sqrt</A><BR>
256254 <A HREF="manual.html#pdf-math.tan">math.tan</A><BR>
255+<A HREF="manual.html#pdf-math.tointeger">math.tointeger</A><BR>
257256 <A HREF="manual.html#pdf-math.type">math.type</A><BR>
257+<A HREF="manual.html#pdf-math.ult">math.ult</A><BR>
258258
259259 <P>
260260 <A HREF="manual.html#pdf-os.clock">os.clock</A><BR>
@@ -301,6 +301,7 @@ Freely available under the terms of the · 40 unmodified lines
301301
302302 <P>
303303 <A HREF="manual.html#pdf-table.concat">table.concat</A><BR>
304+<A HREF="manual.html#pdf-table.copy">table.copy</A><BR>
304305 <A HREF="manual.html#pdf-table.insert">table.insert</A><BR>
305306 <A HREF="manual.html#pdf-table.pack">table.pack</A><BR>
306307 <A HREF="manual.html#pdf-table.remove">table.remove</A><BR>
@@ -321,6 +322,7 @@ Freely available under the terms of the · 14 unmodified lines
321322 <P>
322323 <A HREF="manual.html#lua_Alloc">lua_Alloc</A><BR>
323324 <A HREF="manual.html#lua_CFunction">lua_CFunction</A><BR>
325+<A HREF="manual.html#lua_Ctx">lua_Ctx</A><BR>
324326 <A HREF="manual.html#lua_Debug">lua_Debug</A><BR>
325327 <A HREF="manual.html#lua_Hook">lua_Hook</A><BR>
326328 <A HREF="manual.html#lua_Integer">lua_Integer</A><BR>
@@ -347,6 +349,7 @@ Freely available under the terms of the · 20 unmodified lines
347349 <A HREF="manual.html#lua_error">lua_error</A><BR>
348350 <A HREF="manual.html#lua_gc">lua_gc</A><BR>
349351 <A HREF="manual.html#lua_getallocf">lua_getallocf</A><BR>
352+<A HREF="manual.html#lua_getextraspace">lua_getextraspace</A><BR>
350353 <A HREF="manual.html#lua_getfield">lua_getfield</A><BR>
351354 <A HREF="manual.html#lua_getglobal">lua_getglobal</A><BR>
352355 <A HREF="manual.html#lua_gethook">lua_gethook</A><BR>
@@ -399,7 +402,6 @@ Freely available under the terms of the · 46 unmodified lines
399402 <A HREF="manual.html#lua_pushnumber">lua_pushnumber</A><BR>
400403 <A HREF="manual.html#lua_pushstring">lua_pushstring</A><BR>
401404 <A HREF="manual.html#lua_pushthread">lua_pushthread</A><BR>
402−<A HREF="manual.html#lua_pushunsigned">lua_pushunsigned</A><BR>
403405 <A HREF="manual.html#lua_pushvalue">lua_pushvalue</A><BR>
404406 <A HREF="manual.html#lua_pushvfstring">lua_pushvfstring</A><BR>
405407 <A HREF="manual.html#lua_rawequal">lua_rawequal</A><BR>
@@ -437,8 +439,6 @@ Freely available under the terms of the · 31 unmodified lines
437439 <A HREF="manual.html#lua_topointer">lua_topointer</A><BR>
438440 <A HREF="manual.html#lua_tostring">lua_tostring</A><BR>
439441 <A HREF="manual.html#lua_tothread">lua_tothread</A><BR>
440−<A HREF="manual.html#lua_tounsigned">lua_tounsigned</A><BR>
441−<A HREF="manual.html#lua_tounsignedx">lua_tounsignedx</A><BR>
442442 <A HREF="manual.html#lua_touserdata">lua_touserdata</A><BR>
443443 <A HREF="manual.html#lua_type">lua_type</A><BR>
444444 <A HREF="manual.html#lua_typename">lua_typename</A><BR>
@@ -480,7 +480,6 @@ Freely available under the terms of the · 35 unmodified lines
480480 <A HREF="manual.html#luaL_checkstring">luaL_checkstring</A><BR>
481481 <A HREF="manual.html#luaL_checktype">luaL_checktype</A><BR>
482482 <A HREF="manual.html#luaL_checkudata">luaL_checkudata</A><BR>
483−<A HREF="manual.html#luaL_checkunsigned">luaL_checkunsigned</A><BR>
484483 <A HREF="manual.html#luaL_checkversion">luaL_checkversion</A><BR>
485484 <A HREF="manual.html#luaL_dofile">luaL_dofile</A><BR>
486485 <A HREF="manual.html#luaL_dostring">luaL_dostring</A><BR>
@@ -508,7 +507,6 @@ Freely available under the terms of the · 21 unmodified lines
508507 <A HREF="manual.html#luaL_optlstring">luaL_optlstring</A><BR>
509508 <A HREF="manual.html#luaL_optnumber">luaL_optnumber</A><BR>
510509 <A HREF="manual.html#luaL_optstring">luaL_optstring</A><BR>
511−<A HREF="manual.html#luaL_optunsigned">luaL_optunsigned</A><BR>
512510 <A HREF="manual.html#luaL_prepbuffer">luaL_prepbuffer</A><BR>
513511 <A HREF="manual.html#luaL_prepbuffsize">luaL_prepbuffsize</A><BR>
514512 <A HREF="manual.html#luaL_pushresult">luaL_pushresult</A><BR>
@@ -531,10 +529,10 @@ Freely available under the terms of the · 16 unmodified lines
531529 <HR>
532530 <SMALL CLASS="footer">
533531 Last update:
534−Thu Jun 19 17:09:46 BRT 2014
532+Thu Jul 31 14:04:18 BRT 2014
535533 </SMALL>
536534 <!--
537−Last change: updated for Lua 5.3.0 (work3)
535+Last change: updated for Lua 5.3.0 (alpha)
538536 -->
539537
540538 </BODY>
doc/lua.css+5−2
@@ -59,6 +59,10 @@ a:link:active, a:visited:active { · 58 unmodified lines
5959 color: #FF0000 ;
6060 }
6161
62+h1 a img {
63+ vertical-align: text-bottom ;
64+}
65+
6266 hr {
6367 border: 0 ;
6468 height: 1px ;
@@ -88,9 +92,8 @@ input[type=text] { · 23 unmodified lines
8892 border-radius: 2em ;
8993 -moz-border-radius: 2em ;
9094 background-image: url('images/search.png') ;
91− background-repeat: no-repeat;
95+ background-repeat: no-repeat ;
9296 background-position: 4px center ;
9397 padding-left: 20px ;
9498 height: 2em ;
9599 }
96−
doc/manual.html+231−185
@@ -18,13 +18,13 @@ Lua 5.3 Reference Manual · 17 unmodified lines
1818
1919 <P>
2020 <IMG SRC="alert.png" ALIGN="absbottom">
21−<EM>All details may change in the final version.</EM>
21+<EM>Some details may change in the final version.</EM>
2222 <P>
2323
2424 by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes
2525 <p>
2626 <small>
27−Copyright &copy; 2011&ndash;2014 Lua.org, PUC-Rio.
27+Copyright &copy; 2014 Lua.org, PUC-Rio.
2828 Freely available under the terms of the
2929 <a href="http://www.lua.org/license.html">Lua license</a>.
3030 </small>
@@ -38,7 +38,7 @@ Freely available under the terms of the · 7 unmodified lines
3838 <!-- ====================================================================== -->
3939 <p>
4040
41−<!-- $Id: manual.of,v 1.121 2014/06/18 19:12:23 roberto Exp $ -->
41+<!-- $Id: manual.of,v 1.125 2014/07/31 13:58:08 roberto Exp $ -->
4242
4343
4444
@@ -125,7 +125,7 @@ it usually represents the absence of a useful value. · 80 unmodified lines
125125 Both <b>nil</b> and <b>false</b> make a condition false;
126126 any other value makes it true.
127127 <em>Number</em> represents both
128−integral numbers and real (floating-point) numbers.
128+integer numbers and real (floating-point) numbers.
129129 <em>String</em> represents immutable sequences of bytes.
130130
131131 Lua is 8-bit clean:
@@ -146,7 +146,7 @@ Standard Lua uses 64-bit integers and double-precision (64-bit) floats, · 14 unmodified lines
146146 but you can also compile Lua so that it
147147 uses 32-bit integers and/or single-precision (32-bit) floats.
148148 The option with 32 bits both for integers and floats
149−(what is called <em>Small Lua</em>) is particularly attractive
149+(called <em>Small Lua</em>) is particularly attractive
150150 for small machines.
151151
152152
@@ -159,7 +159,7 @@ functions written in C · 6 unmodified lines
159159 <p>
160160 The type <em>userdata</em> is provided to allow arbitrary C&nbsp;data to
161161 be stored in Lua variables.
162−A userdata value is a pointer to a block of raw memory.
162+A userdata value represents a block of raw memory.
163163 There are two kinds of userdata:
164164 full userdata, where the block of memory is managed by Lua,
165165 and light userdata, where the block of memory is managed by the host.
@@ -178,7 +178,7 @@ The type <em>thread</em> represents independent threads of execution · 12 unmodified lines
178178 and it is used to implement coroutines (see <a href="#2.6">&sect;2.6</a>).
179179 Do not confuse Lua threads with operating-system threads.
180180 Lua supports coroutines on all systems,
181−even those that do not support threads.
181+even those that do not support threads natively.
182182
183183
184184 <p>
@@ -324,7 +324,9 @@ is never updated by Lua. · 139 unmodified lines
324324 <p>
325325 Because Lua is an embedded extension language,
326326 all Lua actions start from C&nbsp;code in the host program
327−calling a function from the Lua library (see <a href="#lua_pcall"><code>lua_pcall</code></a>).
327+calling a function from the Lua library.
328+(when you use Lua standalone,
329+the <code>lua</code> application is the host program.)
328330 Whenever an error occurs during
329331 the compilation or execution of a Lua chunk,
330332 control returns to the host,
@@ -1891,9 +1893,9 @@ that rounds the quotient towards minus infinite (floor). · 1560 unmodified lines
18911893 In case of overflows in integer arithmetic,
18921894 all operations <em>wrap around</em>,
18931895 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>.)
1895−
1896−
1896+(In other words,
1897+they return the unique representable integer
1898+that is equal modulo <em>2<sup>64</sup></em> to the mathematical result.)
18971899
18981900
18991901
@@ -1921,7 +1923,7 @@ Both right and left shifts fill the vacant bits with zeros. · 21 unmodified lines
19211923 Negative displacements shift to the other direction;
19221924 displacements with absolute values equal to or higher than
19231925 the number of bits in an integer
1924−result in zero (all bits are shifted out).
1926+result in zero (as all bits are shifted out).
19251927
19261928
19271929
@@ -1952,16 +1954,17 @@ In a conversion from integer to float, · 24 unmodified lines
19521954 if the integer value has an exact representation as a float,
19531955 that is the result.
19541956 Otherwise,
1955−the conversion gets the nearest higher or lower representable value.
1957+the conversion gets the nearest higher or
1958+the nearest lower representable value.
19561959 This kind of conversion never fails.
19571960
19581961
19591962 <p>
19601963 The conversion from float to integer
1961−first takes the floor of the float number.
1962−If that value can be represented as an integer
1963−(that is, it is in the range of integer representation),
1964−that is the result.
1964+checks whether the float has an exact representation as an integer
1965+(that is, the float has an integral value and
1966+it is in the range of integer representation).
1967+If it does, that representation is the result.
19651968 Otherwise, the conversion fails.
19661969
19671970
@@ -2005,7 +2008,7 @@ Otherwise, the values of the operands are compared. · 37 unmodified lines
20052008 Strings are compared in the obvious way.
20062009 Numbers follow the usual rule for binary operations:
20072010 if both operands are integers,
2008−the are compared as integers;
2011+they are compared as integers;
20092012 otherwise, they are converted to floats
20102013 and compared as such.
20112014
@@ -2028,7 +2031,7 @@ by using the "eq" metamethod (see <a href="#2.4">&sect;2.4</a>). · 16 unmodified lines
20282031
20292032
20302033 <p>
2031−Equality comparisons never convert strings to numbers
2034+Equality comparisons do not convert strings to numbers
20322035 or vice versa.
20332036 Thus, <code>"0"==0</code> evaluates to <b>false</b>,
20342037 and <code>t[0]</code> and <code>t["0"]</code> denote different
@@ -2190,7 +2193,7 @@ with key <code>exp1</code> and value <code>exp2</code>. · 155 unmodified lines
21902193 A field of the form <code>name = exp</code> is equivalent to
21912194 <code>["name"] = exp</code>.
21922195 Finally, fields of the form <code>exp</code> are equivalent to
2193−<code>[i] = exp</code>, where <code>i</code> are consecutive numerical integers,
2196+<code>[i] = exp</code>, where <code>i</code> are consecutive integers
21942197 starting with 1.
21952198 Fields in the other formats do not affect this counting.
21962199 For example,
@@ -2861,7 +2864,7 @@ the Lua code being ran by <a href="#lua_pcall"><code>lua_pcall</code></a> to yie · 664 unmodified lines
28612864 First, we can rewrite our function like here:
28622865
28632866 <pre>
2864− int k (lua_State *L, int status, int ctx) {
2867+ int k (lua_State *L, int status, lua_Ctx ctx) {
28652868 ... /* code 2 */
28662869 }
28672870
@@ -2905,6 +2908,12 @@ the status is always <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> when Lu · 37 unmodified lines
29052908 (For these two functions,
29062909 Lua will not call the continuation in case of errors,
29072910 because they do not handle errors.)
2911+Similarly, when using <a href="#lua_callk"><code>lua_callk</code></a>,
2912+you should call the continuation function
2913+with <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> as the status.
2914+(For <a href="#lua_yieldk"><code>lua_yieldk</code></a>, there is not much point in calling
2915+directly the continuation function,
2916+because <a href="#lua_yieldk"><code>lua_yieldk</code></a> usually does not return.)
29082917
29092918
29102919 <p>
@@ -3159,7 +3168,7 @@ This is considered good programming practice. · 248 unmodified lines
31593168
31603169 <hr><h3><a name="lua_callk"><code>lua_callk</code></a></h3><p>
31613170 <span class="apii">[-(nargs + 1), +nresults, <em>e</em>]</span>
3162−<pre>void lua_callk (lua_State *L, int nargs, int nresults, int ctx,
3171+<pre>void lua_callk (lua_State *L, int nargs, int nresults, lua_Ctx ctx,
31633172 lua_KFunction k);</pre>
31643173
31653174 <p>
@@ -3307,7 +3316,7 @@ Concatenation is performed following the usual semantics of Lua · 141 unmodified lines
33073316 <pre>void lua_copy (lua_State *L, int fromidx, int toidx);</pre>
33083317
33093318 <p>
3310−Moves the element at index <code>fromidx</code>
3319+Copies the element at index <code>fromidx</code>
33113320 into the valid index <code>toidx</code>
33123321 without shifting any element
33133322 (therefore replacing the value at that position).
@@ -3335,6 +3344,21 @@ Otherwise you can use the function <a href="#lua_newtable"><code>lua_newtable</c · 21 unmodified lines
33353344
33363345
33373346
3347+<hr><h3><a name="lua_Ctx"><code>lua_Ctx</code></a></h3>
3348+<pre>typedef ... lua_Ctx;</pre>
3349+
3350+<p>
3351+The type for continuation-function contexts.
3352+It must be a numerical type.
3353+This type is defined as <code>intptr_t</code>
3354+when <code>intptr_t</code> is available,
3355+so that it can store pointers too.
3356+Otherwise, it is defined as <code>ptrdiff_t</code>.
3357+
3358+
3359+
3360+
3361+
33383362 <hr><h3><a name="lua_dump"><code>lua_dump</code></a></h3><p>
33393363 <span class="apii">[-0, +0, <em>e</em>]</span>
33403364 <pre>int lua_dump (lua_State *L,
@@ -3485,6 +3509,31 @@ Returns the type of the pushed value. · 144 unmodified lines
34853509
34863510
34873511
3512+<hr><h3><a name="lua_getextraspace"><code>lua_getextraspace</code></a></h3><p>
3513+<span class="apii">[-0, +0, &ndash;]</span>
3514+<pre>void *lua_getextraspace (lua_State *L);</pre>
3515+
3516+<p>
3517+Returns a pointer to a raw memory area associated with the
3518+given Lua state.
3519+The application can use this area for any purpose;
3520+Lua does not use it for anything.
3521+
3522+
3523+<p>
3524+Each new thread has this area initialized with a copy
3525+of the area of the main thread.
3526+
3527+
3528+<p>
3529+By default, this area has the size of a pointer to void,
3530+but you can recompile Lua with a different size for this area.
3531+(See <code>LUA_EXTRASPACE</code> in <code>luaconf.h</code>.)
3532+
3533+
3534+
3535+
3536+
34883537 <hr><h3><a name="lua_getglobal"><code>lua_getglobal</code></a></h3><p>
34893538 <span class="apii">[-0, +1, <em>e</em>]</span>
34903539 <pre>int lua_getglobal (lua_State *L, const char *name);</pre>
@@ -3502,8 +3551,9 @@ Returns the type of that value. · 11 unmodified lines
35023551 <pre>int lua_getmetatable (lua_State *L, int index);</pre>
35033552
35043553 <p>
3505−Pushes onto the stack the metatable of the value at the given index.
3506−If the value does not have a metatable,
3554+If the value at the given index has a metatable,
3555+the function pushes that metatable onto the stack and returns&nbsp;1.
3556+Otherwise,
35073557 the function returns&nbsp;0 and pushes nothing on the stack.
35083558
35093559
@@ -3588,9 +3638,9 @@ The type of integers in Lua. · 78 unmodified lines
35883638 <p>
35893639 By default this type is <code>long long</code>
35903640 (usually a 64-bit two-complement integer),
3591−but that can be changed in <code>luaconf.h</code>
3592−to <code>long</code> or <code>int</code>
3593−(usually a 32-bit two-complement integer).
3641+but that can be changed to <code>long</code> or <code>int</code>,
3642+usually a 32-bit two-complement integer.
3643+(See <code>LUA_INT</code> in <code>luaconf.h</code>.)
35943644
35953645
35963646 <p>
@@ -3775,7 +3825,7 @@ and 0&nbsp;otherwise. · 178 unmodified lines
37753825
37763826
37773827 <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>
3828+<pre>typedef int (*lua_KFunction) (lua_State *L, int status, lua_Ctx ctx);</pre>
37793829
37803830 <p>
37813831 Type for continuation functions (see <a href="#4.7">&sect;4.7</a>).
@@ -3988,7 +4038,8 @@ The type of floats in Lua. · 206 unmodified lines
39884038
39894039 <p>
39904040 By default this type is double,
3991−but that can be changed in <code>luaconf.h</code> to a single float.
4041+but that can be changed to a single float.
4042+(See <code>LUA_REAL</code> in <code>luaconf.h</code>.)
39924043
39934044
39944045
@@ -4096,7 +4147,7 @@ error while running a <code>__gc</code> metamethod. · 101 unmodified lines
40964147 int nargs,
40974148 int nresults,
40984149 int errfunc,
4099− int ctx,
4150+ lua_Ctx ctx,
41004151 lua_KFunction k);</pre>
41014152
41024153 <p>
@@ -4366,17 +4417,6 @@ Returns 1 if this thread is the main thread of its state. · 263 unmodified lines
43664417
43674418
43684419
4369−<hr><h3><a name="lua_pushunsigned"><code>lua_pushunsigned</code></a></h3><p>
4370−<span class="apii">[-0, +1, &ndash;]</span>
4371−<pre>void lua_pushunsigned (lua_State *L, lua_Unsigned n);</pre>
4372−
4373−<p>
4374−Pushes an integer with value <code>n</code> onto the stack.
4375−
4376−
4377−
4378−
4379−
43804420 <hr><h3><a name="lua_pushvalue"><code>lua_pushvalue</code></a></h3><p>
43814421 <span class="apii">[-0, +1, &ndash;]</span>
43824422 <pre>void lua_pushvalue (lua_State *L, int index);</pre>
@@ -4916,9 +4956,12 @@ to a string inside the Lua state. · 533 unmodified lines
49164956 This string always has a zero ('<code>\0</code>')
49174957 after its last character (as in&nbsp;C),
49184958 but can contain other zeros in its body.
4959+
4960+
4961+<p>
49194962 Because Lua has garbage collection,
49204963 there is no guarantee that the pointer returned by <code>lua_tolstring</code>
4921−will be valid after the corresponding value is removed from the stack.
4964+will be valid after the corresponding Lua value is removed from the stack.
49224965
49234966
49244967
@@ -5001,48 +5044,6 @@ otherwise, the function returns <code>NULL</code>. · 76 unmodified lines
50015044
50025045
50035046
5004−<hr><h3><a name="lua_tounsigned"><code>lua_tounsigned</code></a></h3><p>
5005−<span class="apii">[-0, +0, &ndash;]</span>
5006−<pre>lua_Unsigned lua_tounsigned (lua_State *L, int index);</pre>
5007−
5008−<p>
5009−Equivalent to <a href="#lua_tounsignedx"><code>lua_tounsignedx</code></a> with <code>isnum</code> equal to <code>NULL</code>.
5010−
5011−
5012−
5013−
5014−
5015−<hr><h3><a name="lua_tounsignedx"><code>lua_tounsignedx</code></a></h3><p>
5016−<span class="apii">[-0, +0, &ndash;]</span>
5017−<pre>lua_Unsigned lua_tounsignedx (lua_State *L, int index, int *isnum);</pre>
5018−
5019−<p>
5020−Converts the Lua value at the given index
5021−to the unsigned integral type <a href="#lua_Unsigned"><code>lua_Unsigned</code></a>.
5022−The Lua value must be an integer,
5023−or a float,
5024−or a string convertible to a number
5025−(see <a href="#3.4.3">&sect;3.4.3</a>);
5026−otherwise, <code>lua_tounsignedx</code> returns&nbsp;0.
5027−
5028−
5029−<p>
5030−If the number is not an integer,
5031−it is rounded towards minus infinite (floor).
5032−If the result is outside the range of representable values,
5033−it is normalized to the module of its division by
5034−one more than the maximum representable value.
5035−
5036−
5037−<p>
5038−If <code>isnum</code> is not <code>NULL</code>,
5039−its referent is assigned a boolean value that
5040−indicates whether the operation succeeded.
5041−
5042−
5043−
5044−
5045−
50465047 <hr><h3><a name="lua_touserdata"><code>lua_touserdata</code></a></h3><p>
50475048 <span class="apii">[-0, +0, &ndash;]</span>
50485049 <pre>void *lua_touserdata (lua_State *L, int index);</pre>
@@ -5101,11 +5102,6 @@ which must be one the values returned by <a href="#lua_type"><code>lua_type</cod · 52 unmodified lines
51015102 The unsigned version of <a href="#lua_Integer"><code>lua_Integer</code></a>.
51025103
51035104
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−
51095105
51105106
51115107
@@ -5178,14 +5174,14 @@ and pushes them onto the stack <code>to</code>. · 66 unmodified lines
51785174
51795175
51805176 <hr><h3><a name="lua_yield"><code>lua_yield</code></a></h3><p>
5181−<span class="apii">[-?, +?, &ndash;]</span>
5177+<span class="apii">[-?, +?, <em>e</em>]</span>
51825178 <pre>int lua_yield (lua_State *L, int nresults);</pre>
51835179
51845180 <p>
51855181 This function is equivalent to <a href="#lua_yieldk"><code>lua_yieldk</code></a>,
51865182 but it has no continuation (see <a href="#4.7">&sect;4.7</a>).
51875183 Therefore, when the thread resumes,
5188−it returns to the function that called
5184+it continues the function that called
51895185 the function calling <code>lua_yield</code>.
51905186
51915187
@@ -5193,21 +5189,15 @@ the function calling <code>lua_yield</code>. · 1 unmodified line
51935189
51945190
51955191 <hr><h3><a name="lua_yieldk"><code>lua_yieldk</code></a></h3><p>
5196−<span class="apii">[-?, +?, &ndash;]</span>
5197−<pre>int lua_yieldk (lua_State *L, int nresults, int ctx, lua_KFunction k);</pre>
5192+<span class="apii">[-?, +?, <em>e</em>]</span>
5193+<pre>int lua_yieldk (lua_State *L, int nresults, lua_Ctx ctx, lua_KFunction k);</pre>
51985194
51995195 <p>
5200−Yields a coroutine.
5196+Yields a coroutine (thread).
52015197
52025198
52035199 <p>
5204−This function should only be called as the
5205−return expression of a C&nbsp;function, as follows:
5206−
5207−<pre>
5208− return lua_yieldk (L, n, ctx, k);
5209−</pre><p>
5210−When a C&nbsp;function calls <a href="#lua_yieldk"><code>lua_yieldk</code></a> in that way,
5200+When a C&nbsp;function calls <a href="#lua_yieldk"><code>lua_yieldk</code></a>,
52115201 the running coroutine suspends its execution,
52125202 and the call to <a href="#lua_resume"><code>lua_resume</code></a> that started this coroutine returns.
52135203 The parameter <code>nresults</code> is the number of values from the stack
@@ -5227,6 +5217,29 @@ the continuation function receives the value <code>ctx</code> · 13 unmodified lines
52275217 that was passed to <a href="#lua_yieldk"><code>lua_yieldk</code></a>.
52285218
52295219
5220+<p>
5221+Usually, this function does not return;
5222+when the coroutine eventually resumes,
5223+it continues executing the continuation function.
5224+However, there is one special case,
5225+which is when this function is called
5226+from inside a line hook (see <a href="#4.9">&sect;4.9</a>).
5227+In that case, <code>lua_yieldk</code> should be called with no continuation
5228+(probably in the form of <a href="#lua_yield"><code>lua_yield</code></a>),
5229+and the hook should return immediately after the call.
5230+Lua will yield and,
5231+when the coroutine resumes again,
5232+it will continue the normal execution
5233+of the (Lua) function that triggered the hook.
5234+
5235+
5236+<p>
5237+This function can raise an error if it is called from a thread
5238+with a pending C call with no continuation function,
5239+or it is called from a thread that is not running inside a resume
5240+(e.g., the main thread).
5241+
5242+
52305243
52315244
52325245
@@ -6196,18 +6209,6 @@ returns the userdata address (see <a href="#lua_touserdata"><code>lua_touserdata · 963 unmodified lines
61966209
61976210
61986211
6199−<hr><h3><a name="luaL_checkunsigned"><code>luaL_checkunsigned</code></a></h3><p>
6200−<span class="apii">[-0, +0, <em>v</em>]</span>
6201−<pre>lua_Unsigned luaL_checkunsigned (lua_State *L, int arg);</pre>
6202−
6203−<p>
6204−Checks whether the function argument <code>arg</code> is a number
6205−and returns this number converted to a <a href="#lua_Unsigned"><code>lua_Unsigned</code></a>.
6206−
6207−
6208−
6209−
6210−
62116212 <hr><h3><a name="luaL_checkversion"><code>luaL_checkversion</code></a></h3><p>
62126213 <span class="apii">[-0, +0, &ndash;]</span>
62136214 <pre>void luaL_checkversion (lua_State *L);</pre>
@@ -6682,23 +6683,6 @@ Otherwise, raises an error. · 468 unmodified lines
66826683
66836684
66846685
6685−<hr><h3><a name="luaL_optunsigned"><code>luaL_optunsigned</code></a></h3><p>
6686−<span class="apii">[-0, +0, <em>v</em>]</span>
6687−<pre>lua_Unsigned luaL_optunsigned (lua_State *L,
6688− int arg,
6689− lua_Unsigned u);</pre>
6690−
6691−<p>
6692−If the function argument <code>arg</code> is a number,
6693−returns this number converted to a <a href="#lua_Unsigned"><code>lua_Unsigned</code></a>.
6694−If this argument is absent or is <b>nil</b>,
6695−returns <code>u</code>.
6696−Otherwise, raises an error.
6697−
6698−
6699−
6700−
6701−
67026686 <hr><h3><a name="luaL_prepbuffer"><code>luaL_prepbuffer</code></a></h3><p>
67036687 <span class="apii">[-?, +?, <em>e</em>]</span>
67046688 <pre>char *luaL_prepbuffer (luaL_Buffer *B);</pre>
@@ -6803,18 +6787,19 @@ in which both <code>name</code> and <code>func</code> are <code>NULL</code>. · 98 unmodified lines
68036787 lua_CFunction openf, int glb);</pre>
68046788
68056789 <p>
6806−Calls function <code>openf</code> with string <code>modname</code> as an argument
6790+If <code>modname</code> is not already present in <a href="#pdf-package.loaded"><code>package.loaded</code></a>,
6791+calls function <code>openf</code> with string <code>modname</code> as an argument
68076792 and sets the call result in <code>package.loaded[modname]</code>,
68086793 as if that function has been called through <a href="#pdf-require"><code>require</code></a>.
68096794
68106795
68116796 <p>
68126797 If <code>glb</code> is true,
6813−also stores the result into global <code>modname</code>.
6798+also stores the module into global <code>modname</code>.
68146799
68156800
68166801 <p>
6817−Leaves a copy of that result on the stack.
6802+Leaves a copy of the module on the stack.
68186803
68196804
68206805
@@ -7218,21 +7203,19 @@ Otherwise, returns the metatable of the given object. · 397 unmodified lines
72187203
72197204
72207205 <p>
7221−If <code>t</code> has a metamethod <code>__ipairs</code>,
7222−calls it with <code>t</code> as argument and returns the first three
7223−results from the call.
7224−
7225−
7226−<p>
7227−Otherwise,
7228−returns three values: an iterator function, the table <code>t</code>, and 0,
7206+Returns three values (an iterator function, the table <code>t</code>, and 0)
72297207 so that the construction
72307208
72317209 <pre>
72327210 for i,v in ipairs(t) do <em>body</em> end
72337211 </pre><p>
7234−will iterate over the pairs (<code>1,t[1]</code>), (<code>2,t[2]</code>), ...,
7235−up to the first integer key absent from the table.
7212+will iterate over the pairs
7213+(<code>1,t[1]</code>), (<code>2,t[2]</code>), ..., (<code>#t,t[#t]</code>).
7214+
7215+
7216+<p>
7217+The table should be a proper sequence
7218+or have a <code>__len</code> metamethod (see <a href="#3.4.7">&sect;3.4.7</a>).
72367219
72377220
72387221
@@ -7973,9 +7956,9 @@ The name of this C&nbsp;function is the string "<code>luaopen_</code>" · 734 unmodified lines
79737956 concatenated with a copy of the module name where each dot
79747957 is replaced by an underscore.
79757958 Moreover, if the module name has a hyphen,
7976−its prefix up to (and including) the first hyphen is removed.
7977−For instance, if the module name is <code>a.v1-b.c</code>,
7978−the function name will be <code>luaopen_b_c</code>.
7959+its sufix after (and including) the first hyphen is removed.
7960+For instance, if the module name is <code>a.b.c-v2.1</code>,
7961+the function name will be <code>luaopen_a_b_c</code>.
79797962
79807963
79817964 <p>
@@ -8138,6 +8121,8 @@ This function may not work correctly in architectures · 156 unmodified lines
81388121 with mixed endian.
81398122
81408123
8124+
8125+
81418126 <p>
81428127 <hr><h3><a name="pdf-string.dumpint"><code>string.dumpint (n [, size [, endianness]])</code></a></h3>
81438128 Returns a string with the two-complement representation of integer <code>n</code>,
@@ -8165,7 +8150,7 @@ that do not use a two-complement representation for integers. · 21 unmodified lines
81658150
81668151 <p>
81678152 Looks for the first match of
8168−<code>pattern</code> in the string <code>s</code>.
8153+<code>pattern</code> (see <a href="#6.4.1">&sect;6.4.1</a>) in the string <code>s</code>.
81698154 If it finds a match, then <code>find</code> returns the indices of&nbsp;<code>s</code>
81708155 where this occurrence starts and ends;
81718156 otherwise, it returns <b>nil</b>.
@@ -8235,7 +8220,8 @@ it is converted to one following the same rules of <a href="#pdf-tostring"><code · 63 unmodified lines
82358220 <hr><h3><a name="pdf-string.gmatch"><code>string.gmatch (s, pattern)</code></a></h3>
82368221 Returns an iterator function that,
82378222 each time it is called,
8238−returns the next captures from <code>pattern</code> over the string <code>s</code>.
8223+returns the next captures from <code>pattern</code> (see <a href="#6.4.1">&sect;6.4.1</a>)
8224+over the string <code>s</code>.
82398225 If <code>pattern</code> specifies no captures,
82408226 then the whole match is produced in each call.
82418227
@@ -8273,7 +8259,7 @@ work as an anchor, as this would prevent the iteration. · 31 unmodified lines
82738259 <hr><h3><a name="pdf-string.gsub"><code>string.gsub (s, pattern, repl [, n])</code></a></h3>
82748260 Returns a copy of <code>s</code>
82758261 in which all (or the first <code>n</code>, if given)
8276−occurrences of the <code>pattern</code> have been
8262+occurrences of the <code>pattern</code> (see <a href="#6.4.1">&sect;6.4.1</a>) have been
82778263 replaced by a replacement string specified by <code>repl</code>,
82788264 which can be a string, a table, or a function.
82798265 <code>gsub</code> also returns, as its second value,
@@ -8368,7 +8354,7 @@ The definition of what an uppercase letter is depends on the current locale. · 88 unmodified lines
83688354 <p>
83698355 <hr><h3><a name="pdf-string.match"><code>string.match (s, pattern [, init])</code></a></h3>
83708356 Looks for the first <em>match</em> of
8371−<code>pattern</code> in the string <code>s</code>.
8357+<code>pattern</code> (see <a href="#6.4.1">&sect;6.4.1</a>) in the string <code>s</code>.
83728358 If it finds one, then <code>match</code> returns
83738359 the captures from the pattern;
83748360 otherwise it returns <b>nil</b>.
@@ -8456,8 +8442,21 @@ The definition of what a lowercase letter is depends on the current locale. · 81 unmodified lines
84568442
84578443
84588444
8445+
8446+
84598447 <h3>6.4.1 &ndash; <a name="6.4.1">Patterns</a></h3>
84608448
8449+<p>
8450+Patterns in Lua are described by regular strings,
8451+which are interpreted as patterns by the pattern-matching functions
8452+<a href="#pdf-string.find"><code>string.find</code></a>,
8453+<a href="#pdf-string.gmatch"><code>string.gmatch</code></a>,
8454+<a href="#pdf-string.gsub"><code>string.gsub</code></a>,
8455+and <a href="#pdf-string.match"><code>string.match</code></a>.
8456+This section describes the syntax and the meaning
8457+(that is, what they match) of these strings.
8458+
8459+
84618460
84628461 <h4>Character Class:</h4><p>
84638462 A <em>character class</em> is used to represent a set of characters.
@@ -8649,8 +8648,6 @@ string <code>"flaaap"</code>, there will be two captures: 3&nbsp;and&nbsp;5. · 185 unmodified lines
86498648
86508649
86518650
8652−
8653−
86548651 <h2>6.5 &ndash; <a name="6.5">UTF-8 Support</a></h2>
86558652
86568653 <p>
@@ -8774,11 +8771,6 @@ All functions ignore non-numeric keys · 117 unmodified lines
87748771 in tables given as arguments.
87758772
87768773
8777−<p>
8778−For performance reasons,
8779−all table accesses (get/set) performed by these functions are raw.
8780−
8781−
87828774 <p>
87838775 <hr><h3><a name="pdf-table.concat"><code>table.concat (list [, sep [, i [, j]]])</code></a></h3>
87848776
@@ -8794,6 +8786,22 @@ If <code>i</code> is greater than <code>j</code>, returns the empty string. · 9 unmodified lines
87948786
87958787
87968788
8789+<p>
8790+<hr><h3><a name="pdf-table.copy"><code>table.copy (a1, f, e, [a2,] t)</code></a></h3>
8791+
8792+
8793+<p>
8794+Copies elements from table <code>a1</code> to table <code>a2</code>.
8795+This function performs the equivalent to the following
8796+multiple assignment:
8797+<code>a2[t],&middot;&middot;&middot; = a1[f],&middot;&middot;&middot;,a1[e]</code>.
8798+The default for <code>a2</code> is <code>a1</code>.
8799+The destination range can overlap with the source range.
8800+Index <code>f</code> must be positive.
8801+
8802+
8803+
8804+
87978805 <p>
87988806 <hr><h3><a name="pdf-table.insert"><code>table.insert (list, [pos,] value)</code></a></h3>
87998807
@@ -9014,18 +9022,6 @@ a value larger than any other numerical value. · 214 unmodified lines
90149022
90159023
90169024
9017−<p>
9018−<hr><h3><a name="pdf-math.ifloor"><code>math.ifloor (x)</code></a></h3>
9019−
9020−
9021−<p>
9022−Returns the largest integer smaller than or equal to <code>x</code>.
9023−If the value does not fit in an integer,
9024−returns <b>nil</b>.
9025−
9026−
9027−
9028−
90299025 <p>
90309026 <hr><h3><a name="pdf-math.log"><code>math.log (x [, base])</code></a></h3>
90319027
@@ -9171,6 +9167,18 @@ Returns the tangent of <code>x</code> (assumed to be in radians). · 139 unmodified lines
91719167
91729168
91739169
9170+<p>
9171+<hr><h3><a name="pdf-math.tointeger"><code>math.tointeger (x)</code></a></h3>
9172+
9173+
9174+<p>
9175+If the value <code>x</code> is convertible to an integer,
9176+returns that integer.
9177+Otherwise, returns <b>nil</b>.
9178+
9179+
9180+
9181+
91749182 <p>
91759183 <hr><h3><a name="pdf-math.type"><code>math.type (x)</code></a></h3>
91769184
@@ -9183,6 +9191,18 @@ or <b>nil</b> if <code>x</code> is not a number. · 6 unmodified lines
91839191
91849192
91859193
9194+<p>
9195+<hr><h3><a name="pdf-math.ult"><code>math.ult (m, n)</code></a></h3>
9196+
9197+
9198+<p>
9199+Returns a boolean,
9200+true if integer <code>m</code> is below integer <code>n</code> when
9201+they are compared as unsigned integers.
9202+
9203+
9204+
9205+
91869206
91879207
91889208
@@ -10272,8 +10292,7 @@ The options are: · 1083 unmodified lines
1027210292 <li><b><code>--</code>: </b> stops handling options;</li>
1027310293 <li><b><code>-</code>: </b> executes <code>stdin</code> as a file and stops handling options.</li>
1027410294 </ul><p>
10275−After handling its options, <code>lua</code> runs the given <em>script</em>,
10276−passing to it the given <em>args</em> as string arguments.
10295+After handling its options, <code>lua</code> runs the given <em>script</em>.
1027710296 When called without arguments,
1027810297 <code>lua</code> behaves as <code>lua -v -i</code>
1027910298 when the standard input (<code>stdin</code>) is a terminal,
@@ -10283,7 +10302,7 @@ and as <code>lua -</code> otherwise. · 3 unmodified lines
1028310302 <p>
1028410303 When called without option <code>-E</code>,
1028510304 the interpreter checks for an environment variable <a name="pdf-LUA_INIT_5_3"><code>LUA_INIT_5_3</code></a>
10286−(or <a name="pdf-LUA_INIT"><code>LUA_INIT</code></a> if it is not defined)
10305+(or <a name="pdf-LUA_INIT"><code>LUA_INIT</code></a> if the versioned name is not defined)
1028710306 before running any argument.
1028810307 If the variable content has the format <code>@<em>filename</em></code>,
1028910308 then <code>lua</code> executes the file.
@@ -10343,6 +10362,11 @@ For instance, the call · 53 unmodified lines
1034310362 $ lua -e "print(arg[1])"
1034410363 </pre><p>
1034510364 will print "<code>-e</code>".
10365+If there is a script,
10366+the script is called with parameters
10367+<code>arg[1]</code>, &middot;&middot;&middot;, <code>arg[#arg]</code>.
10368+(Like all chunks in Lua,
10369+the script is compiled as a vararg function.)
1034610370
1034710371
1034810372 <p>
@@ -10417,7 +10441,7 @@ do not imply source-code changes in a program, · 68 unmodified lines
1041710441 such as the numeric values for constants
1041810442 or the implementation of functions as macros.
1041910443 Therefore,
10420−you should never assume that binaries are compatible between
10444+you should not assume that binaries are compatible between
1042110445 different Lua versions.
1042210446 Always recompile clients of the Lua API when
1042310447 using a new version.
@@ -10426,7 +10450,7 @@ using a new version. · 2 unmodified lines
1042610450 <p>
1042710451 Similarly, Lua versions can always change the internal representation
1042810452 of precompiled chunks;
10429−precompiled chunks are never compatible between different Lua versions.
10453+precompiled chunks are not compatible between different Lua versions.
1043010454
1043110455
1043210456 <p>
@@ -10458,12 +10482,6 @@ it is not a general guideline for good programming. · 25 unmodified lines
1045810482 For good programming,
1045910483 use floats where you need floats
1046010484 and integers where you need integers.)
10461−
10462−
10463−<p>
10464−Although not formally an incompatibility,
10465−the proper differentiation between floats and integers
10466−have an impact on performance.
1046710485 </li>
1046810486
1046910487 <li>
@@ -10502,6 +10520,16 @@ better yet, to replace its functions with appropriate bitwise operations. · 32 unmodified lines
1050210520 while the bitwise operators in Standard Lua operate on 64-bit integers.)
1050310521 </li>
1050410522
10523+<li>
10524+The Table library now respects metamethods
10525+for setting and getting elements.
10526+</li>
10527+
10528+<li>
10529+The <a href="#pdf-ipairs"><code>ipairs</code></a> iterator now respects metamethods and
10530+its id{__ipairs} metamethod has been deprecated.
10531+</li>
10532+
1050510533 <li>
1050610534 Option names in <a href="#pdf-io.read"><code>io.read</code></a> do not have a starting '<code>*</code>' anymore.
1050710535 For compatibility, Lua will continue to ignore this character.
@@ -10513,12 +10541,23 @@ The following functions were deprecated in the mathematical library: · 5 unmodified lines
1051310541 <code>frexp</code>, and <code>ldexp</code>.
1051410542 You can replace <code>math.pow(x,y)</code> with <code>x^y</code>;
1051510543 you can replace <code>math.atan2</code> with <code>math.atan</code>,
10516−which now accepts two parameters;
10544+which now accepts one or two parameters;
1051710545 you can replace <code>math.ldexp(x,exp)</code> with <code>x * 2.0^exp</code>.
1051810546 For the other operations,
1051910547 the best choice is to use an external library.
1052010548 </li>
1052110549
10550+<li>
10551+The searcher for C loaders used by <a href="#pdf-require"><code>require</code></a>
10552+changed the way it handles versioned names.
10553+Now, the version should come after the module name
10554+(as is usual in most other tools).
10555+For compatibility, that searcher still tries the old format
10556+if if cannot find an open function according to the new style.
10557+(Lua&nbsp;5.2 already worked that way,
10558+but it did not document the change.)
10559+</li>
10560+
1052210561 </ul>
1052310562
1052410563
@@ -10541,6 +10580,13 @@ Function <a href="#lua_dump"><code>lua_dump</code></a> has an extra parameter, < · 16 unmodified lines
1054110580 Use 0 as the value of this parameter to get the old behavior.
1054210581 </li>
1054310582
10583+<li>
10584+Functions to inject/project unsigned integers
10585+(<code>lua_pushunsigned</code>, <code>lua_tounsigned</code>, etc.)
10586+were deprecated.
10587+Use their signed equivalents with a type cast.
10588+</li>
10589+
1054410590 </ul>
1054510591
1054610592
@@ -10550,7 +10596,7 @@ Use 0 as the value of this parameter to get the old behavior. · 3 unmodified lines
1055010596
1055110597 <p>
1055210598 Here is the complete syntax of Lua in extended BNF.
10553−(It does not describe operator precedences.)
10599+(For operator precedences, see <a href="#3.4.8">&sect;3.4.8</a>.)
1055410600
1055510601
1055610602
@@ -10635,10 +10681,10 @@ Here is the complete syntax of Lua in extended BNF. · 78 unmodified lines
1063510681 <HR>
1063610682 <SMALL CLASS="footer">
1063710683 Last update:
10638−Thu Jun 19 17:13:19 BRT 2014
10684+Thu Jul 31 14:02:14 BRT 2014
1063910685 </SMALL>
1064010686 <!--
10641−Last change: updated for Lua 5.3.0 (work3)
10687+Last change: revised for Lua 5.3.0 (alpha)
1064210688 -->
1064310689
1064410690 </body></html>
doc/readme.html+18−16
@@ -31,12 +31,12 @@ tt, kbd, code { · 30 unmodified lines
3131 <HR>
3232 <H1>
3333 <A HREF="http://www.lua.org/"><IMG SRC="logo.gif" ALT="Lua" BORDER=0></A>
34−Welcome to Lua 5.3 (work3)
34+Welcome to Lua 5.3 (alpha)
3535 </H1>
3636
3737 <P>
3838 <IMG SRC="alert.png" ALIGN="absbottom">
39−<EM>All details may change in the final version.</EM>
39+<EM>Some details may change in the final version.</EM>
4040
4141 <P>
4242 <A HREF="#about">about</A>
@@ -114,7 +114,7 @@ Here are the details. · 71 unmodified lines
114114 <OL>
115115 <LI>
116116 Open a terminal window and move to
117−the top-level directory, which is named <TT>lua-5.3.0-work3</TT>.
117+the top-level directory, which is named <TT>lua-5.3.0-alpha</TT>.
118118 The <TT>Makefile</TT> there controls both the build process and the installation process.
119119 <P>
120120 <LI>
@@ -275,36 +275,38 @@ lists the · 154 unmodified lines
275275 <LI> better support for small architectures ("Small Lua" with 32-bit numbers)
276276 <LI> bitwise operators
277277 <LI> basic utf-8 library
278+
278279 <LI> utf-8 escapes in literal strings
279−<LI> functions for packing/unpacking numbers
280−<LI> userdata can have any Lua value as uservalue
281280 <LI> strip option in <CODE>lua_dump</CODE>/<CODE>string.dump</CODE>
282281 </UL>
283282
284283 Here are the other changes introduced in Lua 5.3:
285284 <H3>Language</H3>
286285 <UL>
287−<LI>
286+<LI> userdata can have any Lua value as uservalue
287+<LI> integer division
288+<LI> more flexible rules for some metamethods
288289 </UL>
289290
290291 <H3>Libraries</H3>
291292 <UL>
292−<LI>
293+<LI> functions for packing/unpacking numbers
294+<LI> strip option in <CODE>string.dump</CODE>
295+<LI> table library respects metamethods
296+<LI> new function <CODE>table.copy</CODE>
297+<LI> new function <CODE>debug.Csize</CODE>
293298 </UL>
294299
295300 <H3>C API</H3>
296301 <UL>
297−<LI>
298−</UL>
299−
300−<H3>Implementation</H3>
301−<UL>
302−<LI>
302+<LI> new functions: <CODE>lua_rotate</CODE>, <CODE>lua_isyieldable</CODE>, <CODE>lua_strtonum</CODE>
303+<LI> <CODE>lua_gettable</CODE> and similar functions return type of resulted value
303304 </UL>
304305
305306 <H3>Lua standalone interpreter</H3>
306307 <UL>
307−<LI> Can be used as calculator; no need to prefix with '='
308+<LI> can be used as calculator; no need to prefix with '='
309+<LI> <CODE>arg</CODE> table available to all code
308310 </UL>
309311
310312 <H2><A NAME="license">License</A></H2>
@@ -354,10 +356,10 @@ THE SOFTWARE. · 43 unmodified lines
354356 <HR>
355357 <SMALL CLASS="footer">
356358 Last update:
357−Wed Jun 11 22:54:18 BRT 2014
359+Thu Jul 31 15:23:46 BRT 2014
358360 </SMALL>
359361 <!--
360−Last change: updated for Lua 5.3.0 (work3)
362+Last change: updated for Lua 5.3.0 (alpha)
361363 -->
362364
363365 </BODY>
src/Makefile+1−1
@@ -7,7 +7,7 @@ · 6 unmodified lines
77 PLAT= none
88
99 CC= gcc
10−CFLAGS= -O2 -Wall -Wextra -DLUA_COMPAT_ALL $(SYSCFLAGS) $(MYCFLAGS)
10+CFLAGS= -O2 -Wall -Wextra -DLUA_COMPAT_5_2 $(SYSCFLAGS) $(MYCFLAGS)
1111 LDFLAGS= $(SYSLDFLAGS) $(MYLDFLAGS)
1212 LIBS= -lm $(SYSLIBS) $(MYLIBS)
1313
src/lapi.c+84−130
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lapi.c,v 2.219 2014/06/19 18:27:20 roberto Exp $
2+** $Id: lapi.c,v 2.232 2014/07/30 14:00:14 roberto Exp $
33 ** Lua API
44 ** See Copyright Notice in lua.h
55 */
@@ -44,32 +44,35 @@ const char lua_ident[] = · 38 unmodified lines
4444 /* test for pseudo index */
4545 #define ispseudo(i) ((i) <= LUA_REGISTRYINDEX)
4646
47+/* test for upvalue */
48+#define isupvalue(i) ((i) < LUA_REGISTRYINDEX)
49+
4750 /* test for valid but not pseudo index */
4851 #define isstackindex(i, o) (isvalid(o) && !ispseudo(i))
4952
50−#define api_checkvalidindex(L, o) api_check(L, isvalid(o), "invalid index")
53+#define api_checkvalidindex(o) api_check(isvalid(o), "invalid index")
5154
52−#define api_checkstackindex(L, i, o) \
53− api_check(L, isstackindex(i, o), "index not in the stack")
55+#define api_checkstackindex(i, o) \
56+ api_check(isstackindex(i, o), "index not in the stack")
5457
5558
5659 static TValue *index2addr (lua_State *L, int idx) {
5760 CallInfo *ci = L->ci;
5861 if (idx > 0) {
5962 TValue *o = ci->func + idx;
60− api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index");
63+ api_check(idx <= ci->top - (ci->func + 1), "unacceptable index");
6164 if (o >= L->top) return NONVALIDVALUE;
6265 else return o;
6366 }
6467 else if (!ispseudo(idx)) { /* negative index */
65− api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
68+ api_check(idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
6669 return L->top + idx;
6770 }
6871 else if (idx == LUA_REGISTRYINDEX)
6972 return &G(L)->l_registry;
7073 else { /* upvalues */
7174 idx = LUA_REGISTRYINDEX - idx;
72− api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
75+ api_check(idx <= MAXUPVAL + 1, "upvalue index too large");
7376 if (ttislcf(ci->func)) /* light C function? */
7477 return NONVALIDVALUE; /* it has no upvalues */
7578 else {
@@ -94,7 +97,7 @@ LUA_API int lua_checkstack (lua_State *L, int size) { · 18 unmodified lines
9497 int res;
9598 CallInfo *ci = L->ci;
9699 lua_lock(L);
97− api_check(L, size >= 0, "negative 'size'");
100+ api_check(size >= 0, "negative 'size'");
98101 if (L->stack_last - L->top > size) /* stack large enough? */
99102 res = 1; /* yes; check is OK */
100103 else { /* no; need to grow stack */
@@ -116,8 +119,8 @@ LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) { · 15 unmodified lines
116119 if (from == to) return;
117120 lua_lock(to);
118121 api_checknelems(from, n);
119− api_check(from, G(from) == G(to), "moving among independent states");
120− api_check(from, to->ci->top - to->top >= n, "not enough elements to move");
122+ api_check(G(from) == G(to), "moving among independent states");
123+ api_check(to->ci->top - to->top >= n, "not enough elements to move");
121124 from->top -= n;
122125 for (i = 0; i < n; i++) {
123126 setobj2s(to, to->top++, from->top + i);
@@ -168,13 +171,13 @@ LUA_API void lua_settop (lua_State *L, int idx) { · 44 unmodified lines
168171 StkId func = L->ci->func;
169172 lua_lock(L);
170173 if (idx >= 0) {
171− api_check(L, idx <= L->stack_last - (func + 1), "new top too large");
174+ api_check(idx <= L->stack_last - (func + 1), "new top too large");
172175 while (L->top < (func + 1) + idx)
173176 setnilvalue(L->top++);
174177 L->top = (func + 1) + idx;
175178 }
176179 else {
177− api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top");
180+ api_check(-(idx+1) <= (L->top - (func + 1)), "invalid new top");
178181 L->top += idx+1; /* `subtract' index (index is negative) */
179182 }
180183 lua_unlock(L);
@@ -183,7 +186,7 @@ LUA_API void lua_settop (lua_State *L, int idx) { · 2 unmodified lines
183186
184187 /*
185188 ** Reverse the stack segment from 'from' to 'to'
186−** (auxiliar to 'lua_rotate')
189+** (auxiliary to 'lua_rotate')
187190 */
188191 static void reverse (lua_State *L, StkId from, StkId to) {
189192 for (; from < to; from++, to--) {
@@ -204,8 +207,8 @@ LUA_API void lua_rotate (lua_State *L, int idx, int n) { · 14 unmodified lines
204207 lua_lock(L);
205208 t = L->top - 1; /* end of stack segment being rotated */
206209 p = index2addr(L, idx); /* start of segment */
207− api_checkstackindex(L, idx, p);
208− api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'");
210+ api_checkstackindex(idx, p);
211+ api_check((n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'");
209212 m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */
210213 reverse(L, p, m); /* reverse the prefix with length 'n' */
211214 reverse(L, m + 1, t); /* reverse the suffix */
@@ -214,31 +217,17 @@ LUA_API void lua_rotate (lua_State *L, int idx, int n) { · 2 unmodified lines
214217 }
215218
216219
217−static void moveto (lua_State *L, TValue *fr, int idx) {
218− TValue *to = index2addr(L, idx);
219− api_checkvalidindex(L, to);
220+LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
221+ TValue *fr, *to;
222+ lua_lock(L);
223+ fr = index2addr(L, fromidx);
224+ to = index2addr(L, toidx);
225+ api_checkvalidindex(to);
220226 setobj(L, to, fr);
221− if (idx < LUA_REGISTRYINDEX) /* function upvalue? */
227+ if (isupvalue(toidx)) /* function upvalue? */
222228 luaC_barrier(L, clCvalue(L->ci->func), fr);
223229 /* LUA_REGISTRYINDEX does not need gc barrier
224230 (collector revisits it before finishing collection) */
225−}
226−
227−
228−LUA_API void lua_replace (lua_State *L, int idx) {
229− lua_lock(L);
230− api_checknelems(L, 1);
231− moveto(L, L->top - 1, idx);
232− L->top--;
233− lua_unlock(L);
234−}
235−
236−
237−LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
238− TValue *fr;
239− lua_lock(L);
240− fr = index2addr(L, fromidx);
241− moveto(L, fr, toidx);
242231 lua_unlock(L);
243232 }
244233
@@ -265,6 +254,7 @@ LUA_API int lua_type (lua_State *L, int idx) { · 20 unmodified lines
265254
266255 LUA_API const char *lua_typename (lua_State *L, int t) {
267256 UNUSED(L);
257+ api_check(LUA_TNONE <= t && t < LUA_NUMTAGS, "invalid tag");
268258 return ttypename(t);
269259 }
270260
@@ -289,8 +279,8 @@ LUA_API int lua_isnumber (lua_State *L, int idx) { · 18 unmodified lines
289279
290280
291281 LUA_API int lua_isstring (lua_State *L, int idx) {
292− int t = lua_type(L, idx);
293− return (t == LUA_TSTRING || t == LUA_TNUMBER);
282+ const TValue *o = index2addr(L, idx);
283+ return (ttisstring(o) || cvt2str(o));
294284 }
295285
296286
@@ -334,7 +324,7 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { · 37 unmodified lines
334324 case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break;
335325 case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
336326 case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
337− default: api_check(L, 0, "invalid option");
327+ default: api_check(0, "invalid option");
338328 }
339329 }
340330 lua_unlock(L);
@@ -372,40 +362,6 @@ LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) { · 31 unmodified lines
372362 }
373363
374364
375−LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *pisnum) {
376− lua_Unsigned res = 0;
377− const TValue *o = index2addr(L, idx);
378− int isnum = 0;
379− switch (ttype(o)) {
380− case LUA_TNUMINT: {
381− res = l_castS2U(ivalue(o));
382− isnum = 1;
383− break;
384− }
385− case LUA_TNUMFLT: { /* compute floor(n) % 2^(numbits in an integer) */
386− const lua_Number two2n = cast_num(LUA_MAXUNSIGNED) + cast_num(1);
387− lua_Number n = fltvalue(o); /* get value */
388− int neg = 0;
389− n = l_floor(n); /* get its floor */
390− if (n < 0) {
391− neg = 1;
392− n = -n; /* make 'n' positive, so that 'fmod' is the same as '%' */
393− }
394− n = l_mathop(fmod)(n, two2n); /* n = n % 2^(numbits in an integer) */
395− if (luai_numisnan(n)) /* not a number? */
396− break; /* not an integer, too */
397− res = cast(lua_Unsigned, n); /* 'n' now must fit in an unsigned */
398− if (neg) res = 0u - res; /* back to negative, if needed */
399− isnum = 1;
400− break;
401− }
402− default: break;
403− }
404− if (pisnum) *pisnum = isnum;
405− return res;
406−}
407−
408−
409365 LUA_API int lua_toboolean (lua_State *L, int idx) {
410366 const TValue *o = index2addr(L, idx);
411367 return !l_isfalse(o);
@@ -415,14 +371,14 @@ LUA_API int lua_toboolean (lua_State *L, int idx) { · 3 unmodified lines
415371 LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
416372 StkId o = index2addr(L, idx);
417373 if (!ttisstring(o)) {
418− lua_lock(L); /* `luaV_tostring' may create a new string */
419− if (!luaV_tostring(L, o)) { /* conversion failed? */
374+ if (!cvt2str(o)) { /* not convertible? */
420375 if (len != NULL) *len = 0;
421− lua_unlock(L);
422376 return NULL;
423377 }
378+ lua_lock(L); /* `luaO_tostring' may create a new string */
424379 luaC_checkGC(L);
425380 o = index2addr(L, idx); /* previous call may reallocate the stack */
381+ luaO_tostring(L, o);
426382 lua_unlock(L);
427383 }
428384 if (len != NULL) *len = tsvalue(o)->len;
@@ -453,7 +409,7 @@ LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) { · 24 unmodified lines
453409 LUA_API void *lua_touserdata (lua_State *L, int idx) {
454410 StkId o = index2addr(L, idx);
455411 switch (ttnov(o)) {
456− case LUA_TUSERDATA: return (rawuvalue(o) + 1);
412+ case LUA_TUSERDATA: return getudatamem(uvalue(o));
457413 case LUA_TLIGHTUSERDATA: return pvalue(o);
458414 default: return NULL;
459415 }
@@ -512,14 +468,6 @@ LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { · 52 unmodified lines
512468 }
513469
514470
515−LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) {
516− lua_lock(L);
517− setivalue(L->top, l_castU2S(u));
518− api_incr_top(L);
519− lua_unlock(L);
520−}
521−
522−
523471 LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
524472 TString *ts;
525473 lua_lock(L);
@@ -582,7 +530,7 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { · 56 unmodified lines
582530 else {
583531 CClosure *cl;
584532 api_checknelems(L, n);
585− api_check(L, n <= MAXUPVAL, "upvalue index too large");
533+ api_check(n <= MAXUPVAL, "upvalue index too large");
586534 luaC_checkGC(L);
587535 cl = luaF_newCclosure(L, n);
588536 cl->f = fn;
@@ -667,7 +615,7 @@ LUA_API int lua_rawget (lua_State *L, int idx) { · 78 unmodified lines
667615 StkId t;
668616 lua_lock(L);
669617 t = index2addr(L, idx);
670− api_check(L, ttistable(t), "table expected");
618+ api_check(ttistable(t), "table expected");
671619 setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
672620 lua_unlock(L);
673621 return ttnov(L->top - 1);
@@ -678,7 +626,7 @@ LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) { · 4 unmodified lines
678626 StkId t;
679627 lua_lock(L);
680628 t = index2addr(L, idx);
681− api_check(L, ttistable(t), "table expected");
629+ api_check(ttistable(t), "table expected");
682630 setobj2s(L, L->top, luaH_getint(hvalue(t), n));
683631 api_incr_top(L);
684632 lua_unlock(L);
@@ -691,7 +639,7 @@ LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) { · 6 unmodified lines
691639 TValue k;
692640 lua_lock(L);
693641 t = index2addr(L, idx);
694− api_check(L, ttistable(t), "table expected");
642+ api_check(ttistable(t), "table expected");
695643 setpvalue(&k, cast(void *, p));
696644 setobj2s(L, L->top, luaH_get(hvalue(t), &k));
697645 api_incr_top(L);
@@ -746,8 +694,8 @@ LUA_API int lua_getuservalue (lua_State *L, int idx) { · 48 unmodified lines
746694 StkId o;
747695 lua_lock(L);
748696 o = index2addr(L, idx);
749− api_check(L, ttisfulluserdata(o), "full userdata expected");
750− getuservalue(L, rawuvalue(o), L->top);
697+ api_check(ttisfulluserdata(o), "full userdata expected");
698+ getuservalue(L, uvalue(o), L->top);
751699 api_incr_top(L);
752700 lua_unlock(L);
753701 return ttnov(L->top - 1);
@@ -796,42 +744,48 @@ LUA_API void lua_setfield (lua_State *L, int idx, const char *k) { · 42 unmodified lines
796744
797745
798746 LUA_API void lua_rawset (lua_State *L, int idx) {
799− StkId t;
747+ StkId o;
748+ Table *t;
800749 lua_lock(L);
801750 api_checknelems(L, 2);
802− t = index2addr(L, idx);
803− api_check(L, ttistable(t), "table expected");
804− setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
805− invalidateTMcache(hvalue(t));
806− luaC_barrierback(L, gcvalue(t), L->top-1);
751+ o = index2addr(L, idx);
752+ api_check(ttistable(o), "table expected");
753+ t = hvalue(o);
754+ setobj2t(L, luaH_set(L, t, L->top-2), L->top-1);
755+ invalidateTMcache(t);
756+ luaC_barrierback(L, t, L->top-1);
807757 L->top -= 2;
808758 lua_unlock(L);
809759 }
810760
811761
812762 LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
813− StkId t;
763+ StkId o;
764+ Table *t;
814765 lua_lock(L);
815766 api_checknelems(L, 1);
816− t = index2addr(L, idx);
817− api_check(L, ttistable(t), "table expected");
818− luaH_setint(L, hvalue(t), n, L->top - 1);
819− luaC_barrierback(L, gcvalue(t), L->top-1);
767+ o = index2addr(L, idx);
768+ api_check(ttistable(o), "table expected");
769+ t = hvalue(o);
770+ luaH_setint(L, t, n, L->top - 1);
771+ luaC_barrierback(L, t, L->top-1);
820772 L->top--;
821773 lua_unlock(L);
822774 }
823775
824776
825777 LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
826− StkId t;
778+ StkId o;
779+ Table *t;
827780 TValue k;
828781 lua_lock(L);
829782 api_checknelems(L, 1);
830− t = index2addr(L, idx);
831− api_check(L, ttistable(t), "table expected");
783+ o = index2addr(L, idx);
784+ api_check(ttistable(o), "table expected");
785+ t = hvalue(o);
832786 setpvalue(&k, cast(void *, p));
833− setobj2t(L, luaH_set(L, hvalue(t), &k), L->top - 1);
834− luaC_barrierback(L, gcvalue(t), L->top - 1);
787+ setobj2t(L, luaH_set(L, t, &k), L->top - 1);
788+ luaC_barrierback(L, t, L->top - 1);
835789 L->top--;
836790 lua_unlock(L);
837791 }
@@ -846,7 +800,7 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { · 8 unmodified lines
846800 if (ttisnil(L->top - 1))
847801 mt = NULL;
848802 else {
849− api_check(L, ttistable(L->top - 1), "table expected");
803+ api_check(ttistable(L->top - 1), "table expected");
850804 mt = hvalue(L->top - 1);
851805 }
852806 switch (ttnov(obj)) {
@@ -861,7 +815,7 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { · 8 unmodified lines
861815 case LUA_TUSERDATA: {
862816 uvalue(obj)->metatable = mt;
863817 if (mt) {
864− luaC_objbarrier(L, rawuvalue(obj), mt);
818+ luaC_objbarrier(L, uvalue(obj), mt);
865819 luaC_checkfinalizer(L, gcvalue(obj), mt);
866820 }
867821 break;
@@ -882,8 +836,8 @@ LUA_API void lua_setuservalue (lua_State *L, int idx) { · 14 unmodified lines
882836 lua_lock(L);
883837 api_checknelems(L, 1);
884838 o = index2addr(L, idx);
885− api_check(L, ttisfulluserdata(o), "full userdata expected");
886− setuservalue(L, rawuvalue(o), L->top - 1);
839+ api_check(ttisfulluserdata(o), "full userdata expected");
840+ setuservalue(L, uvalue(o), L->top - 1);
887841 luaC_barrier(L, gcvalue(o), L->top - 1);
888842 L->top--;
889843 lua_unlock(L);
@@ -896,18 +850,18 @@ LUA_API void lua_setuservalue (lua_State *L, int idx) { · 6 unmodified lines
896850
897851
898852 #define checkresults(L,na,nr) \
899− api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
853+ api_check((nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
900854 "results from function overflow current stack size")
901855
902856
903−LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx,
857+LUA_API void lua_callk (lua_State *L, int nargs, int nresults, lua_Ctx ctx,
904858 lua_KFunction k) {
905859 StkId func;
906860 lua_lock(L);
907− api_check(L, k == NULL || !isLua(L->ci),
861+ api_check(k == NULL || !isLua(L->ci),
908862 "cannot use continuations inside hooks");
909863 api_checknelems(L, nargs+1);
910− api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
864+ api_check(L->status == LUA_OK, "cannot do calls on non-normal thread");
911865 checkresults(L, nargs, nresults);
912866 func = L->top - (nargs+1);
913867 if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
@@ -940,21 +894,21 @@ static void f_call (lua_State *L, void *ud) { · 26 unmodified lines
940894
941895
942896 LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
943− int ctx, lua_KFunction k) {
897+ lua_Ctx ctx, lua_KFunction k) {
944898 struct CallS c;
945899 int status;
946900 ptrdiff_t func;
947901 lua_lock(L);
948− api_check(L, k == NULL || !isLua(L->ci),
902+ api_check(k == NULL || !isLua(L->ci),
949903 "cannot use continuations inside hooks");
950904 api_checknelems(L, nargs+1);
951− api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
905+ api_check(L->status == LUA_OK, "cannot do calls on non-normal thread");
952906 checkresults(L, nargs, nresults);
953907 if (errfunc == 0)
954908 func = 0;
955909 else {
956910 StkId o = index2addr(L, errfunc);
957− api_checkstackindex(L, errfunc, o);
911+ api_checkstackindex(errfunc, o);
958912 func = savestack(L, o);
959913 }
960914 c.func = L->top - (nargs+1); /* function to be called */
@@ -999,7 +953,7 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, · 38 unmodified lines
999953 const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
1000954 /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
1001955 setobj(L, f->upvals[0]->v, gt);
1002− luaC_barrier(L, f->upvals[0], gt);
956+ luaC_upvalbarrier(L, f->upvals[0]);
1003957 }
1004958 }
1005959 lua_unlock(L);
@@ -1062,7 +1016,7 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { · 56 unmodified lines
10621016 case LUA_GCSTEP: {
10631017 l_mem debt = 1; /* =1 to signal that it did an actual step */
10641018 int oldrunning = g->gcrunning;
1065− g->gcrunning = 1; /* force GC to run */
1019+ g->gcrunning = 1; /* allow GC to run */
10661020 if (data == 0) {
10671021 luaE_setdebt(g, -GCSTEPSIZE); /* to do a "small" step */
10681022 luaC_step(L);
@@ -1119,7 +1073,7 @@ LUA_API int lua_next (lua_State *L, int idx) { · 50 unmodified lines
11191073 int more;
11201074 lua_lock(L);
11211075 t = index2addr(L, idx);
1122− api_check(L, ttistable(t), "table expected");
1076+ api_check(ttistable(t), "table expected");
11231077 more = luaH_next(L, hvalue(t), L->top - 1);
11241078 if (more) {
11251079 api_incr_top(L);
@@ -1183,19 +1137,19 @@ LUA_API void *lua_newuserdata (lua_State *L, size_t size) { · 57 unmodified lines
11831137 setuvalue(L, L->top, u);
11841138 api_incr_top(L);
11851139 lua_unlock(L);
1186− return u + 1;
1140+ return getudatamem(u);
11871141 }
11881142
11891143
11901144
11911145 static const char *aux_upvalue (StkId fi, int n, TValue **val,
1192− GCObject **owner, UpVal **uv) {
1146+ CClosure **owner, UpVal **uv) {
11931147 switch (ttype(fi)) {
11941148 case LUA_TCCL: { /* C closure */
11951149 CClosure *f = clCvalue(fi);
11961150 if (!(1 <= n && n <= f->nupvalues)) return NULL;
11971151 *val = &f->upvalue[n-1];
1198− if (owner) *owner = obj2gco(f);
1152+ if (owner) *owner = f;
11991153 return "";
12001154 }
12011155 case LUA_TLCL: { /* Lua closure */
@@ -1230,7 +1184,7 @@ LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) { · 28 unmodified lines
12301184 LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
12311185 const char *name;
12321186 TValue *val = NULL; /* to avoid warnings */
1233− GCObject *owner = NULL;
1187+ CClosure *owner = NULL;
12341188 UpVal *uv = NULL;
12351189 StkId fi;
12361190 lua_lock(L);
@@ -1251,9 +1205,9 @@ LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { · 14 unmodified lines
12511205 static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
12521206 LClosure *f;
12531207 StkId fi = index2addr(L, fidx);
1254− api_check(L, ttisLclosure(fi), "Lua function expected");
1208+ api_check(ttisLclosure(fi), "Lua function expected");
12551209 f = clLvalue(fi);
1256− api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
1210+ api_check((1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
12571211 if (pf) *pf = f;
12581212 return &f->upvals[n - 1]; /* get its upvalue pointer */
12591213 }
@@ -1267,11 +1221,11 @@ LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) { · 7 unmodified lines
12671221 }
12681222 case LUA_TCCL: { /* C closure */
12691223 CClosure *f = clCvalue(fi);
1270− api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index");
1224+ api_check(1 <= n && n <= f->nupvalues, "invalid upvalue index");
12711225 return &f->upvalue[n - 1];
12721226 }
12731227 default: {
1274− api_check(L, 0, "closure expected");
1228+ api_check(0, "closure expected");
12751229 return NULL;
12761230 }
12771231 }
src/lapi.h+3−3
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lapi.h,v 2.7 2009/11/27 15:37:59 roberto Exp $
2+** $Id: lapi.h,v 2.8 2014/07/15 21:26:50 roberto Exp $
33 ** Auxiliary functions from Lua API
44 ** See Copyright Notice in lua.h
55 */
@@ -11,13 +11,13 @@ · 5 unmodified lines
1111 #include "llimits.h"
1212 #include "lstate.h"
1313
14−#define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \
14+#define api_incr_top(L) {L->top++; api_check(L->top <= L->ci->top, \
1515 "stack overflow");}
1616
1717 #define adjustresults(L,nres) \
1818 { if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; }
1919
20−#define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \
20+#define api_checknelems(L,n) api_check((n) < (L->top - L->ci->func), \
2121 "not enough elements in the stack")
2222
2323
src/lauxlib.c+19−31
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lauxlib.c,v 1.263 2014/05/12 21:44:17 roberto Exp $
2+** $Id: lauxlib.c,v 1.267 2014/07/19 14:37:09 roberto Exp $
33 ** Auxiliary functions for building Lua libraries
44 ** See Copyright Notice in lua.h
55 */
@@ -396,8 +396,8 @@ LUALIB_API lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number def) { · 390 unmodified lines
396396
397397
398398 static void interror (lua_State *L, int arg) {
399− if (lua_type(L, arg) == LUA_TNUMBER)
400− luaL_argerror(L, arg, "float value out of integer range");
399+ if (lua_isnumber(L, arg))
400+ luaL_argerror(L, arg, "number has no integer representation");
401401 else
402402 tag_error(L, arg, LUA_TNUMBER);
403403 }
@@ -413,26 +413,11 @@ LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int arg) { · 9 unmodified lines
413413 }
414414
415415
416−LUALIB_API lua_Unsigned luaL_checkunsigned (lua_State *L, int arg) {
417− int isnum;
418− lua_Unsigned d = lua_tounsignedx(L, arg, &isnum);
419− if (!isnum)
420− interror(L, arg);
421− return d;
422−}
423−
424−
425416 LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int arg,
426417 lua_Integer def) {
427418 return luaL_opt(L, luaL_checkinteger, arg, def);
428419 }
429420
430−
431−LUALIB_API lua_Unsigned luaL_optunsigned (lua_State *L, int arg,
432− lua_Unsigned def) {
433− return luaL_opt(L, luaL_checkunsigned, arg, def);
434−}
435−
436421 /* }====================================================== */
437422
438423
@@ -898,22 +883,26 @@ LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) { · 459 unmodified lines
898883
899884
900885 /*
901−** stripped-down 'require'. Calls 'openf' to open a module,
902−** registers the result in 'package.loaded' table and, if 'glb'
903−** is true, also registers the result in the global table.
886+** Stripped-down 'require': After checking "loaded" table, calls 'openf'
887+** to open a module, registers the result in 'package.loaded' table and,
888+** if 'glb' is true, also registers the result in the global table.
904889 ** Leaves resulting module on the top.
905890 */
906891 LUALIB_API void luaL_requiref (lua_State *L, const char *modname,
907892 lua_CFunction openf, int glb) {
908− lua_pushcfunction(L, openf);
909− lua_pushstring(L, modname); /* argument to open function */
910− lua_call(L, 1, 1); /* open module */
911893 luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");
912− lua_pushvalue(L, -2); /* make copy of module (call result) */
913− lua_setfield(L, -2, modname); /* _LOADED[modname] = module */
914− lua_pop(L, 1); /* remove _LOADED table */
894+ lua_getfield(L, -1, modname); /* _LOADED[modname] */
895+ if (!lua_toboolean(L, -1)) { /* package not already loaded? */
896+ lua_pop(L, 1); /* remove field */
897+ lua_pushcfunction(L, openf);
898+ lua_pushstring(L, modname); /* argument to open function */
899+ lua_call(L, 1, 1); /* call 'openf' to open module */
900+ lua_pushvalue(L, -1); /* make copy of module (call result) */
901+ lua_setfield(L, -3, modname); /* _LOADED[modname] = module */
902+ }
903+ lua_remove(L, -2); /* remove _LOADED table */
915904 if (glb) {
916− lua_pushvalue(L, -1); /* copy of 'mod' */
905+ lua_pushvalue(L, -1); /* copy of module */
917906 lua_setglobal(L, modname); /* _G[modname] = module */
918907 }
919908 }
@@ -963,13 +952,12 @@ LUALIB_API lua_State *luaL_newstate (void) { · 43 unmodified lines
963952
964953 LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) {
965954 const lua_Number *v = lua_version(L);
955+ if (sz != LUAL_NUMSIZES) /* check numeric types */
956+ luaL_error(L, "core and library have incompatible numeric types");
966957 if (v != lua_version(NULL))
967958 luaL_error(L, "multiple Lua VMs detected");
968959 else if (*v != ver)
969960 luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f",
970961 ver, *v);
971− /* check numeric types */
972− if (sz != LUAL_NUMSIZES)
973− luaL_error(L, "core and library have incompatible numeric types");
974962 }
975963
src/lauxlib.h+17−4
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lauxlib.h,v 1.124 2014/04/15 18:25:49 roberto Exp $
2+** $Id: lauxlib.h,v 1.125 2014/06/26 17:25:11 roberto Exp $
33 ** Auxiliary functions for building Lua libraries
44 ** See Copyright Notice in lua.h
55 */
@@ -46,9 +46,6 @@ LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int arg, lua_Number def); · 40 unmodified lines
4646 LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int arg);
4747 LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int arg,
4848 lua_Integer def);
49−LUALIB_API lua_Unsigned (luaL_checkunsigned) (lua_State *L, int arg);
50−LUALIB_API lua_Unsigned (luaL_optunsigned) (lua_State *L, int arg,
51− lua_Unsigned def);
5249
5350 LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg);
5451 LUALIB_API void (luaL_checktype) (lua_State *L, int arg, int t);
@@ -211,6 +208,22 @@ LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname, · 156 unmodified lines
211208 #endif
212209
213210
211+/*
212+** {============================================================
213+** Compatibility with deprecated unsigned conversions
214+** =============================================================
215+*/
216+#if defined(LUA_COMPAT_APIUNSIGNED)
217+
218+#define luaL_checkunsigned(L,a) ((lua_Unsigned)luaL_checkinteger(L,a))
219+#define luaL_optunsigned(L,a,d) \
220+ ((lua_Unsigned)luaL_optinteger(L,a,(lua_Integer)(d)))
221+
222+#endif
223+/* }============================================================ */
224+
225+
226+
214227 #endif
215228
216229
src/lbaselib.c+64−15
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lbaselib.c,v 1.289 2014/06/10 17:41:38 roberto Exp $
2+** $Id: lbaselib.c,v 1.293 2014/07/24 19:33:29 roberto Exp $
33 ** Basic library
44 ** See Copyright Notice in lua.h
55 */
@@ -200,9 +200,12 @@ static int luaB_collectgarbage (lua_State *L) { · 194 unmodified lines
200200 }
201201
202202
203+/*
204+** This function has all type names as upvalues, to maximize performance.
205+*/
203206 static int luaB_type (lua_State *L) {
204207 luaL_checkany(L, 1);
205− lua_pushstring(L, luaL_typename(L, 1));
208+ lua_pushvalue(L, lua_upvalueindex(lua_type(L, 1) + 1));
206209 return 1;
207210 }
208211
@@ -241,17 +244,53 @@ static int luaB_pairs (lua_State *L) { · 32 unmodified lines
241244 }
242245
243246
244−static int ipairsaux (lua_State *L) {
245− int i = luaL_checkint(L, 2);
247+/*
248+** Traversal function for 'ipairs' for raw tables
249+*/
250+static int ipairsaux_raw (lua_State *L) {
251+ int i = luaL_checkint(L, 2) + 1;
246252 luaL_checktype(L, 1, LUA_TTABLE);
247− i++; /* next value */
248253 lua_pushinteger(L, i);
249254 return (lua_rawgeti(L, 1, i) == LUA_TNIL) ? 1 : 2;
250255 }
251256
252257
258+/*
259+** Traversal function for 'ipairs' for tables with metamethods
260+*/
261+static int ipairsaux (lua_State *L) {
262+ int i = luaL_checkint(L, 2) + 1;
263+ if (i > luaL_len(L, 1)) { /* larger than length? */
264+ lua_pushnil(L); /* end traversal */
265+ return 1;
266+ }
267+ else {
268+ lua_pushinteger(L, i);
269+ lua_pushinteger(L, i); /* key for indexing table */
270+ lua_gettable(L, 1);
271+ return 2;
272+ }
273+}
274+
275+
276+/*
277+** This function will use either 'ipairsaux' or 'ipairsaux_raw' to
278+** traverse a table, depending on whether the table has metamethods
279+** that can affect the traversal.
280+*/
253281 static int luaB_ipairs (lua_State *L) {
254− return pairsmeta(L, "__ipairs", 1, ipairsaux);
282+ lua_CFunction iter =
283+ (luaL_getmetafield(L, 1, "__len") ||
284+ luaL_getmetafield(L, 1, "__index"))
285+ ? ipairsaux : ipairsaux_raw;
286+#if defined(LUA_COMPAT_IPAIRS)
287+ return pairsmeta(L, "__ipairs", 1, iter);
288+#else
289+ lua_pushcfunction(L, iter); /* iteration function */
290+ lua_pushvalue(L, 1); /* state */
291+ lua_pushinteger(L, 0); /* initial value */
292+ return 3;
293+#endif
255294 }
256295
257296
@@ -341,7 +380,7 @@ static int luaB_load (lua_State *L) { · 83 unmodified lines
341380 /* }====================================================== */
342381
343382
344−static int dofilecont (lua_State *L, int d1, int d2) {
383+static int dofilecont (lua_State *L, int d1, lua_Ctx d2) {
345384 (void)d1; (void)d2; /* only to match 'lua_Kfunction' prototype */
346385 return lua_gettop(L) - 1;
347386 }
@@ -387,12 +426,12 @@ static int luaB_select (lua_State *L) { · 39 unmodified lines
387426
388427 /*
389428 ** Continuation function for 'pcall' and 'xpcall'. Both functions
390−** already pushed a 'true' before doing the call, so in case of sucess
429+** already pushed a 'true' before doing the call, so in case of success
391430 ** 'finishpcall' only has to return everything in the stack minus
392431 ** 'extra' values (where 'extra' is exactly the number of items to be
393432 ** ignored).
394433 */
395−static int finishpcall (lua_State *L, int status, int extra) {
434+static int finishpcall (lua_State *L, int status, lua_Ctx extra) {
396435 if (status != LUA_OK && status != LUA_YIELD) { /* error? */
397436 lua_pushboolean(L, 0); /* first result (false) */
398437 lua_pushvalue(L, -2); /* error message */
@@ -461,21 +500,31 @@ static const luaL_Reg base_funcs[] = { · 62 unmodified lines
461500 {"setmetatable", luaB_setmetatable},
462501 {"tonumber", luaB_tonumber},
463502 {"tostring", luaB_tostring},
464− {"type", luaB_type},
465503 {"xpcall", luaB_xpcall},
504+ /* placeholders */
505+ {"type", NULL},
506+ {"_G", NULL},
507+ {"_VERSION", NULL},
466508 {NULL, NULL}
467509 };
468510
469511
470512 LUAMOD_API int luaopen_base (lua_State *L) {
471− /* set global _G */
472− lua_pushglobaltable(L);
473− lua_pushglobaltable(L);
474− lua_setfield(L, -2, "_G");
513+ int i;
475514 /* open lib into global table */
515+ lua_pushglobaltable(L);
476516 luaL_setfuncs(L, base_funcs, 0);
517+ /* set global _G */
518+ lua_pushvalue(L, -1);
519+ lua_setfield(L, -2, "_G");
520+ /* set global _VERSION */
477521 lua_pushliteral(L, LUA_VERSION);
478− lua_setfield(L, -2, "_VERSION"); /* set global _VERSION */
522+ lua_setfield(L, -2, "_VERSION");
523+ /* set function 'type' with proper upvalues */
524+ for (i = 0; i < LUA_NUMTAGS; i++) /* push all type names as upvalues */
525+ lua_pushstring(L, lua_typename(L, i));
526+ lua_pushcclosure(L, luaB_type, LUA_NUMTAGS);
527+ lua_setfield(L, -2, "type");
479528 return 1;
480529 }
481530
src/ldebug.c+9−8
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ldebug.c,v 2.97 2013/12/09 14:21:10 roberto Exp $
2+** $Id: ldebug.c,v 2.100 2014/07/30 14:00:14 roberto Exp $
33 ** Debug Interface
44 ** See Copyright Notice in lua.h
55 */
@@ -271,7 +271,7 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { · 265 unmodified lines
271271 if (*what == '>') {
272272 ci = NULL;
273273 func = L->top - 1;
274− api_check(L, ttisfunction(func), "function expected");
274+ api_check(ttisfunction(func), "function expected");
275275 what++; /* skip the '>' */
276276 L->top--; /* pop function */
277277 }
@@ -526,26 +526,27 @@ l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) { · 248 unmodified lines
526526
527527
528528 l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) {
529− if (ttisstring(p1) || ttisnumber(p1)) p1 = p2;
530− lua_assert(!ttisstring(p1) && !ttisnumber(p1));
529+ if (ttisstring(p1) || cvt2str(p1)) p1 = p2;
531530 luaG_typeerror(L, p1, "concatenate");
532531 }
533532
534533
535534 l_noret luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) {
536535 lua_Number temp;
537− if (!tonumber(p1, &temp))
538− p2 = p1; /* first operand is wrong */
536+ if (!tonumber(p1, &temp)) /* first operand is wrong? */
537+ p2 = p1; /* now second is wrong */
539538 luaG_typeerror(L, p2, "perform arithmetic on");
540539 }
541540
542541
542+/*
543+** Error when both values are convertible to numbers, but not to integers
544+*/
543545 l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) {
544546 lua_Integer temp;
545547 if (!tointeger(p1, &temp))
546548 p2 = p1;
547− luaG_runerror(L, "attempt to convert an out of range float%s to an integer",
548− varinfo(L, p2));
549+ luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2));
549550 }
550551
551552
src/ldo.c+5−4
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ldo.c,v 2.123 2014/06/19 18:27:20 roberto Exp $
2+** $Id: ldo.c,v 2.126 2014/07/17 13:53:37 roberto Exp $
33 ** Stack and Call structure of Lua
44 ** See Copyright Notice in lua.h
55 */
@@ -516,7 +516,7 @@ static l_noret resume_error (lua_State *L, const char *msg, StkId firstArg) { · 510 unmodified lines
516516 /*
517517 ** Do the work for 'lua_resume' in protected mode. Most of the work
518518 ** depends on the status of the coroutine: initial state, suspended
519−** inside a hook, or regulary suspended (optionally with a continuation
519+** inside a hook, or regularly suspended (optionally with a continuation
520520 ** function), plus erroneous cases: non-suspended coroutine or dead
521521 ** coroutine.
522522 */
@@ -593,7 +593,8 @@ LUA_API int lua_isyieldable (lua_State *L) { · 70 unmodified lines
593593 }
594594
595595
596−LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_KFunction k) {
596+LUA_API int lua_yieldk (lua_State *L, int nresults, lua_Ctx ctx,
597+ lua_KFunction k) {
597598 CallInfo *ci = L->ci;
598599 luai_userstateyield(L, nresults);
599600 lua_lock(L);
@@ -607,7 +608,7 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_KFunction k) { · 7 unmodified lines
607608 L->status = LUA_YIELD;
608609 ci->extra = savestack(L, ci->func); /* save current 'func' */
609610 if (isLua(ci)) { /* inside a hook? */
610− api_check(L, k == NULL, "hooks cannot continue after yielding");
611+ api_check(k == NULL, "hooks cannot continue after yielding");
611612 }
612613 else {
613614 if ((ci->u.c.k = k) != NULL) /* is there a continuation? */
src/ldump.c+3−3
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ldump.c,v 2.32 2014/06/18 18:35:43 roberto Exp $
2+** $Id: ldump.c,v 2.33 2014/07/18 13:36:14 roberto Exp $
33 ** save precompiled Lua chunks
44 ** See Copyright Notice in lua.h
55 */
@@ -71,7 +71,7 @@ static void DumpString (const TString *s, DumpState *D) { · 65 unmodified lines
7171 if (s == NULL)
7272 DumpByte(0, D);
7373 else {
74− size_t size = s->tsv.len + 1; /* include trailing '\0' */
74+ size_t size = s->len + 1; /* include trailing '\0' */
7575 if (size < 0xFF)
7676 DumpByte(cast_int(size), D);
7777 else {
@@ -112,7 +112,7 @@ static void DumpConstants (const Proto *f, DumpState *D) { · 34 unmodified lines
112112 break;
113113 case LUA_TSHRSTR:
114114 case LUA_TLNGSTR:
115− DumpString(rawtsvalue(o), D);
115+ DumpString(tsvalue(o), D);
116116 break;
117117 default:
118118 lua_assert(0);
src/lgc.c+50−52
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lgc.c,v 2.183 2014/05/25 19:08:32 roberto Exp $
2+** $Id: lgc.c,v 2.192 2014/07/29 16:22:24 roberto Exp $
33 ** Garbage Collector
44 ** See Copyright Notice in lua.h
55 */
@@ -62,10 +62,10 @@ · 56 unmodified lines
6262 */
6363 #define maskcolors (~(bitmask(BLACKBIT) | WHITEBITS))
6464 #define makewhite(g,x) \
65− (gch(x)->marked = cast_byte((gch(x)->marked & maskcolors) | luaC_white(g)))
65+ (x->marked = cast_byte((x->marked & maskcolors) | luaC_white(g)))
6666
67−#define white2gray(x) resetbits(gch(x)->marked, WHITEBITS)
68−#define black2gray(x) resetbit(gch(x)->marked, BLACKBIT)
67+#define white2gray(x) resetbits(x->marked, WHITEBITS)
68+#define black2gray(x) resetbit(x->marked, BLACKBIT)
6969
7070
7171 #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x)))
@@ -81,7 +81,7 @@ · 9 unmodified lines
8181 if (valiswhite(o)) reallymarkobject(g,gcvalue(o)); }
8282
8383 #define markobject(g,t) \
84− { if ((t) && iswhite(obj2gco(t))) reallymarkobject(g, obj2gco(t)); }
84+ { if ((t) && iswhite(t)) reallymarkobject(g, obj2gco(t)); }
8585
8686 static void reallymarkobject (global_State *g, GCObject *o);
8787
@@ -112,7 +112,7 @@ static void reallymarkobject (global_State *g, GCObject *o); · 24 unmodified lines
112112 static void removeentry (Node *n) {
113113 lua_assert(ttisnil(gval(n)));
114114 if (valiswhite(gkey(n)))
115− setdeadvalue(gkey(n)); /* unused and unmarked key; remove it */
115+ setdeadvalue(wgkey(n)); /* unused and unmarked key; remove it */
116116 }
117117
118118
@@ -126,7 +126,7 @@ static void removeentry (Node *n) { · 7 unmodified lines
126126 static int iscleared (global_State *g, const TValue *o) {
127127 if (!iscollectable(o)) return 0;
128128 else if (ttisstring(o)) {
129− markobject(g, rawtsvalue(o)); /* strings are `values', so are never weak */
129+ markobject(g, tsvalue(o)); /* strings are `values', so are never weak */
130130 return 0;
131131 }
132132 else return iswhite(gcvalue(o));
@@ -135,13 +135,13 @@ static int iscleared (global_State *g, const TValue *o) { · 2 unmodified lines
135135
136136 /*
137137 ** barrier that moves collector forward, that is, mark the white object
138−** being pointed by a black object.
138+** being pointed by a black object. (If in sweep phase, clear the black
139+** object to white [sweep it] to avoid other barrier calls for this
140+** same object.)
139141 */
140142 void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) {
141143 global_State *g = G(L);
142144 lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o));
143− lua_assert(g->gcstate != GCSpause);
144− lua_assert(gch(o)->tt != LUA_TTABLE); /* tables use a back barrier */
145145 if (keepinvariant(g)) /* must keep invariant? */
146146 reallymarkobject(g, v); /* restore invariant */
147147 else { /* sweep phase */
@@ -153,16 +153,14 @@ void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) { · 5 unmodified lines
153153
154154 /*
155155 ** barrier that moves collector backward, that is, mark the black object
156−** pointing to a white object as gray again. (Current implementation
157−** only works for tables; access to 'gclist' is not uniform across
158−** different types.)
156+** pointing to a white object as gray again.
159157 */
160−void luaC_barrierback_ (lua_State *L, GCObject *o) {
158+void luaC_barrierback_ (lua_State *L, Table *t) {
161159 global_State *g = G(L);
162− lua_assert(isblack(o) && !isdead(g, o) && gch(o)->tt == LUA_TTABLE);
163− black2gray(o); /* make object gray (again) */
164− gco2t(o)->gclist = g->grayagain;
165− g->grayagain = o;
160+ lua_assert(isblack(t) && !isdead(g, t));
161+ black2gray(t); /* make table gray (again) */
162+ t->gclist = g->grayagain;
163+ g->grayagain = obj2gco(t);
166164 }
167165
168166
@@ -185,8 +183,8 @@ void luaC_fix (lua_State *L, GCObject *o) { · 16 unmodified lines
185183 global_State *g = G(L);
186184 lua_assert(g->allgc == o); /* object must be 1st in 'allgc' list! */
187185 white2gray(o); /* they will be gray forever */
188− g->allgc = o->gch.next; /* remove object from 'allgc' list */
189− o->gch.next = g->fixedgc; /* link it to 'fixedgc' list */
186+ g->allgc = o->next; /* remove object from 'allgc' list */
187+ o->next = g->fixedgc; /* link it to 'fixedgc' list */
190188 g->fixedgc = o;
191189 }
192190
@@ -198,9 +196,9 @@ void luaC_fix (lua_State *L, GCObject *o) { · 5 unmodified lines
198196 GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) {
199197 global_State *g = G(L);
200198 GCObject *o = cast(GCObject *, luaM_newobject(L, novariant(tt), sz));
201− gch(o)->marked = luaC_white(g);
202− gch(o)->tt = tt;
203− gch(o)->next = g->allgc;
199+ o->marked = luaC_white(g);
200+ o->tt = tt;
201+ o->next = g->allgc;
204202 g->allgc = o;
205203 return o;
206204 }
@@ -225,7 +223,7 @@ GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) { · 18 unmodified lines
225223 static void reallymarkobject (global_State *g, GCObject *o) {
226224 reentry:
227225 white2gray(o);
228− switch (gch(o)->tt) {
226+ switch (o->tt) {
229227 case LUA_TSHRSTR:
230228 case LUA_TLNGSTR: {
231229 gray2black(o);
@@ -237,7 +235,7 @@ static void reallymarkobject (global_State *g, GCObject *o) { · 5 unmodified lines
237235 markobject(g, gco2u(o)->metatable); /* mark its metatable */
238236 gray2black(o);
239237 g->GCmemtrav += sizeudata(gco2u(o));
240− getuservalue(g->mainthread, rawgco2u(o), &uvalue);
238+ getuservalue(g->mainthread, gco2u(o), &uvalue);
241239 if (valiswhite(&uvalue)) { /* markvalue(g, &uvalue); */
242240 o = gcvalue(&uvalue);
243241 goto reentry;
@@ -288,7 +286,7 @@ static void markmt (global_State *g) { · 44 unmodified lines
288286 */
289287 static void markbeingfnz (global_State *g) {
290288 GCObject *o;
291− for (o = g->tobefnz; o != NULL; o = gch(o)->next)
289+ for (o = g->tobefnz; o != NULL; o = o->next)
292290 markobject(g, o);
293291 }
294292
@@ -303,8 +301,8 @@ static void remarkupvals (global_State *g) { · 8 unmodified lines
303301 lua_State *thread;
304302 lua_State **p = &g->twups;
305303 while ((thread = *p) != NULL) {
306− lua_assert(!isblack(obj2gco(thread))); /* threads are never black */
307− if (isgray(obj2gco(thread)) && thread->openupval != NULL)
304+ lua_assert(!isblack(thread)); /* threads are never black */
305+ if (isgray(thread) && thread->openupval != NULL)
308306 p = &thread->twups; /* keep marked thread with upvalues in the list */
309307 else { /* thread is not marked or without upvalues */
310308 UpVal *uv;
@@ -429,7 +427,7 @@ static lu_mem traversetable (global_State *g, Table *h) { · 118 unmodified lines
429427 ((weakkey = strchr(svalue(mode), 'k')),
430428 (weakvalue = strchr(svalue(mode), 'v')),
431429 (weakkey || weakvalue))) { /* is really weak? */
432− black2gray(obj2gco(h)); /* keep table gray */
430+ black2gray(h); /* keep table gray */
433431 if (!weakkey) /* strong keys? */
434432 traverseweakvalue(g, h);
435433 else if (!weakvalue) /* strong values? */
@@ -446,7 +444,7 @@ static lu_mem traversetable (global_State *g, Table *h) { · 10 unmodified lines
446444
447445 static int traverseproto (global_State *g, Proto *f) {
448446 int i;
449− if (f->cache && iswhite(obj2gco(f->cache)))
447+ if (f->cache && iswhite(f->cache))
450448 f->cache = NULL; /* allow cache to be collected */
451449 markobject(g, f->source);
452450 for (i = 0; i < f->sizek; i++) /* mark literals */
@@ -528,7 +526,7 @@ static void propagatemark (global_State *g) { · 75 unmodified lines
528526 GCObject *o = g->gray;
529527 lua_assert(isgray(o));
530528 gray2black(o);
531− switch (gch(o)->tt) {
529+ switch (o->tt) {
532530 case LUA_TTABLE: {
533531 Table *h = gco2t(o);
534532 g->gray = h->gclist; /* remove from 'gray' list */
@@ -685,7 +683,7 @@ static void freeLclosure (lua_State *L, LClosure *cl) { · 150 unmodified lines
685683
686684
687685 static void freeobj (lua_State *L, GCObject *o) {
688− switch (gch(o)->tt) {
686+ switch (o->tt) {
689687 case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break;
690688 case LUA_TLCL: {
691689 freeLclosure(L, gco2lcl(o));
@@ -699,7 +697,7 @@ static void freeobj (lua_State *L, GCObject *o) { · 7 unmodified lines
699697 case LUA_TTHREAD: luaE_freethread(L, gco2th(o)); break;
700698 case LUA_TUSERDATA: luaM_freemem(L, o, sizeudata(gco2u(o))); break;
701699 case LUA_TSHRSTR:
702− luaS_remove(L, rawgco2ts(o)); /* remove it from hash table */
700+ luaS_remove(L, gco2ts(o)); /* remove it from hash table */
703701 /* go through */
704702 case LUA_TLNGSTR: {
705703 luaM_freemem(L, o, sizestring(gco2ts(o)));
@@ -727,14 +725,14 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) { · 21 unmodified lines
727725 int white = luaC_white(g); /* current white */
728726 while (*p != NULL && count-- > 0) {
729727 GCObject *curr = *p;
730− int marked = gch(curr)->marked;
728+ int marked = curr->marked;
731729 if (isdeadm(ow, marked)) { /* is 'curr' dead? */
732− *p = gch(curr)->next; /* remove 'curr' from list */
730+ *p = curr->next; /* remove 'curr' from list */
733731 freeobj(L, curr); /* erase 'curr' */
734732 }
735733 else { /* update marks */
736− gch(curr)->marked = cast_byte((marked & maskcolors) | white);
737− p = &gch(curr)->next; /* go to next element */
734+ curr->marked = cast_byte((marked & maskcolors) | white);
735+ p = &curr->next; /* go to next element */
738736 }
739737 }
740738 return (*p == NULL) ? NULL : p;
@@ -781,10 +779,10 @@ static void checkSizes (lua_State *L, global_State *g) { · 40 unmodified lines
781779 static GCObject *udata2finalize (global_State *g) {
782780 GCObject *o = g->tobefnz; /* get first element */
783781 lua_assert(tofinalize(o));
784− g->tobefnz = gch(o)->next; /* remove it from 'tobefnz' list */
785− gch(o)->next = g->allgc; /* return it to 'allgc' list */
782+ g->tobefnz = o->next; /* remove it from 'tobefnz' list */
783+ o->next = g->allgc; /* return it to 'allgc' list */
786784 g->allgc = o;
787− resetbit(gch(o)->marked, FINALIZEDBIT); /* object is "normal" again */
785+ resetbit(o->marked, FINALIZEDBIT); /* object is "normal" again */
788786 if (issweepphase(g))
789787 makewhite(g, o); /* "sweep" object */
790788 return o;
@@ -859,7 +857,7 @@ static void callallpendingfinalizers (lua_State *L, int propagateerrors) { · 68 unmodified lines
859857 */
860858 static GCObject **findlast (GCObject **p) {
861859 while (*p != NULL)
862− p = &gch(*p)->next;
860+ p = &(*p)->next;
863861 return p;
864862 }
865863
@@ -875,12 +873,12 @@ static void separatetobefnz (global_State *g, int all) { · 9 unmodified lines
875873 while ((curr = *p) != NULL) { /* traverse all finalizable objects */
876874 lua_assert(tofinalize(curr));
877875 if (!(iswhite(curr) || all)) /* not being collected? */
878− p = &gch(curr)->next; /* don't bother with it */
876+ p = &curr->next; /* don't bother with it */
879877 else {
880− *p = gch(curr)->next; /* remove 'curr' from "fin" list */
881− gch(curr)->next = *lastnext; /* link at the end of 'tobefnz' list */
878+ *p = curr->next; /* remove 'curr' from "fin" list */
879+ curr->next = *lastnext; /* link at the end of 'tobefnz' list */
882880 *lastnext = curr;
883− lastnext = &gch(curr)->next;
881+ lastnext = &curr->next;
884882 }
885883 }
886884 }
@@ -899,15 +897,15 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { · 12 unmodified lines
899897 GCObject **p;
900898 if (issweepphase(g)) {
901899 makewhite(g, o); /* "sweep" object 'o' */
902− if (g->sweepgc == &o->gch.next) /* shoud not remove 'sweepgc' object */
900+ if (g->sweepgc == &o->next) /* should not remove 'sweepgc' object */
903901 g->sweepgc = sweeptolive(L, g->sweepgc, NULL); /* change 'sweepgc' */
904902 }
905903 /* search for pointer pointing to 'o' */
906− for (p = &g->allgc; *p != o; p = &gch(*p)->next) { /* empty */ }
907− *p = o->gch.next; /* remove 'o' from 'allgc' list */
908− o->gch.next = g->finobj; /* link it in "fin" list */
904+ for (p = &g->allgc; *p != o; p = &(*p)->next) { /* empty */ }
905+ *p = o->next; /* remove 'o' from 'allgc' list */
906+ o->next = g->finobj; /* link it in "fin" list */
909907 g->finobj = o;
910− l_setbit(o->gch.marked, FINALIZEDBIT); /* mark it as such */
908+ l_setbit(o->marked, FINALIZEDBIT); /* mark it as such */
911909 }
912910 }
913911
@@ -975,7 +973,7 @@ static l_mem atomic (lua_State *L) { · 61 unmodified lines
975973 l_mem work;
976974 GCObject *origweak, *origall;
977975 g->GCmemtrav = 0; /* start counting work */
978− lua_assert(!iswhite(obj2gco(g->mainthread)));
976+ lua_assert(!iswhite(g->mainthread));
979977 g->gcstate = GCSinsideatomic;
980978 markobject(g, L); /* mark running thread */
981979 /* registry and global metatables may be changed by API */
@@ -1066,7 +1064,7 @@ static lu_mem singlestep (lua_State *L) { · 84 unmodified lines
10661064 return sweepstep(L, g, GCSswpend, NULL);
10671065 }
10681066 case GCSswpend: { /* finish sweeps */
1069− makewhite(g, obj2gco(g->mainthread)); /* sweep main thread */
1067+ makewhite(g, g->mainthread); /* sweep main thread */
10701068 checkSizes(L, g);
10711069 g->gcstate = GCScallfin;
10721070 return 0;
src/lgc.h+13−13
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lgc.h,v 2.82 2014/03/19 18:51:16 roberto Exp $
2+** $Id: lgc.h,v 2.85 2014/07/19 15:14:46 roberto Exp $
33 ** Garbage Collector
44 ** See Copyright Notice in lua.h
55 */
@@ -84,19 +84,19 @@ · 78 unmodified lines
8484 #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)
8585
8686
87−#define iswhite(x) testbits((x)->gch.marked, WHITEBITS)
88−#define isblack(x) testbit((x)->gch.marked, BLACKBIT)
87+#define iswhite(x) testbits((x)->marked, WHITEBITS)
88+#define isblack(x) testbit((x)->marked, BLACKBIT)
8989 #define isgray(x) /* neither white nor black */ \
90− (!testbits((x)->gch.marked, WHITEBITS | bitmask(BLACKBIT)))
90+ (!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT)))
9191
92−#define tofinalize(x) testbit((x)->gch.marked, FINALIZEDBIT)
92+#define tofinalize(x) testbit((x)->marked, FINALIZEDBIT)
9393
9494 #define otherwhite(g) ((g)->currentwhite ^ WHITEBITS)
9595 #define isdeadm(ow,m) (!(((m) ^ WHITEBITS) & (ow)))
96−#define isdead(g,v) isdeadm(otherwhite(g), (v)->gch.marked)
96+#define isdead(g,v) isdeadm(otherwhite(g), (v)->marked)
9797
98−#define changewhite(x) ((x)->gch.marked ^= WHITEBITS)
99−#define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT)
98+#define changewhite(x) ((x)->marked ^= WHITEBITS)
99+#define gray2black(x) l_setbit((x)->marked, BLACKBIT)
100100
101101 #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS)
102102
@@ -107,15 +107,15 @@ · 4 unmodified lines
107107
108108
109109 #define luaC_barrier(L,p,v) { \
110− if (iscollectable(v) && isblack(obj2gco(p)) && iswhite(gcvalue(v))) \
110+ if (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) \
111111 luaC_barrier_(L,obj2gco(p),gcvalue(v)); }
112112
113113 #define luaC_barrierback(L,p,v) { \
114− if (iscollectable(v) && isblack(obj2gco(p)) && iswhite(gcvalue(v))) \
115− luaC_barrierback_(L,obj2gco(p)); }
114+ if (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) \
115+ luaC_barrierback_(L,p); }
116116
117117 #define luaC_objbarrier(L,p,o) { \
118− if (isblack(obj2gco(p)) && iswhite(obj2gco(o))) \
118+ if (isblack(p) && iswhite(o)) \
119119 luaC_barrier_(L,obj2gco(p),obj2gco(o)); }
120120
121121 #define luaC_upvalbarrier(L,uv) \
@@ -129,7 +129,7 @@ LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask); · 7 unmodified lines
129129 LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
130130 LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz);
131131 LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v);
132−LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o);
132+LUAI_FUNC void luaC_barrierback_ (lua_State *L, Table *o);
133133 LUAI_FUNC void luaC_upvalbarrier_ (lua_State *L, UpVal *uv);
134134 LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt);
135135 LUAI_FUNC void luaC_upvdeccount (lua_State *L, UpVal *uv);
src/liolib.c+10−10
@@ -1,5 +1,5 @@
11 /*
2−** $Id: liolib.c,v 2.126 2014/06/02 03:00:51 roberto Exp $
2+** $Id: liolib.c,v 2.128 2014/07/29 16:01:00 roberto Exp $
33 ** Standard I/O (and system) library
44 ** See Copyright Notice in lua.h
55 */
@@ -402,11 +402,11 @@ static int test2 (RN *rn, const char *set) { · 396 unmodified lines
402402
403403
404404 /*
405−** Read a sequence of (hexa)digits
405+** Read a sequence of (hex)digits
406406 */
407−static int readdigits (RN *rn, int hexa) {
407+static int readdigits (RN *rn, int hex) {
408408 int count = 0;
409− while ((hexa ? isxdigit(rn->c) : isdigit(rn->c)) && nextc(rn))
409+ while ((hex ? isxdigit(rn->c) : isdigit(rn->c)) && nextc(rn))
410410 count++;
411411 return count;
412412 }
@@ -426,7 +426,7 @@ static int readdigits (RN *rn, int hexa) { · 13 unmodified lines
426426 static int read_number (lua_State *L, FILE *f) {
427427 RN rn;
428428 int count = 0;
429− int hexa = 0;
429+ int hex = 0;
430430 char decp[2] = ".";
431431 rn.f = f; rn.n = 0;
432432 decp[0] = getlocaledecpoint(); /* get decimal point from locale */
@@ -434,13 +434,13 @@ static int read_number (lua_State *L, FILE *f) { · 1 unmodified line
434434 do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */
435435 test2(&rn, "-+"); /* optional signal */
436436 if (test2(&rn, "0")) {
437− if (test2(&rn, "xX")) hexa = 1; /* numeral is hexadecimal */
437+ if (test2(&rn, "xX")) hex = 1; /* numeral is hexadecimal */
438438 else count = 1; /* count initial '0' as a valid digit */
439439 }
440− count += readdigits(&rn, hexa); /* integral part */
440+ count += readdigits(&rn, hex); /* integral part */
441441 if (test2(&rn, decp)) /* decimal point? */
442− count += readdigits(&rn, hexa); /* fractionary part */
443− if (count > 0 && test2(&rn, (hexa ? "pP" : "eE"))) { /* exponent mark? */
442+ count += readdigits(&rn, hex); /* fractional part */
443+ if (count > 0 && test2(&rn, (hex ? "pP" : "eE"))) { /* exponent mark? */
444444 test2(&rn, "-+"); /* exponent signal */
445445 readdigits(&rn, 0); /* exponent digits */
446446 }
@@ -654,7 +654,7 @@ static int f_setvbuf (lua_State *L) { · 207 unmodified lines
654654 FILE *f = tofile(L);
655655 int op = luaL_checkoption(L, 2, NULL, modenames);
656656 lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);
657− int res = setvbuf(f, NULL, mode[op], sz);
657+ int res = setvbuf(f, NULL, mode[op], (size_t)sz);
658658 return luaL_fileresult(L, res == 0, NULL);
659659 }
660660
src/llex.c+4−4
@@ -1,5 +1,5 @@
11 /*
2−** $Id: llex.c,v 2.78 2014/05/21 15:22:02 roberto Exp $
2+** $Id: llex.c,v 2.80 2014/07/18 13:36:14 roberto Exp $
33 ** Lexical Analyzer
44 ** See Copyright Notice in lua.h
55 */
@@ -71,7 +71,7 @@ void luaX_init (lua_State *L) { · 65 unmodified lines
7171 for (i=0; i<NUM_RESERVED; i++) {
7272 TString *ts = luaS_new(L, luaX_tokens[i]);
7373 luaC_fix(L, obj2gco(ts)); /* reserved words are never collected */
74− ts->tsv.extra = cast_byte(i+1); /* reserved word */
74+ ts->extra = cast_byte(i+1); /* reserved word */
7575 }
7676 }
7777
@@ -137,7 +137,7 @@ TString *luaX_newstring (LexState *ls, const char *str, size_t l) { · 59 unmodified lines
137137 luaC_checkGC(L);
138138 }
139139 else { /* string already present */
140− ts = rawtsvalue(keyfromval(o)); /* re-use value previously stored */
140+ ts = tsvalue(keyfromval(o)); /* re-use value previously stored */
141141 }
142142 L->top--; /* remove string from stack */
143143 return ts;
@@ -565,7 +565,7 @@ static int llex (LexState *ls, SemInfo *seminfo) { · 421 unmodified lines
565565 luaZ_bufflen(ls->buff));
566566 seminfo->ts = ts;
567567 if (isreserved(ts)) /* reserved word? */
568− return ts->tsv.extra - 1 + FIRST_RESERVED;
568+ return ts->extra - 1 + FIRST_RESERVED;
569569 else {
570570 return TK_NAME;
571571 }
src/llimits.h+15−17
@@ -1,5 +1,5 @@
11 /*
2−** $Id: llimits.h,v 1.116 2014/04/15 16:32:49 roberto Exp $
2+** $Id: llimits.h,v 1.120 2014/07/18 18:29:12 roberto Exp $
33 ** Limits, basic types, and some other `installation-dependent' definitions
44 ** See Copyright Notice in lua.h
55 */
@@ -28,36 +28,37 @@ typedef unsigned char lu_byte; · 22 unmodified lines
2828
2929
3030 /* maximum value for size_t */
31−#define MAX_SIZET ((size_t)(~(size_t)0)-2)
31+#define MAX_SIZET ((size_t)(~(size_t)0))
3232
3333 /* maximum size visible for Lua (must be representable in a lua_Integer */
3434 #define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \
35− : (size_t)(LUA_MAXINTEGER)-2)
35+ : (size_t)(LUA_MAXINTEGER))
3636
3737
38−#define MAX_LUMEM ((lu_mem)(~(lu_mem)0)-2)
38+#define MAX_LUMEM ((lu_mem)(~(lu_mem)0))
3939
40−#define MAX_LMEM ((l_mem) ((MAX_LUMEM >> 1) - 2))
40+#define MAX_LMEM ((l_mem)(MAX_LUMEM >> 1))
4141
4242
43−#define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */
43+#define MAX_INT INT_MAX /* maximum value of an int */
4444
4545
4646 /*
47−** conversion of pointer to integer
47+** conversion of pointer to integer:
4848 ** this is for hashing only; there is no problem if the integer
4949 ** cannot hold the whole pointer value
5050 */
51−#define IntPoint(p) ((unsigned int)(lu_mem)(p))
51+#define point2int(p) ((unsigned int)((lu_mem)(p) & UINT_MAX))
5252
5353
5454
5555 /* type to ensure maximum alignment */
56−#if !defined(LUAI_USER_ALIGNMENT_T)
57−#define LUAI_USER_ALIGNMENT_T union { double u; void *s; long l; }
56+#if defined(LUAI_USER_ALIGNMENT_T)
57+typedef LUAI_USER_ALIGNMENT_T L_Umaxalign;
58+#else
59+typedef union { double u; void *s; lua_Integer i; long l; } L_Umaxalign;
5860 #endif
5961
60−typedef LUAI_USER_ALIGNMENT_T L_Umaxalign;
6162
6263
6364 /* types of 'usual argument conversions' for lua_Number and lua_Integer */
@@ -79,18 +80,15 @@ typedef LUAI_UACINT l_uacInt; · 15 unmodified lines
7980 /*
8081 ** assertion for checking API calls
8182 */
82−#if !defined(luai_apicheck)
83−
8483 #if defined(LUA_USE_APICHECK)
8584 #include <assert.h>
86−#define luai_apicheck(L,e) assert(e)
85+#define luai_apicheck(e) assert(e)
8786 #else
88−#define luai_apicheck(L,e) lua_assert(e)
87+#define luai_apicheck(e) lua_assert(e)
8988 #endif
9089
91−#endif
9290
93−#define api_check(l,e,msg) luai_apicheck(l,(e) && msg)
91+#define api_check(e,msg) luai_apicheck((e) && msg)
9492
9593
9694 #if !defined(UNUSED)
src/lmathlib.c+38−23
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lmathlib.c,v 1.103 2014/06/18 12:35:53 roberto Exp $
2+** $Id: lmathlib.c,v 1.108 2014/07/28 17:35:47 roberto Exp $
33 ** Standard mathematical library
44 ** See Copyright Notice in lua.h
55 */
@@ -76,39 +76,39 @@ static int math_atan (lua_State *L) { · 70 unmodified lines
7676 }
7777
7878
79−static int math_ifloor (lua_State *L) {
79+static int math_toint (lua_State *L) {
8080 int valid;
8181 lua_Integer n = lua_tointegerx(L, 1, &valid);
8282 if (valid)
83− lua_pushinteger(L, n); /* floor computed by Lua */
83+ lua_pushinteger(L, n);
8484 else {
85− luaL_checktype(L, 1, LUA_TNUMBER); /* argument must be a number */
86− lua_pushnil(L); /* number is not convertible to integer */
85+ luaL_checkany(L, 1);
86+ lua_pushnil(L); /* value is not convertible to integer */
8787 }
8888 return 1;
8989 }
9090
9191
92−static int math_floor (lua_State *L) {
93− int valid;
94− lua_Integer n = lua_tointegerx(L, 1, &valid);
95− if (valid)
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−
10392 static void pushnumint (lua_State *L, lua_Number d) {
10493 lua_Integer n;
105− if (lua_numtointeger(d, &n)) /* fits in an integer? */
94+ if (lua_numtointeger(d, &n)) /* does 'd' fit in an integer? */
10695 lua_pushinteger(L, n); /* result is integer */
10796 else
10897 lua_pushnumber(L, d); /* result is float */
10998 }
11099
111100
101+static int math_floor (lua_State *L) {
102+ if (lua_isinteger(L, 1))
103+ lua_settop(L, 1); /* integer is its own floor */
104+ else {
105+ lua_Number d = l_mathop(floor)(luaL_checknumber(L, 1));
106+ pushnumint(L, d);
107+ }
108+ return 1;
109+}
110+
111+
112112 static int math_ceil (lua_State *L) {
113113 if (lua_isinteger(L, 1))
114114 lua_settop(L, 1); /* integer is its own ceil */
@@ -145,14 +145,14 @@ static int math_fmod (lua_State *L) { · 30 unmodified lines
145145 static int math_modf (lua_State *L) {
146146 if (lua_isinteger(L ,1)) {
147147 lua_settop(L, 1); /* number is its own integer part */
148− lua_pushnumber(L, 0); /* no fractionary part */
148+ lua_pushnumber(L, 0); /* no fractional part */
149149 }
150150 else {
151151 lua_Number n = luaL_checknumber(L, 1);
152152 /* integer part (rounds toward zero) */
153153 lua_Number ip = (n < 0) ? l_mathop(ceil)(n) : l_mathop(floor)(n);
154154 pushnumint(L, ip);
155− /* fractionary part (test needed for inf/-inf) */
155+ /* fractional part (test needed for inf/-inf) */
156156 lua_pushnumber(L, (n == ip) ? 0.0 : (n - ip));
157157 }
158158 return 2;
@@ -164,6 +164,14 @@ static int math_sqrt (lua_State *L) { · 5 unmodified lines
164164 return 1;
165165 }
166166
167+
168+static int math_ult (lua_State *L) {
169+ lua_Integer a = luaL_checkinteger(L, 1);
170+ lua_Integer b = luaL_checkinteger(L, 2);
171+ lua_pushboolean(L, (lua_Unsigned)a < (lua_Unsigned)b);
172+ return 1;
173+}
174+
167175 static int math_log (lua_State *L) {
168176 lua_Number x = luaL_checknumber(L, 1);
169177 lua_Number res;
@@ -257,22 +265,23 @@ static int math_random (lua_State *L) { · 87 unmodified lines
257265
258266
259267 static int math_randomseed (lua_State *L) {
260− l_srand((unsigned int)luaL_checkunsigned(L, 1));
268+ l_srand((unsigned int)(lua_Integer)luaL_checknumber(L, 1));
261269 (void)rand(); /* discard first value to avoid undesirable correlations */
262270 return 0;
263271 }
264272
265273
266274 static int math_type (lua_State *L) {
267− luaL_checkany(L, 1);
268275 if (lua_type(L, 1) == LUA_TNUMBER) {
269276 if (lua_isinteger(L, 1))
270277 lua_pushliteral(L, "integer");
271278 else
272279 lua_pushliteral(L, "float");
273280 }
274− else
281+ else {
282+ luaL_checkany(L, 1);
275283 lua_pushnil(L);
284+ }
276285 return 1;
277286 }
278287
@@ -339,9 +348,10 @@ static const luaL_Reg mathlib[] = { · 60 unmodified lines
339348 {"cos", math_cos},
340349 {"deg", math_deg},
341350 {"exp", math_exp},
342− {"ifloor", math_ifloor},
351+ {"tointeger", math_toint},
343352 {"floor", math_floor},
344353 {"fmod", math_fmod},
354+ {"ult", math_ult},
345355 {"log", math_log},
346356 {"max", math_max},
347357 {"min", math_min},
@@ -363,6 +373,11 @@ static const luaL_Reg mathlib[] = { · 15 unmodified lines
363373 {"ldexp", math_ldexp},
364374 {"log10", math_log10},
365375 #endif
376+ /* placeholders */
377+ {"pi", NULL},
378+ {"huge", NULL},
379+ {"maxinteger", NULL},
380+ {"mininteger", NULL},
366381 {NULL, NULL}
367382 };
368383
src/lmem.c+4−6
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lmem.c,v 1.84 2012/05/23 15:41:53 roberto Exp $
2+** $Id: lmem.c,v 1.86 2014/07/15 21:26:50 roberto Exp $
33 ** Interface to Memory Manager
44 ** See Copyright Notice in lua.h
55 */
@@ -83,12 +83,10 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { · 77 unmodified lines
8383 #endif
8484 newblock = (*g->frealloc)(g->ud, block, osize, nsize);
8585 if (newblock == NULL && nsize > 0) {
86− api_check(L, nsize > realosize,
86+ api_check( nsize > realosize,
8787 "realloc cannot fail when shrinking a block");
88− if (g->gcrunning) {
89− luaC_fullgc(L, 1); /* try to free some memory... */
90− newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */
91− }
88+ luaC_fullgc(L, 1); /* try to free some memory... */
89+ newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */
9290 if (newblock == NULL)
9391 luaD_throw(L, LUA_ERRMEM);
9492 }
src/loadlib.c+103−46
@@ -1,5 +1,5 @@
11 /*
2−** $Id: loadlib.c,v 1.113 2014/03/12 20:57:40 roberto Exp $
2+** $Id: loadlib.c,v 1.116 2014/07/29 16:01:00 roberto Exp $
33 ** Dynamic library loader for Lua
44 ** See Copyright Notice in lua.h
55 **
@@ -97,20 +97,33 @@ · 91 unmodified lines
9797
9898 #define LIB_FAIL "open"
9999
100−
101−/* error codes for ll_loadfunc */
102−#define ERRLIB 1
103−#define ERRFUNC 2
104−
105100 #define setprogdir(L) ((void)0)
106101
107102
108103 /*
109104 ** system-dependent functions
110105 */
111−static void ll_unloadlib (void *lib);
112−static void *ll_load (lua_State *L, const char *path, int seeglb);
113−static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym);
106+
107+/*
108+** unload library 'lib'
109+*/
110+static void lsys_unloadlib (void *lib);
111+
112+/*
113+** load C library in file 'path'. If 'seeglb', load with all names in
114+** the library global.
115+** Returns the library; in case of error, returns NULL plus an
116+** error string in the stack.
117+*/
118+static void *lsys_load (lua_State *L, const char *path, int seeglb);
119+
120+/*
121+** Try to find a function named 'sym' in library 'lib'.
122+** Returns the function; in case of error, returns NULL plus an
123+** error string in the stack.
124+*/
125+static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym);
126+
114127
115128
116129
@@ -126,19 +139,19 @@ static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym); · 9 unmodified lines
126139
127140 #include <dlfcn.h>
128141
129−static void ll_unloadlib (void *lib) {
142+static void lsys_unloadlib (void *lib) {
130143 dlclose(lib);
131144 }
132145
133146
134−static void *ll_load (lua_State *L, const char *path, int seeglb) {
147+static void *lsys_load (lua_State *L, const char *path, int seeglb) {
135148 void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : RTLD_LOCAL));
136149 if (lib == NULL) lua_pushstring(L, dlerror());
137150 return lib;
138151 }
139152
140153
141−static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
154+static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
142155 lua_CFunction f = (lua_CFunction)dlsym(lib, sym);
143156 if (f == NULL) lua_pushstring(L, dlerror());
144157 return f;
@@ -190,12 +203,12 @@ static void pusherror (lua_State *L) { · 45 unmodified lines
190203 lua_pushfstring(L, "system error %d\n", error);
191204 }
192205
193−static void ll_unloadlib (void *lib) {
206+static void lsys_unloadlib (void *lib) {
194207 FreeLibrary((HMODULE)lib);
195208 }
196209
197210
198−static void *ll_load (lua_State *L, const char *path, int seeglb) {
211+static void *lsys_load (lua_State *L, const char *path, int seeglb) {
199212 HMODULE lib = LoadLibraryExA(path, NULL, LUA_LLE_FLAGS);
200213 (void)(seeglb); /* not used: symbols are 'global' by default */
201214 if (lib == NULL) pusherror(L);
@@ -203,7 +216,7 @@ static void *ll_load (lua_State *L, const char *path, int seeglb) { · 1 unmodified line
203216 }
204217
205218
206−static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
219+static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
207220 lua_CFunction f = (lua_CFunction)GetProcAddress((HMODULE)lib, sym);
208221 if (f == NULL) pusherror(L);
209222 return f;
@@ -226,19 +239,19 @@ static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { · 16 unmodified lines
226239 #define DLMSG "dynamic libraries not enabled; check your Lua installation"
227240
228241
229−static void ll_unloadlib (void *lib) {
242+static void lsys_unloadlib (void *lib) {
230243 (void)(lib); /* not used */
231244 }
232245
233246
234−static void *ll_load (lua_State *L, const char *path, int seeglb) {
247+static void *lsys_load (lua_State *L, const char *path, int seeglb) {
235248 (void)(path); (void)(seeglb); /* not used */
236249 lua_pushliteral(L, DLMSG);
237250 return NULL;
238251 }
239252
240253
241−static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
254+static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
242255 (void)(lib); (void)(sym); /* not used */
243256 lua_pushliteral(L, DLMSG);
244257 return NULL;
@@ -248,7 +261,10 @@ static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { · 3 unmodified lines
248261 #endif
249262
250263
251−static void *ll_checkclib (lua_State *L, const char *path) {
264+/*
265+** return registry.CLIBS[path]
266+*/
267+static void *checkclib (lua_State *L, const char *path) {
252268 void *plib;
253269 lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
254270 lua_getfield(L, -1, path);
@@ -258,7 +274,11 @@ static void *ll_checkclib (lua_State *L, const char *path) { · 3 unmodified lines
258274 }
259275
260276
261−static void ll_addtoclib (lua_State *L, const char *path, void *plib) {
277+/*
278+** registry.CLIBS[path] = plib -- for queries
279+** registry.CLIBS[#CLIBS + 1] = plib -- also keep a list of all libraries
280+*/
281+static void addtoclib (lua_State *L, const char *path, void *plib) {
262282 lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
263283 lua_pushlightuserdata(L, plib);
264284 lua_pushvalue(L, -1);
@@ -269,33 +289,49 @@ static void ll_addtoclib (lua_State *L, const char *path, void *plib) { · 4 unmodified lines
269289
270290
271291 /*
272−** __gc tag method for CLIBS table: calls 'll_unloadlib' for all lib
292+** __gc tag method for CLIBS table: calls 'lsys_unloadlib' for all lib
273293 ** handles in list CLIBS
274294 */
275295 static int gctm (lua_State *L) {
276− int n = luaL_len(L, 1);
296+ lua_Integer n = luaL_len(L, 1);
277297 for (; n >= 1; n--) { /* for each handle, in reverse order */
278298 lua_rawgeti(L, 1, n); /* get handle CLIBS[n] */
279− ll_unloadlib(lua_touserdata(L, -1));
299+ lsys_unloadlib(lua_touserdata(L, -1));
280300 lua_pop(L, 1); /* pop handle */
281301 }
282302 return 0;
283303 }
284304
285305
286−static int ll_loadfunc (lua_State *L, const char *path, const char *sym) {
287− void *reg = ll_checkclib(L, path); /* check loaded C libraries */
306+
307+/* error codes for 'lookforfunc' */
308+#define ERRLIB 1
309+#define ERRFUNC 2
310+
311+/*
312+** Look for a C function named 'sym' in a dynamically loaded library
313+** 'path'.
314+** First, check whether the library is already loaded; if not, try
315+** to load it.
316+** Then, if 'sym' is '*', return true (as library has been loaded).
317+** Otherwise, look for symbol 'sym' in the library and push a
318+** C function with that symbol.
319+** Return 0 and 'true' or a function in the stack; in case of
320+** errors, return an error code and an error message in the stack.
321+*/
322+static int lookforfunc (lua_State *L, const char *path, const char *sym) {
323+ void *reg = checkclib(L, path); /* check loaded C libraries */
288324 if (reg == NULL) { /* must load library? */
289− reg = ll_load(L, path, *sym == '*');
325+ reg = lsys_load(L, path, *sym == '*'); /* global symbols if 'sym'=='*' */
290326 if (reg == NULL) return ERRLIB; /* unable to load library */
291− ll_addtoclib(L, path, reg);
327+ addtoclib(L, path, reg);
292328 }
293329 if (*sym == '*') { /* loading only library (no function)? */
294330 lua_pushboolean(L, 1); /* return 'true' */
295331 return 0; /* no errors */
296332 }
297333 else {
298− lua_CFunction f = ll_sym(L, reg, sym);
334+ lua_CFunction f = lsys_sym(L, reg, sym);
299335 if (f == NULL)
300336 return ERRFUNC; /* unable to find function */
301337 lua_pushcfunction(L, f); /* else create new function */
@@ -307,7 +343,7 @@ static int ll_loadfunc (lua_State *L, const char *path, const char *sym) { · 5 unmodified lines
307343 static int ll_loadlib (lua_State *L) {
308344 const char *path = luaL_checkstring(L, 1);
309345 const char *init = luaL_checkstring(L, 2);
310− int stat = ll_loadfunc(L, path, init);
346+ int stat = lookforfunc(L, path, init);
311347 if (stat == 0) /* no errors? */
312348 return 1; /* return the loaded function */
313349 else { /* error; error message is on stack top */
@@ -416,21 +452,29 @@ static int searcher_Lua (lua_State *L) { · 102 unmodified lines
416452 }
417453
418454
455+/*
456+** Try to find a load function for module 'modname' at file 'filename'.
457+** First, change '.' to '_' in 'modname'; then, if 'modname' has
458+** the form X-Y (that is, it has an "ignore mark"), build a function
459+** name "luaopen_X" and look for it. (For compatibility, if that
460+** fails, it also tries "luaopen_Y".) If there is no ignore mark,
461+** look for a function named "luaopen_modname".
462+*/
419463 static int loadfunc (lua_State *L, const char *filename, const char *modname) {
420− const char *funcname;
464+ const char *openfunc;
421465 const char *mark;
422466 modname = luaL_gsub(L, modname, ".", LUA_OFSEP);
423467 mark = strchr(modname, *LUA_IGMARK);
424468 if (mark) {
425469 int stat;
426− funcname = lua_pushlstring(L, modname, mark - modname);
427− funcname = lua_pushfstring(L, LUA_POF"%s", funcname);
428− stat = ll_loadfunc(L, filename, funcname);
470+ openfunc = lua_pushlstring(L, modname, mark - modname);
471+ openfunc = lua_pushfstring(L, LUA_POF"%s", openfunc);
472+ stat = lookforfunc(L, filename, openfunc);
429473 if (stat != ERRFUNC) return stat;
430474 modname = mark + 1; /* else go ahead and try old-style name */
431475 }
432− funcname = lua_pushfstring(L, LUA_POF"%s", modname);
433− return ll_loadfunc(L, filename, funcname);
476+ openfunc = lua_pushfstring(L, LUA_POF"%s", modname);
477+ return lookforfunc(L, filename, openfunc);
434478 }
435479
436480
@@ -655,6 +699,12 @@ static const luaL_Reg pk_funcs[] = { · 218 unmodified lines
655699 #if defined(LUA_COMPAT_MODULE)
656700 {"seeall", ll_seeall},
657701 #endif
702+ /* placeholders */
703+ {"preload", NULL},
704+ {"cpath", NULL},
705+ {"path", NULL},
706+ {"searchers", NULL},
707+ {"loaded", NULL},
658708 {NULL, NULL}
659709 };
660710
@@ -680,24 +730,31 @@ static void createsearcherstable (lua_State *L) { · 19 unmodified lines
680730 lua_pushcclosure(L, searchers[i], 1);
681731 lua_rawseti(L, -2, i+1);
682732 }
733+#if defined(LUA_COMPAT_LOADERS)
734+ lua_pushvalue(L, -1); /* make a copy of 'searchers' table */
735+ lua_setfield(L, -3, "loaders"); /* put it in field `loaders' */
736+#endif
737+ lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */
683738 }
684739
685740
686−LUAMOD_API int luaopen_package (lua_State *L) {
687− /* create table CLIBS to keep track of loaded C libraries */
688− luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS);
689− lua_createtable(L, 0, 1); /* metatable for CLIBS */
741+/*
742+** create table CLIBS to keep track of loaded C libraries,
743+** setting a finalizer to close all libraries when closing state.
744+*/
745+static void createclibstable (lua_State *L) {
746+ luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); /* create CLIBS table */
747+ lua_createtable(L, 0, 1); /* create metatable for CLIBS */
690748 lua_pushcfunction(L, gctm);
691749 lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */
692750 lua_setmetatable(L, -2);
693− /* create `package' table */
694− luaL_newlib(L, pk_funcs);
751+}
752+
753+
754+LUAMOD_API int luaopen_package (lua_State *L) {
755+ createclibstable(L);
756+ luaL_newlib(L, pk_funcs); /* create `package' table */
695757 createsearcherstable(L);
696−#if defined(LUA_COMPAT_LOADERS)
697− lua_pushvalue(L, -1); /* make a copy of 'searchers' table */
698− lua_setfield(L, -3, "loaders"); /* put it in field `loaders' */
699−#endif
700− lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */
701758 /* set field 'path' */
702759 setpath(L, "path", LUA_PATHVARVERSION, LUA_PATH_VAR, LUA_PATH_DEFAULT);
703760 /* set field 'cpath' */
src/lobject.c+34−9
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lobject.c,v 2.86 2014/05/12 21:44:17 roberto Exp $
2+** $Id: lobject.c,v 2.88 2014/07/30 14:00:14 roberto Exp $
33 ** Some generic functions over Lua objects
44 ** See Copyright Notice in lua.h
55 */
@@ -256,7 +256,7 @@ static const char *l_str2d (const char *s, lua_Number *result) { · 250 unmodified lines
256256 char *endptr;
257257 if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */
258258 return NULL;
259− else if (strpbrk(s, "xX")) /* hexa? */
259+ else if (strpbrk(s, "xX")) /* hex? */
260260 *result = lua_strx2number(s, &endptr);
261261 else
262262 *result = lua_str2number(s, &endptr);
@@ -273,7 +273,7 @@ static const char *l_str2int (const char *s, lua_Integer *result) { · 10 unmodified lines
273273 while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
274274 neg = isneg(&s);
275275 if (s[0] == '0' &&
276− (s[1] == 'x' || s[1] == 'X')) { /* hexa? */
276+ (s[1] == 'x' || s[1] == 'X')) { /* hex? */
277277 s += 2; /* skip '0x' */
278278 for (; lisxdigit(cast_uchar(*s)); s++) {
279279 a = a * 16 + luaO_hexavalue(cast_uchar(*s));
@@ -327,6 +327,32 @@ int luaO_utf8esc (char *buff, unsigned int x) { · 47 unmodified lines
327327 }
328328
329329
330+/* maximum length of the conversion of a number to a string */
331+#define MAXNUMBER2STR 50
332+
333+
334+/*
335+** Convert a number object to a string
336+*/
337+void luaO_tostring (lua_State *L, StkId obj) {
338+ char buff[MAXNUMBER2STR];
339+ size_t len;
340+ lua_assert(ttisnumber(obj));
341+ if (ttisinteger(obj))
342+ len = lua_integer2str(buff, ivalue(obj));
343+ else {
344+ len = lua_number2str(buff, fltvalue(obj));
345+#if !defined(LUA_COMPAT_FLOATSTRING)
346+ if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */
347+ buff[len++] = '.';
348+ buff[len++] = '0'; /* adds '.0' to result */
349+ }
350+#endif
351+ }
352+ setsvalue2s(L, obj, luaS_newlstr(L, buff, len));
353+}
354+
355+
330356 static void pushstr (lua_State *L, const char *str, size_t l) {
331357 setsvalue2s(L, L->top++, luaS_newlstr(L, str, l));
332358 }
@@ -349,24 +375,23 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { · 16 unmodified lines
349375 break;
350376 }
351377 case 'c': {
352− char buff;
353− buff = cast(char, va_arg(argp, int));
378+ char buff = cast(char, va_arg(argp, int));
354379 pushstr(L, &buff, 1);
355380 break;
356381 }
357382 case 'd': {
358− setivalue(L->top++, cast_int(va_arg(argp, int)));
359− luaV_tostring(L, L->top - 1);
383+ setivalue(L->top++, va_arg(argp, int));
384+ luaO_tostring(L, L->top - 1);
360385 break;
361386 }
362387 case 'I': {
363388 setivalue(L->top++, cast(lua_Integer, va_arg(argp, l_uacInt)));
364− luaV_tostring(L, L->top - 1);
389+ luaO_tostring(L, L->top - 1);
365390 break;
366391 }
367392 case 'f': {
368393 setfltvalue(L->top++, cast_num(va_arg(argp, l_uacNumber)));
369− luaV_tostring(L, L->top - 1);
394+ luaO_tostring(L, L->top - 1);
370395 break;
371396 }
372397 case 'p': {
src/lobject.h+69−38
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lobject.h,v 2.94 2014/06/19 18:39:36 roberto Exp $
2+** $Id: lobject.h,v 2.101 2014/07/30 14:00:14 roberto Exp $
33 ** Type definitions for Lua objects
44 ** See Copyright Notice in lua.h
55 */
@@ -69,9 +69,9 @@ · 63 unmodified lines
6969
7070
7171 /*
72−** Union of all collectable objects
72+** Common type for all collectable objects
7373 */
74−typedef union GCObject GCObject;
74+typedef struct GCObject GCObject;
7575
7676
7777 /*
@@ -82,11 +82,11 @@ typedef union GCObject GCObject; · 4 unmodified lines
8282
8383
8484 /*
85−** Common header in struct form
85+** Common type has only the common header
8686 */
87−typedef struct GCheader {
87+struct GCObject {
8888 CommonHeader;
89−} GCheader;
89+};
9090
9191
9292
@@ -156,10 +156,8 @@ typedef struct lua_TValue TValue; · 63 unmodified lines
156156 #define fltvalue(o) check_exp(ttisfloat(o), val_(o).n)
157157 #define gcvalue(o) check_exp(iscollectable(o), val_(o).gc)
158158 #define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
159−#define rawtsvalue(o) check_exp(ttisstring(o), rawgco2ts(val_(o).gc))
160−#define tsvalue(o) (&rawtsvalue(o)->tsv)
161−#define rawuvalue(o) check_exp(ttisfulluserdata(o), rawgco2u(val_(o).gc))
162−#define uvalue(o) (&rawuvalue(o)->uv)
159+#define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc))
160+#define uvalue(o) check_exp(ttisfulluserdata(o), gco2u(val_(o).gc))
163161 #define clvalue(o) check_exp(ttisclosure(o), gco2cl(val_(o).gc))
164162 #define clLvalue(o) check_exp(ttisLclosure(o), gco2lcl(val_(o).gc))
165163 #define clCvalue(o) check_exp(ttisCclosure(o), gco2ccl(val_(o).gc))
@@ -177,7 +175,7 @@ typedef struct lua_TValue TValue; · 11 unmodified lines
177175
178176
179177 /* Macros for internal tests */
180−#define righttt(obj) (ttype(obj) == gcvalue(obj)->gch.tt)
178+#define righttt(obj) (ttype(obj) == gcvalue(obj)->tt)
181179
182180 #define checkliveness(g,obj) \
183181 lua_longassert(!iscollectable(obj) || \
@@ -206,11 +204,11 @@ typedef struct lua_TValue TValue; · 22 unmodified lines
206204
207205 #define setgcovalue(L,obj,x) \
208206 { TValue *io = (obj); GCObject *i_g=(x); \
209− val_(io).gc = i_g; settt_(io, ctb(gch(i_g)->tt)); }
207+ val_(io).gc = i_g; settt_(io, ctb(i_g->tt)); }
210208
211209 #define setsvalue(L,obj,x) \
212210 { TValue *io = (obj); TString *x_ = (x); \
213− val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tsv.tt)); \
211+ val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \
214212 checkliveness(G(L),io); }
215213
216214 #define setuvalue(L,obj,x) \
@@ -243,8 +241,7 @@ typedef struct lua_TValue TValue; · 26 unmodified lines
243241
244242
245243 #define setobj(L,obj1,obj2) \
246− { const TValue *io2=(obj2); TValue *io1=(obj1); \
247− io1->value_ = io2->value_; io1->tt_ = io2->tt_; \
244+ { TValue *io1=(obj1); *io1 = *(obj2); \
248245 (void)L; checkliveness(G(L),io1); }
249246
250247
@@ -299,50 +296,76 @@ typedef TValue *StkId; /* index to stack elements */ · 48 unmodified lines
299296
300297 /*
301298 ** Header for string value; string bytes follow the end of this structure
299+** (aligned according to 'UTString'; see next).
302300 */
303−typedef union TString {
304− L_Umaxalign dummy; /* ensures maximum alignment for strings */
305− struct {
306− CommonHeader;
307− lu_byte extra; /* reserved words for short strings; "has hash" for longs */
308− unsigned int hash;
309− size_t len; /* number of characters in string */
310− union TString *hnext; /* linked list for hash table */
311− } tsv;
301+typedef struct TString {
302+ CommonHeader;
303+ lu_byte extra; /* reserved words for short strings; "has hash" for longs */
304+ unsigned int hash;
305+ size_t len; /* number of characters in string */
306+ struct TString *hnext; /* linked list for hash table */
312307 } TString;
313308
314309
315−/* get the actual string (array of bytes) from a TString */
316−#define getstr(ts) cast(const char *, (ts) + 1)
310+/*
311+** Ensures that address after this type is always fully aligned.
312+*/
313+typedef union UTString {
314+ L_Umaxalign dummy; /* ensures maximum alignment for strings */
315+ TString tsv;
316+} UTString;
317+
318+
319+/*
320+** Get the actual string (array of bytes) from a 'TString'.
321+** (Access to 'extra' ensures that value is really a 'TString'.)
322+*/
323+#define getaddrstr(ts) (cast(char *, (ts)) + sizeof(UTString))
324+#define getstr(ts) \
325+ check_exp(sizeof((ts)->extra), cast(const char*, getaddrstr(ts)))
317326
318327 /* get the actual string (array of bytes) from a Lua value */
319−#define svalue(o) getstr(rawtsvalue(o))
328+#define svalue(o) getstr(tsvalue(o))
320329
321330
322331 /*
323332 ** Header for userdata; memory area follows the end of this structure
333+** (aligned according to 'UUdata'; see next).
324334 */
325−typedef union Udata {
326− L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
327− struct {
328− CommonHeader;
329− lu_byte ttuv_; /* user value's tag */
330− struct Table *metatable;
331− size_t len; /* number of bytes */
332− union Value user_; /* user value */
333− } uv;
335+typedef struct Udata {
336+ CommonHeader;
337+ lu_byte ttuv_; /* user value's tag */
338+ struct Table *metatable;
339+ size_t len; /* number of bytes */
340+ union Value user_; /* user value */
334341 } Udata;
335342
336343
344+/*
345+** Ensures that address after this type is always fully aligned.
346+*/
347+typedef union UUdata {
348+ L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
349+ Udata uv;
350+} UUdata;
351+
352+
353+/*
354+** Get the address of memory block inside 'Udata'.
355+** (Access to 'ttuv_' ensures that value is really a 'Udata'.)
356+*/
357+#define getudatamem(u) \
358+ check_exp(sizeof((u)->ttuv_), (cast(char*, (u)) + sizeof(UUdata)))
359+
337360 #define setuservalue(L,u,o) \
338361 { const TValue *io=(o); Udata *iu = (u); \
339− iu->uv.user_ = io->value_; iu->uv.ttuv_ = io->tt_; \
362+ iu->user_ = io->value_; iu->ttuv_ = io->tt_; \
340363 checkliveness(G(L),io); }
341364
342365
343366 #define getuservalue(L,u,o) \
344367 { TValue *io=(o); const Udata *iu = (u); \
345− io->value_ = iu->uv.user_; io->tt_ = iu->uv.ttuv_; \
368+ io->value_ = iu->user_; io->tt_ = iu->ttuv_; \
346369 checkliveness(G(L),io); }
347370
348371
@@ -447,6 +470,13 @@ typedef union TKey { · 98 unmodified lines
447470 } TKey;
448471
449472
473+/* copy a value into a key without messing up field 'next' */
474+#define setkey(L,key,obj) \
475+ { TKey *k_=(key); const TValue *io_=(obj); \
476+ k_->nk.value_ = io_->value_; k_->nk.tt_ = io_->tt_; \
477+ (void)L; checkliveness(G(L),io_); }
478+
479+
450480 typedef struct Node {
451481 TValue i_val;
452482 TKey i_key;
@@ -497,6 +527,7 @@ LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1, · 44 unmodified lines
497527 const TValue *p2, TValue *res);
498528 LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o);
499529 LUAI_FUNC int luaO_hexavalue (int c);
530+LUAI_FUNC void luaO_tostring (lua_State *L, StkId obj);
500531 LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
501532 va_list argp);
502533 LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
src/lparser.c+1−1
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lparser.c,v 2.139 2014/06/19 18:27:20 roberto Exp $
2+** $Id: lparser.c,v 2.142 2014/07/21 16:02:10 roberto Exp $
33 ** Lua Parser
44 ** See Copyright Notice in lua.h
55 */
src/lstate.c+7−5
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lstate.c,v 2.121 2014/02/18 13:46:26 roberto Exp $
2+** $Id: lstate.c,v 2.125 2014/07/24 16:17:56 roberto Exp $
33 ** Global State
44 ** See Copyright Notice in lua.h
55 */
@@ -53,9 +53,7 @@ · 47 unmodified lines
5353 ** thread state + extra space
5454 */
5555 typedef struct LX {
56−#if defined(LUAI_EXTRASPACE)
57− char buff[LUAI_EXTRASPACE];
58−#endif
56+ lu_byte extra_[LUA_EXTRASPACE];
5957 lua_State l;
6058 } LX;
6159
@@ -252,7 +250,7 @@ static void close_state (lua_State *L) { · 190 unmodified lines
252250
253251
254252 LUA_API lua_State *lua_newthread (lua_State *L) {
255− global_State *g = G(L);
253+ global_State *g = G(L);
256254 lua_State *L1;
257255 lua_lock(L);
258256 luaC_checkGC(L);
@@ -263,6 +261,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) { · 4 unmodified lines
263261 /* link it on list 'allgc' */
264262 L1->next = g->allgc;
265263 g->allgc = obj2gco(L1);
264+ /* anchor it on L stack */
266265 setthvalue(L, L->top, L1);
267266 api_incr_top(L);
268267 preinit_thread(L1, g);
@@ -270,6 +269,9 @@ LUA_API lua_State *lua_newthread (lua_State *L) { · 1 unmodified line
270269 L1->basehookcount = L->basehookcount;
271270 L1->hook = L->hook;
272271 resethookcount(L1);
272+ /* initialize L1 extra space */
273+ memcpy(lua_getextraspace(L1), lua_getextraspace(g->mainthread),
274+ LUA_EXTRASPACE);
273275 luai_userstatethread(L, L1);
274276 stack_init(L1, L); /* init stack */
275277 lua_unlock(L);
src/lstate.h+33−33
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lstate.h,v 2.107 2014/06/12 19:07:30 roberto Exp $
2+** $Id: lstate.h,v 2.114 2014/07/23 17:15:43 roberto Exp $
33 ** Global State
44 ** See Copyright Notice in lua.h
55 */
@@ -60,9 +60,6 @@ typedef struct CallInfo { · 54 unmodified lines
6060 StkId func; /* function index in the stack */
6161 StkId top; /* top for this function */
6262 struct CallInfo *previous, *next; /* dynamic call link */
63− ptrdiff_t extra;
64− short nresults; /* expected number of results from this function */
65− lu_byte callstatus;
6663 union {
6764 struct { /* only for Lua functions */
6865 StkId base; /* base for this function */
@@ -71,9 +68,12 @@ typedef struct CallInfo { · 2 unmodified lines
7168 struct { /* only for C functions */
7269 lua_KFunction k; /* continuation in case of yields */
7370 ptrdiff_t old_errfunc;
74− int ctx; /* context info. in case of yields */
71+ lua_Ctx ctx; /* context info. in case of yields */
7572 } c;
7673 } u;
74+ ptrdiff_t extra;
75+ short nresults; /* expected number of results from this function */
76+ lu_byte callstatus;
7777 } CallInfo;
7878
7979
@@ -149,20 +149,20 @@ struct lua_State { · 69 unmodified lines
149149 const Instruction *oldpc; /* last pc traced */
150150 StkId stack_last; /* last free slot in the stack */
151151 StkId stack; /* stack base */
152− int stacksize;
153− unsigned short nny; /* number of non-yieldable calls in stack */
154− unsigned short nCcalls; /* number of nested C calls */
155− lu_byte hookmask;
156− lu_byte allowhook;
157− int basehookcount;
158− int hookcount;
159− lua_Hook hook;
160152 UpVal *openupval; /* list of open upvalues in this stack */
161153 GCObject *gclist;
162154 struct lua_State *twups; /* list of threads with open upvalues */
163155 struct lua_longjmp *errorJmp; /* current error recover point */
164− ptrdiff_t errfunc; /* current error handling function (stack index) */
165156 CallInfo base_ci; /* CallInfo for first level (C calling Lua) */
157+ lua_Hook hook;
158+ ptrdiff_t errfunc; /* current error handling function (stack index) */
159+ int stacksize;
160+ int basehookcount;
161+ int hookcount;
162+ unsigned short nny; /* number of non-yieldable calls in stack */
163+ unsigned short nCcalls; /* number of nested C calls */
164+ lu_byte hookmask;
165+ lu_byte allowhook;
166166 };
167167
168168
@@ -170,12 +170,12 @@ struct lua_State { · 1 unmodified line
170170
171171
172172 /*
173−** Union of all collectable objects
173+** Union of all collectable objects (only for conversions)
174174 */
175−union GCObject {
176− GCheader gch; /* common header */
177− union TString ts;
178− union Udata u;
175+union GCUnion {
176+ GCObject gc; /* common header */
177+ struct TString ts;
178+ struct Udata u;
179179 union Closure cl;
180180 struct Table h;
181181 struct Proto p;
@@ -183,24 +183,24 @@ union GCObject { · 1 unmodified line
183183 };
184184
185185
186−#define gch(o) (&(o)->gch)
186+#define cast_u(o) cast(union GCUnion *, (o))
187187
188188 /* macros to convert a GCObject into a specific value */
189−#define rawgco2ts(o) \
190− check_exp(novariant((o)->gch.tt) == LUA_TSTRING, &((o)->ts))
191−#define gco2ts(o) (&rawgco2ts(o)->tsv)
192−#define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u))
193−#define gco2u(o) (&rawgco2u(o)->uv)
194−#define gco2lcl(o) check_exp((o)->gch.tt == LUA_TLCL, &((o)->cl.l))
195−#define gco2ccl(o) check_exp((o)->gch.tt == LUA_TCCL, &((o)->cl.c))
189+#define gco2ts(o) \
190+ check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts))
191+#define gco2u(o) check_exp((o)->tt == LUA_TUSERDATA, &((cast_u(o))->u))
192+#define gco2lcl(o) check_exp((o)->tt == LUA_TLCL, &((cast_u(o))->cl.l))
193+#define gco2ccl(o) check_exp((o)->tt == LUA_TCCL, &((cast_u(o))->cl.c))
196194 #define gco2cl(o) \
197− check_exp(novariant((o)->gch.tt) == LUA_TFUNCTION, &((o)->cl))
198−#define gco2t(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h))
199−#define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p))
200−#define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th))
195+ check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl))
196+#define gco2t(o) check_exp((o)->tt == LUA_TTABLE, &((cast_u(o))->h))
197+#define gco2p(o) check_exp((o)->tt == LUA_TPROTO, &((cast_u(o))->p))
198+#define gco2th(o) check_exp((o)->tt == LUA_TTHREAD, &((cast_u(o))->th))
199+
201200
202−/* macro to convert any Lua object into a GCObject */
203−#define obj2gco(v) (cast(GCObject *, (v)))
201+/* macro to convert a Lua object into a GCObject */
202+#define obj2gco(v) \
203+ check_exp(novariant((v)->tt) < LUA_TDEADKEY, (&(cast_u(v)->gc)))
204204
205205
206206 /* actual number of total bytes allocated */
src/lstring.c+26−26
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lstring.c,v 2.40 2014/06/18 22:59:29 roberto Exp $
2+** $Id: lstring.c,v 2.44 2014/07/21 16:02:10 roberto Exp $
33 ** String table (keeps all strings handled by Lua)
44 ** See Copyright Notice in lua.h
55 */
@@ -34,10 +34,10 @@ · 28 unmodified lines
3434 ** equality for long strings
3535 */
3636 int luaS_eqlngstr (TString *a, TString *b) {
37− size_t len = a->tsv.len;
38− lua_assert(a->tsv.tt == LUA_TLNGSTR && b->tsv.tt == LUA_TLNGSTR);
37+ size_t len = a->len;
38+ lua_assert(a->tt == LUA_TLNGSTR && b->tt == LUA_TLNGSTR);
3939 return (a == b) || /* same instance or... */
40− ((len == b->tsv.len) && /* equal length and ... */
40+ ((len == b->len) && /* equal length and ... */
4141 (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */
4242 }
4343
@@ -67,9 +67,9 @@ void luaS_resize (lua_State *L, int newsize) { · 23 unmodified lines
6767 TString *p = tb->hash[i];
6868 tb->hash[i] = NULL;
6969 while (p) { /* for each node in the list */
70− TString *hnext = p->tsv.hnext; /* save next */
71− unsigned int h = lmod(p->tsv.hash, newsize); /* new position */
72− p->tsv.hnext = tb->hash[h]; /* chain it */
70+ TString *hnext = p->hnext; /* save next */
71+ unsigned int h = lmod(p->hash, newsize); /* new position */
72+ p->hnext = tb->hash[h]; /* chain it */
7373 tb->hash[h] = p;
7474 p = hnext;
7575 }
@@ -92,24 +92,24 @@ static TString *createstrobj (lua_State *L, const char *str, size_t l, · 16 unmodified lines
9292 TString *ts;
9393 GCObject *o;
9494 size_t totalsize; /* total size of TString object */
95− totalsize = sizeof(TString) + ((l + 1) * sizeof(char));
95+ totalsize = sizelstring(l);
9696 o = luaC_newobj(L, tag, totalsize);
97− ts = rawgco2ts(o);
98− ts->tsv.len = l;
99− ts->tsv.hash = h;
100− ts->tsv.extra = 0;
101− memcpy(ts+1, str, l*sizeof(char));
102− ((char *)(ts+1))[l] = '\0'; /* ending 0 */
97+ ts = gco2ts(o);
98+ ts->len = l;
99+ ts->hash = h;
100+ ts->extra = 0;
101+ memcpy(getaddrstr(ts), str, l * sizeof(char));
102+ getaddrstr(ts)[l] = '\0'; /* ending 0 */
103103 return ts;
104104 }
105105
106106
107107 void luaS_remove (lua_State *L, TString *ts) {
108108 stringtable *tb = &G(L)->strt;
109− TString **p = &tb->hash[lmod(ts->tsv.hash, tb->size)];
109+ TString **p = &tb->hash[lmod(ts->hash, tb->size)];
110110 while (*p != ts) /* find previous element */
111− p = &(*p)->tsv.hnext;
112− *p = (*p)->tsv.hnext; /* remove element from its list */
111+ p = &(*p)->hnext;
112+ *p = (*p)->hnext; /* remove element from its list */
113113 tb->nuse--;
114114 }
115115
@@ -122,12 +122,12 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) { · 6 unmodified lines
122122 global_State *g = G(L);
123123 unsigned int h = luaS_hash(str, l, g->seed);
124124 TString **list = &g->strt.hash[lmod(h, g->strt.size)];
125− for (ts = *list; ts != NULL; ts = ts->tsv.hnext) {
126− if (l == ts->tsv.len &&
125+ for (ts = *list; ts != NULL; ts = ts->hnext) {
126+ if (l == ts->len &&
127127 (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
128128 /* found! */
129− if (isdead(g, obj2gco(ts))) /* dead (but not collected yet)? */
130− changewhite(obj2gco(ts)); /* resurrect it */
129+ if (isdead(g, ts)) /* dead (but not collected yet)? */
130+ changewhite(ts); /* resurrect it */
131131 return ts;
132132 }
133133 }
@@ -136,7 +136,7 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) { · 2 unmodified lines
136136 list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */
137137 }
138138 ts = createstrobj(L, str, l, LUA_TSHRSTR, h);
139− ts->tsv.hnext = *list;
139+ ts->hnext = *list;
140140 *list = ts;
141141 g->strt.nuse++;
142142 return ts;
@@ -170,10 +170,10 @@ Udata *luaS_newudata (lua_State *L, size_t s) { · 27 unmodified lines
170170 GCObject *o;
171171 if (s > MAX_SIZE - sizeof(Udata))
172172 luaM_toobig(L);
173− o = luaC_newobj(L, LUA_TUSERDATA, sizeof(Udata) + s);
174− u = rawgco2u(o);
175− u->uv.len = s;
176− u->uv.metatable = NULL;
173+ o = luaC_newobj(L, LUA_TUSERDATA, sizeludata(s));
174+ u = gco2u(o);
175+ u->len = s;
176+ u->metatable = NULL;
177177 setuservalue(L, u, luaO_nilobject);
178178 return u;
179179 }
src/lstring.h+7−5
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lstring.h,v 1.54 2014/03/19 18:51:42 roberto Exp $
2+** $Id: lstring.h,v 1.56 2014/07/18 14:46:47 roberto Exp $
33 ** String table (keep all strings handled by Lua)
44 ** See Copyright Notice in lua.h
55 */
@@ -12,9 +12,11 @@ · 6 unmodified lines
1212 #include "lstate.h"
1313
1414
15−#define sizestring(s) (sizeof(union TString)+((s)->len+1)*sizeof(char))
15+#define sizelstring(l) (sizeof(union UTString) + ((l) + 1) * sizeof(char))
16+#define sizestring(s) sizelstring((s)->len)
1617
17−#define sizeudata(u) (sizeof(union Udata)+(u)->len)
18+#define sizeludata(l) (sizeof(union UUdata) + (l))
19+#define sizeudata(u) sizeludata((u)->len)
1820
1921 #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \
2022 (sizeof(s)/sizeof(char))-1))
@@ -23,13 +25,13 @@ · 2 unmodified lines
2325 /*
2426 ** test whether a string is a reserved word
2527 */
26−#define isreserved(s) ((s)->tsv.tt == LUA_TSHRSTR && (s)->tsv.extra > 0)
28+#define isreserved(s) ((s)->tt == LUA_TSHRSTR && (s)->extra > 0)
2729
2830
2931 /*
3032 ** equality for short strings, which are always internalized
3133 */
32−#define eqshrstr(a,b) check_exp((a)->tsv.tt == LUA_TSHRSTR, (a) == (b))
34+#define eqshrstr(a,b) check_exp((a)->tt == LUA_TSHRSTR, (a) == (b))
3335
3436
3537 LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed);
src/lstrlib.c+12−9
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lstrlib.c,v 1.198 2014/04/27 14:42:26 roberto Exp $
2+** $Id: lstrlib.c,v 1.200 2014/07/30 13:59:24 roberto Exp $
33 ** Standard library for string operations and pattern-matching
44 ** See Copyright Notice in lua.h
55 */
@@ -59,7 +59,7 @@ static int str_sub (lua_State *L) { · 53 unmodified lines
5959 if (start < 1) start = 1;
6060 if (end > (lua_Integer)l) end = l;
6161 if (start <= end)
62− lua_pushlstring(L, s + start - 1, end - start + 1);
62+ lua_pushlstring(L, s + start - 1, (size_t)(end - start + 1));
6363 else lua_pushliteral(L, "");
6464 return 1;
6565 }
@@ -119,7 +119,7 @@ static int str_rep (lua_State *L) { · 53 unmodified lines
119119 else if (l + lsep < l || l + lsep > MAXSIZE / n) /* may overflow? */
120120 return luaL_error(L, "resulting string too large");
121121 else {
122− size_t totallen = n * l + (n - 1) * lsep;
122+ size_t totallen = (size_t)n * l + (size_t)(n - 1) * lsep;
123123 luaL_Buffer b;
124124 char *p = luaL_buffinitsize(L, &b, totallen);
125125 while (n-- > 1) { /* first n-1 copies (followed by separator) */
@@ -594,7 +594,7 @@ static int str_find_aux (lua_State *L, int find) { · 468 unmodified lines
594594 /* explicit request or no special characters? */
595595 if (find && (lua_toboolean(L, 4) || nospecials(p, lp))) {
596596 /* do a plain search */
597− const char *s2 = lmemfind(s + init - 1, ls - init + 1, p, lp);
597+ const char *s2 = lmemfind(s + init - 1, ls - (size_t)init + 1, p, lp);
598598 if (s2) {
599599 lua_pushinteger(L, s2 - s + 1);
600600 lua_pushinteger(L, s2 - s + lp);
@@ -685,7 +685,8 @@ static int gmatch (lua_State *L) { · 84 unmodified lines
685685 static void add_s (MatchState *ms, luaL_Buffer *b, const char *s,
686686 const char *e) {
687687 size_t l, i;
688− const char *news = lua_tolstring(ms->L, 3, &l);
688+ lua_State *L = ms->L;
689+ const char *news = lua_tolstring(L, 3, &l);
689690 for (i = 0; i < l; i++) {
690691 if (news[i] != L_ESC)
691692 luaL_addchar(b, news[i]);
@@ -693,14 +694,16 @@ static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, · 1 unmodified line
693694 i++; /* skip ESC */
694695 if (!isdigit(uchar(news[i]))) {
695696 if (news[i] != L_ESC)
696− luaL_error(ms->L, "invalid use of " LUA_QL("%c")
697− " in replacement string", L_ESC);
697+ luaL_error(L, "invalid use of " LUA_QL("%c")
698+ " in replacement string", L_ESC);
698699 luaL_addchar(b, news[i]);
699700 }
700701 else if (news[i] == '0')
701702 luaL_addlstring(b, s, e - s);
702703 else {
703704 push_onecapture(ms, news[i] - '1', s, e);
705+ luaL_tolstring(L, -1, NULL); /* if number, convert it to string */
706+ lua_remove(L, -2); /* remove original value */
704707 luaL_addvalue(b); /* add capture to accumulated result */
705708 }
706709 }
@@ -744,9 +747,9 @@ static int str_gsub (lua_State *L) { · 37 unmodified lines
744747 const char *src = luaL_checklstring(L, 1, &srcl);
745748 const char *p = luaL_checklstring(L, 2, &lp);
746749 int tr = lua_type(L, 3);
747− size_t max_s = luaL_optinteger(L, 4, srcl+1);
750+ lua_Integer max_s = luaL_optinteger(L, 4, srcl + 1);
748751 int anchor = (*p == '^');
749− size_t n = 0;
752+ lua_Integer n = 0;
750753 MatchState ms;
751754 luaL_Buffer b;
752755 luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING ||
src/ltable.c+15−14
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ltable.c,v 2.90 2014/06/18 22:59:29 roberto Exp $
2+** $Id: ltable.c,v 2.93 2014/07/29 16:22:24 roberto Exp $
33 ** Lua tables (hash)
44 ** See Copyright Notice in lua.h
55 */
@@ -52,7 +52,7 @@ · 46 unmodified lines
5252
5353 #define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t))))
5454
55−#define hashstr(t,str) hashpow2(t, (str)->tsv.hash)
55+#define hashstr(t,str) hashpow2(t, (str)->hash)
5656 #define hashboolean(t,p) hashpow2(t, p)
5757 #define hashint(t,i) hashpow2(t, i)
5858
@@ -64,7 +64,7 @@ · 5 unmodified lines
6464 #define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1))))
6565
6666
67−#define hashpointer(t,p) hashmod(t, IntPoint(p))
67+#define hashpointer(t,p) hashmod(t, point2int(p))
6868
6969
7070 #define dummynode (&dummynode_)
@@ -116,14 +116,14 @@ static Node *mainposition (const Table *t, const TValue *key) { · 45 unmodified lines
116116 case LUA_TNUMFLT:
117117 return hashfloat(t, fltvalue(key));
118118 case LUA_TSHRSTR:
119− return hashstr(t, rawtsvalue(key));
119+ return hashstr(t, tsvalue(key));
120120 case LUA_TLNGSTR: {
121− TString *s = rawtsvalue(key);
122− if (s->tsv.extra == 0) { /* no hash? */
123− s->tsv.hash = luaS_hash(getstr(s), s->tsv.len, s->tsv.hash);
124− s->tsv.extra = 1; /* now it has its hash */
121+ TString *s = tsvalue(key);
122+ if (s->extra == 0) { /* no hash? */
123+ s->hash = luaS_hash(getstr(s), s->len, s->hash);
124+ s->extra = 1; /* now it has its hash */
125125 }
126− return hashstr(t, rawtsvalue(key));
126+ return hashstr(t, tsvalue(key));
127127 }
128128 case LUA_TBOOLEAN:
129129 return hashboolean(t, bvalue(key));
@@ -309,7 +309,7 @@ static void setnodevector (lua_State *L, Table *t, int size) { · 179 unmodified lines
309309 for (i=0; i<size; i++) {
310310 Node *n = gnode(t, i);
311311 gnext(n) = 0;
312− setnilvalue(gkey(n));
312+ setnilvalue(wgkey(n));
313313 setnilvalue(gval(n));
314314 }
315315 }
@@ -466,7 +466,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { · 150 unmodified lines
466466 mp = f;
467467 }
468468 }
469− setobj2t(L, gkey(mp), key);
469+ setkey(L, &mp->i_key, key);
470470 luaC_barrierback(L, t, key);
471471 lua_assert(ttisnil(gval(mp)));
472472 return gval(mp);
@@ -501,9 +501,10 @@ const TValue *luaH_getint (Table *t, lua_Integer key) { · 28 unmodified lines
501501 */
502502 const TValue *luaH_getstr (Table *t, TString *key) {
503503 Node *n = hashstr(t, key);
504− lua_assert(key->tsv.tt == LUA_TSHRSTR);
504+ lua_assert(key->tt == LUA_TSHRSTR);
505505 for (;;) { /* check whether `key' is somewhere in the chain */
506− if (ttisshrstring(gkey(n)) && eqshrstr(rawtsvalue(gkey(n)), key))
506+ const TValue *k = gkey(n);
507+ if (ttisshrstring(k) && eqshrstr(tsvalue(k), key))
507508 return gval(n); /* that's it */
508509 else {
509510 int nx = gnext(n);
@@ -520,7 +521,7 @@ const TValue *luaH_getstr (Table *t, TString *key) { · 10 unmodified lines
520521 */
521522 const TValue *luaH_get (Table *t, const TValue *key) {
522523 switch (ttype(key)) {
523− case LUA_TSHRSTR: return luaH_getstr(t, rawtsvalue(key));
524+ case LUA_TSHRSTR: return luaH_getstr(t, tsvalue(key));
524525 case LUA_TNUMINT: return luaH_getint(t, ivalue(key));
525526 case LUA_TNIL: return luaO_nilobject;
526527 case LUA_TNUMFLT: {
src/ltable.h+7−2
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ltable.h,v 2.18 2013/08/30 16:01:37 roberto Exp $
2+** $Id: ltable.h,v 2.19 2014/07/29 16:22:24 roberto Exp $
33 ** Lua tables (hash)
44 ** See Copyright Notice in lua.h
55 */
@@ -11,10 +11,15 @@ · 5 unmodified lines
1111
1212
1313 #define gnode(t,i) (&(t)->node[i])
14−#define gkey(n) (&(n)->i_key.tvk)
1514 #define gval(n) (&(n)->i_val)
1615 #define gnext(n) ((n)->i_key.nk.next)
1716
17+
18+/* 'const' to avoid wrong writings that can mess up field 'next' */
19+#define gkey(n) cast(const TValue*, (&(n)->i_key.tvk))
20+
21+#define wgkey(n) (&(n)->i_key.nk)
22+
1823 #define invalidateTMcache(t) ((t)->flags = 0)
1924
2025
src/ltablib.c+137−46
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ltablib.c,v 1.70 2014/05/16 18:53:25 roberto Exp $
2+** $Id: ltablib.c,v 1.73 2014/07/29 16:01:00 roberto Exp $
33 ** Library for Table Manipulation
44 ** See Copyright Notice in lua.h
55 */
@@ -17,8 +17,56 @@ · 11 unmodified lines
1717 #include "lualib.h"
1818
1919
20−#define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), luaL_len(L, n))
2120
21+/*
22+** Structure with table-access functions
23+*/
24+typedef struct {
25+ int (*geti) (lua_State *L, int idx, lua_Integer n);
26+ void (*seti) (lua_State *L, int idx, lua_Integer n);
27+} TabA;
28+
29+
30+/*
31+** equivalent to 'lua_rawgeti', but not raw
32+*/
33+static int geti (lua_State *L, int idx, lua_Integer n) {
34+ lua_pushinteger(L, n);
35+ return lua_gettable(L, idx); /* assume 'idx' is not negative */
36+}
37+
38+
39+/*
40+** equivalent to 'lua_rawseti', but not raw
41+*/
42+static void seti (lua_State *L, int idx, lua_Integer n) {
43+ lua_pushinteger(L, n);
44+ lua_rotate(L, -2, 1); /* exchange key and value */
45+ lua_settable(L, idx); /* assume 'idx' is not negative */
46+}
47+
48+
49+/*
50+** Check that 'arg' has a table and set access functions in 'ta' to raw
51+** or non-raw according to the presence of corresponding metamethods.
52+*/
53+static void checktab (lua_State *L, int arg, TabA *ta) {
54+ luaL_checktype(L, arg, LUA_TTABLE);
55+ if (!lua_getmetatable(L, arg)) { /* fast track */
56+ ta->geti = lua_rawgeti; /* with no metatable, all is raw */
57+ ta->seti = lua_rawseti;
58+ }
59+ else {
60+ lua_pushliteral(L, "__index"); /* 'index' metamethod */
61+ ta->geti = (lua_rawget(L, -2) == LUA_TNIL) ? lua_rawgeti : geti;
62+ lua_pushliteral(L, "__newindex"); /* 'newindex' metamethod */
63+ ta->seti = (lua_rawget(L, -3) == LUA_TNIL) ? lua_rawseti : seti;
64+ lua_pop(L, 3); /* pop metatable plus both metamethods */
65+ }
66+}
67+
68+
69+#define aux_getn(L,n,ta) (checktab(L, n, ta), luaL_len(L, n))
2270
2371
2472 #if defined(LUA_COMPAT_MAXN)
@@ -40,7 +88,8 @@ static int maxn (lua_State *L) { · 15 unmodified lines
4088
4189
4290 static int tinsert (lua_State *L) {
43− lua_Integer e = aux_getn(L, 1) + 1; /* first empty element */
91+ TabA ta;
92+ lua_Integer e = aux_getn(L, 1, &ta) + 1; /* first empty element */
4493 lua_Integer pos; /* where to insert new element */
4594 switch (lua_gettop(L)) {
4695 case 2: { /* called with only 2 arguments */
@@ -52,8 +101,8 @@ static int tinsert (lua_State *L) { · 5 unmodified lines
52101 pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */
53102 luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds");
54103 for (i = e; i > pos; i--) { /* move up elements */
55− lua_rawgeti(L, 1, i-1);
56− lua_rawseti(L, 1, i); /* t[i] = t[i-1] */
104+ (*ta.geti)(L, 1, i - 1);
105+ (*ta.seti)(L, 1, i); /* t[i] = t[i - 1] */
57106 }
58107 break;
59108 }
@@ -61,29 +110,67 @@ static int tinsert (lua_State *L) { · 1 unmodified line
61110 return luaL_error(L, "wrong number of arguments to " LUA_QL("insert"));
62111 }
63112 }
64− lua_rawseti(L, 1, pos); /* t[pos] = v */
113+ (*ta.seti)(L, 1, pos); /* t[pos] = v */
65114 return 0;
66115 }
67116
68117
69118 static int tremove (lua_State *L) {
70− lua_Integer size = aux_getn(L, 1);
119+ TabA ta;
120+ lua_Integer size = aux_getn(L, 1, &ta);
71121 lua_Integer pos = luaL_optinteger(L, 2, size);
72122 if (pos != size) /* validate 'pos' if given */
73123 luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds");
74− lua_rawgeti(L, 1, pos); /* result = t[pos] */
124+ (*ta.geti)(L, 1, pos); /* result = t[pos] */
75125 for ( ; pos < size; pos++) {
76− lua_rawgeti(L, 1, pos+1);
77− lua_rawseti(L, 1, pos); /* t[pos] = t[pos+1] */
126+ (*ta.geti)(L, 1, pos + 1);
127+ (*ta.seti)(L, 1, pos); /* t[pos] = t[pos + 1] */
78128 }
79129 lua_pushnil(L);
80− lua_rawseti(L, 1, pos); /* t[pos] = nil */
130+ (*ta.seti)(L, 1, pos); /* t[pos] = nil */
131+ return 1;
132+}
133+
134+
135+static int tcopy (lua_State *L) {
136+ TabA ta;
137+ lua_Integer f = luaL_checkinteger(L, 2);
138+ lua_Integer e = luaL_checkinteger(L, 3);
139+ lua_Integer t;
140+ int tt = 4; /* destination table */
141+ /* the following restriction avoids several problems with overflows */
142+ luaL_argcheck(L, f > 0, 2, "initial position must be positive");
143+ if (lua_istable(L, tt))
144+ t = luaL_checkinteger(L, 5);
145+ else {
146+ tt = 1; /* destination table is equal to source */
147+ t = luaL_checkinteger(L, 4);
148+ }
149+ if (e >= f) { /* otherwise, nothing to move */
150+ lua_Integer n, i;
151+ ta.geti = (!luaL_getmetafield(L, 1, "__index")) ? lua_rawgeti : geti;
152+ ta.seti = (!luaL_getmetafield(L, tt, "__newindex")) ? lua_rawseti : seti;
153+ n = e - f + 1; /* number of elements to move */
154+ if (t > f) {
155+ for (i = n - 1; i >= 0; i--) {
156+ (*ta.geti)(L, 1, f + i);
157+ (*ta.seti)(L, tt, t + i);
158+ }
159+ }
160+ else {
161+ for (i = 0; i < n; i++) {
162+ (*ta.geti)(L, 1, f + i);
163+ (*ta.seti)(L, tt, t + i);
164+ }
165+ }
166+ }
167+ lua_pushvalue(L, tt); /* return "to table" */
81168 return 1;
82169 }
83170
84171
85−static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) {
86− lua_rawgeti(L, 1, i);
172+static void addfield (lua_State *L, luaL_Buffer *b, TabA *ta, lua_Integer i) {
173+ (*ta->geti)(L, 1, i);
87174 if (!lua_isstring(L, -1))
88175 luaL_error(L, "invalid value (%s) at index %d in table for "
89176 LUA_QL("concat"), luaL_typename(L, -1), i);
@@ -92,20 +179,21 @@ static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) { · 2 unmodified lines
92179
93180
94181 static int tconcat (lua_State *L) {
182+ TabA ta;
95183 luaL_Buffer b;
96184 size_t lsep;
97185 lua_Integer i, last;
98186 const char *sep = luaL_optlstring(L, 2, "", &lsep);
99− luaL_checktype(L, 1, LUA_TTABLE);
187+ checktab(L, 1, &ta);
100188 i = luaL_optinteger(L, 3, 1);
101189 last = luaL_opt(L, luaL_checkinteger, 4, luaL_len(L, 1));
102190 luaL_buffinit(L, &b);
103191 for (; i < last; i++) {
104− addfield(L, &b, i);
192+ addfield(L, &b, &ta, i);
105193 luaL_addlstring(&b, sep, lsep);
106194 }
107195 if (i == last) /* add last value (if interval was not empty) */
108− addfield(L, &b, i);
196+ addfield(L, &b, &ta, i);
109197 luaL_pushresult(&b);
110198 return 1;
111199 }
@@ -131,20 +219,21 @@ static int pack (lua_State *L) { · 19 unmodified lines
131219
132220
133221 static int unpack (lua_State *L) {
222+ TabA ta;
134223 lua_Integer i, e;
135224 lua_Unsigned n;
136− luaL_checktype(L, 1, LUA_TTABLE);
225+ checktab(L, 1, &ta);
137226 i = luaL_optinteger(L, 2, 1);
138227 e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1));
139228 if (i > e) return 0; /* empty range */
140229 n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */
141− if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, ++n))
230+ if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, (int)(++n)))
142231 return luaL_error(L, "too many results to unpack");
143232 do { /* must have at least one element */
144− lua_rawgeti(L, 1, i); /* push arg[i..e] */
233+ (*ta.geti)(L, 1, i); /* push arg[i..e] */
145234 } while (i++ < e);
146235
147− return n;
236+ return (int)n;
148237 }
149238
150239 /* }====================================================== */
@@ -160,9 +249,9 @@ static int unpack (lua_State *L) { · 9 unmodified lines
160249 */
161250
162251
163−static void set2 (lua_State *L, int i, int j) {
164− lua_rawseti(L, 1, i);
165− lua_rawseti(L, 1, j);
252+static void set2 (lua_State *L, TabA *ta, int i, int j) {
253+ (*ta->seti)(L, 1, i);
254+ (*ta->seti)(L, 1, j);
166255 }
167256
168257 static int sort_comp (lua_State *L, int a, int b) {
@@ -180,45 +269,45 @@ static int sort_comp (lua_State *L, int a, int b) { · 11 unmodified lines
180269 return lua_compare(L, a, b, LUA_OPLT);
181270 }
182271
183−static void auxsort (lua_State *L, int l, int u) {
272+static void auxsort (lua_State *L, TabA *ta, int l, int u) {
184273 while (l < u) { /* for tail recursion */
185274 int i, j;
186275 /* sort elements a[l], a[(l+u)/2] and a[u] */
187− lua_rawgeti(L, 1, l);
188− lua_rawgeti(L, 1, u);
276+ (*ta->geti)(L, 1, l);
277+ (*ta->geti)(L, 1, u);
189278 if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */
190− set2(L, l, u); /* swap a[l] - a[u] */
279+ set2(L, ta, l, u); /* swap a[l] - a[u] */
191280 else
192281 lua_pop(L, 2);
193282 if (u-l == 1) break; /* only 2 elements */
194283 i = (l+u)/2;
195− lua_rawgeti(L, 1, i);
196− lua_rawgeti(L, 1, l);
284+ (*ta->geti)(L, 1, i);
285+ (*ta->geti)(L, 1, l);
197286 if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */
198− set2(L, i, l);
287+ set2(L, ta, i, l);
199288 else {
200289 lua_pop(L, 1); /* remove a[l] */
201− lua_rawgeti(L, 1, u);
290+ (*ta->geti)(L, 1, u);
202291 if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */
203− set2(L, i, u);
292+ set2(L, ta, i, u);
204293 else
205294 lua_pop(L, 2);
206295 }
207296 if (u-l == 2) break; /* only 3 elements */
208− lua_rawgeti(L, 1, i); /* Pivot */
297+ (*ta->geti)(L, 1, i); /* Pivot */
209298 lua_pushvalue(L, -1);
210− lua_rawgeti(L, 1, u-1);
211− set2(L, i, u-1);
299+ (*ta->geti)(L, 1, u-1);
300+ set2(L, ta, i, u-1);
212301 /* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */
213302 i = l; j = u-1;
214303 for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
215304 /* repeat ++i until a[i] >= P */
216− while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) {
305+ while ((*ta->geti)(L, 1, ++i), sort_comp(L, -1, -2)) {
217306 if (i>=u) luaL_error(L, "invalid order function for sorting");
218307 lua_pop(L, 1); /* remove a[i] */
219308 }
220309 /* repeat --j until a[j] <= P */
221− while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) {
310+ while ((*ta->geti)(L, 1, --j), sort_comp(L, -3, -1)) {
222311 if (j<=l) luaL_error(L, "invalid order function for sorting");
223312 lua_pop(L, 1); /* remove a[j] */
224313 }
@@ -226,11 +315,11 @@ static void auxsort (lua_State *L, int l, int u) { · 1 unmodified line
226315 lua_pop(L, 3); /* pop pivot, a[i], a[j] */
227316 break;
228317 }
229− set2(L, i, j);
318+ set2(L, ta, i, j);
230319 }
231− lua_rawgeti(L, 1, u-1);
232− lua_rawgeti(L, 1, i);
233− set2(L, u-1, i); /* swap pivot (a[u-1]) with a[i] */
320+ (*ta->geti)(L, 1, u-1);
321+ (*ta->geti)(L, 1, i);
322+ set2(L, ta, u-1, i); /* swap pivot (a[u-1]) with a[i] */
234323 /* a[l..i-1] <= a[i] == P <= a[i+1..u] */
235324 /* adjust so that smaller half is in [j..i] and larger one in [l..u] */
236325 if (i-l < u-i) {
@@ -239,17 +328,18 @@ static void auxsort (lua_State *L, int l, int u) { · 2 unmodified lines
239328 else {
240329 j=i+1; i=u; u=j-2;
241330 }
242− auxsort(L, j, i); /* call recursively the smaller one */
331+ auxsort(L, ta, j, i); /* call recursively the smaller one */
243332 } /* repeat the routine for the larger one */
244333 }
245334
246335 static int sort (lua_State *L) {
247− int n = aux_getn(L, 1);
248− luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */
336+ TabA ta;
337+ int n = (int)aux_getn(L, 1, &ta);
338+ luaL_checkstack(L, 50, ""); /* assume array is smaller than 2^50 */
249339 if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */
250340 luaL_checktype(L, 2, LUA_TFUNCTION);
251− lua_settop(L, 2); /* make sure there is two arguments */
252− auxsort(L, 1, n);
341+ lua_settop(L, 2); /* make sure there are two arguments */
342+ auxsort(L, &ta, 1, n);
253343 return 0;
254344 }
255345
@@ -265,6 +355,7 @@ static const luaL_Reg tab_funcs[] = { · 9 unmodified lines
265355 {"pack", pack},
266356 {"unpack", unpack},
267357 {"remove", tremove},
358+ {"copy", tcopy},
268359 {"sort", sort},
269360 {NULL, NULL}
270361 };
src/ltm.c+1−1
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ltm.c,v 2.27 2014/06/10 18:53:18 roberto Exp $
2+** $Id: ltm.c,v 2.29 2014/07/18 13:36:14 roberto Exp $
33 ** Tag methods
44 ** See Copyright Notice in lua.h
55 */
src/lua.c+18−12
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lua.c,v 1.211 2014/06/05 20:42:06 roberto Exp $
2+** $Id: lua.c,v 1.213 2014/06/30 19:48:08 roberto Exp $
33 ** Lua stand-alone interpreter
44 ** See Copyright Notice in lua.h
55 */
@@ -115,7 +115,7 @@ static void lstop (lua_State *L, lua_Debug *ar) { · 109 unmodified lines
115115
116116 /*
117117 ** 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),
118+** just change a Lua state (as there is no proper synchronization),
119119 ** this function only sets a hook that, when called, will stop the
120120 ** interpreter.
121121 */
@@ -284,7 +284,7 @@ static const char *get_prompt (lua_State *L, int firstline) { · 162 unmodified lines
284284 /*
285285 ** Check whether 'status' signals a syntax error and the error
286286 ** message at the top of the stack ends with the above mark for
287−** incoplete statements.
287+** incomplete statements.
288288 */
289289 static int incomplete (lua_State *L, int status) {
290290 if (status == LUA_ERRSYNTAX) {
@@ -417,25 +417,31 @@ static void doREPL (lua_State *L) { · 126 unmodified lines
417417
418418
419419 /*
420−** Push on the stack 'n' strings from 'argv'
420+** Push on the stack the contents of table 'arg' from 1 to #arg
421421 */
422−static void pushargs (lua_State *L, char **argv, int n) {
423− int i;
422+static int pushargs (lua_State *L) {
423+ int i, n;
424+ lua_getglobal(L, "arg");
425+ if (!lua_istable(L, -1))
426+ luaL_error(L, "'arg' is not a table");
427+ n = (int)luaL_len(L, -1);
424428 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]);
429+ for (i = 1; i <= n; i++)
430+ lua_rawgeti(L, -i, i);
431+ lua_remove(L, -i); /* remove table from the stack */
432+ return n;
427433 }
428434
429435
430−static int handle_script (lua_State *L, char **argv, int n) {
436+static int handle_script (lua_State *L, char **argv) {
431437 int status;
432438 const char *fname = argv[0];
433439 if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0)
434440 fname = NULL; /* stdin */
435441 status = luaL_loadfile(L, fname);
436442 if (status == LUA_OK) {
437− pushargs(L, argv, n); /* push arguments to script */
438− status = docall(L, n - 1, LUA_MULTRET);
443+ int n = pushargs(L); /* push arguments to script */
444+ status = docall(L, n, LUA_MULTRET);
439445 }
440446 return report(L, status);
441447 }
@@ -570,7 +576,7 @@ static int pmain (lua_State *L) { · 128 unmodified lines
570576 if (!runargs(L, argv, script)) /* execute arguments -e and -l */
571577 return 0; /* something failed */
572578 if (script < argc && /* execute main script (if there is one) */
573− handle_script(L, argv + script, argc - script) != LUA_OK)
579+ handle_script(L, argv + script) != LUA_OK)
574580 return 0;
575581 if (args & has_i) /* -i option? */
576582 doREPL(L); /* do read-eval-print loop */
src/lua.h+54−34
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lua.h,v 1.307 2014/06/10 17:41:38 roberto Exp $
2+** $Id: lua.h,v 1.312 2014/07/31 13:44:30 roberto Exp $
33 ** Lua - A Scripting Language
44 ** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
55 ** See Copyright Notice at the end of this file
@@ -19,7 +19,7 @@ · 13 unmodified lines
1919 #define LUA_VERSION_MAJOR "5"
2020 #define LUA_VERSION_MINOR "3"
2121 #define LUA_VERSION_NUM 503
22−#define LUA_VERSION_RELEASE "0 (work3)"
22+#define LUA_VERSION_RELEASE "0 (alpha)"
2323
2424 #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
2525 #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE
@@ -53,30 +53,6 @@ · 27 unmodified lines
5353
5454 typedef struct lua_State lua_State;
5555
56−/*
57−** Type for C functions registered with Lua
58−*/
59−typedef int (*lua_CFunction) (lua_State *L);
60−
61−/*
62−** Type for continuation functions
63−*/
64−typedef int (*lua_KFunction) (lua_State *L, int status, int ctx);
65−
66−
67−/*
68−** functions that read/write blocks when loading/dumping Lua chunks
69−*/
70−typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz);
71−
72−typedef int (*lua_Writer) (lua_State *L, const void *p, size_t sz, void *ud);
73−
74−
75−/*
76−** prototype for memory-allocation functions
77−*/
78−typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize);
79−
8056
8157 /*
8258 ** basic types
@@ -117,6 +93,34 @@ typedef LUA_INTEGER lua_Integer; · 34 unmodified lines
11793 /* unsigned integer type */
11894 typedef LUA_UNSIGNED lua_Unsigned;
11995
96+/* type for continuation-function contexts */
97+typedef LUA_CTXT lua_Ctx;
98+
99+
100+/*
101+** Type for C functions registered with Lua
102+*/
103+typedef int (*lua_CFunction) (lua_State *L);
104+
105+/*
106+** Type for continuation functions
107+*/
108+typedef int (*lua_KFunction) (lua_State *L, int status, lua_Ctx ctx);
109+
110+
111+/*
112+** Type for functions that read/write blocks when loading/dumping Lua chunks
113+*/
114+typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz);
115+
116+typedef int (*lua_Writer) (lua_State *L, const void *p, size_t sz, void *ud);
117+
118+
119+/*
120+** Type for memory-allocation functions
121+*/
122+typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize);
123+
120124
121125
122126 /*
@@ -154,7 +158,6 @@ LUA_API int (lua_gettop) (lua_State *L); · 31 unmodified lines
154158 LUA_API void (lua_settop) (lua_State *L, int idx);
155159 LUA_API void (lua_pushvalue) (lua_State *L, int idx);
156160 LUA_API void (lua_rotate) (lua_State *L, int idx, int n);
157−LUA_API void (lua_replace) (lua_State *L, int idx);
158161 LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx);
159162 LUA_API int (lua_checkstack) (lua_State *L, int sz);
160163
@@ -175,7 +178,6 @@ LUA_API const char *(lua_typename) (lua_State *L, int tp); · 14 unmodified lines
175178
176179 LUA_API lua_Number (lua_tonumberx) (lua_State *L, int idx, int *isnum);
177180 LUA_API lua_Integer (lua_tointegerx) (lua_State *L, int idx, int *isnum);
178−LUA_API lua_Unsigned (lua_tounsignedx) (lua_State *L, int idx, int *isnum);
179181 LUA_API int (lua_toboolean) (lua_State *L, int idx);
180182 LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len);
181183 LUA_API size_t (lua_rawlen) (lua_State *L, int idx);
@@ -220,7 +222,6 @@ LUA_API int (lua_compare) (lua_State *L, int idx1, int idx2, int op); · 38 unmodified lines
220222 LUA_API void (lua_pushnil) (lua_State *L);
221223 LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n);
222224 LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n);
223−LUA_API void (lua_pushunsigned) (lua_State *L, lua_Unsigned n);
224225 LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t l);
225226 LUA_API const char *(lua_pushstring) (lua_State *L, const char *s);
226227 LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt,
@@ -264,12 +265,12 @@ LUA_API void (lua_setuservalue) (lua_State *L, int idx); · 37 unmodified lines
264265 /*
265266 ** 'load' and 'call' functions (load and run Lua code)
266267 */
267−LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults, int ctx,
268+LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults, lua_Ctx ctx,
268269 lua_KFunction k);
269270 #define lua_call(L,n,r) lua_callk(L, (n), (r), 0, NULL)
270271
271272 LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
272− int ctx, lua_KFunction k);
273+ lua_Ctx ctx, lua_KFunction k);
273274 #define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL)
274275
275276 LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt,
@@ -282,7 +283,7 @@ LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip); · 6 unmodified lines
282283 /*
283284 ** coroutine functions
284285 */
285−LUA_API int (lua_yieldk) (lua_State *L, int nresults, int ctx,
286+LUA_API int (lua_yieldk) (lua_State *L, int nresults, lua_Ctx ctx,
286287 lua_KFunction k);
287288 #define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL)
288289 LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg);
@@ -326,14 +327,15 @@ LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); · 37 unmodified lines
326327
327328
328329 /*
329−** ===============================================================
330+** {==============================================================
330331 ** some useful macros
331332 ** ===============================================================
332333 */
333334
335+#define lua_getextraspace(L) ((void *)((char *)(L) - LUA_EXTRASPACE))
336+
334337 #define lua_tonumber(L,i) lua_tonumberx(L,(i),NULL)
335338 #define lua_tointeger(L,i) lua_tointegerx(L,(i),NULL)
336−#define lua_tounsigned(L,i) lua_tounsignedx(L,(i),NULL)
337339
338340 #define lua_pop(L,n) lua_settop(L, -(n)-1)
339341
@@ -365,6 +367,24 @@ LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); · 25 unmodified lines
365367
366368 #define lua_remove(L,idx) (lua_rotate(L, (idx), -1), lua_pop(L, 1))
367369
370+#define lua_replace(L,idx) (lua_copy(L, -1, (idx)), lua_pop(L, 1))
371+
372+/* }============================================================== */
373+
374+
375+/*
376+** {==============================================================
377+** compatibility macros for unsigned conversions
378+** ===============================================================
379+*/
380+#if defined(LUA_COMPAT_APIUNSIGNED)
381+
382+#define lua_pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n))
383+#define lua_tounsignedx(L,i,is) ((lua_Integer)lua_tointegerx(L,i,is))
384+#define lua_tounsigned(L,i) lua_tounsignedx(L,(i),NULL)
385+
386+#endif
387+/* }============================================================== */
368388
369389 /*
370390 ** {======================================================================
src/luac.c+3−3
@@ -203,7 +203,7 @@ int main(int argc, char* argv[]) · 202 unmodified lines
203203 }
204204
205205 /*
206−** $Id: print.c,v 1.73 2014/06/12 02:41:25 lhf Exp $
206+** $Id: print.c,v 1.74 2014/07/21 01:41:45 lhf Exp $
207207 ** print bytecodes
208208 ** See Copyright Notice in lua.h
209209 */
@@ -223,7 +223,7 @@ int main(int argc, char* argv[]) · 13 unmodified lines
223223 static void PrintString(const TString* ts)
224224 {
225225 const char* s=getstr(ts);
226− size_t i,n=ts->tsv.len;
226+ size_t i,n=ts->len;
227227 printf("%c",'"');
228228 for (i=0; i<n; i++)
229229 {
@@ -266,7 +266,7 @@ static void PrintConstant(const Proto* f, int i) · 36 unmodified lines
266266 printf(LUA_INTEGER_FMT,ivalue(o));
267267 break;
268268 case LUA_TSHRSTR: case LUA_TLNGSTR:
269− PrintString(rawtsvalue(o));
269+ PrintString(tsvalue(o));
270270 break;
271271 default: /* cannot happen */
272272 printf("? type=%d",ttype(o));
src/luaconf.h+36−7
@@ -1,5 +1,5 @@
11 /*
2−** $Id: luaconf.h,v 1.207 2014/06/10 19:21:20 roberto Exp $
2+** $Id: luaconf.h,v 1.212 2014/07/24 19:33:29 roberto Exp $
33 ** Configuration file for Lua
44 ** See Copyright Notice in lua.h
55 */
@@ -210,6 +210,14 @@ · 204 unmodified lines
210210
211211
212212
213+/*
214+@@ LUA_EXTRASPACE defines the size of a raw memory area associated with
215+** a Lua state with very fast access.
216+** CHANGE it if you need a different size.
217+*/
218+#define LUA_EXTRASPACE (sizeof(void *))
219+
220+
213221 /*
214222 @@ LUA_QL describes how error messages quote program elements.
215223 ** CHANGE it if you want a different appearance.
@@ -254,6 +262,22 @@ · 38 unmodified lines
254262 #define LUAI_MAXSHORTLEN 40
255263
256264
265+/*
266+@@ LUA_CTXT is the type of the context ('ctx') for continuation functions.
267+@@ It must be a numerical type; Lua will use 'intptr_t' if available.
268+*/
269+#if defined (LUA_USE_C99)
270+#include <stdint.h>
271+#if defined (INTPTR_MAX) /* even in C99 this type is optional */
272+#define LUA_CTXT intptr_t
273+#endif
274+#endif
275+
276+#if !defined(LUA_CTXT)
277+/* default definition (the nearest thing to 'intptr_t' in C89) */
278+#define LUA_CTXT ptrdiff_t
279+#endif
280+
257281
258282 /*
259283 ** {==================================================================
@@ -280,6 +304,17 @@ · 20 unmodified lines
280304 */
281305 #define LUA_COMPAT_BITLIB
282306
307+/*
308+@@ LUA_COMPAT_IPAIRS controls the effectivness of the __ipairs metamethod.
309+*/
310+#define LUA_COMPAT_IPAIRS
311+
312+/*
313+@@ LUA_COMPAT_APIUNSIGNED controls the presence of macros for
314+** manipulating unsigned integers (lua_pushunsigned, lua_tounsigned, etc.)
315+*/
316+#define LUA_COMPAT_APIUNSIGNED
317+
283318
284319 /*
285320 @@ LUA_COMPAT_FLOATSTRING makes Lua format integral floats without a
@@ -569,7 +604,6 @@ · 283 unmodified lines
569604 @@ LUA_INTEGER_FMT is the format for writing integers.
570605 @@ LUA_MAXINTEGER is the maximum value for a LUA_INTEGER.
571606 @@ LUA_MININTEGER is the minimum value for a LUA_INTEGER.
572−@@ LUA_MAXUNSIGNED is the maximum value for a LUA_UNSIGNED.
573607 @@ lua_integer2str converts an integer to a string.
574608 */
575609
@@ -595,7 +629,6 @@ · 19 unmodified lines
595629 #define LUA_INTEGER int
596630 #define LUA_INTEGER_FRMLEN ""
597631
598−#define LUA_MAXUNSIGNED UINT_MAX
599632 #define LUA_MAXINTEGER INT_MAX
600633 #define LUA_MININTEGER INT_MIN
601634
@@ -604,7 +637,6 @@ · 2 unmodified lines
604637 #define LUA_INTEGER long
605638 #define LUA_INTEGER_FRMLEN "l"
606639
607−#define LUA_MAXUNSIGNED ULONG_MAX
608640 #define LUA_MAXINTEGER LONG_MAX
609641 #define LUA_MININTEGER LONG_MIN
610642
@@ -615,7 +647,6 @@ · 4 unmodified lines
615647 #define LUA_INTEGER __int64
616648 #define LUA_INTEGER_FRMLEN "I64"
617649
618−#define LUA_MAXUNSIGNED _UI64_MAX
619650 #define LUA_MAXINTEGER _I64_MAX
620651 #define LUA_MININTEGER _I64_MIN
621652
@@ -624,7 +655,6 @@ · 2 unmodified lines
624655 #define LUA_INTEGER long long
625656 #define LUA_INTEGER_FRMLEN "ll"
626657
627−#define LUA_MAXUNSIGNED ULLONG_MAX
628658 #define LUA_MAXINTEGER LLONG_MAX
629659 #define LUA_MININTEGER LLONG_MIN
630660
@@ -639,7 +669,6 @@ · 8 unmodified lines
639669 #define LUA_INTEGER short int
640670 #define LUA_INTEGER_FRMLEN ""
641671
642−#define LUA_MAXUNSIGNED ((LUA_UNSIGNED)USHRT_MAX)
643672 #define LUA_MAXINTEGER SHRT_MAX
644673 #define LUA_MININTEGER SHRT_MIN
645674
src/lutf8lib.c+3−1
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lutf8lib.c,v 1.9 2014/05/14 18:33:37 roberto Exp $
2+** $Id: lutf8lib.c,v 1.10 2014/07/16 13:56:14 roberto Exp $
33 ** Standard library for UTF-8 manipulation
44 ** See Copyright Notice in lua.h
55 */
@@ -238,6 +238,8 @@ static struct luaL_Reg funcs[] = { · 232 unmodified lines
238238 {"char", utfchar},
239239 {"len", utflen},
240240 {"codes", iter_codes},
241+ /* placeholders */
242+ {"charpatt", NULL},
241243 {NULL, NULL}
242244 };
243245
src/lvm.c+28−49
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lvm.c,v 2.216 2014/06/19 18:27:20 roberto Exp $
2+** $Id: lvm.c,v 2.222 2014/07/30 14:42:44 roberto Exp $
33 ** Lua virtual machine
44 ** See Copyright Notice in lua.h
55 */
@@ -33,10 +33,6 @@ · 27 unmodified lines
3333 #define MAXTAGLOOP 2000
3434
3535
36−/* maximum length of the conversion of a number to a string */
37−#define MAXNUMBER2STR 50
38−
39−
4036 /*
4137 ** Similar to 'tonumber', but does not attempt to convert strings and
4238 ** ensure correct precision (no extra bits). Used in comparisons.
@@ -70,7 +66,7 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) { · 27 unmodified lines
7066 *n = fltvalue(obj);
7167 return 1;
7268 }
73− else if (ttisstring(obj) && /* string convertible to number? */
69+ else if (cvt2num(obj) && /* string convertible to number? */
7470 luaO_str2num(svalue(obj), &v) == tsvalue(obj)->len + 1) {
7571 obj = &v;
7672 goto again; /* convert result from 'luaO_str2num' to a float */
@@ -80,21 +76,29 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) { · 3 unmodified lines
8076
8177
8278 /*
83−** try to convert a value to an integer, rounding up if 'up' is true
79+** try to convert a value to an integer, rounding according to 'mode':
80+** mode == 0: accepts only integral values
81+** mode < 0: takes the floor of the number
82+** mode > 0: takes the ceil of the number
8483 */
85−static int tointeger_aux (const TValue *obj, lua_Integer *p, int up) {
84+static int tointeger_aux (const TValue *obj, lua_Integer *p, int mode) {
8685 TValue v;
8786 again:
8887 if (ttisfloat(obj)) {
8988 lua_Number n = fltvalue(obj);
90− n = (up ? -l_floor(-n) : l_floor(n));
91− return lua_numtointeger(n, p);
89+ lua_Number f = l_floor(n);
90+ if (n != f) { /* not an integral value? */
91+ if (mode == 0) return 0; /* fails if mode demands integral value */
92+ else if (mode > 0) /* needs ceil? */
93+ f += 1; /* convert floor to ceil (remember: n != f) */
94+ }
95+ return lua_numtointeger(f, p);
9296 }
9397 else if (ttisinteger(obj)) {
9498 *p = ivalue(obj);
9599 return 1;
96100 }
97− else if (ttisstring(obj) &&
101+ else if (cvt2num(obj) &&
98102 luaO_str2num(svalue(obj), &v) == tsvalue(obj)->len + 1) {
99103 obj = &v;
100104 goto again; /* convert result from 'luaO_str2num' to an integer */
@@ -104,39 +108,13 @@ static int tointeger_aux (const TValue *obj, lua_Integer *p, int up) { · 3 unmodified lines
104108
105109
106110 /*
107−** try to convert a non-integer value to an integer, rounding down
111+** try to convert a value to an integer
108112 */
109113 int luaV_tointeger_ (const TValue *obj, lua_Integer *p) {
110114 return tointeger_aux(obj, p, 0);
111115 }
112116
113117
114−/*
115−** Convert a number object to a string
116−*/
117−int luaV_tostring (lua_State *L, StkId obj) {
118− if (!ttisnumber(obj))
119− return 0;
120− else {
121− char buff[MAXNUMBER2STR];
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− }
134− setsvalue2s(L, obj, luaS_newlstr(L, buff, len));
135− return 1;
136− }
137−}
138−
139−
140118 /*
141119 ** Try to convert a 'for' limit to an integer, preserving the
142120 ** semantics of the loop.
@@ -155,7 +133,7 @@ int luaV_tostring (lua_State *L, StkId obj) { · 12 unmodified lines
155133 static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step,
156134 int *stopnow) {
157135 *stopnow = 0; /* usually, let loops run */
158− if (!tointeger_aux(obj, p, (step < 0))) { /* does not fit in integer? */
136+ if (!tointeger_aux(obj, p, (step < 0 ? 1 : -1))) { /* not fit in integer? */
159137 lua_Number n; /* try to convert to float */
160138 if (!tonumber(obj, &n)) /* cannot convert to float? */
161139 return 0; /* not a number */
@@ -239,7 +217,7 @@ void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) { · 77 unmodified lines
239217 luaT_callTM(L, tm, t, key, val, 0);
240218 return;
241219 }
242− t = tm; /* else repeat assginment over 'tm' */
220+ t = tm; /* else repeat assignment over 'tm' */
243221 }
244222 luaG_runerror(L, "settable chain too long; possible loop");
245223 }
@@ -254,9 +232,9 @@ void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) { · 8 unmodified lines
254232 */
255233 static int l_strcmp (const TString *ls, const TString *rs) {
256234 const char *l = getstr(ls);
257− size_t ll = ls->tsv.len;
235+ size_t ll = ls->len;
258236 const char *r = getstr(rs);
259− size_t lr = rs->tsv.len;
237+ size_t lr = rs->len;
260238 for (;;) { /* for each segment */
261239 int temp = strcoll(l, r);
262240 if (temp != 0) /* not equal? */
@@ -286,7 +264,7 @@ int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { · 23 unmodified lines
286264 else if (tofloat(l, &nl) && tofloat(r, &nr)) /* both are numbers? */
287265 return luai_numlt(nl, nr);
288266 else if (ttisstring(l) && ttisstring(r)) /* both are strings? */
289− return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
267+ return l_strcmp(tsvalue(l), tsvalue(r)) < 0;
290268 else if ((res = luaT_callorderTM(L, l, r, TM_LT)) < 0) /* no metamethod? */
291269 luaG_ordererror(L, l, r); /* error */
292270 return res;
@@ -304,7 +282,7 @@ int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) { · 11 unmodified lines
304282 else if (tofloat(l, &nl) && tofloat(r, &nr)) /* both are numbers? */
305283 return luai_numle(nl, nr);
306284 else if (ttisstring(l) && ttisstring(r)) /* both are strings? */
307− return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
285+ return l_strcmp(tsvalue(l), tsvalue(r)) <= 0;
308286 else if ((res = luaT_callorderTM(L, l, r, TM_LE)) >= 0) /* first try `le' */
309287 return res;
310288 else if ((res = luaT_callorderTM(L, r, l, TM_LT)) < 0) /* else try `lt' */
@@ -337,8 +315,8 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { · 26 unmodified lines
337315 case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */
338316 case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
339317 case LUA_TLCF: return fvalue(t1) == fvalue(t2);
340− case LUA_TSHRSTR: return eqshrstr(rawtsvalue(t1), rawtsvalue(t2));
341− case LUA_TLNGSTR: return luaS_eqlngstr(rawtsvalue(t1), rawtsvalue(t2));
318+ case LUA_TSHRSTR: return eqshrstr(tsvalue(t1), tsvalue(t2));
319+ case LUA_TLNGSTR: return luaS_eqlngstr(tsvalue(t1), tsvalue(t2));
342320 case LUA_TUSERDATA: {
343321 if (uvalue(t1) == uvalue(t2)) return 1;
344322 else if (L == NULL) return 0;
@@ -366,7 +344,8 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { · 21 unmodified lines
366344
367345
368346 /* 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)))
347+#define tostring(L,o) \
348+ (ttisstring(o) || (cvt2str(o) && (luaO_tostring(L, o), 1)))
370349
371350 /*
372351 ** Main operation for concatenation: concat 'total' values in the stack,
@@ -377,7 +356,7 @@ void luaV_concat (lua_State *L, int total) { · 4 unmodified lines
377356 do {
378357 StkId top = L->top;
379358 int n = 2; /* number of elements handled in this pass (at least 2) */
380− if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1))
359+ if (!(ttisstring(top-2) || cvt2str(top-2)) || !tostring(L, top-1))
381360 luaT_trybinTM(L, top-2, top-1, top-2, TM_CONCAT);
382361 else if (tsvalue(top-1)->len == 0) /* second operand is empty? */
383362 cast_void(tostring(L, top - 2)); /* result is first operand */
@@ -541,7 +520,7 @@ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, · 157 unmodified lines
541520 ncl->upvals[i]->refcount++;
542521 /* new closure is white, so we do not need a barrier here */
543522 }
544− if (!isblack(obj2gco(p))) /* cache will not break GC invariant? */
523+ if (!isblack(p)) /* cache will not break GC invariant? */
545524 p->cache = ncl; /* save it on cache for reuse */
546525 }
547526
src/lvm.h+15−2
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lvm.h,v 2.31 2014/05/26 17:10:22 roberto Exp $
2+** $Id: lvm.h,v 2.33 2014/07/30 14:42:44 roberto Exp $
33 ** Lua virtual machine
44 ** See Copyright Notice in lua.h
55 */
@@ -13,6 +13,20 @@ · 7 unmodified lines
1313 #include "ltm.h"
1414
1515
16+#if !defined(LUA_NOCVTN2S)
17+#define cvt2str(o) ttisnumber(o)
18+#else
19+#define cvt2str(o) 0 /* no convertion from numbers to strings */
20+#endif
21+
22+
23+#if !defined(LUA_NOCVTS2N)
24+#define cvt2num(o) ttisstring(o)
25+#else
26+#define cvt2num(o) 0 /* no convertion from strings to numbers */
27+#endif
28+
29+
1630 #define tonumber(o,n) \
1731 (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n))
1832
@@ -29,7 +43,6 @@ LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); · 10 unmodified lines
2943 LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r);
3044 LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n);
3145 LUAI_FUNC int luaV_tointeger_ (const TValue *obj, lua_Integer *p);
32−LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj);
3346 LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key,
3447 StkId val);
3548 LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key,

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t